IP to Geo

IP to Geo Git Source Tree

Root/src/cmdline.c

1/*
2 File autogenerated by gengetopt version 2.22.5
3 generated with the following command:
4 gengetopt --input cmdline.ggo
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
28const char *gengetopt_args_info_purpose = "Convert an IP to country code";
29
30const char *gengetopt_args_info_usage = "Usage: ip_to_geo --ip <ip>|--daemon [--port <port>] [--bind-ip <ip>]";
31
32const char *gengetopt_args_info_description = "Convert an IP to country code.";
33
34const char *gengetopt_args_info_help[] = {
35 " -h, --help Print help and exit",
36 " -V, --version Print version and exit",
37 "\nInteractive mode:",
38 " -i, --ip=ip IP to convert",
39 " -q, --quiet Quiet mode (default=off)",
40 " -v, --verbose Verbose mode (default=off)",
41 "\nDaemon mode:",
42 " -D, --daemon Daemonize (default=off)",
43 " -p, --port=INT Port (default=`53333')",
44 " -b, --bind-ip=STRING IP to bind to",
45 " -B, --no-background Don't go to background for daemon mode \n (default=off)",
46 "\nAdvanced daemon options:",
47 " -s, --sockets-per-thread=INT Number of sockets managed by a single thread \n (default=`10')",
48 " -t, --sockets-timeout=INT Close connection after X seconds (default=`5')",
49 " -r, --client-max-requests=INT Number of requests allowed for one client \n (default=`100')",
50 0
51};
52
53typedef enum {ARG_NO
54 , ARG_FLAG
55 , ARG_STRING
56 , ARG_INT
57} cmdline_parser_arg_type;
58
59static
60void clear_given (struct gengetopt_args_info *args_info);
61static
62void clear_args (struct gengetopt_args_info *args_info);
63
64static int
65cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
66 struct cmdline_parser_params *params, const char *additional_error);
67
68
69static char *
70gengetopt_strdup (const char *s);
71
72static
73void clear_given (struct gengetopt_args_info *args_info)
74{
75 args_info->help_given = 0 ;
76 args_info->version_given = 0 ;
77 args_info->ip_given = 0 ;
78 args_info->quiet_given = 0 ;
79 args_info->verbose_given = 0 ;
80 args_info->daemon_given = 0 ;
81 args_info->port_given = 0 ;
82 args_info->bind_ip_given = 0 ;
83 args_info->no_background_given = 0 ;
84 args_info->sockets_per_thread_given = 0 ;
85 args_info->sockets_timeout_given = 0 ;
86 args_info->client_max_requests_given = 0 ;
87}
88
89static
90void clear_args (struct gengetopt_args_info *args_info)
91{
92 FIX_UNUSED (args_info);
93 args_info->ip_arg = NULL;
94 args_info->ip_orig = NULL;
95 args_info->quiet_flag = 0;
96 args_info->verbose_flag = 0;
97 args_info->daemon_flag = 0;
98 args_info->port_arg = 53333;
99 args_info->port_orig = NULL;
100 args_info->bind_ip_arg = NULL;
101 args_info->bind_ip_orig = NULL;
102 args_info->no_background_flag = 0;
103 args_info->sockets_per_thread_arg = 10;
104 args_info->sockets_per_thread_orig = NULL;
105 args_info->sockets_timeout_arg = 5;
106 args_info->sockets_timeout_orig = NULL;
107 args_info->client_max_requests_arg = 100;
108 args_info->client_max_requests_orig = NULL;
109
110}
111
112static
113void init_args_info(struct gengetopt_args_info *args_info)
114{
115
116
117 args_info->help_help = gengetopt_args_info_help[0] ;
118 args_info->version_help = gengetopt_args_info_help[1] ;
119 args_info->ip_help = gengetopt_args_info_help[3] ;
120 args_info->quiet_help = gengetopt_args_info_help[4] ;
121 args_info->verbose_help = gengetopt_args_info_help[5] ;
122 args_info->daemon_help = gengetopt_args_info_help[7] ;
123 args_info->port_help = gengetopt_args_info_help[8] ;
124 args_info->bind_ip_help = gengetopt_args_info_help[9] ;
125 args_info->no_background_help = gengetopt_args_info_help[10] ;
126 args_info->sockets_per_thread_help = gengetopt_args_info_help[12] ;
127 args_info->sockets_timeout_help = gengetopt_args_info_help[13] ;
128 args_info->client_max_requests_help = gengetopt_args_info_help[14] ;
129
130}
131
132void
133cmdline_parser_print_version (void)
134{
135 printf ("%s %s\n",
136 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
137 CMDLINE_PARSER_VERSION);
138}
139
140static void print_help_common(void) {
141 cmdline_parser_print_version ();
142
143 if (strlen(gengetopt_args_info_purpose) > 0)
144 printf("\n%s\n", gengetopt_args_info_purpose);
145
146 if (strlen(gengetopt_args_info_usage) > 0)
147 printf("\n%s\n", gengetopt_args_info_usage);
148
149 printf("\n");
150
151 if (strlen(gengetopt_args_info_description) > 0)
152 printf("%s\n\n", gengetopt_args_info_description);
153}
154
155void
156cmdline_parser_print_help (void)
157{
158 int i = 0;
159 print_help_common();
160 while (gengetopt_args_info_help[i])
161 printf("%s\n", gengetopt_args_info_help[i++]);
162}
163
164void
165cmdline_parser_init (struct gengetopt_args_info *args_info)
166{
167 clear_given (args_info);
168 clear_args (args_info);
169 init_args_info (args_info);
170}
171
172void
173cmdline_parser_params_init(struct cmdline_parser_params *params)
174{
175 if (params)
176 {
177 params->override = 0;
178 params->initialize = 1;
179 params->check_required = 1;
180 params->check_ambiguity = 0;
181 params->print_errors = 1;
182 }
183}
184
185struct cmdline_parser_params *
186cmdline_parser_params_create(void)
187{
188 struct cmdline_parser_params *params =
189 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
190 cmdline_parser_params_init(params);
191 return params;
192}
193
194static void
195free_string_field (char **s)
196{
197 if (*s)
198 {
199 free (*s);
200 *s = 0;
201 }
202}
203
204
205static void
206cmdline_parser_release (struct gengetopt_args_info *args_info)
207{
208
209 free_string_field (&(args_info->ip_arg));
210 free_string_field (&(args_info->ip_orig));
211 free_string_field (&(args_info->port_orig));
212 free_string_field (&(args_info->bind_ip_arg));
213 free_string_field (&(args_info->bind_ip_orig));
214 free_string_field (&(args_info->sockets_per_thread_orig));
215 free_string_field (&(args_info->sockets_timeout_orig));
216 free_string_field (&(args_info->client_max_requests_orig));
217
218
219
220 clear_given (args_info);
221}
222
223
224static void
225write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
226{
227 FIX_UNUSED (values);
228 if (arg) {
229 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
230 } else {
231 fprintf(outfile, "%s\n", opt);
232 }
233}
234
235
236int
237cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
238{
239 int i = 0;
240
241 if (!outfile)
242 {
243 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
244 return EXIT_FAILURE;
245 }
246
247 if (args_info->help_given)
248 write_into_file(outfile, "help", 0, 0 );
249 if (args_info->version_given)
250 write_into_file(outfile, "version", 0, 0 );
251 if (args_info->ip_given)
252 write_into_file(outfile, "ip", args_info->ip_orig, 0);
253 if (args_info->quiet_given)
254 write_into_file(outfile, "quiet", 0, 0 );
255 if (args_info->verbose_given)
256 write_into_file(outfile, "verbose", 0, 0 );
257 if (args_info->daemon_given)
258 write_into_file(outfile, "daemon", 0, 0 );
259 if (args_info->port_given)
260 write_into_file(outfile, "port", args_info->port_orig, 0);
261 if (args_info->bind_ip_given)
262 write_into_file(outfile, "bind-ip", args_info->bind_ip_orig, 0);
263 if (args_info->no_background_given)
264 write_into_file(outfile, "no-background", 0, 0 );
265 if (args_info->sockets_per_thread_given)
266 write_into_file(outfile, "sockets-per-thread", args_info->sockets_per_thread_orig, 0);
267 if (args_info->sockets_timeout_given)
268 write_into_file(outfile, "sockets-timeout", args_info->sockets_timeout_orig, 0);
269 if (args_info->client_max_requests_given)
270 write_into_file(outfile, "client-max-requests", args_info->client_max_requests_orig, 0);
271
272
273 i = EXIT_SUCCESS;
274 return i;
275}
276
277int
278cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
279{
280 FILE *outfile;
281 int i = 0;
282
283 outfile = fopen(filename, "w");
284
285 if (!outfile)
286 {
287 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
288 return EXIT_FAILURE;
289 }
290
291 i = cmdline_parser_dump(outfile, args_info);
292 fclose (outfile);
293
294 return i;
295}
296
297void
298cmdline_parser_free (struct gengetopt_args_info *args_info)
299{
300 cmdline_parser_release (args_info);
301}
302
303/** @brief replacement of strdup, which is not standard */
304char *
305gengetopt_strdup (const char *s)
306{
307 char *result = 0;
308 if (!s)
309 return result;
310
311 result = (char*)malloc(strlen(s) + 1);
312 if (result == (char*)0)
313 return (char*)0;
314 strcpy(result, s);
315 return result;
316}
317
318int
319cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
320{
321 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
322}
323
324int
325cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
326 struct cmdline_parser_params *params)
327{
328 int result;
329 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
330
331 if (result == EXIT_FAILURE)
332 {
333 cmdline_parser_free (args_info);
334 exit (EXIT_FAILURE);
335 }
336
337 return result;
338}
339
340int
341cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
342{
343 int result;
344 struct cmdline_parser_params params;
345
346 params.override = override;
347 params.initialize = initialize;
348 params.check_required = check_required;
349 params.check_ambiguity = 0;
350 params.print_errors = 1;
351
352 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
353
354 if (result == EXIT_FAILURE)
355 {
356 cmdline_parser_free (args_info);
357 exit (EXIT_FAILURE);
358 }
359
360 return result;
361}
362
363int
364cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
365{
366 FIX_UNUSED (args_info);
367 FIX_UNUSED (prog_name);
368 return EXIT_SUCCESS;
369}
370
371
372static char *package_name = 0;
373
374/**
375 * @brief updates an option
376 * @param field the generic pointer to the field to update
377 * @param orig_field the pointer to the orig field
378 * @param field_given the pointer to the number of occurrence of this option
379 * @param prev_given the pointer to the number of occurrence already seen
380 * @param value the argument for this option (if null no arg was specified)
381 * @param possible_values the possible values for this option (if specified)
382 * @param default_value the default value (in case the option only accepts fixed values)
383 * @param arg_type the type of this option
384 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
385 * @param override @see cmdline_parser_params.override
386 * @param no_free whether to free a possible previous value
387 * @param multiple_option whether this is a multiple option
388 * @param long_opt the corresponding long option
389 * @param short_opt the corresponding short option (or '-' if none)
390 * @param additional_error possible further error specification
391 */
392static
393int update_arg(void *field, char **orig_field,
394 unsigned int *field_given, unsigned int *prev_given,
395 char *value, const char *possible_values[],
396 const char *default_value,
397 cmdline_parser_arg_type arg_type,
398 int check_ambiguity, int override,
399 int no_free, int multiple_option,
400 const char *long_opt, char short_opt,
401 const char *additional_error)
402{
403 char *stop_char = 0;
404 const char *val = value;
405 int found;
406 char **string_field;
407 FIX_UNUSED (field);
408
409 stop_char = 0;
410 found = 0;
411
412 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
413 {
414 if (short_opt != '-')
415 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
416 package_name, long_opt, short_opt,
417 (additional_error ? additional_error : ""));
418 else
419 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
420 package_name, long_opt,
421 (additional_error ? additional_error : ""));
422 return 1; /* failure */
423 }
424
425 FIX_UNUSED (default_value);
426
427 if (field_given && *field_given && ! override)
428 return 0;
429 if (prev_given)
430 (*prev_given)++;
431 if (field_given)
432 (*field_given)++;
433 if (possible_values)
434 val = possible_values[found];
435
436 switch(arg_type) {
437 case ARG_FLAG:
438 *((int *)field) = !*((int *)field);
439 break;
440 case ARG_INT:
441 if (val) *((int *)field) = strtol (val, &stop_char, 0);
442 break;
443 case ARG_STRING:
444 if (val) {
445 string_field = (char **)field;
446 if (!no_free && *string_field)
447 free (*string_field); /* free previous string */
448 *string_field = gengetopt_strdup (val);
449 }
450 break;
451 default:
452 break;
453 };
454
455 /* check numeric conversion */
456 switch(arg_type) {
457 case ARG_INT:
458 if (val && !(stop_char && *stop_char == '\0')) {
459 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
460 return 1; /* failure */
461 }
462 break;
463 default:
464 ;
465 };
466
467 /* store the original value */
468 switch(arg_type) {
469 case ARG_NO:
470 case ARG_FLAG:
471 break;
472 default:
473 if (value && orig_field) {
474 if (no_free) {
475 *orig_field = value;
476 } else {
477 if (*orig_field)
478 free (*orig_field); /* free previous string */
479 *orig_field = gengetopt_strdup (value);
480 }
481 }
482 };
483
484 return 0; /* OK */
485}
486
487
488int
489cmdline_parser_internal (
490 int argc, char **argv, struct gengetopt_args_info *args_info,
491 struct cmdline_parser_params *params, const char *additional_error)
492{
493 int c;/* Character of the parsed option. */
494
495 int error = 0;
496 struct gengetopt_args_info local_args_info;
497
498 int override;
499 int initialize;
500 int check_required;
501 int check_ambiguity;
502
503 package_name = argv[0];
504
505 override = params->override;
506 initialize = params->initialize;
507 check_required = params->check_required;
508 check_ambiguity = params->check_ambiguity;
509
510 if (initialize)
511 cmdline_parser_init (args_info);
512
513 cmdline_parser_init (&local_args_info);
514
515 optarg = 0;
516 optind = 0;
517 opterr = params->print_errors;
518 optopt = '?';
519
520 while (1)
521 {
522 int option_index = 0;
523
524 static struct option long_options[] = {
525 { "help",0, NULL, 'h' },
526 { "version",0, NULL, 'V' },
527 { "ip",1, NULL, 'i' },
528 { "quiet",0, NULL, 'q' },
529 { "verbose",0, NULL, 'v' },
530 { "daemon",0, NULL, 'D' },
531 { "port",1, NULL, 'p' },
532 { "bind-ip",1, NULL, 'b' },
533 { "no-background",0, NULL, 'B' },
534 { "sockets-per-thread",1, NULL, 's' },
535 { "sockets-timeout",1, NULL, 't' },
536 { "client-max-requests",1, NULL, 'r' },
537 { 0, 0, 0, 0 }
538 };
539
540 c = getopt_long (argc, argv, "hVi:qvDp:b:Bs:t:r:", long_options, &option_index);
541
542 if (c == -1) break;/* Exit from `while (1)' loop. */
543
544 switch (c)
545 {
546 case 'h':/* Print help and exit. */
547 cmdline_parser_print_help ();
548 cmdline_parser_free (&local_args_info);
549 exit (EXIT_SUCCESS);
550
551 case 'V':/* Print version and exit. */
552 cmdline_parser_print_version ();
553 cmdline_parser_free (&local_args_info);
554 exit (EXIT_SUCCESS);
555
556 case 'i':/* IP to convert. */
557
558
559 if (update_arg( (void *)&(args_info->ip_arg),
560 &(args_info->ip_orig), &(args_info->ip_given),
561 &(local_args_info.ip_given), optarg, 0, 0, ARG_STRING,
562 check_ambiguity, override, 0, 0,
563 "ip", 'i',
564 additional_error))
565 goto failure;
566
567 break;
568 case 'q':/* Quiet mode. */
569
570
571 if (update_arg((void *)&(args_info->quiet_flag), 0, &(args_info->quiet_given),
572 &(local_args_info.quiet_given), optarg, 0, 0, ARG_FLAG,
573 check_ambiguity, override, 1, 0, "quiet", 'q',
574 additional_error))
575 goto failure;
576
577 break;
578 case 'v':/* Verbose mode. */
579
580
581 if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
582 &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
583 check_ambiguity, override, 1, 0, "verbose", 'v',
584 additional_error))
585 goto failure;
586
587 break;
588 case 'D':/* Daemonize. */
589
590
591 if (update_arg((void *)&(args_info->daemon_flag), 0, &(args_info->daemon_given),
592 &(local_args_info.daemon_given), optarg, 0, 0, ARG_FLAG,
593 check_ambiguity, override, 1, 0, "daemon", 'D',
594 additional_error))
595 goto failure;
596
597 break;
598 case 'p':/* Port. */
599
600
601 if (update_arg( (void *)&(args_info->port_arg),
602 &(args_info->port_orig), &(args_info->port_given),
603 &(local_args_info.port_given), optarg, 0, "53333", ARG_INT,
604 check_ambiguity, override, 0, 0,
605 "port", 'p',
606 additional_error))
607 goto failure;
608
609 break;
610 case 'b':/* IP to bind to. */
611
612
613 if (update_arg( (void *)&(args_info->bind_ip_arg),
614 &(args_info->bind_ip_orig), &(args_info->bind_ip_given),
615 &(local_args_info.bind_ip_given), optarg, 0, 0, ARG_STRING,
616 check_ambiguity, override, 0, 0,
617 "bind-ip", 'b',
618 additional_error))
619 goto failure;
620
621 break;
622 case 'B':/* Don't go to background for daemon mode. */
623
624
625 if (update_arg((void *)&(args_info->no_background_flag), 0, &(args_info->no_background_given),
626 &(local_args_info.no_background_given), optarg, 0, 0, ARG_FLAG,
627 check_ambiguity, override, 1, 0, "no-background", 'B',
628 additional_error))
629 goto failure;
630
631 break;
632 case 's':/* Number of sockets managed by a single thread. */
633
634
635 if (update_arg( (void *)&(args_info->sockets_per_thread_arg),
636 &(args_info->sockets_per_thread_orig), &(args_info->sockets_per_thread_given),
637 &(local_args_info.sockets_per_thread_given), optarg, 0, "10", ARG_INT,
638 check_ambiguity, override, 0, 0,
639 "sockets-per-thread", 's',
640 additional_error))
641 goto failure;
642
643 break;
644 case 't':/* Close connection after X seconds. */
645
646
647 if (update_arg( (void *)&(args_info->sockets_timeout_arg),
648 &(args_info->sockets_timeout_orig), &(args_info->sockets_timeout_given),
649 &(local_args_info.sockets_timeout_given), optarg, 0, "5", ARG_INT,
650 check_ambiguity, override, 0, 0,
651 "sockets-timeout", 't',
652 additional_error))
653 goto failure;
654
655 break;
656 case 'r':/* Number of requests allowed for one client. */
657
658
659 if (update_arg( (void *)&(args_info->client_max_requests_arg),
660 &(args_info->client_max_requests_orig), &(args_info->client_max_requests_given),
661 &(local_args_info.client_max_requests_given), optarg, 0, "100", ARG_INT,
662 check_ambiguity, override, 0, 0,
663 "client-max-requests", 'r',
664 additional_error))
665 goto failure;
666
667 break;
668
669 case 0:/* Long option with no short option */
670 case '?':/* Invalid option. */
671 /* `getopt_long' already printed an error message. */
672 goto failure;
673
674 default:/* bug: option not considered. */
675 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
676 abort ();
677 } /* switch */
678 } /* while */
679
680
681
682
683 cmdline_parser_release (&local_args_info);
684
685 if ( error )
686 return (EXIT_FAILURE);
687
688 return 0;
689
690failure:
691
692 cmdline_parser_release (&local_args_info);
693 return (EXIT_FAILURE);
694}

Archive Download this file

Branches

Tags