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
);
118 printf("in search of a description\n");
119 printf(".Sh SYNOPSIS\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
) {
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
)
134 if(args
[i
].long_name
) {
135 print_arg(buf
, sizeof(buf
), 1, 1, args
+ i
, i18n
);
137 args
[i
].type
== arg_negative_flag
? "no-" : "",
138 args
[i
].long_name
, buf
);
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
++){
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
);
163 if(args
[i
].long_name
)
167 if(args
[i
].long_name
){
169 args
[i
].type
== arg_negative_flag
? "no-" : "",
171 print_arg(buf
, sizeof(buf
), 1, 1, args
+ i
, i18n
);
176 printf("%s\n", args
[i
].help
);
178 if(args[i].type == arg_strings)
179 fprintf (stderr, "...");
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");
195 check_column(FILE *f
, int col
, int len
, int columns
)
197 if(col
+ len
> columns
) {
199 col
= fprintf(f
, " ");
205 builtin_i18n(const char *str
)
207 return rk_UNCONST(str
);
210 void ROKEN_LIB_FUNCTION
211 arg_printusage (struct getargs
*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 void ROKEN_LIB_FUNCTION
221 arg_printusage_i18n (struct getargs
*args
,
224 const char *progname
,
225 const char *extra_string
,
226 char *(i18n
)(const char *))
231 int col
= 0, columns
;
234 if (progname
== NULL
)
235 progname
= getprogname();
240 if(getenv("GETARGMANDOC")){
241 mandoc_template(args
, num_args
, progname
, extra_string
, i18n
);
244 if(get_window_size(2, &ws
) == 0)
249 col
+= fprintf (stderr
, "%s: %s", usage
, progname
);
251 for (i
= 0; i
< num_args
; ++i
) {
252 if(args
[i
].short_name
&& ISFLAG(args
[i
])) {
255 strlcpy(buf
, "[-", sizeof(buf
));
256 s
[0] = args
[i
].short_name
;
258 strlcat(buf
, s
, sizeof(buf
));
262 strlcat(buf
, "]", sizeof(buf
));
263 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
264 col
+= fprintf(stderr
, " %s", buf
);
267 for (i
= 0; i
< num_args
; ++i
) {
270 if (args
[i
].long_name
) {
272 strlcat(buf
, "[--", sizeof(buf
));
274 if(args
[i
].type
== arg_negative_flag
) {
275 strlcat(buf
, "no-", sizeof(buf
));
278 strlcat(buf
, args
[i
].long_name
, sizeof(buf
));
279 len
+= strlen(args
[i
].long_name
);
280 len
+= print_arg(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
281 0, 1, &args
[i
], i18n
);
282 strlcat(buf
, "]", sizeof(buf
));
283 if(args
[i
].type
== arg_strings
)
284 strlcat(buf
, "...", sizeof(buf
));
285 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
286 col
+= fprintf(stderr
, " %s", buf
);
288 if (args
[i
].short_name
&& !ISFLAG(args
[i
])) {
289 snprintf(buf
, sizeof(buf
), "[-%c", args
[i
].short_name
);
291 len
+= print_arg(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
292 0, 0, &args
[i
], i18n
);
293 strlcat(buf
, "]", sizeof(buf
));
294 if(args
[i
].type
== arg_strings
)
295 strlcat(buf
, "...", sizeof(buf
));
296 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
297 col
+= fprintf(stderr
, " %s", buf
);
299 if (args
[i
].long_name
&& args
[i
].short_name
)
301 max_len
= max(max_len
, len
);
304 check_column(stderr
, col
, strlen(extra_string
) + 1, columns
);
305 fprintf (stderr
, " %s\n", extra_string
);
307 fprintf (stderr
, "\n");
308 for (i
= 0; i
< num_args
; ++i
) {
312 if (args
[i
].short_name
) {
313 count
+= fprintf (stderr
, "-%c", args
[i
].short_name
);
314 print_arg (buf
, sizeof(buf
), 0, 0, &args
[i
], i18n
);
315 count
+= fprintf(stderr
, "%s", buf
);
317 if (args
[i
].short_name
&& args
[i
].long_name
)
318 count
+= fprintf (stderr
, ", ");
319 if (args
[i
].long_name
) {
320 count
+= fprintf (stderr
, "--");
321 if (args
[i
].type
== arg_negative_flag
)
322 count
+= fprintf (stderr
, "no-");
323 count
+= fprintf (stderr
, "%s", args
[i
].long_name
);
324 print_arg (buf
, sizeof(buf
), 0, 1, &args
[i
], i18n
);
325 count
+= fprintf(stderr
, "%s", buf
);
327 while(count
++ <= max_len
)
329 fprintf (stderr
, "%s\n", (*i18n
)(args
[i
].help
));
335 add_string(getarg_strings
*s
, char *value
)
339 strings
= realloc(s
->strings
, (s
->num_strings
+ 1) * sizeof(*s
->strings
));
340 if (strings
== NULL
) {
346 s
->strings
= strings
;
347 s
->strings
[s
->num_strings
] = value
;
353 arg_match_long(struct getargs
*args
, size_t num_args
,
354 char *argv
, int argc
, char **rargv
, int *goptind
)
357 char *goptarg
= NULL
;
359 int partial_match
= 0;
360 struct getargs
*partial
= NULL
;
361 struct getargs
*current
= NULL
;
366 argv_len
= strlen(argv
);
367 p
= strchr (argv
, '=');
371 for (i
= 0; i
< num_args
; ++i
) {
372 if(args
[i
].long_name
) {
373 int len
= strlen(args
[i
].long_name
);
379 if (strncmp (args
[i
].long_name
, p
, p_len
) == 0) {
387 } else if (ISFLAG(args
[i
]) && strncmp (p
, "no-", 3) == 0) {
399 if (current
== NULL
) {
400 if (partial_match
== 1)
403 return ARG_ERR_NO_MATCH
;
408 && current
->type
!= arg_collect
409 && current
->type
!= arg_counter
)
410 return ARG_ERR_NO_MATCH
;
411 switch(current
->type
){
415 if(sscanf(goptarg
+ 1, "%d", &tmp
) != 1)
416 return ARG_ERR_BAD_ARG
;
417 *(int*)current
->value
= tmp
;
422 *(char**)current
->value
= goptarg
+ 1;
427 return add_string((getarg_strings
*)current
->value
, goptarg
+ 1);
430 case arg_negative_flag
:
432 int *flag
= current
->value
;
433 if(*goptarg
== '\0' ||
434 strcmp(goptarg
+ 1, "yes") == 0 ||
435 strcmp(goptarg
+ 1, "true") == 0){
438 } else if (*goptarg
&& strcmp(goptarg
+ 1, "maybe") == 0) {
440 *flag
= random() & 1;
448 return ARG_ERR_BAD_ARG
;
454 if (*goptarg
== '\0')
456 else if(sscanf(goptarg
+ 1, "%d", &val
) != 1)
457 return ARG_ERR_BAD_ARG
;
458 *(int *)current
->value
+= val
;
464 if(sscanf(goptarg
+ 1, "%lf", &tmp
) != 1)
465 return ARG_ERR_BAD_ARG
;
466 *(double*)current
->value
= tmp
;
470 struct getarg_collect_info
*c
= current
->value
;
471 int o
= argv
- rargv
[*goptind
];
472 return (*c
->func
)(FALSE
, argc
, rargv
, goptind
, &o
, c
->data
);
480 return ARG_ERR_NO_MATCH
;
484 arg_match_short (struct getargs
*args
, size_t num_args
,
485 char *argv
, int argc
, char **rargv
, int *goptind
)
489 for(j
= 1; j
> 0 && j
< strlen(rargv
[*goptind
]); j
++) {
490 for(k
= 0; k
< num_args
; k
++) {
493 if(args
[k
].short_name
== 0)
495 if(argv
[j
] == args
[k
].short_name
) {
496 if(args
[k
].type
== arg_flag
) {
497 *(int*)args
[k
].value
= 1;
500 if(args
[k
].type
== arg_negative_flag
) {
501 *(int*)args
[k
].value
= 0;
504 if(args
[k
].type
== arg_counter
) {
505 ++*(int *)args
[k
].value
;
508 if(args
[k
].type
== arg_collect
) {
509 struct getarg_collect_info
*c
= args
[k
].value
;
511 if((*c
->func
)(TRUE
, argc
, rargv
, goptind
, &j
, c
->data
))
512 return ARG_ERR_BAD_ARG
;
517 goptarg
= &argv
[j
+ 1];
520 goptarg
= rargv
[*goptind
];
522 if(goptarg
== NULL
) {
524 return ARG_ERR_NO_ARG
;
526 if(args
[k
].type
== arg_integer
) {
528 if(sscanf(goptarg
, "%d", &tmp
) != 1)
529 return ARG_ERR_BAD_ARG
;
530 *(int*)args
[k
].value
= tmp
;
532 } else if(args
[k
].type
== arg_string
) {
533 *(char**)args
[k
].value
= goptarg
;
535 } else if(args
[k
].type
== arg_strings
) {
536 return add_string((getarg_strings
*)args
[k
].value
, goptarg
);
537 } else if(args
[k
].type
== arg_double
) {
539 if(sscanf(goptarg
, "%lf", &tmp
) != 1)
540 return ARG_ERR_BAD_ARG
;
541 *(double*)args
[k
].value
= tmp
;
544 return ARG_ERR_BAD_ARG
;
548 return ARG_ERR_NO_MATCH
;
553 int ROKEN_LIB_FUNCTION
554 getarg(struct getargs
*args
, size_t num_args
,
555 int argc
, char **argv
, int *goptind
)
560 #if defined(HAVE_SRANDOMDEV)
562 #elif defined(HAVE_RANDOM)
568 for(i
= *goptind
; i
< argc
; i
++) {
569 if(argv
[i
][0] != '-')
571 if(argv
[i
][1] == '-'){
576 ret
= arg_match_long (args
, num_args
, argv
[i
] + 2,
579 ret
= arg_match_short (args
, num_args
, argv
[i
],
589 void ROKEN_LIB_FUNCTION
590 free_getarg_strings (getarg_strings
*s
)
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" },
610 int main(int argc
, char **argv
)
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");