diff options
author | jsing <> | 2014-08-27 14:59:44 +0000 |
---|---|---|
committer | jsing <> | 2014-08-27 14:59:44 +0000 |
commit | 1fbc8b7e3211e362b5507749d1c5b692dcb8c64f (patch) | |
tree | d84799ea674d39450c109ebc4c3cb39140e5201e /src/usr.bin/openssl/apps.c | |
parent | 10e31157bd2d409218ed09f4b52af2de773a8a0f (diff) | |
download | openbsd-1fbc8b7e3211e362b5507749d1c5b692dcb8c64f.tar.gz openbsd-1fbc8b7e3211e362b5507749d1c5b692dcb8c64f.tar.bz2 openbsd-1fbc8b7e3211e362b5507749d1c5b692dcb8c64f.zip |
Implement table-driven option parsing that allows an application to
specify what its valid options are and where it wants them to be stored.
This also allows for usage to be generated, almost for free, ensuring
that the options and usage are automatically kept in sync.
This will allow for a single option parsing implementation, rather than the
current one-hand-rolled-option-parsing-and-random-usage-implementation per
application.
As a starting point, port the openssl(1) rand application to the new option
parsing and usage (along with associated code clean up).
With input from doug@.
ok bcook@ doug@
Diffstat (limited to '')
-rw-r--r-- | src/usr.bin/openssl/apps.c | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/src/usr.bin/openssl/apps.c b/src/usr.bin/openssl/apps.c index ac1c5107f1..e5eda3f53b 100644 --- a/src/usr.bin/openssl/apps.c +++ b/src/usr.bin/openssl/apps.c | |||
@@ -1,4 +1,19 @@ | |||
1 | /* $OpenBSD: apps.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */ | 1 | /* $OpenBSD: apps.c,v 1.2 2014/08/27 14:59:44 jsing Exp $ */ |
2 | /* | ||
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 17 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 18 | * All rights reserved. |
4 | * | 19 | * |
@@ -2218,3 +2233,83 @@ app_isdir(const char *name) | |||
2218 | return S_ISDIR(st.st_mode); | 2233 | return S_ISDIR(st.st_mode); |
2219 | return -1; | 2234 | return -1; |
2220 | } | 2235 | } |
2236 | |||
2237 | void | ||
2238 | options_usage(struct option *opts) | ||
2239 | { | ||
2240 | const char *argname; | ||
2241 | char buf[32]; | ||
2242 | int i; | ||
2243 | |||
2244 | for (i = 0; opts[i].name != NULL; i++) { | ||
2245 | if (opts[i].desc == NULL) | ||
2246 | continue; | ||
2247 | argname = (opts[i].argname != NULL) ? opts[i].argname : ""; | ||
2248 | snprintf(buf, sizeof(buf), "-%s %s", opts[i].name, argname); | ||
2249 | fprintf(stderr, " %-*s %s\n", 14, buf, opts[i].desc); | ||
2250 | } | ||
2251 | } | ||
2252 | |||
2253 | int | ||
2254 | options_parse(int argc, char **argv, struct option *opts, char **unnamed) | ||
2255 | { | ||
2256 | struct option *opt; | ||
2257 | char *arg, *p; | ||
2258 | int i, j; | ||
2259 | |||
2260 | for (i = 1; i < argc; i++) { | ||
2261 | p = arg = argv[i]; | ||
2262 | |||
2263 | if (*p++ != '-') { | ||
2264 | if (unnamed == NULL) | ||
2265 | goto unknown; | ||
2266 | *unnamed = arg; | ||
2267 | continue; | ||
2268 | } | ||
2269 | if (*p == '\0') | ||
2270 | goto unknown; | ||
2271 | |||
2272 | for (j = 0; opts[j].name != NULL; j++) { | ||
2273 | opt = &opts[j]; | ||
2274 | if (strcmp(p, opt->name) != 0) | ||
2275 | continue; | ||
2276 | |||
2277 | switch (opt->type) { | ||
2278 | case OPTION_ARG: | ||
2279 | if (++i >= argc) { | ||
2280 | fprintf(stderr, | ||
2281 | "missing %s argument for -%s\n", | ||
2282 | opt->argname, opt->name); | ||
2283 | return (1); | ||
2284 | } | ||
2285 | *opt->opt.arg = argv[i]; | ||
2286 | break; | ||
2287 | |||
2288 | case OPTION_FLAG: | ||
2289 | *opt->opt.flag = 1; | ||
2290 | break; | ||
2291 | |||
2292 | case OPTION_VALUE: | ||
2293 | *opt->opt.value = opt->value; | ||
2294 | break; | ||
2295 | |||
2296 | default: | ||
2297 | fprintf(stderr, | ||
2298 | "option %s - unknown type %i\n", | ||
2299 | opt->name, opt->type); | ||
2300 | return (1); | ||
2301 | } | ||
2302 | |||
2303 | break; | ||
2304 | } | ||
2305 | |||
2306 | if (opts[j].name == NULL) | ||
2307 | goto unknown; | ||
2308 | } | ||
2309 | |||
2310 | return (0); | ||
2311 | |||
2312 | unknown: | ||
2313 | fprintf(stderr, "unknown option '%s'\n", arg); | ||
2314 | return (1); | ||
2315 | } | ||