-
Notifications
You must be signed in to change notification settings - Fork 156
/
gethopt.c
347 lines (289 loc) · 7.08 KB
/
gethopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* gehopt; options processing with both single-character and whole-word
* options both introduced with -
*/
#include <stdio.h>
#include <string.h>
#include "gethopt.h"
void
hoptset(struct h_context *ctx, int argc, char **argv)
{
memset(ctx, 0, sizeof *ctx);
ctx->argc = argc;
ctx->argv = argv;
ctx->optind = 1;
}
char *
hoptarg(struct h_context *ctx)
{
return ctx->optarg;
}
int
hoptind(struct h_context *ctx)
{
return ctx->optind;
}
char
hoptopt(struct h_context *ctx)
{
return ctx->optopt;
}
int
hopterr(struct h_context *ctx, int val)
{
int old = ctx->opterr;
ctx->opterr = !!val;
return old;
}
struct h_opt *
gethopt(struct h_context *ctx, struct h_opt *opts, int nropts)
{
int i;
int dashes;
if ( (ctx == 0) || ctx->optend || (ctx->optind >= ctx->argc) )
return 0;
ctx->optarg = 0;
ctx->optopt = 0;
if ( ctx->optchar == 0) {
/* check for leading -
*/
if ( ctx->argv[ctx->optind][0] != '-' ) {
/* out of arguments */
ctx->optend = 1;
return 0;
}
if ( ctx->argv[ctx->optind][1] == 0
|| strcmp(ctx->argv[ctx->optind], "--") == 0 ) {
/* option list finishes with - or -- token
*/
ctx->optend = 1;
ctx->optind++;
return 0;
}
dashes = 1;
if ( ctx->argv[ctx->optind][dashes] == '-' ) {
/* support GNU-style long option double-dash prefix
* (if gethopt is passed an unknown option with a double-dash
* prefix, it won't match a word and then the second dash
* will be scanned as if it was a regular old single-character
* option.)
*/
dashes = 2;
}
for ( i=0; i < nropts; i++ ) {
if ( ! opts[i].optword )
continue;
if (strcmp(opts[i].optword, dashes+(ctx->argv[ctx->optind]) ) == 0 ) {
if ( opts[i].opthasarg ) {
if ( ctx->argc > ctx->optind ) {
ctx->optarg = ctx->argv[ctx->optind+1];
ctx->optind += 2;
}
else {
/* word argument with required arg at end of
*command line
*/
if ( ctx->opterr )
fprintf(stderr,
"%s: option requires an argument -- %s\n",
ctx->argv[0], opts[i].optword);
ctx->optind ++;
return HOPTERR;
}
}
else {
ctx->optind ++;
}
return &opts[i];
}
}
ctx->optchar = 1;
}
ctx->optopt = ctx->argv[ctx->optind][ctx->optchar++];
if ( !ctx->optopt ) {
/* fell off the end of this argument */
ctx->optind ++;
ctx->optchar = 0;
return gethopt(ctx, opts, nropts);
}
for ( i=0; i<nropts; i++ ) {
if ( opts[i].optchar == ctx->optopt ) {
/* found a single-char option!
*/
if ( opts[i].opthasarg ) {
if ( ctx->argv[ctx->optind][ctx->optchar] ) {
/* argument immediately follows this options (-Oc)
*/
ctx->optarg = &ctx->argv[ctx->optind][ctx->optchar];
ctx->optind ++;
ctx->optchar = 0;
}
else if ( ctx->optind < ctx->argc-1 ) {
/* argument is next arg (-O c)
*/
ctx->optarg = &ctx->argv[ctx->optind+1][0];
ctx->optind += 2;
ctx->optchar = 0;
}
else {
/* end of arg string (-O); set optarg to null, return
* (should it opterr on me?)
*/
ctx->optarg = 0;
ctx->optind ++;
ctx->optchar = 0;
if ( ctx->opterr )
fprintf(stderr,
"%s: option requires an argument -- %c\n",
ctx->argv[0], opts[i].optchar);
return HOPTERR;
}
}
else {
if ( !ctx->argv[ctx->optind][ctx->optchar] ) {
ctx->optind ++;
ctx->optchar = 0;
}
}
return &opts[i];
}
}
if ( ctx->opterr )
fprintf(stderr, "%s: illegal option -- %c\n", ctx->argv[0], ctx->optopt);
return HOPTERR;
}
void
hoptdescribe(char *pgm, struct h_opt opts[], int nropts, char *arguments, int verbose)
{
int i;
int sz;
if ( verbose ) {
fprintf(stderr, "usage: %s", pgm);
if ( nropts > 0 )
fprintf(stderr, " [options]");
if ( arguments )
fprintf(stderr, " %s", arguments);
fputc('\n', stderr);
}
else
fprintf(stderr, "usage: %s", pgm);
/* print out the options that don't have flags first */
if ( verbose ) {
int maxoptwidth = 0;
int maxargwidth = 0;
int hasoptchar = 0;
int j;
if ( nropts > 0 )
fprintf(stderr, "options:\n");
/* find column widths */
for ( i=0; i < nropts; i++ ) {
if ( opts[i].optword && (( sz = strlen(opts[i].optword) ) > maxoptwidth ) )
maxoptwidth = sz;
if ( opts[i].opthasarg && (( sz = strlen(opts[i].opthasarg) ) > maxargwidth ) )
maxargwidth = sz;
if ( opts[i].optchar )
hasoptchar = 1;
}
for ( i=0; i < nropts; i++ ) {
if ( opts[i].optword ) {
fprintf(stderr, " -%s", opts[i].optword);
j = strlen(opts[i].optword);
}
else j = -2;
while ( j++ < maxoptwidth )
fputc(' ', stderr);
if ( opts[i].optchar )
fprintf(stderr, " -%c ", opts[i].optchar);
else if ( hasoptchar )
fprintf(stderr, " ");
if ( maxargwidth > 0 ) {
if ( opts[i].opthasarg ) {
fprintf(stderr, " [%s]", opts[i].opthasarg);
j = strlen(opts[i].opthasarg);
}
else {
fprintf(stderr, " ");
j = 0;
}
while ( j++ < maxargwidth )
fputc(' ', stderr);
}
if ( opts[i].optdesc )
fprintf(stderr, " %s", opts[i].optdesc);
fputc('\n', stderr);
}
}
else {
int optcount;
for ( optcount=i=0; i < nropts; i++ ) {
if ( opts[i].optchar && !opts[i].opthasarg) {
if (optcount == 0 )
fputs(" [-", stderr);
fputc(opts[i].optchar, stderr);
optcount++;
}
}
if ( optcount )
fputc(']', stderr);
/* print out the options WITH flags */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optchar && opts[i].opthasarg)
fprintf(stderr, " [-%c %s]", opts[i].optchar, opts[i].opthasarg);
/* print out the long options */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optword ) {
fprintf(stderr, " [-%s", opts[i].optword);
if ( opts[i].opthasarg )
fprintf(stderr, " %s", opts[i].opthasarg);
fputc(']', stderr);
}
if ( arguments )
fprintf(stderr, " %s", arguments);
}
/* and we're done */
fputc('\n', stderr);
}
void
hoptusage(char *pgm, struct h_opt opts[], int nropts, char *arguments)
{
hoptdescribe(pgm, opts, nropts, arguments, 0);
}
#if DEBUG
struct h_opt opts[] = {
{ 0, "css", 0, 1, "css file" },
{ 1, "header", 0, 1, "header file" },
{ 2, 0, 'a', 0, "option a (no arg)" },
{ 3, 0, 'b', 1, "option B (with arg)" },
{ 4, "help", '?', 0, "help message" },
} ;
#define NROPT (sizeof opts/sizeof opts[0])
int
main(int argc, char **argv)
{
struct h_opt *ret;
struct h_context ctx;
int i;
hoptset(&ctx, argc, argv);
hopterr(&ctx, 1);
while (( ret = gethopt(&ctx, opts, NROPT) )) {
if ( ret != HOPTERR ) {
if ( ret->optword )
printf("%s", ret->optword);
else
printf("%c", ret->optchar);
if ( ret->opthasarg ) {
if ( hoptarg(&ctx) )
printf(" with argument \"%s\"", hoptarg(&ctx));
else
printf(" with no argument?");
}
printf(" (%s)\n", ret->optdesc);
}
}
argc -= hoptind(&ctx);
argv += hoptind(&ctx);
for ( i=0; i < argc; i++ )
printf("%d: %s\n", i, argv[i]);
return 0;
}
#endif /*DEBUG*/