wb: interpret unknown escapes as normal characters
[neatroff.git] / roff.h
blob2b300c4920ffc3a56ede8e6455ee0c48ae741e55
1 /*
2 * Most functions and variables in neatroff are prefixed with tokens
3 * that indicate their purpose, such as:
5 * + tr_xyz: the implementation of troff request .xyz (mostly tr.c)
6 * + in_xyz: input layer (in.c)
7 * + cp_xyz: copy-mode interpretation layer (cp.c)
8 * + ren_xyz: rendering characters into lines (ren.c)
9 * + out_xyz: output layer for generating troff output (out.c)
10 * + dev_xyz: output devices (dev.c)
11 * + num_xyz: number registers (reg.c)
12 * + str_xyz: string registers (reg.c)
13 * + env_xyz: environments (reg.c)
14 * + eval_xyz: integer expression evaluation (eval.c)
15 * + font_xyz: fonts (font.c)
16 * + sbuf_xyz: variable length string buffers (sbuf.c)
17 * + wb_xyz: word buffers (wb.c)
18 * + adj_xyz: line adjustment buffers (adj.c)
19 * + n_xyz: builtin number register xyz
20 * + c_xyz: characters for requests like hc and mc
24 /* predefined array limits */
25 #define PATHLEN 1024 /* path length */
26 #define NFILES 16 /* number of input files */
27 #define NFONTS 32 /* number of fonts */
28 #define NGLYPHS 1024 /* glyphs in fonts */
29 #define NLIGS 128 /* number of font ligatures */
30 #define NKERNS 1024 /* number of font kerning pairs */
31 #define FNLEN 32 /* font name length */
32 #define NMLEN 32 /* macro/register/environment/glyph name length */
33 #define GNLEN NMLEN /* glyph name length */
34 #define RNLEN NMLEN /* register/macro name */
35 #define ILNLEN 1000 /* line limit of input files */
36 #define LNLEN 4000 /* line buffer length (ren.c/out.c) */
37 #define NWORDS 256 /* number of words in line buffer */
38 #define NARGS 16 /* number of macro arguments */
39 #define NPREV 16 /* environment stack depth */
40 #define NTRAPS 1024 /* number of traps per page */
41 #define NIES 128 /* number of nested .ie commands */
42 #define NTABS 16 /* number of tab stops */
43 #define NCMAPS 512 /* number of character translations (.tr) */
44 #define NSSTR 32 /* number of nested sstr_push() calls */
45 #define NFIELDS 32 /* number of fields */
46 #define MAXFRAC 100000 /* maximum value of the fractional part */
47 #define LIGLEN 4 /* length of ligatures */
48 #define NCDEFS 128 /* number of character definitions (.char) */
50 /* converting scales */
51 #define SC_IN (dev_res) /* inch in units */
52 #define SC_PT (SC_IN / 72) /* point in units */
53 #define SC_EM (n_s * SC_IN / 72)
54 #define SC_DW (SC_EM / 3) /* default width */
55 #define SC_HT (n_s * SC_PT) /* character height */
57 /* escape sequences */
58 #define ESC_Q "bCDhHlLNoSvwxX" /* \X'ccc' quoted escape sequences */
59 #define ESC_P "*fgkmns" /* \Xc \X(cc \X[ccc] escape sequences */
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define LEN(a) (sizeof(a) / sizeof((a)[0]))
65 /* special characters */
66 extern int c_ec; /* escape character (\) */
67 extern int c_cc; /* basic control character (.) */
68 extern int c_c2; /* no-break control character (') */
69 #define c_ni 4 /* non-interpreted copy-mode escape */
70 #define c_hc env_hc()/* hyphenation character */
71 #define c_mc env_mc()/* margin character (.mc) */
72 #define c_tc env_tc()
73 #define c_lc env_lc()
74 #define c_bp "\\:" /* zero-width word break point */
76 /* number registers */
77 int num_get(int id, int inc);
78 void num_set(int id, int val);
79 void num_inc(int id, int val);
80 void num_del(int id);
81 char *num_str(int id);
82 char *num_getfmt(int id);
83 void num_setfmt(int id, char *fmt);
84 int *nreg(int id);
85 int eval(char *s, int unit);
86 int eval_up(char **s, int unit);
87 int eval_re(char *s, int orig, int unit);
89 /* string registers */
90 void str_set(int id, char *s);
91 void str_dset(int id, void *d);
92 char *str_get(int id);
93 void *str_dget(int id);
94 void str_rm(int id);
95 void str_rn(int src, int dst);
97 /* saving and restoring registers before and after printing diverted lines */
98 void odiv_beg(void);
99 void odiv_end(void);
101 /* enviroments */
102 void env_init(void);
103 void env_done(void);
104 struct adj *env_adj(void);
105 char *env_hc(void);
106 char *env_mc(void);
107 char *env_tc(void);
108 char *env_lc(void);
109 int tab_next(int pos);
110 int tab_type(int pos);
112 /* device related variables */
113 extern int dev_res;
114 extern int dev_uwid;
115 extern int dev_hor;
116 extern int dev_ver;
118 struct glyph {
119 char id[GNLEN]; /* device-dependent glyph identifier */
120 char name[GNLEN]; /* the first character mapped to this glyph */
121 struct font *font; /* glyph font */
122 int wid; /* character width */
123 int type; /* character type; ascender/descender */
126 struct font {
127 char name[FNLEN];
128 char fontname[FNLEN];
129 struct glyph glyphs[NGLYPHS];
130 int nglyphs;
131 int spacewid;
132 int special;
133 int cs, bd; /* for .cs and .bd requests */
134 /* charset section characters */
135 char c[NGLYPHS][GNLEN]; /* character names in charset */
136 struct glyph *g[NGLYPHS]; /* character glyphs in charset */
137 struct glyph *g_map[NGLYPHS]; /* character remapped via font_map() */
138 int n; /* number of characters in charset */
139 /* glyph table based on the first character of their id fields in glyphs[] */
140 int ghead[256]; /* glyph list heads */
141 int gnext[NGLYPHS]; /* next item in glyph lists */
142 /* character table based on the first character of glyph names in c[] */
143 int chead[256]; /* character list heads */
144 int cnext[NGLYPHS]; /* next item in character lists */
145 /* font ligatures (lg*) */
146 char lg[NLIGS][LIGLEN * GNLEN]; /* ligatures */
147 int lgn; /* number of ligatures in lg[] */
148 /* kerning pair table per glyph (kn*) */
149 int knhead[NGLYPHS]; /* kerning pairs of glyphs[] */
150 int knnext[NKERNS]; /* next item in knhead[] list */
151 int knpair[NKERNS]; /* kerning pair 2nd glyphs */
152 int knval[NKERNS]; /* font pairwise kerning value */
153 int knn; /* number of kerning pairs */
156 /* output device functions */
157 int dev_open(char *dir, char *dev);
158 void dev_close(void);
159 int dev_mnt(int pos, char *id, char *name);
160 int dev_pos(char *id);
161 struct font *dev_font(int pos);
162 int dev_fontpos(struct font *fn);
163 void dev_setcs(int fn, int cs);
164 int dev_getcs(int fn);
165 void dev_setbd(int fn, int bd);
166 int dev_getbd(int fn);
168 /* font-related functions */
169 struct font *font_open(char *path);
170 void font_close(struct font *fn);
171 struct glyph *font_glyph(struct font *fn, char *id);
172 struct glyph *font_find(struct font *fn, char *name);
173 int font_lig(struct font *fn, char **c, int n);
174 int font_kern(struct font *fn, char *c1, char *c2);
175 int font_islig(struct font *fn, char *s);
176 int font_map(struct font *fn, char *name, struct glyph *gl);
177 int font_mapped(struct font *fn, char *name);
179 /* glyph handling functions */
180 struct glyph *dev_glyph(char *c, int fn);
181 struct glyph *dev_glyph_byid(char *id, int fn);
182 int charwid(int fn, int sz, int wid);
183 int spacewid(int fn, int sz);
184 int charwid_base(int fn, int sz, int wid);
186 /* different layers of neatroff */
187 int in_next(void); /* input layer */
188 int cp_next(void); /* copy-mode layer */
189 int tr_next(void); /* troff layer */
191 void in_push(char *s, char **args);
192 void in_pushnl(char *s, char **args);
193 void in_so(char *path); /* .so request */
194 void in_nx(char *path); /* .nx request */
195 void in_ex(void); /* .ex request */
196 void in_lf(char *path, int ln); /* .lf request */
197 void in_queue(char *path); /* queue the given input file */
198 char *in_arg(int i); /* look up argument */
199 int in_nargs(void); /* number of arguments */
200 void in_back(int c); /* push back input character */
201 int in_top(void); /* the first pushed-back character */
202 char *in_filename(void); /* current filename */
203 int in_lnum(void); /* current line number */
205 void cp_blk(int skip); /* skip or read the next line or block */
206 void cp_wid(int enable); /* control inlining \w requests */
207 #define cp_back in_back /* cp.c is stateless */
208 void tr_first(void); /* read until the first non-command line */
210 /* variable length string buffer */
211 struct sbuf {
212 char *s; /* allocated buffer */
213 int sz; /* buffer size */
214 int n; /* length of the string stored in s */
217 void sbuf_init(struct sbuf *sbuf);
218 void sbuf_done(struct sbuf *sbuf);
219 char *sbuf_buf(struct sbuf *sbuf);
220 void sbuf_add(struct sbuf *sbuf, int c);
221 void sbuf_append(struct sbuf *sbuf, char *s);
222 void sbuf_printf(struct sbuf *sbuf, char *s, ...);
223 void sbuf_putnl(struct sbuf *sbuf);
224 void sbuf_cut(struct sbuf *sbuf, int n);
225 int sbuf_len(struct sbuf *sbuf);
226 int sbuf_empty(struct sbuf *sbuf);
228 /* word buffer */
229 struct wb {
230 struct sbuf sbuf;
231 int f, s, m; /* the last output font and size */
232 int r_f, r_s, r_m; /* current font and size; use n_f and n_s if -1 */
233 int part; /* partial input (\c) */
234 int els_neg, els_pos; /* extra line spacing */
235 int h, v; /* buffer vertical and horizontal positions */
236 int ct, sb, st; /* \w registers */
237 /* saving previous characters added via wb_put() */
238 char prev_c[LIGLEN][GNLEN];
239 int prev_l[LIGLEN]; /* sbuf_len(&wb->sbuf) before wb_put() calls */
240 int prev_h[LIGLEN]; /* wb->h before wb_put() calls */
241 int prev_n; /* number of characters in prev_c[] */
242 int prev_ll; /* sbuf_len(&wb->sbuf) after the last wb_put() */
245 void wb_init(struct wb *wb);
246 void wb_done(struct wb *wb);
247 void wb_hmov(struct wb *wb, int n);
248 void wb_vmov(struct wb *wb, int n);
249 void wb_els(struct wb *wb, int els);
250 void wb_etc(struct wb *wb, char *x);
251 void wb_put(struct wb *wb, char *c);
252 void wb_putexpand(struct wb *wb, char *c);
253 int wb_part(struct wb *wb);
254 void wb_setpart(struct wb *wb);
255 void wb_drawl(struct wb *wb, int c, int h, int v);
256 void wb_drawc(struct wb *wb, int c, int r);
257 void wb_drawe(struct wb *wb, int c, int h, int v);
258 void wb_drawa(struct wb *wb, int c, int h1, int v1, int h2, int v2);
259 void wb_drawxbeg(struct wb *wb, int c);
260 void wb_drawxdot(struct wb *wb, int h, int v);
261 void wb_drawxend(struct wb *wb);
262 void wb_cat(struct wb *wb, struct wb *src);
263 int wb_hyph(struct wb *wb, int w, struct wb *w1, struct wb *w2, int flg);
264 int wb_wid(struct wb *wb);
265 int wb_empty(struct wb *wb);
266 int wb_eos(struct wb *wb);
267 void wb_wconf(struct wb *wb, int *ct, int *st, int *sb);
268 int wb_lig(struct wb *wb, char *c);
269 int wb_kern(struct wb *wb, char *c);
271 /* character translation (.tr) */
272 void cmap_add(char *c1, char *c2);
273 char *cmap_map(char *c);
274 /* character definition (.char) */
275 char *cdef_map(char *c, int fn);
276 int cdef_expand(struct wb *wb, char *c, int fn);
278 /* hyphenation flags */
279 #define HY_MASK 0x0f /* enable hyphenation */
280 #define HY_LAST 0x02 /* do not hyphenate last lines */
281 #define HY_FINAL2 0x04 /* do not hyphenate the final two characters */
282 #define HY_FIRST2 0x08 /* do not hyphenate the first two characters */
283 #define HY_ANY 0x10 /* break at any possible position */
285 void hyphenate(char *hyphs, char *word, int flg);
287 /* adjustment */
288 #define AD_L 0
289 #define AD_B 1
290 #define AD_C 3
291 #define AD_R 5
293 struct adj *adj_alloc(void);
294 void adj_free(struct adj *adj);
295 int adj_fill(struct adj *adj, int ad_b, int fill, int hyph, struct sbuf *dst,
296 int *ll, int *in, int *ti, int *els_neg, int *els_pos);
297 int adj_full(struct adj *adj, int fill);
298 int adj_empty(struct adj *adj, int fill);
299 int adj_wid(struct adj *adj);
300 void adj_swid(struct adj *adj, int swid);
301 void adj_ll(struct adj *adj, int ll);
302 void adj_in(struct adj *adj, int in);
303 void adj_ti(struct adj *adj, int ti);
304 void adj_wb(struct adj *adj, struct wb *wb);
305 void adj_nl(struct adj *adj);
306 void adj_sp(struct adj *adj);
307 void adj_nonl(struct adj *adj);
309 /* rendering */
310 int render(void); /* the main loop */
311 int ren_parse(struct wb *wb, char *c);
312 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int));
313 int ren_wid(int (*next)(void), void (*back)(int));
314 void ren_tl(int (*next)(void), void (*back)(int));
315 void ren_hline(struct wb *wb, int l, char *c); /* horizontal line */
316 void ren_hlcmd(struct wb *wb, char *arg); /* \l */
317 void ren_vlcmd(struct wb *wb, char *arg); /* \L */
318 void ren_bcmd(struct wb *wb, char *arg); /* \b */
319 void ren_ocmd(struct wb *wb, char *arg); /* \o */
320 void ren_dcmd(struct wb *wb, char *arg); /* \D */
322 /* out.c */
323 void out_line(char *s); /* output rendered line */
324 void out(char *s, ...); /* output troff cmd */
326 /* troff commands */
327 void tr_ab(char **args);
328 void tr_bp(char **args);
329 void tr_br(char **args);
330 void tr_ce(char **args);
331 void tr_ch(char **args);
332 void tr_cl(char **args);
333 void tr_di(char **args);
334 void tr_divbeg(char **args);
335 void tr_divend(char **args);
336 void tr_dt(char **args);
337 void tr_em(char **args);
338 void tr_ev(char **args);
339 void tr_fc(char **args);
340 void tr_fi(char **args);
341 void tr_fp(char **args);
342 void tr_fspecial(char **args);
343 void tr_ft(char **args);
344 void tr_hw(char **args);
345 void tr_in(char **args);
346 void tr_ll(char **args);
347 void tr_mk(char **args);
348 void tr_ne(char **args);
349 void tr_nf(char **args);
350 void tr_ns(char **args);
351 void tr_os(char **args);
352 void tr_pn(char **args);
353 void tr_ps(char **args);
354 void tr_rs(char **args);
355 void tr_rt(char **args);
356 void tr_sp(char **args);
357 void tr_sv(char **args);
358 void tr_ta(char **args);
359 void tr_ti(char **args);
360 void tr_wh(char **args);
361 void tr_eject(char **args);
363 void tr_init(void);
365 /* helpers */
366 void errmsg(char *msg, ...);
367 void errdie(char *msg);
368 int utf8len(int c);
369 int utf8next(char *s, int (*next)(void));
370 int utf8read(char **s, char *d);
371 int utf8one(char *s);
372 int charnext(char *c, int (*next)(void), void (*back)(int));
373 int charread(char **s, char *c);
374 int charnext_delim(char *c, int (*next)(void), void (*back)(int), char *delim);
375 void charnext_str(char *d, char *c);
376 void argnext(char *d, int cmd, int (*next)(void), void (*back)(int));
377 void argread(char **sp, char *d, int cmd);
378 int escread(char **s, char *d);
379 /* string streams; nested next()/back() interface for string buffers */
380 void sstr_push(char *s);
381 char *sstr_pop(void);
382 int sstr_next(void);
383 void sstr_back(int c);
385 /* internal commands */
386 #define TR_DIVBEG "\07<" /* diversion begins */
387 #define TR_DIVEND "\07>" /* diversion ends */
388 #define TR_EJECT "\07P" /* page eject */
390 /* mapping register, macro and environment names to indices */
391 #define NREGS 4096 /* maximum number of mapped names */
392 #define DOTMAP(c2) (c2) /* optimized mapping for ".x" names */
394 int map(char *s); /* map name s to an index */
395 char *map_name(int id); /* return the name mapped to id */
397 /* colors */
398 #define CLR_R(c) (((c) >> 16) & 0xff)
399 #define CLR_G(c) (((c) >> 8) & 0xff)
400 #define CLR_B(c) ((c) & 0xff)
401 #define CLR_RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))
403 char *clr_str(int c);
404 int clr_get(char *s);
406 /* builtin number registers; n_X for .X register */
407 #define n_a (*nreg(DOTMAP('a')))
408 #define n_cp (*nreg(DOTMAP('C')))
409 #define n_d (*nreg(DOTMAP('d')))
410 #define n_f (*nreg(DOTMAP('f')))
411 #define n_h (*nreg(DOTMAP('h')))
412 #define n_i (*nreg(DOTMAP('i')))
413 #define n_it (*nreg(map(".it"))) /* .it trap macro */
414 #define n_itn (*nreg(map(".itn"))) /* .it lines left */
415 #define n_j (*nreg(DOTMAP('j')))
416 #define n_l (*nreg(DOTMAP('l')))
417 #define n_L (*nreg(DOTMAP('L')))
418 #define n_n (*nreg(DOTMAP('n')))
419 #define n_nI (*nreg(map(".nI"))) /* i for .nm */
420 #define n_nm (*nreg(map(".nm"))) /* .nm enabled */
421 #define n_nM (*nreg(map(".nM"))) /* m for .nm */
422 #define n_nn (*nreg(map(".nn"))) /* remaining .nn */
423 #define n_nS (*nreg(map(".nS"))) /* s for .nm */
424 #define n_m (*nreg(DOTMAP('m')))
425 #define n_mc (*nreg(map(".mc"))) /* .mc enabled */
426 #define n_mcn (*nreg(map(".mcn"))) /* .mc distance */
427 #define n_o (*nreg(DOTMAP('o')))
428 #define n_p (*nreg(DOTMAP('p')))
429 #define n_s (*nreg(DOTMAP('s')))
430 #define n_u (*nreg(DOTMAP('u')))
431 #define n_v (*nreg(DOTMAP('v')))
432 #define n_ct (*nreg(map("ct")))
433 #define n_dl (*nreg(map("dl")))
434 #define n_dn (*nreg(map("dn")))
435 #define n_ln (*nreg(map("ln")))
436 #define n_nl (*nreg(map("nl")))
437 #define n_sb (*nreg(map("sb")))
438 #define n_st (*nreg(map("st")))
439 #define n_pg (*nreg(map("%"))) /* % */
440 #define n_lb (*nreg(map(".b0"))) /* input line beg */
441 #define n_ce (*nreg(map(".ce"))) /* .ce remaining */
442 #define n_f0 (*nreg(map(".f0"))) /* last .f */
443 #define n_lg (*nreg(map(".lg"))) /* .lg mode */
444 #define n_hy (*nreg(map(".hy"))) /* .hy mode */
445 #define n_i0 (*nreg(map(".i0"))) /* last .i */
446 #define n_kn (*nreg(map(".kern"))) /* .kn mode */
447 #define n_l0 (*nreg(map(".l0"))) /* last .l */
448 #define n_L0 (*nreg(map(".L0"))) /* last .L */
449 #define n_m0 (*nreg(map(".m0"))) /* last .m */
450 #define n_mk (*nreg(map(".mk"))) /* .mk internal register */
451 #define n_na (*nreg(map(".na"))) /* .na mode */
452 #define n_ns (*nreg(map(".ns"))) /* .ns mode */
453 #define n_o0 (*nreg(map(".o0"))) /* last .o */
454 #define n_ss (*nreg(map(".ss"))) /* .ss value */
455 #define n_s0 (*nreg(map(".s0"))) /* last .s */
456 #define n_sv (*nreg(map(".sv"))) /* .sv value */
457 #define n_lt (*nreg(map(".lt"))) /* .lt value */
458 #define n_t0 (*nreg(map(".lt0"))) /* previous .lt value */
459 #define n_v0 (*nreg(map(".v0"))) /* last .v */
461 /* functions for implementing read-only registers */
462 int f_nexttrap(void); /* .t */
463 int f_divreg(void); /* .z */
464 int f_hpos(void); /* .k */