| 1 : |
anton
|
1.1
|
/* Getopt for GNU. |
| 2 : |
|
|
Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc. |
| 3 : |
|
|
|
| 4 : |
|
|
This file is part of the GNU C Library. |
| 5 : |
|
|
|
| 6 : |
|
|
The GNU C Library is free software; you can redistribute it and/or |
| 7 : |
|
|
modify it under the terms of the GNU Library General Public License as |
| 8 : |
|
|
published by the Free Software Foundation; either version 2 of the |
| 9 : |
|
|
License, or (at your option) any later version. |
| 10 : |
|
|
|
| 11 : |
|
|
The GNU C Library is distributed in the hope that it will be useful, |
| 12 : |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 : |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 : |
|
|
Library General Public License for more details. |
| 15 : |
|
|
|
| 16 : |
|
|
You should have received a copy of the GNU Library General Public |
| 17 : |
|
|
License along with the GNU C Library; see the file COPYING.LIB. If |
| 18 : |
|
|
not, write to the Free Software Foundation, Inc., 675 Mass Ave, |
| 19 : |
|
|
Cambridge, MA 02139, USA. */ |
| 20 : |
|
|
|
| 21 : |
|
|
#include "getopt.h" |
| 22 : |
|
|
|
| 23 : |
|
|
#ifndef __STDC__ |
| 24 : |
|
|
#define const |
| 25 : |
|
|
#endif |
| 26 : |
|
|
|
| 27 : |
|
|
#if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) || defined (LIBC) |
| 28 : |
|
|
#include <stdlib.h> |
| 29 : |
|
|
#else /* STDC_HEADERS or __GNU_LIBRARY__ */ |
| 30 : |
|
|
char *getenv (); |
| 31 : |
|
|
#endif /* STDC_HEADERS or __GNU_LIBRARY__ */ |
| 32 : |
|
|
|
| 33 : |
|
|
#ifndef NULL |
| 34 : |
|
|
#define NULL 0 |
| 35 : |
|
|
#endif |
| 36 : |
|
|
|
| 37 : |
|
|
int |
| 38 : |
|
|
getopt_long (argc, argv, options, long_options, opt_index) |
| 39 : |
|
|
int argc; |
| 40 : |
|
|
char *const *argv; |
| 41 : |
|
|
const char *options; |
| 42 : |
|
|
const struct option *long_options; |
| 43 : |
|
|
int *opt_index; |
| 44 : |
|
|
{ |
| 45 : |
|
|
return _getopt_internal (argc, argv, options, long_options, opt_index, 0); |
| 46 : |
|
|
} |
| 47 : |
|
|
|
| 48 : |
|
|
/* Like getopt_long, but '-' as well as '--' can indicate a long option. |
| 49 : |
|
|
If an option that starts with '-' (not '--') doesn't match a long option, |
| 50 : |
|
|
but does match a short option, it is parsed as a short option |
| 51 : |
|
|
instead. */ |
| 52 : |
|
|
|
| 53 : |
|
|
int |
| 54 : |
|
|
getopt_long_only (argc, argv, options, long_options, opt_index) |
| 55 : |
|
|
int argc; |
| 56 : |
|
|
char *const *argv; |
| 57 : |
|
|
const char *options; |
| 58 : |
|
|
const struct option *long_options; |
| 59 : |
|
|
int *opt_index; |
| 60 : |
|
|
{ |
| 61 : |
|
|
return _getopt_internal (argc, argv, options, long_options, opt_index, 1); |
| 62 : |
|
|
} |
| 63 : |
|
|
|
| 64 : |
|
|
#ifdef TEST |
| 65 : |
|
|
|
| 66 : |
|
|
#include <stdio.h> |
| 67 : |
|
|
|
| 68 : |
|
|
int |
| 69 : |
|
|
main (argc, argv) |
| 70 : |
|
|
int argc; |
| 71 : |
|
|
char **argv; |
| 72 : |
|
|
{ |
| 73 : |
|
|
int c; |
| 74 : |
|
|
int digit_optind = 0; |
| 75 : |
|
|
|
| 76 : |
|
|
while (1) |
| 77 : |
|
|
{ |
| 78 : |
|
|
int this_option_optind = optind ? optind : 1; |
| 79 : |
|
|
int option_index = 0; |
| 80 : |
|
|
static struct option long_options[] = |
| 81 : |
|
|
{ |
| 82 : |
|
|
{"add", 1, 0, 0}, |
| 83 : |
|
|
{"append", 0, 0, 0}, |
| 84 : |
|
|
{"delete", 1, 0, 0}, |
| 85 : |
|
|
{"verbose", 0, 0, 0}, |
| 86 : |
|
|
{"create", 0, 0, 0}, |
| 87 : |
|
|
{"file", 1, 0, 0}, |
| 88 : |
|
|
{0, 0, 0, 0} |
| 89 : |
|
|
}; |
| 90 : |
|
|
|
| 91 : |
|
|
c = getopt_long (argc, argv, "abc:d:0123456789", |
| 92 : |
|
|
long_options, &option_index); |
| 93 : |
|
|
if (c == EOF) |
| 94 : |
|
|
break; |
| 95 : |
|
|
|
| 96 : |
|
|
switch (c) |
| 97 : |
|
|
{ |
| 98 : |
|
|
case 0: |
| 99 : |
|
|
printf ("option %s", long_options[option_index].name); |
| 100 : |
|
|
if (optarg) |
| 101 : |
|
|
printf (" with arg %s", optarg); |
| 102 : |
|
|
printf ("\n"); |
| 103 : |
|
|
break; |
| 104 : |
|
|
|
| 105 : |
|
|
case '0': |
| 106 : |
|
|
case '1': |
| 107 : |
|
|
case '2': |
| 108 : |
|
|
case '3': |
| 109 : |
|
|
case '4': |
| 110 : |
|
|
case '5': |
| 111 : |
|
|
case '6': |
| 112 : |
|
|
case '7': |
| 113 : |
|
|
case '8': |
| 114 : |
|
|
case '9': |
| 115 : |
|
|
if (digit_optind != 0 && digit_optind != this_option_optind) |
| 116 : |
|
|
printf ("digits occur in two different argv-elements.\n"); |
| 117 : |
|
|
digit_optind = this_option_optind; |
| 118 : |
|
|
printf ("option %c\n", c); |
| 119 : |
|
|
break; |
| 120 : |
|
|
|
| 121 : |
|
|
case 'a': |
| 122 : |
|
|
printf ("option a\n"); |
| 123 : |
|
|
break; |
| 124 : |
|
|
|
| 125 : |
|
|
case 'b': |
| 126 : |
|
|
printf ("option b\n"); |
| 127 : |
|
|
break; |
| 128 : |
|
|
|
| 129 : |
|
|
case 'c': |
| 130 : |
|
|
printf ("option c with value `%s'\n", optarg); |
| 131 : |
|
|
break; |
| 132 : |
|
|
|
| 133 : |
|
|
case 'd': |
| 134 : |
|
|
printf ("option d with value `%s'\n", optarg); |
| 135 : |
|
|
break; |
| 136 : |
|
|
|
| 137 : |
|
|
case '?': |
| 138 : |
|
|
break; |
| 139 : |
|
|
|
| 140 : |
|
|
default: |
| 141 : |
|
|
printf ("?? getopt returned character code 0%o ??\n", c); |
| 142 : |
|
|
} |
| 143 : |
|
|
} |
| 144 : |
|
|
|
| 145 : |
|
|
if (optind < argc) |
| 146 : |
|
|
{ |
| 147 : |
|
|
printf ("non-option ARGV-elements: "); |
| 148 : |
|
|
while (optind < argc) |
| 149 : |
|
|
printf ("%s ", argv[optind++]); |
| 150 : |
|
|
printf ("\n"); |
| 151 : |
|
|
} |
| 152 : |
|
|
|
| 153 : |
|
|
exit (0); |
| 154 : |
|
|
} |
| 155 : |
|
|
|
| 156 : |
|
|
#endif /* TEST */ |