2 * Maybe `simple' is a misnomer.
10 * Search through the following code to see if we're just going to exit.
14 union code *c=&runq->code[runq->pc];
15 while(c->f==Xpopredir) c++;
28 Xerror1("empty argument list");
32 pfmt(err, "%v\n", p->argv->words); /* wrong, should do redirs */
37 if(strcmp(a->word, "builtin")==0){
39 pfmt(err, "builtin: empty argument list\n");
40 setstatus("empty arg list");
47 for(bp=Builtin;bp->name;bp++)
48 if(strcmp(a->word, bp->name)==0){
53 /* fork and wait is redundant */
60 Updenv(); /* necessary so changes don't go out again */
68 strcpy(buf, "can't exec: ");
70 errstr(buf+n, ERRMAX-n);
75 /* interrupts don't get us out */
76 while(Waitfor(pid, 1) < 0)
83 struct word nullpath={ "", 0};
84 void doredir(redir *rp)
91 Dup(rp->from, rp->to);
95 case RDUP: Dup(rp->from, rp->to); break;
96 case RCLOSE: close(rp->from); break;
100 word *searchpath(char *w){
102 if(strncmp(w, "/", 1)==0
103 || strncmp(w, "#", 1)==0
104 || strncmp(w, "./", 2)==0
105 || strncmp(w, "../", 3)==0
106 || (path=vlook("path")->val)==0)
111 popword(); /* "exec" */
112 if(runq->argv->words==0){
113 Xerror1("empty argument list");
116 doredir(runq->redir);
117 Execute(runq->argv->words, searchpath(runq->argv->words->word));
120 void execfunc(var *func)
124 starval=runq->argv->words;
127 start(func->fn, func->pc, (struct var *)0);
128 runq->local=newvar(strdup("*"), runq->local);
129 runq->local->val=starval;
130 runq->local->changed=1;
132 int dochdir(char *word){
133 /* report to /dev/wdir if it exists and we're interactive */
134 static int wdirfd = -2;
135 if(chdir(word)<0) return -1;
137 if(wdirfd==-2) /* try only once */
138 wdirfd = open("/dev/wdir", OWRITE|OCEXEC);
140 write(wdirfd, word, strlen(word));
145 word *a=runq->argv->words;
148 setstatus("can't cd");
149 cdpath=vlook("cdpath")->val;
152 pfmt(err, "Usage: cd [directory]\n");
155 if(a->next->word[0]=='/' || cdpath==0) cdpath=&nullpath;
156 for(;cdpath;cdpath=cdpath->next){
157 strcpy(dir, cdpath->word);
158 if(dir[0]) strcat(dir, "/");
159 strcat(dir, a->next->word);
161 if(strlen(cdpath->word)
162 && strcmp(cdpath->word, ".")!=0)
163 pfmt(err, "%s\n", dir);
168 if(cdpath==0) pfmt(err, "Can't cd %s: %r\n", a->next->word);
171 a=vlook("home")->val;
173 if(dochdir(a->word)>=0)
176 pfmt(err, "Can't cd %s: %r\n", a->word);
179 pfmt(err, "Can't cd -- $home empty\n");
185 switch(count(runq->argv->words)){
186 default: pfmt(err, "Usage: exit [status]\nExiting anyway\n");
187 case 2: setstatus(runq->argv->words->next->word);
191 void execshift(void){
195 switch(count(runq->argv->words)){
197 pfmt(err, "Usage: shift [n]\n");
198 setstatus("shift usage");
201 case 2: n=atoi(runq->argv->words->next->word); break;
205 for(;n && star->val;--n){
207 efree(star->val->word);
208 efree((char *)star->val);
218 while(*s==' ' || *s=='\t' || *s=='\n') s++;
219 while('0'<=*s && *s<='7') n=n*8+*s++-'0';
225 for(rp=runq->redir;rp;rp=rp->next){
228 if(rp->from==fd) fd=-1;
232 if(rp->to==fd) fd=rp->from;
238 union code rdcmds[4];
248 start(rdcmds, 1, runq->local);
253 char *cmdline, *s, *t;
256 if(count(runq->argv->words)<=1){
257 Xerror1("Usage: eval cmd ...");
261 for(ap=runq->argv->words->next;ap;ap=ap->next)
262 len+=1+strlen(ap->word);
263 cmdline=emalloc(len);
265 for(ap=runq->argv->words->next;ap;ap=ap->next){
266 for(t=ap->word;*t;) *s++=*t++;
271 execcmds(opencore(cmdline, len));
274 union code dotcmds[14];
294 dotcmds[9].f=Xrdcmds;
295 dotcmds[10].f=Xunlocal;
296 dotcmds[11].f=Xunlocal;
297 dotcmds[12].f=Xreturn;
303 if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){
308 if(p->argv->words==0){
309 Xerror1("Usage: . [-i] file [arg ...]");
312 zero=strdup(p->argv->words->word);
315 for(path=searchpath(zero);path;path=path->next){
316 strcpy(file, path->word);
317 if(file[0]) strcat(file, "/");
319 if((fd=open(file, 0))>=0) break;
320 if(strcmp(file, "/dev/stdin")==0){ /* for sun & ucb */
326 pfmt(err, "%s: ", zero);
327 setstatus("can't open");
328 Xerror(".: can't open");
331 /* set up for a new command loop */
332 start(dotcmds, 1, (struct var *)0);
333 pushredir(RCLOSE, fd, 0);
335 runq->cmdfd=openfd(fd);
340 runq->argv->words=p->argv->words;
341 /* free caller's copy of $* */
352 switch(count(runq->argv->words)){
354 setstatus(flag[(uchar)runq->argv->words->next->word[0]]?"":"flag not set");
357 letter=runq->argv->words->next->word;
358 val=runq->argv->words->next->next->word;
359 if(strlen(letter)==1){
360 if(strcmp(val, "+")==0){
361 flag[(uchar)letter[0]]=flagset;
364 if(strcmp(val, "-")==0){
365 flag[(uchar)letter[0]]=0;
370 Xerror1("Usage: flag [letter] [+-]");
375 void execwhatis(void){ /* mildly wrong -- should fork before writing */
382 a=runq->argv->words->next;
384 Xerror1("Usage: whatis name ...");
390 out->ebuf=&out->buf[NBUF];
395 pfmt(out, "%s=", a->word);
397 pfmt(out, "%q\n", v->val->word);
400 for(b=v->val;b && b->word;b=b->next){
401 pfmt(out, "%c%q", sep, b->word);
411 if(v->fn) pfmt(out, "fn %s %s\n", v->name, v->fn[v->pc-1].s);
413 for(bp=Builtin;bp->name;bp++)
414 if(strcmp(a->word, bp->name)==0){
415 pfmt(out, "builtin %s\n", a->word);
419 for(path=searchpath(a->word);path;path=path->next){
420 strcpy(file, path->word);
421 if(file[0]) strcat(file, "/");
422 strcat(file, a->word);
423 if(Executable(file)){
424 pfmt(out, "%s\n", file);
429 pfmt(err, "%s: not found\n", a->word);
430 setstatus("not found");
439 switch(count(runq->argv->words)){
440 default: Xerror1("Usage: wait [pid]"); return;
441 case 2: Waitfor(atoi(runq->argv->words->next->word), 0); break;
442 case 1: Waitfor(-1, 0); break;