jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 2 | File autogenerated by gengetopt version 2.17 |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3 | generated with the following command: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 4 | gengetopt --conf-parser |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | /* If we use autoconf. */ |
| 12 | #ifdef HAVE_CONFIG_H |
| 13 | #include "config.h" |
| 14 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 15 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "getopt.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 21 | |
| 22 | #include "cmdline.h" |
| 23 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 24 | const char *gengetopt_args_info_purpose = ""; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 25 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 26 | const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]..."; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 27 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 28 | const char *gengetopt_args_info_help[] = { |
| 29 | " -h, --help Print help and exit", |
| 30 | " -V, --version Print version and exit", |
| 31 | " -f, --fg Run in foreground (default=off)", |
| 32 | " -d, --debug Run in debug mode (default=off)", |
| 33 | " -c, --conf=STRING Read configuration file (default=`/etc/ggsn.conf')", |
| 34 | " --pidfile=STRING Filename of process id file \n (default=`/var/run/ggsn.pid')", |
| 35 | " --statedir=STRING Directory of nonvolatile data \n (default=`/var/lib/ggsn/')", |
| 36 | " -l, --listen=STRING Local interface", |
| 37 | " -n, --net=STRING Network (default=`192.168.0.0/24')", |
| 38 | " --ipup=STRING Script to run after link-up", |
| 39 | " --ipdown=STRING Script to run after link-down", |
| 40 | " --dynip=STRING Dynamic IP address pool", |
| 41 | " --statip=STRING Static IP address pool", |
| 42 | " --pcodns1=STRING PCO DNS Server 1 (default=`0.0.0.0')", |
| 43 | " --pcodns2=STRING PCO DNS Server 2 (default=`0.0.0.0')", |
| 44 | " --timelimit=INT Exit after timelimit seconds (default=`0')", |
| 45 | " -a, --apn=STRING Access point name (default=`internet')", |
| 46 | " -q, --qos=INT Requested quality of service (default=`0x0b921f')", |
| 47 | 0 |
| 48 | }; |
| 49 | |
| 50 | static |
| 51 | void clear_given (struct gengetopt_args_info *args_info); |
| 52 | static |
| 53 | void clear_args (struct gengetopt_args_info *args_info); |
| 54 | |
| 55 | static int |
| 56 | cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error); |
| 57 | |
| 58 | struct line_list |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 59 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 60 | char * string_arg; |
| 61 | struct line_list * next; |
| 62 | }; |
| 63 | |
| 64 | static struct line_list *cmd_line_list = 0; |
| 65 | static struct line_list *cmd_line_list_tmp = 0; |
| 66 | |
| 67 | static void |
| 68 | free_cmd_list(void) |
| 69 | { |
| 70 | /* free the list of a previous call */ |
| 71 | if (cmd_line_list) |
| 72 | { |
| 73 | while (cmd_line_list) { |
| 74 | cmd_line_list_tmp = cmd_line_list; |
| 75 | cmd_line_list = cmd_line_list->next; |
| 76 | free (cmd_line_list_tmp->string_arg); |
| 77 | free (cmd_line_list_tmp); |
| 78 | } |
| 79 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 83 | static char * |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 84 | gengetopt_strdup (const char *s); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 85 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 86 | static |
| 87 | void clear_given (struct gengetopt_args_info *args_info) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 88 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 89 | args_info->help_given = 0 ; |
| 90 | args_info->version_given = 0 ; |
| 91 | args_info->fg_given = 0 ; |
| 92 | args_info->debug_given = 0 ; |
| 93 | args_info->conf_given = 0 ; |
| 94 | args_info->pidfile_given = 0 ; |
| 95 | args_info->statedir_given = 0 ; |
| 96 | args_info->listen_given = 0 ; |
| 97 | args_info->net_given = 0 ; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 98 | args_info->ipup_given = 0 ; |
| 99 | args_info->ipdown_given = 0 ; |
| 100 | args_info->dynip_given = 0 ; |
| 101 | args_info->statip_given = 0 ; |
| 102 | args_info->pcodns1_given = 0 ; |
| 103 | args_info->pcodns2_given = 0 ; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 104 | args_info->timelimit_given = 0 ; |
| 105 | args_info->apn_given = 0 ; |
| 106 | args_info->qos_given = 0 ; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 109 | static |
| 110 | void clear_args (struct gengetopt_args_info *args_info) |
| 111 | { |
| 112 | args_info->fg_flag = 0; |
| 113 | args_info->debug_flag = 0; |
| 114 | args_info->conf_arg = gengetopt_strdup ("/etc/ggsn.conf"); |
| 115 | args_info->conf_orig = NULL; |
| 116 | args_info->pidfile_arg = gengetopt_strdup ("/var/run/ggsn.pid"); |
| 117 | args_info->pidfile_orig = NULL; |
| 118 | args_info->statedir_arg = gengetopt_strdup ("/var/lib/ggsn/"); |
| 119 | args_info->statedir_orig = NULL; |
| 120 | args_info->listen_arg = NULL; |
| 121 | args_info->listen_orig = NULL; |
| 122 | args_info->net_arg = gengetopt_strdup ("192.168.0.0/24"); |
| 123 | args_info->net_orig = NULL; |
| 124 | args_info->ipup_arg = NULL; |
| 125 | args_info->ipup_orig = NULL; |
| 126 | args_info->ipdown_arg = NULL; |
| 127 | args_info->ipdown_orig = NULL; |
| 128 | args_info->dynip_arg = NULL; |
| 129 | args_info->dynip_orig = NULL; |
| 130 | args_info->statip_arg = NULL; |
| 131 | args_info->statip_orig = NULL; |
| 132 | args_info->pcodns1_arg = gengetopt_strdup ("0.0.0.0"); |
| 133 | args_info->pcodns1_orig = NULL; |
| 134 | args_info->pcodns2_arg = gengetopt_strdup ("0.0.0.0"); |
| 135 | args_info->pcodns2_orig = NULL; |
| 136 | args_info->timelimit_arg = 0; |
| 137 | args_info->timelimit_orig = NULL; |
| 138 | args_info->apn_arg = gengetopt_strdup ("internet"); |
| 139 | args_info->apn_orig = NULL; |
| 140 | args_info->qos_arg = 0x0b921f; |
| 141 | args_info->qos_orig = NULL; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | static |
| 146 | void init_args_info(struct gengetopt_args_info *args_info) |
| 147 | { |
| 148 | args_info->help_help = gengetopt_args_info_help[0] ; |
| 149 | args_info->version_help = gengetopt_args_info_help[1] ; |
| 150 | args_info->fg_help = gengetopt_args_info_help[2] ; |
| 151 | args_info->debug_help = gengetopt_args_info_help[3] ; |
| 152 | args_info->conf_help = gengetopt_args_info_help[4] ; |
| 153 | args_info->pidfile_help = gengetopt_args_info_help[5] ; |
| 154 | args_info->statedir_help = gengetopt_args_info_help[6] ; |
| 155 | args_info->listen_help = gengetopt_args_info_help[7] ; |
| 156 | args_info->net_help = gengetopt_args_info_help[8] ; |
| 157 | args_info->ipup_help = gengetopt_args_info_help[9] ; |
| 158 | args_info->ipdown_help = gengetopt_args_info_help[10] ; |
| 159 | args_info->dynip_help = gengetopt_args_info_help[11] ; |
| 160 | args_info->statip_help = gengetopt_args_info_help[12] ; |
| 161 | args_info->pcodns1_help = gengetopt_args_info_help[13] ; |
| 162 | args_info->pcodns2_help = gengetopt_args_info_help[14] ; |
| 163 | args_info->timelimit_help = gengetopt_args_info_help[15] ; |
| 164 | args_info->apn_help = gengetopt_args_info_help[16] ; |
| 165 | args_info->qos_help = gengetopt_args_info_help[17] ; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | cmdline_parser_print_version (void) |
| 171 | { |
| 172 | printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | cmdline_parser_print_help (void) |
| 177 | { |
| 178 | int i = 0; |
| 179 | cmdline_parser_print_version (); |
| 180 | |
| 181 | if (strlen(gengetopt_args_info_purpose) > 0) |
| 182 | printf("\n%s\n", gengetopt_args_info_purpose); |
| 183 | |
| 184 | printf("\n%s\n\n", gengetopt_args_info_usage); |
| 185 | while (gengetopt_args_info_help[i]) |
| 186 | printf("%s\n", gengetopt_args_info_help[i++]); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | cmdline_parser_init (struct gengetopt_args_info *args_info) |
| 191 | { |
| 192 | clear_given (args_info); |
| 193 | clear_args (args_info); |
| 194 | init_args_info (args_info); |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | cmdline_parser_release (struct gengetopt_args_info *args_info) |
| 199 | { |
| 200 | |
| 201 | if (args_info->conf_arg) |
| 202 | { |
| 203 | free (args_info->conf_arg); /* free previous argument */ |
| 204 | args_info->conf_arg = 0; |
| 205 | } |
| 206 | if (args_info->conf_orig) |
| 207 | { |
| 208 | free (args_info->conf_orig); /* free previous argument */ |
| 209 | args_info->conf_orig = 0; |
| 210 | } |
| 211 | if (args_info->pidfile_arg) |
| 212 | { |
| 213 | free (args_info->pidfile_arg); /* free previous argument */ |
| 214 | args_info->pidfile_arg = 0; |
| 215 | } |
| 216 | if (args_info->pidfile_orig) |
| 217 | { |
| 218 | free (args_info->pidfile_orig); /* free previous argument */ |
| 219 | args_info->pidfile_orig = 0; |
| 220 | } |
| 221 | if (args_info->statedir_arg) |
| 222 | { |
| 223 | free (args_info->statedir_arg); /* free previous argument */ |
| 224 | args_info->statedir_arg = 0; |
| 225 | } |
| 226 | if (args_info->statedir_orig) |
| 227 | { |
| 228 | free (args_info->statedir_orig); /* free previous argument */ |
| 229 | args_info->statedir_orig = 0; |
| 230 | } |
| 231 | if (args_info->listen_arg) |
| 232 | { |
| 233 | free (args_info->listen_arg); /* free previous argument */ |
| 234 | args_info->listen_arg = 0; |
| 235 | } |
| 236 | if (args_info->listen_orig) |
| 237 | { |
| 238 | free (args_info->listen_orig); /* free previous argument */ |
| 239 | args_info->listen_orig = 0; |
| 240 | } |
| 241 | if (args_info->net_arg) |
| 242 | { |
| 243 | free (args_info->net_arg); /* free previous argument */ |
| 244 | args_info->net_arg = 0; |
| 245 | } |
| 246 | if (args_info->net_orig) |
| 247 | { |
| 248 | free (args_info->net_orig); /* free previous argument */ |
| 249 | args_info->net_orig = 0; |
| 250 | } |
| 251 | if (args_info->ipup_arg) |
| 252 | { |
| 253 | free (args_info->ipup_arg); /* free previous argument */ |
| 254 | args_info->ipup_arg = 0; |
| 255 | } |
| 256 | if (args_info->ipup_orig) |
| 257 | { |
| 258 | free (args_info->ipup_orig); /* free previous argument */ |
| 259 | args_info->ipup_orig = 0; |
| 260 | } |
| 261 | if (args_info->ipdown_arg) |
| 262 | { |
| 263 | free (args_info->ipdown_arg); /* free previous argument */ |
| 264 | args_info->ipdown_arg = 0; |
| 265 | } |
| 266 | if (args_info->ipdown_orig) |
| 267 | { |
| 268 | free (args_info->ipdown_orig); /* free previous argument */ |
| 269 | args_info->ipdown_orig = 0; |
| 270 | } |
| 271 | if (args_info->dynip_arg) |
| 272 | { |
| 273 | free (args_info->dynip_arg); /* free previous argument */ |
| 274 | args_info->dynip_arg = 0; |
| 275 | } |
| 276 | if (args_info->dynip_orig) |
| 277 | { |
| 278 | free (args_info->dynip_orig); /* free previous argument */ |
| 279 | args_info->dynip_orig = 0; |
| 280 | } |
| 281 | if (args_info->statip_arg) |
| 282 | { |
| 283 | free (args_info->statip_arg); /* free previous argument */ |
| 284 | args_info->statip_arg = 0; |
| 285 | } |
| 286 | if (args_info->statip_orig) |
| 287 | { |
| 288 | free (args_info->statip_orig); /* free previous argument */ |
| 289 | args_info->statip_orig = 0; |
| 290 | } |
| 291 | if (args_info->pcodns1_arg) |
| 292 | { |
| 293 | free (args_info->pcodns1_arg); /* free previous argument */ |
| 294 | args_info->pcodns1_arg = 0; |
| 295 | } |
| 296 | if (args_info->pcodns1_orig) |
| 297 | { |
| 298 | free (args_info->pcodns1_orig); /* free previous argument */ |
| 299 | args_info->pcodns1_orig = 0; |
| 300 | } |
| 301 | if (args_info->pcodns2_arg) |
| 302 | { |
| 303 | free (args_info->pcodns2_arg); /* free previous argument */ |
| 304 | args_info->pcodns2_arg = 0; |
| 305 | } |
| 306 | if (args_info->pcodns2_orig) |
| 307 | { |
| 308 | free (args_info->pcodns2_orig); /* free previous argument */ |
| 309 | args_info->pcodns2_orig = 0; |
| 310 | } |
| 311 | if (args_info->timelimit_orig) |
| 312 | { |
| 313 | free (args_info->timelimit_orig); /* free previous argument */ |
| 314 | args_info->timelimit_orig = 0; |
| 315 | } |
| 316 | if (args_info->apn_arg) |
| 317 | { |
| 318 | free (args_info->apn_arg); /* free previous argument */ |
| 319 | args_info->apn_arg = 0; |
| 320 | } |
| 321 | if (args_info->apn_orig) |
| 322 | { |
| 323 | free (args_info->apn_orig); /* free previous argument */ |
| 324 | args_info->apn_orig = 0; |
| 325 | } |
| 326 | if (args_info->qos_orig) |
| 327 | { |
| 328 | free (args_info->qos_orig); /* free previous argument */ |
| 329 | args_info->qos_orig = 0; |
| 330 | } |
| 331 | |
| 332 | clear_given (args_info); |
| 333 | } |
| 334 | |
| 335 | int |
| 336 | cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) |
| 337 | { |
| 338 | FILE *outfile; |
| 339 | int i = 0; |
| 340 | |
| 341 | outfile = fopen(filename, "w"); |
| 342 | |
| 343 | if (!outfile) |
| 344 | { |
| 345 | fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); |
| 346 | return EXIT_FAILURE; |
| 347 | } |
| 348 | |
| 349 | if (args_info->help_given) { |
| 350 | fprintf(outfile, "%s\n", "help"); |
| 351 | } |
| 352 | if (args_info->version_given) { |
| 353 | fprintf(outfile, "%s\n", "version"); |
| 354 | } |
| 355 | if (args_info->fg_given) { |
| 356 | fprintf(outfile, "%s\n", "fg"); |
| 357 | } |
| 358 | if (args_info->debug_given) { |
| 359 | fprintf(outfile, "%s\n", "debug"); |
| 360 | } |
| 361 | if (args_info->conf_given) { |
| 362 | if (args_info->conf_orig) { |
| 363 | fprintf(outfile, "%s=\"%s\"\n", "conf", args_info->conf_orig); |
| 364 | } else { |
| 365 | fprintf(outfile, "%s\n", "conf"); |
| 366 | } |
| 367 | } |
| 368 | if (args_info->pidfile_given) { |
| 369 | if (args_info->pidfile_orig) { |
| 370 | fprintf(outfile, "%s=\"%s\"\n", "pidfile", args_info->pidfile_orig); |
| 371 | } else { |
| 372 | fprintf(outfile, "%s\n", "pidfile"); |
| 373 | } |
| 374 | } |
| 375 | if (args_info->statedir_given) { |
| 376 | if (args_info->statedir_orig) { |
| 377 | fprintf(outfile, "%s=\"%s\"\n", "statedir", args_info->statedir_orig); |
| 378 | } else { |
| 379 | fprintf(outfile, "%s\n", "statedir"); |
| 380 | } |
| 381 | } |
| 382 | if (args_info->listen_given) { |
| 383 | if (args_info->listen_orig) { |
| 384 | fprintf(outfile, "%s=\"%s\"\n", "listen", args_info->listen_orig); |
| 385 | } else { |
| 386 | fprintf(outfile, "%s\n", "listen"); |
| 387 | } |
| 388 | } |
| 389 | if (args_info->net_given) { |
| 390 | if (args_info->net_orig) { |
| 391 | fprintf(outfile, "%s=\"%s\"\n", "net", args_info->net_orig); |
| 392 | } else { |
| 393 | fprintf(outfile, "%s\n", "net"); |
| 394 | } |
| 395 | } |
| 396 | if (args_info->ipup_given) { |
| 397 | if (args_info->ipup_orig) { |
| 398 | fprintf(outfile, "%s=\"%s\"\n", "ipup", args_info->ipup_orig); |
| 399 | } else { |
| 400 | fprintf(outfile, "%s\n", "ipup"); |
| 401 | } |
| 402 | } |
| 403 | if (args_info->ipdown_given) { |
| 404 | if (args_info->ipdown_orig) { |
| 405 | fprintf(outfile, "%s=\"%s\"\n", "ipdown", args_info->ipdown_orig); |
| 406 | } else { |
| 407 | fprintf(outfile, "%s\n", "ipdown"); |
| 408 | } |
| 409 | } |
| 410 | if (args_info->dynip_given) { |
| 411 | if (args_info->dynip_orig) { |
| 412 | fprintf(outfile, "%s=\"%s\"\n", "dynip", args_info->dynip_orig); |
| 413 | } else { |
| 414 | fprintf(outfile, "%s\n", "dynip"); |
| 415 | } |
| 416 | } |
| 417 | if (args_info->statip_given) { |
| 418 | if (args_info->statip_orig) { |
| 419 | fprintf(outfile, "%s=\"%s\"\n", "statip", args_info->statip_orig); |
| 420 | } else { |
| 421 | fprintf(outfile, "%s\n", "statip"); |
| 422 | } |
| 423 | } |
| 424 | if (args_info->pcodns1_given) { |
| 425 | if (args_info->pcodns1_orig) { |
| 426 | fprintf(outfile, "%s=\"%s\"\n", "pcodns1", args_info->pcodns1_orig); |
| 427 | } else { |
| 428 | fprintf(outfile, "%s\n", "pcodns1"); |
| 429 | } |
| 430 | } |
| 431 | if (args_info->pcodns2_given) { |
| 432 | if (args_info->pcodns2_orig) { |
| 433 | fprintf(outfile, "%s=\"%s\"\n", "pcodns2", args_info->pcodns2_orig); |
| 434 | } else { |
| 435 | fprintf(outfile, "%s\n", "pcodns2"); |
| 436 | } |
| 437 | } |
| 438 | if (args_info->timelimit_given) { |
| 439 | if (args_info->timelimit_orig) { |
| 440 | fprintf(outfile, "%s=\"%s\"\n", "timelimit", args_info->timelimit_orig); |
| 441 | } else { |
| 442 | fprintf(outfile, "%s\n", "timelimit"); |
| 443 | } |
| 444 | } |
| 445 | if (args_info->apn_given) { |
| 446 | if (args_info->apn_orig) { |
| 447 | fprintf(outfile, "%s=\"%s\"\n", "apn", args_info->apn_orig); |
| 448 | } else { |
| 449 | fprintf(outfile, "%s\n", "apn"); |
| 450 | } |
| 451 | } |
| 452 | if (args_info->qos_given) { |
| 453 | if (args_info->qos_orig) { |
| 454 | fprintf(outfile, "%s=\"%s\"\n", "qos", args_info->qos_orig); |
| 455 | } else { |
| 456 | fprintf(outfile, "%s\n", "qos"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | fclose (outfile); |
| 461 | |
| 462 | i = EXIT_SUCCESS; |
| 463 | return i; |
| 464 | } |
| 465 | |
| 466 | void |
| 467 | cmdline_parser_free (struct gengetopt_args_info *args_info) |
| 468 | { |
| 469 | cmdline_parser_release (args_info); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* gengetopt_strdup() */ |
| 474 | /* strdup.c replacement of strdup, which is not standard */ |
| 475 | char * |
| 476 | gengetopt_strdup (const char *s) |
| 477 | { |
| 478 | char *result = NULL; |
| 479 | if (!s) |
| 480 | return result; |
| 481 | |
| 482 | result = (char*)malloc(strlen(s) + 1); |
| 483 | if (result == (char*)0) |
| 484 | return (char*)0; |
| 485 | strcpy(result, s); |
| 486 | return result; |
| 487 | } |
| 488 | |
| 489 | int |
| 490 | cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) |
| 491 | { |
| 492 | return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
| 497 | { |
| 498 | int result; |
| 499 | |
| 500 | result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL); |
| 501 | |
| 502 | if (result == EXIT_FAILURE) |
| 503 | { |
| 504 | cmdline_parser_free (args_info); |
| 505 | exit (EXIT_FAILURE); |
| 506 | } |
| 507 | |
| 508 | return result; |
| 509 | } |
| 510 | |
| 511 | int |
| 512 | cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) |
| 513 | { |
| 514 | return EXIT_SUCCESS; |
| 515 | } |
| 516 | |
| 517 | int |
| 518 | cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error) |
| 519 | { |
| 520 | int c; /* Character of the parsed option. */ |
| 521 | |
| 522 | int error = 0; |
| 523 | struct gengetopt_args_info local_args_info; |
| 524 | |
| 525 | if (initialize) |
| 526 | cmdline_parser_init (args_info); |
| 527 | |
| 528 | cmdline_parser_init (&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 529 | |
| 530 | optarg = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 531 | optind = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 532 | opterr = 1; |
| 533 | optopt = '?'; |
| 534 | |
| 535 | while (1) |
| 536 | { |
| 537 | int option_index = 0; |
| 538 | char *stop_char; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 539 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 540 | static struct option long_options[] = { |
| 541 | { "help", 0, NULL, 'h' }, |
| 542 | { "version", 0, NULL, 'V' }, |
| 543 | { "fg", 0, NULL, 'f' }, |
| 544 | { "debug", 0, NULL, 'd' }, |
| 545 | { "conf", 1, NULL, 'c' }, |
| 546 | { "pidfile", 1, NULL, 0 }, |
| 547 | { "statedir", 1, NULL, 0 }, |
| 548 | { "listen", 1, NULL, 'l' }, |
| 549 | { "net", 1, NULL, 'n' }, |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 550 | { "ipup", 1, NULL, 0 }, |
| 551 | { "ipdown", 1, NULL, 0 }, |
| 552 | { "dynip", 1, NULL, 0 }, |
| 553 | { "statip", 1, NULL, 0 }, |
| 554 | { "pcodns1", 1, NULL, 0 }, |
| 555 | { "pcodns2", 1, NULL, 0 }, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 556 | { "timelimit", 1, NULL, 0 }, |
| 557 | { "apn", 1, NULL, 'a' }, |
| 558 | { "qos", 1, NULL, 'q' }, |
| 559 | { NULL, 0, NULL, 0 } |
| 560 | }; |
| 561 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 562 | stop_char = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 563 | c = getopt_long (argc, argv, "hVfdc:l:n:a:q:", long_options, &option_index); |
| 564 | |
| 565 | if (c == -1) break; /* Exit from `while (1)' loop. */ |
| 566 | |
| 567 | switch (c) |
| 568 | { |
| 569 | case 'h': /* Print help and exit. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 570 | cmdline_parser_print_help (); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 571 | cmdline_parser_free (&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 572 | exit (EXIT_SUCCESS); |
| 573 | |
| 574 | case 'V': /* Print version and exit. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 575 | cmdline_parser_print_version (); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 576 | cmdline_parser_free (&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 577 | exit (EXIT_SUCCESS); |
| 578 | |
| 579 | case 'f': /* Run in foreground. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 580 | if (local_args_info.fg_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 581 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 582 | fprintf (stderr, "%s: `--fg' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 583 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 584 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 585 | if (args_info->fg_given && ! override) |
| 586 | continue; |
| 587 | local_args_info.fg_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 588 | args_info->fg_given = 1; |
| 589 | args_info->fg_flag = !(args_info->fg_flag); |
| 590 | break; |
| 591 | |
| 592 | case 'd': /* Run in debug mode. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 593 | if (local_args_info.debug_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 594 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 595 | fprintf (stderr, "%s: `--debug' (`-d') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 596 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 597 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 598 | if (args_info->debug_given && ! override) |
| 599 | continue; |
| 600 | local_args_info.debug_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 601 | args_info->debug_given = 1; |
| 602 | args_info->debug_flag = !(args_info->debug_flag); |
| 603 | break; |
| 604 | |
| 605 | case 'c': /* Read configuration file. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 606 | if (local_args_info.conf_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 607 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 608 | fprintf (stderr, "%s: `--conf' (`-c') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 609 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 610 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 611 | if (args_info->conf_given && ! override) |
| 612 | continue; |
| 613 | local_args_info.conf_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 614 | args_info->conf_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 615 | if (args_info->conf_arg) |
| 616 | free (args_info->conf_arg); /* free previous string */ |
| 617 | args_info->conf_arg = gengetopt_strdup (optarg); |
| 618 | if (args_info->conf_orig) |
| 619 | free (args_info->conf_orig); /* free previous string */ |
| 620 | args_info->conf_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 621 | break; |
| 622 | |
| 623 | case 'l': /* Local interface. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 624 | if (local_args_info.listen_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 625 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 626 | fprintf (stderr, "%s: `--listen' (`-l') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 627 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 628 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 629 | if (args_info->listen_given && ! override) |
| 630 | continue; |
| 631 | local_args_info.listen_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 632 | args_info->listen_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 633 | if (args_info->listen_arg) |
| 634 | free (args_info->listen_arg); /* free previous string */ |
| 635 | args_info->listen_arg = gengetopt_strdup (optarg); |
| 636 | if (args_info->listen_orig) |
| 637 | free (args_info->listen_orig); /* free previous string */ |
| 638 | args_info->listen_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case 'n': /* Network. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 642 | if (local_args_info.net_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 643 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 644 | fprintf (stderr, "%s: `--net' (`-n') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 645 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 646 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 647 | if (args_info->net_given && ! override) |
| 648 | continue; |
| 649 | local_args_info.net_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 650 | args_info->net_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 651 | if (args_info->net_arg) |
| 652 | free (args_info->net_arg); /* free previous string */ |
| 653 | args_info->net_arg = gengetopt_strdup (optarg); |
| 654 | if (args_info->net_orig) |
| 655 | free (args_info->net_orig); /* free previous string */ |
| 656 | args_info->net_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 657 | break; |
| 658 | |
| 659 | case 'a': /* Access point name. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 660 | if (local_args_info.apn_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 661 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 662 | fprintf (stderr, "%s: `--apn' (`-a') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 663 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 664 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 665 | if (args_info->apn_given && ! override) |
| 666 | continue; |
| 667 | local_args_info.apn_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 668 | args_info->apn_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 669 | if (args_info->apn_arg) |
| 670 | free (args_info->apn_arg); /* free previous string */ |
| 671 | args_info->apn_arg = gengetopt_strdup (optarg); |
| 672 | if (args_info->apn_orig) |
| 673 | free (args_info->apn_orig); /* free previous string */ |
| 674 | args_info->apn_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case 'q': /* Requested quality of service. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 678 | if (local_args_info.qos_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 679 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 680 | fprintf (stderr, "%s: `--qos' (`-q') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 681 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 682 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 683 | if (args_info->qos_given && ! override) |
| 684 | continue; |
| 685 | local_args_info.qos_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 686 | args_info->qos_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 687 | args_info->qos_arg = strtol (optarg, &stop_char, 0); |
| 688 | if (!(stop_char && *stop_char == '\0')) { |
| 689 | fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); |
| 690 | goto failure; |
| 691 | } |
| 692 | if (args_info->qos_orig) |
| 693 | free (args_info->qos_orig); /* free previous string */ |
| 694 | args_info->qos_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 695 | break; |
| 696 | |
| 697 | |
| 698 | case 0: /* Long option with no short option */ |
| 699 | /* Filename of process id file. */ |
| 700 | if (strcmp (long_options[option_index].name, "pidfile") == 0) |
| 701 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 702 | if (local_args_info.pidfile_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 703 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 704 | fprintf (stderr, "%s: `--pidfile' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 705 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 706 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 707 | if (args_info->pidfile_given && ! override) |
| 708 | continue; |
| 709 | local_args_info.pidfile_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 710 | args_info->pidfile_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 711 | if (args_info->pidfile_arg) |
| 712 | free (args_info->pidfile_arg); /* free previous string */ |
| 713 | args_info->pidfile_arg = gengetopt_strdup (optarg); |
| 714 | if (args_info->pidfile_orig) |
| 715 | free (args_info->pidfile_orig); /* free previous string */ |
| 716 | args_info->pidfile_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 717 | } |
| 718 | /* Directory of nonvolatile data. */ |
| 719 | else if (strcmp (long_options[option_index].name, "statedir") == 0) |
| 720 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 721 | if (local_args_info.statedir_given) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 722 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 723 | fprintf (stderr, "%s: `--statedir' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 724 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 725 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 726 | if (args_info->statedir_given && ! override) |
| 727 | continue; |
| 728 | local_args_info.statedir_given = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 729 | args_info->statedir_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 730 | if (args_info->statedir_arg) |
| 731 | free (args_info->statedir_arg); /* free previous string */ |
| 732 | args_info->statedir_arg = gengetopt_strdup (optarg); |
| 733 | if (args_info->statedir_orig) |
| 734 | free (args_info->statedir_orig); /* free previous string */ |
| 735 | args_info->statedir_orig = gengetopt_strdup (optarg); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 736 | } |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 737 | /* Script to run after link-up. */ |
| 738 | else if (strcmp (long_options[option_index].name, "ipup") == 0) |
| 739 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 740 | if (local_args_info.ipup_given) |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 741 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 742 | fprintf (stderr, "%s: `--ipup' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 743 | goto failure; |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 744 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 745 | if (args_info->ipup_given && ! override) |
| 746 | continue; |
| 747 | local_args_info.ipup_given = 1; |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 748 | args_info->ipup_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 749 | if (args_info->ipup_arg) |
| 750 | free (args_info->ipup_arg); /* free previous string */ |
| 751 | args_info->ipup_arg = gengetopt_strdup (optarg); |
| 752 | if (args_info->ipup_orig) |
| 753 | free (args_info->ipup_orig); /* free previous string */ |
| 754 | args_info->ipup_orig = gengetopt_strdup (optarg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 755 | } |
| 756 | /* Script to run after link-down. */ |
| 757 | else if (strcmp (long_options[option_index].name, "ipdown") == 0) |
| 758 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 759 | if (local_args_info.ipdown_given) |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 760 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 761 | fprintf (stderr, "%s: `--ipdown' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 762 | goto failure; |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 763 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 764 | if (args_info->ipdown_given && ! override) |
| 765 | continue; |
| 766 | local_args_info.ipdown_given = 1; |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 767 | args_info->ipdown_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 768 | if (args_info->ipdown_arg) |
| 769 | free (args_info->ipdown_arg); /* free previous string */ |
| 770 | args_info->ipdown_arg = gengetopt_strdup (optarg); |
| 771 | if (args_info->ipdown_orig) |
| 772 | free (args_info->ipdown_orig); /* free previous string */ |
| 773 | args_info->ipdown_orig = gengetopt_strdup (optarg); |
jjako | 4b26b51 | 2003-01-28 16:13:57 +0000 | [diff] [blame] | 774 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 775 | /* Dynamic IP address pool. */ |
| 776 | else if (strcmp (long_options[option_index].name, "dynip") == 0) |
| 777 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 778 | if (local_args_info.dynip_given) |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 779 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 780 | fprintf (stderr, "%s: `--dynip' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 781 | goto failure; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 782 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 783 | if (args_info->dynip_given && ! override) |
| 784 | continue; |
| 785 | local_args_info.dynip_given = 1; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 786 | args_info->dynip_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 787 | if (args_info->dynip_arg) |
| 788 | free (args_info->dynip_arg); /* free previous string */ |
| 789 | args_info->dynip_arg = gengetopt_strdup (optarg); |
| 790 | if (args_info->dynip_orig) |
| 791 | free (args_info->dynip_orig); /* free previous string */ |
| 792 | args_info->dynip_orig = gengetopt_strdup (optarg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 793 | } |
| 794 | /* Static IP address pool. */ |
| 795 | else if (strcmp (long_options[option_index].name, "statip") == 0) |
| 796 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 797 | if (local_args_info.statip_given) |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 798 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 799 | fprintf (stderr, "%s: `--statip' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 800 | goto failure; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 801 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 802 | if (args_info->statip_given && ! override) |
| 803 | continue; |
| 804 | local_args_info.statip_given = 1; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 805 | args_info->statip_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 806 | if (args_info->statip_arg) |
| 807 | free (args_info->statip_arg); /* free previous string */ |
| 808 | args_info->statip_arg = gengetopt_strdup (optarg); |
| 809 | if (args_info->statip_orig) |
| 810 | free (args_info->statip_orig); /* free previous string */ |
| 811 | args_info->statip_orig = gengetopt_strdup (optarg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 812 | } |
| 813 | /* PCO DNS Server 1. */ |
| 814 | else if (strcmp (long_options[option_index].name, "pcodns1") == 0) |
| 815 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 816 | if (local_args_info.pcodns1_given) |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 817 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 818 | fprintf (stderr, "%s: `--pcodns1' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 819 | goto failure; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 820 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 821 | if (args_info->pcodns1_given && ! override) |
| 822 | continue; |
| 823 | local_args_info.pcodns1_given = 1; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 824 | args_info->pcodns1_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 825 | if (args_info->pcodns1_arg) |
| 826 | free (args_info->pcodns1_arg); /* free previous string */ |
| 827 | args_info->pcodns1_arg = gengetopt_strdup (optarg); |
| 828 | if (args_info->pcodns1_orig) |
| 829 | free (args_info->pcodns1_orig); /* free previous string */ |
| 830 | args_info->pcodns1_orig = gengetopt_strdup (optarg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 831 | } |
| 832 | /* PCO DNS Server 2. */ |
| 833 | else if (strcmp (long_options[option_index].name, "pcodns2") == 0) |
| 834 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 835 | if (local_args_info.pcodns2_given) |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 836 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 837 | fprintf (stderr, "%s: `--pcodns2' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 838 | goto failure; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 839 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 840 | if (args_info->pcodns2_given && ! override) |
| 841 | continue; |
| 842 | local_args_info.pcodns2_given = 1; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 843 | args_info->pcodns2_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 844 | if (args_info->pcodns2_arg) |
| 845 | free (args_info->pcodns2_arg); /* free previous string */ |
| 846 | args_info->pcodns2_arg = gengetopt_strdup (optarg); |
| 847 | if (args_info->pcodns2_orig) |
| 848 | free (args_info->pcodns2_orig); /* free previous string */ |
| 849 | args_info->pcodns2_orig = gengetopt_strdup (optarg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 850 | } |
| 851 | /* Exit after timelimit seconds. */ |
| 852 | else if (strcmp (long_options[option_index].name, "timelimit") == 0) |
| 853 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 854 | if (local_args_info.timelimit_given) |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 855 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 856 | fprintf (stderr, "%s: `--timelimit' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); |
| 857 | goto failure; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 858 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 859 | if (args_info->timelimit_given && ! override) |
| 860 | continue; |
| 861 | local_args_info.timelimit_given = 1; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 862 | args_info->timelimit_given = 1; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 863 | args_info->timelimit_arg = strtol (optarg, &stop_char, 0); |
| 864 | if (!(stop_char && *stop_char == '\0')) { |
| 865 | fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); |
| 866 | goto failure; |
| 867 | } |
| 868 | if (args_info->timelimit_orig) |
| 869 | free (args_info->timelimit_orig); /* free previous string */ |
| 870 | args_info->timelimit_orig = gengetopt_strdup (optarg); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 871 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 872 | |
| 873 | break; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 874 | case '?': /* Invalid option. */ |
| 875 | /* `getopt_long' already printed an error message. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 876 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 877 | |
| 878 | default: /* bug: option not considered. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 879 | fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 880 | abort (); |
| 881 | } /* switch */ |
| 882 | } /* while */ |
| 883 | |
| 884 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 885 | |
| 886 | |
| 887 | cmdline_parser_release (&local_args_info); |
| 888 | |
| 889 | if ( error ) |
| 890 | return (EXIT_FAILURE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 891 | |
| 892 | return 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 893 | |
| 894 | failure: |
| 895 | |
| 896 | cmdline_parser_release (&local_args_info); |
| 897 | return (EXIT_FAILURE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 900 | #ifndef CONFIG_FILE_LINE_SIZE |
| 901 | #define CONFIG_FILE_LINE_SIZE 2048 |
| 902 | #endif |
| 903 | #define ADDITIONAL_ERROR " in configuration file " |
| 904 | |
| 905 | #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3) |
| 906 | /* 3 is for "--" and "=" */ |
| 907 | |
| 908 | char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 909 | |
| 910 | int |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 911 | cmdline_parser_configfile (char * const filename, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 912 | { |
| 913 | FILE* file; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 914 | char linebuf[CONFIG_FILE_LINE_SIZE]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 915 | int line_num = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 916 | int i, result, equal; |
| 917 | char *fopt, *farg; |
| 918 | char *str_index; |
| 919 | size_t len, next_token; |
| 920 | char delimiter; |
| 921 | int my_argc = 0; |
| 922 | char **my_argv_arg; |
| 923 | char *additional_error; |
| 924 | |
| 925 | /* store the program name */ |
| 926 | cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); |
| 927 | cmd_line_list_tmp->next = cmd_line_list; |
| 928 | cmd_line_list = cmd_line_list_tmp; |
| 929 | cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 930 | |
| 931 | if ((file = fopen(filename, "r")) == NULL) |
| 932 | { |
| 933 | fprintf (stderr, "%s: Error opening configuration file '%s'\n", |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 934 | CMDLINE_PARSER_PACKAGE, filename); |
| 935 | result = EXIT_FAILURE; |
| 936 | goto conf_failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 939 | while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 940 | { |
| 941 | ++line_num; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 942 | my_argv[0] = '\0'; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 943 | len = strlen(linebuf); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 944 | if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 945 | { |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 946 | fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n", |
| 947 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 948 | result = EXIT_FAILURE; |
| 949 | goto conf_failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 952 | /* find first non-whitespace character in the line */ |
| 953 | next_token = strspn ( linebuf, " \t\r\n"); |
| 954 | str_index = linebuf + next_token; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 955 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 956 | if ( str_index[0] == '\0' || str_index[0] == '#') |
| 957 | continue; /* empty line or comment line is skipped */ |
| 958 | |
| 959 | fopt = str_index; |
| 960 | |
| 961 | /* truncate fopt at the end of the first non-valid character */ |
| 962 | next_token = strcspn (fopt, " \t\r\n="); |
| 963 | |
| 964 | if (fopt[next_token] == '\0') /* the line is over */ |
| 965 | { |
| 966 | farg = NULL; |
| 967 | equal = 0; |
| 968 | goto noarg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 969 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 970 | |
| 971 | /* remember if equal sign is present */ |
| 972 | equal = (fopt[next_token] == '='); |
| 973 | fopt[next_token++] = '\0'; |
| 974 | |
| 975 | /* advance pointers to the next token after the end of fopt */ |
| 976 | next_token += strspn (fopt + next_token, " \t\r\n"); |
| 977 | /* check for the presence of equal sign, and if so, skip it */ |
| 978 | if ( !equal ) |
| 979 | if ((equal = (fopt[next_token] == '='))) |
| 980 | { |
| 981 | next_token++; |
| 982 | next_token += strspn (fopt + next_token, " \t\r\n"); |
| 983 | } |
| 984 | str_index += next_token; |
| 985 | |
| 986 | /* find argument */ |
| 987 | farg = str_index; |
| 988 | if ( farg[0] == '\"' || farg[0] == '\'' ) |
| 989 | { /* quoted argument */ |
| 990 | str_index = strchr (++farg, str_index[0] ); /* skip opening quote */ |
| 991 | if (! str_index) |
| 992 | { |
| 993 | fprintf |
| 994 | (stderr, |
| 995 | "%s:%s:%d: unterminated string in configuration file\n", |
| 996 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 997 | result = EXIT_FAILURE; |
| 998 | goto conf_failure; |
| 999 | } |
| 1000 | } |
| 1001 | else |
| 1002 | { /* read up the remaining part up to a delimiter */ |
| 1003 | next_token = strcspn (farg, " \t\r\n#\'\""); |
| 1004 | str_index += next_token; |
| 1005 | } |
| 1006 | |
| 1007 | /* truncate farg at the delimiter and store it for further check */ |
| 1008 | delimiter = *str_index, *str_index++ = '\0'; |
| 1009 | |
| 1010 | /* everything but comment is illegal at the end of line */ |
| 1011 | if (delimiter != '\0' && delimiter != '#') |
| 1012 | { |
| 1013 | str_index += strspn(str_index, " \t\r\n"); |
| 1014 | if (*str_index != '\0' && *str_index != '#') |
| 1015 | { |
| 1016 | fprintf |
| 1017 | (stderr, |
| 1018 | "%s:%s:%d: malformed string in configuration file\n", |
| 1019 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 1020 | result = EXIT_FAILURE; |
| 1021 | goto conf_failure; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | noarg: |
| 1026 | ++my_argc; |
| 1027 | len = strlen(fopt); |
| 1028 | |
| 1029 | strcat (my_argv, len > 1 ? "--" : "-"); |
| 1030 | strcat (my_argv, fopt); |
| 1031 | if (len > 1 && ((farg &&*farg) || equal)) |
| 1032 | strcat (my_argv, "="); |
| 1033 | if (farg && *farg) |
| 1034 | strcat (my_argv, farg); |
| 1035 | |
| 1036 | cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); |
| 1037 | cmd_line_list_tmp->next = cmd_line_list; |
| 1038 | cmd_line_list = cmd_line_list_tmp; |
| 1039 | cmd_line_list->string_arg = gengetopt_strdup(my_argv); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1040 | } /* while */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1041 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1042 | ++my_argc; /* for program name */ |
| 1043 | my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *)); |
| 1044 | cmd_line_list_tmp = cmd_line_list; |
| 1045 | for (i = my_argc - 1; i >= 0; --i) { |
| 1046 | my_argv_arg[i] = cmd_line_list_tmp->string_arg; |
| 1047 | cmd_line_list_tmp = cmd_line_list_tmp->next; |
| 1048 | } |
| 1049 | my_argv_arg[my_argc] = 0; |
| 1050 | |
| 1051 | additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1); |
| 1052 | strcpy (additional_error, ADDITIONAL_ERROR); |
| 1053 | strcat (additional_error, filename); |
| 1054 | result = |
| 1055 | cmdline_parser_internal (my_argc, my_argv_arg, args_info, override, initialize, check_required, additional_error); |
| 1056 | |
| 1057 | free (additional_error); |
| 1058 | free (my_argv_arg); |
| 1059 | |
| 1060 | conf_failure: |
| 1061 | if (file) |
| 1062 | fclose(file); |
| 1063 | |
| 1064 | free_cmd_list(); |
| 1065 | if (result == EXIT_FAILURE) |
| 1066 | { |
| 1067 | cmdline_parser_free (args_info); |
| 1068 | exit (EXIT_FAILURE); |
| 1069 | } |
| 1070 | |
| 1071 | return result; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1072 | } |