2 * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
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
11 * This file is also used by the test suite. Do not #include "apps.h".
15 #include "app_libctx.h"
16 #include "internal/nelem.h"
17 #include "internal/numbers.h"
19 #if !defined(OPENSSL_SYS_MSDOS)
27 #include <openssl/err.h>
28 #include <openssl/bio.h>
29 #include <openssl/x509v3.h>
31 #define MAX_OPT_HELP_WIDTH 30
32 const char OPT_HELP_STR[] = "-H";
33 const char OPT_MORE_STR[] = "-M";
34 const char OPT_SECTION_STR[] = "-S";
35 const char OPT_PARAM_STR[] = "-P";
44 static const char *unknown_name;
45 static const OPTIONS *unknown;
46 static const OPTIONS *opts;
50 * Return the simple name of the program; removing various platform gunk.
52 #if defined(OPENSSL_SYS_WIN32)
54 const char *opt_path_end(const char *filename)
58 /* find the last '/', '\' or ':' */
59 for (p = filename + strlen(filename); --p > filename; )
60 if (*p == '/' || *p == '\\' || *p == ':') {
67 char *opt_progname(const char *argv0)
73 p = opt_path_end(argv0);
75 /* Strip off trailing nonsense. */
78 (strcmp(&p[n - 4], ".exe") == 0 || strcmp(&p[n - 4], ".EXE") == 0))
81 /* Copy over the name, in lowercase. */
82 if (n > sizeof(prog) - 1)
84 for (q = prog, i = 0; i < n; i++, p++)
85 *q++ = tolower((unsigned char)*p);
90 #elif defined(OPENSSL_SYS_VMS)
92 const char *opt_path_end(const char *filename)
96 /* Find last special character sys:[foo.bar]openssl */
97 for (p = filename + strlen(filename); --p > filename;)
98 if (*p == ':' || *p == ']' || *p == '>') {
105 char *opt_progname(const char *argv0)
109 /* Find last special character sys:[foo.bar]openssl */
110 p = opt_path_end(argv0);
113 strncpy(prog, p, sizeof(prog) - 1);
114 prog[sizeof(prog) - 1] = '\0';
115 if (q != NULL && q - p < sizeof(prog))
122 const char *opt_path_end(const char *filename)
126 /* Could use strchr, but this is like the ones above. */
127 for (p = filename + strlen(filename); --p > filename;)
135 char *opt_progname(const char *argv0)
139 p = opt_path_end(argv0);
141 strncpy(prog, p, sizeof(prog) - 1);
142 prog[sizeof(prog) - 1] = '\0';
147 char *opt_appname(const char *argv0)
149 size_t len = strlen(prog);
152 BIO_snprintf(prog + len, sizeof(prog) - len - 1, " %s", argv0);
156 char *opt_getprog(void)
161 /* Set up the arg parsing. */
162 char *opt_init(int ac, char **av, const OPTIONS *o)
170 /* Make sure prog name is set for usage output */
171 (void)opt_progname(argv[0]);
173 /* Check all options up until the PARAM marker (if present) */
174 for (; o->name != NULL && o->name != OPT_PARAM_STR; ++o) {
180 if (o->name == OPT_HELP_STR
181 || o->name == OPT_MORE_STR
182 || o->name == OPT_SECTION_STR)
187 /* Make sure options are legit. */
188 OPENSSL_assert(o->name[0] != '-');
189 if (o->valtype == '.')
190 OPENSSL_assert(o->retval == OPT_PARAM);
192 OPENSSL_assert(o->retval == OPT_DUP || o->retval > OPT_PARAM);
194 case 0: case '-': case '.':
195 case '/': case '<': case '>': case 'E': case 'F':
196 case 'M': case 'U': case 'f': case 'l': case 'n': case 'p': case 's':
197 case 'u': case 'c': case ':': case 'N': case 'A':
203 /* Make sure there are no duplicates. */
204 for (next = o + 1; next->name; ++next) {
206 * Some compilers inline strcmp and the assert string is too long.
208 duplicated = next->retval != OPT_DUP
209 && strcmp(o->name, next->name) == 0;
211 opt_printf_stderr("%s: Internal error: duplicate option %s\n",
213 OPENSSL_assert(!duplicated);
217 if (o->name[0] == '\0') {
218 OPENSSL_assert(unknown_name != NULL);
219 OPENSSL_assert(unknown == NULL);
221 OPENSSL_assert(unknown->valtype == 0 || unknown->valtype == '-');
227 static OPT_PAIR formats[] = {
228 {"pem", OPT_FMT_PEM},
229 {"der", OPT_FMT_DER},
230 {"b64", OPT_FMT_B64},
231 {"pkcs12", OPT_FMT_PKCS12},
232 {"smime", OPT_FMT_SMIME},
233 {"engine", OPT_FMT_ENGINE},
234 {"msblob", OPT_FMT_MSBLOB},
235 {"nss", OPT_FMT_NSS},
236 {"text", OPT_FMT_TEXT},
237 {"http", OPT_FMT_HTTP},
238 {"pvk", OPT_FMT_PVK},
242 void opt_set_unknown_name(const char *name)
247 /* Print an error message about a failed format parse. */
248 static int opt_format_error(const char *s, unsigned long flags)
252 opt_printf_stderr("%s: Bad format \"%s\"; must be one of: ", prog, s);
253 for (ap = formats; ap->name; ap++)
254 if (flags & ap->retval)
255 opt_printf_stderr(" %s", ap->name);
256 opt_printf_stderr("\n");
261 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
262 int opt_format(const char *s, unsigned long flags, int *result)
266 opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
271 || strcmp(s, "B64") == 0 || strcmp(s, "b64") == 0
272 || strcmp(s, "BASE64") == 0 || strcmp(s, "base64") == 0 ) {
273 if ((flags & OPT_FMT_B64) == 0)
274 return opt_format_error(s, flags);
275 *result = FORMAT_BASE64;
282 if ((flags & OPT_FMT_DER) == 0)
283 return opt_format_error(s, flags);
284 *result = FORMAT_ASN1;
288 if ((flags & OPT_FMT_TEXT) == 0)
289 return opt_format_error(s, flags);
290 *result = FORMAT_TEXT;
294 if ((flags & OPT_FMT_NSS) == 0)
295 return opt_format_error(s, flags);
296 if (strcmp(s, "NSS") != 0 && strcmp(s, "nss") != 0)
297 return opt_format_error(s, flags);
298 *result = FORMAT_NSS;
302 if ((flags & OPT_FMT_SMIME) == 0)
303 return opt_format_error(s, flags);
304 *result = FORMAT_SMIME;
308 if ((flags & OPT_FMT_MSBLOB) == 0)
309 return opt_format_error(s, flags);
310 *result = FORMAT_MSBLOB;
314 if ((flags & OPT_FMT_ENGINE) == 0)
315 return opt_format_error(s, flags);
316 *result = FORMAT_ENGINE;
320 if ((flags & OPT_FMT_HTTP) == 0)
321 return opt_format_error(s, flags);
322 *result = FORMAT_HTTP;
325 if ((flags & OPT_FMT_PKCS12) == 0)
326 return opt_format_error(s, flags);
327 *result = FORMAT_PKCS12;
331 if (s[1] == '\0' || strcmp(s, "PEM") == 0 || strcmp(s, "pem") == 0) {
332 if ((flags & OPT_FMT_PEM) == 0)
333 return opt_format_error(s, flags);
334 *result = FORMAT_PEM;
335 } else if (strcmp(s, "PVK") == 0 || strcmp(s, "pvk") == 0) {
336 if ((flags & OPT_FMT_PVK) == 0)
337 return opt_format_error(s, flags);
338 *result = FORMAT_PVK;
339 } else if (strcmp(s, "P12") == 0 || strcmp(s, "p12") == 0
340 || strcmp(s, "PKCS12") == 0 || strcmp(s, "pkcs12") == 0) {
341 if ((flags & OPT_FMT_PKCS12) == 0)
342 return opt_format_error(s, flags);
343 *result = FORMAT_PKCS12;
345 opt_printf_stderr("%s: Bad format \"%s\"\n", prog, s);
353 /* Return string representing the given format. */
354 static const char *format2str(int format)
358 return "(undefined)";
382 /* Print an error message about unsuitable/unsupported format requested. */
383 void print_format_error(int format, unsigned long flags)
385 (void)opt_format_error(format2str(format), flags);
389 * Parse a cipher name, put it in *cipherp after freeing what was there, if
390 * cipherp is not NULL. Return 0 on failure, else 1.
392 int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp)
397 if ((c = EVP_CIPHER_fetch(app_get0_libctx(), name,
398 app_get0_propq())) != NULL
399 || (opt_legacy_okay()
400 && (c = (EVP_CIPHER *)EVP_get_cipherbyname(name)) != NULL)) {
402 if (cipherp != NULL) {
403 EVP_CIPHER_free(*cipherp);
410 ERR_clear_last_mark();
414 int opt_cipher_any(const char *name, EVP_CIPHER **cipherp)
420 if ((ret = opt_cipher_silent(name, cipherp)) == 0)
421 opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog, name);
425 int opt_cipher(const char *name, EVP_CIPHER **cipherp)
428 unsigned long int flags;
429 EVP_CIPHER *c = NULL;
433 if (opt_cipher_any(name, &c)) {
434 mode = EVP_CIPHER_get_mode(c);
435 flags = EVP_CIPHER_get_flags(c);
436 if (mode == EVP_CIPH_XTS_MODE) {
437 opt_printf_stderr("%s XTS ciphers not supported\n", prog);
438 } else if ((flags & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
439 opt_printf_stderr("%s: AEAD ciphers not supported\n", prog);
450 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
452 int opt_md_silent(const char *name, EVP_MD **mdp)
457 if ((md = EVP_MD_fetch(app_get0_libctx(), name, app_get0_propq())) != NULL
458 || (opt_legacy_okay()
459 && (md = (EVP_MD *)EVP_get_digestbyname(name)) != NULL)) {
469 ERR_clear_last_mark();
473 int opt_md(const char *name, EVP_MD **mdp)
479 if ((ret = opt_md_silent(name, mdp)) == 0)
480 opt_printf_stderr("%s: Unknown option or message digest: %s\n",
485 int opt_check_md(const char *name)
487 if (opt_md(name, NULL))
493 /* Look through a list of name/value pairs. */
494 int opt_pair(const char *name, const OPT_PAIR* pairs, int *result)
498 for (pp = pairs; pp->name; pp++)
499 if (strcmp(pp->name, name) == 0) {
500 *result = pp->retval;
503 opt_printf_stderr("%s: Value must be one of:\n", prog);
504 for (pp = pairs; pp->name; pp++)
505 opt_printf_stderr("\t%s\n", pp->name);
509 /* Look through a list of valid names */
510 int opt_string(const char *name, const char **options)
514 for (p = options; *p != NULL; p++)
515 if (strcmp(*p, name) == 0)
517 opt_printf_stderr("%s: Value must be one of:\n", prog);
518 for (p = options; *p != NULL; p++)
519 opt_printf_stderr("\t%s\n", *p);
523 /* Parse an int, put it into *result; return 0 on failure, else 1. */
524 int opt_int(const char *value, int *result)
528 if (!opt_long(value, &l))
532 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
539 /* Parse and return an integer, assuming range has been checked before. */
540 int opt_int_arg(void)
544 (void)opt_int(arg, &result);
548 static void opt_number_error(const char *v)
551 struct strstr_pair_st {
555 {"0x", "a hexadecimal"},
556 {"0X", "a hexadecimal"},
560 for (i = 0; i < OSSL_NELEM(b); i++) {
561 if (strncmp(v, b[i].prefix, strlen(b[i].prefix)) == 0) {
562 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
567 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog, v);
571 /* Parse a long, put it into *result; return 0 on failure, else 1. */
572 int opt_long(const char *value, long *result)
579 l = strtol(value, &endp, 0);
582 || ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
583 || (l == 0 && errno != 0)) {
584 opt_number_error(value);
593 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
594 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
595 !defined(OPENSSL_NO_INTTYPES_H)
597 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
598 int opt_intmax(const char *value, ossl_intmax_t *result)
605 m = strtoimax(value, &endp, 0);
608 || ((m == INTMAX_MAX || m == INTMAX_MIN)
610 || (m == 0 && errno != 0)) {
611 opt_number_error(value);
615 /* Ensure that the value in |m| is never too big for |*result| */
616 if (sizeof(m) > sizeof(*result)
617 && (m < OSSL_INTMAX_MIN || m > OSSL_INTMAX_MAX)) {
618 opt_number_error(value);
621 *result = (ossl_intmax_t)m;
626 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
627 int opt_uintmax(const char *value, ossl_uintmax_t *result)
634 m = strtoumax(value, &endp, 0);
637 || (m == UINTMAX_MAX && errno == ERANGE)
638 || (m == 0 && errno != 0)) {
639 opt_number_error(value);
643 /* Ensure that the value in |m| is never too big for |*result| */
644 if (sizeof(m) > sizeof(*result)
645 && m > OSSL_UINTMAX_MAX) {
646 opt_number_error(value);
649 *result = (ossl_intmax_t)m;
654 /* Fallback implementations based on long */
655 int opt_intmax(const char *value, ossl_intmax_t *result)
660 if ((ret = opt_long(value, &m)))
665 int opt_uintmax(const char *value, ossl_uintmax_t *result)
670 if ((ret = opt_ulong(value, &m)))
677 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
679 int opt_ulong(const char *value, unsigned long *result)
686 l = strtoul(value, &endptr, 0);
689 || ((l == ULONG_MAX) && errno == ERANGE)
690 || (l == 0 && errno != 0)) {
691 opt_number_error(value);
701 * We pass opt as an int but cast it to "enum range" so that all the
702 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
703 * in gcc do the right thing.
705 enum range { OPT_V_ENUM };
707 int opt_verify(int opt, X509_VERIFY_PARAM *vpm)
713 const X509_VERIFY_PARAM *vtmp;
715 OPENSSL_assert(vpm != NULL);
716 OPENSSL_assert(opt > OPT_V__FIRST);
717 OPENSSL_assert(opt < OPT_V__LAST);
719 switch ((enum range)opt) {
724 otmp = OBJ_txt2obj(opt_arg(), 0);
726 opt_printf_stderr("%s: Invalid Policy %s\n", prog, opt_arg());
729 if (!X509_VERIFY_PARAM_add0_policy(vpm, otmp)) {
730 ASN1_OBJECT_free(otmp);
731 opt_printf_stderr("%s: Internal error adding Policy %s\n",
737 /* purpose name -> purpose index */
738 i = X509_PURPOSE_get_by_sname(opt_arg());
740 opt_printf_stderr("%s: Invalid purpose %s\n", prog, opt_arg());
744 /* purpose index -> purpose object */
745 xptmp = X509_PURPOSE_get0(i);
747 /* purpose object -> purpose value */
748 i = X509_PURPOSE_get_id(xptmp);
750 if (!X509_VERIFY_PARAM_set_purpose(vpm, i)) {
751 opt_printf_stderr("%s: Internal error setting purpose %s\n",
756 case OPT_V_VERIFY_NAME:
757 vtmp = X509_VERIFY_PARAM_lookup(opt_arg());
759 opt_printf_stderr("%s: Invalid verify name %s\n",
763 X509_VERIFY_PARAM_set1(vpm, vtmp);
765 case OPT_V_VERIFY_DEPTH:
768 X509_VERIFY_PARAM_set_depth(vpm, i);
770 case OPT_V_VERIFY_AUTH_LEVEL:
773 X509_VERIFY_PARAM_set_auth_level(vpm, i);
776 if (!opt_intmax(opt_arg(), &t))
778 if (t != (time_t)t) {
779 opt_printf_stderr("%s: epoch time out of range %s\n",
783 X509_VERIFY_PARAM_set_time(vpm, (time_t)t);
785 case OPT_V_VERIFY_HOSTNAME:
786 if (!X509_VERIFY_PARAM_set1_host(vpm, opt_arg(), 0))
789 case OPT_V_VERIFY_EMAIL:
790 if (!X509_VERIFY_PARAM_set1_email(vpm, opt_arg(), 0))
793 case OPT_V_VERIFY_IP:
794 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm, opt_arg()))
797 case OPT_V_IGNORE_CRITICAL:
798 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_IGNORE_CRITICAL);
800 case OPT_V_ISSUER_CHECKS:
801 /* NOP, deprecated */
803 case OPT_V_CRL_CHECK:
804 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CRL_CHECK);
806 case OPT_V_CRL_CHECK_ALL:
807 X509_VERIFY_PARAM_set_flags(vpm,
808 X509_V_FLAG_CRL_CHECK |
809 X509_V_FLAG_CRL_CHECK_ALL);
811 case OPT_V_POLICY_CHECK:
812 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_POLICY_CHECK);
814 case OPT_V_EXPLICIT_POLICY:
815 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXPLICIT_POLICY);
817 case OPT_V_INHIBIT_ANY:
818 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_ANY);
820 case OPT_V_INHIBIT_MAP:
821 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_INHIBIT_MAP);
823 case OPT_V_X509_STRICT:
824 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_X509_STRICT);
826 case OPT_V_EXTENDED_CRL:
827 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_EXTENDED_CRL_SUPPORT);
829 case OPT_V_USE_DELTAS:
830 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_DELTAS);
832 case OPT_V_POLICY_PRINT:
833 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NOTIFY_POLICY);
835 case OPT_V_CHECK_SS_SIG:
836 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_CHECK_SS_SIGNATURE);
838 case OPT_V_TRUSTED_FIRST:
839 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_TRUSTED_FIRST);
841 case OPT_V_SUITEB_128_ONLY:
842 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS_ONLY);
844 case OPT_V_SUITEB_128:
845 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_128_LOS);
847 case OPT_V_SUITEB_192:
848 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_SUITEB_192_LOS);
850 case OPT_V_PARTIAL_CHAIN:
851 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
853 case OPT_V_NO_ALT_CHAINS:
854 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_ALT_CHAINS);
856 case OPT_V_NO_CHECK_TIME:
857 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME);
859 case OPT_V_ALLOW_PROXY_CERTS:
860 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_ALLOW_PROXY_CERTS);
875 * Parse the next flag (and value if specified), return 0 if done, -1 on
876 * error, otherwise the flag's retval.
886 ossl_uintmax_t umval;
888 /* Look at current arg; at end of the list? */
894 /* If word doesn't start with a -, we're done. */
898 /* Hit "--" ? We're done. */
900 if (strcmp(p, "--") == 0)
903 /* Allow -nnn and --nnn */
908 /* If we have --flag=foo, snip it off */
909 if ((arg = strchr(p, '=')) != NULL)
911 for (o = opts; o->name; ++o) {
912 /* If not this option, move on to the next one. */
913 if (!(strcmp(p, "h") == 0 && strcmp(o->name, "help") == 0)
914 && strcmp(p, o->name) != 0)
917 /* If it doesn't take a value, make sure none was given. */
918 if (o->valtype == 0 || o->valtype == '-') {
920 opt_printf_stderr("%s: Option -%s does not take a value\n",
927 /* Want a value; get the next param if =foo not used. */
929 if (argv[opt_index] == NULL) {
930 opt_printf_stderr("%s: Option -%s needs a value\n",
934 arg = argv[opt_index++];
937 /* Syntax-check value. */
938 switch (o->valtype) {
948 if (opt_isdir(arg) > 0)
950 opt_printf_stderr("%s: Not a directory: %s\n", prog, arg);
961 if (!opt_int(arg, &ival))
963 if (o->valtype == 'p' && ival <= 0) {
964 opt_printf_stderr("%s: Non-positive number \"%s\" for option -%s\n",
968 if (o->valtype == 'N' && ival < 0) {
969 opt_printf_stderr("%s: Negative number \"%s\" for option -%s\n",
975 if (!opt_intmax(arg, &imval))
979 if (!opt_uintmax(arg, &umval))
983 if (!opt_long(arg, &lval))
987 if (!opt_ulong(arg, &ulval))
997 o->valtype == 'c' ? OPT_FMT_PDS :
998 o->valtype == 'E' ? OPT_FMT_PDE :
999 o->valtype == 'F' ? OPT_FMT_PEMDER :
1000 o->valtype == 'A' ? OPT_FMT_ASN1 :
1001 OPT_FMT_ANY, &ival))
1003 opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
1004 prog, arg, o->name);
1008 /* Return the flag value. */
1011 if (unknown != NULL) {
1012 if (dunno != NULL) {
1013 opt_printf_stderr("%s: Multiple %s or unknown options: -%s and -%s\n",
1014 prog, unknown_name, dunno, p);
1018 return unknown->retval;
1020 opt_printf_stderr("%s: Unknown option: -%s\n", prog, p);
1024 /* Return the most recent flag parameter. */
1030 /* Return the most recent flag (option name including the preceding '-'). */
1031 char *opt_flag(void)
1036 /* Return the unknown option. */
1037 char *opt_unknown(void)
1042 /* Reset the unknown option; needed by ocsp to allow multiple digest options. */
1043 void reset_unknown(void)
1048 /* Return the rest of the arguments after parsing flags. */
1049 char **opt_rest(void)
1051 return &argv[opt_index];
1054 /* How many items in remaining args? */
1055 int opt_num_rest(void)
1060 for (pp = opt_rest(); *pp; pp++, i++)
1065 int opt_check_rest_arg(const char *expected)
1067 char *opt = *opt_rest();
1069 if (opt == NULL || *opt == '\0') {
1070 if (expected == NULL)
1072 opt_printf_stderr("%s: Missing argument: %s\n", prog, expected);
1075 if (expected != NULL) {
1076 opt = argv[opt_index + 1];
1077 if (opt == NULL || *opt == '\0')
1079 opt_printf_stderr("%s: Extra argument after %s: \"%s\"\n", prog, expected, opt);
1082 if (opt_unknown() == NULL)
1083 opt_printf_stderr("%s: Extra option: \"%s\"\n", prog, opt);
1085 opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
1086 prog, opt_unknown(), opt);
1090 /* Return a string describing the parameter type. */
1091 static const char *valtype2param(const OPTIONS *o)
1093 switch (o->valtype) {
1116 return "PEM|DER|ENGINE";
1131 static void opt_print(const OPTIONS *o, int doingparams, int width)
1135 int linelen, printlen;
1137 /* Avoid OOB if width is beyond the buffer size of start */
1138 if (width >= (int)sizeof(start))
1139 width = (int)sizeof(start) - 1;
1141 help = o->helpstr ? o->helpstr : "(No additional info)";
1142 if (o->name == OPT_HELP_STR) {
1143 opt_printf_stderr(help, prog);
1145 } else if (o->name == OPT_SECTION_STR) {
1146 opt_printf_stderr("\n");
1147 opt_printf_stderr(help, prog);
1149 } else if (o->name == OPT_PARAM_STR) {
1150 opt_printf_stderr("\nParameters:\n");
1154 /* Pad out prefix */
1155 memset(start, ' ', sizeof(start) - 1);
1156 start[sizeof(start) - 1] = '\0';
1158 if (o->name == OPT_MORE_STR) {
1159 /* Continuation of previous line; pad and print. */
1160 start[width] = '\0';
1161 opt_printf_stderr("%s %s\n", start, help);
1165 /* Build up the "-flag [param]" part. */
1168 printlen = opt_printf_stderr(" %s", !doingparams ? "-" : "");
1169 linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
1171 printlen = opt_printf_stderr("%s" , o->name[0] ? o->name : "*");
1172 linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
1174 if (o->valtype != '-') {
1175 printlen = opt_printf_stderr(" %s" , valtype2param(o));
1176 linelen += (printlen > 0) ? printlen : MAX_OPT_HELP_WIDTH;
1179 if (linelen >= MAX_OPT_HELP_WIDTH || linelen > width) {
1180 opt_printf_stderr("%s", "\n");
1181 memset(start, ' ', sizeof(start));
1187 start[width] = '\0';
1188 opt_printf_stderr("%s %s\n", start, help);
1191 void opt_help(const OPTIONS *list)
1194 int i, sawparams = 0, width = 5;
1195 int standard_prolog;
1197 /* Starts with its own help message? */
1198 standard_prolog = list[0].name != OPT_HELP_STR;
1200 /* Find the widest help. */
1201 for (o = list; o->name; o++) {
1202 if (o->name == OPT_MORE_STR)
1205 i = 2 + (int)strlen(o->name);
1206 if (o->valtype != '-')
1207 i += 1 + strlen(valtype2param(o));
1213 if (width > MAX_OPT_HELP_WIDTH)
1214 width = MAX_OPT_HELP_WIDTH;
1216 if (standard_prolog) {
1217 opt_printf_stderr("Usage: %s [options]\n", prog);
1218 if (list[0].name != OPT_SECTION_STR)
1219 opt_printf_stderr("Valid options are:\n", prog);
1222 /* Now let's print. */
1223 for (o = list; o->name; o++) {
1224 if (o->name == OPT_PARAM_STR)
1226 opt_print(o, sawparams, width);
1230 /* opt_isdir section */
1232 # include <windows.h>
1233 int opt_isdir(const char *name)
1236 # if defined(UNICODE) || defined(_UNICODE)
1237 size_t i, len_0 = strlen(name) + 1;
1238 WCHAR tempname[MAX_PATH];
1240 if (len_0 > MAX_PATH)
1243 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
1244 if (!MultiByteToWideChar(CP_ACP, 0, name, len_0, tempname, MAX_PATH))
1246 for (i = 0; i < len_0; i++)
1247 tempname[i] = (WCHAR)name[i];
1249 attr = GetFileAttributes(tempname);
1251 attr = GetFileAttributes(name);
1253 if (attr == INVALID_FILE_ATTRIBUTES)
1255 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
1258 # include <sys/stat.h>
1260 # if defined(_S_IFMT) && defined(_S_IFDIR)
1261 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1263 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1267 int opt_isdir(const char *name)
1269 # if defined(S_ISDIR)
1272 if (stat(name, &st) == 0)
1273 return S_ISDIR(st.st_mode);