builtin.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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[] = "aBbcCdDfFgGiIoOqQrRsSuUVWxXYZ38";
  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. case '3':
  923. case '8':
  924. if(type != TFLOAT)
  925. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  926. else
  927. Bprint(bout, "%g", res->fval);
  928. break;
  929. case 's':
  930. case 'g':
  931. case 'G':
  932. if(type != TSTRING)
  933. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  934. else
  935. Bwrite(bout, res->string->string, res->string->len);
  936. break;
  937. case 'R':
  938. if(type != TSTRING)
  939. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  940. else
  941. Bprint(bout, "%S", (Rune*)res->string->string);
  942. break;
  943. case 'a':
  944. case 'A':
  945. symoff(buf, sizeof(buf), res->ival, CANY);
  946. Bprint(bout, "%s", buf);
  947. break;
  948. case 'I':
  949. case 'i':
  950. if(type != TINT)
  951. Bprint(bout, "*%c<%s>*", res->fmt, typenames[type]);
  952. else {
  953. if (symmap == nil || (*machdata->das)(symmap, res->ival, res->fmt, buf, sizeof(buf)) < 0)
  954. Bprint(bout, "no instruction");
  955. else
  956. Bprint(bout, "%s", buf);
  957. }
  958. break;
  959. }
  960. }
  961. void
  962. blprint(List *l)
  963. {
  964. Bprint(bout, "{");
  965. while(l) {
  966. switch(l->type) {
  967. default:
  968. patom(l->type, &l->Store);
  969. break;
  970. case TSTRING:
  971. Bputc(bout, '"');
  972. patom(l->type, &l->Store);
  973. Bputc(bout, '"');
  974. break;
  975. case TLIST:
  976. blprint(l->l);
  977. break;
  978. case TCODE:
  979. pcode(l->cc, 0);
  980. break;
  981. }
  982. l = l->next;
  983. if(l)
  984. Bprint(bout, ", ");
  985. }
  986. Bprint(bout, "}");
  987. }
  988. int
  989. comx(Node res)
  990. {
  991. Lsym *sl;
  992. Node *n, xx;
  993. if(res.fmt != 'a' && res.fmt != 'A')
  994. return 0;
  995. if(res.comt == 0 || res.comt->base == 0)
  996. return 0;
  997. sl = res.comt->base;
  998. if(sl->proc) {
  999. res.left = ZN;
  1000. res.right = ZN;
  1001. n = an(ONAME, ZN, ZN);
  1002. n->sym = sl;
  1003. n = an(OCALL, n, &res);
  1004. n->left->sym = sl;
  1005. expr(n, &xx);
  1006. return 1;
  1007. }
  1008. print("(%s)", sl->name);
  1009. return 0;
  1010. }
  1011. void
  1012. bprint(Node *r, Node *args)
  1013. {
  1014. int i, nas;
  1015. Node res, *av[Maxarg];
  1016. USED(r);
  1017. na = 0;
  1018. flatten(av, args);
  1019. nas = na;
  1020. for(i = 0; i < nas; i++) {
  1021. expr(av[i], &res);
  1022. switch(res.type) {
  1023. default:
  1024. if(comx(res))
  1025. break;
  1026. patom(res.type, &res.Store);
  1027. break;
  1028. case TCODE:
  1029. pcode(res.cc, 0);
  1030. break;
  1031. case TLIST:
  1032. blprint(res.l);
  1033. break;
  1034. }
  1035. }
  1036. if(ret == 0)
  1037. Bputc(bout, '\n');
  1038. }
  1039. void
  1040. printto(Node *r, Node *args)
  1041. {
  1042. int fd;
  1043. Biobuf *b;
  1044. int i, nas;
  1045. Node res, *av[Maxarg];
  1046. USED(r);
  1047. na = 0;
  1048. flatten(av, args);
  1049. nas = na;
  1050. expr(av[0], &res);
  1051. if(res.type != TSTRING)
  1052. error("printto(string, ...): need string");
  1053. fd = create(res.string->string, OWRITE, 0666);
  1054. if(fd < 0)
  1055. fd = open(res.string->string, OWRITE);
  1056. if(fd < 0)
  1057. error("printto: open %s: %r", res.string->string);
  1058. b = gmalloc(sizeof(Biobuf));
  1059. Binit(b, fd, OWRITE);
  1060. Bflush(bout);
  1061. io[iop++] = bout;
  1062. bout = b;
  1063. for(i = 1; i < nas; i++) {
  1064. expr(av[i], &res);
  1065. switch(res.type) {
  1066. default:
  1067. if(comx(res))
  1068. break;
  1069. patom(res.type, &res.Store);
  1070. break;
  1071. case TLIST:
  1072. blprint(res.l);
  1073. break;
  1074. }
  1075. }
  1076. if(ret == 0)
  1077. Bputc(bout, '\n');
  1078. Bterm(b);
  1079. close(fd);
  1080. free(b);
  1081. bout = io[--iop];
  1082. }
  1083. void
  1084. pcfile(Node *r, Node *args)
  1085. {
  1086. Node res;
  1087. char *p, buf[128];
  1088. if(args == 0)
  1089. error("pcfile(addr): arg count");
  1090. expr(args, &res);
  1091. if(res.type != TINT)
  1092. error("pcfile(addr): arg type");
  1093. r->type = TSTRING;
  1094. r->fmt = 's';
  1095. if(fileline(buf, sizeof(buf), res.ival) == 0) {
  1096. r->string = strnode("?file?");
  1097. return;
  1098. }
  1099. p = strrchr(buf, ':');
  1100. if(p == 0)
  1101. error("pcfile(addr): funny file %s", buf);
  1102. *p = '\0';
  1103. r->string = strnode(buf);
  1104. }
  1105. void
  1106. pcline(Node *r, Node *args)
  1107. {
  1108. Node res;
  1109. char *p, buf[128];
  1110. if(args == 0)
  1111. error("pcline(addr): arg count");
  1112. expr(args, &res);
  1113. if(res.type != TINT)
  1114. error("pcline(addr): arg type");
  1115. r->type = TINT;
  1116. r->fmt = 'D';
  1117. if(fileline(buf, sizeof(buf), res.ival) == 0) {
  1118. r->ival = 0;
  1119. return;
  1120. }
  1121. p = strrchr(buf, ':');
  1122. if(p == 0)
  1123. error("pcline(addr): funny file %s", buf);
  1124. r->ival = strtol(p+1, 0, 0);
  1125. }