1 #ifndef lint 2 static char sccsid[] = "@(#)io.c 4.2 08/11/83"; 3 #endif 4 5 # 6 /* 7 * UNIX shell 8 * 9 * S. R. Bourne 10 * Bell Telephone Laboratories 11 * 12 */ 13 14 #include "defs.h" 15 #include "dup.h" 16 17 18 /* ======== input output and file copying ======== */ 19 20 initf(fd) 21 UFD fd; 22 { 23 REG FILE f=standin; 24 25 f->fdes=fd; f->fsiz=((flags&(oneflg|ttyflg))==0 ? BUFSIZ : 1); 26 f->fnxt=f->fend=f->fbuf; f->feval=0; f->flin=1; 27 f->feof=FALSE; 28 } 29 30 estabf(s) 31 REG STRING s; 32 { 33 REG FILE f; 34 35 (f=standin)->fdes = -1; 36 f->fend=length(s)+(f->fnxt=s); 37 f->flin=1; 38 return(f->feof=(s==0)); 39 } 40 41 push(af) 42 FILE af; 43 { 44 REG FILE f; 45 46 (f=af)->fstak=standin; 47 f->feof=0; f->feval=0; 48 standin=f; 49 } 50 51 pop() 52 { 53 REG FILE f; 54 55 IF (f=standin)->fstak 56 THEN IF f->fdes>=0 THEN close(f->fdes) FI 57 standin=f->fstak; 58 return(TRUE); 59 ELSE return(FALSE); 60 FI 61 } 62 63 chkpipe(pv) 64 INT *pv; 65 { 66 IF pipe(pv)<0 ORF pv[INPIPE]<0 ORF pv[OTPIPE]<0 67 THEN error(piperr); 68 FI 69 } 70 71 chkopen(idf) 72 STRING idf; 73 { 74 REG INT rc; 75 76 IF (rc=open(idf,0))<0 77 THEN failed(idf,badopen); 78 ELSE return(rc); 79 FI 80 } 81 82 rename(f1,f2) 83 REG INT f1, f2; 84 { 85 IF f1!=f2 86 THEN dup(f1|DUPFLG, f2); 87 close(f1); 88 IF f2==0 THEN ioset|=1 FI 89 FI 90 } 91 92 create(s) 93 STRING s; 94 { 95 REG INT rc; 96 97 IF (rc=creat(s,0666))<0 98 THEN failed(s,badcreate); 99 ELSE return(rc); 100 FI 101 } 102 103 tmpfil() 104 { 105 itos(serial++); movstr(numbuf,tmpnam); 106 return(create(tmpout)); 107 } 108 109 /* set by trim */ 110 BOOL nosubst; 111 112 copy(ioparg) 113 IOPTR ioparg; 114 { 115 CHAR c, *ends; 116 REG CHAR *cline, *clinep; 117 INT fd; 118 REG IOPTR iop; 119 120 IF iop=ioparg 121 THEN copy(iop->iolst); 122 ends=mactrim(iop->ioname); IF nosubst THEN iop->iofile &= ~IODOC FI 123 fd=tmpfil(); 124 iop->ioname=cpystak(tmpout); 125 iop->iolst=iotemp; iotemp=iop; 126 cline=locstak(); 127 128 LOOP clinep=cline; chkpr(NL); 129 WHILE (c = (nosubst ? readc() : nextc(*ends)), !eolchar(c)) DO *clinep++ = c OD 130 *clinep=0; 131 IF eof ORF eq(cline,ends) THEN break FI 132 *clinep++=NL; 133 write(fd,cline,clinep-cline); 134 POOL 135 close(fd); 136 FI 137 } 138