n10.c 11 KB

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