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