Changeset 1198 for trunk/src/kash/options.c
- Timestamp:
- Oct 6, 2007, 11:19:19 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/options.c
r809 r1198 66 66 #endif 67 67 #include "show.h" 68 69 char *arg0; /* value of $0 */ 70 struct shparam shellparam; /* current positional parameters */ 71 char **argptr; /* argument list for builtin commands */ 72 char *optionarg; /* set by nextopt (like getopt) */ 73 char *optptr; /* used by nextopt */ 74 75 char *minusc; /* argument to -c option */ 76 77 78 STATIC void options(int); 79 STATIC void minus_o(char *, int); 80 STATIC void setoption(int, int); 81 STATIC int getopts(char *, char *, char **, char ***, char **); 68 #include "shinstance.h" 69 70 //char *arg0; /* value of $0 */ 71 //struct shparam shellparam; /* current positional parameters */ 72 //char **argptr; /* argument list for builtin commands */ 73 //char *optionarg; /* set by nextopt (like getopt) */ 74 //char *optptr; /* used by nextopt */ 75 76 //char *minusc; /* argument to -c option */ 77 78 79 STATIC void options(shinstance *, int); 80 STATIC void minus_o(shinstance *, char *, int); 81 STATIC void setoption(shinstance *, int, int); 82 STATIC int getopts(shinstance *, char *, char *, char **, char ***, char **); 82 83 83 84 … … 87 88 88 89 void 89 procargs( int argc, char **argv)90 procargs(shinstance *psh, int argc, char **argv) 90 91 { 91 92 int i; 92 93 93 argptr = argv;94 psh->argptr = argv; 94 95 if (argc > 0) 95 argptr++;96 psh->argptr++; 96 97 for (i = 0; i < NOPTS; i++) 97 optlist[i].val = 2;98 options( 1);99 if (* argptr == NULL &&minusc == NULL)100 sflag = 1;101 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))102 iflag = 1;103 if (mflag == 2)104 mflag = iflag;98 psh->optlist[i].val = 2; 99 options(psh, 1); 100 if (*psh->argptr == NULL && psh->minusc == NULL) 101 sflag(psh) = 1; 102 if (iflag(psh) == 2 && sflag(psh) == 1 && shfile_isatty(&psh->fdtab, 0) && shfile_isatty(&psh->fdtab, 1)) 103 iflag(psh) = 1; 104 if (mflag(psh) == 2) 105 mflag(psh) = iflag(psh); 105 106 for (i = 0; i < NOPTS; i++) 106 if ( optlist[i].val == 2)107 optlist[i].val = 0;107 if (psh->optlist[i].val == 2) 108 psh->optlist[i].val = 0; 108 109 #if DEBUG == 2 109 debug = 1;110 #endif 111 arg0 = argv[0];112 if (sflag == 0 &&minusc == NULL) {113 commandname = argv[0];114 arg0 = *argptr++;115 setinputfile( arg0, 0);116 commandname =arg0;110 debug(psh) = 1; 111 #endif 112 psh->arg0 = argv[0]; 113 if (sflag(psh) == 0 && psh->minusc == NULL) { 114 psh->commandname = argv[0]; 115 psh->arg0 = *psh->argptr++; 116 setinputfile(psh, psh->arg0, 0); 117 psh->commandname = psh->arg0; 117 118 } 118 119 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 119 if ( minusc != NULL) {120 if ( argptr == NULL || *argptr == NULL)121 error( "Bad -c option");122 minusc = *argptr++;123 if (* argptr != 0)124 arg0 = *argptr++;125 } 126 127 shellparam.p =argptr;128 shellparam.reset = 1;120 if (psh->minusc != NULL) { 121 if (psh->argptr == NULL || *psh->argptr == NULL) 122 error(psh, "Bad -c option"); 123 psh->minusc = *psh->argptr++; 124 if (*psh->argptr != 0) 125 psh->arg0 = *psh->argptr++; 126 } 127 128 psh->shellparam.p = psh->argptr; 129 psh->shellparam.reset = 1; 129 130 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 130 while (* argptr) {131 shellparam.nparam++;132 argptr++;133 } 134 optschanged( );131 while (*psh->argptr) { 132 psh->shellparam.nparam++; 133 psh->argptr++; 134 } 135 optschanged(psh); 135 136 } 136 137 137 138 138 139 void 139 optschanged( void)140 { 141 setinteractive( iflag);140 optschanged(shinstance *psh) 141 { 142 setinteractive(psh, iflag(psh)); 142 143 #ifndef SMALL 143 histedit( );144 #endif 145 setjobctl( mflag);144 histedit(psh); 145 #endif 146 setjobctl(psh, mflag(psh)); 146 147 } 147 148 … … 152 153 153 154 STATIC void 154 options( int cmdline)155 options(shinstance *psh, int cmdline) 155 156 { 156 157 static char empty[] = ""; … … 160 161 161 162 if (cmdline) 162 minusc = NULL;163 while ((p = * argptr) != NULL) {164 argptr++;163 psh->minusc = NULL; 164 while ((p = *psh->argptr) != NULL) { 165 psh->argptr++; 165 166 if ((c = *p++) == '-') { 166 167 val = 1; … … 169 170 /* "-" means turn off -x and -v */ 170 171 if (p[0] == '\0') 171 xflag = vflag= 0;172 xflag(psh) = vflag(psh) = 0; 172 173 /* "--" means reset params */ 173 else if (* argptr == NULL)174 setparam( argptr);174 else if (*psh->argptr == NULL) 175 setparam(psh, psh->argptr); 175 176 } 176 177 break; /* "-" or "--" terminates options */ … … 179 180 val = 0; 180 181 } else { 181 argptr--;182 psh->argptr--; 182 183 break; 183 184 } … … 185 186 if (c == 'c' && cmdline) { 186 187 /* command is after shell args*/ 187 minusc = empty;188 psh->minusc = empty; 188 189 } else if (c == 'o') { 189 minus_o( *argptr, val);190 if (* argptr)191 argptr++;190 minus_o(psh, *psh->argptr, val); 191 if (*psh->argptr) 192 psh->argptr++; 192 193 } else { 193 setoption( c, val);194 setoption(psh, c, val); 194 195 } 195 196 } … … 198 199 199 200 static void 200 set_opt_val( int i, int val)201 set_opt_val(shinstance *psh, int i, int val) 201 202 { 202 203 int j; 203 204 int flag; 204 205 205 if (val && (flag = optlist[i].opt_set)) {206 if (val && (flag = psh->optlist[i].opt_set)) { 206 207 /* some options (eg vi/emacs) are mutually exclusive */ 207 208 for (j = 0; j < NOPTS; j++) 208 if ( optlist[j].opt_set == flag)209 optlist[j].val = 0;210 } 211 optlist[i].val = val;209 if (psh->optlist[j].opt_set == flag) 210 psh->optlist[j].val = 0; 211 } 212 psh->optlist[i].val = val; 212 213 #ifdef DEBUG 213 if (& optlist[i].val == &debug)214 if (&psh->optlist[i].val == &debug(psh)) 214 215 opentrace(); 215 216 #endif … … 217 218 218 219 STATIC void 219 minus_o( char *name, int val)220 minus_o(shinstance *psh, char *name, int val) 220 221 { 221 222 int i; 222 223 223 224 if (name == NULL) { 224 out1str( "Current option settings\n");225 out1str(psh, "Current option settings\n"); 225 226 for (i = 0; i < NOPTS; i++) 226 out1fmt( "%-16s%s\n",optlist[i].name,227 optlist[i].val ? "on" : "off");227 out1fmt(psh, "%-16s%s\n", psh->optlist[i].name, 228 psh->optlist[i].val ? "on" : "off"); 228 229 } else { 229 230 for (i = 0; i < NOPTS; i++) 230 if (equal(name, optlist[i].name)) {231 set_opt_val( i, val);231 if (equal(name, psh->optlist[i].name)) { 232 set_opt_val(psh, i, val); 232 233 return; 233 234 } 234 error( "Illegal option -o %s", name);235 error(psh, "Illegal option -o %s", name); 235 236 } 236 237 } … … 238 239 239 240 STATIC void 240 setoption( int flag, int val)241 setoption(shinstance *psh, int flag, int val) 241 242 { 242 243 int i; 243 244 244 245 for (i = 0; i < NOPTS; i++) 245 if ( optlist[i].letter == flag) {246 set_opt_val( i, val );246 if (psh->optlist[i].letter == flag) { 247 set_opt_val(psh, i, val ); 247 248 return; 248 249 } 249 error( "Illegal option -%c", flag);250 error(psh, "Illegal option -%c", flag); 250 251 /* NOTREACHED */ 251 252 } … … 259 260 int i; 260 261 261 for (i = 0; optlist[i].name; i++)262 optlist[i].val = 0;263 optschanged( );262 for (i = 0; psh->optlist[i].name; i++) 263 psh->optlist[i].val = 0; 264 optschanged(psh); 264 265 265 266 } … … 272 273 273 274 void 274 setparam( char **argv)275 setparam(shinstance *psh, char **argv) 275 276 { 276 277 char **newparam; … … 285 286 } 286 287 *ap = NULL; 287 freeparam(& shellparam);288 shellparam.malloc = 1;289 shellparam.nparam = nparam;290 shellparam.p = newparam;291 shellparam.optnext = NULL;288 freeparam(&psh->shellparam); 289 psh->shellparam.malloc = 1; 290 psh->shellparam.nparam = nparam; 291 psh->shellparam.p = newparam; 292 psh->shellparam.optnext = NULL; 292 293 } 293 294 … … 316 317 317 318 int 318 shiftcmd( int argc, char **argv)319 shiftcmd(shinstance *psh, int argc, char **argv) 319 320 { 320 321 int n; … … 323 324 n = 1; 324 325 if (argc > 1) 325 n = number( argv[1]);326 if (n > shellparam.nparam)327 error( "can't shift that many");326 n = number(psh, argv[1]); 327 if (n > psh->shellparam.nparam) 328 error(psh, "can't shift that many"); 328 329 INTOFF; 329 shellparam.nparam -= n;330 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {331 if ( shellparam.malloc)330 psh->shellparam.nparam -= n; 331 for (ap1 = psh->shellparam.p ; --n >= 0 ; ap1++) { 332 if (psh->shellparam.malloc) 332 333 ckfree(*ap1); 333 334 } 334 ap2 = shellparam.p;335 ap2 = psh->shellparam.p; 335 336 while ((*ap2++ = *ap1++) != NULL); 336 shellparam.optnext = NULL;337 psh->shellparam.optnext = NULL; 337 338 INTON; 338 339 return 0; … … 346 347 347 348 int 348 setcmd( int argc, char **argv)349 setcmd(shinstance *psh, int argc, char **argv) 349 350 { 350 351 if (argc == 1) 351 return showvars( 0, 0, 1);352 return showvars(psh, 0, 0, 1); 352 353 INTOFF; 353 options( 0);354 optschanged( );355 if (* argptr != NULL) {356 setparam( argptr);354 options(psh, 0); 355 optschanged(psh); 356 if (*psh->argptr != NULL) { 357 setparam(psh, psh->argptr); 357 358 } 358 359 INTON; … … 362 363 363 364 void 364 getoptsreset(value) 365 const char *value; 366 { 367 if (number(value) == 1) { 368 shellparam.optnext = NULL; 369 shellparam.reset = 1; 365 getoptsreset(shinstance *psh, const char *value) 366 { 367 if (number(psh, value) == 1) { 368 psh->shellparam.optnext = NULL; 369 psh->shellparam.reset = 1; 370 370 } 371 371 } … … 379 379 380 380 int 381 getoptscmd( int argc, char **argv)381 getoptscmd(shinstance *psh, int argc, char **argv) 382 382 { 383 383 char **optbase; 384 384 385 385 if (argc < 3) 386 error( "usage: getopts optstring var [arg]");386 error(psh, "usage: getopts optstring var [arg]"); 387 387 else if (argc == 3) 388 optbase = shellparam.p;388 optbase = psh->shellparam.p; 389 389 else 390 390 optbase = &argv[3]; 391 391 392 if ( shellparam.reset == 1) {393 shellparam.optnext = optbase;394 shellparam.optptr = NULL;395 shellparam.reset = 0;396 } 397 398 return getopts( argv[1], argv[2], optbase, &shellparam.optnext,399 & shellparam.optptr);392 if (psh->shellparam.reset == 1) { 393 psh->shellparam.optnext = optbase; 394 psh->shellparam.optptr = NULL; 395 psh->shellparam.reset = 0; 396 } 397 398 return getopts(psh, argv[1], argv[2], optbase, &psh->shellparam.optnext, 399 &psh->shellparam.optptr); 400 400 } 401 401 402 402 STATIC int 403 getopts( char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)403 getopts(shinstance *psh, char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr) 404 404 { 405 405 char *p, *q; … … 417 417 if (p == NULL || *p != '-' || *++p == '\0') { 418 418 atend: 419 ind = *optnext - optfirst + 1;419 ind = (int)(*optnext - optfirst + 1); 420 420 *optnext = NULL; 421 421 p = NULL; … … 434 434 s[0] = c; 435 435 s[1] = '\0'; 436 err |= setvarsafe( "OPTARG", s, 0);436 err |= setvarsafe(psh, "OPTARG", s, 0); 437 437 } else { 438 outfmt(& errout, "Illegal option -%c\n", c);439 (void) unsetvar( "OPTARG", 0);438 outfmt(&psh->errout, "Illegal option -%c\n", c); 439 (void) unsetvar(psh, "OPTARG", 0); 440 440 } 441 441 c = '?'; … … 451 451 s[0] = c; 452 452 s[1] = '\0'; 453 err |= setvarsafe( "OPTARG", s, 0);453 err |= setvarsafe(psh, "OPTARG", s, 0); 454 454 c = ':'; 455 455 } else { 456 outfmt(& errout, "No arg for -%c option\n", c);457 (void) unsetvar( "OPTARG", 0);456 outfmt(&psh->errout, "No arg for -%c option\n", c); 457 (void) unsetvar(psh, "OPTARG", 0); 458 458 c = '?'; 459 459 } … … 463 463 if (p == **optnext) 464 464 (*optnext)++; 465 err |= setvarsafe( "OPTARG", p, 0);465 err |= setvarsafe(psh, "OPTARG", p, 0); 466 466 p = NULL; 467 467 } else 468 err |= setvarsafe( "OPTARG", "", 0);469 ind = *optnext - optfirst + 1;468 err |= setvarsafe(psh, "OPTARG", "", 0); 469 ind = (int)(*optnext - optfirst + 1); 470 470 goto out; 471 471 … … 477 477 *optpptr = p; 478 478 fmtstr(s, sizeof(s), "%d", ind); 479 err |= setvarsafe( "OPTIND", s, VNOFUNC);479 err |= setvarsafe(psh, "OPTIND", s, VNOFUNC); 480 480 s[0] = c; 481 481 s[1] = '\0'; 482 err |= setvarsafe( optvar, s, 0);482 err |= setvarsafe(psh, optvar, s, 0); 483 483 if (err) { 484 484 *optnext = NULL; 485 485 *optpptr = NULL; 486 output_flushall( );487 exraise( EXERROR);486 output_flushall(psh); 487 exraise(psh, EXERROR); 488 488 } 489 489 return done; … … 502 502 503 503 int 504 nextopt( const char *optstring)504 nextopt(shinstance *psh, const char *optstring) 505 505 { 506 506 char *p; … … 508 508 char c; 509 509 510 if ((p = optptr) == NULL || *p == '\0') {511 p = * argptr;510 if ((p = psh->optptr) == NULL || *p == '\0') { 511 p = *psh->argptr; 512 512 if (p == NULL || *p != '-' || *++p == '\0') 513 513 return '\0'; 514 argptr++;514 psh->argptr++; 515 515 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 516 516 return '\0'; … … 519 519 for (q = optstring ; *q != c ; ) { 520 520 if (*q == '\0') 521 error( "Illegal option -%c", c);521 error(psh, "Illegal option -%c", c); 522 522 if (*++q == ':') 523 523 q++; 524 524 } 525 525 if (*++q == ':') { 526 if (*p == '\0' && (p = * argptr++) == NULL)527 error( "No arg for -%c option", c);528 optionarg = p;526 if (*p == '\0' && (p = *psh->argptr++) == NULL) 527 error(psh, "No arg for -%c option", c); 528 psh->optionarg = p; 529 529 p = NULL; 530 530 } 531 optptr = p;531 psh->optptr = p; 532 532 return c; 533 533 }
Note:
See TracChangeset
for help on using the changeset viewer.