~ubuntu-branches/ubuntu/natty/x264/natty

« back to all changes in this revision

Viewing changes to extras/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-02-14 12:51:13 UTC
  • Revision ID: james.westby@ubuntu.com-20060214125113-t2vdkiqgcctz9ndd
Tags: upstream-0.cvs20060210
ImportĀ upstreamĀ versionĀ 0.cvs20060210

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $      */
 
2
 
 
3
/*-
 
4
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 
5
 * All rights reserved.
 
6
 *
 
7
 * This code is derived from software contributed to The NetBSD Foundation
 
8
 * by Dieter Baron and Thomas Klausner.
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions
 
12
 * are met:
 
13
 * 1. Redistributions of source code must retain the above copyright
 
14
 *    notice, this list of conditions and the following disclaimer.
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *    notice, this list of conditions and the following disclaimer in the
 
17
 *    documentation and/or other materials provided with the distribution.
 
18
 * 3. All advertising materials mentioning features or use of this software
 
19
 *    must display the following acknowledgement:
 
20
 *        This product includes software developed by the NetBSD
 
21
 *        Foundation, Inc. and its contributors.
 
22
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 
23
 *    contributors may be used to endorse or promote products derived
 
24
 *    from this software without specific prior written permission.
 
25
 *
 
26
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 
27
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
28
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
29
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 
30
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
32
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
33
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
34
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
35
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
36
 * POSSIBILITY OF SUCH DAMAGE.
 
37
 */
 
38
 
 
39
#include <assert.h>
 
40
#include <errno.h>
 
41
#include <stdlib.h>
 
42
#include <string.h>
 
43
#include <getopt.h>
 
44
#include <stdarg.h>
 
45
#include <stdio.h>
 
46
 
 
47
#define REPLACE_GETOPT
 
48
 
 
49
#define _DIAGASSERT(x) do {} while (0)
 
50
 
 
51
#ifdef REPLACE_GETOPT
 
52
#ifdef __weak_alias
 
53
__weak_alias(getopt,_getopt)
 
54
#endif
 
55
int opterr = 1; /* if error message should be printed */
 
56
int optind = 1; /* index into parent argv vector */
 
57
int optopt = '?';       /* character checked for validity */
 
58
int optreset;   /* reset getopt */
 
59
char *optarg;   /* argument associated with option */
 
60
#endif
 
61
 
 
62
#ifdef __weak_alias
 
63
__weak_alias(getopt_long,_getopt_long)
 
64
#endif
 
65
 
 
66
char *__progname = "x264";
 
67
 
 
68
#define IGNORE_FIRST    (*options == '-' || *options == '+')
 
69
#define PRINT_ERROR     ((opterr) && ((*options != ':') \
 
70
                                      || (IGNORE_FIRST && options[1] != ':')))
 
71
 
 
72
#define IS_POSIXLY_CORRECT (getenv("POSIXLY_INCORRECT_GETOPT") == NULL)
 
73
 
 
74
#define PERMUTE         (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
 
75
/* XXX: GNU ignores PC if *options == '-' */
 
76
#define IN_ORDER        (!IS_POSIXLY_CORRECT && *options == '-')
 
77
 
 
78
/* return values */
 
79
#define BADCH   (int)'?'
 
80
#define BADARG          ((IGNORE_FIRST && options[1] == ':') \
 
81
                         || (*options == ':') ? (int)':' : (int)'?')
 
82
#define INORDER (int)1
 
83
 
 
84
static char EMSG[1];
 
85
 
 
86
static int getopt_internal (int, char * const *, const char *);
 
87
static int gcd (int, int);
 
88
static void permute_args (int, int, int, char * const *);
 
89
 
 
90
static char *place = EMSG; /* option letter processing */
 
91
 
 
92
/* XXX: set optreset to 1 rather than these two */
 
93
static int nonopt_start = -1; /* first non option argument (for permute) */
 
94
static int nonopt_end = -1;   /* first option after non options (for permute) */
 
95
 
 
96
/* Error messages */
 
97
static const char recargchar[] = "option requires an argument -- %c";
 
98
static const char recargstring[] = "option requires an argument -- %s";
 
99
static const char ambig[] = "ambiguous option -- %.*s";
 
100
static const char noarg[] = "option doesn't take an argument -- %.*s";
 
101
static const char illoptchar[] = "unknown option -- %c";
 
102
static const char illoptstring[] = "unknown option -- %s";
 
103
 
 
104
static void
 
105
_vwarnx(const char *fmt, va_list ap)
 
106
{
 
107
  (void)fprintf(stderr, "%s: ", __progname);
 
108
  if (fmt != NULL)
 
109
    (void)vfprintf(stderr, fmt, ap);
 
110
  (void)fprintf(stderr, "\n");
 
111
}
 
112
 
 
113
static void
 
114
warnx(const char *fmt, ...)
 
115
{
 
116
  va_list ap;
 
117
  va_start(ap, fmt);
 
118
  _vwarnx(fmt, ap);
 
119
  va_end(ap);
 
120
}
 
121
 
 
122
/*
 
123
 * Compute the greatest common divisor of a and b.
 
124
 */
 
125
static int
 
126
gcd(a, b)
 
127
        int a;
 
128
        int b;
 
129
{
 
130
        int c;
 
131
 
 
132
        c = a % b;
 
133
        while (c != 0) {
 
134
                a = b;
 
135
                b = c;
 
136
                c = a % b;
 
137
        }
 
138
 
 
139
        return b;
 
140
}
 
141
 
 
142
/*
 
143
 * Exchange the block from nonopt_start to nonopt_end with the block
 
144
 * from nonopt_end to opt_end (keeping the same order of arguments
 
145
 * in each block).
 
146
 */
 
147
static void
 
148
permute_args(panonopt_start, panonopt_end, opt_end, nargv)
 
149
        int panonopt_start;
 
150
        int panonopt_end;
 
151
        int opt_end;
 
152
        char * const *nargv;
 
153
{
 
154
        int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
 
155
        char *swap;
 
156
 
 
157
        _DIAGASSERT(nargv != NULL);
 
158
 
 
159
        /*
 
160
         * compute lengths of blocks and number and size of cycles
 
161
         */
 
162
        nnonopts = panonopt_end - panonopt_start;
 
163
        nopts = opt_end - panonopt_end;
 
164
        ncycle = gcd(nnonopts, nopts);
 
165
        cyclelen = (opt_end - panonopt_start) / ncycle;
 
166
 
 
167
        for (i = 0; i < ncycle; i++) {
 
168
                cstart = panonopt_end+i;
 
169
                pos = cstart;
 
170
                for (j = 0; j < cyclelen; j++) {
 
171
                        if (pos >= panonopt_end)
 
172
                                pos -= nnonopts;
 
173
                        else
 
174
                                pos += nopts;
 
175
                        swap = nargv[pos];
 
176
                        /* LINTED const cast */
 
177
                        ((char **) nargv)[pos] = nargv[cstart];
 
178
                        /* LINTED const cast */
 
179
                        ((char **)nargv)[cstart] = swap;
 
180
                }
 
181
        }
 
182
}
 
183
 
 
184
/*
 
185
 * getopt_internal --
 
186
 *      Parse argc/argv argument vector.  Called by user level routines.
 
187
 *  Returns -2 if -- is found (can be long option or end of options marker).
 
188
 */
 
189
static int
 
190
getopt_internal(nargc, nargv, options)
 
191
        int nargc;
 
192
        char * const *nargv;
 
193
        const char *options;
 
194
{
 
195
        char *oli;                              /* option letter list index */
 
196
        int optchar;
 
197
 
 
198
        _DIAGASSERT(nargv != NULL);
 
199
        _DIAGASSERT(options != NULL);
 
200
 
 
201
        optarg = NULL;
 
202
 
 
203
        /*
 
204
         * XXX Some programs (like rsyncd) expect to be able to
 
205
         * XXX re-initialize optind to 0 and have getopt_long(3)
 
206
         * XXX properly function again.  Work around this braindamage.
 
207
         */
 
208
        if (optind == 0)
 
209
                optind = 1;
 
210
 
 
211
        if (optreset)
 
212
                nonopt_start = nonopt_end = -1;
 
213
start:
 
214
        if (optreset || !*place) {              /* update scanning pointer */
 
215
                optreset = 0;
 
216
                if (optind >= nargc) {          /* end of argument vector */
 
217
                        place = EMSG;
 
218
                        if (nonopt_end != -1) {
 
219
                                /* do permutation, if we have to */
 
220
                                permute_args(nonopt_start, nonopt_end,
 
221
                                    optind, nargv);
 
222
                                optind -= nonopt_end - nonopt_start;
 
223
                        }
 
224
                        else if (nonopt_start != -1) {
 
225
                                /*
 
226
                                 * If we skipped non-options, set optind
 
227
                                 * to the first of them.
 
228
                                 */
 
229
                                optind = nonopt_start;
 
230
                        }
 
231
                        nonopt_start = nonopt_end = -1;
 
232
                        return -1;
 
233
                }
 
234
                if ((*(place = nargv[optind]) != '-')
 
235
                    || (place[1] == '\0')) {    /* found non-option */
 
236
                        place = EMSG;
 
237
                        if (IN_ORDER) {
 
238
                                /*
 
239
                                 * GNU extension:
 
240
                                 * return non-option as argument to option 1
 
241
                                 */
 
242
                                optarg = nargv[optind++];
 
243
                                return INORDER;
 
244
                        }
 
245
                        if (!PERMUTE) {
 
246
                                /*
 
247
                                 * if no permutation wanted, stop parsing
 
248
                                 * at first non-option
 
249
                                 */
 
250
                                return -1;
 
251
                        }
 
252
                        /* do permutation */
 
253
                        if (nonopt_start == -1)
 
254
                                nonopt_start = optind;
 
255
                        else if (nonopt_end != -1) {
 
256
                                permute_args(nonopt_start, nonopt_end,
 
257
                                    optind, nargv);
 
258
                                nonopt_start = optind -
 
259
                                    (nonopt_end - nonopt_start);
 
260
                                nonopt_end = -1;
 
261
                        }
 
262
                        optind++;
 
263
                        /* process next argument */
 
264
                        goto start;
 
265
                }
 
266
                if (nonopt_start != -1 && nonopt_end == -1)
 
267
                        nonopt_end = optind;
 
268
                if (place[1] && *++place == '-') {      /* found "--" */
 
269
                        place++;
 
270
                        return -2;
 
271
                }
 
272
        }
 
273
        if ((optchar = (int)*place++) == (int)':' ||
 
274
            (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
 
275
                /* option letter unknown or ':' */
 
276
                if (!*place)
 
277
                        ++optind;
 
278
                if (PRINT_ERROR)
 
279
                        warnx(illoptchar, optchar);
 
280
                optopt = optchar;
 
281
                return BADCH;
 
282
        }
 
283
        if (optchar == 'W' && oli[1] == ';') {          /* -W long-option */
 
284
                /* XXX: what if no long options provided (called by getopt)? */
 
285
                if (*place)
 
286
                        return -2;
 
287
 
 
288
                if (++optind >= nargc) {        /* no arg */
 
289
                        place = EMSG;
 
290
                        if (PRINT_ERROR)
 
291
                                warnx(recargchar, optchar);
 
292
                        optopt = optchar;
 
293
                        return BADARG;
 
294
                } else                          /* white space */
 
295
                        place = nargv[optind];
 
296
                /*
 
297
                 * Handle -W arg the same as --arg (which causes getopt to
 
298
                 * stop parsing).
 
299
                 */
 
300
                return -2;
 
301
        }
 
302
        if (*++oli != ':') {                    /* doesn't take argument */
 
303
                if (!*place)
 
304
                        ++optind;
 
305
        } else {                                /* takes (optional) argument */
 
306
                optarg = NULL;
 
307
                if (*place)                     /* no white space */
 
308
                        optarg = place;
 
309
                /* XXX: disable test for :: if PC? (GNU doesn't) */
 
310
                else if (oli[1] != ':') {       /* arg not optional */
 
311
                        if (++optind >= nargc) {        /* no arg */
 
312
                                place = EMSG;
 
313
                                if (PRINT_ERROR)
 
314
                                        warnx(recargchar, optchar);
 
315
                                optopt = optchar;
 
316
                                return BADARG;
 
317
                        } else
 
318
                                optarg = nargv[optind];
 
319
                }
 
320
                place = EMSG;
 
321
                ++optind;
 
322
        }
 
323
        /* dump back option letter */
 
324
        return optchar;
 
325
}
 
326
 
 
327
#ifdef REPLACE_GETOPT
 
328
/*
 
329
 * getopt --
 
330
 *      Parse argc/argv argument vector.
 
331
 *
 
332
 * [eventually this will replace the real getopt]
 
333
 */
 
334
int
 
335
getopt(nargc, nargv, options)
 
336
        int nargc;
 
337
        char * const *nargv;
 
338
        const char *options;
 
339
{
 
340
        int retval;
 
341
 
 
342
        _DIAGASSERT(nargv != NULL);
 
343
        _DIAGASSERT(options != NULL);
 
344
 
 
345
        if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
 
346
                ++optind;
 
347
                /*
 
348
                 * We found an option (--), so if we skipped non-options,
 
349
                 * we have to permute.
 
350
                 */
 
351
                if (nonopt_end != -1) {
 
352
                        permute_args(nonopt_start, nonopt_end, optind,
 
353
                                       nargv);
 
354
                        optind -= nonopt_end - nonopt_start;
 
355
                }
 
356
                nonopt_start = nonopt_end = -1;
 
357
                retval = -1;
 
358
        }
 
359
        return retval;
 
360
}
 
361
#endif
 
362
 
 
363
/*
 
364
 * getopt_long --
 
365
 *      Parse argc/argv argument vector.
 
366
 */
 
367
int
 
368
getopt_long(nargc, nargv, options, long_options, idx)
 
369
        int nargc;
 
370
        char * const *nargv;
 
371
        const char *options;
 
372
        const struct option *long_options;
 
373
        int *idx;
 
374
{
 
375
        int retval;
 
376
 
 
377
        _DIAGASSERT(nargv != NULL);
 
378
        _DIAGASSERT(options != NULL);
 
379
        _DIAGASSERT(long_options != NULL);
 
380
        /* idx may be NULL */
 
381
 
 
382
        if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
 
383
                char *current_argv, *has_equal;
 
384
                size_t current_argv_len;
 
385
                int i, match;
 
386
 
 
387
                current_argv = place;
 
388
                match = -1;
 
389
 
 
390
                optind++;
 
391
                place = EMSG;
 
392
 
 
393
                if (*current_argv == '\0') {            /* found "--" */
 
394
                        /*
 
395
                         * We found an option (--), so if we skipped
 
396
                         * non-options, we have to permute.
 
397
                         */
 
398
                        if (nonopt_end != -1) {
 
399
                                permute_args(nonopt_start, nonopt_end,
 
400
                                    optind, nargv);
 
401
                                optind -= nonopt_end - nonopt_start;
 
402
                        }
 
403
                        nonopt_start = nonopt_end = -1;
 
404
                        return -1;
 
405
                }
 
406
                if ((has_equal = strchr(current_argv, '=')) != NULL) {
 
407
                        /* argument found (--option=arg) */
 
408
                        current_argv_len = has_equal - current_argv;
 
409
                        has_equal++;
 
410
                } else
 
411
                        current_argv_len = strlen(current_argv);
 
412
 
 
413
                for (i = 0; long_options[i].name; i++) {
 
414
                        /* find matching long option */
 
415
                        if (strncmp(current_argv, long_options[i].name,
 
416
                            current_argv_len))
 
417
                                continue;
 
418
 
 
419
                        if (strlen(long_options[i].name) ==
 
420
                            (unsigned)current_argv_len) {
 
421
                                /* exact match */
 
422
                                match = i;
 
423
                                break;
 
424
                        }
 
425
                        if (match == -1)                /* partial match */
 
426
                                match = i;
 
427
                        else {
 
428
                                /* ambiguous abbreviation */
 
429
                                if (PRINT_ERROR)
 
430
                                        warnx(ambig, (int)current_argv_len,
 
431
                                             current_argv);
 
432
                                optopt = 0;
 
433
                                return BADCH;
 
434
                        }
 
435
                }
 
436
                if (match != -1) {                      /* option found */
 
437
                        if (long_options[match].has_arg == no_argument
 
438
                            && has_equal) {
 
439
                                if (PRINT_ERROR)
 
440
                                        warnx(noarg, (int)current_argv_len,
 
441
                                             current_argv);
 
442
                                /*
 
443
                                 * XXX: GNU sets optopt to val regardless of
 
444
                                 * flag
 
445
                                 */
 
446
                                if (long_options[match].flag == NULL)
 
447
                                        optopt = long_options[match].val;
 
448
                                else
 
449
                                        optopt = 0;
 
450
                                return BADARG;
 
451
                        }
 
452
                        if (long_options[match].has_arg == required_argument ||
 
453
                            long_options[match].has_arg == optional_argument) {
 
454
                                if (has_equal)
 
455
                                        optarg = has_equal;
 
456
                                else if (long_options[match].has_arg ==
 
457
                                    required_argument) {
 
458
                                        /*
 
459
                                         * optional argument doesn't use
 
460
                                         * next nargv
 
461
                                         */
 
462
                                        optarg = nargv[optind++];
 
463
                                }
 
464
                        }
 
465
                        if ((long_options[match].has_arg == required_argument)
 
466
                            && (optarg == NULL)) {
 
467
                                /*
 
468
                                 * Missing argument; leading ':'
 
469
                                 * indicates no error should be generated
 
470
                                 */
 
471
                                if (PRINT_ERROR)
 
472
                                        warnx(recargstring, current_argv);
 
473
                                /*
 
474
                                 * XXX: GNU sets optopt to val regardless
 
475
                                 * of flag
 
476
                                 */
 
477
                                if (long_options[match].flag == NULL)
 
478
                                        optopt = long_options[match].val;
 
479
                                else
 
480
                                        optopt = 0;
 
481
                                --optind;
 
482
                                return BADARG;
 
483
                        }
 
484
                } else {                        /* unknown option */
 
485
                        if (PRINT_ERROR)
 
486
                                warnx(illoptstring, current_argv);
 
487
                        optopt = 0;
 
488
                        return BADCH;
 
489
                }
 
490
                if (long_options[match].flag) {
 
491
                        *long_options[match].flag = long_options[match].val;
 
492
                        retval = 0;
 
493
                } else
 
494
                        retval = long_options[match].val;
 
495
                if (idx)
 
496
                        *idx = match;
 
497
        }
 
498
        return retval;
 
499
}