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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/* cmdline.c: a reentrant version of getopt(). Written 2006 by Brian
* Raiter. This code is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cmdline.h"
#define docallback(opt, val) \
do { if ((r = callback(opt, val, data)) != 0) return r; } while (0)
/* Parse the given cmdline arguments.
*/
int readoptions(option const* list, int argc, char **argv,
int (*callback)(int, char const*, void*), void *data)
{
char argstring[] = "--";
option const *opt;
char const *val;
char const *p;
int stop = 0;
int argi, len, r;
if (!list || !callback)
return -1;
for (argi = 1 ; argi < argc ; ++argi)
{
/* First, check for "--", which forces all remaining arguments
* to be treated as non-options.
*/
if (!stop && argv[argi][0] == '-' && argv[argi][1] == '-'
&& argv[argi][2] == '\0') {
stop = 1;
continue;
}
/* Arguments that do not begin with '-' (or are only "-") are
* not options.
*/
if (stop || argv[argi][0] != '-' || argv[argi][1] == '\0') {
docallback(0, argv[argi]);
continue;
}
if (argv[argi][1] == '-')
{
/* Arguments that begin with a double-dash are long
* options.
*/
p = argv[argi] + 2;
val = strchr(p, '=');
if (val)
len = val++ - p;
else
len = strlen(p);
/* Is it on the list of valid options? If so, does it
* expect a parameter?
*/
for (opt = list ; opt->optval ; ++opt)
if (opt->name && !strncmp(p, opt->name, len)
&& !opt->name[len])
break;
if (!opt->optval) {
docallback('?', argv[argi]);
} else if (!val && opt->arg == 1) {
docallback(':', argv[argi]);
} else if (val && opt->arg == 0) {
docallback('=', argv[argi]);
} else {
docallback(opt->optval, val);
}
}
else
{
/* Arguments that begin with a single dash contain one or
* more short options. Each character in the argument is
* examined in turn, unless a parameter consumes the rest
* of the argument (or possibly even the following
* argument).
*/
for (p = argv[argi] + 1 ; *p ; ++p) {
for (opt = list ; opt->optval ; ++opt)
if (opt->chname == *p)
break;
if (!opt->optval) {
argstring[1] = *p;
docallback('?', argstring);
continue;
} else if (opt->arg == 0) {
docallback(opt->optval, NULL);
continue;
} else if (p[1]) {
docallback(opt->optval, p + 1);
break;
} else if (argi + 1 < argc && strcmp(argv[argi + 1], "--")) {
++argi;
docallback(opt->optval, argv[argi]);
break;
} else if (opt->arg == 2) {
docallback(opt->optval, NULL);
continue;
} else {
argstring[1] = *p;
docallback(':', argstring);
break;
}
}
}
}
return 0;
}
/* Verify that str points to an ASCII zero or one (optionally with
* whitespace) and return the value present, or -1 if str's contents
* are anything else.
*/
static int readboolvalue(char const *str)
{
char d;
while (isspace(*str))
++str;
if (!*str)
return -1;
d = *str++;
while (isspace(*str))
++str;
if (*str)
return -1;
if (d == '0')
return 0;
else if (d == '1')
return 1;
else
return -1;
}
/* Parse a configuration file.
*/
int readcfgfile(option const* list, FILE *fp,
int (*callback)(int, char const*, void*), void *data)
{
char buf[1024];
option const *opt;
char *name, *val, *p;
int len, f, r;
while (fgets(buf, sizeof buf, fp) != NULL)
{
/* Strip off the trailing newline and any leading whitespace.
* If the line begins with a hash sign, skip it entirely.
*/
len = strlen(buf);
if (len && buf[len - 1] == '\n')
buf[--len] = '\0';
for (p = buf ; isspace(*p) ; ++p) ;
if (!*p || *p == '#')
continue;
/* Find the end of the option's name and the beginning of the
* parameter, if any.
*/
for (name = p ; *p && *p != '=' && !isspace(*p) ; ++p) ;
len = p - name;
for ( ; *p == '=' || isspace(*p) ; ++p) ;
val = p;
/* Is it on the list of valid options? Does it take a
* full parameter, or just an optional boolean?
*/
for (opt = list ; opt->optval ; ++opt)
if (opt->name && !strncmp(name, opt->name, len)
&& !opt->name[len])
break;
if (!opt->optval) {
docallback('?', name);
} else if (!*val && opt->arg == 1) {
docallback(':', name);
} else if (*val && opt->arg == 0) {
f = readboolvalue(val);
if (f < 0)
docallback('=', name);
else if (f == 1)
docallback(opt->optval, NULL);
} else {
docallback(opt->optval, val);
}
}
return ferror(fp) ? -1 : 0;
}
/* Turn a string containing a cmdline into an argc-argv pair.
*/
int makecmdline(char const *cmdline, int *argcp, char ***argvp)
{
char **argv;
int argc;
char const *s;
int n, quoted;
if (!cmdline)
return 0;
/* Calcuate argc by counting the number of "clumps" of non-spaces.
*/
for (s = cmdline ; isspace(*s) ; ++s) ;
if (!*s) {
*argcp = 1;
if (argvp) {
*argvp = malloc(2 * sizeof(char*));
if (!*argvp)
return 0;
(*argvp)[0] = NULL;
(*argvp)[1] = NULL;
}
return 1;
}
for (argc = 2, quoted = 0 ; *s ; ++s) {
if (quoted == '"') {
if (*s == '"')
quoted = 0;
else if (*s == '\\' && s[1])
++s;
} else if (quoted == '\'') {
if (*s == '\'')
quoted = 0;
} else {
if (isspace(*s)) {
for ( ; isspace(s[1]) ; ++s) ;
if (!s[1])
break;
++argc;
} else if (*s == '"' || *s == '\'') {
quoted = *s;
}
}
}
*argcp = argc;
if (!argvp)
return 1;
/* Allocate space for all the arguments and their pointers.
*/
argv = malloc((argc + 1) * sizeof(char*) + strlen(cmdline) + 1);
*argvp = argv;
if (!argv)
return 0;
argv[0] = NULL;
argv[1] = (char*)(argv + argc + 1);
/* Copy the string into the allocated memory immediately after the
* argv array. Where spaces immediately follows a nonspace,
* replace it with a \0. Where a nonspace immediately follows
* spaces, store a pointer to it. (Except, of course, when the
* space-nonspace transitions occur within quotes.)
*/
for (s = cmdline ; isspace(*s) ; ++s) ;
for (argc = 1, n = 0, quoted = 0 ; *s ; ++s) {
if (quoted == '"') {
if (*s == '"') {
quoted = 0;
} else {
if (*s == '\\' && s[1])
++s;
argv[argc][n++] = *s;
}
} else if (quoted == '\'') {
if (*s == '\'')
quoted = 0;
else
argv[argc][n++] = *s;
} else {
if (isspace(*s)) {
argv[argc][n] = '\0';
for ( ; isspace(s[1]) ; ++s) ;
if (!s[1])
break;
argv[argc + 1] = argv[argc] + n + 1;
++argc;
n = 0;
} else {
if (*s == '"' || *s == '\'')
quoted = *s;
else
argv[argc][n++] = *s;
}
}
}
argv[argc + 1] = NULL;
return 1;
}