| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "gethopt.h" |
| 10 |
|
| 11 |
|
| 12 |
void |
| 13 |
hoptset(struct h_context *ctx, int argc, char **argv) |
| 14 |
{ |
| 15 |
memset(ctx, 0, sizeof *ctx); |
| 16 |
ctx->argc = argc; |
| 17 |
ctx->argv = argv; |
| 18 |
ctx->optind = 1; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
char * |
| 23 |
hoptarg(struct h_context *ctx) |
| 24 |
{ |
| 25 |
return ctx->optarg; |
| 26 |
} |
| 27 |
|
| 28 |
int |
| 29 |
hoptind(struct h_context *ctx) |
| 30 |
{ |
| 31 |
return ctx->optind; |
| 32 |
} |
| 33 |
|
| 34 |
char |
| 35 |
hoptopt(struct h_context *ctx) |
| 36 |
{ |
| 37 |
return ctx->optopt; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
int |
| 42 |
hopterr(struct h_context *ctx, int val) |
| 43 |
{ |
| 44 |
int old = ctx->opterr; |
| 45 |
|
| 46 |
ctx->opterr = !!val; |
| 47 |
return old; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
struct h_opt * |
| 52 |
gethopt(struct h_context *ctx, struct h_opt *opts, int nropts) |
| 53 |
{ |
| 54 |
int i; |
| 55 |
int dashes; |
| 56 |
|
| 57 |
|
| 58 |
if ( (ctx == 0) || ctx->optend || (ctx->optind >= ctx->argc) ) |
| 59 |
return 0; |
| 60 |
|
| 61 |
ctx->optarg = 0; |
| 62 |
ctx->optopt = 0; |
| 63 |
|
| 64 |
if ( ctx->optchar == 0) { |
| 65 |
|
| 66 |
|
| 67 |
if ( ctx->argv[ctx->optind][0] != '-' ) { |
| 68 |
|
| 69 |
ctx->optend = 1; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ctx->argv[ctx->optind][1] == 0 |
| 74 |
|| strcmp(ctx->argv[ctx->optind], "--") == 0 ) { |
| 75 |
|
| 76 |
|
| 77 |
ctx->optend = 1; |
| 78 |
ctx->optind++; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
dashes = 1; |
| 83 |
if ( ctx->argv[ctx->optind][dashes] == '-' ) { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
dashes = 2; |
| 91 |
} |
| 92 |
|
| 93 |
for ( i=0; i < nropts; i++ ) { |
| 94 |
if ( ! opts[i].optword ) |
| 95 |
continue; |
| 96 |
|
| 97 |
if (strcmp(opts[i].optword, dashes+(ctx->argv[ctx->optind]) ) == 0 ) { |
| 98 |
if ( opts[i].opthasarg ) { |
| 99 |
if ( ctx->argc > ctx->optind ) { |
| 100 |
ctx->optarg = ctx->argv[ctx->optind+1]; |
| 101 |
ctx->optind += 2; |
| 102 |
} |
| 103 |
else { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if ( ctx->opterr ) |
| 108 |
fprintf(stderr, |
| 109 |
"%s: option requires an argument -- %s\n", |
| 110 |
ctx->argv[0], opts[i].optword); |
| 111 |
ctx->optind ++; |
| 112 |
return HOPTERR; |
| 113 |
} |
| 114 |
} |
| 115 |
else { |
| 116 |
ctx->optind ++; |
| 117 |
} |
| 118 |
return &opts[i]; |
| 119 |
} |
| 120 |
} |
| 121 |
ctx->optchar = 1; |
| 122 |
} |
| 123 |
|
| 124 |
ctx->optopt = ctx->argv[ctx->optind][ctx->optchar++]; |
| 125 |
|
| 126 |
if ( !ctx->optopt ) { |
| 127 |
|
| 128 |
ctx->optind ++; |
| 129 |
ctx->optchar = 0; |
| 130 |
return gethopt(ctx, opts, nropts); |
| 131 |
} |
| 132 |
|
| 133 |
for ( i=0; i<nropts; i++ ) { |
| 134 |
if ( opts[i].optchar == ctx->optopt ) { |
| 135 |
|
| 136 |
|
| 137 |
if ( opts[i].opthasarg ) { |
| 138 |
if ( ctx->argv[ctx->optind][ctx->optchar] ) { |
| 139 |
|
| 140 |
|
| 141 |
ctx->optarg = &ctx->argv[ctx->optind][ctx->optchar]; |
| 142 |
ctx->optind ++; |
| 143 |
ctx->optchar = 0; |
| 144 |
} |
| 145 |
else if ( ctx->optind < ctx->argc-1 ) { |
| 146 |
|
| 147 |
|
| 148 |
ctx->optarg = &ctx->argv[ctx->optind+1][0]; |
| 149 |
ctx->optind += 2; |
| 150 |
ctx->optchar = 0; |
| 151 |
} |
| 152 |
else { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
ctx->optarg = 0; |
| 157 |
ctx->optind ++; |
| 158 |
ctx->optchar = 0; |
| 159 |
if ( ctx->opterr ) |
| 160 |
fprintf(stderr, |
| 161 |
"%s: option requires an argument -- %c\n", |
| 162 |
ctx->argv[0], opts[i].optchar); |
| 163 |
return HOPTERR; |
| 164 |
} |
| 165 |
} |
| 166 |
else { |
| 167 |
if ( !ctx->argv[ctx->optind][ctx->optchar] ) { |
| 168 |
ctx->optind ++; |
| 169 |
ctx->optchar = 0; |
| 170 |
} |
| 171 |
} |
| 172 |
return &opts[i]; |
| 173 |
} |
| 174 |
} |
| 175 |
if ( ctx->opterr ) |
| 176 |
fprintf(stderr, "%s: illegal option -- %c\n", ctx->argv[0], ctx->optopt); |
| 177 |
return HOPTERR; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void |
| 182 |
hoptusage(char *pgm, struct h_opt opts[], int nropts, char *arguments) |
| 183 |
{ |
| 184 |
int i; |
| 185 |
int optcount; |
| 186 |
|
| 187 |
fprintf(stderr, "usage: %s", pgm); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
for ( optcount=i=0; i < nropts; i++ ) { |
| 192 |
if ( opts[i].optchar && !opts[i].opthasarg) { |
| 193 |
if (optcount == 0 ) |
| 194 |
fputs(" [-", stderr); |
| 195 |
fputc(opts[i].optchar, stderr); |
| 196 |
optcount++; |
| 197 |
} |
| 198 |
} |
| 199 |
if ( optcount ) |
| 200 |
fputc(']', stderr); |
| 201 |
|
| 202 |
|
| 203 |
for ( i = 0; i < nropts; i++ ) |
| 204 |
if ( opts[i].optchar && opts[i].opthasarg) |
| 205 |
fprintf(stderr, " [-%c %s]", opts[i].optchar, opts[i].opthasarg); |
| 206 |
|
| 207 |
|
| 208 |
for ( i = 0; i < nropts; i++ ) |
| 209 |
if ( opts[i].optword ) { |
| 210 |
fprintf(stderr, " [-%s", opts[i].optword); |
| 211 |
if ( opts[i].opthasarg ) |
| 212 |
fprintf(stderr, " %s", opts[i].opthasarg); |
| 213 |
fputc(']', stderr); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if ( arguments ) |
| 219 |
fprintf(stderr, " %s", arguments); |
| 220 |
|
| 221 |
|
| 222 |
fputc('\n', stderr); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
#if DEBUG |
| 227 |
struct h_opt opts[] = { |
| 228 |
{ 0, "css", 0, 1, "css file" }, |
| 229 |
{ 1, "header", 0, 1, "header file" }, |
| 230 |
{ 2, 0, 'a', 0, "option a (no arg)" }, |
| 231 |
{ 3, 0, 'b', 1, "option B (with arg)" }, |
| 232 |
{ 4, "help", '?', 0, "help message" }, |
| 233 |
} ; |
| 234 |
|
| 235 |
#define NROPT (sizeof opts/sizeof opts[0]) |
| 236 |
|
| 237 |
|
| 238 |
int |
| 239 |
main(argc, argv) |
| 240 |
char **argv; |
| 241 |
{ |
| 242 |
struct h_opt *ret; |
| 243 |
struct h_context ctx; |
| 244 |
int i; |
| 245 |
|
| 246 |
|
| 247 |
hoptset(&ctx, argc, argv); |
| 248 |
hopterr(&ctx, 1); |
| 249 |
|
| 250 |
while (( ret = gethopt(&ctx, opts, NROPT) )) { |
| 251 |
|
| 252 |
if ( ret != HOPTERR ) { |
| 253 |
if ( ret->optword ) |
| 254 |
printf("%s", ret->optword); |
| 255 |
else |
| 256 |
printf("%c", ret->optchar); |
| 257 |
|
| 258 |
if ( ret->opthasarg ) { |
| 259 |
if ( hoptarg(&ctx) ) |
| 260 |
printf(" with argument \"%s\"", hoptarg(&ctx)); |
| 261 |
else |
| 262 |
printf(" with no argument?"); |
| 263 |
} |
| 264 |
printf(" (%s)\n", ret->optdesc); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
argc -= hoptind(&ctx); |
| 269 |
argv += hoptind(&ctx); |
| 270 |
|
| 271 |
for ( i=0; i < argc; i++ ) |
| 272 |
printf("%d: %s\n", i, argv[i]); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
#endif |