blob: 31c0744cb3a6a2c917ca80309d67e5152677e585 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +02002 File autogenerated by gengetopt version 2.22.6
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +02004 gengetopt -i cmdline.ggo --conf-parser
jjako52c24142002-12-16 13:33:51 +00005
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9*/
10
jjako52c24142002-12-16 13:33:51 +000011/* If we use autoconf. */
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
jjako52c24142002-12-16 13:33:51 +000015
Harald Welte1b3e5772010-05-04 11:13:56 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010020#ifndef FIX_UNUSED
21#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22#endif
23
24#include <getopt.h>
jjako52c24142002-12-16 13:33:51 +000025
26#include "cmdline.h"
27
Harald Welte1b3e5772010-05-04 11:13:56 +020028const char *gengetopt_args_info_purpose = "";
jjako52c24142002-12-16 13:33:51 +000029
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010030const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]...";
31
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +020032const char *gengetopt_args_info_versiontext = "";
33
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010034const char *gengetopt_args_info_description = "";
jjako52c24142002-12-16 13:33:51 +000035
Harald Welte1b3e5772010-05-04 11:13:56 +020036const char *gengetopt_args_info_help[] = {
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010037 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -f, --fg Run in foreground (default=off)",
40 " -d, --debug Run in debug mode (default=off)",
41 " -c, --conf=STRING Read configuration file (default=`/etc/ggsn.conf')",
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +020042 " --pidfile=STRING Filename of process id file\n (default=`/var/run/ggsn.pid')",
43 " --statedir=STRING Directory of nonvolatile data\n (default=`/var/lib/ggsn/')",
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010044 " -l, --listen=STRING Local interface",
45 " -n, --net=STRING Network (default=`192.168.0.0/24')",
46 " --ipup=STRING Script to run after link-up",
47 " --ipdown=STRING Script to run after link-down",
48 " --dynip=STRING Dynamic IP address pool",
49 " --statip=STRING Static IP address pool",
50 " --pcodns1=STRING PCO DNS Server 1 (default=`0.0.0.0')",
51 " --pcodns2=STRING PCO DNS Server 2 (default=`0.0.0.0')",
52 " --timelimit=INT Exit after timelimit seconds (default=`0')",
53 " -a, --apn=STRING Access point name (default=`internet')",
54 " -q, --qos=INT Requested quality of service (default=`0x0b921f')",
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010055 " --logfile=STRING Logfile for errors",
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +010056 " --loglevel=STRING Global log ldevel (default=`error')",
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +020057 " -g, --gtp-linux GTP linux kernel support (default=off)",
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010058 0
Harald Welte1b3e5772010-05-04 11:13:56 +020059};
60
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010061typedef enum {ARG_NO
62 , ARG_FLAG
63 , ARG_STRING
64 , ARG_INT
65} cmdline_parser_arg_type;
66
Harald Welte1b3e5772010-05-04 11:13:56 +020067static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010068void clear_given (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020069static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010070void clear_args (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020071
72static int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010073cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
74 struct cmdline_parser_params *params, const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020075
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010076struct line_list
77{
78 char * string_arg;
79 struct line_list * next;
Harald Welte1b3e5772010-05-04 11:13:56 +020080};
81
82static struct line_list *cmd_line_list = 0;
83static struct line_list *cmd_line_list_tmp = 0;
84
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010085static void
86free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +020087{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010088 /* free the list of a previous call */
89 if (cmd_line_list)
90 {
91 while (cmd_line_list) {
92 cmd_line_list_tmp = cmd_line_list;
93 cmd_line_list = cmd_line_list->next;
94 free (cmd_line_list_tmp->string_arg);
95 free (cmd_line_list_tmp);
96 }
97 }
jjako52c24142002-12-16 13:33:51 +000098}
99
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100100
101static char *
102gengetopt_strdup (const char *s);
jjako52c24142002-12-16 13:33:51 +0000103
Harald Welte1b3e5772010-05-04 11:13:56 +0200104static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100105void clear_given (struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000106{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100107 args_info->help_given = 0 ;
108 args_info->version_given = 0 ;
109 args_info->fg_given = 0 ;
110 args_info->debug_given = 0 ;
111 args_info->conf_given = 0 ;
112 args_info->pidfile_given = 0 ;
113 args_info->statedir_given = 0 ;
114 args_info->listen_given = 0 ;
115 args_info->net_given = 0 ;
116 args_info->ipup_given = 0 ;
117 args_info->ipdown_given = 0 ;
118 args_info->dynip_given = 0 ;
119 args_info->statip_given = 0 ;
120 args_info->pcodns1_given = 0 ;
121 args_info->pcodns2_given = 0 ;
122 args_info->timelimit_given = 0 ;
123 args_info->apn_given = 0 ;
124 args_info->qos_given = 0 ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100125 args_info->logfile_given = 0 ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100126 args_info->loglevel_given = 0 ;
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200127 args_info->gtp_linux_given = 0 ;
jjako52c24142002-12-16 13:33:51 +0000128}
129
Harald Welte1b3e5772010-05-04 11:13:56 +0200130static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100131void clear_args (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200132{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100133 FIX_UNUSED (args_info);
134 args_info->fg_flag = 0;
135 args_info->debug_flag = 0;
136 args_info->conf_arg = gengetopt_strdup ("/etc/ggsn.conf");
137 args_info->conf_orig = NULL;
138 args_info->pidfile_arg = gengetopt_strdup ("/var/run/ggsn.pid");
139 args_info->pidfile_orig = NULL;
140 args_info->statedir_arg = gengetopt_strdup ("/var/lib/ggsn/");
141 args_info->statedir_orig = NULL;
142 args_info->listen_arg = NULL;
143 args_info->listen_orig = NULL;
144 args_info->net_arg = gengetopt_strdup ("192.168.0.0/24");
145 args_info->net_orig = NULL;
146 args_info->ipup_arg = NULL;
147 args_info->ipup_orig = NULL;
148 args_info->ipdown_arg = NULL;
149 args_info->ipdown_orig = NULL;
150 args_info->dynip_arg = NULL;
151 args_info->dynip_orig = NULL;
152 args_info->statip_arg = NULL;
153 args_info->statip_orig = NULL;
154 args_info->pcodns1_arg = gengetopt_strdup ("0.0.0.0");
155 args_info->pcodns1_orig = NULL;
156 args_info->pcodns2_arg = gengetopt_strdup ("0.0.0.0");
157 args_info->pcodns2_orig = NULL;
158 args_info->timelimit_arg = 0;
159 args_info->timelimit_orig = NULL;
160 args_info->apn_arg = gengetopt_strdup ("internet");
161 args_info->apn_orig = NULL;
162 args_info->qos_arg = 0x0b921f;
163 args_info->qos_orig = NULL;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100164 args_info->logfile_arg = NULL;
165 args_info->logfile_orig = NULL;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100166 args_info->loglevel_arg = gengetopt_strdup ("error");
167 args_info->loglevel_orig = NULL;
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200168 args_info->gtp_linux_flag = 0;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100169
Harald Welte1b3e5772010-05-04 11:13:56 +0200170}
171
172static
173void init_args_info(struct gengetopt_args_info *args_info)
174{
Harald Weltebed35df2011-11-02 13:06:18 +0100175
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100176
177 args_info->help_help = gengetopt_args_info_help[0] ;
178 args_info->version_help = gengetopt_args_info_help[1] ;
179 args_info->fg_help = gengetopt_args_info_help[2] ;
180 args_info->debug_help = gengetopt_args_info_help[3] ;
181 args_info->conf_help = gengetopt_args_info_help[4] ;
182 args_info->pidfile_help = gengetopt_args_info_help[5] ;
183 args_info->statedir_help = gengetopt_args_info_help[6] ;
184 args_info->listen_help = gengetopt_args_info_help[7] ;
185 args_info->net_help = gengetopt_args_info_help[8] ;
186 args_info->ipup_help = gengetopt_args_info_help[9] ;
187 args_info->ipdown_help = gengetopt_args_info_help[10] ;
188 args_info->dynip_help = gengetopt_args_info_help[11] ;
189 args_info->statip_help = gengetopt_args_info_help[12] ;
190 args_info->pcodns1_help = gengetopt_args_info_help[13] ;
191 args_info->pcodns2_help = gengetopt_args_info_help[14] ;
192 args_info->timelimit_help = gengetopt_args_info_help[15] ;
193 args_info->apn_help = gengetopt_args_info_help[16] ;
194 args_info->qos_help = gengetopt_args_info_help[17] ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100195 args_info->logfile_help = gengetopt_args_info_help[18] ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100196 args_info->loglevel_help = gengetopt_args_info_help[19] ;
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200197 args_info->gtp_linux_help = gengetopt_args_info_help[20] ;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100198
Harald Welte1b3e5772010-05-04 11:13:56 +0200199}
200
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100201void
202cmdline_parser_print_version (void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200203{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100204 printf ("%s %s\n",
205 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
206 CMDLINE_PARSER_VERSION);
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200207
208 if (strlen(gengetopt_args_info_versiontext) > 0)
209 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200210}
211
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100212static void print_help_common(void) {
213 cmdline_parser_print_version ();
Harald Welte1b3e5772010-05-04 11:13:56 +0200214
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100215 if (strlen(gengetopt_args_info_purpose) > 0)
216 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200217
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100218 if (strlen(gengetopt_args_info_usage) > 0)
219 printf("\n%s\n", gengetopt_args_info_usage);
220
221 printf("\n");
222
223 if (strlen(gengetopt_args_info_description) > 0)
224 printf("%s\n\n", gengetopt_args_info_description);
Harald Welte1b3e5772010-05-04 11:13:56 +0200225}
226
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100227void
228cmdline_parser_print_help (void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200229{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100230 int i = 0;
231 print_help_common();
232 while (gengetopt_args_info_help[i])
233 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200234}
235
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100236void
237cmdline_parser_init (struct gengetopt_args_info *args_info)
238{
239 clear_given (args_info);
240 clear_args (args_info);
241 init_args_info (args_info);
242}
243
244void
245cmdline_parser_params_init(struct cmdline_parser_params *params)
246{
247 if (params)
248 {
249 params->override = 0;
250 params->initialize = 1;
251 params->check_required = 1;
252 params->check_ambiguity = 0;
253 params->print_errors = 1;
254 }
255}
256
257struct cmdline_parser_params *
258cmdline_parser_params_create(void)
259{
260 struct cmdline_parser_params *params =
261 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
262 cmdline_parser_params_init(params);
263 return params;
264}
265
266static void
267free_string_field (char **s)
268{
269 if (*s)
270 {
271 free (*s);
272 *s = 0;
273 }
274}
275
276
277static void
278cmdline_parser_release (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200279{
Harald Weltebed35df2011-11-02 13:06:18 +0100280
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100281 free_string_field (&(args_info->conf_arg));
282 free_string_field (&(args_info->conf_orig));
283 free_string_field (&(args_info->pidfile_arg));
284 free_string_field (&(args_info->pidfile_orig));
285 free_string_field (&(args_info->statedir_arg));
286 free_string_field (&(args_info->statedir_orig));
287 free_string_field (&(args_info->listen_arg));
288 free_string_field (&(args_info->listen_orig));
289 free_string_field (&(args_info->net_arg));
290 free_string_field (&(args_info->net_orig));
291 free_string_field (&(args_info->ipup_arg));
292 free_string_field (&(args_info->ipup_orig));
293 free_string_field (&(args_info->ipdown_arg));
294 free_string_field (&(args_info->ipdown_orig));
295 free_string_field (&(args_info->dynip_arg));
296 free_string_field (&(args_info->dynip_orig));
297 free_string_field (&(args_info->statip_arg));
298 free_string_field (&(args_info->statip_orig));
299 free_string_field (&(args_info->pcodns1_arg));
300 free_string_field (&(args_info->pcodns1_orig));
301 free_string_field (&(args_info->pcodns2_arg));
302 free_string_field (&(args_info->pcodns2_orig));
303 free_string_field (&(args_info->timelimit_orig));
304 free_string_field (&(args_info->apn_arg));
305 free_string_field (&(args_info->apn_orig));
306 free_string_field (&(args_info->qos_orig));
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100307 free_string_field (&(args_info->logfile_arg));
308 free_string_field (&(args_info->logfile_orig));
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100309 free_string_field (&(args_info->loglevel_arg));
310 free_string_field (&(args_info->loglevel_orig));
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100311
312
Harald Weltebed35df2011-11-02 13:06:18 +0100313
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100314 clear_given (args_info);
315}
316
317
318static void
319write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
320{
321 FIX_UNUSED (values);
322 if (arg) {
323 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
324 } else {
325 fprintf(outfile, "%s\n", opt);
326 }
327}
328
329
330int
331cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
332{
333 int i = 0;
334
335 if (!outfile)
336 {
337 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
338 return EXIT_FAILURE;
339 }
340
341 if (args_info->help_given)
342 write_into_file(outfile, "help", 0, 0 );
343 if (args_info->version_given)
344 write_into_file(outfile, "version", 0, 0 );
345 if (args_info->fg_given)
346 write_into_file(outfile, "fg", 0, 0 );
347 if (args_info->debug_given)
348 write_into_file(outfile, "debug", 0, 0 );
349 if (args_info->conf_given)
350 write_into_file(outfile, "conf", args_info->conf_orig, 0);
351 if (args_info->pidfile_given)
352 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
353 if (args_info->statedir_given)
354 write_into_file(outfile, "statedir", args_info->statedir_orig, 0);
355 if (args_info->listen_given)
356 write_into_file(outfile, "listen", args_info->listen_orig, 0);
357 if (args_info->net_given)
358 write_into_file(outfile, "net", args_info->net_orig, 0);
359 if (args_info->ipup_given)
360 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
361 if (args_info->ipdown_given)
362 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
363 if (args_info->dynip_given)
364 write_into_file(outfile, "dynip", args_info->dynip_orig, 0);
365 if (args_info->statip_given)
366 write_into_file(outfile, "statip", args_info->statip_orig, 0);
367 if (args_info->pcodns1_given)
368 write_into_file(outfile, "pcodns1", args_info->pcodns1_orig, 0);
369 if (args_info->pcodns2_given)
370 write_into_file(outfile, "pcodns2", args_info->pcodns2_orig, 0);
371 if (args_info->timelimit_given)
372 write_into_file(outfile, "timelimit", args_info->timelimit_orig, 0);
373 if (args_info->apn_given)
374 write_into_file(outfile, "apn", args_info->apn_orig, 0);
375 if (args_info->qos_given)
376 write_into_file(outfile, "qos", args_info->qos_orig, 0);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100377 if (args_info->logfile_given)
378 write_into_file(outfile, "logfile", args_info->logfile_orig, 0);
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100379 if (args_info->loglevel_given)
380 write_into_file(outfile, "loglevel", args_info->loglevel_orig, 0);
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200381 if (args_info->gtp_linux_given)
382 write_into_file(outfile, "gtp-linux", 0, 0 );
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100383
384
385 i = EXIT_SUCCESS;
386 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200387}
388
389int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100390cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200391{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100392 FILE *outfile;
393 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200394
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100395 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200396
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100397 if (!outfile)
398 {
399 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
400 return EXIT_FAILURE;
401 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200402
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100403 i = cmdline_parser_dump(outfile, args_info);
404 fclose (outfile);
Harald Welte1b3e5772010-05-04 11:13:56 +0200405
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100406 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200407}
408
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100409void
410cmdline_parser_free (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200411{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100412 cmdline_parser_release (args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200413}
414
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100415/** @brief replacement of strdup, which is not standard */
416char *
417gengetopt_strdup (const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200418{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100419 char *result = 0;
420 if (!s)
421 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200422
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100423 result = (char*)malloc(strlen(s) + 1);
424 if (result == (char*)0)
425 return (char*)0;
426 strcpy(result, s);
427 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200428}
429
430int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100431cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200432{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100433 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200434}
435
436int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100437cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
438 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200439{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100440 int result;
441 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200442
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100443 if (result == EXIT_FAILURE)
444 {
445 cmdline_parser_free (args_info);
446 exit (EXIT_FAILURE);
447 }
448
449 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200450}
451
452int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100453cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
Harald Welte1b3e5772010-05-04 11:13:56 +0200454{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100455 int result;
456 struct cmdline_parser_params params;
457
458 params.override = override;
459 params.initialize = initialize;
460 params.check_required = check_required;
461 params.check_ambiguity = 0;
462 params.print_errors = 1;
463
464 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
465
466 if (result == EXIT_FAILURE)
467 {
468 cmdline_parser_free (args_info);
469 exit (EXIT_FAILURE);
470 }
471
472 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200473}
474
475int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100476cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200477{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100478 FIX_UNUSED (args_info);
479 FIX_UNUSED (prog_name);
480 return EXIT_SUCCESS;
481}
Harald Welte1b3e5772010-05-04 11:13:56 +0200482
Harald Welte1b3e5772010-05-04 11:13:56 +0200483
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100484static char *package_name = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200485
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100486/**
487 * @brief updates an option
488 * @param field the generic pointer to the field to update
489 * @param orig_field the pointer to the orig field
490 * @param field_given the pointer to the number of occurrence of this option
491 * @param prev_given the pointer to the number of occurrence already seen
492 * @param value the argument for this option (if null no arg was specified)
493 * @param possible_values the possible values for this option (if specified)
494 * @param default_value the default value (in case the option only accepts fixed values)
495 * @param arg_type the type of this option
496 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
497 * @param override @see cmdline_parser_params.override
498 * @param no_free whether to free a possible previous value
499 * @param multiple_option whether this is a multiple option
500 * @param long_opt the corresponding long option
501 * @param short_opt the corresponding short option (or '-' if none)
502 * @param additional_error possible further error specification
503 */
504static
505int update_arg(void *field, char **orig_field,
506 unsigned int *field_given, unsigned int *prev_given,
507 char *value, const char *possible_values[],
508 const char *default_value,
509 cmdline_parser_arg_type arg_type,
510 int check_ambiguity, int override,
511 int no_free, int multiple_option,
512 const char *long_opt, char short_opt,
513 const char *additional_error)
514{
515 char *stop_char = 0;
516 const char *val = value;
517 int found;
518 char **string_field;
519 FIX_UNUSED (field);
jjako52c24142002-12-16 13:33:51 +0000520
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100521 stop_char = 0;
522 found = 0;
jjako52c24142002-12-16 13:33:51 +0000523
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100524 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
525 {
526 if (short_opt != '-')
527 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
528 package_name, long_opt, short_opt,
529 (additional_error ? additional_error : ""));
530 else
531 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
532 package_name, long_opt,
533 (additional_error ? additional_error : ""));
534 return 1; /* failure */
535 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200536
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100537 FIX_UNUSED (default_value);
538
539 if (field_given && *field_given && ! override)
540 return 0;
541 if (prev_given)
542 (*prev_given)++;
543 if (field_given)
544 (*field_given)++;
545 if (possible_values)
546 val = possible_values[found];
jjako52c24142002-12-16 13:33:51 +0000547
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100548 switch(arg_type) {
549 case ARG_FLAG:
550 *((int *)field) = !*((int *)field);
551 break;
552 case ARG_INT:
553 if (val) *((int *)field) = strtol (val, &stop_char, 0);
554 break;
555 case ARG_STRING:
556 if (val) {
557 string_field = (char **)field;
558 if (!no_free && *string_field)
559 free (*string_field); /* free previous string */
560 *string_field = gengetopt_strdup (val);
561 }
562 break;
563 default:
564 break;
565 };
jjako52c24142002-12-16 13:33:51 +0000566
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100567 /* check numeric conversion */
568 switch(arg_type) {
569 case ARG_INT:
570 if (val && !(stop_char && *stop_char == '\0')) {
571 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
572 return 1; /* failure */
573 }
574 break;
575 default:
576 ;
577 };
jjako52c24142002-12-16 13:33:51 +0000578
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100579 /* store the original value */
580 switch(arg_type) {
581 case ARG_NO:
582 case ARG_FLAG:
583 break;
584 default:
585 if (value && orig_field) {
586 if (no_free) {
587 *orig_field = value;
588 } else {
589 if (*orig_field)
590 free (*orig_field); /* free previous string */
591 *orig_field = gengetopt_strdup (value);
592 }
593 }
594 };
jjako52c24142002-12-16 13:33:51 +0000595
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100596 return 0; /* OK */
597}
jjako52c24142002-12-16 13:33:51 +0000598
jjako52c24142002-12-16 13:33:51 +0000599
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100600int
601cmdline_parser_internal (
602 int argc, char **argv, struct gengetopt_args_info *args_info,
603 struct cmdline_parser_params *params, const char *additional_error)
604{
605 int c; /* Character of the parsed option. */
jjako52c24142002-12-16 13:33:51 +0000606
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200607 int error_occurred = 0;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100608 struct gengetopt_args_info local_args_info;
609
610 int override;
611 int initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100612 int check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100613 int check_ambiguity;
614
615 package_name = argv[0];
616
617 override = params->override;
618 initialize = params->initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100619 check_required = params->check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100620 check_ambiguity = params->check_ambiguity;
jjako52c24142002-12-16 13:33:51 +0000621
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100622 if (initialize)
623 cmdline_parser_init (args_info);
jjako52c24142002-12-16 13:33:51 +0000624
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100625 cmdline_parser_init (&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000626
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100627 optarg = 0;
628 optind = 0;
629 opterr = params->print_errors;
630 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000631
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100632 while (1)
633 {
634 int option_index = 0;
jjako52c24142002-12-16 13:33:51 +0000635
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100636 static struct option long_options[] = {
637 { "help", 0, NULL, 'h' },
638 { "version", 0, NULL, 'V' },
639 { "fg", 0, NULL, 'f' },
640 { "debug", 0, NULL, 'd' },
641 { "conf", 1, NULL, 'c' },
642 { "pidfile", 1, NULL, 0 },
643 { "statedir", 1, NULL, 0 },
644 { "listen", 1, NULL, 'l' },
645 { "net", 1, NULL, 'n' },
646 { "ipup", 1, NULL, 0 },
647 { "ipdown", 1, NULL, 0 },
648 { "dynip", 1, NULL, 0 },
649 { "statip", 1, NULL, 0 },
650 { "pcodns1", 1, NULL, 0 },
651 { "pcodns2", 1, NULL, 0 },
652 { "timelimit", 1, NULL, 0 },
653 { "apn", 1, NULL, 'a' },
654 { "qos", 1, NULL, 'q' },
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100655 { "logfile", 1, NULL, 0 },
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100656 { "loglevel", 1, NULL, 0 },
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200657 { "gtp-linux", 0, NULL, 'g' },
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100658 { 0, 0, 0, 0 }
659 };
jjako52c24142002-12-16 13:33:51 +0000660
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200661 c = getopt_long (argc, argv, "hVfdc:l:n:a:q:g", long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000662
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100663 if (c == -1) break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000664
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100665 switch (c)
666 {
667 case 'h': /* Print help and exit. */
668 cmdline_parser_print_help ();
669 cmdline_parser_free (&local_args_info);
670 exit (EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000671
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100672 case 'V': /* Print version and exit. */
673 cmdline_parser_print_version ();
674 cmdline_parser_free (&local_args_info);
675 exit (EXIT_SUCCESS);
Harald Welte1b3e5772010-05-04 11:13:56 +0200676
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100677 case 'f': /* Run in foreground. */
678
679
680 if (update_arg((void *)&(args_info->fg_flag), 0, &(args_info->fg_given),
681 &(local_args_info.fg_given), optarg, 0, 0, ARG_FLAG,
682 check_ambiguity, override, 1, 0, "fg", 'f',
683 additional_error))
684 goto failure;
685
686 break;
687 case 'd': /* Run in debug mode. */
688
689
690 if (update_arg((void *)&(args_info->debug_flag), 0, &(args_info->debug_given),
691 &(local_args_info.debug_given), optarg, 0, 0, ARG_FLAG,
692 check_ambiguity, override, 1, 0, "debug", 'd',
693 additional_error))
694 goto failure;
695
696 break;
697 case 'c': /* Read configuration file. */
698
699
700 if (update_arg( (void *)&(args_info->conf_arg),
701 &(args_info->conf_orig), &(args_info->conf_given),
702 &(local_args_info.conf_given), optarg, 0, "/etc/ggsn.conf", ARG_STRING,
703 check_ambiguity, override, 0, 0,
704 "conf", 'c',
705 additional_error))
706 goto failure;
707
708 break;
709 case 'l': /* Local interface. */
710
711
712 if (update_arg( (void *)&(args_info->listen_arg),
713 &(args_info->listen_orig), &(args_info->listen_given),
714 &(local_args_info.listen_given), optarg, 0, 0, ARG_STRING,
715 check_ambiguity, override, 0, 0,
716 "listen", 'l',
717 additional_error))
718 goto failure;
719
720 break;
721 case 'n': /* Network. */
722
723
724 if (update_arg( (void *)&(args_info->net_arg),
725 &(args_info->net_orig), &(args_info->net_given),
726 &(local_args_info.net_given), optarg, 0, "192.168.0.0/24", ARG_STRING,
727 check_ambiguity, override, 0, 0,
728 "net", 'n',
729 additional_error))
730 goto failure;
731
732 break;
733 case 'a': /* Access point name. */
734
735
736 if (update_arg( (void *)&(args_info->apn_arg),
737 &(args_info->apn_orig), &(args_info->apn_given),
738 &(local_args_info.apn_given), optarg, 0, "internet", ARG_STRING,
739 check_ambiguity, override, 0, 0,
740 "apn", 'a',
741 additional_error))
742 goto failure;
743
744 break;
745 case 'q': /* Requested quality of service. */
746
747
748 if (update_arg( (void *)&(args_info->qos_arg),
749 &(args_info->qos_orig), &(args_info->qos_given),
750 &(local_args_info.qos_given), optarg, 0, "0x0b921f", ARG_INT,
751 check_ambiguity, override, 0, 0,
752 "qos", 'q',
753 additional_error))
754 goto failure;
755
756 break;
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200757 case 'g': /* GTP linux kernel support. */
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100758
759
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200760 if (update_arg((void *)&(args_info->gtp_linux_flag), 0, &(args_info->gtp_linux_given),
761 &(local_args_info.gtp_linux_given), optarg, 0, 0, ARG_FLAG,
762 check_ambiguity, override, 1, 0, "gtp-linux", 'g',
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100763 additional_error))
764 goto failure;
765
766 break;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100767
768 case 0: /* Long option with no short option */
769 /* Filename of process id file. */
770 if (strcmp (long_options[option_index].name, "pidfile") == 0)
771 {
772
773
774 if (update_arg( (void *)&(args_info->pidfile_arg),
775 &(args_info->pidfile_orig), &(args_info->pidfile_given),
776 &(local_args_info.pidfile_given), optarg, 0, "/var/run/ggsn.pid", ARG_STRING,
777 check_ambiguity, override, 0, 0,
778 "pidfile", '-',
779 additional_error))
780 goto failure;
781
782 }
783 /* Directory of nonvolatile data. */
784 else if (strcmp (long_options[option_index].name, "statedir") == 0)
785 {
786
787
788 if (update_arg( (void *)&(args_info->statedir_arg),
789 &(args_info->statedir_orig), &(args_info->statedir_given),
790 &(local_args_info.statedir_given), optarg, 0, "/var/lib/ggsn/", ARG_STRING,
791 check_ambiguity, override, 0, 0,
792 "statedir", '-',
793 additional_error))
794 goto failure;
795
796 }
797 /* Script to run after link-up. */
798 else if (strcmp (long_options[option_index].name, "ipup") == 0)
799 {
800
801
802 if (update_arg( (void *)&(args_info->ipup_arg),
803 &(args_info->ipup_orig), &(args_info->ipup_given),
804 &(local_args_info.ipup_given), optarg, 0, 0, ARG_STRING,
805 check_ambiguity, override, 0, 0,
806 "ipup", '-',
807 additional_error))
808 goto failure;
809
810 }
811 /* Script to run after link-down. */
812 else if (strcmp (long_options[option_index].name, "ipdown") == 0)
813 {
814
815
816 if (update_arg( (void *)&(args_info->ipdown_arg),
817 &(args_info->ipdown_orig), &(args_info->ipdown_given),
818 &(local_args_info.ipdown_given), optarg, 0, 0, ARG_STRING,
819 check_ambiguity, override, 0, 0,
820 "ipdown", '-',
821 additional_error))
822 goto failure;
823
824 }
825 /* Dynamic IP address pool. */
826 else if (strcmp (long_options[option_index].name, "dynip") == 0)
827 {
828
829
830 if (update_arg( (void *)&(args_info->dynip_arg),
831 &(args_info->dynip_orig), &(args_info->dynip_given),
832 &(local_args_info.dynip_given), optarg, 0, 0, ARG_STRING,
833 check_ambiguity, override, 0, 0,
834 "dynip", '-',
835 additional_error))
836 goto failure;
837
838 }
839 /* Static IP address pool. */
840 else if (strcmp (long_options[option_index].name, "statip") == 0)
841 {
842
843
844 if (update_arg( (void *)&(args_info->statip_arg),
845 &(args_info->statip_orig), &(args_info->statip_given),
846 &(local_args_info.statip_given), optarg, 0, 0, ARG_STRING,
847 check_ambiguity, override, 0, 0,
848 "statip", '-',
849 additional_error))
850 goto failure;
851
852 }
853 /* PCO DNS Server 1. */
854 else if (strcmp (long_options[option_index].name, "pcodns1") == 0)
855 {
856
857
858 if (update_arg( (void *)&(args_info->pcodns1_arg),
859 &(args_info->pcodns1_orig), &(args_info->pcodns1_given),
860 &(local_args_info.pcodns1_given), optarg, 0, "0.0.0.0", ARG_STRING,
861 check_ambiguity, override, 0, 0,
862 "pcodns1", '-',
863 additional_error))
864 goto failure;
865
866 }
867 /* PCO DNS Server 2. */
868 else if (strcmp (long_options[option_index].name, "pcodns2") == 0)
869 {
870
871
872 if (update_arg( (void *)&(args_info->pcodns2_arg),
873 &(args_info->pcodns2_orig), &(args_info->pcodns2_given),
874 &(local_args_info.pcodns2_given), optarg, 0, "0.0.0.0", ARG_STRING,
875 check_ambiguity, override, 0, 0,
876 "pcodns2", '-',
877 additional_error))
878 goto failure;
879
880 }
881 /* Exit after timelimit seconds. */
882 else if (strcmp (long_options[option_index].name, "timelimit") == 0)
883 {
884
885
886 if (update_arg( (void *)&(args_info->timelimit_arg),
887 &(args_info->timelimit_orig), &(args_info->timelimit_given),
888 &(local_args_info.timelimit_given), optarg, 0, "0", ARG_INT,
889 check_ambiguity, override, 0, 0,
890 "timelimit", '-',
891 additional_error))
892 goto failure;
893
894 }
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100895 /* Logfile for errors. */
896 else if (strcmp (long_options[option_index].name, "logfile") == 0)
897 {
898
899
900 if (update_arg( (void *)&(args_info->logfile_arg),
901 &(args_info->logfile_orig), &(args_info->logfile_given),
902 &(local_args_info.logfile_given), optarg, 0, 0, ARG_STRING,
903 check_ambiguity, override, 0, 0,
904 "logfile", '-',
905 additional_error))
906 goto failure;
907
908 }
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100909 /* Global log ldevel. */
910 else if (strcmp (long_options[option_index].name, "loglevel") == 0)
911 {
912
913
914 if (update_arg( (void *)&(args_info->loglevel_arg),
915 &(args_info->loglevel_orig), &(args_info->loglevel_given),
916 &(local_args_info.loglevel_given), optarg, 0, "error", ARG_STRING,
917 check_ambiguity, override, 0, 0,
918 "loglevel", '-',
919 additional_error))
920 goto failure;
921
922 }
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100923
924 break;
925 case '?': /* Invalid option. */
926 /* `getopt_long' already printed an error message. */
927 goto failure;
928
929 default: /* bug: option not considered. */
930 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
931 abort ();
932 } /* switch */
933 } /* while */
934
935
936
937
938 cmdline_parser_release (&local_args_info);
939
Pablo Neira Ayusod9d7be32016-05-10 18:43:12 +0200940 if ( error_occurred )
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100941 return (EXIT_FAILURE);
942
943 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200944
945failure:
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100946
947 cmdline_parser_release (&local_args_info);
948 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +0000949}
950
Harald Welte1b3e5772010-05-04 11:13:56 +0200951#ifndef CONFIG_FILE_LINE_SIZE
952#define CONFIG_FILE_LINE_SIZE 2048
953#endif
954#define ADDITIONAL_ERROR " in configuration file "
955
956#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
957/* 3 is for "--" and "=" */
958
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100959static int
960_cmdline_parser_configfile (const char *filename, int *my_argc)
961{
962 FILE* file;
963 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
964 char linebuf[CONFIG_FILE_LINE_SIZE];
965 int line_num = 0;
966 int result = 0, equal;
967 char *fopt, *farg;
968 char *str_index;
969 size_t len, next_token;
970 char delimiter;
971
972 if ((file = fopen(filename, "r")) == 0)
973 {
974 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
975 CMDLINE_PARSER_PACKAGE, filename);
976 return EXIT_FAILURE;
977 }
978
979 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
980 {
981 ++line_num;
982 my_argv[0] = '\0';
983 len = strlen(linebuf);
984 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
985 {
986 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
987 CMDLINE_PARSER_PACKAGE, filename, line_num);
988 result = EXIT_FAILURE;
989 break;
990 }
991
992 /* find first non-whitespace character in the line */
993 next_token = strspn (linebuf, " \t\r\n");
994 str_index = linebuf + next_token;
995
996 if ( str_index[0] == '\0' || str_index[0] == '#')
997 continue; /* empty line or comment line is skipped */
998
999 fopt = str_index;
1000
1001 /* truncate fopt at the end of the first non-valid character */
1002 next_token = strcspn (fopt, " \t\r\n=");
1003
1004 if (fopt[next_token] == '\0') /* the line is over */
1005 {
1006 farg = 0;
1007 equal = 0;
1008 goto noarg;
1009 }
1010
1011 /* remember if equal sign is present */
1012 equal = (fopt[next_token] == '=');
1013 fopt[next_token++] = '\0';
1014
1015 /* advance pointers to the next token after the end of fopt */
1016 next_token += strspn (fopt + next_token, " \t\r\n");
1017
1018 /* check for the presence of equal sign, and if so, skip it */
1019 if ( !equal )
1020 if ((equal = (fopt[next_token] == '=')))
1021 {
1022 next_token++;
1023 next_token += strspn (fopt + next_token, " \t\r\n");
1024 }
1025 str_index += next_token;
1026
1027 /* find argument */
1028 farg = str_index;
1029 if ( farg[0] == '\"' || farg[0] == '\'' )
1030 { /* quoted argument */
1031 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1032 if (! str_index)
1033 {
1034 fprintf
1035 (stderr,
1036 "%s:%s:%d: unterminated string in configuration file\n",
1037 CMDLINE_PARSER_PACKAGE, filename, line_num);
1038 result = EXIT_FAILURE;
1039 break;
1040 }
1041 }
1042 else
1043 { /* read up the remaining part up to a delimiter */
1044 next_token = strcspn (farg, " \t\r\n#\'\"");
1045 str_index += next_token;
1046 }
1047
1048 /* truncate farg at the delimiter and store it for further check */
1049 delimiter = *str_index, *str_index++ = '\0';
1050
1051 /* everything but comment is illegal at the end of line */
1052 if (delimiter != '\0' && delimiter != '#')
1053 {
1054 str_index += strspn(str_index, " \t\r\n");
1055 if (*str_index != '\0' && *str_index != '#')
1056 {
1057 fprintf
1058 (stderr,
1059 "%s:%s:%d: malformed string in configuration file\n",
1060 CMDLINE_PARSER_PACKAGE, filename, line_num);
1061 result = EXIT_FAILURE;
1062 break;
1063 }
1064 }
1065
1066 noarg:
1067 if (!strcmp(fopt,"include")) {
1068 if (farg && *farg) {
1069 result = _cmdline_parser_configfile(farg, my_argc);
1070 } else {
1071 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1072 CMDLINE_PARSER_PACKAGE, filename, line_num);
1073 }
1074 continue;
1075 }
1076 len = strlen(fopt);
1077 strcat (my_argv, len > 1 ? "--" : "-");
1078 strcat (my_argv, fopt);
1079 if (len > 1 && ((farg && *farg) || equal))
1080 strcat (my_argv, "=");
1081 if (farg && *farg)
1082 strcat (my_argv, farg);
1083 ++(*my_argc);
1084
1085 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1086 cmd_line_list_tmp->next = cmd_line_list;
1087 cmd_line_list = cmd_line_list_tmp;
1088 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1089 } /* while */
1090
1091 if (file)
1092 fclose(file);
1093 return result;
1094}
jjako52c24142002-12-16 13:33:51 +00001095
1096int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001097cmdline_parser_configfile (
1098 const char *filename,
1099 struct gengetopt_args_info *args_info,
1100 int override, int initialize, int check_required)
jjako52c24142002-12-16 13:33:51 +00001101{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001102 struct cmdline_parser_params params;
Harald Welte1b3e5772010-05-04 11:13:56 +02001103
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001104 params.override = override;
1105 params.initialize = initialize;
1106 params.check_required = check_required;
1107 params.check_ambiguity = 0;
1108 params.print_errors = 1;
1109
1110 return cmdline_parser_config_file (filename, args_info, &params);
1111}
jjako52c24142002-12-16 13:33:51 +00001112
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001113int
1114cmdline_parser_config_file (const char *filename,
1115 struct gengetopt_args_info *args_info,
1116 struct cmdline_parser_params *params)
1117{
1118 int i, result;
1119 int my_argc = 1;
1120 char **my_argv_arg;
1121 char *additional_error;
jjako52c24142002-12-16 13:33:51 +00001122
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001123 /* store the program name */
1124 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1125 cmd_line_list_tmp->next = cmd_line_list;
1126 cmd_line_list = cmd_line_list_tmp;
1127 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
jjako52c24142002-12-16 13:33:51 +00001128
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001129 result = _cmdline_parser_configfile(filename, &my_argc);
jjako52c24142002-12-16 13:33:51 +00001130
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001131 if (result != EXIT_FAILURE) {
1132 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1133 cmd_line_list_tmp = cmd_line_list;
Harald Welte1b3e5772010-05-04 11:13:56 +02001134
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001135 for (i = my_argc - 1; i >= 0; --i) {
1136 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1137 cmd_line_list_tmp = cmd_line_list_tmp->next;
1138 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001139
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001140 my_argv_arg[my_argc] = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001141
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001142 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1143 strcpy (additional_error, ADDITIONAL_ERROR);
1144 strcat (additional_error, filename);
1145 result =
1146 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
1147 params,
1148 additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +02001149
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001150 free (additional_error);
1151 free (my_argv_arg);
1152 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001153
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001154 free_cmd_list();
1155 if (result == EXIT_FAILURE)
1156 {
1157 cmdline_parser_free (args_info);
1158 exit (EXIT_FAILURE);
1159 }
1160
1161 return result;
jjako52c24142002-12-16 13:33:51 +00001162}