1 /*
2 File autogenerated by gengetopt version 2.22.6
3 generated with the following command:
4 /usr/bin/gengetopt -u -i options.ggo -f options -F options
5
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9 */
10
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23
24 #include <getopt.h>
25
26 #include "options.h"
27
28 const char *gengetopt_args_info_purpose = "convert Photoshop gradient to SVG gradient";
29
30 const char *gengetopt_args_info_usage = "Usage: pssvg [OPTIONS]... [FILES]...";
31
32 const char *gengetopt_args_info_versiontext = "";
33
34 const char *gengetopt_args_info_description = "";
35
36 const char *gengetopt_args_info_help[] = {
37 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -b, --background=STRING background colour (default=`0/0/0')",
40 " --backtrace-file=STRING write backtrace to file",
41 " --backtrace-format=STRING backtrace format",
42 " -f, --foreground=STRING foreground colour (default=`255/255/255')",
43 " -o, --output=STRING output filename",
44 " -t, --title=STRING title format string",
45 " -v, --verbose verbose operation (default=off)",
46 0
47 };
48
49 typedef enum {ARG_NO
50 , ARG_FLAG
51 , ARG_STRING
52 } options_arg_type;
53
54 static
55 void clear_given (struct gengetopt_args_info *args_info);
56 static
57 void clear_args (struct gengetopt_args_info *args_info);
58
59 static int
60 options_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
61 struct options_params *params, const char *additional_error);
62
63
64 static char *
65 gengetopt_strdup (const char *s);
66
67 static
clear_given(struct gengetopt_args_info * args_info)68 void clear_given (struct gengetopt_args_info *args_info)
69 {
70 args_info->help_given = 0 ;
71 args_info->version_given = 0 ;
72 args_info->background_given = 0 ;
73 args_info->backtrace_file_given = 0 ;
74 args_info->backtrace_format_given = 0 ;
75 args_info->foreground_given = 0 ;
76 args_info->output_given = 0 ;
77 args_info->title_given = 0 ;
78 args_info->verbose_given = 0 ;
79 }
80
81 static
clear_args(struct gengetopt_args_info * args_info)82 void clear_args (struct gengetopt_args_info *args_info)
83 {
84 FIX_UNUSED (args_info);
85 args_info->background_arg = gengetopt_strdup ("0/0/0");
86 args_info->background_orig = NULL;
87 args_info->backtrace_file_arg = NULL;
88 args_info->backtrace_file_orig = NULL;
89 args_info->backtrace_format_arg = NULL;
90 args_info->backtrace_format_orig = NULL;
91 args_info->foreground_arg = gengetopt_strdup ("255/255/255");
92 args_info->foreground_orig = NULL;
93 args_info->output_arg = NULL;
94 args_info->output_orig = NULL;
95 args_info->title_arg = NULL;
96 args_info->title_orig = NULL;
97 args_info->verbose_flag = 0;
98
99 }
100
101 static
init_args_info(struct gengetopt_args_info * args_info)102 void init_args_info(struct gengetopt_args_info *args_info)
103 {
104
105
106 args_info->help_help = gengetopt_args_info_help[0] ;
107 args_info->version_help = gengetopt_args_info_help[1] ;
108 args_info->background_help = gengetopt_args_info_help[2] ;
109 args_info->backtrace_file_help = gengetopt_args_info_help[3] ;
110 args_info->backtrace_format_help = gengetopt_args_info_help[4] ;
111 args_info->foreground_help = gengetopt_args_info_help[5] ;
112 args_info->output_help = gengetopt_args_info_help[6] ;
113 args_info->title_help = gengetopt_args_info_help[7] ;
114 args_info->verbose_help = gengetopt_args_info_help[8] ;
115
116 }
117
118 void
options_print_version(void)119 options_print_version (void)
120 {
121 printf ("%s %s\n",
122 (strlen(OPTIONS_PACKAGE_NAME) ? OPTIONS_PACKAGE_NAME : OPTIONS_PACKAGE),
123 OPTIONS_VERSION);
124
125 if (strlen(gengetopt_args_info_versiontext) > 0)
126 printf("\n%s\n", gengetopt_args_info_versiontext);
127 }
128
print_help_common(void)129 static void print_help_common(void) {
130 options_print_version ();
131
132 if (strlen(gengetopt_args_info_purpose) > 0)
133 printf("\n%s\n", gengetopt_args_info_purpose);
134
135 if (strlen(gengetopt_args_info_usage) > 0)
136 printf("\n%s\n", gengetopt_args_info_usage);
137
138 printf("\n");
139
140 if (strlen(gengetopt_args_info_description) > 0)
141 printf("%s\n\n", gengetopt_args_info_description);
142 }
143
144 void
options_print_help(void)145 options_print_help (void)
146 {
147 int i = 0;
148 print_help_common();
149 while (gengetopt_args_info_help[i])
150 printf("%s\n", gengetopt_args_info_help[i++]);
151 }
152
153 void
options_init(struct gengetopt_args_info * args_info)154 options_init (struct gengetopt_args_info *args_info)
155 {
156 clear_given (args_info);
157 clear_args (args_info);
158 init_args_info (args_info);
159
160 args_info->inputs = 0;
161 args_info->inputs_num = 0;
162 }
163
164 void
options_params_init(struct options_params * params)165 options_params_init(struct options_params *params)
166 {
167 if (params)
168 {
169 params->override = 0;
170 params->initialize = 1;
171 params->check_required = 1;
172 params->check_ambiguity = 0;
173 params->print_errors = 1;
174 }
175 }
176
177 struct options_params *
options_params_create(void)178 options_params_create(void)
179 {
180 struct options_params *params =
181 (struct options_params *)malloc(sizeof(struct options_params));
182 options_params_init(params);
183 return params;
184 }
185
186 static void
free_string_field(char ** s)187 free_string_field (char **s)
188 {
189 if (*s)
190 {
191 free (*s);
192 *s = 0;
193 }
194 }
195
196
197 static void
options_release(struct gengetopt_args_info * args_info)198 options_release (struct gengetopt_args_info *args_info)
199 {
200 unsigned int i;
201 free_string_field (&(args_info->background_arg));
202 free_string_field (&(args_info->background_orig));
203 free_string_field (&(args_info->backtrace_file_arg));
204 free_string_field (&(args_info->backtrace_file_orig));
205 free_string_field (&(args_info->backtrace_format_arg));
206 free_string_field (&(args_info->backtrace_format_orig));
207 free_string_field (&(args_info->foreground_arg));
208 free_string_field (&(args_info->foreground_orig));
209 free_string_field (&(args_info->output_arg));
210 free_string_field (&(args_info->output_orig));
211 free_string_field (&(args_info->title_arg));
212 free_string_field (&(args_info->title_orig));
213
214
215 for (i = 0; i < args_info->inputs_num; ++i)
216 free (args_info->inputs [i]);
217
218 if (args_info->inputs_num)
219 free (args_info->inputs);
220
221 clear_given (args_info);
222 }
223
224
225 static void
write_into_file(FILE * outfile,const char * opt,const char * arg,const char * values[])226 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
227 {
228 FIX_UNUSED (values);
229 if (arg) {
230 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
231 } else {
232 fprintf(outfile, "%s\n", opt);
233 }
234 }
235
236
237 int
options_dump(FILE * outfile,struct gengetopt_args_info * args_info)238 options_dump(FILE *outfile, struct gengetopt_args_info *args_info)
239 {
240 int i = 0;
241
242 if (!outfile)
243 {
244 fprintf (stderr, "%s: cannot dump options to stream\n", OPTIONS_PACKAGE);
245 return EXIT_FAILURE;
246 }
247
248 if (args_info->help_given)
249 write_into_file(outfile, "help", 0, 0 );
250 if (args_info->version_given)
251 write_into_file(outfile, "version", 0, 0 );
252 if (args_info->background_given)
253 write_into_file(outfile, "background", args_info->background_orig, 0);
254 if (args_info->backtrace_file_given)
255 write_into_file(outfile, "backtrace-file", args_info->backtrace_file_orig, 0);
256 if (args_info->backtrace_format_given)
257 write_into_file(outfile, "backtrace-format", args_info->backtrace_format_orig, 0);
258 if (args_info->foreground_given)
259 write_into_file(outfile, "foreground", args_info->foreground_orig, 0);
260 if (args_info->output_given)
261 write_into_file(outfile, "output", args_info->output_orig, 0);
262 if (args_info->title_given)
263 write_into_file(outfile, "title", args_info->title_orig, 0);
264 if (args_info->verbose_given)
265 write_into_file(outfile, "verbose", 0, 0 );
266
267
268 i = EXIT_SUCCESS;
269 return i;
270 }
271
272 int
options_file_save(const char * filename,struct gengetopt_args_info * args_info)273 options_file_save(const char *filename, struct gengetopt_args_info *args_info)
274 {
275 FILE *outfile;
276 int i = 0;
277
278 outfile = fopen(filename, "w");
279
280 if (!outfile)
281 {
282 fprintf (stderr, "%s: cannot open file for writing: %s\n", OPTIONS_PACKAGE, filename);
283 return EXIT_FAILURE;
284 }
285
286 i = options_dump(outfile, args_info);
287 fclose (outfile);
288
289 return i;
290 }
291
292 void
options_free(struct gengetopt_args_info * args_info)293 options_free (struct gengetopt_args_info *args_info)
294 {
295 options_release (args_info);
296 }
297
298 /** @brief replacement of strdup, which is not standard */
299 char *
gengetopt_strdup(const char * s)300 gengetopt_strdup (const char *s)
301 {
302 char *result = 0;
303 if (!s)
304 return result;
305
306 result = (char*)malloc(strlen(s) + 1);
307 if (result == (char*)0)
308 return (char*)0;
309 strcpy(result, s);
310 return result;
311 }
312
313 int
options(int argc,char ** argv,struct gengetopt_args_info * args_info)314 options (int argc, char **argv, struct gengetopt_args_info *args_info)
315 {
316 return options2 (argc, argv, args_info, 0, 1, 1);
317 }
318
319 int
options_ext(int argc,char ** argv,struct gengetopt_args_info * args_info,struct options_params * params)320 options_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
321 struct options_params *params)
322 {
323 int result;
324 result = options_internal (argc, argv, args_info, params, 0);
325
326 if (result == EXIT_FAILURE)
327 {
328 options_free (args_info);
329 exit (EXIT_FAILURE);
330 }
331
332 return result;
333 }
334
335 int
options2(int argc,char ** argv,struct gengetopt_args_info * args_info,int override,int initialize,int check_required)336 options2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
337 {
338 int result;
339 struct options_params params;
340
341 params.override = override;
342 params.initialize = initialize;
343 params.check_required = check_required;
344 params.check_ambiguity = 0;
345 params.print_errors = 1;
346
347 result = options_internal (argc, argv, args_info, ¶ms, 0);
348
349 if (result == EXIT_FAILURE)
350 {
351 options_free (args_info);
352 exit (EXIT_FAILURE);
353 }
354
355 return result;
356 }
357
358 int
options_required(struct gengetopt_args_info * args_info,const char * prog_name)359 options_required (struct gengetopt_args_info *args_info, const char *prog_name)
360 {
361 FIX_UNUSED (args_info);
362 FIX_UNUSED (prog_name);
363 return EXIT_SUCCESS;
364 }
365
366
367 static char *package_name = 0;
368
369 /**
370 * @brief updates an option
371 * @param field the generic pointer to the field to update
372 * @param orig_field the pointer to the orig field
373 * @param field_given the pointer to the number of occurrence of this option
374 * @param prev_given the pointer to the number of occurrence already seen
375 * @param value the argument for this option (if null no arg was specified)
376 * @param possible_values the possible values for this option (if specified)
377 * @param default_value the default value (in case the option only accepts fixed values)
378 * @param arg_type the type of this option
379 * @param check_ambiguity @see options_params.check_ambiguity
380 * @param override @see options_params.override
381 * @param no_free whether to free a possible previous value
382 * @param multiple_option whether this is a multiple option
383 * @param long_opt the corresponding long option
384 * @param short_opt the corresponding short option (or '-' if none)
385 * @param additional_error possible further error specification
386 */
387 static
update_arg(void * field,char ** orig_field,unsigned int * field_given,unsigned int * prev_given,char * value,const char * possible_values[],const char * default_value,options_arg_type arg_type,int check_ambiguity,int override,int no_free,int multiple_option,const char * long_opt,char short_opt,const char * additional_error)388 int update_arg(void *field, char **orig_field,
389 unsigned int *field_given, unsigned int *prev_given,
390 char *value, const char *possible_values[],
391 const char *default_value,
392 options_arg_type arg_type,
393 int check_ambiguity, int override,
394 int no_free, int multiple_option,
395 const char *long_opt, char short_opt,
396 const char *additional_error)
397 {
398 char *stop_char = 0;
399 const char *val = value;
400 int found;
401 char **string_field;
402 FIX_UNUSED (field);
403
404 stop_char = 0;
405 found = 0;
406
407 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
408 {
409 if (short_opt != '-')
410 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
411 package_name, long_opt, short_opt,
412 (additional_error ? additional_error : ""));
413 else
414 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
415 package_name, long_opt,
416 (additional_error ? additional_error : ""));
417 return 1; /* failure */
418 }
419
420 FIX_UNUSED (default_value);
421
422 if (field_given && *field_given && ! override)
423 return 0;
424 if (prev_given)
425 (*prev_given)++;
426 if (field_given)
427 (*field_given)++;
428 if (possible_values)
429 val = possible_values[found];
430
431 switch(arg_type) {
432 case ARG_FLAG:
433 *((int *)field) = !*((int *)field);
434 break;
435 case ARG_STRING:
436 if (val) {
437 string_field = (char **)field;
438 if (!no_free && *string_field)
439 free (*string_field); /* free previous string */
440 *string_field = gengetopt_strdup (val);
441 }
442 break;
443 default:
444 break;
445 };
446
447
448 /* store the original value */
449 switch(arg_type) {
450 case ARG_NO:
451 case ARG_FLAG:
452 break;
453 default:
454 if (value && orig_field) {
455 if (no_free) {
456 *orig_field = value;
457 } else {
458 if (*orig_field)
459 free (*orig_field); /* free previous string */
460 *orig_field = gengetopt_strdup (value);
461 }
462 }
463 };
464
465 return 0; /* OK */
466 }
467
468
469 int
options_internal(int argc,char ** argv,struct gengetopt_args_info * args_info,struct options_params * params,const char * additional_error)470 options_internal (
471 int argc, char **argv, struct gengetopt_args_info *args_info,
472 struct options_params *params, const char *additional_error)
473 {
474 int c; /* Character of the parsed option. */
475
476 int error_occurred = 0;
477 struct gengetopt_args_info local_args_info;
478
479 int override;
480 int initialize;
481 int check_required;
482 int check_ambiguity;
483
484 package_name = argv[0];
485
486 override = params->override;
487 initialize = params->initialize;
488 check_required = params->check_required;
489 check_ambiguity = params->check_ambiguity;
490
491 if (initialize)
492 options_init (args_info);
493
494 options_init (&local_args_info);
495
496 optarg = 0;
497 optind = 0;
498 opterr = params->print_errors;
499 optopt = '?';
500
501 while (1)
502 {
503 int option_index = 0;
504
505 static struct option long_options[] = {
506 { "help", 0, NULL, 'h' },
507 { "version", 0, NULL, 'V' },
508 { "background", 1, NULL, 'b' },
509 { "backtrace-file", 1, NULL, 0 },
510 { "backtrace-format", 1, NULL, 0 },
511 { "foreground", 1, NULL, 'f' },
512 { "output", 1, NULL, 'o' },
513 { "title", 1, NULL, 't' },
514 { "verbose", 0, NULL, 'v' },
515 { 0, 0, 0, 0 }
516 };
517
518 c = getopt_long (argc, argv, "hVb:f:o:t:v", long_options, &option_index);
519
520 if (c == -1) break; /* Exit from `while (1)' loop. */
521
522 switch (c)
523 {
524 case 'h': /* Print help and exit. */
525 options_print_help ();
526 options_free (&local_args_info);
527 exit (EXIT_SUCCESS);
528
529 case 'V': /* Print version and exit. */
530 options_print_version ();
531 options_free (&local_args_info);
532 exit (EXIT_SUCCESS);
533
534 case 'b': /* background colour. */
535
536
537 if (update_arg( (void *)&(args_info->background_arg),
538 &(args_info->background_orig), &(args_info->background_given),
539 &(local_args_info.background_given), optarg, 0, "0/0/0", ARG_STRING,
540 check_ambiguity, override, 0, 0,
541 "background", 'b',
542 additional_error))
543 goto failure;
544
545 break;
546 case 'f': /* foreground colour. */
547
548
549 if (update_arg( (void *)&(args_info->foreground_arg),
550 &(args_info->foreground_orig), &(args_info->foreground_given),
551 &(local_args_info.foreground_given), optarg, 0, "255/255/255", ARG_STRING,
552 check_ambiguity, override, 0, 0,
553 "foreground", 'f',
554 additional_error))
555 goto failure;
556
557 break;
558 case 'o': /* output filename. */
559
560
561 if (update_arg( (void *)&(args_info->output_arg),
562 &(args_info->output_orig), &(args_info->output_given),
563 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
564 check_ambiguity, override, 0, 0,
565 "output", 'o',
566 additional_error))
567 goto failure;
568
569 break;
570 case 't': /* title format string. */
571
572
573 if (update_arg( (void *)&(args_info->title_arg),
574 &(args_info->title_orig), &(args_info->title_given),
575 &(local_args_info.title_given), optarg, 0, 0, ARG_STRING,
576 check_ambiguity, override, 0, 0,
577 "title", 't',
578 additional_error))
579 goto failure;
580
581 break;
582 case 'v': /* verbose operation. */
583
584
585 if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
586 &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
587 check_ambiguity, override, 1, 0, "verbose", 'v',
588 additional_error))
589 goto failure;
590
591 break;
592
593 case 0: /* Long option with no short option */
594 /* write backtrace to file. */
595 if (strcmp (long_options[option_index].name, "backtrace-file") == 0)
596 {
597
598
599 if (update_arg( (void *)&(args_info->backtrace_file_arg),
600 &(args_info->backtrace_file_orig), &(args_info->backtrace_file_given),
601 &(local_args_info.backtrace_file_given), optarg, 0, 0, ARG_STRING,
602 check_ambiguity, override, 0, 0,
603 "backtrace-file", '-',
604 additional_error))
605 goto failure;
606
607 }
608 /* backtrace format. */
609 else if (strcmp (long_options[option_index].name, "backtrace-format") == 0)
610 {
611
612
613 if (update_arg( (void *)&(args_info->backtrace_format_arg),
614 &(args_info->backtrace_format_orig), &(args_info->backtrace_format_given),
615 &(local_args_info.backtrace_format_given), optarg, 0, 0, ARG_STRING,
616 check_ambiguity, override, 0, 0,
617 "backtrace-format", '-',
618 additional_error))
619 goto failure;
620
621 }
622
623 break;
624 case '?': /* Invalid option. */
625 /* `getopt_long' already printed an error message. */
626 goto failure;
627
628 default: /* bug: option not considered. */
629 fprintf (stderr, "%s: option unknown: %c%s\n", OPTIONS_PACKAGE, c, (additional_error ? additional_error : ""));
630 abort ();
631 } /* switch */
632 } /* while */
633
634
635
636
637 options_release (&local_args_info);
638
639 if ( error_occurred )
640 return (EXIT_FAILURE);
641
642 if (optind < argc)
643 {
644 int i = 0 ;
645 int found_prog_name = 0;
646 /* whether program name, i.e., argv[0], is in the remaining args
647 (this may happen with some implementations of getopt,
648 but surely not with the one included by gengetopt) */
649
650 i = optind;
651 while (i < argc)
652 if (argv[i++] == argv[0]) {
653 found_prog_name = 1;
654 break;
655 }
656 i = 0;
657
658 args_info->inputs_num = argc - optind - found_prog_name;
659 args_info->inputs =
660 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
661 while (optind < argc)
662 if (argv[optind++] != argv[0])
663 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
664 }
665
666 return 0;
667
668 failure:
669
670 options_release (&local_args_info);
671 return (EXIT_FAILURE);
672 }
673