4 #if defined(PLAN9PORT) && defined(__sun__)
5 # define BSD_COMP /* sigh. for TIOCNOTTY */
13 * Start executing the given code at the given pc with the given redirection
16 void start(code *c, int pc, var *local)
18 struct thread *p=new(struct thread);
22 p->redir=p->startredir=runq?runq->redir:0;
33 word *newword(char *wd, word *next)
40 void pushword(char *wd)
42 if(runq->argv==0) panic("pushword but no argv!", 0);
43 runq->argv->words=newword(wd, runq->argv->words);
47 if(runq->argv==0) panic("popword but no argv!", 0);
49 if(p==0) panic("popword but no word!", 0);
50 runq->argv->words=p->next;
54 void freelist(word *w)
72 if(p==0) panic("poplist but no argv", 0);
80 for(n=0;w;n++) w=w->next;
83 void pushredir(int type, int from, int to){
84 redir * rp=new(redir);
91 var *newvar(char *name, var *next)
104 * get command line flags, initialize keywords & traps.
105 * get values from environment.
106 * set $pid, $cflag, $*
107 * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*)
108 * start interpreting code
111 main(int argc, char *argv[])
114 char num[12], *rcmain;
117 /* needed for rcmain later */
118 setenv("PLAN9", unsharp("#9"), 1);
120 argc=getflags(argc, argv, "srdiIlxepvVc:1m:1[command]", 1);
121 if(argc==-1) usage("[file [arg ...]]");
122 if(argv[0][0]=='-') flag['l']=flagset;
123 if(flag['I']) flag['i'] = 0;
124 else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
125 rcmain=flag['m']?flag['m'][0]:Rcmain();
130 itoa(num, mypid=getpid());
132 setvar("pid", newword(num, (word *)0));
133 setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
135 setvar("rcname", newword(argv[0], (word *)0));
138 bootstrap[i++].f=Xmark;
139 bootstrap[i++].f=Xword;
140 bootstrap[i++].s="*";
141 bootstrap[i++].f=Xassign;
142 bootstrap[i++].f=Xmark;
143 bootstrap[i++].f=Xmark;
144 bootstrap[i++].f=Xword;
145 bootstrap[i++].s="*";
146 bootstrap[i++].f=Xdol;
147 bootstrap[i++].f=Xword;
148 bootstrap[i++].s=rcmain;
149 bootstrap[i++].f=Xword;
150 bootstrap[i++].s=".";
151 bootstrap[i++].f=Xsimple;
152 bootstrap[i++].f=Xexit;
154 start(bootstrap, 1, (var *)0);
155 /* prime bootstrap argv */
157 argv0 = strdup(argv[0]);
158 for(i=argc-1;i!=0;--i) pushword(argv[i]);
160 if(flag['r']) pfnc(err, runq);
162 (*runq->code[runq->pc-1].f)();
168 * Arguments on stack (...)
169 * Arguments in line [...]
170 * Code in line with jump around {...}
172 * Xappend(file)[fd] open file to append
173 * Xassign(name, val) assign val to name
174 * Xasync{... Xexit} make thread for {}, no wait
175 * Xbackq{... Xreturn} make thread for {}, push stdout
176 * Xbang complement condition
177 * Xcase(pat, value){...} exec code on match, leave (value) on
179 * Xclose[i] close file descriptor
180 * Xconc(left, right) concatenate, push results
181 * Xcount(name) push var count
182 * Xdelfn(name) delete function definition
183 * Xdeltraps(names) delete named traps
184 * Xdol(name) get variable value
185 * Xqdol(name) concatenate variable components
186 * Xdup[i j] dup file descriptor
187 * Xexit rc exits with status
188 * Xfalse{...} execute {} if false
189 * Xfn(name){... Xreturn} define function
190 * Xfor(var, list){... Xreturn} for loop
192 * Xlocal(name, val) create local variable, assign value
194 * Xmatch(pat, str) match pattern, set status
195 * Xpipe[i j]{... Xreturn}{... Xreturn} construct a pipe between 2 new threads,
197 * Xpipefd[type]{... Xreturn} connect {} to pipe (input or output,
198 * depending on type), push /dev/fd/??
199 * Xpopm(value) pop value from stack
200 * Xread(file)[fd] open file to read
201 * Xsettraps(names){... Xreturn} define trap functions
202 * Xshowtraps print trap list
203 * Xsimple(args) run command and wait
204 * Xreturn kill thread
205 * Xsubshell{... Xexit} execute {} in a subshell and wait
206 * Xtrue{...} execute {} if true
207 * Xunlocal delete local variable
208 * Xword[string] push string
209 * Xwrite(file)[fd] open file to write
214 switch(count(runq->argv->words)){
215 default: Xerror1(">> requires singleton"); return;
216 case 0: Xerror1(">> requires file"); return;
219 file=runq->argv->words->word;
220 if((f=open(file, 1))<0 && (f=Creat(file))<0){
221 pfmt(err, "%s: ", file);
222 Xerror("can't open");
226 pushredir(ROPEN, f, runq->code[runq->pc].i);
231 int null=open("/dev/null", 0);
236 Xerror("Can't open /dev/null\n");
239 switch(pid=rfork(RFFDG|RFPROC|RFNOTEG)){
246 * I don't know what the right thing to do here is,
247 * so this is all experimentally determined.
248 * If we just dup /dev/null onto 0, then running
249 * ssh foo & will reopen /dev/tty, try to read a password,
250 * get a signal, and repeat, in a tight loop, forever.
251 * Arguably this is a bug in ssh (it behaves the same
252 * way under bash as under rc) but I'm fixing it here
253 * anyway. If we dissociate the process from the tty,
254 * then it won't be able to open /dev/tty ever again.
255 * The SIG_IGN on SIGTTOU makes writing the tty
256 * (via fd 1 or 2, for example) succeed even though
257 * our pgrp is not the terminal's controlling pgrp.
259 if((tty=open("/dev/tty", OREAD)) >= 0){
261 * Should make reads of tty fail, writes succeed.
263 signal(SIGTTIN, SIG_IGN);
264 signal(SIGTTOU, SIG_IGN);
265 ioctl(tty, TIOCNOTTY);
269 pushredir(ROPEN, null, 0);
272 start(runq->code, runq->pc+1, runq->local);
277 runq->pc=runq->code[runq->pc].i;
279 setvar("apid", newword(npid, (word *)0));
287 setstatus(truestatus()?"false":"");
290 pushredir(RCLOSE, runq->code[runq->pc].i, 0);
294 pushredir(RDUP, runq->code[runq->pc].i, runq->code[runq->pc+1].i);
298 if(eflagok && !truestatus()) Xexit();
302 struct word *starval;
303 static int beenhere=0;
304 if(getpid()==mypid && !beenhere){
305 trapreq=vlook("sigexit");
309 starval=vlook("*")->val;
310 start(trapreq->fn, trapreq->pc, (struct var *)0);
311 runq->local=newvar(strdup("*"), runq->local);
312 runq->local->val=copywords(starval, (struct word *)0);
313 runq->local->changed=1;
314 runq->redir=runq->startredir=0;
321 if(truestatus()) runq->pc=runq->code[runq->pc].i;
324 int ifnot; /* dynamic if not flag */
329 runq->pc=runq->code[runq->pc].i;
332 runq->pc=runq->code[runq->pc].i;
343 switch(count(runq->argv->words)){
344 default: Xerror1("< requires singleton\n"); return;
345 case 0: Xerror1("< requires file\n"); return;
348 file=runq->argv->words->word;
349 if((f=open(file, 0))<0){
350 pfmt(err, "%s: ", file);
351 Xerror("can't open");
354 pushredir(ROPEN, f, runq->code[runq->pc].i);
358 void turfredir(void){
359 while(runq->redir!=runq->startredir)
362 void Xpopredir(void){
363 struct redir *rp=runq->redir;
364 if(rp==0) panic("turfredir null!", 0);
365 runq->redir=rp->next;
366 if(rp->type==ROPEN) close(rp->from);
370 struct thread *p=runq;
372 while(p->argv) poplist();
376 if(runq==0) Exit(getstatus());
379 if(truestatus()) runq->pc++;
380 else runq->pc=runq->code[runq->pc].i;
384 if(truestatus()) runq->pc++;
385 else runq->pc=runq->code[runq->pc].i;
391 pushword(runq->code[runq->pc++].s);
396 switch(count(runq->argv->words)){
397 default: Xerror1("> requires singleton\n"); return;
398 case 0: Xerror1("> requires file\n"); return;
401 file=runq->argv->words->word;
402 if((f=Creat(file))<0){
403 pfmt(err, "%s: ", file);
404 Xerror("can't open");
407 pushredir(ROPEN, f, runq->code[runq->pc].i);
411 char *_list2str(word *words, int c){
415 for(ap=words;ap;ap=ap->next)
416 len+=1+strlen(ap->word);
417 value=emalloc(len+1);
419 for(ap=words;ap;ap=ap->next){
420 for(t=ap->word;*t;) *s++=*t++;
423 if(s==value) *s='\0';
427 char *list2str(word *words){
428 return _list2str(words, ' ');
433 subject=list2str(runq->argv->words);
434 setstatus("no match");
435 for(p=runq->argv->next->words;p;p=p->next)
436 if(match(subject, p->word, '\0')){
448 s=list2str(runq->argv->next->words);
449 for(p=runq->argv->words;p;p=p->next){
450 if(match(s, p->word, '\0')){
459 runq->pc=runq->code[runq->pc].i;
462 word *conclist(word *lp, word *rp, word *tail)
466 if(lp->next || rp->next)
467 tail=conclist(lp->next==0?lp:lp->next, rp->next==0?rp:rp->next,
469 buf=emalloc(strlen(lp->word)+strlen(rp->word)+1);
470 strcpy(buf, lp->word);
471 strcat(buf, rp->word);
472 v=newword(buf, tail);
477 word *lp=runq->argv->words;
478 word *rp=runq->argv->next->words;
479 word *vp=runq->argv->next->next->words;
480 int lc=count(lp), rc=count(rp);
483 Xerror1("null list in concatenation");
486 if(lc!=1 && rc!=1 && lc!=rc){
487 Xerror1("mismatched list lengths in concatenation");
490 vp=conclist(lp, rp, vp);
494 runq->argv->words=vp;
498 if(count(runq->argv->words)!=1){
499 Xerror1("variable name not singleton!");
502 deglob(runq->argv->words->word);
503 v=vlook(runq->argv->words->word);
507 v->val=runq->argv->words;
515 * copy arglist a, adding the copy to the front of tail
517 word *copywords(word *a, word *tail)
520 for(end=&v;a;a=a->next,end=&(*end)->next)
521 *end=newword(a->word, 0);
529 if(count(runq->argv->words)!=1){
530 Xerror1("variable name not singleton!");
533 s=runq->argv->words->word;
536 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
537 a=runq->argv->next->words;
539 a=copywords(vlook(s)->val, a);
541 star=vlook("*")->val;
542 if(star && 1<=n && n<=count(star)){
543 while(--n) star=star->next;
544 a=newword(star->word, a);
554 if(count(runq->argv->words)!=1){
555 Xerror1("variable name not singleton!");
558 s=runq->argv->words->word;
567 for(p=a;p;p=p->next) n+=strlen(p->word);
571 for(p=a->next;p;p=p->next){
581 word *subwords(word *val, int len, word *sub, word *a)
586 a=subwords(val, len, sub->next, a);
590 while('0'<=*s && *s<='9') n=n*10+ *s++ -'0';
591 if(n<1 || len<n) return a;
592 for(;n!=1;--n) val=val->next;
593 return newword(val->word, a);
598 if(count(runq->argv->next->words)!=1){
599 Xerror1("variable name not singleton!");
602 s=runq->argv->next->words->word;
604 a=runq->argv->next->next->words;
606 a=subwords(v, count(v), runq->argv->words, a);
616 if(count(runq->argv->words)!=1){
617 Xerror1("variable name not singleton!");
620 s=runq->argv->words->word;
623 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
630 itoa(num, a && 1<=n && n<=count(a)?1:0);
636 if(count(runq->argv->words)!=1){
637 Xerror1("variable name must be singleton\n");
640 deglob(runq->argv->words->word);
641 runq->local=newvar(strdup(runq->argv->words->word), runq->local);
642 runq->local->val=copywords(runq->argv->next->words, (word *)0);
643 runq->local->changed=1;
648 var *v=runq->local, *hid;
649 if(v==0) panic("Xunlocal: no locals!", 0);
657 void freewords(word *w)
671 end=runq->code[runq->pc].i;
672 for(a=runq->argv->words;a;a=a->next){
674 if(v->fn) codefree(v->fn);
675 v->fn=codecopy(runq->code);
685 for(a=runq->argv->words;a;a=a->next){
687 if(v->fn) codefree(v->fn);
694 struct thread *p=runq;
695 int pc=p->pc, forkid;
696 int lfd=p->code[pc++].i;
697 int rfd=p->code[pc++].i;
700 Xerror("can't get pipe");
703 switch(forkid=fork()){
708 start(p->code, pc+2, runq->local);
711 pushredir(ROPEN, pfd[PWR], lfd);
714 start(p->code, p->code[pc].i, runq->local);
716 pushredir(ROPEN, pfd[PRD], rfd);
717 p->pc=p->code[pc+1].i;
722 char *concstatus(char *s, char *t)
724 static char v[NSTATUS+1];
726 strncpy(v, s, NSTATUS);
729 strncpy(v+n+1, t, NSTATUS-n-1);
734 void Xpipewait(void){
735 char status[NSTATUS+1];
737 setstatus(concstatus(runq->status, getstatus()));
739 strncpy(status, getstatus(), NSTATUS);
740 status[NSTATUS]='\0';
741 Waitfor(runq->pid, 1);
743 setstatus(concstatus(getstatus(), status));
747 struct thread *p=runq;
751 if(flag['s'] && !truestatus())
752 pfmt(err, "status=%v\n", vlook("status")->val);
754 prompt=vlook("prompt")->val;
756 promptstr=prompt->word;
762 if(!p->iflag || p->eof && !Eintr()){
763 if(p->cmdfile) efree(p->cmdfile);
765 Xreturn(); /* should this be omitted? */
772 --p->pc; /* go back for next command */
776 ntrap = 0; /* avoid double-interrupts during blocked writes */
777 --p->pc; /* re-execute Xrdcmds after codebuf runs */
778 start(codebuf, 1, runq->local);
784 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
785 pfmt(err, "rc: %s: %r\n", s);
787 pfmt(err, "rc (%s): %s: %r\n", argv0, s);
789 while(!runq->iflag) Xreturn();
791 void Xerror1(char *s)
793 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
794 pfmt(err, "rc: %s\n", s);
796 pfmt(err, "rc (%s): %s\n", argv0, s);
798 while(!runq->iflag) Xreturn();
803 char *s, *ewd=&wd[8192], *stop;
805 var *ifs=vlook("ifs");
809 stop=ifs->val?ifs->val->word:"";
811 Xerror("can't make pipe");
815 case -1: Xerror("try again");
821 start(runq->code, runq->pc+1, runq->local);
822 pushredir(ROPEN, pfd[PWR], 1);
829 while((c=rchr(f))!=EOF){
830 if(strchr(stop, c) || s==ewd){
845 /* v points to reversed arglist -- reverse it onto argv */
848 v->next=runq->argv->words;
852 runq->pc=runq->code[runq->pc].i;
857 * Who should wait for the exit from the fork?
860 struct thread *p=runq;
866 Xerror("can't get pipe");
869 if(p->code[pc].i==READ){
882 start(p->code, pc+2, runq->local);
884 pushredir(ROPEN, sidefd, p->code[pc].i==READ?1:0);
889 pushredir(ROPEN, mainfd, mainfd); /* isn't this a noop? */
890 strcpy(name, Fdprefix);
891 itoa(name+strlen(name), mainfd);
893 p->pc=p->code[pc+1].i;
897 void Xsubshell(void){
904 start(runq->code, runq->pc+1, runq->local);
909 runq->pc=runq->code[runq->pc].i;
913 void setstatus(char *s)
915 setvar("status", newword(s, (word *)0));
917 char *getstatus(void){
918 var *status=vlook("status");
919 return status->val?status->val->word:"";
921 int truestatus(void){
923 for(s=getstatus();*s;s++)
924 if(*s!='|' && *s!='0') return 0;
928 Unlink(runq->code[runq->pc++].s);
931 if(runq->argv->words==0){
933 runq->pc=runq->code[runq->pc].i;
936 freelist(runq->local->val);
937 runq->local->val=runq->argv->words;
938 runq->local->changed=1;
939 runq->argv->words=runq->argv->words->next;
940 runq->local->val->next=0;