2 * Copyright 2015-2021 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 OPTIONS
*unknown
;
45 static const OPTIONS
*opts
;
49 * Return the simple name of the program; removing various platform gunk.
51 #if defined(OPENSSL_SYS_WIN32)
53 const char *opt_path_end(const char *filename
)
57 /* find the last '/', '\' or ':' */
58 for (p
= filename
+ strlen(filename
); --p
> filename
; )
59 if (*p
== '/' || *p
== '\\' || *p
== ':') {
66 char *opt_progname(const char *argv0
)
72 p
= opt_path_end(argv0
);
74 /* Strip off trailing nonsense. */
77 (strcmp(&p
[n
- 4], ".exe") == 0 || strcmp(&p
[n
- 4], ".EXE") == 0))
80 /* Copy over the name, in lowercase. */
81 if (n
> sizeof(prog
) - 1)
83 for (q
= prog
, i
= 0; i
< n
; i
++, p
++)
84 *q
++ = tolower((unsigned char)*p
);
89 #elif defined(OPENSSL_SYS_VMS)
91 const char *opt_path_end(const char *filename
)
95 /* Find last special character sys:[foo.bar]openssl */
96 for (p
= filename
+ strlen(filename
); --p
> filename
;)
97 if (*p
== ':' || *p
== ']' || *p
== '>') {
104 char *opt_progname(const char *argv0
)
108 /* Find last special character sys:[foo.bar]openssl */
109 p
= opt_path_end(argv0
);
112 strncpy(prog
, p
, sizeof(prog
) - 1);
113 prog
[sizeof(prog
) - 1] = '\0';
114 if (q
!= NULL
&& q
- p
< sizeof(prog
))
121 const char *opt_path_end(const char *filename
)
125 /* Could use strchr, but this is like the ones above. */
126 for (p
= filename
+ strlen(filename
); --p
> filename
;)
134 char *opt_progname(const char *argv0
)
138 p
= opt_path_end(argv0
);
140 strncpy(prog
, p
, sizeof(prog
) - 1);
141 prog
[sizeof(prog
) - 1] = '\0';
146 char *opt_appname(const char *argv0
)
148 size_t len
= strlen(prog
);
151 BIO_snprintf(prog
+ len
, sizeof(prog
) - len
- 1, " %s", argv0
);
155 char *opt_getprog(void)
160 /* Set up the arg parsing. */
161 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':
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
== NULL
);
220 OPENSSL_assert(unknown
->valtype
== 0 || unknown
->valtype
== '-');
226 static OPT_PAIR formats
[] = {
227 {"PEM/DER", OPT_FMT_PEMDER
},
228 {"pkcs12", OPT_FMT_PKCS12
},
229 {"smime", OPT_FMT_SMIME
},
230 {"engine", OPT_FMT_ENGINE
},
231 {"msblob", OPT_FMT_MSBLOB
},
232 {"nss", OPT_FMT_NSS
},
233 {"text", OPT_FMT_TEXT
},
234 {"http", OPT_FMT_HTTP
},
235 {"pvk", OPT_FMT_PVK
},
239 /* Print an error message about a failed format parse. */
240 static int opt_format_error(const char *s
, unsigned long flags
)
244 if (flags
== OPT_FMT_PEMDER
) {
245 opt_printf_stderr("%s: Bad format \"%s\"; must be pem or der\n",
248 opt_printf_stderr("%s: Bad format \"%s\"; must be one of:\n",
250 for (ap
= formats
; ap
->name
; ap
++)
251 if (flags
& ap
->retval
)
252 opt_printf_stderr(" %s\n", ap
->name
);
257 /* Parse a format string, put it into *result; return 0 on failure, else 1. */
258 int opt_format(const char *s
, unsigned long flags
, int *result
)
262 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
266 if ((flags
& OPT_FMT_PEMDER
) == 0)
267 return opt_format_error(s
, flags
);
268 *result
= FORMAT_ASN1
;
272 if ((flags
& OPT_FMT_TEXT
) == 0)
273 return opt_format_error(s
, flags
);
274 *result
= FORMAT_TEXT
;
278 if ((flags
& OPT_FMT_NSS
) == 0)
279 return opt_format_error(s
, flags
);
280 if (strcmp(s
, "NSS") != 0 && strcmp(s
, "nss") != 0)
281 return opt_format_error(s
, flags
);
282 *result
= FORMAT_NSS
;
286 if ((flags
& OPT_FMT_SMIME
) == 0)
287 return opt_format_error(s
, flags
);
288 *result
= FORMAT_SMIME
;
292 if ((flags
& OPT_FMT_MSBLOB
) == 0)
293 return opt_format_error(s
, flags
);
294 *result
= FORMAT_MSBLOB
;
298 if ((flags
& OPT_FMT_ENGINE
) == 0)
299 return opt_format_error(s
, flags
);
300 *result
= FORMAT_ENGINE
;
304 if ((flags
& OPT_FMT_HTTP
) == 0)
305 return opt_format_error(s
, flags
);
306 *result
= FORMAT_HTTP
;
309 if ((flags
& OPT_FMT_PKCS12
) == 0)
310 return opt_format_error(s
, flags
);
311 *result
= FORMAT_PKCS12
;
315 if (s
[1] == '\0' || strcmp(s
, "PEM") == 0 || strcmp(s
, "pem") == 0) {
316 if ((flags
& OPT_FMT_PEMDER
) == 0)
317 return opt_format_error(s
, flags
);
318 *result
= FORMAT_PEM
;
319 } else if (strcmp(s
, "PVK") == 0 || strcmp(s
, "pvk") == 0) {
320 if ((flags
& OPT_FMT_PVK
) == 0)
321 return opt_format_error(s
, flags
);
322 *result
= FORMAT_PVK
;
323 } else if (strcmp(s
, "P12") == 0 || strcmp(s
, "p12") == 0
324 || strcmp(s
, "PKCS12") == 0 || strcmp(s
, "pkcs12") == 0) {
325 if ((flags
& OPT_FMT_PKCS12
) == 0)
326 return opt_format_error(s
, flags
);
327 *result
= FORMAT_PKCS12
;
329 opt_printf_stderr("%s: Bad format \"%s\"\n", prog
, s
);
337 /* Return string representing the given format. */
338 static const char *format2str(int format
)
342 return "(undefined)";
366 /* Print an error message about unsuitable/unsupported format requested. */
367 void print_format_error(int format
, unsigned long flags
)
369 (void)opt_format_error(format2str(format
), flags
);
373 * Parse a cipher name, put it in *cipherp after freeing what was there, if
374 * cipherp is not NULL. Return 0 on failure, else 1.
376 int opt_cipher_silent(const char *name
, EVP_CIPHER
**cipherp
)
381 if ((c
= EVP_CIPHER_fetch(app_get0_libctx(), name
,
382 app_get0_propq())) != NULL
383 || (opt_legacy_okay()
384 && (c
= (EVP_CIPHER
*)EVP_get_cipherbyname(name
)) != NULL
)) {
386 if (cipherp
!= NULL
) {
387 EVP_CIPHER_free(*cipherp
);
394 ERR_clear_last_mark();
398 int opt_cipher_any(const char *name
, EVP_CIPHER
**cipherp
)
404 if ((ret
= opt_cipher_silent(name
, cipherp
)) == 0)
405 opt_printf_stderr("%s: Unknown option or cipher: %s\n", prog
, name
);
409 int opt_cipher(const char *name
, EVP_CIPHER
**cipherp
)
412 unsigned long int flags
;
413 EVP_CIPHER
*c
= NULL
;
417 if (opt_cipher_any(name
, &c
)) {
418 mode
= EVP_CIPHER_get_mode(c
);
419 flags
= EVP_CIPHER_get_flags(c
);
420 if (mode
== EVP_CIPH_XTS_MODE
) {
421 opt_printf_stderr("%s XTS ciphers not supported\n", prog
);
422 } else if ((flags
& EVP_CIPH_FLAG_AEAD_CIPHER
) != 0) {
423 opt_printf_stderr("%s: AEAD ciphers not supported\n", prog
);
434 * Parse message digest name, put it in *EVP_MD; return 0 on failure, else 1.
436 int opt_md_silent(const char *name
, EVP_MD
**mdp
)
441 if ((md
= EVP_MD_fetch(app_get0_libctx(), name
, app_get0_propq())) != NULL
442 || (opt_legacy_okay()
443 && (md
= (EVP_MD
*)EVP_get_digestbyname(name
)) != NULL
)) {
453 ERR_clear_last_mark();
457 int opt_md(const char *name
, EVP_MD
**mdp
)
463 if ((ret
= opt_md_silent(name
, mdp
)) == 0)
464 opt_printf_stderr("%s: Unknown option or message digest: %s\n",
469 int opt_check_md(const char *name
)
471 if (opt_md(name
, NULL
))
477 /* Look through a list of name/value pairs. */
478 int opt_pair(const char *name
, const OPT_PAIR
* pairs
, int *result
)
482 for (pp
= pairs
; pp
->name
; pp
++)
483 if (strcmp(pp
->name
, name
) == 0) {
484 *result
= pp
->retval
;
487 opt_printf_stderr("%s: Value must be one of:\n", prog
);
488 for (pp
= pairs
; pp
->name
; pp
++)
489 opt_printf_stderr("\t%s\n", pp
->name
);
493 /* Look through a list of valid names */
494 int opt_string(const char *name
, const char **options
)
498 for (p
= options
; *p
!= NULL
; p
++)
499 if (strcmp(*p
, name
) == 0)
501 opt_printf_stderr("%s: Value must be one of:\n", prog
);
502 for (p
= options
; *p
!= NULL
; p
++)
503 opt_printf_stderr("\t%s\n", *p
);
507 /* Parse an int, put it into *result; return 0 on failure, else 1. */
508 int opt_int(const char *value
, int *result
)
512 if (!opt_long(value
, &l
))
516 opt_printf_stderr("%s: Value \"%s\" outside integer range\n",
523 /* Parse and return an integer, assuming range has been checked before. */
524 int opt_int_arg(void)
528 (void)opt_int(arg
, &result
);
532 static void opt_number_error(const char *v
)
535 struct strstr_pair_st
{
539 {"0x", "a hexadecimal"},
540 {"0X", "a hexadecimal"},
544 for (i
= 0; i
< OSSL_NELEM(b
); i
++) {
545 if (strncmp(v
, b
[i
].prefix
, strlen(b
[i
].prefix
)) == 0) {
546 opt_printf_stderr("%s: Can't parse \"%s\" as %s number\n",
551 opt_printf_stderr("%s: Can't parse \"%s\" as a number\n", prog
, v
);
555 /* Parse a long, put it into *result; return 0 on failure, else 1. */
556 int opt_long(const char *value
, long *result
)
563 l
= strtol(value
, &endp
, 0);
566 || ((l
== LONG_MAX
|| l
== LONG_MIN
) && errno
== ERANGE
)
567 || (l
== 0 && errno
!= 0)) {
568 opt_number_error(value
);
577 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
578 defined(INTMAX_MAX) && defined(UINTMAX_MAX) && \
579 !defined(OPENSSL_NO_INTTYPES_H)
581 /* Parse an intmax_t, put it into *result; return 0 on failure, else 1. */
582 int opt_intmax(const char *value
, ossl_intmax_t
*result
)
589 m
= strtoimax(value
, &endp
, 0);
592 || ((m
== INTMAX_MAX
|| m
== INTMAX_MIN
)
594 || (m
== 0 && errno
!= 0)) {
595 opt_number_error(value
);
599 /* Ensure that the value in |m| is never too big for |*result| */
600 if (sizeof(m
) > sizeof(*result
)
601 && (m
< OSSL_INTMAX_MIN
|| m
> OSSL_INTMAX_MAX
)) {
602 opt_number_error(value
);
605 *result
= (ossl_intmax_t
)m
;
610 /* Parse a uintmax_t, put it into *result; return 0 on failure, else 1. */
611 int opt_uintmax(const char *value
, ossl_uintmax_t
*result
)
618 m
= strtoumax(value
, &endp
, 0);
621 || (m
== UINTMAX_MAX
&& errno
== ERANGE
)
622 || (m
== 0 && errno
!= 0)) {
623 opt_number_error(value
);
627 /* Ensure that the value in |m| is never too big for |*result| */
628 if (sizeof(m
) > sizeof(*result
)
629 && m
> OSSL_UINTMAX_MAX
) {
630 opt_number_error(value
);
633 *result
= (ossl_intmax_t
)m
;
638 /* Fallback implementations based on long */
639 int opt_intmax(const char *value
, ossl_intmax_t
*result
)
644 if ((ret
= opt_long(value
, &m
)))
649 int opt_uintmax(const char *value
, ossl_uintmax_t
*result
)
654 if ((ret
= opt_ulong(value
, &m
)))
661 * Parse an unsigned long, put it into *result; return 0 on failure, else 1.
663 int opt_ulong(const char *value
, unsigned long *result
)
670 l
= strtoul(value
, &endptr
, 0);
673 || ((l
== ULONG_MAX
) && errno
== ERANGE
)
674 || (l
== 0 && errno
!= 0)) {
675 opt_number_error(value
);
685 * We pass opt as an int but cast it to "enum range" so that all the
686 * items in the OPT_V_ENUM enumeration are caught; this makes -Wswitch
687 * in gcc do the right thing.
689 enum range
{ OPT_V_ENUM
};
691 int opt_verify(int opt
, X509_VERIFY_PARAM
*vpm
)
697 const X509_VERIFY_PARAM
*vtmp
;
699 OPENSSL_assert(vpm
!= NULL
);
700 OPENSSL_assert(opt
> OPT_V__FIRST
);
701 OPENSSL_assert(opt
< OPT_V__LAST
);
703 switch ((enum range
)opt
) {
708 otmp
= OBJ_txt2obj(opt_arg(), 0);
710 opt_printf_stderr("%s: Invalid Policy %s\n", prog
, opt_arg());
713 X509_VERIFY_PARAM_add0_policy(vpm
, otmp
);
716 /* purpose name -> purpose index */
717 i
= X509_PURPOSE_get_by_sname(opt_arg());
719 opt_printf_stderr("%s: Invalid purpose %s\n", prog
, opt_arg());
723 /* purpose index -> purpose object */
724 xptmp
= X509_PURPOSE_get0(i
);
726 /* purpose object -> purpose value */
727 i
= X509_PURPOSE_get_id(xptmp
);
729 if (!X509_VERIFY_PARAM_set_purpose(vpm
, i
)) {
730 opt_printf_stderr("%s: Internal error setting purpose %s\n",
735 case OPT_V_VERIFY_NAME
:
736 vtmp
= X509_VERIFY_PARAM_lookup(opt_arg());
738 opt_printf_stderr("%s: Invalid verify name %s\n",
742 X509_VERIFY_PARAM_set1(vpm
, vtmp
);
744 case OPT_V_VERIFY_DEPTH
:
747 X509_VERIFY_PARAM_set_depth(vpm
, i
);
749 case OPT_V_VERIFY_AUTH_LEVEL
:
752 X509_VERIFY_PARAM_set_auth_level(vpm
, i
);
755 if (!opt_intmax(opt_arg(), &t
))
757 if (t
!= (time_t)t
) {
758 opt_printf_stderr("%s: epoch time out of range %s\n",
762 X509_VERIFY_PARAM_set_time(vpm
, (time_t)t
);
764 case OPT_V_VERIFY_HOSTNAME
:
765 if (!X509_VERIFY_PARAM_set1_host(vpm
, opt_arg(), 0))
768 case OPT_V_VERIFY_EMAIL
:
769 if (!X509_VERIFY_PARAM_set1_email(vpm
, opt_arg(), 0))
772 case OPT_V_VERIFY_IP
:
773 if (!X509_VERIFY_PARAM_set1_ip_asc(vpm
, opt_arg()))
776 case OPT_V_IGNORE_CRITICAL
:
777 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_IGNORE_CRITICAL
);
779 case OPT_V_ISSUER_CHECKS
:
780 /* NOP, deprecated */
782 case OPT_V_CRL_CHECK
:
783 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CRL_CHECK
);
785 case OPT_V_CRL_CHECK_ALL
:
786 X509_VERIFY_PARAM_set_flags(vpm
,
787 X509_V_FLAG_CRL_CHECK
|
788 X509_V_FLAG_CRL_CHECK_ALL
);
790 case OPT_V_POLICY_CHECK
:
791 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_POLICY_CHECK
);
793 case OPT_V_EXPLICIT_POLICY
:
794 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXPLICIT_POLICY
);
796 case OPT_V_INHIBIT_ANY
:
797 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_ANY
);
799 case OPT_V_INHIBIT_MAP
:
800 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_INHIBIT_MAP
);
802 case OPT_V_X509_STRICT
:
803 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_X509_STRICT
);
805 case OPT_V_EXTENDED_CRL
:
806 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_EXTENDED_CRL_SUPPORT
);
808 case OPT_V_USE_DELTAS
:
809 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_USE_DELTAS
);
811 case OPT_V_POLICY_PRINT
:
812 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NOTIFY_POLICY
);
814 case OPT_V_CHECK_SS_SIG
:
815 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
817 case OPT_V_TRUSTED_FIRST
:
818 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_TRUSTED_FIRST
);
820 case OPT_V_SUITEB_128_ONLY
:
821 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS_ONLY
);
823 case OPT_V_SUITEB_128
:
824 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_128_LOS
);
826 case OPT_V_SUITEB_192
:
827 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_SUITEB_192_LOS
);
829 case OPT_V_PARTIAL_CHAIN
:
830 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_PARTIAL_CHAIN
);
832 case OPT_V_NO_ALT_CHAINS
:
833 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_ALT_CHAINS
);
835 case OPT_V_NO_CHECK_TIME
:
836 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_NO_CHECK_TIME
);
838 case OPT_V_ALLOW_PROXY_CERTS
:
839 X509_VERIFY_PARAM_set_flags(vpm
, X509_V_FLAG_ALLOW_PROXY_CERTS
);
854 * Parse the next flag (and value if specified), return 0 if done, -1 on
855 * error, otherwise the flag's retval.
865 ossl_uintmax_t umval
;
867 /* Look at current arg; at end of the list? */
873 /* If word doesn't start with a -, we're done. */
877 /* Hit "--" ? We're done. */
879 if (strcmp(p
, "--") == 0)
882 /* Allow -nnn and --nnn */
887 /* If we have --flag=foo, snip it off */
888 if ((arg
= strchr(p
, '=')) != NULL
)
890 for (o
= opts
; o
->name
; ++o
) {
891 /* If not this option, move on to the next one. */
892 if (!(strcmp(p
, "h") == 0 && strcmp(o
->name
, "help") == 0)
893 && strcmp(p
, o
->name
) != 0)
896 /* If it doesn't take a value, make sure none was given. */
897 if (o
->valtype
== 0 || o
->valtype
== '-') {
899 opt_printf_stderr("%s: Option -%s does not take a value\n",
906 /* Want a value; get the next param if =foo not used. */
908 if (argv
[opt_index
] == NULL
) {
909 opt_printf_stderr("%s: Option -%s needs a value\n",
913 arg
= argv
[opt_index
++];
916 /* Syntax-check value. */
917 switch (o
->valtype
) {
927 if (opt_isdir(arg
) > 0)
929 opt_printf_stderr("%s: Not a directory: %s\n", prog
, arg
);
940 if (!opt_int(arg
, &ival
))
942 if (o
->valtype
== 'p' && ival
<= 0) {
943 opt_printf_stderr("%s: Non-positive number \"%s\" for option -%s\n",
947 if (o
->valtype
== 'N' && ival
< 0) {
948 opt_printf_stderr("%s: Negative number \"%s\" for option -%s\n",
954 if (!opt_intmax(arg
, &imval
))
958 if (!opt_uintmax(arg
, &umval
))
962 if (!opt_long(arg
, &lval
))
966 if (!opt_ulong(arg
, &ulval
))
974 o
->valtype
== 'c' ? OPT_FMT_PDS
:
975 o
->valtype
== 'E' ? OPT_FMT_PDE
:
976 o
->valtype
== 'F' ? OPT_FMT_PEMDER
977 : OPT_FMT_ANY
, &ival
))
979 opt_printf_stderr("%s: Invalid format \"%s\" for option -%s\n",
984 /* Return the flag value. */
987 if (unknown
!= NULL
) {
989 return unknown
->retval
;
991 opt_printf_stderr("%s: Unknown option: -%s\n", prog
, p
);
995 /* Return the most recent flag parameter. */
1001 /* Return the most recent flag (option name including the preceding '-'). */
1002 char *opt_flag(void)
1007 /* Return the unknown option. */
1008 char *opt_unknown(void)
1013 /* Return the rest of the arguments after parsing flags. */
1014 char **opt_rest(void)
1016 return &argv
[opt_index
];
1019 /* How many items in remaining args? */
1020 int opt_num_rest(void)
1025 for (pp
= opt_rest(); *pp
; pp
++, i
++)
1030 int opt_check_rest_arg(const char *expected
)
1032 char *opt
= *opt_rest();
1034 if (opt
== NULL
|| *opt
== '\0') {
1035 if (expected
== NULL
)
1037 opt_printf_stderr("%s: Missing argument: %s\n", prog
, expected
);
1039 } else if (expected
!= NULL
) {
1042 if (opt_unknown() == NULL
)
1043 opt_printf_stderr("%s: Extra option: \"%s\"\n", prog
, opt
);
1045 opt_printf_stderr("%s: Extra (unknown) options: \"%s\" \"%s\"\n",
1046 prog
, opt_unknown(), opt
!= NULL
? opt
: "");
1050 /* Return a string describing the parameter type. */
1051 static const char *valtype2param(const OPTIONS
*o
)
1053 switch (o
->valtype
) {
1076 return "PEM|DER|ENGINE";
1091 static void opt_print(const OPTIONS
*o
, int doingparams
, int width
)
1097 help
= o
->helpstr
? o
->helpstr
: "(No additional info)";
1098 if (o
->name
== OPT_HELP_STR
) {
1099 opt_printf_stderr(help
, prog
);
1102 if (o
->name
== OPT_SECTION_STR
) {
1103 opt_printf_stderr("\n");
1104 opt_printf_stderr(help
, prog
);
1107 if (o
->name
== OPT_PARAM_STR
) {
1108 opt_printf_stderr("\nParameters:\n");
1112 /* Pad out prefix */
1113 memset(start
, ' ', sizeof(start
) - 1);
1114 start
[sizeof(start
) - 1] = '\0';
1116 if (o
->name
== OPT_MORE_STR
) {
1117 /* Continuation of previous line; pad and print. */
1118 start
[width
] = '\0';
1119 opt_printf_stderr("%s %s\n", start
, help
);
1123 /* Build up the "-flag [param]" part. */
1129 p
+= strlen(strcpy(p
, o
->name
));
1132 if (o
->valtype
!= '-') {
1134 p
+= strlen(strcpy(p
, valtype2param(o
)));
1137 if ((int)(p
- start
) >= MAX_OPT_HELP_WIDTH
) {
1139 opt_printf_stderr("%s\n", start
);
1140 memset(start
, ' ', sizeof(start
));
1142 start
[width
] = '\0';
1143 opt_printf_stderr("%s %s\n", start
, help
);
1146 void opt_help(const OPTIONS
*list
)
1149 int i
, sawparams
= 0, width
= 5;
1150 int standard_prolog
;
1153 /* Starts with its own help message? */
1154 standard_prolog
= list
[0].name
!= OPT_HELP_STR
;
1156 /* Find the widest help. */
1157 for (o
= list
; o
->name
; o
++) {
1158 if (o
->name
== OPT_MORE_STR
)
1160 i
= 2 + (int)strlen(o
->name
);
1161 if (o
->valtype
!= '-')
1162 i
+= 1 + strlen(valtype2param(o
));
1163 if (i
< MAX_OPT_HELP_WIDTH
&& i
> width
)
1165 OPENSSL_assert(i
< (int)sizeof(start
));
1168 if (standard_prolog
) {
1169 opt_printf_stderr("Usage: %s [options]\n", prog
);
1170 if (list
[0].name
!= OPT_SECTION_STR
)
1171 opt_printf_stderr("Valid options are:\n", prog
);
1174 /* Now let's print. */
1175 for (o
= list
; o
->name
; o
++) {
1176 if (o
->name
== OPT_PARAM_STR
)
1178 opt_print(o
, sawparams
, width
);
1182 /* opt_isdir section */
1184 # include <windows.h>
1185 int opt_isdir(const char *name
)
1188 # if defined(UNICODE) || defined(_UNICODE)
1189 size_t i
, len_0
= strlen(name
) + 1;
1190 WCHAR tempname
[MAX_PATH
];
1192 if (len_0
> MAX_PATH
)
1195 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
1196 if (!MultiByteToWideChar(CP_ACP
, 0, name
, len_0
, tempname
, MAX_PATH
))
1198 for (i
= 0; i
< len_0
; i
++)
1199 tempname
[i
] = (WCHAR
)name
[i
];
1201 attr
= GetFileAttributes(tempname
);
1203 attr
= GetFileAttributes(name
);
1205 if (attr
== INVALID_FILE_ATTRIBUTES
)
1207 return ((attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0);
1210 # include <sys/stat.h>
1212 # if defined(_S_IFMT) && defined(_S_IFDIR)
1213 # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
1215 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
1219 int opt_isdir(const char *name
)
1221 # if defined(S_ISDIR)
1224 if (stat(name
, &st
) == 0)
1225 return S_ISDIR(st
.st_mode
);