Sun, 21 May 2000 17:33:48 +0200
trunk: changeset 15
- Optparser generates --help from option descriptions in the
OptParserOpt structure
- Changed the options --license, --authors and --proginfo to single
--about
0 | 1 | /* |
2 | * libtu/optparser.c | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
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); | |
0 | 149 | }else if(*(p+1)=='-'){ |
150 | /* --foo */ | |
151 | if(*(p+2)=='\0'){ | |
3 | 152 | /* -- arguments */ |
0 | 153 | o_args_left=o_left; |
154 | RET(OPT_ID_ARGUMENT); | |
155 | } | |
3 | 156 | type=LONG; |
0 | 157 | p2=p+2; |
158 | }else{ | |
159 | /* -foo */ | |
160 | if(*(p+1)=='\0'){ | |
161 | /* - */ | |
162 | o_args_left=1; | |
163 | RET(OPT_ID_ARGUMENT); | |
164 | } | |
3 | 165 | if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG) |
166 | type=MIDLONG; | |
167 | ||
0 | 168 | p2=p+1; |
169 | } | |
170 | ||
171 | o=o_opts; | |
172 | ||
173 | again: | |
12 | 174 | for(; O_ID(o); o++){ |
3 | 175 | if(type==LONG){ |
0 | 176 | /* Do long option (--foo=bar) */ |
177 | if(o->longopt==NULL) | |
178 | continue; | |
179 | l=strlen(o->longopt); | |
180 | if(strncmp(p2, o->longopt, l)!=0) | |
181 | continue; | |
182 | ||
183 | if(p2[l]=='\0'){ | |
184 | if(O_ARGS(o)==OPT_ARG) | |
185 | RET(E_OPT_MISSING_ARGUMENT); | |
186 | return O_ID(o); | |
187 | }else if(p2[l]=='='){ | |
188 | if(!O_ARGS(o)) | |
189 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
190 | if(p2[l+1]=='\0') | |
191 | RET(E_OPT_MISSING_ARGUMENT); | |
192 | o_tmp=p2+l+1; | |
193 | o_args_left=1; | |
194 | return O_ID(o); | |
195 | } | |
3 | 196 | continue; |
197 | }else if(type==MIDLONG){ | |
198 | if(o->longopt==NULL) | |
199 | continue; | |
200 | ||
201 | if(strcmp(p2, o->longopt)!=0) | |
202 | continue; | |
203 | }else{ /* type==SHORT */ | |
12 | 204 | if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG)) |
0 | 205 | continue; |
206 | ||
207 | if(*(p2+1)!='\0'){ | |
3 | 208 | if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){ |
209 | /*valid_chain(p2+1, o_opts)*/ | |
210 | o_chain_ptr=p2+1; | |
211 | p++; | |
212 | }else if(o_mode==OPTP_IMMEDIATE){ | |
213 | if(!O_ARGS(o)){ | |
214 | if(*(p2+1)!='\0') | |
215 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
216 | }else{ | |
217 | if(*(p2+1)=='\0') | |
218 | RET(E_OPT_MISSING_ARGUMENT); | |
219 | o_tmp=p2+1; | |
220 | o_args_left=1; | |
221 | } | |
222 | return O_ID(o); | |
0 | 223 | }else{ |
3 | 224 | RET(E_OPT_SYNTAX_ERROR); |
0 | 225 | } |
226 | } | |
3 | 227 | } |
228 | ||
229 | do_arg: | |
230 | ||
231 | if(!O_ARGS(o)) | |
232 | return O_ID(o); | |
233 | ||
234 | if(!o_left || is_option(*o_current)){ | |
235 | if(O_ARGS(o)==OPT_OPT_ARG) | |
0 | 236 | return O_ID(o); |
3 | 237 | RET(E_OPT_MISSING_ARGUMENT); |
238 | } | |
0 | 239 | |
3 | 240 | o_args_left=1; |
241 | return O_ID(o); | |
0 | 242 | } |
12 | 243 | |
244 | if(o!=&(common_opts[3])){ | |
245 | o=common_opts; | |
246 | goto again; | |
247 | } | |
0 | 248 | |
249 | if(dash) | |
250 | RET(E_OPT_INVALID_OPTION); | |
251 | ||
252 | RET(OPT_ID_ARGUMENT); | |
253 | #undef RET | |
254 | } | |
255 | ||
256 | ||
12 | 257 | int optparser_get_opt() |
258 | { | |
259 | int oid=optparser_do_get_opt(); | |
260 | ||
261 | if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0) | |
262 | return oid; | |
263 | ||
264 | switch(oid){ | |
265 | case OPT_ID_ABOUT: | |
266 | if(o_cinfo->about!=NULL) | |
267 | printf("%s", o_cinfo->about); | |
268 | break; | |
269 | ||
270 | case OPT_ID_VERSION: | |
271 | if(o_cinfo->version!=NULL) | |
272 | printf("%s\n", o_cinfo->version); | |
273 | break; | |
274 | ||
275 | case OPT_ID_HELP: | |
276 | print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo); | |
277 | break; | |
278 | } | |
279 | ||
280 | exit(EXIT_SUCCESS); | |
281 | } | |
282 | ||
283 | ||
0 | 284 | /* */ |
285 | ||
286 | ||
287 | const char* optparser_get_arg() | |
288 | { | |
289 | const char *p; | |
290 | ||
291 | if(o_tmp!=NULL){ | |
292 | /* If o_args_left==0, then were returning an invalid option | |
293 | * otherwise an immediate argument (e.g. -funsigned-char | |
294 | * where '-f' is the option and 'unsigned-char' the argument) | |
295 | */ | |
296 | if(o_args_left>0) | |
297 | o_args_left--; | |
298 | p=o_tmp; | |
299 | o_tmp=NULL; | |
300 | return p; | |
301 | } | |
302 | ||
303 | if(o_args_left<1 || o_left<1) | |
304 | return NULL; | |
305 | ||
306 | o_left--; | |
307 | o_args_left--; | |
308 | return *o_current++; | |
309 | } | |
310 | ||
311 | ||
312 | /* */ | |
313 | ||
314 | static void warn_arg(const char *e) | |
315 | { | |
3 | 316 | const char *p=optparser_get_arg(); |
0 | 317 | |
318 | if(p==NULL) | |
319 | warn("%s (null)", e); | |
320 | else | |
321 | warn("%s \'%s\'", e, p); | |
322 | } | |
323 | ||
324 | ||
325 | static void warn_opt(const char *e) | |
326 | { | |
3 | 327 | if(o_tmp!=NULL && o_chain_ptr!=NULL) |
0 | 328 | warn("%s \'-%c\'", e, *o_tmp); |
329 | else | |
330 | warn_arg(e); | |
331 | } | |
332 | ||
333 | ||
334 | void optparser_print_error() | |
335 | { | |
336 | switch(o_error){ | |
337 | case E_OPT_INVALID_OPTION: | |
3 | 338 | case E_OPT_INVALID_CHAIN_OPTION: |
0 | 339 | warn_opt(TR("Invalid option")); |
340 | break; | |
341 | ||
342 | case E_OPT_SYNTAX_ERROR: | |
343 | warn_arg(TR("Syntax error while parsing")); | |
344 | break; | |
345 | ||
346 | case E_OPT_MISSING_ARGUMENT: | |
347 | warn_opt(TR("Missing argument to")); | |
348 | break; | |
349 | ||
350 | case E_OPT_UNEXPECTED_ARGUMENT: | |
3 | 351 | warn_opt(TR("No argument expected:")); |
0 | 352 | break; |
353 | ||
354 | case OPT_ID_ARGUMENT: | |
355 | warn(TR("Unexpected argument")); | |
356 | break; | |
357 | ||
358 | default: | |
359 | warn(TR("(unknown error)")); | |
360 | } | |
361 | ||
362 | o_tmp=NULL; | |
363 | o_error=0; | |
364 | } | |
12 | 365 | |
366 | ||
367 | /* */ | |
368 | ||
369 | ||
370 | static uint opt_w(const OptParserOpt *opt, bool midlong) | |
371 | { | |
372 | uint w=0; | |
373 | ||
374 | if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){ | |
375 | w+=2; /* "-o" */ | |
376 | if(opt->longopt!=NULL) | |
377 | w+=2; /* ", " */ | |
378 | } | |
379 | ||
380 | if(opt->longopt!=NULL) | |
381 | w+=strlen(opt->longopt)+(midlong ? 1 : 2); | |
382 | ||
383 | if(O_ARGS(opt)){ | |
384 | if(opt->argname==NULL) | |
385 | w+=4; | |
386 | else | |
387 | w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */ | |
388 | ||
389 | if(O_OPT_ARG(opt)) | |
390 | w+=2; /* [ARG] */ | |
391 | } | |
392 | ||
393 | return w; | |
394 | } | |
395 | ||
396 | ||
397 | #define TERM_W 80 | |
398 | #define OFF1 2 | |
399 | #define OFF2 2 | |
400 | #define SPACER1 " " | |
401 | #define SPACER2 " " | |
402 | ||
403 | static void print_opt(const OptParserOpt *opt, bool midlong, | |
404 | uint maxw, uint tw) | |
405 | { | |
406 | FILE *f=stdout; | |
407 | const char *p, *p2, *p3; | |
408 | uint w=0; | |
409 | ||
410 | fprintf(f, SPACER1); | |
411 | ||
412 | /* short opt */ | |
413 | ||
414 | if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){ | |
415 | fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG); | |
416 | w+=2; | |
417 | ||
418 | if(opt->longopt!=NULL){ | |
419 | fprintf(f, ", "); | |
420 | w+=2; | |
421 | } | |
422 | } | |
423 | ||
424 | /* long opt */ | |
425 | ||
426 | if(opt->longopt!=NULL){ | |
427 | if(midlong){ | |
428 | w++; | |
429 | fprintf(f, "-%s", opt->longopt); | |
430 | }else{ | |
431 | w+=2; | |
432 | fprintf(f, "--%s", opt->longopt); | |
433 | } | |
434 | w+=strlen(opt->longopt); | |
435 | } | |
436 | ||
437 | /* arg */ | |
438 | ||
439 | if(O_ARGS(opt)){ | |
440 | w++; | |
441 | if(opt->longopt!=NULL && !midlong) | |
442 | putc('=', f); | |
443 | else | |
444 | putc(' ', f); | |
445 | ||
446 | if(O_OPT_ARG(opt)){ | |
447 | w+=2; | |
448 | putc('[', f); | |
449 | } | |
450 | ||
451 | if(opt->argname!=NULL){ | |
452 | fprintf(f, "%s", opt->argname); | |
453 | w+=strlen(opt->argname); | |
454 | }else{ | |
455 | w+=3; | |
456 | fprintf(f, "ARG"); | |
457 | } | |
458 | ||
459 | if(O_OPT_ARG(opt)) | |
460 | putc(']', f); | |
461 | } | |
462 | ||
463 | while(w++<maxw) | |
464 | putc(' ', f); | |
465 | ||
466 | /* descr */ | |
467 | ||
468 | p=p2=opt->descr; | |
469 | ||
470 | if(p==NULL){ | |
471 | putc('\n', f); | |
472 | return; | |
473 | } | |
474 | ||
475 | fprintf(f, SPACER2); | |
476 | ||
477 | maxw+=OFF1+OFF2; | |
478 | tw-=maxw; | |
479 | ||
480 | while(strlen(p)>tw){ | |
481 | p3=p2=p+tw-2; | |
482 | ||
483 | while(*p2!=' ' && p2!=p) | |
484 | p2--; | |
485 | ||
486 | while(*p3!=' ' && *p3!='\0') | |
487 | p3++; | |
488 | ||
489 | if((uint)(p3-p2)>tw){ | |
490 | /* long word - just wrap */ | |
491 | p2=p+tw-2; | |
492 | } | |
493 | ||
494 | writef(f, p, p2-p); | |
495 | if(*p2==' ') | |
496 | putc('\n', f); | |
497 | else | |
498 | fprintf(f, "\\\n"); | |
499 | ||
500 | p=p2+1; | |
501 | ||
502 | w=maxw; | |
503 | while(w--) | |
504 | putc(' ', f); | |
505 | } | |
506 | ||
507 | fprintf(f, "%s\n", p); | |
508 | } | |
509 | ||
510 | ||
511 | static void print_opts(const OptParserOpt *opts, bool midlong, | |
512 | const OptParserCommonInfo *cinfo) | |
513 | { | |
514 | uint w, maxw=0; | |
515 | const OptParserOpt *o; | |
516 | ||
517 | o=opts; | |
518 | again: | |
519 | for(; O_ID(o); o++){ | |
520 | w=opt_w(o, midlong); | |
521 | if(w>maxw) | |
522 | maxw=w; | |
523 | } | |
524 | ||
525 | if(o!=&(common_opts[3])){ | |
526 | o=common_opts; | |
527 | goto again; | |
528 | } | |
529 | ||
530 | o=opts; | |
531 | again2: | |
532 | for(; O_ID(o); o++) | |
533 | print_opt(o, midlong, maxw, TERM_W); | |
534 | ||
535 | if(o!=&(common_opts[3])){ | |
536 | printf("\n"); | |
537 | o=common_opts; | |
538 | goto again2; | |
539 | } | |
540 | } | |
541 | ||
542 | ||
543 | static void print_help(const OptParserOpt *opts, bool midlong, | |
544 | const OptParserCommonInfo *cinfo) | |
545 | { | |
546 | const char *tmp, *p=cinfo->usage_tmpl; | |
547 | size_t len; | |
548 | size_t start; | |
549 | ||
550 | while(1){ | |
551 | tmp=strchr(p, '$'); | |
552 | ||
553 | if(tmp==NULL){ | |
554 | writef(stdout, p, strlen(p)); | |
555 | return; | |
556 | } | |
557 | ||
558 | if(tmp!=p) | |
559 | writef(stdout, p, tmp-p); | |
560 | ||
561 | p=tmp+1; | |
562 | ||
563 | if(*p=='p'){ | |
564 | tmp=prog_execname(); | |
565 | writef(stdout, tmp, strlen(tmp)); | |
566 | }else if(*p=='o'){ | |
567 | print_opts(opts, midlong, cinfo); | |
568 | } | |
569 | p++; | |
570 | } | |
571 | } | |
572 |