1 /*
2   File autogenerated by gengetopt version 2.22.4
3   generated with the following command:
4   gengetopt --c-extension=c.in --input ./cmdline.sh
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 "cmdline.h"
27 
28 const char *gengetopt_args_info_purpose = "The Ganglia Monitoring Daemon (gmond) listens to the cluster\nmessage channel, stores the data in-memory and when requested\nwill output an XML description of the state of the cluster";
29 
30 const char *gengetopt_args_info_usage = "Usage: gmond [OPTIONS]...";
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   "  -c, --conf=STRING      Location of gmond configuration file  \n                           (default=`/usr/local/etc/gmond.conf')",
38   "  -l, --location=STRING  Location of this host in the cluster \n                           'rack,rank,plane'.  (default=`0,0,0')",
39   "  -d, --debug=INT        Debug level. If greater than zero, daemon will stay in \n                           foreground.  (default=`0')",
40   "  -f, --foreground       Run in foreground (don't daemonize)  (default=off)",
41   "  -t, --default_config   Print the default configuration to stdout and exit  \n                           (default=off)",
42   "  -m, --metrics          Print the list of metrics this gmond supports  \n                           (default=off)",
43   "  -b, --bandwidth        Calculate minimum bandwidth use for configuration  \n                           (default=off)",
44   "  -r, --convert=STRING   Convert a 2.5.x configuration file to the new 3.x \n                           format",
45   "  -p, --pid-file=STRING  Write process-id to file",
46     0
47 };
48 
49 typedef enum {ARG_NO
50   , ARG_FLAG
51   , ARG_STRING
52   , ARG_INT
53 } cmdline_parser_arg_type;
54 
55 static
56 void clear_given (struct gengetopt_args_info *args_info);
57 static
58 void clear_args (struct gengetopt_args_info *args_info);
59 
60 static int
61 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
62                         struct cmdline_parser_params *params, const char *additional_error);
63 
64 
65 static char *
66 gengetopt_strdup (const char *s);
67 
68 static
clear_given(struct gengetopt_args_info * args_info)69 void clear_given (struct gengetopt_args_info *args_info)
70 {
71   args_info->help_given = 0 ;
72   args_info->version_given = 0 ;
73   args_info->conf_given = 0 ;
74   args_info->location_given = 0 ;
75   args_info->debug_given = 0 ;
76   args_info->foreground_given = 0 ;
77   args_info->default_config_given = 0 ;
78   args_info->metrics_given = 0 ;
79   args_info->bandwidth_given = 0 ;
80   args_info->convert_given = 0 ;
81   args_info->pid_file_given = 0 ;
82 }
83 
84 static
clear_args(struct gengetopt_args_info * args_info)85 void clear_args (struct gengetopt_args_info *args_info)
86 {
87   FIX_UNUSED (args_info);
88   args_info->conf_arg = gengetopt_strdup ("/usr/local/etc/gmond.conf");
89   args_info->conf_orig = NULL;
90   args_info->location_arg = gengetopt_strdup ("0,0,0");
91   args_info->location_orig = NULL;
92   args_info->debug_arg = 0;
93   args_info->debug_orig = NULL;
94   args_info->foreground_flag = 0;
95   args_info->default_config_flag = 0;
96   args_info->metrics_flag = 0;
97   args_info->bandwidth_flag = 0;
98   args_info->convert_arg = NULL;
99   args_info->convert_orig = NULL;
100   args_info->pid_file_arg = NULL;
101   args_info->pid_file_orig = NULL;
102 
103 }
104 
105 static
init_args_info(struct gengetopt_args_info * args_info)106 void init_args_info(struct gengetopt_args_info *args_info)
107 {
108 
109 
110   args_info->help_help = gengetopt_args_info_help[0] ;
111   args_info->version_help = gengetopt_args_info_help[1] ;
112   args_info->conf_help = gengetopt_args_info_help[2] ;
113   args_info->location_help = gengetopt_args_info_help[3] ;
114   args_info->debug_help = gengetopt_args_info_help[4] ;
115   args_info->foreground_help = gengetopt_args_info_help[5] ;
116   args_info->default_config_help = gengetopt_args_info_help[6] ;
117   args_info->metrics_help = gengetopt_args_info_help[7] ;
118   args_info->bandwidth_help = gengetopt_args_info_help[8] ;
119   args_info->convert_help = gengetopt_args_info_help[9] ;
120   args_info->pid_file_help = gengetopt_args_info_help[10] ;
121 
122 }
123 
124 void
cmdline_parser_print_version(void)125 cmdline_parser_print_version (void)
126 {
127   printf ("%s %s\n",
128      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
129      CMDLINE_PARSER_VERSION);
130 }
131 
print_help_common(void)132 static void print_help_common(void) {
133   cmdline_parser_print_version ();
134 
135   if (strlen(gengetopt_args_info_purpose) > 0)
136     printf("\n%s\n", gengetopt_args_info_purpose);
137 
138   if (strlen(gengetopt_args_info_usage) > 0)
139     printf("\n%s\n", gengetopt_args_info_usage);
140 
141   printf("\n");
142 
143   if (strlen(gengetopt_args_info_description) > 0)
144     printf("%s\n\n", gengetopt_args_info_description);
145 }
146 
147 void
cmdline_parser_print_help(void)148 cmdline_parser_print_help (void)
149 {
150   int i = 0;
151   print_help_common();
152   while (gengetopt_args_info_help[i])
153     printf("%s\n", gengetopt_args_info_help[i++]);
154 }
155 
156 void
cmdline_parser_init(struct gengetopt_args_info * args_info)157 cmdline_parser_init (struct gengetopt_args_info *args_info)
158 {
159   clear_given (args_info);
160   clear_args (args_info);
161   init_args_info (args_info);
162 }
163 
164 void
cmdline_parser_params_init(struct cmdline_parser_params * params)165 cmdline_parser_params_init(struct cmdline_parser_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 cmdline_parser_params *
cmdline_parser_params_create(void)178 cmdline_parser_params_create(void)
179 {
180   struct cmdline_parser_params *params =
181     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
182   cmdline_parser_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
cmdline_parser_release(struct gengetopt_args_info * args_info)198 cmdline_parser_release (struct gengetopt_args_info *args_info)
199 {
200 
201   free_string_field (&(args_info->conf_arg));
202   free_string_field (&(args_info->conf_orig));
203   free_string_field (&(args_info->location_arg));
204   free_string_field (&(args_info->location_orig));
205   free_string_field (&(args_info->debug_orig));
206   free_string_field (&(args_info->convert_arg));
207   free_string_field (&(args_info->convert_orig));
208   free_string_field (&(args_info->pid_file_arg));
209   free_string_field (&(args_info->pid_file_orig));
210 
211 
212 
213   clear_given (args_info);
214 }
215 
216 
217 static void
write_into_file(FILE * outfile,const char * opt,const char * arg,const char * values[])218 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
219 {
220   FIX_UNUSED (values);
221   if (arg) {
222     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
223   } else {
224     fprintf(outfile, "%s\n", opt);
225   }
226 }
227 
228 
229 int
cmdline_parser_dump(FILE * outfile,struct gengetopt_args_info * args_info)230 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
231 {
232   int i = 0;
233 
234   if (!outfile)
235     {
236       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
237       return EXIT_FAILURE;
238     }
239 
240   if (args_info->help_given)
241     write_into_file(outfile, "help", 0, 0 );
242   if (args_info->version_given)
243     write_into_file(outfile, "version", 0, 0 );
244   if (args_info->conf_given)
245     write_into_file(outfile, "conf", args_info->conf_orig, 0);
246   if (args_info->location_given)
247     write_into_file(outfile, "location", args_info->location_orig, 0);
248   if (args_info->debug_given)
249     write_into_file(outfile, "debug", args_info->debug_orig, 0);
250   if (args_info->foreground_given)
251     write_into_file(outfile, "foreground", 0, 0 );
252   if (args_info->default_config_given)
253     write_into_file(outfile, "default_config", 0, 0 );
254   if (args_info->metrics_given)
255     write_into_file(outfile, "metrics", 0, 0 );
256   if (args_info->bandwidth_given)
257     write_into_file(outfile, "bandwidth", 0, 0 );
258   if (args_info->convert_given)
259     write_into_file(outfile, "convert", args_info->convert_orig, 0);
260   if (args_info->pid_file_given)
261     write_into_file(outfile, "pid-file", args_info->pid_file_orig, 0);
262 
263 
264   i = EXIT_SUCCESS;
265   return i;
266 }
267 
268 int
cmdline_parser_file_save(const char * filename,struct gengetopt_args_info * args_info)269 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
270 {
271   FILE *outfile;
272   int i = 0;
273 
274   outfile = fopen(filename, "w");
275 
276   if (!outfile)
277     {
278       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
279       return EXIT_FAILURE;
280     }
281 
282   i = cmdline_parser_dump(outfile, args_info);
283   fclose (outfile);
284 
285   return i;
286 }
287 
288 void
cmdline_parser_free(struct gengetopt_args_info * args_info)289 cmdline_parser_free (struct gengetopt_args_info *args_info)
290 {
291   cmdline_parser_release (args_info);
292 }
293 
294 /** @brief replacement of strdup, which is not standard */
295 char *
gengetopt_strdup(const char * s)296 gengetopt_strdup (const char *s)
297 {
298   char *result = 0;
299   if (!s)
300     return result;
301 
302   result = (char*)malloc(strlen(s) + 1);
303   if (result == (char*)0)
304     return (char*)0;
305   strcpy(result, s);
306   return result;
307 }
308 
309 int
cmdline_parser(int argc,char ** argv,struct gengetopt_args_info * args_info)310 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
311 {
312   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
313 }
314 
315 int
cmdline_parser_ext(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params)316 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
317                    struct cmdline_parser_params *params)
318 {
319   int result;
320   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
321 
322   if (result == EXIT_FAILURE)
323     {
324       cmdline_parser_free (args_info);
325       exit (EXIT_FAILURE);
326     }
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   if (result == EXIT_FAILURE)
346     {
347       cmdline_parser_free (args_info);
348       exit (EXIT_FAILURE);
349     }
350 
351   return result;
352 }
353 
354 int
cmdline_parser_required(struct gengetopt_args_info * args_info,const char * prog_name)355 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
356 {
357   FIX_UNUSED (args_info);
358   FIX_UNUSED (prog_name);
359   return EXIT_SUCCESS;
360 }
361 
362 
363 static char *package_name = 0;
364 
365 /**
366  * @brief updates an option
367  * @param field the generic pointer to the field to update
368  * @param orig_field the pointer to the orig field
369  * @param field_given the pointer to the number of occurrence of this option
370  * @param prev_given the pointer to the number of occurrence already seen
371  * @param value the argument for this option (if null no arg was specified)
372  * @param possible_values the possible values for this option (if specified)
373  * @param default_value the default value (in case the option only accepts fixed values)
374  * @param arg_type the type of this option
375  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
376  * @param override @see cmdline_parser_params.override
377  * @param no_free whether to free a possible previous value
378  * @param multiple_option whether this is a multiple option
379  * @param long_opt the corresponding long option
380  * @param short_opt the corresponding short option (or '-' if none)
381  * @param additional_error possible further error specification
382  */
383 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)384 int update_arg(void *field, char **orig_field,
385                unsigned int *field_given, unsigned int *prev_given,
386                char *value, const char *possible_values[],
387                const char *default_value,
388                cmdline_parser_arg_type arg_type,
389                int check_ambiguity, int override,
390                int no_free, int multiple_option,
391                const char *long_opt, char short_opt,
392                const char *additional_error)
393 {
394   char *stop_char = 0;
395   const char *val = value;
396   int found;
397   char **string_field;
398   FIX_UNUSED (field);
399 
400   stop_char = 0;
401   found = 0;
402 
403   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
404     {
405       if (short_opt != '-')
406         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
407                package_name, long_opt, short_opt,
408                (additional_error ? additional_error : ""));
409       else
410         fprintf (stderr, "%s: `--%s' option given more than once%s\n",
411                package_name, long_opt,
412                (additional_error ? additional_error : ""));
413       return 1; /* failure */
414     }
415 
416   FIX_UNUSED (default_value);
417 
418   if (field_given && *field_given && ! override)
419     return 0;
420   if (prev_given)
421     (*prev_given)++;
422   if (field_given)
423     (*field_given)++;
424   if (possible_values)
425     val = possible_values[found];
426 
427   switch(arg_type) {
428   case ARG_FLAG:
429     *((int *)field) = !*((int *)field);
430     break;
431   case ARG_INT:
432     if (val) *((int *)field) = strtol (val, &stop_char, 0);
433     break;
434   case ARG_STRING:
435     if (val) {
436       string_field = (char **)field;
437       if (!no_free && *string_field)
438         free (*string_field); /* free previous string */
439       *string_field = gengetopt_strdup (val);
440     }
441     break;
442   default:
443     break;
444   };
445 
446   /* check numeric conversion */
447   switch(arg_type) {
448   case ARG_INT:
449     if (val && !(stop_char && *stop_char == '\0')) {
450       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
451       return 1; /* failure */
452     }
453     break;
454   default:
455     ;
456   };
457 
458   /* store the original value */
459   switch(arg_type) {
460   case ARG_NO:
461   case ARG_FLAG:
462     break;
463   default:
464     if (value && orig_field) {
465       if (no_free) {
466         *orig_field = value;
467       } else {
468         if (*orig_field)
469           free (*orig_field); /* free previous string */
470         *orig_field = gengetopt_strdup (value);
471       }
472     }
473   };
474 
475   return 0; /* OK */
476 }
477 
478 
479 int
cmdline_parser_internal(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params,const char * additional_error)480 cmdline_parser_internal (
481   int argc, char **argv, struct gengetopt_args_info *args_info,
482                         struct cmdline_parser_params *params, const char *additional_error)
483 {
484   int c;	/* Character of the parsed option.  */
485 
486   int error = 0;
487   struct gengetopt_args_info local_args_info;
488 
489   int override;
490   int initialize;
491   int check_required;
492   int check_ambiguity;
493 
494   package_name = argv[0];
495 
496   override = params->override;
497   initialize = params->initialize;
498   check_required = params->check_required;
499   check_ambiguity = params->check_ambiguity;
500 
501   if (initialize)
502     cmdline_parser_init (args_info);
503 
504   cmdline_parser_init (&local_args_info);
505 
506   optarg = 0;
507   optind = 0;
508   opterr = params->print_errors;
509   optopt = '?';
510 
511   while (1)
512     {
513       int option_index = 0;
514 
515       static struct option long_options[] = {
516         { "help",	0, NULL, 'h' },
517         { "version",	0, NULL, 'V' },
518         { "conf",	1, NULL, 'c' },
519         { "location",	1, NULL, 'l' },
520         { "debug",	1, NULL, 'd' },
521         { "foreground",	0, NULL, 'f' },
522         { "default_config",	0, NULL, 't' },
523         { "metrics",	0, NULL, 'm' },
524         { "bandwidth",	0, NULL, 'b' },
525         { "convert",	1, NULL, 'r' },
526         { "pid-file",	1, NULL, 'p' },
527         { 0,  0, 0, 0 }
528       };
529 
530       c = getopt_long (argc, argv, "hVc:l:d:ftmbr:p:z", long_options, &option_index);
531 
532       if (c == -1) break;	/* Exit from `while (1)' loop.  */
533 
534       switch (c)
535         {
536         case 'h':	/* Print help and exit.  */
537           cmdline_parser_print_help ();
538           cmdline_parser_free (&local_args_info);
539           exit (EXIT_SUCCESS);
540 
541         case 'V':	/* Print version and exit.  */
542           cmdline_parser_print_version ();
543           cmdline_parser_free (&local_args_info);
544           exit (EXIT_SUCCESS);
545 
546         case 'c':	/* Location of gmond configuration file.  */
547 
548 
549           if (update_arg( (void *)&(args_info->conf_arg),
550                &(args_info->conf_orig), &(args_info->conf_given),
551               &(local_args_info.conf_given), optarg, 0, "/usr/local/etc/gmond.conf", ARG_STRING,
552               check_ambiguity, override, 0, 0,
553               "conf", 'c',
554               additional_error))
555             goto failure;
556 
557           break;
558         case 'l':	/* Location of this host in the cluster 'rack,rank,plane'..  */
559 
560 
561           if (update_arg( (void *)&(args_info->location_arg),
562                &(args_info->location_orig), &(args_info->location_given),
563               &(local_args_info.location_given), optarg, 0, "0,0,0", ARG_STRING,
564               check_ambiguity, override, 0, 0,
565               "location", 'l',
566               additional_error))
567             goto failure;
568 
569           break;
570         case 'd':	/* Debug level. If greater than zero, daemon will stay in foreground..  */
571 
572 
573           if (update_arg( (void *)&(args_info->debug_arg),
574                &(args_info->debug_orig), &(args_info->debug_given),
575               &(local_args_info.debug_given), optarg, 0, "0", ARG_INT,
576               check_ambiguity, override, 0, 0,
577               "debug", 'd',
578               additional_error))
579             goto failure;
580 
581           break;
582         case 'f':	/* Run in foreground (don't daemonize).  */
583 
584 
585           if (update_arg((void *)&(args_info->foreground_flag), 0, &(args_info->foreground_given),
586               &(local_args_info.foreground_given), optarg, 0, 0, ARG_FLAG,
587               check_ambiguity, override, 1, 0, "foreground", 'f',
588               additional_error))
589             goto failure;
590 
591           break;
592         case 't':	/* Print the default configuration to stdout and exit.  */
593 
594 
595           if (update_arg((void *)&(args_info->default_config_flag), 0, &(args_info->default_config_given),
596               &(local_args_info.default_config_given), optarg, 0, 0, ARG_FLAG,
597               check_ambiguity, override, 1, 0, "default_config", 't',
598               additional_error))
599             goto failure;
600 
601           break;
602         case 'm':	/* Print the list of metrics this gmond supports.  */
603 
604 
605           if (update_arg((void *)&(args_info->metrics_flag), 0, &(args_info->metrics_given),
606               &(local_args_info.metrics_given), optarg, 0, 0, ARG_FLAG,
607               check_ambiguity, override, 1, 0, "metrics", 'm',
608               additional_error))
609             goto failure;
610 
611           break;
612         case 'b':	/* Calculate minimum bandwidth use for configuration.  */
613 
614 
615           if (update_arg((void *)&(args_info->bandwidth_flag), 0, &(args_info->bandwidth_given),
616               &(local_args_info.bandwidth_given), optarg, 0, 0, ARG_FLAG,
617               check_ambiguity, override, 1, 0, "bandwidth", 'b',
618               additional_error))
619             goto failure;
620 
621           break;
622         case 'r':	/* Convert a 2.5.x configuration file to the new 3.x format.  */
623 
624 
625           if (update_arg( (void *)&(args_info->convert_arg),
626                &(args_info->convert_orig), &(args_info->convert_given),
627               &(local_args_info.convert_given), optarg, 0, 0, ARG_STRING,
628               check_ambiguity, override, 0, 0,
629               "convert", 'r',
630               additional_error))
631             goto failure;
632 
633           break;
634         case 'p':	/* Write process-id to file.  */
635 
636 
637           if (update_arg( (void *)&(args_info->pid_file_arg),
638                &(args_info->pid_file_orig), &(args_info->pid_file_given),
639               &(local_args_info.pid_file_given), optarg, 0, 0, ARG_STRING,
640               check_ambiguity, override, 0, 0,
641               "pid-file", 'p',
642               additional_error))
643             goto failure;
644 
645           break;
646 
647         case 0:	/* Long option with no short option */
648         case '?':	/* Invalid option.  */
649           /* `getopt_long' already printed an error message.  */
650           goto failure;
651 
652         default:	/* bug: option not considered.  */
653           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
654           abort ();
655         } /* switch */
656     } /* while */
657 
658 
659 
660 
661   cmdline_parser_release (&local_args_info);
662 
663   if ( error )
664     return (EXIT_FAILURE);
665 
666   return 0;
667 
668 failure:
669 
670   cmdline_parser_release (&local_args_info);
671   return (EXIT_FAILURE);
672 }
673