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
|
/*
* (c) Copyright 1990, Kim Fabricius Storm. All rights reserved.
* Copyright (c) 1996-2005 Michael T Pins. All rights reserved.
*
* Generic option parsing
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "global.h"
#include "nn.h"
#include "options.h"
#include "variable.h"
/* options.c */
static void dump_options(int type, char *tail);
static void error(char *message, char option_letter);
static char **save_argv, *usage_mesg;
static struct option_descr *save_optd;
extern int no_update;
char *
program_name(char **av)
{
char *cp;
/* skip "/path/" part of program name */
if ((cp = strrchr(*av, '/')))
return cp + 1;
else
return *av;
}
static void
dump_options(int type, char *tail)
{
register struct option_descr *od;
int any = 0;
for (od = save_optd; od->option_letter; od++) {
if (od->option_type != type)
continue;
fprintf(stderr, any ? "%c" : " -%c", od->option_letter);
any++;
}
if (any && tail && tail[0]) {
fprintf(stderr, "%s", tail);
}
}
static void
error(char *message, char option_letter)
{
char *prog_name = program_name(save_argv);
fprintf(stderr, "%s: ", prog_name);
fprintf(stderr, message, option_letter);
fputc('\n', stderr);
fprintf(stderr, "usage: %s", prog_name);
dump_options(1, "");
dump_options(2, " STR");
dump_options(3, " [STR]");
dump_options(4, " NUM");
dump_options(5, " [NUM]");
if (usage_mesg)
fprintf(stderr, "%s", usage_mesg);
fputc(NL, stderr);
nn_exit(9);
}
/* parse variables set on command line seperately from everything
* else, very early. This is necessary because open_master is done
* so early for nntp; don't alter the arg, so we can skip it later.
*/
void
parseargv_variables(char **av, int ac)
{
char *value;
while (--ac > 0) {
if (**++av != '-' && (value = strchr(*av, '='))) {
*value++ = NUL;
set_variable(*av, 1, value);
*--value = '='; /* restore for later parsing to skip it */
}
}
}
int
parse_options(int ac, char **av, char *envname, struct option_descr options[], char *usage)
{
register char *cp, opt;
register struct option_descr *od;
int files;
char **names;
char *envinit;
save_argv = av;
save_optd = options;
if (options == NULL)
return 0;
usage_mesg = usage;
--ac;
names = ++av;
files = 0;
envinit = envname ? getenv(envname) : NULL;
cp = envinit;
next_option:
if (envinit) {
while (*cp && isspace(*cp))
cp++;
if (*cp == '-') {
cp++;
goto next_option;
}
if (*cp == NUL) {
envinit = NULL;
goto next_option;
}
} else if (cp == NULL || *cp == NUL) {
if ((cp = *av++) == NULL) {
*names = NULL;
return files;
}
ac--;
if (*cp != '-') {
if (!strchr(cp, '=')) {
*names++ = cp;
cp = NULL;
files++;
}
/*
* else it's a variable that was handled in parseargv_variables()
* earlier, so nothing to do.
*/
cp = NULL;
goto next_option;
}
cp++; /* skip - */
}
opt = *cp++;
for (od = options; od->option_letter; od++) {
if (od->option_letter != opt)
continue;
switch (od->option_type) {
case 1: /* BOOL_OPTION */
*((int *) (od->option_address)) = !*((int *) (od->option_address));
goto next_option;
case 2: /* STRING_OPTION */
case 3: /* OPTIONAL_STRING */
/* -oSTR or -o STR */
while (*cp && isspace(*cp))
cp++;
if (*cp == NUL) {
if (envinit || ac == 0) {
if (od->option_type == 3)
goto opt_str;
error("missing string argumet to -%c", opt);
}
cp = *av++;
ac--;
}
if (od->option_type == 3 && *cp == '-') {
cp++;
goto opt_str;
}
*(od->option_address) = cp;
if (envinit) {
while (*cp && !isspace(*cp))
cp++;
if (*cp)
*cp++ = NUL;
} else
cp = NULL;
goto next_option;
opt_str:
*(od->option_address) = od->option_default;
goto next_option;
case 4:
case 5:
/* -oN or -o N */
while (*cp && isspace(*cp))
cp++;
if (*cp) {
if (!isdigit(*cp)) {
if (od->option_type == 5)
goto opt_int;
error("non-numeric argument to -%c", opt);
}
} else {
if (envinit || ac == 0 || !isdigit(**av)) {
if (od->option_type == 5)
goto opt_int;
error("missing argument to -%c", opt);
}
cp = *av++;
ac--;
}
*((int *) (od->option_address)) = atoi(cp);
while (isdigit(*cp))
cp++;
goto next_option;
opt_int:
*((int *) (od->option_address)) = (int) (od->option_default);
goto next_option;
}
}
error("unknown option '-%c'", opt);
/* NOTREACHED */
return 0;
}
|