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
36 RCSID("$Id: getarg.c,v 1.46 2002/08/20 16:23:07 joda Exp $");
45 #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
48 print_arg (char *string
, size_t len
, int mdoc
, int longp
, struct getargs
*arg
)
54 if (ISFLAG(*arg
) || (!longp
&& arg
->type
== arg_counter
))
59 strlcat(string
, "= Ns", len
);
60 strlcat(string
, " Ar ", len
);
63 strlcat (string
, "=", len
);
65 strlcat (string
, " ", len
);
70 else if (arg
->type
== arg_integer
|| arg
->type
== arg_counter
)
72 else if (arg
->type
== arg_string
)
74 else if (arg
->type
== arg_strings
)
76 else if (arg
->type
== arg_double
)
81 strlcat(string
, s
, len
);
86 mandoc_template(struct getargs
*args
,
89 const char *extra_string
)
92 char timestr
[64], cmd
[64];
97 printf(".\\\" Things to fix:\n");
98 printf(".\\\" * correct section, and operating system\n");
99 printf(".\\\" * remove Op from mandatory flags\n");
100 printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
103 strftime(timestr
, sizeof(timestr
), "%B %e, %Y", localtime(&t
));
104 printf(".Dd %s\n", timestr
);
105 p
= strrchr(progname
, '/');
106 if(p
) p
++; else p
= progname
;
107 strlcpy(cmd
, p
, sizeof(cmd
));
110 printf(".Dt %s SECTION\n", cmd
);
111 printf(".Os OPERATING_SYSTEM\n");
112 printf(".Sh NAME\n");
113 printf(".Nm %s\n", p
);
115 printf("in search of a description\n");
116 printf(".Sh SYNOPSIS\n");
118 for(i
= 0; i
< num_args
; i
++){
119 /* we seem to hit a limit on number of arguments if doing
120 short and long flags with arguments -- split on two lines */
121 if(ISFLAG(args
[i
]) ||
122 args
[i
].short_name
== 0 || args
[i
].long_name
== NULL
) {
125 if(args
[i
].short_name
) {
126 print_arg(buf
, sizeof(buf
), 1, 0, args
+ i
);
127 printf("Fl %c%s", args
[i
].short_name
, buf
);
128 if(args
[i
].long_name
)
131 if(args
[i
].long_name
) {
132 print_arg(buf
, sizeof(buf
), 1, 1, args
+ i
);
134 args
[i
].type
== arg_negative_flag
? "no-" : "",
135 args
[i
].long_name
, buf
);
139 print_arg(buf
, sizeof(buf
), 1, 0, args
+ i
);
140 printf(".Oo Fl %c%s \\*(Ba Xo\n", args
[i
].short_name
, buf
);
141 print_arg(buf
, sizeof(buf
), 1, 1, args
+ i
);
142 printf(".Fl -%s%s\n.Xc\n.Oc\n", args
[i
].long_name
, buf
);
145 if(args[i].type == arg_strings)
146 fprintf (stderr, "...");
149 if (extra_string
&& *extra_string
)
150 printf (".Ar %s\n", extra_string
);
151 printf(".Sh DESCRIPTION\n");
152 printf("Supported options:\n");
153 printf(".Bl -tag -width Ds\n");
154 for(i
= 0; i
< num_args
; i
++){
156 if(args
[i
].short_name
){
157 printf(".Fl %c", args
[i
].short_name
);
158 print_arg(buf
, sizeof(buf
), 1, 0, args
+ i
);
160 if(args
[i
].long_name
)
164 if(args
[i
].long_name
){
166 args
[i
].type
== arg_negative_flag
? "no-" : "",
168 print_arg(buf
, sizeof(buf
), 1, 1, args
+ i
);
173 printf("%s\n", args
[i
].help
);
175 if(args[i].type == arg_strings)
176 fprintf (stderr, "...");
180 printf(".\\\".Sh ENVIRONMENT\n");
181 printf(".\\\".Sh FILES\n");
182 printf(".\\\".Sh EXAMPLES\n");
183 printf(".\\\".Sh DIAGNOSTICS\n");
184 printf(".\\\".Sh SEE ALSO\n");
185 printf(".\\\".Sh STANDARDS\n");
186 printf(".\\\".Sh HISTORY\n");
187 printf(".\\\".Sh AUTHORS\n");
188 printf(".\\\".Sh BUGS\n");
192 check_column(FILE *f
, int col
, int len
, int columns
)
194 if(col
+ len
> columns
) {
196 col
= fprintf(f
, " ");
202 arg_printusage (struct getargs
*args
,
204 const char *progname
,
205 const char *extra_string
)
210 int col
= 0, columns
;
213 if (progname
== NULL
)
214 progname
= getprogname();
216 if(getenv("GETARGMANDOC")){
217 mandoc_template(args
, num_args
, progname
, extra_string
);
220 if(get_window_size(2, &ws
) == 0)
225 col
+= fprintf (stderr
, "Usage: %s", progname
);
227 for (i
= 0; i
< num_args
; ++i
) {
228 if(args
[i
].short_name
&& ISFLAG(args
[i
])) {
231 strlcpy(buf
, "[-", sizeof(buf
));
232 s
[0] = args
[i
].short_name
;
234 strlcat(buf
, s
, sizeof(buf
));
238 strlcat(buf
, "]", sizeof(buf
));
239 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
240 col
+= fprintf(stderr
, " %s", buf
);
243 for (i
= 0; i
< num_args
; ++i
) {
246 if (args
[i
].long_name
) {
248 strlcat(buf
, "[--", sizeof(buf
));
250 if(args
[i
].type
== arg_negative_flag
) {
251 strlcat(buf
, "no-", sizeof(buf
));
254 strlcat(buf
, args
[i
].long_name
, sizeof(buf
));
255 len
+= strlen(args
[i
].long_name
);
256 len
+= print_arg(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
258 strlcat(buf
, "]", sizeof(buf
));
259 if(args
[i
].type
== arg_strings
)
260 strlcat(buf
, "...", sizeof(buf
));
261 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
262 col
+= fprintf(stderr
, " %s", buf
);
264 if (args
[i
].short_name
&& !ISFLAG(args
[i
])) {
265 snprintf(buf
, sizeof(buf
), "[-%c", args
[i
].short_name
);
267 len
+= print_arg(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
269 strlcat(buf
, "]", sizeof(buf
));
270 if(args
[i
].type
== arg_strings
)
271 strlcat(buf
, "...", sizeof(buf
));
272 col
= check_column(stderr
, col
, strlen(buf
) + 1, columns
);
273 col
+= fprintf(stderr
, " %s", buf
);
275 if (args
[i
].long_name
&& args
[i
].short_name
)
277 max_len
= max(max_len
, len
);
280 col
= check_column(stderr
, col
, strlen(extra_string
) + 1, columns
);
281 fprintf (stderr
, " %s\n", extra_string
);
283 fprintf (stderr
, "\n");
284 for (i
= 0; i
< num_args
; ++i
) {
288 if (args
[i
].short_name
) {
289 count
+= fprintf (stderr
, "-%c", args
[i
].short_name
);
290 print_arg (buf
, sizeof(buf
), 0, 0, &args
[i
]);
291 count
+= fprintf(stderr
, "%s", buf
);
293 if (args
[i
].short_name
&& args
[i
].long_name
)
294 count
+= fprintf (stderr
, ", ");
295 if (args
[i
].long_name
) {
296 count
+= fprintf (stderr
, "--");
297 if (args
[i
].type
== arg_negative_flag
)
298 count
+= fprintf (stderr
, "no-");
299 count
+= fprintf (stderr
, "%s", args
[i
].long_name
);
300 print_arg (buf
, sizeof(buf
), 0, 1, &args
[i
]);
301 count
+= fprintf(stderr
, "%s", buf
);
303 while(count
++ <= max_len
)
305 fprintf (stderr
, "%s\n", args
[i
].help
);
311 add_string(getarg_strings
*s
, char *value
)
313 s
->strings
= realloc(s
->strings
, (s
->num_strings
+ 1) * sizeof(*s
->strings
));
314 s
->strings
[s
->num_strings
] = value
;
319 arg_match_long(struct getargs
*args
, size_t num_args
,
320 char *argv
, int argc
, char **rargv
, int *goptind
)
323 char *goptarg
= NULL
;
325 int partial_match
= 0;
326 struct getargs
*partial
= NULL
;
327 struct getargs
*current
= NULL
;
332 argv_len
= strlen(argv
);
333 p
= strchr (argv
, '=');
337 for (i
= 0; i
< num_args
; ++i
) {
338 if(args
[i
].long_name
) {
339 int len
= strlen(args
[i
].long_name
);
345 if (strncmp (args
[i
].long_name
, p
, p_len
) == 0) {
353 } else if (ISFLAG(args
[i
]) && strncmp (p
, "no-", 3) == 0) {
365 if (current
== NULL
) {
366 if (partial_match
== 1)
369 return ARG_ERR_NO_MATCH
;
374 && current
->type
!= arg_collect
375 && current
->type
!= arg_counter
)
376 return ARG_ERR_NO_MATCH
;
377 switch(current
->type
){
381 if(sscanf(goptarg
+ 1, "%d", &tmp
) != 1)
382 return ARG_ERR_BAD_ARG
;
383 *(int*)current
->value
= tmp
;
388 *(char**)current
->value
= goptarg
+ 1;
393 add_string((getarg_strings
*)current
->value
, goptarg
+ 1);
397 case arg_negative_flag
:
399 int *flag
= current
->value
;
400 if(*goptarg
== '\0' ||
401 strcmp(goptarg
+ 1, "yes") == 0 ||
402 strcmp(goptarg
+ 1, "true") == 0){
405 } else if (*goptarg
&& strcmp(goptarg
+ 1, "maybe") == 0) {
407 *flag
= random() & 1;
415 return ARG_ERR_BAD_ARG
;
421 if (*goptarg
== '\0')
423 else if(sscanf(goptarg
+ 1, "%d", &val
) != 1)
424 return ARG_ERR_BAD_ARG
;
425 *(int *)current
->value
+= val
;
431 if(sscanf(goptarg
+ 1, "%lf", &tmp
) != 1)
432 return ARG_ERR_BAD_ARG
;
433 *(double*)current
->value
= tmp
;
437 struct getarg_collect_info
*c
= current
->value
;
438 int o
= argv
- rargv
[*goptind
];
439 return (*c
->func
)(FALSE
, argc
, rargv
, goptind
, &o
, c
->data
);
448 arg_match_short (struct getargs
*args
, size_t num_args
,
449 char *argv
, int argc
, char **rargv
, int *goptind
)
453 for(j
= 1; j
> 0 && j
< strlen(rargv
[*goptind
]); j
++) {
454 for(k
= 0; k
< num_args
; k
++) {
457 if(args
[k
].short_name
== 0)
459 if(argv
[j
] == args
[k
].short_name
) {
460 if(args
[k
].type
== arg_flag
) {
461 *(int*)args
[k
].value
= 1;
464 if(args
[k
].type
== arg_negative_flag
) {
465 *(int*)args
[k
].value
= 0;
468 if(args
[k
].type
== arg_counter
) {
469 ++*(int *)args
[k
].value
;
472 if(args
[k
].type
== arg_collect
) {
473 struct getarg_collect_info
*c
= args
[k
].value
;
475 if((*c
->func
)(TRUE
, argc
, rargv
, goptind
, &j
, c
->data
))
476 return ARG_ERR_BAD_ARG
;
481 goptarg
= &argv
[j
+ 1];
484 goptarg
= rargv
[*goptind
];
486 if(goptarg
== NULL
) {
488 return ARG_ERR_NO_ARG
;
490 if(args
[k
].type
== arg_integer
) {
492 if(sscanf(goptarg
, "%d", &tmp
) != 1)
493 return ARG_ERR_BAD_ARG
;
494 *(int*)args
[k
].value
= tmp
;
496 } else if(args
[k
].type
== arg_string
) {
497 *(char**)args
[k
].value
= goptarg
;
499 } else if(args
[k
].type
== arg_strings
) {
500 add_string((getarg_strings
*)args
[k
].value
, goptarg
);
502 } else if(args
[k
].type
== arg_double
) {
504 if(sscanf(goptarg
, "%lf", &tmp
) != 1)
505 return ARG_ERR_BAD_ARG
;
506 *(double*)args
[k
].value
= tmp
;
509 return ARG_ERR_BAD_ARG
;
513 return ARG_ERR_NO_MATCH
;
519 getarg(struct getargs
*args
, size_t num_args
,
520 int argc
, char **argv
, int *goptind
)
525 #if defined(HAVE_SRANDOMDEV)
527 #elif defined(HAVE_RANDOM)
533 for(i
= *goptind
; i
< argc
; i
++) {
534 if(argv
[i
][0] != '-')
536 if(argv
[i
][1] == '-'){
541 ret
= arg_match_long (args
, num_args
, argv
[i
] + 2,
544 ret
= arg_match_short (args
, num_args
, argv
[i
],
555 free_getarg_strings (getarg_strings
*s
)
567 struct getargs args
[] = {
568 { NULL
, '1', arg_flag
, &flag1
, "one", NULL
},
569 { NULL
, '2', arg_flag
, &flag2
, "two", NULL
},
570 { "foo", 'f', arg_negative_flag
, &foo_flag
, "foo", NULL
},
571 { "bar", 'b', arg_integer
, &bar_int
, "bar", "seconds"},
572 { "baz", 'x', arg_string
, &baz_string
, "baz", "name" },
575 int main(int argc
, char **argv
)
578 while(getarg(args
, 5, argc
, argv
, &goptind
))
579 printf("Bad arg: %s\n", argv
[goptind
]);
580 printf("flag1 = %d\n", flag1
);
581 printf("flag2 = %d\n", flag2
);
582 printf("foo_flag = %d\n", foo_flag
);
583 printf("bar_int = %d\n", bar_int
);
584 printf("baz_flag = %s\n", baz_string
);
585 arg_printusage (args
, 5, argv
[0], "nothing here");