Annotation of src/usr.bin/asn1_compile/getarg.c, Revision 1.3
1.1 hin 1: /*
2: * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3: * (Royal Institute of Technology, Stockholm, Sweden).
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: *
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * 3. Neither the name of the Institute nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: */
33:
34: #ifdef HAVE_CONFIG_H
35: #include <config.h>
36: RCSID("$KTH: getarg.c,v 1.46 2002/08/20 16:23:07 joda Exp $");
37: #endif
38:
39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
42: #include "getarg.h"
43:
44: #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
45:
46: extern char *__progname;
47:
48: static size_t
49: print_arg (char *string, size_t len, int mdoc, int longp, struct getargs *arg)
50: {
51: const char *s;
52:
53: *string = '\0';
54:
55: if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
56: return 0;
57:
58: if(mdoc){
59: if(longp)
60: strlcat(string, "= Ns", len);
61: strlcat(string, " Ar ", len);
62: } else {
63: if (longp)
64: strlcat (string, "=", len);
65: else
66: strlcat (string, " ", len);
67: }
68:
69: if (arg->arg_help)
70: s = arg->arg_help;
71: else if (arg->type == arg_integer || arg->type == arg_counter)
72: s = "integer";
73: else if (arg->type == arg_string)
74: s = "string";
75: else if (arg->type == arg_strings)
76: s = "strings";
77: else if (arg->type == arg_double)
78: s = "float";
79: else
80: s = "<undefined>";
81:
82: strlcat(string, s, len);
83: return 1 + strlen(s);
84: }
85:
86: #if 0
87: static void
88: mandoc_template(struct getargs *args,
89: size_t num_args,
90: const char *progname,
91: const char *extra_string)
92: {
93: int i;
94: char timestr[64], cmd[64];
95: char buf[128];
96: const char *p;
97: time_t t;
98:
99: printf(".\\\" Things to fix:\n");
100: printf(".\\\" * correct section, and operating system\n");
101: printf(".\\\" * remove Op from mandatory flags\n");
102: printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
103: printf(".\\\"\n");
104: t = time(NULL);
105: strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t));
106: printf(".Dd %s\n", timestr);
107: p = strrchr(progname, '/');
108: if(p) p++; else p = progname;
109: strlcpy(cmd, p, sizeof(cmd));
110: strupr(cmd);
111:
112: printf(".Dt %s SECTION\n", cmd);
113: printf(".Os OPERATING_SYSTEM\n");
114: printf(".Sh NAME\n");
115: printf(".Nm %s\n", p);
116: printf(".Nd\n");
117: printf("in search of a description\n");
118: printf(".Sh SYNOPSIS\n");
119: printf(".Nm\n");
120: for(i = 0; i < num_args; i++){
121: /* we seem to hit a limit on number of arguments if doing
122: short and long flags with arguments -- split on two lines */
123: if(ISFLAG(args[i]) ||
124: args[i].short_name == 0 || args[i].long_name == NULL) {
125: printf(".Op ");
126:
127: if(args[i].short_name) {
128: print_arg(buf, sizeof(buf), 1, 0, args + i);
129: printf("Fl %c%s", args[i].short_name, buf);
130: if(args[i].long_name)
131: printf(" | ");
132: }
133: if(args[i].long_name) {
134: print_arg(buf, sizeof(buf), 1, 1, args + i);
135: printf("Fl -%s%s%s",
136: args[i].type == arg_negative_flag ? "no-" : "",
137: args[i].long_name, buf);
138: }
139: printf("\n");
140: } else {
141: print_arg(buf, sizeof(buf), 1, 0, args + i);
142: printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
143: print_arg(buf, sizeof(buf), 1, 1, args + i);
144: printf(".Fl -%s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
145: }
146: /*
147: if(args[i].type == arg_strings)
148: fprintf (stderr, "...");
149: */
150: }
151: if (extra_string && *extra_string)
152: printf (".Ar %s\n", extra_string);
153: printf(".Sh DESCRIPTION\n");
154: printf("Supported options:\n");
155: printf(".Bl -tag -width Ds\n");
156: for(i = 0; i < num_args; i++){
157: printf(".It Xo\n");
158: if(args[i].short_name){
159: printf(".Fl %c", args[i].short_name);
160: print_arg(buf, sizeof(buf), 1, 0, args + i);
161: printf("%s", buf);
162: if(args[i].long_name)
163: printf(" ,");
164: printf("\n");
165: }
166: if(args[i].long_name){
167: printf(".Fl -%s%s",
168: args[i].type == arg_negative_flag ? "no-" : "",
169: args[i].long_name);
170: print_arg(buf, sizeof(buf), 1, 1, args + i);
171: printf("%s\n", buf);
172: }
173: printf(".Xc\n");
174: if(args[i].help)
175: printf("%s\n", args[i].help);
176: /*
177: if(args[i].type == arg_strings)
178: fprintf (stderr, "...");
179: */
180: }
181: printf(".El\n");
182: printf(".\\\".Sh ENVIRONMENT\n");
183: printf(".\\\".Sh FILES\n");
184: printf(".\\\".Sh EXAMPLES\n");
185: printf(".\\\".Sh DIAGNOSTICS\n");
186: printf(".\\\".Sh SEE ALSO\n");
187: printf(".\\\".Sh STANDARDS\n");
188: printf(".\\\".Sh HISTORY\n");
189: printf(".\\\".Sh AUTHORS\n");
190: printf(".\\\".Sh BUGS\n");
191: }
192: #endif
193:
194: static int
195: check_column(FILE *f, int col, int len, int columns)
196: {
197: if(col + len > columns) {
198: fprintf(f, "\n");
199: col = fprintf(f, " ");
200: }
201: return col;
202: }
203:
204: void
205: arg_printusage (struct getargs *args,
206: size_t num_args,
207: const char *progname,
208: const char *extra_string)
209: {
210: int i;
211: size_t max_len = 0;
212: char buf[128];
213: int col = 0, columns;
214: #if 0
215: struct winsize ws;
216: #endif
217:
218: if (progname == NULL)
219: progname = __progname;
220:
221: #if 0
222: if(getenv("GETARGMANDOC")){
223: mandoc_template(args, num_args, progname, extra_string);
224: return;
225: }
226: #endif
227: #if 0
228: if(get_window_size(2, &ws) == 0)
229: columns = ws.ws_col;
230: else
231: #endif
232: columns = 80;
233: col = 0;
234: col += fprintf (stderr, "Usage: %s", progname);
235: buf[0] = '\0';
236: for (i = 0; i < num_args; ++i) {
237: if(args[i].short_name && ISFLAG(args[i])) {
238: char s[2];
239: if(buf[0] == '\0')
240: strlcpy(buf, "[-", sizeof(buf));
241: s[0] = args[i].short_name;
242: s[1] = '\0';
243: strlcat(buf, s, sizeof(buf));
244: }
245: }
246: if(buf[0] != '\0') {
247: strlcat(buf, "]", sizeof(buf));
248: col = check_column(stderr, col, strlen(buf) + 1, columns);
249: col += fprintf(stderr, " %s", buf);
250: }
251:
252: for (i = 0; i < num_args; ++i) {
253: size_t len = 0;
254:
255: if (args[i].long_name) {
256: buf[0] = '\0';
257: strlcat(buf, "[--", sizeof(buf));
258: len += 2;
259: if(args[i].type == arg_negative_flag) {
260: strlcat(buf, "no-", sizeof(buf));
261: len += 3;
262: }
263: strlcat(buf, args[i].long_name, sizeof(buf));
264: len += strlen(args[i].long_name);
265: len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
266: 0, 1, &args[i]);
267: strlcat(buf, "]", sizeof(buf));
268: if(args[i].type == arg_strings)
269: strlcat(buf, "...", sizeof(buf));
270: col = check_column(stderr, col, strlen(buf) + 1, columns);
271: col += fprintf(stderr, " %s", buf);
272: }
273: if (args[i].short_name && !ISFLAG(args[i])) {
274: snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
275: len += 2;
276: len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
277: 0, 0, &args[i]);
278: strlcat(buf, "]", sizeof(buf));
279: if(args[i].type == arg_strings)
280: strlcat(buf, "...", sizeof(buf));
281: col = check_column(stderr, col, strlen(buf) + 1, columns);
282: col += fprintf(stderr, " %s", buf);
283: }
284: if (args[i].long_name && args[i].short_name)
285: len += 2; /* ", " */
286: #if 0
287: max_len = max(max_len, len);
288: #else
289: if(len > max_len)
290: max_len = len;
291: #endif
292: }
293: if (extra_string) {
294: col = check_column(stderr, col, strlen(extra_string) + 1, columns);
295: fprintf (stderr, " %s\n", extra_string);
296: } else
297: fprintf (stderr, "\n");
298: for (i = 0; i < num_args; ++i) {
299: if (args[i].help) {
300: size_t count = 0;
301:
302: if (args[i].short_name) {
303: count += fprintf (stderr, "-%c", args[i].short_name);
304: print_arg (buf, sizeof(buf), 0, 0, &args[i]);
305: count += fprintf(stderr, "%s", buf);
306: }
307: if (args[i].short_name && args[i].long_name)
308: count += fprintf (stderr, ", ");
309: if (args[i].long_name) {
310: count += fprintf (stderr, "--");
311: if (args[i].type == arg_negative_flag)
312: count += fprintf (stderr, "no-");
313: count += fprintf (stderr, "%s", args[i].long_name);
314: print_arg (buf, sizeof(buf), 0, 1, &args[i]);
315: count += fprintf(stderr, "%s", buf);
316: }
317: while(count++ <= max_len)
318: putc (' ', stderr);
319: fprintf (stderr, "%s\n", args[i].help);
320: }
321: }
322: }
323:
324: static void
325: add_string(getarg_strings *s, char *value)
326: {
1.3 ! deraadt 327: if ((s->strings = realloc(s->strings, (s->num_strings + 1) *
! 328: sizeof(*s->strings))) == NULL)
! 329: err(1, "realloc");
1.1 hin 330: s->strings[s->num_strings] = value;
331: s->num_strings++;
332: }
333:
334: static int
335: arg_match_long(struct getargs *args, size_t num_args,
336: char *argv, int argc, char **rargv, int *goptind)
337: {
338: int i;
339: char *goptarg = NULL;
340: int negate = 0;
341: int partial_match = 0;
342: struct getargs *partial = NULL;
343: struct getargs *current = NULL;
344: int argv_len;
345: char *p;
346: int p_len;
347:
348: argv_len = strlen(argv);
349: p = strchr (argv, '=');
350: if (p != NULL)
351: argv_len = p - argv;
352:
353: for (i = 0; i < num_args; ++i) {
354: if(args[i].long_name) {
355: int len = strlen(args[i].long_name);
356: p = argv;
357: p_len = argv_len;
358: negate = 0;
359:
360: for (;;) {
361: if (strncmp (args[i].long_name, p, p_len) == 0) {
362: if(p_len == len)
363: current = &args[i];
364: else {
365: ++partial_match;
366: partial = &args[i];
367: }
368: goptarg = p + p_len;
369: } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
370: negate = !negate;
371: p += 3;
372: p_len -= 3;
373: continue;
374: }
375: break;
376: }
377: if (current)
378: break;
379: }
380: }
381: if (current == NULL) {
382: if (partial_match == 1)
383: current = partial;
384: else
385: return ARG_ERR_NO_MATCH;
386: }
387:
388: if(*goptarg == '\0'
389: && !ISFLAG(*current)
390: && current->type != arg_collect
391: && current->type != arg_counter)
392: return ARG_ERR_NO_MATCH;
393: switch(current->type){
394: case arg_integer:
395: {
396: int tmp;
397: if(sscanf(goptarg + 1, "%d", &tmp) != 1)
398: return ARG_ERR_BAD_ARG;
399: *(int*)current->value = tmp;
400: return 0;
401: }
402: case arg_string:
403: {
404: *(char**)current->value = goptarg + 1;
405: return 0;
406: }
407: case arg_strings:
408: {
409: add_string((getarg_strings*)current->value, goptarg + 1);
410: return 0;
411: }
412: case arg_flag:
413: case arg_negative_flag:
414: {
415: int *flag = current->value;
416: if(*goptarg == '\0' ||
417: strcmp(goptarg + 1, "yes") == 0 ||
418: strcmp(goptarg + 1, "true") == 0){
419: *flag = !negate;
420: return 0;
421: } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
1.2 deraadt 422: #ifdef HAVE_ARC4RANDOM
423: *flag = arc4random() & 1;
424: #elif HAVE_RANDOM
1.1 hin 425: *flag = random() & 1;
426: #else
427: *flag = rand() & 1;
428: #endif
429: } else {
430: *flag = negate;
431: return 0;
432: }
433: return ARG_ERR_BAD_ARG;
434: }
435: case arg_counter :
436: {
437: int val;
438:
439: if (*goptarg == '\0')
440: val = 1;
441: else if(sscanf(goptarg + 1, "%d", &val) != 1)
442: return ARG_ERR_BAD_ARG;
443: *(int *)current->value += val;
444: return 0;
445: }
446: case arg_double:
447: {
448: double tmp;
449: if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
450: return ARG_ERR_BAD_ARG;
451: *(double*)current->value = tmp;
452: return 0;
453: }
454: case arg_collect:{
455: struct getarg_collect_info *c = current->value;
456: int o = argv - rargv[*goptind];
457: return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
458: }
459:
460: default:
461: abort ();
462: }
463: }
464:
465: static int
466: arg_match_short (struct getargs *args, size_t num_args,
467: char *argv, int argc, char **rargv, int *goptind)
468: {
469: int j, k;
470:
471: for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
472: for(k = 0; k < num_args; k++) {
473: char *goptarg;
474:
475: if(args[k].short_name == 0)
476: continue;
477: if(argv[j] == args[k].short_name) {
478: if(args[k].type == arg_flag) {
479: *(int*)args[k].value = 1;
480: break;
481: }
482: if(args[k].type == arg_negative_flag) {
483: *(int*)args[k].value = 0;
484: break;
485: }
486: if(args[k].type == arg_counter) {
487: ++*(int *)args[k].value;
488: break;
489: }
490: if(args[k].type == arg_collect) {
491: struct getarg_collect_info *c = args[k].value;
492:
493: if((*c->func)(TRUE, argc, rargv, goptind, &j, c->data))
494: return ARG_ERR_BAD_ARG;
495: break;
496: }
497:
498: if(argv[j + 1])
499: goptarg = &argv[j + 1];
500: else {
501: ++*goptind;
502: goptarg = rargv[*goptind];
503: }
504: if(goptarg == NULL) {
505: --*goptind;
506: return ARG_ERR_NO_ARG;
507: }
508: if(args[k].type == arg_integer) {
509: int tmp;
510: if(sscanf(goptarg, "%d", &tmp) != 1)
511: return ARG_ERR_BAD_ARG;
512: *(int*)args[k].value = tmp;
513: return 0;
514: } else if(args[k].type == arg_string) {
515: *(char**)args[k].value = goptarg;
516: return 0;
517: } else if(args[k].type == arg_strings) {
518: add_string((getarg_strings*)args[k].value, goptarg);
519: return 0;
520: } else if(args[k].type == arg_double) {
521: double tmp;
522: if(sscanf(goptarg, "%lf", &tmp) != 1)
523: return ARG_ERR_BAD_ARG;
524: *(double*)args[k].value = tmp;
525: return 0;
526: }
527: return ARG_ERR_BAD_ARG;
528: }
529: }
530: if (k == num_args)
531: return ARG_ERR_NO_MATCH;
532: }
533: return 0;
534: }
535:
536: int
537: getarg(struct getargs *args, size_t num_args,
538: int argc, char **argv, int *goptind)
539: {
540: int i;
541: int ret = 0;
542:
1.2 deraadt 543: #ifndef HAVE_ARC4RANDOM
544:
1.1 hin 545: #if defined(HAVE_SRANDOMDEV)
546: srandomdev();
547: #elif defined(HAVE_RANDOM)
548: srandom(time(NULL));
549: #else
550: srand (time(NULL));
551: #endif
1.2 deraadt 552:
553: #endif /* HAVE_ARC4RANDOM */
554:
1.1 hin 555: (*goptind)++;
556: for(i = *goptind; i < argc; i++) {
557: if(argv[i][0] != '-')
558: break;
559: if(argv[i][1] == '-'){
560: if(argv[i][2] == 0){
561: i++;
562: break;
563: }
564: ret = arg_match_long (args, num_args, argv[i] + 2,
565: argc, argv, &i);
566: } else {
567: ret = arg_match_short (args, num_args, argv[i],
568: argc, argv, &i);
569: }
570: if(ret)
571: break;
572: }
573: *goptind = i;
574: return ret;
575: }
576:
577: void
578: free_getarg_strings (getarg_strings *s)
579: {
580: free (s->strings);
581: }
582:
583: #if TEST
584: int foo_flag = 2;
585: int flag1 = 0;
586: int flag2 = 0;
587: int bar_int;
588: char *baz_string;
589:
590: struct getargs args[] = {
591: { NULL, '1', arg_flag, &flag1, "one", NULL },
592: { NULL, '2', arg_flag, &flag2, "two", NULL },
593: { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
594: { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
595: { "baz", 'x', arg_string, &baz_string, "baz", "name" },
596: };
597:
598: int main(int argc, char **argv)
599: {
600: int goptind = 0;
601: while(getarg(args, 5, argc, argv, &goptind))
602: printf("Bad arg: %s\n", argv[goptind]);
603: printf("flag1 = %d\n", flag1);
604: printf("flag2 = %d\n", flag2);
605: printf("foo_flag = %d\n", foo_flag);
606: printf("bar_int = %d\n", bar_int);
607: printf("baz_flag = %s\n", baz_string);
608: arg_printusage (args, 5, argv[0], "nothing here");
609: }
610: #endif