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
45
#define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
48
print_arg (char *string,
53
char *(i18n)(const char *))
59
if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
64
strlcat(string, "= Ns", len);
65
strlcat(string, " Ar ", len);
68
strlcat (string, "=", len);
70
strlcat (string, " ", len);
74
s = (*i18n)(arg->arg_help);
75
else if (arg->type == arg_integer || arg->type == arg_counter)
77
else if (arg->type == arg_string)
79
else if (arg->type == arg_strings)
81
else if (arg->type == arg_double)
86
strlcat(string, s, len);
91
mandoc_template(struct getargs *args,
94
const char *extra_string,
95
char *(i18n)(const char *))
98
char timestr[64], cmd[64];
103
printf(".\\\" Things to fix:\n");
104
printf(".\\\" * correct section, and operating system\n");
105
printf(".\\\" * remove Op from mandatory flags\n");
106
printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
109
strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t));
110
printf(".Dd %s\n", timestr);
111
p = strrchr(progname, '/');
112
if(p) p++; else p = progname;
113
strlcpy(cmd, p, sizeof(cmd));
116
printf(".Dt %s SECTION\n", cmd);
117
printf(".Os OPERATING_SYSTEM\n");
118
printf(".Sh NAME\n");
119
printf(".Nm %s\n", p);
121
printf("in search of a description\n");
122
printf(".Sh SYNOPSIS\n");
124
for(i = 0; i < num_args; i++){
125
/* we seem to hit a limit on number of arguments if doing
126
short and long flags with arguments -- split on two lines */
127
if(ISFLAG(args[i]) ||
128
args[i].short_name == 0 || args[i].long_name == NULL) {
131
if(args[i].short_name) {
132
print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
133
printf("Fl %c%s", args[i].short_name, buf);
134
if(args[i].long_name)
137
if(args[i].long_name) {
138
print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
140
args[i].type == arg_negative_flag ? "no-" : "",
141
args[i].long_name, buf);
145
print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
146
printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
147
print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
148
printf(".Fl -%s%s\n.Xc\n.Oc\n", args[i].long_name, buf);
151
if(args[i].type == arg_strings)
152
fprintf (stderr, "...");
155
if (extra_string && *extra_string)
156
printf (".Ar %s\n", extra_string);
157
printf(".Sh DESCRIPTION\n");
158
printf("Supported options:\n");
159
printf(".Bl -tag -width Ds\n");
160
for(i = 0; i < num_args; i++){
162
if(args[i].short_name){
163
printf(".Fl %c", args[i].short_name);
164
print_arg(buf, sizeof(buf), 1, 0, args + i, i18n);
166
if(args[i].long_name)
170
if(args[i].long_name){
172
args[i].type == arg_negative_flag ? "no-" : "",
174
print_arg(buf, sizeof(buf), 1, 1, args + i, i18n);
179
printf("%s\n", args[i].help);
181
if(args[i].type == arg_strings)
182
fprintf (stderr, "...");
186
printf(".\\\".Sh ENVIRONMENT\n");
187
printf(".\\\".Sh FILES\n");
188
printf(".\\\".Sh EXAMPLES\n");
189
printf(".\\\".Sh DIAGNOSTICS\n");
190
printf(".\\\".Sh SEE ALSO\n");
191
printf(".\\\".Sh STANDARDS\n");
192
printf(".\\\".Sh HISTORY\n");
193
printf(".\\\".Sh AUTHORS\n");
194
printf(".\\\".Sh BUGS\n");
198
check_column(FILE *f, int col, int len, int columns)
200
if(col + len > columns) {
202
col = fprintf(f, " ");
208
builtin_i18n(const char *str)
210
return rk_UNCONST(str);
213
void ROKEN_LIB_FUNCTION
214
arg_printusage (struct getargs *args,
216
const char *progname,
217
const char *extra_string)
219
arg_printusage_i18n(args, num_args, "Usage",
220
progname, extra_string, builtin_i18n);
223
void ROKEN_LIB_FUNCTION
224
arg_printusage_i18n (struct getargs *args,
227
const char *progname,
228
const char *extra_string,
229
char *(i18n)(const char *))
234
int col = 0, columns;
237
if (progname == NULL)
238
progname = getprogname();
243
if(getenv("GETARGMANDOC")){
244
mandoc_template(args, num_args, progname, extra_string, i18n);
247
if(get_window_size(2, &ws) == 0)
252
col += fprintf (stderr, "%s: %s", usage, progname);
254
for (i = 0; i < num_args; ++i) {
255
if(args[i].short_name && ISFLAG(args[i])) {
258
strlcpy(buf, "[-", sizeof(buf));
259
s[0] = args[i].short_name;
261
strlcat(buf, s, sizeof(buf));
265
strlcat(buf, "]", sizeof(buf));
266
col = check_column(stderr, col, strlen(buf) + 1, columns);
267
col += fprintf(stderr, " %s", buf);
270
for (i = 0; i < num_args; ++i) {
273
if (args[i].long_name) {
275
strlcat(buf, "[--", sizeof(buf));
277
if(args[i].type == arg_negative_flag) {
278
strlcat(buf, "no-", sizeof(buf));
281
strlcat(buf, args[i].long_name, sizeof(buf));
282
len += strlen(args[i].long_name);
283
len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
284
0, 1, &args[i], i18n);
285
strlcat(buf, "]", sizeof(buf));
286
if(args[i].type == arg_strings)
287
strlcat(buf, "...", sizeof(buf));
288
col = check_column(stderr, col, strlen(buf) + 1, columns);
289
col += fprintf(stderr, " %s", buf);
291
if (args[i].short_name && !ISFLAG(args[i])) {
292
snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
294
len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
295
0, 0, &args[i], i18n);
296
strlcat(buf, "]", sizeof(buf));
297
if(args[i].type == arg_strings)
298
strlcat(buf, "...", sizeof(buf));
299
col = check_column(stderr, col, strlen(buf) + 1, columns);
300
col += fprintf(stderr, " %s", buf);
302
if (args[i].long_name && args[i].short_name)
304
max_len = max(max_len, len);
307
check_column(stderr, col, strlen(extra_string) + 1, columns);
308
fprintf (stderr, " %s\n", extra_string);
310
fprintf (stderr, "\n");
311
for (i = 0; i < num_args; ++i) {
315
if (args[i].short_name) {
316
count += fprintf (stderr, "-%c", args[i].short_name);
317
print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n);
318
count += fprintf(stderr, "%s", buf);
320
if (args[i].short_name && args[i].long_name)
321
count += fprintf (stderr, ", ");
322
if (args[i].long_name) {
323
count += fprintf (stderr, "--");
324
if (args[i].type == arg_negative_flag)
325
count += fprintf (stderr, "no-");
326
count += fprintf (stderr, "%s", args[i].long_name);
327
print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n);
328
count += fprintf(stderr, "%s", buf);
330
while(count++ <= max_len)
332
fprintf (stderr, "%s\n", (*i18n)(args[i].help));
338
add_string(getarg_strings *s, char *value)
342
strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
343
if (strings == NULL) {
349
s->strings = strings;
350
s->strings[s->num_strings] = value;
356
arg_match_long(struct getargs *args, size_t num_args,
357
char *argv, int argc, char **rargv, int *goptind)
360
char *goptarg = NULL;
362
int partial_match = 0;
363
struct getargs *partial = NULL;
364
struct getargs *current = NULL;
369
argv_len = strlen(argv);
370
p = strchr (argv, '=');
374
for (i = 0; i < num_args; ++i) {
375
if(args[i].long_name) {
376
int len = strlen(args[i].long_name);
382
if (strncmp (args[i].long_name, p, p_len) == 0) {
390
} else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
402
if (current == NULL) {
403
if (partial_match == 1)
406
return ARG_ERR_NO_MATCH;
411
&& current->type != arg_collect
412
&& current->type != arg_counter)
413
return ARG_ERR_NO_MATCH;
414
switch(current->type){
418
if(sscanf(goptarg + 1, "%d", &tmp) != 1)
419
return ARG_ERR_BAD_ARG;
420
*(int*)current->value = tmp;
425
*(char**)current->value = goptarg + 1;
430
return add_string((getarg_strings*)current->value, goptarg + 1);
433
case arg_negative_flag:
435
int *flag = current->value;
436
if(*goptarg == '\0' ||
437
strcmp(goptarg + 1, "yes") == 0 ||
438
strcmp(goptarg + 1, "true") == 0){
441
} else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) {
443
*flag = random() & 1;
451
return ARG_ERR_BAD_ARG;
457
if (*goptarg == '\0')
459
else if(sscanf(goptarg + 1, "%d", &val) != 1)
460
return ARG_ERR_BAD_ARG;
461
*(int *)current->value += val;
467
if(sscanf(goptarg + 1, "%lf", &tmp) != 1)
468
return ARG_ERR_BAD_ARG;
469
*(double*)current->value = tmp;
473
struct getarg_collect_info *c = current->value;
474
int o = argv - rargv[*goptind];
475
return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data);
483
return ARG_ERR_NO_MATCH;
487
arg_match_short (struct getargs *args, size_t num_args,
488
char *argv, int argc, char **rargv, int *goptind)
492
for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) {
493
for(k = 0; k < num_args; k++) {
496
if(args[k].short_name == 0)
498
if(argv[j] == args[k].short_name) {
499
if(args[k].type == arg_flag) {
500
*(int*)args[k].value = 1;
503
if(args[k].type == arg_negative_flag) {
504
*(int*)args[k].value = 0;
507
if(args[k].type == arg_counter) {
508
++*(int *)args[k].value;
511
if(args[k].type == arg_collect) {
512
struct getarg_collect_info *c = args[k].value;
514
if((*c->func)(TRUE, argc, rargv, goptind, &j, c->data))
515
return ARG_ERR_BAD_ARG;
520
goptarg = &argv[j + 1];
523
goptarg = rargv[*goptind];
525
if(goptarg == NULL) {
527
return ARG_ERR_NO_ARG;
529
if(args[k].type == arg_integer) {
531
if(sscanf(goptarg, "%d", &tmp) != 1)
532
return ARG_ERR_BAD_ARG;
533
*(int*)args[k].value = tmp;
535
} else if(args[k].type == arg_string) {
536
*(char**)args[k].value = goptarg;
538
} else if(args[k].type == arg_strings) {
539
return add_string((getarg_strings*)args[k].value, goptarg);
540
} else if(args[k].type == arg_double) {
542
if(sscanf(goptarg, "%lf", &tmp) != 1)
543
return ARG_ERR_BAD_ARG;
544
*(double*)args[k].value = tmp;
547
return ARG_ERR_BAD_ARG;
551
return ARG_ERR_NO_MATCH;
556
int ROKEN_LIB_FUNCTION
557
getarg(struct getargs *args, size_t num_args,
558
int argc, char **argv, int *goptind)
563
#if defined(HAVE_SRANDOMDEV)
565
#elif defined(HAVE_RANDOM)
571
for(i = *goptind; i < argc; i++) {
572
if(argv[i][0] != '-')
574
if(argv[i][1] == '-'){
579
ret = arg_match_long (args, num_args, argv[i] + 2,
582
ret = arg_match_short (args, num_args, argv[i],
592
void ROKEN_LIB_FUNCTION
593
free_getarg_strings (getarg_strings *s)
605
struct getargs args[] = {
606
{ NULL, '1', arg_flag, &flag1, "one", NULL },
607
{ NULL, '2', arg_flag, &flag2, "two", NULL },
608
{ "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
609
{ "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
610
{ "baz", 'x', arg_string, &baz_string, "baz", "name" },
613
int main(int argc, char **argv)
616
while(getarg(args, 5, argc, argv, &goptind))
617
printf("Bad arg: %s\n", argv[goptind]);
618
printf("flag1 = %d\n", flag1);
619
printf("flag2 = %d\n", flag2);
620
printf("foo_flag = %d\n", foo_flag);
621
printf("bar_int = %d\n", bar_int);
622
printf("baz_flag = %s\n", baz_string);
623
arg_printusage (args, 5, argv[0], "nothing here");