aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/rc/here.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2007-03-26 12:02:41 +0000
committerrsc <devnull@localhost>2007-03-26 12:02:41 +0000
commitc8f538425f4e92e1e438b9bd25cb08e250a93d5b (patch)
treed701abbcd4973d5b6909d104eefe521de70a4e1c /src/cmd/rc/here.c
parent79049567a0fac8707ea3f2927403445bdb2394fa (diff)
downloadplan9port-c8f538425f4e92e1e438b9bd25cb08e250a93d5b.tar.gz
plan9port-c8f538425f4e92e1e438b9bd25cb08e250a93d5b.tar.bz2
plan9port-c8f538425f4e92e1e438b9bd25cb08e250a93d5b.zip
sync with plan 9
Diffstat (limited to 'src/cmd/rc/here.c')
-rw-r--r--src/cmd/rc/here.c97
1 files changed, 58 insertions, 39 deletions
diff --git a/src/cmd/rc/here.c b/src/cmd/rc/here.c
index 2a0fe2f2..17c62458 100644
--- a/src/cmd/rc/here.c
+++ b/src/cmd/rc/here.c
@@ -3,32 +3,37 @@
#include "io.h"
#include "fns.h"
struct here *here, **ehere;
-int ser=0;
+int ser = 0;
char tmp[]="/tmp/here0000.0000";
char hex[]="0123456789abcdef";
void psubst(io*, char*);
void pstrs(io*, word*);
-void hexnum(char *p, int n)
+
+void
+hexnum(char *p, int n)
{
*p++=hex[(n>>12)&0xF];
*p++=hex[(n>>8)&0xF];
*p++=hex[(n>>4)&0xF];
- *p=hex[n&0xF];
+ *p = hex[n&0xF];
}
-tree *heredoc(tree *tag)
+
+tree*
+heredoc(tree *tag)
{
- struct here *h=new(struct here);
- if(tag->type!=WORD) yyerror("Bad here tag");
- h->next=0;
+ struct here *h = new(struct here);
+ if(tag->type!=WORD)
+ yyerror("Bad here tag");
+ h->next = 0;
if(here)
- *ehere=h;
+ *ehere = h;
else
- here=h;
+ here = h;
ehere=&h->next;
- h->tag=tag;
+ h->tag = tag;
hexnum(&tmp[9], getpid());
hexnum(&tmp[14], ser++);
- h->name=strdup(tmp);
+ h->name = strdup(tmp);
return token(tmp, WORD);
}
/*
@@ -36,27 +41,32 @@ tree *heredoc(tree *tag)
* missubstitution, or a misrecognized EOF marker.
*/
#define NLINE 4096
-void readhere(void){
+
+void
+readhere(void)
+{
struct here *h, *nexth;
io *f;
char *s, *tag;
int c, subst;
char line[NLINE+1];
- for(h=here;h;h=nexth){
+ for(h = here;h;h = nexth){
subst=!h->tag->quoted;
- tag=h->tag->str;
- c=Creat(h->name);
- if(c<0) yyerror("can't create here document");
- f=openfd(c);
- s=line;
+ tag = h->tag->str;
+ c = Creat(h->name);
+ if(c<0)
+ yyerror("can't create here document");
+ f = openfd(c);
+ s = line;
pprompt();
- while((c=rchr(runq->cmdfd))!=EOF){
+ while((c = rchr(runq->cmdfd))!=EOF){
if(c=='\n' || s==&line[NLINE]){
*s='\0';
- if(strcmp(line, tag)==0) break;
- if(subst) psubst(f, line);
+ if(tag && strcmp(line, tag)==0) break;
+ if(subst)
+ psubst(f, line);
else pstr(f, line);
- s=line;
+ s = line;
if(c=='\n'){
pprompt();
pchr(f, c);
@@ -68,13 +78,15 @@ void readhere(void){
flush(f);
closeio(f);
cleanhere(h->name);
- nexth=h->next;
+ nexth = h->next;
efree((char *)h);
}
- here=0;
- doprompt=1;
+ here = 0;
+ doprompt = 1;
}
-void psubst(io *f, char *s)
+
+void
+psubst(io *f, char *s)
{
char *t, *u;
int savec, n;
@@ -83,48 +95,55 @@ void psubst(io *f, char *s)
if(*s!='$'){
if(0xa0<=(*s&0xff) && (*s&0xff)<=0xf5){
pchr(f, *s++);
- if(*s=='\0') break;
+ if(*s=='\0')
+ break;
}
else if(0xf6<=(*s&0xff) && (*s&0xff)<=0xf7){
pchr(f, *s++);
- if(*s=='\0') break;
+ if(*s=='\0')
+ break;
pchr(f, *s++);
- if(*s=='\0') break;
+ if(*s=='\0')
+ break;
}
pchr(f, *s++);
}
else{
t=++s;
- if(*t=='$') pchr(f, *t++);
+ if(*t=='$')
+ pchr(f, *t++);
else{
while(*t && idchr(*t)) t++;
savec=*t;
*t='\0';
- n=0;
- for(u=s;*u && '0'<=*u && *u<='9';u++) n=n*10+*u-'0';
+ n = 0;
+ for(u = s;*u && '0'<=*u && *u<='9';u++) n = n*10+*u-'0';
if(n && *u=='\0'){
- star=vlook("*")->val;
+ star = vlook("*")->val;
if(star && 1<=n && n<=count(star)){
- while(--n) star=star->next;
+ while(--n) star = star->next;
pstr(f, star->word);
}
}
else
pstrs(f, vlook(s)->val);
- *t=savec;
- if(savec=='^') t++;
+ *t = savec;
+ if(savec=='^')
+ t++;
}
- s=t;
+ s = t;
}
}
}
-void pstrs(io *f, word *a)
+
+void
+pstrs(io *f, word *a)
{
if(a){
while(a->next && a->next->word){
pstr(f, a->word);
pchr(f, ' ');
- a=a->next;
+ a = a->next;
}
pstr(f, a->word);
}