xref: /freebsd/crypto/openssl/doc/internal/man3/OPTIONS.pod (revision 44096ebd22ddd0081a357011714eff8963614b65)
1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre ProncheryOPTIONS, OPT_PAIR, OPT_COMMON, OPT_ERR, OPT_EOF, OPT_HELP,
6b077aed3SPierre Proncheryopt_init, opt_progname, opt_appname, opt_getprog, opt_help,
7b077aed3SPierre Proncheryopt_begin, opt_next, opt_flag, opt_arg, opt_unknown, opt_cipher,
8b077aed3SPierre Proncheryopt_cipher_any, opt_cipher_silent, opt_md,
9b077aed3SPierre Proncheryopt_int, opt_int_arg, opt_long, opt_ulong, opt_intmax, opt_uintmax,
10b077aed3SPierre Proncheryopt_format, opt_isdir, opt_string, opt_pair,
11b077aed3SPierre Proncheryopt_num_rest, opt_rest, opt_legacy_okay
12b077aed3SPierre Pronchery- Option parsing for commands and tests
13b077aed3SPierre Pronchery
14b077aed3SPierre Pronchery=head1 SYNOPSIS
15b077aed3SPierre Pronchery
16b077aed3SPierre Pronchery #include "opt.h"
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery typedef struct { ... }  OPTIONS;
19b077aed3SPierre Pronchery typedef struct { ... } OPT_PAIR;
20b077aed3SPierre Pronchery #define OPT_COMMON
21b077aed3SPierre Pronchery #define OPT_ERR
22b077aed3SPierre Pronchery #define OPT_EOF
23b077aed3SPierre Pronchery #define OPT_HELP
24b077aed3SPierre Pronchery
25b077aed3SPierre Pronchery char *opt_init(int argc, char **argv, const OPTIONS *o);
26b077aed3SPierre Pronchery char *opt_progname(const char *argv0);
27b077aed3SPierre Pronchery char *opt_appname(const char *argv0);
28b077aed3SPierre Pronchery char *opt_getprog(void);
29b077aed3SPierre Pronchery void opt_help(const OPTIONS *list);
30b077aed3SPierre Pronchery
31b077aed3SPierre Pronchery void opt_begin(void);
32b077aed3SPierre Pronchery int opt_next(void);
33b077aed3SPierre Pronchery char *opt_flag(void);
34b077aed3SPierre Pronchery char *opt_arg(void);
35b077aed3SPierre Pronchery char *opt_unknown(void);
36b077aed3SPierre Pronchery int opt_cipher(const char *name, EVP_CIPHER **cipherp);
37b077aed3SPierre Pronchery int opt_cipher_any(const char *name, EVP_CIPHER **cipherp);
38b077aed3SPierre Pronchery int opt_cipher_silent(const char *name, EVP_CIPHER **cipherp);
39b077aed3SPierre Pronchery int opt_md(const char *name, EVP_MD **mdp);
40b077aed3SPierre Pronchery
41b077aed3SPierre Pronchery int opt_int(const char *value, int *result);
42b077aed3SPierre Pronchery int opt_int_arg(void);
43b077aed3SPierre Pronchery int opt_long(const char *value, long *result);
44b077aed3SPierre Pronchery int opt_ulong(const char *value, unsigned long *result);
45b077aed3SPierre Pronchery int opt_intmax(const char *value, intmax_t *result);
46b077aed3SPierre Pronchery int opt_uintmax(const char *value, uintmax_t *result);
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery int opt_format(const char *s, unsigned long flags, int *result);
49b077aed3SPierre Pronchery int opt_isdir(const char *name);
50b077aed3SPierre Pronchery int opt_string(const char *name, const char **options);
51b077aed3SPierre Pronchery int opt_pair(const char *name, const OPT_PAIR* pairs, int *result);
52b077aed3SPierre Pronchery
53b077aed3SPierre Pronchery int opt_num_rest(void);
54b077aed3SPierre Pronchery char **opt_rest(void);
55b077aed3SPierre Pronchery
56b077aed3SPierre Pronchery int opt_legacy_okay(void);
57b077aed3SPierre Pronchery
58b077aed3SPierre Pronchery=head1 DESCRIPTION
59b077aed3SPierre Pronchery
60b077aed3SPierre ProncheryThe functions on this page provide a common set of option-parsing for
61b077aed3SPierre Proncherythe OpenSSL command and the internal test programs.
62b077aed3SPierre ProncheryIt is intended to be used like the standard getopt(3) routine, except
63b077aed3SPierre Proncherythat multi-character flag names are supported, and a variety of parsing
64b077aed3SPierre Proncheryand other utility functions are also provided.
65b077aed3SPierre Pronchery
66b077aed3SPierre ProncheryPrograms that use this should make sure to set the appropriate C<-I>
67b077aed3SPierre Proncheryflag.
68b077aed3SPierre Pronchery
69b077aed3SPierre ProncheryThese routines expect a global B<BIO> named B<bio_err> to point to
70b077aed3SPierre Proncherythe equivalent of B<stderr>. This is already done in the OpenSSL
71b077aed3SPierre Proncheryapplication.
72b077aed3SPierre Pronchery
73b077aed3SPierre Pronchery=head2 Data Types
74b077aed3SPierre Pronchery
75b077aed3SPierre ProncheryEach program should define, near the main() routine, an enumeration
76b077aed3SPierre Proncherythat is the set of options the program accepts. For example:
77b077aed3SPierre Pronchery
78b077aed3SPierre Pronchery    typedef enum OPTION_choice {
79b077aed3SPierre Pronchery        OPT_COMMON,
80b077aed3SPierre Pronchery        OPT_YES, OPT_NAME, OPT_COUNT, OPT_OFILE,
81b077aed3SPierre Pronchery        ...
82b077aed3SPierre Pronchery    } OPTION_CHOICE;
83b077aed3SPierre Pronchery
84b077aed3SPierre ProncheryThe first two lines must appear exactly as shown.
85b077aed3SPierre ProncheryOPT_COMMON is a macro that expands to C<OPT_ERR = -1, OPT_EOF = 0, OPT_HELP>.
86b077aed3SPierre ProncheryIn addition to defining symbolic names for the constants that opt_next()
87b077aed3SPierre Proncheryreturns, it also helps guarantee that every command has a C<-help> option.
88b077aed3SPierre ProncheryThe third line is a sample
89b077aed3SPierre Proncheryset of flags, and the closing C<typedef> name is used for error-checking
90b077aed3SPierre Proncheryas discussed below.
91b077aed3SPierre ProncheryBy declaring the variable as an C<OPTION_CHOICE>, with the right warning
92b077aed3SPierre Proncheryflags, the compiler could check that all specified options are handled.
93b077aed3SPierre Pronchery
94b077aed3SPierre ProncheryThe B<OPTIONS> C<typedef> specifies an option: what type of argument
95b077aed3SPierre Proncheryit takes (if any), and an optional "help" string.  It is a C<struct>
96b077aed3SPierre Proncherycontaining these fields:
97b077aed3SPierre Pronchery
98b077aed3SPierre Pronchery    const char *name;
99b077aed3SPierre Pronchery    int retval;
100b077aed3SPierre Pronchery    int valtype;
101b077aed3SPierre Pronchery    const char *helpstr;
102b077aed3SPierre Pronchery
103b077aed3SPierre ProncheryThe B<name> is the name of the option that the user would type. Options
104b077aed3SPierre Proncheryare words prefaced with a minus sign. If the user uses two minus signs,
105b077aed3SPierre Proncherythis is also accepted for compatibility with other GNU software. Some
106b077aed3SPierre Proncherynames are special, and are described below.
107b077aed3SPierre Pronchery
108b077aed3SPierre ProncheryThe B<retval> is the value to return if the option is found. It should be
109b077aed3SPierre Proncheryone of the choices in the enumeration above.
110b077aed3SPierre Pronchery
111b077aed3SPierre ProncheryThe B<valtype> defines what the option's parameter must be. It should
112b077aed3SPierre Proncherybe chosen from the following set:
113b077aed3SPierre Pronchery
114b077aed3SPierre Pronchery    \0  No value
115b077aed3SPierre Pronchery    '-' No value
116b077aed3SPierre Pronchery    's' A text string
117b077aed3SPierre Pronchery    '/' A directory
118b077aed3SPierre Pronchery    '<' Name of file to open for input
119b077aed3SPierre Pronchery    '>' Name of file to open for output
120b077aed3SPierre Pronchery    'n' A signed number that fits in the C<int> type
121b077aed3SPierre Pronchery    'p' A positive number that fits in the C<int> type
122b077aed3SPierre Pronchery    'N' A nonnegative number that fits in the C<int> type
123b077aed3SPierre Pronchery    'M' A signed number that fits in the C<intmax_t> type
124b077aed3SPierre Pronchery    'U' An unsigned number that fits in the C<uintmax_t> type
125b077aed3SPierre Pronchery    'l' A signed number that fits in the C<long> type
126b077aed3SPierre Pronchery    'u' An unsigned number that fits in the C<unsigned long> type
127b077aed3SPierre Pronchery    'c' File in PEM, DER, or S/MIME format
128b077aed3SPierre Pronchery    'F' A file in PEM or DER format
129b077aed3SPierre Pronchery    'E' Like 'F' but also allows ENGINE
130b077aed3SPierre Pronchery    'f' Any file format
131b077aed3SPierre Pronchery
132b077aed3SPierre ProncheryThe B<helpstr> is what to display when the user uses the help option,
133b077aed3SPierre Proncherywhich should be C<"help">.
134b077aed3SPierre Pronchery
135b077aed3SPierre ProncheryA program should declare its options right after the enumeration,
136b077aed3SPierre Proncheryand should follow the ordering of the enumeration as this helps
137b077aed3SPierre Proncheryreadability and maintainability:
138b077aed3SPierre Pronchery
139b077aed3SPierre Pronchery    static OPTIONS my_options[] = {
140b077aed3SPierre Pronchery        {"help", OPT_HELP, '-', "Display this summary"},
141b077aed3SPierre Pronchery        {"yes", OPT_YES, '-', "Print an affirmative reply"},
142b077aed3SPierre Pronchery        {"count", OPT_COUNT, 'p', "Repeat count"},
143b077aed3SPierre Pronchery        {"output" OPT_OFILE, '>', "Output file; default is stdout"},
144b077aed3SPierre Pronchery        {NULL}
145b077aed3SPierre Pronchery    };
146b077aed3SPierre Pronchery
147b077aed3SPierre ProncheryNote that the B<OPT_HELP> option is explicitly listed, and the list ends with
148b077aed3SPierre Proncheryan entry of all-null's. The other two special options, B<OPT_ERR> and B<OPT_EOF>
149b077aed3SPierre Proncheryshould not appear in the array.
150b077aed3SPierre Pronchery
151b077aed3SPierre ProncheryIf the help string is too long to fit into one line, it may be continued
152b077aed3SPierre Proncheryon multiple lines; each entry should use B<OPT_MORE_STR>, like this:
153b077aed3SPierre Pronchery
154b077aed3SPierre Pronchery        {"output" OPT_OFILE, '>', "Output file; default is stdout"},
155b077aed3SPierre Pronchery        {OPT_MORE_STR, 0, 0,
156b077aed3SPierre Pronchery         "This flag is not really needed on Unix systems"},
157b077aed3SPierre Pronchery        {OPT_MORE_STR, 0, 0,
158*44096ebdSEnji Cooper         "(Unix and descendents for the win!)"}
159b077aed3SPierre Pronchery
160b077aed3SPierre ProncheryEach subsequent line will be indented the correct amount.
161b077aed3SPierre Pronchery
162b077aed3SPierre ProncheryBy default, the help display will include a standard prolog:
163b077aed3SPierre Pronchery
164b077aed3SPierre Pronchery    Usage: PROGRAM [options]
165b077aed3SPierre Pronchery    Valid options are:
166b077aed3SPierre Pronchery    ...detailed list of options...
167b077aed3SPierre Pronchery
168b077aed3SPierre ProncherySometimes there are parameters that should appear in the synopsis.
169b077aed3SPierre ProncheryUse B<OPT_HELP_STR> as the first entry in your array:
170b077aed3SPierre Pronchery
171b077aed3SPierre Pronchery    {OPT_HELP_STR, 1, '-', Usage: %s [options] [text...]\n"}
172b077aed3SPierre Pronchery
173b077aed3SPierre ProncheryThe B<retval> and B<valtype> are ignored, and the B<helpstr> should
174b077aed3SPierre Proncheryfollow the general construction as shown. The C<%s> will get the program
175b077aed3SPierre Proncheryname.
176b077aed3SPierre Pronchery
177b077aed3SPierre ProncheryIf a command has a large set of options, it can be useful to break them
178b077aed3SPierre Proncheryinto sections.  Use the macro B<OPT_SECTION> or B<OPT_SECTION_STR>
179b077aed3SPierre Proncheryto indicate this. The two lines below are equivalent:
180b077aed3SPierre Pronchery
181b077aed3SPierre Pronchery    OPT_SECTION("Validation"),
182b077aed3SPierre Pronchery    {OPT_SECTION_STR, 1, '-', "Validation options:\n"},
183b077aed3SPierre Pronchery
184b077aed3SPierre ProncheryIn addition to providing help about options, you can provide a description
185b077aed3SPierre Proncheryof the parameters a command takes. These should appear at the end of
186b077aed3SPierre Proncherythe options and are indicated by using B<OPT_PARAM_STR> or the
187b077aed3SPierre ProncheryB<OPT_PARAMETERS> macro:
188b077aed3SPierre Pronchery
189b077aed3SPierre Pronchery    OPT_PARAMETERS()
190b077aed3SPierre Pronchery    {OPT_PARAM_STR, 1, '-', "Parameters:\n"}
191b077aed3SPierre Pronchery
192b077aed3SPierre ProncheryEvery "option" after after this should contain the parameter and
193b077aed3SPierre Proncherythe help string:
194b077aed3SPierre Pronchery
195b077aed3SPierre Pronchery    {"text", 0, 0, "Words to display (optional)"},
196b077aed3SPierre Pronchery
197b077aed3SPierre Pronchery=head2 Functions
198b077aed3SPierre Pronchery
199b077aed3SPierre ProncheryThe opt_init() function takes the I<argc> and I<argv> arguments given to main()
200b077aed3SPierre Proncheryand a pointer I<o> to the list of options. It returns the simple program
201b077aed3SPierre Proncheryname, as defined by opt_progname().
202b077aed3SPierre Pronchery
203b077aed3SPierre ProncheryThe opt_progname() function takes the full pathname C<argv[0]> in its I<arg0>
204b077aed3SPierre Proncheryparameter and returns
205b077aed3SPierre Proncherythe simple short name of the executable, to be used for error messages and
206b077aed3SPierre Proncherythe like.
207b077aed3SPierre Pronchery
208b077aed3SPierre ProncheryThe opt_appname() function takes in its I<argv0> parameter
209b077aed3SPierre Proncherythe "application" name (such
210b077aed3SPierre Proncheryas the specific command from L<openssl(1)> and appends it to the program
211b077aed3SPierre Proncheryname. This function should only be called once.
212b077aed3SPierre Pronchery
213b077aed3SPierre ProncheryThe opt_getprog() function returns the value set by opt_appname().
214b077aed3SPierre Pronchery
215b077aed3SPierre ProncheryThe opt_help() function takes a list of option definitions and prints a
216b077aed3SPierre Proncherynicely-formatted output.
217b077aed3SPierre Pronchery
218b077aed3SPierre ProncheryThe opt_begin() function, which is called automatically by opt_init(),
219b077aed3SPierre Proncherycan be used to reset the option parsing loop.
220b077aed3SPierre Pronchery
221b077aed3SPierre ProncheryThe opt_next() function is called, once opt_init() has been called,
222b077aed3SPierre Proncheryin a loop to fetch each option in turn. It returns -1, or B<OPT_EOF> when the
223b077aed3SPierre Proncheryend of arguments has been reached. This is typically done like this:
224b077aed3SPierre Pronchery
225b077aed3SPierre Pronchery    prog = opt_init(argc, argv, my_options);
226b077aed3SPierre Pronchery    while ((o = opt_next()) != OPT_EOF) {
227b077aed3SPierre Pronchery        switch (o) {
228b077aed3SPierre Pronchery        case OPT_EOF:
229b077aed3SPierre Pronchery        case OPT_ERR:
230b077aed3SPierre Pronchery    opthelp:
231b077aed3SPierre Pronchery            fprintf(stderr, "%s: Use -help for summary\n", prog);
232b077aed3SPierre Pronchery            exit(1);
233b077aed3SPierre Pronchery        case OPT_HELP:
234b077aed3SPierre Pronchery            opt_help(my_options);
235b077aed3SPierre Pronchery            exit(0);
236b077aed3SPierre Pronchery        ...other options...
237b077aed3SPierre Pronchery        }
238b077aed3SPierre Pronchery    }
239b077aed3SPierre Pronchery
240b077aed3SPierre ProncheryWithin the option parsing loop, the following functions may be called.
241b077aed3SPierre Pronchery
242b077aed3SPierre ProncheryThe opt_flag() function returns the most recent option name
243b077aed3SPierre Proncheryincluding the preceding C<->.
244b077aed3SPierre Pronchery
245b077aed3SPierre ProncheryThe opt_arg() function returns the option's argument value, if there is one.
246b077aed3SPierre Pronchery
247b077aed3SPierre ProncheryThe opt_unknown() function returns the unknown option.
248b077aed3SPierre ProncheryIn an option list, there can be at most one option with the empty string.
249b077aed3SPierre ProncheryThis is a "wildcard" or "unknown" option. For example, it allows an
250b077aed3SPierre Proncheryoption to be be taken as digest algorithm, like C<-sha1>. The function
251b077aed3SPierre Proncheryopt_md() takes the specified I<name> and fills in the digest into I<mdp>.
252b077aed3SPierre ProncheryThe functions opt_cipher(), opt_cipher_any() and opt_cipher_silent()
253b077aed3SPierre Proncheryeach takes the specified I<name> and fills in the cipher into I<cipherp>.
254b077aed3SPierre ProncheryThe function opt_cipher() only accepts ciphers which are not
255b077aed3SPierre ProncheryAEAD and are not using XTS mode.  The functions opt_cipher_any() and
256b077aed3SPierre Proncheryopt_cipher_silent() accept any cipher, the latter not emitting an error
257b077aed3SPierre Proncheryif the cipher is not located.
258b077aed3SPierre Pronchery
259b077aed3SPierre ProncheryThere are a several useful functions for parsing numbers.  These are
260b077aed3SPierre Proncheryopt_int(), opt_long(), opt_ulong(), opt_intmax(), and opt_uintmax().  They all
261b077aed3SPierre Proncherytake C<0x> to mean hexadecimal and C<0> to mean octal, and will do the
262b077aed3SPierre Proncherynecessary range-checking. They return 1 if successful and fill in the
263b077aed3SPierre ProncheryC<result> pointer with the value, or 0 on error. Note that opt_next()
264b077aed3SPierre Proncherywill also do range-check on the argument if the appropriate B<valtype>
265b077aed3SPierre Proncheryfield is specified for the option. This means that error-checking inside
266b077aed3SPierre Proncherythe C<switch> C<case> can often be elided.
267b077aed3SPierre Pronchery
268b077aed3SPierre ProncheryThe opt_int_arg() function is a convenience abbreviation to opt_int().
269b077aed3SPierre ProncheryIt parses and returns an integer, assuming its range has been checked before.
270b077aed3SPierre Pronchery
271b077aed3SPierre ProncheryThe opt_format() function takes a string value,
272b077aed3SPierre Proncherysuch as used with the B<-informat> or similar option, and fills
273b077aed3SPierre Proncherythe value from the constants in F<fmt.h> file.
274b077aed3SPierre Pronchery
275b077aed3SPierre ProncheryThe opt_isdir() function returns 1 if the specified I<name> is
276b077aed3SPierre Proncherya directory, or 0 if not.
277b077aed3SPierre Pronchery
278b077aed3SPierre ProncheryThe opt_string() function checks that I<name> appears in the
279b077aed3SPierre ProncheryNULL-terminated array of strings. It returns 1 if found,
280b077aed3SPierre Proncheryor prints a diagnostic and returns 0 if not.
281b077aed3SPierre Pronchery
282b077aed3SPierre ProncheryThe opt_pair() function takes a list of I<pairs>, each of which
283b077aed3SPierre Proncheryhas a text name and an integer. The specified I<name> is
284b077aed3SPierre Proncheryfound on the list, it puts the index in I<*result>, and returns
285b077aed3SPierre Pronchery1. If not found, it returns 0.
286b077aed3SPierre Pronchery
287b077aed3SPierre ProncheryThe following functions can be used after processing all the options.
288b077aed3SPierre Pronchery
289b077aed3SPierre ProncheryThe opt_num_rest() function returns what is left.
290b077aed3SPierre Pronchery
291b077aed3SPierre ProncheryThe opt_rest() function returns a pointer to the first non-option.
292b077aed3SPierre ProncheryIf there were no parameters, it will point to the NULL that is
293b077aed3SPierre Proncheryat the end of the standard I<argv> array.
294b077aed3SPierre Pronchery
295b077aed3SPierre ProncheryThe opt_legacy_okay() function returns true if no options have been
296b077aed3SPierre Proncheryspecified that would preclude using legacy code paths.  Currently,
297b077aed3SPierre Proncherythe various provider options preclude legacy operation.  This means,
298b077aed3SPierre Proncheryfor example, that specifying both B<-provider> and B<-engine> in the
299b077aed3SPierre Proncherysame command line will not work as expected.
300b077aed3SPierre Pronchery
301b077aed3SPierre Pronchery=head2 Common Options
302b077aed3SPierre Pronchery
303b077aed3SPierre ProncheryThere are a few groups of options that are common to many OpenSSL programs.
304b077aed3SPierre ProncheryThese are handled with sets of macros that define common option names
305b077aed3SPierre Proncheryand common code to handle them.  The categories are identified by a
306b077aed3SPierre Proncheryletter:
307b077aed3SPierre Pronchery
308b077aed3SPierre Pronchery    V   Validation
309b077aed3SPierre Pronchery    X   Extended certificate
310b077aed3SPierre Pronchery    S   TLS/SSL
311b077aed3SPierre Pronchery    R   Random state
312b077aed3SPierre Pronchery
313b077aed3SPierre ProncheryThe B<OPT_x_ENUM> macro is used to define the numeration values, where B<x>
314b077aed3SPierre Proncheryis one of the letters above.  The B<OPT_x_OPTIONS> macro is used to
315b077aed3SPierre Proncherylist the set of common options, and the B<OPT_x_CASES> is used in
316b077aed3SPierre Proncherythe C<switch> statement.
317b077aed3SPierre Pronchery
318b077aed3SPierre ProncheryThe common options are used throughout the sources for the OpenSSL commands.
319b077aed3SPierre ProncheryThey are also used with common descriptions when generating the
320b077aed3SPierre Proncherymanpages, in the file F<doc/perlvars.pm>, which follow a similar naming
321b077aed3SPierre Proncheryconvention.
322b077aed3SPierre Pronchery
323b077aed3SPierre Pronchery=head1 RETURN VALUES
324b077aed3SPierre Pronchery
325b077aed3SPierre ProncheryDetailed above.
326b077aed3SPierre Pronchery
327b077aed3SPierre Pronchery=head1 EXAMPLES
328b077aed3SPierre Pronchery
329b077aed3SPierre ProncheryThe best examples can be found in sources for the commands in the F<apps>
330b077aed3SPierre Proncherydirectory of the source tree.
331b077aed3SPierre ProncheryA notable exception is F<apps/cmp.c> which uses this API, but does
332b077aed3SPierre Proncherythings very differently.
333b077aed3SPierre Pronchery
334b077aed3SPierre Pronchery=head1 COPYRIGHT
335b077aed3SPierre Pronchery
336*44096ebdSEnji CooperCopyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
337b077aed3SPierre Pronchery
338b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use this
339b077aed3SPierre Proncheryfile except in compliance with the License.  You can obtain a copy in the file
340b077aed3SPierre ProncheryLICENSE in the source distribution or at
341b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
342b077aed3SPierre Pronchery
343b077aed3SPierre Pronchery=cut
344