~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.cc

  • Committer: Brian Aker
  • Date: 2009-10-12 06:15:02 UTC
  • mfrom: (1165.1.178 static-functions)
  • Revision ID: brian@gaz-20091012061502-cds4m0cya7ow8sj7
Merge Stewart

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
typedef void (*init_func_p)(const struct my_option *option, char **variable,
33
33
                            int64_t value);
34
34
 
35
 
void default_reporter(enum loglevel level, const char *format, ...);
 
35
extern "C" void default_reporter(enum loglevel level, const char *format, ...);
36
36
my_error_reporter my_getopt_error_reporter= &default_reporter;
37
37
 
38
38
static int findopt(char *optpat, uint32_t length,
67
67
char *disabled_my_option= (char*) "0";
68
68
 
69
69
/*
70
 
   This is a flag that can be set in client programs. 0 means that
71
 
   my_getopt will not print error messages, but the client should do
72
 
   it by itself
73
 
*/
74
 
 
75
 
bool my_getopt_print_errors= 1;
76
 
 
77
 
/*
78
70
   This is a flag that can be set in client programs. 1 means that
79
71
   my_getopt will skip over options it does not know how to handle.
80
72
*/
81
73
 
82
74
bool my_getopt_skip_unknown= 0;
83
75
 
84
 
void default_reporter(enum loglevel level,
85
 
                      const char *format, ...)
 
76
extern "C" void default_reporter(enum loglevel level, const char *format, ...)
86
77
{
87
78
  va_list args;
88
79
  va_start(args, format);
108
99
 
109
100
static getopt_get_addr_func getopt_get_addr;
110
101
 
111
 
void my_getopt_register_get_addr(getopt_get_addr_func func_addr)
112
 
{
113
 
  getopt_get_addr= func_addr;
114
 
}
115
 
 
116
102
int handle_options(int *argc, char ***argv,
117
103
                   const struct my_option *longopts,
118
104
                   my_get_one_option get_one_option)
156
142
            /* the argument must be in next argv */
157
143
            if (!*++pos)
158
144
            {
159
 
              if (my_getopt_print_errors)
160
 
                my_getopt_error_reporter(ERROR_LEVEL,
161
 
                                         "%s: Option '-O' requires an argument",
162
 
                                         my_progname);
 
145
              my_getopt_error_reporter(ERROR_LEVEL,
 
146
                                       "%s: Option '-O' requires an argument",
 
147
                                       my_progname);
163
148
              return EXIT_ARGUMENT_REQUIRED;
164
149
            }
165
150
            cur_arg= *pos;
174
159
            cur_arg+= 14;
175
160
            if (!*cur_arg)
176
161
            {
177
 
              if (my_getopt_print_errors)
178
 
                my_getopt_error_reporter(ERROR_LEVEL,
179
 
                                         "%s: Option '--set-variable' requires an argument",
180
 
                                         my_progname);
 
162
              my_getopt_error_reporter(ERROR_LEVEL,
 
163
                                       "%s: Option '--set-variable' requires an argument",
 
164
                                       my_progname);
181
165
              return EXIT_ARGUMENT_REQUIRED;
182
166
            }
183
167
          }
188
172
            /* the argument must be in next argv */
189
173
            if (!*++pos)
190
174
            {
191
 
              if (my_getopt_print_errors)
192
 
                my_getopt_error_reporter(ERROR_LEVEL,
193
 
                                         "%s: Option '--set-variable' requires an argument",
194
 
                                         my_progname);
 
175
              my_getopt_error_reporter(ERROR_LEVEL,
 
176
                                       "%s: Option '--set-variable' requires an argument",
 
177
                                       my_progname);
195
178
              return EXIT_ARGUMENT_REQUIRED;
196
179
            }
197
180
            cur_arg= *pos;
254
237
                {
255
238
                  if (opt_found > 1)
256
239
                  {
257
 
                    if (my_getopt_print_errors)
258
 
                      my_getopt_error_reporter(ERROR_LEVEL,
259
 
                                               "%s: ambiguous option '--%s-%s' (--%s-%s)",
260
 
                                               my_progname, special_opt_prefix[i],
261
 
                                               cur_arg, special_opt_prefix[i],
262
 
                                               prev_found);
 
240
                    my_getopt_error_reporter(ERROR_LEVEL,
 
241
                                             "%s: ambiguous option '--%s-%s' (--%s-%s)",
 
242
                                             my_progname, special_opt_prefix[i],
 
243
                                             cur_arg, special_opt_prefix[i],
 
244
                                             prev_found);
263
245
                    return EXIT_AMBIGUOUS_OPTION;
264
246
                  }
265
247
                  switch (i) {
304
286
            }
305
287
            if (must_be_var)
306
288
            {
307
 
              if (my_getopt_print_errors)
308
 
                my_getopt_error_reporter(option_is_loose ?
309
 
                                           WARNING_LEVEL : ERROR_LEVEL,
310
 
                                         "%s: unknown variable '%s'",
311
 
                                         my_progname, cur_arg);
 
289
              my_getopt_error_reporter(option_is_loose ?
 
290
                                       WARNING_LEVEL : ERROR_LEVEL,
 
291
                                       "%s: unknown variable '%s'",
 
292
                                       my_progname, cur_arg);
312
293
              if (!option_is_loose)
313
294
                return EXIT_UNKNOWN_VARIABLE;
314
295
            }
315
296
            else
316
297
            {
317
 
              if (my_getopt_print_errors)
318
 
                my_getopt_error_reporter(option_is_loose ?
319
 
                                           WARNING_LEVEL : ERROR_LEVEL,
320
 
                                         "%s: unknown option '--%s'",
321
 
                                         my_progname, cur_arg);
 
298
              my_getopt_error_reporter(option_is_loose ?
 
299
                                       WARNING_LEVEL : ERROR_LEVEL,
 
300
                                       "%s: unknown option '--%s'",
 
301
                                       my_progname, cur_arg);
322
302
              if (!option_is_loose)
323
303
                return EXIT_UNKNOWN_OPTION;
324
304
            }
333
313
        {
334
314
          if (must_be_var)
335
315
          {
336
 
            if (my_getopt_print_errors)
337
 
              my_getopt_error_reporter(ERROR_LEVEL,
338
 
                                       "%s: variable prefix '%s' is not unique",
339
 
                                       my_progname, opt_str);
 
316
            my_getopt_error_reporter(ERROR_LEVEL,
 
317
                                     "%s: variable prefix '%s' is not unique",
 
318
                                     my_progname, opt_str);
340
319
            return EXIT_VAR_PREFIX_NOT_UNIQUE;
341
320
          }
342
321
          else
343
322
          {
344
 
            if (my_getopt_print_errors)
345
 
              my_getopt_error_reporter(ERROR_LEVEL,
346
 
                                       "%s: ambiguous option '--%s' (%s, %s)",
347
 
                                       my_progname, opt_str, prev_found,
348
 
                                       optp->name);
 
323
            my_getopt_error_reporter(ERROR_LEVEL,
 
324
                                     "%s: ambiguous option '--%s' (%s, %s)",
 
325
                                     my_progname, opt_str, prev_found,
 
326
                                     optp->name);
349
327
            return EXIT_AMBIGUOUS_OPTION;
350
328
          }
351
329
        }
352
330
        if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
353
331
        {
354
 
          if (my_getopt_print_errors)
355
 
            fprintf(stderr,
356
 
                    _("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
357
 
                    option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
 
332
          fprintf(stderr,
 
333
                  _("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
 
334
                  option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
358
335
          if (option_is_loose)
359
336
          {
360
337
            (*argc)--;
364
341
        }
365
342
        if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG)
366
343
        {
367
 
          if (my_getopt_print_errors)
368
 
            my_getopt_error_reporter(ERROR_LEVEL,
369
 
                                     "%s: option '%s' cannot take an argument",
370
 
                                     my_progname, optp->name);
 
344
          my_getopt_error_reporter(ERROR_LEVEL,
 
345
                                   "%s: option '%s' cannot take an argument",
 
346
                                   my_progname, optp->name);
371
347
          return EXIT_NO_ARGUMENT_ALLOWED;
372
348
        }
373
349
        value= optp->var_type & GET_ASK_ADDR ?
377
353
        {
378
354
          if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL)
379
355
          {
380
 
            if (my_getopt_print_errors)
381
 
              my_getopt_error_reporter(ERROR_LEVEL,
382
 
                                       "%s: option '--%s' cannot take an argument",
383
 
                                       my_progname, optp->name);
 
356
            my_getopt_error_reporter(ERROR_LEVEL,
 
357
                                     "%s: option '--%s' cannot take an argument",
 
358
                                     my_progname, optp->name);
384
359
            return EXIT_NO_ARGUMENT_ALLOWED;
385
360
          }
386
361
          if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL)
430
405
          /* Check if there are more arguments after this one */
431
406
          if (!*++pos)
432
407
          {
433
 
            if (my_getopt_print_errors)
434
 
              my_getopt_error_reporter(ERROR_LEVEL,
435
 
                                       "%s: option '--%s' requires an argument",
436
 
                                       my_progname, optp->name);
 
408
            my_getopt_error_reporter(ERROR_LEVEL,
 
409
                                     "%s: option '--%s' requires an argument",
 
410
                                     my_progname, optp->name);
437
411
            return EXIT_ARGUMENT_REQUIRED;
438
412
          }
439
413
          argument= *pos;
455
429
              opt_found= 1;
456
430
              if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
457
431
              {
458
 
                if (my_getopt_print_errors)
459
 
                  fprintf(stderr,
460
 
                          _("%s: ERROR: Option '-%c' used, but is disabled\n"),
461
 
                          my_progname, optp->id);
 
432
                fprintf(stderr,
 
433
                        _("%s: ERROR: Option '-%c' used, but is disabled\n"),
 
434
                        my_progname, optp->id);
462
435
                return EXIT_OPTION_DISABLED;
463
436
              }
464
437
              if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL &&
490
463
                  /* Check if there are more arguments after this one */
491
464
                  if (!pos[1])
492
465
                  {
493
 
                    if (my_getopt_print_errors)
494
 
                      my_getopt_error_reporter(ERROR_LEVEL,
495
 
                                               "%s: option '-%c' requires an argument",
496
 
                                               my_progname, optp->id);
 
466
                    my_getopt_error_reporter(ERROR_LEVEL,
 
467
                                             "%s: option '-%c' requires an argument",
 
468
                                             my_progname, optp->id);
497
469
                    return EXIT_ARGUMENT_REQUIRED;
498
470
                  }
499
471
                  argument= *++pos;
515
487
          }
516
488
          if (!opt_found)
517
489
          {
518
 
            if (my_getopt_print_errors)
519
 
              my_getopt_error_reporter(ERROR_LEVEL,
520
 
                                       "%s: unknown option '-%c'",
521
 
                                       my_progname, *optend);
 
490
            my_getopt_error_reporter(ERROR_LEVEL,
 
491
                                     "%s: unknown option '-%c'",
 
492
                                     my_progname, *optend);
522
493
            return EXIT_UNKNOWN_OPTION;
523
494
          }
524
495
        }