pinentry-dmenu

a pinentry program based on dmenu
git clone anongit@rnpnr.xyz:pinentry-dmenu.git
Log | Files | Refs | Feed | README | LICENSE

argparse.h (7826B)


      1 /* argparse.h - Argument parser for option handling.
      2  *	Copyright (C) 1998,1999,2000,2001,2006 Free Software Foundation, Inc.
      3  *
      4  * This file is part of JNLIB, which is a subsystem of GnuPG.
      5  *
      6  * JNLIB is free software; you can redistribute it and/or modify it
      7  * under the terms of either
      8  *
      9  *   - the GNU Lesser General Public License as published by the Free
     10  *     Software Foundation; either version 3 of the License, or (at
     11  *     your option) any later version.
     12  *
     13  * or
     14  *
     15  *   - the GNU General Public License as published by the Free
     16  *     Software Foundation; either version 2 of the License, or (at
     17  *     your option) any later version.
     18  *
     19  * or both in parallel, as here.
     20  *
     21  * JNLIB is distributed in the hope that it will be useful, but
     22  * WITHOUT ANY WARRANTY; without even the implied warranty of
     23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     24  * General Public License for more details.
     25  *
     26  * You should have received a copies of the GNU General Public License
     27  * and the GNU Lesser General Public License along with this program;
     28  * if not, see <http://www.gnu.org/licenses/>.
     29  */
     30 
     31 #ifndef LIBJNLIB_ARGPARSE_H
     32 #define LIBJNLIB_ARGPARSE_H
     33 
     34 #include <stdio.h>
     35 
     36 typedef struct
     37 {
     38   int  *argc;	      /* Pointer to ARGC (value subject to change). */
     39   char ***argv;	      /* Pointer to ARGV (value subject to change). */
     40   unsigned int flags; /* Global flags.  May be set prior to calling the
     41                          parser.  The parser may change the value.  */
     42   int err;            /* Print error description for last option.
     43                          Either 0,  ARGPARSE_PRINT_WARNING or
     44                          ARGPARSE_PRINT_ERROR.  */
     45 
     46   int r_opt; 	      /* Returns option code. */
     47   int r_type;	      /* Returns type of option value.  */
     48   union {
     49     int   ret_int;
     50     long  ret_long;
     51     unsigned long ret_ulong;
     52     char *ret_str;
     53   } r;		      /* Return values */
     54 
     55   struct {
     56     int idx;
     57     int inarg;
     58     int stopped;
     59     const char *last;
     60     void *aliases;
     61     const void *cur_alias;
     62     void *iio_list;
     63   } internal;	    /* Private - do not change. */
     64 } ARGPARSE_ARGS;
     65 
     66 typedef struct
     67 {
     68   int          short_opt;
     69   const char  *long_opt;
     70   unsigned int flags;
     71   const char  *description; /* Optional option description. */
     72 } ARGPARSE_OPTS;
     73 
     74 
     75 /* Global flags (ARGPARSE_ARGS).  */
     76 #define ARGPARSE_FLAG_KEEP       1   /* Do not remove options form argv.     */
     77 #define ARGPARSE_FLAG_ALL        2   /* Do not stop at last option but return
     78                                         remaining args with R_OPT set to -1. */
     79 #define ARGPARSE_FLAG_MIXED      4   /* Assume options and args are mixed.   */
     80 #define ARGPARSE_FLAG_NOSTOP     8   /* Do not stop processing at "--".      */
     81 #define ARGPARSE_FLAG_ARG0      16   /* Do not skip the first arg.           */
     82 #define ARGPARSE_FLAG_ONEDASH   32   /* Allow long options with one dash.    */
     83 #define ARGPARSE_FLAG_NOVERSION 64   /* No output for "--version".           */
     84 
     85 #define ARGPARSE_FLAG_STOP_SEEN 256  /* Set to true if a "--" has been seen. */
     86 
     87 /* Flags for each option (ARGPARSE_OPTS).  The type code may be
     88    ORed with the OPT flags.  */
     89 #define ARGPARSE_TYPE_NONE        0  /* Does not take an argument.        */
     90 #define ARGPARSE_TYPE_INT         1  /* Takes an int argument.            */
     91 #define ARGPARSE_TYPE_STRING      2  /* Takes a string argument.          */
     92 #define ARGPARSE_TYPE_LONG        3  /* Takes a long argument.            */
     93 #define ARGPARSE_TYPE_ULONG       4  /* Takes an unsigned long argument.  */
     94 #define ARGPARSE_OPT_OPTIONAL (1<<3) /* Argument is optional.             */
     95 #define ARGPARSE_OPT_PREFIX   (1<<4) /* Allow 0x etc. prefixed values.    */
     96 #define ARGPARSE_OPT_IGNORE   (1<<6) /* Ignore command or option.         */
     97 #define ARGPARSE_OPT_COMMAND  (1<<7) /* The argument is a command.        */
     98 
     99 #define ARGPARSE_TYPE_MASK  7  /* Mask for the type values (internal).  */
    100 
    101 /* A set of macros to make option definitions easier to read.  */
    102 #define ARGPARSE_x(s,l,t,f,d) \
    103      { (s), (l), ARGPARSE_TYPE_ ## t | (f), (d) }
    104 
    105 #define ARGPARSE_s(s,l,t,d) \
    106      { (s), (l), ARGPARSE_TYPE_ ## t, (d) }
    107 #define ARGPARSE_s_n(s,l,d) \
    108      { (s), (l), ARGPARSE_TYPE_NONE, (d) }
    109 #define ARGPARSE_s_i(s,l,d) \
    110      { (s), (l), ARGPARSE_TYPE_INT, (d) }
    111 #define ARGPARSE_s_s(s,l,d) \
    112      { (s), (l), ARGPARSE_TYPE_STRING, (d) }
    113 #define ARGPARSE_s_l(s,l,d) \
    114      { (s), (l), ARGPARSE_TYPE_LONG, (d) }
    115 #define ARGPARSE_s_u(s,l,d) \
    116      { (s), (l), ARGPARSE_TYPE_ULONG, (d) }
    117 
    118 #define ARGPARSE_o(s,l,t,d) \
    119      { (s), (l), (ARGPARSE_TYPE_ ## t  | ARGPARSE_OPT_OPTIONAL), (d) }
    120 #define ARGPARSE_o_n(s,l,d) \
    121      { (s), (l), (ARGPARSE_TYPE_NONE   | ARGPARSE_OPT_OPTIONAL), (d) }
    122 #define ARGPARSE_o_i(s,l,d) \
    123      { (s), (l), (ARGPARSE_TYPE_INT    | ARGPARSE_OPT_OPTIONAL), (d) }
    124 #define ARGPARSE_o_s(s,l,d) \
    125      { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_OPTIONAL), (d) }
    126 #define ARGPARSE_o_l(s,l,d) \
    127      { (s), (l), (ARGPARSE_TYPE_LONG   | ARGPARSE_OPT_OPTIONAL), (d) }
    128 #define ARGPARSE_o_u(s,l,d) \
    129      { (s), (l), (ARGPARSE_TYPE_ULONG  | ARGPARSE_OPT_OPTIONAL), (d) }
    130 
    131 #define ARGPARSE_p(s,l,t,d) \
    132      { (s), (l), (ARGPARSE_TYPE_ ## t  | ARGPARSE_OPT_PREFIX), (d) }
    133 #define ARGPARSE_p_n(s,l,d) \
    134      { (s), (l), (ARGPARSE_TYPE_NONE   | ARGPARSE_OPT_PREFIX), (d) }
    135 #define ARGPARSE_p_i(s,l,d) \
    136      { (s), (l), (ARGPARSE_TYPE_INT    | ARGPARSE_OPT_PREFIX), (d) }
    137 #define ARGPARSE_p_s(s,l,d) \
    138      { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_PREFIX), (d) }
    139 #define ARGPARSE_p_l(s,l,d) \
    140      { (s), (l), (ARGPARSE_TYPE_LONG   | ARGPARSE_OPT_PREFIX), (d) }
    141 #define ARGPARSE_p_u(s,l,d) \
    142      { (s), (l), (ARGPARSE_TYPE_ULONG  | ARGPARSE_OPT_PREFIX), (d) }
    143 
    144 #define ARGPARSE_op(s,l,t,d) \
    145      { (s), (l), (ARGPARSE_TYPE_ ## t \
    146                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    147 #define ARGPARSE_op_n(s,l,d) \
    148      { (s), (l), (ARGPARSE_TYPE_NONE \
    149                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    150 #define ARGPARSE_op_i(s,l,d) \
    151      { (s), (l), (ARGPARSE_TYPE_INT \
    152                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    153 #define ARGPARSE_op_s(s,l,d) \
    154      { (s), (l), (ARGPARSE_TYPE_STRING \
    155                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    156 #define ARGPARSE_op_l(s,l,d) \
    157      { (s), (l), (ARGPARSE_TYPE_LONG \
    158                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    159 #define ARGPARSE_op_u(s,l,d) \
    160      { (s), (l), (ARGPARSE_TYPE_ULONG \
    161                   | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
    162 
    163 #define ARGPARSE_c(s,l,d) \
    164      { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_COMMAND), (d) }
    165 
    166 #define ARGPARSE_ignore(s,l) \
    167      { (s), (l), (ARGPARSE_OPT_IGNORE), "@" }
    168 
    169 #define ARGPARSE_group(s,d) \
    170      { (s), NULL, 0, (d) }
    171 
    172 #define ARGPARSE_end()  { 0, NULL, 0, NULL }
    173 
    174 
    175 /* Other constants.  */
    176 #define ARGPARSE_PRINT_WARNING  1
    177 #define ARGPARSE_PRINT_ERROR    2
    178 
    179 
    180 /* Error values.  */
    181 #define ARGPARSE_IS_ARG            (-1)
    182 #define ARGPARSE_INVALID_OPTION    (-2)
    183 #define ARGPARSE_MISSING_ARG       (-3)
    184 #define ARGPARSE_KEYWORD_TOO_LONG  (-4)
    185 #define ARGPARSE_READ_ERROR        (-5)
    186 #define ARGPARSE_UNEXPECTED_ARG    (-6)
    187 #define ARGPARSE_INVALID_COMMAND   (-7)
    188 #define ARGPARSE_AMBIGUOUS_OPTION  (-8)
    189 #define ARGPARSE_AMBIGUOUS_COMMAND (-9)
    190 #define ARGPARSE_INVALID_ALIAS     (-10)
    191 #define ARGPARSE_OUT_OF_CORE       (-11)
    192 #define ARGPARSE_INVALID_ARG       (-12)
    193 
    194 
    195 int arg_parse (ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
    196 int optfile_parse (FILE *fp, const char *filename, unsigned *lineno,
    197 		   ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
    198 void usage (int level);
    199 const char *strusage (int level);
    200 void set_strusage (const char *(*f)( int ));
    201 void argparse_register_outfnc (int (*fnc)(int, const char *));
    202 
    203 #endif /*LIBJNLIB_ARGPARSE_H*/