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