]> git.cameronkatri.com Git - mandoc.git/blob - roff.c
Partial implementation of \h (horizontal line drawing function).
[mandoc.git] / roff.c
1 /* $Id: roff.c,v 1.302 2017/05/08 20:33:53 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "mandoc.h"
32 #include "mandoc_aux.h"
33 #include "mandoc_ohash.h"
34 #include "roff.h"
35 #include "libmandoc.h"
36 #include "roff_int.h"
37 #include "libroff.h"
38
39 /* Maximum number of string expansions per line, to break infinite loops. */
40 #define EXPAND_LIMIT 1000
41
42 /* --- data types --------------------------------------------------------- */
43
44 /*
45 * An incredibly-simple string buffer.
46 */
47 struct roffstr {
48 char *p; /* nil-terminated buffer */
49 size_t sz; /* saved strlen(p) */
50 };
51
52 /*
53 * A key-value roffstr pair as part of a singly-linked list.
54 */
55 struct roffkv {
56 struct roffstr key;
57 struct roffstr val;
58 struct roffkv *next; /* next in list */
59 };
60
61 /*
62 * A single number register as part of a singly-linked list.
63 */
64 struct roffreg {
65 struct roffstr key;
66 int val;
67 struct roffreg *next;
68 };
69
70 /*
71 * Association of request and macro names with token IDs.
72 */
73 struct roffreq {
74 enum roff_tok tok;
75 char name[];
76 };
77
78 struct roff {
79 struct mparse *parse; /* parse point */
80 struct roff_man *man; /* mdoc or man parser */
81 struct roffnode *last; /* leaf of stack */
82 int *rstack; /* stack of inverted `ie' values */
83 struct ohash *reqtab; /* request lookup table */
84 struct roffreg *regtab; /* number registers */
85 struct roffkv *strtab; /* user-defined strings & macros */
86 struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
87 struct roffstr *xtab; /* single-byte trans table (`tr') */
88 const char *current_string; /* value of last called user macro */
89 struct tbl_node *first_tbl; /* first table parsed */
90 struct tbl_node *last_tbl; /* last table parsed */
91 struct tbl_node *tbl; /* current table being parsed */
92 struct eqn_node *last_eqn; /* last equation parsed */
93 struct eqn_node *first_eqn; /* first equation parsed */
94 struct eqn_node *eqn; /* current equation being parsed */
95 int eqn_inline; /* current equation is inline */
96 int options; /* parse options */
97 int rstacksz; /* current size limit of rstack */
98 int rstackpos; /* position in rstack */
99 int format; /* current file in mdoc or man format */
100 int argc; /* number of args of the last macro */
101 char control; /* control character */
102 };
103
104 struct roffnode {
105 enum roff_tok tok; /* type of node */
106 struct roffnode *parent; /* up one in stack */
107 int line; /* parse line */
108 int col; /* parse col */
109 char *name; /* node name, e.g. macro name */
110 char *end; /* end-rules: custom token */
111 int endspan; /* end-rules: next-line or infty */
112 int rule; /* current evaluation rule */
113 };
114
115 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
116 enum roff_tok tok, /* tok of macro */ \
117 struct buf *buf, /* input buffer */ \
118 int ln, /* parse line */ \
119 int ppos, /* original pos in buffer */ \
120 int pos, /* current pos in buffer */ \
121 int *offs /* reset offset of buffer data */
122
123 typedef enum rofferr (*roffproc)(ROFF_ARGS);
124
125 struct roffmac {
126 roffproc proc; /* process new macro */
127 roffproc text; /* process as child text of macro */
128 roffproc sub; /* process as child of macro */
129 int flags;
130 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
131 };
132
133 struct predef {
134 const char *name; /* predefined input name */
135 const char *str; /* replacement symbol */
136 };
137
138 #define PREDEF(__name, __str) \
139 { (__name), (__str) },
140
141 /* --- function prototypes ------------------------------------------------ */
142
143 static void roffnode_cleanscope(struct roff *);
144 static void roffnode_pop(struct roff *);
145 static void roffnode_push(struct roff *, enum roff_tok,
146 const char *, int, int);
147 static enum rofferr roff_block(ROFF_ARGS);
148 static enum rofferr roff_block_text(ROFF_ARGS);
149 static enum rofferr roff_block_sub(ROFF_ARGS);
150 static enum rofferr roff_br(ROFF_ARGS);
151 static enum rofferr roff_cblock(ROFF_ARGS);
152 static enum rofferr roff_cc(ROFF_ARGS);
153 static void roff_ccond(struct roff *, int, int);
154 static enum rofferr roff_cond(ROFF_ARGS);
155 static enum rofferr roff_cond_text(ROFF_ARGS);
156 static enum rofferr roff_cond_sub(ROFF_ARGS);
157 static enum rofferr roff_ds(ROFF_ARGS);
158 static enum rofferr roff_eqndelim(struct roff *, struct buf *, int);
159 static int roff_evalcond(struct roff *r, int, char *, int *);
160 static int roff_evalnum(struct roff *, int,
161 const char *, int *, int *, int);
162 static int roff_evalpar(struct roff *, int,
163 const char *, int *, int *, int);
164 static int roff_evalstrcond(const char *, int *);
165 static void roff_free1(struct roff *);
166 static void roff_freereg(struct roffreg *);
167 static void roff_freestr(struct roffkv *);
168 static size_t roff_getname(struct roff *, char **, int, int);
169 static int roff_getnum(const char *, int *, int *, int);
170 static int roff_getop(const char *, int *, char *);
171 static int roff_getregn(const struct roff *,
172 const char *, size_t);
173 static int roff_getregro(const struct roff *,
174 const char *name);
175 static const char *roff_getstrn(const struct roff *,
176 const char *, size_t);
177 static int roff_hasregn(const struct roff *,
178 const char *, size_t);
179 static enum rofferr roff_insec(ROFF_ARGS);
180 static enum rofferr roff_it(ROFF_ARGS);
181 static enum rofferr roff_line_ignore(ROFF_ARGS);
182 static void roff_man_alloc1(struct roff_man *);
183 static void roff_man_free1(struct roff_man *);
184 static enum rofferr roff_manyarg(ROFF_ARGS);
185 static enum rofferr roff_nr(ROFF_ARGS);
186 static enum rofferr roff_onearg(ROFF_ARGS);
187 static enum roff_tok roff_parse(struct roff *, char *, int *,
188 int, int);
189 static enum rofferr roff_parsetext(struct buf *, int, int *);
190 static enum rofferr roff_res(struct roff *, struct buf *, int, int);
191 static enum rofferr roff_rm(ROFF_ARGS);
192 static enum rofferr roff_rr(ROFF_ARGS);
193 static void roff_setstr(struct roff *,
194 const char *, const char *, int);
195 static void roff_setstrn(struct roffkv **, const char *,
196 size_t, const char *, size_t, int);
197 static enum rofferr roff_so(ROFF_ARGS);
198 static enum rofferr roff_tr(ROFF_ARGS);
199 static enum rofferr roff_Dd(ROFF_ARGS);
200 static enum rofferr roff_TH(ROFF_ARGS);
201 static enum rofferr roff_TE(ROFF_ARGS);
202 static enum rofferr roff_TS(ROFF_ARGS);
203 static enum rofferr roff_EQ(ROFF_ARGS);
204 static enum rofferr roff_EN(ROFF_ARGS);
205 static enum rofferr roff_T_(ROFF_ARGS);
206 static enum rofferr roff_unsupp(ROFF_ARGS);
207 static enum rofferr roff_userdef(ROFF_ARGS);
208
209 /* --- constant data ------------------------------------------------------ */
210
211 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
212 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
213
214 const char *__roff_name[MAN_MAX + 1] = {
215 "br", "ft", "ll", "sp",
216 "ta", "ti", NULL,
217 "ab", "ad", "af", "aln",
218 "als", "am", "am1", "ami",
219 "ami1", "as", "as1", "asciify",
220 "backtrace", "bd", "bleedat", "blm",
221 "box", "boxa", "bp", "BP",
222 "break", "breakchar", "brnl", "brp",
223 "brpnl", "c2", "cc", "ce",
224 "cf", "cflags", "ch", "char",
225 "chop", "class", "close", "CL",
226 "color", "composite", "continue", "cp",
227 "cropat", "cs", "cu", "da",
228 "dch", "Dd", "de", "de1",
229 "defcolor", "dei", "dei1", "device",
230 "devicem", "di", "do", "ds",
231 "ds1", "dwh", "dt", "ec",
232 "ecr", "ecs", "el", "em",
233 "EN", "eo", "EP", "EQ",
234 "errprint", "ev", "evc", "ex",
235 "fallback", "fam", "fc", "fchar",
236 "fcolor", "fdeferlig", "feature", "fkern",
237 "fl", "flig", "fp", "fps",
238 "fschar", "fspacewidth", "fspecial", "ftr",
239 "fzoom", "gcolor", "hc", "hcode",
240 "hidechar", "hla", "hlm", "hpf",
241 "hpfa", "hpfcode", "hw", "hy",
242 "hylang", "hylen", "hym", "hypp",
243 "hys", "ie", "if", "ig",
244 "index", "it", "itc", "IX",
245 "kern", "kernafter", "kernbefore", "kernpair",
246 "lc", "lc_ctype", "lds", "length",
247 "letadj", "lf", "lg", "lhang",
248 "linetabs", "lnr", "lnrf", "lpfx",
249 "ls", "lsm", "lt", "mc",
250 "mediasize", "minss", "mk", "mso",
251 "na", "ne", "nh", "nhychar",
252 "nm", "nn", "nop", "nr",
253 "nrf", "nroff", "ns", "nx",
254 "open", "opena", "os", "output",
255 "padj", "papersize", "pc", "pev",
256 "pi", "PI", "pl", "pm",
257 "pn", "pnr", "po", "ps",
258 "psbb", "pshape", "pso", "ptr",
259 "pvs", "rchar", "rd", "recursionlimit",
260 "return", "rfschar", "rhang", "rj",
261 "rm", "rn", "rnn", "rr",
262 "rs", "rt", "schar", "sentchar",
263 "shc", "shift", "sizes", "so",
264 "spacewidth", "special", "spreadwarn", "ss",
265 "sty", "substring", "sv", "sy",
266 "T&", "tc", "TE",
267 "TH", "tkf", "tl",
268 "tm", "tm1", "tmc", "tr",
269 "track", "transchar", "trf", "trimat",
270 "trin", "trnt", "troff", "TS",
271 "uf", "ul", "unformat", "unwatch",
272 "unwatchn", "vpt", "vs", "warn",
273 "warnscale", "watch", "watchlength", "watchn",
274 "wh", "while", "write", "writec",
275 "writem", "xflag", ".", NULL,
276 "text",
277 "Dd", "Dt", "Os", "Sh",
278 "Ss", "Pp", "D1", "Dl",
279 "Bd", "Ed", "Bl", "El",
280 "It", "Ad", "An", "Ap",
281 "Ar", "Cd", "Cm", "Dv",
282 "Er", "Ev", "Ex", "Fa",
283 "Fd", "Fl", "Fn", "Ft",
284 "Ic", "In", "Li", "Nd",
285 "Nm", "Op", "Ot", "Pa",
286 "Rv", "St", "Va", "Vt",
287 "Xr", "%A", "%B", "%D",
288 "%I", "%J", "%N", "%O",
289 "%P", "%R", "%T", "%V",
290 "Ac", "Ao", "Aq", "At",
291 "Bc", "Bf", "Bo", "Bq",
292 "Bsx", "Bx", "Db", "Dc",
293 "Do", "Dq", "Ec", "Ef",
294 "Em", "Eo", "Fx", "Ms",
295 "No", "Ns", "Nx", "Ox",
296 "Pc", "Pf", "Po", "Pq",
297 "Qc", "Ql", "Qo", "Qq",
298 "Re", "Rs", "Sc", "So",
299 "Sq", "Sm", "Sx", "Sy",
300 "Tn", "Ux", "Xc", "Xo",
301 "Fo", "Fc", "Oo", "Oc",
302 "Bk", "Ek", "Bt", "Hf",
303 "Fr", "Ud", "Lb", "Lp",
304 "Lk", "Mt", "Brq", "Bro",
305 "Brc", "%C", "Es", "En",
306 "Dx", "%Q", "%U", "Ta",
307 NULL,
308 "TH", "SH", "SS", "TP",
309 "LP", "PP", "P", "IP",
310 "HP", "SM", "SB", "BI",
311 "IB", "BR", "RB", "R",
312 "B", "I", "IR", "RI",
313 "nf", "fi",
314 "RE", "RS", "DT", "UC",
315 "PD", "AT", "in",
316 "OP", "EX", "EE", "UR",
317 "UE", NULL
318 };
319 const char *const *roff_name = __roff_name;
320
321 static struct roffmac roffs[TOKEN_NONE] = {
322 { roff_br, NULL, NULL, 0 }, /* br */
323 { roff_onearg, NULL, NULL, 0 }, /* ft */
324 { roff_onearg, NULL, NULL, 0 }, /* ll */
325 { roff_onearg, NULL, NULL, 0 }, /* sp */
326 { roff_manyarg, NULL, NULL, 0 }, /* ta */
327 { roff_onearg, NULL, NULL, 0 }, /* ti */
328 { NULL, NULL, NULL, 0 }, /* ROFF_MAX */
329 { roff_unsupp, NULL, NULL, 0 }, /* ab */
330 { roff_line_ignore, NULL, NULL, 0 }, /* ad */
331 { roff_line_ignore, NULL, NULL, 0 }, /* af */
332 { roff_unsupp, NULL, NULL, 0 }, /* aln */
333 { roff_unsupp, NULL, NULL, 0 }, /* als */
334 { roff_block, roff_block_text, roff_block_sub, 0 }, /* am */
335 { roff_block, roff_block_text, roff_block_sub, 0 }, /* am1 */
336 { roff_block, roff_block_text, roff_block_sub, 0 }, /* ami */
337 { roff_block, roff_block_text, roff_block_sub, 0 }, /* ami1 */
338 { roff_ds, NULL, NULL, 0 }, /* as */
339 { roff_ds, NULL, NULL, 0 }, /* as1 */
340 { roff_unsupp, NULL, NULL, 0 }, /* asciify */
341 { roff_line_ignore, NULL, NULL, 0 }, /* backtrace */
342 { roff_line_ignore, NULL, NULL, 0 }, /* bd */
343 { roff_line_ignore, NULL, NULL, 0 }, /* bleedat */
344 { roff_unsupp, NULL, NULL, 0 }, /* blm */
345 { roff_unsupp, NULL, NULL, 0 }, /* box */
346 { roff_unsupp, NULL, NULL, 0 }, /* boxa */
347 { roff_line_ignore, NULL, NULL, 0 }, /* bp */
348 { roff_unsupp, NULL, NULL, 0 }, /* BP */
349 { roff_unsupp, NULL, NULL, 0 }, /* break */
350 { roff_line_ignore, NULL, NULL, 0 }, /* breakchar */
351 { roff_line_ignore, NULL, NULL, 0 }, /* brnl */
352 { roff_br, NULL, NULL, 0 }, /* brp */
353 { roff_line_ignore, NULL, NULL, 0 }, /* brpnl */
354 { roff_unsupp, NULL, NULL, 0 }, /* c2 */
355 { roff_cc, NULL, NULL, 0 }, /* cc */
356 { roff_line_ignore, NULL, NULL, 0 }, /* ce */
357 { roff_insec, NULL, NULL, 0 }, /* cf */
358 { roff_line_ignore, NULL, NULL, 0 }, /* cflags */
359 { roff_line_ignore, NULL, NULL, 0 }, /* ch */
360 { roff_unsupp, NULL, NULL, 0 }, /* char */
361 { roff_unsupp, NULL, NULL, 0 }, /* chop */
362 { roff_line_ignore, NULL, NULL, 0 }, /* class */
363 { roff_insec, NULL, NULL, 0 }, /* close */
364 { roff_unsupp, NULL, NULL, 0 }, /* CL */
365 { roff_line_ignore, NULL, NULL, 0 }, /* color */
366 { roff_unsupp, NULL, NULL, 0 }, /* composite */
367 { roff_unsupp, NULL, NULL, 0 }, /* continue */
368 { roff_line_ignore, NULL, NULL, 0 }, /* cp */
369 { roff_line_ignore, NULL, NULL, 0 }, /* cropat */
370 { roff_line_ignore, NULL, NULL, 0 }, /* cs */
371 { roff_line_ignore, NULL, NULL, 0 }, /* cu */
372 { roff_unsupp, NULL, NULL, 0 }, /* da */
373 { roff_unsupp, NULL, NULL, 0 }, /* dch */
374 { roff_Dd, NULL, NULL, 0 }, /* Dd */
375 { roff_block, roff_block_text, roff_block_sub, 0 }, /* de */
376 { roff_block, roff_block_text, roff_block_sub, 0 }, /* de1 */
377 { roff_line_ignore, NULL, NULL, 0 }, /* defcolor */
378 { roff_block, roff_block_text, roff_block_sub, 0 }, /* dei */
379 { roff_block, roff_block_text, roff_block_sub, 0 }, /* dei1 */
380 { roff_unsupp, NULL, NULL, 0 }, /* device */
381 { roff_unsupp, NULL, NULL, 0 }, /* devicem */
382 { roff_unsupp, NULL, NULL, 0 }, /* di */
383 { roff_unsupp, NULL, NULL, 0 }, /* do */
384 { roff_ds, NULL, NULL, 0 }, /* ds */
385 { roff_ds, NULL, NULL, 0 }, /* ds1 */
386 { roff_unsupp, NULL, NULL, 0 }, /* dwh */
387 { roff_unsupp, NULL, NULL, 0 }, /* dt */
388 { roff_unsupp, NULL, NULL, 0 }, /* ec */
389 { roff_unsupp, NULL, NULL, 0 }, /* ecr */
390 { roff_unsupp, NULL, NULL, 0 }, /* ecs */
391 { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* el */
392 { roff_unsupp, NULL, NULL, 0 }, /* em */
393 { roff_EN, NULL, NULL, 0 }, /* EN */
394 { roff_unsupp, NULL, NULL, 0 }, /* eo */
395 { roff_unsupp, NULL, NULL, 0 }, /* EP */
396 { roff_EQ, NULL, NULL, 0 }, /* EQ */
397 { roff_line_ignore, NULL, NULL, 0 }, /* errprint */
398 { roff_unsupp, NULL, NULL, 0 }, /* ev */
399 { roff_unsupp, NULL, NULL, 0 }, /* evc */
400 { roff_unsupp, NULL, NULL, 0 }, /* ex */
401 { roff_line_ignore, NULL, NULL, 0 }, /* fallback */
402 { roff_line_ignore, NULL, NULL, 0 }, /* fam */
403 { roff_unsupp, NULL, NULL, 0 }, /* fc */
404 { roff_unsupp, NULL, NULL, 0 }, /* fchar */
405 { roff_line_ignore, NULL, NULL, 0 }, /* fcolor */
406 { roff_line_ignore, NULL, NULL, 0 }, /* fdeferlig */
407 { roff_line_ignore, NULL, NULL, 0 }, /* feature */
408 { roff_line_ignore, NULL, NULL, 0 }, /* fkern */
409 { roff_line_ignore, NULL, NULL, 0 }, /* fl */
410 { roff_line_ignore, NULL, NULL, 0 }, /* flig */
411 { roff_line_ignore, NULL, NULL, 0 }, /* fp */
412 { roff_line_ignore, NULL, NULL, 0 }, /* fps */
413 { roff_unsupp, NULL, NULL, 0 }, /* fschar */
414 { roff_line_ignore, NULL, NULL, 0 }, /* fspacewidth */
415 { roff_line_ignore, NULL, NULL, 0 }, /* fspecial */
416 { roff_line_ignore, NULL, NULL, 0 }, /* ftr */
417 { roff_line_ignore, NULL, NULL, 0 }, /* fzoom */
418 { roff_line_ignore, NULL, NULL, 0 }, /* gcolor */
419 { roff_line_ignore, NULL, NULL, 0 }, /* hc */
420 { roff_line_ignore, NULL, NULL, 0 }, /* hcode */
421 { roff_line_ignore, NULL, NULL, 0 }, /* hidechar */
422 { roff_line_ignore, NULL, NULL, 0 }, /* hla */
423 { roff_line_ignore, NULL, NULL, 0 }, /* hlm */
424 { roff_line_ignore, NULL, NULL, 0 }, /* hpf */
425 { roff_line_ignore, NULL, NULL, 0 }, /* hpfa */
426 { roff_line_ignore, NULL, NULL, 0 }, /* hpfcode */
427 { roff_line_ignore, NULL, NULL, 0 }, /* hw */
428 { roff_line_ignore, NULL, NULL, 0 }, /* hy */
429 { roff_line_ignore, NULL, NULL, 0 }, /* hylang */
430 { roff_line_ignore, NULL, NULL, 0 }, /* hylen */
431 { roff_line_ignore, NULL, NULL, 0 }, /* hym */
432 { roff_line_ignore, NULL, NULL, 0 }, /* hypp */
433 { roff_line_ignore, NULL, NULL, 0 }, /* hys */
434 { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* ie */
435 { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* if */
436 { roff_block, roff_block_text, roff_block_sub, 0 }, /* ig */
437 { roff_unsupp, NULL, NULL, 0 }, /* index */
438 { roff_it, NULL, NULL, 0 }, /* it */
439 { roff_unsupp, NULL, NULL, 0 }, /* itc */
440 { roff_line_ignore, NULL, NULL, 0 }, /* IX */
441 { roff_line_ignore, NULL, NULL, 0 }, /* kern */
442 { roff_line_ignore, NULL, NULL, 0 }, /* kernafter */
443 { roff_line_ignore, NULL, NULL, 0 }, /* kernbefore */
444 { roff_line_ignore, NULL, NULL, 0 }, /* kernpair */
445 { roff_unsupp, NULL, NULL, 0 }, /* lc */
446 { roff_unsupp, NULL, NULL, 0 }, /* lc_ctype */
447 { roff_unsupp, NULL, NULL, 0 }, /* lds */
448 { roff_unsupp, NULL, NULL, 0 }, /* length */
449 { roff_line_ignore, NULL, NULL, 0 }, /* letadj */
450 { roff_insec, NULL, NULL, 0 }, /* lf */
451 { roff_line_ignore, NULL, NULL, 0 }, /* lg */
452 { roff_line_ignore, NULL, NULL, 0 }, /* lhang */
453 { roff_unsupp, NULL, NULL, 0 }, /* linetabs */
454 { roff_unsupp, NULL, NULL, 0 }, /* lnr */
455 { roff_unsupp, NULL, NULL, 0 }, /* lnrf */
456 { roff_unsupp, NULL, NULL, 0 }, /* lpfx */
457 { roff_line_ignore, NULL, NULL, 0 }, /* ls */
458 { roff_unsupp, NULL, NULL, 0 }, /* lsm */
459 { roff_line_ignore, NULL, NULL, 0 }, /* lt */
460 { roff_line_ignore, NULL, NULL, 0 }, /* mc */
461 { roff_line_ignore, NULL, NULL, 0 }, /* mediasize */
462 { roff_line_ignore, NULL, NULL, 0 }, /* minss */
463 { roff_line_ignore, NULL, NULL, 0 }, /* mk */
464 { roff_insec, NULL, NULL, 0 }, /* mso */
465 { roff_line_ignore, NULL, NULL, 0 }, /* na */
466 { roff_line_ignore, NULL, NULL, 0 }, /* ne */
467 { roff_line_ignore, NULL, NULL, 0 }, /* nh */
468 { roff_line_ignore, NULL, NULL, 0 }, /* nhychar */
469 { roff_unsupp, NULL, NULL, 0 }, /* nm */
470 { roff_unsupp, NULL, NULL, 0 }, /* nn */
471 { roff_unsupp, NULL, NULL, 0 }, /* nop */
472 { roff_nr, NULL, NULL, 0 }, /* nr */
473 { roff_unsupp, NULL, NULL, 0 }, /* nrf */
474 { roff_line_ignore, NULL, NULL, 0 }, /* nroff */
475 { roff_line_ignore, NULL, NULL, 0 }, /* ns */
476 { roff_insec, NULL, NULL, 0 }, /* nx */
477 { roff_insec, NULL, NULL, 0 }, /* open */
478 { roff_insec, NULL, NULL, 0 }, /* opena */
479 { roff_line_ignore, NULL, NULL, 0 }, /* os */
480 { roff_unsupp, NULL, NULL, 0 }, /* output */
481 { roff_line_ignore, NULL, NULL, 0 }, /* padj */
482 { roff_line_ignore, NULL, NULL, 0 }, /* papersize */
483 { roff_line_ignore, NULL, NULL, 0 }, /* pc */
484 { roff_line_ignore, NULL, NULL, 0 }, /* pev */
485 { roff_insec, NULL, NULL, 0 }, /* pi */
486 { roff_unsupp, NULL, NULL, 0 }, /* PI */
487 { roff_line_ignore, NULL, NULL, 0 }, /* pl */
488 { roff_line_ignore, NULL, NULL, 0 }, /* pm */
489 { roff_line_ignore, NULL, NULL, 0 }, /* pn */
490 { roff_line_ignore, NULL, NULL, 0 }, /* pnr */
491 { roff_line_ignore, NULL, NULL, 0 }, /* po */
492 { roff_line_ignore, NULL, NULL, 0 }, /* ps */
493 { roff_unsupp, NULL, NULL, 0 }, /* psbb */
494 { roff_unsupp, NULL, NULL, 0 }, /* pshape */
495 { roff_insec, NULL, NULL, 0 }, /* pso */
496 { roff_line_ignore, NULL, NULL, 0 }, /* ptr */
497 { roff_line_ignore, NULL, NULL, 0 }, /* pvs */
498 { roff_unsupp, NULL, NULL, 0 }, /* rchar */
499 { roff_line_ignore, NULL, NULL, 0 }, /* rd */
500 { roff_line_ignore, NULL, NULL, 0 }, /* recursionlimit */
501 { roff_unsupp, NULL, NULL, 0 }, /* return */
502 { roff_unsupp, NULL, NULL, 0 }, /* rfschar */
503 { roff_line_ignore, NULL, NULL, 0 }, /* rhang */
504 { roff_line_ignore, NULL, NULL, 0 }, /* rj */
505 { roff_rm, NULL, NULL, 0 }, /* rm */
506 { roff_unsupp, NULL, NULL, 0 }, /* rn */
507 { roff_unsupp, NULL, NULL, 0 }, /* rnn */
508 { roff_rr, NULL, NULL, 0 }, /* rr */
509 { roff_line_ignore, NULL, NULL, 0 }, /* rs */
510 { roff_line_ignore, NULL, NULL, 0 }, /* rt */
511 { roff_unsupp, NULL, NULL, 0 }, /* schar */
512 { roff_line_ignore, NULL, NULL, 0 }, /* sentchar */
513 { roff_line_ignore, NULL, NULL, 0 }, /* shc */
514 { roff_unsupp, NULL, NULL, 0 }, /* shift */
515 { roff_line_ignore, NULL, NULL, 0 }, /* sizes */
516 { roff_so, NULL, NULL, 0 }, /* so */
517 { roff_line_ignore, NULL, NULL, 0 }, /* spacewidth */
518 { roff_line_ignore, NULL, NULL, 0 }, /* special */
519 { roff_line_ignore, NULL, NULL, 0 }, /* spreadwarn */
520 { roff_line_ignore, NULL, NULL, 0 }, /* ss */
521 { roff_line_ignore, NULL, NULL, 0 }, /* sty */
522 { roff_unsupp, NULL, NULL, 0 }, /* substring */
523 { roff_line_ignore, NULL, NULL, 0 }, /* sv */
524 { roff_insec, NULL, NULL, 0 }, /* sy */
525 { roff_T_, NULL, NULL, 0 }, /* T& */
526 { roff_unsupp, NULL, NULL, 0 }, /* tc */
527 { roff_TE, NULL, NULL, 0 }, /* TE */
528 { roff_TH, NULL, NULL, 0 }, /* TH */
529 { roff_line_ignore, NULL, NULL, 0 }, /* tkf */
530 { roff_unsupp, NULL, NULL, 0 }, /* tl */
531 { roff_line_ignore, NULL, NULL, 0 }, /* tm */
532 { roff_line_ignore, NULL, NULL, 0 }, /* tm1 */
533 { roff_line_ignore, NULL, NULL, 0 }, /* tmc */
534 { roff_tr, NULL, NULL, 0 }, /* tr */
535 { roff_line_ignore, NULL, NULL, 0 }, /* track */
536 { roff_line_ignore, NULL, NULL, 0 }, /* transchar */
537 { roff_insec, NULL, NULL, 0 }, /* trf */
538 { roff_line_ignore, NULL, NULL, 0 }, /* trimat */
539 { roff_unsupp, NULL, NULL, 0 }, /* trin */
540 { roff_unsupp, NULL, NULL, 0 }, /* trnt */
541 { roff_line_ignore, NULL, NULL, 0 }, /* troff */
542 { roff_TS, NULL, NULL, 0 }, /* TS */
543 { roff_line_ignore, NULL, NULL, 0 }, /* uf */
544 { roff_line_ignore, NULL, NULL, 0 }, /* ul */
545 { roff_unsupp, NULL, NULL, 0 }, /* unformat */
546 { roff_line_ignore, NULL, NULL, 0 }, /* unwatch */
547 { roff_line_ignore, NULL, NULL, 0 }, /* unwatchn */
548 { roff_line_ignore, NULL, NULL, 0 }, /* vpt */
549 { roff_line_ignore, NULL, NULL, 0 }, /* vs */
550 { roff_line_ignore, NULL, NULL, 0 }, /* warn */
551 { roff_line_ignore, NULL, NULL, 0 }, /* warnscale */
552 { roff_line_ignore, NULL, NULL, 0 }, /* watch */
553 { roff_line_ignore, NULL, NULL, 0 }, /* watchlength */
554 { roff_line_ignore, NULL, NULL, 0 }, /* watchn */
555 { roff_unsupp, NULL, NULL, 0 }, /* wh */
556 { roff_unsupp, NULL, NULL, 0 }, /* while */
557 { roff_insec, NULL, NULL, 0 }, /* write */
558 { roff_insec, NULL, NULL, 0 }, /* writec */
559 { roff_insec, NULL, NULL, 0 }, /* writem */
560 { roff_line_ignore, NULL, NULL, 0 }, /* xflag */
561 { roff_cblock, NULL, NULL, 0 }, /* . */
562 { roff_userdef, NULL, NULL, 0 }
563 };
564
565 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
566 const char *const __mdoc_reserved[] = {
567 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
568 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
569 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
570 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
571 "Dt", "Dv", "Dx", "D1",
572 "Ec", "Ed", "Ef", "Ek", "El", "Em",
573 "En", "Eo", "Er", "Es", "Ev", "Ex",
574 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
575 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
576 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
577 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
578 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
579 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
580 "Sc", "Sh", "Sm", "So", "Sq",
581 "Ss", "St", "Sx", "Sy",
582 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
583 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
584 "%P", "%Q", "%R", "%T", "%U", "%V",
585 NULL
586 };
587
588 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
589 const char *const __man_reserved[] = {
590 "AT", "B", "BI", "BR", "DT",
591 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
592 "LP", "OP", "P", "PD", "PP",
593 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
594 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
595 NULL
596 };
597
598 /* Array of injected predefined strings. */
599 #define PREDEFS_MAX 38
600 static const struct predef predefs[PREDEFS_MAX] = {
601 #include "predefs.in"
602 };
603
604 static int roffit_lines; /* number of lines to delay */
605 static char *roffit_macro; /* nil-terminated macro line */
606
607
608 /* --- request table ------------------------------------------------------ */
609
610 struct ohash *
611 roffhash_alloc(enum roff_tok mintok, enum roff_tok maxtok)
612 {
613 struct ohash *htab;
614 struct roffreq *req;
615 enum roff_tok tok;
616 size_t sz;
617 unsigned int slot;
618
619 htab = mandoc_malloc(sizeof(*htab));
620 mandoc_ohash_init(htab, 8, offsetof(struct roffreq, name));
621
622 for (tok = mintok; tok < maxtok; tok++) {
623 if (roff_name[tok] == NULL)
624 continue;
625 sz = strlen(roff_name[tok]);
626 req = mandoc_malloc(sizeof(*req) + sz + 1);
627 req->tok = tok;
628 memcpy(req->name, roff_name[tok], sz + 1);
629 slot = ohash_qlookup(htab, req->name);
630 ohash_insert(htab, slot, req);
631 }
632 return htab;
633 }
634
635 void
636 roffhash_free(struct ohash *htab)
637 {
638 struct roffreq *req;
639 unsigned int slot;
640
641 if (htab == NULL)
642 return;
643 for (req = ohash_first(htab, &slot); req != NULL;
644 req = ohash_next(htab, &slot))
645 free(req);
646 ohash_delete(htab);
647 free(htab);
648 }
649
650 enum roff_tok
651 roffhash_find(struct ohash *htab, const char *name, size_t sz)
652 {
653 struct roffreq *req;
654 const char *end;
655
656 if (sz) {
657 end = name + sz;
658 req = ohash_find(htab, ohash_qlookupi(htab, name, &end));
659 } else
660 req = ohash_find(htab, ohash_qlookup(htab, name));
661 return req == NULL ? TOKEN_NONE : req->tok;
662 }
663
664 /* --- stack of request blocks -------------------------------------------- */
665
666 /*
667 * Pop the current node off of the stack of roff instructions currently
668 * pending.
669 */
670 static void
671 roffnode_pop(struct roff *r)
672 {
673 struct roffnode *p;
674
675 assert(r->last);
676 p = r->last;
677
678 r->last = r->last->parent;
679 free(p->name);
680 free(p->end);
681 free(p);
682 }
683
684 /*
685 * Push a roff node onto the instruction stack. This must later be
686 * removed with roffnode_pop().
687 */
688 static void
689 roffnode_push(struct roff *r, enum roff_tok tok, const char *name,
690 int line, int col)
691 {
692 struct roffnode *p;
693
694 p = mandoc_calloc(1, sizeof(struct roffnode));
695 p->tok = tok;
696 if (name)
697 p->name = mandoc_strdup(name);
698 p->parent = r->last;
699 p->line = line;
700 p->col = col;
701 p->rule = p->parent ? p->parent->rule : 0;
702
703 r->last = p;
704 }
705
706 /* --- roff parser state data management ---------------------------------- */
707
708 static void
709 roff_free1(struct roff *r)
710 {
711 struct tbl_node *tbl;
712 struct eqn_node *e;
713 int i;
714
715 while (NULL != (tbl = r->first_tbl)) {
716 r->first_tbl = tbl->next;
717 tbl_free(tbl);
718 }
719 r->first_tbl = r->last_tbl = r->tbl = NULL;
720
721 while (NULL != (e = r->first_eqn)) {
722 r->first_eqn = e->next;
723 eqn_free(e);
724 }
725 r->first_eqn = r->last_eqn = r->eqn = NULL;
726
727 while (r->last)
728 roffnode_pop(r);
729
730 free (r->rstack);
731 r->rstack = NULL;
732 r->rstacksz = 0;
733 r->rstackpos = -1;
734
735 roff_freereg(r->regtab);
736 r->regtab = NULL;
737
738 roff_freestr(r->strtab);
739 roff_freestr(r->xmbtab);
740 r->strtab = r->xmbtab = NULL;
741
742 if (r->xtab)
743 for (i = 0; i < 128; i++)
744 free(r->xtab[i].p);
745 free(r->xtab);
746 r->xtab = NULL;
747 }
748
749 void
750 roff_reset(struct roff *r)
751 {
752 roff_free1(r);
753 r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
754 r->control = 0;
755 }
756
757 void
758 roff_free(struct roff *r)
759 {
760 roff_free1(r);
761 roffhash_free(r->reqtab);
762 free(r);
763 }
764
765 struct roff *
766 roff_alloc(struct mparse *parse, int options)
767 {
768 struct roff *r;
769
770 r = mandoc_calloc(1, sizeof(struct roff));
771 r->parse = parse;
772 r->reqtab = roffhash_alloc(0, ROFF_USERDEF);
773 r->options = options;
774 r->format = options & (MPARSE_MDOC | MPARSE_MAN);
775 r->rstackpos = -1;
776 return r;
777 }
778
779 /* --- syntax tree state data management ---------------------------------- */
780
781 static void
782 roff_man_free1(struct roff_man *man)
783 {
784
785 if (man->first != NULL)
786 roff_node_delete(man, man->first);
787 free(man->meta.msec);
788 free(man->meta.vol);
789 free(man->meta.os);
790 free(man->meta.arch);
791 free(man->meta.title);
792 free(man->meta.name);
793 free(man->meta.date);
794 }
795
796 static void
797 roff_man_alloc1(struct roff_man *man)
798 {
799
800 memset(&man->meta, 0, sizeof(man->meta));
801 man->first = mandoc_calloc(1, sizeof(*man->first));
802 man->first->type = ROFFT_ROOT;
803 man->last = man->first;
804 man->last_es = NULL;
805 man->flags = 0;
806 man->macroset = MACROSET_NONE;
807 man->lastsec = man->lastnamed = SEC_NONE;
808 man->next = ROFF_NEXT_CHILD;
809 }
810
811 void
812 roff_man_reset(struct roff_man *man)
813 {
814
815 roff_man_free1(man);
816 roff_man_alloc1(man);
817 }
818
819 void
820 roff_man_free(struct roff_man *man)
821 {
822
823 roff_man_free1(man);
824 free(man);
825 }
826
827 struct roff_man *
828 roff_man_alloc(struct roff *roff, struct mparse *parse,
829 const char *defos, int quick)
830 {
831 struct roff_man *man;
832
833 man = mandoc_calloc(1, sizeof(*man));
834 man->parse = parse;
835 man->roff = roff;
836 man->defos = defos;
837 man->quick = quick;
838 roff_man_alloc1(man);
839 roff->man = man;
840 return man;
841 }
842
843 /* --- syntax tree handling ----------------------------------------------- */
844
845 struct roff_node *
846 roff_node_alloc(struct roff_man *man, int line, int pos,
847 enum roff_type type, int tok)
848 {
849 struct roff_node *n;
850
851 n = mandoc_calloc(1, sizeof(*n));
852 n->line = line;
853 n->pos = pos;
854 n->tok = tok;
855 n->type = type;
856 n->sec = man->lastsec;
857
858 if (man->flags & MDOC_SYNOPSIS)
859 n->flags |= NODE_SYNPRETTY;
860 else
861 n->flags &= ~NODE_SYNPRETTY;
862 if (man->flags & MDOC_NEWLINE)
863 n->flags |= NODE_LINE;
864 man->flags &= ~MDOC_NEWLINE;
865
866 return n;
867 }
868
869 void
870 roff_node_append(struct roff_man *man, struct roff_node *n)
871 {
872
873 switch (man->next) {
874 case ROFF_NEXT_SIBLING:
875 if (man->last->next != NULL) {
876 n->next = man->last->next;
877 man->last->next->prev = n;
878 } else
879 man->last->parent->last = n;
880 man->last->next = n;
881 n->prev = man->last;
882 n->parent = man->last->parent;
883 break;
884 case ROFF_NEXT_CHILD:
885 if (man->last->child != NULL) {
886 n->next = man->last->child;
887 man->last->child->prev = n;
888 } else
889 man->last->last = n;
890 man->last->child = n;
891 n->parent = man->last;
892 break;
893 default:
894 abort();
895 }
896 man->last = n;
897
898 switch (n->type) {
899 case ROFFT_HEAD:
900 n->parent->head = n;
901 break;
902 case ROFFT_BODY:
903 if (n->end != ENDBODY_NOT)
904 return;
905 n->parent->body = n;
906 break;
907 case ROFFT_TAIL:
908 n->parent->tail = n;
909 break;
910 default:
911 return;
912 }
913
914 /*
915 * Copy over the normalised-data pointer of our parent. Not
916 * everybody has one, but copying a null pointer is fine.
917 */
918
919 n->norm = n->parent->norm;
920 assert(n->parent->type == ROFFT_BLOCK);
921 }
922
923 void
924 roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
925 {
926 struct roff_node *n;
927
928 n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
929 n->string = roff_strdup(man->roff, word);
930 roff_node_append(man, n);
931 n->flags |= NODE_VALID | NODE_ENDED;
932 man->next = ROFF_NEXT_SIBLING;
933 }
934
935 void
936 roff_word_append(struct roff_man *man, const char *word)
937 {
938 struct roff_node *n;
939 char *addstr, *newstr;
940
941 n = man->last;
942 addstr = roff_strdup(man->roff, word);
943 mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
944 free(addstr);
945 free(n->string);
946 n->string = newstr;
947 man->next = ROFF_NEXT_SIBLING;
948 }
949
950 void
951 roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
952 {
953 struct roff_node *n;
954
955 n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
956 roff_node_append(man, n);
957 man->next = ROFF_NEXT_CHILD;
958 }
959
960 struct roff_node *
961 roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
962 {
963 struct roff_node *n;
964
965 n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
966 roff_node_append(man, n);
967 man->next = ROFF_NEXT_CHILD;
968 return n;
969 }
970
971 struct roff_node *
972 roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
973 {
974 struct roff_node *n;
975
976 n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
977 roff_node_append(man, n);
978 man->next = ROFF_NEXT_CHILD;
979 return n;
980 }
981
982 struct roff_node *
983 roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
984 {
985 struct roff_node *n;
986
987 n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
988 roff_node_append(man, n);
989 man->next = ROFF_NEXT_CHILD;
990 return n;
991 }
992
993 void
994 roff_addeqn(struct roff_man *man, const struct eqn *eqn)
995 {
996 struct roff_node *n;
997
998 n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
999 n->eqn = eqn;
1000 if (eqn->ln > man->last->line)
1001 n->flags |= NODE_LINE;
1002 roff_node_append(man, n);
1003 man->next = ROFF_NEXT_SIBLING;
1004 }
1005
1006 void
1007 roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
1008 {
1009 struct roff_node *n;
1010
1011 if (man->macroset == MACROSET_MAN)
1012 man_breakscope(man, ROFF_TS);
1013 n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
1014 n->span = tbl;
1015 roff_node_append(man, n);
1016 n->flags |= NODE_VALID | NODE_ENDED;
1017 man->next = ROFF_NEXT_SIBLING;
1018 }
1019
1020 void
1021 roff_node_unlink(struct roff_man *man, struct roff_node *n)
1022 {
1023
1024 /* Adjust siblings. */
1025
1026 if (n->prev)
1027 n->prev->next = n->next;
1028 if (n->next)
1029 n->next->prev = n->prev;
1030
1031 /* Adjust parent. */
1032
1033 if (n->parent != NULL) {
1034 if (n->parent->child == n)
1035 n->parent->child = n->next;
1036 if (n->parent->last == n)
1037 n->parent->last = n->prev;
1038 }
1039
1040 /* Adjust parse point. */
1041
1042 if (man == NULL)
1043 return;
1044 if (man->last == n) {
1045 if (n->prev == NULL) {
1046 man->last = n->parent;
1047 man->next = ROFF_NEXT_CHILD;
1048 } else {
1049 man->last = n->prev;
1050 man->next = ROFF_NEXT_SIBLING;
1051 }
1052 }
1053 if (man->first == n)
1054 man->first = NULL;
1055 }
1056
1057 void
1058 roff_node_free(struct roff_node *n)
1059 {
1060
1061 if (n->args != NULL)
1062 mdoc_argv_free(n->args);
1063 if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
1064 free(n->norm);
1065 free(n->string);
1066 free(n);
1067 }
1068
1069 void
1070 roff_node_delete(struct roff_man *man, struct roff_node *n)
1071 {
1072
1073 while (n->child != NULL)
1074 roff_node_delete(man, n->child);
1075 roff_node_unlink(man, n);
1076 roff_node_free(n);
1077 }
1078
1079 void
1080 deroff(char **dest, const struct roff_node *n)
1081 {
1082 char *cp;
1083 size_t sz;
1084
1085 if (n->type != ROFFT_TEXT) {
1086 for (n = n->child; n != NULL; n = n->next)
1087 deroff(dest, n);
1088 return;
1089 }
1090
1091 /* Skip leading whitespace. */
1092
1093 for (cp = n->string; *cp != '\0'; cp++) {
1094 if (cp[0] == '\\' && cp[1] != '\0' &&
1095 strchr(" %&0^|~", cp[1]) != NULL)
1096 cp++;
1097 else if ( ! isspace((unsigned char)*cp))
1098 break;
1099 }
1100
1101 /* Skip trailing backslash. */
1102
1103 sz = strlen(cp);
1104 if (sz > 0 && cp[sz - 1] == '\\')
1105 sz--;
1106
1107 /* Skip trailing whitespace. */
1108
1109 for (; sz; sz--)
1110 if ( ! isspace((unsigned char)cp[sz-1]))
1111 break;
1112
1113 /* Skip empty strings. */
1114
1115 if (sz == 0)
1116 return;
1117
1118 if (*dest == NULL) {
1119 *dest = mandoc_strndup(cp, sz);
1120 return;
1121 }
1122
1123 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
1124 free(*dest);
1125 *dest = cp;
1126 }
1127
1128 /* --- main functions of the roff parser ---------------------------------- */
1129
1130 /*
1131 * In the current line, expand escape sequences that tend to get
1132 * used in numerical expressions and conditional requests.
1133 * Also check the syntax of the remaining escape sequences.
1134 */
1135 static enum rofferr
1136 roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1137 {
1138 char ubuf[24]; /* buffer to print the number */
1139 const char *start; /* start of the string to process */
1140 char *stesc; /* start of an escape sequence ('\\') */
1141 const char *stnam; /* start of the name, after "[(*" */
1142 const char *cp; /* end of the name, e.g. before ']' */
1143 const char *res; /* the string to be substituted */
1144 char *nbuf; /* new buffer to copy buf->buf to */
1145 size_t maxl; /* expected length of the escape name */
1146 size_t naml; /* actual length of the escape name */
1147 enum mandoc_esc esc; /* type of the escape sequence */
1148 int inaml; /* length returned from mandoc_escape() */
1149 int expand_count; /* to avoid infinite loops */
1150 int npos; /* position in numeric expression */
1151 int arg_complete; /* argument not interrupted by eol */
1152 char term; /* character terminating the escape */
1153
1154 expand_count = 0;
1155 start = buf->buf + pos;
1156 stesc = strchr(start, '\0') - 1;
1157 while (stesc-- > start) {
1158
1159 /* Search backwards for the next backslash. */
1160
1161 if (*stesc != '\\')
1162 continue;
1163
1164 /* If it is escaped, skip it. */
1165
1166 for (cp = stesc - 1; cp >= start; cp--)
1167 if (*cp != '\\')
1168 break;
1169
1170 if ((stesc - cp) % 2 == 0) {
1171 stesc = (char *)cp;
1172 continue;
1173 }
1174
1175 /* Decide whether to expand or to check only. */
1176
1177 term = '\0';
1178 cp = stesc + 1;
1179 switch (*cp) {
1180 case '*':
1181 res = NULL;
1182 break;
1183 case 'B':
1184 case 'w':
1185 term = cp[1];
1186 /* FALLTHROUGH */
1187 case 'n':
1188 res = ubuf;
1189 break;
1190 default:
1191 esc = mandoc_escape(&cp, &stnam, &inaml);
1192 if (esc == ESCAPE_ERROR ||
1193 (esc == ESCAPE_SPECIAL &&
1194 mchars_spec2cp(stnam, inaml) < 0))
1195 mandoc_vmsg(MANDOCERR_ESC_BAD,
1196 r->parse, ln, (int)(stesc - buf->buf),
1197 "%.*s", (int)(cp - stesc), stesc);
1198 continue;
1199 }
1200
1201 if (EXPAND_LIMIT < ++expand_count) {
1202 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1203 ln, (int)(stesc - buf->buf), NULL);
1204 return ROFF_IGN;
1205 }
1206
1207 /*
1208 * The third character decides the length
1209 * of the name of the string or register.
1210 * Save a pointer to the name.
1211 */
1212
1213 if (term == '\0') {
1214 switch (*++cp) {
1215 case '\0':
1216 maxl = 0;
1217 break;
1218 case '(':
1219 cp++;
1220 maxl = 2;
1221 break;
1222 case '[':
1223 cp++;
1224 term = ']';
1225 maxl = 0;
1226 break;
1227 default:
1228 maxl = 1;
1229 break;
1230 }
1231 } else {
1232 cp += 2;
1233 maxl = 0;
1234 }
1235 stnam = cp;
1236
1237 /* Advance to the end of the name. */
1238
1239 naml = 0;
1240 arg_complete = 1;
1241 while (maxl == 0 || naml < maxl) {
1242 if (*cp == '\0') {
1243 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1244 ln, (int)(stesc - buf->buf), stesc);
1245 arg_complete = 0;
1246 break;
1247 }
1248 if (maxl == 0 && *cp == term) {
1249 cp++;
1250 break;
1251 }
1252 if (*cp++ != '\\' || stesc[1] != 'w') {
1253 naml++;
1254 continue;
1255 }
1256 switch (mandoc_escape(&cp, NULL, NULL)) {
1257 case ESCAPE_SPECIAL:
1258 case ESCAPE_UNICODE:
1259 case ESCAPE_NUMBERED:
1260 case ESCAPE_OVERSTRIKE:
1261 naml++;
1262 break;
1263 default:
1264 break;
1265 }
1266 }
1267
1268 /*
1269 * Retrieve the replacement string; if it is
1270 * undefined, resume searching for escapes.
1271 */
1272
1273 switch (stesc[1]) {
1274 case '*':
1275 if (arg_complete)
1276 res = roff_getstrn(r, stnam, naml);
1277 break;
1278 case 'B':
1279 npos = 0;
1280 ubuf[0] = arg_complete &&
1281 roff_evalnum(r, ln, stnam, &npos,
1282 NULL, ROFFNUM_SCALE) &&
1283 stnam + npos + 1 == cp ? '1' : '0';
1284 ubuf[1] = '\0';
1285 break;
1286 case 'n':
1287 if (arg_complete)
1288 (void)snprintf(ubuf, sizeof(ubuf), "%d",
1289 roff_getregn(r, stnam, naml));
1290 else
1291 ubuf[0] = '\0';
1292 break;
1293 case 'w':
1294 /* use even incomplete args */
1295 (void)snprintf(ubuf, sizeof(ubuf), "%d",
1296 24 * (int)naml);
1297 break;
1298 }
1299
1300 if (res == NULL) {
1301 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1302 r->parse, ln, (int)(stesc - buf->buf),
1303 "%.*s", (int)naml, stnam);
1304 res = "";
1305 } else if (buf->sz + strlen(res) > SHRT_MAX) {
1306 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1307 ln, (int)(stesc - buf->buf), NULL);
1308 return ROFF_IGN;
1309 }
1310
1311 /* Replace the escape sequence by the string. */
1312
1313 *stesc = '\0';
1314 buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
1315 buf->buf, res, cp) + 1;
1316
1317 /* Prepare for the next replacement. */
1318
1319 start = nbuf + pos;
1320 stesc = nbuf + (stesc - buf->buf) + strlen(res);
1321 free(buf->buf);
1322 buf->buf = nbuf;
1323 }
1324 return ROFF_CONT;
1325 }
1326
1327 /*
1328 * Process text streams.
1329 */
1330 static enum rofferr
1331 roff_parsetext(struct buf *buf, int pos, int *offs)
1332 {
1333 size_t sz;
1334 const char *start;
1335 char *p;
1336 int isz;
1337 enum mandoc_esc esc;
1338
1339 /* Spring the input line trap. */
1340
1341 if (roffit_lines == 1) {
1342 isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1343 free(buf->buf);
1344 buf->buf = p;
1345 buf->sz = isz + 1;
1346 *offs = 0;
1347 free(roffit_macro);
1348 roffit_lines = 0;
1349 return ROFF_REPARSE;
1350 } else if (roffit_lines > 1)
1351 --roffit_lines;
1352
1353 /* Convert all breakable hyphens into ASCII_HYPH. */
1354
1355 start = p = buf->buf + pos;
1356
1357 while (*p != '\0') {
1358 sz = strcspn(p, "-\\");
1359 p += sz;
1360
1361 if (*p == '\0')
1362 break;
1363
1364 if (*p == '\\') {
1365 /* Skip over escapes. */
1366 p++;
1367 esc = mandoc_escape((const char **)&p, NULL, NULL);
1368 if (esc == ESCAPE_ERROR)
1369 break;
1370 while (*p == '-')
1371 p++;
1372 continue;
1373 } else if (p == start) {
1374 p++;
1375 continue;
1376 }
1377
1378 if (isalpha((unsigned char)p[-1]) &&
1379 isalpha((unsigned char)p[1]))
1380 *p = ASCII_HYPH;
1381 p++;
1382 }
1383 return ROFF_CONT;
1384 }
1385
1386 enum rofferr
1387 roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1388 {
1389 enum roff_tok t;
1390 enum rofferr e;
1391 int pos; /* parse point */
1392 int spos; /* saved parse point for messages */
1393 int ppos; /* original offset in buf->buf */
1394 int ctl; /* macro line (boolean) */
1395
1396 ppos = pos = *offs;
1397
1398 /* Handle in-line equation delimiters. */
1399
1400 if (r->tbl == NULL &&
1401 r->last_eqn != NULL && r->last_eqn->delim &&
1402 (r->eqn == NULL || r->eqn_inline)) {
1403 e = roff_eqndelim(r, buf, pos);
1404 if (e == ROFF_REPARSE)
1405 return e;
1406 assert(e == ROFF_CONT);
1407 }
1408
1409 /* Expand some escape sequences. */
1410
1411 e = roff_res(r, buf, ln, pos);
1412 if (e == ROFF_IGN)
1413 return e;
1414 assert(e == ROFF_CONT);
1415
1416 ctl = roff_getcontrol(r, buf->buf, &pos);
1417
1418 /*
1419 * First, if a scope is open and we're not a macro, pass the
1420 * text through the macro's filter.
1421 * Equations process all content themselves.
1422 * Tables process almost all content themselves, but we want
1423 * to warn about macros before passing it there.
1424 */
1425
1426 if (r->last != NULL && ! ctl) {
1427 t = r->last->tok;
1428 e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1429 if (e == ROFF_IGN)
1430 return e;
1431 assert(e == ROFF_CONT);
1432 }
1433 if (r->eqn != NULL)
1434 return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);
1435 if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1436 return tbl_read(r->tbl, ln, buf->buf, ppos);
1437 if ( ! ctl)
1438 return roff_parsetext(buf, pos, offs);
1439
1440 /* Skip empty request lines. */
1441
1442 if (buf->buf[pos] == '"') {
1443 mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
1444 ln, pos, NULL);
1445 return ROFF_IGN;
1446 } else if (buf->buf[pos] == '\0')
1447 return ROFF_IGN;
1448
1449 /*
1450 * If a scope is open, go to the child handler for that macro,
1451 * as it may want to preprocess before doing anything with it.
1452 * Don't do so if an equation is open.
1453 */
1454
1455 if (r->last) {
1456 t = r->last->tok;
1457 return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1458 }
1459
1460 /* No scope is open. This is a new request or macro. */
1461
1462 spos = pos;
1463 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1464
1465 /* Tables ignore most macros. */
1466
1467 if (r->tbl != NULL && (t == TOKEN_NONE || t == ROFF_TS)) {
1468 mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
1469 ln, pos, buf->buf + spos);
1470 if (t == ROFF_TS)
1471 return ROFF_IGN;
1472 while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1473 pos++;
1474 while (buf->buf[pos] == ' ')
1475 pos++;
1476 return tbl_read(r->tbl, ln, buf->buf, pos);
1477 }
1478
1479 /*
1480 * This is neither a roff request nor a user-defined macro.
1481 * Let the standard macro set parsers handle it.
1482 */
1483
1484 if (t == TOKEN_NONE)
1485 return ROFF_CONT;
1486
1487 /* Execute a roff request or a user defined macro. */
1488
1489 return (*roffs[t].proc)(r, t, buf, ln, spos, pos, offs);
1490 }
1491
1492 void
1493 roff_endparse(struct roff *r)
1494 {
1495
1496 if (r->last)
1497 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1498 r->last->line, r->last->col,
1499 roff_name[r->last->tok]);
1500
1501 if (r->eqn) {
1502 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1503 r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1504 eqn_end(&r->eqn);
1505 }
1506
1507 if (r->tbl) {
1508 mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1509 r->tbl->line, r->tbl->pos, "TS");
1510 tbl_end(&r->tbl);
1511 }
1512 }
1513
1514 /*
1515 * Parse a roff node's type from the input buffer. This must be in the
1516 * form of ".foo xxx" in the usual way.
1517 */
1518 static enum roff_tok
1519 roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1520 {
1521 char *cp;
1522 const char *mac;
1523 size_t maclen;
1524 enum roff_tok t;
1525
1526 cp = buf + *pos;
1527
1528 if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1529 return TOKEN_NONE;
1530
1531 mac = cp;
1532 maclen = roff_getname(r, &cp, ln, ppos);
1533
1534 t = (r->current_string = roff_getstrn(r, mac, maclen))
1535 ? ROFF_USERDEF : roffhash_find(r->reqtab, mac, maclen);
1536
1537 if (t != TOKEN_NONE)
1538 *pos = cp - buf;
1539
1540 return t;
1541 }
1542
1543 /* --- handling of request blocks ----------------------------------------- */
1544
1545 static enum rofferr
1546 roff_cblock(ROFF_ARGS)
1547 {
1548
1549 /*
1550 * A block-close `..' should only be invoked as a child of an
1551 * ignore macro, otherwise raise a warning and just ignore it.
1552 */
1553
1554 if (r->last == NULL) {
1555 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1556 ln, ppos, "..");
1557 return ROFF_IGN;
1558 }
1559
1560 switch (r->last->tok) {
1561 case ROFF_am:
1562 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1563 case ROFF_ami:
1564 case ROFF_de:
1565 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1566 case ROFF_dei:
1567 case ROFF_ig:
1568 break;
1569 default:
1570 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1571 ln, ppos, "..");
1572 return ROFF_IGN;
1573 }
1574
1575 if (buf->buf[pos] != '\0')
1576 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1577 ".. %s", buf->buf + pos);
1578
1579 roffnode_pop(r);
1580 roffnode_cleanscope(r);
1581 return ROFF_IGN;
1582
1583 }
1584
1585 static void
1586 roffnode_cleanscope(struct roff *r)
1587 {
1588
1589 while (r->last) {
1590 if (--r->last->endspan != 0)
1591 break;
1592 roffnode_pop(r);
1593 }
1594 }
1595
1596 static void
1597 roff_ccond(struct roff *r, int ln, int ppos)
1598 {
1599
1600 if (NULL == r->last) {
1601 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1602 ln, ppos, "\\}");
1603 return;
1604 }
1605
1606 switch (r->last->tok) {
1607 case ROFF_el:
1608 case ROFF_ie:
1609 case ROFF_if:
1610 break;
1611 default:
1612 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1613 ln, ppos, "\\}");
1614 return;
1615 }
1616
1617 if (r->last->endspan > -1) {
1618 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1619 ln, ppos, "\\}");
1620 return;
1621 }
1622
1623 roffnode_pop(r);
1624 roffnode_cleanscope(r);
1625 return;
1626 }
1627
1628 static enum rofferr
1629 roff_block(ROFF_ARGS)
1630 {
1631 const char *name;
1632 char *iname, *cp;
1633 size_t namesz;
1634
1635 /* Ignore groff compatibility mode for now. */
1636
1637 if (tok == ROFF_de1)
1638 tok = ROFF_de;
1639 else if (tok == ROFF_dei1)
1640 tok = ROFF_dei;
1641 else if (tok == ROFF_am1)
1642 tok = ROFF_am;
1643 else if (tok == ROFF_ami1)
1644 tok = ROFF_ami;
1645
1646 /* Parse the macro name argument. */
1647
1648 cp = buf->buf + pos;
1649 if (tok == ROFF_ig) {
1650 iname = NULL;
1651 namesz = 0;
1652 } else {
1653 iname = cp;
1654 namesz = roff_getname(r, &cp, ln, ppos);
1655 iname[namesz] = '\0';
1656 }
1657
1658 /* Resolve the macro name argument if it is indirect. */
1659
1660 if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1661 if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1662 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1663 r->parse, ln, (int)(iname - buf->buf),
1664 "%.*s", (int)namesz, iname);
1665 namesz = 0;
1666 } else
1667 namesz = strlen(name);
1668 } else
1669 name = iname;
1670
1671 if (namesz == 0 && tok != ROFF_ig) {
1672 mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1673 ln, ppos, roff_name[tok]);
1674 return ROFF_IGN;
1675 }
1676
1677 roffnode_push(r, tok, name, ln, ppos);
1678
1679 /*
1680 * At the beginning of a `de' macro, clear the existing string
1681 * with the same name, if there is one. New content will be
1682 * appended from roff_block_text() in multiline mode.
1683 */
1684
1685 if (tok == ROFF_de || tok == ROFF_dei)
1686 roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1687
1688 if (*cp == '\0')
1689 return ROFF_IGN;
1690
1691 /* Get the custom end marker. */
1692
1693 iname = cp;
1694 namesz = roff_getname(r, &cp, ln, ppos);
1695
1696 /* Resolve the end marker if it is indirect. */
1697
1698 if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1699 if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1700 mandoc_vmsg(MANDOCERR_STR_UNDEF,
1701 r->parse, ln, (int)(iname - buf->buf),
1702 "%.*s", (int)namesz, iname);
1703 namesz = 0;
1704 } else
1705 namesz = strlen(name);
1706 } else
1707 name = iname;
1708
1709 if (namesz)
1710 r->last->end = mandoc_strndup(name, namesz);
1711
1712 if (*cp != '\0')
1713 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1714 ln, pos, ".%s ... %s", roff_name[tok], cp);
1715
1716 return ROFF_IGN;
1717 }
1718
1719 static enum rofferr
1720 roff_block_sub(ROFF_ARGS)
1721 {
1722 enum roff_tok t;
1723 int i, j;
1724
1725 /*
1726 * First check whether a custom macro exists at this level. If
1727 * it does, then check against it. This is some of groff's
1728 * stranger behaviours. If we encountered a custom end-scope
1729 * tag and that tag also happens to be a "real" macro, then we
1730 * need to try interpreting it again as a real macro. If it's
1731 * not, then return ignore. Else continue.
1732 */
1733
1734 if (r->last->end) {
1735 for (i = pos, j = 0; r->last->end[j]; j++, i++)
1736 if (buf->buf[i] != r->last->end[j])
1737 break;
1738
1739 if (r->last->end[j] == '\0' &&
1740 (buf->buf[i] == '\0' ||
1741 buf->buf[i] == ' ' ||
1742 buf->buf[i] == '\t')) {
1743 roffnode_pop(r);
1744 roffnode_cleanscope(r);
1745
1746 while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1747 i++;
1748
1749 pos = i;
1750 if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1751 TOKEN_NONE)
1752 return ROFF_RERUN;
1753 return ROFF_IGN;
1754 }
1755 }
1756
1757 /*
1758 * If we have no custom end-query or lookup failed, then try
1759 * pulling it out of the hashtable.
1760 */
1761
1762 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1763
1764 if (t != ROFF_cblock) {
1765 if (tok != ROFF_ig)
1766 roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1767 return ROFF_IGN;
1768 }
1769
1770 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1771 }
1772
1773 static enum rofferr
1774 roff_block_text(ROFF_ARGS)
1775 {
1776
1777 if (tok != ROFF_ig)
1778 roff_setstr(r, r->last->name, buf->buf + pos, 2);
1779
1780 return ROFF_IGN;
1781 }
1782
1783 static enum rofferr
1784 roff_cond_sub(ROFF_ARGS)
1785 {
1786 enum roff_tok t;
1787 char *ep;
1788 int rr;
1789
1790 rr = r->last->rule;
1791 roffnode_cleanscope(r);
1792 t = roff_parse(r, buf->buf, &pos, ln, ppos);
1793
1794 /*
1795 * Fully handle known macros when they are structurally
1796 * required or when the conditional evaluated to true.
1797 */
1798
1799 if (t != TOKEN_NONE && (rr || roffs[t].flags & ROFFMAC_STRUCT))
1800 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1801
1802 /*
1803 * If `\}' occurs on a macro line without a preceding macro,
1804 * drop the line completely.
1805 */
1806
1807 ep = buf->buf + pos;
1808 if (ep[0] == '\\' && ep[1] == '}')
1809 rr = 0;
1810
1811 /* Always check for the closing delimiter `\}'. */
1812
1813 while ((ep = strchr(ep, '\\')) != NULL) {
1814 if (*(++ep) == '}') {
1815 *ep = '&';
1816 roff_ccond(r, ln, ep - buf->buf - 1);
1817 }
1818 if (*ep != '\0')
1819 ++ep;
1820 }
1821 return rr ? ROFF_CONT : ROFF_IGN;
1822 }
1823
1824 static enum rofferr
1825 roff_cond_text(ROFF_ARGS)
1826 {
1827 char *ep;
1828 int rr;
1829
1830 rr = r->last->rule;
1831 roffnode_cleanscope(r);
1832
1833 ep = buf->buf + pos;
1834 while ((ep = strchr(ep, '\\')) != NULL) {
1835 if (*(++ep) == '}') {
1836 *ep = '&';
1837 roff_ccond(r, ln, ep - buf->buf - 1);
1838 }
1839 if (*ep != '\0')
1840 ++ep;
1841 }
1842 return rr ? ROFF_CONT : ROFF_IGN;
1843 }
1844
1845 /* --- handling of numeric and conditional expressions -------------------- */
1846
1847 /*
1848 * Parse a single signed integer number. Stop at the first non-digit.
1849 * If there is at least one digit, return success and advance the
1850 * parse point, else return failure and let the parse point unchanged.
1851 * Ignore overflows, treat them just like the C language.
1852 */
1853 static int
1854 roff_getnum(const char *v, int *pos, int *res, int flags)
1855 {
1856 int myres, scaled, n, p;
1857
1858 if (NULL == res)
1859 res = &myres;
1860
1861 p = *pos;
1862 n = v[p] == '-';
1863 if (n || v[p] == '+')
1864 p++;
1865
1866 if (flags & ROFFNUM_WHITE)
1867 while (isspace((unsigned char)v[p]))
1868 p++;
1869
1870 for (*res = 0; isdigit((unsigned char)v[p]); p++)
1871 *res = 10 * *res + v[p] - '0';
1872 if (p == *pos + n)
1873 return 0;
1874
1875 if (n)
1876 *res = -*res;
1877
1878 /* Each number may be followed by one optional scaling unit. */
1879
1880 switch (v[p]) {
1881 case 'f':
1882 scaled = *res * 65536;
1883 break;
1884 case 'i':
1885 scaled = *res * 240;
1886 break;
1887 case 'c':
1888 scaled = *res * 240 / 2.54;
1889 break;
1890 case 'v':
1891 case 'P':
1892 scaled = *res * 40;
1893 break;
1894 case 'm':
1895 case 'n':
1896 scaled = *res * 24;
1897 break;
1898 case 'p':
1899 scaled = *res * 10 / 3;
1900 break;
1901 case 'u':
1902 scaled = *res;
1903 break;
1904 case 'M':
1905 scaled = *res * 6 / 25;
1906 break;
1907 default:
1908 scaled = *res;
1909 p--;
1910 break;
1911 }
1912 if (flags & ROFFNUM_SCALE)
1913 *res = scaled;
1914
1915 *pos = p + 1;
1916 return 1;
1917 }
1918
1919 /*
1920 * Evaluate a string comparison condition.
1921 * The first character is the delimiter.
1922 * Succeed if the string up to its second occurrence
1923 * matches the string up to its third occurence.
1924 * Advance the cursor after the third occurrence
1925 * or lacking that, to the end of the line.
1926 */
1927 static int
1928 roff_evalstrcond(const char *v, int *pos)
1929 {
1930 const char *s1, *s2, *s3;
1931 int match;
1932
1933 match = 0;
1934 s1 = v + *pos; /* initial delimiter */
1935 s2 = s1 + 1; /* for scanning the first string */
1936 s3 = strchr(s2, *s1); /* for scanning the second string */
1937
1938 if (NULL == s3) /* found no middle delimiter */
1939 goto out;
1940
1941 while ('\0' != *++s3) {
1942 if (*s2 != *s3) { /* mismatch */
1943 s3 = strchr(s3, *s1);
1944 break;
1945 }
1946 if (*s3 == *s1) { /* found the final delimiter */
1947 match = 1;
1948 break;
1949 }
1950 s2++;
1951 }
1952
1953 out:
1954 if (NULL == s3)
1955 s3 = strchr(s2, '\0');
1956 else if (*s3 != '\0')
1957 s3++;
1958 *pos = s3 - v;
1959 return match;
1960 }
1961
1962 /*
1963 * Evaluate an optionally negated single character, numerical,
1964 * or string condition.
1965 */
1966 static int
1967 roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1968 {
1969 char *cp, *name;
1970 size_t sz;
1971 int number, savepos, wanttrue;
1972
1973 if ('!' == v[*pos]) {
1974 wanttrue = 0;
1975 (*pos)++;
1976 } else
1977 wanttrue = 1;
1978
1979 switch (v[*pos]) {
1980 case '\0':
1981 return 0;
1982 case 'n':
1983 case 'o':
1984 (*pos)++;
1985 return wanttrue;
1986 case 'c':
1987 case 'd':
1988 case 'e':
1989 case 't':
1990 case 'v':
1991 (*pos)++;
1992 return !wanttrue;
1993 case 'r':
1994 cp = name = v + ++*pos;
1995 sz = roff_getname(r, &cp, ln, *pos);
1996 *pos = cp - v;
1997 return (sz && roff_hasregn(r, name, sz)) == wanttrue;
1998 default:
1999 break;
2000 }
2001
2002 savepos = *pos;
2003 if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
2004 return (number > 0) == wanttrue;
2005 else if (*pos == savepos)
2006 return roff_evalstrcond(v, pos) == wanttrue;
2007 else
2008 return 0;
2009 }
2010
2011 static enum rofferr
2012 roff_line_ignore(ROFF_ARGS)
2013 {
2014
2015 return ROFF_IGN;
2016 }
2017
2018 static enum rofferr
2019 roff_insec(ROFF_ARGS)
2020 {
2021
2022 mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
2023 ln, ppos, roff_name[tok]);
2024 return ROFF_IGN;
2025 }
2026
2027 static enum rofferr
2028 roff_unsupp(ROFF_ARGS)
2029 {
2030
2031 mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
2032 ln, ppos, roff_name[tok]);
2033 return ROFF_IGN;
2034 }
2035
2036 static enum rofferr
2037 roff_cond(ROFF_ARGS)
2038 {
2039
2040 roffnode_push(r, tok, NULL, ln, ppos);
2041
2042 /*
2043 * An `.el' has no conditional body: it will consume the value
2044 * of the current rstack entry set in prior `ie' calls or
2045 * defaults to DENY.
2046 *
2047 * If we're not an `el', however, then evaluate the conditional.
2048 */
2049
2050 r->last->rule = tok == ROFF_el ?
2051 (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
2052 roff_evalcond(r, ln, buf->buf, &pos);
2053
2054 /*
2055 * An if-else will put the NEGATION of the current evaluated
2056 * conditional into the stack of rules.
2057 */
2058
2059 if (tok == ROFF_ie) {
2060 if (r->rstackpos + 1 == r->rstacksz) {
2061 r->rstacksz += 16;
2062 r->rstack = mandoc_reallocarray(r->rstack,
2063 r->rstacksz, sizeof(int));
2064 }
2065 r->rstack[++r->rstackpos] = !r->last->rule;
2066 }
2067
2068 /* If the parent has false as its rule, then so do we. */
2069
2070 if (r->last->parent && !r->last->parent->rule)
2071 r->last->rule = 0;
2072
2073 /*
2074 * Determine scope.
2075 * If there is nothing on the line after the conditional,
2076 * not even whitespace, use next-line scope.
2077 */
2078
2079 if (buf->buf[pos] == '\0') {
2080 r->last->endspan = 2;
2081 goto out;
2082 }
2083
2084 while (buf->buf[pos] == ' ')
2085 pos++;
2086
2087 /* An opening brace requests multiline scope. */
2088
2089 if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
2090 r->last->endspan = -1;
2091 pos += 2;
2092 while (buf->buf[pos] == ' ')
2093 pos++;
2094 goto out;
2095 }
2096
2097 /*
2098 * Anything else following the conditional causes
2099 * single-line scope. Warn if the scope contains
2100 * nothing but trailing whitespace.
2101 */
2102
2103 if (buf->buf[pos] == '\0')
2104 mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
2105 ln, ppos, roff_name[tok]);
2106
2107 r->last->endspan = 1;
2108
2109 out:
2110 *offs = pos;
2111 return ROFF_RERUN;
2112 }
2113
2114 static enum rofferr
2115 roff_ds(ROFF_ARGS)
2116 {
2117 char *string;
2118 const char *name;
2119 size_t namesz;
2120
2121 /* Ignore groff compatibility mode for now. */
2122
2123 if (tok == ROFF_ds1)
2124 tok = ROFF_ds;
2125 else if (tok == ROFF_as1)
2126 tok = ROFF_as;
2127
2128 /*
2129 * The first word is the name of the string.
2130 * If it is empty or terminated by an escape sequence,
2131 * abort the `ds' request without defining anything.
2132 */
2133
2134 name = string = buf->buf + pos;
2135 if (*name == '\0')
2136 return ROFF_IGN;
2137
2138 namesz = roff_getname(r, &string, ln, pos);
2139 if (name[namesz] == '\\')
2140 return ROFF_IGN;
2141
2142 /* Read past the initial double-quote, if any. */
2143 if (*string == '"')
2144 string++;
2145
2146 /* The rest is the value. */
2147 roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2148 ROFF_as == tok);
2149 return ROFF_IGN;
2150 }
2151
2152 /*
2153 * Parse a single operator, one or two characters long.
2154 * If the operator is recognized, return success and advance the
2155 * parse point, else return failure and let the parse point unchanged.
2156 */
2157 static int
2158 roff_getop(const char *v, int *pos, char *res)
2159 {
2160
2161 *res = v[*pos];
2162
2163 switch (*res) {
2164 case '+':
2165 case '-':
2166 case '*':
2167 case '/':
2168 case '%':
2169 case '&':
2170 case ':':
2171 break;
2172 case '<':
2173 switch (v[*pos + 1]) {
2174 case '=':
2175 *res = 'l';
2176 (*pos)++;
2177 break;
2178 case '>':
2179 *res = '!';
2180 (*pos)++;
2181 break;
2182 case '?':
2183 *res = 'i';
2184 (*pos)++;
2185 break;
2186 default:
2187 break;
2188 }
2189 break;
2190 case '>':
2191 switch (v[*pos + 1]) {
2192 case '=':
2193 *res = 'g';
2194 (*pos)++;
2195 break;
2196 case '?':
2197 *res = 'a';
2198 (*pos)++;
2199 break;
2200 default:
2201 break;
2202 }
2203 break;
2204 case '=':
2205 if ('=' == v[*pos + 1])
2206 (*pos)++;
2207 break;
2208 default:
2209 return 0;
2210 }
2211 (*pos)++;
2212
2213 return *res;
2214 }
2215
2216 /*
2217 * Evaluate either a parenthesized numeric expression
2218 * or a single signed integer number.
2219 */
2220 static int
2221 roff_evalpar(struct roff *r, int ln,
2222 const char *v, int *pos, int *res, int flags)
2223 {
2224
2225 if ('(' != v[*pos])
2226 return roff_getnum(v, pos, res, flags);
2227
2228 (*pos)++;
2229 if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
2230 return 0;
2231
2232 /*
2233 * Omission of the closing parenthesis
2234 * is an error in validation mode,
2235 * but ignored in evaluation mode.
2236 */
2237
2238 if (')' == v[*pos])
2239 (*pos)++;
2240 else if (NULL == res)
2241 return 0;
2242
2243 return 1;
2244 }
2245
2246 /*
2247 * Evaluate a complete numeric expression.
2248 * Proceed left to right, there is no concept of precedence.
2249 */
2250 static int
2251 roff_evalnum(struct roff *r, int ln, const char *v,
2252 int *pos, int *res, int flags)
2253 {
2254 int mypos, operand2;
2255 char operator;
2256
2257 if (NULL == pos) {
2258 mypos = 0;
2259 pos = &mypos;
2260 }
2261
2262 if (flags & ROFFNUM_WHITE)
2263 while (isspace((unsigned char)v[*pos]))
2264 (*pos)++;
2265
2266 if ( ! roff_evalpar(r, ln, v, pos, res, flags))
2267 return 0;
2268
2269 while (1) {
2270 if (flags & ROFFNUM_WHITE)
2271 while (isspace((unsigned char)v[*pos]))
2272 (*pos)++;
2273
2274 if ( ! roff_getop(v, pos, &operator))
2275 break;
2276
2277 if (flags & ROFFNUM_WHITE)
2278 while (isspace((unsigned char)v[*pos]))
2279 (*pos)++;
2280
2281 if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
2282 return 0;
2283
2284 if (flags & ROFFNUM_WHITE)
2285 while (isspace((unsigned char)v[*pos]))
2286 (*pos)++;
2287
2288 if (NULL == res)
2289 continue;
2290
2291 switch (operator) {
2292 case '+':
2293 *res += operand2;
2294 break;
2295 case '-':
2296 *res -= operand2;
2297 break;
2298 case '*':
2299 *res *= operand2;
2300 break;
2301 case '/':
2302 if (operand2 == 0) {
2303 mandoc_msg(MANDOCERR_DIVZERO,
2304 r->parse, ln, *pos, v);
2305 *res = 0;
2306 break;
2307 }
2308 *res /= operand2;
2309 break;
2310 case '%':
2311 if (operand2 == 0) {
2312 mandoc_msg(MANDOCERR_DIVZERO,
2313 r->parse, ln, *pos, v);
2314 *res = 0;
2315 break;
2316 }
2317 *res %= operand2;
2318 break;
2319 case '<':
2320 *res = *res < operand2;
2321 break;
2322 case '>':
2323 *res = *res > operand2;
2324 break;
2325 case 'l':
2326 *res = *res <= operand2;
2327 break;
2328 case 'g':
2329 *res = *res >= operand2;
2330 break;
2331 case '=':
2332 *res = *res == operand2;
2333 break;
2334 case '!':
2335 *res = *res != operand2;
2336 break;
2337 case '&':
2338 *res = *res && operand2;
2339 break;
2340 case ':':
2341 *res = *res || operand2;
2342 break;
2343 case 'i':
2344 if (operand2 < *res)
2345 *res = operand2;
2346 break;
2347 case 'a':
2348 if (operand2 > *res)
2349 *res = operand2;
2350 break;
2351 default:
2352 abort();
2353 }
2354 }
2355 return 1;
2356 }
2357
2358 /* --- register management ------------------------------------------------ */
2359
2360 void
2361 roff_setreg(struct roff *r, const char *name, int val, char sign)
2362 {
2363 struct roffreg *reg;
2364
2365 /* Search for an existing register with the same name. */
2366 reg = r->regtab;
2367
2368 while (reg && strcmp(name, reg->key.p))
2369 reg = reg->next;
2370
2371 if (NULL == reg) {
2372 /* Create a new register. */
2373 reg = mandoc_malloc(sizeof(struct roffreg));
2374 reg->key.p = mandoc_strdup(name);
2375 reg->key.sz = strlen(name);
2376 reg->val = 0;
2377 reg->next = r->regtab;
2378 r->regtab = reg;
2379 }
2380
2381 if ('+' == sign)
2382 reg->val += val;
2383 else if ('-' == sign)
2384 reg->val -= val;
2385 else
2386 reg->val = val;
2387 }
2388
2389 /*
2390 * Handle some predefined read-only number registers.
2391 * For now, return -1 if the requested register is not predefined;
2392 * in case a predefined read-only register having the value -1
2393 * were to turn up, another special value would have to be chosen.
2394 */
2395 static int
2396 roff_getregro(const struct roff *r, const char *name)
2397 {
2398
2399 switch (*name) {
2400 case '$': /* Number of arguments of the last macro evaluated. */
2401 return r->argc;
2402 case 'A': /* ASCII approximation mode is always off. */
2403 return 0;
2404 case 'g': /* Groff compatibility mode is always on. */
2405 return 1;
2406 case 'H': /* Fixed horizontal resolution. */
2407 return 24;
2408 case 'j': /* Always adjust left margin only. */
2409 return 0;
2410 case 'T': /* Some output device is always defined. */
2411 return 1;
2412 case 'V': /* Fixed vertical resolution. */
2413 return 40;
2414 default:
2415 return -1;
2416 }
2417 }
2418
2419 int
2420 roff_getreg(const struct roff *r, const char *name)
2421 {
2422 struct roffreg *reg;
2423 int val;
2424
2425 if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
2426 val = roff_getregro(r, name + 1);
2427 if (-1 != val)
2428 return val;
2429 }
2430
2431 for (reg = r->regtab; reg; reg = reg->next)
2432 if (0 == strcmp(name, reg->key.p))
2433 return reg->val;
2434
2435 return 0;
2436 }
2437
2438 static int
2439 roff_getregn(const struct roff *r, const char *name, size_t len)
2440 {
2441 struct roffreg *reg;
2442 int val;
2443
2444 if ('.' == name[0] && 2 == len) {
2445 val = roff_getregro(r, name + 1);
2446 if (-1 != val)
2447 return val;
2448 }
2449
2450 for (reg = r->regtab; reg; reg = reg->next)
2451 if (len == reg->key.sz &&
2452 0 == strncmp(name, reg->key.p, len))
2453 return reg->val;
2454
2455 return 0;
2456 }
2457
2458 static int
2459 roff_hasregn(const struct roff *r, const char *name, size_t len)
2460 {
2461 struct roffreg *reg;
2462 int val;
2463
2464 if ('.' == name[0] && 2 == len) {
2465 val = roff_getregro(r, name + 1);
2466 if (-1 != val)
2467 return 1;
2468 }
2469
2470 for (reg = r->regtab; reg; reg = reg->next)
2471 if (len == reg->key.sz &&
2472 0 == strncmp(name, reg->key.p, len))
2473 return 1;
2474
2475 return 0;
2476 }
2477
2478 static void
2479 roff_freereg(struct roffreg *reg)
2480 {
2481 struct roffreg *old_reg;
2482
2483 while (NULL != reg) {
2484 free(reg->key.p);
2485 old_reg = reg;
2486 reg = reg->next;
2487 free(old_reg);
2488 }
2489 }
2490
2491 static enum rofferr
2492 roff_nr(ROFF_ARGS)
2493 {
2494 char *key, *val;
2495 size_t keysz;
2496 int iv;
2497 char sign;
2498
2499 key = val = buf->buf + pos;
2500 if (*key == '\0')
2501 return ROFF_IGN;
2502
2503 keysz = roff_getname(r, &val, ln, pos);
2504 if (key[keysz] == '\\')
2505 return ROFF_IGN;
2506 key[keysz] = '\0';
2507
2508 sign = *val;
2509 if (sign == '+' || sign == '-')
2510 val++;
2511
2512 if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
2513 roff_setreg(r, key, iv, sign);
2514
2515 return ROFF_IGN;
2516 }
2517
2518 static enum rofferr
2519 roff_rr(ROFF_ARGS)
2520 {
2521 struct roffreg *reg, **prev;
2522 char *name, *cp;
2523 size_t namesz;
2524
2525 name = cp = buf->buf + pos;
2526 if (*name == '\0')
2527 return ROFF_IGN;
2528 namesz = roff_getname(r, &cp, ln, pos);
2529 name[namesz] = '\0';
2530
2531 prev = &r->regtab;
2532 while (1) {
2533 reg = *prev;
2534 if (reg == NULL || !strcmp(name, reg->key.p))
2535 break;
2536 prev = &reg->next;
2537 }
2538 if (reg != NULL) {
2539 *prev = reg->next;
2540 free(reg->key.p);
2541 free(reg);
2542 }
2543 return ROFF_IGN;
2544 }
2545
2546 /* --- handler functions for roff requests -------------------------------- */
2547
2548 static enum rofferr
2549 roff_rm(ROFF_ARGS)
2550 {
2551 const char *name;
2552 char *cp;
2553 size_t namesz;
2554
2555 cp = buf->buf + pos;
2556 while (*cp != '\0') {
2557 name = cp;
2558 namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
2559 roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
2560 if (name[namesz] == '\\')
2561 break;
2562 }
2563 return ROFF_IGN;
2564 }
2565
2566 static enum rofferr
2567 roff_it(ROFF_ARGS)
2568 {
2569 int iv;
2570
2571 /* Parse the number of lines. */
2572
2573 if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
2574 mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
2575 ln, ppos, buf->buf + 1);
2576 return ROFF_IGN;
2577 }
2578
2579 while (isspace((unsigned char)buf->buf[pos]))
2580 pos++;
2581
2582 /*
2583 * Arm the input line trap.
2584 * Special-casing "an-trap" is an ugly workaround to cope
2585 * with DocBook stupidly fiddling with man(7) internals.
2586 */
2587
2588 roffit_lines = iv;
2589 roffit_macro = mandoc_strdup(iv != 1 ||
2590 strcmp(buf->buf + pos, "an-trap") ?
2591 buf->buf + pos : "br");
2592 return ROFF_IGN;
2593 }
2594
2595 static enum rofferr
2596 roff_Dd(ROFF_ARGS)
2597 {
2598 const char *const *cp;
2599
2600 if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
2601 for (cp = __mdoc_reserved; *cp; cp++)
2602 roff_setstr(r, *cp, NULL, 0);
2603
2604 if (r->format == 0)
2605 r->format = MPARSE_MDOC;
2606
2607 return ROFF_CONT;
2608 }
2609
2610 static enum rofferr
2611 roff_TH(ROFF_ARGS)
2612 {
2613 const char *const *cp;
2614
2615 if ((r->options & MPARSE_QUICK) == 0)
2616 for (cp = __man_reserved; *cp; cp++)
2617 roff_setstr(r, *cp, NULL, 0);
2618
2619 if (r->format == 0)
2620 r->format = MPARSE_MAN;
2621
2622 return ROFF_CONT;
2623 }
2624
2625 static enum rofferr
2626 roff_TE(ROFF_ARGS)
2627 {
2628
2629 if (NULL == r->tbl)
2630 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2631 ln, ppos, "TE");
2632 else if ( ! tbl_end(&r->tbl)) {
2633 free(buf->buf);
2634 buf->buf = mandoc_strdup(".sp");
2635 buf->sz = 4;
2636 return ROFF_REPARSE;
2637 }
2638 return ROFF_IGN;
2639 }
2640
2641 static enum rofferr
2642 roff_T_(ROFF_ARGS)
2643 {
2644
2645 if (NULL == r->tbl)
2646 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2647 ln, ppos, "T&");
2648 else
2649 tbl_restart(ln, ppos, r->tbl);
2650
2651 return ROFF_IGN;
2652 }
2653
2654 /*
2655 * Handle in-line equation delimiters.
2656 */
2657 static enum rofferr
2658 roff_eqndelim(struct roff *r, struct buf *buf, int pos)
2659 {
2660 char *cp1, *cp2;
2661 const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
2662
2663 /*
2664 * Outside equations, look for an opening delimiter.
2665 * If we are inside an equation, we already know it is
2666 * in-line, or this function wouldn't have been called;
2667 * so look for a closing delimiter.
2668 */
2669
2670 cp1 = buf->buf + pos;
2671 cp2 = strchr(cp1, r->eqn == NULL ?
2672 r->last_eqn->odelim : r->last_eqn->cdelim);
2673 if (cp2 == NULL)
2674 return ROFF_CONT;
2675
2676 *cp2++ = '\0';
2677 bef_pr = bef_nl = aft_nl = aft_pr = "";
2678
2679 /* Handle preceding text, protecting whitespace. */
2680
2681 if (*buf->buf != '\0') {
2682 if (r->eqn == NULL)
2683 bef_pr = "\\&";
2684 bef_nl = "\n";
2685 }
2686
2687 /*
2688 * Prepare replacing the delimiter with an equation macro
2689 * and drop leading white space from the equation.
2690 */
2691
2692 if (r->eqn == NULL) {
2693 while (*cp2 == ' ')
2694 cp2++;
2695 mac = ".EQ";
2696 } else
2697 mac = ".EN";
2698
2699 /* Handle following text, protecting whitespace. */
2700
2701 if (*cp2 != '\0') {
2702 aft_nl = "\n";
2703 if (r->eqn != NULL)
2704 aft_pr = "\\&";
2705 }
2706
2707 /* Do the actual replacement. */
2708
2709 buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
2710 bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
2711 free(buf->buf);
2712 buf->buf = cp1;
2713
2714 /* Toggle the in-line state of the eqn subsystem. */
2715
2716 r->eqn_inline = r->eqn == NULL;
2717 return ROFF_REPARSE;
2718 }
2719
2720 static enum rofferr
2721 roff_EQ(ROFF_ARGS)
2722 {
2723 struct eqn_node *e;
2724
2725 assert(r->eqn == NULL);
2726 e = eqn_alloc(ppos, ln, r->parse);
2727
2728 if (r->last_eqn) {
2729 r->last_eqn->next = e;
2730 e->delim = r->last_eqn->delim;
2731 e->odelim = r->last_eqn->odelim;
2732 e->cdelim = r->last_eqn->cdelim;
2733 } else
2734 r->first_eqn = r->last_eqn = e;
2735
2736 r->eqn = r->last_eqn = e;
2737
2738 if (buf->buf[pos] != '\0')
2739 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
2740 ".EQ %s", buf->buf + pos);
2741
2742 return ROFF_IGN;
2743 }
2744
2745 static enum rofferr
2746 roff_EN(ROFF_ARGS)
2747 {
2748
2749 mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
2750 return ROFF_IGN;
2751 }
2752
2753 static enum rofferr
2754 roff_TS(ROFF_ARGS)
2755 {
2756 struct tbl_node *tbl;
2757
2758 if (r->tbl) {
2759 mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
2760 ln, ppos, "TS breaks TS");
2761 tbl_end(&r->tbl);
2762 }
2763
2764 tbl = tbl_alloc(ppos, ln, r->parse);
2765
2766 if (r->last_tbl)
2767 r->last_tbl->next = tbl;
2768 else
2769 r->first_tbl = r->last_tbl = tbl;
2770
2771 r->tbl = r->last_tbl = tbl;
2772 return ROFF_IGN;
2773 }
2774
2775 static enum rofferr
2776 roff_onearg(ROFF_ARGS)
2777 {
2778 struct roff_node *n;
2779 char *cp;
2780
2781 if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
2782 (tok == ROFF_sp || tok == ROFF_ti))
2783 man_breakscope(r->man, tok);
2784
2785 roff_elem_alloc(r->man, ln, ppos, tok);
2786 n = r->man->last;
2787
2788 cp = buf->buf + pos;
2789 if (*cp != '\0') {
2790 while (*cp != '\0' && *cp != ' ')
2791 cp++;
2792 while (*cp == ' ')
2793 *cp++ = '\0';
2794 if (*cp != '\0')
2795 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
2796 r->parse, ln, cp - buf->buf,
2797 "%s ... %s", roff_name[tok], cp);
2798 roff_word_alloc(r->man, ln, pos, buf->buf + pos);
2799 }
2800
2801 n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
2802 r->man->last = n;
2803 r->man->next = ROFF_NEXT_SIBLING;
2804 return ROFF_IGN;
2805 }
2806
2807 static enum rofferr
2808 roff_manyarg(ROFF_ARGS)
2809 {
2810 struct roff_node *n;
2811 char *sp, *ep;
2812
2813 roff_elem_alloc(r->man, ln, ppos, tok);
2814 n = r->man->last;
2815
2816 for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
2817 while (*ep != '\0' && *ep != ' ')
2818 ep++;
2819 while (*ep == ' ')
2820 *ep++ = '\0';
2821 roff_word_alloc(r->man, ln, sp - buf->buf, sp);
2822 }
2823
2824 n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
2825 r->man->last = n;
2826 r->man->next = ROFF_NEXT_SIBLING;
2827 return ROFF_IGN;
2828 }
2829
2830 static enum rofferr
2831 roff_br(ROFF_ARGS)
2832 {
2833 if (r->man->flags & (MAN_BLINE | MAN_ELINE))
2834 man_breakscope(r->man, ROFF_br);
2835 roff_elem_alloc(r->man, ln, ppos, ROFF_br);
2836 if (buf->buf[pos] != '\0')
2837 mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
2838 "%s %s", roff_name[tok], buf->buf + pos);
2839 r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
2840 r->man->next = ROFF_NEXT_SIBLING;
2841 return ROFF_IGN;
2842 }
2843
2844 static enum rofferr
2845 roff_cc(ROFF_ARGS)
2846 {
2847 const char *p;
2848
2849 p = buf->buf + pos;
2850
2851 if (*p == '\0' || (r->control = *p++) == '.')
2852 r->control = 0;
2853
2854 if (*p != '\0')
2855 mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
2856 ln, p - buf->buf, "cc ... %s", p);
2857
2858 return ROFF_IGN;
2859 }
2860
2861 static enum rofferr
2862 roff_tr(ROFF_ARGS)
2863 {
2864 const char *p, *first, *second;
2865 size_t fsz, ssz;
2866 enum mandoc_esc esc;
2867
2868 p = buf->buf + pos;
2869
2870 if (*p == '\0') {
2871 mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
2872 return ROFF_IGN;
2873 }
2874
2875 while (*p != '\0') {
2876 fsz = ssz = 1;
2877
2878 first = p++;
2879 if (*first == '\\') {
2880 esc = mandoc_escape(&p, NULL, NULL);
2881 if (esc == ESCAPE_ERROR) {
2882 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
2883 ln, (int)(p - buf->buf), first);
2884 return ROFF_IGN;
2885 }
2886 fsz = (size_t)(p - first);
2887 }
2888
2889 second = p++;
2890 if (*second == '\\') {
2891 esc = mandoc_escape(&p, NULL, NULL);
2892 if (esc == ESCAPE_ERROR) {
2893 mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
2894 ln, (int)(p - buf->buf), second);
2895 return ROFF_IGN;
2896 }
2897 ssz = (size_t)(p - second);
2898 } else if (*second == '\0') {
2899 mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
2900 ln, first - buf->buf, "tr %s", first);
2901 second = " ";
2902 p--;
2903 }
2904
2905 if (fsz > 1) {
2906 roff_setstrn(&r->xmbtab, first, fsz,
2907 second, ssz, 0);
2908 continue;
2909 }
2910
2911 if (r->xtab == NULL)
2912 r->xtab = mandoc_calloc(128,
2913 sizeof(struct roffstr));
2914
2915 free(r->xtab[(int)*first].p);
2916 r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
2917 r->xtab[(int)*first].sz = ssz;
2918 }
2919
2920 return ROFF_IGN;
2921 }
2922
2923 static enum rofferr
2924 roff_so(ROFF_ARGS)
2925 {
2926 char *name, *cp;
2927
2928 name = buf->buf + pos;
2929 mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
2930
2931 /*
2932 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2933 * opening anything that's not in our cwd or anything beneath
2934 * it. Thus, explicitly disallow traversing up the file-system
2935 * or using absolute paths.
2936 */
2937
2938 if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
2939 mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
2940 ".so %s", name);
2941 buf->sz = mandoc_asprintf(&cp,
2942 ".sp\nSee the file %s.\n.sp", name) + 1;
2943 free(buf->buf);
2944 buf->buf = cp;
2945 *offs = 0;
2946 return ROFF_REPARSE;
2947 }
2948
2949 *offs = pos;
2950 return ROFF_SO;
2951 }
2952
2953 /* --- user defined strings and macros ------------------------------------ */
2954
2955 static enum rofferr
2956 roff_userdef(ROFF_ARGS)
2957 {
2958 const char *arg[9], *ap;
2959 char *cp, *n1, *n2;
2960 int expand_count, i, ib, ie;
2961 size_t asz, rsz;
2962
2963 /*
2964 * Collect pointers to macro argument strings
2965 * and NUL-terminate them.
2966 */
2967
2968 r->argc = 0;
2969 cp = buf->buf + pos;
2970 for (i = 0; i < 9; i++) {
2971 if (*cp == '\0')
2972 arg[i] = "";
2973 else {
2974 arg[i] = mandoc_getarg(r->parse, &cp, ln, &pos);
2975 r->argc = i + 1;
2976 }
2977 }
2978
2979 /*
2980 * Expand macro arguments.
2981 */
2982
2983 buf->sz = strlen(r->current_string) + 1;
2984 n1 = n2 = cp = mandoc_malloc(buf->sz);
2985 memcpy(n1, r->current_string, buf->sz);
2986 expand_count = 0;
2987 while (*cp != '\0') {
2988
2989 /* Scan ahead for the next argument invocation. */
2990
2991 if (*cp++ != '\\')
2992 continue;
2993 if (*cp++ != '$')
2994 continue;
2995 if (*cp == '*') { /* \\$* inserts all arguments */
2996 ib = 0;
2997 ie = r->argc - 1;
2998 } else { /* \\$1 .. \\$9 insert one argument */
2999 ib = ie = *cp - '1';
3000 if (ib < 0 || ib > 8)
3001 continue;
3002 }
3003 cp -= 2;
3004
3005 /*
3006 * Prevent infinite recursion.
3007 */
3008
3009 if (cp >= n2)
3010 expand_count = 1;
3011 else if (++expand_count > EXPAND_LIMIT) {
3012 mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
3013 ln, (int)(cp - n1), NULL);
3014 free(buf->buf);
3015 buf->buf = n1;
3016 return ROFF_IGN;
3017 }
3018
3019 /*
3020 * Determine the size of the expanded argument,
3021 * taking escaping of quotes into account.
3022 */
3023
3024 asz = ie > ib ? ie - ib : 0; /* for blanks */
3025 for (i = ib; i <= ie; i++) {
3026 for (ap = arg[i]; *ap != '\0'; ap++) {
3027 asz++;
3028 if (*ap == '"')
3029 asz += 3;
3030 }
3031 }
3032 if (asz != 3) {
3033
3034 /*
3035 * Determine the size of the rest of the
3036 * unexpanded macro, including the NUL.
3037 */
3038
3039 rsz = buf->sz - (cp - n1) - 3;
3040
3041 /*
3042 * When shrinking, move before
3043 * releasing the storage.
3044 */
3045
3046 if (asz < 3)
3047 memmove(cp + asz, cp + 3, rsz);
3048
3049 /*
3050 * Resize the storage for the macro
3051 * and readjust the parse pointer.
3052 */
3053
3054 buf->sz += asz - 3;
3055 n2 = mandoc_realloc(n1, buf->sz);
3056 cp = n2 + (cp - n1);
3057 n1 = n2;
3058
3059 /*
3060 * When growing, make room
3061 * for the expanded argument.
3062 */
3063
3064 if (asz > 3)
3065 memmove(cp + asz, cp + 3, rsz);
3066 }
3067
3068 /* Copy the expanded argument, escaping quotes. */
3069
3070 n2 = cp;
3071 for (i = ib; i <= ie; i++) {
3072 for (ap = arg[i]; *ap != '\0'; ap++) {
3073 if (*ap == '"') {
3074 memcpy(n2, "\\(dq", 4);
3075 n2 += 4;
3076 } else
3077 *n2++ = *ap;
3078 }
3079 if (i < ie)
3080 *n2++ = ' ';
3081 }
3082 }
3083
3084 /*
3085 * Replace the macro invocation
3086 * by the expanded macro.
3087 */
3088
3089 free(buf->buf);
3090 buf->buf = n1;
3091 *offs = 0;
3092
3093 return buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
3094 ROFF_REPARSE : ROFF_APPEND;
3095 }
3096
3097 static size_t
3098 roff_getname(struct roff *r, char **cpp, int ln, int pos)
3099 {
3100 char *name, *cp;
3101 size_t namesz;
3102
3103 name = *cpp;
3104 if ('\0' == *name)
3105 return 0;
3106
3107 /* Read until end of name and terminate it with NUL. */
3108 for (cp = name; 1; cp++) {
3109 if ('\0' == *cp || ' ' == *cp) {
3110 namesz = cp - name;
3111 break;
3112 }
3113 if ('\\' != *cp)
3114 continue;
3115 namesz = cp - name;
3116 if ('{' == cp[1] || '}' == cp[1])
3117 break;
3118 cp++;
3119 if ('\\' == *cp)
3120 continue;
3121 mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
3122 "%.*s", (int)(cp - name + 1), name);
3123 mandoc_escape((const char **)&cp, NULL, NULL);
3124 break;
3125 }
3126
3127 /* Read past spaces. */
3128 while (' ' == *cp)
3129 cp++;
3130
3131 *cpp = cp;
3132 return namesz;
3133 }
3134
3135 /*
3136 * Store *string into the user-defined string called *name.
3137 * To clear an existing entry, call with (*r, *name, NULL, 0).
3138 * append == 0: replace mode
3139 * append == 1: single-line append mode
3140 * append == 2: multiline append mode, append '\n' after each call
3141 */
3142 static void
3143 roff_setstr(struct roff *r, const char *name, const char *string,
3144 int append)
3145 {
3146
3147 roff_setstrn(&r->strtab, name, strlen(name), string,
3148 string ? strlen(string) : 0, append);
3149 }
3150
3151 static void
3152 roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
3153 const char *string, size_t stringsz, int append)
3154 {
3155 struct roffkv *n;
3156 char *c;
3157 int i;
3158 size_t oldch, newch;
3159
3160 /* Search for an existing string with the same name. */
3161 n = *r;
3162
3163 while (n && (namesz != n->key.sz ||
3164 strncmp(n->key.p, name, namesz)))
3165 n = n->next;
3166
3167 if (NULL == n) {
3168 /* Create a new string table entry. */
3169 n = mandoc_malloc(sizeof(struct roffkv));
3170 n->key.p = mandoc_strndup(name, namesz);
3171 n->key.sz = namesz;
3172 n->val.p = NULL;
3173 n->val.sz = 0;
3174 n->next = *r;
3175 *r = n;
3176 } else if (0 == append) {
3177 free(n->val.p);
3178 n->val.p = NULL;
3179 n->val.sz = 0;
3180 }
3181
3182 if (NULL == string)
3183 return;
3184
3185 /*
3186 * One additional byte for the '\n' in multiline mode,
3187 * and one for the terminating '\0'.
3188 */
3189 newch = stringsz + (1 < append ? 2u : 1u);
3190
3191 if (NULL == n->val.p) {
3192 n->val.p = mandoc_malloc(newch);
3193 *n->val.p = '\0';
3194 oldch = 0;
3195 } else {
3196 oldch = n->val.sz;
3197 n->val.p = mandoc_realloc(n->val.p, oldch + newch);
3198 }
3199
3200 /* Skip existing content in the destination buffer. */
3201 c = n->val.p + (int)oldch;
3202
3203 /* Append new content to the destination buffer. */
3204 i = 0;
3205 while (i < (int)stringsz) {
3206 /*
3207 * Rudimentary roff copy mode:
3208 * Handle escaped backslashes.
3209 */
3210 if ('\\' == string[i] && '\\' == string[i + 1])
3211 i++;
3212 *c++ = string[i++];
3213 }
3214
3215 /* Append terminating bytes. */
3216 if (1 < append)
3217 *c++ = '\n';
3218
3219 *c = '\0';
3220 n->val.sz = (int)(c - n->val.p);
3221 }
3222
3223 static const char *
3224 roff_getstrn(const struct roff *r, const char *name, size_t len)
3225 {
3226 const struct roffkv *n;
3227 int i;
3228
3229 for (n = r->strtab; n; n = n->next)
3230 if (0 == strncmp(name, n->key.p, len) &&
3231 '\0' == n->key.p[(int)len])
3232 return n->val.p;
3233
3234 for (i = 0; i < PREDEFS_MAX; i++)
3235 if (0 == strncmp(name, predefs[i].name, len) &&
3236 '\0' == predefs[i].name[(int)len])
3237 return predefs[i].str;
3238
3239 return NULL;
3240 }
3241
3242 static void
3243 roff_freestr(struct roffkv *r)
3244 {
3245 struct roffkv *n, *nn;
3246
3247 for (n = r; n; n = nn) {
3248 free(n->key.p);
3249 free(n->val.p);
3250 nn = n->next;
3251 free(n);
3252 }
3253 }
3254
3255 /* --- accessors and utility functions ------------------------------------ */
3256
3257 const struct tbl_span *
3258 roff_span(const struct roff *r)
3259 {
3260
3261 return r->tbl ? tbl_span(r->tbl) : NULL;
3262 }
3263
3264 const struct eqn *
3265 roff_eqn(const struct roff *r)
3266 {
3267
3268 return r->last_eqn ? &r->last_eqn->eqn : NULL;
3269 }
3270
3271 /*
3272 * Duplicate an input string, making the appropriate character
3273 * conversations (as stipulated by `tr') along the way.
3274 * Returns a heap-allocated string with all the replacements made.
3275 */
3276 char *
3277 roff_strdup(const struct roff *r, const char *p)
3278 {
3279 const struct roffkv *cp;
3280 char *res;
3281 const char *pp;
3282 size_t ssz, sz;
3283 enum mandoc_esc esc;
3284
3285 if (NULL == r->xmbtab && NULL == r->xtab)
3286 return mandoc_strdup(p);
3287 else if ('\0' == *p)
3288 return mandoc_strdup("");
3289
3290 /*
3291 * Step through each character looking for term matches
3292 * (remember that a `tr' can be invoked with an escape, which is
3293 * a glyph but the escape is multi-character).
3294 * We only do this if the character hash has been initialised
3295 * and the string is >0 length.
3296 */
3297
3298 res = NULL;
3299 ssz = 0;
3300
3301 while ('\0' != *p) {
3302 assert((unsigned int)*p < 128);
3303 if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
3304 sz = r->xtab[(int)*p].sz;
3305 res = mandoc_realloc(res, ssz + sz + 1);
3306 memcpy(res + ssz, r->xtab[(int)*p].p, sz);
3307 ssz += sz;
3308 p++;
3309 continue;
3310 } else if ('\\' != *p) {
3311 res = mandoc_realloc(res, ssz + 2);
3312 res[ssz++] = *p++;
3313 continue;
3314 }
3315
3316 /* Search for term matches. */
3317 for (cp = r->xmbtab; cp; cp = cp->next)
3318 if (0 == strncmp(p, cp->key.p, cp->key.sz))
3319 break;
3320
3321 if (NULL != cp) {
3322 /*
3323 * A match has been found.
3324 * Append the match to the array and move
3325 * forward by its keysize.
3326 */
3327 res = mandoc_realloc(res,
3328 ssz + cp->val.sz + 1);
3329 memcpy(res + ssz, cp->val.p, cp->val.sz);
3330 ssz += cp->val.sz;
3331 p += (int)cp->key.sz;
3332 continue;
3333 }
3334
3335 /*
3336 * Handle escapes carefully: we need to copy
3337 * over just the escape itself, or else we might
3338 * do replacements within the escape itself.
3339 * Make sure to pass along the bogus string.
3340 */
3341 pp = p++;
3342 esc = mandoc_escape(&p, NULL, NULL);
3343 if (ESCAPE_ERROR == esc) {
3344 sz = strlen(pp);
3345 res = mandoc_realloc(res, ssz + sz + 1);
3346 memcpy(res + ssz, pp, sz);
3347 break;
3348 }
3349 /*
3350 * We bail out on bad escapes.
3351 * No need to warn: we already did so when
3352 * roff_res() was called.
3353 */
3354 sz = (int)(p - pp);
3355 res = mandoc_realloc(res, ssz + sz + 1);
3356 memcpy(res + ssz, pp, sz);
3357 ssz += sz;
3358 }
3359
3360 res[(int)ssz] = '\0';
3361 return res;
3362 }
3363
3364 int
3365 roff_getformat(const struct roff *r)
3366 {
3367
3368 return r->format;
3369 }
3370
3371 /*
3372 * Find out whether a line is a macro line or not.
3373 * If it is, adjust the current position and return one; if it isn't,
3374 * return zero and don't change the current position.
3375 * If the control character has been set with `.cc', then let that grain
3376 * precedence.
3377 * This is slighly contrary to groff, where using the non-breaking
3378 * control character when `cc' has been invoked will cause the
3379 * non-breaking macro contents to be printed verbatim.
3380 */
3381 int
3382 roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
3383 {
3384 int pos;
3385
3386 pos = *ppos;
3387
3388 if (0 != r->control && cp[pos] == r->control)
3389 pos++;
3390 else if (0 != r->control)
3391 return 0;
3392 else if ('\\' == cp[pos] && '.' == cp[pos + 1])
3393 pos += 2;
3394 else if ('.' == cp[pos] || '\'' == cp[pos])
3395 pos++;
3396 else
3397 return 0;
3398
3399 while (' ' == cp[pos] || '\t' == cp[pos])
3400 pos++;
3401
3402 *ppos = pos;
3403 return 1;
3404 }