plan9port/src/cmd/rc/simple.c

506 lines
8.8 KiB
C
Raw Normal View History

/*
* Maybe `simple' is a misnomer.
*/
#include "rc.h"
#include "getflags.h"
#include "exec.h"
#include "io.h"
#include "fns.h"
/*
* Search through the following code to see if we're just going to exit.
*/
int
exitnext(void){
union code *c=&runq->code[runq->pc];
while(c->f==Xpopredir) c++;
return c->f==Xexit;
}
2007-03-26 12:02:41 +00:00
void
Xsimple(void)
{
word *a;
2007-03-26 12:02:41 +00:00
thread *p = runq;
var *v;
struct builtin *bp;
2007-03-26 12:02:41 +00:00
int pid;
globlist();
2007-03-26 12:02:41 +00:00
a = runq->argv->words;
if(a==0){
Xerror1("empty argument list");
return;
}
if(flag['x'])
pfmt(err, "%v\n", p->argv->words); /* wrong, should do redirs */
2007-03-26 12:02:41 +00:00
v = gvlook(a->word);
if(v->fn)
execfunc(v);
else{
if(strcmp(a->word, "builtin")==0){
if(count(a)==1){
pfmt(err, "builtin: empty argument list\n");
setstatus("empty arg list");
poplist();
return;
}
2007-03-26 12:02:41 +00:00
a = a->next;
popword();
}
2007-03-26 12:02:41 +00:00
for(bp = Builtin;bp->name;bp++)
if(strcmp(a->word, bp->name)==0){
(*bp->fnc)();
return;
}
if(exitnext()){
/* fork and wait is redundant */
pushword("exec");
execexec();
Xexit();
}
else{
flush(err);
Updenv(); /* necessary so changes don't go out again */
2007-03-26 12:02:41 +00:00
if((pid = execforkexec()) < 0){
Xerror("try again");
return;
}
2007-03-26 12:02:41 +00:00
/* interrupts don't get us out */
poplist();
while(Waitfor(pid, 1) < 0)
;
}
}
}
2007-03-26 12:02:41 +00:00
struct word nullpath = { "", 0};
void
doredir(redir *rp)
{
if(rp){
doredir(rp->next);
switch(rp->type){
case ROPEN:
if(rp->from!=rp->to){
Dup(rp->from, rp->to);
close(rp->from);
}
break;
2007-03-26 12:02:41 +00:00
case RDUP:
Dup(rp->from, rp->to);
break;
case RCLOSE:
close(rp->from);
break;
}
}
}
2007-03-26 12:02:41 +00:00
word*
searchpath(char *w)
{
word *path;
if(strncmp(w, "/", 1)==0
2005-01-23 23:19:47 +00:00
/* || strncmp(w, "#", 1)==0 */
|| strncmp(w, "./", 2)==0
|| strncmp(w, "../", 3)==0
2007-03-26 12:02:41 +00:00
|| (path = vlook("path")->val)==0)
path=&nullpath;
return path;
}
2007-03-26 12:02:41 +00:00
void
execexec(void)
{
popword(); /* "exec" */
if(runq->argv->words==0){
Xerror1("empty argument list");
return;
}
doredir(runq->redir);
Execute(runq->argv->words, searchpath(runq->argv->words->word));
poplist();
}
2007-03-26 12:02:41 +00:00
void
execfunc(var *func)
{
word *starval;
popword();
2007-03-26 12:02:41 +00:00
starval = runq->argv->words;
runq->argv->words = 0;
poplist();
start(func->fn, func->pc, runq->local);
2007-03-26 12:02:41 +00:00
runq->local = newvar(strdup("*"), runq->local);
runq->local->val = starval;
runq->local->changed = 1;
}
2007-03-26 12:02:41 +00:00
int
dochdir(char *word)
{
/* report to /dev/wdir if it exists and we're interactive */
static int wdirfd = -2;
if(chdir(word)<0) return -1;
if(flag['i']!=0){
if(wdirfd==-2) /* try only once */
wdirfd = open("/dev/wdir", OWRITE|OCEXEC);
if(wdirfd>=0)
write(wdirfd, word, strlen(word));
}
return 1;
}
2007-03-26 12:02:41 +00:00
void
execcd(void)
{
word *a = runq->argv->words;
word *cdpath;
char dir[512];
setstatus("can't cd");
2007-03-26 12:02:41 +00:00
cdpath = vlook("cdpath")->val;
switch(count(a)){
default:
pfmt(err, "Usage: cd [directory]\n");
break;
case 2:
2007-03-26 12:02:41 +00:00
if(a->next->word[0]=='/' || cdpath==0)
cdpath=&nullpath;
for(;cdpath;cdpath = cdpath->next){
strcpy(dir, cdpath->word);
2007-03-26 12:02:41 +00:00
if(dir[0])
strcat(dir, "/");
strcat(dir, a->next->word);
if(dochdir(dir)>=0){
if(strlen(cdpath->word)
&& strcmp(cdpath->word, ".")!=0)
pfmt(err, "%s\n", dir);
setstatus("");
break;
}
}
2007-03-26 12:02:41 +00:00
if(cdpath==0)
pfmt(err, "Can't cd %s: %r\n", a->next->word);
break;
case 1:
2007-03-26 12:02:41 +00:00
a = vlook("home")->val;
if(count(a)>=1){
if(dochdir(a->word)>=0)
setstatus("");
else
pfmt(err, "Can't cd %s: %r\n", a->word);
}
else
pfmt(err, "Can't cd -- $home empty\n");
break;
}
poplist();
}
2007-03-26 12:02:41 +00:00
void
execexit(void)
{
switch(count(runq->argv->words)){
2007-03-26 12:02:41 +00:00
default:
pfmt(err, "Usage: exit [status]\nExiting anyway\n");
case 2:
setstatus(runq->argv->words->next->word);
case 1: Xexit();
}
}
2007-03-26 12:02:41 +00:00
void
execshift(void)
{
int n;
word *a;
var *star;
switch(count(runq->argv->words)){
default:
pfmt(err, "Usage: shift [n]\n");
setstatus("shift usage");
poplist();
return;
2007-03-26 12:02:41 +00:00
case 2:
n = atoi(runq->argv->words->next->word);
break;
case 1:
n = 1;
break;
}
2007-03-26 12:02:41 +00:00
star = vlook("*");
for(;n && star->val;--n){
2007-03-26 12:02:41 +00:00
a = star->val->next;
efree(star->val->word);
efree((char *)star->val);
2007-03-26 12:02:41 +00:00
star->val = a;
star->changed = 1;
}
setstatus("");
poplist();
}
2007-03-26 12:02:41 +00:00
int
octal(char *s)
{
2007-03-26 12:02:41 +00:00
int n = 0;
while(*s==' ' || *s=='\t' || *s=='\n') s++;
2007-03-26 12:02:41 +00:00
while('0'<=*s && *s<='7') n = n*8+*s++-'0';
return n;
}
2007-03-26 12:02:41 +00:00
int
mapfd(int fd)
{
redir *rp;
2007-03-26 12:02:41 +00:00
for(rp = runq->redir;rp;rp = rp->next){
switch(rp->type){
case RCLOSE:
2007-03-26 12:02:41 +00:00
if(rp->from==fd)
fd=-1;
break;
case RDUP:
case ROPEN:
2007-03-26 12:02:41 +00:00
if(rp->to==fd)
fd = rp->from;
break;
}
}
return fd;
}
union code rdcmds[4];
2007-03-26 12:02:41 +00:00
void
execcmds(io *f)
{
2007-03-26 12:02:41 +00:00
static int first = 1;
if(first){
2007-03-26 12:02:41 +00:00
rdcmds[0].i = 1;
rdcmds[1].f = Xrdcmds;
rdcmds[2].f = Xreturn;
first = 0;
}
start(rdcmds, 1, runq->local);
2007-03-26 12:02:41 +00:00
runq->cmdfd = f;
runq->iflast = 0;
}
2007-03-26 12:02:41 +00:00
void
execeval(void)
{
char *cmdline, *s, *t;
2007-03-26 12:02:41 +00:00
int len = 0;
word *ap;
if(count(runq->argv->words)<=1){
Xerror1("Usage: eval cmd ...");
return;
}
2007-03-26 12:02:41 +00:00
eflagok = 1;
for(ap = runq->argv->words->next;ap;ap = ap->next)
len+=1+strlen(ap->word);
2007-03-26 12:02:41 +00:00
cmdline = emalloc(len);
s = cmdline;
for(ap = runq->argv->words->next;ap;ap = ap->next){
for(t = ap->word;*t;) *s++=*t++;
*s++=' ';
}
s[-1]='\n';
poplist();
execcmds(opencore(cmdline, len));
efree(cmdline);
}
union code dotcmds[14];
2007-03-26 12:02:41 +00:00
void
execdot(void)
{
int iflag = 0;
int fd;
list *av;
2007-03-26 12:02:41 +00:00
thread *p = runq;
char *zero;
2007-03-26 12:02:41 +00:00
static int first = 1;
char file[512];
word *path;
rc: add recursive descent parser The old yacc-based parser is available with the -Y flag, which will probably be removed at some point. The new -D flag dumps a parse tree of the input, without executing it. This allows comparing the output of rc -D and rc -DY on different scripts to see that the two parsers behave the same. The rc paper ends by saying: It is remarkable that in the four most recent editions of the UNIX system programmer’s manual the Bourne shell grammar described in the manual page does not admit the command who|wc. This is surely an oversight, but it suggests something darker: nobody really knows what the Bourne shell’s grammar is. Even examination of the source code is little help. The parser is implemented by recursive descent, but the routines corresponding to the syntactic categories all have a flag argument that subtly changes their operation depending on the context. Rc’s parser is implemented using yacc, so I can say precisely what the grammar is. The new recursive descent parser here has no such flags. It is a straightforward translation of the yacc. The new parser will make it easier to handle free carats in more generality as well as potentially allow the use of unquoted = as a word character. Going through this exercise has highlighted a few dark corners here as well. For example, I was surprised to find that x >f | y >f x | y are different commands (the latter redirects y's output). It is similarly surprising that a=b x | y sets a during the execution of y. It is also a bit counter-intuitive x | y | z x | if(c) y | z are not both 3-phase pipelines. These are certainly not things we should change, but they are not entirely obvious from the man page description, undercutting the quoted claim a bit. On the other hand, who | wc is clearly accepted by the grammar in the manual page, and the new parser still handles that test case.
2020-05-04 18:34:19 -04:00
if(first){
2007-03-26 12:02:41 +00:00
dotcmds[0].i = 1;
dotcmds[1].f = Xmark;
dotcmds[2].f = Xword;
dotcmds[3].s="0";
2007-03-26 12:02:41 +00:00
dotcmds[4].f = Xlocal;
dotcmds[5].f = Xmark;
dotcmds[6].f = Xword;
dotcmds[7].s="*";
2007-03-26 12:02:41 +00:00
dotcmds[8].f = Xlocal;
dotcmds[9].f = Xrdcmds;
dotcmds[10].f = Xunlocal;
dotcmds[11].f = Xunlocal;
dotcmds[12].f = Xreturn;
first = 0;
}
else
2007-03-26 12:02:41 +00:00
eflagok = 1;
popword();
if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){
2007-03-26 12:02:41 +00:00
iflag = 1;
popword();
}
/* get input file */
if(p->argv->words==0){
Xerror1("Usage: . [-i] file [arg ...]");
return;
}
2007-03-26 12:02:41 +00:00
zero = strdup(p->argv->words->word);
popword();
fd=-1;
2007-03-26 12:02:41 +00:00
for(path = searchpath(zero);path;path = path->next){
strcpy(file, path->word);
2007-03-26 12:02:41 +00:00
if(file[0])
strcat(file, "/");
strcat(file, zero);
2007-03-26 12:02:41 +00:00
if((fd = open(file, 0))>=0) break;
if(strcmp(file, "/dev/stdin")==0){ /* for sun & ucb */
2007-03-26 12:02:41 +00:00
fd = Dup1(0);
if(fd>=0)
break;
}
}
if(fd<0){
pfmt(err, "%s: ", zero);
setstatus("can't open");
Xerror(".: can't open");
return;
}
/* set up for a new command loop */
start(dotcmds, 1, (struct var *)0);
pushredir(RCLOSE, fd, 0);
2007-03-26 12:02:41 +00:00
runq->cmdfile = zero;
runq->cmdfd = openfd(fd);
runq->iflag = iflag;
runq->iflast = 0;
/* push $* value */
pushlist();
2007-03-26 12:02:41 +00:00
runq->argv->words = p->argv->words;
/* free caller's copy of $* */
2007-03-26 12:02:41 +00:00
av = p->argv;
p->argv = av->next;
efree((char *)av);
/* push $0 value */
pushlist();
pushword(zero);
ndot++;
}
2007-03-26 12:02:41 +00:00
void
execflag(void)
{
char *letter, *val;
switch(count(runq->argv->words)){
case 2:
setstatus(flag[(uchar)runq->argv->words->next->word[0]]?"":"flag not set");
break;
case 3:
2007-03-26 12:02:41 +00:00
letter = runq->argv->words->next->word;
val = runq->argv->words->next->next->word;
if(strlen(letter)==1){
if(strcmp(val, "+")==0){
2007-03-26 12:02:41 +00:00
flag[(uchar)letter[0]] = flagset;
break;
}
if(strcmp(val, "-")==0){
2007-03-26 12:02:41 +00:00
flag[(uchar)letter[0]] = 0;
break;
}
}
default:
Xerror1("Usage: flag [letter] [+-]");
return;
}
poplist();
}
2007-03-26 12:02:41 +00:00
void
execwhatis(void){ /* mildly wrong -- should fork before writing */
word *a, *b, *path;
var *v;
struct builtin *bp;
char file[512];
struct io out[1];
int found, sep;
2007-03-26 12:02:41 +00:00
a = runq->argv->words->next;
if(a==0){
Xerror1("Usage: whatis name ...");
return;
}
setstatus("");
2007-03-26 12:02:41 +00:00
out->fd = mapfd(1);
out->bufp = out->buf;
out->ebuf = &out->buf[NBUF];
out->strp = 0;
for(;a;a = a->next){
v = vlook(a->word);
if(v->val){
pfmt(out, "%s=", a->word);
if(v->val->next==0)
pfmt(out, "%q\n", v->val->word);
else{
sep='(';
2007-03-26 12:02:41 +00:00
for(b = v->val;b && b->word;b = b->next){
pfmt(out, "%c%q", sep, b->word);
sep=' ';
}
pfmt(out, ")\n");
}
2007-03-26 12:02:41 +00:00
found = 1;
}
else
2007-03-26 12:02:41 +00:00
found = 0;
v = gvlook(a->word);
if(v->fn)
pfmt(out, "fn %s %s\n", v->name, v->fn[v->pc-1].s);
else{
2007-03-26 12:02:41 +00:00
for(bp = Builtin;bp->name;bp++)
if(strcmp(a->word, bp->name)==0){
pfmt(out, "builtin %s\n", a->word);
break;
}
if(!bp->name){
2007-03-26 12:02:41 +00:00
for(path = searchpath(a->word);path;path = path->next){
strcpy(file, path->word);
2007-03-26 12:02:41 +00:00
if(file[0])
strcat(file, "/");
strcat(file, a->word);
if(Executable(file)){
pfmt(out, "%s\n", file);
break;
}
}
if(!path && !found){
pfmt(err, "%s: not found\n", a->word);
setstatus("not found");
}
}
}
}
poplist();
flush(err);
}
2007-03-26 12:02:41 +00:00
void
execwait(void)
{
switch(count(runq->argv->words)){
2007-03-26 12:02:41 +00:00
default:
Xerror1("Usage: wait [pid]");
return;
case 2:
Waitfor(atoi(runq->argv->words->next->word), 0);
break;
case 1:
Waitfor(-1, 0);
break;
}
poplist();
}