2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
42 #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
45 print_arg (char *string,
50 char *(i18n)(const char *))
56 if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
61 strlcat(string, "= Ns", len);
62 strlcat(string, " Ar ", len);
65 strlcat (string, "=", len);
67 strlcat (string, " ", len);
71 s = (*i18n)(arg->arg_help);
72 else if (arg->type == arg_integer || arg->type == arg_counter)
74 else if (arg->type == arg_string)
76 else if (arg->type == arg_strings)
78 else if (arg->type == arg_double)
83 strlcat(string, s, len);
88 mandoc_template(struct getargs *args,
91 const char *extra_string,
92 char *(i18n)(const char *))
95 char timestr[64], cmd[64];
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");
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));
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 in search of a description\n");
118 printf(".Sh SYNOPSIS\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) {
127 if(args[i].short_name) {
128 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
129 printf("Fl %c%s", args[i].short_name, buf);
130 if(args[i].long_name)
133 if(args[i].long_name) {
134 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
135 printf("Fl Fl %s%s%s",
136 args[i].type == arg_negative_flag ? "no-" : "",
137 args[i].long_name, buf);
141 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
142 printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
143 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
144 printf(".Fl Fl %s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
147 if(args[i].type == arg_strings)
148 fprintf (stderr, "...");
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++){
158 if(args[i].short_name){
159 printf(".Fl %c", args[i].short_name);
160 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
162 if(args[i].long_name)
166 if(args[i].long_name){
167 printf(".Fl Fl %s%s",
168 args[i].type == arg_negative_flag ? "no-" : "",
170 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
175 printf("%s\n", args[i].help);
177 if(args[i].type == arg_strings)
178 fprintf (stderr, "...");
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");
194 check_column(FILE *f, int col, int len, int columns)
196 if(col + len > columns) {
198 col = fprintf(f, " ");
204 builtin_i18n(const char *str)
206 return rk_UNCONST(str);
209 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
210 arg_printusage (struct getargs *args,
212 const char *progname,
213 const char *extra_string)
215 arg_printusage_i18n(args, num_args, "Usage",
216 progname, extra_string, builtin_i18n);
219 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
220 arg_printusage_i18n (struct getargs *args,
223 const char *progname,
224 const char *extra_string,
225 char *(*i18n)(const char *))
227 size_t i, max_len = 0;
229 int col = 0, columns;
231 if (progname == NULL)
232 progname = getprogname();
237 if(getenv("GETARGMANDOC")){
238 mandoc_template(args, num_args, progname, extra_string, i18n);
241 if(get_window_size(2, NULL, &columns) == -1)
244 col += fprintf (stderr, "%s: %s", usage, progname);
246 for (i = 0; i < num_args; ++i) {
247 if(args[i].short_name && ISFLAG(args[i])) {
250 strlcpy(buf, "[-", sizeof(buf));
251 s[0] = args[i].short_name;
253 strlcat(buf, s, sizeof(buf));
257 strlcat(buf, "]", sizeof(buf));
258 col = check_column(stderr, col, strlen(buf) + 1, columns);
259 col += fprintf(stderr, " %s", buf);
262 for (i = 0; i < num_args; ++i) {
265 if (args[i].long_name) {
267 strlcat(buf, "[--", sizeof(buf));
269 if(args[i].type == arg_negative_flag) {
270 strlcat(buf, "no-", sizeof(buf));
273 strlcat(buf, args[i].long_name, sizeof(buf));
274 len += strlen(args[i].long_name);
275 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
276 0, 1, &args[i], i18n);
277 strlcat(buf, "]", sizeof(buf));
278 if(args[i].type == arg_strings)
279 strlcat(buf, "...", sizeof(buf));
280 col = check_column(stderr, col, strlen(buf) + 1, columns);
281 col += fprintf(stderr, " %s", buf);
283 if (args[i].short_name && !ISFLAG(args[i])) {
284 snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
286 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
287 0, 0, &args[i], i18n);
288 strlcat(buf, "]", sizeof(buf));
289 if(args[i].type == arg_strings)
290 strlcat(buf, "...", sizeof(buf));
291 col = check_column(stderr, col, strlen(buf) + 1, columns);
292 col += fprintf(stderr, " %s", buf);
294 if (args[i].long_name && args[i].short_name)
296 max_len = max(max_len, len);
299 check_column(stderr, col, strlen(extra_string) + 1, columns);
300 fprintf (stderr, " %s\n", extra_string);
302 fprintf (stderr, "\n");
303 for (i = 0; i < num_args; ++i) {
307 if (args[i].short_name) {
308 count += fprintf (stderr, "-%c", args[i].short_name);
309 print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
310 count += fprintf(stderr, "%s", buf);
312 if (args[i].short_name && args[i].long_name)
313 count += fprintf (stderr, ", ");
314 if (args[i].long_name) {
315 count += fprintf (stderr, "--");
316 if (args[i].type == arg_negative_flag)
317 count += fprintf (stderr, "no-");
318 count += fprintf (stderr, "%s", args[i].long_name);
319 print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
320 count += fprintf(stderr, "%s", buf);
322 while(count++ <= max_len)
324 fprintf (stderr, "%s\n", (*i18n)(args[i].help));
330 add_string(getarg_strings *s, char *value)
334 strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
335 if (strings == NULL) {
341 s->strings = strings;
342 s->strings[s->num_strings] = value;
348 arg_match_long(struct getargs *args, size_t num_args,
349 char *argv, int argc, char **rargv, int *goptind)
352 char *goptarg = NULL;
354 int partial_match = 0;
355 struct getargs *partial = NULL;
356 struct getargs *current = NULL;
361 argv_len = strlen(argv);
362 p = strchr (argv, '=');
366 for (i = 0; i < num_args; ++i) {
367 if(args[i].long_name) {
368 int len = strlen(args[i].long_name);
374 if (strncmp (args[i].long_name, p, p_len) == 0) {
382 } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
394 if (current == NULL) {
395 if (partial_match == 1)
398 return ARG_ERR_NO_MATCH;
403 && current->type != arg_collect
404 && current->type != arg_counter)
405 return ARG_ERR_NO_MATCH;
406 switch(current->type){
410 if(sscanf(goptarg + 1, "%d", &tmp) != 1)
411 return ARG_ERR_BAD_ARG;
412 *(int*)current->value = tmp;
417 *(char**)current->value = goptarg + 1;
422 return add_string((getarg_strings*)current->value, goptarg + 1);
425 case arg_negative_flag:
427 int *flag = current->value;
428 if(*goptarg == '\0' ||
429 strcmp(goptarg + 1, "yes") == 0 ||
430 strcmp(goptarg + 1, "true") == 0){
433 } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
434 *flag = rk_random() & 1;
439 return ARG_ERR_BAD_ARG;
445 if (*goptarg == '\0')
447 else if(sscanf(goptarg + 1, "%d", &val) != 1)
448 return ARG_ERR_BAD_ARG;
449 *(int *)current->value += val;
455 if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
456 return ARG_ERR_BAD_ARG;
457 *(double*)current->value = tmp;
461 struct getarg_collect_info *c = current->value;
462 int o = argv - rargv[*goptind];
463 return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
468 UNREACHABLE(return 0);
473 arg_match_short (struct getargs *args, size_t num_args,
474 char *argv, int argc, char **rargv, int *goptind)
478 for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
479 for(k = 0; k < num_args; k++) {
482 if(args[k].short_name == 0)
484 if(argv[j] == args[k].short_name) {
485 if(args[k].type == arg_flag) {
486 *(int*)args[k].value = 1;
489 if(args[k].type == arg_negative_flag) {
490 *(int*)args[k].value = 0;
493 if(args[k].type == arg_counter) {
494 ++*(int *)args[k].value;
497 if(args[k].type == arg_collect) {
498 struct getarg_collect_info *c = args[k].value;
501 if((*c->func)(TRUE, argc, rargv, goptind, &a, c->data))
502 return ARG_ERR_BAD_ARG;
508 goptarg = &argv[j + 1];
511 goptarg = rargv[*goptind];
513 if(goptarg == NULL) {
515 return ARG_ERR_NO_ARG;
517 if(args[k].type == arg_integer) {
519 if(sscanf(goptarg, "%d", &tmp) != 1)
520 return ARG_ERR_BAD_ARG;
521 *(int*)args[k].value = tmp;
523 } else if(args[k].type == arg_string) {
524 *(char**)args[k].value = goptarg;
526 } else if(args[k].type == arg_strings) {
527 return add_string((getarg_strings*)args[k].value, goptarg);
528 } else if(args[k].type == arg_double) {
530 if(sscanf(goptarg, "%lf", &tmp) != 1)
531 return ARG_ERR_BAD_ARG;
532 *(double*)args[k].value = tmp;
535 return ARG_ERR_BAD_ARG;
539 return ARG_ERR_NO_MATCH;
544 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
545 getarg(struct getargs *args, size_t num_args,
546 int argc, char **argv, int *goptind)
553 for(i = *goptind; i < argc; i++) {
554 if(argv[i][0] != '-')
556 if(argv[i][1] == '-'){
561 ret = arg_match_long (args, num_args, argv[i] + 2,
564 ret = arg_match_short (args, num_args, argv[i],
574 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
575 free_getarg_strings (getarg_strings *s)
587 struct getargs args[] = {
588 { NULL, '1', arg_flag, &flag1, "one", NULL },
589 { NULL, '2', arg_flag, &flag2, "two", NULL },
590 { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
591 { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
592 { "baz", 'x', arg_string, &baz_string, "baz", "name" },
595 int main(int argc, char **argv)
598 while(getarg(args, 5, argc, argv, &goptind))
599 printf("Bad arg: %s\n", argv[goptind]);
600 printf("flag1 = %d\n", flag1);
601 printf("flag2 = %d\n", flag2);
602 printf("foo_flag = %d\n", foo_flag);
603 printf("bar_int = %d\n", bar_int);
604 printf("baz_flag = %s\n", baz_string);
605 arg_printusage (args, 5, argv[0], "nothing here");