options.c revision 146255
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 * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/options.c 146255 2005-05-16 04:32:41Z gad $"); 401556Srgrimes 4117987Speter#include <signal.h> 4217987Speter#include <unistd.h> 4317987Speter#include <stdlib.h> 4417987Speter 451556Srgrimes#include "shell.h" 461556Srgrimes#define DEFINE_OPTIONS 471556Srgrimes#include "options.h" 481556Srgrimes#undef DEFINE_OPTIONS 491556Srgrimes#include "nodes.h" /* for other header files */ 501556Srgrimes#include "eval.h" 511556Srgrimes#include "jobs.h" 521556Srgrimes#include "input.h" 531556Srgrimes#include "output.h" 541556Srgrimes#include "trap.h" 551556Srgrimes#include "var.h" 561556Srgrimes#include "memalloc.h" 571556Srgrimes#include "error.h" 581556Srgrimes#include "mystring.h" 5917987Speter#ifndef NO_HISTORY 6017987Speter#include "myhistedit.h" 6117987Speter#endif 621556Srgrimes 631556Srgrimeschar *arg0; /* value of $0 */ 641556Srgrimesstruct shparam shellparam; /* current positional parameters */ 651556Srgrimeschar **argptr; /* argument list for builtin commands */ 6659436Scracauerchar *shoptarg; /* set by nextopt (like getopt) */ 671556Srgrimeschar *optptr; /* used by nextopt */ 681556Srgrimes 691556Srgrimeschar *minusc; /* argument to -c option */ 701556Srgrimes 711556Srgrimes 7290111SimpSTATIC void options(int); 7390111SimpSTATIC void minus_o(char *, int); 7490111SimpSTATIC void setoption(int, int); 7590111SimpSTATIC int getopts(char *, char *, char **, char ***, char **); 761556Srgrimes 771556Srgrimes 781556Srgrimes/* 791556Srgrimes * Process the shell command line arguments. 801556Srgrimes */ 811556Srgrimes 821556Srgrimesvoid 8390111Simpprocargs(int argc, char **argv) 8417987Speter{ 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; 9219240Ssteve privileged = (getuid() != geteuid() || getgid() != getegid()); 931556Srgrimes options(1); 941556Srgrimes if (*argptr == NULL && minusc == NULL) 951556Srgrimes sflag = 1; 961556Srgrimes if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) 971556Srgrimes iflag = 1; 981556Srgrimes if (mflag == 2) 991556Srgrimes mflag = iflag; 1001556Srgrimes for (i = 0; i < NOPTS; i++) 1011556Srgrimes if (optlist[i].val == 2) 1021556Srgrimes optlist[i].val = 0; 1031556Srgrimes arg0 = argv[0]; 1041556Srgrimes if (sflag == 0 && minusc == NULL) { 1051556Srgrimes commandname = arg0 = *argptr++; 1061556Srgrimes setinputfile(commandname, 0); 1071556Srgrimes } 10820425Ssteve /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 10925227Ssteve if (argptr && minusc && *argptr) 11011111Sjoerg arg0 = *argptr++; 11120742Ssteve 1121556Srgrimes shellparam.p = argptr; 11320742Ssteve shellparam.reset = 1; 1141556Srgrimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 1151556Srgrimes while (*argptr) { 1161556Srgrimes shellparam.nparam++; 1171556Srgrimes argptr++; 1181556Srgrimes } 1191556Srgrimes optschanged(); 1201556Srgrimes} 1211556Srgrimes 1221556Srgrimes 12317987Spetervoid 12490111Simpoptschanged(void) 12517987Speter{ 1261556Srgrimes setinteractive(iflag); 12717987Speter#ifndef NO_HISTORY 1281556Srgrimes histedit(); 12917987Speter#endif 1301556Srgrimes setjobctl(mflag); 1311556Srgrimes} 1321556Srgrimes 1331556Srgrimes/* 1341556Srgrimes * Process shell options. The global variable argptr contains a pointer 1351556Srgrimes * to the argument list; we advance it past the options. 1361556Srgrimes */ 1371556Srgrimes 1381556SrgrimesSTATIC void 13990111Simpoptions(int cmdline) 14017987Speter{ 141146255Sgad char *kp, *p; 1421556Srgrimes int val; 1431556Srgrimes int c; 1441556Srgrimes 1451556Srgrimes if (cmdline) 1461556Srgrimes minusc = NULL; 1471556Srgrimes while ((p = *argptr) != NULL) { 1481556Srgrimes argptr++; 1491556Srgrimes if ((c = *p++) == '-') { 1501556Srgrimes val = 1; 151141962Sgad /* A "-" or "--" terminates options */ 152141962Sgad if (p[0] == '\0') 153141962Sgad goto end_options1; 154141962Sgad if (p[0] == '-' && p[1] == '\0') 155141962Sgad goto end_options2; 156146255Sgad /** 157146255Sgad * For the benefit of `#!' lines in shell scripts, 158146255Sgad * treat a string of '-- *#.*' the same as '--'. 159146255Sgad * This is needed so that a script starting with: 160146255Sgad * #!/bin/sh -- # -*- perl -*- 161146255Sgad * will continue to work after a change is made to 162146255Sgad * kern/imgact_shell.c to NOT token-ize the options 163146255Sgad * specified on a '#!' line. A bit of a kludge, 164146255Sgad * but that trick is recommended in documentation 165146255Sgad * for some scripting languages, and we might as 166146255Sgad * well continue to support it. 167146255Sgad */ 168146255Sgad if (p[0] == '-') { 169146255Sgad kp = p + 1; 170146255Sgad while (*kp == ' ' || *kp == '\t') 171146255Sgad kp++; 172146255Sgad if (*kp == '#' || *kp == '\0') 173146255Sgad goto end_options2; 174146255Sgad } 1751556Srgrimes } else if (c == '+') { 1761556Srgrimes val = 0; 1771556Srgrimes } else { 1781556Srgrimes argptr--; 1791556Srgrimes break; 1801556Srgrimes } 1811556Srgrimes while ((c = *p++) != '\0') { 1821556Srgrimes if (c == 'c' && cmdline) { 1831556Srgrimes char *q; 1841556Srgrimes#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 1851556Srgrimes if (*p == '\0') 1861556Srgrimes#endif 1871556Srgrimes q = *argptr++; 1881556Srgrimes if (q == NULL || minusc != NULL) 1891556Srgrimes error("Bad -c option"); 1901556Srgrimes minusc = q; 1911556Srgrimes#ifdef NOHACK 1921556Srgrimes break; 1931556Srgrimes#endif 1941556Srgrimes } else if (c == 'o') { 1951556Srgrimes minus_o(*argptr, val); 1961556Srgrimes if (*argptr) 1971556Srgrimes argptr++; 1981556Srgrimes } else { 19919240Ssteve if (c == 'p' && !val && privileged) { 20019240Ssteve (void) setuid(getuid()); 20119240Ssteve (void) setgid(getgid()); 20219240Ssteve } 2031556Srgrimes setoption(c, val); 2041556Srgrimes } 2051556Srgrimes } 2061556Srgrimes } 207141962Sgad return; 208141962Sgad 209141962Sgad /* When processing `set', a single "-" means turn off -x and -v */ 210141962Sgadend_options1: 211141962Sgad if (!cmdline) { 212141962Sgad xflag = vflag = 0; 213141962Sgad return; 214141962Sgad } 215141962Sgad 216141962Sgad /* 217141962Sgad * When processing `set', a "--" means the remaining arguments 218141962Sgad * replace the positional parameters in the active shell. If 219141962Sgad * there are no remaining options, then all the positional 220141962Sgad * parameters are cleared (equivalent to doing ``shift $#''). 221141962Sgad */ 222141962Sgadend_options2: 223141962Sgad if (!cmdline) { 224141962Sgad if (*argptr == NULL) 225141962Sgad setparam(argptr); 226141962Sgad return; 227141962Sgad } 228141962Sgad 229141962Sgad /* 230141962Sgad * At this point we are processing options given to 'sh' on a command 231141962Sgad * line. If an end-of-options marker ("-" or "--") is followed by an 232141962Sgad * arg of "#", then skip over all remaining arguments. Some scripting 233141962Sgad * languages (e.g.: perl) document that /bin/sh will implement this 234141962Sgad * behavior, and they recommend that users take advantage of it to 235141962Sgad * solve certain issues that can come up when writing a perl script. 236141962Sgad * Yes, this feature is in /bin/sh to help users write perl scripts. 237141962Sgad */ 238141962Sgad p = *argptr; 239141962Sgad if (p != NULL && p[0] == '#' && p[1] == '\0') { 240141962Sgad while (*argptr != NULL) 241141962Sgad argptr++; 242141962Sgad /* We need to keep the final argument */ 243141962Sgad argptr--; 244141962Sgad } 2451556Srgrimes} 2461556Srgrimes 2471556SrgrimesSTATIC void 24890111Simpminus_o(char *name, int val) 2491556Srgrimes{ 25097276Stjr int doneset, i; 2511556Srgrimes 2521556Srgrimes if (name == NULL) { 25397276Stjr if (val) { 25497276Stjr /* "Pretty" output. */ 25597276Stjr out1str("Current option settings\n"); 25697276Stjr for (i = 0; i < NOPTS; i++) 25797276Stjr out1fmt("%-16s%s\n", optlist[i].name, 25897276Stjr optlist[i].val ? "on" : "off"); 25997276Stjr } else { 26097276Stjr /* Output suitable for re-input to shell. */ 26197276Stjr for (doneset = i = 0; i < NOPTS; i++) 26297276Stjr if (optlist[i].val) { 26397276Stjr if (!doneset) { 26497276Stjr out1str("set"); 26597276Stjr doneset = 1; 26697276Stjr } 26797276Stjr out1fmt(" -o %s", optlist[i].name); 26897276Stjr } 26997276Stjr if (doneset) 27097276Stjr out1c('\n'); 27197276Stjr } 2721556Srgrimes } else { 2731556Srgrimes for (i = 0; i < NOPTS; i++) 2741556Srgrimes if (equal(name, optlist[i].name)) { 27519240Ssteve if (!val && privileged && equal(name, "privileged")) { 27619240Ssteve (void) setuid(getuid()); 27719240Ssteve (void) setgid(getgid()); 27819240Ssteve } 2791556Srgrimes setoption(optlist[i].letter, val); 2801556Srgrimes return; 2811556Srgrimes } 2821556Srgrimes error("Illegal option -o %s", name); 2831556Srgrimes } 2841556Srgrimes} 2851556Srgrimes 2868855Srgrimes 2871556SrgrimesSTATIC void 28890111Simpsetoption(int flag, int val) 28990111Simp{ 2901556Srgrimes int i; 2911556Srgrimes 2921556Srgrimes for (i = 0; i < NOPTS; i++) 2931556Srgrimes if (optlist[i].letter == flag) { 2941556Srgrimes optlist[i].val = val; 2951556Srgrimes if (val) { 2961556Srgrimes /* #%$ hack for ksh semantics */ 2971556Srgrimes if (flag == 'V') 2981556Srgrimes Eflag = 0; 2991556Srgrimes else if (flag == 'E') 3001556Srgrimes Vflag = 0; 3011556Srgrimes } 3021556Srgrimes return; 3031556Srgrimes } 3041556Srgrimes error("Illegal option -%c", flag); 3051556Srgrimes} 3061556Srgrimes 3071556Srgrimes 3081556Srgrimes 3091556Srgrimes#ifdef mkinit 3101556SrgrimesINCLUDE "options.h" 3111556Srgrimes 3121556SrgrimesSHELLPROC { 3131556Srgrimes int i; 3141556Srgrimes 3151556Srgrimes for (i = 0; i < NOPTS; i++) 3161556Srgrimes optlist[i].val = 0; 3171556Srgrimes optschanged(); 3181556Srgrimes 3191556Srgrimes} 3201556Srgrimes#endif 3211556Srgrimes 3221556Srgrimes 3231556Srgrimes/* 3241556Srgrimes * Set the shell parameters. 3251556Srgrimes */ 3261556Srgrimes 3271556Srgrimesvoid 32890111Simpsetparam(char **argv) 32990111Simp{ 3301556Srgrimes char **newparam; 3311556Srgrimes char **ap; 3321556Srgrimes int nparam; 3331556Srgrimes 3341556Srgrimes for (nparam = 0 ; argv[nparam] ; nparam++); 3351556Srgrimes ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 3361556Srgrimes while (*argv) { 3371556Srgrimes *ap++ = savestr(*argv++); 3381556Srgrimes } 3391556Srgrimes *ap = NULL; 3401556Srgrimes freeparam(&shellparam); 3411556Srgrimes shellparam.malloc = 1; 3421556Srgrimes shellparam.nparam = nparam; 3431556Srgrimes shellparam.p = newparam; 3441556Srgrimes shellparam.optnext = NULL; 3451556Srgrimes} 3461556Srgrimes 3471556Srgrimes 3481556Srgrimes/* 3491556Srgrimes * Free the list of positional parameters. 3501556Srgrimes */ 3511556Srgrimes 3521556Srgrimesvoid 35390111Simpfreeparam(struct shparam *param) 35490111Simp{ 3551556Srgrimes char **ap; 3561556Srgrimes 3571556Srgrimes if (param->malloc) { 3581556Srgrimes for (ap = param->p ; *ap ; ap++) 3591556Srgrimes ckfree(*ap); 3601556Srgrimes ckfree(param->p); 3611556Srgrimes } 3621556Srgrimes} 3631556Srgrimes 3641556Srgrimes 3651556Srgrimes 3661556Srgrimes/* 3671556Srgrimes * The shift builtin command. 3681556Srgrimes */ 3691556Srgrimes 37017987Speterint 37190111Simpshiftcmd(int argc, char **argv) 37217987Speter{ 3731556Srgrimes int n; 3741556Srgrimes char **ap1, **ap2; 3751556Srgrimes 3761556Srgrimes n = 1; 3771556Srgrimes if (argc > 1) 3781556Srgrimes n = number(argv[1]); 3791556Srgrimes if (n > shellparam.nparam) 3801556Srgrimes error("can't shift that many"); 3811556Srgrimes INTOFF; 3821556Srgrimes shellparam.nparam -= n; 3831556Srgrimes for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 3841556Srgrimes if (shellparam.malloc) 3851556Srgrimes ckfree(*ap1); 3861556Srgrimes } 3871556Srgrimes ap2 = shellparam.p; 3881556Srgrimes while ((*ap2++ = *ap1++) != NULL); 3891556Srgrimes shellparam.optnext = NULL; 3901556Srgrimes INTON; 3911556Srgrimes return 0; 3921556Srgrimes} 3931556Srgrimes 3941556Srgrimes 3951556Srgrimes 3961556Srgrimes/* 3971556Srgrimes * The set command builtin. 3981556Srgrimes */ 3991556Srgrimes 40017987Speterint 40190111Simpsetcmd(int argc, char **argv) 40217987Speter{ 4031556Srgrimes if (argc == 1) 4041556Srgrimes return showvarscmd(argc, argv); 4051556Srgrimes INTOFF; 4061556Srgrimes options(0); 4071556Srgrimes optschanged(); 4081556Srgrimes if (*argptr != NULL) { 4091556Srgrimes setparam(argptr); 4101556Srgrimes } 4111556Srgrimes INTON; 4121556Srgrimes return 0; 4131556Srgrimes} 4141556Srgrimes 4151556Srgrimes 41620425Sstevevoid 41790111Simpgetoptsreset(const char *value) 41820425Ssteve{ 41920425Ssteve if (number(value) == 1) { 42020425Ssteve shellparam.optnext = NULL; 42120425Ssteve shellparam.reset = 1; 42220425Ssteve } 42320425Ssteve} 42420425Ssteve 4251556Srgrimes/* 4261556Srgrimes * The getopts builtin. Shellparam.optnext points to the next argument 4271556Srgrimes * to be processed. Shellparam.optptr points to the next character to 4281556Srgrimes * be processed in the current argument. If shellparam.optnext is NULL, 4291556Srgrimes * then it's the first time getopts has been called. 4301556Srgrimes */ 4311556Srgrimes 43217987Speterint 43390111Simpgetoptscmd(int argc, char **argv) 43417987Speter{ 43520425Ssteve char **optbase = NULL; 43620425Ssteve 43720425Ssteve if (argc < 3) 43895258Sdes error("usage: getopts optstring var [arg]"); 43920425Ssteve else if (argc == 3) 44020425Ssteve optbase = shellparam.p; 44120425Ssteve else 44220425Ssteve optbase = &argv[3]; 44320425Ssteve 44420425Ssteve if (shellparam.reset == 1) { 44520425Ssteve shellparam.optnext = optbase; 44620425Ssteve shellparam.optptr = NULL; 44720425Ssteve shellparam.reset = 0; 44820425Ssteve } 44920425Ssteve 45020425Ssteve return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 45120425Ssteve &shellparam.optptr); 45220425Ssteve} 45320425Ssteve 45420425SsteveSTATIC int 45590111Simpgetopts(char *optstr, char *optvar, char **optfirst, char ***optnext, 45690111Simp char **optptr) 45720425Ssteve{ 45825227Ssteve char *p, *q; 45920425Ssteve char c = '?'; 46020425Ssteve int done = 0; 46120425Ssteve int ind = 0; 46220425Ssteve int err = 0; 4631556Srgrimes char s[10]; 4641556Srgrimes 46520425Ssteve if ((p = *optptr) == NULL || *p == '\0') { 46620425Ssteve /* Current word is done, advance */ 46720425Ssteve if (*optnext == NULL) 46820425Ssteve return 1; 46920425Ssteve p = **optnext; 4701556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') { 4711556Srgrimesatend: 47220742Ssteve ind = *optnext - optfirst + 1; 47320425Ssteve *optnext = NULL; 47420742Ssteve p = NULL; 47520425Ssteve done = 1; 47620425Ssteve goto out; 4771556Srgrimes } 47820425Ssteve (*optnext)++; 4791556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 4801556Srgrimes goto atend; 4811556Srgrimes } 48220425Ssteve 4831556Srgrimes c = *p++; 48420425Ssteve for (q = optstr; *q != c; ) { 4851556Srgrimes if (*q == '\0') { 48620425Ssteve if (optstr[0] == ':') { 48720425Ssteve s[0] = c; 48820425Ssteve s[1] = '\0'; 48920425Ssteve err |= setvarsafe("OPTARG", s, 0); 49020425Ssteve } 49120425Ssteve else { 49220425Ssteve out1fmt("Illegal option -%c\n", c); 49320425Ssteve (void) unsetvar("OPTARG"); 49420425Ssteve } 4951556Srgrimes c = '?'; 49620425Ssteve goto bad; 4971556Srgrimes } 4981556Srgrimes if (*++q == ':') 4991556Srgrimes q++; 5001556Srgrimes } 50120425Ssteve 5021556Srgrimes if (*++q == ':') { 50320425Ssteve if (*p == '\0' && (p = **optnext) == NULL) { 50420425Ssteve if (optstr[0] == ':') { 50520425Ssteve s[0] = c; 50620425Ssteve s[1] = '\0'; 50720425Ssteve err |= setvarsafe("OPTARG", s, 0); 50820425Ssteve c = ':'; 50920425Ssteve } 51020425Ssteve else { 51120425Ssteve out1fmt("No arg for -%c option\n", c); 51220425Ssteve (void) unsetvar("OPTARG"); 51320425Ssteve c = '?'; 51420425Ssteve } 51520425Ssteve goto bad; 5161556Srgrimes } 51720425Ssteve 51820425Ssteve if (p == **optnext) 51920425Ssteve (*optnext)++; 52020425Ssteve setvarsafe("OPTARG", p, 0); 5211556Srgrimes p = NULL; 5221556Srgrimes } 52320425Ssteve else 52420425Ssteve setvarsafe("OPTARG", "", 0); 52520425Ssteve ind = *optnext - optfirst + 1; 52620425Ssteve goto out; 52720425Ssteve 52820425Sstevebad: 52920425Ssteve ind = 1; 53020425Ssteve *optnext = NULL; 53120425Ssteve p = NULL; 5321556Srgrimesout: 53320425Ssteve *optptr = p; 53420425Ssteve fmtstr(s, sizeof(s), "%d", ind); 53520425Ssteve err |= setvarsafe("OPTIND", s, VNOFUNC); 5361556Srgrimes s[0] = c; 5371556Srgrimes s[1] = '\0'; 53820425Ssteve err |= setvarsafe(optvar, s, 0); 53920425Ssteve if (err) { 54020425Ssteve *optnext = NULL; 54120425Ssteve *optptr = NULL; 54220425Ssteve flushall(); 54320425Ssteve exraise(EXERROR); 54420425Ssteve } 54520425Ssteve return done; 5461556Srgrimes} 5471556Srgrimes 5481556Srgrimes/* 5491556Srgrimes * XXX - should get rid of. have all builtins use getopt(3). the 5501556Srgrimes * library getopt must have the BSD extension static variable "optreset" 5511556Srgrimes * otherwise it can't be used within the shell safely. 5521556Srgrimes * 5531556Srgrimes * Standard option processing (a la getopt) for builtin routines. The 5541556Srgrimes * only argument that is passed to nextopt is the option string; the 5551556Srgrimes * other arguments are unnecessary. It return the character, or '\0' on 5561556Srgrimes * end of input. 5571556Srgrimes */ 5581556Srgrimes 5591556Srgrimesint 56090111Simpnextopt(char *optstring) 56190111Simp{ 56225227Ssteve char *p, *q; 5631556Srgrimes char c; 5641556Srgrimes 5651556Srgrimes if ((p = optptr) == NULL || *p == '\0') { 5661556Srgrimes p = *argptr; 5671556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') 5681556Srgrimes return '\0'; 5691556Srgrimes argptr++; 5701556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 5711556Srgrimes return '\0'; 5721556Srgrimes } 5731556Srgrimes c = *p++; 5741556Srgrimes for (q = optstring ; *q != c ; ) { 5751556Srgrimes if (*q == '\0') 5761556Srgrimes error("Illegal option -%c", c); 5771556Srgrimes if (*++q == ':') 5781556Srgrimes q++; 5791556Srgrimes } 5801556Srgrimes if (*++q == ':') { 5811556Srgrimes if (*p == '\0' && (p = *argptr++) == NULL) 5821556Srgrimes error("No arg for -%c option", c); 58359436Scracauer shoptarg = p; 5841556Srgrimes p = NULL; 5851556Srgrimes } 5861556Srgrimes optptr = p; 5871556Srgrimes return c; 5881556Srgrimes} 589