builtin.c 20 KB

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