Add "sections" to -help output
[openssl.git] / apps / errstr.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "apps.h"
14 #include "progs.h"
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/ssl.h>
18
19 typedef enum OPTION_choice {
20     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP
21 } OPTION_CHOICE;
22
23 const OPTIONS errstr_options[] = {
24     {OPT_HELP_STR, 1, '-', "Usage: %s [options] errnum...\n"},
25     {OPT_HELP_STR, 1, '-', "  errnum  Error number\n"},
26
27     OPT_SECTION("General"),
28     {"help", OPT_HELP, '-', "Display this summary"},
29     {NULL}
30 };
31
32 int errstr_main(int argc, char **argv)
33 {
34     OPTION_CHOICE o;
35     char buf[256], *prog;
36     int ret = 1;
37     unsigned long l;
38
39     prog = opt_init(argc, argv, errstr_options);
40     while ((o = opt_next()) != OPT_EOF) {
41         switch (o) {
42         case OPT_EOF:
43         case OPT_ERR:
44             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
45             goto end;
46         case OPT_HELP:
47             opt_help(errstr_options);
48             ret = 0;
49             goto end;
50         }
51     }
52
53     ret = 0;
54     for (argv = opt_rest(); *argv; argv++) {
55         if (sscanf(*argv, "%lx", &l) == 0) {
56             ret++;
57         } else {
58             /* We're not really an SSL application so this won't auto-init, but
59              * we're still interested in SSL error strings
60              */
61             OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
62                              | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
63             ERR_error_string_n(l, buf, sizeof(buf));
64             BIO_printf(bio_out, "%s\n", buf);
65         }
66     }
67  end:
68     return ret;
69 }