8 /* -------- io.c -------- */
15 static struct iobuf sharedbuf
= {AFID_NOBUF
};
16 static struct iobuf mainbuf
= {AFID_NOBUF
};
17 static unsigned bufid
= AFID_ID
; /* buffer id counter */
19 struct ioarg temparg
= {0, 0, 0, AFID_NOBUF
, 0};
21 _PROTOTYPE(static void readhere
, (char **name
, char *s
, int ec
));
22 _PROTOTYPE(void pushio
, (struct ioarg
*argp
, int (*fn
)()));
23 _PROTOTYPE(static int xxchar
, (struct ioarg
*ap
));
24 _PROTOTYPE(void tempname
, (char *tname
));
32 if(e
.linep
> elinep
) {
33 while((c
=readc()) != '\n' && c
)
35 err("input line too long");
40 if (ec
!= '\'' && e
.iop
->task
!= XGRAVE
) {
43 if (c
== '\n' && ec
!= '\"')
55 if (e
.iop
>= e
.iobase
)
63 return e
.iop
< e
.iobase
|| (e
.iop
->peekc
== 0 && e
.iop
->prev
== 0);
71 for (; e
.iop
>= e
.iobase
; e
.iop
--)
72 if ((c
= e
.iop
->peekc
) != '\0') {
77 if (e
.iop
->prev
!= 0) {
78 if ((c
= (*e
.iop
->iofn
)(e
.iop
->argp
, e
.iop
)) != '\0') {
85 return(e
.iop
->prev
= c
);
87 else if (e
.iop
->task
== XIO
&& e
.iop
->prev
!= '\n') {
94 if (e
.iop
->task
== XIO
) {
96 return e
.iop
->prev
= 0;
97 if (talking
&& e
.iop
== iostack
+1)
101 if (e
.iop
>= iostack
)
112 write(2, &c
, sizeof c
);
120 if (++e
.iop
>= &iostack
[NPUSH
]) {
122 err("Shell input nested too deeply");
128 if (argp
->afid
!= AFID_NOBUF
)
131 e
.iop
->argp
= ioargstack
+ (e
.iop
- iostack
);
132 *e
.iop
->argp
= *argp
;
133 e
.iop
->argp
->afbuf
= e
.iop
== &iostack
[0] ? &mainbuf
: &sharedbuf
;
134 if (isatty(e
.iop
->argp
->afile
) == 0 &&
135 (e
.iop
== &iostack
[0] ||
136 lseek(e
.iop
->argp
->afile
, 0L, 1) != -1)) {
137 if (++bufid
== AFID_NOBUF
)
139 e
.iop
->argp
->afid
= bufid
;
147 if (fn
== filechar
|| fn
== linechar
)
149 else if (fn
== gravechar
|| fn
== qgravechar
)
150 e
.iop
->task
= XGRAVE
;
152 e
.iop
->task
= XOTHER
;
159 register struct io
*xp
;
167 * Input generating functions
171 * Produce the characters of a string, then a newline, then EOF.
175 register struct ioarg
*ap
;
179 if (ap
->aword
== NULL
)
181 if ((c
= *ap
->aword
++) == 0) {
189 * Given a list of words, produce the characters
190 * in them, with a space after each word.
194 register struct ioarg
*ap
;
199 if ((wl
= ap
->awordlist
) == NULL
)
202 if ((c
= *(*wl
)++) != 0)
207 ap
->awordlist
= NULL
;
212 * Return the characters of a list of words,
213 * producing a space between them.
217 register struct ioarg
*ap
;
221 if ((wp
= *ap
->awordlist
++) != NULL
) {
222 PUSHIO(aword
, wp
, *ap
->awordlist
== NULL
? strchar
: xxchar
);
230 register struct ioarg
*ap
;
234 if (ap
->aword
== NULL
)
236 if ((c
= *ap
->aword
++) == '\0') {
244 * Produce the characters from a single word (string).
248 register struct ioarg
*ap
;
252 if (ap
->aword
== NULL
|| (c
= *ap
->aword
++) == 0)
258 * Produce quoted characters from a single word (string).
262 register struct ioarg
*ap
;
266 if (ap
->aword
== NULL
|| (c
= *ap
->aword
++) == 0)
272 * Return the characters from a file.
276 register struct ioarg
*ap
;
280 struct iobuf
*bp
= ap
->afbuf
;
282 if (ap
->afid
!= AFID_NOBUF
) {
283 if ((i
= ap
->afid
!= bp
->id
) || bp
->bufp
== bp
->ebufp
) {
285 lseek(ap
->afile
, ap
->afpos
, 0);
287 i
= read(ap
->afile
, bp
->buf
, sizeof(bp
->buf
));
288 } while (i
< 0 && errno
== EINTR
);
294 bp
->ebufp
= (bp
->bufp
= bp
->buf
) + i
;
297 return *bp
->bufp
++ & 0177;
301 i
= read(ap
->afile
, &c
, sizeof(c
));
302 } while (i
< 0 && errno
== EINTR
);
303 return(i
== sizeof(c
)? c
&0177: (closef(ap
->afile
), 0));
307 * Return the characters from a here temp file.
311 register struct ioarg
*ap
;
316 if (read(ap
->afile
, &c
, sizeof(c
)) != sizeof(c
)) {
325 * Return the characters produced by a process (`...`).
326 * Quote them if required, and remove any trailing newline characters.
335 if ((c
= qgravechar(ap
, iop
)&~QUOTE
) == '\n')
342 register struct ioarg
*ap
;
354 } else if ((c
= filechar(ap
)) == '\n') {
356 while ((c
= filechar(ap
)) == '\n')
364 return(c
!=0? c
|QUOTE
: 0);
368 * Return a single command (usually the first line) from a file.
372 register struct ioarg
*ap
;
376 if ((c
= filechar(ap
)) == '\n') {
379 ap
->afile
= -1; /* illegal value */
390 write(2, s
, strlen(s
));
397 write(2, &c
, sizeof c
);
420 for (u
=NUFILE
; u
<NOFILE
;)
425 * remap fd into Shell's fd space
435 for (i
=0; i
<NOFILE
; i
++)
440 } while (fd
>= 0 && fd
< e
.iofd
);
441 for (i
=0; i
<NOFILE
; i
++)
445 err("too many files open in shell");
456 if ((i
= pipe(pv
)) < 0)
457 err("can't create pipe - try again");
471 /* -------- here.c -------- */
472 /* #include "sh.h" */
481 struct ioword
*h_iop
;
485 static struct here
*inhere
; /* list of hear docs while parsing */
486 static struct here
*acthere
; /* list of active here documents */
491 inhere
=acthere
=(struct here
*)0;
499 register struct here
*h
, *lh
;
501 h
= (struct here
*) space(sizeof(struct here
));
504 h
->h_tag
= evalstr(s
, DOSUB
);
513 for (lh
= inhere
; lh
!=NULL
; lh
= lh
->h_next
)
514 if (lh
->h_next
== 0) {
518 iop
->io_flag
|= IOHERE
|IOXHERE
;
519 for (s
= h
->h_tag
; *s
; s
++)
521 iop
->io_flag
&= ~ IOXHERE
;
524 h
->h_dosub
= iop
->io_flag
& IOXHERE
;
530 register struct here
*h
, *hp
;
532 /* Scan here files first leaving inhere list in place */
533 for (hp
= h
= inhere
; h
!= NULL
; hp
= h
, h
= h
->h_next
)
534 readhere(&h
->h_iop
->io_name
, h
->h_tag
, h
->h_dosub
? 0: '\'');
536 /* Make inhere list active - keep list intact for scraphere */
538 hp
->h_next
= acthere
;
545 readhere(name
, s
, ec
)
554 char line
[LINELIM
+1];
558 *name
= strsave(tname
, areanum
);
559 tf
= creat(tname
, 0600);
562 if (newenv(setjmp(errpt
= ev
)) != 0)
565 pushio(e
.iop
->argp
, e
.iop
->iofn
);
568 if (talking
&& e
.iop
<= iostack
)
571 while ((c
= readc()) != '\n' && c
) {
572 if (next
>= &line
[LINELIM
]) {
579 if (strcmp(s
, line
) == 0 || c
== 0)
582 write (tf
, line
, (int)(next
-line
));
585 prs("here document `"); prs(s
); err("' unclosed");
593 * open here temp file.
594 * if unquoted here, expand here temp file into second temp file.
614 if ((tf
= creat(tname
, 0600)) < 0)
616 if (newenv(setjmp(errpt
= ev
)) == 0) {
617 PUSHIO(afile
, hf
, herechar
);
619 while ((c
= subgetc(0, 0)) != 0) {
622 if (c
"E
&& !any(c1
,"`$\\"))
640 register struct here
*h
;
642 for (h
= inhere
; h
!= NULL
; h
= h
->h_next
) {
643 if (h
->h_iop
&& h
->h_iop
->io_name
)
644 unlink(h
->h_iop
->io_name
);
649 /* unlink here temp files before a freearea(area) */
654 register struct here
*h
, *hl
;
657 for (h
= acthere
; h
!= NULL
; h
= h
->h_next
)
658 if (getarea((char *) h
) >= area
) {
659 if (h
->h_iop
->io_name
!= NULL
)
660 unlink(h
->h_iop
->io_name
);
664 hl
->h_next
= h
->h_next
;
674 register char *cp
, *lp
;
676 for (cp
= tname
, lp
= "/tmp/shtm"; (*cp
= *lp
++) != '\0'; cp
++)
678 lp
= putn(getpid()*1000 + inc
++);
679 for (; (*cp
= *lp
++) != '\0'; cp
++)