]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/rc/simple.c
git/branch: somewhere in the syncing, the fix for junk files was lost
[plan9front.git] / sys / src / cmd / rc / simple.c
index d4b897cd162d3a9f4ca18d3aabd1a6117f4ab482..3987dbe5ad6f952f218f2a244c408397410b0b50 100644 (file)
  */
 int
 exitnext(void){
-       union code *c=&runq->code[runq->pc];
-       while(c->f==Xpopredir) c++;
+       code *c=&runq->code[runq->pc];
+       while(1){
+               if(c->f==Xpopredir || c->f==Xunlocal)
+                       c++;
+               else if(c->f==Xsrcline || c->f==Xsrcfile)
+                       c += 2;
+               else
+                       break;
+       }
        return c->f==Xexit;
 }
 
@@ -20,18 +27,17 @@ void
 Xsimple(void)
 {
        word *a;
-       thread *p = runq;
        var *v;
        struct builtin *bp;
        int pid;
-       globlist();
-       a = runq->argv->words;
+
+       a = globlist(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 */
+               pfmt(err, "%v\n", a); /* wrong, should do redirs */
        v = gvlook(a->word);
        if(v->fn)
                execfunc(v);
@@ -55,7 +61,6 @@ Xsimple(void)
                        /* fork and wait is redundant */
                        pushword("exec");
                        execexec();
-                       Xexit();
                }
                else{
                        flush(err);
@@ -72,7 +77,6 @@ Xsimple(void)
                }
        }
 }
-struct word nullpath = { "", 0};
 
 void
 doredir(redir *rp)
@@ -97,16 +101,18 @@ doredir(redir *rp)
 }
 
 word*
-searchpath(char *w)
+searchpath(char *w, char *v)
 {
+       static struct word nullpath = { "", 0 };
        word *path;
-       if(strncmp(w, "/", 1)==0
-       || strncmp(w, "#", 1)==0
-       || strncmp(w, "./", 2)==0
-       || strncmp(w, "../", 3)==0
-       || (path = vlook("path")->val)==0)
-               path=&nullpath;
-       return path;
+
+       if(w[0] && w[0] != '/' && w[0] != '#' &&
+         (w[0] != '.' || (w[1] && w[1] != '/' && (w[1] != '.' || w[2] && w[2] != '/')))){
+               path = vlook(v)->val;
+               if(path)
+                       return path;
+       }
+       return &nullpath;
 }
 
 void
@@ -118,8 +124,9 @@ execexec(void)
                return;
        }
        doredir(runq->redir);
-       Execute(runq->argv->words, searchpath(runq->argv->words->word));
+       Execute(runq->argv->words, searchpath(runq->argv->words->word, "path"));
        poplist();
+       Xexit();
 }
 
 void
@@ -131,7 +138,7 @@ execfunc(var *func)
        runq->argv->words = 0;
        poplist();
        start(func->fn, func->pc, runq->local);
-       runq->local = newvar(strdup("*"), runq->local);
+       runq->local = newvar("*", runq->local);
        runq->local->val = starval;
        runq->local->changed = 1;
 }
@@ -140,14 +147,9 @@ 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));
-       }
+       if(chdir(word)<0)
+               return -1;
+       newwdir = 1;
        return 1;
 }
 
@@ -159,24 +161,19 @@ execcd(void)
        char *dir;
 
        setstatus("can't cd");
-       cdpath = vlook("cdpath")->val;
        switch(count(a)){
        default:
                pfmt(err, "Usage: cd [directory]\n");
                break;
        case 2:
-               if(a->next->word[0]=='/' || cdpath==0)
-                       cdpath = &nullpath;
-               for(; cdpath; cdpath = cdpath->next){
+               a = a->next;
+               for(cdpath = searchpath(a->word, "cdpath"); cdpath; cdpath = cdpath->next){
                        if(cdpath->word[0] != '\0')
-                               dir = smprint("%s/%s", cdpath->word,
-                                       a->next->word);
+                               dir = smprint("%s/%s", cdpath->word, a->word);
                        else
-                               dir = strdup(a->next->word);
-
+                               dir = estrdup(a->word);
                        if(dochdir(dir) >= 0){
-                               if(cdpath->word[0] != '\0' &&
-                                   strcmp(cdpath->word, ".") != 0)
+                               if(cdpath->word[0] != '\0' && strcmp(cdpath->word, ".") != 0)
                                        pfmt(err, "%s\n", dir);
                                free(dir);
                                setstatus("");
@@ -185,7 +182,7 @@ execcd(void)
                        free(dir);
                }
                if(cdpath==0)
-                       pfmt(err, "Can't cd %s: %r\n", a->next->word);
+                       pfmt(err, "Can't cd %s: %r\n", a->word);
                break;
        case 1:
                a = vlook("home")->val;
@@ -234,10 +231,10 @@ execshift(void)
                break;
        }
        star = vlook("*");
-       for(;n && star->val;--n){
+       for(;n>0 && star->val;--n){
                a = star->val->next;
-               efree(star->val->word);
-               efree((char *)star->val);
+               free(star->val->word);
+               free(star->val);
                star->val = a;
                star->changed = 1;
        }
@@ -245,15 +242,6 @@ execshift(void)
        poplist();
 }
 
-int
-octal(char *s)
-{
-       int n = 0;
-       while(*s==' ' || *s=='\t' || *s=='\n') s++;
-       while('0'<=*s && *s<='7') n = n*8+*s++-'0';
-       return n;
-}
-
 int
 mapfd(int fd)
 {
@@ -279,6 +267,7 @@ void
 execcmds(io *f)
 {
        static int first = 1;
+
        if(first){
                rdcmds[0].i = 1;
                rdcmds[1].f = Xrdcmds;
@@ -293,26 +282,19 @@ execcmds(io *f)
 void
 execeval(void)
 {
-       char *cmdline, *s, *t;
-       int len = 0;
-       word *ap;
+       char *cmdline;
+       int len;
        if(count(runq->argv->words)<=1){
                Xerror1("Usage: eval cmd ...");
                return;
        }
        eflagok = 1;
-       for(ap = runq->argv->words->next;ap;ap = ap->next)
-               len+=1+strlen(ap->word);
-       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';
+       cmdline = list2str(runq->argv->words->next);
+       len = strlen(cmdline);
+       cmdline[len] = '\n';
        poplist();
-       execcmds(opencore(cmdline, len));
-       efree(cmdline);
+       execcmds(opencore(cmdline, len+1));
+       free(cmdline);
 }
 union code dotcmds[14];
 
@@ -345,6 +327,7 @@ execdot(void)
        }
        else
                eflagok = 1;
+
        popword();
        if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){
                iflag = 1;
@@ -355,14 +338,14 @@ execdot(void)
                Xerror1("Usage: . [-i] file [arg ...]");
                return;
        }
-       zero = strdup(p->argv->words->word);
+       zero = estrdup(p->argv->words->word);
        popword();
        fd = -1;
-       for(path = searchpath(zero); path; path = path->next){
+       for(path = searchpath(zero, "path"); path; path = path->next){
                if(path->word[0] != '\0')
                        file = smprint("%s/%s", path->word, zero);
                else
-                       file = strdup(zero);
+                       file = estrdup(zero);
 
                fd = open(file, 0);
                free(file);
@@ -380,6 +363,9 @@ execdot(void)
                Xerror(".: can't open");
                return;
        }
+
+       lexline = 1;
+
        /* set up for a new command loop */
        start(dotcmds, 1, (struct var *)0);
        pushredir(RCLOSE, fd, 0);
@@ -393,7 +379,7 @@ execdot(void)
        /* free caller's copy of $* */
        av = p->argv;
        p->argv = av->next;
-       efree((char *)av);
+       free(av);
        /* push $0 value */
        pushlist();
        pushword(zero);
@@ -474,13 +460,13 @@ execwhatis(void){ /* mildly wrong -- should fork before writing */
                                        break;
                                }
                        if(!bp->name){
-                               for(path = searchpath(a->word); path;
+                               for(path = searchpath(a->word, "path"); path;
                                    path = path->next){
                                        if(path->word[0] != '\0')
                                                file = smprint("%s/%s",
                                                        path->word, a->word);
                                        else
-                                               file = strdup(a->word);
+                                               file = estrdup(a->word);
                                        if(Executable(file)){
                                                pfmt(out, "%s\n", file);
                                                free(file);