OPTIONS.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. =pod
  2. =head1 NAME
  3. OPTIONS, OPT_PAIR, OPT_COMMON, OPT_ERR, OPT_EOF, OPT_HELP,
  4. opt_init, opt_progname, opt_appname, opt_getprog, opt_help,
  5. opt_begin, opt_next, opt_flag, opt_arg, opt_unknown, opt_cipher,
  6. opt_cipher_any, opt_cipher_silent, opt_md,
  7. opt_int, opt_int_arg, opt_long, opt_ulong, opt_intmax, opt_uintmax,
  8. opt_format, opt_isdir, opt_string, opt_pair,
  9. opt_num_rest, opt_rest, opt_legacy_okay
  10. - Option parsing for commands and tests
  11. =head1 SYNOPSIS
  12. #include "opt.h"
  13. typedef struct { ... } OPTIONS;
  14. typedef struct { ... } OPT_PAIR;
  15. #define OPT_COMMON
  16. #define OPT_ERR
  17. #define OPT_EOF
  18. #define OPT_HELP
  19. char *opt_init(int argc, char **argv, const OPTIONS *o);
  20. char *opt_progname(const char *argv0);
  21. char *opt_appname(const char *argv0);
  22. char *opt_getprog(void);
  23. void opt_help(const OPTIONS *list);
  24. void opt_begin(void);
  25. int opt_next(void);
  26. char *opt_flag(void);
  27. char *opt_arg(void);
  28. char *opt_unknown(void);
  29. int opt_cipher(const char *name, EVP_CIPHER **cipherp);
  30. int opt_cipher_any(const char *name, EVP_CIPHER **cipherp);
  31. int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp);
  32. int opt_md(const char *name, EVP_MD **mdp);
  33. int opt_int(const char *value, int *result);
  34. int opt_int_arg(void);
  35. int opt_long(const char *value, long *result);
  36. int opt_ulong(const char *value, unsigned long *result);
  37. int opt_intmax(const char *value, intmax_t *result);
  38. int opt_uintmax(const char *value, uintmax_t *result);
  39. int opt_format(const char *s, unsigned long flags, int *result);
  40. int opt_isdir(const char *name);
  41. int opt_string(const char *name, const char **options);
  42. int opt_pair(const char *name, const OPT_PAIR* pairs, int *result);
  43. int opt_num_rest(void);
  44. char **opt_rest(void);
  45. int opt_legacy_okay(void);
  46. =head1 DESCRIPTION
  47. The functions on this page provide a common set of option-parsing for
  48. the OpenSSL command and the internal test programs.
  49. It is intended to be used like the standard getopt(3) routine, except
  50. that multi-character flag names are supported, and a variety of parsing
  51. and other utility functions are also provided.
  52. Programs that use this should make sure to set the appropriate C<-I>
  53. flag.
  54. These routines expect a global B<BIO> named B<bio_err> to point to
  55. the equivalent of B<stderr>. This is already done in the OpenSSL
  56. application.
  57. =head2 Data Types
  58. Each program should define, near the main() routine, an enumeration
  59. that is the set of options the program accepts. For example:
  60. typedef enum OPTION_choice {
  61. OPT_COMMON,
  62. OPT_YES, OPT_NAME, OPT_COUNT, OPT_OFILE,
  63. ...
  64. } OPTION_CHOICE;
  65. The first two lines must appear exactly as shown.
  66. OPT_COMMON is a macro that expands to C<OPT_ERR = -1, OPT_EOF = 0, OPT_HELP>.
  67. In addition to defining symbolic names for the constants that opt_next()
  68. returns, it also helps guarantee that every command has a C<-help> option.
  69. The third line is a sample
  70. set of flags, and the closing C<typedef> name is used for error-checking
  71. as discussed below.
  72. By declaring the variable as an C<OPTION_CHOICE>, with the right warning
  73. flags, the compiler could check that all specified options are handled.
  74. The B<OPTIONS> C<typedef> specifies an option: what type of argument
  75. it takes (if any), and an optional "help" string. It is a C<struct>
  76. containing these fields:
  77. const char *name;
  78. int retval;
  79. int valtype;
  80. const char *helpstr;
  81. The B<name> is the name of the option that the user would type. Options
  82. are words prefaced with a minus sign. If the user uses two minus signs,
  83. this is also accepted for compatibility with other GNU software. Some
  84. names are special, and are described below.
  85. The B<retval> is the value to return if the option is found. It should be
  86. one of the choices in the enumeration above.
  87. The B<valtype> defines what the option's parameter must be. It should
  88. be chosen from the following set:
  89. \0 No value
  90. '-' No value
  91. 's' A text string
  92. '/' A directory
  93. '<' Name of file to open for input
  94. '>' Name of file to open for output
  95. 'n' A signed number that fits in the C<int> type
  96. 'p' A positive number that fits in the C<int> type
  97. 'N' A nonnegative number that fits in the C<int> type
  98. 'M' A signed number that fits in the C<intmax_t> type
  99. 'U' An unsigned number that fits in the C<uintmax_t> type
  100. 'l' A signed number that fits in the C<long> type
  101. 'u' An unsigned number that fits in the C<unsigned long> type
  102. 'c' File in PEM, DER, or S/MIME format
  103. 'F' A file in PEM or DER format
  104. 'E' Like 'F' but also allows ENGINE
  105. 'f' Any file format
  106. The B<helpstr> is what to display when the user uses the help option,
  107. which should be C<"help">.
  108. A program should declare its options right after the enumeration,
  109. and should follow the ordering of the enumeration as this helps
  110. readability and maintainability:
  111. static OPTIONS my_options[] = {
  112. {"help", OPT_HELP, '-', "Display this summary"},
  113. {"yes", OPT_YES, '-', "Print an affirmative reply"},
  114. {"count", OPT_COUNT, 'p', "Repeat count"},
  115. {"output" OPT_OFILE, '>', "Output file; default is stdout"},
  116. {NULL}
  117. };
  118. Note that the B<OPT_HELP> option is explicitly listed, and the list ends with
  119. an entry of all-null's. The other two special options, B<OPT_ERR> and B<OPT_EOF>
  120. should not appear in the array.
  121. If the help string is too long to fit into one line, it may be continued
  122. on multiple lines; each entry should use B<OPT_MORE_STR>, like this:
  123. {"output" OPT_OFILE, '>', "Output file; default is stdout"},
  124. {OPT_MORE_STR, 0, 0,
  125. "This flag is not really needed on Unix systems"},
  126. {OPT_MORE_STR, 0, 0,
  127. "(Unix and descendents for the win!)"}
  128. Each subsequent line will be indented the correct amount.
  129. By default, the help display will include a standard prolog:
  130. Usage: PROGRAM [options]
  131. Valid options are:
  132. ...detailed list of options...
  133. Sometimes there are parameters that should appear in the synopsis.
  134. Use B<OPT_HELP_STR> as the first entry in your array:
  135. {OPT_HELP_STR, 1, '-', Usage: %s [options] [text...]\n"}
  136. The B<retval> and B<valtype> are ignored, and the B<helpstr> should
  137. follow the general construction as shown. The C<%s> will get the program
  138. name.
  139. If a command has a large set of options, it can be useful to break them
  140. into sections. Use the macro B<OPT_SECTION> or B<OPT_SECTION_STR>
  141. to indicate this. The two lines below are equivalent:
  142. OPT_SECTION("Validation"),
  143. {OPT_SECTION_STR, 1, '-', "Validation options:\n"},
  144. In addition to providing help about options, you can provide a description
  145. of the parameters a command takes. These should appear at the end of
  146. the options and are indicated by using B<OPT_PARAM_STR> or the
  147. B<OPT_PARAMETERS> macro:
  148. OPT_PARAMETERS()
  149. {OPT_PARAM_STR, 1, '-', "Parameters:\n"}
  150. Every "option" after after this should contain the parameter and
  151. the help string:
  152. {"text", 0, 0, "Words to display (optional)"},
  153. =head2 Functions
  154. The opt_init() function takes the I<argc> and I<argv> arguments given to main()
  155. and a pointer I<o> to the list of options. It returns the simple program
  156. name, as defined by opt_progname().
  157. The opt_progname() function takes the full pathname C<argv[0]> in its I<arg0>
  158. parameter and returns
  159. the simple short name of the executable, to be used for error messages and
  160. the like.
  161. The opt_appname() function takes in its I<argv0> parameter
  162. the "application" name (such
  163. as the specific command from L<openssl(1)> and appends it to the program
  164. name. This function should only be called once.
  165. The opt_getprog() function returns the value set by opt_appname().
  166. The opt_help() function takes a list of option definitions and prints a
  167. nicely-formatted output.
  168. The opt_begin() function, which is called automatically by opt_init(),
  169. can be used to reset the option parsing loop.
  170. The opt_next() function is called, once opt_init() has been called,
  171. in a loop to fetch each option in turn. It returns -1, or B<OPT_EOF> when the
  172. end of arguments has been reached. This is typically done like this:
  173. prog = opt_init(argc, argv, my_options);
  174. while ((o = opt_next()) != OPT_EOF) {
  175. switch (o) {
  176. case OPT_EOF:
  177. case OPT_ERR:
  178. opthelp:
  179. fprintf(stderr, "%s: Use -help for summary\n", prog);
  180. exit(1);
  181. case OPT_HELP:
  182. opt_help(my_options);
  183. exit(0);
  184. ...other options...
  185. }
  186. }
  187. Within the option parsing loop, the following functions may be called.
  188. The opt_flag() function returns the most recent option name
  189. including the preceding C<->.
  190. The opt_arg() function returns the option's argument value, if there is one.
  191. The opt_unknown() function returns the unknown option.
  192. In an option list, there can be at most one option with the empty string.
  193. This is a "wildcard" or "unknown" option. For example, it allows an
  194. option to be be taken as digest algorithm, like C<-sha1>. The function
  195. opt_md() takes the specified I<name> and fills in the digest into I<mdp>.
  196. The functions opt_cipher(), opt_cipher_any() and opt_cipher_silent()
  197. each takes the specified I<name> and fills in the cipher into I<cipherp>.
  198. The function opt_cipher() only accepts ciphers which are not
  199. AEAD and are not using XTS mode. The functions opt_cipher_any() and
  200. opt_cipher_silent() accept any cipher, the latter not emitting an error
  201. if the cipher is not located.
  202. There are a several useful functions for parsing numbers. These are
  203. opt_int(), opt_long(), opt_ulong(), opt_intmax(), and opt_uintmax(). They all
  204. take C<0x> to mean hexadecimal and C<0> to mean octal, and will do the
  205. necessary range-checking. They return 1 if successful and fill in the
  206. C<result> pointer with the value, or 0 on error. Note that opt_next()
  207. will also do range-check on the argument if the appropriate B<valtype>
  208. field is specified for the option. This means that error-checking inside
  209. the C<switch> C<case> can often be elided.
  210. The opt_int_arg() function is a convenience abbreviation to opt_int().
  211. It parses and returns an integer, assuming its range has been checked before.
  212. The opt_format() function takes a string value,
  213. such as used with the B<-informat> or similar option, and fills
  214. the value from the constants in F<fmt.h> file.
  215. The opt_isdir() function returns 1 if the specified I<name> is
  216. a directory, or 0 if not.
  217. The opt_string() function checks that I<name> appears in the
  218. NULL-terminated array of strings. It returns 1 if found,
  219. or prints a diagnostic and returns 0 if not.
  220. The opt_pair() function takes a list of I<pairs>, each of which
  221. has a text name and an integer. The specified I<name> is
  222. found on the list, it puts the index in I<*result>, and returns
  223. 1. If not found, it returns 0.
  224. The following functions can be used after processing all the options.
  225. The opt_num_rest() function returns what is left.
  226. The opt_rest() function returns a pointer to the first non-option.
  227. If there were no parameters, it will point to the NULL that is
  228. at the end of the standard I<argv> array.
  229. The opt_legacy_okay() function returns true if no options have been
  230. specified that would preclude using legacy code paths. Currently,
  231. the various provider options preclude legacy operation. This means,
  232. for example, that specifying both B<-provider> and B<-engine> in the
  233. same command line will not work as expected.
  234. =head2 Common Options
  235. There are a few groups of options that are common to many OpenSSL programs.
  236. These are handled with sets of macros that define common option names
  237. and common code to handle them. The categories are identified by a
  238. letter:
  239. V Validation
  240. X Extended certificate
  241. S TLS/SSL
  242. R Random state
  243. The B<OPT_x_ENUM> macro is used to define the numeration values, where B<x>
  244. is one of the letters above. The B<OPT_x_OPTIONS> macro is used to
  245. list the set of common options, and the B<OPT_x_CASES> is used in
  246. the C<switch> statement.
  247. The common options are used throughout the sources for the OpenSSL commands.
  248. They are also used with common descriptions when generating the
  249. manpages, in the file F<doc/perlvars.pm>, which follow a similar naming
  250. convention.
  251. =head1 RETURN VALUES
  252. Detailed above.
  253. =head1 EXAMPLES
  254. The best examples can be found in sources for the commands in the F<apps>
  255. directory of the source tree.
  256. A notable exception is F<apps/cmp.c> which uses this API, but does
  257. things very differently.
  258. =head1 COPYRIGHT
  259. Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  260. Licensed under the Apache License 2.0 (the "License"). You may not use this
  261. file except in compliance with the License. You can obtain a copy in the file
  262. LICENSE in the source distribution or at
  263. L<https://www.openssl.org/source/license.html>.
  264. =cut