1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)options.c 8.1 (Berkeley) 5/31/93"; 39 #endif /* not lint */ 40 41 #include "shell.h" 42 #define DEFINE_OPTIONS 43 #include "options.h" 44 #undef DEFINE_OPTIONS 45 #include "nodes.h" /* for other header files */ 46 #include "eval.h" 47 #include "jobs.h" 48 #include "input.h" 49 #include "output.h" 50 #include "trap.h" 51 #include "var.h" 52 #include "memalloc.h" 53 #include "error.h" 54 #include "mystring.h" 55 56 char *arg0; /* value of $0 */ 57 struct shparam shellparam; /* current positional parameters */ 58 char **argptr; /* argument list for builtin commands */ 59 char *optarg; /* set by nextopt (like getopt) */ 60 char *optptr; /* used by nextopt */ 61 62 char *minusc; /* argument to -c option */ 63 64 65 #ifdef __STDC__ 66 STATIC void options(int); 67 STATIC void setoption(int, int); 68 STATIC void minus_o(char *, int); 69 #else 70 STATIC void options(); 71 STATIC void setoption(); 72 STATIC void minus_o(); 73 #endif 74 75 76 77 /* 78 * Process the shell command line arguments. 79 */ 80 81 void 82 procargs(argc, argv) 83 char **argv; 84 { 85 int i; 86 87 argptr = argv; 88 if (argc > 0) 89 argptr++; 90 for (i = 0; i < NOPTS; i++) 91 optlist[i].val = 2; 92 options(1); 93 if (*argptr == NULL && minusc == NULL) 94 sflag = 1; 95 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 96 iflag = 1; 97 if (mflag == 2) 98 mflag = iflag; 99 for (i = 0; i < NOPTS; i++) 100 if (optlist[i].val == 2) 101 optlist[i].val = 0; 102 arg0 = argv[0]; 103 if (sflag == 0 && minusc == NULL) { 104 commandname = arg0 = *argptr++; 105 setinputfile(commandname, 0); 106 } 107 shellparam.p = argptr; 108 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 109 while (*argptr) { 110 shellparam.nparam++; 111 argptr++; 112 } 113 optschanged(); 114 } 115 116 117 optschanged() { 118 setinteractive(iflag); 119 histedit(); 120 setjobctl(mflag); 121 } 122 123 /* 124 * Process shell options. The global variable argptr contains a pointer 125 * to the argument list; we advance it past the options. 126 */ 127 128 STATIC void 129 options(cmdline) { 130 register char *p; 131 int val; 132 int c; 133 134 if (cmdline) 135 minusc = NULL; 136 while ((p = *argptr) != NULL) { 137 argptr++; 138 if ((c = *p++) == '-') { 139 val = 1; 140 if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') { 141 if (!cmdline) { 142 /* "-" means turn off -x and -v */ 143 if (p[0] == '\0') 144 xflag = vflag = 0; 145 /* "--" means reset params */ 146 else if (*argptr == NULL) 147 setparam(argptr); 148 } 149 break; /* "-" or "--" terminates options */ 150 } 151 } else if (c == '+') { 152 val = 0; 153 } else { 154 argptr--; 155 break; 156 } 157 while ((c = *p++) != '\0') { 158 if (c == 'c' && cmdline) { 159 char *q; 160 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 161 if (*p == '\0') 162 #endif 163 q = *argptr++; 164 if (q == NULL || minusc != NULL) 165 error("Bad -c option"); 166 minusc = q; 167 #ifdef NOHACK 168 break; 169 #endif 170 } else if (c == 'o') { 171 minus_o(*argptr, val); 172 if (*argptr) 173 argptr++; 174 } else { 175 setoption(c, val); 176 } 177 } 178 } 179 } 180 181 STATIC void 182 minus_o(name, val) 183 char *name; 184 int val; 185 { 186 int i; 187 188 if (name == NULL) { 189 out1str("Current option settings\n"); 190 for (i = 0; i < NOPTS; i++) 191 out1fmt("%-16s%s\n", optlist[i].name, 192 optlist[i].val ? "on" : "off"); 193 } else { 194 for (i = 0; i < NOPTS; i++) 195 if (equal(name, optlist[i].name)) { 196 setoption(optlist[i].letter, val); 197 return; 198 } 199 error("Illegal option -o %s", name); 200 } 201 } 202 203 204 STATIC void 205 setoption(flag, val) 206 char flag; 207 int val; 208 { 209 int i; 210 211 for (i = 0; i < NOPTS; i++) 212 if (optlist[i].letter == flag) { 213 optlist[i].val = val; 214 if (val) { 215 /* #%$ hack for ksh semantics */ 216 if (flag == 'V') 217 Eflag = 0; 218 else if (flag == 'E') 219 Vflag = 0; 220 } 221 return; 222 } 223 error("Illegal option -%c", flag); 224 } 225 226 227 228 #ifdef mkinit 229 INCLUDE "options.h" 230 231 SHELLPROC { 232 int i; 233 234 for (i = 0; i < NOPTS; i++) 235 optlist[i].val = 0; 236 optschanged(); 237 238 } 239 #endif 240 241 242 /* 243 * Set the shell parameters. 244 */ 245 246 void 247 setparam(argv) 248 char **argv; 249 { 250 char **newparam; 251 char **ap; 252 int nparam; 253 254 for (nparam = 0 ; argv[nparam] ; nparam++); 255 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 256 while (*argv) { 257 *ap++ = savestr(*argv++); 258 } 259 *ap = NULL; 260 freeparam(&shellparam); 261 shellparam.malloc = 1; 262 shellparam.nparam = nparam; 263 shellparam.p = newparam; 264 shellparam.optnext = NULL; 265 } 266 267 268 /* 269 * Free the list of positional parameters. 270 */ 271 272 void 273 freeparam(param) 274 struct shparam *param; 275 { 276 char **ap; 277 278 if (param->malloc) { 279 for (ap = param->p ; *ap ; ap++) 280 ckfree(*ap); 281 ckfree(param->p); 282 } 283 } 284 285 286 287 /* 288 * The shift builtin command. 289 */ 290 291 shiftcmd(argc, argv) char **argv; { 292 int n; 293 char **ap1, **ap2; 294 295 n = 1; 296 if (argc > 1) 297 n = number(argv[1]); 298 if (n > shellparam.nparam) 299 error("can't shift that many"); 300 INTOFF; 301 shellparam.nparam -= n; 302 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 303 if (shellparam.malloc) 304 ckfree(*ap1); 305 } 306 ap2 = shellparam.p; 307 while ((*ap2++ = *ap1++) != NULL); 308 shellparam.optnext = NULL; 309 INTON; 310 return 0; 311 } 312 313 314 315 /* 316 * The set command builtin. 317 */ 318 319 setcmd(argc, argv) char **argv; { 320 if (argc == 1) 321 return showvarscmd(argc, argv); 322 INTOFF; 323 options(0); 324 optschanged(); 325 if (*argptr != NULL) { 326 setparam(argptr); 327 } 328 INTON; 329 return 0; 330 } 331 332 333 /* 334 * The getopts builtin. Shellparam.optnext points to the next argument 335 * to be processed. Shellparam.optptr points to the next character to 336 * be processed in the current argument. If shellparam.optnext is NULL, 337 * then it's the first time getopts has been called. 338 */ 339 340 getoptscmd(argc, argv) char **argv; { 341 register char *p, *q; 342 char c; 343 char s[10]; 344 345 if (argc != 3) 346 error("Usage: getopts optstring var"); 347 if (shellparam.optnext == NULL) { 348 shellparam.optnext = shellparam.p; 349 shellparam.optptr = NULL; 350 } 351 if ((p = shellparam.optptr) == NULL || *p == '\0') { 352 p = *shellparam.optnext; 353 if (p == NULL || *p != '-' || *++p == '\0') { 354 atend: 355 fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1); 356 setvar("OPTIND", s, 0); 357 shellparam.optnext = NULL; 358 return 1; 359 } 360 shellparam.optnext++; 361 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 362 goto atend; 363 } 364 c = *p++; 365 for (q = argv[1] ; *q != c ; ) { 366 if (*q == '\0') { 367 out1fmt("Illegal option -%c\n", c); 368 c = '?'; 369 goto out; 370 } 371 if (*++q == ':') 372 q++; 373 } 374 if (*++q == ':') { 375 if (*p == '\0' && (p = *shellparam.optnext) == NULL) { 376 out1fmt("No arg for -%c option\n", c); 377 c = '?'; 378 goto out; 379 } 380 shellparam.optnext++; 381 setvar("OPTARG", p, 0); 382 p = NULL; 383 } 384 out: 385 shellparam.optptr = p; 386 s[0] = c; 387 s[1] = '\0'; 388 setvar(argv[2], s, 0); 389 return 0; 390 } 391 392 /* 393 * XXX - should get rid of. have all builtins use getopt(3). the 394 * library getopt must have the BSD extension static variable "optreset" 395 * otherwise it can't be used within the shell safely. 396 * 397 * Standard option processing (a la getopt) for builtin routines. The 398 * only argument that is passed to nextopt is the option string; the 399 * other arguments are unnecessary. It return the character, or '\0' on 400 * end of input. 401 */ 402 403 int 404 nextopt(optstring) 405 char *optstring; 406 { 407 register char *p, *q; 408 char c; 409 410 if ((p = optptr) == NULL || *p == '\0') { 411 p = *argptr; 412 if (p == NULL || *p != '-' || *++p == '\0') 413 return '\0'; 414 argptr++; 415 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 416 return '\0'; 417 } 418 c = *p++; 419 for (q = optstring ; *q != c ; ) { 420 if (*q == '\0') 421 error("Illegal option -%c", c); 422 if (*++q == ':') 423 q++; 424 } 425 if (*++q == ':') { 426 if (*p == '\0' && (p = *argptr++) == NULL) 427 error("No arg for -%c option", c); 428 optarg = p; 429 p = NULL; 430 } 431 optptr = p; 432 return c; 433 } 434