1/*
2  File autogenerated by gengetopt @generator_version@
3  generated with the following command:
4  @command_line@
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@IF@ not include_getopt @THEN@
25#include <getopt.h>
26@ENDIF@
27
28#include "@source_name@.@header_file_ext@"
29
30const char *@args_info@_purpose = "@purpose@";
31
32const char *@args_info@_usage = "Usage: @if@ no_package @then@" @package_var_name@ "@endif@@usage_string@";
33
34const char *@args_info@_versiontext = "@versiontext@";
35
36const char *@args_info@_description = "@description@";
37
38@IF@ has_hidden or has_details @THEN@
39@IF@ has_details @THEN@
40const char *@args_info@_detailed_help[] = {
41  @detailed_help_option_print:method@  0
42};
43@IF@ has_hidden @THEN@
44static void
45init_full_help_array(void)
46{
47  @full_help_option_print:method@
48}
49
50const char *@args_info@_full_help[@help_string_num@];
51@ENDIF@
52@ELSEIF@ has_hidden @THEN@
53const char *@args_info@_full_help[] = {
54  @full_help_option_print:method@  0
55};
56@ENDIF@
57
58static void
59init_help_array(void)
60{
61  @help_option_print:method@
62}
63
64const char *@args_info@_help[@help_string_num@];
65@ELSE@
66const char *@args_info@_help[] = {
67  @help_option_print:method@  0
68};
69@ENDIF@
70
71typedef enum {ARG_NO
72@IF@ has_arg_flag @THEN@
73  , ARG_FLAG
74@ENDIF@
75@IF@ has_arg_string @THEN@
76  , ARG_STRING
77@ENDIF@
78@IF@ has_arg_int @THEN@
79  , ARG_INT
80@ENDIF@
81@IF@ has_arg_short @THEN@
82  , ARG_SHORT
83@ENDIF@
84@IF@ has_arg_long @THEN@
85  , ARG_LONG
86@ENDIF@
87@IF@ has_arg_float @THEN@
88  , ARG_FLOAT
89@ENDIF@
90@IF@ has_arg_double @THEN@
91  , ARG_DOUBLE
92@ENDIF@
93@IF@ has_arg_longdouble @THEN@
94  , ARG_LONGDOUBLE
95@ENDIF@
96@IF@ has_arg_longlong @THEN@
97  , ARG_LONGLONG
98@ENDIF@
99@IF@ has_arg_enum @THEN@
100  , ARG_ENUM
101@ENDIF@} @parser_name@_arg_type;
102
103static
104void clear_given (struct @args_info@ *args_info);
105static
106void clear_args (struct @args_info@ *args_info);
107
108static int
109@parser_name@_internal (int argc, char **argv, struct @args_info@ *args_info,
110                        struct @parser_name@_params *params, const char *additional_error);
111
112@IF@ check_required_options @THEN@
113static int
114@parser_name@_required2 (struct @args_info@ *args_info, const char *prog_name, const char *additional_error);
115@ENDIF@
116@IF@ cmd_list @THEN@
117struct line_list
118{
119  char * string_arg;
120  struct line_list * next;
121};
122
123static struct line_list *cmd_line_list = 0;
124static struct line_list *cmd_line_list_tmp = 0;
125
126static void
127free_cmd_list(void)
128{
129  /* free the list of a previous call */
130  if (cmd_line_list)
131    {
132      while (cmd_line_list) {
133        cmd_line_list_tmp = cmd_line_list;
134        cmd_line_list = cmd_line_list->next;
135        free (cmd_line_list_tmp->string_arg);
136        free (cmd_line_list_tmp);
137      }
138    }
139}
140
141@ENDIF@
142
143@option_values:method{iteration=true}@
144@IF@ do_generate_strdup @THEN@
145static char *
146gengetopt_strdup (const char *s);
147
148@ENDIF@
149static
150void clear_given (struct @args_info@ *args_info)
151{
152  @given_init:method{iteration=true}@
153}
154
155static
156void clear_args (struct @args_info@ *args_info)
157{
158  FIX_UNUSED (args_info);
159  @clear_arg:method@
160}
161
162static
163void init_args_info(struct @args_info@ *args_info)
164{
165@IF@ has_hidden and has_details @then@  init_full_help_array(); @endif@
166@IF@ has_hidden or has_details @then@  init_help_array(); @endif@
167  @init_args_info:method@
168}
169
170void
171@parser_name@_print_version (void)
172{
173  printf ("%s %s\n",
174     (strlen(@package_var_name@_NAME) ? @package_var_name@_NAME : @package_var_name@),
175     @version_var_name@);
176
177  if (strlen(@args_info@_versiontext) > 0)
178    printf("\n%s\n", @args_info@_versiontext);
179}
180
181static void print_help_common(void)
182{
183	size_t len_purpose = strlen(@args_info@_purpose);
184	size_t len_usage = strlen(@args_info@_usage);
185
186	if (len_usage > 0) {
187		printf("%s\n", @args_info@_usage);
188	}
189	if (len_purpose > 0) {
190		printf("%s\n", @args_info@_purpose);
191	}
192
193	if (len_usage || len_purpose) {
194		printf("\n");
195	}
196
197	if (strlen(@args_info@_description) > 0) {
198		printf("%s\n\n", @args_info@_description);
199	}
200}
201
202void
203@parser_name@_print_help (void)
204{
205  int i = 0;
206  print_help_common();
207  while (@args_info@_help[i])
208    printf("%s\n", @args_info@_help[i++]);
209}
210
211@IF@ has_hidden @THEN@
212void
213@parser_name@_print_full_help (void)
214{
215  int i = 0;
216  print_help_common();
217  while (@args_info@_full_help[i])
218    printf("%s\n", @args_info@_full_help[i++]);
219}
220
221@ENDIF@
222@IF@ has_details @THEN@
223void
224@parser_name@_print_detailed_help (void)
225{
226  int i = 0;
227  print_help_common();
228  while (@args_info@_detailed_help[i])
229    printf("%s\n", @args_info@_detailed_help[i++]);
230}
231
232@ENDIF@
233void
234@parser_name@_init (struct @args_info@ *args_info)
235{
236  clear_given (args_info);
237  clear_args (args_info);
238  init_args_info (args_info);
239@IF@ handle_unnamed @THEN@
240
241  args_info->inputs = 0;
242  args_info->inputs_num = 0;
243@ENDIF@
244}
245
246void
247@parser_name@_params_init(struct @parser_name@_params *params)
248{
249  if (params)
250    {
251      params->override = 0;
252      params->initialize = 1;
253      params->check_required = 1;
254      params->check_ambiguity = 0;
255      params->print_errors = 1;
256    }
257}
258
259struct @parser_name@_params *
260@parser_name@_params_create(void)
261{
262  struct @parser_name@_params *params =
263    (struct @parser_name@_params *)malloc(sizeof(struct @parser_name@_params));
264  @parser_name@_params_init(params);
265  return params;
266}
267
268@IF@ has_typed_options @THEN@
269static void
270free_string_field (char **s)
271{
272  if (*s)
273    {
274      free (*s);
275      *s = 0;
276    }
277}
278@ENDIF@
279
280@IF@ multiple_token_functions @THEN@
281/** @@brief generic value variable */
282union generic_value {
283@IF@ has_arg_int or has_arg_enum @THEN@
284    int int_arg;
285@ENDIF@
286@IF@ has_arg_short @THEN@
287    short short_arg;
288@ENDIF@
289@IF@ has_arg_long @THEN@
290    long long_arg;
291@ENDIF@
292@IF@ has_arg_float @THEN@
293    float float_arg;
294@ENDIF@
295@IF@ has_arg_double @THEN@
296    double double_arg;
297@ENDIF@
298@IF@ has_arg_longdouble @THEN@
299    long double longdouble_arg;
300@ENDIF@
301@IF@ has_arg_longlong @THEN@
302#if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT)
303    long long int longlong_arg;
304#else
305    long longlong_arg;
306#endif
307@ENDIF@
308    char *string_arg;
309    const char *default_string_arg;
310};
311
312/** @@brief holds temporary values for multiple options */
313struct generic_list
314{
315  union generic_value arg;
316  char *orig;
317  struct generic_list *next;
318};
319
320/**
321 * @@brief add a node at the head of the list
322 */
323static void add_node(struct generic_list **list) {
324  struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list));
325  new_node->next = *list;
326  *list = new_node;
327  new_node->arg.string_arg = 0;
328  new_node->orig = 0;
329}
330
331@IF@ (not multiple_options_all_string) and multiple_token_functions @THEN@
332/**
333 * The passed arg parameter is NOT set to 0 from this function
334 */
335static void
336free_multiple_field(unsigned int len, void *arg, char ***orig)
337{
338  unsigned int i;
339  if (arg) {
340    for (i = 0; i < len; ++i)
341      {
342        free_string_field(&((*orig)[i]));
343      }
344
345    free (arg);
346    free (*orig);
347    *orig = 0;
348  }
349}
350@ENDIF@
351
352@IF@ multiple_options_string @THEN@
353static void
354free_multiple_string_field(unsigned int len, char ***arg, char ***orig)
355{
356  unsigned int i;
357  if (*arg) {
358    for (i = 0; i < len; ++i)
359      {
360        free_string_field(&((*arg)[i]));
361        free_string_field(&((*orig)[i]));
362      }
363    free_string_field(&((*arg)[0])); /* free default string */
364
365    free (*arg);
366    *arg = 0;
367    free (*orig);
368    *orig = 0;
369  }
370}
371@ENDIF@
372@ENDIF@
373
374static void
375@parser_name@_release (struct @args_info@ *args_info)
376{
377@IF@ handle_unnamed @THEN@
378  unsigned int i;@ENDIF@
379
380  @free:method@
381
382@IF@ handle_unnamed @THEN@
383  for (i = 0; i < args_info->inputs_num; ++i)
384    free (args_info->inputs [i]);
385
386  if (args_info->inputs_num)
387    free (args_info->inputs);
388@ENDIF@
389
390  clear_given (args_info);
391}
392
393@IF@ check_possible_values @THEN@
394/**
395 * @@param val the value to check
396 * @@param values the possible values
397 * @@return the index of the matched value:
398 * -1 if no value matched,
399 * -2 if more than one value has matched
400 */
401static int
402check_possible_values(const char *val, const char *values[])
403{
404  int i, found, last;
405  size_t len;
406
407  if (!val)   /* otherwise strlen() crashes below */
408    return -1; /* -1 means no argument for the option */
409
410  found = last = 0;
411
412  for (i = 0, len = strlen(val); values[i]; ++i)
413    {
414      if (strncmp(val, values[i], len) == 0)
415        {
416          ++found;
417          last = i;
418          if (strlen(values[i]) == len)
419            return i; /* exact macth no need to check more */
420        }
421    }
422
423  if (found == 1) /* one match: OK */
424    return last;
425
426  return (found ? -2 : -1); /* return many values or none matched */
427}
428
429@ENDIF@
430
431static void
432write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
433{
434  @IF@ check_possible_values @THEN@
435  int found = -1;
436  @ELSE@
437  FIX_UNUSED (values);
438  @ENDIF@
439  if (arg) {
440    @IF@ check_possible_values @THEN@
441    if (values) {
442      found = check_possible_values(arg, values);
443    }
444    if (found >= 0)
445      fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
446    else
447      fprintf(outfile, "%s=\"%s\"\n", opt, arg);
448    @ELSE@
449    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
450    @ENDIF@
451  } else {
452    fprintf(outfile, "%s\n", opt);
453  }
454}
455
456@IF@ multiple_options @THEN@
457static void
458write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
459{
460  int i;
461
462  for (i = 0; i < len; ++i)
463    write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
464}
465@ENDIF@
466
467int
468@parser_name@_dump(FILE *outfile, struct @args_info@ *args_info)
469{
470  int i = 0;
471
472  if (!outfile)
473    {
474      fprintf (stderr, "%s: cannot dump options to stream\n", @package_var_name@);
475      return EXIT_FAILURE;
476    }
477
478  @file_save_loop:method@
479
480  i = EXIT_SUCCESS;
481  return i;
482}
483
484int
485@parser_name@_file_save(const char *filename, struct @args_info@ *args_info)
486{
487  FILE *outfile;
488  int i = 0;
489
490  outfile = fopen(filename, "w");
491
492  if (!outfile)
493    {
494      fprintf (stderr, "%s: cannot open file for writing: %s\n", @package_var_name@, filename);
495      return EXIT_FAILURE;
496    }
497
498  i = @parser_name@_dump(outfile, args_info);
499  fclose (outfile);
500
501  return i;
502}
503
504void
505@parser_name@_free (struct @args_info@ *args_info)
506{
507  @parser_name@_release (args_info);
508}
509
510@IF@ do_generate_strdup @THEN@
511/** @@brief replacement of strdup, which is not standard */
512char *
513gengetopt_strdup (const char *s)
514{
515  char *result = 0;
516  if (!s)
517    return result;
518
519  result = (char*)malloc(strlen(s) + 1);
520  if (result == (char*)0)
521    return (char*)0;
522  strcpy(result, s);
523  return result;
524}
525
526@ENDIF@
527@IF@ multiple_token_functions @THEN@
528static char *
529get_multiple_arg_token(const char *arg)
530{
531  const char *tok;
532  char *ret;
533  size_t len, num_of_escape, i, j;
534
535  if (!arg)
536    return 0;
537
538  tok = strchr (arg, ',');
539  num_of_escape = 0;
540
541  /* make sure it is not escaped */
542  while (tok)
543    {
544      if (*(tok-1) == '\\')
545        {
546          /* find the next one */
547          tok = strchr (tok+1, ',');
548          ++num_of_escape;
549        }
550      else
551        break;
552    }
553
554  if (tok)
555    len = (size_t)(tok - arg + 1);
556  else
557    len = strlen (arg) + 1;
558
559  len -= num_of_escape;
560
561  ret = (char *) malloc (len);
562
563  i = 0;
564  j = 0;
565  while (arg[i] && (j < len-1))
566    {
567      if (arg[i] == '\\' &&
568	  arg[ i + 1 ] &&
569	  arg[ i + 1 ] == ',')
570        ++i;
571
572      ret[j++] = arg[i++];
573    }
574
575  ret[len-1] = '\0';
576
577  return ret;
578}
579
580static const char *
581get_multiple_arg_token_next(const char *arg)
582{
583  const char *tok;
584
585  if (!arg)
586    return 0;
587
588  tok = strchr (arg, ',');
589
590  /* make sure it is not escaped */
591  while (tok)
592    {
593      if (*(tok-1) == '\\')
594        {
595          /* find the next one */
596          tok = strchr (tok+1, ',');
597        }
598      else
599        break;
600    }
601
602  if (! tok || strlen(tok) == 1)
603    return 0;
604
605  return tok+1;
606}
607
608@ENDIF@
609@IF@ multiple_options @THEN@
610static int
611check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
612
613int
614check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
615{
616  int error_occurred = 0;
617
618  if (option_given && (min > 0 || max > 0))
619    {
620      if (min > 0 && max > 0)
621        {
622          if (min == max)
623            {
624              /* specific occurrences */
625              if (option_given != (unsigned int) min)
626                {
627                  fprintf (stderr, "%s: %s option occurrences must be %d\n",
628                    prog_name, option_desc, min);
629                  error_occurred = 1;
630                }
631            }
632          else if (option_given < (unsigned int) min
633                || option_given > (unsigned int) max)
634            {
635              /* range occurrences */
636              fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
637                prog_name, option_desc, min, max);
638              error_occurred = 1;
639            }
640        }
641      else if (min > 0)
642        {
643          /* at least check */
644          if (option_given < min)
645            {
646              fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
647                prog_name, option_desc, min);
648              error_occurred = 1;
649            }
650        }
651      else if (max > 0)
652        {
653          /* at most check */
654          if (option_given > max)
655            {
656              fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
657                prog_name, option_desc, max);
658              error_occurred = 1;
659            }
660        }
661    }
662
663  return error_occurred;
664}
665@ENDIF@
666@reset_groups:method{iteration=true}@
667int
668@parser_name@ (int argc, char **argv, struct @args_info@ *args_info)
669{
670  return @parser_name@2 (argc, argv, args_info, 0, 1, 1);
671}
672
673int
674@parser_name@_ext (int argc, char **argv, struct @args_info@ *args_info,
675                   struct @parser_name@_params *params)
676{
677  int result;
678  result = @parser_name@_internal (argc, argv, args_info, params, 0);
679
680  @final_exit@
681}
682
683int
684@parser_name@2 (int argc, char **argv, struct @args_info@ *args_info, int override, int initialize, int check_required)
685{
686  int result;
687  struct @parser_name@_params params;
688
689  params.override = override;
690  params.initialize = initialize;
691  params.check_required = check_required;
692  params.check_ambiguity = 0;
693  params.print_errors = 1;
694
695  result = @parser_name@_internal (argc, argv, args_info, &params, 0);
696
697  @final_exit@
698}
699
700int
701@parser_name@_required (struct @args_info@ *args_info, const char *prog_name)
702{
703@IF@ check_required_options @THEN@
704  int result = EXIT_SUCCESS;
705
706  if (@parser_name@_required2(args_info, prog_name, 0) > 0)
707    result = EXIT_FAILURE;
708
709  @final_exit@
710@ELSE@
711  FIX_UNUSED (args_info);
712  FIX_UNUSED (prog_name);
713  return EXIT_SUCCESS;
714@ENDIF@
715}
716
717@IF@ check_required_options @THEN@
718int
719@parser_name@_required2 (struct @args_info@ *args_info, const char *prog_name, const char *additional_error)
720{
721  int error_occurred = 0;
722  FIX_UNUSED (additional_error);
723
724  /* checks for required options */
725  @handle_required:method@
726  /* checks for dependences among options */
727  @handle_dependencies:method{iteration=true}@
728
729  return error_occurred;
730}
731
732@ENDIF@
733@IF@ include_getopt @THEN@
734@custom_getopt:method@
735@ENDIF@
736
737static char *package_name = 0;
738
739@IF@ not no_options @THEN@
740/**
741 * @@brief updates an option
742 * @@param field the generic pointer to the field to update
743 * @@param orig_field the pointer to the orig field
744 * @@param field_given the pointer to the number of occurrence of this option
745 * @@param prev_given the pointer to the number of occurrence already seen
746 * @@param value the argument for this option (if null no arg was specified)
747 * @@param possible_values the possible values for this option (if specified)
748 * @@param default_value the default value (in case the option only accepts fixed values)
749 * @@param arg_type the type of this option
750 * @@param check_ambiguity @@see @parser_name@_params.check_ambiguity
751 * @@param override @@see @parser_name@_params.override
752 * @@param no_free whether to free a possible previous value
753 * @@param multiple_option whether this is a multiple option
754 * @@param long_opt the corresponding long option
755 * @@param short_opt the corresponding short option (or '-' if none)
756 * @@param additional_error possible further error specification
757 */
758static
759int update_arg(void *field, char **orig_field,
760               unsigned int *field_given, unsigned int *prev_given,
761               char *value, const char *possible_values[],
762               const char *default_value,
763               @parser_name@_arg_type arg_type,
764               int check_ambiguity, int override,
765               int no_free, int multiple_option,
766               const char *long_opt, char short_opt,
767               const char *additional_error)
768{
769  char *stop_char = 0;
770  const char *val = value;
771  int found;
772@IF@ has_arg_string @THEN@
773  char **string_field;
774@ENDIF@
775  FIX_UNUSED (field);
776
777  stop_char = 0;
778  found = 0;
779
780  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
781    {
782      if (short_opt != '-')
783        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
784               package_name, long_opt, short_opt,
785               (additional_error ? additional_error : ""));
786      else
787        fprintf (stderr, "%s: `--%s' option given more than once%s\n",
788               package_name, long_opt,
789               (additional_error ? additional_error : ""));
790      return 1; /* failure */
791    }
792
793@IF@ check_possible_values @THEN@
794  if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
795    {
796      if (short_opt != '-')
797        fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
798          package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
799          (additional_error ? additional_error : ""));
800      else
801        fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n",
802          package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
803          (additional_error ? additional_error : ""));
804      return 1; /* failure */
805    }
806@ELSE@
807  FIX_UNUSED (default_value);
808@ENDIF@
809
810  if (field_given && *field_given && ! override)
811    return 0;
812  if (prev_given)
813    (*prev_given)++;
814  if (field_given)
815    (*field_given)++;
816  if (possible_values)
817    val = possible_values[found];
818
819  switch(arg_type) {
820@IF@ has_arg_flag @THEN@
821  case ARG_FLAG:
822    *((int *)field) = !*((int *)field);
823    break;
824@ENDIF@
825@IF@ has_arg_int @THEN@
826  case ARG_INT:
827    if (val) *((int *)field) = strtol (val, &stop_char, 0);
828    break;
829@ENDIF@
830@IF@ has_arg_short @THEN@
831  case ARG_SHORT:
832    if (val) *((short *)field) = (short)strtol (val, &stop_char, 0);
833    break;
834@ENDIF@
835@IF@ has_arg_long @THEN@
836  case ARG_LONG:
837    if (val) *((long *)field) = (long)strtol (val, &stop_char, 0);
838    break;
839@ENDIF@
840@IF@ has_arg_float @THEN@
841  case ARG_FLOAT:
842    if (val) *((float *)field) = (float)strtod (val, &stop_char);
843    break;
844@ENDIF@
845@IF@ has_arg_double @THEN@
846  case ARG_DOUBLE:
847    if (val) *((double *)field) = strtod (val, &stop_char);
848    break;
849@ENDIF@
850@IF@ has_arg_longdouble @THEN@
851  case ARG_LONGDOUBLE:
852    if (val) *((long double *)field) = (long double)strtod (val, &stop_char);
853    break;
854@ENDIF@
855@IF@ has_arg_longlong @THEN@
856  case ARG_LONGLONG:
857#if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT)
858    if (val) *((long long int*)field) = (long long int) strtoll (val, &stop_char, 0);
859#else
860    if (val) *((long *)field) = (long)strtol (val, &stop_char, 0);
861#endif
862    break;
863@ENDIF@
864@IF@ has_arg_enum @THEN@
865  case ARG_ENUM:
866    if (val) *((int *)field) = found;
867    break;
868@ENDIF@
869@IF@ has_arg_string @THEN@
870  case ARG_STRING:
871    if (val) {
872      string_field = (char **)field;
873      if (!no_free && *string_field)
874        free (*string_field); /* free previous string */
875      *string_field = gengetopt_strdup (val);
876    }
877    break;
878@ENDIF@
879  default:
880    break;
881  };
882
883@IF@ has_arg_int or has_arg_short or has_arg_long or
884     has_arg_float or has_arg_double or
885     has_arg_longdouble or has_arg_longlong @THEN@
886  /* check numeric conversion */
887  switch(arg_type) {
888@IF@ has_arg_int @THEN@
889  case ARG_INT:
890@ENDIF@
891@IF@ has_arg_short @THEN@
892  case ARG_SHORT:
893@ENDIF@
894@IF@ has_arg_long @THEN@
895  case ARG_LONG:
896@ENDIF@
897@IF@ has_arg_float @THEN@
898  case ARG_FLOAT:
899@ENDIF@
900@IF@ has_arg_double @THEN@
901  case ARG_DOUBLE:
902@ENDIF@
903@IF@ has_arg_longdouble @THEN@
904  case ARG_LONGDOUBLE:
905@ENDIF@
906@IF@ has_arg_longlong @THEN@
907  case ARG_LONGLONG:
908@ENDIF@
909    if (val && !(stop_char && *stop_char == '\0')) {
910      fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
911      return 1; /* failure */
912    }
913    break;
914  default:
915    ;
916  };
917@ELSE@
918	FIX_UNUSED(stop_char);
919	@IF@ not (has_arg_enum or has_arg_string) @THEN@
920		FIX_UNUSED(val);
921	@ENDIF@
922@ENDIF@
923
924  /* store the original value */
925  switch(arg_type) {
926  case ARG_NO:
927@IF@ has_arg_flag @THEN@
928  case ARG_FLAG:
929@ENDIF@
930    break;
931  default:
932    if (value && orig_field) {
933      if (no_free) {
934        *orig_field = value;
935      } else {
936        if (*orig_field)
937          free (*orig_field); /* free previous string */
938        *orig_field = gengetopt_strdup (value);
939      }
940    }
941  };
942
943  return 0; /* OK */
944}
945@ENDIF@
946
947@IF@ multiple_token_functions @THEN@
948/**
949 * @@brief store information about a multiple option in a temporary list
950 * @@param list where to (temporarily) store multiple options
951 */
952static
953int update_multiple_arg_temp(struct generic_list **list,
954               unsigned int *prev_given, const char *val,
955               const char *possible_values[], const char *default_value,
956               @parser_name@_arg_type arg_type,
957               const char *long_opt, char short_opt,
958               const char *additional_error)
959{
960  /* store single arguments */
961  char *multi_token;
962  const char *multi_next;
963
964  if (arg_type == ARG_NO) {
965    (*prev_given)++;
966    return 0; /* OK */
967  }
968
969  multi_token = get_multiple_arg_token(val);
970  multi_next = get_multiple_arg_token_next (val);
971
972  while (1)
973    {
974      add_node (list);
975      if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0,
976          prev_given, multi_token, possible_values, default_value,
977          arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) {
978        if (multi_token) free(multi_token);
979        return 1; /* failure */
980      }
981
982      if (multi_next)
983        {
984          multi_token = get_multiple_arg_token(multi_next);
985          multi_next = get_multiple_arg_token_next (multi_next);
986        }
987      else
988        break;
989    }
990
991  return 0; /* OK */
992}
993
994/**
995 * @@brief free the passed list (including possible string argument)
996 */
997static
998void free_list(struct generic_list *list, short string_arg)
999{
1000  if (list) {
1001    struct generic_list *tmp;
1002    while (list)
1003      {
1004        tmp = list;
1005        if (string_arg && list->arg.string_arg)
1006          free (list->arg.string_arg);
1007        if (list->orig)
1008          free (list->orig);
1009        list = list->next;
1010        free (tmp);
1011      }
1012  }
1013}
1014
1015/**
1016 * @@brief updates a multiple option starting from the passed list
1017 */
1018static
1019void update_multiple_arg(void *field, char ***orig_field,
1020               unsigned int field_given, unsigned int prev_given, union generic_value *default_value,
1021               @parser_name@_arg_type arg_type,
1022               struct generic_list *list)
1023{
1024  int i;
1025  struct generic_list *tmp;
1026
1027  if (prev_given && list) {
1028    *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
1029
1030    switch(arg_type) {
1031@IF@ has_arg_int or has_arg_enum @THEN@
1032@IF@ has_arg_int @THEN@
1033    case ARG_INT:
1034@ENDIF@
1035@IF@ has_arg_enum @THEN@
1036    case ARG_ENUM:
1037@ENDIF@
1038      *((int **)field) = (int *)realloc (*((int **)field), (field_given + prev_given) * sizeof (int)); break;
1039@ENDIF@
1040@IF@ has_arg_short @THEN@
1041    case ARG_SHORT:
1042      *((short **)field) = (short *)realloc (*((short **)field), (field_given + prev_given) * sizeof (short)); break;
1043@ENDIF@
1044@IF@ has_arg_long @THEN@
1045    case ARG_LONG:
1046      *((long **)field) = (long *)realloc (*((long **)field), (field_given + prev_given) * sizeof (long)); break;
1047@ENDIF@
1048@IF@ has_arg_float @THEN@
1049    case ARG_FLOAT:
1050      *((float **)field) = (float *)realloc (*((float **)field), (field_given + prev_given) * sizeof (float)); break;
1051@ENDIF@
1052@IF@ has_arg_double @THEN@
1053    case ARG_DOUBLE:
1054      *((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break;
1055@ENDIF@
1056@IF@ has_arg_longdouble @THEN@
1057    case ARG_LONGDOUBLE:
1058      *((long double **)field) = (long double *)realloc (*((long double **)field), (field_given + prev_given) * sizeof (long double)); break;
1059@ENDIF@
1060@IF@ has_arg_longlong @THEN@
1061    case ARG_LONGLONG:
1062#if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT)
1063      *((long long int **)field) = (long long int *)realloc (*((long long int **)field), (field_given + prev_given) * sizeof (long long int)); break;
1064#else
1065      *((long **)field) = (long *)realloc (*((long **)field), (field_given + prev_given) * sizeof (long)); break;
1066#endif
1067@ENDIF@
1068@IF@ has_arg_string @THEN@
1069    case ARG_STRING:
1070      *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break;
1071@ENDIF@
1072    default:
1073      break;
1074    };
1075
1076    for (i = (prev_given - 1); i >= 0; --i)
1077      {
1078        tmp = list;
1079
1080        switch(arg_type) {
1081@IF@ has_arg_int @THEN@
1082        case ARG_INT:
1083          (*((int **)field))[i + field_given] = tmp->arg.int_arg; break;
1084@ENDIF@
1085@IF@ has_arg_short @THEN@
1086        case ARG_SHORT:
1087          (*((short **)field))[i + field_given] = tmp->arg.short_arg; break;
1088@ENDIF@
1089@IF@ has_arg_long @THEN@
1090        case ARG_LONG:
1091          (*((long **)field))[i + field_given] = tmp->arg.long_arg; break;
1092@ENDIF@
1093@IF@ has_arg_float @THEN@
1094        case ARG_FLOAT:
1095          (*((float **)field))[i + field_given] = tmp->arg.float_arg; break;
1096@ENDIF@
1097@IF@ has_arg_double @THEN@
1098        case ARG_DOUBLE:
1099          (*((double **)field))[i + field_given] = tmp->arg.double_arg; break;
1100@ENDIF@
1101@IF@ has_arg_longdouble @THEN@
1102        case ARG_LONGDOUBLE:
1103          (*((long double **)field))[i + field_given] = tmp->arg.longdouble_arg; break;
1104@ENDIF@
1105@IF@ has_arg_longlong @THEN@
1106        case ARG_LONGLONG:
1107#if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT)
1108          (*((long long int **)field))[i + field_given] = tmp->arg.longlong_arg; break;
1109#else
1110          (*((long **)field))[i + field_given] = tmp->arg.longlong_arg; break;
1111#endif
1112@ENDIF@
1113@IF@ has_arg_enum @THEN@
1114        case ARG_ENUM:
1115          (*((int **)field))[i + field_given] = tmp->arg.int_arg; break;
1116@ENDIF@
1117@IF@ has_arg_string @THEN@
1118        case ARG_STRING:
1119          (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break;
1120@ENDIF@
1121        default:
1122          break;
1123        }
1124        (*orig_field) [i + field_given] = list->orig;
1125        list = list->next;
1126        free (tmp);
1127      }
1128  } else { /* set the default value */
1129    if (default_value && ! field_given) {
1130      switch(arg_type) {
1131@IF@ has_arg_int or has_arg_enum @THEN@
1132@IF@ has_arg_int @THEN@
1133      case ARG_INT:
1134@ENDIF@
1135@IF@ has_arg_enum @THEN@
1136      case ARG_ENUM:
1137@ENDIF@
1138        if (! *((int **)field)) {
1139          *((int **)field) = (int *)malloc (sizeof (int));
1140          (*((int **)field))[0] = default_value->int_arg;
1141        }
1142        break;
1143@ENDIF@
1144@IF@ has_arg_short @THEN@
1145      case ARG_SHORT:
1146        if (! *((short **)field)) {
1147          *((short **)field) = (short *)malloc (sizeof (short));
1148          (*((short **)field))[0] = default_value->short_arg;
1149        }
1150        break;
1151@ENDIF@
1152@IF@ has_arg_long @THEN@
1153      case ARG_LONG:
1154        if (! *((long **)field)) {
1155          *((long **)field) = (long *)malloc (sizeof (long));
1156          (*((long **)field))[0] = default_value->long_arg;
1157        }
1158        break;
1159@ENDIF@
1160@IF@ has_arg_float @THEN@
1161      case ARG_FLOAT:
1162        if (! *((float **)field)) {
1163          *((float **)field) = (float *)malloc (sizeof (float));
1164          (*((float **)field))[0] = default_value->float_arg;
1165        }
1166        break;
1167@ENDIF@
1168@IF@ has_arg_double @THEN@
1169      case ARG_DOUBLE:
1170        if (! *((double **)field)) {
1171          *((double **)field) = (double *)malloc (sizeof (double));
1172          (*((double **)field))[0] = default_value->double_arg;
1173        }
1174        break;
1175@ENDIF@
1176@IF@ has_arg_longdouble @THEN@
1177      case ARG_LONGDOUBLE:
1178        if (! *((long double **)field)) {
1179          *((long double **)field) = (long double *)malloc (sizeof (long double));
1180          (*((long double **)field))[0] = default_value->longdouble_arg;
1181        }
1182        break;
1183@ENDIF@
1184@IF@ has_arg_longlong @THEN@
1185      case ARG_LONGLONG:
1186#if defined(HAVE_LONG_LONG) || defined(HAVE_LONG_LONG_INT)
1187        if (! *((long long int **)field)) {
1188          *((long long int **)field) = (long long int *)malloc (sizeof (long long int));
1189          (*((long long int **)field))[0] = default_value->longlong_arg;
1190        }
1191#else
1192        if (! *((long **)field)) {
1193          *((long **)field) = (long *)malloc (sizeof (long));
1194          (*((long **)field))[0] = default_value->longlong_arg;
1195        }
1196#endif
1197        break;
1198@ENDIF@
1199@IF@ has_arg_string @THEN@
1200      case ARG_STRING:
1201        if (! *((char ***)field)) {
1202          *((char ***)field) = (char **)malloc (sizeof (char *));
1203          (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg);
1204        }
1205        break;
1206@ENDIF@
1207      default: break;
1208      }
1209      if (!(*orig_field)) {
1210        *orig_field = (char **) malloc (sizeof (char *));
1211        (*orig_field)[0] = 0;
1212      }
1213    }
1214  }
1215}
1216@ENDIF@
1217@IF@ has_modes @THEN@
1218
1219static int check_modes(
1220  int given1[], const char *options1[],
1221                       int given2[], const char *options2[])
1222{
1223  int i = 0, j = 0, errors = 0;
1224
1225  while (given1[i] >= 0) {
1226    if (given1[i]) {
1227      while (given2[j] >= 0) {
1228        if (given2[j]) {
1229          ++errors;
1230          fprintf(stderr, "%s: option %s conflicts with option %s\n",
1231                  package_name, options1[i], options2[j]);
1232        }
1233        ++j;
1234      }
1235    }
1236    ++i;
1237  }
1238
1239  return errors;
1240}
1241@ENDIF@
1242
1243int
1244@parser_name@_internal (
1245  int argc, char **argv, struct @args_info@ *args_info,
1246                        struct @parser_name@_params *params, const char *additional_error)
1247{
1248  int c;	/* Character of the parsed option.  */
1249@IF@ multiple_options_with_default @THEN@
1250  union generic_value multiple_default_value;
1251@endif@
1252  @list_def:method{iteration=true}@
1253  int error_occurred = 0;
1254  struct @args_info@ local_args_info;
1255
1256  int override;
1257  int initialize;
1258  int check_required;
1259  int check_ambiguity;
1260@IF@ include_getopt @THEN@
1261
1262  char *optarg;
1263  int optind;
1264  int opterr;
1265  int optopt;
1266@ENDIF@
1267
1268  package_name = argv[0];
1269
1270  /* TODO: Why is this here? It is not used anywhere. */
1271  override = params->override;
1272  FIX_UNUSED(override);
1273
1274  initialize = params->initialize;
1275  check_required = params->check_required;
1276
1277  /* TODO: Why is this here? It is not used anywhere. */
1278  check_ambiguity = params->check_ambiguity;
1279  FIX_UNUSED(check_ambiguity);
1280
1281  if (initialize)
1282    @parser_name@_init (args_info);
1283
1284  @parser_name@_init (&local_args_info);
1285
1286  optarg = 0;
1287  optind = 0;
1288  opterr = params->print_errors;
1289  optopt = '?';
1290
1291  while (1)
1292    {
1293      int option_index = 0;
1294
1295      static struct option long_options[] = {
1296        @long_option_struct:method{iteration=true}@
1297        { 0,  0, 0, 0 }
1298      };
1299
1300      @IF@ include_getopt @THEN@
1301      custom_optarg = optarg;
1302      custom_optind = optind;
1303      custom_opterr = opterr;
1304      custom_optopt = optopt;
1305
1306      c = custom_getopt_long (argc, argv, "@getopt_string@", long_options, &option_index);
1307
1308      optarg = custom_optarg;
1309      optind = custom_optind;
1310      opterr = custom_opterr;
1311      optopt = custom_optopt;
1312      @ELSE@
1313      c = getopt_long (argc, argv, "@getopt_string@", long_options, &option_index);
1314      @ENDIF@
1315
1316      if (c == -1) break;	/* Exit from `while (1)' loop.  */
1317
1318      switch (c)
1319        {
1320        @handle_option:method{iteration=true}@
1321
1322        case 0:	/* Long option with no short option */
1323          @handle_no_short_option:method{iteration=true}@
1324@IF@ handle_question_mark @THEN@
1325        case '?':	/* Invalid option.  */
1326          /* `getopt_long' already printed an error message.  */
1327          goto failure;
1328@ENDIF@
1329
1330        default:	/* bug: option not considered.  */
1331          fprintf (stderr, "%s: option unknown: %c%s\n", @package_var_name@, c, (additional_error ? additional_error : ""));
1332          abort ();
1333        } /* switch */
1334    } /* while */
1335
1336  @handle_group:method{iteration=true}@
1337
1338  @multiple_fill_array:method{iteration=true}@
1339  @update_multiple_given:method{iteration=true}@
1340  @check_modes:method{iteration=true}@
1341
1342@IF@ check_required_options @THEN@
1343  if (check_required)
1344    {
1345      error_occurred += @parser_name@_required2 (args_info, argv[0], additional_error);
1346    }
1347@ELSE@
1348	FIX_UNUSED(check_required);
1349@ENDIF@
1350
1351  @parser_name@_release (&local_args_info);
1352
1353  if ( error_occurred )
1354    return (EXIT_FAILURE);
1355
1356@IF@ handle_unnamed @THEN@
1357  if (optind < argc)
1358    {
1359      int i = 0 ;
1360      int found_prog_name = 0;
1361      /* whether program name, i.e., argv[0], is in the remaining args
1362         (this may happen with some implementations of getopt,
1363          but surely not with the one included by gengetopt) */
1364
1365@IF@ include_getopt @THEN@
1366@ELSE@
1367      i = optind;
1368      while (i < argc)
1369        if (argv[i++] == argv[0]) {
1370          found_prog_name = 1;
1371          break;
1372        }
1373      i = 0;
1374@ENDIF@
1375
1376      args_info->inputs_num = argc - optind - found_prog_name;
1377      args_info->inputs =
1378        (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1379      while (optind < argc)
1380@IF@ include_getopt @THEN@
1381        args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1382@ELSE@
1383        if (argv[optind++] != argv[0])
1384          args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
1385@ENDIF@
1386    }
1387
1388@ENDIF@
1389  return 0;
1390
1391failure:
1392  @list_free:method@
1393  @parser_name@_release (&local_args_info);
1394  return (EXIT_FAILURE);
1395}
1396@IF@ conf_parser @THEN@
1397
1398#ifndef CONFIG_FILE_LINE_SIZE
1399#define CONFIG_FILE_LINE_SIZE 2048
1400#endif
1401#define ADDITIONAL_ERROR " in configuration file "
1402
1403#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1404/* 3 is for "--" and "=" */
1405
1406static int
1407_@parser_name@_configfile (const char *filename, int *my_argc)
1408{
1409  FILE* file;
1410  char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1411  char linebuf[CONFIG_FILE_LINE_SIZE];
1412  int line_num = 0;
1413  int result = 0, equal;
1414  char *fopt, *farg;
1415  char *str_index;
1416  size_t len, next_token;
1417  char delimiter;
1418
1419  if ((file = fopen(filename, "r")) == 0)
1420    {
1421      fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1422               @package_var_name@, filename);
1423      return EXIT_FAILURE;
1424    }
1425
1426  while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
1427    {
1428      ++line_num;
1429      my_argv[0] = '\0';
1430      len = strlen(linebuf);
1431      if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1432        {
1433          fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1434                   @package_var_name@, filename, line_num);
1435          result = EXIT_FAILURE;
1436          break;
1437        }
1438
1439      /* find first non-whitespace character in the line */
1440      next_token = strspn (linebuf, " \t\r\n");
1441      str_index  = linebuf + next_token;
1442
1443      if ( str_index[0] == '\0' || str_index[0] == '#')
1444        continue; /* empty line or comment line is skipped */
1445
1446      fopt = str_index;
1447
1448      /* truncate fopt at the end of the first non-valid character */
1449      next_token = strcspn (fopt, " \t\r\n=");
1450
1451      if (fopt[next_token] == '\0') /* the line is over */
1452        {
1453          farg  = 0;
1454          equal = 0;
1455          goto noarg;
1456        }
1457
1458      /* remember if equal sign is present */
1459      equal = (fopt[next_token] == '=');
1460      fopt[next_token++] = '\0';
1461
1462      /* advance pointers to the next token after the end of fopt */
1463      next_token += strspn (fopt + next_token, " \t\r\n");
1464
1465      /* check for the presence of equal sign, and if so, skip it */
1466      if ( !equal )
1467        if ((equal = (fopt[next_token] == '=')))
1468          {
1469            next_token++;
1470            next_token += strspn (fopt + next_token, " \t\r\n");
1471          }
1472      str_index  += next_token;
1473
1474      /* find argument */
1475      farg = str_index;
1476      if ( farg[0] == '\"' || farg[0] == '\'' )
1477        { /* quoted argument */
1478          str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1479          if (! str_index)
1480            {
1481              fprintf
1482                (stderr,
1483                 "%s:%s:%d: unterminated string in configuration file\n",
1484                 @package_var_name@, filename, line_num);
1485              result = EXIT_FAILURE;
1486              break;
1487            }
1488        }
1489      else
1490        { /* read up the remaining part up to a delimiter */
1491          next_token = strcspn (farg, " \t\r\n#\'\"");
1492          str_index += next_token;
1493        }
1494
1495      /* truncate farg at the delimiter and store it for further check */
1496      delimiter = *str_index, *str_index++ = '\0';
1497
1498      /* everything but comment is illegal at the end of line */
1499      if (delimiter != '\0' && delimiter != '#')
1500        {
1501          str_index += strspn(str_index, " \t\r\n");
1502          if (*str_index != '\0' && *str_index != '#')
1503            {
1504              fprintf
1505                (stderr,
1506                 "%s:%s:%d: malformed string in configuration file\n",
1507                 @package_var_name@, filename, line_num);
1508              result = EXIT_FAILURE;
1509              break;
1510            }
1511        }
1512
1513    noarg:
1514      if (!strcmp(fopt,"include")) {
1515        if (farg && *farg) {
1516          result = _@parser_name@_configfile(farg, my_argc);
1517        } else {
1518          fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1519                  @package_var_name@, filename, line_num);
1520        }
1521        continue;
1522      }
1523      len = strlen(fopt);
1524      strcat (my_argv, len > 1 ? "--" : "-");
1525      strcat (my_argv, fopt);
1526      if (len > 1 && ((farg && *farg) || equal))
1527        strcat (my_argv, "=");
1528      if (farg && *farg)
1529        strcat (my_argv, farg);
1530      ++(*my_argc);
1531
1532      cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1533      cmd_line_list_tmp->next = cmd_line_list;
1534      cmd_line_list = cmd_line_list_tmp;
1535      cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1536    } /* while */
1537
1538  if (file)
1539    fclose(file);
1540  return result;
1541}
1542
1543int
1544@parser_name@_configfile (
1545  const char *filename,
1546                           struct @args_info@ *args_info,
1547                           int override, int initialize, int check_required)
1548{
1549  struct @parser_name@_params params;
1550
1551  params.override = override;
1552  params.initialize = initialize;
1553  params.check_required = check_required;
1554  params.check_ambiguity = 0;
1555  params.print_errors = 1;
1556
1557  return @parser_name@_config_file (filename, args_info, &params);
1558}
1559
1560int
1561@parser_name@_config_file (const char *filename,
1562                           struct @args_info@ *args_info,
1563                           struct @parser_name@_params *params)
1564{
1565  int i, result;
1566  int my_argc = 1;
1567  char **my_argv_arg;
1568  char *additional_error;
1569
1570  /* store the program name */
1571  cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1572  cmd_line_list_tmp->next = cmd_line_list;
1573  cmd_line_list = cmd_line_list_tmp;
1574  cmd_line_list->string_arg = gengetopt_strdup (@package_var_name@);
1575
1576  result = _@parser_name@_configfile(filename, &my_argc);
1577
1578  if (result != EXIT_FAILURE) {
1579    my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1580    cmd_line_list_tmp = cmd_line_list;
1581
1582    for (i = my_argc - 1; i >= 0; --i) {
1583      my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1584      cmd_line_list_tmp = cmd_line_list_tmp->next;
1585    }
1586
1587    my_argv_arg[my_argc] = 0;
1588
1589    additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1590    strcpy (additional_error, ADDITIONAL_ERROR);
1591    strcat (additional_error, filename);
1592    result =
1593      @parser_name@_internal (my_argc, my_argv_arg, args_info,
1594                              params,
1595                              additional_error);
1596
1597    free (additional_error);
1598    free (my_argv_arg);
1599  }
1600
1601  free_cmd_list();
1602  @final_exit@
1603}
1604@ENDIF@
1605@IF@ generate_string_parser @THEN@
1606
1607static unsigned int
1608@parser_name@_create_argv(const char *cmdline_, char ***argv_ptr, const char *prog_name)
1609{
1610  char *cmdline, *p;
1611  size_t n = 0, j;
1612  int i;
1613
1614  if (prog_name) {
1615    cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1616    cmd_line_list_tmp->next = cmd_line_list;
1617    cmd_line_list = cmd_line_list_tmp;
1618    cmd_line_list->string_arg = gengetopt_strdup (prog_name);
1619
1620    ++n;
1621  }
1622
1623  cmdline = gengetopt_strdup(cmdline_);
1624  p = cmdline;
1625
1626  while (p && strlen(p))
1627    {
1628      j = strcspn(p, " \t");
1629      ++n;
1630      if (j && j < strlen(p))
1631        {
1632          p[j] = '\0';
1633
1634          cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1635          cmd_line_list_tmp->next = cmd_line_list;
1636          cmd_line_list = cmd_line_list_tmp;
1637          cmd_line_list->string_arg = gengetopt_strdup (p);
1638
1639          p += (j+1);
1640          p += strspn(p, " \t");
1641        }
1642      else
1643        {
1644          cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1645          cmd_line_list_tmp->next = cmd_line_list;
1646          cmd_line_list = cmd_line_list_tmp;
1647          cmd_line_list->string_arg = gengetopt_strdup (p);
1648
1649          break;
1650        }
1651    }
1652
1653  *argv_ptr = (char **) malloc((n + 1) * sizeof(char *));
1654  cmd_line_list_tmp = cmd_line_list;
1655  for (i = (n-1); i >= 0; --i)
1656    {
1657      (*argv_ptr)[i] = cmd_line_list_tmp->string_arg;
1658      cmd_line_list_tmp = cmd_line_list_tmp->next;
1659    }
1660
1661  (*argv_ptr)[n] = 0;
1662
1663  free(cmdline);
1664  return n;
1665}
1666
1667int
1668@parser_name@_string(const char *cmdline, struct @args_info@ *args_info, const char *prog_name)
1669{
1670  return @parser_name@_string2(cmdline, args_info, prog_name, 0, 1, 1);
1671}
1672
1673int
1674@parser_name@_string2(const char *cmdline, struct @args_info@ *args_info, const char *prog_name,
1675    int override, int initialize, int check_required)
1676{
1677  struct @parser_name@_params params;
1678
1679  params.override = override;
1680  params.initialize = initialize;
1681  params.check_required = check_required;
1682  params.check_ambiguity = 0;
1683  params.print_errors = 1;
1684
1685  return @parser_name@_string_ext(cmdline, args_info, prog_name, &params);
1686}
1687
1688int
1689@parser_name@_string_ext(const char *cmdline, struct @args_info@ *args_info, const char *prog_name,
1690    struct @parser_name@_params *params)
1691{
1692  char **argv_ptr = 0;
1693  int result;
1694  unsigned int argc;
1695
1696  argc = @parser_name@_create_argv(cmdline, &argv_ptr, prog_name);
1697
1698  result =
1699    @parser_name@_internal (argc, argv_ptr, args_info, params, 0);
1700
1701  if (argv_ptr)
1702    {
1703      free (argv_ptr);
1704    }
1705
1706  free_cmd_list();
1707
1708  @final_exit@
1709}
1710
1711@ENDIF@
1712/* vim: set ft=c noet ts=8 sts=8 sw=8 tw=80 nojs spell : */
1713