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. 3589730b29SDavid Greenman * 366d753bddSJoerg Wunsch * $Id: options.c,v 1.4 1995/08/06 19:35:33 joerg Exp $ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 404b88c807SRodney W. Grimes static char sccsid[] = "@(#)options.c 8.1 (Berkeley) 5/31/93"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 434b88c807SRodney W. Grimes #include "shell.h" 444b88c807SRodney W. Grimes #define DEFINE_OPTIONS 454b88c807SRodney W. Grimes #include "options.h" 464b88c807SRodney W. Grimes #undef DEFINE_OPTIONS 474b88c807SRodney W. Grimes #include "nodes.h" /* for other header files */ 484b88c807SRodney W. Grimes #include "eval.h" 494b88c807SRodney W. Grimes #include "jobs.h" 504b88c807SRodney W. Grimes #include "input.h" 514b88c807SRodney W. Grimes #include "output.h" 524b88c807SRodney W. Grimes #include "trap.h" 534b88c807SRodney W. Grimes #include "var.h" 544b88c807SRodney W. Grimes #include "memalloc.h" 554b88c807SRodney W. Grimes #include "error.h" 564b88c807SRodney W. Grimes #include "mystring.h" 574b88c807SRodney W. Grimes 584b88c807SRodney W. Grimes char *arg0; /* value of $0 */ 594b88c807SRodney W. Grimes struct shparam shellparam; /* current positional parameters */ 604b88c807SRodney W. Grimes char **argptr; /* argument list for builtin commands */ 614b88c807SRodney W. Grimes char *optarg; /* set by nextopt (like getopt) */ 624b88c807SRodney W. Grimes char *optptr; /* used by nextopt */ 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes char *minusc; /* argument to -c option */ 654b88c807SRodney W. Grimes 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes #ifdef __STDC__ 684b88c807SRodney W. Grimes STATIC void options(int); 694b88c807SRodney W. Grimes STATIC void setoption(int, int); 704b88c807SRodney W. Grimes STATIC void minus_o(char *, int); 714b88c807SRodney W. Grimes #else 724b88c807SRodney W. Grimes STATIC void options(); 734b88c807SRodney W. Grimes STATIC void setoption(); 744b88c807SRodney W. Grimes STATIC void minus_o(); 754b88c807SRodney W. Grimes #endif 764b88c807SRodney W. Grimes 774b88c807SRodney W. Grimes 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes /* 804b88c807SRodney W. Grimes * Process the shell command line arguments. 814b88c807SRodney W. Grimes */ 824b88c807SRodney W. Grimes 834b88c807SRodney W. Grimes void 844b88c807SRodney W. Grimes procargs(argc, argv) 854b88c807SRodney W. Grimes char **argv; 864b88c807SRodney W. Grimes { 874b88c807SRodney W. Grimes int i; 884b88c807SRodney W. Grimes 894b88c807SRodney W. Grimes argptr = argv; 904b88c807SRodney W. Grimes if (argc > 0) 914b88c807SRodney W. Grimes argptr++; 924b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 934b88c807SRodney W. Grimes optlist[i].val = 2; 944b88c807SRodney W. Grimes options(1); 954b88c807SRodney W. Grimes if (*argptr == NULL && minusc == NULL) 964b88c807SRodney W. Grimes sflag = 1; 974b88c807SRodney W. Grimes if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 984b88c807SRodney W. Grimes iflag = 1; 994b88c807SRodney W. Grimes if (mflag == 2) 1004b88c807SRodney W. Grimes mflag = iflag; 1014b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 1024b88c807SRodney W. Grimes if (optlist[i].val == 2) 1034b88c807SRodney W. Grimes optlist[i].val = 0; 1044b88c807SRodney W. Grimes arg0 = argv[0]; 1054b88c807SRodney W. Grimes if (sflag == 0 && minusc == NULL) { 1064b88c807SRodney W. Grimes commandname = arg0 = *argptr++; 1074b88c807SRodney W. Grimes setinputfile(commandname, 0); 1084b88c807SRodney W. Grimes } 1096d753bddSJoerg Wunsch if (minusc) 1106d753bddSJoerg Wunsch /* Posix.2: first arg after -c cmd is $0, remainder $1... */ 1116d753bddSJoerg Wunsch arg0 = *argptr++; 1124b88c807SRodney W. Grimes shellparam.p = argptr; 1134b88c807SRodney W. Grimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 1144b88c807SRodney W. Grimes while (*argptr) { 1154b88c807SRodney W. Grimes shellparam.nparam++; 1164b88c807SRodney W. Grimes argptr++; 1174b88c807SRodney W. Grimes } 1184b88c807SRodney W. Grimes optschanged(); 1194b88c807SRodney W. Grimes } 1204b88c807SRodney W. Grimes 1214b88c807SRodney W. Grimes 1224b88c807SRodney W. Grimes optschanged() { 1234b88c807SRodney W. Grimes setinteractive(iflag); 1244b88c807SRodney W. Grimes histedit(); 1254b88c807SRodney W. Grimes setjobctl(mflag); 1264b88c807SRodney W. Grimes } 1274b88c807SRodney W. Grimes 1284b88c807SRodney W. Grimes /* 1294b88c807SRodney W. Grimes * Process shell options. The global variable argptr contains a pointer 1304b88c807SRodney W. Grimes * to the argument list; we advance it past the options. 1314b88c807SRodney W. Grimes */ 1324b88c807SRodney W. Grimes 1334b88c807SRodney W. Grimes STATIC void 1344b88c807SRodney W. Grimes options(cmdline) { 1354b88c807SRodney W. Grimes register char *p; 1364b88c807SRodney W. Grimes int val; 1374b88c807SRodney W. Grimes int c; 1384b88c807SRodney W. Grimes 1394b88c807SRodney W. Grimes if (cmdline) 1404b88c807SRodney W. Grimes minusc = NULL; 1414b88c807SRodney W. Grimes while ((p = *argptr) != NULL) { 1424b88c807SRodney W. Grimes argptr++; 1434b88c807SRodney W. Grimes if ((c = *p++) == '-') { 1444b88c807SRodney W. Grimes val = 1; 1454b88c807SRodney W. Grimes if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') { 1464b88c807SRodney W. Grimes if (!cmdline) { 1474b88c807SRodney W. Grimes /* "-" means turn off -x and -v */ 1484b88c807SRodney W. Grimes if (p[0] == '\0') 1494b88c807SRodney W. Grimes xflag = vflag = 0; 1504b88c807SRodney W. Grimes /* "--" means reset params */ 1514b88c807SRodney W. Grimes else if (*argptr == NULL) 1524b88c807SRodney W. Grimes setparam(argptr); 1534b88c807SRodney W. Grimes } 1544b88c807SRodney W. Grimes break; /* "-" or "--" terminates options */ 1554b88c807SRodney W. Grimes } 1564b88c807SRodney W. Grimes } else if (c == '+') { 1574b88c807SRodney W. Grimes val = 0; 1584b88c807SRodney W. Grimes } else { 1594b88c807SRodney W. Grimes argptr--; 1604b88c807SRodney W. Grimes break; 1614b88c807SRodney W. Grimes } 1624b88c807SRodney W. Grimes while ((c = *p++) != '\0') { 1634b88c807SRodney W. Grimes if (c == 'c' && cmdline) { 1644b88c807SRodney W. Grimes char *q; 1654b88c807SRodney W. Grimes #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 1664b88c807SRodney W. Grimes if (*p == '\0') 1674b88c807SRodney W. Grimes #endif 1684b88c807SRodney W. Grimes q = *argptr++; 1694b88c807SRodney W. Grimes if (q == NULL || minusc != NULL) 1704b88c807SRodney W. Grimes error("Bad -c option"); 1714b88c807SRodney W. Grimes minusc = q; 1724b88c807SRodney W. Grimes #ifdef NOHACK 1734b88c807SRodney W. Grimes break; 1744b88c807SRodney W. Grimes #endif 1754b88c807SRodney W. Grimes } else if (c == 'o') { 1764b88c807SRodney W. Grimes minus_o(*argptr, val); 1774b88c807SRodney W. Grimes if (*argptr) 1784b88c807SRodney W. Grimes argptr++; 1794b88c807SRodney W. Grimes } else { 1804b88c807SRodney W. Grimes setoption(c, val); 1814b88c807SRodney W. Grimes } 1824b88c807SRodney W. Grimes } 1834b88c807SRodney W. Grimes } 1844b88c807SRodney W. Grimes } 1854b88c807SRodney W. Grimes 1864b88c807SRodney W. Grimes STATIC void 1874b88c807SRodney W. Grimes minus_o(name, val) 1884b88c807SRodney W. Grimes char *name; 1894b88c807SRodney W. Grimes int val; 1904b88c807SRodney W. Grimes { 1914b88c807SRodney W. Grimes int i; 1924b88c807SRodney W. Grimes 1934b88c807SRodney W. Grimes if (name == NULL) { 1944b88c807SRodney W. Grimes out1str("Current option settings\n"); 1954b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 1964b88c807SRodney W. Grimes out1fmt("%-16s%s\n", optlist[i].name, 1974b88c807SRodney W. Grimes optlist[i].val ? "on" : "off"); 1984b88c807SRodney W. Grimes } else { 1994b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2004b88c807SRodney W. Grimes if (equal(name, optlist[i].name)) { 2014b88c807SRodney W. Grimes setoption(optlist[i].letter, val); 2024b88c807SRodney W. Grimes return; 2034b88c807SRodney W. Grimes } 2044b88c807SRodney W. Grimes error("Illegal option -o %s", name); 2054b88c807SRodney W. Grimes } 2064b88c807SRodney W. Grimes } 2074b88c807SRodney W. Grimes 2084b88c807SRodney W. Grimes 2094b88c807SRodney W. Grimes STATIC void 2104b88c807SRodney W. Grimes setoption(flag, val) 2114b88c807SRodney W. Grimes char flag; 2124b88c807SRodney W. Grimes int val; 2134b88c807SRodney W. Grimes { 2144b88c807SRodney W. Grimes int i; 2154b88c807SRodney W. Grimes 2164b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2174b88c807SRodney W. Grimes if (optlist[i].letter == flag) { 2184b88c807SRodney W. Grimes optlist[i].val = val; 2194b88c807SRodney W. Grimes if (val) { 2204b88c807SRodney W. Grimes /* #%$ hack for ksh semantics */ 2214b88c807SRodney W. Grimes if (flag == 'V') 2224b88c807SRodney W. Grimes Eflag = 0; 2234b88c807SRodney W. Grimes else if (flag == 'E') 2244b88c807SRodney W. Grimes Vflag = 0; 2254b88c807SRodney W. Grimes } 2264b88c807SRodney W. Grimes return; 2274b88c807SRodney W. Grimes } 2284b88c807SRodney W. Grimes error("Illegal option -%c", flag); 2294b88c807SRodney W. Grimes } 2304b88c807SRodney W. Grimes 2314b88c807SRodney W. Grimes 2324b88c807SRodney W. Grimes 2334b88c807SRodney W. Grimes #ifdef mkinit 2344b88c807SRodney W. Grimes INCLUDE "options.h" 2354b88c807SRodney W. Grimes 2364b88c807SRodney W. Grimes SHELLPROC { 2374b88c807SRodney W. Grimes int i; 2384b88c807SRodney W. Grimes 2394b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++) 2404b88c807SRodney W. Grimes optlist[i].val = 0; 2414b88c807SRodney W. Grimes optschanged(); 2424b88c807SRodney W. Grimes 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes #endif 2454b88c807SRodney W. Grimes 2464b88c807SRodney W. Grimes 2474b88c807SRodney W. Grimes /* 2484b88c807SRodney W. Grimes * Set the shell parameters. 2494b88c807SRodney W. Grimes */ 2504b88c807SRodney W. Grimes 2514b88c807SRodney W. Grimes void 2524b88c807SRodney W. Grimes setparam(argv) 2534b88c807SRodney W. Grimes char **argv; 2544b88c807SRodney W. Grimes { 2554b88c807SRodney W. Grimes char **newparam; 2564b88c807SRodney W. Grimes char **ap; 2574b88c807SRodney W. Grimes int nparam; 2584b88c807SRodney W. Grimes 2594b88c807SRodney W. Grimes for (nparam = 0 ; argv[nparam] ; nparam++); 2604b88c807SRodney W. Grimes ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 2614b88c807SRodney W. Grimes while (*argv) { 2624b88c807SRodney W. Grimes *ap++ = savestr(*argv++); 2634b88c807SRodney W. Grimes } 2644b88c807SRodney W. Grimes *ap = NULL; 2654b88c807SRodney W. Grimes freeparam(&shellparam); 2664b88c807SRodney W. Grimes shellparam.malloc = 1; 2674b88c807SRodney W. Grimes shellparam.nparam = nparam; 2684b88c807SRodney W. Grimes shellparam.p = newparam; 2694b88c807SRodney W. Grimes shellparam.optnext = NULL; 2704b88c807SRodney W. Grimes } 2714b88c807SRodney W. Grimes 2724b88c807SRodney W. Grimes 2734b88c807SRodney W. Grimes /* 2744b88c807SRodney W. Grimes * Free the list of positional parameters. 2754b88c807SRodney W. Grimes */ 2764b88c807SRodney W. Grimes 2774b88c807SRodney W. Grimes void 2784b88c807SRodney W. Grimes freeparam(param) 2794b88c807SRodney W. Grimes struct shparam *param; 2804b88c807SRodney W. Grimes { 2814b88c807SRodney W. Grimes char **ap; 2824b88c807SRodney W. Grimes 2834b88c807SRodney W. Grimes if (param->malloc) { 2844b88c807SRodney W. Grimes for (ap = param->p ; *ap ; ap++) 2854b88c807SRodney W. Grimes ckfree(*ap); 2864b88c807SRodney W. Grimes ckfree(param->p); 2874b88c807SRodney W. Grimes } 2884b88c807SRodney W. Grimes } 2894b88c807SRodney W. Grimes 2904b88c807SRodney W. Grimes 2914b88c807SRodney W. Grimes 2924b88c807SRodney W. Grimes /* 2934b88c807SRodney W. Grimes * The shift builtin command. 2944b88c807SRodney W. Grimes */ 2954b88c807SRodney W. Grimes 2964b88c807SRodney W. Grimes shiftcmd(argc, argv) char **argv; { 2974b88c807SRodney W. Grimes int n; 2984b88c807SRodney W. Grimes char **ap1, **ap2; 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes n = 1; 3014b88c807SRodney W. Grimes if (argc > 1) 3024b88c807SRodney W. Grimes n = number(argv[1]); 3034b88c807SRodney W. Grimes if (n > shellparam.nparam) 3044b88c807SRodney W. Grimes error("can't shift that many"); 3054b88c807SRodney W. Grimes INTOFF; 3064b88c807SRodney W. Grimes shellparam.nparam -= n; 3074b88c807SRodney W. Grimes for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 3084b88c807SRodney W. Grimes if (shellparam.malloc) 3094b88c807SRodney W. Grimes ckfree(*ap1); 3104b88c807SRodney W. Grimes } 3114b88c807SRodney W. Grimes ap2 = shellparam.p; 3124b88c807SRodney W. Grimes while ((*ap2++ = *ap1++) != NULL); 3134b88c807SRodney W. Grimes shellparam.optnext = NULL; 3144b88c807SRodney W. Grimes INTON; 3154b88c807SRodney W. Grimes return 0; 3164b88c807SRodney W. Grimes } 3174b88c807SRodney W. Grimes 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes 3204b88c807SRodney W. Grimes /* 3214b88c807SRodney W. Grimes * The set command builtin. 3224b88c807SRodney W. Grimes */ 3234b88c807SRodney W. Grimes 3244b88c807SRodney W. Grimes setcmd(argc, argv) char **argv; { 3254b88c807SRodney W. Grimes if (argc == 1) 3264b88c807SRodney W. Grimes return showvarscmd(argc, argv); 3274b88c807SRodney W. Grimes INTOFF; 3284b88c807SRodney W. Grimes options(0); 3294b88c807SRodney W. Grimes optschanged(); 3304b88c807SRodney W. Grimes if (*argptr != NULL) { 3314b88c807SRodney W. Grimes setparam(argptr); 3324b88c807SRodney W. Grimes } 3334b88c807SRodney W. Grimes INTON; 3344b88c807SRodney W. Grimes return 0; 3354b88c807SRodney W. Grimes } 3364b88c807SRodney W. Grimes 3374b88c807SRodney W. Grimes 3384b88c807SRodney W. Grimes /* 3394b88c807SRodney W. Grimes * The getopts builtin. Shellparam.optnext points to the next argument 3404b88c807SRodney W. Grimes * to be processed. Shellparam.optptr points to the next character to 3414b88c807SRodney W. Grimes * be processed in the current argument. If shellparam.optnext is NULL, 3424b88c807SRodney W. Grimes * then it's the first time getopts has been called. 3434b88c807SRodney W. Grimes */ 3444b88c807SRodney W. Grimes 3454b88c807SRodney W. Grimes getoptscmd(argc, argv) char **argv; { 3464b88c807SRodney W. Grimes register char *p, *q; 3474b88c807SRodney W. Grimes char c; 3484b88c807SRodney W. Grimes char s[10]; 3494b88c807SRodney W. Grimes 3504b88c807SRodney W. Grimes if (argc != 3) 3514b88c807SRodney W. Grimes error("Usage: getopts optstring var"); 3524b88c807SRodney W. Grimes if (shellparam.optnext == NULL) { 3534b88c807SRodney W. Grimes shellparam.optnext = shellparam.p; 3544b88c807SRodney W. Grimes shellparam.optptr = NULL; 3554b88c807SRodney W. Grimes } 3564b88c807SRodney W. Grimes if ((p = shellparam.optptr) == NULL || *p == '\0') { 3574b88c807SRodney W. Grimes p = *shellparam.optnext; 3584b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0') { 3594b88c807SRodney W. Grimes atend: 3604b88c807SRodney W. Grimes fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1); 3614b88c807SRodney W. Grimes setvar("OPTIND", s, 0); 3624b88c807SRodney W. Grimes shellparam.optnext = NULL; 3634b88c807SRodney W. Grimes return 1; 3644b88c807SRodney W. Grimes } 3654b88c807SRodney W. Grimes shellparam.optnext++; 3664b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 3674b88c807SRodney W. Grimes goto atend; 3684b88c807SRodney W. Grimes } 3694b88c807SRodney W. Grimes c = *p++; 3704b88c807SRodney W. Grimes for (q = argv[1] ; *q != c ; ) { 3714b88c807SRodney W. Grimes if (*q == '\0') { 3724b88c807SRodney W. Grimes out1fmt("Illegal option -%c\n", c); 3734b88c807SRodney W. Grimes c = '?'; 3744b88c807SRodney W. Grimes goto out; 3754b88c807SRodney W. Grimes } 3764b88c807SRodney W. Grimes if (*++q == ':') 3774b88c807SRodney W. Grimes q++; 3784b88c807SRodney W. Grimes } 3794b88c807SRodney W. Grimes if (*++q == ':') { 38078e1fb60SJoerg Wunsch if (*p == '\0' && (p = *shellparam.optnext++) == NULL) { 3814b88c807SRodney W. Grimes out1fmt("No arg for -%c option\n", c); 3824b88c807SRodney W. Grimes c = '?'; 3834b88c807SRodney W. Grimes goto out; 3844b88c807SRodney W. Grimes } 3854b88c807SRodney W. Grimes setvar("OPTARG", p, 0); 3864b88c807SRodney W. Grimes p = NULL; 3874b88c807SRodney W. Grimes } 3884b88c807SRodney W. Grimes out: 3894b88c807SRodney W. Grimes shellparam.optptr = p; 3904b88c807SRodney W. Grimes s[0] = c; 3914b88c807SRodney W. Grimes s[1] = '\0'; 3924b88c807SRodney W. Grimes setvar(argv[2], s, 0); 3934b88c807SRodney W. Grimes return 0; 3944b88c807SRodney W. Grimes } 3954b88c807SRodney W. Grimes 3964b88c807SRodney W. Grimes /* 3974b88c807SRodney W. Grimes * XXX - should get rid of. have all builtins use getopt(3). the 3984b88c807SRodney W. Grimes * library getopt must have the BSD extension static variable "optreset" 3994b88c807SRodney W. Grimes * otherwise it can't be used within the shell safely. 4004b88c807SRodney W. Grimes * 4014b88c807SRodney W. Grimes * Standard option processing (a la getopt) for builtin routines. The 4024b88c807SRodney W. Grimes * only argument that is passed to nextopt is the option string; the 4034b88c807SRodney W. Grimes * other arguments are unnecessary. It return the character, or '\0' on 4044b88c807SRodney W. Grimes * end of input. 4054b88c807SRodney W. Grimes */ 4064b88c807SRodney W. Grimes 4074b88c807SRodney W. Grimes int 4084b88c807SRodney W. Grimes nextopt(optstring) 4094b88c807SRodney W. Grimes char *optstring; 4104b88c807SRodney W. Grimes { 4114b88c807SRodney W. Grimes register char *p, *q; 4124b88c807SRodney W. Grimes char c; 4134b88c807SRodney W. Grimes 4144b88c807SRodney W. Grimes if ((p = optptr) == NULL || *p == '\0') { 4154b88c807SRodney W. Grimes p = *argptr; 4164b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0') 4174b88c807SRodney W. Grimes return '\0'; 4184b88c807SRodney W. Grimes argptr++; 4194b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 4204b88c807SRodney W. Grimes return '\0'; 4214b88c807SRodney W. Grimes } 4224b88c807SRodney W. Grimes c = *p++; 4234b88c807SRodney W. Grimes for (q = optstring ; *q != c ; ) { 4244b88c807SRodney W. Grimes if (*q == '\0') 4254b88c807SRodney W. Grimes error("Illegal option -%c", c); 4264b88c807SRodney W. Grimes if (*++q == ':') 4274b88c807SRodney W. Grimes q++; 4284b88c807SRodney W. Grimes } 4294b88c807SRodney W. Grimes if (*++q == ':') { 4304b88c807SRodney W. Grimes if (*p == '\0' && (p = *argptr++) == NULL) 4314b88c807SRodney W. Grimes error("No arg for -%c option", c); 4324b88c807SRodney W. Grimes optarg = p; 4334b88c807SRodney W. Grimes p = NULL; 4344b88c807SRodney W. Grimes } 4354b88c807SRodney W. Grimes optptr = p; 4364b88c807SRodney W. Grimes return c; 4374b88c807SRodney W. Grimes } 438