~ubuntu-branches/ubuntu/quantal/gengetopt/quantal

« back to all changes in this revision

Viewing changes to tests/test_only_flags_cmd.c.test

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2011-09-26 10:27:31 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: package-import@ubuntu.com-20110926102731-vh42p0vriqz4kp3h
Tags: 2.22.5-1
* New upstream release:
  - correctly wraps and preprocess the specified usage string
  - handle escaped newline chars in the .ggo file correctly
  - initialize enum variables with a generated null value (makes
    the parser compilable in C++)
  - removed warnings in generated parser when only flags are used
* Fix gbp config file.
* Bump Standards.
* Drop unneeded dependency on dpkg (>= 1.15.4), stable has a newer version.
* Refresh patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  File autogenerated by gengetopt 
 
3
  generated with the following command:
 
4
  ../src/gengetopt --gen-version --input=../../tests/test_only_flags_cmd.ggo --func-name=test_only_flags_cmd_parser --file-name=test_only_flags_cmd --unamed-opt --show-required 
 
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 "test_only_flags_cmd.h"
 
27
 
 
28
const char *gengetopt_args_info_purpose = "";
 
29
 
 
30
const char *gengetopt_args_info_usage = "Usage: test_only_flags [OPTIONS]... [FILES]...";
 
31
 
 
32
const char *gengetopt_args_info_description = "";
 
33
 
 
34
const char *gengetopt_args_info_help[] = {
 
35
  "  -h, --help     Print help and exit",
 
36
  "  -V, --version  Print version and exit",
 
37
  "  -f, --foo      foo option  (default=on)",
 
38
    0
 
39
};
 
40
 
 
41
typedef enum {ARG_NO
 
42
  , ARG_FLAG
 
43
} test_only_flags_cmd_parser_arg_type;
 
44
 
 
45
static
 
46
void clear_given (struct gengetopt_args_info *args_info);
 
47
static
 
48
void clear_args (struct gengetopt_args_info *args_info);
 
49
 
 
50
static int
 
51
test_only_flags_cmd_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
 
52
                        struct test_only_flags_cmd_parser_params *params, const char *additional_error);
 
53
 
 
54
 
 
55
static char *
 
56
gengetopt_strdup (const char *s);
 
57
 
 
58
static
 
59
void clear_given (struct gengetopt_args_info *args_info)
 
60
{
 
61
  args_info->help_given = 0 ;
 
62
  args_info->version_given = 0 ;
 
63
  args_info->foo_given = 0 ;
 
64
}
 
65
 
 
66
static
 
67
void clear_args (struct gengetopt_args_info *args_info)
 
68
{
 
69
  FIX_UNUSED (args_info);
 
70
  args_info->foo_flag = 1;
 
71
  
 
72
}
 
73
 
 
74
static
 
75
void init_args_info(struct gengetopt_args_info *args_info)
 
76
{
 
77
 
 
78
 
 
79
  args_info->help_help = gengetopt_args_info_help[0] ;
 
80
  args_info->version_help = gengetopt_args_info_help[1] ;
 
81
  args_info->foo_help = gengetopt_args_info_help[2] ;
 
82
  
 
83
}
 
84
 
 
85
void
 
86
test_only_flags_cmd_parser_print_version (void)
 
87
{
 
88
  printf ("%s %s\n",
 
89
     (strlen(TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE_NAME) ? TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE_NAME : TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE),
 
90
     TEST_ONLY_FLAGS_CMD_PARSER_VERSION);
 
91
}
 
92
 
 
93
static void print_help_common(void) {
 
94
  test_only_flags_cmd_parser_print_version ();
 
95
 
 
96
  if (strlen(gengetopt_args_info_purpose) > 0)
 
97
    printf("\n%s\n", gengetopt_args_info_purpose);
 
98
 
 
99
  if (strlen(gengetopt_args_info_usage) > 0)
 
100
    printf("\n%s\n", gengetopt_args_info_usage);
 
101
 
 
102
  printf("\n");
 
103
 
 
104
  if (strlen(gengetopt_args_info_description) > 0)
 
105
    printf("%s\n\n", gengetopt_args_info_description);
 
106
}
 
107
 
 
108
void
 
109
test_only_flags_cmd_parser_print_help (void)
 
110
{
 
111
  int i = 0;
 
112
  print_help_common();
 
113
  while (gengetopt_args_info_help[i])
 
114
    printf("%s\n", gengetopt_args_info_help[i++]);
 
115
}
 
116
 
 
117
void
 
118
test_only_flags_cmd_parser_init (struct gengetopt_args_info *args_info)
 
119
{
 
120
  clear_given (args_info);
 
121
  clear_args (args_info);
 
122
  init_args_info (args_info);
 
123
 
 
124
  args_info->inputs = 0;
 
125
  args_info->inputs_num = 0;
 
126
}
 
127
 
 
128
void
 
129
test_only_flags_cmd_parser_params_init(struct test_only_flags_cmd_parser_params *params)
 
130
{
 
131
  if (params)
 
132
    { 
 
133
      params->override = 0;
 
134
      params->initialize = 1;
 
135
      params->check_required = 1;
 
136
      params->check_ambiguity = 0;
 
137
      params->print_errors = 1;
 
138
    }
 
139
}
 
140
 
 
141
struct test_only_flags_cmd_parser_params *
 
142
test_only_flags_cmd_parser_params_create(void)
 
143
{
 
144
  struct test_only_flags_cmd_parser_params *params = 
 
145
    (struct test_only_flags_cmd_parser_params *)malloc(sizeof(struct test_only_flags_cmd_parser_params));
 
146
  test_only_flags_cmd_parser_params_init(params);  
 
147
  return params;
 
148
}
 
149
 
 
150
 
 
151
 
 
152
static void
 
153
test_only_flags_cmd_parser_release (struct gengetopt_args_info *args_info)
 
154
{
 
155
  unsigned int i;
 
156
  
 
157
  
 
158
  for (i = 0; i < args_info->inputs_num; ++i)
 
159
    free (args_info->inputs [i]);
 
160
 
 
161
  if (args_info->inputs_num)
 
162
    free (args_info->inputs);
 
163
 
 
164
  clear_given (args_info);
 
165
}
 
166
 
 
167
 
 
168
static void
 
169
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
 
170
{
 
171
  FIX_UNUSED (values);
 
172
  if (arg) {
 
173
    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
 
174
  } else {
 
175
    fprintf(outfile, "%s\n", opt);
 
176
  }
 
177
}
 
178
 
 
179
 
 
180
int
 
181
test_only_flags_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
 
182
{
 
183
  int i = 0;
 
184
 
 
185
  if (!outfile)
 
186
    {
 
187
      fprintf (stderr, "%s: cannot dump options to stream\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE);
 
188
      return EXIT_FAILURE;
 
189
    }
 
190
 
 
191
  if (args_info->help_given)
 
192
    write_into_file(outfile, "help", 0, 0 );
 
193
  if (args_info->version_given)
 
194
    write_into_file(outfile, "version", 0, 0 );
 
195
  if (args_info->foo_given)
 
196
    write_into_file(outfile, "foo", 0, 0 );
 
197
  
 
198
 
 
199
  i = EXIT_SUCCESS;
 
200
  return i;
 
201
}
 
202
 
 
203
int
 
204
test_only_flags_cmd_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
 
205
{
 
206
  FILE *outfile;
 
207
  int i = 0;
 
208
 
 
209
  outfile = fopen(filename, "w");
 
210
 
 
211
  if (!outfile)
 
212
    {
 
213
      fprintf (stderr, "%s: cannot open file for writing: %s\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE, filename);
 
214
      return EXIT_FAILURE;
 
215
    }
 
216
 
 
217
  i = test_only_flags_cmd_parser_dump(outfile, args_info);
 
218
  fclose (outfile);
 
219
 
 
220
  return i;
 
221
}
 
222
 
 
223
void
 
224
test_only_flags_cmd_parser_free (struct gengetopt_args_info *args_info)
 
225
{
 
226
  test_only_flags_cmd_parser_release (args_info);
 
227
}
 
228
 
 
229
/** @brief replacement of strdup, which is not standard */
 
230
char *
 
231
gengetopt_strdup (const char *s)
 
232
{
 
233
  char *result = 0;
 
234
  if (!s)
 
235
    return result;
 
236
 
 
237
  result = (char*)malloc(strlen(s) + 1);
 
238
  if (result == (char*)0)
 
239
    return (char*)0;
 
240
  strcpy(result, s);
 
241
  return result;
 
242
}
 
243
 
 
244
int
 
245
test_only_flags_cmd_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
 
246
{
 
247
  return test_only_flags_cmd_parser2 (argc, argv, args_info, 0, 1, 1);
 
248
}
 
249
 
 
250
int
 
251
test_only_flags_cmd_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
 
252
                   struct test_only_flags_cmd_parser_params *params)
 
253
{
 
254
  int result;
 
255
  result = test_only_flags_cmd_parser_internal (argc, argv, args_info, params, 0);
 
256
 
 
257
  if (result == EXIT_FAILURE)
 
258
    {
 
259
      test_only_flags_cmd_parser_free (args_info);
 
260
      exit (EXIT_FAILURE);
 
261
    }
 
262
  
 
263
  return result;
 
264
}
 
265
 
 
266
int
 
267
test_only_flags_cmd_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
 
268
{
 
269
  int result;
 
270
  struct test_only_flags_cmd_parser_params params;
 
271
  
 
272
  params.override = override;
 
273
  params.initialize = initialize;
 
274
  params.check_required = check_required;
 
275
  params.check_ambiguity = 0;
 
276
  params.print_errors = 1;
 
277
 
 
278
  result = test_only_flags_cmd_parser_internal (argc, argv, args_info, &params, 0);
 
279
 
 
280
  if (result == EXIT_FAILURE)
 
281
    {
 
282
      test_only_flags_cmd_parser_free (args_info);
 
283
      exit (EXIT_FAILURE);
 
284
    }
 
285
  
 
286
  return result;
 
287
}
 
288
 
 
289
int
 
290
test_only_flags_cmd_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
 
291
{
 
292
  FIX_UNUSED (args_info);
 
293
  FIX_UNUSED (prog_name);
 
294
  return EXIT_SUCCESS;
 
295
}
 
296
 
 
297
 
 
298
static char *package_name = 0;
 
299
 
 
300
/**
 
301
 * @brief updates an option
 
302
 * @param field the generic pointer to the field to update
 
303
 * @param orig_field the pointer to the orig field
 
304
 * @param field_given the pointer to the number of occurrence of this option
 
305
 * @param prev_given the pointer to the number of occurrence already seen
 
306
 * @param value the argument for this option (if null no arg was specified)
 
307
 * @param possible_values the possible values for this option (if specified)
 
308
 * @param default_value the default value (in case the option only accepts fixed values)
 
309
 * @param arg_type the type of this option
 
310
 * @param check_ambiguity @see test_only_flags_cmd_parser_params.check_ambiguity
 
311
 * @param override @see test_only_flags_cmd_parser_params.override
 
312
 * @param no_free whether to free a possible previous value
 
313
 * @param multiple_option whether this is a multiple option
 
314
 * @param long_opt the corresponding long option
 
315
 * @param short_opt the corresponding short option (or '-' if none)
 
316
 * @param additional_error possible further error specification
 
317
 */
 
318
static
 
319
int update_arg(void *field, char **orig_field,
 
320
               unsigned int *field_given, unsigned int *prev_given, 
 
321
               char *value, const char *possible_values[],
 
322
               const char *default_value,
 
323
               test_only_flags_cmd_parser_arg_type arg_type,
 
324
               int check_ambiguity, int override,
 
325
               int no_free, int multiple_option,
 
326
               const char *long_opt, char short_opt,
 
327
               const char *additional_error)
 
328
{
 
329
  char *stop_char = 0;
 
330
  const char *val = value;
 
331
  int found;
 
332
  FIX_UNUSED (field);
 
333
 
 
334
  stop_char = 0;
 
335
  found = 0;
 
336
 
 
337
  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
 
338
    {
 
339
      if (short_opt != '-')
 
340
        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
 
341
               package_name, long_opt, short_opt,
 
342
               (additional_error ? additional_error : ""));
 
343
      else
 
344
        fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
 
345
               package_name, long_opt,
 
346
               (additional_error ? additional_error : ""));
 
347
      return 1; /* failure */
 
348
    }
 
349
 
 
350
  FIX_UNUSED (default_value);
 
351
    
 
352
  if (field_given && *field_given && ! override)
 
353
    return 0;
 
354
  if (prev_given)
 
355
    (*prev_given)++;
 
356
  if (field_given)
 
357
    (*field_given)++;
 
358
  if (possible_values)
 
359
    val = possible_values[found];
 
360
 
 
361
  switch(arg_type) {
 
362
  case ARG_FLAG:
 
363
    *((int *)field) = !*((int *)field);
 
364
    break;
 
365
  default:
 
366
    break;
 
367
  };
 
368
 
 
369
 
 
370
  /* store the original value */
 
371
  switch(arg_type) {
 
372
  case ARG_NO:
 
373
  case ARG_FLAG:
 
374
    break;
 
375
  default:
 
376
    if (value && orig_field) {
 
377
      if (no_free) {
 
378
        *orig_field = value;
 
379
      } else {
 
380
        if (*orig_field)
 
381
          free (*orig_field); /* free previous string */
 
382
        *orig_field = gengetopt_strdup (value);
 
383
      }
 
384
    }
 
385
  };
 
386
 
 
387
  return 0; /* OK */
 
388
}
 
389
 
 
390
 
 
391
int
 
392
test_only_flags_cmd_parser_internal (
 
393
  int argc, char **argv, struct gengetopt_args_info *args_info,
 
394
                        struct test_only_flags_cmd_parser_params *params, const char *additional_error)
 
395
{
 
396
  int c;        /* Character of the parsed option.  */
 
397
 
 
398
  int error = 0;
 
399
  struct gengetopt_args_info local_args_info;
 
400
  
 
401
  int override;
 
402
  int initialize;
 
403
  int check_required;
 
404
  int check_ambiguity;
 
405
  
 
406
  package_name = argv[0];
 
407
  
 
408
  override = params->override;
 
409
  initialize = params->initialize;
 
410
  check_required = params->check_required;
 
411
  check_ambiguity = params->check_ambiguity;
 
412
 
 
413
  if (initialize)
 
414
    test_only_flags_cmd_parser_init (args_info);
 
415
 
 
416
  test_only_flags_cmd_parser_init (&local_args_info);
 
417
 
 
418
  optarg = 0;
 
419
  optind = 0;
 
420
  opterr = params->print_errors;
 
421
  optopt = '?';
 
422
 
 
423
  while (1)
 
424
    {
 
425
      int option_index = 0;
 
426
 
 
427
      static struct option long_options[] = {
 
428
        { "help",       0, NULL, 'h' },
 
429
        { "version",    0, NULL, 'V' },
 
430
        { "foo",        0, NULL, 'f' },
 
431
        { 0,  0, 0, 0 }
 
432
      };
 
433
 
 
434
      c = getopt_long (argc, argv, "hVf", long_options, &option_index);
 
435
 
 
436
      if (c == -1) break;       /* Exit from `while (1)' loop.  */
 
437
 
 
438
      switch (c)
 
439
        {
 
440
        case 'h':       /* Print help and exit.  */
 
441
          test_only_flags_cmd_parser_print_help ();
 
442
          test_only_flags_cmd_parser_free (&local_args_info);
 
443
          exit (EXIT_SUCCESS);
 
444
 
 
445
        case 'V':       /* Print version and exit.  */
 
446
          test_only_flags_cmd_parser_print_version ();
 
447
          test_only_flags_cmd_parser_free (&local_args_info);
 
448
          exit (EXIT_SUCCESS);
 
449
 
 
450
        case 'f':       /* foo option.  */
 
451
        
 
452
        
 
453
          if (update_arg((void *)&(args_info->foo_flag), 0, &(args_info->foo_given),
 
454
              &(local_args_info.foo_given), optarg, 0, 0, ARG_FLAG,
 
455
              check_ambiguity, override, 1, 0, "foo", 'f',
 
456
              additional_error))
 
457
            goto failure;
 
458
        
 
459
          break;
 
460
 
 
461
        case 0: /* Long option with no short option */
 
462
        case '?':       /* Invalid option.  */
 
463
          /* `getopt_long' already printed an error message.  */
 
464
          goto failure;
 
465
 
 
466
        default:        /* bug: option not considered.  */
 
467
          fprintf (stderr, "%s: option unknown: %c%s\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
 
468
          abort ();
 
469
        } /* switch */
 
470
    } /* while */
 
471
 
 
472
 
 
473
 
 
474
 
 
475
  test_only_flags_cmd_parser_release (&local_args_info);
 
476
 
 
477
  if ( error )
 
478
    return (EXIT_FAILURE);
 
479
 
 
480
  if (optind < argc)
 
481
    {
 
482
      int i = 0 ;
 
483
      int found_prog_name = 0;
 
484
      /* whether program name, i.e., argv[0], is in the remaining args
 
485
         (this may happen with some implementations of getopt,
 
486
          but surely not with the one included by gengetopt) */
 
487
 
 
488
      i = optind;
 
489
      while (i < argc)
 
490
        if (argv[i++] == argv[0]) {
 
491
          found_prog_name = 1;
 
492
          break;
 
493
        }
 
494
      i = 0;
 
495
 
 
496
      args_info->inputs_num = argc - optind - found_prog_name;
 
497
      args_info->inputs =
 
498
        (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
 
499
      while (optind < argc)
 
500
        if (argv[optind++] != argv[0])
 
501
          args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
 
502
    }
 
503
 
 
504
  return 0;
 
505
 
 
506
failure:
 
507
  
 
508
  test_only_flags_cmd_parser_release (&local_args_info);
 
509
  return (EXIT_FAILURE);
 
510
}