args.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 362 行
C
362 行
#ifndef lintstatic CHTYPE *sccsid = "@(#)args.c 4.1 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1986 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * * Modification History: * * 002 - Gary A. Gaudet, Wed Nov 9 10:24:49 EST 1988 * MIPS portability and bug fixes * * 001 - Gary Gaudet for Andy Gadsby 09-mar-88 * i18n version of csh *//* * UNIX shell * * Bell Telephone Laboratories * */#include "defs.h"static struct dolnod *copyargs();static void freedolh(); /* DAG -- bug fix (no value returned) */extern struct dolnod *freeargs();static struct dolnod *dolh;CHTYPE flagadr[14];CHTYPE flagchar[] ={ 'x', 'n', 'v', 't', STDFLG, 'i', 'e', 'r', 'k', 'u', 'h', 'f', 'a', 0};long flagval[] ={ execpr, noexec, readpr, oneflg, stdflg, intflg, errflg, rshflg, keyflg, setflg, hashflg, nofngflg, exportflg, 0};/* ======== option handling ======== */options(argc,argv,conv) CHTYPE **argv; int argc; int conv; /* if TRUE then argv is really a char ** */{ register CHTYPE *cp; register CHTYPE **argp = argv; register CHTYPE *flagc; CHTYPE *flagp; /* * convert all of argument list to wide chars */ if (conv) { int c; char **acp = (char **)argv; for (c = 0; c < argc; c++) acp[c] = (char *)make(ctow(acp[c])); } if (argc > 1 && *argp[1] == '-') { /* * if first argument is "--" then options are not * to be changed. Fix for problems getting * $1 starting with a "-" */ cp = argp[1]; if (cp[1] == '-') { argp[1] = argp[0]; argc--; return(argc); } if (cp[1] == '\0') flags &= ~(execpr|readpr); /* * Step along 'flagchar[]' looking for matches. * 'sicr' are not legal with 'set' command. */ while (*++cp) { flagc = flagchar; while (*flagc && *flagc != *cp) flagc++; if (*cp == *flagc) { if (eq(argv[0], "set") && any(*cp, "sicr")) failed(argv[1], badopt); else { flags |= flagval[flagc-flagchar]; if (flags & errflg) eflag = errflg; } } else if (*cp == 'c' && argc > 2 && comdiv == 0) { comdiv = argp[2]; argp[1] = argp[0]; argp++; argc--; } else failed(argv[1],badopt); } argp[1] = argp[0]; argc--; } else if (argc > 1 && *argp[1] == '+') /* unset flags x, k, t, n, v, e, u */ { cp = argp[1]; while (*++cp) { flagc = flagchar; while (*flagc && *flagc != *cp) flagc++; /* * step through flags */ if (!any(*cp, "sicr") && *cp == *flagc) { /* * only turn off if already on */ if ((flags & flagval[flagc-flagchar])) { flags &= ~(flagval[flagc-flagchar]); if (*cp == 'e') eflag = 0; } } } argp[1] = argp[0]; argc--; } /* * set up $- */ flagp = flagadr; if (flags) { flagc = flagchar; while (*flagc) { if (flags & flagval[flagc-flagchar]) *flagp++ = *flagc; flagc++; } } *flagp = 0; return(argc);}/* * sets up positional parameters */setargs(argi) CHTYPE *argi[];{ register CHTYPE **argp = argi; /* count args */ register int argn = 0; while (Rcheat(*argp++) != ENDARGS) argn++; /* * free old ones unless on for loop chain */ freedolh(); dolh = copyargs(argi, argn); dolc = argn - 1;}static void /* DAG -- bug fix (no value returned) */freedolh(){ register CHTYPE **argp; register struct dolnod *argblk; if (argblk = dolh) { if ((--argblk->doluse) == 0) { for (argp = argblk->dolarg; Rcheat(*argp) != ENDARGS; argp++) free(*argp); free(argblk); } }}struct dolnod *freeargs(blk) struct dolnod *blk;{ register CHTYPE **argp; register struct dolnod *argr = 0; register struct dolnod *argblk; int cnt; if (argblk = blk) { argr = argblk->dolnxt; cnt = --argblk->doluse; if (argblk == dolh) { if (cnt == 1) return(argr); else return(argblk); } else { if (cnt == 0) { for (argp = argblk->dolarg; Rcheat(*argp) != ENDARGS; argp++) free(*argp); free(argblk); } } } return(argr);}static struct dolnod *copyargs(from, n) CHTYPE *from[];{ register struct dolnod *np = (struct dolnod *)alloc(sizeof(char**) * n + 3 * BYTESPERWORD); register CHTYPE **fp = from; register CHTYPE **pp; np->doluse = 1; /* use count */ pp = np->dolarg; dolv = pp; while (n--) *pp++ = make(*fp++); *pp++ = ENDARGS; return(np);}struct dolnod *clean_args(blk) struct dolnod *blk;{ register CHTYPE **argp; register struct dolnod *argr = 0; register struct dolnod *argblk; if (argblk = blk) { argr = argblk->dolnxt; if (argblk == dolh) argblk->doluse = 1; else { for (argp = argblk->dolarg; Rcheat(*argp) != ENDARGS; argp++) free(*argp); free(argblk); } } return(argr);}clearup(){ /* * force `for' $* lists to go away */ while (argfor = clean_args(argfor)) ; /* * clean up io files */ while (pop()) ; /* * clean up tmp files */ while (poptemp()) ;}struct dolnod *useargs(){ if (dolh) { if (dolh->doluse++ == 1) { dolh->dolnxt = argfor; argfor = dolh; } } return(dolh);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?