summaryrefslogtreecommitdiff
path: root/src/yopt.h
blob: 86ee1b92baab518f7311942d81c503687938cec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* ncdu - NCurses Disk Usage

  Copyright (c) 2007-2012 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

/* This is a simple command-line option parser. Operation is similar to
 * getopt_long(), except with a cleaner API.
 *
 * This is implemented in a single header file, as it's pretty small and you
 * generally only use an option parser in a single .c file in your program.
 *
 * Supports (examples from GNU tar(1)):
 *   "--gzip"
 *   "--file <arg>"
 *   "--file=<arg>"
 *   "-z"
 *   "-f <arg>"
 *   "-f<arg>"
 *   "-zf <arg>"
 *   "-zf<arg>"
 *   "--" (To stop looking for futher options)
 *   "<arg>" (Non-option arguments)
 *
 * Issues/non-features:
 * - An option either requires an argument or it doesn't.
 * - No way to specify how often an option can/should be used.
 * - No way to specify the type of an argument (filename/integer/enum/whatever)
 */



#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>


typedef struct {
  /* Value yopt_next() will return for this option */
  int val;
  /* Whether this option needs an argument */
  int needarg;
  /* Name(s) of this option, prefixed with '-' or '--' and separated by a
   * comma. E.g. "-z", "--gzip", "-z,--gzip".
   * An option can have any number of aliasses.
   */
  const char *name;
} yopt_opt_t;


typedef struct {
  int argc;
  int cur;
  int argsep; /* '--' found */
  char **argv;
  char *sh;
  yopt_opt_t *opts;
  char errbuf[128];
} yopt_t;


/* opts must be an array of options, terminated with an option with val=0 */
static inline void yopt_init(yopt_t *o, int argc, char **argv, yopt_opt_t *opts) {
  o->argc = argc;
  o->argv = argv;
  o->opts = opts;
  o->cur = 0;
  o->argsep = 0;
  o->sh = NULL;
}


static inline yopt_opt_t *_yopt_find(yopt_opt_t *o, const char *v) {
  const char *tn, *tv;

  for(; o->val; o++) {
    tn = o->name;
    while(*tn) {
      tv = v;
      while(*tn && *tn != ',' && *tv && *tv != '=' && *tn == *tv) {
        tn++;
        tv++;
      }
      if(!(*tn && *tn != ',') && !(*tv && *tv != '='))
        return o;
      while(*tn && *tn != ',')
        tn++;
      while(*tn == ',')
        tn++;
    }
  }

  return NULL;
}


static inline int _yopt_err(yopt_t *o, char **val, const char *fmt, ...) {
  va_list va;
  va_start(va, fmt);
  vsnprintf(o->errbuf, sizeof(o->errbuf), fmt, va);
  va_end(va);
  *val = o->errbuf;
  return -2;
}


/* Return values:
 *  0 -> Non-option argument, val is its value
 * -1 -> Last argument has been processed
 * -2 -> Error, val will contain the error message.
 *  x -> Option with val = x found. If the option requires an argument, its
 *       value will be in val.
 */
static inline int yopt_next(yopt_t *o, char **val) {
  yopt_opt_t *opt;
  char sh[3];

  *val = NULL;
  if(o->sh)
    goto inshort;

  if(++o->cur >= o->argc)
    return -1;

  if(!o->argsep && o->argv[o->cur][0] == '-' && o->argv[o->cur][1] == '-' && o->argv[o->cur][2] == 0) {
    o->argsep = 1;
    if(++o->cur >= o->argc)
      return -1;
  }

  if(o->argsep || *o->argv[o->cur] != '-') {
    *val = o->argv[o->cur];
    return 0;
  }

  if(o->argv[o->cur][1] != '-') {
    o->sh = o->argv[o->cur]+1;
    goto inshort;
  }

  /* Now we're supposed to have a long option */
  if(!(opt = _yopt_find(o->opts, o->argv[o->cur])))
    return _yopt_err(o, val, "Unknown option '%s'", o->argv[o->cur]);
  if((*val = strchr(o->argv[o->cur], '=')) != NULL)
    (*val)++;
  if(!opt->needarg && *val)
    return _yopt_err(o, val, "Option '%s' does not accept an argument", o->argv[o->cur]);
  if(opt->needarg && !*val) {
    if(o->cur+1 >= o->argc)
      return _yopt_err(o, val, "Option '%s' requires an argument", o->argv[o->cur]);
    *val = o->argv[++o->cur];
  }
  return opt->val;

  /* And here we're supposed to have a short option */
inshort:
  sh[0] = '-';
  sh[1] = *o->sh;
  sh[2] = 0;
  if(!(opt = _yopt_find(o->opts, sh)))
    return _yopt_err(o, val, "Unknown option '%s'", sh);
  o->sh++;
  if(opt->needarg && *o->sh)
    *val = o->sh;
  else if(opt->needarg) {
    if(++o->cur >= o->argc)
      return _yopt_err(o, val, "Option '%s' requires an argument", sh);
    *val = o->argv[o->cur];
  }
  if(!*o->sh || opt->needarg)
    o->sh = NULL;
  return opt->val;
}