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;
27 word *newword(char *wd, word *next)
34 void pushword(char *wd)
36 if(runq->argv==0) panic("pushword but no argv!", 0);
37 runq->argv->words=newword(wd, runq->argv->words);
41 if(runq->argv==0) panic("popword but no argv!", 0);
43 if(p==0) panic("popword but no word!", 0);
44 runq->argv->words=p->next;
48 void freelist(word *w)
66 if(p==0) panic("poplist but no argv", 0);
74 for(n=0;w;n++) w=w->next;
77 void pushredir(int type, int from, int to){
78 redir * rp=new(redir);
85 var *newvar(char *name, var *next)
98 * get command line flags, initialize keywords & traps.
99 * get values from environment.
100 * set $pid, $cflag, $*
101 * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*)
102 * start interpreting code
105 main(int argc, char *argv[])
108 char num[12], *rcmain;
111 argc=getflags(argc, argv, "srdiIlxepvVc:1m:1[command]", 1);
112 if(argc==-1) usage("[file [arg ...]]");
113 if(argv[0][0]=='-') flag['l']=flagset;
114 if(flag['I']) flag['i'] = 0;
115 else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
116 rcmain=flag['m']?flag['m'][0]:Rcmain();
121 itoa(num, mypid=getpid());
123 setvar("pid", newword(num, (word *)0));
124 setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
126 setvar("rcname", newword(argv[0], (word *)0));
129 bootstrap[i++].f=Xmark;
130 bootstrap[i++].f=Xword;
131 bootstrap[i++].s="*";
132 bootstrap[i++].f=Xassign;
133 bootstrap[i++].f=Xmark;
134 bootstrap[i++].f=Xmark;
135 bootstrap[i++].f=Xword;
136 bootstrap[i++].s="*";
137 bootstrap[i++].f=Xdol;
138 bootstrap[i++].f=Xword;
139 bootstrap[i++].s=rcmain;
140 bootstrap[i++].f=Xword;
141 bootstrap[i++].s=".";
142 bootstrap[i++].f=Xsimple;
143 bootstrap[i++].f=Xexit;
145 start(bootstrap, 1, (var *)0);
146 /* prime bootstrap argv */
148 argv0 = strdup(argv[0]);
149 for(i=argc-1;i!=0;--i) pushword(argv[i]);
151 if(flag['r']) pfnc(err, runq);
153 (*runq->code[runq->pc-1].f)();
159 * Arguments on stack (...)
160 * Arguments in line [...]
161 * Code in line with jump around {...}
163 * Xappend(file)[fd] open file to append
164 * Xassign(name, val) assign val to name
165 * Xasync{... Xexit} make thread for {}, no wait
166 * Xbackq{... Xreturn} make thread for {}, push stdout
167 * Xbang complement condition
168 * Xcase(pat, value){...} exec code on match, leave (value) on
170 * Xclose[i] close file descriptor
171 * Xconc(left, right) concatenate, push results
172 * Xcount(name) push var count
173 * Xdelfn(name) delete function definition
174 * Xdeltraps(names) delete named traps
175 * Xdol(name) get variable value
176 * Xqdol(name) concatenate variable components
177 * Xdup[i j] dup file descriptor
178 * Xexit rc exits with status
179 * Xfalse{...} execute {} if false
180 * Xfn(name){... Xreturn} define function
181 * Xfor(var, list){... Xreturn} for loop
183 * Xlocal(name, val) create local variable, assign value
185 * Xmatch(pat, str) match pattern, set status
186 * Xpipe[i j]{... Xreturn}{... Xreturn} construct a pipe between 2 new threads,
188 * Xpipefd[type]{... Xreturn} connect {} to pipe (input or output,
189 * depending on type), push /dev/fd/??
190 * Xpopm(value) pop value from stack
191 * Xread(file)[fd] open file to read
192 * Xsettraps(names){... Xreturn} define trap functions
193 * Xshowtraps print trap list
194 * Xsimple(args) run command and wait
195 * Xreturn kill thread
196 * Xsubshell{... Xexit} execute {} in a subshell and wait
197 * Xtrue{...} execute {} if true
198 * Xunlocal delete local variable
199 * Xword[string] push string
200 * Xwrite(file)[fd] open file to write
205 switch(count(runq->argv->words)){
206 default: Xerror1(">> requires singleton"); return;
207 case 0: Xerror1(">> requires file"); return;
210 file=runq->argv->words->word;
211 if((f=open(file, 1))<0 && (f=Creat(file))<0){
212 pfmt(err, "%s: ", file);
213 Xerror("can't open");
217 pushredir(ROPEN, f, runq->code[runq->pc].i);
222 int null=open("/dev/null", 0);
226 Xerror("Can't open /dev/null\n");
229 switch(pid=rfork(RFFDG|RFPROC|RFNOTEG)){
235 pushredir(ROPEN, null, 0);
236 start(runq->code, runq->pc+1, runq->local);
241 runq->pc=runq->code[runq->pc].i;
243 setvar("apid", newword(npid, (word *)0));
251 setstatus(truestatus()?"false":"");
254 pushredir(RCLOSE, runq->code[runq->pc].i, 0);
258 pushredir(RDUP, runq->code[runq->pc].i, runq->code[runq->pc+1].i);
262 if(eflagok && !truestatus()) Xexit();
266 struct word *starval;
267 static int beenhere=0;
268 if(getpid()==mypid && !beenhere){
269 trapreq=vlook("sigexit");
273 starval=vlook("*")->val;
274 start(trapreq->fn, trapreq->pc, (struct var *)0);
275 runq->local=newvar(strdup("*"), runq->local);
276 runq->local->val=copywords(starval, (struct word *)0);
277 runq->local->changed=1;
278 runq->redir=runq->startredir=0;
285 if(truestatus()) runq->pc=runq->code[runq->pc].i;
288 int ifnot; /* dynamic if not flag */
293 runq->pc=runq->code[runq->pc].i;
296 runq->pc=runq->code[runq->pc].i;
307 switch(count(runq->argv->words)){
308 default: Xerror1("< requires singleton\n"); return;
309 case 0: Xerror1("< requires file\n"); return;
312 file=runq->argv->words->word;
313 if((f=open(file, 0))<0){
314 pfmt(err, "%s: ", file);
315 Xerror("can't open");
318 pushredir(ROPEN, f, runq->code[runq->pc].i);
322 void turfredir(void){
323 while(runq->redir!=runq->startredir)
326 void Xpopredir(void){
327 struct redir *rp=runq->redir;
328 if(rp==0) panic("turfredir null!", 0);
329 runq->redir=rp->next;
330 if(rp->type==ROPEN) close(rp->from);
334 struct thread *p=runq;
336 while(p->argv) poplist();
340 if(runq==0) Exit(getstatus());
343 if(truestatus()) runq->pc++;
344 else runq->pc=runq->code[runq->pc].i;
348 if(truestatus()) runq->pc++;
349 else runq->pc=runq->code[runq->pc].i;
355 pushword(runq->code[runq->pc++].s);
360 switch(count(runq->argv->words)){
361 default: Xerror1("> requires singleton\n"); return;
362 case 0: Xerror1("> requires file\n"); return;
365 file=runq->argv->words->word;
366 if((f=Creat(file))<0){
367 pfmt(err, "%s: ", file);
368 Xerror("can't open");
371 pushredir(ROPEN, f, runq->code[runq->pc].i);
375 char *_list2str(word *words, int c){
379 for(ap=words;ap;ap=ap->next)
380 len+=1+strlen(ap->word);
381 value=emalloc(len+1);
383 for(ap=words;ap;ap=ap->next){
384 for(t=ap->word;*t;) *s++=*t++;
387 if(s==value) *s='\0';
391 char *list2str(word *words){
392 return _list2str(words, ' ');
397 subject=list2str(runq->argv->words);
398 setstatus("no match");
399 for(p=runq->argv->next->words;p;p=p->next)
400 if(match(subject, p->word, '\0')){
412 s=list2str(runq->argv->next->words);
413 for(p=runq->argv->words;p;p=p->next){
414 if(match(s, p->word, '\0')){
423 runq->pc=runq->code[runq->pc].i;
426 word *conclist(word *lp, word *rp, word *tail)
430 if(lp->next || rp->next)
431 tail=conclist(lp->next==0?lp:lp->next, rp->next==0?rp:rp->next,
433 buf=emalloc(strlen(lp->word)+strlen(rp->word)+1);
434 strcpy(buf, lp->word);
435 strcat(buf, rp->word);
436 v=newword(buf, tail);
441 word *lp=runq->argv->words;
442 word *rp=runq->argv->next->words;
443 word *vp=runq->argv->next->next->words;
444 int lc=count(lp), rc=count(rp);
447 Xerror1("null list in concatenation");
450 if(lc!=1 && rc!=1 && lc!=rc){
451 Xerror1("mismatched list lengths in concatenation");
454 vp=conclist(lp, rp, vp);
458 runq->argv->words=vp;
462 if(count(runq->argv->words)!=1){
463 Xerror1("variable name not singleton!");
466 deglob(runq->argv->words->word);
467 v=vlook(runq->argv->words->word);
471 v->val=runq->argv->words;
479 * copy arglist a, adding the copy to the front of tail
481 word *copywords(word *a, word *tail)
484 for(end=&v;a;a=a->next,end=&(*end)->next)
485 *end=newword(a->word, 0);
493 if(count(runq->argv->words)!=1){
494 Xerror1("variable name not singleton!");
497 s=runq->argv->words->word;
500 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
501 a=runq->argv->next->words;
503 a=copywords(vlook(s)->val, a);
505 star=vlook("*")->val;
506 if(star && 1<=n && n<=count(star)){
507 while(--n) star=star->next;
508 a=newword(star->word, a);
518 if(count(runq->argv->words)!=1){
519 Xerror1("variable name not singleton!");
522 s=runq->argv->words->word;
531 for(p=a;p;p=p->next) n+=strlen(p->word);
535 for(p=a->next;p;p=p->next){
545 word *subwords(word *val, int len, word *sub, word *a)
550 a=subwords(val, len, sub->next, a);
554 while('0'<=*s && *s<='9') n=n*10+ *s++ -'0';
555 if(n<1 || len<n) return a;
556 for(;n!=1;--n) val=val->next;
557 return newword(val->word, a);
562 if(count(runq->argv->next->words)!=1){
563 Xerror1("variable name not singleton!");
566 s=runq->argv->next->words->word;
568 a=runq->argv->next->next->words;
570 a=subwords(v, count(v), runq->argv->words, a);
580 if(count(runq->argv->words)!=1){
581 Xerror1("variable name not singleton!");
584 s=runq->argv->words->word;
587 for(t=s;'0'<=*t && *t<='9';t++) n=n*10+*t-'0';
594 itoa(num, a && 1<=n && n<=count(a)?1:0);
600 if(count(runq->argv->words)!=1){
601 Xerror1("variable name must be singleton\n");
604 deglob(runq->argv->words->word);
605 runq->local=newvar(strdup(runq->argv->words->word), runq->local);
606 runq->local->val=copywords(runq->argv->next->words, (word *)0);
607 runq->local->changed=1;
612 var *v=runq->local, *hid;
613 if(v==0) panic("Xunlocal: no locals!", 0);
621 void freewords(word *w)
635 end=runq->code[runq->pc].i;
636 for(a=runq->argv->words;a;a=a->next){
638 if(v->fn) codefree(v->fn);
639 v->fn=codecopy(runq->code);
649 for(a=runq->argv->words;a;a=a->next){
651 if(v->fn) codefree(v->fn);
658 struct thread *p=runq;
659 int pc=p->pc, forkid;
660 int lfd=p->code[pc++].i;
661 int rfd=p->code[pc++].i;
664 Xerror("can't get pipe");
667 switch(forkid=fork()){
672 start(p->code, pc+2, runq->local);
675 pushredir(ROPEN, pfd[PWR], lfd);
678 start(p->code, p->code[pc].i, runq->local);
680 pushredir(ROPEN, pfd[PRD], rfd);
681 p->pc=p->code[pc+1].i;
686 char *concstatus(char *s, char *t)
688 static char v[NSTATUS+1];
690 strncpy(v, s, NSTATUS);
693 strncpy(v+n+1, t, NSTATUS-n-1);
698 void Xpipewait(void){
699 char status[NSTATUS+1];
701 setstatus(concstatus(runq->status, getstatus()));
703 strncpy(status, getstatus(), NSTATUS);
704 status[NSTATUS]='\0';
705 Waitfor(runq->pid, 1);
707 setstatus(concstatus(getstatus(), status));
711 struct thread *p=runq;
715 if(flag['s'] && !truestatus())
716 pfmt(err, "status=%v\n", vlook("status")->val);
718 prompt=vlook("prompt")->val;
720 promptstr=prompt->word;
726 if(!p->iflag || p->eof && !Eintr()){
727 if(p->cmdfile) efree(p->cmdfile);
729 Xreturn(); /* should this be omitted? */
736 --p->pc; /* go back for next command */
740 ntrap = 0; /* avoid double-interrupts during blocked writes */
741 --p->pc; /* re-execute Xrdcmds after codebuf runs */
742 start(codebuf, 1, runq->local);
748 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
749 pfmt(err, "rc: %s: %r\n", s);
751 pfmt(err, "rc (%s): %s: %r\n", argv0, s);
753 while(!runq->iflag) Xreturn();
755 void Xerror1(char *s)
757 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
758 pfmt(err, "rc: %s\n", s);
760 pfmt(err, "rc (%s): %s\n", argv0, s);
762 while(!runq->iflag) Xreturn();
767 char *s, *ewd=&wd[8192], *stop;
769 var *ifs=vlook("ifs");
773 stop=ifs->val?ifs->val->word:"";
775 Xerror("can't make pipe");
779 case -1: Xerror("try again");
785 start(runq->code, runq->pc+1, runq->local);
786 pushredir(ROPEN, pfd[PWR], 1);
793 while((c=rchr(f))!=EOF){
794 if(strchr(stop, c) || s==ewd){
809 /* v points to reversed arglist -- reverse it onto argv */
812 v->next=runq->argv->words;
816 runq->pc=runq->code[runq->pc].i;
821 * Who should wait for the exit from the fork?
824 struct thread *p=runq;
830 Xerror("can't get pipe");
833 if(p->code[pc].i==READ){
846 start(p->code, pc+2, runq->local);
848 pushredir(ROPEN, sidefd, p->code[pc].i==READ?1:0);
853 pushredir(ROPEN, mainfd, mainfd); /* isn't this a noop? */
854 strcpy(name, Fdprefix);
855 itoa(name+strlen(name), mainfd);
857 p->pc=p->code[pc+1].i;
861 void Xsubshell(void){
868 start(runq->code, runq->pc+1, runq->local);
873 runq->pc=runq->code[runq->pc].i;
877 void setstatus(char *s)
879 setvar("status", newword(s, (word *)0));
881 char *getstatus(void){
882 var *status=vlook("status");
883 return status->val?status->val->word:"";
885 int truestatus(void){
887 for(s=getstatus();*s;s++)
888 if(*s!='|' && *s!='0') return 0;
892 Unlink(runq->code[runq->pc++].s);
895 if(runq->argv->words==0){
897 runq->pc=runq->code[runq->pc].i;
900 freelist(runq->local->val);
901 runq->local->val=runq->argv->words;
902 runq->local->changed=1;
903 runq->argv->words=runq->argv->words->next;
904 runq->local->val->next=0;