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);
74 /* interrupts don't get us out */
75 while(Waitfor(pid, 1) < 0)
81 struct word nullpath={ "", 0};
82 void doredir(redir *rp)
89 Dup(rp->from, rp->to);
93 case RDUP: Dup(rp->from, rp->to); break;
94 case RCLOSE: close(rp->from); break;
98 word *searchpath(char *w){
100 if(strncmp(w, "/", 1)==0
101 || strncmp(w, "#", 1)==0
102 || strncmp(w, "./", 2)==0
103 || strncmp(w, "../", 3)==0
104 || (path=vlook("path")->val)==0)
109 popword(); /* "exec" */
110 if(runq->argv->words==0){
111 Xerror1("empty argument list");
114 doredir(runq->redir);
115 Execute(runq->argv->words, searchpath(runq->argv->words->word));
118 void execfunc(var *func)
122 starval=runq->argv->words;
125 start(func->fn, func->pc, (struct var *)0);
126 runq->local=newvar(strdup("*"), runq->local);
127 runq->local->val=starval;
128 runq->local->changed=1;
130 int dochdir(char *word){
131 /* report to /dev/wdir if it exists and we're interactive */
132 static int wdirfd = -2;
133 if(chdir(word)<0) return -1;
135 if(wdirfd==-2) /* try only once */
136 wdirfd = open("/dev/wdir", OWRITE|OCEXEC);
138 write(wdirfd, word, strlen(word));
143 word *a=runq->argv->words;
146 setstatus("can't cd");
147 cdpath=vlook("cdpath")->val;
150 pfmt(err, "Usage: cd [directory]\n");
153 if(a->next->word[0]=='/' || cdpath==0) cdpath=&nullpath;
154 for(;cdpath;cdpath=cdpath->next){
155 strcpy(dir, cdpath->word);
156 if(dir[0]) strcat(dir, "/");
157 strcat(dir, a->next->word);
159 if(strlen(cdpath->word)
160 && strcmp(cdpath->word, ".")!=0)
161 pfmt(err, "%s\n", dir);
166 if(cdpath==0) pfmt(err, "Can't cd %s: %r\n", a->next->word);
169 a=vlook("home")->val;
171 if(dochdir(a->word)>=0)
174 pfmt(err, "Can't cd %s: %r\n", a->word);
177 pfmt(err, "Can't cd -- $home empty\n");
183 switch(count(runq->argv->words)){
184 default: pfmt(err, "Usage: exit [status]\nExiting anyway\n");
185 case 2: setstatus(runq->argv->words->next->word);
189 void execshift(void){
193 switch(count(runq->argv->words)){
195 pfmt(err, "Usage: shift [n]\n");
196 setstatus("shift usage");
199 case 2: n=atoi(runq->argv->words->next->word); break;
203 for(;n && star->val;--n){
205 efree(star->val->word);
206 efree((char *)star->val);
216 while(*s==' ' || *s=='\t' || *s=='\n') s++;
217 while('0'<=*s && *s<='7') n=n*8+*s++-'0';
223 for(rp=runq->redir;rp;rp=rp->next){
226 if(rp->from==fd) fd=-1;
230 if(rp->to==fd) fd=rp->from;
236 union code rdcmds[4];
246 start(rdcmds, 1, runq->local);
251 char *cmdline, *s, *t;
254 if(count(runq->argv->words)<=1){
255 Xerror1("Usage: eval cmd ...");
259 for(ap=runq->argv->words->next;ap;ap=ap->next)
260 len+=1+strlen(ap->word);
261 cmdline=emalloc(len);
263 for(ap=runq->argv->words->next;ap;ap=ap->next){
264 for(t=ap->word;*t;) *s++=*t++;
269 execcmds(opencore(cmdline, len));
272 union code dotcmds[14];
292 dotcmds[9].f=Xrdcmds;
293 dotcmds[10].f=Xunlocal;
294 dotcmds[11].f=Xunlocal;
295 dotcmds[12].f=Xreturn;
301 if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){
306 if(p->argv->words==0){
307 Xerror1("Usage: . [-i] file [arg ...]");
310 zero=strdup(p->argv->words->word);
313 for(path=searchpath(zero);path;path=path->next){
314 strcpy(file, path->word);
315 if(file[0]) strcat(file, "/");
317 if((fd=open(file, 0))>=0) break;
318 if(strcmp(file, "/dev/stdin")==0){ /* for sun & ucb */
324 pfmt(err, "%s: ", zero);
325 setstatus("can't open");
326 Xerror(".: can't open");
329 /* set up for a new command loop */
330 start(dotcmds, 1, (struct var *)0);
331 pushredir(RCLOSE, fd, 0);
333 runq->cmdfd=openfd(fd);
338 runq->argv->words=p->argv->words;
339 /* free caller's copy of $* */
350 switch(count(runq->argv->words)){
352 setstatus(flag[(uchar)runq->argv->words->next->word[0]]?"":"flag not set");
355 letter=runq->argv->words->next->word;
356 val=runq->argv->words->next->next->word;
357 if(strlen(letter)==1){
358 if(strcmp(val, "+")==0){
359 flag[(uchar)letter[0]]=flagset;
362 if(strcmp(val, "-")==0){
363 flag[(uchar)letter[0]]=0;
368 Xerror1("Usage: flag [letter] [+-]");
373 void execwhatis(void){ /* mildly wrong -- should fork before writing */
380 a=runq->argv->words->next;
382 Xerror1("Usage: whatis name ...");
388 out->ebuf=&out->buf[NBUF];
393 pfmt(out, "%s=", a->word);
395 pfmt(out, "%q\n", v->val->word);
398 for(b=v->val;b && b->word;b=b->next){
399 pfmt(out, "%c%q", sep, b->word);
409 if(v->fn) pfmt(out, "fn %s %s\n", v->name, v->fn[v->pc-1].s);
411 for(bp=Builtin;bp->name;bp++)
412 if(strcmp(a->word, bp->name)==0){
413 pfmt(out, "builtin %s\n", a->word);
417 for(path=searchpath(a->word);path;path=path->next){
418 strcpy(file, path->word);
419 if(file[0]) strcat(file, "/");
420 strcat(file, a->word);
421 if(Executable(file)){
422 pfmt(out, "%s\n", file);
427 pfmt(err, "%s: not found\n", a->word);
428 setstatus("not found");
437 switch(count(runq->argv->words)){
438 default: Xerror1("Usage: wait [pid]"); return;
439 case 2: Waitfor(atoi(runq->argv->words->next->word), 0); break;
440 case 1: Waitfor(-1, 0); break;