1 /* Copyright (c) Michael Anthony 2002 <mike at unlikely org>
2 * Permission is hereby granted, free of charge, to any person obtaining
3 * a copy of this software and associated documentation files (the
4 * "Software"), to deal in the Software without restriction, including
5 * without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to
7 * permit persons to whom the Software is furnished to do so, subject to
8 * the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /* Modified by Stephen Bach for Viewglob's purposes */
23
24 #include "common.h"
25 #include "getopt.h"
26
27 #include <string.h>
28 #include <stdio.h>
29
30 char *optarg = 0;
31 int optind = -1;
32 int opterr = 1;
33 int optopt = 0;
34 static int newinv = 1;
35
fgetopt_long(int argc,char * const argv[],const char * optstring,const struct option * longopts,int * longindex)36 int fgetopt_long (
37 int argc, char *const argv[],
38 const char *optstring,
39 const struct option *longopts,
40 int *longindex)
41 {
42 static int nextchar;
43
44 // New invocation.
45 if (newinv == 1)
46 {
47 optind = 0;
48 nextchar = 0;
49 newinv = 0;
50 }
51
52 // No arguments left.
53 if (optind + 1 == argc)
54 {
55 ++optind;
56 newinv = 1;
57 return -1;
58 }
59
60 // New argument.
61 if (nextchar == 0)
62 {
63 ++optind;
64
65 // Long option or end-of-options marker.
66 if (argv[optind][0] == '-'
67 && argv[optind][1] == '-')
68 {
69 if (argv[optind][2] == '\0')
70 {
71 ++optind;
72 return -1;
73 }
74 else
75 {
76 const char *begin = argv[optind] + 2;
77 const char *end;
78 const struct option *op;
79
80 end = strchr (begin, '=');
81 if (end == 0)
82 {
83 end = begin + strlen (begin);
84 }
85
86 for (op = longopts; op->name; ++op)
87 {
88 if (end - begin == strlen (op->name)
89 && strncmp (begin, op->name, end - begin) == 0)
90 {
91 int ret = 0;
92 if (! op->has_arg && *end == '=')
93 {
94 g_warning("%s: option `--%s' doesn't"
95 " allow an argument",
96 argv[0], op->name);
97 ret = '?';
98 }
99 else
100 {
101 if (op->has_arg)
102 {
103 optarg = 0;
104 if (*end == '=')
105 {
106 optarg = (char*) end + 1;
107 }
108 else if (optind + 1 >= argc
109 && op->has_arg == required_argument)
110 {
111 g_warning("%s: option `--%s' requires"
112 " an argument",
113 argv[0], op->name);
114 return '?';
115 }
116 else if (optind + 1 >= argc)
117 {
118 // do nothing.
119 }
120 else if (argv[optind + 1][0] != '-'
121 || op->has_arg == required_argument)
122 {
123 optarg = argv[optind + 1];
124 ++optind;
125 }
126 }
127 if (longindex)
128 {
129 *longindex = op - longopts;
130 }
131 if (op->flag)
132 {
133 *(((struct option*)op)->flag) = op->val;
134 }
135 else
136 {
137 ret = op->val;
138 }
139 }
140 return ret;
141 }
142 }
143 g_warning("%s: unrecognized option `--%s'",
144 argv[0], begin);
145 return '?';
146 }
147 }
148 // Short option.
149 else if (argv[optind][0] == '-' && argv[optind][1] != '\0')
150 {
151 nextchar = 1;
152 }
153 // Non-option.
154 else
155 {
156 newinv = 1;
157 return -1;
158 }
159 }
160
161 // Second+ character of a short option. Note that this cannot be an
162 // else block because nextchar is updated in the block above.
163 if (nextchar)
164 {
165 const char *op = strchr (optstring, argv[optind][nextchar]);
166 ++nextchar;
167 if (op)
168 {
169 if (op[1] == ':')
170 {
171 if (argv[optind][nextchar] != '\0')
172 {
173 optarg = argv[optind] + nextchar;
174 nextchar = 0;
175 }
176 else if (optind + 1 >= argc)
177 {
178 g_warning("%s: option requires an argument -- %c",
179 argv[0], *op);
180 return '?';
181 }
182 else
183 {
184 optarg = argv[optind + 1];
185 ++optind;
186 nextchar = 0;
187 }
188 }
189 if (argv[optind][nextchar] == '\0')
190 {
191 nextchar = 0;
192 }
193 return *op;
194 }
195 else
196 {
197 g_warning("%s: invalid option -- %c",
198 argv[0], argv[optind][nextchar - 1]);
199 return '?';
200 }
201 }
202
203 /* FIXME? */
204 return -1;
205 }
206
207