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