OPTIONS.pod 12 KB

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