1 /*
2   File autogenerated by gengetopt version 2.22.5
3   generated with the following command:
4   gengetopt --unamed-opts=INFILE [OUTFILE] --no-handle-help --no-handle-error --input ./ccache2shishi.ggo --file-name ccache2shishi_cmd
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 "ccache2shishi_cmd.h"
27 
28 const char *gengetopt_args_info_purpose = "Read ccache data from INFILE (usually /tmp/krb5cc_UID) and\nwrite Shishi tickets to OUTFILE.  If INFILE and OUTFILE are not\nspecified, use the defaults.";
29 
30 const char *gengetopt_args_info_usage = "Usage: ccache2shishi [OPTIONS]... [INFILE [OUTFILE]]...";
31 
32 const char *gengetopt_args_info_description = "";
33 
34 const char *gengetopt_args_info_help[] = {
35   "  -h, --help     Print help and exit",
36   "  -V, --version  Print version and exit",
37   "  -v, --verbose  Produce verbose output.\n                   Use multiple times to increase amount of information.",
38   "  -q, --quiet    Don't produce any diagnostic output.\n                     (default=off)",
39     0
40 };
41 
42 typedef enum {ARG_NO
43   , ARG_FLAG
44 } cmdline_parser_arg_type;
45 
46 static
47 void clear_given (struct gengetopt_args_info *args_info);
48 static
49 void clear_args (struct gengetopt_args_info *args_info);
50 
51 static int
52 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
53                         struct cmdline_parser_params *params, const char *additional_error);
54 
55 static int
56 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
57 
58 static char *
59 gengetopt_strdup (const char *s);
60 
61 static
clear_given(struct gengetopt_args_info * args_info)62 void clear_given (struct gengetopt_args_info *args_info)
63 {
64   args_info->help_given = 0 ;
65   args_info->version_given = 0 ;
66   args_info->verbose_given = 0 ;
67   args_info->quiet_given = 0 ;
68 }
69 
70 static
clear_args(struct gengetopt_args_info * args_info)71 void clear_args (struct gengetopt_args_info *args_info)
72 {
73   FIX_UNUSED (args_info);
74   args_info->quiet_flag = 0;
75 
76 }
77 
78 static
init_args_info(struct gengetopt_args_info * args_info)79 void init_args_info(struct gengetopt_args_info *args_info)
80 {
81 
82 
83   args_info->help_help = gengetopt_args_info_help[0] ;
84   args_info->version_help = gengetopt_args_info_help[1] ;
85   args_info->verbose_help = gengetopt_args_info_help[2] ;
86   args_info->verbose_min = 0;
87   args_info->verbose_max = 0;
88   args_info->quiet_help = gengetopt_args_info_help[3] ;
89 
90 }
91 
92 void
cmdline_parser_print_version(void)93 cmdline_parser_print_version (void)
94 {
95   printf ("%s %s\n",
96      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
97      CMDLINE_PARSER_VERSION);
98 }
99 
print_help_common(void)100 static void print_help_common(void) {
101   cmdline_parser_print_version ();
102 
103   if (strlen(gengetopt_args_info_purpose) > 0)
104     printf("\n%s\n", gengetopt_args_info_purpose);
105 
106   if (strlen(gengetopt_args_info_usage) > 0)
107     printf("\n%s\n", gengetopt_args_info_usage);
108 
109   printf("\n");
110 
111   if (strlen(gengetopt_args_info_description) > 0)
112     printf("%s\n\n", gengetopt_args_info_description);
113 }
114 
115 void
cmdline_parser_print_help(void)116 cmdline_parser_print_help (void)
117 {
118   int i = 0;
119   print_help_common();
120   while (gengetopt_args_info_help[i])
121     printf("%s\n", gengetopt_args_info_help[i++]);
122 }
123 
124 void
cmdline_parser_init(struct gengetopt_args_info * args_info)125 cmdline_parser_init (struct gengetopt_args_info *args_info)
126 {
127   clear_given (args_info);
128   clear_args (args_info);
129   init_args_info (args_info);
130 
131   args_info->inputs = 0;
132   args_info->inputs_num = 0;
133 }
134 
135 void
cmdline_parser_params_init(struct cmdline_parser_params * params)136 cmdline_parser_params_init(struct cmdline_parser_params *params)
137 {
138   if (params)
139     {
140       params->override = 0;
141       params->initialize = 1;
142       params->check_required = 1;
143       params->check_ambiguity = 0;
144       params->print_errors = 1;
145     }
146 }
147 
148 struct cmdline_parser_params *
cmdline_parser_params_create(void)149 cmdline_parser_params_create(void)
150 {
151   struct cmdline_parser_params *params =
152     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
153   cmdline_parser_params_init(params);
154   return params;
155 }
156 
157 
158 
159 static void
cmdline_parser_release(struct gengetopt_args_info * args_info)160 cmdline_parser_release (struct gengetopt_args_info *args_info)
161 {
162   unsigned int i;
163 
164 
165   for (i = 0; i < args_info->inputs_num; ++i)
166     free (args_info->inputs [i]);
167 
168   if (args_info->inputs_num)
169     free (args_info->inputs);
170 
171   clear_given (args_info);
172 }
173 
174 
175 static void
write_into_file(FILE * outfile,const char * opt,const char * arg,const char * values[])176 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
177 {
178   FIX_UNUSED (values);
179   if (arg) {
180     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
181   } else {
182     fprintf(outfile, "%s\n", opt);
183   }
184 }
185 
186 static void
write_multiple_into_file(FILE * outfile,int len,const char * opt,char ** arg,const char * values[])187 write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
188 {
189   int i;
190 
191   for (i = 0; i < len; ++i)
192     write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
193 }
194 
195 int
cmdline_parser_dump(FILE * outfile,struct gengetopt_args_info * args_info)196 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
197 {
198   int i = 0;
199 
200   if (!outfile)
201     {
202       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
203       return EXIT_FAILURE;
204     }
205 
206   if (args_info->help_given)
207     write_into_file(outfile, "help", 0, 0 );
208   if (args_info->version_given)
209     write_into_file(outfile, "version", 0, 0 );
210   write_multiple_into_file(outfile, args_info->verbose_given, "verbose", 0, 0);
211   if (args_info->quiet_given)
212     write_into_file(outfile, "quiet", 0, 0 );
213 
214 
215   i = EXIT_SUCCESS;
216   return i;
217 }
218 
219 int
cmdline_parser_file_save(const char * filename,struct gengetopt_args_info * args_info)220 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
221 {
222   FILE *outfile;
223   int i = 0;
224 
225   outfile = fopen(filename, "w");
226 
227   if (!outfile)
228     {
229       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
230       return EXIT_FAILURE;
231     }
232 
233   i = cmdline_parser_dump(outfile, args_info);
234   fclose (outfile);
235 
236   return i;
237 }
238 
239 void
cmdline_parser_free(struct gengetopt_args_info * args_info)240 cmdline_parser_free (struct gengetopt_args_info *args_info)
241 {
242   cmdline_parser_release (args_info);
243 }
244 
245 /** @brief replacement of strdup, which is not standard */
246 char *
gengetopt_strdup(const char * s)247 gengetopt_strdup (const char *s)
248 {
249   char *result = 0;
250   if (!s)
251     return result;
252 
253   result = (char*)malloc(strlen(s) + 1);
254   if (result == (char*)0)
255     return (char*)0;
256   strcpy(result, s);
257   return result;
258 }
259 
260 static int
261 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
262 
263 int
check_multiple_option_occurrences(const char * prog_name,unsigned int option_given,unsigned int min,unsigned int max,const char * option_desc)264 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
265 {
266   int error = 0;
267 
268   if (option_given && (min > 0 || max > 0))
269     {
270       if (min > 0 && max > 0)
271         {
272           if (min == max)
273             {
274               /* specific occurrences */
275               if (option_given != (unsigned int) min)
276                 {
277                   fprintf (stderr, "%s: %s option occurrences must be %d\n",
278                     prog_name, option_desc, min);
279                   error = 1;
280                 }
281             }
282           else if (option_given < (unsigned int) min
283                 || option_given > (unsigned int) max)
284             {
285               /* range occurrences */
286               fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
287                 prog_name, option_desc, min, max);
288               error = 1;
289             }
290         }
291       else if (min > 0)
292         {
293           /* at least check */
294           if (option_given < min)
295             {
296               fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
297                 prog_name, option_desc, min);
298               error = 1;
299             }
300         }
301       else if (max > 0)
302         {
303           /* at most check */
304           if (option_given > max)
305             {
306               fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
307                 prog_name, option_desc, max);
308               error = 1;
309             }
310         }
311     }
312 
313   return error;
314 }
315 int
cmdline_parser(int argc,char ** argv,struct gengetopt_args_info * args_info)316 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
317 {
318   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
319 }
320 
321 int
cmdline_parser_ext(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params)322 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
323                    struct cmdline_parser_params *params)
324 {
325   int result;
326   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
327 
328   return result;
329 }
330 
331 int
cmdline_parser2(int argc,char ** argv,struct gengetopt_args_info * args_info,int override,int initialize,int check_required)332 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
333 {
334   int result;
335   struct cmdline_parser_params params;
336 
337   params.override = override;
338   params.initialize = initialize;
339   params.check_required = check_required;
340   params.check_ambiguity = 0;
341   params.print_errors = 1;
342 
343   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
344 
345   return result;
346 }
347 
348 int
cmdline_parser_required(struct gengetopt_args_info * args_info,const char * prog_name)349 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
350 {
351   int result = EXIT_SUCCESS;
352 
353   if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
354     result = EXIT_FAILURE;
355 
356   return result;
357 }
358 
359 int
cmdline_parser_required2(struct gengetopt_args_info * args_info,const char * prog_name,const char * additional_error)360 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
361 {
362   int error = 0;
363   FIX_UNUSED (additional_error);
364 
365   /* checks for required options */
366   if (check_multiple_option_occurrences(prog_name, args_info->verbose_given, args_info->verbose_min, args_info->verbose_max, "'--verbose' ('-v')"))
367      error = 1;
368 
369 
370   /* checks for dependences among options */
371 
372   return error;
373 }
374 
375 
376 static char *package_name = 0;
377 
378 /**
379  * @brief updates an option
380  * @param field the generic pointer to the field to update
381  * @param orig_field the pointer to the orig field
382  * @param field_given the pointer to the number of occurrence of this option
383  * @param prev_given the pointer to the number of occurrence already seen
384  * @param value the argument for this option (if null no arg was specified)
385  * @param possible_values the possible values for this option (if specified)
386  * @param default_value the default value (in case the option only accepts fixed values)
387  * @param arg_type the type of this option
388  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
389  * @param override @see cmdline_parser_params.override
390  * @param no_free whether to free a possible previous value
391  * @param multiple_option whether this is a multiple option
392  * @param long_opt the corresponding long option
393  * @param short_opt the corresponding short option (or '-' if none)
394  * @param additional_error possible further error specification
395  */
396 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,cmdline_parser_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)397 int update_arg(void *field, char **orig_field,
398                unsigned int *field_given, unsigned int *prev_given,
399                char *value, const char *possible_values[],
400                const char *default_value,
401                cmdline_parser_arg_type arg_type,
402                int check_ambiguity, int override,
403                int no_free, int multiple_option,
404                const char *long_opt, char short_opt,
405                const char *additional_error)
406 {
407   char *stop_char = 0;
408   const char *val = value;
409   int found;
410   FIX_UNUSED (field);
411 
412   stop_char = 0;
413   found = 0;
414 
415   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
416     {
417       if (short_opt != '-')
418         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
419                package_name, long_opt, short_opt,
420                (additional_error ? additional_error : ""));
421       else
422         fprintf (stderr, "%s: `--%s' option given more than once%s\n",
423                package_name, long_opt,
424                (additional_error ? additional_error : ""));
425       return 1; /* failure */
426     }
427 
428   FIX_UNUSED (default_value);
429 
430   if (field_given && *field_given && ! override)
431     return 0;
432   if (prev_given)
433     (*prev_given)++;
434   if (field_given)
435     (*field_given)++;
436   if (possible_values)
437     val = possible_values[found];
438 
439   switch(arg_type) {
440   case ARG_FLAG:
441     *((int *)field) = !*((int *)field);
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
cmdline_parser_internal(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params,const char * additional_error)470 cmdline_parser_internal (
471   int argc, char **argv, struct gengetopt_args_info *args_info,
472                         struct cmdline_parser_params *params, const char *additional_error)
473 {
474   int c;	/* Character of the parsed option.  */
475 
476   int error = 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     cmdline_parser_init (args_info);
493 
494   cmdline_parser_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         { "verbose",	0, NULL, 'v' },
509         { "quiet",	0, NULL, 'q' },
510         { 0,  0, 0, 0 }
511       };
512 
513       c = getopt_long (argc, argv, "hVvq", long_options, &option_index);
514 
515       if (c == -1) break;	/* Exit from `while (1)' loop.  */
516 
517       switch (c)
518         {
519         case 'h':	/* Print help and exit.  */
520 
521 
522           if (update_arg( 0 ,
523                0 , &(args_info->help_given),
524               &(local_args_info.help_given), optarg, 0, 0, ARG_NO,
525               check_ambiguity, override, 0, 0,
526               "help", 'h',
527               additional_error))
528             goto failure;
529           cmdline_parser_free (&local_args_info);
530           return 0;
531 
532           break;
533         case 'V':	/* Print version and exit.  */
534           cmdline_parser_print_version ();
535           cmdline_parser_free (&local_args_info);
536           exit (EXIT_SUCCESS);
537 
538         case 'v':	/* Produce verbose output.
539         Use multiple times to increase amount of information..  */
540 
541           local_args_info.verbose_given++;
542 
543           break;
544         case 'q':	/* Don't produce any diagnostic output.
545 .  */
546 
547 
548           if (update_arg((void *)&(args_info->quiet_flag), 0, &(args_info->quiet_given),
549               &(local_args_info.quiet_given), optarg, 0, 0, ARG_FLAG,
550               check_ambiguity, override, 1, 0, "quiet", 'q',
551               additional_error))
552             goto failure;
553 
554           break;
555 
556         case 0:	/* Long option with no short option */
557         case '?':	/* Invalid option.  */
558           /* `getopt_long' already printed an error message.  */
559           goto failure;
560 
561         default:	/* bug: option not considered.  */
562           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
563           abort ();
564         } /* switch */
565     } /* while */
566 
567 
568 
569   args_info->verbose_given += local_args_info.verbose_given;
570   local_args_info.verbose_given = 0;
571 
572   if (check_required)
573     {
574       error += cmdline_parser_required2 (args_info, argv[0], additional_error);
575     }
576 
577   cmdline_parser_release (&local_args_info);
578 
579   if ( error )
580     return (EXIT_FAILURE);
581 
582   if (optind < argc)
583     {
584       int i = 0 ;
585       int found_prog_name = 0;
586       /* whether program name, i.e., argv[0], is in the remaining args
587          (this may happen with some implementations of getopt,
588           but surely not with the one included by gengetopt) */
589 
590       i = optind;
591       while (i < argc)
592         if (argv[i++] == argv[0]) {
593           found_prog_name = 1;
594           break;
595         }
596       i = 0;
597 
598       args_info->inputs_num = argc - optind - found_prog_name;
599       args_info->inputs =
600         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
601       while (optind < argc)
602         if (argv[optind++] != argv[0])
603           args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
604     }
605 
606   return 0;
607 
608 failure:
609 
610   cmdline_parser_release (&local_args_info);
611   return (EXIT_FAILURE);
612 }
613