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++;
27 a = globlist(runq->argv->words);
29 Xerror1("empty argument list");
33 pfmt(err, "%v\n", a); /* wrong, should do redirs */
38 if(strcmp(a->word, "builtin")==0){
40 pfmt(err, "builtin: empty argument list\n");
41 setstatus("empty arg list");
48 for(bp = Builtin;bp->name;bp++)
49 if(strcmp(a->word, bp->name)==0){
54 /* fork and wait is redundant */
60 Updenv(); /* necessary so changes don't go out again */
61 if((pid = execforkexec()) < 0){
66 /* interrupts don't get us out */
68 while(Waitfor(pid, 1) < 0)
73 struct word nullpath = { "", 0};
83 Dup(rp->from, rp->to);
88 Dup(rp->from, rp->to);
101 if(strncmp(w, "/", 1)==0
102 || strncmp(w, "#", 1)==0
103 || strncmp(w, "./", 2)==0
104 || strncmp(w, "../", 3)==0
105 || (path = vlook("path")->val)==0)
113 popword(); /* "exec" */
114 if(runq->argv->words==0){
115 Xerror1("empty argument list");
118 doredir(runq->redir);
119 Execute(runq->argv->words, searchpath(runq->argv->words->word));
129 starval = runq->argv->words;
130 runq->argv->words = 0;
132 start(func->fn, func->pc, runq->local);
133 runq->local = newvar("*", runq->local);
134 runq->local->val = starval;
135 runq->local->changed = 1;
141 /* report to /dev/wdir if it exists and we're interactive */
151 word *a = runq->argv->words;
155 setstatus("can't cd");
156 cdpath = vlook("cdpath")->val;
159 pfmt(err, "Usage: cd [directory]\n");
162 if(a->next->word[0]=='/' || cdpath==0)
164 for(; cdpath; cdpath = cdpath->next){
165 if(cdpath->word[0] != '\0')
166 dir = smprint("%s/%s", cdpath->word,
169 dir = estrdup(a->next->word);
171 if(dochdir(dir) >= 0){
172 if(cdpath->word[0] != '\0' &&
173 strcmp(cdpath->word, ".") != 0)
174 pfmt(err, "%s\n", dir);
182 pfmt(err, "Can't cd %s: %r\n", a->next->word);
185 a = vlook("home")->val;
187 if(dochdir(a->word)>=0)
190 pfmt(err, "Can't cd %s: %r\n", a->word);
193 pfmt(err, "Can't cd -- $home empty\n");
202 switch(count(runq->argv->words)){
204 pfmt(err, "Usage: exit [status]\nExiting anyway\n");
206 setstatus(runq->argv->words->next->word);
217 switch(count(runq->argv->words)){
219 pfmt(err, "Usage: shift [n]\n");
220 setstatus("shift usage");
224 n = atoi(runq->argv->words->next->word);
231 for(;n>0 && star->val;--n){
233 free(star->val->word);
246 for(rp = runq->redir;rp;rp = rp->next){
261 union code rdcmds[4];
266 static int first = 1;
269 rdcmds[1].f = Xrdcmds;
270 rdcmds[2].f = Xreturn;
273 start(rdcmds, 1, runq->local);
283 if(count(runq->argv->words)<=1){
284 Xerror1("Usage: eval cmd ...");
288 cmdline = list2str(runq->argv->words->next);
289 len = strlen(cmdline);
292 execcmds(opencore(cmdline, len+1));
295 union code dotcmds[14];
306 static int first = 1;
310 dotcmds[1].f = Xmark;
311 dotcmds[2].f = Xword;
313 dotcmds[4].f = Xlocal;
314 dotcmds[5].f = Xmark;
315 dotcmds[6].f = Xword;
317 dotcmds[8].f = Xlocal;
318 dotcmds[9].f = Xrdcmds;
319 dotcmds[10].f = Xunlocal;
320 dotcmds[11].f = Xunlocal;
321 dotcmds[12].f = Xreturn;
327 if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){
332 if(p->argv->words==0){
333 Xerror1("Usage: . [-i] file [arg ...]");
336 zero = estrdup(p->argv->words->word);
339 for(path = searchpath(zero); path; path = path->next){
340 if(path->word[0] != '\0')
341 file = smprint("%s/%s", path->word, zero);
343 file = estrdup(zero);
349 if(strcmp(file, "/dev/stdin")==0){ /* for sun & ucb */
356 pfmt(err, "%s: ", zero);
357 setstatus("can't open");
358 Xerror(".: can't open");
361 /* set up for a new command loop */
362 start(dotcmds, 1, (struct var *)0);
363 pushredir(RCLOSE, fd, 0);
364 runq->cmdfile = zero;
365 runq->cmdfd = openfd(fd);
370 runq->argv->words = p->argv->words;
371 /* free caller's copy of $* */
385 switch(count(runq->argv->words)){
387 setstatus(flag[(uchar)runq->argv->words->next->word[0]]?"":"flag not set");
390 letter = runq->argv->words->next->word;
391 val = runq->argv->words->next->next->word;
392 if(strlen(letter)==1){
393 if(strcmp(val, "+")==0){
394 flag[(uchar)letter[0]] = flagset;
397 if(strcmp(val, "-")==0){
398 flag[(uchar)letter[0]] = 0;
403 Xerror1("Usage: flag [letter] [+-]");
410 execwhatis(void){ /* mildly wrong -- should fork before writing */
417 a = runq->argv->words->next;
419 Xerror1("Usage: whatis name ...");
424 out->bufp = out->buf;
425 out->ebuf = &out->buf[NBUF];
430 pfmt(out, "%s=", a->word);
432 pfmt(out, "%q\n", v->val->word);
435 for(b = v->val;b && b->word;b = b->next){
436 pfmt(out, "%c%q", sep, b->word);
447 pfmt(out, "fn %q %s\n", v->name, v->fn[v->pc-1].s);
449 for(bp = Builtin;bp->name;bp++)
450 if(strcmp(a->word, bp->name)==0){
451 pfmt(out, "builtin %s\n", a->word);
455 for(path = searchpath(a->word); path;
457 if(path->word[0] != '\0')
458 file = smprint("%s/%s",
459 path->word, a->word);
461 file = estrdup(a->word);
462 if(Executable(file)){
463 pfmt(out, "%s\n", file);
470 pfmt(err, "%s: not found\n", a->word);
471 setstatus("not found");
483 switch(count(runq->argv->words)){
485 Xerror1("Usage: wait [pid]");
488 Waitfor(atoi(runq->argv->words->next->word), 0);