exec.c 17 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "rc.h"
  10. #include "getflags.h"
  11. #include "exec.h"
  12. #include "io.h"
  13. #include "fns.h"
  14. /*
  15. * Start executing the given code at the given pc with the given redirection
  16. */
  17. char *argv0="rc";
  18. void
  19. start(code *c, int pc, var *local)
  20. {
  21. struct thread *p = new(struct thread);
  22. p->code = codecopy(c);
  23. p->pc = pc;
  24. p->argv = 0;
  25. p->redir = p->startredir = runq?runq->redir:0;
  26. p->local = local;
  27. p->cmdfile = 0;
  28. p->cmdfd = 0;
  29. p->eof = 0;
  30. p->iflag = 0;
  31. p->lineno = 1;
  32. p->ret = runq;
  33. runq = p;
  34. }
  35. word*
  36. newword(char *wd, word *next)
  37. {
  38. word *p = new(word);
  39. p->word = strdup(wd);
  40. p->next = next;
  41. return p;
  42. }
  43. void
  44. pushword(char *wd)
  45. {
  46. if(runq->argv==0)
  47. panic("pushword but no argv!", 0);
  48. runq->argv->words = newword(wd, runq->argv->words);
  49. }
  50. void
  51. popword(void)
  52. {
  53. word *p;
  54. if(runq->argv==0)
  55. panic("popword but no argv!", 0);
  56. p = runq->argv->words;
  57. if(p==0)
  58. panic("popword but no word!", 0);
  59. runq->argv->words = p->next;
  60. efree(p->word);
  61. efree((char *)p);
  62. }
  63. void
  64. freelist(word *w)
  65. {
  66. word *nw;
  67. while(w){
  68. nw = w->next;
  69. efree(w->word);
  70. efree((char *)w);
  71. w = nw;
  72. }
  73. }
  74. void
  75. pushlist(void)
  76. {
  77. list *p = new(list);
  78. p->next = runq->argv;
  79. p->words = 0;
  80. runq->argv = p;
  81. }
  82. void
  83. poplist(void)
  84. {
  85. list *p = runq->argv;
  86. if(p==0)
  87. panic("poplist but no argv", 0);
  88. freelist(p->words);
  89. runq->argv = p->next;
  90. efree((char *)p);
  91. }
  92. int
  93. count(word *w)
  94. {
  95. int n;
  96. for(n = 0;w;n++) w = w->next;
  97. return n;
  98. }
  99. void
  100. pushredir(int type, int from, int to)
  101. {
  102. redir * rp = new(redir);
  103. rp->type = type;
  104. rp->from = from;
  105. rp->to = to;
  106. rp->next = runq->redir;
  107. runq->redir = rp;
  108. }
  109. var*
  110. newvar(char *name, var *next)
  111. {
  112. var *v = new(var);
  113. v->name = name;
  114. v->val = 0;
  115. v->fn = 0;
  116. v->changed = 0;
  117. v->fnchanged = 0;
  118. v->next = next;
  119. return v;
  120. }
  121. /*
  122. * get command line flags, initialize keywords & traps.
  123. * get values from environment.
  124. * set $pid, $cflag, $*
  125. * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*)
  126. * start interpreting code
  127. */
  128. void
  129. main(int argc, char *argv[])
  130. {
  131. code bootstrap[17];
  132. char num[12], *rcmain;
  133. int i;
  134. argc = getflags(argc, argv, "SsrdiIlxepvVc:1m:1[command]", 1);
  135. if(argc==-1)
  136. usage("[file [arg ...]]");
  137. if(argv[0][0]=='-')
  138. flag['l'] = flagset;
  139. if(flag['I'])
  140. flag['i'] = 0;
  141. else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
  142. rcmain = flag['m']?flag['m'][0]:Rcmain;
  143. err = openfd(2);
  144. kinit();
  145. Trapinit();
  146. Vinit();
  147. inttoascii(num, mypid = getpid());
  148. setvar("pid", newword(num, (word *)0));
  149. setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
  150. :(word *)0);
  151. setvar("rcname", newword(argv[0], (word *)0));
  152. i = 0;
  153. memset(bootstrap, 0, sizeof bootstrap);
  154. bootstrap[i++].i = 1;
  155. bootstrap[i++].f = Xmark;
  156. bootstrap[i++].f = Xword;
  157. bootstrap[i++].s="*";
  158. bootstrap[i++].f = Xassign;
  159. bootstrap[i++].f = Xmark;
  160. bootstrap[i++].f = Xmark;
  161. bootstrap[i++].f = Xword;
  162. bootstrap[i++].s="*";
  163. bootstrap[i++].f = Xdol;
  164. bootstrap[i++].f = Xword;
  165. bootstrap[i++].s = rcmain;
  166. bootstrap[i++].f = Xword;
  167. bootstrap[i++].s=".";
  168. bootstrap[i++].f = Xsimple;
  169. bootstrap[i++].f = Xexit;
  170. bootstrap[i].i = 0;
  171. start(bootstrap, 1, (var *)0);
  172. /* prime bootstrap argv */
  173. pushlist();
  174. argv0 = strdup(argv[0]);
  175. for(i = argc-1;i!=0;--i) pushword(argv[i]);
  176. for(;;){
  177. if(flag['r'])
  178. pfnc(err, runq);
  179. runq->pc++;
  180. (*runq->code[runq->pc-1].f)();
  181. if(ntrap)
  182. dotrap();
  183. }
  184. }
  185. /*
  186. * Opcode routines
  187. * Arguments on stack (...)
  188. * Arguments in line [...]
  189. * Code in line with jump around {...}
  190. *
  191. * Xappend(file)[fd] open file to append
  192. * Xassign(name, val) assign val to name
  193. * Xasync{... Xexit} make thread for {}, no wait
  194. * Xbackq{... Xreturn} make thread for {}, push stdout
  195. * Xbang complement condition
  196. * Xcase(pat, value){...} exec code on match, leave (value) on
  197. * stack
  198. * Xclose[i] close file descriptor
  199. * Xconc(left, right) concatenate, push results
  200. * Xcount(name) push var count
  201. * Xdelfn(name) delete function definition
  202. * Xdelhere
  203. * Xdol(name) get variable value
  204. * Xdup[i j] dup file descriptor
  205. * Xeflag
  206. * Xerror
  207. * Xexit rc exits with status
  208. * Xfalse{...} execute {} if false
  209. * Xfn(name){... Xreturn} define function
  210. * Xfor(var, list){... Xreturn} for loop
  211. * Xglob
  212. * Xif
  213. * Xifnot
  214. * Xjump[addr] goto
  215. * Xlocal(name, val) create local variable, assign value
  216. * Xmark mark stack
  217. * Xmatch(pat, str) match pattern, set status
  218. * Xpipe[i j]{... Xreturn}{... Xreturn} construct a pipe between 2 new threads,
  219. * wait for both
  220. * Xpipefd[type]{... Xreturn} connect {} to pipe (input or output,
  221. * depending on type), push /dev/fd/??
  222. * Xpipewait
  223. * Xpopm(value) pop value from stack
  224. * Xpopredir
  225. * Xrdcmds
  226. * Xrdfn
  227. * Xrdwr(file)[fd] open file for reading and writing
  228. * Xread(file)[fd] open file to read
  229. * Xqdol(name) concatenate variable components
  230. * Xreturn kill thread
  231. * Xsimple(args) run command and wait
  232. * Xsub
  233. * Xsubshell{... Xexit} execute {} in a subshell and wait
  234. * Xtrue{...} execute {} if true
  235. * Xunlocal delete local variable
  236. * Xwastrue
  237. * Xword[string] push string
  238. * Xwrite(file)[fd] open file to write
  239. */
  240. void
  241. Xappend(void)
  242. {
  243. char *file;
  244. int f;
  245. switch(count(runq->argv->words)){
  246. default:
  247. Xerror1(">> requires singleton");
  248. return;
  249. case 0:
  250. Xerror1(">> requires file");
  251. return;
  252. case 1:
  253. break;
  254. }
  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");
  259. return;
  260. }
  261. Seek(f, 0L, 2);
  262. pushredir(ROPEN, f, runq->code[runq->pc].i);
  263. runq->pc++;
  264. poplist();
  265. }
  266. void
  267. Xsettrue(void)
  268. {
  269. setstatus("");
  270. }
  271. void
  272. Xbang(void)
  273. {
  274. setstatus(truestatus()?"false":"");
  275. }
  276. void
  277. Xclose(void)
  278. {
  279. pushredir(RCLOSE, runq->code[runq->pc].i, 0);
  280. runq->pc++;
  281. }
  282. void
  283. Xdup(void)
  284. {
  285. pushredir(RDUP, runq->code[runq->pc].i, runq->code[runq->pc+1].i);
  286. runq->pc+=2;
  287. }
  288. void
  289. Xeflag(void)
  290. {
  291. if(eflagok && !truestatus()) Xexit();
  292. }
  293. void
  294. Xexit(void)
  295. {
  296. struct var *trapreq;
  297. struct word *starval;
  298. static int beenhere = 0;
  299. if(getpid()==mypid && !beenhere){
  300. trapreq = vlook("sigexit");
  301. if(trapreq->fn){
  302. beenhere = 1;
  303. --runq->pc;
  304. starval = vlook("*")->val;
  305. start(trapreq->fn, trapreq->pc, (struct var *)0);
  306. runq->local = newvar(strdup("*"), runq->local);
  307. runq->local->val = copywords(starval, (struct word *)0);
  308. runq->local->changed = 1;
  309. runq->redir = runq->startredir = 0;
  310. return;
  311. }
  312. }
  313. Exit(getstatus());
  314. }
  315. void
  316. Xfalse(void)
  317. {
  318. if(truestatus()) runq->pc = runq->code[runq->pc].i;
  319. else runq->pc++;
  320. }
  321. int ifnot; /* dynamic if not flag */
  322. void
  323. Xifnot(void)
  324. {
  325. if(ifnot)
  326. runq->pc++;
  327. else
  328. runq->pc = runq->code[runq->pc].i;
  329. }
  330. void
  331. Xjump(void)
  332. {
  333. runq->pc = runq->code[runq->pc].i;
  334. }
  335. void
  336. Xmark(void)
  337. {
  338. pushlist();
  339. }
  340. void
  341. Xpopm(void)
  342. {
  343. poplist();
  344. }
  345. void
  346. Xread(void)
  347. {
  348. char *file;
  349. int f;
  350. switch(count(runq->argv->words)){
  351. default:
  352. Xerror1("< requires singleton\n");
  353. return;
  354. case 0:
  355. Xerror1("< requires file\n");
  356. return;
  357. case 1:
  358. break;
  359. }
  360. file = runq->argv->words->word;
  361. if((f = open(file, 0))<0){
  362. pfmt(err, "%s: ", file);
  363. Xerror("can't open");
  364. return;
  365. }
  366. pushredir(ROPEN, f, runq->code[runq->pc].i);
  367. runq->pc++;
  368. poplist();
  369. }
  370. void
  371. Xrdwr(void)
  372. {
  373. char *file;
  374. int f;
  375. switch(count(runq->argv->words)){
  376. default:
  377. Xerror1("<> requires singleton\n");
  378. return;
  379. case 0:
  380. Xerror1("<> requires file\n");
  381. return;
  382. case 1:
  383. break;
  384. }
  385. file = runq->argv->words->word;
  386. if((f = open(file, ORDWR))<0){
  387. pfmt(err, "%s: ", file);
  388. Xerror("can't open");
  389. return;
  390. }
  391. pushredir(ROPEN, f, runq->code[runq->pc].i);
  392. runq->pc++;
  393. poplist();
  394. }
  395. void
  396. turfredir(void)
  397. {
  398. while(runq->redir!=runq->startredir)
  399. Xpopredir();
  400. }
  401. void
  402. Xpopredir(void)
  403. {
  404. struct redir *rp = runq->redir;
  405. if(rp==0)
  406. panic("turfredir null!", 0);
  407. runq->redir = rp->next;
  408. if(rp->type==ROPEN)
  409. close(rp->from);
  410. efree((char *)rp);
  411. }
  412. void
  413. Xreturn(void)
  414. {
  415. struct thread *p = runq;
  416. turfredir();
  417. while(p->argv) poplist();
  418. codefree(p->code);
  419. runq = p->ret;
  420. efree((char *)p);
  421. if(runq==0)
  422. Exit(getstatus());
  423. }
  424. void
  425. Xtrue(void)
  426. {
  427. if(truestatus()) runq->pc++;
  428. else runq->pc = runq->code[runq->pc].i;
  429. }
  430. void
  431. Xif(void)
  432. {
  433. ifnot = 1;
  434. if(truestatus()) runq->pc++;
  435. else runq->pc = runq->code[runq->pc].i;
  436. }
  437. void
  438. Xwastrue(void)
  439. {
  440. ifnot = 0;
  441. }
  442. void
  443. Xword(void)
  444. {
  445. pushword(runq->code[runq->pc++].s);
  446. }
  447. void
  448. Xwrite(void)
  449. {
  450. char *file;
  451. int f;
  452. switch(count(runq->argv->words)){
  453. default:
  454. Xerror1("> requires singleton\n");
  455. return;
  456. case 0:
  457. Xerror1("> requires file\n");
  458. return;
  459. case 1:
  460. break;
  461. }
  462. file = runq->argv->words->word;
  463. if((f = Creat(file))<0){
  464. pfmt(err, "%s: ", file);
  465. Xerror("can't open");
  466. return;
  467. }
  468. pushredir(ROPEN, f, runq->code[runq->pc].i);
  469. runq->pc++;
  470. poplist();
  471. }
  472. char*
  473. list2str(word *words)
  474. {
  475. char *value, *s, *t;
  476. int len = 0;
  477. word *ap;
  478. for(ap = words;ap;ap = ap->next)
  479. len+=1+strlen(ap->word);
  480. value = emalloc(len+1);
  481. s = value;
  482. for(ap = words;ap;ap = ap->next){
  483. for(t = ap->word;*t;) *s++=*t++;
  484. *s++=' ';
  485. }
  486. if(s==value)
  487. *s='\0';
  488. else s[-1]='\0';
  489. return value;
  490. }
  491. void
  492. Xmatch(void)
  493. {
  494. word *p;
  495. char *subject;
  496. subject = list2str(runq->argv->words);
  497. setstatus("no match");
  498. for(p = runq->argv->next->words;p;p = p->next)
  499. if(match(subject, p->word, '\0')){
  500. setstatus("");
  501. break;
  502. }
  503. efree(subject);
  504. poplist();
  505. poplist();
  506. }
  507. void
  508. Xcase(void)
  509. {
  510. word *p;
  511. char *s;
  512. int ok = 0;
  513. s = list2str(runq->argv->next->words);
  514. for(p = runq->argv->words;p;p = p->next){
  515. if(match(s, p->word, '\0')){
  516. ok = 1;
  517. break;
  518. }
  519. }
  520. efree(s);
  521. if(ok)
  522. runq->pc++;
  523. else
  524. runq->pc = runq->code[runq->pc].i;
  525. poplist();
  526. }
  527. word*
  528. conclist(word *lp, word *rp, word *tail)
  529. {
  530. char *buf;
  531. word *v;
  532. if(lp->next || rp->next)
  533. tail = conclist(lp->next==0? lp: lp->next,
  534. rp->next==0? rp: rp->next, tail);
  535. buf = emalloc(strlen(lp->word)+strlen((char *)rp->word)+1);
  536. strcpy(buf, lp->word);
  537. strcat(buf, rp->word);
  538. v = newword(buf, tail);
  539. efree(buf);
  540. return v;
  541. }
  542. void
  543. Xconc(void)
  544. {
  545. word *lp = runq->argv->words;
  546. word *rp = runq->argv->next->words;
  547. word *vp = runq->argv->next->next->words;
  548. int lc = count(lp), rc = count(rp);
  549. if(lc!=0 || rc!=0){
  550. if(lc==0 || rc==0){
  551. Xerror1("null list in concatenation");
  552. return;
  553. }
  554. if(lc!=1 && rc!=1 && lc!=rc){
  555. Xerror1("mismatched list lengths in concatenation");
  556. return;
  557. }
  558. vp = conclist(lp, rp, vp);
  559. }
  560. poplist();
  561. poplist();
  562. runq->argv->words = vp;
  563. }
  564. void
  565. Xassign(void)
  566. {
  567. var *v;
  568. if(count(runq->argv->words)!=1){
  569. Xerror1("variable name not singleton!");
  570. return;
  571. }
  572. deglob(runq->argv->words->word);
  573. v = vlook(runq->argv->words->word);
  574. poplist();
  575. globlist();
  576. freewords(v->val);
  577. v->val = runq->argv->words;
  578. v->changed = 1;
  579. runq->argv->words = 0;
  580. poplist();
  581. }
  582. /*
  583. * copy arglist a, adding the copy to the front of tail
  584. */
  585. word*
  586. copywords(word *a, word *tail)
  587. {
  588. word *v = 0, **end;
  589. for(end=&v;a;a = a->next,end=&(*end)->next)
  590. *end = newword(a->word, 0);
  591. *end = tail;
  592. return v;
  593. }
  594. void
  595. Xdol(void)
  596. {
  597. word *a, *star;
  598. char *s, *t;
  599. int n;
  600. if(count(runq->argv->words)!=1){
  601. Xerror1("variable name not singleton!");
  602. return;
  603. }
  604. s = runq->argv->words->word;
  605. deglob(s);
  606. n = 0;
  607. for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
  608. a = runq->argv->next->words;
  609. if(n==0 || *t)
  610. a = copywords(vlook(s)->val, a);
  611. else{
  612. star = vlook("*")->val;
  613. if(star && 1<=n && n<=count(star)){
  614. while(--n) star = star->next;
  615. a = newword(star->word, a);
  616. }
  617. }
  618. poplist();
  619. runq->argv->words = a;
  620. }
  621. void
  622. Xqdol(void)
  623. {
  624. word *a, *p;
  625. char *s;
  626. int n;
  627. if(count(runq->argv->words)!=1){
  628. Xerror1("variable name not singleton!");
  629. return;
  630. }
  631. s = runq->argv->words->word;
  632. deglob(s);
  633. a = vlook(s)->val;
  634. poplist();
  635. n = count(a);
  636. if(n==0){
  637. pushword("");
  638. return;
  639. }
  640. for(p = a;p;p = p->next) n+=strlen(p->word);
  641. s = emalloc(n);
  642. if(a){
  643. strcpy(s, a->word);
  644. for(p = a->next;p;p = p->next){
  645. strcat(s, " ");
  646. strcat(s, p->word);
  647. }
  648. }
  649. else
  650. s[0]='\0';
  651. pushword(s);
  652. efree(s);
  653. }
  654. word*
  655. copynwords(word *a, word *tail, int n)
  656. {
  657. word *v, **end;
  658. v = 0;
  659. end = &v;
  660. while(n-- > 0){
  661. *end = newword(a->word, 0);
  662. end = &(*end)->next;
  663. a = a->next;
  664. }
  665. *end = tail;
  666. return v;
  667. }
  668. word*
  669. subwords(word *val, int len, word *sub, word *a)
  670. {
  671. int n, m;
  672. char *s;
  673. if(!sub)
  674. return a;
  675. a = subwords(val, len, sub->next, a);
  676. s = sub->word;
  677. deglob(s);
  678. m = 0;
  679. n = 0;
  680. while('0'<=*s && *s<='9')
  681. n = n*10+ *s++ -'0';
  682. if(*s == '-'){
  683. if(*++s == 0)
  684. m = len - n;
  685. else{
  686. while('0'<=*s && *s<='9')
  687. m = m*10+ *s++ -'0';
  688. m -= n;
  689. }
  690. }
  691. if(n<1 || n>len || m<0)
  692. return a;
  693. if(n+m>len)
  694. m = len-n;
  695. while(--n > 0)
  696. val = val->next;
  697. return copynwords(val, a, m+1);
  698. }
  699. void
  700. Xsub(void)
  701. {
  702. word *a, *v;
  703. char *s;
  704. if(count(runq->argv->next->words)!=1){
  705. Xerror1("variable name not singleton!");
  706. return;
  707. }
  708. s = runq->argv->next->words->word;
  709. deglob(s);
  710. a = runq->argv->next->next->words;
  711. v = vlook(s)->val;
  712. a = subwords(v, count(v), runq->argv->words, a);
  713. poplist();
  714. poplist();
  715. runq->argv->words = a;
  716. }
  717. void
  718. Xcount(void)
  719. {
  720. word *a;
  721. char *s, *t;
  722. int n;
  723. char num[12];
  724. if(count(runq->argv->words)!=1){
  725. Xerror1("variable name not singleton!");
  726. return;
  727. }
  728. s = runq->argv->words->word;
  729. deglob(s);
  730. n = 0;
  731. for(t = s;'0'<=*t && *t<='9';t++) n = n*10+*t-'0';
  732. if(n==0 || *t){
  733. a = vlook(s)->val;
  734. inttoascii(num, count(a));
  735. }
  736. else{
  737. a = vlook("*")->val;
  738. inttoascii(num, a && 1<=n && n<=count(a)?1:0);
  739. }
  740. poplist();
  741. pushword(num);
  742. }
  743. void
  744. Xlocal(void)
  745. {
  746. if(count(runq->argv->words)!=1){
  747. Xerror1("variable name must be singleton\n");
  748. return;
  749. }
  750. deglob(runq->argv->words->word);
  751. runq->local = newvar(strdup(runq->argv->words->word), runq->local);
  752. poplist();
  753. globlist();
  754. runq->local->val = runq->argv->words;
  755. runq->local->changed = 1;
  756. runq->argv->words = 0;
  757. poplist();
  758. }
  759. void
  760. Xunlocal(void)
  761. {
  762. var *v = runq->local, *hid;
  763. if(v==0)
  764. panic("Xunlocal: no locals!", 0);
  765. runq->local = v->next;
  766. hid = vlook(v->name);
  767. hid->changed = 1;
  768. efree(v->name);
  769. freewords(v->val);
  770. efree((char *)v);
  771. }
  772. void
  773. freewords(word *w)
  774. {
  775. word *nw;
  776. while(w){
  777. efree(w->word);
  778. nw = w->next;
  779. efree((char *)w);
  780. w = nw;
  781. }
  782. }
  783. void
  784. Xfn(void)
  785. {
  786. var *v;
  787. word *a;
  788. int end;
  789. end = runq->code[runq->pc].i;
  790. globlist();
  791. for(a = runq->argv->words;a;a = a->next){
  792. v = gvlook(a->word);
  793. if(v->fn)
  794. codefree(v->fn);
  795. v->fn = codecopy(runq->code);
  796. v->pc = runq->pc+2;
  797. v->fnchanged = 1;
  798. }
  799. runq->pc = end;
  800. poplist();
  801. }
  802. void
  803. Xdelfn(void)
  804. {
  805. var *v;
  806. word *a;
  807. for(a = runq->argv->words;a;a = a->next){
  808. v = gvlook(a->word);
  809. if(v->fn)
  810. codefree(v->fn);
  811. v->fn = 0;
  812. v->fnchanged = 1;
  813. }
  814. poplist();
  815. }
  816. char*
  817. concstatus(char *s, char *t)
  818. {
  819. static char v[NSTATUS+1];
  820. int n = strlen(s);
  821. strncpy(v, s, NSTATUS);
  822. if(n<NSTATUS){
  823. v[n]='|';
  824. strncpy(v+n+1, t, NSTATUS-n-1);
  825. }
  826. v[NSTATUS]='\0';
  827. return v;
  828. }
  829. void
  830. Xpipewait(void)
  831. {
  832. char status[NSTATUS+1];
  833. if(runq->pid==-1)
  834. setstatus(concstatus(runq->status, getstatus()));
  835. else{
  836. strncpy(status, getstatus(), NSTATUS);
  837. status[NSTATUS]='\0';
  838. Waitfor(runq->pid, 1);
  839. runq->pid=-1;
  840. setstatus(concstatus(getstatus(), status));
  841. }
  842. }
  843. void
  844. Xrdcmds(void)
  845. {
  846. struct thread *p = runq;
  847. word *prompt;
  848. flush(err);
  849. nerror = 0;
  850. if(flag['s'] && !truestatus())
  851. pfmt(err, "status=%v\n", vlook("status")->val);
  852. if(runq->iflag){
  853. prompt = vlook("prompt")->val;
  854. if(prompt)
  855. promptstr = prompt->word;
  856. else
  857. promptstr="% ";
  858. }
  859. Noerror();
  860. if(yyparse()){
  861. if(!p->iflag || (p->eof && !Eintr())){
  862. if(p->cmdfile)
  863. efree(p->cmdfile);
  864. closeio(p->cmdfd);
  865. Xreturn(); /* should this be omitted? */
  866. }
  867. else{
  868. if(Eintr()){
  869. pchr(err, '\n');
  870. p->eof = 0;
  871. }
  872. --p->pc; /* go back for next command */
  873. }
  874. }
  875. else{
  876. ntrap = 0; /* avoid double-interrupts during blocked writes */
  877. --p->pc; /* re-execute Xrdcmds after codebuf runs */
  878. start(codebuf, 1, runq->local);
  879. }
  880. freenodes();
  881. }
  882. void
  883. Xerror(char *s)
  884. {
  885. if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
  886. pfmt(err, "rc: %s: %r\n", s);
  887. else
  888. pfmt(err, "rc (%s): %s: %r\n", argv0, s);
  889. flush(err);
  890. setstatus("error");
  891. while(!runq->iflag) Xreturn();
  892. }
  893. void
  894. Xerror1(char *s)
  895. {
  896. if(strcmp(argv0, "rc")==0 || strcmp(argv0, "/bin/rc")==0)
  897. pfmt(err, "rc: %s\n", s);
  898. else
  899. pfmt(err, "rc (%s): %s\n", argv0, s);
  900. flush(err);
  901. setstatus("error");
  902. while(!runq->iflag) Xreturn();
  903. }
  904. void
  905. setstatus(char *s)
  906. {
  907. setvar("status", newword(s, (word *)0));
  908. }
  909. char*
  910. getstatus(void)
  911. {
  912. var *status = vlook("status");
  913. return status->val?status->val->word:"";
  914. }
  915. int
  916. truestatus(void)
  917. {
  918. char *s;
  919. for(s = getstatus();*s;s++)
  920. if(*s!='|' && *s!='0')
  921. return 0;
  922. return 1;
  923. }
  924. void
  925. Xdelhere(void)
  926. {
  927. Unlink(runq->code[runq->pc++].s);
  928. }
  929. void
  930. Xfor(void)
  931. {
  932. if(runq->argv->words==0){
  933. poplist();
  934. runq->pc = runq->code[runq->pc].i;
  935. }
  936. else{
  937. freelist(runq->local->val);
  938. runq->local->val = runq->argv->words;
  939. runq->local->changed = 1;
  940. runq->argv->words = runq->argv->words->next;
  941. runq->local->val->next = 0;
  942. runq->pc++;
  943. }
  944. }
  945. void
  946. Xglob(void)
  947. {
  948. globlist();
  949. }