Windows: Enable weak crypto by default
[heimdal.git] / lib / roken / getarg.c
blobe7dc74b7bc64b2efd720e1430f77669febe17336
1 /*
2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "roken.h"
40 #include "getarg.h"
42 #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
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 *))
52 const char *s;
54 *string = '\0';
56 if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
57 return 0;
59 if(mdoc){
60 if(longp)
61 strlcat(string, "= Ns", len);
62 strlcat(string, " Ar ", len);
63 } else {
64 if (longp)
65 strlcat (string, "=", len);
66 else
67 strlcat (string, " ", len);
70 if (arg->arg_help)
71 s = (*i18n)(arg->arg_help);
72 else if (arg->type == arg_integer || arg->type == arg_counter)
73 s = "integer";
74 else if (arg->type == arg_string)
75 s = "string";
76 else if (arg->type == arg_strings)
77 s = "strings";
78 else if (arg->type == arg_double)
79 s = "float";
80 else
81 s = "<undefined>";
83 strlcat(string, s, len);
84 return 1 + strlen(s);
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 *))
94 size_t i;
95 char timestr[64], cmd[64];
96 char buf[128];
97 const char *p;
98 time_t t;
100 printf(".\\\" Things to fix:\n");
101 printf(".\\\" * correct section, and operating system\n");
102 printf(".\\\" * remove Op from mandatory flags\n");
103 printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
104 printf(".\\\"\n");
105 t = time(NULL);
106 strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t));
107 printf(".Dd %s\n", timestr);
108 p = strrchr(progname, '/');
109 if(p) p++; else p = progname;
110 strlcpy(cmd, p, sizeof(cmd));
111 strupr(cmd);
113 printf(".Dt %s SECTION\n", cmd);
114 printf(".Os OPERATING_SYSTEM\n");
115 printf(".Sh NAME\n");
116 printf(".Nm %s\n", p);
117 printf(".Nd\n");
118 printf("in search of a description\n");
119 printf(".Sh SYNOPSIS\n");
120 printf(".Nm\n");
121 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 if(ISFLAG(args[i]) ||
125 args[i].short_name == 0 || args[i].long_name == NULL) {
126 printf(".Op ");
128 if(args[i].short_name) {
129 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
130 printf("Fl %c%s", args[i].short_name, buf);
131 if(args[i].long_name)
132 printf(" | ");
134 if(args[i].long_name) {
135 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
136 printf("Fl -%s%s%s",
137 args[i].type == arg_negative_flag ? "no-" : "",
138 args[i].long_name, buf);
140 printf("\n");
141 } else {
142 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
143 printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
144 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
145 printf(".Fl -%s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
148 if(args[i].type == arg_strings)
149 fprintf (stderr, "...");
152 if (extra_string && *extra_string)
153 printf (".Ar %s\n", extra_string);
154 printf(".Sh DESCRIPTION\n");
155 printf("Supported options:\n");
156 printf(".Bl -tag -width Ds\n");
157 for(i = 0; i < num_args; i++){
158 printf(".It Xo\n");
159 if(args[i].short_name){
160 printf(".Fl %c", args[i].short_name);
161 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
162 printf("%s", buf);
163 if(args[i].long_name)
164 printf(" ,");
165 printf("\n");
167 if(args[i].long_name){
168 printf(".Fl -%s%s",
169 args[i].type == arg_negative_flag ? "no-" : "",
170 args[i].long_name);
171 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
172 printf("%s\n", buf);
174 printf(".Xc\n");
175 if(args[i].help)
176 printf("%s\n", args[i].help);
178 if(args[i].type == arg_strings)
179 fprintf (stderr, "...");
182 printf(".El\n");
183 printf(".\\\".Sh ENVIRONMENT\n");
184 printf(".\\\".Sh FILES\n");
185 printf(".\\\".Sh EXAMPLES\n");
186 printf(".\\\".Sh DIAGNOSTICS\n");
187 printf(".\\\".Sh SEE ALSO\n");
188 printf(".\\\".Sh STANDARDS\n");
189 printf(".\\\".Sh HISTORY\n");
190 printf(".\\\".Sh AUTHORS\n");
191 printf(".\\\".Sh BUGS\n");
194 static int
195 check_column(FILE *f, int col, int len, int columns)
197 if(col + len > columns) {
198 fprintf(f, "\n");
199 col = fprintf(f, " ");
201 return col;
204 static char *
205 builtin_i18n(const char *str)
207 return rk_UNCONST(str);
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)
216 arg_printusage_i18n(args, num_args, "Usage",
217 progname, extra_string, builtin_i18n);
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 *))
228 size_t i, max_len = 0;
229 char buf[128];
230 int col = 0, columns;
231 struct winsize ws;
233 if (progname == NULL)
234 progname = getprogname();
236 if (i18n == NULL)
237 i18n = builtin_i18n;
239 if(getenv("GETARGMANDOC")){
240 mandoc_template(args, num_args, progname, extra_string, i18n);
241 return;
243 if(get_window_size(2, &ws) == 0)
244 columns = ws.ws_col;
245 else
246 columns = 80;
247 col = 0;
248 col += fprintf (stderr, "%s: %s", usage, progname);
249 buf[0] = '\0';
250 for (i = 0; i < num_args; ++i) {
251 if(args[i].short_name && ISFLAG(args[i])) {
252 char s[2];
253 if(buf[0] == '\0')
254 strlcpy(buf, "[-", sizeof(buf));
255 s[0] = args[i].short_name;
256 s[1] = '\0';
257 strlcat(buf, s, sizeof(buf));
260 if(buf[0] != '\0') {
261 strlcat(buf, "]", sizeof(buf));
262 col = check_column(stderr, col, strlen(buf) + 1, columns);
263 col += fprintf(stderr, " %s", buf);
266 for (i = 0; i < num_args; ++i) {
267 size_t len = 0;
269 if (args[i].long_name) {
270 buf[0] = '\0';
271 strlcat(buf, "[--", sizeof(buf));
272 len += 2;
273 if(args[i].type == arg_negative_flag) {
274 strlcat(buf, "no-", sizeof(buf));
275 len += 3;
277 strlcat(buf, args[i].long_name, sizeof(buf));
278 len += strlen(args[i].long_name);
279 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
280 0, 1, &args[i], i18n);
281 strlcat(buf, "]", sizeof(buf));
282 if(args[i].type == arg_strings)
283 strlcat(buf, "...", sizeof(buf));
284 col = check_column(stderr, col, strlen(buf) + 1, columns);
285 col += fprintf(stderr, " %s", buf);
287 if (args[i].short_name && !ISFLAG(args[i])) {
288 snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
289 len += 2;
290 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
291 0, 0, &args[i], i18n);
292 strlcat(buf, "]", sizeof(buf));
293 if(args[i].type == arg_strings)
294 strlcat(buf, "...", sizeof(buf));
295 col = check_column(stderr, col, strlen(buf) + 1, columns);
296 col += fprintf(stderr, " %s", buf);
298 if (args[i].long_name && args[i].short_name)
299 len += 2; /* ", " */
300 max_len = max(max_len, len);
302 if (extra_string) {
303 check_column(stderr, col, strlen(extra_string) + 1, columns);
304 fprintf (stderr, " %s\n", extra_string);
305 } else
306 fprintf (stderr, "\n");
307 for (i = 0; i < num_args; ++i) {
308 if (args[i].help) {
309 size_t count = 0;
311 if (args[i].short_name) {
312 count += fprintf (stderr, "-%c", args[i].short_name);
313 print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
314 count += fprintf(stderr, "%s", buf);
316 if (args[i].short_name && args[i].long_name)
317 count += fprintf (stderr, ", ");
318 if (args[i].long_name) {
319 count += fprintf (stderr, "--");
320 if (args[i].type == arg_negative_flag)
321 count += fprintf (stderr, "no-");
322 count += fprintf (stderr, "%s", args[i].long_name);
323 print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
324 count += fprintf(stderr, "%s", buf);
326 while(count++ <= max_len)
327 putc (' ', stderr);
328 fprintf (stderr, "%s\n", (*i18n)(args[i].help));
333 static int
334 add_string(getarg_strings *s, char *value)
336 char **strings;
338 strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
339 if (strings == NULL) {
340 free(s->strings);
341 s->strings = NULL;
342 s->num_strings = 0;
343 return ENOMEM;
345 s->strings = strings;
346 s->strings[s->num_strings] = value;
347 s->num_strings++;
348 return 0;
351 static int
352 arg_match_long(struct getargs *args, size_t num_args,
353 char *argv, int argc, char **rargv, int *goptind)
355 int i;
356 char *goptarg = NULL;
357 int negate = 0;
358 int partial_match = 0;
359 struct getargs *partial = NULL;
360 struct getargs *current = NULL;
361 int argv_len;
362 char *p;
363 int p_len;
365 argv_len = strlen(argv);
366 p = strchr (argv, '=');
367 if (p != NULL)
368 argv_len = p - argv;
370 for (i = 0; i < num_args; ++i) {
371 if(args[i].long_name) {
372 int len = strlen(args[i].long_name);
373 p = argv;
374 p_len = argv_len;
375 negate = 0;
377 for (;;) {
378 if (strncmp (args[i].long_name, p, p_len) == 0) {
379 if(p_len == len)
380 current = &args[i];
381 else {
382 ++partial_match;
383 partial = &args[i];
385 goptarg = p + p_len;
386 } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
387 negate = !negate;
388 p += 3;
389 p_len -= 3;
390 continue;
392 break;
394 if (current)
395 break;
398 if (current == NULL) {
399 if (partial_match == 1)
400 current = partial;
401 else
402 return ARG_ERR_NO_MATCH;
405 if(*goptarg == '\0'
406 && !ISFLAG(*current)
407 && current->type != arg_collect
408 && current->type != arg_counter)
409 return ARG_ERR_NO_MATCH;
410 switch(current->type){
411 case arg_integer:
413 int tmp;
414 if(sscanf(goptarg + 1, "%d", &tmp) != 1)
415 return ARG_ERR_BAD_ARG;
416 *(int*)current->value = tmp;
417 return 0;
419 case arg_string:
421 *(char**)current->value = goptarg + 1;
422 return 0;
424 case arg_strings:
426 return add_string((getarg_strings*)current->value, goptarg + 1);
428 case arg_flag:
429 case arg_negative_flag:
431 int *flag = current->value;
432 if(*goptarg == '\0' ||
433 strcmp(goptarg + 1, "yes") == 0 ||
434 strcmp(goptarg + 1, "true") == 0){
435 *flag = !negate;
436 return 0;
437 } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
438 #ifdef HAVE_RANDOM
439 *flag = random() & 1;
440 #else
441 *flag = rand() & 1;
442 #endif
443 } else {
444 *flag = negate;
445 return 0;
447 return ARG_ERR_BAD_ARG;
449 case arg_counter :
451 int val;
453 if (*goptarg == '\0')
454 val = 1;
455 else if(sscanf(goptarg + 1, "%d", &val) != 1)
456 return ARG_ERR_BAD_ARG;
457 *(int *)current->value += val;
458 return 0;
460 case arg_double:
462 double tmp;
463 if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
464 return ARG_ERR_BAD_ARG;
465 *(double*)current->value = tmp;
466 return 0;
468 case arg_collect:{
469 struct getarg_collect_info *c = current->value;
470 int o = argv - rargv[*goptind];
471 return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
474 default:
475 abort ();
476 UNREACHABLE(return 0);
480 static int
481 arg_match_short (struct getargs *args, size_t num_args,
482 char *argv, int argc, char **rargv, int *goptind)
484 int j, k;
486 for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
487 for(k = 0; k < num_args; k++) {
488 char *goptarg;
490 if(args[k].short_name == 0)
491 continue;
492 if(argv[j] == args[k].short_name) {
493 if(args[k].type == arg_flag) {
494 *(int*)args[k].value = 1;
495 break;
497 if(args[k].type == arg_negative_flag) {
498 *(int*)args[k].value = 0;
499 break;
501 if(args[k].type == arg_counter) {
502 ++*(int *)args[k].value;
503 break;
505 if(args[k].type == arg_collect) {
506 struct getarg_collect_info *c = args[k].value;
508 if((*c->func)(TRUE, argc, rargv, goptind, &j, c->data))
509 return ARG_ERR_BAD_ARG;
510 break;
513 if(argv[j + 1])
514 goptarg = &argv[j + 1];
515 else {
516 ++*goptind;
517 goptarg = rargv[*goptind];
519 if(goptarg == NULL) {
520 --*goptind;
521 return ARG_ERR_NO_ARG;
523 if(args[k].type == arg_integer) {
524 int tmp;
525 if(sscanf(goptarg, "%d", &tmp) != 1)
526 return ARG_ERR_BAD_ARG;
527 *(int*)args[k].value = tmp;
528 return 0;
529 } else if(args[k].type == arg_string) {
530 *(char**)args[k].value = goptarg;
531 return 0;
532 } else if(args[k].type == arg_strings) {
533 return add_string((getarg_strings*)args[k].value, goptarg);
534 } else if(args[k].type == arg_double) {
535 double tmp;
536 if(sscanf(goptarg, "%lf", &tmp) != 1)
537 return ARG_ERR_BAD_ARG;
538 *(double*)args[k].value = tmp;
539 return 0;
541 return ARG_ERR_BAD_ARG;
544 if (k == num_args)
545 return ARG_ERR_NO_MATCH;
547 return 0;
550 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
551 getarg(struct getargs *args, size_t num_args,
552 int argc, char **argv, int *goptind)
554 int i;
555 int ret = 0;
557 #if defined(HAVE_SRANDOMDEV)
558 srandomdev();
559 #elif defined(HAVE_RANDOM)
560 srandom(time(NULL));
561 #else
562 srand ((int) time(NULL));
563 #endif
564 (*goptind)++;
565 for(i = *goptind; i < argc; i++) {
566 if(argv[i][0] != '-')
567 break;
568 if(argv[i][1] == '-'){
569 if(argv[i][2] == 0){
570 i++;
571 break;
573 ret = arg_match_long (args, num_args, argv[i] + 2,
574 argc, argv, &i);
575 } else {
576 ret = arg_match_short (args, num_args, argv[i],
577 argc, argv, &i);
579 if(ret)
580 break;
582 *goptind = i;
583 return ret;
586 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
587 free_getarg_strings (getarg_strings *s)
589 free (s->strings);
592 #if TEST
593 int foo_flag = 2;
594 int flag1 = 0;
595 int flag2 = 0;
596 int bar_int;
597 char *baz_string;
599 struct getargs args[] = {
600 { NULL, '1', arg_flag, &flag1, "one", NULL },
601 { NULL, '2', arg_flag, &flag2, "two", NULL },
602 { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
603 { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
604 { "baz", 'x', arg_string, &baz_string, "baz", "name" },
607 int main(int argc, char **argv)
609 int goptind = 0;
610 while(getarg(args, 5, argc, argv, &goptind))
611 printf("Bad arg: %s\n", argv[goptind]);
612 printf("flag1 = %d\n", flag1);
613 printf("flag2 = %d\n", flag2);
614 printf("foo_flag = %d\n", foo_flag);
615 printf("bar_int = %d\n", bar_int);
616 printf("baz_flag = %s\n", baz_string);
617 arg_printusage (args, 5, argv[0], "nothing here");
619 #endif