options.c revision 1556
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 381556Srgrimesstatic char sccsid[] = "@(#)options.c 8.1 (Berkeley) 5/31/93"; 391556Srgrimes#endif /* not lint */ 401556Srgrimes 411556Srgrimes#include "shell.h" 421556Srgrimes#define DEFINE_OPTIONS 431556Srgrimes#include "options.h" 441556Srgrimes#undef DEFINE_OPTIONS 451556Srgrimes#include "nodes.h" /* for other header files */ 461556Srgrimes#include "eval.h" 471556Srgrimes#include "jobs.h" 481556Srgrimes#include "input.h" 491556Srgrimes#include "output.h" 501556Srgrimes#include "trap.h" 511556Srgrimes#include "var.h" 521556Srgrimes#include "memalloc.h" 531556Srgrimes#include "error.h" 541556Srgrimes#include "mystring.h" 551556Srgrimes 561556Srgrimeschar *arg0; /* value of $0 */ 571556Srgrimesstruct shparam shellparam; /* current positional parameters */ 581556Srgrimeschar **argptr; /* argument list for builtin commands */ 591556Srgrimeschar *optarg; /* set by nextopt (like getopt) */ 601556Srgrimeschar *optptr; /* used by nextopt */ 611556Srgrimes 621556Srgrimeschar *minusc; /* argument to -c option */ 631556Srgrimes 641556Srgrimes 651556Srgrimes#ifdef __STDC__ 661556SrgrimesSTATIC void options(int); 671556SrgrimesSTATIC void setoption(int, int); 681556SrgrimesSTATIC void minus_o(char *, int); 691556Srgrimes#else 701556SrgrimesSTATIC void options(); 711556SrgrimesSTATIC void setoption(); 721556SrgrimesSTATIC void minus_o(); 731556Srgrimes#endif 741556Srgrimes 751556Srgrimes 761556Srgrimes 771556Srgrimes/* 781556Srgrimes * Process the shell command line arguments. 791556Srgrimes */ 801556Srgrimes 811556Srgrimesvoid 821556Srgrimesprocargs(argc, argv) 831556Srgrimes char **argv; 841556Srgrimes { 851556Srgrimes int i; 861556Srgrimes 871556Srgrimes argptr = argv; 881556Srgrimes if (argc > 0) 891556Srgrimes argptr++; 901556Srgrimes for (i = 0; i < NOPTS; i++) 911556Srgrimes optlist[i].val = 2; 921556Srgrimes options(1); 931556Srgrimes if (*argptr == NULL && minusc == NULL) 941556Srgrimes sflag = 1; 951556Srgrimes if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 961556Srgrimes iflag = 1; 971556Srgrimes if (mflag == 2) 981556Srgrimes mflag = iflag; 991556Srgrimes for (i = 0; i < NOPTS; i++) 1001556Srgrimes if (optlist[i].val == 2) 1011556Srgrimes optlist[i].val = 0; 1021556Srgrimes arg0 = argv[0]; 1031556Srgrimes if (sflag == 0 && minusc == NULL) { 1041556Srgrimes commandname = arg0 = *argptr++; 1051556Srgrimes setinputfile(commandname, 0); 1061556Srgrimes } 1071556Srgrimes shellparam.p = argptr; 1081556Srgrimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 1091556Srgrimes while (*argptr) { 1101556Srgrimes shellparam.nparam++; 1111556Srgrimes argptr++; 1121556Srgrimes } 1131556Srgrimes optschanged(); 1141556Srgrimes} 1151556Srgrimes 1161556Srgrimes 1171556Srgrimesoptschanged() { 1181556Srgrimes setinteractive(iflag); 1191556Srgrimes histedit(); 1201556Srgrimes setjobctl(mflag); 1211556Srgrimes} 1221556Srgrimes 1231556Srgrimes/* 1241556Srgrimes * Process shell options. The global variable argptr contains a pointer 1251556Srgrimes * to the argument list; we advance it past the options. 1261556Srgrimes */ 1271556Srgrimes 1281556SrgrimesSTATIC void 1291556Srgrimesoptions(cmdline) { 1301556Srgrimes register char *p; 1311556Srgrimes int val; 1321556Srgrimes int c; 1331556Srgrimes 1341556Srgrimes if (cmdline) 1351556Srgrimes minusc = NULL; 1361556Srgrimes while ((p = *argptr) != NULL) { 1371556Srgrimes argptr++; 1381556Srgrimes if ((c = *p++) == '-') { 1391556Srgrimes val = 1; 1401556Srgrimes if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') { 1411556Srgrimes if (!cmdline) { 1421556Srgrimes /* "-" means turn off -x and -v */ 1431556Srgrimes if (p[0] == '\0') 1441556Srgrimes xflag = vflag = 0; 1451556Srgrimes /* "--" means reset params */ 1461556Srgrimes else if (*argptr == NULL) 1471556Srgrimes setparam(argptr); 1481556Srgrimes } 1491556Srgrimes break; /* "-" or "--" terminates options */ 1501556Srgrimes } 1511556Srgrimes } else if (c == '+') { 1521556Srgrimes val = 0; 1531556Srgrimes } else { 1541556Srgrimes argptr--; 1551556Srgrimes break; 1561556Srgrimes } 1571556Srgrimes while ((c = *p++) != '\0') { 1581556Srgrimes if (c == 'c' && cmdline) { 1591556Srgrimes char *q; 1601556Srgrimes#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 1611556Srgrimes if (*p == '\0') 1621556Srgrimes#endif 1631556Srgrimes q = *argptr++; 1641556Srgrimes if (q == NULL || minusc != NULL) 1651556Srgrimes error("Bad -c option"); 1661556Srgrimes minusc = q; 1671556Srgrimes#ifdef NOHACK 1681556Srgrimes break; 1691556Srgrimes#endif 1701556Srgrimes } else if (c == 'o') { 1711556Srgrimes minus_o(*argptr, val); 1721556Srgrimes if (*argptr) 1731556Srgrimes argptr++; 1741556Srgrimes } else { 1751556Srgrimes setoption(c, val); 1761556Srgrimes } 1771556Srgrimes } 1781556Srgrimes } 1791556Srgrimes} 1801556Srgrimes 1811556SrgrimesSTATIC void 1821556Srgrimesminus_o(name, val) 1831556Srgrimes char *name; 1841556Srgrimes int val; 1851556Srgrimes{ 1861556Srgrimes int i; 1871556Srgrimes 1881556Srgrimes if (name == NULL) { 1891556Srgrimes out1str("Current option settings\n"); 1901556Srgrimes for (i = 0; i < NOPTS; i++) 1911556Srgrimes out1fmt("%-16s%s\n", optlist[i].name, 1921556Srgrimes optlist[i].val ? "on" : "off"); 1931556Srgrimes } else { 1941556Srgrimes for (i = 0; i < NOPTS; i++) 1951556Srgrimes if (equal(name, optlist[i].name)) { 1961556Srgrimes setoption(optlist[i].letter, val); 1971556Srgrimes return; 1981556Srgrimes } 1991556Srgrimes error("Illegal option -o %s", name); 2001556Srgrimes } 2011556Srgrimes} 2021556Srgrimes 2031556Srgrimes 2041556SrgrimesSTATIC void 2051556Srgrimessetoption(flag, val) 2061556Srgrimes char flag; 2071556Srgrimes int val; 2081556Srgrimes { 2091556Srgrimes int i; 2101556Srgrimes 2111556Srgrimes for (i = 0; i < NOPTS; i++) 2121556Srgrimes if (optlist[i].letter == flag) { 2131556Srgrimes optlist[i].val = val; 2141556Srgrimes if (val) { 2151556Srgrimes /* #%$ hack for ksh semantics */ 2161556Srgrimes if (flag == 'V') 2171556Srgrimes Eflag = 0; 2181556Srgrimes else if (flag == 'E') 2191556Srgrimes Vflag = 0; 2201556Srgrimes } 2211556Srgrimes return; 2221556Srgrimes } 2231556Srgrimes error("Illegal option -%c", flag); 2241556Srgrimes} 2251556Srgrimes 2261556Srgrimes 2271556Srgrimes 2281556Srgrimes#ifdef mkinit 2291556SrgrimesINCLUDE "options.h" 2301556Srgrimes 2311556SrgrimesSHELLPROC { 2321556Srgrimes int i; 2331556Srgrimes 2341556Srgrimes for (i = 0; i < NOPTS; i++) 2351556Srgrimes optlist[i].val = 0; 2361556Srgrimes optschanged(); 2371556Srgrimes 2381556Srgrimes} 2391556Srgrimes#endif 2401556Srgrimes 2411556Srgrimes 2421556Srgrimes/* 2431556Srgrimes * Set the shell parameters. 2441556Srgrimes */ 2451556Srgrimes 2461556Srgrimesvoid 2471556Srgrimessetparam(argv) 2481556Srgrimes char **argv; 2491556Srgrimes { 2501556Srgrimes char **newparam; 2511556Srgrimes char **ap; 2521556Srgrimes int nparam; 2531556Srgrimes 2541556Srgrimes for (nparam = 0 ; argv[nparam] ; nparam++); 2551556Srgrimes ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 2561556Srgrimes while (*argv) { 2571556Srgrimes *ap++ = savestr(*argv++); 2581556Srgrimes } 2591556Srgrimes *ap = NULL; 2601556Srgrimes freeparam(&shellparam); 2611556Srgrimes shellparam.malloc = 1; 2621556Srgrimes shellparam.nparam = nparam; 2631556Srgrimes shellparam.p = newparam; 2641556Srgrimes shellparam.optnext = NULL; 2651556Srgrimes} 2661556Srgrimes 2671556Srgrimes 2681556Srgrimes/* 2691556Srgrimes * Free the list of positional parameters. 2701556Srgrimes */ 2711556Srgrimes 2721556Srgrimesvoid 2731556Srgrimesfreeparam(param) 2741556Srgrimes struct shparam *param; 2751556Srgrimes { 2761556Srgrimes char **ap; 2771556Srgrimes 2781556Srgrimes if (param->malloc) { 2791556Srgrimes for (ap = param->p ; *ap ; ap++) 2801556Srgrimes ckfree(*ap); 2811556Srgrimes ckfree(param->p); 2821556Srgrimes } 2831556Srgrimes} 2841556Srgrimes 2851556Srgrimes 2861556Srgrimes 2871556Srgrimes/* 2881556Srgrimes * The shift builtin command. 2891556Srgrimes */ 2901556Srgrimes 2911556Srgrimesshiftcmd(argc, argv) char **argv; { 2921556Srgrimes int n; 2931556Srgrimes char **ap1, **ap2; 2941556Srgrimes 2951556Srgrimes n = 1; 2961556Srgrimes if (argc > 1) 2971556Srgrimes n = number(argv[1]); 2981556Srgrimes if (n > shellparam.nparam) 2991556Srgrimes error("can't shift that many"); 3001556Srgrimes INTOFF; 3011556Srgrimes shellparam.nparam -= n; 3021556Srgrimes for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 3031556Srgrimes if (shellparam.malloc) 3041556Srgrimes ckfree(*ap1); 3051556Srgrimes } 3061556Srgrimes ap2 = shellparam.p; 3071556Srgrimes while ((*ap2++ = *ap1++) != NULL); 3081556Srgrimes shellparam.optnext = NULL; 3091556Srgrimes INTON; 3101556Srgrimes return 0; 3111556Srgrimes} 3121556Srgrimes 3131556Srgrimes 3141556Srgrimes 3151556Srgrimes/* 3161556Srgrimes * The set command builtin. 3171556Srgrimes */ 3181556Srgrimes 3191556Srgrimessetcmd(argc, argv) char **argv; { 3201556Srgrimes if (argc == 1) 3211556Srgrimes return showvarscmd(argc, argv); 3221556Srgrimes INTOFF; 3231556Srgrimes options(0); 3241556Srgrimes optschanged(); 3251556Srgrimes if (*argptr != NULL) { 3261556Srgrimes setparam(argptr); 3271556Srgrimes } 3281556Srgrimes INTON; 3291556Srgrimes return 0; 3301556Srgrimes} 3311556Srgrimes 3321556Srgrimes 3331556Srgrimes/* 3341556Srgrimes * The getopts builtin. Shellparam.optnext points to the next argument 3351556Srgrimes * to be processed. Shellparam.optptr points to the next character to 3361556Srgrimes * be processed in the current argument. If shellparam.optnext is NULL, 3371556Srgrimes * then it's the first time getopts has been called. 3381556Srgrimes */ 3391556Srgrimes 3401556Srgrimesgetoptscmd(argc, argv) char **argv; { 3411556Srgrimes register char *p, *q; 3421556Srgrimes char c; 3431556Srgrimes char s[10]; 3441556Srgrimes 3451556Srgrimes if (argc != 3) 3461556Srgrimes error("Usage: getopts optstring var"); 3471556Srgrimes if (shellparam.optnext == NULL) { 3481556Srgrimes shellparam.optnext = shellparam.p; 3491556Srgrimes shellparam.optptr = NULL; 3501556Srgrimes } 3511556Srgrimes if ((p = shellparam.optptr) == NULL || *p == '\0') { 3521556Srgrimes p = *shellparam.optnext; 3531556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') { 3541556Srgrimesatend: 3551556Srgrimes fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1); 3561556Srgrimes setvar("OPTIND", s, 0); 3571556Srgrimes shellparam.optnext = NULL; 3581556Srgrimes return 1; 3591556Srgrimes } 3601556Srgrimes shellparam.optnext++; 3611556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 3621556Srgrimes goto atend; 3631556Srgrimes } 3641556Srgrimes c = *p++; 3651556Srgrimes for (q = argv[1] ; *q != c ; ) { 3661556Srgrimes if (*q == '\0') { 3671556Srgrimes out1fmt("Illegal option -%c\n", c); 3681556Srgrimes c = '?'; 3691556Srgrimes goto out; 3701556Srgrimes } 3711556Srgrimes if (*++q == ':') 3721556Srgrimes q++; 3731556Srgrimes } 3741556Srgrimes if (*++q == ':') { 3751556Srgrimes if (*p == '\0' && (p = *shellparam.optnext) == NULL) { 3761556Srgrimes out1fmt("No arg for -%c option\n", c); 3771556Srgrimes c = '?'; 3781556Srgrimes goto out; 3791556Srgrimes } 3801556Srgrimes shellparam.optnext++; 3811556Srgrimes setvar("OPTARG", p, 0); 3821556Srgrimes p = NULL; 3831556Srgrimes } 3841556Srgrimesout: 3851556Srgrimes shellparam.optptr = p; 3861556Srgrimes s[0] = c; 3871556Srgrimes s[1] = '\0'; 3881556Srgrimes setvar(argv[2], s, 0); 3891556Srgrimes return 0; 3901556Srgrimes} 3911556Srgrimes 3921556Srgrimes/* 3931556Srgrimes * XXX - should get rid of. have all builtins use getopt(3). the 3941556Srgrimes * library getopt must have the BSD extension static variable "optreset" 3951556Srgrimes * otherwise it can't be used within the shell safely. 3961556Srgrimes * 3971556Srgrimes * Standard option processing (a la getopt) for builtin routines. The 3981556Srgrimes * only argument that is passed to nextopt is the option string; the 3991556Srgrimes * other arguments are unnecessary. It return the character, or '\0' on 4001556Srgrimes * end of input. 4011556Srgrimes */ 4021556Srgrimes 4031556Srgrimesint 4041556Srgrimesnextopt(optstring) 4051556Srgrimes char *optstring; 4061556Srgrimes { 4071556Srgrimes register char *p, *q; 4081556Srgrimes char c; 4091556Srgrimes 4101556Srgrimes if ((p = optptr) == NULL || *p == '\0') { 4111556Srgrimes p = *argptr; 4121556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') 4131556Srgrimes return '\0'; 4141556Srgrimes argptr++; 4151556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 4161556Srgrimes return '\0'; 4171556Srgrimes } 4181556Srgrimes c = *p++; 4191556Srgrimes for (q = optstring ; *q != c ; ) { 4201556Srgrimes if (*q == '\0') 4211556Srgrimes error("Illegal option -%c", c); 4221556Srgrimes if (*++q == ':') 4231556Srgrimes q++; 4241556Srgrimes } 4251556Srgrimes if (*++q == ':') { 4261556Srgrimes if (*p == '\0' && (p = *argptr++) == NULL) 4271556Srgrimes error("No arg for -%c option", c); 4281556Srgrimes optarg = p; 4291556Srgrimes p = NULL; 4301556Srgrimes } 4311556Srgrimes optptr = p; 4321556Srgrimes return c; 4331556Srgrimes} 434