|
|
1.1 root 1: /* @(#)io.c 1.5 */
2: /*
3: * UNIX shell
4: *
5: * Bell Telephone Laboratories
6: *
7: */
8:
9: #include "defs.h"
10: #include <fcntl.h>
11:
12: short topfd;
13:
14: /* ======== input output and file copying ======== */
15:
16: initf(fd)
17: int fd;
18: {
19: register struct fileblk *f = standin;
20:
21: f->fdes = fd;
22: f->fsiz = ((flags & oneflg) == 0 ? BUFSIZ : 1);
23: f->fnxt = f->fend = f->fbuf;
24: f->feval = 0;
25: f->flin = 1;
26: f->feof = FALSE;
27: }
28:
29: estabf(s)
30: register char *s;
31: {
32: register struct fileblk *f;
33:
34: (f = standin)->fdes = -1;
35: f->fend = length(s) + (f->fnxt = s);
36: f->flin = 1;
37: return(f->feof = (s == 0));
38: }
39:
40: push(af)
41: struct fileblk *af;
42: {
43: register struct fileblk *f;
44:
45: (f = af)->fstak = standin;
46: f->feof = 0;
47: f->feval = 0;
48: standin = f;
49: }
50:
51: pop()
52: {
53: register struct fileblk *f;
54:
55: if ((f = standin)->fstak)
56: {
57: if (f->fdes >= 0)
58: close(f->fdes);
59: standin = f->fstak;
60: return(TRUE);
61: }
62: else
63: return(FALSE);
64: }
65:
66: struct tempblk *tmpfptr;
67:
68: pushtemp(fd,tb)
69: int fd;
70: struct tempblk *tb;
71: {
72: tb->fdes = fd;
73: tb->fstak = tmpfptr;
74: tmpfptr = tb;
75: }
76:
77: poptemp()
78: {
79: if (tmpfptr)
80: {
81: close(tmpfptr->fdes);
82: tmpfptr = tmpfptr->fstak;
83: return(TRUE);
84: }
85: else
86: return(FALSE);
87: }
88:
89: chkpipe(pv)
90: int *pv;
91: {
92: if (pipe(pv) < 0 || pv[INPIPE] < 0 || pv[OTPIPE] < 0)
93: error(piperr);
94: }
95:
96: chkopen(idf)
97: char *idf;
98: {
99: register int rc;
100:
101: if ((rc = open(idf, 0)) < 0)
102: failed(idf, NIL, 1);
103: else
104: return(rc);
105: }
106:
107: rename(f1, f2)
108: register int f1, f2;
109: {
110: #ifndef SYSV
111: if (f1 != f2)
112: {
113: dup2(f1, f2);
114: close(f1);
115: if (f2 == 0)
116: ioset |= 1;
117: }
118: #else
119: int fs;
120:
121: if (f1 != f2)
122: {
123: fs = fcntl(f2, 1, 0);
124: close(f2);
125: fcntl(f1, 0, f2);
126: close(f1);
127: if (fs == 1)
128: fcntl(f2, 2, 1);
129: if (f2 == 0)
130: ioset |= 1;
131: }
132: #endif
133: }
134:
135: create(s)
136: char *s;
137: {
138: register int rc;
139:
140: if ((rc = creat(s, 0666)) < 0)
141: failed(s, NIL, 1);
142: else
143: return(rc);
144: }
145:
146: tmpfil(tb)
147: struct tempblk *tb;
148: {
149: int fd;
150:
151: itos(serial++);
152: movstr(numbuf, tmpnam);
153: fd = create(tmpout);
154: pushtemp(fd,tb);
155: return(fd);
156: }
157:
158: /*
159: * set by trim
160: */
161: extern BOOL nosubst;
162: #define CPYSIZ 512
163:
164: copy(ioparg)
165: struct ionod *ioparg;
166: {
167: register char *cline;
168: register char *clinep;
169: register struct ionod *iop;
170: char c;
171: char *ends;
172: char *start;
173: int fd;
174: int i;
175:
176:
177: if (iop = ioparg)
178: {
179: struct tempblk tb;
180:
181: copy(iop->iolst);
182: ends = mactrim(iop->ioname);
183: if (nosubst)
184: iop->iofile &= ~IODOC;
185: fd = tmpfil(&tb);
186:
187: if (fndef)
188: iop->ioname = make(tmpout);
189: else
190: iop->ioname = cpystak(tmpout);
191:
192: iop->iolst = iotemp;
193: iotemp = iop;
194:
195: cline = clinep = start = locstak();
196: for (;;)
197: {
198: chkpr();
199: if (nosubst)
200: {
201: c = readc();
202:
203: while (!eolchar(c))
204: {
205: *clinep++ = c;
206: c = readc();
207: }
208: }
209: else
210: {
211: c = nextc(*ends);
212:
213: while (!eolchar(c))
214: {
215: *clinep++ = c;
216: c = nextc(*ends);
217: }
218: }
219:
220: *clinep = 0;
221: if (eof || eq(cline, ends))
222: {
223: if ((i = cline - start) > 0)
224: write(fd, start, i);
225: break;
226: }
227: else
228: *clinep++ = NL;
229:
230: if ((i = clinep - start) < CPYSIZ)
231: cline = clinep;
232: else
233: {
234: write(fd, start, i);
235: cline = clinep = start;
236: }
237: }
238:
239: poptemp(); /* pushed in tmpfil -- bug fix for problem
240: deleting in-line scripts */
241: }
242: }
243:
244:
245: link_iodocs(i)
246: struct ionod *i;
247: {
248: while(i)
249: {
250: shfree(i->iolink);
251:
252: itos(serial++);
253: movstr(numbuf, tmpnam);
254: i->iolink = make(tmpout);
255: link(i->ioname, i->iolink);
256:
257: i = i->iolst;
258: }
259: }
260:
261:
262: swap_iodoc_nm(i)
263: struct ionod *i;
264: {
265: while(i)
266: {
267: shfree(i->ioname);
268: i->ioname = i->iolink;
269: i->iolink = 0;
270:
271: i = i->iolst;
272: }
273: }
274:
275:
276: savefd(fd)
277: int fd;
278: {
279: register int f;
280:
281: #ifdef SYSV
282: f = fcntl(fd, F_DUPFD, 10);
283: #else
284: f = dup2(fd, 10);
285: #endif
286: return(f);
287: }
288:
289:
290: restore(last)
291: register int last;
292: {
293: register int i;
294: register int dupfd;
295:
296: for (i = topfd - 1; i >= last; i--)
297: {
298: if ((dupfd = fdmap[i].dup_fd) > 0)
299: rename(dupfd, fdmap[i].org_fd);
300: else
301: close(fdmap[i].org_fd);
302: }
303: topfd = last;
304: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.