builtin.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. #include <regexp.h>
  7. #define Extern extern
  8. #include "acid.h"
  9. #include "y.tab.h"
  10. void cvtatof(Node*, Node*);
  11. void cvtatoi(Node*, Node*);
  12. void cvtitoa(Node*, Node*);
  13. void bprint(Node*, Node*);
  14. void funcbound(Node*, Node*);
  15. void printto(Node*, Node*);
  16. void getfile(Node*, Node*);
  17. void fmt(Node*, Node*);
  18. void pcfile(Node*, Node*);
  19. void pcline(Node*, Node*);
  20. void setproc(Node*, Node*);
  21. void strace(Node*, Node*);
  22. void follow(Node*, Node*);
  23. void reason(Node*, Node*);
  24. void newproc(Node*, Node*);
  25. void startstop(Node*, Node*);
  26. void match(Node*, Node*);
  27. void status(Node*, Node*);
  28. void kill(Node*,Node*);
  29. void waitstop(Node*, Node*);
  30. void stop(Node*, Node*);
  31. void start(Node*, Node*);
  32. void filepc(Node*, Node*);
  33. void doerror(Node*, Node*);
  34. void rc(Node*, Node*);
  35. void doaccess(Node*, Node*);
  36. void map(Node*, Node*);
  37. void readfile(Node*, Node*);
  38. void interpret(Node*, Node*);
  39. void include(Node*, Node*);
  40. void regexp(Node*, Node*);
  41. void dosysr1(Node*, Node*);
  42. typedef struct Btab Btab;
  43. struct Btab
  44. {
  45. char *name;
  46. void (*fn)(Node*, Node*);
  47. } tab[] =
  48. {
  49. "atof", cvtatof,
  50. "atoi", cvtatoi,
  51. "error", doerror,
  52. "file", getfile,
  53. "readfile", readfile,
  54. "access", doaccess,
  55. "filepc", filepc,
  56. "fnbound", funcbound,
  57. "fmt", fmt,
  58. "follow", follow,
  59. "itoa", cvtitoa,
  60. "kill", kill,
  61. "match", match,
  62. "newproc", newproc,
  63. "pcfile", pcfile,
  64. "pcline", pcline,
  65. "print", bprint,
  66. "printto", printto,
  67. "rc", rc,
  68. "reason", reason,
  69. "setproc", setproc,
  70. "start", start,
  71. "startstop", startstop,
  72. "status", status,
  73. "stop", stop,
  74. "strace", strace,
  75. "sysr1", dosysr1,
  76. "waitstop", waitstop,
  77. "map", map,
  78. "interpret", interpret,
  79. "include", include,
  80. "regexp", regexp,
  81. 0
  82. };
  83. char vfmt[] = "aBbcCdDfFgGiIoOqQrRsSuUVWxXYZ";
  84. void
  85. mkprint(Lsym *s)
  86. {
  87. prnt = malloc(sizeof(Node));
  88. memset(prnt, 0, sizeof(Node));
  89. prnt->op = OCALL;
  90. prnt->left = malloc(sizeof(Node));
  91. memset(prnt->left, 0, sizeof(Node));
  92. prnt->left->sym = s;
  93. }
  94. void
  95. installbuiltin(void)
  96. {
  97. Btab *b;
  98. Lsym *s;
  99. b = tab;
  100. while(b->name) {
  101. s = look(b->name);
  102. if(s == 0)
  103. s = enter(b->name, Tid);
  104. s->builtin = b->fn;
  105. if(b->fn == bprint)
  106. mkprint(s);
  107. b++;
  108. }
  109. }
  110. void
  111. dosysr1(Node *r, Node*)
  112. {
  113. extern int sysr1(void);
  114. r->op = OCONST;
  115. r->type = TINT;
  116. r->fmt = 'D';
  117. r->ival = sysr1();
  118. }
  119. void
  120. match(Node *r, Node *args)
  121. {
  122. int i;
  123. List *f;
  124. Node *av[Maxarg];
  125. Node resi, resl;
  126. na = 0;
  127. flatten(av, args);
  128. if(na != 2)
  129. error("match(obj, list): arg count");
  130. expr(av[1], &resl);
  131. if(resl.type != TLIST)
  132. error("match(obj, list): need list");
  133. expr(av[0], &resi);
  134. r->op = OCONST;
  135. r->type = TINT;
  136. r->fmt = 'D';
  137. r->ival = -1;
  138. i = 0;
  139. for(f = resl.l; f; f = f->next) {
  140. if(resi.type == f->type) {
  141. switch(resi.type) {
  142. case TINT:
  143. if(resi.ival == f->ival) {
  144. r->ival = i;
  145. return;
  146. }
  147. break;
  148. case TFLOAT:
  149. if(resi.fval == f->fval) {
  150. r->ival = i;
  151. return;
  152. }
  153. break;
  154. case TSTRING:
  155. if(scmp(resi.string, f->string)) {
  156. r->ival = i;
  157. return;
  158. }
  159. break;
  160. case TLIST:
  161. error("match(obj, list): not defined for list");
  162. }
  163. }
  164. i++;
  165. }
  166. }
  167. void
  168. newproc(Node *r, Node *args)
  169. {
  170. int i;
  171. Node res;
  172. char *p, *e;
  173. char *argv[Maxarg], buf[Strsize];
  174. i = 1;
  175. argv[0] = aout;
  176. if(args) {
  177. expr(args, &res);
  178. if(res.type != TSTRING)
  179. error("newproc(): arg not string");
  180. if(res.string->len >= sizeof(buf))
  181. error("newproc(): too many arguments");
  182. memmove(buf, res.string->string, res.string->len);
  183. buf[res.string->len] = '\0';
  184. p = buf;
  185. e = buf+res.string->len;
  186. for(;;) {
  187. while(p < e && (*p == '\t' || *p == ' '))
  188. *p++ = '\0';
  189. if(p >= e)
  190. break;
  191. argv[i++] = p;
  192. if(i >= Maxarg)
  193. error("newproc: too many arguments");
  194. while(p < e && *p != '\t' && *p != ' ')
  195. p++;
  196. }
  197. }
  198. argv[i] = 0;
  199. r->op = OCONST;
  200. r->type = TINT;
  201. r->fmt = 'D';
  202. r->ival = nproc(argv);
  203. }
  204. void
  205. startstop(Node *r, Node *args)
  206. {
  207. Node res;
  208. USED(r);
  209. if(args == 0)
  210. error("startstop(pid): no pid");
  211. expr(args, &res);
  212. if(res.type != TINT)
  213. error("startstop(pid): arg type");
  214. msg(res.ival, "startstop");
  215. notes(res.ival);
  216. dostop(res.ival);
  217. }
  218. void
  219. waitstop(Node *r, Node *args)
  220. {
  221. Node res;
  222. USED(r);
  223. if(args == 0)
  224. error("waitstop(pid): no pid");
  225. expr(args, &res);
  226. if(res.type != TINT)
  227. error("waitstop(pid): arg type");
  228. Bflush(bout);
  229. msg(res.ival, "waitstop");
  230. notes(res.ival);
  231. dostop(res.ival);
  232. }
  233. void
  234. start(Node *r, Node *args)
  235. {
  236. Node res;
  237. USED(r);
  238. if(args == 0)
  239. error("start(pid): no pid");
  240. expr(args, &res);
  241. if(res.type != TINT)
  242. error("start(pid): arg type");
  243. msg(res.ival, "start");
  244. }
  245. void
  246. stop(Node *r, Node *args)
  247. {
  248. Node res;
  249. USED(r);
  250. if(args == 0)
  251. error("stop(pid): no pid");
  252. expr(args, &res);
  253. if(res.type != TINT)
  254. error("stop(pid): arg type");
  255. Bflush(bout);
  256. msg(res.ival, "stop");
  257. notes(res.ival);
  258. dostop(res.ival);
  259. }
  260. void
  261. kill(Node *r, Node *args)
  262. {
  263. Node res;
  264. USED(r);
  265. if(args == 0)
  266. error("kill(pid): no pid");
  267. expr(args, &res);
  268. if(res.type != TINT)
  269. error("kill(pid): arg type");
  270. msg(res.ival, "kill");
  271. deinstall(res.ival);
  272. }
  273. void
  274. status(Node *r, Node *args)
  275. {
  276. Node res;
  277. char *p;
  278. USED(r);
  279. if(args == 0)
  280. error("status(pid): no pid");
  281. expr(args, &res);
  282. if(res.type != TINT)
  283. error("status(pid): arg type");
  284. p = getstatus(res.ival);
  285. r->string = strnode(p);
  286. r->op = OCONST;
  287. r->fmt = 's';
  288. r->type = TSTRING;
  289. }
  290. void
  291. reason(Node *r, Node *args)
  292. {
  293. Node res;
  294. if(args == 0)
  295. error("reason(cause): no cause");
  296. expr(args, &res);
  297. if(res.type != TINT)
  298. error("reason(cause): arg type");
  299. r->op = OCONST;
  300. r->type = TSTRING;
  301. r->fmt = 's';
  302. r->string = strnode((*machdata->excep)(cormap, rget));
  303. }
  304. void
  305. follow(Node *r, Node *args)
  306. {
  307. int n, i;
  308. Node res;
  309. uvlong f[10];
  310. List **tail, *l;
  311. if(args == 0)
  312. error("follow(addr): no addr");
  313. expr(args, &res);
  314. if(res.type != TINT)
  315. error("follow(addr): arg type");
  316. n = (*machdata->foll)(cormap, res.ival, rget, f);
  317. if (n < 0)
  318. error("follow(addr): %r");
  319. tail = &r->l;
  320. for(i = 0; i < n; i++) {
  321. l = al(TINT);
  322. l->ival = f[i];
  323. l->fmt = 'X';
  324. *tail = l;
  325. tail = &l->next;
  326. }
  327. }
  328. void
  329. funcbound(Node *r, Node *args)
  330. {
  331. int n;
  332. Node res;
  333. uvlong bounds[2];
  334. List *l;
  335. if(args == 0)
  336. error("fnbound(addr): no addr");
  337. expr(args, &res);
  338. if(res.type != TINT)
  339. error("fnbound(addr): arg type");
  340. n = fnbound(res.ival, bounds);
  341. if (n != 0) {
  342. r->l = al(TINT);
  343. l = r->l;
  344. l->ival = bounds[0];
  345. l->fmt = 'X';
  346. l->next = al(TINT);
  347. l = l->next;
  348. l->ival = bounds[1];
  349. l->fmt = 'X';
  350. }
  351. }
  352. void
  353. setproc(Node *r, Node *args)
  354. {
  355. Node res;
  356. USED(r);
  357. if(args == 0)
  358. error("setproc(pid): no pid");
  359. expr(args, &res);
  360. if(res.type != TINT)
  361. error("setproc(pid): arg type");
  362. sproc(res.ival);
  363. }
  364. void
  365. filepc(Node *r, Node *args)
  366. {
  367. Node res;
  368. char *p, c;
  369. if(args == 0)
  370. error("filepc(filename:line): arg count");
  371. expr(args, &res);
  372. if(res.type != TSTRING)
  373. error("filepc(filename:line): arg type");
  374. p = strchr(res.string->string, ':');
  375. if(p == 0)
  376. error("filepc(filename:line): bad arg format");
  377. c = *p;
  378. *p++ = '\0';
  379. r->ival = file2pc(res.string->string, strtol(p, 0, 0));
  380. p[-1] = c;
  381. if(r->ival == ~0)
  382. error("filepc(filename:line): can't find address");
  383. r->op = OCONST;
  384. r->type = TINT;
  385. r->fmt = 'V';
  386. }
  387. void
  388. interpret(Node *r, Node *args)
  389. {
  390. Node res;
  391. int isave;
  392. if(args == 0)
  393. error("interpret(string): arg count");
  394. expr(args, &res);
  395. if(res.type != TSTRING)
  396. error("interpret(string): arg type");
  397. pushstr(&res);
  398. isave = interactive;
  399. interactive = 0;
  400. r->ival = yyparse();
  401. interactive = isave;
  402. popio();
  403. r->op = OCONST;
  404. r->type = TINT;
  405. r->fmt = 'D';
  406. }
  407. void
  408. include(Node *r, Node *args)
  409. {
  410. Node res;
  411. int isave;
  412. if(args == 0)
  413. error("include(string): arg count");
  414. expr(args, &res);
  415. if(res.type != TSTRING)
  416. error("include(string): arg type");
  417. pushfile(res.string->string);
  418. isave = interactive;
  419. interactive = 0;
  420. r->ival = yyparse();
  421. interactive = isave;
  422. popio();
  423. r->op = OCONST;
  424. r->type = TINT;
  425. r->fmt = 'D';
  426. }
  427. void
  428. rc(Node *r, Node *args)
  429. {
  430. Node res;
  431. int pid;
  432. char *p, *q, *argv[4];
  433. Waitmsg *w;
  434. USED(r);
  435. if(args == 0)
  436. error("error(string): arg count");
  437. expr(args, &res);
  438. if(res.type != TSTRING)
  439. error("error(string): arg type");
  440. argv[0] = "/bin/rc";
  441. argv[1] = "-c";
  442. argv[2] = res.string->string;
  443. argv[3] = 0;
  444. pid = fork();
  445. switch(pid) {
  446. case -1:
  447. error("fork %r");
  448. case 0:
  449. exec("/bin/rc", argv);
  450. exits(0);
  451. default:
  452. w = waitfor(pid);
  453. break;
  454. }
  455. p = w->msg;
  456. q = strrchr(p, ':');
  457. if (q)
  458. p = q+1;
  459. r->op = OCONST;
  460. r->type = TSTRING;
  461. r->string = strnode(p);
  462. free(w);
  463. r->fmt = 's';
  464. }
  465. void
  466. doerror(Node *r, Node *args)
  467. {
  468. Node res;
  469. USED(r);
  470. if(args == 0)
  471. error("error(string): arg count");
  472. expr(args, &res);
  473. if(res.type != TSTRING)
  474. error("error(string): arg type");
  475. error(res.string->string);
  476. }
  477. void
  478. doaccess(Node *r, Node *args)
  479. {
  480. Node res;
  481. if(args == 0)
  482. error("access(filename): arg count");
  483. expr(args, &res);
  484. if(res.type != TSTRING)
  485. error("access(filename): arg type");
  486. r->op = OCONST;
  487. r->type = TINT;
  488. r->ival = 0;
  489. if(access(res.string->string, 4) == 0)
  490. r->ival = 1;
  491. }
  492. void
  493. readfile(Node *r, Node *args)
  494. {
  495. Node res;
  496. int n, fd;
  497. char *buf;
  498. Dir *db;
  499. if(args == 0)
  500. error("readfile(filename): arg count");
  501. expr(args, &res);
  502. if(res.type != TSTRING)
  503. error("readfile(filename): arg type");
  504. fd = open(res.string->string, OREAD);
  505. if(fd < 0)
  506. return;
  507. db = dirfstat(fd);
  508. if(db == nil || db->length == 0)
  509. n = 8192;
  510. else
  511. n = db->length;
  512. free(db);
  513. buf = malloc(n);
  514. n = read(fd, buf, n);
  515. if(n > 0) {
  516. r->op = OCONST;
  517. r->type = TSTRING;
  518. r->string = strnodlen(buf, n);
  519. r->fmt = 's';
  520. }
  521. free(buf);
  522. close(fd);
  523. }
  524. void
  525. getfile(Node *r, Node *args)
  526. {
  527. int n;
  528. char *p;
  529. Node res;
  530. String *s;
  531. Biobuf *bp;
  532. List **l, *new;
  533. if(args == 0)
  534. error("file(filename): arg count");
  535. expr(args, &res);
  536. if(res.type != TSTRING)
  537. error("file(filename): arg type");
  538. r->op = OCONST;
  539. r->type = TLIST;
  540. r->l = 0;
  541. p = res.string->string;
  542. bp = Bopen(p, OREAD);
  543. if(bp == 0)
  544. return;
  545. l = &r->l;
  546. for(;;) {
  547. p = Brdline(bp, '\n');
  548. n = Blinelen(bp);
  549. if(p == 0) {
  550. if(n == 0)
  551. break;
  552. s = strnodlen(0, n);
  553. Bread(bp, s->string, n);
  554. }
  555. else
  556. s = strnodlen(p, n-1);
  557. new = al(TSTRING);
  558. new->string = s;
  559. new->fmt = 's';
  560. *l = new;
  561. l = &new->next;
  562. }
  563. Bterm(bp);
  564. }
  565. void
  566. cvtatof(Node *r, Node *args)
  567. {
  568. Node res;
  569. if(args == 0)
  570. error("atof(string): arg count");
  571. expr(args, &res);
  572. if(res.type != TSTRING)
  573. error("atof(string): arg type");
  574. r->op = OCONST;
  575. r->type = TFLOAT;
  576. r->fval = atof(res.string->string);
  577. r->fmt = 'f';
  578. }
  579. void
  580. cvtatoi(Node *r, Node *args)
  581. {
  582. Node res;
  583. if(args == 0)
  584. error("atoi(string): arg count");
  585. expr(args, &res);
  586. if(res.type != TSTRING)
  587. error("atoi(string): arg type");
  588. r->op = OCONST;
  589. r->type = TINT;
  590. r->ival = strtoull(res.string->string, 0, 0);
  591. r->fmt = 'V';
  592. }
  593. static char *fmtflags = "-0123456789. #,u";
  594. static char *fmtverbs = "bdox";
  595. static int
  596. acidfmt(char *fmt, char *buf, int blen)
  597. {
  598. char *r, *w, *e;
  599. w = buf;
  600. e = buf+blen;
  601. for(r=fmt; *r; r++){
  602. if(w >= e)
  603. return -1;
  604. if(*r != '%'){
  605. *w++ = *r;
  606. continue;
  607. }
  608. if(*r == '%'){
  609. *w++ = *r++;
  610. if(*r == '%'){
  611. if(w >= e)
  612. return -1;
  613. *w++ = *r;
  614. continue;
  615. }
  616. while(*r && strchr(fmtflags, *r)){
  617. if(w >= e)
  618. return -1;
  619. *w++ = *r++;
  620. }
  621. if(*r == 0 || strchr(fmtverbs, *r) == nil)
  622. return -1;
  623. if(w+3 > e)
  624. return -1;
  625. *w++ = 'l';
  626. *w++ = 'l';
  627. *w++ = *r;
  628. }
  629. }
  630. if(w >= e)
  631. return -1;
  632. *w = 0;
  633. return 0;
  634. }
  635. void
  636. cvtitoa(Node *r, Node *args)
  637. {
  638. Node res;
  639. Node *av[Maxarg];
  640. vlong ival;
  641. char buf[128], fmt[32];
  642. if(args == 0)
  643. err:
  644. error("itoa(number [, fmt]): arg count");
  645. na = 0;
  646. flatten(av, args);
  647. if(na == 0 || na > 2)
  648. goto err;
  649. expr(av[0], &res);
  650. if(res.type != TINT)
  651. error("itoa(number [, fmt]): arg type");
  652. ival = res.ival;
  653. strncpy(fmt, "%lld", sizeof(fmt));
  654. if(na == 2){
  655. expr(av[1], &res);
  656. if(res.type != TSTRING)
  657. error("itoa(number [, fmt]): fmt type");
  658. if(acidfmt(res.string->string, fmt, sizeof(buf)))
  659. error("itoa(number [, fmt]): malformed fmt");
  660. }
  661. snprint(buf, sizeof(buf), fmt, ival);
  662. r->op = OCONST;
  663. r->type = TSTRING;
  664. r->string = strnode(buf);
  665. r->fmt = 's';
  666. }
  667. List*
  668. mapent(Map *m)
  669. {
  670. int i;
  671. List *l, *n, **t, *h;
  672. h = 0;
  673. t = &h;
  674. for(i = 0; i < m->nsegs; i++) {
  675. if(m->seg[i].inuse == 0)
  676. continue;
  677. l = al(TSTRING);
  678. n = al(TLIST);
  679. n->l = l;
  680. *t = n;
  681. t = &n->next;
  682. l->string = strnode(m->seg[i].name);
  683. l->fmt = 's';
  684. l->next = al(TINT);
  685. l = l->next;
  686. l->ival = m->seg[i].b;
  687. l->fmt = 'W';
  688. l->next = al(TINT);
  689. l = l->next;
  690. l->ival = m->seg[i].e;
  691. l->fmt = 'W';
  692. l->next = al(TINT);
  693. l = l->next;
  694. l->ival = m->seg[i].f;
  695. l->fmt = 'W';
  696. }
  697. return h;
  698. }
  699. void
  700. map(Node *r, Node *args)
  701. {
  702. int i;
  703. Map *m;
  704. List *l;
  705. char *ent;
  706. Node *av[Maxarg], res;
  707. na = 0;
  708. flatten(av, args);
  709. if(na != 0) {
  710. expr(av[0], &res);
  711. if(res.type != TLIST)
  712. error("map(list): map needs a list");
  713. if(listlen(res.l) != 4)
  714. error("map(list): list must have 4 entries");
  715. l = res.l;
  716. if(l->type != TSTRING)
  717. error("map name must be a string");
  718. ent = l->string->string;
  719. m = symmap;
  720. i = findseg(m, ent);
  721. if(i < 0) {
  722. m = cormap;
  723. i = findseg(m, ent);
  724. }
  725. if(i < 0)
  726. error("%s is not a map entry", ent);
  727. l = l->next;
  728. if(l->type != TINT)
  729. error("map entry not int");
  730. m->seg[i].b = l->ival;
  731. if (strcmp(ent, "text") == 0)
  732. textseg(l->ival, &fhdr);
  733. l = l->next;
  734. if(l->type != TINT)
  735. error("map entry not int");
  736. m->seg[i].e = l->ival;
  737. l = l->next;
  738. if(l->type != TINT)
  739. error("map entry not int");
  740. m->seg[i].f = l->ival;
  741. }
  742. r->type = TLIST;
  743. r->l = 0;
  744. if(symmap)
  745. r->l = mapent(symmap);
  746. if(cormap) {
  747. if(r->l == 0)
  748. r->l = mapent(cormap);
  749. else {
  750. for(l = r->l; l->next; l = l->next)
  751. ;
  752. l->next = mapent(cormap);
  753. }
  754. }
  755. }
  756. void
  757. flatten(Node **av, Node *n)
  758. {
  759. if(n == 0)
  760. return;
  761. switch(n->op) {
  762. case OLIST:
  763. flatten(av, n->left);
  764. flatten(av, n->right);
  765. break;
  766. default:
  767. av[na++] = n;
  768. if(na >= Maxarg)
  769. error("too many function arguments");
  770. break;
  771. }
  772. }
  773. void
  774. strace(Node *r, Node *args)
  775. {
  776. Node *av[Maxarg], *n, res;
  777. uvlong pc, sp;
  778. na = 0;
  779. flatten(av, args);
  780. if(na != 3)
  781. error("strace(pc, sp, link): arg count");
  782. n = av[0];
  783. expr(n, &res);
  784. if(res.type != TINT)
  785. error("strace(pc, sp, link): pc bad type");
  786. pc = res.ival;
  787. n = av[1];
  788. expr(n, &res);
  789. if(res.type != TINT)
  790. error("strace(pc, sp, link): sp bad type");
  791. sp = res.ival;
  792. n = av[2];
  793. expr(n, &res);
  794. if(res.type != TINT)
  795. error("strace(pc, sp, link): link bad type");
  796. tracelist = 0;
  797. if ((*machdata->ctrace)(cormap, pc, sp, res.ival, trlist) <= 0)
  798. error("no stack frame: %r");
  799. r->type = TLIST;
  800. r->l = tracelist;
  801. }
  802. void
  803. regerror(char *msg)
  804. {
  805. error(msg);
  806. }
  807. void
  808. regexp(Node *r, Node *args)
  809. {
  810. Node res;
  811. Reprog *rp;
  812. Node *av[Maxarg];
  813. na = 0;
  814. flatten(av, args);
  815. if(na != 2)
  816. error("regexp(pattern, string): arg count");
  817. expr(av[0], &res);
  818. if(res.type != TSTRING)
  819. error("regexp(pattern, string): pattern must be string");
  820. rp = regcomp(res.string->string);
  821. if(rp == 0)
  822. return;
  823. expr(av[1], &res);
  824. if(res.type != TSTRING)
  825. error("regexp(pattern, string): bad string");
  826. r->fmt = 'D';
  827. r->type = TINT;
  828. r->ival = regexec(rp, res.string->string, 0, 0);
  829. free(rp);
  830. }
  831. void
  832. fmt(Node *r, Node *args)
  833. {
  834. Node res;
  835. Node *av[Maxarg];
  836. na = 0;
  837. flatten(av, args);
  838. if(na != 2)
  839. error("fmt(obj, fmt): arg count");
  840. expr(av[1], &res);
  841. if(res.type != TINT || strchr(vfmt, res.ival) == 0)
  842. error("fmt(obj, fmt): bad format '%c'", (char)res.ival);
  843. expr(av[0], r);
  844. r->fmt = res.ival;
  845. }
  846. void
  847. patom(char type, Store *res)
  848. {
  849. int i;
  850. char buf[512];
  851. extern char *typenames[];
  852. switch(res->fmt) {
  853. case 'c':
  854. Bprint(bout, "%c", (int)res->ival);
  855. break;
  856. case 'C':
  857. if(res->ival < ' ' || res->ival >= 0x7f)
  858. Bprint(bout, "%3d", (int)res->ival&0xff);
  859. else
  860. Bprint(bout, "%3c", (int)res->ival);
  861. break;
  862. case 'r':
  863. Bprint(bout, "%C", (int)res->ival);
  864. break;
  865. case 'B':
  866. memset(buf, '0', 34);
  867. buf[1] = 'b';
  868. for(i = 0; i < 32; i++) {
  869. if(res->ival & (1<<i))
  870. buf[33-i] = '1';
  871. }
  872. buf[35] = '\0';
  873. Bprint(bout, "%s", buf);
  874. break;
  875. case 'b':
  876. Bprint(bout, "%.2x", (int)res->ival&0xff);
  877. break;
  878. case 'X':
  879. Bprint(bout, "%.8lux", (ulong)res->ival);
  880. break;
  881. case 'x':
  882. Bprint(bout, "%.4lux", (ulong)res->ival&0xffff);
  883. break;
  884. case 'D':
  885. Bprint(bout, "%d", (int)res->ival);
  886. break;
  887. case 'd':
  888. Bprint(bout, "%d", (ushort)res->ival);
  889. break;
  890. case 'u':
  891. Bprint(bout, "%d", (int)res->ival&0xffff);
  892. break;
  893. case 'U':
  894. Bprint(bout, "%lud", (ulong)res->ival);
  895. break;
  896. case 'Z':
  897. Bprint(bout, "%llud", res->ival);
  898. break;
  899. case 'V':
  900. Bprint(bout, "%lld", res->ival);
  901. break;
  902. case 'W':
  903. Bprint(bout, "%.8llux", res->ival);
  904. break;
  905. case 'Y':
  906. Bprint(bout, "%.16llux", res->ival);
  907. break;
  908. case 'o':
  909. Bprint(bout, "0%.11uo", (int)res->ival&0xffff);
  910. break;
  911. case 'O':
  912. Bprint(bout, "0%.6uo", (int)res->ival);
  913. break;
  914. case 'q':
  915. Bprint(bout, "0%.11o", (short)(res->ival&0xffff));
  916. break;
  917. case 'Q':
  918. Bprint(bout, "0%.6o", (int)res->ival);
  919. break;
  920. case 'f':
  921. case 'F':
  922. if(type != TFLOAT)
  923. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  924. else
  925. Bprint(bout, "%g", res->fval);
  926. break;
  927. case 's':
  928. case 'g':
  929. case 'G':
  930. if(type != TSTRING)
  931. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  932. else
  933. Bwrite(bout, res->string->string, res->string->len);
  934. break;
  935. case 'R':
  936. if(type != TSTRING)
  937. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  938. else
  939. Bprint(bout, "%S", (Rune*)res->string->string);
  940. break;
  941. case 'a':
  942. case 'A':
  943. symoff(buf, sizeof(buf), res->ival, CANY);
  944. Bprint(bout, "%s", buf);
  945. break;
  946. case 'I':
  947. case 'i':
  948. if(type != TINT)
  949. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  950. else {
  951. if (symmap == nil || (*machdata->das)(symmap, res->ival, res->fmt, buf, sizeof(buf)) < 0)
  952. Bprint(bout, "no instruction");
  953. else
  954. Bprint(bout, "%s", buf);
  955. }
  956. break;
  957. }
  958. }
  959. void
  960. blprint(List *l)
  961. {
  962. Bprint(bout, "{");
  963. while(l) {
  964. switch(l->type) {
  965. default:
  966. patom(l->type, &l->Store);
  967. break;
  968. case TSTRING:
  969. Bputc(bout, '"');
  970. patom(l->type, &l->Store);
  971. Bputc(bout, '"');
  972. break;
  973. case TLIST:
  974. blprint(l->l);
  975. break;
  976. case TCODE:
  977. pcode(l->cc, 0);
  978. break;
  979. }
  980. l = l->next;
  981. if(l)
  982. Bprint(bout, ", ");
  983. }
  984. Bprint(bout, "}");
  985. }
  986. int
  987. comx(Node res)
  988. {
  989. Lsym *sl;
  990. Node *n, xx;
  991. if(res.fmt != 'a' && res.fmt != 'A')
  992. return 0;
  993. if(res.comt == 0 || res.comt->base == 0)
  994. return 0;
  995. sl = res.comt->base;
  996. if(sl->proc) {
  997. res.left = ZN;
  998. res.right = ZN;
  999. n = an(ONAME, ZN, ZN);
  1000. n->sym = sl;
  1001. n = an(OCALL, n, &res);
  1002. n->left->sym = sl;
  1003. expr(n, &xx);
  1004. return 1;
  1005. }
  1006. print("(%s)", sl->name);
  1007. return 0;
  1008. }
  1009. void
  1010. bprint(Node *r, Node *args)
  1011. {
  1012. int i, nas;
  1013. Node res, *av[Maxarg];
  1014. USED(r);
  1015. na = 0;
  1016. flatten(av, args);
  1017. nas = na;
  1018. for(i = 0; i < nas; i++) {
  1019. expr(av[i], &res);
  1020. switch(res.type) {
  1021. default:
  1022. if(comx(res))
  1023. break;
  1024. patom(res.type, &res.Store);
  1025. break;
  1026. case TCODE:
  1027. pcode(res.cc, 0);
  1028. break;
  1029. case TLIST:
  1030. blprint(res.l);
  1031. break;
  1032. }
  1033. }
  1034. if(ret == 0)
  1035. Bputc(bout, '\n');
  1036. }
  1037. void
  1038. printto(Node *r, Node *args)
  1039. {
  1040. int fd;
  1041. Biobuf *b;
  1042. int i, nas;
  1043. Node res, *av[Maxarg];
  1044. USED(r);
  1045. na = 0;
  1046. flatten(av, args);
  1047. nas = na;
  1048. expr(av[0], &res);
  1049. if(res.type != TSTRING)
  1050. error("printto(string, ...): need string");
  1051. fd = create(res.string->string, OWRITE, 0666);
  1052. if(fd < 0)
  1053. fd = open(res.string->string, OWRITE);
  1054. if(fd < 0)
  1055. error("printto: open %s: %r", res.string->string);
  1056. b = gmalloc(sizeof(Biobuf));
  1057. Binit(b, fd, OWRITE);
  1058. Bflush(bout);
  1059. io[iop++] = bout;
  1060. bout = b;
  1061. for(i = 1; i < nas; i++) {
  1062. expr(av[i], &res);
  1063. switch(res.type) {
  1064. default:
  1065. if(comx(res))
  1066. break;
  1067. patom(res.type, &res.Store);
  1068. break;
  1069. case TLIST:
  1070. blprint(res.l);
  1071. break;
  1072. }
  1073. }
  1074. if(ret == 0)
  1075. Bputc(bout, '\n');
  1076. Bterm(b);
  1077. close(fd);
  1078. free(b);
  1079. bout = io[--iop];
  1080. }
  1081. void
  1082. pcfile(Node *r, Node *args)
  1083. {
  1084. Node res;
  1085. char *p, buf[128];
  1086. if(args == 0)
  1087. error("pcfile(addr): arg count");
  1088. expr(args, &res);
  1089. if(res.type != TINT)
  1090. error("pcfile(addr): arg type");
  1091. r->type = TSTRING;
  1092. r->fmt = 's';
  1093. if(fileline(buf, sizeof(buf), res.ival) == 0) {
  1094. r->string = strnode("?file?");
  1095. return;
  1096. }
  1097. p = strrchr(buf, ':');
  1098. if(p == 0)
  1099. error("pcfile(addr): funny file %s", buf);
  1100. *p = '\0';
  1101. r->string = strnode(buf);
  1102. }
  1103. void
  1104. pcline(Node *r, Node *args)
  1105. {
  1106. Node res;
  1107. char *p, buf[128];
  1108. if(args == 0)
  1109. error("pcline(addr): arg count");
  1110. expr(args, &res);
  1111. if(res.type != TINT)
  1112. error("pcline(addr): arg type");
  1113. r->type = TINT;
  1114. r->fmt = 'D';
  1115. if(fileline(buf, sizeof(buf), res.ival) == 0) {
  1116. r->ival = 0;
  1117. return;
  1118. }
  1119. p = strrchr(buf, ':');
  1120. if(p == 0)
  1121. error("pcline(addr): funny file %s", buf);
  1122. r->ival = strtol(p+1, 0, 0);
  1123. }