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