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

« back to all changes in this revision

Viewing changes to tests/no_optgiven_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:
34
34
    0
35
35
};
36
36
 
 
37
typedef enum {ARG_NO
 
38
  , ARG_INT
 
39
} no_optgiven_cmd_parser_arg_type;
 
40
 
37
41
static
38
42
void clear_given (struct gengetopt_args_info *args_info);
39
43
static
67
71
static
68
72
void init_args_info(struct gengetopt_args_info *args_info)
69
73
{
 
74
 
 
75
 
70
76
  args_info->help_help = gengetopt_args_info_help[0] ;
71
77
  args_info->version_help = gengetopt_args_info_help[1] ;
72
78
  args_info->foo_help = gengetopt_args_info_help[2] ;
79
85
  printf ("%s %s\n", NO_OPTGIVEN_CMD_PARSER_PACKAGE, NO_OPTGIVEN_CMD_PARSER_VERSION);
80
86
}
81
87
 
82
 
void
83
 
no_optgiven_cmd_parser_print_help (void)
84
 
{
85
 
  int i = 0;
 
88
static void print_help_common(void) {
86
89
  no_optgiven_cmd_parser_print_version ();
87
90
 
88
91
  if (strlen(gengetopt_args_info_purpose) > 0)
89
92
    printf("\n%s\n", gengetopt_args_info_purpose);
90
93
 
91
 
  printf("\n%s\n\n", gengetopt_args_info_usage);
 
94
  if (strlen(gengetopt_args_info_usage) > 0)
 
95
    printf("\n%s\n", gengetopt_args_info_usage);
 
96
 
 
97
  printf("\n");
92
98
 
93
99
  if (strlen(gengetopt_args_info_description) > 0)
94
100
    printf("%s\n", gengetopt_args_info_description);
 
101
}
95
102
 
 
103
void
 
104
no_optgiven_cmd_parser_print_help (void)
 
105
{
 
106
  int i = 0;
 
107
  print_help_common();
96
108
  while (gengetopt_args_info_help[i])
97
109
    printf("%s\n", gengetopt_args_info_help[i++]);
98
110
}
108
120
  args_info->inputs_num = 0;
109
121
}
110
122
 
111
 
struct no_optgiven_cmd_parser_params *
112
 
no_optgiven_cmd_parser_params_init()
 
123
void
 
124
no_optgiven_cmd_parser_params_init(struct no_optgiven_cmd_parser_params *params)
113
125
{
114
 
  struct no_optgiven_cmd_parser_params *params = 
115
 
    (struct no_optgiven_cmd_parser_params *)malloc(sizeof(struct no_optgiven_cmd_parser_params));
116
 
 
117
126
  if (params)
118
127
    { 
119
128
      params->override = 0;
120
 
      params->initialize = 0;
121
 
      params->check_required = 0;
 
129
      params->initialize = 1;
 
130
      params->check_required = 1;
122
131
      params->check_ambiguity = 0;
 
132
      params->print_errors = 1;
123
133
    }
124
 
    
 
134
}
 
135
 
 
136
struct no_optgiven_cmd_parser_params *
 
137
no_optgiven_cmd_parser_params_create(void)
 
138
{
 
139
  struct no_optgiven_cmd_parser_params *params = 
 
140
    (struct no_optgiven_cmd_parser_params *)malloc(sizeof(struct no_optgiven_cmd_parser_params));
 
141
  no_optgiven_cmd_parser_params_init(params);  
125
142
  return params;
126
143
}
127
144
 
128
145
static void
 
146
free_string_field (char **s)
 
147
{
 
148
  if (*s)
 
149
    {
 
150
      free (*s);
 
151
      *s = 0;
 
152
    }
 
153
}
 
154
 
 
155
 
 
156
static void
129
157
no_optgiven_cmd_parser_release (struct gengetopt_args_info *args_info)
130
158
{
131
 
  
132
159
  unsigned int i;
133
 
  if (args_info->foo_orig)
134
 
    {
135
 
      free (args_info->foo_orig); /* free previous argument */
136
 
      args_info->foo_orig = 0;
137
 
    }
 
160
  free_string_field (&(args_info->foo_orig));
 
161
  
138
162
  
139
163
  for (i = 0; i < args_info->inputs_num; ++i)
140
164
    free (args_info->inputs [i]);
141
 
  
 
165
 
142
166
  if (args_info->inputs_num)
143
167
    free (args_info->inputs);
 
168
 
 
169
  clear_given (args_info);
 
170
}
 
171
 
 
172
 
 
173
static void
 
174
write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
 
175
{
 
176
  if (arg) {
 
177
    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
 
178
  } else {
 
179
    fprintf(outfile, "%s\n", opt);
 
180
  }
 
181
}
 
182
 
 
183
 
 
184
int
 
185
no_optgiven_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
 
186
{
 
187
  int i = 0;
 
188
 
 
189
  if (!outfile)
 
190
    {
 
191
      fprintf (stderr, "%s: cannot dump options to stream\n", NO_OPTGIVEN_CMD_PARSER_PACKAGE);
 
192
      return EXIT_FAILURE;
 
193
    }
 
194
 
 
195
  if (args_info->help_given)
 
196
    write_into_file(outfile, "help", 0, 0 );
 
197
  if (args_info->version_given)
 
198
    write_into_file(outfile, "version", 0, 0 );
 
199
  if (args_info->foo_given)
 
200
    write_into_file(outfile, "foo", args_info->foo_orig, 0);
144
201
  
145
 
  clear_given (args_info);
 
202
 
 
203
  i = EXIT_SUCCESS;
 
204
  return i;
146
205
}
147
206
 
148
207
int
159
218
      return EXIT_FAILURE;
160
219
    }
161
220
 
162
 
  if (args_info->help_given) {
163
 
    fprintf(outfile, "%s\n", "help");
164
 
  }
165
 
  if (args_info->version_given) {
166
 
    fprintf(outfile, "%s\n", "version");
167
 
  }
168
 
  if (args_info->foo_given) {
169
 
    if (args_info->foo_orig) {
170
 
      fprintf(outfile, "%s=\"%s\"\n", "foo", args_info->foo_orig);
171
 
    } else {
172
 
      fprintf(outfile, "%s\n", "foo");
173
 
    }
174
 
  }
175
 
  
 
221
  i = no_optgiven_cmd_parser_dump(outfile, args_info);
176
222
  fclose (outfile);
177
223
 
178
 
  i = EXIT_SUCCESS;
179
224
  return i;
180
225
}
181
226
 
185
230
  no_optgiven_cmd_parser_release (args_info);
186
231
}
187
232
 
188
 
 
189
 
/* gengetopt_strdup() */
190
 
/* strdup.c replacement of strdup, which is not standard */
 
233
/** @brief replacement of strdup, which is not standard */
191
234
char *
192
235
gengetopt_strdup (const char *s)
193
236
{
228
271
  params.initialize = initialize;
229
272
  params.check_required = check_required;
230
273
  params.check_ambiguity = 0;
 
274
  params.print_errors = 1;
231
275
 
232
276
  result = no_optgiven_cmd_parser_internal (argc, argv, args_info, &params, NULL);
233
277
 
263
307
  return error;
264
308
}
265
309
 
 
310
 
 
311
static char *package_name = 0;
 
312
 
 
313
/**
 
314
 * @brief updates an option
 
315
 * @param field the generic pointer to the field to update
 
316
 * @param orig_field the pointer to the orig field
 
317
 * @param field_given the pointer to the number of occurrence of this option
 
318
 * @param prev_given the pointer to the number of occurrence already seen
 
319
 * @param value the argument for this option (if null no arg was specified)
 
320
 * @param possible_values the possible values for this option (if specified)
 
321
 * @param default_value the default value (in case the option only accepts fixed values)
 
322
 * @param arg_type the type of this option
 
323
 * @param check_ambiguity @see no_optgiven_cmd_parser_params.check_ambiguity
 
324
 * @param override @see no_optgiven_cmd_parser_params.override
 
325
 * @param no_free whether to free a possible previous value
 
326
 * @param multiple_option whether this is a multiple option
 
327
 * @param long_opt the corresponding long option
 
328
 * @param short_opt the corresponding short option (or '-' if none)
 
329
 * @param additional_error possible further error specification
 
330
 */
 
331
static
 
332
int update_arg(void *field, char **orig_field,
 
333
               unsigned int *field_given, unsigned int *prev_given, 
 
334
               char *value, char *possible_values[], const char *default_value,
 
335
               no_optgiven_cmd_parser_arg_type arg_type,
 
336
               int check_ambiguity, int override,
 
337
               int no_free, int multiple_option,
 
338
               const char *long_opt, char short_opt,
 
339
               const char *additional_error)
 
340
{
 
341
  char *stop_char = 0;
 
342
  const char *val = value;
 
343
  int found;
 
344
 
 
345
  stop_char = 0;
 
346
  found = 0;
 
347
 
 
348
  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
 
349
    {
 
350
      if (short_opt != '-')
 
351
        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
 
352
               package_name, long_opt, short_opt,
 
353
               (additional_error ? additional_error : ""));
 
354
      else
 
355
        fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
 
356
               package_name, long_opt,
 
357
               (additional_error ? additional_error : ""));
 
358
      return 1; /* failure */
 
359
    }
 
360
 
 
361
    
 
362
  if (field_given && *field_given && ! override)
 
363
    return 0;
 
364
  if (prev_given)
 
365
    (*prev_given)++;
 
366
  if (field_given)
 
367
    (*field_given)++;
 
368
  if (possible_values)
 
369
    val = possible_values[found];
 
370
 
 
371
  switch(arg_type) {
 
372
  case ARG_INT:
 
373
    if (val) *((int *)field) = strtol (val, &stop_char, 0);
 
374
    break;
 
375
  default:
 
376
    break;
 
377
  };
 
378
 
 
379
  /* check numeric conversion */
 
380
  switch(arg_type) {
 
381
  case ARG_INT:
 
382
    if (val && !(stop_char && *stop_char == '\0')) {
 
383
      fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
 
384
      return 1; /* failure */
 
385
    }
 
386
    break;
 
387
  default:
 
388
    ;
 
389
  };
 
390
 
 
391
  /* store the original value */
 
392
  switch(arg_type) {
 
393
  case ARG_NO:
 
394
    break;
 
395
  default:
 
396
    if (value && orig_field) {
 
397
      if (no_free) {
 
398
        *orig_field = value;
 
399
      } else {
 
400
        if (*orig_field)
 
401
          free (*orig_field); /* free previous string */
 
402
        *orig_field = gengetopt_strdup (value);
 
403
      }
 
404
    }
 
405
  };
 
406
 
 
407
  return 0; /* OK */
 
408
}
 
409
 
 
410
 
266
411
int
267
412
no_optgiven_cmd_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
268
413
                        struct no_optgiven_cmd_parser_params *params, const char *additional_error)
277
422
  int check_required;
278
423
  int check_ambiguity;
279
424
  
 
425
  package_name = argv[0];
 
426
  
280
427
  override = params->override;
281
428
  initialize = params->initialize;
282
429
  check_required = params->check_required;
289
436
 
290
437
  optarg = 0;
291
438
  optind = 0;
292
 
  opterr = 1;
 
439
  opterr = params->print_errors;
293
440
  optopt = '?';
294
441
 
295
442
  while (1)
296
443
    {
297
444
      int option_index = 0;
298
 
      char *stop_char;
299
445
 
300
446
      static struct option long_options[] = {
301
447
        { "help",       0, NULL, 'h' },
304
450
        { NULL, 0, NULL, 0 }
305
451
      };
306
452
 
307
 
      stop_char = 0;
308
453
      c = getopt_long (argc, argv, "hVi:", long_options, &option_index);
309
454
 
310
455
      if (c == -1) break;       /* Exit from `while (1)' loop.  */
322
467
          exit (EXIT_SUCCESS);
323
468
 
324
469
        case 'i':       /* foo option.  */
325
 
          if (local_args_info.foo_given || (check_ambiguity && args_info->foo_given))
326
 
            {
327
 
              fprintf (stderr, "%s: `--foo' (`-i') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
328
 
              goto failure;
329
 
            }
330
 
          if (args_info->foo_given && ! override)
331
 
            continue;
332
 
          local_args_info.foo_given = 1;
333
 
          args_info->foo_given = 1;
334
 
          args_info->foo_arg = strtol (optarg, &stop_char, 0);
335
 
          if (!(stop_char && *stop_char == '\0')) {
336
 
            fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
 
470
        
 
471
        
 
472
          if (update_arg( (void *)&(args_info->foo_arg), 
 
473
               &(args_info->foo_orig), &(args_info->foo_given),
 
474
              &(local_args_info.foo_given), optarg, 0, 0, ARG_INT,
 
475
              check_ambiguity, override, 0, 0,
 
476
              "foo", 'i',
 
477
              additional_error))
337
478
            goto failure;
338
 
          }
339
 
          if (args_info->foo_orig)
340
 
            free (args_info->foo_orig); /* free previous string */
341
 
          args_info->foo_orig = gengetopt_strdup (optarg);
 
479
        
342
480
          break;
343
481
 
344
 
 
345
482
        case 0: /* Long option with no short option */
346
483
        case '?':       /* Invalid option.  */
347
484
          /* `getopt_long' already printed an error message.  */