質問編集履歴
2
誤字
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
FreeBSD getopt_long getopt_ine
|
1
|
+
FreeBSD getopt_long getopt_internal
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
FreeBS
|
1
|
+
FreeBSDのソースコードをSubverisonで取ってきて読んでいるんですけど、lib/libc/stdlib/getopt_long.cにある、getopt_internal()の対になって終わる、最後の"}"の括弧がないのですが、どうゆう意味があるんでしょうか?詳しい方誰か教えてください。よろしくお願い申し上げます。
|
2
2
|
|
3
3
|
versionは12.1です。
|
4
4
|
|
1
ソースコードの追加。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,429 @@
|
|
1
1
|
FreeBSdのソースコードをSubverisonで取ってきて読んでいるんですけど、lib/libc/stdlib/getopt_long.cにある、getopt_internal()の対になって終わる、最後の"}"の括弧がないののですが、どうゆう意味があるんでしょうか?詳しい方誰か教えてください。よろしくお願い申し上げます。
|
2
|
+
|
3
|
+
versionは12.1です。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
```c
|
8
|
+
|
9
|
+
static int
|
10
|
+
|
11
|
+
getopt_internal(int nargc, char * const *nargv, const char *options,
|
12
|
+
|
13
|
+
const struct option *long_options, int *idx, int flags)
|
14
|
+
|
15
|
+
{
|
16
|
+
|
17
|
+
char *oli; /* option letter list index */
|
18
|
+
|
19
|
+
int optchar, short_too;
|
20
|
+
|
21
|
+
static int posixly_correct = -1;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
if (options == NULL)
|
26
|
+
|
27
|
+
return (-1);
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
/*
|
32
|
+
|
33
|
+
* XXX Some GNU programs (like cvs) set optind to 0 instead of
|
34
|
+
|
35
|
+
* XXX using optreset. Work around this braindamage.
|
36
|
+
|
37
|
+
*/
|
38
|
+
|
39
|
+
if (optind == 0)
|
40
|
+
|
41
|
+
optind = optreset = 1;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
/*
|
46
|
+
|
47
|
+
* Disable GNU extensions if POSIXLY_CORRECT is set or options
|
48
|
+
|
49
|
+
* string begins with a '+'.
|
50
|
+
|
51
|
+
*/
|
52
|
+
|
53
|
+
if (posixly_correct == -1 || optreset)
|
54
|
+
|
55
|
+
posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
|
56
|
+
|
57
|
+
if (*options == '-')
|
58
|
+
|
59
|
+
flags |= FLAG_ALLARGS;
|
60
|
+
|
61
|
+
else if (posixly_correct || *options == '+')
|
62
|
+
|
63
|
+
flags &= ~FLAG_PERMUTE;
|
64
|
+
|
65
|
+
if (*options == '+' || *options == '-')
|
66
|
+
|
67
|
+
options++;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
optarg = NULL;
|
72
|
+
|
73
|
+
if (optreset)
|
74
|
+
|
75
|
+
nonopt_start = nonopt_end = -1;
|
76
|
+
|
77
|
+
start:
|
78
|
+
|
79
|
+
if (optreset || !*place) { /* update scanning pointer */
|
80
|
+
|
81
|
+
optreset = 0;
|
82
|
+
|
83
|
+
if (optind >= nargc) { /* end of argument vector */
|
84
|
+
|
85
|
+
place = EMSG;
|
86
|
+
|
87
|
+
if (nonopt_end != -1) {
|
88
|
+
|
89
|
+
/* do permutation, if we have to */
|
90
|
+
|
91
|
+
permute_args(nonopt_start, nonopt_end,
|
92
|
+
|
93
|
+
optind, nargv);
|
94
|
+
|
95
|
+
optind -= nonopt_end - nonopt_start;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
else if (nonopt_start != -1) {
|
100
|
+
|
101
|
+
/*
|
102
|
+
|
103
|
+
* If we skipped non-options, set optind
|
104
|
+
|
105
|
+
* to the first of them.
|
106
|
+
|
107
|
+
*/
|
108
|
+
|
109
|
+
optind = nonopt_start;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
nonopt_start = nonopt_end = -1;
|
114
|
+
|
115
|
+
return (-1);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
if (*(place = nargv[optind]) != '-' ||
|
120
|
+
|
121
|
+
#ifdef GNU_COMPATIBLE
|
122
|
+
|
123
|
+
place[1] == '\0') {
|
124
|
+
|
125
|
+
#else
|
126
|
+
|
127
|
+
(place[1] == '\0' && strchr(options, '-') == NULL)) {
|
128
|
+
|
129
|
+
#endif
|
130
|
+
|
131
|
+
place = EMSG; /* found non-option */
|
132
|
+
|
133
|
+
if (flags & FLAG_ALLARGS) {
|
134
|
+
|
135
|
+
/*
|
136
|
+
|
137
|
+
* GNU extension:
|
138
|
+
|
139
|
+
* return non-option as argument to option 1
|
140
|
+
|
141
|
+
*/
|
142
|
+
|
143
|
+
optarg = nargv[optind++];
|
144
|
+
|
145
|
+
return (INORDER);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
if (!(flags & FLAG_PERMUTE)) {
|
150
|
+
|
151
|
+
/*
|
152
|
+
|
153
|
+
* If no permutation wanted, stop parsing
|
154
|
+
|
155
|
+
* at first non-option.
|
156
|
+
|
157
|
+
*/
|
158
|
+
|
159
|
+
return (-1);
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
/* do permutation */
|
164
|
+
|
165
|
+
if (nonopt_start == -1)
|
166
|
+
|
167
|
+
nonopt_start = optind;
|
168
|
+
|
169
|
+
else if (nonopt_end != -1) {
|
170
|
+
|
171
|
+
permute_args(nonopt_start, nonopt_end,
|
172
|
+
|
173
|
+
optind, nargv);
|
174
|
+
|
175
|
+
nonopt_start = optind -
|
176
|
+
|
177
|
+
(nonopt_end - nonopt_start);
|
178
|
+
|
179
|
+
nonopt_end = -1;
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
optind++;
|
184
|
+
|
185
|
+
/* process next argument */
|
186
|
+
|
187
|
+
goto start;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
if (nonopt_start != -1 && nonopt_end == -1)
|
192
|
+
|
193
|
+
nonopt_end = optind;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
/*
|
198
|
+
|
199
|
+
* If we have "-" do nothing, if "--" we are done.
|
200
|
+
|
201
|
+
*/
|
202
|
+
|
203
|
+
if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
|
204
|
+
|
205
|
+
optind++;
|
206
|
+
|
207
|
+
place = EMSG;
|
208
|
+
|
209
|
+
/*
|
210
|
+
|
211
|
+
* We found an option (--), so if we skipped
|
212
|
+
|
213
|
+
* non-options, we have to permute.
|
214
|
+
|
215
|
+
*/
|
216
|
+
|
217
|
+
if (nonopt_end != -1) {
|
218
|
+
|
219
|
+
permute_args(nonopt_start, nonopt_end,
|
220
|
+
|
221
|
+
optind, nargv);
|
222
|
+
|
223
|
+
optind -= nonopt_end - nonopt_start;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
nonopt_start = nonopt_end = -1;
|
228
|
+
|
229
|
+
return (-1);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/*
|
238
|
+
|
239
|
+
* Check long options if:
|
240
|
+
|
241
|
+
* 1) we were passed some
|
242
|
+
|
243
|
+
* 2) the arg is not just "-"
|
244
|
+
|
245
|
+
* 3) either the arg starts with -- we are getopt_long_only()
|
246
|
+
|
247
|
+
*/
|
248
|
+
|
249
|
+
if (long_options != NULL && place != nargv[optind] &&
|
250
|
+
|
251
|
+
(*place == '-' || (flags & FLAG_LONGONLY))) {
|
252
|
+
|
253
|
+
short_too = 0;
|
254
|
+
|
255
|
+
#ifdef GNU_COMPATIBLE
|
256
|
+
|
257
|
+
dash_prefix = D_PREFIX;
|
258
|
+
|
259
|
+
#endif
|
260
|
+
|
261
|
+
if (*place == '-') {
|
262
|
+
|
263
|
+
place++; /* --foo long option */
|
264
|
+
|
265
|
+
if (*place == '\0')
|
266
|
+
|
267
|
+
return (BADARG); /* malformed option */
|
268
|
+
|
269
|
+
#ifdef GNU_COMPATIBLE
|
270
|
+
|
271
|
+
dash_prefix = DD_PREFIX;
|
272
|
+
|
273
|
+
#endif
|
274
|
+
|
275
|
+
} else if (*place != ':' && strchr(options, *place) != NULL)
|
276
|
+
|
277
|
+
short_too = 1; /* could be short option too */
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
optchar = parse_long_options(nargv, options, long_options,
|
282
|
+
|
283
|
+
idx, short_too, flags);
|
284
|
+
|
285
|
+
if (optchar != -1) {
|
286
|
+
|
287
|
+
place = EMSG;
|
288
|
+
|
289
|
+
return (optchar);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
if ((optchar = (int)*place++) == (int)':' ||
|
298
|
+
|
299
|
+
(optchar == (int)'-' && *place != '\0') ||
|
300
|
+
|
301
|
+
(oli = strchr(options, optchar)) == NULL) {
|
302
|
+
|
303
|
+
/*
|
304
|
+
|
305
|
+
* If the user specified "-" and '-' isn't listed in
|
306
|
+
|
307
|
+
* options, return -1 (non-option) as per POSIX.
|
308
|
+
|
309
|
+
* Otherwise, it is an unknown option character (or ':').
|
310
|
+
|
311
|
+
*/
|
312
|
+
|
313
|
+
if (optchar == (int)'-' && *place == '\0')
|
314
|
+
|
315
|
+
return (-1);
|
316
|
+
|
317
|
+
if (!*place)
|
318
|
+
|
319
|
+
++optind;
|
320
|
+
|
321
|
+
#ifdef GNU_COMPATIBLE
|
322
|
+
|
323
|
+
if (PRINT_ERROR)
|
324
|
+
|
325
|
+
warnx(posixly_correct ? illoptchar : gnuoptchar,
|
326
|
+
|
327
|
+
optchar);
|
328
|
+
|
329
|
+
#else
|
330
|
+
|
331
|
+
if (PRINT_ERROR)
|
332
|
+
|
333
|
+
warnx(illoptchar, optchar);
|
334
|
+
|
335
|
+
#endif
|
336
|
+
|
337
|
+
optopt = optchar;
|
338
|
+
|
339
|
+
return (BADCH);
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
|
344
|
+
|
345
|
+
/* -W long-option */
|
346
|
+
|
347
|
+
if (*place) /* no space */
|
348
|
+
|
349
|
+
/* NOTHING */;
|
350
|
+
|
351
|
+
else if (++optind >= nargc) { /* no arg */
|
352
|
+
|
353
|
+
place = EMSG;
|
354
|
+
|
355
|
+
if (PRINT_ERROR)
|
356
|
+
|
357
|
+
warnx(recargchar, optchar);
|
358
|
+
|
359
|
+
optopt = optchar;
|
360
|
+
|
361
|
+
return (BADARG);
|
362
|
+
|
363
|
+
} else /* white space */
|
364
|
+
|
365
|
+
place = nargv[optind];
|
366
|
+
|
367
|
+
#ifdef GNU_COMPATIBLE
|
368
|
+
|
369
|
+
dash_prefix = W_PREFIX;
|
370
|
+
|
371
|
+
#endif
|
372
|
+
|
373
|
+
optchar = parse_long_options(nargv, options, long_options,
|
374
|
+
|
375
|
+
idx, 0, flags);
|
376
|
+
|
377
|
+
place = EMSG;
|
378
|
+
|
379
|
+
return (optchar);
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
if (*++oli != ':') { /* doesn't take argument */
|
384
|
+
|
385
|
+
if (!*place)
|
386
|
+
|
387
|
+
++optind;
|
388
|
+
|
389
|
+
} else { /* takes (optional) argument */
|
390
|
+
|
391
|
+
optarg = NULL;
|
392
|
+
|
393
|
+
if (*place) /* no white space */
|
394
|
+
|
395
|
+
optarg = place;
|
396
|
+
|
397
|
+
else if (oli[1] != ':') { /* arg not optional */
|
398
|
+
|
399
|
+
if (++optind >= nargc) { /* no arg */
|
400
|
+
|
401
|
+
place = EMSG;
|
402
|
+
|
403
|
+
if (PRINT_ERROR)
|
404
|
+
|
405
|
+
warnx(recargchar, optchar);
|
406
|
+
|
407
|
+
optopt = optchar;
|
408
|
+
|
409
|
+
return (BADARG);
|
410
|
+
|
411
|
+
} else
|
412
|
+
|
413
|
+
optarg = nargv[optind];
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
place = EMSG;
|
418
|
+
|
419
|
+
++optind;
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
/* dump back option letter */
|
424
|
+
|
425
|
+
return (optchar);
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
```
|