1: # 2: /* 3: * UNIX shell 4: * 5: * S. R. Bourne 6: * Bell Telephone Laboratories 7: * 8: */ 9: 10: #include "defs.h" 11: 12: PROC STRING *copyargs(); 13: LOCAL DOLPTR dolh; 14: 15: CHAR flagadr[10]; 16: 17: CHAR flagchar[] = { 18: 'x', 'n', 'v', 't', 's', 'i', 'e', 'r', 'k', 'u', 0 19: }; 20: INT flagval[] = { 21: execpr, noexec, readpr, oneflg, stdflg, intflg, errflg, rshflg, keyflg, setflg, 0 22: }; 23: 24: /* ======== option handling ======== */ 25: 26: 27: INT options(argc,argv) 28: STRING *argv; 29: INT argc; 30: { 31: REG STRING cp; 32: REG STRING *argp=argv; 33: REG STRING flagc; 34: STRING flagp; 35: 36: IF argc>1 ANDF *argp[1]=='-' 37: THEN cp=argp[1]; 38: flags &= ~(execpr|readpr); 39: WHILE *++cp 40: DO flagc=flagchar; 41: 42: WHILE *flagc ANDF *flagc != *cp DO flagc++ OD 43: IF *cp == *flagc 44: THEN flags |= flagval[flagc-flagchar]; 45: ELIF *cp=='c' ANDF argc>2 ANDF comdiv==0 46: THEN comdiv=argp[2]; 47: argp[1]=argp[0]; argp++; argc--; 48: ELSE failed(argv[1],badopt); 49: FI 50: OD 51: argp[1]=argp[0]; argc--; 52: FI 53: 54: /* set up $- */ 55: flagc=flagchar; 56: flagp=flagadr; 57: WHILE *flagc 58: DO IF flags&flagval[flagc-flagchar] 59: THEN *flagp++ = *flagc; 60: FI 61: flagc++; 62: OD 63: *flagp++=0; 64: 65: return(argc); 66: } 67: 68: VOID setargs(argi) 69: STRING argi[]; 70: { 71: /* count args */ 72: REG STRING *argp=argi; 73: REG INT argn=0; 74: 75: WHILE Rcheat(*argp++)!=ENDARGS DO argn++ OD 76: 77: /* free old ones unless on for loop chain */ 78: freeargs(dolh); 79: dolh=copyargs(argi,argn); /* sets dolv */ 80: assnum(&dolladr,dolc=argn-1); 81: } 82: 83: freeargs(blk) 84: DOLPTR blk; 85: { 86: REG STRING *argp; 87: REG DOLPTR argr=0; 88: REG DOLPTR argblk; 89: 90: IF argblk=blk 91: THEN argr = argblk->dolnxt; 92: IF (--argblk->doluse)==0 93: THEN FOR argp=argblk->dolarg; Rcheat(*argp)!=ENDARGS; argp++ 94: DO free(*argp) OD 95: free(argblk); 96: FI 97: FI 98: return(argr); 99: } 100: 101: LOCAL STRING * copyargs(from, n) 102: STRING from[]; 103: { 104: REG STRING * np=alloc(sizeof(STRING*)*n+3*BYTESPERWORD); 105: REG STRING * fp=from; 106: REG STRING * pp=np; 107: 108: np->doluse=1; /* use count */ 109: np=np->dolarg; 110: dolv=np; 111: 112: WHILE n-- 113: DO *np++ = make(*fp++) OD 114: *np++ = ENDARGS; 115: return(pp); 116: } 117: 118: clearup() 119: { 120: /* force `for' $* lists to go away */ 121: WHILE argfor=freeargs(argfor) DONE 122: 123: /* clean up io files */ 124: WHILE pop() DONE 125: } 126: 127: DOLPTR useargs() 128: { 129: IF dolh 130: THEN dolh->doluse++; 131: dolh->dolnxt=argfor; 132: return(argfor=dolh); 133: ELSE return(0); 134: FI 135: }