builtin.c 21 KB

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