14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 354b88c807SRodney W. Grimes */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 383d7b5b93SPhilippe Charnier #if 0 393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; 403d7b5b93SPhilippe Charnier #endif 413d7b5b93SPhilippe Charnier static const char rcsid[] = 422a456239SPeter Wemm "$FreeBSD$"; 434b88c807SRodney W. Grimes #endif /* not lint */ 444b88c807SRodney W. Grimes 45aa9caaf6SPeter Wemm #include <signal.h> 46aa9caaf6SPeter Wemm #include <unistd.h> 47aa9caaf6SPeter Wemm #include <stdlib.h> 48aa9caaf6SPeter Wemm 494b88c807SRodney W. Grimes #include "shell.h" 504b88c807SRodney W. Grimes #define DEFINE_OPTIONS 514b88c807SRodney W. Grimes #include "options.h" 524b88c807SRodney W. Grimes #undef DEFINE_OPTIONS 534b88c807SRodney W. Grimes #include "nodes.h" /* for other header files */ 544b88c807SRodney W. Grimes #include "eval.h" 554b88c807SRodney W. Grimes #include "jobs.h" 564b88c807SRodney W. Grimes #include "input.h" 574b88c807SRodney W. Grimes #include "output.h" 584b88c807SRodney W. Grimes #include "trap.h" 594b88c807SRodney W. Grimes #include "var.h" 604b88c807SRodney W. Grimes #include "memalloc.h" 614b88c807SRodney W. Grimes #include "error.h" 624b88c807SRodney W. Grimes #include "mystring.h" 63aa9caaf6SPeter Wemm #ifndef NO_HISTORY 64aa9caaf6SPeter Wemm #include "myhistedit.h" 65aa9caaf6SPeter Wemm #endif 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes char *arg0; /* value of $0 */ 684b88c807SRodney W. Grimes struct shparam shellparam; /* current positional parameters */ 694b88c807SRodney W. Grimes char **argptr; /* argument list for builtin commands */ 70f01e3d0cSMartin Cracauer char *shoptarg; /* set by nextopt (like getopt) */ 714b88c807SRodney W. Grimes char *optptr; /* used by nextopt */ 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes char *minusc; /* argument to -c option */ 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes 765134c3f7SWarner Losh STATIC void options(int); 775134c3f7SWarner Losh STATIC void minus_o(char *, int); 785134c3f7SWarner Losh STATIC void setoption(int, int); 795134c3f7SWarner Losh STATIC int getopts(char *, char *, char **, char ***, char **); 804b88c807SRodney W. Grimes 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes /* 834b88c807SRodney W. Grimes * Process the shell command line arguments. 844b88c807SRodney W. Grimes */ 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes void 875134c3f7SWarner Losh procargs(int argc, char **argv) 884b88c807SRodney W. Grimes { 894b88c807SRodney W. Grimes int i; 904b88c807SRodney W. Grimes 914b88c807SRodney W. Grimes argptr = argv; 924b88c807SRodney W. Grimes if (argc > 0) 934b88c807SRodney W. Grimes argptr++; 944b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 954b88c807SRodney W. Grimes optlist[i].val = 2; 96621a31c6SSteve Price privileged = (getuid() != geteuid() || getgid() != getegid()); 974b88c807SRodney W. Grimes options(1); 984b88c807SRodney W. Grimes if (*argptr == NULL && minusc == NULL) 994b88c807SRodney W. Grimes sflag = 1; 1004b88c807SRodney W. Grimes if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 1014b88c807SRodney W. Grimes iflag = 1; 1024b88c807SRodney W. Grimes if (mflag == 2) 1034b88c807SRodney W. Grimes mflag = iflag; 1044b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 1054b88c807SRodney W. Grimes if (optlist[i].val == 2) 1064b88c807SRodney W. Grimes optlist[i].val = 0; 1074b88c807SRodney W. Grimes arg0 = argv[0]; 1084b88c807SRodney W. Grimes if (sflag == 0 && minusc == NULL) { 1094b88c807SRodney W. Grimes commandname = arg0 = *argptr++; 1104b88c807SRodney W. Grimes setinputfile(commandname, 0); 1114b88c807SRodney W. Grimes } 112ab0a2172SSteve Price /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 113904a3dc8SSteve Price if (argptr && minusc && *argptr) 1146d753bddSJoerg Wunsch arg0 = *argptr++; 11593d0e5efSSteve Price 1164b88c807SRodney W. Grimes shellparam.p = argptr; 11793d0e5efSSteve Price shellparam.reset = 1; 1184b88c807SRodney W. Grimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 1194b88c807SRodney W. Grimes while (*argptr) { 1204b88c807SRodney W. Grimes shellparam.nparam++; 1214b88c807SRodney W. Grimes argptr++; 1224b88c807SRodney W. Grimes } 1234b88c807SRodney W. Grimes optschanged(); 1244b88c807SRodney W. Grimes } 1254b88c807SRodney W. Grimes 1264b88c807SRodney W. Grimes 127aa9caaf6SPeter Wemm void 1285134c3f7SWarner Losh optschanged(void) 129aa9caaf6SPeter Wemm { 1304b88c807SRodney W. Grimes setinteractive(iflag); 131aa9caaf6SPeter Wemm #ifndef NO_HISTORY 1324b88c807SRodney W. Grimes histedit(); 133aa9caaf6SPeter Wemm #endif 1344b88c807SRodney W. Grimes setjobctl(mflag); 1354b88c807SRodney W. Grimes } 1364b88c807SRodney W. Grimes 1374b88c807SRodney W. Grimes /* 1384b88c807SRodney W. Grimes * Process shell options. The global variable argptr contains a pointer 1394b88c807SRodney W. Grimes * to the argument list; we advance it past the options. 1404b88c807SRodney W. Grimes */ 1414b88c807SRodney W. Grimes 1424b88c807SRodney W. Grimes STATIC void 1435134c3f7SWarner Losh options(int cmdline) 144aa9caaf6SPeter Wemm { 145904a3dc8SSteve Price char *p; 1464b88c807SRodney W. Grimes int val; 1474b88c807SRodney W. Grimes int c; 1484b88c807SRodney W. Grimes 1494b88c807SRodney W. Grimes if (cmdline) 1504b88c807SRodney W. Grimes minusc = NULL; 1514b88c807SRodney W. Grimes while ((p = *argptr) != NULL) { 1524b88c807SRodney W. Grimes argptr++; 1534b88c807SRodney W. Grimes if ((c = *p++) == '-') { 1544b88c807SRodney W. Grimes val = 1; 155aa9caaf6SPeter Wemm if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { 1564b88c807SRodney W. Grimes if (!cmdline) { 1574b88c807SRodney W. Grimes /* "-" means turn off -x and -v */ 1584b88c807SRodney W. Grimes if (p[0] == '\0') 1594b88c807SRodney W. Grimes xflag = vflag = 0; 1604b88c807SRodney W. Grimes /* "--" means reset params */ 1614b88c807SRodney W. Grimes else if (*argptr == NULL) 1624b88c807SRodney W. Grimes setparam(argptr); 1634b88c807SRodney W. Grimes } 1644b88c807SRodney W. Grimes break; /* "-" or "--" terminates options */ 1654b88c807SRodney W. Grimes } 1664b88c807SRodney W. Grimes } else if (c == '+') { 1674b88c807SRodney W. Grimes val = 0; 1684b88c807SRodney W. Grimes } else { 1694b88c807SRodney W. Grimes argptr--; 1704b88c807SRodney W. Grimes break; 1714b88c807SRodney W. Grimes } 1724b88c807SRodney W. Grimes while ((c = *p++) != '\0') { 1734b88c807SRodney W. Grimes if (c == 'c' && cmdline) { 1744b88c807SRodney W. Grimes char *q; 1754b88c807SRodney W. Grimes #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 1764b88c807SRodney W. Grimes if (*p == '\0') 1774b88c807SRodney W. Grimes #endif 1784b88c807SRodney W. Grimes q = *argptr++; 1794b88c807SRodney W. Grimes if (q == NULL || minusc != NULL) 1804b88c807SRodney W. Grimes error("Bad -c option"); 1814b88c807SRodney W. Grimes minusc = q; 1824b88c807SRodney W. Grimes #ifdef NOHACK 1834b88c807SRodney W. Grimes break; 1844b88c807SRodney W. Grimes #endif 1854b88c807SRodney W. Grimes } else if (c == 'o') { 1864b88c807SRodney W. Grimes minus_o(*argptr, val); 1874b88c807SRodney W. Grimes if (*argptr) 1884b88c807SRodney W. Grimes argptr++; 1894b88c807SRodney W. Grimes } else { 190621a31c6SSteve Price if (c == 'p' && !val && privileged) { 191621a31c6SSteve Price (void) setuid(getuid()); 192621a31c6SSteve Price (void) setgid(getgid()); 193621a31c6SSteve Price } 1944b88c807SRodney W. Grimes setoption(c, val); 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes } 1974b88c807SRodney W. Grimes } 1984b88c807SRodney W. Grimes } 1994b88c807SRodney W. Grimes 2004b88c807SRodney W. Grimes STATIC void 2015134c3f7SWarner Losh minus_o(char *name, int val) 2024b88c807SRodney W. Grimes { 2034b88c807SRodney W. Grimes int i; 2044b88c807SRodney W. Grimes 2054b88c807SRodney W. Grimes if (name == NULL) { 2064b88c807SRodney W. Grimes out1str("Current option settings\n"); 2074b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2084b88c807SRodney W. Grimes out1fmt("%-16s%s\n", optlist[i].name, 2094b88c807SRodney W. Grimes optlist[i].val ? "on" : "off"); 2104b88c807SRodney W. Grimes } else { 2114b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2124b88c807SRodney W. Grimes if (equal(name, optlist[i].name)) { 213621a31c6SSteve Price if (!val && privileged && equal(name, "privileged")) { 214621a31c6SSteve Price (void) setuid(getuid()); 215621a31c6SSteve Price (void) setgid(getgid()); 216621a31c6SSteve Price } 2174b88c807SRodney W. Grimes setoption(optlist[i].letter, val); 2184b88c807SRodney W. Grimes return; 2194b88c807SRodney W. Grimes } 2204b88c807SRodney W. Grimes error("Illegal option -o %s", name); 2214b88c807SRodney W. Grimes } 2224b88c807SRodney W. Grimes } 2234b88c807SRodney W. Grimes 2244b88c807SRodney W. Grimes 2254b88c807SRodney W. Grimes STATIC void 2265134c3f7SWarner Losh setoption(int flag, int val) 2274b88c807SRodney W. Grimes { 2284b88c807SRodney W. Grimes int i; 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2314b88c807SRodney W. Grimes if (optlist[i].letter == flag) { 2324b88c807SRodney W. Grimes optlist[i].val = val; 2334b88c807SRodney W. Grimes if (val) { 2344b88c807SRodney W. Grimes /* #%$ hack for ksh semantics */ 2354b88c807SRodney W. Grimes if (flag == 'V') 2364b88c807SRodney W. Grimes Eflag = 0; 2374b88c807SRodney W. Grimes else if (flag == 'E') 2384b88c807SRodney W. Grimes Vflag = 0; 2394b88c807SRodney W. Grimes } 2404b88c807SRodney W. Grimes return; 2414b88c807SRodney W. Grimes } 2424b88c807SRodney W. Grimes error("Illegal option -%c", flag); 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes 2454b88c807SRodney W. Grimes 2464b88c807SRodney W. Grimes 2474b88c807SRodney W. Grimes #ifdef mkinit 2484b88c807SRodney W. Grimes INCLUDE "options.h" 2494b88c807SRodney W. Grimes 2504b88c807SRodney W. Grimes SHELLPROC { 2514b88c807SRodney W. Grimes int i; 2524b88c807SRodney W. Grimes 2534b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2544b88c807SRodney W. Grimes optlist[i].val = 0; 2554b88c807SRodney W. Grimes optschanged(); 2564b88c807SRodney W. Grimes 2574b88c807SRodney W. Grimes } 2584b88c807SRodney W. Grimes #endif 2594b88c807SRodney W. Grimes 2604b88c807SRodney W. Grimes 2614b88c807SRodney W. Grimes /* 2624b88c807SRodney W. Grimes * Set the shell parameters. 2634b88c807SRodney W. Grimes */ 2644b88c807SRodney W. Grimes 2654b88c807SRodney W. Grimes void 2665134c3f7SWarner Losh setparam(char **argv) 2674b88c807SRodney W. Grimes { 2684b88c807SRodney W. Grimes char **newparam; 2694b88c807SRodney W. Grimes char **ap; 2704b88c807SRodney W. Grimes int nparam; 2714b88c807SRodney W. Grimes 2724b88c807SRodney W. Grimes for (nparam = 0 ; argv[nparam] ; nparam++); 2734b88c807SRodney W. Grimes ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 2744b88c807SRodney W. Grimes while (*argv) { 2754b88c807SRodney W. Grimes *ap++ = savestr(*argv++); 2764b88c807SRodney W. Grimes } 2774b88c807SRodney W. Grimes *ap = NULL; 2784b88c807SRodney W. Grimes freeparam(&shellparam); 2794b88c807SRodney W. Grimes shellparam.malloc = 1; 2804b88c807SRodney W. Grimes shellparam.nparam = nparam; 2814b88c807SRodney W. Grimes shellparam.p = newparam; 2824b88c807SRodney W. Grimes shellparam.optnext = NULL; 2834b88c807SRodney W. Grimes } 2844b88c807SRodney W. Grimes 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes /* 2874b88c807SRodney W. Grimes * Free the list of positional parameters. 2884b88c807SRodney W. Grimes */ 2894b88c807SRodney W. Grimes 2904b88c807SRodney W. Grimes void 2915134c3f7SWarner Losh freeparam(struct shparam *param) 2924b88c807SRodney W. Grimes { 2934b88c807SRodney W. Grimes char **ap; 2944b88c807SRodney W. Grimes 2954b88c807SRodney W. Grimes if (param->malloc) { 2964b88c807SRodney W. Grimes for (ap = param->p ; *ap ; ap++) 2974b88c807SRodney W. Grimes ckfree(*ap); 2984b88c807SRodney W. Grimes ckfree(param->p); 2994b88c807SRodney W. Grimes } 3004b88c807SRodney W. Grimes } 3014b88c807SRodney W. Grimes 3024b88c807SRodney W. Grimes 3034b88c807SRodney W. Grimes 3044b88c807SRodney W. Grimes /* 3054b88c807SRodney W. Grimes * The shift builtin command. 3064b88c807SRodney W. Grimes */ 3074b88c807SRodney W. Grimes 308aa9caaf6SPeter Wemm int 3095134c3f7SWarner Losh shiftcmd(int argc, char **argv) 310aa9caaf6SPeter Wemm { 3114b88c807SRodney W. Grimes int n; 3124b88c807SRodney W. Grimes char **ap1, **ap2; 3134b88c807SRodney W. Grimes 3144b88c807SRodney W. Grimes n = 1; 3154b88c807SRodney W. Grimes if (argc > 1) 3164b88c807SRodney W. Grimes n = number(argv[1]); 3174b88c807SRodney W. Grimes if (n > shellparam.nparam) 3184b88c807SRodney W. Grimes error("can't shift that many"); 3194b88c807SRodney W. Grimes INTOFF; 3204b88c807SRodney W. Grimes shellparam.nparam -= n; 3214b88c807SRodney W. Grimes for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 3224b88c807SRodney W. Grimes if (shellparam.malloc) 3234b88c807SRodney W. Grimes ckfree(*ap1); 3244b88c807SRodney W. Grimes } 3254b88c807SRodney W. Grimes ap2 = shellparam.p; 3264b88c807SRodney W. Grimes while ((*ap2++ = *ap1++) != NULL); 3274b88c807SRodney W. Grimes shellparam.optnext = NULL; 3284b88c807SRodney W. Grimes INTON; 3294b88c807SRodney W. Grimes return 0; 3304b88c807SRodney W. Grimes } 3314b88c807SRodney W. Grimes 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes 3344b88c807SRodney W. Grimes /* 3354b88c807SRodney W. Grimes * The set command builtin. 3364b88c807SRodney W. Grimes */ 3374b88c807SRodney W. Grimes 338aa9caaf6SPeter Wemm int 3395134c3f7SWarner Losh setcmd(int argc, char **argv) 340aa9caaf6SPeter Wemm { 3414b88c807SRodney W. Grimes if (argc == 1) 3424b88c807SRodney W. Grimes return showvarscmd(argc, argv); 3434b88c807SRodney W. Grimes INTOFF; 3444b88c807SRodney W. Grimes options(0); 3454b88c807SRodney W. Grimes optschanged(); 3464b88c807SRodney W. Grimes if (*argptr != NULL) { 3474b88c807SRodney W. Grimes setparam(argptr); 3484b88c807SRodney W. Grimes } 3494b88c807SRodney W. Grimes INTON; 3504b88c807SRodney W. Grimes return 0; 3514b88c807SRodney W. Grimes } 3524b88c807SRodney W. Grimes 3534b88c807SRodney W. Grimes 354ab0a2172SSteve Price void 3555134c3f7SWarner Losh getoptsreset(const char *value) 356ab0a2172SSteve Price { 357ab0a2172SSteve Price if (number(value) == 1) { 358ab0a2172SSteve Price shellparam.optnext = NULL; 359ab0a2172SSteve Price shellparam.reset = 1; 360ab0a2172SSteve Price } 361ab0a2172SSteve Price } 362ab0a2172SSteve Price 3634b88c807SRodney W. Grimes /* 3644b88c807SRodney W. Grimes * The getopts builtin. Shellparam.optnext points to the next argument 3654b88c807SRodney W. Grimes * to be processed. Shellparam.optptr points to the next character to 3664b88c807SRodney W. Grimes * be processed in the current argument. If shellparam.optnext is NULL, 3674b88c807SRodney W. Grimes * then it's the first time getopts has been called. 3684b88c807SRodney W. Grimes */ 3694b88c807SRodney W. Grimes 370aa9caaf6SPeter Wemm int 3715134c3f7SWarner Losh getoptscmd(int argc, char **argv) 372aa9caaf6SPeter Wemm { 373ab0a2172SSteve Price char **optbase = NULL; 374ab0a2172SSteve Price 375ab0a2172SSteve Price if (argc < 3) 376d3974088SDag-Erling Smørgrav error("usage: getopts optstring var [arg]"); 377ab0a2172SSteve Price else if (argc == 3) 378ab0a2172SSteve Price optbase = shellparam.p; 379ab0a2172SSteve Price else 380ab0a2172SSteve Price optbase = &argv[3]; 381ab0a2172SSteve Price 382ab0a2172SSteve Price if (shellparam.reset == 1) { 383ab0a2172SSteve Price shellparam.optnext = optbase; 384ab0a2172SSteve Price shellparam.optptr = NULL; 385ab0a2172SSteve Price shellparam.reset = 0; 386ab0a2172SSteve Price } 387ab0a2172SSteve Price 388ab0a2172SSteve Price return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 389ab0a2172SSteve Price &shellparam.optptr); 390ab0a2172SSteve Price } 391ab0a2172SSteve Price 392ab0a2172SSteve Price STATIC int 3935134c3f7SWarner Losh getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, 3945134c3f7SWarner Losh char **optptr) 395ab0a2172SSteve Price { 396904a3dc8SSteve Price char *p, *q; 397ab0a2172SSteve Price char c = '?'; 398ab0a2172SSteve Price int done = 0; 399ab0a2172SSteve Price int ind = 0; 400ab0a2172SSteve Price int err = 0; 4014b88c807SRodney W. Grimes char s[10]; 4024b88c807SRodney W. Grimes 403ab0a2172SSteve Price if ((p = *optptr) == NULL || *p == '\0') { 404ab0a2172SSteve Price /* Current word is done, advance */ 405ab0a2172SSteve Price if (*optnext == NULL) 406ab0a2172SSteve Price return 1; 407ab0a2172SSteve Price p = **optnext; 4084b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0') { 4094b88c807SRodney W. Grimes atend: 410ab0a2172SSteve Price ind = *optnext - optfirst + 1; 41193d0e5efSSteve Price *optnext = NULL; 41293d0e5efSSteve Price p = NULL; 413ab0a2172SSteve Price done = 1; 414ab0a2172SSteve Price goto out; 4154b88c807SRodney W. Grimes } 416ab0a2172SSteve Price (*optnext)++; 4174b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 4184b88c807SRodney W. Grimes goto atend; 4194b88c807SRodney W. Grimes } 420ab0a2172SSteve Price 4214b88c807SRodney W. Grimes c = *p++; 422ab0a2172SSteve Price for (q = optstr; *q != c; ) { 4234b88c807SRodney W. Grimes if (*q == '\0') { 424ab0a2172SSteve Price if (optstr[0] == ':') { 425ab0a2172SSteve Price s[0] = c; 426ab0a2172SSteve Price s[1] = '\0'; 427ab0a2172SSteve Price err |= setvarsafe("OPTARG", s, 0); 428ab0a2172SSteve Price } 429ab0a2172SSteve Price else { 4304b88c807SRodney W. Grimes out1fmt("Illegal option -%c\n", c); 431ab0a2172SSteve Price (void) unsetvar("OPTARG"); 432ab0a2172SSteve Price } 4334b88c807SRodney W. Grimes c = '?'; 434ab0a2172SSteve Price goto bad; 4354b88c807SRodney W. Grimes } 4364b88c807SRodney W. Grimes if (*++q == ':') 4374b88c807SRodney W. Grimes q++; 4384b88c807SRodney W. Grimes } 439ab0a2172SSteve Price 4404b88c807SRodney W. Grimes if (*++q == ':') { 441ab0a2172SSteve Price if (*p == '\0' && (p = **optnext) == NULL) { 442ab0a2172SSteve Price if (optstr[0] == ':') { 4434b88c807SRodney W. Grimes s[0] = c; 4444b88c807SRodney W. Grimes s[1] = '\0'; 445ab0a2172SSteve Price err |= setvarsafe("OPTARG", s, 0); 446ab0a2172SSteve Price c = ':'; 447ab0a2172SSteve Price } 448ab0a2172SSteve Price else { 449ab0a2172SSteve Price out1fmt("No arg for -%c option\n", c); 450ab0a2172SSteve Price (void) unsetvar("OPTARG"); 451ab0a2172SSteve Price c = '?'; 452ab0a2172SSteve Price } 453ab0a2172SSteve Price goto bad; 454ab0a2172SSteve Price } 455ab0a2172SSteve Price 456ab0a2172SSteve Price if (p == **optnext) 457ab0a2172SSteve Price (*optnext)++; 458ab0a2172SSteve Price setvarsafe("OPTARG", p, 0); 459ab0a2172SSteve Price p = NULL; 460ab0a2172SSteve Price } 461ab0a2172SSteve Price else 462ab0a2172SSteve Price setvarsafe("OPTARG", "", 0); 463ab0a2172SSteve Price ind = *optnext - optfirst + 1; 464ab0a2172SSteve Price goto out; 465ab0a2172SSteve Price 466ab0a2172SSteve Price bad: 467ab0a2172SSteve Price ind = 1; 468ab0a2172SSteve Price *optnext = NULL; 469ab0a2172SSteve Price p = NULL; 470ab0a2172SSteve Price out: 471ab0a2172SSteve Price *optptr = p; 472ab0a2172SSteve Price fmtstr(s, sizeof(s), "%d", ind); 473ab0a2172SSteve Price err |= setvarsafe("OPTIND", s, VNOFUNC); 474ab0a2172SSteve Price s[0] = c; 475ab0a2172SSteve Price s[1] = '\0'; 476ab0a2172SSteve Price err |= setvarsafe(optvar, s, 0); 477ab0a2172SSteve Price if (err) { 478ab0a2172SSteve Price *optnext = NULL; 479ab0a2172SSteve Price *optptr = NULL; 480ab0a2172SSteve Price flushall(); 481ab0a2172SSteve Price exraise(EXERROR); 482ab0a2172SSteve Price } 483ab0a2172SSteve Price return done; 4844b88c807SRodney W. Grimes } 4854b88c807SRodney W. Grimes 4864b88c807SRodney W. Grimes /* 4874b88c807SRodney W. Grimes * XXX - should get rid of. have all builtins use getopt(3). the 4884b88c807SRodney W. Grimes * library getopt must have the BSD extension static variable "optreset" 4894b88c807SRodney W. Grimes * otherwise it can't be used within the shell safely. 4904b88c807SRodney W. Grimes * 4914b88c807SRodney W. Grimes * Standard option processing (a la getopt) for builtin routines. The 4924b88c807SRodney W. Grimes * only argument that is passed to nextopt is the option string; the 4934b88c807SRodney W. Grimes * other arguments are unnecessary. It return the character, or '\0' on 4944b88c807SRodney W. Grimes * end of input. 4954b88c807SRodney W. Grimes */ 4964b88c807SRodney W. Grimes 4974b88c807SRodney W. Grimes int 4985134c3f7SWarner Losh nextopt(char *optstring) 4994b88c807SRodney W. Grimes { 500904a3dc8SSteve Price char *p, *q; 5014b88c807SRodney W. Grimes char c; 5024b88c807SRodney W. Grimes 5034b88c807SRodney W. Grimes if ((p = optptr) == NULL || *p == '\0') { 5044b88c807SRodney W. Grimes p = *argptr; 5054b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0') 5064b88c807SRodney W. Grimes return '\0'; 5074b88c807SRodney W. Grimes argptr++; 5084b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 5094b88c807SRodney W. Grimes return '\0'; 5104b88c807SRodney W. Grimes } 5114b88c807SRodney W. Grimes c = *p++; 5124b88c807SRodney W. Grimes for (q = optstring ; *q != c ; ) { 5134b88c807SRodney W. Grimes if (*q == '\0') 5144b88c807SRodney W. Grimes error("Illegal option -%c", c); 5154b88c807SRodney W. Grimes if (*++q == ':') 5164b88c807SRodney W. Grimes q++; 5174b88c807SRodney W. Grimes } 5184b88c807SRodney W. Grimes if (*++q == ':') { 5194b88c807SRodney W. Grimes if (*p == '\0' && (p = *argptr++) == NULL) 5204b88c807SRodney W. Grimes error("No arg for -%c option", c); 521f01e3d0cSMartin Cracauer shoptarg = p; 5224b88c807SRodney W. Grimes p = NULL; 5234b88c807SRodney W. Grimes } 5244b88c807SRodney W. Grimes optptr = p; 5254b88c807SRodney W. Grimes return c; 5264b88c807SRodney W. Grimes } 527