| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "squidconf.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#include "squid.h" |
| 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 |
int |
| 54 |
Getopt(int argc, char **argv, struct opt_s *opt, int nopts, char *usage, |
| 55 |
int *ret_optind, char **ret_optname, char **ret_optarg) |
| 56 |
{ |
| 57 |
int i; |
| 58 |
int arglen; |
| 59 |
int nmatch; |
| 60 |
static int optind = 1; |
| 61 |
static char *optptr = NULL; |
| 62 |
int opti = 0; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (optind >= argc || argv[optind][0] != '-' || strcmp(argv[optind], "-") == 0) |
| 69 |
{ |
| 70 |
*ret_optind = optind; |
| 71 |
*ret_optarg = NULL; |
| 72 |
*ret_optname = NULL; |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (strcmp(argv[optind], "--") == 0) |
| 80 |
{ |
| 81 |
optind++; |
| 82 |
*ret_optind = optind; |
| 83 |
*ret_optname = NULL; |
| 84 |
*ret_optarg = NULL; |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (optptr == NULL && strncmp(argv[optind], "--", 2) == 0) |
| 96 |
{ |
| 97 |
|
| 98 |
|
| 99 |
if ((optptr = strchr(argv[optind], '=')) != NULL) |
| 100 |
{ *optptr = '\0'; optptr++; } |
| 101 |
|
| 102 |
arglen = strlen(argv[optind]); |
| 103 |
nmatch = 0; |
| 104 |
for (i = 0; i < nopts; i++) |
| 105 |
if (opt[i].single == FALSE && |
| 106 |
strncmp(opt[i].name, argv[optind], arglen) == 0) |
| 107 |
{ |
| 108 |
nmatch++; |
| 109 |
opti = i; |
| 110 |
if (arglen == strlen(opt[i].name)) break; |
| 111 |
} |
| •112 |
if (nmatch > 1 |
| 113 |
&& i == nopts) |
| 114 |
Die("Option \"%s\" is ambiguous; please be more specific.\n%s", |
| 115 |
argv[optind], usage); |
| 116 |
if (nmatch == 0) |
| 117 |
Die("No such option \"%s\".\n%s", argv[optind], usage); |
| 118 |
|
| 119 |
*ret_optname = opt[opti].name; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if (opt[opti].argtype != sqdARG_NONE) |
| 124 |
{ |
| 125 |
if (optptr != NULL) |
| 126 |
{ |
| 127 |
*ret_optarg = optptr; |
| 128 |
optptr = NULL; |
| 129 |
optind++; |
| 130 |
} |
| 131 |
else if (optind+1 >= argc) |
| 132 |
Die("Option %s requires an argument\n%s", opt[opti].name, usage); |
| 133 |
else |
| 134 |
{ |
| 135 |
*ret_optarg = argv[optind+1]; |
| 136 |
optind+=2; |
| 137 |
} |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
if (optptr != NULL) |
| 142 |
Die("Option %s does not take an argument\n%s", opt[opti].name, usage); |
| 143 |
*ret_optarg = NULL; |
| 144 |
optind++; |
| 145 |
} |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
|
| 150 |
if (optptr == NULL) |
| 151 |
optptr = argv[optind]+1; |
| 152 |
for (opti = -1, i = 0; i < nopts; i++) |
| 153 |
if (opt[i].single == TRUE && *optptr == opt[i].name[1]) |
| 154 |
{ opti = i; break; } |
| 155 |
if (opti == -1) |
| 156 |
Die("No such option \"%c\".\n%s", *optptr, usage); |
| 157 |
*ret_optname = opt[opti].name; |
| 158 |
|
| 159 |
|
| 160 |
if (opt[opti].argtype != sqdARG_NONE) |
| 161 |
{ |
| 162 |
if (*(optptr+1) != '\0') |
| 163 |
{ |
| 164 |
*ret_optarg = optptr+1; |
| 165 |
optind++; |
| 166 |
} |
| 167 |
else if (optind+1 < argc) |
| 168 |
{ |
| 169 |
*ret_optarg = argv[optind+1]; |
| 170 |
optind+=2; |
| 171 |
} |
| 172 |
else Die("Option %s requires an argument\n%s", opt[opti].name, usage); |
| 173 |
|
| 174 |
optptr = NULL; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
*ret_optarg = NULL; |
| 179 |
if (*(optptr+1) != '\0') |
| 180 |
optptr++; |
| 181 |
else |
| 182 |
{ |
| 183 |
optind++; |
| 184 |
optptr = NULL; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (opt[opti].argtype != sqdARG_NONE) |
| 193 |
{ |
| 194 |
if (opt[opti].argtype == sqdARG_INT && ! IsInt(*ret_optarg)) |
| 195 |
Die("Option %s requires an integer argument\n%s", |
| 196 |
opt[opti].name, usage); |
| 197 |
else if (opt[opti].argtype == sqdARG_FLOAT && ! IsReal(*ret_optarg)) |
| 198 |
Die("Option %s requires a numerical argument\n%s", |
| 199 |
opt[opti].name, usage); |
| 200 |
else if (opt[opti].argtype == sqdARG_CHAR && strlen(*ret_optarg) != 1) |
| 201 |
Die("Option %s requires a single-character argument\n%s", |
| 202 |
opt[opti].name, usage); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
*ret_optind = optind; |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#ifdef GETOPT_TESTDRIVER |
| 213 |
|
| 214 |
|
| 215 |
struct opt_s OPTIONS[] = { |
| 216 |
{ "--test1", FALSE, sqdARG_INT }, |
| 217 |
{ "--test2", FALSE, sqdARG_FLOAT }, |
| 218 |
{ "--test3", FALSE, sqdARG_STRING }, |
| 219 |
{ "--test4", FALSE, sqdARG_CHAR }, |
| 220 |
{ "-a", TRUE, sqdARG_NONE }, |
| 221 |
{ "-b", TRUE, sqdARG_INT }, |
| 222 |
}; |
| 223 |
#define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s)) |
| 224 |
|
| 225 |
int |
| 226 |
main(int argc, char **argv) |
| 227 |
{ |
| 228 |
int optind; |
| 229 |
char *optarg; |
| 230 |
char *optname; |
| 231 |
|
| 232 |
while (Getopt(argc, argv, OPTIONS, NOPTIONS, "Usage/help here", |
| 233 |
&optind, &optname, &optarg)) |
| 234 |
{ |
| 235 |
printf("Option: index: %d name: %s argument: %s\n", |
| 236 |
optind, optname, optarg); |
| 237 |
} |
| 238 |
while (optind < argc) |
| 239 |
{ |
| 240 |
printf("Argument: index: %d name: %s\n", optind, argv[optind]); |
| 241 |
optind++; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
#endif |