Set release details for 0.2.13
[quvi.git] / src / cmdline.c
blobee204550f472d16ae17fa9a0428a0d8e6cb020a2
1 /*
2 File autogenerated by gengetopt version 2.22.4
3 generated with the following command:
4 /usr/bin/gengetopt -C --unamed-opts=URL --output-dir ../src --no-version
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 */
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
24 #include <getopt.h>
26 #include "cmdline.h"
28 const char *gengetopt_args_info_purpose = "";
30 const char *gengetopt_args_info_usage = "Usage: quvi [OPTIONS]... [URL]...";
32 const char *gengetopt_args_info_description = "";
34 const char *gengetopt_args_info_help[] =
36 " -h, --help Print help and exit",
37 " --version Print version and exit",
38 " --license Print license and exit",
39 " --support Print supported websites and exit",
40 " --xml Print details in XML",
41 " --old Print details in original format",
42 " -q, --quiet Turn off output to stderr",
43 " --verbose-libcurl Turn on libcurl verbose mode",
44 " --exec=arg Invoke arg after parsing",
45 " -s, --no-shortened Do not decompress shortened URLs",
46 " -n, --no-verify Do not verify video link",
47 " --category-http Category HTTP website scripts",
48 " --category-mms Category MMS website scripts",
49 " --category-rtsp Category RTSP website scripts",
50 " --category-rtmp Category RTMP website scripts",
51 " --category-all All website script categories",
52 " --page-title=arg Check that parsed page title matches arg",
53 " --video-id=arg Check that parsed video ID matches arg",
54 " --file-length=arg Check that parsed video length matches arg",
55 " --file-suffix=arg Check that parsed video suffix matches arg",
56 " --test-all Run all built-in tests of category QUVIPROTO_HTTP",
57 " --dump Dump video details with --test-all",
58 " -t, --test=arg Pattern to match to built-in test URLs",
59 " -f, --format=arg Video format to query (default=`default')",
60 " --agent=arg Identify as arg (default=`Mozilla/5.0')",
61 " --proxy=arg Use proxy for HTTP connections",
62 " --no-proxy Disable use of HTTP proxy",
63 " --connect-timeout=arg Seconds connecting allowed to take (default=`30')",
64 "\nExamples:\n quvi YOUTUBE_URL -f webm_720p\n quvi URL --exec \"/usr/bin/vlc %u\"\n quvi --support\n quvi --support URL",
68 typedef enum { ARG_NO, ARG_STRING, ARG_INT, ARG_DOUBLE
69 } cmdline_parser_arg_type;
71 static
72 void clear_given(struct gengetopt_args_info *args_info);
73 static
74 void clear_args(struct gengetopt_args_info *args_info);
76 static int
77 cmdline_parser_internal(int argc, char **argv,
78 struct gengetopt_args_info *args_info,
79 struct cmdline_parser_params *params,
80 const char *additional_error);
82 static int
83 cmdline_parser_required2(struct gengetopt_args_info *args_info,
84 const char *prog_name, const char *additional_error);
85 struct line_list
87 char *string_arg;
88 struct line_list *next;
91 static struct line_list *cmd_line_list = 0;
92 static struct line_list *cmd_line_list_tmp = 0;
94 static void free_cmd_list(void)
96 /* free the list of a previous call */
97 if (cmd_line_list)
99 while (cmd_line_list)
101 cmd_line_list_tmp = cmd_line_list;
102 cmd_line_list = cmd_line_list->next;
103 free(cmd_line_list_tmp->string_arg);
104 free(cmd_line_list_tmp);
109 static char *gengetopt_strdup(const char *s);
111 static
112 void clear_given(struct gengetopt_args_info *args_info)
114 args_info->help_given = 0;
115 args_info->version_given = 0;
116 args_info->license_given = 0;
117 args_info->support_given = 0;
118 args_info->xml_given = 0;
119 args_info->old_given = 0;
120 args_info->quiet_given = 0;
121 args_info->verbose_libcurl_given = 0;
122 args_info->exec_given = 0;
123 args_info->no_shortened_given = 0;
124 args_info->no_verify_given = 0;
125 args_info->category_http_given = 0;
126 args_info->category_mms_given = 0;
127 args_info->category_rtsp_given = 0;
128 args_info->category_rtmp_given = 0;
129 args_info->category_all_given = 0;
130 args_info->page_title_given = 0;
131 args_info->video_id_given = 0;
132 args_info->file_length_given = 0;
133 args_info->file_suffix_given = 0;
134 args_info->test_all_given = 0;
135 args_info->dump_given = 0;
136 args_info->test_given = 0;
137 args_info->format_given = 0;
138 args_info->agent_given = 0;
139 args_info->proxy_given = 0;
140 args_info->no_proxy_given = 0;
141 args_info->connect_timeout_given = 0;
144 static
145 void clear_args(struct gengetopt_args_info *args_info)
147 FIX_UNUSED(args_info);
148 args_info->exec_arg = NULL;
149 args_info->exec_orig = NULL;
150 args_info->page_title_arg = NULL;
151 args_info->page_title_orig = NULL;
152 args_info->video_id_arg = NULL;
153 args_info->video_id_orig = NULL;
154 args_info->file_length_orig = NULL;
155 args_info->file_suffix_arg = NULL;
156 args_info->file_suffix_orig = NULL;
157 args_info->test_arg = NULL;
158 args_info->test_orig = NULL;
159 args_info->format_arg = gengetopt_strdup("default");
160 args_info->format_orig = NULL;
161 args_info->agent_arg = gengetopt_strdup("Mozilla/5.0");
162 args_info->agent_orig = NULL;
163 args_info->proxy_arg = NULL;
164 args_info->proxy_orig = NULL;
165 args_info->connect_timeout_arg = 30;
166 args_info->connect_timeout_orig = NULL;
170 static
171 void init_args_info(struct gengetopt_args_info *args_info)
174 args_info->help_help = gengetopt_args_info_help[0];
175 args_info->version_help = gengetopt_args_info_help[1];
176 args_info->license_help = gengetopt_args_info_help[2];
177 args_info->support_help = gengetopt_args_info_help[3];
178 args_info->xml_help = gengetopt_args_info_help[4];
179 args_info->old_help = gengetopt_args_info_help[5];
180 args_info->quiet_help = gengetopt_args_info_help[6];
181 args_info->verbose_libcurl_help = gengetopt_args_info_help[7];
182 args_info->exec_help = gengetopt_args_info_help[8];
183 args_info->no_shortened_help = gengetopt_args_info_help[9];
184 args_info->no_verify_help = gengetopt_args_info_help[10];
185 args_info->category_http_help = gengetopt_args_info_help[11];
186 args_info->category_mms_help = gengetopt_args_info_help[12];
187 args_info->category_rtsp_help = gengetopt_args_info_help[13];
188 args_info->category_rtmp_help = gengetopt_args_info_help[14];
189 args_info->category_all_help = gengetopt_args_info_help[15];
190 args_info->page_title_help = gengetopt_args_info_help[16];
191 args_info->video_id_help = gengetopt_args_info_help[17];
192 args_info->file_length_help = gengetopt_args_info_help[18];
193 args_info->file_suffix_help = gengetopt_args_info_help[19];
194 args_info->test_all_help = gengetopt_args_info_help[20];
195 args_info->dump_help = gengetopt_args_info_help[21];
196 args_info->test_help = gengetopt_args_info_help[22];
197 args_info->format_help = gengetopt_args_info_help[23];
198 args_info->agent_help = gengetopt_args_info_help[24];
199 args_info->proxy_help = gengetopt_args_info_help[25];
200 args_info->no_proxy_help = gengetopt_args_info_help[26];
201 args_info->connect_timeout_help = gengetopt_args_info_help[27];
205 void cmdline_parser_print_version(void)
207 printf("%s %s\n",
208 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME :
209 CMDLINE_PARSER_PACKAGE), CMDLINE_PARSER_VERSION);
212 static void print_help_common(void)
214 cmdline_parser_print_version();
216 if (strlen(gengetopt_args_info_purpose) > 0)
217 printf("\n%s\n", gengetopt_args_info_purpose);
219 if (strlen(gengetopt_args_info_usage) > 0)
220 printf("\n%s\n", gengetopt_args_info_usage);
222 printf("\n");
224 if (strlen(gengetopt_args_info_description) > 0)
225 printf("%s\n\n", gengetopt_args_info_description);
228 void cmdline_parser_print_help(void)
230 int i = 0;
231 print_help_common();
232 while (gengetopt_args_info_help[i])
233 printf("%s\n", gengetopt_args_info_help[i++]);
236 void cmdline_parser_init(struct gengetopt_args_info *args_info)
238 clear_given(args_info);
239 clear_args(args_info);
240 init_args_info(args_info);
242 args_info->inputs = 0;
243 args_info->inputs_num = 0;
246 void cmdline_parser_params_init(struct cmdline_parser_params *params)
248 if (params)
250 params->override = 0;
251 params->initialize = 1;
252 params->check_required = 1;
253 params->check_ambiguity = 0;
254 params->print_errors = 1;
258 struct cmdline_parser_params *cmdline_parser_params_create(void)
260 struct cmdline_parser_params *params = (struct cmdline_parser_params *)
261 malloc(sizeof(struct cmdline_parser_params));
262 cmdline_parser_params_init(params);
263 return params;
266 static void free_string_field(char **s)
268 if (*s)
270 free(*s);
271 *s = 0;
275 static void cmdline_parser_release(struct gengetopt_args_info *args_info)
277 unsigned int i;
278 free_string_field(&(args_info->exec_arg));
279 free_string_field(&(args_info->exec_orig));
280 free_string_field(&(args_info->page_title_arg));
281 free_string_field(&(args_info->page_title_orig));
282 free_string_field(&(args_info->video_id_arg));
283 free_string_field(&(args_info->video_id_orig));
284 free_string_field(&(args_info->file_length_orig));
285 free_string_field(&(args_info->file_suffix_arg));
286 free_string_field(&(args_info->file_suffix_orig));
287 free_string_field(&(args_info->test_arg));
288 free_string_field(&(args_info->test_orig));
289 free_string_field(&(args_info->format_arg));
290 free_string_field(&(args_info->format_orig));
291 free_string_field(&(args_info->agent_arg));
292 free_string_field(&(args_info->agent_orig));
293 free_string_field(&(args_info->proxy_arg));
294 free_string_field(&(args_info->proxy_orig));
295 free_string_field(&(args_info->connect_timeout_orig));
297 for (i = 0; i < args_info->inputs_num; ++i)
298 free(args_info->inputs[i]);
300 if (args_info->inputs_num)
301 free(args_info->inputs);
303 clear_given(args_info);
306 static void
307 write_into_file(FILE * outfile, const char *opt, const char *arg,
308 const char *values[])
310 FIX_UNUSED(values);
311 if (arg)
313 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
315 else
317 fprintf(outfile, "%s\n", opt);
321 int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
323 int i = 0;
325 if (!outfile)
327 fprintf(stderr, "%s: cannot dump options to stream\n",
328 CMDLINE_PARSER_PACKAGE);
329 return EXIT_FAILURE;
332 if (args_info->help_given)
333 write_into_file(outfile, "help", 0, 0);
334 if (args_info->version_given)
335 write_into_file(outfile, "version", 0, 0);
336 if (args_info->license_given)
337 write_into_file(outfile, "license", 0, 0);
338 if (args_info->support_given)
339 write_into_file(outfile, "support", 0, 0);
340 if (args_info->xml_given)
341 write_into_file(outfile, "xml", 0, 0);
342 if (args_info->old_given)
343 write_into_file(outfile, "old", 0, 0);
344 if (args_info->quiet_given)
345 write_into_file(outfile, "quiet", 0, 0);
346 if (args_info->verbose_libcurl_given)
347 write_into_file(outfile, "verbose-libcurl", 0, 0);
348 if (args_info->exec_given)
349 write_into_file(outfile, "exec", args_info->exec_orig, 0);
350 if (args_info->no_shortened_given)
351 write_into_file(outfile, "no-shortened", 0, 0);
352 if (args_info->no_verify_given)
353 write_into_file(outfile, "no-verify", 0, 0);
354 if (args_info->category_http_given)
355 write_into_file(outfile, "category-http", 0, 0);
356 if (args_info->category_mms_given)
357 write_into_file(outfile, "category-mms", 0, 0);
358 if (args_info->category_rtsp_given)
359 write_into_file(outfile, "category-rtsp", 0, 0);
360 if (args_info->category_rtmp_given)
361 write_into_file(outfile, "category-rtmp", 0, 0);
362 if (args_info->category_all_given)
363 write_into_file(outfile, "category-all", 0, 0);
364 if (args_info->page_title_given)
365 write_into_file(outfile, "page-title", args_info->page_title_orig, 0);
366 if (args_info->video_id_given)
367 write_into_file(outfile, "video-id", args_info->video_id_orig, 0);
368 if (args_info->file_length_given)
369 write_into_file(outfile, "file-length", args_info->file_length_orig, 0);
370 if (args_info->file_suffix_given)
371 write_into_file(outfile, "file-suffix", args_info->file_suffix_orig, 0);
372 if (args_info->test_all_given)
373 write_into_file(outfile, "test-all", 0, 0);
374 if (args_info->dump_given)
375 write_into_file(outfile, "dump", 0, 0);
376 if (args_info->test_given)
377 write_into_file(outfile, "test", args_info->test_orig, 0);
378 if (args_info->format_given)
379 write_into_file(outfile, "format", args_info->format_orig, 0);
380 if (args_info->agent_given)
381 write_into_file(outfile, "agent", args_info->agent_orig, 0);
382 if (args_info->proxy_given)
383 write_into_file(outfile, "proxy", args_info->proxy_orig, 0);
384 if (args_info->no_proxy_given)
385 write_into_file(outfile, "no-proxy", 0, 0);
386 if (args_info->connect_timeout_given)
387 write_into_file(outfile, "connect-timeout", args_info->connect_timeout_orig,
390 i = EXIT_SUCCESS;
391 return i;
395 cmdline_parser_file_save(const char *filename,
396 struct gengetopt_args_info *args_info)
398 FILE *outfile;
399 int i = 0;
401 outfile = fopen(filename, "w");
403 if (!outfile)
405 fprintf(stderr, "%s: cannot open file for writing: %s\n",
406 CMDLINE_PARSER_PACKAGE, filename);
407 return EXIT_FAILURE;
410 i = cmdline_parser_dump(outfile, args_info);
411 fclose(outfile);
413 return i;
416 void cmdline_parser_free(struct gengetopt_args_info *args_info)
418 cmdline_parser_release(args_info);
421 /** @brief replacement of strdup, which is not standard */
422 char *gengetopt_strdup(const char *s)
424 char *result = 0;
425 if (!s)
426 return result;
428 result = (char *)malloc(strlen(s) + 1);
429 if (result == (char *)0)
430 return (char *)0;
431 strcpy(result, s);
432 return result;
435 int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
437 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
441 cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
442 struct cmdline_parser_params *params)
444 int result;
445 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
447 if (result == EXIT_FAILURE)
449 cmdline_parser_free(args_info);
450 exit(EXIT_FAILURE);
453 return result;
457 cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
458 int override, int initialize, int check_required)
460 int result;
461 struct cmdline_parser_params params;
463 params.override = override;
464 params.initialize = initialize;
465 params.check_required = check_required;
466 params.check_ambiguity = 0;
467 params.print_errors = 1;
469 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
471 if (result == EXIT_FAILURE)
473 cmdline_parser_free(args_info);
474 exit(EXIT_FAILURE);
477 return result;
481 cmdline_parser_required(struct gengetopt_args_info *args_info,
482 const char *prog_name)
484 int result = EXIT_SUCCESS;
486 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
487 result = EXIT_FAILURE;
489 if (result == EXIT_FAILURE)
491 cmdline_parser_free(args_info);
492 exit(EXIT_FAILURE);
495 return result;
499 cmdline_parser_required2(struct gengetopt_args_info *args_info,
500 const char *prog_name, const char *additional_error)
502 int error = 0;
503 FIX_UNUSED(additional_error);
505 /* checks for required options */
507 /* checks for dependences among options */
508 if (args_info->dump_given && !args_info->test_all_given)
510 fprintf(stderr, "%s: '--dump' option depends on option 'test-all'%s\n",
511 prog_name, (additional_error ? additional_error : ""));
512 error = 1;
515 return error;
518 static char *package_name = 0;
521 * @brief updates an option
522 * @param field the generic pointer to the field to update
523 * @param orig_field the pointer to the orig field
524 * @param field_given the pointer to the number of occurrence of this option
525 * @param prev_given the pointer to the number of occurrence already seen
526 * @param value the argument for this option (if null no arg was specified)
527 * @param possible_values the possible values for this option (if specified)
528 * @param default_value the default value (in case the option only accepts fixed values)
529 * @param arg_type the type of this option
530 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
531 * @param override @see cmdline_parser_params.override
532 * @param no_free whether to free a possible previous value
533 * @param multiple_option whether this is a multiple option
534 * @param long_opt the corresponding long option
535 * @param short_opt the corresponding short option (or '-' if none)
536 * @param additional_error possible further error specification
538 static
539 int update_arg(void *field, char **orig_field,
540 unsigned int *field_given, unsigned int *prev_given,
541 char *value, const char *possible_values[],
542 const char *default_value,
543 cmdline_parser_arg_type arg_type,
544 int check_ambiguity, int override,
545 int no_free, int multiple_option,
546 const char *long_opt, char short_opt,
547 const char *additional_error)
549 char *stop_char = 0;
550 const char *val = value;
551 int found;
552 char **string_field;
553 FIX_UNUSED(field);
555 stop_char = 0;
556 found = 0;
558 if (!multiple_option && prev_given
559 && (*prev_given || (check_ambiguity && *field_given)))
561 if (short_opt != '-')
562 fprintf(stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
563 package_name, long_opt, short_opt,
564 (additional_error ? additional_error : ""));
565 else
566 fprintf(stderr, "%s: `--%s' option given more than once%s\n",
567 package_name, long_opt,
568 (additional_error ? additional_error : ""));
569 return 1; /* failure */
572 FIX_UNUSED(default_value);
574 if (field_given && *field_given && !override)
575 return 0;
576 if (prev_given)
577 (*prev_given)++;
578 if (field_given)
579 (*field_given)++;
580 if (possible_values)
581 val = possible_values[found];
583 switch (arg_type)
585 case ARG_INT:
586 if (val)
587 *((int *)field) = strtol(val, &stop_char, 0);
588 break;
589 case ARG_DOUBLE:
590 if (val)
591 *((double *)field) = strtod(val, &stop_char);
592 break;
593 case ARG_STRING:
594 if (val)
596 string_field = (char **)field;
597 if (!no_free && *string_field)
598 free(*string_field); /* free previous string */
599 *string_field = gengetopt_strdup(val);
601 break;
602 default:
603 break;
606 /* check numeric conversion */
607 switch (arg_type)
609 case ARG_INT:
610 case ARG_DOUBLE:
611 if (val && !(stop_char && *stop_char == '\0'))
613 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
614 return 1; /* failure */
616 break;
617 default:
621 /* store the original value */
622 switch (arg_type)
624 case ARG_NO:
625 break;
626 default:
627 if (value && orig_field)
629 if (no_free)
631 *orig_field = value;
633 else
635 if (*orig_field)
636 free(*orig_field); /* free previous string */
637 *orig_field = gengetopt_strdup(value);
642 return 0; /* OK */
646 cmdline_parser_internal(int argc, char **argv,
647 struct gengetopt_args_info *args_info,
648 struct cmdline_parser_params *params,
649 const char *additional_error)
651 int c; /* Character of the parsed option. */
653 int error = 0;
654 struct gengetopt_args_info local_args_info;
656 int override;
657 int initialize;
658 int check_required;
659 int check_ambiguity;
661 package_name = argv[0];
663 override = params->override;
664 initialize = params->initialize;
665 check_required = params->check_required;
666 check_ambiguity = params->check_ambiguity;
668 if (initialize)
669 cmdline_parser_init(args_info);
671 cmdline_parser_init(&local_args_info);
673 optarg = 0;
674 optind = 0;
675 opterr = params->print_errors;
676 optopt = '?';
678 while (1)
680 int option_index = 0;
682 static struct option long_options[] =
684 {"help", 0, NULL, 'h'},
685 {"version", 0, NULL, 0},
686 {"license", 0, NULL, 0},
687 {"support", 0, NULL, 0},
688 {"xml", 0, NULL, 0},
689 {"old", 0, NULL, 0},
690 {"quiet", 0, NULL, 'q'},
691 {"verbose-libcurl", 0, NULL, 0},
692 {"exec", 1, NULL, 0},
693 {"no-shortened", 0, NULL, 's'},
694 {"no-verify", 0, NULL, 'n'},
695 {"category-http", 0, NULL, 0},
696 {"category-mms", 0, NULL, 0},
697 {"category-rtsp", 0, NULL, 0},
698 {"category-rtmp", 0, NULL, 0},
699 {"category-all", 0, NULL, 0},
700 {"page-title", 1, NULL, 0},
701 {"video-id", 1, NULL, 0},
702 {"file-length", 1, NULL, 0},
703 {"file-suffix", 1, NULL, 0},
704 {"test-all", 0, NULL, 0},
705 {"dump", 0, NULL, 0},
706 {"test", 1, NULL, 't'},
707 {"format", 1, NULL, 'f'},
708 {"agent", 1, NULL, 0},
709 {"proxy", 1, NULL, 0},
710 {"no-proxy", 0, NULL, 0},
711 {"connect-timeout", 1, NULL, 0},
712 {0, 0, 0, 0}
715 c = getopt_long(argc, argv, "hqsnt:f:", long_options, &option_index);
717 if (c == -1)
718 break; /* Exit from `while (1)' loop. */
720 switch (c)
722 case 'h': /* Print help and exit. */
723 cmdline_parser_print_help();
724 cmdline_parser_free(&local_args_info);
725 exit(EXIT_SUCCESS);
727 case 'q': /* Turn off output to stderr. */
729 if (update_arg(0,
730 0, &(args_info->quiet_given),
731 &(local_args_info.quiet_given), optarg, 0, 0, ARG_NO,
732 check_ambiguity, override, 0, 0,
733 "quiet", 'q', additional_error))
734 goto failure;
736 break;
737 case 's': /* Do not decompress shortened URLs. */
739 if (update_arg(0,
740 0, &(args_info->no_shortened_given),
741 &(local_args_info.no_shortened_given), optarg, 0, 0,
742 ARG_NO, check_ambiguity, override, 0, 0, "no-shortened",
743 's', additional_error))
744 goto failure;
746 break;
747 case 'n': /* Do not verify video link. */
749 if (update_arg(0,
750 0, &(args_info->no_verify_given),
751 &(local_args_info.no_verify_given), optarg, 0, 0, ARG_NO,
752 check_ambiguity, override, 0, 0,
753 "no-verify", 'n', additional_error))
754 goto failure;
756 break;
757 case 't': /* Pattern to match to built-in test URLs. */
759 if (update_arg((void *)&(args_info->test_arg),
760 &(args_info->test_orig), &(args_info->test_given),
761 &(local_args_info.test_given), optarg, 0, 0, ARG_STRING,
762 check_ambiguity, override, 0, 0,
763 "test", 't', additional_error))
764 goto failure;
766 break;
767 case 'f': /* Video format to query. */
769 if (update_arg((void *)&(args_info->format_arg),
770 &(args_info->format_orig), &(args_info->format_given),
771 &(local_args_info.format_given), optarg, 0, "default",
772 ARG_STRING, check_ambiguity, override, 0, 0, "format", 'f',
773 additional_error))
774 goto failure;
776 break;
778 case 0: /* Long option with no short option */
779 /* Print version and exit. */
780 if (strcmp(long_options[option_index].name, "version") == 0)
783 if (update_arg(0,
784 0, &(args_info->version_given),
785 &(local_args_info.version_given), optarg, 0, 0, ARG_NO,
786 check_ambiguity, override, 0, 0,
787 "version", '-', additional_error))
788 goto failure;
791 /* Print license and exit. */
792 else if (strcmp(long_options[option_index].name, "license") == 0)
795 if (update_arg(0,
796 0, &(args_info->license_given),
797 &(local_args_info.license_given), optarg, 0, 0, ARG_NO,
798 check_ambiguity, override, 0, 0,
799 "license", '-', additional_error))
800 goto failure;
803 /* Print supported websites and exit. */
804 else if (strcmp(long_options[option_index].name, "support") == 0)
807 if (update_arg(0,
808 0, &(args_info->support_given),
809 &(local_args_info.support_given), optarg, 0, 0, ARG_NO,
810 check_ambiguity, override, 0, 0,
811 "support", '-', additional_error))
812 goto failure;
815 /* Print details in XML. */
816 else if (strcmp(long_options[option_index].name, "xml") == 0)
819 if (update_arg(0,
820 0, &(args_info->xml_given),
821 &(local_args_info.xml_given), optarg, 0, 0, ARG_NO,
822 check_ambiguity, override, 0, 0,
823 "xml", '-', additional_error))
824 goto failure;
827 /* Print details in original format. */
828 else if (strcmp(long_options[option_index].name, "old") == 0)
831 if (update_arg(0,
832 0, &(args_info->old_given),
833 &(local_args_info.old_given), optarg, 0, 0, ARG_NO,
834 check_ambiguity, override, 0, 0,
835 "old", '-', additional_error))
836 goto failure;
839 /* Turn on libcurl verbose mode. */
840 else if (strcmp(long_options[option_index].name, "verbose-libcurl") == 0)
843 if (update_arg(0,
844 0, &(args_info->verbose_libcurl_given),
845 &(local_args_info.verbose_libcurl_given), optarg, 0, 0,
846 ARG_NO, check_ambiguity, override, 0, 0,
847 "verbose-libcurl", '-', additional_error))
848 goto failure;
851 /* Invoke arg after parsing. */
852 else if (strcmp(long_options[option_index].name, "exec") == 0)
855 if (update_arg((void *)&(args_info->exec_arg),
856 &(args_info->exec_orig), &(args_info->exec_given),
857 &(local_args_info.exec_given), optarg, 0, 0, ARG_STRING,
858 check_ambiguity, override, 0, 0,
859 "exec", '-', additional_error))
860 goto failure;
863 /* Category HTTP website scripts. */
864 else if (strcmp(long_options[option_index].name, "category-http") == 0)
867 if (update_arg(0,
868 0, &(args_info->category_http_given),
869 &(local_args_info.category_http_given), optarg, 0, 0,
870 ARG_NO, check_ambiguity, override, 0, 0, "category-http",
871 '-', additional_error))
872 goto failure;
875 /* Category MMS website scripts. */
876 else if (strcmp(long_options[option_index].name, "category-mms") == 0)
879 if (update_arg(0,
880 0, &(args_info->category_mms_given),
881 &(local_args_info.category_mms_given), optarg, 0, 0,
882 ARG_NO, check_ambiguity, override, 0, 0, "category-mms",
883 '-', additional_error))
884 goto failure;
887 /* Category RTSP website scripts. */
888 else if (strcmp(long_options[option_index].name, "category-rtsp") == 0)
891 if (update_arg(0,
892 0, &(args_info->category_rtsp_given),
893 &(local_args_info.category_rtsp_given), optarg, 0, 0,
894 ARG_NO, check_ambiguity, override, 0, 0, "category-rtsp",
895 '-', additional_error))
896 goto failure;
899 /* Category RTMP website scripts. */
900 else if (strcmp(long_options[option_index].name, "category-rtmp") == 0)
903 if (update_arg(0,
904 0, &(args_info->category_rtmp_given),
905 &(local_args_info.category_rtmp_given), optarg, 0, 0,
906 ARG_NO, check_ambiguity, override, 0, 0, "category-rtmp",
907 '-', additional_error))
908 goto failure;
911 /* All website script categories. */
912 else if (strcmp(long_options[option_index].name, "category-all") == 0)
915 if (update_arg(0,
916 0, &(args_info->category_all_given),
917 &(local_args_info.category_all_given), optarg, 0, 0,
918 ARG_NO, check_ambiguity, override, 0, 0, "category-all",
919 '-', additional_error))
920 goto failure;
923 /* Check that parsed page title matches arg. */
924 else if (strcmp(long_options[option_index].name, "page-title") == 0)
927 if (update_arg((void *)&(args_info->page_title_arg),
928 &(args_info->page_title_orig),
929 &(args_info->page_title_given),
930 &(local_args_info.page_title_given), optarg, 0, 0,
931 ARG_STRING, check_ambiguity, override, 0, 0,
932 "page-title", '-', additional_error))
933 goto failure;
936 /* Check that parsed video ID matches arg. */
937 else if (strcmp(long_options[option_index].name, "video-id") == 0)
940 if (update_arg((void *)&(args_info->video_id_arg),
941 &(args_info->video_id_orig),
942 &(args_info->video_id_given),
943 &(local_args_info.video_id_given), optarg, 0, 0,
944 ARG_STRING, check_ambiguity, override, 0, 0, "video-id",
945 '-', additional_error))
946 goto failure;
949 /* Check that parsed video length matches arg. */
950 else if (strcmp(long_options[option_index].name, "file-length") == 0)
953 if (update_arg((void *)&(args_info->file_length_arg),
954 &(args_info->file_length_orig),
955 &(args_info->file_length_given),
956 &(local_args_info.file_length_given), optarg, 0, 0,
957 ARG_DOUBLE, check_ambiguity, override, 0, 0,
958 "file-length", '-', additional_error))
959 goto failure;
962 /* Check that parsed video suffix matches arg. */
963 else if (strcmp(long_options[option_index].name, "file-suffix") == 0)
966 if (update_arg((void *)&(args_info->file_suffix_arg),
967 &(args_info->file_suffix_orig),
968 &(args_info->file_suffix_given),
969 &(local_args_info.file_suffix_given), optarg, 0, 0,
970 ARG_STRING, check_ambiguity, override, 0, 0,
971 "file-suffix", '-', additional_error))
972 goto failure;
975 /* Run all built-in tests of category QUVIPROTO_HTTP. */
976 else if (strcmp(long_options[option_index].name, "test-all") == 0)
979 if (update_arg(0,
980 0, &(args_info->test_all_given),
981 &(local_args_info.test_all_given), optarg, 0, 0, ARG_NO,
982 check_ambiguity, override, 0, 0,
983 "test-all", '-', additional_error))
984 goto failure;
987 /* Dump video details with --test-all. */
988 else if (strcmp(long_options[option_index].name, "dump") == 0)
991 if (update_arg(0,
992 0, &(args_info->dump_given),
993 &(local_args_info.dump_given), optarg, 0, 0, ARG_NO,
994 check_ambiguity, override, 0, 0,
995 "dump", '-', additional_error))
996 goto failure;
999 /* Identify as arg. */
1000 else if (strcmp(long_options[option_index].name, "agent") == 0)
1003 if (update_arg((void *)&(args_info->agent_arg),
1004 &(args_info->agent_orig), &(args_info->agent_given),
1005 &(local_args_info.agent_given), optarg, 0, "Mozilla/5.0",
1006 ARG_STRING, check_ambiguity, override, 0, 0, "agent",
1007 '-', additional_error))
1008 goto failure;
1011 /* Use proxy for HTTP connections. */
1012 else if (strcmp(long_options[option_index].name, "proxy") == 0)
1015 if (update_arg((void *)&(args_info->proxy_arg),
1016 &(args_info->proxy_orig), &(args_info->proxy_given),
1017 &(local_args_info.proxy_given), optarg, 0, 0, ARG_STRING,
1018 check_ambiguity, override, 0, 0,
1019 "proxy", '-', additional_error))
1020 goto failure;
1023 /* Disable use of HTTP proxy. */
1024 else if (strcmp(long_options[option_index].name, "no-proxy") == 0)
1027 if (update_arg(0,
1028 0, &(args_info->no_proxy_given),
1029 &(local_args_info.no_proxy_given), optarg, 0, 0, ARG_NO,
1030 check_ambiguity, override, 0, 0,
1031 "no-proxy", '-', additional_error))
1032 goto failure;
1035 /* Seconds connecting allowed to take. */
1036 else if (strcmp(long_options[option_index].name, "connect-timeout") == 0)
1039 if (update_arg((void *)&(args_info->connect_timeout_arg),
1040 &(args_info->connect_timeout_orig),
1041 &(args_info->connect_timeout_given),
1042 &(local_args_info.connect_timeout_given), optarg, 0,
1043 "30", ARG_INT, check_ambiguity, override, 0, 0,
1044 "connect-timeout", '-', additional_error))
1045 goto failure;
1049 break;
1050 case '?': /* Invalid option. */
1051 /* `getopt_long' already printed an error message. */
1052 goto failure;
1054 default: /* bug: option not considered. */
1055 fprintf(stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c,
1056 (additional_error ? additional_error : ""));
1057 abort();
1058 } /* switch */
1059 } /* while */
1061 if (check_required)
1063 error += cmdline_parser_required2(args_info, argv[0], additional_error);
1066 cmdline_parser_release(&local_args_info);
1068 if (error)
1069 return (EXIT_FAILURE);
1071 if (optind < argc)
1073 int i = 0;
1074 int found_prog_name = 0;
1075 /* whether program name, i.e., argv[0], is in the remaining args
1076 (this may happen with some implementations of getopt,
1077 but surely not with the one included by gengetopt) */
1079 i = optind;
1080 while (i < argc)
1081 if (argv[i++] == argv[0])
1083 found_prog_name = 1;
1084 break;
1086 i = 0;
1088 args_info->inputs_num = argc - optind - found_prog_name;
1089 args_info->inputs =
1090 (char **)(malloc((args_info->inputs_num) * sizeof(char *)));
1091 while (optind < argc)
1092 if (argv[optind++] != argv[0])
1093 args_info->inputs[i++] = gengetopt_strdup(argv[optind - 1]);
1096 return 0;
1098 failure:
1100 cmdline_parser_release(&local_args_info);
1101 return (EXIT_FAILURE);
1104 #ifndef CONFIG_FILE_LINE_SIZE
1105 #define CONFIG_FILE_LINE_SIZE 2048
1106 #endif
1107 #define ADDITIONAL_ERROR " in configuration file "
1109 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1110 /* 3 is for "--" and "=" */
1112 static int _cmdline_parser_configfile(const char *filename, int *my_argc)
1114 FILE *file;
1115 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
1116 char linebuf[CONFIG_FILE_LINE_SIZE];
1117 int line_num = 0;
1118 int result = 0, equal;
1119 char *fopt, *farg;
1120 char *str_index;
1121 size_t len, next_token;
1122 char delimiter;
1124 if ((file = fopen(filename, "r")) == 0)
1126 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1127 CMDLINE_PARSER_PACKAGE, filename);
1128 return EXIT_FAILURE;
1131 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
1133 ++line_num;
1134 my_argv[0] = '\0';
1135 len = strlen(linebuf);
1136 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1))
1138 fprintf(stderr, "%s:%s:%d: Line too long in configuration file\n",
1139 CMDLINE_PARSER_PACKAGE, filename, line_num);
1140 result = EXIT_FAILURE;
1141 break;
1144 /* find first non-whitespace character in the line */
1145 next_token = strspn(linebuf, " \t\r\n");
1146 str_index = linebuf + next_token;
1148 if (str_index[0] == '\0' || str_index[0] == '#')
1149 continue; /* empty line or comment line is skipped */
1151 fopt = str_index;
1153 /* truncate fopt at the end of the first non-valid character */
1154 next_token = strcspn(fopt, " \t\r\n=");
1156 if (fopt[next_token] == '\0') /* the line is over */
1158 farg = 0;
1159 equal = 0;
1160 goto noarg;
1163 /* remember if equal sign is present */
1164 equal = (fopt[next_token] == '=');
1165 fopt[next_token++] = '\0';
1167 /* advance pointers to the next token after the end of fopt */
1168 next_token += strspn(fopt + next_token, " \t\r\n");
1170 /* check for the presence of equal sign, and if so, skip it */
1171 if (!equal)
1172 if ((equal = (fopt[next_token] == '=')))
1174 next_token++;
1175 next_token += strspn(fopt + next_token, " \t\r\n");
1177 str_index += next_token;
1179 /* find argument */
1180 farg = str_index;
1181 if (farg[0] == '\"' || farg[0] == '\'') /* quoted argument */
1183 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1184 if (!str_index)
1186 fprintf
1187 (stderr,
1188 "%s:%s:%d: unterminated string in configuration file\n",
1189 CMDLINE_PARSER_PACKAGE, filename, line_num);
1190 result = EXIT_FAILURE;
1191 break;
1194 else /* read up the remaining part up to a delimiter */
1196 next_token = strcspn(farg, " \t\r\n#\'\"");
1197 str_index += next_token;
1200 /* truncate farg at the delimiter and store it for further check */
1201 delimiter = *str_index, *str_index++ = '\0';
1203 /* everything but comment is illegal at the end of line */
1204 if (delimiter != '\0' && delimiter != '#')
1206 str_index += strspn(str_index, " \t\r\n");
1207 if (*str_index != '\0' && *str_index != '#')
1209 fprintf
1210 (stderr,
1211 "%s:%s:%d: malformed string in configuration file\n",
1212 CMDLINE_PARSER_PACKAGE, filename, line_num);
1213 result = EXIT_FAILURE;
1214 break;
1218 noarg:
1219 if (!strcmp(fopt, "include"))
1221 if (farg && *farg)
1223 result = _cmdline_parser_configfile(farg, my_argc);
1225 else
1227 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1228 CMDLINE_PARSER_PACKAGE, filename, line_num);
1230 continue;
1232 len = strlen(fopt);
1233 strcat(my_argv, len > 1 ? "--" : "-");
1234 strcat(my_argv, fopt);
1235 if (len > 1 && ((farg && *farg) || equal))
1236 strcat(my_argv, "=");
1237 if (farg && *farg)
1238 strcat(my_argv, farg);
1239 ++(*my_argc);
1241 cmd_line_list_tmp = (struct line_list *)malloc(sizeof(struct line_list));
1242 cmd_line_list_tmp->next = cmd_line_list;
1243 cmd_line_list = cmd_line_list_tmp;
1244 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1245 } /* while */
1247 if (file)
1248 fclose(file);
1249 return result;
1253 cmdline_parser_configfile(const char *filename,
1254 struct gengetopt_args_info *args_info,
1255 int override, int initialize, int check_required)
1257 struct cmdline_parser_params params;
1259 params.override = override;
1260 params.initialize = initialize;
1261 params.check_required = check_required;
1262 params.check_ambiguity = 0;
1263 params.print_errors = 1;
1265 return cmdline_parser_config_file(filename, args_info, &params);
1269 cmdline_parser_config_file(const char *filename,
1270 struct gengetopt_args_info *args_info,
1271 struct cmdline_parser_params *params)
1273 int i, result;
1274 int my_argc = 1;
1275 char **my_argv_arg;
1276 char *additional_error;
1278 /* store the program name */
1279 cmd_line_list_tmp = (struct line_list *)malloc(sizeof(struct line_list));
1280 cmd_line_list_tmp->next = cmd_line_list;
1281 cmd_line_list = cmd_line_list_tmp;
1282 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1284 result = _cmdline_parser_configfile(filename, &my_argc);
1286 if (result != EXIT_FAILURE)
1288 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1289 cmd_line_list_tmp = cmd_line_list;
1291 for (i = my_argc - 1; i >= 0; --i)
1293 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1294 cmd_line_list_tmp = cmd_line_list_tmp->next;
1297 my_argv_arg[my_argc] = 0;
1299 additional_error =
1300 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1301 strcpy(additional_error, ADDITIONAL_ERROR);
1302 strcat(additional_error, filename);
1303 result =
1304 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1305 params, additional_error);
1307 free(additional_error);
1308 free(my_argv_arg);
1311 free_cmd_list();
1312 if (result == EXIT_FAILURE)
1314 cmdline_parser_free(args_info);
1315 exit(EXIT_FAILURE);
1318 return result;