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 f08fdedc 2003-11-23 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 f08fdedc 2003-11-23 devnull void hexnum(char *p, int n)
13 f08fdedc 2003-11-23 devnull *p++=hex[(n>>12)&0xF];
14 f08fdedc 2003-11-23 devnull *p++=hex[(n>>8)&0xF];
15 f08fdedc 2003-11-23 devnull *p++=hex[(n>>4)&0xF];
16 f08fdedc 2003-11-23 devnull *p=hex[n&0xF];
18 f08fdedc 2003-11-23 devnull tree *heredoc(tree *tag)
20 f08fdedc 2003-11-23 devnull struct here *h=new(struct here);
21 f08fdedc 2003-11-23 devnull if(tag->type!=WORD) yyerror("Bad here tag");
22 f08fdedc 2003-11-23 devnull h->next=0;
24 f08fdedc 2003-11-23 devnull *ehere=h;
27 f08fdedc 2003-11-23 devnull ehere=&h->next;
28 f08fdedc 2003-11-23 devnull h->tag=tag;
29 f08fdedc 2003-11-23 devnull hexnum(&tmp[9], getpid());
30 f08fdedc 2003-11-23 devnull hexnum(&tmp[14], ser++);
31 f08fdedc 2003-11-23 devnull h->name=strdup(tmp);
32 f08fdedc 2003-11-23 devnull return token(tmp, WORD);
35 f08fdedc 2003-11-23 devnull * bug: lines longer than NLINE get split -- this can cause spurious
36 f08fdedc 2003-11-23 devnull * missubstitution, or a misrecognized EOF marker.
38 f08fdedc 2003-11-23 devnull #define NLINE 4096
39 f08fdedc 2003-11-23 devnull void readhere(void){
40 f08fdedc 2003-11-23 devnull struct here *h, *nexth;
42 f08fdedc 2003-11-23 devnull char *s, *tag;
43 f08fdedc 2003-11-23 devnull int c, subst;
44 f08fdedc 2003-11-23 devnull char line[NLINE+1];
45 f08fdedc 2003-11-23 devnull for(h=here;h;h=nexth){
46 f08fdedc 2003-11-23 devnull subst=!h->tag->quoted;
47 f08fdedc 2003-11-23 devnull tag=h->tag->str;
48 f08fdedc 2003-11-23 devnull c=Creat(h->name);
49 f08fdedc 2003-11-23 devnull if(c<0) yyerror("can't create here document");
50 f08fdedc 2003-11-23 devnull f=openfd(c);
52 f08fdedc 2003-11-23 devnull pprompt();
53 f08fdedc 2003-11-23 devnull while((c=rchr(runq->cmdfd))!=EOF){
54 f08fdedc 2003-11-23 devnull if(c=='\n' || s==&line[NLINE]){
56 f08fdedc 2003-11-23 devnull if(strcmp(line, tag)==0) break;
57 f08fdedc 2003-11-23 devnull if(subst) psubst(f, line);
58 f08fdedc 2003-11-23 devnull else pstr(f, line);
60 f08fdedc 2003-11-23 devnull if(c=='\n'){
61 f08fdedc 2003-11-23 devnull pprompt();
62 f08fdedc 2003-11-23 devnull pchr(f, c);
64 f08fdedc 2003-11-23 devnull else *s++=c;
66 f08fdedc 2003-11-23 devnull else *s++=c;
68 f08fdedc 2003-11-23 devnull flush(f);
69 f08fdedc 2003-11-23 devnull closeio(f);
70 f08fdedc 2003-11-23 devnull cleanhere(h->name);
71 f08fdedc 2003-11-23 devnull nexth=h->next;
72 f08fdedc 2003-11-23 devnull efree((char *)h);
75 f08fdedc 2003-11-23 devnull doprompt=1;
77 f08fdedc 2003-11-23 devnull void psubst(io *f, char *s)
79 f08fdedc 2003-11-23 devnull char *t, *u;
80 f08fdedc 2003-11-23 devnull int savec, n;
81 f08fdedc 2003-11-23 devnull word *star;
82 f08fdedc 2003-11-23 devnull while(*s){
83 f08fdedc 2003-11-23 devnull if(*s!='$'){
84 f08fdedc 2003-11-23 devnull if(0xa0<=(*s&0xff) && (*s&0xff)<=0xf5){
85 f08fdedc 2003-11-23 devnull pchr(f, *s++);
86 f08fdedc 2003-11-23 devnull if(*s=='\0') break;
88 f08fdedc 2003-11-23 devnull else if(0xf6<=(*s&0xff) && (*s&0xff)<=0xf7){
89 f08fdedc 2003-11-23 devnull pchr(f, *s++);
90 f08fdedc 2003-11-23 devnull if(*s=='\0') break;
91 f08fdedc 2003-11-23 devnull pchr(f, *s++);
92 f08fdedc 2003-11-23 devnull if(*s=='\0') break;
94 f08fdedc 2003-11-23 devnull pchr(f, *s++);
98 f08fdedc 2003-11-23 devnull if(*t=='$') pchr(f, *t++);
100 f08fdedc 2003-11-23 devnull while(*t && idchr(*t)) t++;
101 f08fdedc 2003-11-23 devnull savec=*t;
102 f08fdedc 2003-11-23 devnull *t='\0';
104 f08fdedc 2003-11-23 devnull for(u=s;*u && '0'<=*u && *u<='9';u++) n=n*10+*u-'0';
105 f08fdedc 2003-11-23 devnull if(n && *u=='\0'){
106 f08fdedc 2003-11-23 devnull star=vlook("*")->val;
107 f08fdedc 2003-11-23 devnull if(star && 1<=n && n<=count(star)){
108 f08fdedc 2003-11-23 devnull while(--n) star=star->next;
109 f08fdedc 2003-11-23 devnull pstr(f, star->word);
113 f08fdedc 2003-11-23 devnull pstrs(f, vlook(s)->val);
114 f08fdedc 2003-11-23 devnull *t=savec;
115 f08fdedc 2003-11-23 devnull if(savec=='^') t++;
121 f08fdedc 2003-11-23 devnull void pstrs(io *f, word *a)
124 f08fdedc 2003-11-23 devnull while(a->next && a->next->word){
125 f08fdedc 2003-11-23 devnull pstr(f, a->word);
126 f08fdedc 2003-11-23 devnull pchr(f, ' ');
127 f08fdedc 2003-11-23 devnull a=a->next;
129 f08fdedc 2003-11-23 devnull pstr(f, a->word);