include troff data directory
[troff.git] / troff / tdef.h
blob3fedcc5b1d701331d638365baf306fb892d00183
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <ctype.h>
5 #include <string.h>
7 #define NROFF (!TROFF)
10 /* Site dependent definitions */
12 #ifndef TMACDIR
13 #define TMACDIR "lib/tmac/tmac."
14 #endif
15 #ifndef FONTDIR
16 #define FONTDIR "lib/font"
17 #endif
18 #ifndef NTERMDIR
19 #define NTERMDIR "lib/term/tab."
20 #endif
21 #ifndef TEXHYPHENS
22 #define TEXHYPHENS "/usr/lib/tex/macros/hyphen.tex"
23 #endif
25 #define TDEVNAME "utf"
26 #define NDEVNAME "utf"
27 #define ALTHYPHENS TEXHYPHENS /* another place to look */
29 typedef unsigned char Uchar;
30 typedef unsigned short Ushort;
32 typedef /*unsigned*/ long Tchar;
34 typedef struct Blockp Blockp;
35 typedef struct Diver Diver;
36 typedef struct Stack Stack;
37 typedef struct Divsiz Divsiz;
38 typedef struct Contab Contab;
39 typedef struct Numtab Numtab;
40 typedef struct Numerr Numerr;
41 typedef struct Env Env;
42 typedef struct Term Term;
43 typedef struct Chwid Chwid;
44 typedef struct Font Font;
45 typedef struct Spnames Spnames;
46 typedef struct Wcache Wcache;
47 typedef struct Tbuf Tbuf;
49 /* this simulates printf into a buffer that gets flushed sporadically */
50 /* the BSD goo is because SunOS sprintf doesn't return anything useful */
52 #ifdef BSD4_2
53 #define OUT (obufp += strlen(sprintf(obufp,
54 #define PUT ))) > obuf+BUFSIZ ? flusho() : 1
55 #else
56 #define OUT (obufp += sprintf(obufp,
57 #define PUT )) > obuf+BUFSIZ ? flusho() : 1
58 #endif
60 #define oputs(a) OUT "%s", a PUT
61 #define oput(c) ( *obufp++ = (c), obufp > obuf+BUFSIZ ? flusho() : 1 )
63 extern char errbuf[];
64 #define ERROR sprintf(errbuf,
65 #define WARN ), errprint()
66 #define FATAL ), errprint(), exit(1)
68 /* starting values for typesetting parameters: */
70 #define PS 10 /* default point size */
71 #define FT 1 /* default font position */
72 #define ULFONT 2 /* default underline font */
73 #define BDFONT 3 /* default emboldening font */
74 #define BIFONT 4 /* default bold italic font */
75 #define LL (unsigned) 65*INCH/10 /* line length; 39picas=6.5in */
76 #define VS ((12*INCH)/72) /* initial vert space */
79 #define EMPTS(pts) (((long)Inch*(pts) + 36) / 72)
80 #define EM (TROFF? EMPTS(pts): t.Em)
81 #define INCH (TROFF? Inch: 240)
82 #define HOR (TROFF? Hor: t.Adj)
83 #define VERT (TROFF? Vert: t.Vert)
84 #define PO (TROFF? Inch: 0)
85 #define SPS (TROFF? EMPTS(pts)/3: INCH/10)
86 #define SS (TROFF? 12: INCH/10)
87 #define ICS (TROFF? EMPTS(pts): 2*INCH/10)
88 #define DTAB (TROFF? (INCH/2): 0)
91 * These "characters" are used to encode various internal functions
92 * Some make use of the fact that most ascii characters between
93 * 0 and 040 don't have any graphic or other function.
94 * The few that do have a purpose (e.g., \n, \b, \t, ...
95 * are avoided by the ad hoc choices here.
96 * See ifilt[] in n1.c for others -- 1, 2, 3, 5, 6, 7, 010, 011, 012
99 #define LEADER 001
100 #define IMP 004 /* impossible char; glues things together */
101 #define TAB 011
102 #define RPT 014 /* next character is to be repeated many times */
103 #define CHARHT 015 /* size field sets character height */
104 #define SLANT 016 /* size field sets amount of slant */
105 #define DRAWFCN 017 /* next several chars describe arb drawing fcns */
106 # define DRAWLINE 'l' /* line: 'l' dx dy char */
107 # define DRAWCIRCLE 'c' /* circle: 'c' r */
108 # define DRAWELLIPSE 'e' /* ellipse: 'e' rx ry */
109 # define DRAWARC 'a' /* arc: 'a' dx dy dx dy */
110 # define DRAWSPLINE '~' /* quadratic B spline: '~' dx dy dx dy ... */
111 /* other splines go thru too */
112 /* NOTE: the use of ~ is a botch since it's often used in .tr commands */
113 /* better to use a letter like s, but change it in the postprocessors too */
114 /* for now, this is taken care of in n9.c and t10.c */
115 # define DRAWBUILD 'b' /* built-up character (e.g., { */
117 #define LEFT 020 /* \{ */
118 #define RIGHT 021 /* \} */
119 #define FILLER 022 /* \& and similar purposes */
120 #define XON 023 /* \X'...' starts here */
121 #define OHC 024 /* optional hyphenation character \% */
122 #define CONT 025 /* \c character */
123 #define PRESC 026 /* printable escape */
124 #define UNPAD 027 /* unpaddable blank */
125 #define XPAR 030 /* transparent mode indicator */
126 #define FLSS 031 /* next Tchar contains vertical space */
127 /* used when recalling diverted text */
128 #define WORDSP 032 /* paddable word space */
129 #define ESC 033 /* current escape character */
130 #define XOFF 034 /* \X'...' ends here */
131 /* matches XON, but they will probably never nest */
132 /* so could drop this when another control is needed */
133 #define HX 035 /* next character is value of \x'...' */
134 #define MOTCH 036 /* this "character" is really motion; used by cbits() */
136 #define HYPHEN c_hyphen
137 #define EMDASH c_emdash /* \(em */
138 #define RULE c_rule /* \(ru */
139 #define MINUS c_minus /* minus sign on current font */
140 #define LIG_FI c_fi /* \(ff */
141 #define LIG_FL c_fl /* \(fl */
142 #define LIG_FF c_ff /* \(ff */
143 #define LIG_FFI c_ffi /* \(Fi */
144 #define LIG_FFL c_ffl /* \(Fl */
145 #define ACUTE c_acute /* acute accent \(aa */
146 #define GRAVE c_grave /* grave accent \(ga */
147 #define UNDERLINE c_under /* \(ul */
148 #define ROOTEN c_rooten /* root en \(rn */
149 #define BOXRULE c_boxrule /* box rule \(br */
150 #define LEFTHAND c_lefthand /* left hand for word overflow */
151 #define DAGGER c_dagger /* dagger for end of sentence/footnote */
153 #define HYPHALG 1 /* hyphenation algorithm: 0=>good old troff, 1=>tex */
156 /* array sizes, and similar limits: */
158 #define MAXFONTS 99 /* Maximum number of fonts in fontab */
159 #define NM 90 /* requests + macros */
160 #define NN NNAMES /* number registers */
161 #define NNAMES 15 /* predefined reg names */
162 #define NIF 15 /* if-else nesting */
163 #define NS 128 /* name buffer */
164 #define NTM 1024 /* tm buffer */
165 #define NEV 3 /* environments */
166 #define EVLSZ 10 /* size of ev stack */
168 #define STACKSIZE (12*1024) /* stack for macros and strings in progress */
169 #define NHYP 10 /* max hyphens per word */
170 #define NHEX 512 /* byte size of exception word list */
171 #define NTAB 100 /* tab stops */
172 #define NSO 5 /* "so" depth */
173 #define NMF 5 /* number of -m flags */
174 #define WDSIZE 500 /* word buffer click size */
175 #define LNSIZE 4000 /* line buffer click size */
176 #define OLNSIZE 5000 /* output line buffer click; bigger for 'w', etc. */
177 #define NDI 5 /* number of diversions */
179 #define ALPHABET alphabet /* number of characters in basic alphabet. */
180 /* 128 for parochial USA 7-bit ascii, */
181 /* 256 for "European" mode with e.g., Latin-1 */
184 * NCHARS must be greater than
185 * ALPHABET (ascii stuff) + total number of distinct char names
186 * from all fonts that will be run in this job (including
187 * unnamed ones and \N's)
189 #define NCHARS (8*1024) /* maximum size of troff character set*/
193 * However for nroff you want only :
194 * 1. number of special codes in charset of DESC, which ends up being the
195 * value of nchtab and which must be less than 512.
196 * 2. ALPHABET, which apparently is the size of the portion of the tables reserved
197 * for special control symbols
198 * Apparently the max N of \N is irrelevant;
199 * to allow \N of up to 254 with up to 338 special characters
200 * you need NCHARS of 338 + ALPHABET = 466
203 #define NROFFCHARS 1024 /* maximum size of nroff character set */
205 #define NTRTAB NCHARS /* number of items in trtab[] */
206 #define NWIDCACHE NCHARS /* number of items in widcache[] */
208 #define NTRAP 20 /* number of traps */
209 #define NPN 20 /* numbers in "-o" */
210 #define FBUFSZ 512 /* field buf size words */
211 #define IBUFSZ 4096 /* bytes */
212 #define NC 1024 /* cbuf size words */
213 #define NOV 10 /* number of overstrike chars */
214 #define NPP 10 /* pads per field */
217 Internal character representation:
218 Internally, every character is carried around as
219 a 32 bit cookie, called a "Tchar" (typedef long).
220 Bits are numbered 31..0 from left to right.
221 If bit 15 is 1, the character is motion, with
222 if bit 16 it's vertical motion
223 if bit 17 it's negative motion
224 If bit 15 is 0, the character is a real character.
225 if bit 31 zero motion
226 bits 30..24 size
227 bits 23..16 font
230 /* in the following, "L" should really be a Tchar, but ... */
231 /* numerology leaves room for 16 bit chars */
233 #define MOT (01uL << 16) /* motion character indicator */
234 #define VMOT (01uL << 30) /* vertical motion bit */
235 #define NMOT (01uL << 29) /* negative motion indicator */
236 /* #define MOTV (MOT|VMOT|NMOT) motion flags */
237 /* #define MAXMOT (~MOTV) maximum motion permitted */
238 #define MAXMOT 0xFFFF
240 #define ismot(n) ((n) & MOT)
241 #define isvmot(n) (((n) & (MOT|VMOT)) == (MOT|VMOT)) /* must have tested MOT previously */
242 #define isnmot(n) (((n) & (MOT|NMOT)) == (MOT|NMOT)) /* ditto */
243 #define absmot(n) ((n) & 0xFFFF)
245 #define ZBIT (01uL << 31) /* zero width char */
246 #define iszbit(n) ((n) & ZBIT)
248 #define FSHIFT 17
249 #define SSHIFT (FSHIFT+7)
250 #define SMASK (0177uL << SSHIFT) /* 128 distinct sizes */
251 #define FMASK (0177uL << FSHIFT) /* 128 distinct fonts */
252 #define SFMASK (SMASK|FMASK) /* size and font in a Tchar */
253 #define sbits(n) (((n) >> SSHIFT) & 0177)
254 #define fbits(n) (((n) >> FSHIFT) & 0177)
255 #define sfbits(n) (((n) & SFMASK) >> FSHIFT)
256 #define cbits(n) ((n) & 0x1FFFF) /* isolate character bits, */
257 /* but don't include motions */
258 extern int realcbits(Tchar);
260 #define setsbits(n,s) n = (n & ~SMASK) | (Tchar)(s) << SSHIFT
261 #define setfbits(n,f) n = (n & ~FMASK) | (Tchar)(f) << FSHIFT
262 #define setsfbits(n,sf) n = (n & ~SFMASK) | (Tchar)(sf) << FSHIFT
263 #define setcbits(n,c) n = (n & ~0xFFFFuL | (c)) /* set character bits */
265 #define BYTEMASK 0377
266 #define BYTE 8
268 #define SHORTMASK 0XFFFF
269 #define SHORT 16
271 #define TABMASK ((unsigned) INT_MAX >> 1)
272 #define RTAB ((TABMASK << 1) & ~TABMASK)
273 #define CTAB (RTAB << 1)
275 #define TABBIT 02 /* bits in gchtab */
276 #define LDRBIT 04
277 #define FCBIT 010
279 #define PAIR(A,B) (A|(B<<SHORT))
282 extern int Inch, Hor, Vert, Unitwidth;
284 struct Spnames {
285 int *n;
286 char *v;
289 extern Spnames spnames[];
292 String and macro definitions are stored conceptually in a giant array
293 indexed by type Offset. In olden times, this array was real, and thus
294 both huge and limited in size, leading to the "Out of temp file space"
295 error. In this version, the array is represented by a list of blocks,
296 pointed to by blist[].bp. Each block is of size BLK Tchars, and BLK
297 MUST be a power of 2 for the macros below to work.
299 The blocks associated with a particular string or macro are chained
300 together in the array blist[]. Each blist[i].nextoff contains the
301 Offset associated with the next block in the giant array, or -1 if
302 this is the last block in the chain. If .nextoff is 0, the block is
303 free.
305 To find the right index in blist for an Offset, divide by BLK.
308 #define NBLIST 2048 /* starting number of blocks in all definitions */
310 #define BLK 128 /* number of Tchars in a block; must be 2^N with defns below */
312 #define rbf0(o) (blist[bindex(o)].bp[boffset(o)])
313 #define bindex(o) ((o) / BLK)
314 #define boffset(o) ((o) & (BLK-1))
315 #define pastend(o) (((o) & (BLK-1)) == 0)
316 /* #define incoff(o) ( (++o & (BLK-1)) ? o : blist[bindex(o-1)].nextoff ) */
317 #define incoff(o) ( (((o)+1) & (BLK-1)) ? o+1 : blist[bindex(o)].nextoff )
319 #define skipline(f) while (getc(f) != '\n')
320 #define is(s) (strcmp(cmd, s) == 0)
321 #define eq(s1, s2) (strcmp(s1, s2) == 0)
324 typedef unsigned long Offset; /* an offset in macro/string storage */
326 struct Blockp { /* info about a block: */
327 Tchar *bp; /* the data */
328 Offset nextoff; /* offset of next block in a chain */
331 extern Blockp *blist;
333 #define RD_OFFSET (1 * BLK) /* .rd command uses block 1 */
335 struct Diver { /* diversion */
336 Offset op;
337 int dnl;
338 int dimac;
339 int ditrap;
340 int ditf;
341 int alss;
342 int blss;
343 int nls;
344 int mkline;
345 int maxl;
346 int hnl;
347 int curd;
350 struct Stack { /* stack frame */
351 int nargs;
352 Stack *pframe;
353 Offset pip;
354 int pnchar;
355 Tchar prchar;
356 int ppendt;
357 Tchar pch;
358 Tchar *lastpbp;
359 int mname;
362 extern Stack s;
364 struct Divsiz {
365 int dix;
366 int diy;
369 struct Contab { /* command or macro */
370 unsigned int rq;
371 Contab *link;
372 void (*f)(void);
373 Offset mx;
374 Offset emx;
375 Divsiz *divsiz;
378 #define C(a,b) {a, 0, b, 0, 0} /* how to initialize a contab entry */
380 extern Contab contab[NM];
382 struct Numtab { /* number registers */
383 unsigned int r; /* name */
384 int val;
385 short fmt;
386 short inc;
387 Numtab *link;
390 extern Numtab numtab[NN];
392 #define PN 0
393 #define NL 1
394 #define YR 2
395 #define HP 3
396 #define CT 4
397 #define DN 5
398 #define MO 6
399 #define DY 7
400 #define DW 8
401 #define LN 9
402 #define DL 10
403 #define ST 11
404 #define SB 12
405 #define CD 13
406 #define PID 14
408 struct Wcache { /* width cache, indexed by character */
409 short fontpts;
410 short width;
413 struct Tbuf { /* growable Tchar buffer */
414 Tchar *_bufp;
415 unsigned int _size;
418 /* the infamous environment block */
420 #define ics envp->_ics
421 #define sps envp->_sps
422 #define spacesz envp->_spacesz
423 #define lss envp->_lss
424 #define lss1 envp->_lss1
425 #define ll envp->_ll
426 #define ll1 envp->_ll1
427 #define lt envp->_lt
428 #define lt1 envp->_lt1
429 #define ic envp->_ic
430 #define icf envp->_icf
431 #define chbits envp->_chbits
432 #define spbits envp->_spbits
433 #define nmbits envp->_nmbits
434 #define apts envp->_apts
435 #define apts1 envp->_apts1
436 #define pts envp->_pts
437 #define pts1 envp->_pts1
438 #define font envp->_font
439 #define font1 envp->_font1
440 #define ls envp->_ls
441 #define ls1 envp->_ls1
442 #define ad envp->_ad
443 #define nms envp->_nms
444 #define ndf envp->_ndf
445 #define nmwid envp->_nmwid
446 #define fi envp->_fi
447 #define cc envp->_cc
448 #define c2 envp->_c2
449 #define ohc envp->_ohc
450 #define tdelim envp->_tdelim
451 #define hyf envp->_hyf
452 #define hyoff envp->_hyoff
453 #define hyphalg envp->_hyphalg
454 #define un1 envp->_un1
455 #define tabc envp->_tabc
456 #define dotc envp->_dotc
457 #define adsp envp->_adsp
458 #define adrem envp->_adrem
459 #define lastl envp->_lastl
460 #define nel envp->_nel
461 #define admod envp->_admod
462 #define wordp envp->_wordp
463 #define spflg envp->_spflg
464 #define linep envp->_linep
465 #define wdend envp->_wdend
466 #define wdstart envp->_wdstart
467 #define wne envp->_wne
468 #define ne envp->_ne
469 #define nc envp->_nc
470 #define nb envp->_nb
471 #define lnmod envp->_lnmod
472 #define nwd envp->_nwd
473 #define nn envp->_nn
474 #define ni envp->_ni
475 #define ul envp->_ul
476 #define cu envp->_cu
477 #define ce envp->_ce
478 #define in envp->_in
479 #define in1 envp->_in1
480 #define un envp->_un
481 #define wch envp->_wch
482 #define pendt envp->_pendt
483 #define pendw envp->_pendw
484 #define pendnf envp->_pendnf
485 #define spread envp->_spread
486 #define it envp->_it
487 #define itmac envp->_itmac
488 #define hyptr envp->_hyptr
489 #define tabtab envp->_tabtab
490 #define line envp->_line._bufp
491 #define lnsize envp->_line._size
492 #define word envp->_word._bufp
493 #define wdsize envp->_word._size
495 #define oline _oline._bufp
496 #define olnsize _oline._size
499 * Note:
500 * If this structure changes in ni.c, you must change
501 * this as well, and vice versa.
504 struct Env {
505 int _ics;
506 int _sps;
507 int _spacesz;
508 int _lss;
509 int _lss1;
510 int _ll;
511 int _ll1;
512 int _lt;
513 int _lt1;
514 Tchar _ic;
515 int _icf;
516 Tchar _chbits;
517 Tchar _spbits;
518 Tchar _nmbits;
519 int _apts;
520 int _apts1;
521 int _pts;
522 int _pts1;
523 int _font;
524 int _font1;
525 int _ls;
526 int _ls1;
527 int _ad;
528 int _nms;
529 int _ndf;
530 int _nmwid;
531 int _fi;
532 int _cc;
533 int _c2;
534 int _ohc;
535 int _tdelim;
536 int _hyf;
537 int _hyoff;
538 int _hyphalg;
539 int _un1;
540 int _tabc;
541 int _dotc;
542 int _adsp;
543 int _adrem;
544 int _lastl;
545 int _nel;
546 int _admod;
547 Tchar *_wordp;
548 int _spflg;
549 Tchar *_linep;
550 Tchar *_wdend;
551 Tchar *_wdstart;
552 int _wne;
553 int _ne;
554 int _nc;
555 int _nb;
556 int _lnmod;
557 int _nwd;
558 int _nn;
559 int _ni;
560 int _ul;
561 int _cu;
562 int _ce;
563 int _in;
564 int _in1;
565 int _un;
566 int _wch;
567 int _pendt;
568 Tchar *_pendw;
569 int _pendnf;
570 int _spread;
571 int _it;
572 int _itmac;
573 Tchar *_hyptr[NHYP];
574 long _tabtab[NTAB];
575 Tbuf _line;
576 Tbuf _word;
579 extern Env env[];
580 extern Env *envp;
582 enum { MBchar = 'U', Troffchar = 'C', Number = 'N', Install = 'i', Lookup = 'l' };
583 /* U => utf, for instance; C => \(xx, N => \N'...' */
587 struct Chwid { /* data on one character */
588 Ushort num; /* character number:
589 0 -> not on this font
590 >= ALPHABET -> its number among all Cxy's */
591 Ushort code; /* char code for actual device. used for \N */
592 char *str; /* code string for nroff */
593 Uchar wid; /* width */
594 Uchar kern; /* ascender/descender */
597 struct Font { /* characteristics of a font */
598 int name; /* int name, e.g., BI (2 chars) */
599 char longname[64]; /* long name of this font (e.g., "Bembo" */
600 char *truename; /* path name of table if not in standard place */
601 int nchars; /* number of width entries for this font */
602 char specfont; /* 1 == special font */
603 int spacewidth; /* width of space on this font */
604 int defaultwidth; /* default width of characters on this font */
605 Chwid *wp; /* widths, etc., of the real characters */
606 char ligfont; /* 1 == ligatures exist on this font */
609 /* ligatures, ORed into ligfont */
611 #define LFF 01
612 #define LFI 02
613 #define LFL 04
614 #define LFFI 010
615 #define LFFL 020
617 /* tracing modes */
618 #define TRNARGS 01 /* trace legality of numeric arguments */
619 #define TRREQ 02 /* trace requests */
620 #define TRMAC 04 /* trace macros */
621 #define RQERR 01 /* processing request/macro */
623 /* typewriter driving table structure */
625 extern Term t;
626 struct Term {
627 int bset; /* these bits have to be on */
628 int breset; /* these bits have to be off */
629 int Hor; /* #units in minimum horiz motion */
630 int Vert; /* #units in minimum vert motion */
631 int Newline; /* #units in single line space */
632 int Char; /* #units in character width */
633 int Em; /* ditto */
634 int Halfline; /* half line units */
635 int Adj; /* minimum units for horizontal adjustment */
636 char *twinit; /* initialize terminal */
637 char *twrest; /* reinitialize terminal */
638 char *twnl; /* terminal sequence for newline */
639 char *hlr; /* half-line reverse */
640 char *hlf; /* half-line forward */
641 char *flr; /* full-line reverse */
642 char *bdon; /* turn bold mode on */
643 char *bdoff; /* turn bold mode off */
644 char *iton; /* turn italic mode on */
645 char *itoff; /* turn italic mode off */
646 char *ploton; /* turn plot mode on */
647 char *plotoff; /* turn plot mode off */
648 char *up; /* sequence to move up in plot mode */
649 char *down; /* ditto */
650 char *right; /* ditto */
651 char *left; /* ditto */
653 Font tfont; /* widths and other info, as in a troff font */
656 extern Term t;
659 * for error reporting; keep track of escapes/requests with numeric arguments
661 struct Numerr {
662 char type; /* request or escape? */
663 char esc; /* was escape sequence named esc */
664 char escarg; /* argument of esc's like \D'l' */
665 unsigned int req; /* was request or macro named req */