Subversion Repositories HelenOS

Rev

Rev 3318 | Rev 3346 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3318 Rev 3336
Line 37... Line 37...
37
#include <errno.h>
37
#include <errno.h>
38
#include <getopt.h>
38
#include <getopt.h>
39
#include <stdlib.h>
39
#include <stdlib.h>
40
#include <string.h>
40
#include <string.h>
41
 
41
 
42
/* HelenOS Port: Something similar to warnx(), however we just print
-
 
43
 * to the console */
-
 
44
 
-
 
45
void warnx(const char *fmt, ...);
-
 
46
 
-
 
47
void warnx(const char *fmt, ...)
-
 
48
{
-
 
49
    va_list vargs;
-
 
50
    va_start(vargs, fmt);
-
 
51
    vprintf(fmt, vargs);
-
 
52
    va_end(vargs);
-
 
53
    printf("\n");
-
 
54
    return;
-
 
55
}
-
 
56
 
-
 
57
/* HelenOS Port : We're incorporating only the modern getopt_long with wrappers
42
/* HelenOS Port : We're incorporating only the modern getopt_long with wrappers
58
 * to keep legacy getopt() usage from breaking. All references to REPLACE_GETOPT
43
 * to keep legacy getopt() usage from breaking. All references to REPLACE_GETOPT
59
 * are dropped, we just include the code */
44
 * are dropped, we just include the code */
60
 
45
 
61
int opterr = 1;     /* if error message should be printed */
46
int opterr = 1;     /* if error message should be printed */
Line 91... Line 76...
91
/* XXX: set optreset to 1 rather than these two */
76
/* XXX: set optreset to 1 rather than these two */
92
static int nonopt_start = -1; /* first non option argument (for permute) */
77
static int nonopt_start = -1; /* first non option argument (for permute) */
93
static int nonopt_end = -1;   /* first option after non options (for permute) */
78
static int nonopt_end = -1;   /* first option after non options (for permute) */
94
 
79
 
95
/* Error messages */
80
/* Error messages */
-
 
81
 
-
 
82
/* HelenOS Port: Calls to warnx() were eliminated (as we have no stderr that
-
 
83
 * may be redirected) and replaced with printf. As such, error messages now
-
 
84
 * end in a newline */
-
 
85
 
96
static const char recargchar[] = "option requires an argument -- %c";
86
static const char recargchar[] = "option requires an argument -- %c\n";
97
static const char recargstring[] = "option requires an argument -- %s";
87
static const char recargstring[] = "option requires an argument -- %s\n";
98
static const char ambig[] = "ambiguous option -- %.*s";
88
static const char ambig[] = "ambiguous option -- %.*s\n";
99
static const char noarg[] = "option doesn't take an argument -- %.*s";
89
static const char noarg[] = "option doesn't take an argument -- %.*s\n";
100
static const char illoptchar[] = "unknown option -- %c";
90
static const char illoptchar[] = "unknown option -- %c\n";
101
static const char illoptstring[] = "unknown option -- %s";
91
static const char illoptstring[] = "unknown option -- %s\n";
102
 
92
 
103
 
93
 
104
/*
94
/*
105
 * Compute the greatest common divisor of a and b.
95
 * Compute the greatest common divisor of a and b.
106
 */
96
 */
Line 254... Line 244...
254
        (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
244
        (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
255
        /* option letter unknown or ':' */
245
        /* option letter unknown or ':' */
256
        if (!*place)
246
        if (!*place)
257
            ++optind;
247
            ++optind;
258
        if (PRINT_ERROR)
248
        if (PRINT_ERROR)
259
            warnx(illoptchar, optchar);
249
            printf(illoptchar, optchar);
260
        optopt = optchar;
250
        optopt = optchar;
261
        return BADCH;
251
        return BADCH;
262
    }
252
    }
263
    if (optchar == 'W' && oli[1] == ';') {      /* -W long-option */
253
    if (optchar == 'W' && oli[1] == ';') {      /* -W long-option */
264
        /* XXX: what if no long options provided (called by getopt)? */
254
        /* XXX: what if no long options provided (called by getopt)? */
Line 266... Line 256...
266
            return -2;
256
            return -2;
267
 
257
 
268
        if (++optind >= nargc) {    /* no arg */
258
        if (++optind >= nargc) {    /* no arg */
269
            place = EMSG;
259
            place = EMSG;
270
            if (PRINT_ERROR)
260
            if (PRINT_ERROR)
271
                warnx(recargchar, optchar);
261
                printf(recargchar, optchar);
272
            optopt = optchar;
262
            optopt = optchar;
273
            return BADARG;
263
            return BADARG;
274
        } else              /* white space */
264
        } else              /* white space */
275
            place = nargv[optind];
265
            place = nargv[optind];
276
        /*
266
        /*
Line 289... Line 279...
289
        /* XXX: disable test for :: if PC? (GNU doesn't) */
279
        /* XXX: disable test for :: if PC? (GNU doesn't) */
290
        else if (oli[1] != ':') {   /* arg not optional */
280
        else if (oli[1] != ':') {   /* arg not optional */
291
            if (++optind >= nargc) {    /* no arg */
281
            if (++optind >= nargc) {    /* no arg */
292
                place = EMSG;
282
                place = EMSG;
293
                if (PRINT_ERROR)
283
                if (PRINT_ERROR)
294
                    warnx(recargchar, optchar);
284
                    printf(recargchar, optchar);
295
                optopt = optchar;
285
                optopt = optchar;
296
                return BADARG;
286
                return BADARG;
297
            } else
287
            } else
298
                optarg = nargv[optind];
288
                optarg = nargv[optind];
299
        }
289
        }
Line 413... Line 403...
413
                ambiguous = 1;
403
                ambiguous = 1;
414
        }
404
        }
415
        if (ambiguous) {
405
        if (ambiguous) {
416
            /* ambiguous abbreviation */
406
            /* ambiguous abbreviation */
417
            if (PRINT_ERROR)
407
            if (PRINT_ERROR)
418
                warnx(ambig, (int)current_argv_len,
408
                printf(ambig, (int)current_argv_len,
419
                     current_argv);
409
                     current_argv);
420
            optopt = 0;
410
            optopt = 0;
421
            return BADCH;
411
            return BADCH;
422
        }
412
        }
423
        if (match != -1) {          /* option found */
413
        if (match != -1) {          /* option found */
424
                if (long_options[match].has_arg == no_argument
414
                if (long_options[match].has_arg == no_argument
425
                && has_equal) {
415
                && has_equal) {
426
                if (PRINT_ERROR)
416
                if (PRINT_ERROR)
427
                    warnx(noarg, (int)current_argv_len,
417
                    printf(noarg, (int)current_argv_len,
428
                         current_argv);
418
                         current_argv);
429
                /*
419
                /*
430
                 * XXX: GNU sets optopt to val regardless of
420
                 * XXX: GNU sets optopt to val regardless of
431
                 * flag
421
                 * flag
432
                 */
422
                 */
Line 454... Line 444...
454
                /*
444
                /*
455
                 * Missing argument; leading ':'
445
                 * Missing argument; leading ':'
456
                 * indicates no error should be generated
446
                 * indicates no error should be generated
457
                 */
447
                 */
458
                if (PRINT_ERROR)
448
                if (PRINT_ERROR)
459
                    warnx(recargstring, current_argv);
449
                    printf(recargstring, current_argv);
460
                /*
450
                /*
461
                 * XXX: GNU sets optopt to val regardless
451
                 * XXX: GNU sets optopt to val regardless
462
                 * of flag
452
                 * of flag
463
                 */
453
                 */
464
                if (long_options[match].flag == NULL)
454
                if (long_options[match].flag == NULL)
Line 468... Line 458...
468
                --optind;
458
                --optind;
469
                return BADARG;
459
                return BADARG;
470
            }
460
            }
471
        } else {            /* unknown option */
461
        } else {            /* unknown option */
472
            if (PRINT_ERROR)
462
            if (PRINT_ERROR)
473
                warnx(illoptstring, current_argv);
463
                printf(illoptstring, current_argv);
474
            optopt = 0;
464
            optopt = 0;
475
            return BADCH;
465
            return BADCH;
476
        }
466
        }
477
        if (long_options[match].flag) {
467
        if (long_options[match].flag) {
478
            *long_options[match].flag = long_options[match].val;
468
            *long_options[match].flag = long_options[match].val;