n10.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. n10.c
  11. Device interfaces
  12. */
  13. #include "tdef.h"
  14. #include "ext.h"
  15. #include "fns.h"
  16. #include <ctype.h>
  17. Term t; /* terminal characteristics */
  18. int dtab;
  19. int plotmode;
  20. int esct;
  21. enum { Notype = 0, Type = 1 };
  22. static char *parse(char *s, int typeit) /* convert \0, etc to nroff driving table format */
  23. { /* typeit => add a type id to the front for later use */
  24. static char buf[100], *t, *obuf;
  25. int quote = 0;
  26. wchar_t wc;
  27. obuf = typeit == Type ? buf : buf+1;
  28. #ifdef UNICODE
  29. if (mbtowc(&wc, s, strlen(s)) > 1) { /* it's multibyte, */
  30. buf[0] = MBchar;
  31. strcpy(buf+1, s);
  32. return obuf;
  33. } /* so just hand it back */
  34. #endif /*UNICODE*/
  35. buf[0] = Troffchar;
  36. t = buf + 1;
  37. if (*s == '"') {
  38. s++;
  39. quote = 1;
  40. }
  41. for (;;) {
  42. if (quote && *s == '"') {
  43. s++; /* pointless */
  44. break;
  45. }
  46. if (!quote && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\0'))
  47. break;
  48. if (*s != '\\')
  49. *t++ = *s++;
  50. else {
  51. s++; /* skip \\ */
  52. if (isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2])) {
  53. *t++ = (s[0]-'0')<<6 | (s[1]-'0')<<3 | s[2]-'0';
  54. s += 2;
  55. } else if (isdigit(s[0])) {
  56. *t++ = *s - '0';
  57. } else if (*s == 'b') {
  58. *t++ = '\b';
  59. } else if (*s == 'n') {
  60. *t++ = '\n';
  61. } else if (*s == 'r') {
  62. *t++ = '\r';
  63. } else if (*s == 't') {
  64. *t++ = '\t';
  65. } else {
  66. *t++ = *s;
  67. }
  68. s++;
  69. }
  70. }
  71. *t = '\0';
  72. return obuf;
  73. }
  74. static int getnrfont(FILE *fp) /* read the nroff description file */
  75. {
  76. Chwid chtemp[NCHARS];
  77. static Chwid chinit;
  78. int i, nw, n, wid, code, type;
  79. char buf[100], ch[100], s1[100], s2[100];
  80. wchar_t wc;
  81. code = 0; /* no idea what this should be */
  82. chinit.wid = 1;
  83. chinit.str = "";
  84. for (i = 0; i < ALPHABET; i++) {
  85. chtemp[i] = chinit; /* zero out to begin with */
  86. chtemp[i].num = chtemp[i].code = i; /* every alphabetic character is itself */
  87. chtemp[i].wid = 1; /* default ascii widths */
  88. }
  89. skipline(fp);
  90. nw = ALPHABET;
  91. while (fgets(buf, sizeof buf, fp) != NULL) {
  92. sscanf(buf, "%s %s %[^\n]", ch, s1, s2);
  93. if (!eq(s1, "\"")) { /* genuine new character */
  94. sscanf(s1, "%d", &wid);
  95. } /* else it's a synonym for prev character, */
  96. /* so leave previous values intact */
  97. /* decide what kind of alphabet it might come from */
  98. if (strlen(ch) == 1) { /* it's ascii */
  99. n = ch[0]; /* origin includes non-graphics */
  100. chtemp[n].num = ch[0];
  101. } else if (ch[0] == '\\' && ch[1] == '0') {
  102. n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
  103. chtemp[n].num = n;
  104. #ifdef UNICODE
  105. } else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
  106. chtemp[nw].num = chadd(ch, MBchar, Install);
  107. n = nw;
  108. nw++;
  109. #endif /*UNICODE*/
  110. } else {
  111. if (strcmp(ch, "---") == 0) { /* no name */
  112. /* code used to be uninitialised here */
  113. sprintf(ch, "%d", code);
  114. type = Number;
  115. } else
  116. type = Troffchar;
  117. /* BUG in here somewhere when same character occurs twice in table */
  118. chtemp[nw].num = chadd(ch, type, Install);
  119. n = nw;
  120. nw++;
  121. }
  122. chtemp[n].wid = wid;
  123. chtemp[n].str = strdupl(parse(s2, Type));
  124. }
  125. t.tfont.nchars = nw;
  126. t.tfont.wp = (Chwid *) malloc(nw * sizeof(Chwid));
  127. if (t.tfont.wp == NULL)
  128. return -1;
  129. for (i = 0; i < nw; i++)
  130. t.tfont.wp[i] = chtemp[i];
  131. return 1;
  132. }
  133. void n_ptinit(void)
  134. {
  135. int i;
  136. char *p;
  137. char opt[50], cmd[100];
  138. FILE *fp;
  139. hmot = n_hmot;
  140. makem = n_makem;
  141. setabs = n_setabs;
  142. setch = n_setch;
  143. sethl = n_sethl;
  144. setht = n_setht;
  145. setslant = n_setslant;
  146. vmot = n_vmot;
  147. xlss = n_xlss;
  148. findft = n_findft;
  149. width = n_width;
  150. mchbits = n_mchbits;
  151. ptlead = n_ptlead;
  152. ptout = n_ptout;
  153. ptpause = n_ptpause;
  154. setfont = n_setfont;
  155. setps = n_setps;
  156. setwd = n_setwd;
  157. if ((p = getenv("NROFFTERM")) != 0)
  158. strcpy(devname, p);
  159. if (termtab[0] == 0)
  160. strcpy(termtab,DWBntermdir);
  161. if (fontdir[0] == 0)
  162. strcpy(fontdir, "");
  163. if (devname[0] == 0)
  164. strcpy(devname, NDEVNAME);
  165. pl = 11*INCH;
  166. po = PO;
  167. hyf = 0;
  168. ascii = 1;
  169. lg = 0;
  170. fontlab[1] = 'R';
  171. fontlab[2] = 'I';
  172. fontlab[3] = 'B';
  173. fontlab[4] = PAIR('B','I');
  174. fontlab[5] = 'D';
  175. bdtab[3] = 3;
  176. bdtab[4] = 3;
  177. /* hyphalg = 0; /* for testing */
  178. strcat(termtab, devname);
  179. if ((fp = fopen(termtab, "r")) == NULL) {
  180. ERROR "cannot open %s", termtab WARN;
  181. exit(-1);
  182. }
  183. /* this loop isn't robust about input format errors. */
  184. /* it assumes name, name-value pairs..., charset */
  185. /* god help us if we get out of sync. */
  186. fscanf(fp, "%s", cmd); /* should be device name... */
  187. if (!is(devname) && trace)
  188. ERROR "wrong terminal name: saw %s, wanted %s", cmd, devname WARN;
  189. for (;;) {
  190. fscanf(fp, "%s", cmd);
  191. if (is("charset"))
  192. break;
  193. fscanf(fp, " %[^\n]", opt);
  194. if (is("bset")) t.bset = atoi(opt);
  195. else if (is("breset")) t.breset = atoi(opt);
  196. else if (is("Hor")) t.Hor = atoi(opt);
  197. else if (is("Vert")) t.Vert = atoi(opt);
  198. else if (is("Newline")) t.Newline = atoi(opt);
  199. else if (is("Char")) t.Char = atoi(opt);
  200. else if (is("Em")) t.Em = atoi(opt);
  201. else if (is("Halfline")) t.Halfline = atoi(opt);
  202. else if (is("Adj")) t.Adj = atoi(opt);
  203. else if (is("twinit")) t.twinit = strdupl(parse(opt, Notype));
  204. else if (is("twrest")) t.twrest = strdupl(parse(opt, Notype));
  205. else if (is("twnl")) t.twnl = strdupl(parse(opt, Notype));
  206. else if (is("hlr")) t.hlr = strdupl(parse(opt, Notype));
  207. else if (is("hlf")) t.hlf = strdupl(parse(opt, Notype));
  208. else if (is("flr")) t.flr = strdupl(parse(opt, Notype));
  209. else if (is("bdon")) t.bdon = strdupl(parse(opt, Notype));
  210. else if (is("bdoff")) t.bdoff = strdupl(parse(opt, Notype));
  211. else if (is("iton")) t.iton = strdupl(parse(opt, Notype));
  212. else if (is("itoff")) t.itoff = strdupl(parse(opt, Notype));
  213. else if (is("ploton")) t.ploton = strdupl(parse(opt, Notype));
  214. else if (is("plotoff")) t.plotoff = strdupl(parse(opt, Notype));
  215. else if (is("up")) t.up = strdupl(parse(opt, Notype));
  216. else if (is("down")) t.down = strdupl(parse(opt, Notype));
  217. else if (is("right")) t.right = strdupl(parse(opt, Notype));
  218. else if (is("left")) t.left = strdupl(parse(opt, Notype));
  219. else
  220. ERROR "bad tab.%s file, %s %s", devname, cmd, opt WARN;
  221. }
  222. getnrfont(fp);
  223. fclose(fp);
  224. sps = EM;
  225. ics = EM * 2;
  226. dtab = 8 * t.Em;
  227. for (i = 0; i < 16; i++)
  228. tabtab[i] = dtab * (i + 1);
  229. pl = 11 * INCH;
  230. po = PO;
  231. spacesz = SS;
  232. lss = lss1 = VS;
  233. ll = ll1 = lt = lt1 = LL;
  234. smnt = nfonts = 5; /* R I B BI S */
  235. n_specnames(); /* install names like "hyphen", etc. */
  236. if (eqflg)
  237. t.Adj = t.Hor;
  238. }
  239. void n_specnames(void)
  240. {
  241. int i;
  242. for (i = 0; spnames[i].n; i++)
  243. *spnames[i].n = chadd(spnames[i].v, Troffchar, Install);
  244. if (c_isalnum == 0)
  245. c_isalnum = NROFFCHARS;
  246. }
  247. void twdone(void)
  248. {
  249. if (!TROFF && t.twrest) {
  250. obufp = obuf;
  251. oputs(t.twrest);
  252. flusho();
  253. if (pipeflg) {
  254. pclose(ptid);
  255. }
  256. restore_tty();
  257. }
  258. }
  259. void n_ptout(Tchar i)
  260. {
  261. *olinep++ = i;
  262. if (olinep >= &oline[LNSIZE])
  263. olinep--;
  264. if (cbits(i) != '\n')
  265. return;
  266. olinep--;
  267. lead += dip->blss + lss - t.Newline;
  268. dip->blss = 0;
  269. esct = esc = 0;
  270. if (olinep > oline) {
  271. move();
  272. ptout1();
  273. oputs(t.twnl);
  274. } else {
  275. lead += t.Newline;
  276. move();
  277. }
  278. lead += dip->alss;
  279. dip->alss = 0;
  280. olinep = oline;
  281. }
  282. void ptout1(void)
  283. {
  284. int k;
  285. char *codep;
  286. int w, j, phyw;
  287. Tchar *q, i;
  288. static int oxfont = FT; /* start off in roman */
  289. for (q = oline; q < olinep; q++) {
  290. i = *q;
  291. if (ismot(i)) {
  292. j = absmot(i);
  293. if (isnmot(i))
  294. j = -j;
  295. if (isvmot(i))
  296. lead += j;
  297. else
  298. esc += j;
  299. continue;
  300. }
  301. if ((k = cbits(i)) <= ' ') {
  302. switch (k) {
  303. case ' ': /*space*/
  304. esc += t.Char;
  305. break;
  306. case '\033':
  307. case '\007':
  308. case '\016':
  309. case '\017':
  310. oput(k);
  311. break;
  312. }
  313. continue;
  314. }
  315. phyw = w = t.Char * t.tfont.wp[k].wid;
  316. if (iszbit(i))
  317. w = 0;
  318. if (esc || lead)
  319. move();
  320. esct += w;
  321. xfont = fbits(i);
  322. if (xfont != oxfont) {
  323. switch (oxfont) {
  324. case ULFONT: oputs(t.itoff); break;
  325. case BDFONT: oputs(t.bdoff); break;
  326. case BIFONT: oputs(t.itoff); oputs(t.bdoff); break;
  327. }
  328. switch (xfont) {
  329. case ULFONT:
  330. if (*t.iton & 0377) oputs(t.iton); break;
  331. case BDFONT:
  332. if (*t.bdon & 0377) oputs(t.bdon); break;
  333. case BIFONT:
  334. if (*t.bdon & 0377) oputs(t.bdon);
  335. if (*t.iton & 0377) oputs(t.iton);
  336. break;
  337. }
  338. oxfont = xfont;
  339. }
  340. if ((xfont == ulfont || xfont == BIFONT) && !(*t.iton & 0377)) {
  341. for (j = w / t.Char; j > 0; j--)
  342. oput('_');
  343. for (j = w / t.Char; j > 0; j--)
  344. oput('\b');
  345. }
  346. if (!(*t.bdon & 0377) && ((j = bdtab[xfont]) || xfont == BDFONT || xfont == BIFONT))
  347. j++;
  348. else
  349. j = 1; /* number of overstrikes for bold */
  350. if (k < ALPHABET) { /* ordinary ascii */
  351. oput(k);
  352. while (--j > 0) {
  353. oput('\b');
  354. oput(k);
  355. }
  356. } else if (k >= t.tfont.nchars) { /* BUG -- not really understood */
  357. /* fprintf(stderr, "big char %d, name %s\n", k, chname(k)); /* */
  358. oputs(chname(k)+1); /* BUG: should separate Troffchar and MBchar... */
  359. } else if (t.tfont.wp[k].str == 0) {
  360. /* fprintf(stderr, "nostr char %d, name %s\n", k, chname(k)); /* */
  361. oputs(chname(k)+1); /* BUG: should separate Troffchar and MBchar... */
  362. } else if (t.tfont.wp[k].str[0] == MBchar) { /* parse() puts this on */
  363. /* fprintf(stderr, "MBstr char %d, name %s\n", k, chname(k)); /* */
  364. oputs(t.tfont.wp[k].str+1);
  365. } else {
  366. int oj = j;
  367. /* fprintf(stderr, "str char %d, name %s\n", k, chname(k)); /* */
  368. codep = t.tfont.wp[k].str+1; /* Troffchar by default */
  369. while (*codep != 0) {
  370. if (*codep & 0200) {
  371. codep = plot(codep);
  372. oput(' ');
  373. } else {
  374. if (*codep == '%') /* escape */
  375. codep++;
  376. oput(*codep);
  377. if (*codep == '\033')
  378. oput(*++codep);
  379. else if (*codep != '\b')
  380. for (j = oj; --j > 0; ) {
  381. oput('\b');
  382. oput(*codep);
  383. }
  384. codep++;
  385. }
  386. }
  387. }
  388. if (!w)
  389. for (j = phyw / t.Char; j > 0; j--)
  390. oput('\b');
  391. }
  392. }
  393. char *plot(char *x)
  394. {
  395. int i;
  396. char *j, *k;
  397. oputs(t.ploton);
  398. k = x;
  399. if ((*k & 0377) == 0200)
  400. k++;
  401. for (; *k; k++) {
  402. if (*k == '%') { /* quote char within plot mode */
  403. oput(*++k);
  404. } else if (*k & 0200) {
  405. if (*k & 0100) {
  406. if (*k & 040)
  407. j = t.up;
  408. else
  409. j = t.down;
  410. } else {
  411. if (*k & 040)
  412. j = t.left;
  413. else
  414. j = t.right;
  415. }
  416. if ((i = *k & 037) == 0) { /* 2nd 0200 turns it off */
  417. ++k;
  418. break;
  419. }
  420. while (i--)
  421. oputs(j);
  422. } else
  423. oput(*k);
  424. }
  425. oputs(t.plotoff);
  426. return(k);
  427. }
  428. void move(void)
  429. {
  430. int k;
  431. char *i, *j;
  432. char *p, *q;
  433. int iesct, dt;
  434. iesct = esct;
  435. if (esct += esc)
  436. i = "\0";
  437. else
  438. i = "\n\0";
  439. j = t.hlf;
  440. p = t.right;
  441. q = t.down;
  442. if (lead) {
  443. if (lead < 0) {
  444. lead = -lead;
  445. i = t.flr;
  446. /* if(!esct)i = t.flr; else i = "\0";*/
  447. j = t.hlr;
  448. q = t.up;
  449. }
  450. if (*i & 0377) {
  451. k = lead / t.Newline;
  452. lead = lead % t.Newline;
  453. while (k--)
  454. oputs(i);
  455. }
  456. if (*j & 0377) {
  457. k = lead / t.Halfline;
  458. lead = lead % t.Halfline;
  459. while (k--)
  460. oputs(j);
  461. } else { /* no half-line forward, not at line begining */
  462. k = lead / t.Newline;
  463. lead = lead % t.Newline;
  464. if (k > 0)
  465. esc = esct;
  466. i = "\n";
  467. while (k--)
  468. oputs(i);
  469. }
  470. }
  471. if (esc) {
  472. if (esc < 0) {
  473. esc = -esc;
  474. j = "\b";
  475. p = t.left;
  476. } else {
  477. j = " ";
  478. if (hflg)
  479. while ((dt = dtab - (iesct % dtab)) <= esc) {
  480. if (dt % t.Em)
  481. break;
  482. oput(TAB);
  483. esc -= dt;
  484. iesct += dt;
  485. }
  486. }
  487. k = esc / t.Em;
  488. esc = esc % t.Em;
  489. while (k--)
  490. oputs(j);
  491. }
  492. if ((*t.ploton & 0377) && (esc || lead)) {
  493. oputs(t.ploton);
  494. esc /= t.Hor;
  495. lead /= t.Vert;
  496. while (esc--)
  497. oputs(p);
  498. while (lead--)
  499. oputs(q);
  500. oputs(t.plotoff);
  501. }
  502. esc = lead = 0;
  503. }
  504. void n_ptlead(void)
  505. {
  506. move();
  507. }
  508. void n_ptpause(void )
  509. {
  510. char junk;
  511. flusho();
  512. read(2, &junk, 1);
  513. }