Sat, 19 Jan 2002 19:31:04 +0100
trunk: changeset 39
- The tokenizer also supports reading from a buffer now.
- 2002 update
0 | 1 | /* |
2 | * libtu/optparser.c | |
3 | * | |
36 | 4 | * Copyright (c) Tuomo Valkonen 1999-2002. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <string.h> | |
9 | #include <stdlib.h> | |
10 | ||
5 | 11 | #include <libtu/util.h> |
12 | #include <libtu/misc.h> | |
13 | #include <libtu/optparser.h> | |
14 | #include <libtu/output.h> | |
0 | 15 | |
16 | ||
17 | #define O_ARGS(o) (o->flags&OPT_OPT_ARG) | |
18 | #define O_ARG(o) (o->flasg&OPT_ARG) | |
19 | #define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG) | |
20 | #define O_ID(o) (o->optid) | |
21 | ||
12 | 22 | #define OPT_ID_HELP OPT_ID_RESERVED('h') |
23 | #define OPT_ID_VERSION OPT_ID_RESERVED('V') | |
24 | #define OPT_ID_ABOUT OPT_ID_RESERVED('a'|OPT_ID_NOSHORT_FLAG) | |
25 | ||
26 | ||
27 | static OptParserOpt common_opts[]={ | |
28 | {OPT_ID_HELP, "help", 0, NULL, DUMMY_TR("Show this help")}, | |
29 | {OPT_ID_VERSION, "version", 0, NULL, DUMMY_TR("Show program version")}, | |
30 | {OPT_ID_ABOUT, "about", 0, NULL, DUMMY_TR("Show about text")}, | |
31 | {0, NULL, 0, NULL, NULL} | |
32 | }; | |
33 | ||
34 | ||
35 | static OptParserCommonInfo dummy_cinfo[]={ | |
36 | NULL, /* version */ | |
37 | "Usage: $p\n", /* usage_tmpl */ | |
38 | NULL /* about */ | |
39 | }; | |
40 | ||
0 | 41 | |
42 | static const OptParserOpt *o_opts=NULL; | |
43 | static char *const *o_current=NULL; | |
44 | static int o_left=0; | |
45 | static const char* o_chain_ptr=NULL; | |
46 | static int o_args_left=0; | |
47 | static const char*o_tmp=NULL; | |
48 | static int o_error=0; | |
3 | 49 | static int o_mode=OPTP_CHAIN; |
12 | 50 | static const OptParserCommonInfo *o_cinfo=NULL; |
51 | ||
52 | ||
53 | /* */ | |
54 | ||
55 | ||
56 | static void print_help(const OptParserOpt *opts, bool midlong, | |
57 | const OptParserCommonInfo *cinfo); | |
0 | 58 | |
59 | ||
60 | /* */ | |
61 | ||
62 | ||
3 | 63 | void optparser_init(int argc, char *const argv[], int mode, |
12 | 64 | const OptParserOpt *opts, const OptParserCommonInfo *cinfo) |
0 | 65 | { |
3 | 66 | o_mode=mode; |
12 | 67 | o_opts=(opts==NULL ? common_opts : opts); |
0 | 68 | o_current=argv+1; |
69 | o_left=argc-1; | |
70 | o_chain_ptr=NULL; | |
71 | o_args_left=0; | |
72 | o_tmp=NULL; | |
12 | 73 | o_cinfo=(cinfo==NULL ? dummy_cinfo : cinfo); |
0 | 74 | } |
75 | ||
76 | ||
77 | /* */ | |
78 | ||
79 | ||
80 | static const OptParserOpt *find_chain_opt(char p, const OptParserOpt *o) | |
81 | { | |
82 | for(;O_ID(o);o++){ | |
12 | 83 | if((O_ID(o)&~OPT_ID_RESERVED_FLAG)==p) |
0 | 84 | return o; |
85 | } | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | ||
90 | static bool is_option(const char *p) | |
91 | { | |
92 | if(p==NULL) | |
93 | return FALSE; | |
94 | if(*p++!='-') | |
95 | return FALSE; | |
96 | if(*p++!='-') | |
97 | return FALSE; | |
98 | if(*p=='\0') | |
99 | return FALSE; | |
100 | return TRUE; | |
101 | } | |
102 | ||
103 | ||
104 | /* */ | |
105 | ||
3 | 106 | enum{ |
107 | SHORT, MIDLONG, LONG | |
108 | }; | |
109 | ||
0 | 110 | |
12 | 111 | static int optparser_do_get_opt() |
0 | 112 | { |
113 | #define RET(X) return o_tmp=p, o_error=X | |
114 | const char *p, *p2=NULL; | |
3 | 115 | bool dash=TRUE; |
116 | int type=SHORT; | |
0 | 117 | const OptParserOpt *o; |
118 | int l; | |
119 | ||
120 | while(o_args_left) | |
121 | optparser_get_arg(); | |
122 | ||
123 | o_tmp=NULL; | |
124 | ||
3 | 125 | /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */ |
0 | 126 | if(o_chain_ptr!=NULL){ |
127 | p=o_chain_ptr++; | |
128 | if(!*o_chain_ptr) | |
129 | o_chain_ptr=NULL; | |
130 | ||
131 | o=find_chain_opt(*p, o_opts); | |
132 | ||
133 | if(o==NULL) | |
134 | RET(E_OPT_INVALID_CHAIN_OPTION); | |
135 | ||
136 | goto do_arg; | |
137 | } | |
138 | ||
139 | if(o_left<1) | |
140 | return OPT_ID_END; | |
141 | ||
142 | o_left--; | |
143 | p=*o_current++; | |
144 | ||
145 | if(*p!='-'){ | |
146 | dash=FALSE; | |
3 | 147 | if(o_mode!=OPTP_NO_DASH) |
148 | RET(OPT_ID_ARGUMENT); | |
19 | 149 | p2=p; |
0 | 150 | }else if(*(p+1)=='-'){ |
151 | /* --foo */ | |
152 | if(*(p+2)=='\0'){ | |
3 | 153 | /* -- arguments */ |
0 | 154 | o_args_left=o_left; |
155 | RET(OPT_ID_ARGUMENT); | |
156 | } | |
3 | 157 | type=LONG; |
0 | 158 | p2=p+2; |
159 | }else{ | |
160 | /* -foo */ | |
161 | if(*(p+1)=='\0'){ | |
162 | /* - */ | |
163 | o_args_left=1; | |
164 | RET(OPT_ID_ARGUMENT); | |
165 | } | |
3 | 166 | if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG) |
167 | type=MIDLONG; | |
168 | ||
0 | 169 | p2=p+1; |
170 | } | |
171 | ||
172 | o=o_opts; | |
173 | ||
174 | again: | |
12 | 175 | for(; O_ID(o); o++){ |
3 | 176 | if(type==LONG){ |
0 | 177 | /* Do long option (--foo=bar) */ |
178 | if(o->longopt==NULL) | |
179 | continue; | |
180 | l=strlen(o->longopt); | |
181 | if(strncmp(p2, o->longopt, l)!=0) | |
182 | continue; | |
183 | ||
184 | if(p2[l]=='\0'){ | |
185 | if(O_ARGS(o)==OPT_ARG) | |
186 | RET(E_OPT_MISSING_ARGUMENT); | |
187 | return O_ID(o); | |
188 | }else if(p2[l]=='='){ | |
189 | if(!O_ARGS(o)) | |
190 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
191 | if(p2[l+1]=='\0') | |
192 | RET(E_OPT_MISSING_ARGUMENT); | |
193 | o_tmp=p2+l+1; | |
194 | o_args_left=1; | |
195 | return O_ID(o); | |
196 | } | |
3 | 197 | continue; |
198 | }else if(type==MIDLONG){ | |
199 | if(o->longopt==NULL) | |
200 | continue; | |
201 | ||
202 | if(strcmp(p2, o->longopt)!=0) | |
203 | continue; | |
204 | }else{ /* type==SHORT */ | |
12 | 205 | if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG)) |
0 | 206 | continue; |
207 | ||
208 | if(*(p2+1)!='\0'){ | |
3 | 209 | if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){ |
210 | /*valid_chain(p2+1, o_opts)*/ | |
211 | o_chain_ptr=p2+1; | |
212 | p++; | |
213 | }else if(o_mode==OPTP_IMMEDIATE){ | |
214 | if(!O_ARGS(o)){ | |
215 | if(*(p2+1)!='\0') | |
216 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
217 | }else{ | |
218 | if(*(p2+1)=='\0') | |
219 | RET(E_OPT_MISSING_ARGUMENT); | |
220 | o_tmp=p2+1; | |
221 | o_args_left=1; | |
222 | } | |
223 | return O_ID(o); | |
0 | 224 | }else{ |
3 | 225 | RET(E_OPT_SYNTAX_ERROR); |
0 | 226 | } |
227 | } | |
3 | 228 | } |
229 | ||
230 | do_arg: | |
231 | ||
232 | if(!O_ARGS(o)) | |
233 | return O_ID(o); | |
234 | ||
235 | if(!o_left || is_option(*o_current)){ | |
236 | if(O_ARGS(o)==OPT_OPT_ARG) | |
0 | 237 | return O_ID(o); |
3 | 238 | RET(E_OPT_MISSING_ARGUMENT); |
239 | } | |
0 | 240 | |
3 | 241 | o_args_left=1; |
242 | return O_ID(o); | |
0 | 243 | } |
12 | 244 | |
245 | if(o!=&(common_opts[3])){ | |
246 | o=common_opts; | |
247 | goto again; | |
248 | } | |
0 | 249 | |
250 | if(dash) | |
251 | RET(E_OPT_INVALID_OPTION); | |
252 | ||
253 | RET(OPT_ID_ARGUMENT); | |
254 | #undef RET | |
255 | } | |
256 | ||
257 | ||
12 | 258 | int optparser_get_opt() |
259 | { | |
260 | int oid=optparser_do_get_opt(); | |
261 | ||
262 | if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0) | |
263 | return oid; | |
264 | ||
265 | switch(oid){ | |
266 | case OPT_ID_ABOUT: | |
267 | if(o_cinfo->about!=NULL) | |
268 | printf("%s", o_cinfo->about); | |
269 | break; | |
270 | ||
271 | case OPT_ID_VERSION: | |
272 | if(o_cinfo->version!=NULL) | |
273 | printf("%s\n", o_cinfo->version); | |
274 | break; | |
275 | ||
276 | case OPT_ID_HELP: | |
277 | print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo); | |
278 | break; | |
279 | } | |
280 | ||
281 | exit(EXIT_SUCCESS); | |
282 | } | |
283 | ||
284 | ||
0 | 285 | /* */ |
286 | ||
287 | ||
288 | const char* optparser_get_arg() | |
289 | { | |
290 | const char *p; | |
291 | ||
292 | if(o_tmp!=NULL){ | |
293 | /* If o_args_left==0, then were returning an invalid option | |
294 | * otherwise an immediate argument (e.g. -funsigned-char | |
295 | * where '-f' is the option and 'unsigned-char' the argument) | |
296 | */ | |
297 | if(o_args_left>0) | |
298 | o_args_left--; | |
299 | p=o_tmp; | |
300 | o_tmp=NULL; | |
301 | return p; | |
302 | } | |
303 | ||
304 | if(o_args_left<1 || o_left<1) | |
305 | return NULL; | |
306 | ||
307 | o_left--; | |
308 | o_args_left--; | |
309 | return *o_current++; | |
310 | } | |
311 | ||
312 | ||
313 | /* */ | |
314 | ||
315 | static void warn_arg(const char *e) | |
316 | { | |
3 | 317 | const char *p=optparser_get_arg(); |
0 | 318 | |
319 | if(p==NULL) | |
320 | warn("%s (null)", e); | |
321 | else | |
322 | warn("%s \'%s\'", e, p); | |
323 | } | |
324 | ||
325 | ||
326 | static void warn_opt(const char *e) | |
327 | { | |
3 | 328 | if(o_tmp!=NULL && o_chain_ptr!=NULL) |
0 | 329 | warn("%s \'-%c\'", e, *o_tmp); |
330 | else | |
331 | warn_arg(e); | |
332 | } | |
333 | ||
334 | ||
335 | void optparser_print_error() | |
336 | { | |
337 | switch(o_error){ | |
338 | case E_OPT_INVALID_OPTION: | |
3 | 339 | case E_OPT_INVALID_CHAIN_OPTION: |
0 | 340 | warn_opt(TR("Invalid option")); |
341 | break; | |
342 | ||
343 | case E_OPT_SYNTAX_ERROR: | |
344 | warn_arg(TR("Syntax error while parsing")); | |
345 | break; | |
346 | ||
347 | case E_OPT_MISSING_ARGUMENT: | |
348 | warn_opt(TR("Missing argument to")); | |
349 | break; | |
350 | ||
351 | case E_OPT_UNEXPECTED_ARGUMENT: | |
3 | 352 | warn_opt(TR("No argument expected:")); |
0 | 353 | break; |
354 | ||
355 | case OPT_ID_ARGUMENT: | |
356 | warn(TR("Unexpected argument")); | |
357 | break; | |
358 | ||
359 | default: | |
360 | warn(TR("(unknown error)")); | |
361 | } | |
362 | ||
363 | o_tmp=NULL; | |
364 | o_error=0; | |
365 | } | |
12 | 366 | |
367 | ||
368 | /* */ | |
369 | ||
370 | ||
371 | static uint opt_w(const OptParserOpt *opt, bool midlong) | |
372 | { | |
373 | uint w=0; | |
374 | ||
375 | if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){ | |
376 | w+=2; /* "-o" */ | |
377 | if(opt->longopt!=NULL) | |
378 | w+=2; /* ", " */ | |
379 | } | |
380 | ||
381 | if(opt->longopt!=NULL) | |
382 | w+=strlen(opt->longopt)+(midlong ? 1 : 2); | |
383 | ||
384 | if(O_ARGS(opt)){ | |
385 | if(opt->argname==NULL) | |
386 | w+=4; | |
387 | else | |
388 | w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */ | |
389 | ||
390 | if(O_OPT_ARG(opt)) | |
391 | w+=2; /* [ARG] */ | |
392 | } | |
393 | ||
394 | return w; | |
395 | } | |
396 | ||
397 | ||
398 | #define TERM_W 80 | |
399 | #define OFF1 2 | |
400 | #define OFF2 2 | |
401 | #define SPACER1 " " | |
402 | #define SPACER2 " " | |
403 | ||
404 | static void print_opt(const OptParserOpt *opt, bool midlong, | |
405 | uint maxw, uint tw) | |
406 | { | |
407 | FILE *f=stdout; | |
408 | const char *p, *p2, *p3; | |
409 | uint w=0; | |
410 | ||
411 | fprintf(f, SPACER1); | |
412 | ||
413 | /* short opt */ | |
414 | ||
415 | if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){ | |
416 | fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG); | |
417 | w+=2; | |
418 | ||
419 | if(opt->longopt!=NULL){ | |
420 | fprintf(f, ", "); | |
421 | w+=2; | |
422 | } | |
423 | } | |
424 | ||
425 | /* long opt */ | |
426 | ||
427 | if(opt->longopt!=NULL){ | |
428 | if(midlong){ | |
429 | w++; | |
430 | fprintf(f, "-%s", opt->longopt); | |
431 | }else{ | |
432 | w+=2; | |
433 | fprintf(f, "--%s", opt->longopt); | |
434 | } | |
435 | w+=strlen(opt->longopt); | |
436 | } | |
437 | ||
438 | /* arg */ | |
439 | ||
440 | if(O_ARGS(opt)){ | |
441 | w++; | |
442 | if(opt->longopt!=NULL && !midlong) | |
443 | putc('=', f); | |
444 | else | |
445 | putc(' ', f); | |
446 | ||
447 | if(O_OPT_ARG(opt)){ | |
448 | w+=2; | |
449 | putc('[', f); | |
450 | } | |
451 | ||
452 | if(opt->argname!=NULL){ | |
453 | fprintf(f, "%s", opt->argname); | |
454 | w+=strlen(opt->argname); | |
455 | }else{ | |
456 | w+=3; | |
457 | fprintf(f, "ARG"); | |
458 | } | |
459 | ||
460 | if(O_OPT_ARG(opt)) | |
461 | putc(']', f); | |
462 | } | |
463 | ||
464 | while(w++<maxw) | |
465 | putc(' ', f); | |
466 | ||
467 | /* descr */ | |
468 | ||
469 | p=p2=opt->descr; | |
470 | ||
471 | if(p==NULL){ | |
472 | putc('\n', f); | |
473 | return; | |
474 | } | |
475 | ||
476 | fprintf(f, SPACER2); | |
477 | ||
478 | maxw+=OFF1+OFF2; | |
479 | tw-=maxw; | |
480 | ||
481 | while(strlen(p)>tw){ | |
482 | p3=p2=p+tw-2; | |
483 | ||
484 | while(*p2!=' ' && p2!=p) | |
485 | p2--; | |
486 | ||
487 | while(*p3!=' ' && *p3!='\0') | |
488 | p3++; | |
489 | ||
490 | if((uint)(p3-p2)>tw){ | |
491 | /* long word - just wrap */ | |
492 | p2=p+tw-2; | |
493 | } | |
494 | ||
495 | writef(f, p, p2-p); | |
496 | if(*p2==' ') | |
497 | putc('\n', f); | |
498 | else | |
499 | fprintf(f, "\\\n"); | |
500 | ||
501 | p=p2+1; | |
502 | ||
503 | w=maxw; | |
504 | while(w--) | |
505 | putc(' ', f); | |
506 | } | |
507 | ||
508 | fprintf(f, "%s\n", p); | |
509 | } | |
510 | ||
511 | ||
512 | static void print_opts(const OptParserOpt *opts, bool midlong, | |
513 | const OptParserCommonInfo *cinfo) | |
514 | { | |
515 | uint w, maxw=0; | |
516 | const OptParserOpt *o; | |
517 | ||
518 | o=opts; | |
519 | again: | |
520 | for(; O_ID(o); o++){ | |
521 | w=opt_w(o, midlong); | |
522 | if(w>maxw) | |
523 | maxw=w; | |
524 | } | |
525 | ||
526 | if(o!=&(common_opts[3])){ | |
527 | o=common_opts; | |
528 | goto again; | |
529 | } | |
530 | ||
531 | o=opts; | |
532 | again2: | |
533 | for(; O_ID(o); o++) | |
534 | print_opt(o, midlong, maxw, TERM_W); | |
535 | ||
536 | if(o!=&(common_opts[3])){ | |
537 | printf("\n"); | |
538 | o=common_opts; | |
539 | goto again2; | |
540 | } | |
541 | } | |
542 | ||
543 | ||
544 | static void print_help(const OptParserOpt *opts, bool midlong, | |
545 | const OptParserCommonInfo *cinfo) | |
546 | { | |
547 | const char *tmp, *p=cinfo->usage_tmpl; | |
548 | size_t len; | |
549 | size_t start; | |
550 | ||
551 | while(1){ | |
552 | tmp=strchr(p, '$'); | |
553 | ||
554 | if(tmp==NULL){ | |
555 | writef(stdout, p, strlen(p)); | |
556 | return; | |
557 | } | |
558 | ||
559 | if(tmp!=p) | |
560 | writef(stdout, p, tmp-p); | |
561 | ||
562 | p=tmp+1; | |
563 | ||
564 | if(*p=='p'){ | |
565 | tmp=prog_execname(); | |
566 | writef(stdout, tmp, strlen(tmp)); | |
567 | }else if(*p=='o'){ | |
568 | print_opts(opts, midlong, cinfo); | |
569 | } | |
570 | p++; | |
571 | } | |
572 | } | |
573 |