xref: /original-bsd/old/sh/word.c (revision 4ec13c9b)
1 /*	word.c	4.2	83/06/10	*/
2 
3 #
4 /*
5  * UNIX shell
6  *
7  * S. R. Bourne
8  * Bell Telephone Laboratories
9  *
10  */
11 
12 #include	"defs.h"
13 #include	"sym.h"
14 
15 
16 /* ========	character handling for command lines	========*/
17 
18 
19 word()
20 {
21 	REG CHAR	c, d;
22 	REG CHAR	*argp=locstak()+BYTESPERWORD;
23 	INT		alpha=1;
24 
25 	wdnum=0; wdset=0;
26 
27 	WHILE (c=nextc(0), space(c)) DONE
28 
29 	IF c=='#'
30 	THEN	WHILE (c=readc()) ANDF c!=NL DONE
31 	FI
32 
33 	IF !eofmeta(c)
34 	THEN	REP	IF c==LITERAL
35 			THEN	*argp++=(DQUOTE);
36 				WHILE (c=readc()) ANDF c!=LITERAL
37 				DO *argp++=(c|QUOTE); chkpr(c) OD
38 				*argp++=(DQUOTE);
39 
40 			ELSE	*argp++=(c);
41 				IF c=='=' THEN wdset |= alpha FI
42 				IF !alphanum(c) THEN alpha=0 FI
43 				IF qotchar(c)
44 				THEN	d=c;
45 					WHILE (*argp++=(c=nextc(d))) ANDF c!=d
46 					DO chkpr(c) OD
47 				FI
48 			FI
49 		PER (c=nextc(0), !eofmeta(c)) DONE
50 		argp=endstak(argp);
51 		IF !letter(argp->argval[0]) THEN wdset=0 FI
52 
53 		peekc=c|MARK;
54 		IF argp->argval[1]==0 ANDF (d=argp->argval[0], digit(d)) ANDF (c=='>' ORF c=='<')
55 		THEN	word(); wdnum=d-'0';
56 		ELSE	/*check for reserved words*/
57 			IF reserv==FALSE ORF (wdval=syslook(argp->argval,reserved))==0
58 			THEN	wdarg=argp; wdval=0;
59 			FI
60 		FI
61 
62 	ELIF dipchar(c)
63 	THEN	IF (d=nextc(0))==c
64 		THEN	wdval = c|SYMREP;
65 		ELSE	peekc = d|MARK; wdval = c;
66 		FI
67 	ELSE	IF (wdval=c)==EOF
68 		THEN	wdval=EOFSYM;
69 		FI
70 		IF iopend ANDF eolchar(c)
71 		THEN	copy(iopend); iopend=0;
72 		FI
73 	FI
74 	reserv=FALSE;
75 	return(wdval);
76 }
77 
78 nextc(quote)
79 	CHAR		quote;
80 {
81 	REG CHAR	c, d;
82 	IF (d=readc())==ESCAPE
83 	THEN	IF (c=readc())==NL
84 		THEN	chkpr(NL); d=nextc(quote);
85 		ELIF quote ANDF c!=quote ANDF !escchar(c)
86 		THEN	peekc=c|MARK;
87 		ELSE	d = c|QUOTE;
88 		FI
89 	FI
90 	return(d);
91 }
92 
93 readc()
94 {
95 	REG CHAR	c;
96 	REG INT		len;
97 	REG FILE	f;
98 
99 retry:
100 	IF peekc
101 	THEN	c=peekc; peekc=0;
102 	ELIF (f=standin, f->fnxt!=f->fend)
103 	THEN	IF (c = *f->fnxt++)==0
104 		THEN	IF f->feval
105 			THEN	IF estabf(*f->feval++)
106 				THEN	c=EOF;
107 				ELSE	c=SP;
108 				FI
109 			ELSE	goto retry; /* = c=readc(); */
110 			FI
111 		FI
112 		IF flags&readpr ANDF standin->fstak==0 THEN prc(c) FI
113 		IF c==NL THEN f->flin++ FI
114 	ELIF f->feof ORF f->fdes<0
115 	THEN	c=EOF; f->feof++;
116 	ELIF (len=readb())<=0
117 	THEN	close(f->fdes); f->fdes = -1; c=EOF; f->feof++;
118 	ELSE	f->fend = (f->fnxt = f->fbuf)+len;
119 		goto retry;
120 	FI
121 	return(c);
122 }
123 
124 LOCAL	readb()
125 {
126 	REG FILE	f=standin;
127 	REG INT		len;
128 
129 	IF setjmp(INTbuf) == 0 THEN trapjmp[INTR] = 1; FI
130 	REP	IF trapnote&SIGSET THEN newline(); sigchk() FI
131 	PER (len=read(f->fdes,f->fbuf,f->fsiz))<0 ANDF trapnote DONE
132 	trapjmp[INTR] = 0;
133 	return(len);
134 }
135