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