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