From 29138fff8643194ec49cb79304d2a878d46c378b Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 16 Nov 2021 12:08:59 -0800 Subject: Feat: added heredocs Heredocs are simply strings written to tmp files. There was minimal bug testing here. Also, various bug fixes are included --- src/cmd/rc/exec.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 4 deletions(-) (limited to 'src/cmd/rc/exec.c') diff --git a/src/cmd/rc/exec.c b/src/cmd/rc/exec.c index 71f5359..28b6135 100644 --- a/src/cmd/rc/exec.c +++ b/src/cmd/rc/exec.c @@ -684,12 +684,129 @@ Xmark(void) pushlist(); } +void +Xunmark(void) +{ + poplist(); +} + void Xword(void) { pushword(runner->code.exe[runner->code.i++].s); } +void +Xwrite(void) +{ + int fd; + char *path; + + switch(count(runner->args->word)){ + default: + Xerror("> requires singleton\n"); + return; + case 0: + Xerror("> requires path\n"); + return; + case 1: + ; + } + path = runner->args->word->str; + if((fd=open(path,O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR))<0){ + print(shell.err, "%s: ", path); + Xerror("can't open"); + return; + } + pushredir(Ropen, fd, runner->code.exe[runner->code.i++].i); + poplist(); +} + +void +Xappend(void) +{ + int fd; + char *path; + + switch(count(runner->args->word)){ + default: + Xerror(">> requires singleton\n"); + return; + case 0: + Xerror(">> requires path\n"); + return; + case 1: + ; + } + path = runner->args->word->str; + if(((fd=open(path,O_APPEND|O_WRONLY))<0) + && ((fd=open(path,O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR))<0)){ + print(shell.err, "%s: ", path); + Xerror("can't open"); + return; + } + pushredir(Ropen, fd, runner->code.exe[runner->code.i++].i); + poplist(); +} + +void +Xread(void) +{ + int fd; + char *path; + + switch(count(runner->args->word)){ + default: + Xerror("< requires singleton\n"); + return; + case 0: + Xerror("< requires path\n"); + return; + case 1: + ; + } + path = runner->args->word->str; + if((fd=open(path,O_RDONLY))<0){ + print(shell.err, "%s: ", path); + Xerror("can't open"); + return; + } + pushredir(Ropen, fd, runner->code.exe[runner->code.i++].i); + poplist(); +} + +void +Xrdwr(void) +{ + int fd; + char *path; + + switch(count(runner->args->word)){ + default: + Xerror("<> requires singleton\n"); + return; + case 0: + Xerror("<> requires path\n"); + return; + case 1: + ; + } + path = runner->args->word->str; + if((fd=open(path,O_RDWR))<0){ + print(shell.err, "%s: ", path); + Xerror("can't open"); + return; + } + pushredir(Ropen, fd, runner->code.exe[runner->code.i++].i); + poplist(); +} + +void +Xnewpgid(void) +{ + runner->pgid = -1; +} + void Xsettrue(void) { @@ -774,12 +891,15 @@ clean: void Xfor(void) { - if(!runner->args->word){ + if(!runner->args->word){ /* terminate loop */ poplist(); runner->code.i = runner->code.exe[runner->code.i].i; - }else{ + }else{ /* continue loop */ freelist(runner->local->val); + /* reset process group */ + runner->pgid = -1; + /* reset local variables */ runner->local->val = runner->args->word; runner->local->new = 1; runner->args->word = runner->args->word->link; @@ -789,8 +909,7 @@ Xfor(void) } } -static -Word* +static Word* catlist(Word *l, Word *r, Word *tail) { Word *w; @@ -1343,6 +1462,12 @@ Xpopredir(void) efree(r); } +void +Xdelhere(void) +{ + remove(runner->code.exe[runner->code.i++].s); +} + void Xreturn(void) { -- cgit v1.2.1