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 * $Id: options.c,v 1.5 1995/10/01 15:11:42 joerg Exp $ 37 */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)options.c 8.1 (Berkeley) 5/31/93"; 41 #endif /* not lint */ 42 43 #include "shell.h" 44 #define DEFINE_OPTIONS 45 #include "options.h" 46 #undef DEFINE_OPTIONS 47 #include "nodes.h" /* for other header files */ 48 #include "eval.h" 49 #include "jobs.h" 50 #include "input.h" 51 #include "output.h" 52 #include "trap.h" 53 #include "var.h" 54 #include "memalloc.h" 55 #include "error.h" 56 #include "mystring.h" 57 58 char *arg0; /* value of $0 */ 59 struct shparam shellparam; /* current positional parameters */ 60 char **argptr; /* argument list for builtin commands */ 61 char *optarg; /* set by nextopt (like getopt) */ 62 char *optptr; /* used by nextopt */ 63 64 char *minusc; /* argument to -c option */ 65 66 67 #ifdef __STDC__ 68 STATIC void options(int); 69 STATIC void setoption(int, int); 70 STATIC void minus_o(char *, int); 71 #else 72 STATIC void options(); 73 STATIC void setoption(); 74 STATIC void minus_o(); 75 #endif 76 77 78 79 /* 80 * Process the shell command line arguments. 81 */ 82 83 void 84 procargs(argc, argv) 85 char **argv; 86 { 87 int i; 88 89 argptr = argv; 90 if (argc > 0) 91 argptr++; 92 for (i = 0; i < NOPTS; i++) 93 optlist[i].val = 2; 94 options(1); 95 if (*argptr == NULL && minusc == NULL) 96 sflag = 1; 97 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 98 iflag = 1; 99 if (mflag == 2) 100 mflag = iflag; 101 for (i = 0; i < NOPTS; i++) 102 if (optlist[i].val == 2) 103 optlist[i].val = 0; 104 arg0 = argv[0]; 105 if (sflag == 0 && minusc == NULL) { 106 commandname = arg0 = *argptr++; 107 setinputfile(commandname, 0); 108 } 109 if (*argptr && minusc) 110 /* Posix.2: first arg after -c cmd is $0, remainder $1... */ 111 arg0 = *argptr++; 112 shellparam.p = argptr; 113 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 114 while (*argptr) { 115 shellparam.nparam++; 116 argptr++; 117 } 118 optschanged(); 119 } 120 121 122 optschanged() { 123 setinteractive(iflag); 124 histedit(); 125 setjobctl(mflag); 126 } 127 128 /* 129 * Process shell options. The global variable argptr contains a pointer 130 * to the argument list; we advance it past the options. 131 */ 132 133 STATIC void 134 options(cmdline) { 135 register char *p; 136 int val; 137 int c; 138 139 if (cmdline) 140 minusc = NULL; 141 while ((p = *argptr) != NULL) { 142 argptr++; 143 if ((c = *p++) == '-') { 144 val = 1; 145 if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') { 146 if (!cmdline) { 147 /* "-" means turn off -x and -v */ 148 if (p[0] == '\0') 149 xflag = vflag = 0; 150 /* "--" means reset params */ 151 else if (*argptr == NULL) 152 setparam(argptr); 153 } 154 break; /* "-" or "--" terminates options */ 155 } 156 } else if (c == '+') { 157 val = 0; 158 } else { 159 argptr--; 160 break; 161 } 162 while ((c = *p++) != '\0') { 163 if (c == 'c' && cmdline) { 164 char *q; 165 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 166 if (*p == '\0') 167 #endif 168 q = *argptr++; 169 if (q == NULL || minusc != NULL) 170 error("Bad -c option"); 171 minusc = q; 172 #ifdef NOHACK 173 break; 174 #endif 175 } else if (c == 'o') { 176 minus_o(*argptr, val); 177 if (*argptr) 178 argptr++; 179 } else { 180 setoption(c, val); 181 } 182 } 183 } 184 } 185 186 STATIC void 187 minus_o(name, val) 188 char *name; 189 int val; 190 { 191 int i; 192 193 if (name == NULL) { 194 out1str("Current option settings\n"); 195 for (i = 0; i < NOPTS; i++) 196 out1fmt("%-16s%s\n", optlist[i].name, 197 optlist[i].val ? "on" : "off"); 198 } else { 199 for (i = 0; i < NOPTS; i++) 200 if (equal(name, optlist[i].name)) { 201 setoption(optlist[i].letter, val); 202 return; 203 } 204 error("Illegal option -o %s", name); 205 } 206 } 207 208 209 STATIC void 210 setoption(flag, val) 211 char flag; 212 int val; 213 { 214 int i; 215 216 for (i = 0; i < NOPTS; i++) 217 if (optlist[i].letter == flag) { 218 optlist[i].val = val; 219 if (val) { 220 /* #%$ hack for ksh semantics */ 221 if (flag == 'V') 222 Eflag = 0; 223 else if (flag == 'E') 224 Vflag = 0; 225 } 226 return; 227 } 228 error("Illegal option -%c", flag); 229 } 230 231 232 233 #ifdef mkinit 234 INCLUDE "options.h" 235 236 SHELLPROC { 237 int i; 238 239 for (i = 0; i < NOPTS; i++) 240 optlist[i].val = 0; 241 optschanged(); 242 243 } 244 #endif 245 246 247 /* 248 * Set the shell parameters. 249 */ 250 251 void 252 setparam(argv) 253 char **argv; 254 { 255 char **newparam; 256 char **ap; 257 int nparam; 258 259 for (nparam = 0 ; argv[nparam] ; nparam++); 260 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 261 while (*argv) { 262 *ap++ = savestr(*argv++); 263 } 264 *ap = NULL; 265 freeparam(&shellparam); 266 shellparam.malloc = 1; 267 shellparam.nparam = nparam; 268 shellparam.p = newparam; 269 shellparam.optnext = NULL; 270 } 271 272 273 /* 274 * Free the list of positional parameters. 275 */ 276 277 void 278 freeparam(param) 279 struct shparam *param; 280 { 281 char **ap; 282 283 if (param->malloc) { 284 for (ap = param->p ; *ap ; ap++) 285 ckfree(*ap); 286 ckfree(param->p); 287 } 288 } 289 290 291 292 /* 293 * The shift builtin command. 294 */ 295 296 shiftcmd(argc, argv) char **argv; { 297 int n; 298 char **ap1, **ap2; 299 300 n = 1; 301 if (argc > 1) 302 n = number(argv[1]); 303 if (n > shellparam.nparam) 304 error("can't shift that many"); 305 INTOFF; 306 shellparam.nparam -= n; 307 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 308 if (shellparam.malloc) 309 ckfree(*ap1); 310 } 311 ap2 = shellparam.p; 312 while ((*ap2++ = *ap1++) != NULL); 313 shellparam.optnext = NULL; 314 INTON; 315 return 0; 316 } 317 318 319 320 /* 321 * The set command builtin. 322 */ 323 324 setcmd(argc, argv) char **argv; { 325 if (argc == 1) 326 return showvarscmd(argc, argv); 327 INTOFF; 328 options(0); 329 optschanged(); 330 if (*argptr != NULL) { 331 setparam(argptr); 332 } 333 INTON; 334 return 0; 335 } 336 337 338 /* 339 * The getopts builtin. Shellparam.optnext points to the next argument 340 * to be processed. Shellparam.optptr points to the next character to 341 * be processed in the current argument. If shellparam.optnext is NULL, 342 * then it's the first time getopts has been called. 343 */ 344 345 getoptscmd(argc, argv) char **argv; { 346 register char *p, *q; 347 char c; 348 char s[10]; 349 350 if (argc != 3) 351 error("Usage: getopts optstring var"); 352 if (shellparam.optnext == NULL) { 353 shellparam.optnext = shellparam.p; 354 shellparam.optptr = NULL; 355 } 356 if ((p = shellparam.optptr) == NULL || *p == '\0') { 357 p = *shellparam.optnext; 358 if (p == NULL || *p != '-' || *++p == '\0') { 359 atend: 360 fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1); 361 setvar("OPTIND", s, 0); 362 shellparam.optnext = NULL; 363 return 1; 364 } 365 shellparam.optnext++; 366 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 367 goto atend; 368 } 369 c = *p++; 370 for (q = argv[1] ; *q != c ; ) { 371 if (*q == '\0') { 372 out1fmt("Illegal option -%c\n", c); 373 c = '?'; 374 goto out; 375 } 376 if (*++q == ':') 377 q++; 378 } 379 if (*++q == ':') { 380 if (*p == '\0' && (p = *shellparam.optnext++) == NULL) { 381 out1fmt("No arg for -%c option\n", c); 382 c = '?'; 383 goto out; 384 } 385 setvar("OPTARG", p, 0); 386 p = NULL; 387 } 388 out: 389 shellparam.optptr = p; 390 s[0] = c; 391 s[1] = '\0'; 392 setvar(argv[2], s, 0); 393 return 0; 394 } 395 396 /* 397 * XXX - should get rid of. have all builtins use getopt(3). the 398 * library getopt must have the BSD extension static variable "optreset" 399 * otherwise it can't be used within the shell safely. 400 * 401 * Standard option processing (a la getopt) for builtin routines. The 402 * only argument that is passed to nextopt is the option string; the 403 * other arguments are unnecessary. It return the character, or '\0' on 404 * end of input. 405 */ 406 407 int 408 nextopt(optstring) 409 char *optstring; 410 { 411 register char *p, *q; 412 char c; 413 414 if ((p = optptr) == NULL || *p == '\0') { 415 p = *argptr; 416 if (p == NULL || *p != '-' || *++p == '\0') 417 return '\0'; 418 argptr++; 419 if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 420 return '\0'; 421 } 422 c = *p++; 423 for (q = optstring ; *q != c ; ) { 424 if (*q == '\0') 425 error("Illegal option -%c", c); 426 if (*++q == ':') 427 q++; 428 } 429 if (*++q == ':') { 430 if (*p == '\0' && (p = *argptr++) == NULL) 431 error("No arg for -%c option", c); 432 optarg = p; 433 p = NULL; 434 } 435 optptr = p; 436 return c; 437 } 438