blob: 3c5d7e29f7049f22abdad3befa184b44d78f97b1 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjako4b26b512003-01-28 16:13:57 +00002 File autogenerated by gengetopt version 2.8rc
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
jjako4b26b512003-01-28 16:13:57 +00004 ../../gengetopt-2.8rc/src/gengetopt --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
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15/* If we use autoconf. */
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19/* Check for configure's getopt check result. */
20#ifndef HAVE_GETOPT_LONG
21#include "getopt.h"
22#else
23#include <getopt.h>
24#endif
25
26#ifndef HAVE_STRDUP
27#define strdup gengetopt_strdup
28#endif /* HAVE_STRDUP */
29
30#include "cmdline.h"
31
32
33void
34cmdline_parser_print_version (void)
35{
36 printf ("%s %s\n", PACKAGE, VERSION);
37}
38
39void
40cmdline_parser_print_help (void)
41{
42 cmdline_parser_print_version ();
43 printf("\n"
44 "Usage: %s [OPTIONS]...\n", PACKAGE);
45 printf(" -h --help Print help and exit\n");
46 printf(" -V --version Print version and exit\n");
47 printf(" -f --fg Run in foreground (default=off)\n");
48 printf(" -d --debug Run in debug mode (default=off)\n");
49 printf(" -cSTRING --conf=STRING Read configuration file (default='/etc/ggsn.conf')\n");
50 printf(" --pidfile=STRING Filename of process id file (default='/var/run/ggsn.pid')\n");
51 printf(" --statedir=STRING Directory of nonvolatile data (default='/var/lib/ggsn/')\n");
52 printf(" -lSTRING --listen=STRING Local interface\n");
53 printf(" -nSTRING --net=STRING Network (default='192.168.0.0')\n");
54 printf(" --mask=STRING Network mask (default='255.255.255.0')\n");
55 printf(" --timelimit=INT Exit after timelimit seconds (default='0')\n");
56 printf(" -aSTRING --apn=STRING Access point name (default='internet')\n");
57 printf(" -qINT --qos=INT Requested quality of service (default='0x0b921f')\n");
jjako4b26b512003-01-28 16:13:57 +000058 printf(" --ipup=STRING Script to run after link-up\n");
59 printf(" --ipdown=STRING Script to run after link-down\n");
jjako52c24142002-12-16 13:33:51 +000060}
61
62
63#ifndef HAVE_STRDUP
64/* gengetopt_strdup(): automatically generated from strdup.c. */
65/* strdup.c replacement of strdup, which is not standard */
66static char *
67gengetopt_strdup (const char *s)
68{
69 char *result = (char*)malloc(strlen(s) + 1);
70 if (result == (char*)0)
71 return (char*)0;
72 strcpy(result, s);
73 return result;
74}
75#endif /* HAVE_STRDUP */
76
77int
78cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
79{
80 int c; /* Character of the parsed option. */
81 int missing_required_options = 0;
82
83 args_info->help_given = 0 ;
84 args_info->version_given = 0 ;
85 args_info->fg_given = 0 ;
86 args_info->debug_given = 0 ;
87 args_info->conf_given = 0 ;
88 args_info->pidfile_given = 0 ;
89 args_info->statedir_given = 0 ;
90 args_info->listen_given = 0 ;
91 args_info->net_given = 0 ;
92 args_info->mask_given = 0 ;
93 args_info->timelimit_given = 0 ;
94 args_info->apn_given = 0 ;
95 args_info->qos_given = 0 ;
jjako4b26b512003-01-28 16:13:57 +000096 args_info->ipup_given = 0 ;
97 args_info->ipdown_given = 0 ;
jjako52c24142002-12-16 13:33:51 +000098#define clear_args() { \
99 args_info->fg_flag = 0;\
100 args_info->debug_flag = 0;\
101 args_info->conf_arg = strdup("/etc/ggsn.conf") ;\
102 args_info->pidfile_arg = strdup("/var/run/ggsn.pid") ;\
103 args_info->statedir_arg = strdup("/var/lib/ggsn/") ;\
104 args_info->listen_arg = NULL; \
105 args_info->net_arg = strdup("192.168.0.0") ;\
106 args_info->mask_arg = strdup("255.255.255.0") ;\
107 args_info->timelimit_arg = 0 ;\
108 args_info->apn_arg = strdup("internet") ;\
109 args_info->qos_arg = 0x0b921f ;\
jjako4b26b512003-01-28 16:13:57 +0000110 args_info->ipup_arg = NULL; \
111 args_info->ipdown_arg = NULL; \
jjako52c24142002-12-16 13:33:51 +0000112}
113
114 clear_args();
115
116 optarg = 0;
117 optind = 1;
118 opterr = 1;
119 optopt = '?';
120
121 while (1)
122 {
123 int option_index = 0;
124 char *stop_char;
125 static struct option long_options[] = {
126 { "help", 0, NULL, 'h' },
127 { "version", 0, NULL, 'V' },
128 { "fg", 0, NULL, 'f' },
129 { "debug", 0, NULL, 'd' },
130 { "conf", 1, NULL, 'c' },
131 { "pidfile", 1, NULL, 0 },
132 { "statedir", 1, NULL, 0 },
133 { "listen", 1, NULL, 'l' },
134 { "net", 1, NULL, 'n' },
135 { "mask", 1, NULL, 0 },
136 { "timelimit", 1, NULL, 0 },
137 { "apn", 1, NULL, 'a' },
138 { "qos", 1, NULL, 'q' },
jjako4b26b512003-01-28 16:13:57 +0000139 { "ipup", 1, NULL, 0 },
140 { "ipdown", 1, NULL, 0 },
jjako52c24142002-12-16 13:33:51 +0000141 { NULL, 0, NULL, 0 }
142 };
143
144 c = getopt_long (argc, argv, "hVfdc:l:n:a:q:", long_options, &option_index);
145
146 if (c == -1) break; /* Exit from `while (1)' loop. */
147
148 switch (c)
149 {
150 case 'h': /* Print help and exit. */
151 clear_args ();
152 cmdline_parser_print_help ();
153 exit (EXIT_SUCCESS);
154
155 case 'V': /* Print version and exit. */
156 clear_args ();
157 cmdline_parser_print_version ();
158 exit (EXIT_SUCCESS);
159
160 case 'f': /* Run in foreground. */
161 if (args_info->fg_given)
162 {
163 fprintf (stderr, "%s: `--fg' (`-f') option given more than once\n", PACKAGE);
164 clear_args ();
165 exit (EXIT_FAILURE);
166 }
167 args_info->fg_given = 1;
168 args_info->fg_flag = !(args_info->fg_flag);
169 break;
170
171 case 'd': /* Run in debug mode. */
172 if (args_info->debug_given)
173 {
174 fprintf (stderr, "%s: `--debug' (`-d') option given more than once\n", PACKAGE);
175 clear_args ();
176 exit (EXIT_FAILURE);
177 }
178 args_info->debug_given = 1;
179 args_info->debug_flag = !(args_info->debug_flag);
180 break;
181
182 case 'c': /* Read configuration file. */
183 if (args_info->conf_given)
184 {
185 fprintf (stderr, "%s: `--conf' (`-c') option given more than once\n", PACKAGE);
186 clear_args ();
187 exit (EXIT_FAILURE);
188 }
189 args_info->conf_given = 1;
190 args_info->conf_arg = strdup (optarg);
191 break;
192
193 case 'l': /* Local interface. */
194 if (args_info->listen_given)
195 {
196 fprintf (stderr, "%s: `--listen' (`-l') option given more than once\n", PACKAGE);
197 clear_args ();
198 exit (EXIT_FAILURE);
199 }
200 args_info->listen_given = 1;
201 args_info->listen_arg = strdup (optarg);
202 break;
203
204 case 'n': /* Network. */
205 if (args_info->net_given)
206 {
207 fprintf (stderr, "%s: `--net' (`-n') option given more than once\n", PACKAGE);
208 clear_args ();
209 exit (EXIT_FAILURE);
210 }
211 args_info->net_given = 1;
212 args_info->net_arg = strdup (optarg);
213 break;
214
215 case 'a': /* Access point name. */
216 if (args_info->apn_given)
217 {
218 fprintf (stderr, "%s: `--apn' (`-a') option given more than once\n", PACKAGE);
219 clear_args ();
220 exit (EXIT_FAILURE);
221 }
222 args_info->apn_given = 1;
223 args_info->apn_arg = strdup (optarg);
224 break;
225
226 case 'q': /* Requested quality of service. */
227 if (args_info->qos_given)
228 {
229 fprintf (stderr, "%s: `--qos' (`-q') option given more than once\n", PACKAGE);
230 clear_args ();
231 exit (EXIT_FAILURE);
232 }
233 args_info->qos_given = 1;
234 args_info->qos_arg = strtol (optarg,&stop_char,0);
235 break;
236
237
238 case 0: /* Long option with no short option */
239 /* Filename of process id file. */
240 if (strcmp (long_options[option_index].name, "pidfile") == 0)
241 {
242 if (args_info->pidfile_given)
243 {
244 fprintf (stderr, "%s: `--pidfile' option given more than once\n", PACKAGE);
245 clear_args ();
246 exit (EXIT_FAILURE);
247 }
248 args_info->pidfile_given = 1;
249 args_info->pidfile_arg = strdup (optarg);
250 break;
251 }
252 /* Directory of nonvolatile data. */
253 else if (strcmp (long_options[option_index].name, "statedir") == 0)
254 {
255 if (args_info->statedir_given)
256 {
257 fprintf (stderr, "%s: `--statedir' option given more than once\n", PACKAGE);
258 clear_args ();
259 exit (EXIT_FAILURE);
260 }
261 args_info->statedir_given = 1;
262 args_info->statedir_arg = strdup (optarg);
263 break;
264 }
265 /* Network mask. */
266 else if (strcmp (long_options[option_index].name, "mask") == 0)
267 {
268 if (args_info->mask_given)
269 {
270 fprintf (stderr, "%s: `--mask' option given more than once\n", PACKAGE);
271 clear_args ();
272 exit (EXIT_FAILURE);
273 }
274 args_info->mask_given = 1;
275 args_info->mask_arg = strdup (optarg);
276 break;
277 }
278 /* Exit after timelimit seconds. */
279 else if (strcmp (long_options[option_index].name, "timelimit") == 0)
280 {
281 if (args_info->timelimit_given)
282 {
283 fprintf (stderr, "%s: `--timelimit' option given more than once\n", PACKAGE);
284 clear_args ();
285 exit (EXIT_FAILURE);
286 }
287 args_info->timelimit_given = 1;
288 args_info->timelimit_arg = strtol (optarg,&stop_char,0);
289 break;
290 }
jjako4b26b512003-01-28 16:13:57 +0000291 /* Script to run after link-up. */
292 else if (strcmp (long_options[option_index].name, "ipup") == 0)
293 {
294 if (args_info->ipup_given)
295 {
296 fprintf (stderr, "%s: `--ipup' option given more than once\n", PACKAGE);
297 clear_args ();
298 exit (EXIT_FAILURE);
299 }
300 args_info->ipup_given = 1;
301 args_info->ipup_arg = strdup (optarg);
302 break;
303 }
304 /* Script to run after link-down. */
305 else if (strcmp (long_options[option_index].name, "ipdown") == 0)
306 {
307 if (args_info->ipdown_given)
308 {
309 fprintf (stderr, "%s: `--ipdown' option given more than once\n", PACKAGE);
310 clear_args ();
311 exit (EXIT_FAILURE);
312 }
313 args_info->ipdown_given = 1;
314 args_info->ipdown_arg = strdup (optarg);
315 break;
316 }
jjako52c24142002-12-16 13:33:51 +0000317
318 case '?': /* Invalid option. */
319 /* `getopt_long' already printed an error message. */
320 exit (EXIT_FAILURE);
321
322 default: /* bug: option not considered. */
323 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
324 abort ();
325 } /* switch */
326 } /* while */
327
328
329 if ( missing_required_options )
330 exit (EXIT_FAILURE);
331
332 return 0;
333}
334
335#define CONFIGPARSERBUFSIZE 1024
336
337int
338cmdline_parser_configfile (char * const filename, struct gengetopt_args_info *args_info, int override)
339{
340 FILE* file;
341 char linebuf[CONFIGPARSERBUFSIZE];
342 int line_num = 0;
343 int len;
344 int fnum;
345 char fopt[CONFIGPARSERBUFSIZE], farg[CONFIGPARSERBUFSIZE];
346 char *stop_char;
347
348 if ((file = fopen(filename, "r")) == NULL)
349 {
350 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
351 PACKAGE, filename);
352 exit (EXIT_FAILURE);
353 }
354
355 while ((fgets(linebuf, CONFIGPARSERBUFSIZE, file)) != NULL)
356 {
357 ++line_num;
358 len = strlen(linebuf);
359 if (len == CONFIGPARSERBUFSIZE-1)
360 {
361 fprintf (stderr, "%s: Line longer than %d characters found in configuration file '%s'\n",
362 PACKAGE, CONFIGPARSERBUFSIZE, filename);
363 exit (EXIT_FAILURE);
364 }
365
366 if (linebuf[0] == '#')
367 continue; /* Line was a comment */
368
369 /* Get the option */
370 if ((fnum = sscanf(linebuf, "%s %s", fopt, farg)) > 0)
371 {
372 if (!strcmp(fopt, "help"))
373 {
374 if (override || !args_info->help_given)
375 {
376 args_info->help_given = 1;
377
378 }
379 continue;
380 }
381 if (!strcmp(fopt, "version"))
382 {
383 if (override || !args_info->version_given)
384 {
385 args_info->version_given = 1;
386
387 }
388 continue;
389 }
390 if (!strcmp(fopt, "fg"))
391 {
392 if (override || !args_info->fg_given)
393 {
394 args_info->fg_given = 1;
395 args_info->fg_flag = !(args_info->fg_flag);
396 }
397 continue;
398 }
399 if (!strcmp(fopt, "debug"))
400 {
401 if (override || !args_info->debug_given)
402 {
403 args_info->debug_given = 1;
404 args_info->debug_flag = !(args_info->debug_flag);
405 }
406 continue;
407 }
408 if (!strcmp(fopt, "conf"))
409 {
410 if (override || !args_info->conf_given)
411 {
412 args_info->conf_given = 1;
413 if (fnum == 2)
414 args_info->conf_arg = strdup (farg);
415 else
416 {
417 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
418 filename, line_num);
419 exit (EXIT_FAILURE);
420 }
421 }
422 continue;
423 }
424 if (!strcmp(fopt, "pidfile"))
425 {
426 if (override || !args_info->pidfile_given)
427 {
428 args_info->pidfile_given = 1;
429 if (fnum == 2)
430 args_info->pidfile_arg = strdup (farg);
431 else
432 {
433 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
434 filename, line_num);
435 exit (EXIT_FAILURE);
436 }
437 }
438 continue;
439 }
440 if (!strcmp(fopt, "statedir"))
441 {
442 if (override || !args_info->statedir_given)
443 {
444 args_info->statedir_given = 1;
445 if (fnum == 2)
446 args_info->statedir_arg = strdup (farg);
447 else
448 {
449 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
450 filename, line_num);
451 exit (EXIT_FAILURE);
452 }
453 }
454 continue;
455 }
456 if (!strcmp(fopt, "listen"))
457 {
458 if (override || !args_info->listen_given)
459 {
460 args_info->listen_given = 1;
461 if (fnum == 2)
462 args_info->listen_arg = strdup (farg);
463 else
464 {
465 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
466 filename, line_num);
467 exit (EXIT_FAILURE);
468 }
469 }
470 continue;
471 }
472 if (!strcmp(fopt, "net"))
473 {
474 if (override || !args_info->net_given)
475 {
476 args_info->net_given = 1;
477 if (fnum == 2)
478 args_info->net_arg = strdup (farg);
479 else
480 {
481 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
482 filename, line_num);
483 exit (EXIT_FAILURE);
484 }
485 }
486 continue;
487 }
488 if (!strcmp(fopt, "mask"))
489 {
490 if (override || !args_info->mask_given)
491 {
492 args_info->mask_given = 1;
493 if (fnum == 2)
494 args_info->mask_arg = strdup (farg);
495 else
496 {
497 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
498 filename, line_num);
499 exit (EXIT_FAILURE);
500 }
501 }
502 continue;
503 }
504 if (!strcmp(fopt, "timelimit"))
505 {
506 if (override || !args_info->timelimit_given)
507 {
508 args_info->timelimit_given = 1;
509 if (fnum == 2)
510 args_info->timelimit_arg = strtol (farg,&stop_char,0);
511 else
512 {
513 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
514 filename, line_num);
515 exit (EXIT_FAILURE);
516 }
517 }
518 continue;
519 }
520 if (!strcmp(fopt, "apn"))
521 {
522 if (override || !args_info->apn_given)
523 {
524 args_info->apn_given = 1;
525 if (fnum == 2)
526 args_info->apn_arg = strdup (farg);
527 else
528 {
529 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
530 filename, line_num);
531 exit (EXIT_FAILURE);
532 }
533 }
534 continue;
535 }
536 if (!strcmp(fopt, "qos"))
537 {
538 if (override || !args_info->qos_given)
539 {
540 args_info->qos_given = 1;
541 if (fnum == 2)
542 args_info->qos_arg = strtol (farg,&stop_char,0);
543 else
544 {
545 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
546 filename, line_num);
547 exit (EXIT_FAILURE);
548 }
549 }
550 continue;
551 }
jjako4b26b512003-01-28 16:13:57 +0000552 if (!strcmp(fopt, "ipup"))
553 {
554 if (override || !args_info->ipup_given)
555 {
556 args_info->ipup_given = 1;
557 if (fnum == 2)
558 args_info->ipup_arg = strdup (farg);
559 else
560 {
561 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
562 filename, line_num);
563 exit (EXIT_FAILURE);
564 }
565 }
566 continue;
567 }
568 if (!strcmp(fopt, "ipdown"))
569 {
570 if (override || !args_info->ipdown_given)
571 {
572 args_info->ipdown_given = 1;
573 if (fnum == 2)
574 args_info->ipdown_arg = strdup (farg);
575 else
576 {
577 fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
578 filename, line_num);
579 exit (EXIT_FAILURE);
580 }
581 }
582 continue;
583 }
jjako52c24142002-12-16 13:33:51 +0000584
585
586 /* Tried all known options. This one is unknown! */
587 fprintf (stderr, "%s: Unknown option '%s' found in %s\n",
588 PACKAGE, fopt, filename);
589 exit (EXIT_FAILURE);
590 }
591 } /* while */
592 fclose(file); /* No error checking on close */
593
594 return 0;
595}