Blame


1 f08fdedc 2003-11-23 devnull #include "rc.h"
2 f08fdedc 2003-11-23 devnull #include "exec.h"
3 f08fdedc 2003-11-23 devnull #include "io.h"
4 f08fdedc 2003-11-23 devnull #include "fns.h"
5 f08fdedc 2003-11-23 devnull struct here *here, **ehere;
6 c8f53842 2007-03-26 devnull int ser = 0;
7 f08fdedc 2003-11-23 devnull char tmp[]="/tmp/here0000.0000";
8 f08fdedc 2003-11-23 devnull char hex[]="0123456789abcdef";
9 f08fdedc 2003-11-23 devnull void psubst(io*, char*);
10 f08fdedc 2003-11-23 devnull void pstrs(io*, word*);
11 c8f53842 2007-03-26 devnull
12 c8f53842 2007-03-26 devnull void
13 c8f53842 2007-03-26 devnull hexnum(char *p, int n)
14 f08fdedc 2003-11-23 devnull {
15 f08fdedc 2003-11-23 devnull *p++=hex[(n>>12)&0xF];
16 f08fdedc 2003-11-23 devnull *p++=hex[(n>>8)&0xF];
17 f08fdedc 2003-11-23 devnull *p++=hex[(n>>4)&0xF];
18 c8f53842 2007-03-26 devnull *p = hex[n&0xF];
19 f08fdedc 2003-11-23 devnull }
20 c8f53842 2007-03-26 devnull
21 c8f53842 2007-03-26 devnull tree*
22 c8f53842 2007-03-26 devnull heredoc(tree *tag)
23 f08fdedc 2003-11-23 devnull {
24 c8f53842 2007-03-26 devnull struct here *h = new(struct here);
25 c8f53842 2007-03-26 devnull if(tag->type!=WORD)
26 c8f53842 2007-03-26 devnull yyerror("Bad here tag");
27 c8f53842 2007-03-26 devnull h->next = 0;
28 f08fdedc 2003-11-23 devnull if(here)
29 c8f53842 2007-03-26 devnull *ehere = h;
30 f08fdedc 2003-11-23 devnull else
31 c8f53842 2007-03-26 devnull here = h;
32 f08fdedc 2003-11-23 devnull ehere=&h->next;
33 c8f53842 2007-03-26 devnull h->tag = tag;
34 f08fdedc 2003-11-23 devnull hexnum(&tmp[9], getpid());
35 f08fdedc 2003-11-23 devnull hexnum(&tmp[14], ser++);
36 c8f53842 2007-03-26 devnull h->name = strdup(tmp);
37 f08fdedc 2003-11-23 devnull return token(tmp, WORD);
38 f08fdedc 2003-11-23 devnull }
39 f08fdedc 2003-11-23 devnull /*
40 f08fdedc 2003-11-23 devnull * bug: lines longer than NLINE get split -- this can cause spurious
41 f08fdedc 2003-11-23 devnull * missubstitution, or a misrecognized EOF marker.
42 f08fdedc 2003-11-23 devnull */
43 f08fdedc 2003-11-23 devnull #define NLINE 4096
44 c8f53842 2007-03-26 devnull
45 c8f53842 2007-03-26 devnull void
46 c8f53842 2007-03-26 devnull readhere(void)
47 c8f53842 2007-03-26 devnull {
48 f08fdedc 2003-11-23 devnull struct here *h, *nexth;
49 f08fdedc 2003-11-23 devnull io *f;
50 f08fdedc 2003-11-23 devnull char *s, *tag;
51 f08fdedc 2003-11-23 devnull int c, subst;
52 f08fdedc 2003-11-23 devnull char line[NLINE+1];
53 c8f53842 2007-03-26 devnull for(h = here;h;h = nexth){
54 f08fdedc 2003-11-23 devnull subst=!h->tag->quoted;
55 c8f53842 2007-03-26 devnull tag = h->tag->str;
56 c8f53842 2007-03-26 devnull c = Creat(h->name);
57 c8f53842 2007-03-26 devnull if(c<0)
58 c8f53842 2007-03-26 devnull yyerror("can't create here document");
59 c8f53842 2007-03-26 devnull f = openfd(c);
60 c8f53842 2007-03-26 devnull s = line;
61 f08fdedc 2003-11-23 devnull pprompt();
62 c8f53842 2007-03-26 devnull while((c = rchr(runq->cmdfd))!=EOF){
63 f08fdedc 2003-11-23 devnull if(c=='\n' || s==&line[NLINE]){
64 f08fdedc 2003-11-23 devnull *s='\0';
65 c8f53842 2007-03-26 devnull if(tag && strcmp(line, tag)==0) break;
66 c8f53842 2007-03-26 devnull if(subst)
67 c8f53842 2007-03-26 devnull psubst(f, line);
68 f08fdedc 2003-11-23 devnull else pstr(f, line);
69 c8f53842 2007-03-26 devnull s = line;
70 f08fdedc 2003-11-23 devnull if(c=='\n'){
71 f08fdedc 2003-11-23 devnull pprompt();
72 f08fdedc 2003-11-23 devnull pchr(f, c);
73 f08fdedc 2003-11-23 devnull }
74 f08fdedc 2003-11-23 devnull else *s++=c;
75 f08fdedc 2003-11-23 devnull }
76 f08fdedc 2003-11-23 devnull else *s++=c;
77 f08fdedc 2003-11-23 devnull }
78 f08fdedc 2003-11-23 devnull flush(f);
79 f08fdedc 2003-11-23 devnull closeio(f);
80 f08fdedc 2003-11-23 devnull cleanhere(h->name);
81 c8f53842 2007-03-26 devnull nexth = h->next;
82 f08fdedc 2003-11-23 devnull efree((char *)h);
83 f08fdedc 2003-11-23 devnull }
84 c8f53842 2007-03-26 devnull here = 0;
85 c8f53842 2007-03-26 devnull doprompt = 1;
86 f08fdedc 2003-11-23 devnull }
87 c8f53842 2007-03-26 devnull
88 c8f53842 2007-03-26 devnull void
89 c8f53842 2007-03-26 devnull psubst(io *f, char *s)
90 f08fdedc 2003-11-23 devnull {
91 f08fdedc 2003-11-23 devnull char *t, *u;
92 f08fdedc 2003-11-23 devnull int savec, n;
93 f08fdedc 2003-11-23 devnull word *star;
94 f08fdedc 2003-11-23 devnull while(*s){
95 f08fdedc 2003-11-23 devnull if(*s!='$'){
96 f08fdedc 2003-11-23 devnull if(0xa0<=(*s&0xff) && (*s&0xff)<=0xf5){
97 f08fdedc 2003-11-23 devnull pchr(f, *s++);
98 c8f53842 2007-03-26 devnull if(*s=='\0')
99 c8f53842 2007-03-26 devnull break;
100 f08fdedc 2003-11-23 devnull }
101 f08fdedc 2003-11-23 devnull else if(0xf6<=(*s&0xff) && (*s&0xff)<=0xf7){
102 f08fdedc 2003-11-23 devnull pchr(f, *s++);
103 c8f53842 2007-03-26 devnull if(*s=='\0')
104 c8f53842 2007-03-26 devnull break;
105 f08fdedc 2003-11-23 devnull pchr(f, *s++);
106 c8f53842 2007-03-26 devnull if(*s=='\0')
107 c8f53842 2007-03-26 devnull break;
108 f08fdedc 2003-11-23 devnull }
109 f08fdedc 2003-11-23 devnull pchr(f, *s++);
110 f08fdedc 2003-11-23 devnull }
111 f08fdedc 2003-11-23 devnull else{
112 f08fdedc 2003-11-23 devnull t=++s;
113 c8f53842 2007-03-26 devnull if(*t=='$')
114 c8f53842 2007-03-26 devnull pchr(f, *t++);
115 f08fdedc 2003-11-23 devnull else{
116 f08fdedc 2003-11-23 devnull while(*t && idchr(*t)) t++;
117 f08fdedc 2003-11-23 devnull savec=*t;
118 f08fdedc 2003-11-23 devnull *t='\0';
119 c8f53842 2007-03-26 devnull n = 0;
120 c8f53842 2007-03-26 devnull for(u = s;*u && '0'<=*u && *u<='9';u++) n = n*10+*u-'0';
121 f08fdedc 2003-11-23 devnull if(n && *u=='\0'){
122 c8f53842 2007-03-26 devnull star = vlook("*")->val;
123 f08fdedc 2003-11-23 devnull if(star && 1<=n && n<=count(star)){
124 c8f53842 2007-03-26 devnull while(--n) star = star->next;
125 f08fdedc 2003-11-23 devnull pstr(f, star->word);
126 f08fdedc 2003-11-23 devnull }
127 f08fdedc 2003-11-23 devnull }
128 f08fdedc 2003-11-23 devnull else
129 f08fdedc 2003-11-23 devnull pstrs(f, vlook(s)->val);
130 c8f53842 2007-03-26 devnull *t = savec;
131 c8f53842 2007-03-26 devnull if(savec=='^')
132 c8f53842 2007-03-26 devnull t++;
133 f08fdedc 2003-11-23 devnull }
134 c8f53842 2007-03-26 devnull s = t;
135 f08fdedc 2003-11-23 devnull }
136 f08fdedc 2003-11-23 devnull }
137 f08fdedc 2003-11-23 devnull }
138 c8f53842 2007-03-26 devnull
139 c8f53842 2007-03-26 devnull void
140 c8f53842 2007-03-26 devnull pstrs(io *f, word *a)
141 f08fdedc 2003-11-23 devnull {
142 f08fdedc 2003-11-23 devnull if(a){
143 f08fdedc 2003-11-23 devnull while(a->next && a->next->word){
144 f08fdedc 2003-11-23 devnull pstr(f, a->word);
145 f08fdedc 2003-11-23 devnull pchr(f, ' ');
146 c8f53842 2007-03-26 devnull a = a->next;
147 f08fdedc 2003-11-23 devnull }
148 f08fdedc 2003-11-23 devnull pstr(f, a->word);
149 f08fdedc 2003-11-23 devnull }
150 f08fdedc 2003-11-23 devnull }