n3.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. /*
  10. * troff3.c
  11. *
  12. * macro and string routines, storage allocation
  13. */
  14. #include "tdef.h"
  15. #include "fns.h"
  16. #include "ext.h"
  17. Tchar *argtop;
  18. int pagech = '%';
  19. int strflg;
  20. #define MHASHSIZE 128 /* must be 2**n */
  21. #define MHASH(x) ((x>>6)^x) & (MHASHSIZE-1)
  22. Contab *mhash[MHASHSIZE];
  23. Blockp *blist; /* allocated blocks for macros and strings */
  24. int nblist; /* how many there are */
  25. int bfree = -1; /* first (possible) free block in the list */
  26. Contab *contabp = NULL;
  27. #define MDELTA 500
  28. int nm = 0;
  29. int savname; /* name of macro/string being defined */
  30. int savslot; /* place in Contab of savname */
  31. int freeslot = -1; /* first (possible) free slot in contab */
  32. void prcontab(Contab *p)
  33. {
  34. int i;
  35. for (i = 0; i < nm; i++)
  36. if (p)
  37. if (p[i].rq != 0)
  38. fprintf(stderr, "slot %d, %-2.2s\n", i, unpair(p[i].rq));
  39. else
  40. fprintf(stderr, "slot %d empty\n", i);
  41. else
  42. fprintf(stderr, "slot %d empty\n", i);
  43. }
  44. void blockinit(void)
  45. {
  46. blist = (Blockp *) calloc(NBLIST, sizeof(Blockp));
  47. if (blist == NULL) {
  48. ERROR "not enough room for %d blocks", NBLIST WARN;
  49. done2(1);
  50. }
  51. nblist = NBLIST;
  52. blist[0].nextoff = blist[1].nextoff = -1;
  53. blist[0].bp = (Tchar *) calloc(BLK, sizeof(Tchar));
  54. blist[1].bp = (Tchar *) calloc(BLK, sizeof(Tchar));
  55. /* -1 prevents blist[0] from being used; temporary fix */
  56. /* for a design botch: offset==0 is overloaded. */
  57. /* blist[1] reserved for .rd indicator -- also unused. */
  58. /* but someone unwittingly looks at these, so allocate something */
  59. bfree = 2;
  60. }
  61. char *grow(char *ptr, int num, int size) /* make array bigger */
  62. {
  63. char *p;
  64. if (ptr == NULL)
  65. p = (char *) calloc(num, size);
  66. else
  67. p = (char *) realloc(ptr, num * size);
  68. return p;
  69. }
  70. void mnspace(void)
  71. {
  72. nm = sizeof(contab)/sizeof(Contab) + MDELTA;
  73. freeslot = sizeof(contab)/sizeof(Contab) + 1;
  74. contabp = (Contab *) grow((char *) contabp, nm, sizeof(Contab));
  75. if (contabp == NULL) {
  76. ERROR "not enough memory for namespace of %d marcos", nm WARN;
  77. exit(1);
  78. }
  79. contabp = (Contab *) memcpy((char *) contabp, (char *)contab,
  80. sizeof(contab));
  81. if (contabp == NULL) {
  82. ERROR "Cannot reinitialize macro/request name list" WARN;
  83. exit(1);
  84. }
  85. }
  86. void caseig(void)
  87. {
  88. int i;
  89. Offset oldoff = offset;
  90. offset = 0;
  91. i = copyb();
  92. offset = oldoff;
  93. if (i != '.')
  94. control(i, 1);
  95. }
  96. void casern(void)
  97. {
  98. int i, j, k;
  99. lgf++;
  100. skip();
  101. if ((i = getrq()) == 0 || (oldmn = findmn(i)) < 0)
  102. return;
  103. skip();
  104. clrmn(findmn(j = getrq()));
  105. if (j) {
  106. munhash(&contabp[oldmn]);
  107. contabp[oldmn].rq = j;
  108. maddhash(&contabp[oldmn]);
  109. if (dip != d )
  110. for (k = dilev; k; k--)
  111. if (d[k].curd == i)
  112. d[k].curd = j;
  113. }
  114. }
  115. void maddhash(Contab *rp)
  116. {
  117. Contab **hp;
  118. if (rp->rq == 0)
  119. return;
  120. hp = &mhash[MHASH(rp->rq)];
  121. rp->link = *hp;
  122. *hp = rp;
  123. }
  124. void munhash(Contab *mp)
  125. {
  126. Contab *p;
  127. Contab **lp;
  128. if (mp->rq == 0)
  129. return;
  130. lp = &mhash[MHASH(mp->rq)];
  131. p = *lp;
  132. while (p) {
  133. if (p == mp) {
  134. *lp = p->link;
  135. p->link = 0;
  136. return;
  137. }
  138. lp = &p->link;
  139. p = p->link;
  140. }
  141. }
  142. void mrehash(void)
  143. {
  144. Contab *p;
  145. int i;
  146. for (i=0; i < MHASHSIZE; i++)
  147. mhash[i] = 0;
  148. for (p=contabp; p < &contabp[nm]; p++)
  149. p->link = 0;
  150. for (p=contabp; p < &contabp[nm]; p++) {
  151. if (p->rq == 0)
  152. continue;
  153. i = MHASH(p->rq);
  154. p->link = mhash[i];
  155. mhash[i] = p;
  156. }
  157. }
  158. void caserm(void)
  159. {
  160. int j, k;
  161. lgf++;
  162. g0:
  163. while (!skip() && (j = getrq()) != 0) {
  164. if (dip != d)
  165. for (k = dilev; k; k--)
  166. if (d[k].curd == j) {
  167. ERROR "cannot remove diversion %s during definition",
  168. unpair(j) WARN;
  169. goto g0;
  170. }
  171. clrmn(findmn(j));
  172. }
  173. lgf--;
  174. }
  175. void caseas(void)
  176. {
  177. app++;
  178. caseds();
  179. }
  180. void caseds(void)
  181. {
  182. ds++;
  183. casede();
  184. }
  185. void caseam(void)
  186. {
  187. app++;
  188. casede();
  189. }
  190. void casede(void)
  191. {
  192. int i, req;
  193. Offset savoff;
  194. req = '.';
  195. lgf++;
  196. skip();
  197. if ((i = getrq()) == 0)
  198. goto de1;
  199. if ((offset = finds(i)) == 0)
  200. goto de1;
  201. if (newmn)
  202. savslot = newmn;
  203. else
  204. savslot = findmn(i);
  205. savname = i;
  206. if (ds)
  207. copys();
  208. else
  209. req = copyb();
  210. clrmn(oldmn);
  211. if (newmn) {
  212. if (contabp[newmn].rq)
  213. munhash(&contabp[newmn]);
  214. contabp[newmn].rq = i;
  215. maddhash(&contabp[newmn]);
  216. }
  217. if (apptr) {
  218. savoff = offset;
  219. offset = apptr;
  220. wbf((Tchar) IMP);
  221. offset = savoff; /* pointless */
  222. }
  223. offset = dip->op;
  224. if (req != '.')
  225. control(req, 1);
  226. de1:
  227. ds = app = 0;
  228. }
  229. int findmn(int i)
  230. {
  231. Contab *p;
  232. for (p = mhash[MHASH(i)]; p; p = p->link)
  233. if (i == p->rq)
  234. return(p - contabp);
  235. return(-1);
  236. }
  237. void clrmn(int i)
  238. {
  239. if (i >= 0) {
  240. if (contabp[i].mx)
  241. ffree(contabp[i].mx);
  242. munhash(&contabp[i]);
  243. contabp[i].rq = 0;
  244. contabp[i].mx = 0;
  245. contabp[i].emx = 0;
  246. contabp[i].f = 0;
  247. if (contabp[i].divsiz != NULL) {
  248. free(contabp[i].divsiz);
  249. contabp[i].divsiz = NULL;
  250. }
  251. if (freeslot > i)
  252. freeslot = i;
  253. }
  254. }
  255. void growcontab(void)
  256. {
  257. nm += MDELTA;
  258. contabp = (Contab *) grow((char *) contabp , nm, sizeof(Contab));
  259. if (contabp == NULL) {
  260. ERROR "Too many (%d) string/macro names", nm WARN;
  261. done2(02);
  262. } else {
  263. memset((char *)(contabp) + (nm - MDELTA) * sizeof(Contab),
  264. 0, MDELTA * sizeof(Contab));
  265. mrehash();
  266. }
  267. }
  268. Offset finds(int mn)
  269. {
  270. int i;
  271. Offset savip;
  272. oldmn = findmn(mn);
  273. newmn = 0;
  274. apptr = 0;
  275. if (app && oldmn >= 0 && contabp[oldmn].mx) {
  276. savip = ip;
  277. ip = contabp[oldmn].emx;
  278. oldmn = -1;
  279. apptr = ip;
  280. if (!diflg)
  281. ip = incoff(ip);
  282. nextb = ip;
  283. ip = savip;
  284. } else {
  285. for (i = freeslot; i < nm; i++) {
  286. if (contabp[i].rq == 0)
  287. break;
  288. }
  289. if (i == nm)
  290. growcontab();
  291. freeslot = i + 1;
  292. if ((nextb = alloc()) == -1) {
  293. app = 0;
  294. if (macerr++ > 1)
  295. done2(02);
  296. if (nextb == 0)
  297. ERROR "Not enough space for string/macro names" WARN;
  298. edone(04);
  299. return(offset = 0);
  300. }
  301. contabp[i].mx = nextb;
  302. if (!diflg) {
  303. newmn = i;
  304. if (oldmn == -1)
  305. contabp[i].rq = -1;
  306. } else {
  307. contabp[i].rq = mn;
  308. maddhash(&contabp[i]);
  309. }
  310. }
  311. app = 0;
  312. return(offset = nextb);
  313. }
  314. int skip(void)
  315. {
  316. Tchar i;
  317. while (cbits(i = getch()) == ' ' || ismot(i))
  318. ;
  319. ch = i;
  320. return(nlflg);
  321. }
  322. int copyb(void)
  323. {
  324. int i, j, state;
  325. Tchar ii;
  326. int req, k;
  327. Offset savoff;
  328. Uchar *p;
  329. if (skip() || !(j = getrq()))
  330. j = '.';
  331. req = j;
  332. p = unpair(j);
  333. /* was: k = j >> BYTE; j &= BYTEMASK; */
  334. j = p[0];
  335. k = p[1];
  336. copyf++;
  337. flushi();
  338. nlflg = 0;
  339. state = 1;
  340. savoff = 0;
  341. /* state 0 eat up
  342. * state 1 look for .
  343. * state 2 look for first char of end macro
  344. * state 3 look for second char of end macro
  345. */
  346. while (1) {
  347. i = cbits(ii = getch());
  348. if (state == 3) {
  349. if (i == k)
  350. break;
  351. if (!k) {
  352. ch = ii;
  353. i = getach();
  354. ch = ii;
  355. if (!i)
  356. break;
  357. }
  358. state = 0;
  359. goto c0;
  360. }
  361. if (i == '\n') {
  362. state = 1;
  363. nlflg = 0;
  364. goto c0;
  365. }
  366. if (state == 1 && i == '.') {
  367. state++;
  368. savoff = offset;
  369. goto c0;
  370. }
  371. if (state == 2 && i == j) {
  372. state++;
  373. goto c0;
  374. }
  375. state = 0;
  376. c0:
  377. if (offset)
  378. wbf(ii);
  379. }
  380. if (offset) {
  381. offset = savoff;
  382. wbf((Tchar)0);
  383. }
  384. copyf--;
  385. return(req);
  386. }
  387. void copys(void)
  388. {
  389. Tchar i;
  390. copyf++;
  391. if (skip())
  392. goto c0;
  393. if (cbits(i = getch()) != '"')
  394. wbf(i);
  395. while (cbits(i = getch()) != '\n')
  396. wbf(i);
  397. c0:
  398. wbf((Tchar)0);
  399. copyf--;
  400. }
  401. Offset alloc(void) /* return free Offset in nextb */
  402. {
  403. int i, j;
  404. for (i = bfree; i < nblist; i++)
  405. if (blist[i].nextoff == 0)
  406. break;
  407. if (i == nblist) {
  408. blist = (Blockp *) realloc((char *) blist,
  409. 2 * nblist * sizeof(Blockp));
  410. if (blist == NULL) {
  411. ERROR "can't grow blist for string/macro defns" WARN;
  412. done2(2);
  413. }
  414. nblist *= 2;
  415. for (j = i; j < nblist; j++) {
  416. blist[j].nextoff = 0;
  417. blist[j].bp = 0;
  418. }
  419. }
  420. blist[i].nextoff = -1; /* this block is the end */
  421. bfree = i + 1;
  422. if (blist[i].bp == 0)
  423. blist[i].bp = (Tchar *) calloc(BLK, sizeof(Tchar));
  424. if (blist[i].bp == NULL) {
  425. ERROR "can't allocate memory for string/macro definitions" WARN;
  426. done2(2);
  427. }
  428. nextb = (Offset) i * BLK;
  429. return nextb;
  430. }
  431. void ffree(Offset i) /* free list of blocks starting at blist(o) */
  432. { /* (doesn't actually free the blocks, just the pointers) */
  433. int j;
  434. for ( ; blist[j = bindex(i)].nextoff != -1; ) {
  435. if (bfree > j)
  436. bfree = j;
  437. i = blist[j].nextoff;
  438. blist[j].nextoff = 0;
  439. }
  440. blist[j].nextoff = 0;
  441. }
  442. void wbf(Tchar i) /* store i into offset, get ready for next one */
  443. {
  444. int j, off;
  445. if (!offset)
  446. return;
  447. j = bindex(offset);
  448. if (i == 0)
  449. contabp[savslot].emx = offset;
  450. off = boffset(offset);
  451. blist[j].bp[off] = i;
  452. offset++;
  453. if (pastend(offset)) { /* off the end of this block */
  454. if (blist[j].nextoff == -1) {
  455. if ((nextb = alloc()) == -1) {
  456. ERROR "Out of temp file space" WARN;
  457. done2(01);
  458. }
  459. blist[j].nextoff = nextb;
  460. }
  461. offset = blist[j].nextoff;
  462. }
  463. }
  464. Tchar rbf(void) /* return next char from blist[] block */
  465. {
  466. Tchar i, j;
  467. if (ip == RD_OFFSET) { /* for rdtty */
  468. if (j = rdtty())
  469. return(j);
  470. else
  471. return(popi());
  472. }
  473. i = rbf0(ip);
  474. if (i == 0) {
  475. if (!app)
  476. i = popi();
  477. return(i);
  478. }
  479. ip = incoff(ip);
  480. return(i);
  481. }
  482. Offset xxxincoff(Offset p) /* get next blist[] block */
  483. {
  484. p++;
  485. if (pastend(p)) { /* off the end of this block */
  486. if ((p = blist[bindex(p-1)].nextoff) == -1) { /* and nothing was allocated after it */
  487. ERROR "Bad storage allocation" WARN;
  488. done2(-5);
  489. }
  490. }
  491. return(p);
  492. }
  493. Tchar popi(void)
  494. {
  495. Stack *p;
  496. if (frame == stk)
  497. return(0);
  498. if (strflg)
  499. strflg--;
  500. p = nxf = frame;
  501. p->nargs = 0;
  502. frame = p->pframe;
  503. ip = p->pip;
  504. pendt = p->ppendt;
  505. lastpbp = p->lastpbp;
  506. return(p->pch);
  507. }
  508. /*
  509. * test that the end of the allocation is above a certain location
  510. * in memory
  511. */
  512. #define SPACETEST(base, size) \
  513. if ((char*)base + size >= (char*)stk+STACKSIZE) \
  514. ERROR "Stacksize overflow in n3" WARN
  515. Offset pushi(Offset newip, int mname)
  516. {
  517. Stack *p;
  518. SPACETEST(nxf, sizeof(Stack));
  519. p = nxf;
  520. p->pframe = frame;
  521. p->pip = ip;
  522. p->ppendt = pendt;
  523. p->pch = ch;
  524. p->lastpbp = lastpbp;
  525. p->mname = mname;
  526. lastpbp = pbp;
  527. pendt = ch = 0;
  528. frame = nxf;
  529. if (nxf->nargs == 0)
  530. nxf += 1;
  531. else
  532. nxf = (Stack *)argtop;
  533. return(ip = newip);
  534. }
  535. void *setbrk(int x)
  536. {
  537. char *i;
  538. if ((i = (char *) calloc(x, 1)) == 0) {
  539. ERROR "Core limit reached" WARN;
  540. edone(0100);
  541. }
  542. return(i);
  543. }
  544. int getsn(void)
  545. {
  546. int i;
  547. if ((i = getach()) == 0)
  548. return(0);
  549. if (i == '(')
  550. return(getrq());
  551. else
  552. return(i);
  553. }
  554. Offset setstr(void)
  555. {
  556. int i, j;
  557. lgf++;
  558. if ((i = getsn()) == 0 || (j = findmn(i)) == -1 || !contabp[j].mx) {
  559. lgf--;
  560. return(0);
  561. } else {
  562. SPACETEST(nxf, sizeof(Stack));
  563. nxf->nargs = 0;
  564. strflg++;
  565. lgf--;
  566. return pushi(contabp[j].mx, i);
  567. }
  568. }
  569. void collect(void)
  570. {
  571. int j;
  572. Tchar i, *strp, *lim, **argpp, **argppend;
  573. int quote;
  574. Stack *savnxf;
  575. copyf++;
  576. nxf->nargs = 0;
  577. savnxf = nxf;
  578. if (skip())
  579. goto rtn;
  580. {
  581. char *memp;
  582. memp = (char *)savnxf;
  583. /*
  584. * 1 s structure for the macro descriptor
  585. * APERMAC Tchar *'s for pointers into the strings
  586. * space for the Tchar's themselves
  587. */
  588. memp += sizeof(Stack);
  589. /*
  590. * CPERMAC = the total # of characters for ALL arguments
  591. */
  592. #define CPERMAC 200
  593. #define APERMAC 9
  594. memp += APERMAC * sizeof(Tchar *);
  595. memp += CPERMAC * sizeof(Tchar);
  596. nxf = (Stack *)memp;
  597. }
  598. lim = (Tchar *)nxf;
  599. argpp = (Tchar **)(savnxf + 1);
  600. argppend = &argpp[APERMAC];
  601. SPACETEST(argppend, sizeof(Tchar *));
  602. strp = (Tchar *)argppend;
  603. /*
  604. * Zero out all the string pointers before filling them in.
  605. */
  606. for (j = 0; j < APERMAC; j++)
  607. argpp[j] = 0;
  608. /* ERROR "savnxf=0x%x,nxf=0x%x,argpp=0x%x,strp=argppend=0x%x, lim=0x%x",
  609. * savnxf, nxf, argpp, strp, lim WARN;
  610. */
  611. strflg = 0;
  612. while (argpp != argppend && !skip()) {
  613. *argpp++ = strp;
  614. quote = 0;
  615. if (cbits(i = getch()) == '"')
  616. quote++;
  617. else
  618. ch = i;
  619. while (1) {
  620. i = getch();
  621. /* fprintf(stderr, "collect %c %d\n", cbits(i), cbits(i)); */
  622. if (nlflg || (!quote && argpp != argppend && cbits(i) == ' '))
  623. break; /* collects rest into $9 */
  624. if ( quote
  625. && cbits(i) == '"'
  626. && cbits(i = getch()) != '"') {
  627. ch = i;
  628. break;
  629. }
  630. *strp++ = i;
  631. if (strflg && strp >= lim) {
  632. /* ERROR "strp=0x%x, lim = 0x%x", strp, lim WARN; */
  633. ERROR "Macro argument too long" WARN;
  634. copyf--;
  635. edone(004);
  636. }
  637. SPACETEST(strp, 3 * sizeof(Tchar));
  638. }
  639. *strp++ = 0;
  640. }
  641. nxf = savnxf;
  642. nxf->nargs = argpp - (Tchar **)(savnxf + 1);
  643. argtop = strp;
  644. rtn:
  645. copyf--;
  646. }
  647. void seta(void)
  648. {
  649. int i;
  650. i = cbits(getch()) - '0';
  651. if (i > 0 && i <= APERMAC && i <= frame->nargs)
  652. pushback(*(((Tchar **)(frame + 1)) + i - 1));
  653. }
  654. void caseda(void)
  655. {
  656. app++;
  657. casedi();
  658. }
  659. void casegd(void)
  660. {
  661. int i, j;
  662. skip();
  663. if ((i = getrq()) == 0)
  664. return;
  665. if ((j = findmn(i)) >= 0) {
  666. if (contabp[j].divsiz != NULL) {
  667. numtabp[DN].val = contabp[j].divsiz->dix;
  668. numtabp[DL].val = contabp[j].divsiz->diy;
  669. }
  670. }
  671. }
  672. #define FINDDIV(o) if ((o = findmn(dip->curd)) < 0) \
  673. ERROR "lost diversion %s", unpair(dip->curd) WARN
  674. void casedi(void)
  675. {
  676. int i, j, *k;
  677. lgf++;
  678. if (skip() || (i = getrq()) == 0) {
  679. if (dip != d) {
  680. FINDDIV(savslot);
  681. wbf((Tchar)0);
  682. }
  683. if (dilev > 0) {
  684. numtabp[DN].val = dip->dnl;
  685. numtabp[DL].val = dip->maxl;
  686. FINDDIV(j);
  687. if ((contabp[j].divsiz = (Divsiz *) malloc(sizeof(Divsiz))) == NULL) {
  688. ERROR "Cannot alloc diversion size" WARN;
  689. done2(1);
  690. } else {
  691. contabp[j].divsiz->dix = numtabp[DN].val;
  692. contabp[j].divsiz->diy = numtabp[DL].val;
  693. }
  694. dip = &d[--dilev];
  695. offset = dip->op;
  696. }
  697. goto rtn;
  698. }
  699. if (++dilev == NDI) {
  700. --dilev;
  701. ERROR "Diversions nested too deep" WARN;
  702. edone(02);
  703. }
  704. if (dip != d) {
  705. FINDDIV(j);
  706. savslot = j;
  707. wbf((Tchar)0);
  708. }
  709. diflg++;
  710. dip = &d[dilev];
  711. dip->op = finds(i);
  712. dip->curd = i;
  713. clrmn(oldmn);
  714. k = (int *) & dip->dnl;
  715. for (j = 0; j < 10; j++)
  716. k[j] = 0; /*not op and curd*/
  717. rtn:
  718. app = 0;
  719. diflg = 0;
  720. }
  721. void casedt(void)
  722. {
  723. lgf++;
  724. dip->dimac = dip->ditrap = dip->ditf = 0;
  725. skip();
  726. dip->ditrap = vnumb((int *)0);
  727. if (nonumb)
  728. return;
  729. skip();
  730. dip->dimac = getrq();
  731. }
  732. #define LNSIZE 4000
  733. void casetl(void)
  734. {
  735. int j;
  736. int w[3];
  737. Tchar buf[LNSIZE];
  738. Tchar *tp;
  739. Tchar i, delim;
  740. /*
  741. * bug fix
  742. *
  743. * if .tl is the first thing in the file, the p1
  744. * doesn't come out, also the pagenumber will be 0
  745. *
  746. * tends too confuse the device filter (and the user as well)
  747. */
  748. if (dip == d && numtabp[NL].val == -1)
  749. newline(1);
  750. dip->nls = 0;
  751. skip();
  752. if (ismot(delim = getch())) {
  753. ch = delim;
  754. delim = '\'';
  755. } else
  756. delim = cbits(delim);
  757. tp = buf;
  758. numtabp[HP].val = 0;
  759. w[0] = w[1] = w[2] = 0;
  760. j = 0;
  761. while (cbits(i = getch()) != '\n') {
  762. if (cbits(i) == cbits(delim)) {
  763. if (j < 3)
  764. w[j] = numtabp[HP].val;
  765. numtabp[HP].val = 0;
  766. if (w[j] != 0)
  767. *tp++ = WORDSP;
  768. j++;
  769. *tp++ = 0;
  770. } else {
  771. if (cbits(i) == pagech) {
  772. setn1(numtabp[PN].val, numtabp[findr('%')].fmt,
  773. i&SFMASK);
  774. continue;
  775. }
  776. numtabp[HP].val += width(i);
  777. if (tp < &buf[LNSIZE-10]) {
  778. if (cbits(i) == ' ' && *tp != WORDSP)
  779. *tp++ = WORDSP;
  780. *tp++ = i;
  781. } else {
  782. ERROR "Overflow in casetl" WARN;
  783. }
  784. }
  785. }
  786. if (j<3)
  787. w[j] = numtabp[HP].val;
  788. *tp++ = 0;
  789. *tp++ = 0;
  790. *tp = 0;
  791. tp = buf;
  792. if (NROFF)
  793. horiz(po);
  794. while (i = *tp++)
  795. pchar(i);
  796. if (w[1] || w[2])
  797. horiz(j = quant((lt - w[1]) / 2 - w[0], HOR));
  798. while (i = *tp++)
  799. pchar(i);
  800. if (w[2]) {
  801. horiz(lt - w[0] - w[1] - w[2] - j);
  802. while (i = *tp++)
  803. pchar(i);
  804. }
  805. newline(0);
  806. if (dip != d) {
  807. if (dip->dnl > dip->hnl)
  808. dip->hnl = dip->dnl;
  809. } else {
  810. if (numtabp[NL].val > dip->hnl)
  811. dip->hnl = numtabp[NL].val;
  812. }
  813. }
  814. void casepc(void)
  815. {
  816. pagech = chget(IMP);
  817. }
  818. void casepm(void)
  819. {
  820. int i, k;
  821. int xx, cnt, tcnt, kk, tot;
  822. Offset j;
  823. kk = cnt = tcnt = 0;
  824. tot = !skip();
  825. stackdump();
  826. for (i = 0; i < nm; i++) {
  827. if ((xx = contabp[i].rq) == 0 || contabp[i].mx == 0)
  828. continue;
  829. tcnt++;
  830. j = contabp[i].mx;
  831. for (k = 1; (j = blist[bindex(j)].nextoff) != -1; )
  832. k++;
  833. cnt++;
  834. kk += k;
  835. if (!tot)
  836. fprintf(stderr, "%-2.2s %d\n", unpair(xx), k);
  837. }
  838. fprintf(stderr, "pm: total %d, macros %d, space %d\n", tcnt, cnt, kk);
  839. }
  840. void stackdump(void) /* dumps stack of macros in process */
  841. {
  842. Stack *p;
  843. if (frame != stk) {
  844. fprintf(stderr, "stack: ");
  845. for (p = frame; p != stk; p = p->pframe)
  846. fprintf(stderr, "%s ", unpair(p->mname));
  847. fprintf(stderr, "\n");
  848. }
  849. }