7 * Start executing the given code at the given pc with the given redirection
12 start(code *c, int pc, var *local)
14 struct thread *p = new(struct thread);
16 p->code = codecopy(c);
19 p->redir = p->startredir = runq?runq->redir:0;
31 newword(char *wd, word *next)
43 panic("pushword but no argv!", 0);
44 runq->argv->words = newword(wd, runq->argv->words);
52 panic("popword but no argv!", 0);
53 p = runq->argv->words;
55 panic("popword but no word!", 0);
56 runq->argv->words = p->next;
87 panic("poplist but no argv", 0);
97 for(n = 0;w;n++) w = w->next;
102 pushredir(int type, int from, int to)
104 redir * rp = new(redir);
108 rp->next = runq->redir;
113 newvar(char *name, var *next)
126 * get command line flags, initialize keywords & traps.
127 * get values from environment.
128 * set $pid, $cflag, $*
129 * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*)
130 * start interpreting code
133 main(int argc, char *argv[])
136 char num[12], *rcmain;
139 /* needed for rcmain later */
140 putenv("PLAN9", unsharp("#9"));
142 argc = getflags(argc, argv, "SsrdiIlxepvVc:1m:1[command]", 1);
144 usage("[file [arg ...]]");
149 else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
150 rcmain = flag['m'] ? flag['m'][0] : Rcmain();
155 inttoascii(num, mypid = getpid());
157 setvar("pid", newword(num, (word *)0));
158 setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
160 setvar("rcname", newword(argv[0], (word *)0));
162 bootstrap[i++].i = 1;
163 bootstrap[i++].f = Xmark;
164 bootstrap[i++].f = Xword;
165 bootstrap[i++].s="*";
166 bootstrap[i++].f = Xassign;
167 bootstrap[i++].f = Xmark;
168 bootstrap[i++].f = Xmark;
169 bootstrap[i++].f = Xword;
170 bootstrap[i++].s="*";
171 bootstrap[i++].f = Xdol;
172 bootstrap[i++].f = Xword;
173 bootstrap[i++].s = rcmain;
174 bootstrap[i++].f = Xword;
175 bootstrap[i++].s=".";
176 bootstrap[i++].f = Xsimple;
177 bootstrap[i++].f = Xexit;
179 start(bootstrap, 1, (var *)0);
180 /* prime bootstrap argv */
182 argv0 = strdup(argv[0]);
183 for(i = argc-1;i!=0;--i) pushword(argv[i]);
188 (*runq->code[runq->pc-1].f)();
195 * Arguments on stack (...)
196 * Arguments in line [...]
197 * Code in line with jump around {...}
199 * Xappend(file)[fd] open file to append
200 * Xassign(name, val) assign val to name
201 * Xasync{... Xexit} make thread for {}, no wait
202 * Xbackq{... Xreturn} make thread for {}, push stdout
203 * Xbang complement condition
204 * Xcase(pat, value){...} exec code on match, leave (value) on
206 * Xclose[i] close file descriptor
207 * Xconc(left, right) concatenate, push results
208 * Xcount(name) push var count
209 * Xdelfn(name) delete function definition
210 * Xdeltraps(names) delete named traps
211 * Xdol(name) get variable value
212 * Xqdol(name) concatenate variable components
213 * Xdup[i j] dup file descriptor
214 * Xexit rc exits with status
215 * Xfalse{...} execute {} if false
216 * Xfn(name){... Xreturn} define function
217 * Xfor(var, list){... Xreturn} for loop
219 * Xlocal(name, val) create local variable, assign value
221 * Xmatch(pat, str) match pattern, set status
222 * Xpipe[i j]{... Xreturn}{... Xreturn} construct a pipe between 2 new threads,
224 * Xpipefd[type]{... Xreturn} connect {} to pipe (input or output,
225 * depending on type), push /dev/fd/??
226 * Xpopm(value) pop value from stack
227 * Xrdwr(file)[fd] open file for reading and writing
228 * Xread(file)[fd] open file to read
229 * Xsettraps(names){... Xreturn} define trap functions
230 * Xshowtraps print trap list
231 * Xsimple(args) run command and wait
232 * Xreturn kill thread
233 * Xsubshell{... Xexit} execute {} in a subshell and wait
234 * Xtrue{...} execute {} if true
235 * Xunlocal delete local variable
236 * Xword[string] push string
237 * Xwrite(file)[fd] open file to write
245 switch(count(runq->argv->words)){
247 Xerror1(">> requires singleton");
250 Xerror1(">> requires file");
255 file = runq->argv->words->word;
256 if((f = open(file, 1))<0 && (f = Creat(file))<0){
257 pfmt(err, "%s: ", file);
258 Xerror("can't open");
262 pushredir(ROPEN, f, runq->code[runq->pc].i);
276 setstatus(truestatus()?"false":"");
282 pushredir(RCLOSE, runq->code[runq->pc].i, 0);
289 pushredir(RDUP, runq->code[runq->pc].i, runq->code[runq->pc+1].i);
296 if(eflagok && !truestatus()) Xexit();
303 struct word *starval;
304 static int beenhere = 0;
305 if(getpid()==mypid && !beenhere){
306 trapreq = vlook("sigexit");
310 starval = vlook("*")->val;
311 start(trapreq->fn, trapreq->pc, (struct var *)0);
312 runq->local = newvar(strdup("*"), runq->local);
313 runq->local->val = copywords(starval, (struct word *)0);
314 runq->local->changed = 1;
315 runq->redir = runq->startredir = 0;
325 if(truestatus()) runq->pc = runq->code[runq->pc].i;
328 int ifnot; /* dynamic if not flag */
336 runq->pc = runq->code[runq->pc].i;
342 runq->pc = runq->code[runq->pc].i;
362 switch(count(runq->argv->words)){
364 Xerror1("< requires singleton\n");
367 Xerror1("< requires file\n");
372 file = runq->argv->words->word;
373 if((f = open(file, 0))<0){
374 pfmt(err, "%s: ", file);
375 Xerror("can't open");
378 pushredir(ROPEN, f, runq->code[runq->pc].i);
389 switch(count(runq->argv->words)){
391 Xerror1("<> requires singleton\n");
394 Xerror1("<> requires file\n");
399 file = runq->argv->words->word;
400 if((f = open(file, ORDWR))<0){
401 pfmt(err, "%s: ", file);
402 Xerror("can't open");
405 pushredir(ROPEN, f, runq->code[runq->pc].i);
413 while(runq->redir!=runq->startredir)
420 struct redir *rp = runq->redir;
422 panic("turfredir null!", 0);
423 runq->redir = rp->next;
432 struct thread *p = runq;
434 while(p->argv) poplist();
445 if(truestatus()) runq->pc++;
446 else runq->pc = runq->code[runq->pc].i;
453 if(truestatus()) runq->pc++;
454 else runq->pc = runq->code[runq->pc].i;
466 pushword(runq->code[runq->pc++].s);
474 switch(count(runq->argv->words)){
476 Xerror1("> requires singleton\n");
479 Xerror1("> requires file\n");
484 file = runq->argv->words->word;
485 if((f = Creat(file))<0){
486 pfmt(err, "%s: ", file);
487 Xerror("can't open");
490 pushredir(ROPEN, f, runq->code[runq->pc].i);
496 list2str(word *words)
501 for(ap = words;ap;ap = ap->next)
502 len+=1+strlen(ap->word);
503 value = emalloc(len+1);
505 for(ap = words;ap;ap = ap->next){
506 for(t = ap->word;*t;) *s++=*t++;
520 subject = list2str(runq->argv->words);
521 setstatus("no match");
522 for(p = runq->argv->next->words;p;p = p->next)
523 if(match(subject, p->word, '\0')){
538 s = list2str(runq->argv->next->words);
539 for(p = runq->argv->words;p;p = p->next){
540 if(match(s, p->word, '\0')){
549 runq->pc = runq->code[runq->pc].i;
554 conclist(word *lp, word *rp, word *tail)
558 if(lp->next || rp->next)
559 tail = conclist(lp->next==0?lp:lp->next, rp->next==0?rp:rp->next,
561 buf = emalloc(strlen(lp->word)+strlen(rp->word)+1);
562 strcpy(buf, lp->word);
563 strcat(buf, rp->word);
564 v = newword(buf, tail);
572 word *lp = runq->argv->words;
573 word *rp = runq->argv->next->words;
574 word *vp = runq->argv->next->next->words;
575 int lc = count(lp), rc = count(rp);
578 Xerror1("null list in concatenation");
581 if(lc!=1 && rc!=1 && lc!=rc){
582 Xerror1("mismatched list lengths in concatenation");
585 vp = conclist(lp, rp, vp);
589 runq->argv->words = vp;
596 if(count(runq->argv->words)!=1){
597 Xerror1("variable name not singleton!");
600 deglob(runq->argv->words->word);
601 v = vlook(runq->argv->words->word);
605 v->val = runq->argv->words;
609 runq->argv->words = 0;
613 * copy arglist a, adding the copy to the front of tail
617 copywords(word *a, word *tail)
620 for(end=&v;a;a = a->next,end=&(*end)->next)
621 *end = newword(a->word, 0);
632 if(count(runq->argv->words)!=1){
633 Xerror1("variable name not singleton!");
636 s = runq->argv->words->word;
639 for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
640 a = runq->argv->next->words;
642 a = copywords(vlook(s)->val, a);
644 star = vlook("*")->val;
645 if(star && 1<=n && n<=count(star)){
646 while(--n) star = star->next;
647 a = newword(star->word, a);
651 runq->argv->words = a;
660 if(count(runq->argv->words)!=1){
661 Xerror1("variable name not singleton!");
664 s = runq->argv->words->word;
673 for(p = a;p;p = p->next) n+=strlen(p->word);
677 for(p = a->next;p;p = p->next){
689 copynwords(word *a, word *tail, int n)
696 *end = newword(a->word, 0);
705 subwords(word *val, int len, word *sub, word *a)
711 a = subwords(val, len, sub->next, a);
716 while('0'<=*s && *s<='9')
722 while('0'<=*s && *s<='9')
727 if(n<1 || n>len || m<0)
733 return copynwords(val, a, m+1);
741 if(count(runq->argv->next->words)!=1){
742 Xerror1("variable name not singleton!");
745 s = runq->argv->next->words->word;
747 a = runq->argv->next->next->words;
749 a = subwords(v, count(v), runq->argv->words, a);
752 runq->argv->words = a;
762 if(count(runq->argv->words)!=1){
763 Xerror1("variable name not singleton!");
766 s = runq->argv->words->word;
769 for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
772 inttoascii(num, count(a));
776 inttoascii(num, a && 1<=n && n<=count(a)?1:0);
785 if(count(runq->argv->words)!=1){
786 Xerror1("variable name must be singleton\n");
789 deglob(runq->argv->words->word);
790 runq->local = newvar(strdup(runq->argv->words->word), runq->local);
791 runq->local->val = copywords(runq->argv->next->words, (word *)0);
792 runq->local->changed = 1;
800 var *v = runq->local, *hid;
802 panic("Xunlocal: no locals!", 0);
803 runq->local = v->next;
804 hid = vlook(v->name);
829 end = runq->code[runq->pc].i;
830 for(a = runq->argv->words;a;a = a->next){
834 v->fn = codecopy(runq->code);
847 for(a = runq->argv->words;a;a = a->next){
858 concstatus(char *s, char *t)
860 static char v[NSTATUS+1];
862 strncpy(v, s, NSTATUS);
865 strncpy(v+n+1, t, NSTATUS-n-1);
874 char status[NSTATUS+1];
876 setstatus(concstatus(runq->status, getstatus()));
878 strncpy(status, getstatus(), NSTATUS);
879 status[NSTATUS]='\0';
880 Waitfor(runq->pid, 1);
882 setstatus(concstatus(getstatus(), status));
889 struct thread *p = runq;
893 if(flag['s'] && !truestatus())
894 pfmt(err, "status=%v\n", vlook("status")->val);
896 prompt = vlook("prompt")->val;
898 promptstr = prompt->word;
904 if(!p->iflag || p->eof && !Eintr()){
908 Xreturn(); /* should this be omitted? */
915 --p->pc; /* go back for next command */
919 ntrap = 0; /* avoid double-interrupts during blocked writes */
920 --p->pc; /* re-execute Xrdcmds after codebuf runs */
921 start(codebuf, 1, runq->local);
929 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
930 pfmt(err, "rc: %s: %r\n", s);
932 pfmt(err, "rc (%s): %s: %r\n", argv0, s);
935 while(!runq->iflag) Xreturn();
941 if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
942 pfmt(err, "rc: %s\n", s);
944 pfmt(err, "rc (%s): %s\n", argv0, s);
947 while(!runq->iflag) Xreturn();
953 setvar("status", newword(s, (word *)0));
959 var *status = vlook("status");
960 return status->val?status->val->word:"";
967 for(s = getstatus();*s;s++)
968 if(*s!='|' && *s!='0')
976 Unlink(runq->code[runq->pc++].s);
982 if(runq->argv->words==0){
984 runq->pc = runq->code[runq->pc].i;
987 freelist(runq->local->val);
988 runq->local->val = runq->argv->words;
989 runq->local->changed = 1;
990 runq->argv->words = runq->argv->words->next;
991 runq->local->val->next = 0;