7 * Start executing the given code at the given pc with the given redirection
10 void start(code *c, int pc, var *local)
12 struct thread *p=new(struct thread);
16 p->redir=p->startredir=runq?runq->redir:0;
26 word *newword(char *wd, word *next)
33 void pushword(char *wd)
35 if(runq->argv==0) panic("pushword but no argv!", 0);
36 runq->argv->words=newword(wd, runq->argv->words);
40 if(runq->argv==0) panic("popword but no argv!", 0);
42 if(p==0) panic("popword but no word!", 0);
43 runq->argv->words=p->next;
47 void freelist(word *w)
65 if(p==0) panic("poplist but no argv", 0);
73 for(n=0;w;n++) w=w->next;
76 void pushredir(int type, int from, int to){
77 redir * rp=new(redir);
84 var *newvar(char *name, var *next)
96 * get command line flags, initialize keywords & traps.
97 * get values from environment.
98 * set $pid, $cflag, $*
99 * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*)
100 * start interpreting code
103 main(int argc, char *argv[])
106 char num[12], *rcmain;
109 argc=getflags(argc, argv, "srdiIlxepvVc:1m:1[command]", 1);
110 if(argc==-1) usage("[file [arg ...]]");
111 if(argv[0][0]=='-') flag['l']=flagset;
112 if(flag['I']) flag['i'] = 0;
113 else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
114 rcmain=flag['m']?flag['m'][0]:Rcmain();
119 itoa(num, mypid=getpid());
120 setvar("pid", newword(num, (word *)0));
121 setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
123 setvar("rcname", newword(argv[0], (word *)0));
126 bootstrap[i++].f=Xmark;
127 bootstrap[i++].f=Xword;
128 bootstrap[i++].s="*";
129 bootstrap[i++].f=Xassign;
130 bootstrap[i++].f=Xmark;
131 bootstrap[i++].f=Xmark;
132 bootstrap[i++].f=Xword;
133 bootstrap[i++].s="*";
134 bootstrap[i++].f=Xdol;
135 bootstrap[i++].f=Xword;
136 bootstrap[i++].s=rcmain;
137 bootstrap[i++].f=Xword;
138 bootstrap[i++].s=".";
139 bootstrap[i++].f=Xsimple;
140 bootstrap[i++].f=Xexit;
142 start(bootstrap, 1, (var *)0);
143 /* prime bootstrap argv */
145 argv0 = strdup(argv[0]);
146 for(i=argc-1;i!=0;--i) pushword(argv[i]);
148 if(flag['r']) pfnc(err, runq);
150 (*runq->code[runq->pc-1].f)();
156 * Arguments on stack (...)
157 * Arguments in line [...]
158 * Code in line with jump around {...}
160 * Xappend(file)[fd] open file to append
161 * Xassign(name, val) assign val to name
162 * Xasync{... Xexit} make thread for {}, no wait
163 * Xbackq{... Xreturn} make thread for {}, push stdout
164 * Xbang complement condition
165 * Xcase(pat, value){...} exec code on match, leave (value) on
167 * Xclose[i] close file descriptor
168 * Xconc(left, right) concatenate, push results
169 * Xcount(name) push var count
170 * Xdelfn(name) delete function definition
171 * Xdeltraps(names) delete named traps
172 * Xdol(name) get variable value
173 * Xqdol(name) concatenate variable components
174 * Xdup[i j] dup file descriptor
175 * Xexit rc exits with status
176 * Xfalse{...} execute {} if false
177 * Xfn(name){... Xreturn} define function
178 * Xfor(var, list){... Xreturn} for loop
180 * Xlocal(name, val) create local variable, assign value
182 * Xmatch(pat, str) match pattern, set status
183 * Xpipe[i j]{... Xreturn}{... Xreturn} construct a pipe between 2 new threads,
185 * Xpipefd[type]{... Xreturn} connect {} to pipe (input or output,
186 * depending on type), push /dev/fd/??
187 * Xpopm(value) pop value from stack
188 * Xread(file)[fd] open file to read
189 * Xsettraps(names){... Xreturn} define trap functions
190 * Xshowtraps print trap list
191 * Xsimple(args) run command and wait
192 * Xreturn kill thread
193 * Xsubshell{... Xexit} execute {} in a subshell and wait
194 * Xtrue{...} execute {} if true
195 * Xunlocal delete local variable
196 * Xword[string] push string
197 * Xwrite(file)[fd] open file to write
202 switch(count(runq->argv->words)){
203 default: Xerror1(">> requires singleton"); return;
204 case 0: Xerror1(">> requires file"); return;
207 file=runq->argv->words->word;
208 if((f=open(file, 1))<0 && (f=Creat(file))<0){
209 pfmt(err, "%s: ", file);
210 Xerror("can't open");
214 pushredir(ROPEN, f, runq->code[runq->pc].i);
219 int null=open("/dev/null", 0);
223 Xerror("Can't open /dev/null\n");
226 switch(pid=rfork(RFFDG|RFPROC|RFNOTEG)){
232 pushredir(ROPEN, null, 0);
233 start(runq->code, runq->pc+1, runq->local);
238 runq->pc=runq->code[runq->pc].i;
240 setvar("apid", newword(npid, (word *)0));
248 setstatus(truestatus()?"false":"");
251 pushredir(RCLOSE, runq->code[runq->pc].i, 0);
255 pushredir(RDUP, runq->code[runq->pc].i, runq->code[runq->pc+1].i);
259 if(eflagok && !truestatus()) Xexit();
263 struct word *starval;
264 static int beenhere=0;
265 if(getpid()==mypid && !beenhere){
266 trapreq=vlook("sigexit");
270 starval=vlook("*")->val;
271 start(trapreq->fn, trapreq->pc, (struct var *)0);
272 runq->local=newvar(strdup("*"), runq->local);
273 runq->local->val=copywords(starval, (struct word *)0);
274 runq->local->changed=1;
275 runq->redir=runq->startredir=0;
282 if(truestatus()) runq->pc=runq->code[runq->pc].i;
285 int ifnot; /* dynamic if not flag */
290 runq->pc=runq->code[runq->pc].i;
293 runq->pc=runq->code[runq->pc].i;
304 switch(count(runq->argv->words)){
305 default: Xerror1("< requires singleton\n"); return;
306 case 0: Xerror1("< requires file\n"); return;
309 file=runq->argv->words->word;
310 if((f=open(file, 0))<0){
311 pfmt(err, "%s: ", file);
312 Xerror("can't open");
315 pushredir(ROPEN, f, runq->code[runq->pc].i);
319 void turfredir(void){
320 while(runq->redir!=runq->startredir)
323 void Xpopredir(void){
324 struct redir *rp=runq->redir;
325 if(rp==0) panic("turfredir null!", 0);
326 runq->redir=rp->next;
327 if(rp->type==ROPEN) close(rp->from);
331 struct thread *p=runq;
333 while(p->argv) poplist();
337 if(runq==0) Exit(getstatus());
340 if(truestatus()) runq->pc++;
341 else runq->pc=runq->code[runq->pc].i;
345 if(truestatus()) runq->pc++;
346 else runq->pc=runq->code[runq->pc].i;
352 pushword(runq->code[runq->pc++].s);
357 switch(count(runq->argv->words)){
358 default: Xerror1("> requires singleton\n"); return;
359 case 0: Xerror1("> requires file\n"); return;
362 file=runq->argv->words->word;
363 if((f=Creat(file))<0){
364 pfmt(err, "%s: ", file);
365 Xerror("can't open");
368 pushredir(ROPEN, f, runq->code[runq->pc].i);
372 char *list2str(word *words){
376 for(ap=words;ap;ap=ap->next)
377 len+=1+strlen(ap->word);
378 value=emalloc(len+1);
380 for(ap=words;ap;ap=ap->next){
381 for(t=ap->word;*t;) *s++=*t++;
384 if(s==value) *s='\0';
391 subject=list2str(runq->argv->words);
392 setstatus("no match");
393 for(p=runq->argv->next->words;p;p=p->next)
394 if(match(subject, p->word, '\0')){
406 s=list2str(runq->argv->next->words);
407 for(p=runq->argv->words;p;p=p->next){
408 if(match(s, p->word, '\0')){
417 runq->pc=runq->code[runq->pc].i;
420 word *conclist(word *lp, word *rp, word *tail)
424 if(lp->next || rp->next)
425 tail=conclist(lp->next==0?lp:lp->next, rp->next==0?rp:rp->next,
427 buf=emalloc(strlen(lp->word)+strlen(rp->word)+1);
428 strcpy(buf, lp->word);
429 strcat(buf, rp->word);
430 v=newword(buf, tail);
435 word *lp=runq->argv->words;
436 word *rp=runq->argv->next->words;
437 word *vp=runq->argv->next->next->words;
438 int lc=count(lp), rc=count(rp);
441 Xerror1("null list in concatenation");
444 if(lc!=1 && rc!=1 && lc!=rc){
445 Xerror1("mismatched list lengths in concatenation");
448 vp=conclist(lp, rp, vp);
452 runq->argv->words=vp;
456 if(count(runq->argv->words)!=1){
457 Xerror1("variable name not singleton!");
460 deglob(runq->argv->words->word);
461 v=vlook(runq->argv->words->word);
465 v->val=runq->argv->words;
471 * copy arglist a, adding the copy to the front of tail
473 word *copywords(word *a, word *tail)
476 for(end=&v;a;a=a->next,end=&(*end)->next)
477 *end=newword(a->word, 0);
485 if(count(runq->argv->words)!=1){
486 Xerror1("variable name not singleton!");
489 s=runq->argv->words->word;
492 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
493 a=runq->argv->next->words;
495 a=copywords(vlook(s)->val, a);
497 star=vlook("*")->val;
498 if(star && 1<=n && n<=count(star)){
499 while(--n) star=star->next;
500 a=newword(star->word, a);
510 if(count(runq->argv->words)!=1){
511 Xerror1("variable name not singleton!");
514 s=runq->argv->words->word;
523 for(p=a;p;p=p->next) n+=strlen(p->word);
527 for(p=a->next;p;p=p->next){
537 word *subwords(word *val, int len, word *sub, word *a)
542 a=subwords(val, len, sub->next, a);
546 while('0'<=*s && *s<='9') n=n*10+ *s++ -'0';
547 if(n<1 || len<n) return a;
548 for(;n!=1;--n) val=val->next;
549 return newword(val->word, a);
554 if(count(runq->argv->next->words)!=1){
555 Xerror1("variable name not singleton!");
558 s=runq->argv->next->words->word;
560 a=runq->argv->next->next->words;
562 a=subwords(v, count(v), runq->argv->words, a);
572 if(count(runq->argv->words)!=1){
573 Xerror1("variable name not singleton!");
576 s=runq->argv->words->word;
579 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
586 itoa(num, a && 1<=n && n<=count(a)?1:0);
592 if(count(runq->argv->words)!=1){
593 Xerror1("variable name must be singleton\n");
596 deglob(runq->argv->words->word);
597 runq->local=newvar(strdup(runq->argv->words->word), runq->local);
598 runq->local->val=copywords(runq->argv->next->words, (word *)0);
599 runq->local->changed=1;
604 var *v=runq->local, *hid;
605 if(v==0) panic("Xunlocal: no locals!", 0);
613 void freewords(word *w)
627 end=runq->code[runq->pc].i;
628 for(a=runq->argv->words;a;a=a->next){
630 if(v->fn) codefree(v->fn);
631 v->fn=codecopy(runq->code);
641 for(a=runq->argv->words;a;a=a->next){
643 if(v->fn) codefree(v->fn);
650 struct thread *p=runq;
651 int pc=p->pc, forkid;
652 int lfd=p->code[pc++].i;
653 int rfd=p->code[pc++].i;
656 Xerror("can't get pipe");
659 switch(forkid=fork()){
664 start(p->code, pc+2, runq->local);
667 pushredir(ROPEN, pfd[PWR], lfd);
670 start(p->code, p->code[pc].i, runq->local);
672 pushredir(ROPEN, pfd[PRD], rfd);
673 p->pc=p->code[pc+1].i;
678 char *concstatus(char *s, char *t)
680 static char v[NSTATUS+1];
682 strncpy(v, s, NSTATUS);
685 strncpy(v+n+1, t, NSTATUS-n-1);
690 void Xpipewait(void){
691 char status[NSTATUS+1];
693 setstatus(concstatus(runq->status, getstatus()));
695 strncpy(status, getstatus(), NSTATUS);
696 status[NSTATUS]='\0';
697 Waitfor(runq->pid, 1);
699 setstatus(concstatus(getstatus(), status));
703 struct thread *p=runq;
707 if(flag['s'] && !truestatus())
708 pfmt(err, "status=%v\n", vlook("status")->val);
710 prompt=vlook("prompt")->val;
712 promptstr=prompt->word;
718 if(!p->iflag || p->eof && !Eintr()){
719 if(p->cmdfile) efree(p->cmdfile);
721 Xreturn(); /* should this be omitted? */
728 --p->pc; /* go back for next command */
732 ntrap = 0; /* avoid double-interrupts during blocked writes */
733 --p->pc; /* re-execute Xrdcmds after codebuf runs */
734 start(codebuf, 1, runq->local);
740 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
741 pfmt(err, "rc: %s: %r\n", s);
743 pfmt(err, "rc (%s): %s: %r\n", argv0, s);
745 while(!runq->iflag) Xreturn();
747 void Xerror1(char *s)
749 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
750 pfmt(err, "rc: %s\n", s);
752 pfmt(err, "rc (%s): %s\n", argv0, s);
754 while(!runq->iflag) Xreturn();
759 char *s, *ewd=&wd[8192], *stop;
761 var *ifs=vlook("ifs");
765 stop=ifs->val?ifs->val->word:"";
767 Xerror("can't make pipe");
771 case -1: Xerror("try again");
777 start(runq->code, runq->pc+1, runq->local);
778 pushredir(ROPEN, pfd[PWR], 1);
785 while((c=rchr(f))!=EOF){
786 if(strchr(stop, c) || s==ewd){
801 /* v points to reversed arglist -- reverse it onto argv */
804 v->next=runq->argv->words;
808 runq->pc=runq->code[runq->pc].i;
813 * Who should wait for the exit from the fork?
816 struct thread *p=runq;
822 Xerror("can't get pipe");
825 if(p->code[pc].i==READ){
838 start(p->code, pc+2, runq->local);
840 pushredir(ROPEN, sidefd, p->code[pc].i==READ?1:0);
845 pushredir(ROPEN, mainfd, mainfd); /* isn't this a noop? */
846 strcpy(name, Fdprefix);
847 itoa(name+strlen(name), mainfd);
849 p->pc=p->code[pc+1].i;
853 void Xsubshell(void){
860 start(runq->code, runq->pc+1, runq->local);
865 runq->pc=runq->code[runq->pc].i;
869 void setstatus(char *s)
871 setvar("status", newword(s, (word *)0));
873 char *getstatus(void){
874 var *status=vlook("status");
875 return status->val?status->val->word:"";
877 int truestatus(void){
879 for(s=getstatus();*s;s++)
880 if(*s!='|' && *s!='0') return 0;
884 Unlink(runq->code[runq->pc++].s);
887 if(runq->argv->words==0){
889 runq->pc=runq->code[runq->pc].i;
892 freelist(runq->local->val);
893 runq->local->val=runq->argv->words;
894 runq->local->changed=1;
895 runq->argv->words=runq->argv->words->next;
896 runq->local->val->next=0;