00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifdef HAVE_CONFIG_H
00039 #include "config.h"
00040 #endif
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045
00046 #include "getopt.h"
00047
00048 #include "cmdline.h"
00049
00050 const char *gengetopt_args_info_purpose = "Detects lanes in street images.";
00051
00052 const char *gengetopt_args_info_usage = "Usage: LinePerceptor [OPTIONS]... [FILES]...";
00053
00054 const char *gengetopt_args_info_help[] = {
00055 " -h, --help Print help and exit",
00056 " -V, --version Print version and exit",
00057 "\nBasic options:",
00058 " --lanes-conf=STRING Configuration file for lane detection \n (default=`Lanes.conf')",
00059 " --stoplines-conf=STRING Configuration file for stopline detection \n (default=`StopLines.conf')",
00060 " --no-stoplines Don't detect stop lines (default=on)",
00061 " --no-lanes Don't detect lanes (default=off)",
00062 " --camera-conf=STRING Configuration file for the camera paramters \n (default=`CameraInfo.conf')",
00063 " --list-file=STRING Text file containing a list of images one per \n line",
00064 " --list-path=STRING Path where the image files are located, this is \n just appended at the front of each line in \n --list-file (default=`')",
00065 " --image-file=STRING The path to an image",
00066 "\nDebugging options:",
00067 " --wait=INT Number of milliseconds to show the detected \n lanes. Put 0 for infinite i.e. waits for \n keypress. (default=`0')",
00068 " --show Show the detected lines (default=off)",
00069 " --step Step through each image (needs a keypress) or \n fall through (waits for --wait msecs) \n (default=off)",
00070 " --show-lane-numbers Show the lane numbers on the output image \n (default=off)",
00071 " --output-suffix=STRING Suffix of images and results \n (default=`_results')",
00072 " --save-images Export all images with detected lanes to the by \n appending --output-suffix + '.png' to each \n input image (default=off)",
00073 " --save-lanes Export all detected lanes to a text file by \n appending --output-suffix + '.txt' to \n --list-file (default=off)",
00074 " --debug Show debugging information and images \n (default=off)",
00075 0
00076 };
00077
00078 static
00079 void clear_given (struct gengetopt_args_info *args_info);
00080 static
00081 void clear_args (struct gengetopt_args_info *args_info);
00082
00083 static int
00084 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);
00085
00086 struct line_list
00087 {
00088 char * string_arg;
00089 struct line_list * next;
00090 };
00091
00092 static struct line_list *cmd_line_list = 0;
00093 static struct line_list *cmd_line_list_tmp = 0;
00094
00095 static void
00096 free_cmd_list(void)
00097 {
00098
00099 if (cmd_line_list)
00100 {
00101 while (cmd_line_list) {
00102 cmd_line_list_tmp = cmd_line_list;
00103 cmd_line_list = cmd_line_list->next;
00104 free (cmd_line_list_tmp->string_arg);
00105 free (cmd_line_list_tmp);
00106 }
00107 }
00108 }
00109
00110
00111 static char *
00112 gengetopt_strdup (const char *s);
00113
00114 static
00115 void clear_given (struct gengetopt_args_info *args_info)
00116 {
00117 args_info->help_given = 0 ;
00118 args_info->version_given = 0 ;
00119 args_info->lanes_conf_given = 0 ;
00120 args_info->stoplines_conf_given = 0 ;
00121 args_info->no_stoplines_given = 0 ;
00122 args_info->no_lanes_given = 0 ;
00123 args_info->camera_conf_given = 0 ;
00124 args_info->list_file_given = 0 ;
00125 args_info->list_path_given = 0 ;
00126 args_info->image_file_given = 0 ;
00127 args_info->wait_given = 0 ;
00128 args_info->show_given = 0 ;
00129 args_info->step_given = 0 ;
00130 args_info->show_lane_numbers_given = 0 ;
00131 args_info->output_suffix_given = 0 ;
00132 args_info->save_images_given = 0 ;
00133 args_info->save_lanes_given = 0 ;
00134 args_info->debug_given = 0 ;
00135 }
00136
00137 static
00138 void clear_args (struct gengetopt_args_info *args_info)
00139 {
00140 args_info->lanes_conf_arg = gengetopt_strdup ("Lanes.conf");
00141 args_info->lanes_conf_orig = NULL;
00142 args_info->stoplines_conf_arg = gengetopt_strdup ("StopLines.conf");
00143 args_info->stoplines_conf_orig = NULL;
00144 args_info->no_stoplines_flag = 0;
00145 args_info->no_lanes_flag = 0;
00146 args_info->camera_conf_arg = gengetopt_strdup ("CameraInfo.conf");
00147 args_info->camera_conf_orig = NULL;
00148 args_info->list_file_arg = NULL;
00149 args_info->list_file_orig = NULL;
00150 args_info->list_path_arg = gengetopt_strdup ("");
00151 args_info->list_path_orig = NULL;
00152 args_info->image_file_arg = NULL;
00153 args_info->image_file_orig = NULL;
00154 args_info->wait_arg = 0;
00155 args_info->wait_orig = NULL;
00156 args_info->show_flag = 0;
00157 args_info->step_flag = 0;
00158 args_info->show_lane_numbers_flag = 0;
00159 args_info->output_suffix_arg = gengetopt_strdup ("_results");
00160 args_info->output_suffix_orig = NULL;
00161 args_info->save_images_flag = 0;
00162 args_info->save_lanes_flag = 0;
00163 args_info->debug_flag = 0;
00164
00165 }
00166
00167 static
00168 void init_args_info(struct gengetopt_args_info *args_info)
00169 {
00170 args_info->help_help = gengetopt_args_info_help[0] ;
00171 args_info->version_help = gengetopt_args_info_help[1] ;
00172 args_info->lanes_conf_help = gengetopt_args_info_help[3] ;
00173 args_info->stoplines_conf_help = gengetopt_args_info_help[4] ;
00174 args_info->no_stoplines_help = gengetopt_args_info_help[5] ;
00175 args_info->no_lanes_help = gengetopt_args_info_help[6] ;
00176 args_info->camera_conf_help = gengetopt_args_info_help[7] ;
00177 args_info->list_file_help = gengetopt_args_info_help[8] ;
00178 args_info->list_path_help = gengetopt_args_info_help[9] ;
00179 args_info->image_file_help = gengetopt_args_info_help[10] ;
00180 args_info->wait_help = gengetopt_args_info_help[12] ;
00181 args_info->show_help = gengetopt_args_info_help[13] ;
00182 args_info->step_help = gengetopt_args_info_help[14] ;
00183 args_info->show_lane_numbers_help = gengetopt_args_info_help[15] ;
00184 args_info->output_suffix_help = gengetopt_args_info_help[16] ;
00185 args_info->save_images_help = gengetopt_args_info_help[17] ;
00186 args_info->save_lanes_help = gengetopt_args_info_help[18] ;
00187 args_info->debug_help = gengetopt_args_info_help[19] ;
00188
00189 }
00190
00191 void
00192 cmdline_parser_print_version (void)
00193 {
00194 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
00195 }
00196
00197 void
00198 cmdline_parser_print_help (void)
00199 {
00200 int i = 0;
00201 cmdline_parser_print_version ();
00202
00203 if (strlen(gengetopt_args_info_purpose) > 0)
00204 printf("\n%s\n", gengetopt_args_info_purpose);
00205
00206 printf("\n%s\n\n", gengetopt_args_info_usage);
00207 while (gengetopt_args_info_help[i])
00208 printf("%s\n", gengetopt_args_info_help[i++]);
00209 }
00210
00211 void
00212 cmdline_parser_init (struct gengetopt_args_info *args_info)
00213 {
00214 clear_given (args_info);
00215 clear_args (args_info);
00216 init_args_info (args_info);
00217
00218 args_info->inputs = NULL;
00219 args_info->inputs_num = 0;
00220 }
00221
00222 static void
00223 cmdline_parser_release (struct gengetopt_args_info *args_info)
00224 {
00225
00226 unsigned int i;
00227 if (args_info->lanes_conf_arg)
00228 {
00229 free (args_info->lanes_conf_arg);
00230 args_info->lanes_conf_arg = 0;
00231 }
00232 if (args_info->lanes_conf_orig)
00233 {
00234 free (args_info->lanes_conf_orig);
00235 args_info->lanes_conf_orig = 0;
00236 }
00237 if (args_info->stoplines_conf_arg)
00238 {
00239 free (args_info->stoplines_conf_arg);
00240 args_info->stoplines_conf_arg = 0;
00241 }
00242 if (args_info->stoplines_conf_orig)
00243 {
00244 free (args_info->stoplines_conf_orig);
00245 args_info->stoplines_conf_orig = 0;
00246 }
00247 if (args_info->camera_conf_arg)
00248 {
00249 free (args_info->camera_conf_arg);
00250 args_info->camera_conf_arg = 0;
00251 }
00252 if (args_info->camera_conf_orig)
00253 {
00254 free (args_info->camera_conf_orig);
00255 args_info->camera_conf_orig = 0;
00256 }
00257 if (args_info->list_file_arg)
00258 {
00259 free (args_info->list_file_arg);
00260 args_info->list_file_arg = 0;
00261 }
00262 if (args_info->list_file_orig)
00263 {
00264 free (args_info->list_file_orig);
00265 args_info->list_file_orig = 0;
00266 }
00267 if (args_info->list_path_arg)
00268 {
00269 free (args_info->list_path_arg);
00270 args_info->list_path_arg = 0;
00271 }
00272 if (args_info->list_path_orig)
00273 {
00274 free (args_info->list_path_orig);
00275 args_info->list_path_orig = 0;
00276 }
00277 if (args_info->image_file_arg)
00278 {
00279 free (args_info->image_file_arg);
00280 args_info->image_file_arg = 0;
00281 }
00282 if (args_info->image_file_orig)
00283 {
00284 free (args_info->image_file_orig);
00285 args_info->image_file_orig = 0;
00286 }
00287 if (args_info->wait_orig)
00288 {
00289 free (args_info->wait_orig);
00290 args_info->wait_orig = 0;
00291 }
00292 if (args_info->output_suffix_arg)
00293 {
00294 free (args_info->output_suffix_arg);
00295 args_info->output_suffix_arg = 0;
00296 }
00297 if (args_info->output_suffix_orig)
00298 {
00299 free (args_info->output_suffix_orig);
00300 args_info->output_suffix_orig = 0;
00301 }
00302
00303 for (i = 0; i < args_info->inputs_num; ++i)
00304 free (args_info->inputs [i]);
00305
00306 if (args_info->inputs_num)
00307 free (args_info->inputs);
00308
00309 clear_given (args_info);
00310 }
00311
00312 int
00313 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
00314 {
00315 FILE *outfile;
00316 int i = 0;
00317
00318 outfile = fopen(filename, "w");
00319
00320 if (!outfile)
00321 {
00322 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
00323 return EXIT_FAILURE;
00324 }
00325
00326 if (args_info->help_given) {
00327 fprintf(outfile, "%s\n", "help");
00328 }
00329 if (args_info->version_given) {
00330 fprintf(outfile, "%s\n", "version");
00331 }
00332 if (args_info->lanes_conf_given) {
00333 if (args_info->lanes_conf_orig) {
00334 fprintf(outfile, "%s=\"%s\"\n", "lanes-conf", args_info->lanes_conf_orig);
00335 } else {
00336 fprintf(outfile, "%s\n", "lanes-conf");
00337 }
00338 }
00339 if (args_info->stoplines_conf_given) {
00340 if (args_info->stoplines_conf_orig) {
00341 fprintf(outfile, "%s=\"%s\"\n", "stoplines-conf", args_info->stoplines_conf_orig);
00342 } else {
00343 fprintf(outfile, "%s\n", "stoplines-conf");
00344 }
00345 }
00346 if (args_info->no_stoplines_given) {
00347 fprintf(outfile, "%s\n", "no-stoplines");
00348 }
00349 if (args_info->no_lanes_given) {
00350 fprintf(outfile, "%s\n", "no-lanes");
00351 }
00352 if (args_info->camera_conf_given) {
00353 if (args_info->camera_conf_orig) {
00354 fprintf(outfile, "%s=\"%s\"\n", "camera-conf", args_info->camera_conf_orig);
00355 } else {
00356 fprintf(outfile, "%s\n", "camera-conf");
00357 }
00358 }
00359 if (args_info->list_file_given) {
00360 if (args_info->list_file_orig) {
00361 fprintf(outfile, "%s=\"%s\"\n", "list-file", args_info->list_file_orig);
00362 } else {
00363 fprintf(outfile, "%s\n", "list-file");
00364 }
00365 }
00366 if (args_info->list_path_given) {
00367 if (args_info->list_path_orig) {
00368 fprintf(outfile, "%s=\"%s\"\n", "list-path", args_info->list_path_orig);
00369 } else {
00370 fprintf(outfile, "%s\n", "list-path");
00371 }
00372 }
00373 if (args_info->image_file_given) {
00374 if (args_info->image_file_orig) {
00375 fprintf(outfile, "%s=\"%s\"\n", "image-file", args_info->image_file_orig);
00376 } else {
00377 fprintf(outfile, "%s\n", "image-file");
00378 }
00379 }
00380 if (args_info->wait_given) {
00381 if (args_info->wait_orig) {
00382 fprintf(outfile, "%s=\"%s\"\n", "wait", args_info->wait_orig);
00383 } else {
00384 fprintf(outfile, "%s\n", "wait");
00385 }
00386 }
00387 if (args_info->show_given) {
00388 fprintf(outfile, "%s\n", "show");
00389 }
00390 if (args_info->step_given) {
00391 fprintf(outfile, "%s\n", "step");
00392 }
00393 if (args_info->show_lane_numbers_given) {
00394 fprintf(outfile, "%s\n", "show-lane-numbers");
00395 }
00396 if (args_info->output_suffix_given) {
00397 if (args_info->output_suffix_orig) {
00398 fprintf(outfile, "%s=\"%s\"\n", "output-suffix", args_info->output_suffix_orig);
00399 } else {
00400 fprintf(outfile, "%s\n", "output-suffix");
00401 }
00402 }
00403 if (args_info->save_images_given) {
00404 fprintf(outfile, "%s\n", "save-images");
00405 }
00406 if (args_info->save_lanes_given) {
00407 fprintf(outfile, "%s\n", "save-lanes");
00408 }
00409 if (args_info->debug_given) {
00410 fprintf(outfile, "%s\n", "debug");
00411 }
00412
00413 fclose (outfile);
00414
00415 i = EXIT_SUCCESS;
00416 return i;
00417 }
00418
00419 void
00420 cmdline_parser_free (struct gengetopt_args_info *args_info)
00421 {
00422 cmdline_parser_release (args_info);
00423 }
00424
00425
00426
00427
00428 char *
00429 gengetopt_strdup (const char *s)
00430 {
00431 char *result = NULL;
00432 if (!s)
00433 return result;
00434
00435 result = (char*)malloc(strlen(s) + 1);
00436 if (result == (char*)0)
00437 return (char*)0;
00438 strcpy(result, s);
00439 return result;
00440 }
00441
00442 int
00443 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
00444 {
00445 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
00446 }
00447
00448 int
00449 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
00450 {
00451 int result;
00452
00453 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
00454
00455 if (result == EXIT_FAILURE)
00456 {
00457 cmdline_parser_free (args_info);
00458 exit (EXIT_FAILURE);
00459 }
00460
00461 return result;
00462 }
00463
00464 int
00465 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
00466 {
00467 return EXIT_SUCCESS;
00468 }
00469
00470 int
00471 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)
00472 {
00473 int c;
00474
00475 int error = 0;
00476 struct gengetopt_args_info local_args_info;
00477
00478 if (initialize)
00479 cmdline_parser_init (args_info);
00480
00481 cmdline_parser_init (&local_args_info);
00482
00483 optarg = 0;
00484 optind = 0;
00485 opterr = 1;
00486 optopt = '?';
00487
00488 while (1)
00489 {
00490 int option_index = 0;
00491 char *stop_char;
00492
00493 static struct option long_options[] = {
00494 { "help", 0, NULL, 'h' },
00495 { "version", 0, NULL, 'V' },
00496 { "lanes-conf", 1, NULL, 0 },
00497 { "stoplines-conf", 1, NULL, 0 },
00498 { "no-stoplines", 0, NULL, 0 },
00499 { "no-lanes", 0, NULL, 0 },
00500 { "camera-conf", 1, NULL, 0 },
00501 { "list-file", 1, NULL, 0 },
00502 { "list-path", 1, NULL, 0 },
00503 { "image-file", 1, NULL, 0 },
00504 { "wait", 1, NULL, 0 },
00505 { "show", 0, NULL, 0 },
00506 { "step", 0, NULL, 0 },
00507 { "show-lane-numbers", 0, NULL, 0 },
00508 { "output-suffix", 1, NULL, 0 },
00509 { "save-images", 0, NULL, 0 },
00510 { "save-lanes", 0, NULL, 0 },
00511 { "debug", 0, NULL, 0 },
00512 { NULL, 0, NULL, 0 }
00513 };
00514
00515 stop_char = 0;
00516 c = getopt_long (argc, argv, "hV", long_options, &option_index);
00517
00518 if (c == -1) break;
00519
00520 switch (c)
00521 {
00522 case 'h':
00523 cmdline_parser_print_help ();
00524 cmdline_parser_free (&local_args_info);
00525 exit (EXIT_SUCCESS);
00526
00527 case 'V':
00528 cmdline_parser_print_version ();
00529 cmdline_parser_free (&local_args_info);
00530 exit (EXIT_SUCCESS);
00531
00532
00533 case 0:
00534
00535 if (strcmp (long_options[option_index].name, "lanes-conf") == 0)
00536 {
00537 if (local_args_info.lanes_conf_given)
00538 {
00539 fprintf (stderr, "%s: `--lanes-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00540 goto failure;
00541 }
00542 if (args_info->lanes_conf_given && ! override)
00543 continue;
00544 local_args_info.lanes_conf_given = 1;
00545 args_info->lanes_conf_given = 1;
00546 if (args_info->lanes_conf_arg)
00547 free (args_info->lanes_conf_arg);
00548 args_info->lanes_conf_arg = gengetopt_strdup (optarg);
00549 if (args_info->lanes_conf_orig)
00550 free (args_info->lanes_conf_orig);
00551 args_info->lanes_conf_orig = gengetopt_strdup (optarg);
00552 }
00553
00554 else if (strcmp (long_options[option_index].name, "stoplines-conf") == 0)
00555 {
00556 if (local_args_info.stoplines_conf_given)
00557 {
00558 fprintf (stderr, "%s: `--stoplines-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00559 goto failure;
00560 }
00561 if (args_info->stoplines_conf_given && ! override)
00562 continue;
00563 local_args_info.stoplines_conf_given = 1;
00564 args_info->stoplines_conf_given = 1;
00565 if (args_info->stoplines_conf_arg)
00566 free (args_info->stoplines_conf_arg);
00567 args_info->stoplines_conf_arg = gengetopt_strdup (optarg);
00568 if (args_info->stoplines_conf_orig)
00569 free (args_info->stoplines_conf_orig);
00570 args_info->stoplines_conf_orig = gengetopt_strdup (optarg);
00571 }
00572
00573 else if (strcmp (long_options[option_index].name, "no-stoplines") == 0)
00574 {
00575 if (local_args_info.no_stoplines_given)
00576 {
00577 fprintf (stderr, "%s: `--no-stoplines' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00578 goto failure;
00579 }
00580 if (args_info->no_stoplines_given && ! override)
00581 continue;
00582 local_args_info.no_stoplines_given = 1;
00583 args_info->no_stoplines_given = 1;
00584 args_info->no_stoplines_flag = !(args_info->no_stoplines_flag);
00585 }
00586
00587 else if (strcmp (long_options[option_index].name, "no-lanes") == 0)
00588 {
00589 if (local_args_info.no_lanes_given)
00590 {
00591 fprintf (stderr, "%s: `--no-lanes' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00592 goto failure;
00593 }
00594 if (args_info->no_lanes_given && ! override)
00595 continue;
00596 local_args_info.no_lanes_given = 1;
00597 args_info->no_lanes_given = 1;
00598 args_info->no_lanes_flag = !(args_info->no_lanes_flag);
00599 }
00600
00601 else if (strcmp (long_options[option_index].name, "camera-conf") == 0)
00602 {
00603 if (local_args_info.camera_conf_given)
00604 {
00605 fprintf (stderr, "%s: `--camera-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00606 goto failure;
00607 }
00608 if (args_info->camera_conf_given && ! override)
00609 continue;
00610 local_args_info.camera_conf_given = 1;
00611 args_info->camera_conf_given = 1;
00612 if (args_info->camera_conf_arg)
00613 free (args_info->camera_conf_arg);
00614 args_info->camera_conf_arg = gengetopt_strdup (optarg);
00615 if (args_info->camera_conf_orig)
00616 free (args_info->camera_conf_orig);
00617 args_info->camera_conf_orig = gengetopt_strdup (optarg);
00618 }
00619
00620 else if (strcmp (long_options[option_index].name, "list-file") == 0)
00621 {
00622 if (local_args_info.list_file_given)
00623 {
00624 fprintf (stderr, "%s: `--list-file' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00625 goto failure;
00626 }
00627 if (args_info->list_file_given && ! override)
00628 continue;
00629 local_args_info.list_file_given = 1;
00630 args_info->list_file_given = 1;
00631 if (args_info->list_file_arg)
00632 free (args_info->list_file_arg);
00633 args_info->list_file_arg = gengetopt_strdup (optarg);
00634 if (args_info->list_file_orig)
00635 free (args_info->list_file_orig);
00636 args_info->list_file_orig = gengetopt_strdup (optarg);
00637 }
00638
00639 else if (strcmp (long_options[option_index].name, "list-path") == 0)
00640 {
00641 if (local_args_info.list_path_given)
00642 {
00643 fprintf (stderr, "%s: `--list-path' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00644 goto failure;
00645 }
00646 if (args_info->list_path_given && ! override)
00647 continue;
00648 local_args_info.list_path_given = 1;
00649 args_info->list_path_given = 1;
00650 if (args_info->list_path_arg)
00651 free (args_info->list_path_arg);
00652 args_info->list_path_arg = gengetopt_strdup (optarg);
00653 if (args_info->list_path_orig)
00654 free (args_info->list_path_orig);
00655 args_info->list_path_orig = gengetopt_strdup (optarg);
00656 }
00657
00658 else if (strcmp (long_options[option_index].name, "image-file") == 0)
00659 {
00660 if (local_args_info.image_file_given)
00661 {
00662 fprintf (stderr, "%s: `--image-file' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00663 goto failure;
00664 }
00665 if (args_info->image_file_given && ! override)
00666 continue;
00667 local_args_info.image_file_given = 1;
00668 args_info->image_file_given = 1;
00669 if (args_info->image_file_arg)
00670 free (args_info->image_file_arg);
00671 args_info->image_file_arg = gengetopt_strdup (optarg);
00672 if (args_info->image_file_orig)
00673 free (args_info->image_file_orig);
00674 args_info->image_file_orig = gengetopt_strdup (optarg);
00675 }
00676
00677 else if (strcmp (long_options[option_index].name, "wait") == 0)
00678 {
00679 if (local_args_info.wait_given)
00680 {
00681 fprintf (stderr, "%s: `--wait' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00682 goto failure;
00683 }
00684 if (args_info->wait_given && ! override)
00685 continue;
00686 local_args_info.wait_given = 1;
00687 args_info->wait_given = 1;
00688 args_info->wait_arg = strtol (optarg, &stop_char, 0);
00689 if (!(stop_char && *stop_char == '\0')) {
00690 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
00691 goto failure;
00692 }
00693 if (args_info->wait_orig)
00694 free (args_info->wait_orig);
00695 args_info->wait_orig = gengetopt_strdup (optarg);
00696 }
00697
00698 else if (strcmp (long_options[option_index].name, "show") == 0)
00699 {
00700 if (local_args_info.show_given)
00701 {
00702 fprintf (stderr, "%s: `--show' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00703 goto failure;
00704 }
00705 if (args_info->show_given && ! override)
00706 continue;
00707 local_args_info.show_given = 1;
00708 args_info->show_given = 1;
00709 args_info->show_flag = !(args_info->show_flag);
00710 }
00711
00712 else if (strcmp (long_options[option_index].name, "step") == 0)
00713 {
00714 if (local_args_info.step_given)
00715 {
00716 fprintf (stderr, "%s: `--step' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00717 goto failure;
00718 }
00719 if (args_info->step_given && ! override)
00720 continue;
00721 local_args_info.step_given = 1;
00722 args_info->step_given = 1;
00723 args_info->step_flag = !(args_info->step_flag);
00724 }
00725
00726 else if (strcmp (long_options[option_index].name, "show-lane-numbers") == 0)
00727 {
00728 if (local_args_info.show_lane_numbers_given)
00729 {
00730 fprintf (stderr, "%s: `--show-lane-numbers' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00731 goto failure;
00732 }
00733 if (args_info->show_lane_numbers_given && ! override)
00734 continue;
00735 local_args_info.show_lane_numbers_given = 1;
00736 args_info->show_lane_numbers_given = 1;
00737 args_info->show_lane_numbers_flag = !(args_info->show_lane_numbers_flag);
00738 }
00739
00740 else if (strcmp (long_options[option_index].name, "output-suffix") == 0)
00741 {
00742 if (local_args_info.output_suffix_given)
00743 {
00744 fprintf (stderr, "%s: `--output-suffix' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00745 goto failure;
00746 }
00747 if (args_info->output_suffix_given && ! override)
00748 continue;
00749 local_args_info.output_suffix_given = 1;
00750 args_info->output_suffix_given = 1;
00751 if (args_info->output_suffix_arg)
00752 free (args_info->output_suffix_arg);
00753 args_info->output_suffix_arg = gengetopt_strdup (optarg);
00754 if (args_info->output_suffix_orig)
00755 free (args_info->output_suffix_orig);
00756 args_info->output_suffix_orig = gengetopt_strdup (optarg);
00757 }
00758
00759 else if (strcmp (long_options[option_index].name, "save-images") == 0)
00760 {
00761 if (local_args_info.save_images_given)
00762 {
00763 fprintf (stderr, "%s: `--save-images' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00764 goto failure;
00765 }
00766 if (args_info->save_images_given && ! override)
00767 continue;
00768 local_args_info.save_images_given = 1;
00769 args_info->save_images_given = 1;
00770 args_info->save_images_flag = !(args_info->save_images_flag);
00771 }
00772
00773 else if (strcmp (long_options[option_index].name, "save-lanes") == 0)
00774 {
00775 if (local_args_info.save_lanes_given)
00776 {
00777 fprintf (stderr, "%s: `--save-lanes' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00778 goto failure;
00779 }
00780 if (args_info->save_lanes_given && ! override)
00781 continue;
00782 local_args_info.save_lanes_given = 1;
00783 args_info->save_lanes_given = 1;
00784 args_info->save_lanes_flag = !(args_info->save_lanes_flag);
00785 }
00786
00787 else if (strcmp (long_options[option_index].name, "debug") == 0)
00788 {
00789 if (local_args_info.debug_given)
00790 {
00791 fprintf (stderr, "%s: `--debug' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
00792 goto failure;
00793 }
00794 if (args_info->debug_given && ! override)
00795 continue;
00796 local_args_info.debug_given = 1;
00797 args_info->debug_given = 1;
00798 args_info->debug_flag = !(args_info->debug_flag);
00799 }
00800
00801 break;
00802 case '?':
00803
00804 goto failure;
00805
00806 default:
00807 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
00808 abort ();
00809 }
00810 }
00811
00812
00813
00814
00815 cmdline_parser_release (&local_args_info);
00816
00817 if ( error )
00818 return (EXIT_FAILURE);
00819
00820 if (optind < argc)
00821 {
00822 int i = 0 ;
00823 int found_prog_name = 0;
00824
00825
00826
00827
00828 i = optind;
00829 while (i < argc)
00830 if (argv[i++] == argv[0]) {
00831 found_prog_name = 1;
00832 break;
00833 }
00834 i = 0;
00835
00836 args_info->inputs_num = argc - optind - found_prog_name;
00837 args_info->inputs =
00838 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
00839 while (optind < argc)
00840 if (argv[optind++] != argv[0])
00841 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
00842 }
00843
00844 return 0;
00845
00846 failure:
00847
00848 cmdline_parser_release (&local_args_info);
00849 return (EXIT_FAILURE);
00850 }
00851
00852 #ifndef CONFIG_FILE_LINE_SIZE
00853 #define CONFIG_FILE_LINE_SIZE 2048
00854 #endif
00855 #define ADDITIONAL_ERROR " in configuration file "
00856
00857 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
00858
00859
00860 char my2_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
00861
00862 int
00863 cmdline_parser_configfile (char * const filename, struct gengetopt_args_info
00864 *args_info, int override, int initialize, int check_required)
00865 {
00866 FILE* file;
00867 char linebuf[CONFIG_FILE_LINE_SIZE];
00868 int line_num = 0;
00869 int i, result, equal;
00870 char *fopt, *farg;
00871 char *str_index;
00872 size_t len, next_token;
00873 char delimiter;
00874 int my_argc = 0;
00875 char **my2_argv_arg;
00876 char *additional_error;
00877
00878
00879 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
00880 cmd_line_list_tmp->next = cmd_line_list;
00881 cmd_line_list = cmd_line_list_tmp;
00882 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
00883
00884 if ((file = fopen(filename, "r")) == NULL)
00885 {
00886 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
00887 CMDLINE_PARSER_PACKAGE, filename);
00888 result = EXIT_FAILURE;
00889 goto conf_failure;
00890 }
00891
00892 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
00893 {
00894 ++line_num;
00895 my2_argv[0] = '\0';
00896 len = strlen(linebuf);
00897 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
00898 {
00899 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
00900 CMDLINE_PARSER_PACKAGE, filename, line_num);
00901 result = EXIT_FAILURE;
00902 goto conf_failure;
00903 }
00904
00905
00906 next_token = strspn ( linebuf, " \t\r\n");
00907 str_index = linebuf + next_token;
00908
00909 if ( str_index[0] == '\0' || str_index[0] == '#')
00910 continue;
00911
00912 fopt = str_index;
00913
00914
00915 next_token = strcspn (fopt, " \t\r\n=");
00916
00917 if (fopt[next_token] == '\0')
00918 {
00919 farg = NULL;
00920 equal = 0;
00921 goto noarg;
00922 }
00923
00924
00925 equal = (fopt[next_token] == '=');
00926 fopt[next_token++] = '\0';
00927
00928
00929 next_token += strspn (fopt + next_token, " \t\r\n");
00930
00931 if ( !equal )
00932 if ((equal = (fopt[next_token] == '=')))
00933 {
00934 next_token++;
00935 next_token += strspn (fopt + next_token, " \t\r\n");
00936 }
00937 str_index += next_token;
00938
00939
00940 farg = str_index;
00941 if ( farg[0] == '\"' || farg[0] == '\'' )
00942 {
00943 str_index = strchr (++farg, str_index[0] );
00944 if (! str_index)
00945 {
00946 fprintf
00947 (stderr,
00948 "%s:%s:%d: unterminated string in configuration file\n",
00949 CMDLINE_PARSER_PACKAGE, filename, line_num);
00950 result = EXIT_FAILURE;
00951 goto conf_failure;
00952 }
00953 }
00954 else
00955 {
00956 next_token = strcspn (farg, " \t\r\n#\'\"");
00957 str_index += next_token;
00958 }
00959
00960
00961 delimiter = *str_index, *str_index++ = '\0';
00962
00963
00964 if (delimiter != '\0' && delimiter != '#')
00965 {
00966 str_index += strspn(str_index, " \t\r\n");
00967 if (*str_index != '\0' && *str_index != '#')
00968 {
00969 fprintf
00970 (stderr,
00971 "%s:%s:%d: malformed string in configuration file\n",
00972 CMDLINE_PARSER_PACKAGE, filename, line_num);
00973 result = EXIT_FAILURE;
00974 goto conf_failure;
00975 }
00976 }
00977
00978 noarg:
00979 ++my_argc;
00980 len = strlen(fopt);
00981
00982 strcat (my2_argv, len > 1 ? "--" : "-");
00983 strcat (my2_argv, fopt);
00984 if (len > 1 && ((farg &&*farg) || equal))
00985 strcat (my2_argv, "=");
00986 if (farg && *farg)
00987 strcat (my2_argv, farg);
00988
00989 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct
00990 line_list));
00991 cmd_line_list_tmp->next = cmd_line_list;
00992 cmd_line_list = cmd_line_list_tmp;
00993 cmd_line_list->string_arg = gengetopt_strdup(my2_argv);
00994 }
00995
00996 ++my_argc;
00997 my2_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
00998 cmd_line_list_tmp = cmd_line_list;
00999 for (i = my_argc - 1; i >= 0; --i) {
01000 my2_argv_arg[i] = cmd_line_list_tmp->string_arg;
01001 cmd_line_list_tmp = cmd_line_list_tmp->next;
01002 }
01003 my2_argv_arg[my_argc] = 0;
01004
01005 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR)
01006 + 1);
01007 strcpy (additional_error, ADDITIONAL_ERROR);
01008 strcat (additional_error, filename);
01009 result =
01010 cmdline_parser_internal (my_argc, my2_argv_arg, args_info, override,
01011 initialize, check_required, additional_error);
01012
01013 free (additional_error);
01014 free (my2_argv_arg);
01015
01016 conf_failure:
01017 if (file)
01018 fclose(file);
01019
01020 free_cmd_list();
01021 if (result == EXIT_FAILURE)
01022 {
01023 cmdline_parser_free (args_info);
01024 exit (EXIT_FAILURE);
01025 }
01026
01027 return result;
01028 }