~ubuntu-branches/ubuntu/hardy/gengetopt/hardy

« back to all changes in this revision

Viewing changes to tests/test_redef_help_cmd.c.test

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-01-29 14:55:40 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080129145540-bkah1bl330gpelmh
Tags: 2.22-1ubuntu1
* Merge with Debian; remaining changes:
  - Fix build failures with g++-4.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
const char *gengetopt_args_info_description = "";
29
29
 
30
30
const char *gengetopt_args_info_help[] = {
31
 
  "  -h, --height  height",
32
 
  "  -V, --vavoom  vavoom instead of version",
 
31
  "      --help     Print help and exit",
 
32
  "      --version  Print version and exit",
 
33
  "  -h, --height   height",
 
34
  "  -V, --vavoom   vavoom instead of version",
33
35
    0
34
36
};
35
37
 
 
38
typedef enum {ARG_NO
 
39
} test_redef_help_cmd_parser_arg_type;
 
40
 
36
41
static
37
42
void clear_given (struct gengetopt_args_info *args_info);
38
43
static
49
54
static
50
55
void clear_given (struct gengetopt_args_info *args_info)
51
56
{
 
57
  args_info->help_given = 0 ;
 
58
  args_info->version_given = 0 ;
52
59
  args_info->height_given = 0 ;
53
60
  args_info->vavoom_given = 0 ;
54
61
}
62
69
static
63
70
void init_args_info(struct gengetopt_args_info *args_info)
64
71
{
65
 
  args_info->height_help = gengetopt_args_info_help[0] ;
66
 
  args_info->vavoom_help = gengetopt_args_info_help[1] ;
 
72
 
 
73
 
 
74
  args_info->help_help = gengetopt_args_info_help[0] ;
 
75
  args_info->version_help = gengetopt_args_info_help[1] ;
 
76
  args_info->height_help = gengetopt_args_info_help[2] ;
 
77
  args_info->vavoom_help = gengetopt_args_info_help[3] ;
67
78
  
68
79
}
69
80
 
73
84
  printf ("%s %s\n", TEST_REDEF_HELP_CMD_PARSER_PACKAGE, TEST_REDEF_HELP_CMD_PARSER_VERSION);
74
85
}
75
86
 
76
 
void
77
 
test_redef_help_cmd_parser_print_help (void)
78
 
{
79
 
  int i = 0;
 
87
static void print_help_common(void) {
80
88
  test_redef_help_cmd_parser_print_version ();
81
89
 
82
90
  if (strlen(gengetopt_args_info_purpose) > 0)
83
91
    printf("\n%s\n", gengetopt_args_info_purpose);
84
92
 
85
 
  printf("\n%s\n\n", gengetopt_args_info_usage);
 
93
  if (strlen(gengetopt_args_info_usage) > 0)
 
94
    printf("\n%s\n", gengetopt_args_info_usage);
 
95
 
 
96
  printf("\n");
86
97
 
87
98
  if (strlen(gengetopt_args_info_description) > 0)
88
99
    printf("%s\n", gengetopt_args_info_description);
 
100
}
89
101
 
 
102
void
 
103
test_redef_help_cmd_parser_print_help (void)
 
104
{
 
105
  int i = 0;
 
106
  print_help_common();
90
107
  while (gengetopt_args_info_help[i])
91
108
    printf("%s\n", gengetopt_args_info_help[i++]);
92
109
}
102
119
  args_info->inputs_num = 0;
103
120
}
104
121
 
105
 
struct test_redef_help_cmd_parser_params *
106
 
test_redef_help_cmd_parser_params_init()
 
122
void
 
123
test_redef_help_cmd_parser_params_init(struct test_redef_help_cmd_parser_params *params)
107
124
{
108
 
  struct test_redef_help_cmd_parser_params *params = 
109
 
    (struct test_redef_help_cmd_parser_params *)malloc(sizeof(struct test_redef_help_cmd_parser_params));
110
 
 
111
125
  if (params)
112
126
    { 
113
127
      params->override = 0;
114
 
      params->initialize = 0;
115
 
      params->check_required = 0;
 
128
      params->initialize = 1;
 
129
      params->check_required = 1;
116
130
      params->check_ambiguity = 0;
 
131
      params->print_errors = 1;
117
132
    }
118
 
    
 
133
}
 
134
 
 
135
struct test_redef_help_cmd_parser_params *
 
136
test_redef_help_cmd_parser_params_create(void)
 
137
{
 
138
  struct test_redef_help_cmd_parser_params *params = 
 
139
    (struct test_redef_help_cmd_parser_params *)malloc(sizeof(struct test_redef_help_cmd_parser_params));
 
140
  test_redef_help_cmd_parser_params_init(params);  
119
141
  return params;
120
142
}
121
143
 
 
144
 
 
145
 
122
146
static void
123
147
test_redef_help_cmd_parser_release (struct gengetopt_args_info *args_info)
124
148
{
125
 
  
126
149
  unsigned int i;
127
150
  
 
151
  
128
152
  for (i = 0; i < args_info->inputs_num; ++i)
129
153
    free (args_info->inputs [i]);
130
 
  
 
154
 
131
155
  if (args_info->inputs_num)
132
156
    free (args_info->inputs);
 
157
 
 
158
  clear_given (args_info);
 
159
}
 
160
 
 
161
 
 
162
static void
 
163
write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
 
164
{
 
165
  if (arg) {
 
166
    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
 
167
  } else {
 
168
    fprintf(outfile, "%s\n", opt);
 
169
  }
 
170
}
 
171
 
 
172
 
 
173
int
 
174
test_redef_help_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
 
175
{
 
176
  int i = 0;
 
177
 
 
178
  if (!outfile)
 
179
    {
 
180
      fprintf (stderr, "%s: cannot dump options to stream\n", TEST_REDEF_HELP_CMD_PARSER_PACKAGE);
 
181
      return EXIT_FAILURE;
 
182
    }
 
183
 
 
184
  if (args_info->help_given)
 
185
    write_into_file(outfile, "help", 0, 0 );
 
186
  if (args_info->version_given)
 
187
    write_into_file(outfile, "version", 0, 0 );
 
188
  if (args_info->height_given)
 
189
    write_into_file(outfile, "height", 0, 0 );
 
190
  if (args_info->vavoom_given)
 
191
    write_into_file(outfile, "vavoom", 0, 0 );
133
192
  
134
 
  clear_given (args_info);
 
193
 
 
194
  i = EXIT_SUCCESS;
 
195
  return i;
135
196
}
136
197
 
137
198
int
148
209
      return EXIT_FAILURE;
149
210
    }
150
211
 
151
 
  if (args_info->height_given) {
152
 
    fprintf(outfile, "%s\n", "height");
153
 
  }
154
 
  if (args_info->vavoom_given) {
155
 
    fprintf(outfile, "%s\n", "vavoom");
156
 
  }
157
 
  
 
212
  i = test_redef_help_cmd_parser_dump(outfile, args_info);
158
213
  fclose (outfile);
159
214
 
160
 
  i = EXIT_SUCCESS;
161
215
  return i;
162
216
}
163
217
 
167
221
  test_redef_help_cmd_parser_release (args_info);
168
222
}
169
223
 
170
 
 
171
 
/* gengetopt_strdup() */
172
 
/* strdup.c replacement of strdup, which is not standard */
 
224
/** @brief replacement of strdup, which is not standard */
173
225
char *
174
226
gengetopt_strdup (const char *s)
175
227
{
216
268
  params.initialize = initialize;
217
269
  params.check_required = check_required;
218
270
  params.check_ambiguity = 0;
 
271
  params.print_errors = 1;
219
272
 
220
273
  result = test_redef_help_cmd_parser_internal (argc, argv, args_info, &params, NULL);
221
274
 
234
287
  return EXIT_SUCCESS;
235
288
}
236
289
 
 
290
 
 
291
static char *package_name = 0;
 
292
 
 
293
/**
 
294
 * @brief updates an option
 
295
 * @param field the generic pointer to the field to update
 
296
 * @param orig_field the pointer to the orig field
 
297
 * @param field_given the pointer to the number of occurrence of this option
 
298
 * @param prev_given the pointer to the number of occurrence already seen
 
299
 * @param value the argument for this option (if null no arg was specified)
 
300
 * @param possible_values the possible values for this option (if specified)
 
301
 * @param default_value the default value (in case the option only accepts fixed values)
 
302
 * @param arg_type the type of this option
 
303
 * @param check_ambiguity @see test_redef_help_cmd_parser_params.check_ambiguity
 
304
 * @param override @see test_redef_help_cmd_parser_params.override
 
305
 * @param no_free whether to free a possible previous value
 
306
 * @param multiple_option whether this is a multiple option
 
307
 * @param long_opt the corresponding long option
 
308
 * @param short_opt the corresponding short option (or '-' if none)
 
309
 * @param additional_error possible further error specification
 
310
 */
 
311
static
 
312
int update_arg(void *field, char **orig_field,
 
313
               unsigned int *field_given, unsigned int *prev_given, 
 
314
               char *value, char *possible_values[], const char *default_value,
 
315
               test_redef_help_cmd_parser_arg_type arg_type,
 
316
               int check_ambiguity, int override,
 
317
               int no_free, int multiple_option,
 
318
               const char *long_opt, char short_opt,
 
319
               const char *additional_error)
 
320
{
 
321
  char *stop_char = 0;
 
322
  const char *val = value;
 
323
  int found;
 
324
 
 
325
  stop_char = 0;
 
326
  found = 0;
 
327
 
 
328
  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
 
329
    {
 
330
      if (short_opt != '-')
 
331
        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
 
332
               package_name, long_opt, short_opt,
 
333
               (additional_error ? additional_error : ""));
 
334
      else
 
335
        fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
 
336
               package_name, long_opt,
 
337
               (additional_error ? additional_error : ""));
 
338
      return 1; /* failure */
 
339
    }
 
340
 
 
341
    
 
342
  if (field_given && *field_given && ! override)
 
343
    return 0;
 
344
  if (prev_given)
 
345
    (*prev_given)++;
 
346
  if (field_given)
 
347
    (*field_given)++;
 
348
  if (possible_values)
 
349
    val = possible_values[found];
 
350
 
 
351
  switch(arg_type) {
 
352
  default:
 
353
    break;
 
354
  };
 
355
 
 
356
 
 
357
  /* store the original value */
 
358
  switch(arg_type) {
 
359
  case ARG_NO:
 
360
    break;
 
361
  default:
 
362
    if (value && orig_field) {
 
363
      if (no_free) {
 
364
        *orig_field = value;
 
365
      } else {
 
366
        if (*orig_field)
 
367
          free (*orig_field); /* free previous string */
 
368
        *orig_field = gengetopt_strdup (value);
 
369
      }
 
370
    }
 
371
  };
 
372
 
 
373
  return 0; /* OK */
 
374
}
 
375
 
 
376
 
237
377
int
238
378
test_redef_help_cmd_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
239
379
                        struct test_redef_help_cmd_parser_params *params, const char *additional_error)
248
388
  int check_required;
249
389
  int check_ambiguity;
250
390
  
 
391
  package_name = argv[0];
 
392
  
251
393
  override = params->override;
252
394
  initialize = params->initialize;
253
395
  check_required = params->check_required;
260
402
 
261
403
  optarg = 0;
262
404
  optind = 0;
263
 
  opterr = 1;
 
405
  opterr = params->print_errors;
264
406
  optopt = '?';
265
407
 
266
408
  while (1)
267
409
    {
268
410
      int option_index = 0;
269
 
      char *stop_char;
270
411
 
271
412
      static struct option long_options[] = {
 
413
        { "help",       0, NULL, 0 },
 
414
        { "version",    0, NULL, 0 },
272
415
        { "height",     0, NULL, 'h' },
273
416
        { "vavoom",     0, NULL, 'V' },
274
417
        { NULL, 0, NULL, 0 }
275
418
      };
276
419
 
277
 
      stop_char = 0;
278
420
      c = getopt_long (argc, argv, "hV", long_options, &option_index);
279
421
 
280
422
      if (c == -1) break;       /* Exit from `while (1)' loop.  */
282
424
      switch (c)
283
425
        {
284
426
        case 'h':       /* height.  */
285
 
          if (local_args_info.height_given || (check_ambiguity && args_info->height_given))
286
 
            {
287
 
              fprintf (stderr, "%s: `--height' (`-h') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
288
 
              goto failure;
289
 
            }
290
 
          if (args_info->height_given && ! override)
291
 
            continue;
292
 
          local_args_info.height_given = 1;
293
 
          args_info->height_given = 1;
 
427
        
 
428
        
 
429
          if (update_arg( 0 , 
 
430
               0 , &(args_info->height_given),
 
431
              &(local_args_info.height_given), optarg, 0, 0, ARG_NO,
 
432
              check_ambiguity, override, 0, 0,
 
433
              "height", 'h',
 
434
              additional_error))
 
435
            goto failure;
 
436
        
294
437
          break;
295
 
 
296
438
        case 'V':       /* vavoom instead of version.  */
297
 
          if (local_args_info.vavoom_given || (check_ambiguity && args_info->vavoom_given))
298
 
            {
299
 
              fprintf (stderr, "%s: `--vavoom' (`-V') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
300
 
              goto failure;
301
 
            }
302
 
          if (args_info->vavoom_given && ! override)
303
 
            continue;
304
 
          local_args_info.vavoom_given = 1;
305
 
          args_info->vavoom_given = 1;
 
439
        
 
440
        
 
441
          if (update_arg( 0 , 
 
442
               0 , &(args_info->vavoom_given),
 
443
              &(local_args_info.vavoom_given), optarg, 0, 0, ARG_NO,
 
444
              check_ambiguity, override, 0, 0,
 
445
              "vavoom", 'V',
 
446
              additional_error))
 
447
            goto failure;
 
448
        
306
449
          break;
307
450
 
308
 
 
309
451
        case 0: /* Long option with no short option */
 
452
          if (strcmp (long_options[option_index].name, "help") == 0) {
 
453
            test_redef_help_cmd_parser_print_help ();
 
454
            test_redef_help_cmd_parser_free (&local_args_info);
 
455
            exit (EXIT_SUCCESS);
 
456
          }
 
457
 
 
458
          if (strcmp (long_options[option_index].name, "version") == 0) {
 
459
            test_redef_help_cmd_parser_print_version ();
 
460
            test_redef_help_cmd_parser_free (&local_args_info);
 
461
            exit (EXIT_SUCCESS);
 
462
          }
 
463
 
310
464
        case '?':       /* Invalid option.  */
311
465
          /* `getopt_long' already printed an error message.  */
312
466
          goto failure;