diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2021-12-31 23:26:59 +0000 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2021-12-31 23:26:59 +0000 |
commit | 2025214dfcc46ac16cb93abc05b344330987b339 (patch) | |
tree | d121ec515b81006a5272bc1c7fadf93fb4cf4392 /sys/src/cmd/rc | |
parent | d0c9127b10251d85b56eba00bc586b57dd998aaf (diff) |
rc: fix here document handling with quoted end-marker (thanks sigrid)
when end marker is quoted, we should not substitute.
also, pcmd() needs to print the end marker without quotes.
Diffstat (limited to 'sys/src/cmd/rc')
-rw-r--r-- | sys/src/cmd/rc/code.c | 14 | ||||
-rw-r--r-- | sys/src/cmd/rc/exec.c | 39 | ||||
-rw-r--r-- | sys/src/cmd/rc/exec.h | 2 | ||||
-rw-r--r-- | sys/src/cmd/rc/pcmd.c | 2 |
4 files changed, 32 insertions, 25 deletions
diff --git a/sys/src/cmd/rc/code.c b/sys/src/cmd/rc/code.c index 67c2eb925..f39b3a573 100644 --- a/sys/src/cmd/rc/code.c +++ b/sys/src/cmd/rc/code.c @@ -353,14 +353,10 @@ outcode(tree *t, int eflag) stuffdot(p); break; case REDIR: - emitf(Xmark); - if(t->rtype==HERE){ - /* replace end marker with mktmep() pattern */ - free(c0->str); - c0->str=estrdup("/tmp/here.XXXXXXXXXXX"); - c0->glob=0; + if(t->rtype!=HERE){ + emitf(Xmark); + outcode(c0, eflag); } - outcode(c0, eflag); switch(t->rtype){ case APPEND: emitf(Xappend); @@ -375,7 +371,7 @@ outcode(tree *t, int eflag) emitf(Xrdwr); break; case HERE: - emitf(Xhere); + emitf(c0->quoted?Xhereq:Xhere); emits(t->str); t->str=0; /* passed ownership */ break; @@ -539,7 +535,7 @@ codefree(code *cp) || p->f==Xsubshell || p->f==Xtrue) p++; else if(p->f==Xdup || p->f==Xpipefd) p+=2; else if(p->f==Xpipe) p+=4; - else if(p->f==Xhere) free(p[1].s), p+=2; + else if(p->f==Xhere || p->f==Xhereq) free(p[1].s), p+=2; else if(p->f==Xword) free((++p)->s); else if(p->f==Xfn){ free(p[2].s); diff --git a/sys/src/cmd/rc/exec.c b/sys/src/cmd/rc/exec.c index 0af91f261..7e2aaa95a 100644 --- a/sys/src/cmd/rc/exec.c +++ b/sys/src/cmd/rc/exec.c @@ -446,22 +446,11 @@ Xpush(void) void Xhere(void) { - char *file; + char file[] = "/tmp/here.XXXXXXXXXXX"; int fd; io *io; - switch(count(runq->argv->words)){ - default: - Xerror1("<< requires singleton"); - return; - case 0: - Xerror1("<< requires file"); - return; - case 1: - break; - } - file = mktemp(runq->argv->words->word); - if((fd = Creat(file))<0){ + if((fd = Creat(mktemp(file)))<0){ Xerror("can't open"); return; } @@ -469,13 +458,35 @@ Xhere(void) psubst(io, (uchar*)runq->code[runq->pc++].s); flushio(io); closeio(io); + + /* open for reading and unlink */ + if((fd = Open(file, 3))<0){ + Xerror("can't open"); + return; + } + pushredir(ROPEN, fd, runq->code[runq->pc++].i); +} + +void +Xhereq(void) +{ + char file[] = "/tmp/here.XXXXXXXXXXX", *body; + int fd; + + if((fd = Creat(mktemp(file)))<0){ + Xerror("can't open"); + return; + } + body = runq->code[runq->pc++].s; + Write(fd, body, strlen(body)); + Close(fd); + /* open for reading and unlink */ if((fd = Open(file, 3))<0){ Xerror("can't open"); return; } pushredir(ROPEN, fd, runq->code[runq->pc++].i); - poplist(); } void diff --git a/sys/src/cmd/rc/exec.h b/sys/src/cmd/rc/exec.h index d75c7f1cd..a696ece1e 100644 --- a/sys/src/cmd/rc/exec.h +++ b/sys/src/cmd/rc/exec.h @@ -4,7 +4,7 @@ extern void Xappend(void), Xasync(void), Xbackq(void), Xbang(void), Xclose(void); extern void Xconc(void), Xcount(void), Xdelfn(void), Xdol(void), Xqw(void), Xdup(void); extern void Xexit(void), Xfalse(void), Xfn(void), Xfor(void), Xglob(void); -extern void Xjump(void), Xmark(void), Xmatch(void), Xpipe(void), Xread(void), Xhere(void); +extern void Xjump(void), Xmark(void), Xmatch(void), Xpipe(void), Xread(void), Xhere(void), Xhereq(void); extern void Xrdwr(void), Xsrcline(void); extern void Xunredir(void), Xstar(void), Xreturn(void), Xsubshell(void); extern void Xtrue(void), Xword(void), Xwrite(void), Xpipefd(void), Xcase(void); diff --git a/sys/src/cmd/rc/pcmd.c b/sys/src/cmd/rc/pcmd.c index 67edaf856..0e51c0d4a 100644 --- a/sys/src/cmd/rc/pcmd.c +++ b/sys/src/cmd/rc/pcmd.c @@ -147,7 +147,7 @@ pcmd(io *f, tree *t) } pfmt(f, "%t", c0); if(t->rtype == HERE) - pfmt(f, "\n%s%t\n", t->str, c0); + pfmt(f, "\n%s%s\n", t->str, c0->str); else if(c1) pfmt(f, " %t", c1); break; |