builtin.c 22 KB

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