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