source: src/common/cmdline.c @ 5f4d9c3

Last change on this file since 5f4d9c3 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 12 years ago

Rewritten the build system, added lem UTF-8 version.

  • Property mode set to 100644
File size: 37.5 KB
Line 
1/*
2  File autogenerated by gengetopt version 2.22.4
3  generated with the following command:
4  gengetopt -i 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 = "";
29
30const char *gengetopt_args_info_usage = "Usage: guess [OPTIONS]...";
31
32const char *gengetopt_args_info_description = "";
33
34const char *gengetopt_args_info_full_help[] = {
35  "  -h, --help                 Print help and exit",
36  "      --full-help            Print help, including hidden options, and exit",
37  "  -V, --version              Print version and exit",
38  "  -f, --input=STRING         Input file",
39  "  -o, --output=STRING        Output file for succesfully processed segments",
40  "  -e, --fail=STRING          Output file for unsuccesfully processed segments ",
41  "      --only-fail            Print only segments the program failed to process  \n                               (default=off)",
42  "      --no-fail              Print only segments the program processed  \n                               (default=off)",
43  "  -c, --copy                 Copy succesfully processed segments to standard \n                               output  (default=off)",
44  "  -p, --process=STRING       Process segments with this tag",
45  "  -s, --select=STRING        Select only segments with this field",
46  "  -S, --ignore=STRING        Select only segments without this field",
47  "  -O, --output-field=STRING  Output field name",
48  "  -I, --input-field=STRING   Input field name",
49  "  -i, --interactive          Toggle interactive mode  (default=off)",
50  "      --config=FILENAME      Configuration file",
51  "  -1, --one-field            Print all results in one segments (creates \n                               ambiguous annotation)  (default=off)",
52  "      --one-line             Print annotation alternatives as additional fields \n                                (default=off)",
53  "      --language=STRING      Language.",
54  "  -l, --color                Show guessed descriptions in colour.  \n                               (default=off)",
55    0
56};
57
58static void
59init_help_array(void)
60{
61  gengetopt_args_info_help[0] = gengetopt_args_info_full_help[0];
62  gengetopt_args_info_help[1] = gengetopt_args_info_full_help[1];
63  gengetopt_args_info_help[2] = gengetopt_args_info_full_help[2];
64  gengetopt_args_info_help[3] = gengetopt_args_info_full_help[3];
65  gengetopt_args_info_help[4] = gengetopt_args_info_full_help[4];
66  gengetopt_args_info_help[5] = gengetopt_args_info_full_help[5];
67  gengetopt_args_info_help[6] = gengetopt_args_info_full_help[8];
68  gengetopt_args_info_help[7] = gengetopt_args_info_full_help[9];
69  gengetopt_args_info_help[8] = gengetopt_args_info_full_help[10];
70  gengetopt_args_info_help[9] = gengetopt_args_info_full_help[11];
71  gengetopt_args_info_help[10] = gengetopt_args_info_full_help[12];
72  gengetopt_args_info_help[11] = gengetopt_args_info_full_help[13];
73  gengetopt_args_info_help[12] = gengetopt_args_info_full_help[14];
74  gengetopt_args_info_help[13] = gengetopt_args_info_full_help[15];
75  gengetopt_args_info_help[14] = gengetopt_args_info_full_help[16];
76  gengetopt_args_info_help[15] = gengetopt_args_info_full_help[17];
77  gengetopt_args_info_help[16] = gengetopt_args_info_full_help[18];
78  gengetopt_args_info_help[17] = gengetopt_args_info_full_help[19];
79  gengetopt_args_info_help[18] = 0; 
80 
81}
82
83const char *gengetopt_args_info_help[19];
84
85typedef enum {ARG_NO
86  , ARG_FLAG
87  , ARG_STRING
88} cmdline_parser_arg_type;
89
90static
91void clear_given (struct gengetopt_args_info *args_info);
92static
93void clear_args (struct gengetopt_args_info *args_info);
94
95static int
96cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
97                        struct cmdline_parser_params *params, const char *additional_error);
98
99static int
100cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
101
102static char *
103gengetopt_strdup (const char *s);
104
105static
106void clear_given (struct gengetopt_args_info *args_info)
107{
108  args_info->help_given = 0 ;
109  args_info->full_help_given = 0 ;
110  args_info->version_given = 0 ;
111  args_info->input_given = 0 ;
112  args_info->output_given = 0 ;
113  args_info->fail_given = 0 ;
114  args_info->only_fail_given = 0 ;
115  args_info->no_fail_given = 0 ;
116  args_info->copy_given = 0 ;
117  args_info->process_given = 0 ;
118  args_info->select_given = 0 ;
119  args_info->ignore_given = 0 ;
120  args_info->output_field_given = 0 ;
121  args_info->input_field_given = 0 ;
122  args_info->interactive_given = 0 ;
123  args_info->config_given = 0 ;
124  args_info->one_field_given = 0 ;
125  args_info->one_line_given = 0 ;
126  args_info->language_given = 0 ;
127  args_info->color_given = 0 ;
128}
129
130static
131void clear_args (struct gengetopt_args_info *args_info)
132{
133  FIX_UNUSED (args_info);
134  args_info->input_arg = NULL;
135  args_info->input_orig = NULL;
136  args_info->output_arg = NULL;
137  args_info->output_orig = NULL;
138  args_info->fail_arg = NULL;
139  args_info->fail_orig = NULL;
140  args_info->only_fail_flag = 0;
141  args_info->no_fail_flag = 0;
142  args_info->copy_flag = 0;
143  args_info->process_arg = NULL;
144  args_info->process_orig = NULL;
145  args_info->select_arg = NULL;
146  args_info->select_orig = NULL;
147  args_info->ignore_arg = NULL;
148  args_info->ignore_orig = NULL;
149  args_info->output_field_arg = NULL;
150  args_info->output_field_orig = NULL;
151  args_info->input_field_arg = NULL;
152  args_info->input_field_orig = NULL;
153  args_info->interactive_flag = 0;
154  args_info->config_arg = NULL;
155  args_info->config_orig = NULL;
156  args_info->one_field_flag = 0;
157  args_info->one_line_flag = 0;
158  args_info->language_arg = NULL;
159  args_info->language_orig = NULL;
160  args_info->color_flag = 0;
161 
162}
163
164static
165void init_args_info(struct gengetopt_args_info *args_info)
166{
167
168  init_help_array(); 
169  args_info->help_help = gengetopt_args_info_full_help[0] ;
170  args_info->full_help_help = gengetopt_args_info_full_help[1] ;
171  args_info->version_help = gengetopt_args_info_full_help[2] ;
172  args_info->input_help = gengetopt_args_info_full_help[3] ;
173  args_info->output_help = gengetopt_args_info_full_help[4] ;
174  args_info->fail_help = gengetopt_args_info_full_help[5] ;
175  args_info->only_fail_help = gengetopt_args_info_full_help[6] ;
176  args_info->no_fail_help = gengetopt_args_info_full_help[7] ;
177  args_info->copy_help = gengetopt_args_info_full_help[8] ;
178  args_info->process_help = gengetopt_args_info_full_help[9] ;
179  args_info->process_min = 0;
180  args_info->process_max = 0;
181  args_info->select_help = gengetopt_args_info_full_help[10] ;
182  args_info->select_min = 0;
183  args_info->select_max = 0;
184  args_info->ignore_help = gengetopt_args_info_full_help[11] ;
185  args_info->ignore_min = 0;
186  args_info->ignore_max = 0;
187  args_info->output_field_help = gengetopt_args_info_full_help[12] ;
188  args_info->input_field_help = gengetopt_args_info_full_help[13] ;
189  args_info->input_field_min = 0;
190  args_info->input_field_max = 0;
191  args_info->interactive_help = gengetopt_args_info_full_help[14] ;
192  args_info->config_help = gengetopt_args_info_full_help[15] ;
193  args_info->one_field_help = gengetopt_args_info_full_help[16] ;
194  args_info->one_line_help = gengetopt_args_info_full_help[17] ;
195  args_info->language_help = gengetopt_args_info_full_help[18] ;
196  args_info->color_help = gengetopt_args_info_full_help[19] ;
197 
198}
199
200void
201cmdline_parser_print_version (void)
202{
203  printf ("%s %s\n",
204     (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
205     CMDLINE_PARSER_VERSION);
206}
207
208static void print_help_common(void) {
209  cmdline_parser_print_version ();
210
211  if (strlen(gengetopt_args_info_purpose) > 0)
212    printf("\n%s\n", gengetopt_args_info_purpose);
213
214  if (strlen(gengetopt_args_info_usage) > 0)
215    printf("\n%s\n", gengetopt_args_info_usage);
216
217  printf("\n");
218
219  if (strlen(gengetopt_args_info_description) > 0)
220    printf("%s\n\n", gengetopt_args_info_description);
221}
222
223void
224cmdline_parser_print_help (void)
225{
226  int i = 0;
227  print_help_common();
228  while (gengetopt_args_info_help[i])
229    printf("%s\n", gengetopt_args_info_help[i++]);
230}
231
232void
233cmdline_parser_print_full_help (void)
234{
235  int i = 0;
236  print_help_common();
237  while (gengetopt_args_info_full_help[i])
238    printf("%s\n", gengetopt_args_info_full_help[i++]);
239}
240
241void
242cmdline_parser_init (struct gengetopt_args_info *args_info)
243{
244  clear_given (args_info);
245  clear_args (args_info);
246  init_args_info (args_info);
247}
248
249void
250cmdline_parser_params_init(struct cmdline_parser_params *params)
251{
252  if (params)
253    { 
254      params->override = 0;
255      params->initialize = 1;
256      params->check_required = 1;
257      params->check_ambiguity = 0;
258      params->print_errors = 1;
259    }
260}
261
262struct cmdline_parser_params *
263cmdline_parser_params_create(void)
264{
265  struct cmdline_parser_params *params = 
266    (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
267  cmdline_parser_params_init(params); 
268  return params;
269}
270
271static void
272free_string_field (char **s)
273{
274  if (*s)
275    {
276      free (*s);
277      *s = 0;
278    }
279}
280
281/** @brief generic value variable */
282union generic_value {
283    char *string_arg;
284    const char *default_string_arg;
285};
286
287/** @brief holds temporary values for multiple options */
288struct generic_list
289{
290  union generic_value arg;
291  char *orig;
292  struct generic_list *next;
293};
294
295/**
296 * @brief add a node at the head of the list
297 */
298static void add_node(struct generic_list **list) {
299  struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list));
300  new_node->next = *list;
301  *list = new_node;
302  new_node->arg.string_arg = 0;
303  new_node->orig = 0;
304}
305
306
307static void
308free_multiple_string_field(unsigned int len, char ***arg, char ***orig)
309{
310  unsigned int i;
311  if (*arg) {
312    for (i = 0; i < len; ++i)
313      {
314        free_string_field(&((*arg)[i]));
315        free_string_field(&((*orig)[i]));
316      }
317    free_string_field(&((*arg)[0])); /* free default string */
318
319    free (*arg);
320    *arg = 0;
321    free (*orig);
322    *orig = 0;
323  }
324}
325
326static void
327cmdline_parser_release (struct gengetopt_args_info *args_info)
328{
329
330  free_string_field (&(args_info->input_arg));
331  free_string_field (&(args_info->input_orig));
332  free_string_field (&(args_info->output_arg));
333  free_string_field (&(args_info->output_orig));
334  free_string_field (&(args_info->fail_arg));
335  free_string_field (&(args_info->fail_orig));
336  free_multiple_string_field (args_info->process_given, &(args_info->process_arg), &(args_info->process_orig));
337  free_multiple_string_field (args_info->select_given, &(args_info->select_arg), &(args_info->select_orig));
338  free_multiple_string_field (args_info->ignore_given, &(args_info->ignore_arg), &(args_info->ignore_orig));
339  free_string_field (&(args_info->output_field_arg));
340  free_string_field (&(args_info->output_field_orig));
341  free_multiple_string_field (args_info->input_field_given, &(args_info->input_field_arg), &(args_info->input_field_orig));
342  free_string_field (&(args_info->config_arg));
343  free_string_field (&(args_info->config_orig));
344  free_string_field (&(args_info->language_arg));
345  free_string_field (&(args_info->language_orig));
346 
347 
348
349  clear_given (args_info);
350}
351
352
353static void
354write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
355{
356  FIX_UNUSED (values);
357  if (arg) {
358    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
359  } else {
360    fprintf(outfile, "%s\n", opt);
361  }
362}
363
364static void
365write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
366{
367  int i;
368 
369  for (i = 0; i < len; ++i)
370    write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
371}
372
373int
374cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
375{
376  int i = 0;
377
378  if (!outfile)
379    {
380      fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
381      return EXIT_FAILURE;
382    }
383
384  if (args_info->help_given)
385    write_into_file(outfile, "help", 0, 0 );
386  if (args_info->full_help_given)
387    write_into_file(outfile, "full-help", 0, 0 );
388  if (args_info->version_given)
389    write_into_file(outfile, "version", 0, 0 );
390  if (args_info->input_given)
391    write_into_file(outfile, "input", args_info->input_orig, 0);
392  if (args_info->output_given)
393    write_into_file(outfile, "output", args_info->output_orig, 0);
394  if (args_info->fail_given)
395    write_into_file(outfile, "fail", args_info->fail_orig, 0);
396  if (args_info->only_fail_given)
397    write_into_file(outfile, "only-fail", 0, 0 );
398  if (args_info->no_fail_given)
399    write_into_file(outfile, "no-fail", 0, 0 );
400  if (args_info->copy_given)
401    write_into_file(outfile, "copy", 0, 0 );
402  write_multiple_into_file(outfile, args_info->process_given, "process", args_info->process_orig, 0);
403  write_multiple_into_file(outfile, args_info->select_given, "select", args_info->select_orig, 0);
404  write_multiple_into_file(outfile, args_info->ignore_given, "ignore", args_info->ignore_orig, 0);
405  if (args_info->output_field_given)
406    write_into_file(outfile, "output-field", args_info->output_field_orig, 0);
407  write_multiple_into_file(outfile, args_info->input_field_given, "input-field", args_info->input_field_orig, 0);
408  if (args_info->interactive_given)
409    write_into_file(outfile, "interactive", 0, 0 );
410  if (args_info->config_given)
411    write_into_file(outfile, "config", args_info->config_orig, 0);
412  if (args_info->one_field_given)
413    write_into_file(outfile, "one-field", 0, 0 );
414  if (args_info->one_line_given)
415    write_into_file(outfile, "one-line", 0, 0 );
416  if (args_info->language_given)
417    write_into_file(outfile, "language", args_info->language_orig, 0);
418  if (args_info->color_given)
419    write_into_file(outfile, "color", 0, 0 );
420 
421
422  i = EXIT_SUCCESS;
423  return i;
424}
425
426int
427cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
428{
429  FILE *outfile;
430  int i = 0;
431
432  outfile = fopen(filename, "w");
433
434  if (!outfile)
435    {
436      fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
437      return EXIT_FAILURE;
438    }
439
440  i = cmdline_parser_dump(outfile, args_info);
441  fclose (outfile);
442
443  return i;
444}
445
446void
447cmdline_parser_free (struct gengetopt_args_info *args_info)
448{
449  cmdline_parser_release (args_info);
450}
451
452/** @brief replacement of strdup, which is not standard */
453char *
454gengetopt_strdup (const char *s)
455{
456  char *result = 0;
457  if (!s)
458    return result;
459
460  result = (char*)malloc(strlen(s) + 1);
461  if (result == (char*)0)
462    return (char*)0;
463  strcpy(result, s);
464  return result;
465}
466
467static char *
468get_multiple_arg_token(const char *arg)
469{
470  const char *tok;
471  char *ret;
472  size_t len, num_of_escape, i, j;
473
474  if (!arg)
475    return 0;
476
477  tok = strchr (arg, ',');
478  num_of_escape = 0;
479
480  /* make sure it is not escaped */
481  while (tok)
482    {
483      if (*(tok-1) == '\\')
484        {
485          /* find the next one */
486          tok = strchr (tok+1, ',');
487          ++num_of_escape;
488        }
489      else
490        break;
491    }
492
493  if (tok)
494    len = (size_t)(tok - arg + 1);
495  else
496    len = strlen (arg) + 1;
497
498  len -= num_of_escape;
499
500  ret = (char *) malloc (len);
501
502  i = 0;
503  j = 0;
504  while (arg[i] && (j < len-1))
505    {
506      if (arg[i] == '\\' && 
507          arg[ i + 1 ] && 
508          arg[ i + 1 ] == ',')
509        ++i;
510
511      ret[j++] = arg[i++];
512    }
513
514  ret[len-1] = '\0';
515
516  return ret;
517}
518
519static const char *
520get_multiple_arg_token_next(const char *arg)
521{
522  const char *tok;
523
524  if (!arg)
525    return 0;
526
527  tok = strchr (arg, ',');
528
529  /* make sure it is not escaped */
530  while (tok)
531    {
532      if (*(tok-1) == '\\')
533        {
534          /* find the next one */
535          tok = strchr (tok+1, ',');
536        }
537      else
538        break;
539    }
540
541  if (! tok || strlen(tok) == 1)
542    return 0;
543
544  return tok+1;
545}
546
547static int
548check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
549
550int
551check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
552{
553  int error = 0;
554
555  if (option_given && (min > 0 || max > 0))
556    {
557      if (min > 0 && max > 0)
558        {
559          if (min == max)
560            {
561              /* specific occurrences */
562              if (option_given != (unsigned int) min)
563                {
564                  fprintf (stderr, "%s: %s option occurrences must be %d\n",
565                    prog_name, option_desc, min);
566                  error = 1;
567                }
568            }
569          else if (option_given < (unsigned int) min
570                || option_given > (unsigned int) max)
571            {
572              /* range occurrences */
573              fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
574                prog_name, option_desc, min, max);
575              error = 1;
576            }
577        }
578      else if (min > 0)
579        {
580          /* at least check */
581          if (option_given < min)
582            {
583              fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
584                prog_name, option_desc, min);
585              error = 1;
586            }
587        }
588      else if (max > 0)
589        {
590          /* at most check */
591          if (option_given > max)
592            {
593              fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
594                prog_name, option_desc, max);
595              error = 1;
596            }
597        }
598    }
599   
600  return error;
601}
602int
603cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
604{
605  return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
606}
607
608int
609cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
610                   struct cmdline_parser_params *params)
611{
612  int result;
613  result = cmdline_parser_internal (argc, argv, args_info, params, 0);
614
615  if (result == EXIT_FAILURE)
616    {
617      cmdline_parser_free (args_info);
618      exit (EXIT_FAILURE);
619    }
620 
621  return result;
622}
623
624int
625cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
626{
627  int result;
628  struct cmdline_parser_params params;
629 
630  params.override = override;
631  params.initialize = initialize;
632  params.check_required = check_required;
633  params.check_ambiguity = 0;
634  params.print_errors = 1;
635
636  result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
637
638  if (result == EXIT_FAILURE)
639    {
640      cmdline_parser_free (args_info);
641      exit (EXIT_FAILURE);
642    }
643 
644  return result;
645}
646
647int
648cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
649{
650  int result = EXIT_SUCCESS;
651
652  if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
653    result = EXIT_FAILURE;
654
655  if (result == EXIT_FAILURE)
656    {
657      cmdline_parser_free (args_info);
658      exit (EXIT_FAILURE);
659    }
660 
661  return result;
662}
663
664int
665cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
666{
667  int error = 0;
668  FIX_UNUSED (additional_error);
669
670  /* checks for required options */
671  if (check_multiple_option_occurrences(prog_name, args_info->process_given, args_info->process_min, args_info->process_max, "'--process' ('-p')"))
672     error = 1;
673 
674  if (check_multiple_option_occurrences(prog_name, args_info->select_given, args_info->select_min, args_info->select_max, "'--select' ('-s')"))
675     error = 1;
676 
677  if (check_multiple_option_occurrences(prog_name, args_info->ignore_given, args_info->ignore_min, args_info->ignore_max, "'--ignore' ('-S')"))
678     error = 1;
679 
680  if (check_multiple_option_occurrences(prog_name, args_info->input_field_given, args_info->input_field_min, args_info->input_field_max, "'--input-field' ('-I')"))
681     error = 1;
682 
683 
684  /* checks for dependences among options */
685
686  return error;
687}
688
689
690static char *package_name = 0;
691
692/**
693 * @brief updates an option
694 * @param field the generic pointer to the field to update
695 * @param orig_field the pointer to the orig field
696 * @param field_given the pointer to the number of occurrence of this option
697 * @param prev_given the pointer to the number of occurrence already seen
698 * @param value the argument for this option (if null no arg was specified)
699 * @param possible_values the possible values for this option (if specified)
700 * @param default_value the default value (in case the option only accepts fixed values)
701 * @param arg_type the type of this option
702 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
703 * @param override @see cmdline_parser_params.override
704 * @param no_free whether to free a possible previous value
705 * @param multiple_option whether this is a multiple option
706 * @param long_opt the corresponding long option
707 * @param short_opt the corresponding short option (or '-' if none)
708 * @param additional_error possible further error specification
709 */
710static
711int update_arg(void *field, char **orig_field,
712               unsigned int *field_given, unsigned int *prev_given, 
713               char *value, const char *possible_values[],
714               const char *default_value,
715               cmdline_parser_arg_type arg_type,
716               int check_ambiguity, int override,
717               int no_free, int multiple_option,
718               const char *long_opt, char short_opt,
719               const char *additional_error)
720{
721  char *stop_char = 0;
722  const char *val = value;
723  int found;
724  char **string_field;
725  FIX_UNUSED (field);
726
727  stop_char = 0;
728  found = 0;
729
730  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
731    {
732      if (short_opt != '-')
733        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
734               package_name, long_opt, short_opt,
735               (additional_error ? additional_error : ""));
736      else
737        fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
738               package_name, long_opt,
739               (additional_error ? additional_error : ""));
740      return 1; /* failure */
741    }
742
743  FIX_UNUSED (default_value);
744   
745  if (field_given && *field_given && ! override)
746    return 0;
747  if (prev_given)
748    (*prev_given)++;
749  if (field_given)
750    (*field_given)++;
751  if (possible_values)
752    val = possible_values[found];
753
754  switch(arg_type) {
755  case ARG_FLAG:
756    *((int *)field) = !*((int *)field);
757    break;
758  case ARG_STRING:
759    if (val) {
760      string_field = (char **)field;
761      if (!no_free && *string_field)
762        free (*string_field); /* free previous string */
763      *string_field = gengetopt_strdup (val);
764    }
765    break;
766  default:
767    break;
768  };
769
770
771  /* store the original value */
772  switch(arg_type) {
773  case ARG_NO:
774  case ARG_FLAG:
775    break;
776  default:
777    if (value && orig_field) {
778      if (no_free) {
779        *orig_field = value;
780      } else {
781        if (*orig_field)
782          free (*orig_field); /* free previous string */
783        *orig_field = gengetopt_strdup (value);
784      }
785    }
786  };
787
788  return 0; /* OK */
789}
790
791/**
792 * @brief store information about a multiple option in a temporary list
793 * @param list where to (temporarily) store multiple options
794 */
795static
796int update_multiple_arg_temp(struct generic_list **list,
797               unsigned int *prev_given, const char *val,
798               const char *possible_values[], const char *default_value,
799               cmdline_parser_arg_type arg_type,
800               const char *long_opt, char short_opt,
801               const char *additional_error)
802{
803  /* store single arguments */
804  char *multi_token;
805  const char *multi_next;
806
807  if (arg_type == ARG_NO) {
808    (*prev_given)++;
809    return 0; /* OK */
810  }
811
812  multi_token = get_multiple_arg_token(val);
813  multi_next = get_multiple_arg_token_next (val);
814
815  while (1)
816    {
817      add_node (list);
818      if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0,
819          prev_given, multi_token, possible_values, default_value, 
820          arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) {
821        if (multi_token) free(multi_token);
822        return 1; /* failure */
823      }
824
825      if (multi_next)
826        {
827          multi_token = get_multiple_arg_token(multi_next);
828          multi_next = get_multiple_arg_token_next (multi_next);
829        }
830      else
831        break;
832    }
833
834  return 0; /* OK */
835}
836
837/**
838 * @brief free the passed list (including possible string argument)
839 */
840static
841void free_list(struct generic_list *list, short string_arg)
842{
843  if (list) {
844    struct generic_list *tmp;
845    while (list)
846      {
847        tmp = list;
848        if (string_arg && list->arg.string_arg)
849          free (list->arg.string_arg);
850        if (list->orig)
851          free (list->orig);
852        list = list->next;
853        free (tmp);
854      }
855  }
856}
857
858/**
859 * @brief updates a multiple option starting from the passed list
860 */
861static
862void update_multiple_arg(void *field, char ***orig_field,
863               unsigned int field_given, unsigned int prev_given, union generic_value *default_value,
864               cmdline_parser_arg_type arg_type,
865               struct generic_list *list)
866{
867  int i;
868  struct generic_list *tmp;
869
870  if (prev_given && list) {
871    *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
872
873    switch(arg_type) {
874    case ARG_STRING:
875      *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break;
876    default:
877      break;
878    };
879   
880    for (i = (prev_given - 1); i >= 0; --i)
881      {
882        tmp = list;
883       
884        switch(arg_type) {
885        case ARG_STRING:
886          (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break;
887        default:
888          break;
889        }       
890        (*orig_field) [i + field_given] = list->orig;
891        list = list->next;
892        free (tmp);
893      }
894  } else { /* set the default value */
895    if (default_value && ! field_given) {
896      switch(arg_type) {
897      case ARG_STRING:
898        if (! *((char ***)field)) {
899          *((char ***)field) = (char **)malloc (sizeof (char *));
900          (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg);
901        }
902        break;
903      default: break;
904      }
905      if (!(*orig_field)) {
906        *orig_field = (char **) malloc (sizeof (char *));
907        (*orig_field)[0] = 0;
908      }
909    }
910  }
911}
912
913int
914cmdline_parser_internal (
915  int argc, char **argv, struct gengetopt_args_info *args_info,
916                        struct cmdline_parser_params *params, const char *additional_error)
917{
918  int c;        /* Character of the parsed option.  */
919
920  struct generic_list * process_list = NULL;
921  struct generic_list * select_list = NULL;
922  struct generic_list * ignore_list = NULL;
923  struct generic_list * input_field_list = NULL;
924  int error = 0;
925  struct gengetopt_args_info local_args_info;
926 
927  int override;
928  int initialize;
929  int check_required;
930  int check_ambiguity;
931 
932  package_name = argv[0];
933 
934  override = params->override;
935  initialize = params->initialize;
936  check_required = params->check_required;
937  check_ambiguity = params->check_ambiguity;
938
939  if (initialize)
940    cmdline_parser_init (args_info);
941
942  cmdline_parser_init (&local_args_info);
943
944  optarg = 0;
945  optind = 0;
946  opterr = params->print_errors;
947  optopt = '?';
948
949  while (1)
950    {
951      int option_index = 0;
952
953      static struct option long_options[] = {
954        { "help",       0, NULL, 'h' },
955        { "full-help",  0, NULL, 0 },
956        { "version",    0, NULL, 'V' },
957        { "input",      1, NULL, 'f' },
958        { "output",     1, NULL, 'o' },
959        { "fail",       1, NULL, 'e' },
960        { "only-fail",  0, NULL, 0 },
961        { "no-fail",    0, NULL, 0 },
962        { "copy",       0, NULL, 'c' },
963        { "process",    1, NULL, 'p' },
964        { "select",     1, NULL, 's' },
965        { "ignore",     1, NULL, 'S' },
966        { "output-field",       1, NULL, 'O' },
967        { "input-field",        1, NULL, 'I' },
968        { "interactive",        0, NULL, 'i' },
969        { "config",     1, NULL, 0 },
970        { "one-field",  0, NULL, '1' },
971        { "one-line",   0, NULL, 0 },
972        { "language",   1, NULL, 0 },
973        { "color",      0, NULL, 'l' },
974        { 0,  0, 0, 0 }
975      };
976
977      c = getopt_long (argc, argv, "hVf:o:e:cp:s:S:O:I:i1l", long_options, &option_index);
978
979      if (c == -1) break;       /* Exit from `while (1)' loop.  */
980
981      switch (c)
982        {
983        case 'h':       /* Print help and exit.  */
984          cmdline_parser_print_help ();
985          cmdline_parser_free (&local_args_info);
986          exit (EXIT_SUCCESS);
987
988        case 'V':       /* Print version and exit.  */
989          cmdline_parser_print_version ();
990          cmdline_parser_free (&local_args_info);
991          exit (EXIT_SUCCESS);
992
993        case 'f':       /* Input file.  */
994       
995       
996          if (update_arg( (void *)&(args_info->input_arg), 
997               &(args_info->input_orig), &(args_info->input_given),
998              &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
999              check_ambiguity, override, 0, 0,
1000              "input", 'f',
1001              additional_error))
1002            goto failure;
1003       
1004          break;
1005        case 'o':       /* Output file for succesfully processed segments.  */
1006       
1007       
1008          if (update_arg( (void *)&(args_info->output_arg), 
1009               &(args_info->output_orig), &(args_info->output_given),
1010              &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1011              check_ambiguity, override, 0, 0,
1012              "output", 'o',
1013              additional_error))
1014            goto failure;
1015       
1016          break;
1017        case 'e':       /* Output file for unsuccesfully processed segments .  */
1018       
1019       
1020          if (update_arg( (void *)&(args_info->fail_arg), 
1021               &(args_info->fail_orig), &(args_info->fail_given),
1022              &(local_args_info.fail_given), optarg, 0, 0, ARG_STRING,
1023              check_ambiguity, override, 0, 0,
1024              "fail", 'e',
1025              additional_error))
1026            goto failure;
1027       
1028          break;
1029        case 'c':       /* Copy succesfully processed segments to standard output.  */
1030       
1031       
1032          if (update_arg((void *)&(args_info->copy_flag), 0, &(args_info->copy_given),
1033              &(local_args_info.copy_given), optarg, 0, 0, ARG_FLAG,
1034              check_ambiguity, override, 1, 0, "copy", 'c',
1035              additional_error))
1036            goto failure;
1037       
1038          break;
1039        case 'p':       /* Process segments with this tag.  */
1040       
1041          if (update_multiple_arg_temp(&process_list, 
1042              &(local_args_info.process_given), optarg, 0, 0, ARG_STRING,
1043              "process", 'p',
1044              additional_error))
1045            goto failure;
1046       
1047          break;
1048        case 's':       /* Select only segments with this field.  */
1049       
1050          if (update_multiple_arg_temp(&select_list, 
1051              &(local_args_info.select_given), optarg, 0, 0, ARG_STRING,
1052              "select", 's',
1053              additional_error))
1054            goto failure;
1055       
1056          break;
1057        case 'S':       /* Select only segments without this field.  */
1058       
1059          if (update_multiple_arg_temp(&ignore_list, 
1060              &(local_args_info.ignore_given), optarg, 0, 0, ARG_STRING,
1061              "ignore", 'S',
1062              additional_error))
1063            goto failure;
1064       
1065          break;
1066        case 'O':       /* Output field name.  */
1067       
1068       
1069          if (update_arg( (void *)&(args_info->output_field_arg), 
1070               &(args_info->output_field_orig), &(args_info->output_field_given),
1071              &(local_args_info.output_field_given), optarg, 0, 0, ARG_STRING,
1072              check_ambiguity, override, 0, 0,
1073              "output-field", 'O',
1074              additional_error))
1075            goto failure;
1076       
1077          break;
1078        case 'I':       /* Input field name.  */
1079       
1080          if (update_multiple_arg_temp(&input_field_list, 
1081              &(local_args_info.input_field_given), optarg, 0, 0, ARG_STRING,
1082              "input-field", 'I',
1083              additional_error))
1084            goto failure;
1085       
1086          break;
1087        case 'i':       /* Toggle interactive mode.  */
1088       
1089       
1090          if (update_arg((void *)&(args_info->interactive_flag), 0, &(args_info->interactive_given),
1091              &(local_args_info.interactive_given), optarg, 0, 0, ARG_FLAG,
1092              check_ambiguity, override, 1, 0, "interactive", 'i',
1093              additional_error))
1094            goto failure;
1095       
1096          break;
1097        case '1':       /* Print all results in one segments (creates ambiguous annotation).  */
1098       
1099       
1100          if (update_arg((void *)&(args_info->one_field_flag), 0, &(args_info->one_field_given),
1101              &(local_args_info.one_field_given), optarg, 0, 0, ARG_FLAG,
1102              check_ambiguity, override, 1, 0, "one-field", '1',
1103              additional_error))
1104            goto failure;
1105       
1106          break;
1107        case 'l':       /* Show guessed descriptions in colour..  */
1108       
1109       
1110          if (update_arg((void *)&(args_info->color_flag), 0, &(args_info->color_given),
1111              &(local_args_info.color_given), optarg, 0, 0, ARG_FLAG,
1112              check_ambiguity, override, 1, 0, "color", 'l',
1113              additional_error))
1114            goto failure;
1115       
1116          break;
1117
1118        case 0: /* Long option with no short option */
1119          if (strcmp (long_options[option_index].name, "full-help") == 0) {
1120            cmdline_parser_print_full_help ();
1121            cmdline_parser_free (&local_args_info);
1122            exit (EXIT_SUCCESS);
1123          }
1124
1125          /* Print only segments the program failed to process.  */
1126          if (strcmp (long_options[option_index].name, "only-fail") == 0)
1127          {
1128         
1129         
1130            if (update_arg((void *)&(args_info->only_fail_flag), 0, &(args_info->only_fail_given),
1131                &(local_args_info.only_fail_given), optarg, 0, 0, ARG_FLAG,
1132                check_ambiguity, override, 1, 0, "only-fail", '-',
1133                additional_error))
1134              goto failure;
1135         
1136          }
1137          /* Print only segments the program processed.  */
1138          else if (strcmp (long_options[option_index].name, "no-fail") == 0)
1139          {
1140         
1141         
1142            if (update_arg((void *)&(args_info->no_fail_flag), 0, &(args_info->no_fail_given),
1143                &(local_args_info.no_fail_given), optarg, 0, 0, ARG_FLAG,
1144                check_ambiguity, override, 1, 0, "no-fail", '-',
1145                additional_error))
1146              goto failure;
1147         
1148          }
1149          /* Configuration file.  */
1150          else if (strcmp (long_options[option_index].name, "config") == 0)
1151          {
1152         
1153         
1154            if (update_arg( (void *)&(args_info->config_arg), 
1155                 &(args_info->config_orig), &(args_info->config_given),
1156                &(local_args_info.config_given), optarg, 0, 0, ARG_STRING,
1157                check_ambiguity, override, 0, 0,
1158                "config", '-',
1159                additional_error))
1160              goto failure;
1161         
1162          }
1163          /* Print annotation alternatives as additional fields.  */
1164          else if (strcmp (long_options[option_index].name, "one-line") == 0)
1165          {
1166         
1167         
1168            if (update_arg((void *)&(args_info->one_line_flag), 0, &(args_info->one_line_given),
1169                &(local_args_info.one_line_given), optarg, 0, 0, ARG_FLAG,
1170                check_ambiguity, override, 1, 0, "one-line", '-',
1171                additional_error))
1172              goto failure;
1173         
1174          }
1175          /* Language..  */
1176          else if (strcmp (long_options[option_index].name, "language") == 0)
1177          {
1178         
1179         
1180            if (update_arg( (void *)&(args_info->language_arg), 
1181                 &(args_info->language_orig), &(args_info->language_given),
1182                &(local_args_info.language_given), optarg, 0, 0, ARG_STRING,
1183                check_ambiguity, override, 0, 0,
1184                "language", '-',
1185                additional_error))
1186              goto failure;
1187         
1188          }
1189         
1190          break;
1191        case '?':       /* Invalid option.  */
1192          /* `getopt_long' already printed an error message.  */
1193          goto failure;
1194
1195        default:        /* bug: option not considered.  */
1196          fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1197          abort ();
1198        } /* switch */
1199    } /* while */
1200
1201
1202  update_multiple_arg((void *)&(args_info->process_arg),
1203    &(args_info->process_orig), args_info->process_given,
1204    local_args_info.process_given, 0,
1205    ARG_STRING, process_list);
1206  update_multiple_arg((void *)&(args_info->select_arg),
1207    &(args_info->select_orig), args_info->select_given,
1208    local_args_info.select_given, 0,
1209    ARG_STRING, select_list);
1210  update_multiple_arg((void *)&(args_info->ignore_arg),
1211    &(args_info->ignore_orig), args_info->ignore_given,
1212    local_args_info.ignore_given, 0,
1213    ARG_STRING, ignore_list);
1214  update_multiple_arg((void *)&(args_info->input_field_arg),
1215    &(args_info->input_field_orig), args_info->input_field_given,
1216    local_args_info.input_field_given, 0,
1217    ARG_STRING, input_field_list);
1218
1219  args_info->process_given += local_args_info.process_given;
1220  local_args_info.process_given = 0;
1221  args_info->select_given += local_args_info.select_given;
1222  local_args_info.select_given = 0;
1223  args_info->ignore_given += local_args_info.ignore_given;
1224  local_args_info.ignore_given = 0;
1225  args_info->input_field_given += local_args_info.input_field_given;
1226  local_args_info.input_field_given = 0;
1227 
1228  if (check_required)
1229    {
1230      error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1231    }
1232
1233  cmdline_parser_release (&local_args_info);
1234
1235  if ( error )
1236    return (EXIT_FAILURE);
1237
1238  return 0;
1239
1240failure:
1241  free_list (process_list, 1 );
1242  free_list (select_list, 1 );
1243  free_list (ignore_list, 1 );
1244  free_list (input_field_list, 1 );
1245 
1246  cmdline_parser_release (&local_args_info);
1247  return (EXIT_FAILURE);
1248}
Note: See TracBrowser for help on using the repository browser.