diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2014-08-27 14:59:45 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2014-08-27 14:59:45 +0000 |
commit | 99d89c1690382ed26c445bedf4733dc2ee80f34d (patch) | |
tree | 4872bde3ce8421435e2156d18ea8a4590132b240 | |
parent | ecd54814104d0591e7c74d448a06f834b8b630e8 (diff) |
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@
-rw-r--r-- | usr.bin/openssl/apps.c | 97 | ||||
-rw-r--r-- | usr.bin/openssl/apps.h | 22 | ||||
-rw-r--r-- | usr.bin/openssl/rand.c | 149 |
3 files changed, 192 insertions, 76 deletions
diff --git a/usr.bin/openssl/apps.c b/usr.bin/openssl/apps.c index ac1c5107f11..e5eda3f53b9 100644 --- a/usr.bin/openssl/apps.c +++ b/usr.bin/openssl/apps.c @@ -1,4 +1,19 @@ -/* $OpenBSD: apps.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */ +/* $OpenBSD: apps.c,v 1.2 2014/08/27 14:59:44 jsing Exp $ */ +/* + * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -2218,3 +2233,83 @@ app_isdir(const char *name) return S_ISDIR(st.st_mode); return -1; } + +void +options_usage(struct option *opts) +{ + const char *argname; + char buf[32]; + int i; + + for (i = 0; opts[i].name != NULL; i++) { + if (opts[i].desc == NULL) + continue; + argname = (opts[i].argname != NULL) ? opts[i].argname : ""; + snprintf(buf, sizeof(buf), "-%s %s", opts[i].name, argname); + fprintf(stderr, " %-*s %s\n", 14, buf, opts[i].desc); + } +} + +int +options_parse(int argc, char **argv, struct option *opts, char **unnamed) +{ + struct option *opt; + char *arg, *p; + int i, j; + + for (i = 1; i < argc; i++) { + p = arg = argv[i]; + + if (*p++ != '-') { + if (unnamed == NULL) + goto unknown; + *unnamed = arg; + continue; + } + if (*p == '\0') + goto unknown; + + for (j = 0; opts[j].name != NULL; j++) { + opt = &opts[j]; + if (strcmp(p, opt->name) != 0) + continue; + + switch (opt->type) { + case OPTION_ARG: + if (++i >= argc) { + fprintf(stderr, + "missing %s argument for -%s\n", + opt->argname, opt->name); + return (1); + } + *opt->opt.arg = argv[i]; + break; + + case OPTION_FLAG: + *opt->opt.flag = 1; + break; + + case OPTION_VALUE: + *opt->opt.value = opt->value; + break; + + default: + fprintf(stderr, + "option %s - unknown type %i\n", + opt->name, opt->type); + return (1); + } + + break; + } + + if (opts[j].name == NULL) + goto unknown; + } + + return (0); + +unknown: + fprintf(stderr, "unknown option '%s'\n", arg); + return (1); +} diff --git a/usr.bin/openssl/apps.h b/usr.bin/openssl/apps.h index 38c5f4be8c8..64e581b969d 100644 --- a/usr.bin/openssl/apps.h +++ b/usr.bin/openssl/apps.h @@ -1,4 +1,4 @@ -/* $OpenBSD: apps.h,v 1.1 2014/08/26 17:47:24 jsing Exp $ */ +/* $OpenBSD: apps.h,v 1.2 2014/08/27 14:59:44 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -282,4 +282,24 @@ double app_tminterval (int stop, int usertime); #define OPENSSL_NO_SSL_INTERN +struct option { + const char *name; + const char *argname; + const char *desc; + enum { + OPTION_ARG, + OPTION_FLAG, + OPTION_VALUE, + } type; + union { + char **arg; + int *flag; + int *value; + } opt; + const int value; +}; + +void options_usage(struct option *opts); +int options_parse(int argc, char **argv, struct option *opts, char **unnamed); + #endif diff --git a/usr.bin/openssl/rand.c b/usr.bin/openssl/rand.c index 0800157a35d..61c7013340f 100644 --- a/usr.bin/openssl/rand.c +++ b/usr.bin/openssl/rand.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rand.c,v 1.1 2014/08/26 17:47:25 jsing Exp $ */ +/* $OpenBSD: rand.c,v 1.2 2014/08/27 14:59:44 jsing Exp $ */ /* ==================================================================== * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. * @@ -63,123 +63,124 @@ #include <openssl/err.h> #include <openssl/rand.h> -/* -out file - write to file - * -base64 - base64 encode output - * -hex - hex encode output - * num - write 'num' bytes - */ +struct { + int base64; + char *engine; + int hex; + char *outfile; +} rand_config; + +struct option rand_options[] = { + { + .name = "base64", + .desc = "Perform base64 encoding on output", + .type = OPTION_FLAG, + .opt.flag = &rand_config.base64, + }, +#ifndef OPENSSL_NO_ENGINE + { + .name = "engine", + .argname = "id", + .desc = "Use the engine specified by the given identifier", + .type = OPTION_ARG, + .opt.arg = &rand_config.engine, + }, +#endif + { + .name = "hex", + .desc = "Hexadecimal output", + .type = OPTION_FLAG, + .opt.flag = &rand_config.hex, + }, + { + .name = "out", + .argname = "file", + .desc = "Write to the given file instead of standard output", + .type = OPTION_ARG, + .opt.arg = &rand_config.outfile, + }, + {}, +}; + +static void +rand_usage() +{ + fprintf(stderr, + "usage: rand [-base64 | -hex] [-engine id] [-out file] num\n"); + options_usage(rand_options); +} int rand_main(int, char **); int rand_main(int argc, char **argv) { - int i, r, ret = 1; - int badopt; - char *outfile = NULL; - int base64 = 0; - int hex = 0; - BIO *out = NULL; + char *num_bytes = NULL; + int ret = 1; + int badopt = 0; int num = -1; -#ifndef OPENSSL_NO_ENGINE - char *engine = NULL; -#endif + int i, r; + BIO *out = NULL; - badopt = 0; - i = 0; - while (!badopt && argv[++i] != NULL) { - if (strcmp(argv[i], "-out") == 0) { - if ((argv[i + 1] != NULL) && (outfile == NULL)) - outfile = argv[++i]; - else - badopt = 1; - } -#ifndef OPENSSL_NO_ENGINE - else if (strcmp(argv[i], "-engine") == 0) { - if ((argv[i + 1] != NULL) && (engine == NULL)) - engine = argv[++i]; - else - badopt = 1; - } -#endif - else if (strcmp(argv[i], "-base64") == 0) { - if (!base64) - base64 = 1; - else - badopt = 1; - } else if (strcmp(argv[i], "-hex") == 0) { - if (!hex) - hex = 1; - else - badopt = 1; - } else if (isdigit((unsigned char) argv[i][0])) { - if (num < 0) { - r = sscanf(argv[i], "%d", &num); - if (r == 0 || num < 0) - badopt = 1; - } else - badopt = 1; - } else - badopt = 1; + if (options_parse(argc, argv, rand_options, &num_bytes) != 0) { + rand_usage(); + return (1); } - if (hex && base64) + if (num_bytes != NULL) { + r = sscanf(num_bytes, "%d", &num); + if (r == 0 || num < 0) + badopt = 1; + } else badopt = 1; - if (num < 0) + if (rand_config.hex && rand_config.base64) badopt = 1; if (badopt) { - BIO_printf(bio_err, "Usage: rand [options] num\n"); - BIO_printf(bio_err, "where options are\n"); - BIO_printf(bio_err, "-out file - write to file\n"); -#ifndef OPENSSL_NO_ENGINE - BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n"); -#endif - BIO_printf(bio_err, "-base64 - base64 encode output\n"); - BIO_printf(bio_err, "-hex - hex encode output\n"); + rand_usage(); goto err; } + #ifndef OPENSSL_NO_ENGINE - setup_engine(bio_err, engine, 0); + setup_engine(bio_err, rand_config.engine, 0); #endif out = BIO_new(BIO_s_file()); if (out == NULL) goto err; - if (outfile != NULL) - r = BIO_write_filename(out, outfile); - else { + if (rand_config.outfile != NULL) + r = BIO_write_filename(out, rand_config.outfile); + else r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); - } if (r <= 0) goto err; - - if (base64) { + if (rand_config.base64) { BIO *b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) goto err; out = BIO_push(b64, out); } + while (num > 0) { unsigned char buf[4096]; int chunk; chunk = num; if (chunk > (int) sizeof(buf)) - chunk = sizeof buf; + chunk = sizeof(buf); r = RAND_bytes(buf, chunk); if (r <= 0) goto err; - if (!hex) - BIO_write(out, buf, chunk); - else { + if (rand_config.hex) { for (i = 0; i < chunk; i++) BIO_printf(out, "%02x", buf[i]); - } + } else + BIO_write(out, buf, chunk); num -= chunk; } - if (hex) + + if (rand_config.hex) BIO_puts(out, "\n"); (void) BIO_flush(out); |