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