]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.302 2017/05/08 20:33:53 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
33 #include "mandoc_ohash.h"
35 #include "libmandoc.h"
39 /* Maximum number of string expansions per line, to break infinite loops. */
40 #define EXPAND_LIMIT 1000
42 /* --- data types --------------------------------------------------------- */
45 * An incredibly-simple string buffer.
48 char *p
; /* nil-terminated buffer */
49 size_t sz
; /* saved strlen(p) */
53 * A key-value roffstr pair as part of a singly-linked list.
58 struct roffkv
*next
; /* next in list */
62 * A single number register as part of a singly-linked list.
71 * Association of request and macro names with token IDs.
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 */
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 */
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 */
123 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
126 roffproc proc
; /* process new macro */
127 roffproc text
; /* process as child text of macro */
128 roffproc sub
; /* process as child of macro */
130 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
134 const char *name
; /* predefined input name */
135 const char *str
; /* replacement symbol */
138 #define PREDEF(__name, __str) \
139 { (__name), (__str) },
141 /* --- function prototypes ------------------------------------------------ */
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
*,
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 *,
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
);
209 /* --- constant data ------------------------------------------------------ */
211 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
212 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
214 const char *__roff_name
[MAN_MAX
+ 1] = {
215 "br", "ft", "ll", "sp",
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",
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
,
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",
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",
314 "RE", "RS", "DT", "UC",
316 "OP", "EX", "EE", "UR",
319 const char *const *roff_name
= __roff_name
;
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 }
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",
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",
598 /* Array of injected predefined strings. */
599 #define PREDEFS_MAX 38
600 static const struct predef predefs
[PREDEFS_MAX
] = {
601 #include "predefs.in"
604 static int roffit_lines
; /* number of lines to delay */
605 static char *roffit_macro
; /* nil-terminated macro line */
608 /* --- request table ------------------------------------------------------ */
611 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
619 htab
= mandoc_malloc(sizeof(*htab
));
620 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
622 for (tok
= mintok
; tok
< maxtok
; tok
++) {
623 if (roff_name
[tok
] == NULL
)
625 sz
= strlen(roff_name
[tok
]);
626 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
628 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
629 slot
= ohash_qlookup(htab
, req
->name
);
630 ohash_insert(htab
, slot
, req
);
636 roffhash_free(struct ohash
*htab
)
643 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
644 req
= ohash_next(htab
, &slot
))
651 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
658 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
660 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
661 return req
== NULL
? TOKEN_NONE
: req
->tok
;
664 /* --- stack of request blocks -------------------------------------------- */
667 * Pop the current node off of the stack of roff instructions currently
671 roffnode_pop(struct roff
*r
)
678 r
->last
= r
->last
->parent
;
685 * Push a roff node onto the instruction stack. This must later be
686 * removed with roffnode_pop().
689 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
694 p
= mandoc_calloc(1, sizeof(struct roffnode
));
697 p
->name
= mandoc_strdup(name
);
701 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
706 /* --- roff parser state data management ---------------------------------- */
709 roff_free1(struct roff
*r
)
711 struct tbl_node
*tbl
;
715 while (NULL
!= (tbl
= r
->first_tbl
)) {
716 r
->first_tbl
= tbl
->next
;
719 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
721 while (NULL
!= (e
= r
->first_eqn
)) {
722 r
->first_eqn
= e
->next
;
725 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
735 roff_freereg(r
->regtab
);
738 roff_freestr(r
->strtab
);
739 roff_freestr(r
->xmbtab
);
740 r
->strtab
= r
->xmbtab
= NULL
;
743 for (i
= 0; i
< 128; i
++)
750 roff_reset(struct roff
*r
)
753 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
758 roff_free(struct roff
*r
)
761 roffhash_free(r
->reqtab
);
766 roff_alloc(struct mparse
*parse
, int options
)
770 r
= mandoc_calloc(1, sizeof(struct roff
));
772 r
->reqtab
= roffhash_alloc(0, ROFF_USERDEF
);
773 r
->options
= options
;
774 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
779 /* --- syntax tree state data management ---------------------------------- */
782 roff_man_free1(struct roff_man
*man
)
785 if (man
->first
!= NULL
)
786 roff_node_delete(man
, man
->first
);
787 free(man
->meta
.msec
);
790 free(man
->meta
.arch
);
791 free(man
->meta
.title
);
792 free(man
->meta
.name
);
793 free(man
->meta
.date
);
797 roff_man_alloc1(struct roff_man
*man
)
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
;
806 man
->macroset
= MACROSET_NONE
;
807 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
808 man
->next
= ROFF_NEXT_CHILD
;
812 roff_man_reset(struct roff_man
*man
)
816 roff_man_alloc1(man
);
820 roff_man_free(struct roff_man
*man
)
828 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
829 const char *defos
, int quick
)
831 struct roff_man
*man
;
833 man
= mandoc_calloc(1, sizeof(*man
));
838 roff_man_alloc1(man
);
843 /* --- syntax tree handling ----------------------------------------------- */
846 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
847 enum roff_type type
, int tok
)
851 n
= mandoc_calloc(1, sizeof(*n
));
856 n
->sec
= man
->lastsec
;
858 if (man
->flags
& MDOC_SYNOPSIS
)
859 n
->flags
|= NODE_SYNPRETTY
;
861 n
->flags
&= ~NODE_SYNPRETTY
;
862 if (man
->flags
& MDOC_NEWLINE
)
863 n
->flags
|= NODE_LINE
;
864 man
->flags
&= ~MDOC_NEWLINE
;
870 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
874 case ROFF_NEXT_SIBLING
:
875 if (man
->last
->next
!= NULL
) {
876 n
->next
= man
->last
->next
;
877 man
->last
->next
->prev
= n
;
879 man
->last
->parent
->last
= n
;
882 n
->parent
= man
->last
->parent
;
884 case ROFF_NEXT_CHILD
:
885 if (man
->last
->child
!= NULL
) {
886 n
->next
= man
->last
->child
;
887 man
->last
->child
->prev
= n
;
890 man
->last
->child
= n
;
891 n
->parent
= man
->last
;
903 if (n
->end
!= ENDBODY_NOT
)
915 * Copy over the normalised-data pointer of our parent. Not
916 * everybody has one, but copying a null pointer is fine.
919 n
->norm
= n
->parent
->norm
;
920 assert(n
->parent
->type
== ROFFT_BLOCK
);
924 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
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
;
936 roff_word_append(struct roff_man
*man
, const char *word
)
939 char *addstr
, *newstr
;
942 addstr
= roff_strdup(man
->roff
, word
);
943 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
947 man
->next
= ROFF_NEXT_SIBLING
;
951 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
955 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
956 roff_node_append(man
, n
);
957 man
->next
= ROFF_NEXT_CHILD
;
961 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
965 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
966 roff_node_append(man
, n
);
967 man
->next
= ROFF_NEXT_CHILD
;
972 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
976 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
977 roff_node_append(man
, n
);
978 man
->next
= ROFF_NEXT_CHILD
;
983 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
987 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
988 roff_node_append(man
, n
);
989 man
->next
= ROFF_NEXT_CHILD
;
994 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
998 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1000 if (eqn
->ln
> man
->last
->line
)
1001 n
->flags
|= NODE_LINE
;
1002 roff_node_append(man
, n
);
1003 man
->next
= ROFF_NEXT_SIBLING
;
1007 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1009 struct roff_node
*n
;
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
);
1015 roff_node_append(man
, n
);
1016 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1017 man
->next
= ROFF_NEXT_SIBLING
;
1021 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1024 /* Adjust siblings. */
1027 n
->prev
->next
= n
->next
;
1029 n
->next
->prev
= n
->prev
;
1031 /* Adjust parent. */
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
;
1040 /* Adjust parse point. */
1044 if (man
->last
== n
) {
1045 if (n
->prev
== NULL
) {
1046 man
->last
= n
->parent
;
1047 man
->next
= ROFF_NEXT_CHILD
;
1049 man
->last
= n
->prev
;
1050 man
->next
= ROFF_NEXT_SIBLING
;
1053 if (man
->first
== n
)
1058 roff_node_free(struct roff_node
*n
)
1061 if (n
->args
!= NULL
)
1062 mdoc_argv_free(n
->args
);
1063 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1070 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1073 while (n
->child
!= NULL
)
1074 roff_node_delete(man
, n
->child
);
1075 roff_node_unlink(man
, n
);
1080 deroff(char **dest
, const struct roff_node
*n
)
1085 if (n
->type
!= ROFFT_TEXT
) {
1086 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1091 /* Skip leading whitespace. */
1093 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1094 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1095 strchr(" %&0^|~", cp
[1]) != NULL
)
1097 else if ( ! isspace((unsigned char)*cp
))
1101 /* Skip trailing backslash. */
1104 if (sz
> 0 && cp
[sz
- 1] == '\\')
1107 /* Skip trailing whitespace. */
1110 if ( ! isspace((unsigned char)cp
[sz
-1]))
1113 /* Skip empty strings. */
1118 if (*dest
== NULL
) {
1119 *dest
= mandoc_strndup(cp
, sz
);
1123 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1128 /* --- main functions of the roff parser ---------------------------------- */
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.
1136 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
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 */
1155 start
= buf
->buf
+ pos
;
1156 stesc
= strchr(start
, '\0') - 1;
1157 while (stesc
-- > start
) {
1159 /* Search backwards for the next backslash. */
1164 /* If it is escaped, skip it. */
1166 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1170 if ((stesc
- cp
) % 2 == 0) {
1175 /* Decide whether to expand or to check only. */
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
);
1201 if (EXPAND_LIMIT
< ++expand_count
) {
1202 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1203 ln
, (int)(stesc
- buf
->buf
), NULL
);
1208 * The third character decides the length
1209 * of the name of the string or register.
1210 * Save a pointer to the name.
1237 /* Advance to the end of the name. */
1241 while (maxl
== 0 || naml
< maxl
) {
1243 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1244 ln
, (int)(stesc
- buf
->buf
), stesc
);
1248 if (maxl
== 0 && *cp
== term
) {
1252 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1256 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1257 case ESCAPE_SPECIAL
:
1258 case ESCAPE_UNICODE
:
1259 case ESCAPE_NUMBERED
:
1260 case ESCAPE_OVERSTRIKE
:
1269 * Retrieve the replacement string; if it is
1270 * undefined, resume searching for escapes.
1276 res
= roff_getstrn(r
, stnam
, naml
);
1280 ubuf
[0] = arg_complete
&&
1281 roff_evalnum(r
, ln
, stnam
, &npos
,
1282 NULL
, ROFFNUM_SCALE
) &&
1283 stnam
+ npos
+ 1 == cp
? '1' : '0';
1288 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1289 roff_getregn(r
, stnam
, naml
));
1294 /* use even incomplete args */
1295 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1301 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1302 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1303 "%.*s", (int)naml
, stnam
);
1305 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1306 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1307 ln
, (int)(stesc
- buf
->buf
), NULL
);
1311 /* Replace the escape sequence by the string. */
1314 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1315 buf
->buf
, res
, cp
) + 1;
1317 /* Prepare for the next replacement. */
1320 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1328 * Process text streams.
1331 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1337 enum mandoc_esc esc
;
1339 /* Spring the input line trap. */
1341 if (roffit_lines
== 1) {
1342 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1349 return ROFF_REPARSE
;
1350 } else if (roffit_lines
> 1)
1353 /* Convert all breakable hyphens into ASCII_HYPH. */
1355 start
= p
= buf
->buf
+ pos
;
1357 while (*p
!= '\0') {
1358 sz
= strcspn(p
, "-\\");
1365 /* Skip over escapes. */
1367 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1368 if (esc
== ESCAPE_ERROR
)
1373 } else if (p
== start
) {
1378 if (isalpha((unsigned char)p
[-1]) &&
1379 isalpha((unsigned char)p
[1]))
1387 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
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) */
1398 /* Handle in-line equation delimiters. */
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
)
1406 assert(e
== ROFF_CONT
);
1409 /* Expand some escape sequences. */
1411 e
= roff_res(r
, buf
, ln
, pos
);
1414 assert(e
== ROFF_CONT
);
1416 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
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.
1426 if (r
->last
!= NULL
&& ! ctl
) {
1428 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1431 assert(e
== ROFF_CONT
);
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
);
1438 return roff_parsetext(buf
, pos
, offs
);
1440 /* Skip empty request lines. */
1442 if (buf
->buf
[pos
] == '"') {
1443 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1446 } else if (buf
->buf
[pos
] == '\0')
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.
1457 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1460 /* No scope is open. This is a new request or macro. */
1463 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1465 /* Tables ignore most macros. */
1467 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
)) {
1468 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1469 ln
, pos
, buf
->buf
+ spos
);
1472 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1474 while (buf
->buf
[pos
] == ' ')
1476 return tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1480 * This is neither a roff request nor a user-defined macro.
1481 * Let the standard macro set parsers handle it.
1484 if (t
== TOKEN_NONE
)
1487 /* Execute a roff request or a user defined macro. */
1489 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1493 roff_endparse(struct roff
*r
)
1497 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1498 r
->last
->line
, r
->last
->col
,
1499 roff_name
[r
->last
->tok
]);
1502 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1503 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1508 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1509 r
->tbl
->line
, r
->tbl
->pos
, "TS");
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.
1518 static enum roff_tok
1519 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1528 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1532 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1534 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1535 ? ROFF_USERDEF
: roffhash_find(r
->reqtab
, mac
, maclen
);
1537 if (t
!= TOKEN_NONE
)
1543 /* --- handling of request blocks ----------------------------------------- */
1546 roff_cblock(ROFF_ARGS
)
1550 * A block-close `..' should only be invoked as a child of an
1551 * ignore macro, otherwise raise a warning and just ignore it.
1554 if (r
->last
== NULL
) {
1555 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1560 switch (r
->last
->tok
) {
1562 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1565 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1570 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1575 if (buf
->buf
[pos
] != '\0')
1576 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1577 ".. %s", buf
->buf
+ pos
);
1580 roffnode_cleanscope(r
);
1586 roffnode_cleanscope(struct roff
*r
)
1590 if (--r
->last
->endspan
!= 0)
1597 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1600 if (NULL
== r
->last
) {
1601 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1606 switch (r
->last
->tok
) {
1612 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1617 if (r
->last
->endspan
> -1) {
1618 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1624 roffnode_cleanscope(r
);
1629 roff_block(ROFF_ARGS
)
1635 /* Ignore groff compatibility mode for now. */
1637 if (tok
== ROFF_de1
)
1639 else if (tok
== ROFF_dei1
)
1641 else if (tok
== ROFF_am1
)
1643 else if (tok
== ROFF_ami1
)
1646 /* Parse the macro name argument. */
1648 cp
= buf
->buf
+ pos
;
1649 if (tok
== ROFF_ig
) {
1654 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1655 iname
[namesz
] = '\0';
1658 /* Resolve the macro name argument if it is indirect. */
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
);
1667 namesz
= strlen(name
);
1671 if (namesz
== 0 && tok
!= ROFF_ig
) {
1672 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1673 ln
, ppos
, roff_name
[tok
]);
1677 roffnode_push(r
, tok
, name
, ln
, ppos
);
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.
1685 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1686 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1691 /* Get the custom end marker. */
1694 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1696 /* Resolve the end marker if it is indirect. */
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
);
1705 namesz
= strlen(name
);
1710 r
->last
->end
= mandoc_strndup(name
, namesz
);
1713 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1714 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
1720 roff_block_sub(ROFF_ARGS
)
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.
1735 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1736 if (buf
->buf
[i
] != r
->last
->end
[j
])
1739 if (r
->last
->end
[j
] == '\0' &&
1740 (buf
->buf
[i
] == '\0' ||
1741 buf
->buf
[i
] == ' ' ||
1742 buf
->buf
[i
] == '\t')) {
1744 roffnode_cleanscope(r
);
1746 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1750 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1758 * If we have no custom end-query or lookup failed, then try
1759 * pulling it out of the hashtable.
1762 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1764 if (t
!= ROFF_cblock
) {
1766 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1770 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1774 roff_block_text(ROFF_ARGS
)
1778 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1784 roff_cond_sub(ROFF_ARGS
)
1791 roffnode_cleanscope(r
);
1792 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1795 * Fully handle known macros when they are structurally
1796 * required or when the conditional evaluated to true.
1799 if (t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
))
1800 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1803 * If `\}' occurs on a macro line without a preceding macro,
1804 * drop the line completely.
1807 ep
= buf
->buf
+ pos
;
1808 if (ep
[0] == '\\' && ep
[1] == '}')
1811 /* Always check for the closing delimiter `\}'. */
1813 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1814 if (*(++ep
) == '}') {
1816 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1821 return rr
? ROFF_CONT
: ROFF_IGN
;
1825 roff_cond_text(ROFF_ARGS
)
1831 roffnode_cleanscope(r
);
1833 ep
= buf
->buf
+ pos
;
1834 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1835 if (*(++ep
) == '}') {
1837 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1842 return rr
? ROFF_CONT
: ROFF_IGN
;
1845 /* --- handling of numeric and conditional expressions -------------------- */
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.
1854 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
1856 int myres
, scaled
, n
, p
;
1863 if (n
|| v
[p
] == '+')
1866 if (flags
& ROFFNUM_WHITE
)
1867 while (isspace((unsigned char)v
[p
]))
1870 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1871 *res
= 10 * *res
+ v
[p
] - '0';
1878 /* Each number may be followed by one optional scaling unit. */
1882 scaled
= *res
* 65536;
1885 scaled
= *res
* 240;
1888 scaled
= *res
* 240 / 2.54;
1899 scaled
= *res
* 10 / 3;
1905 scaled
= *res
* 6 / 25;
1912 if (flags
& ROFFNUM_SCALE
)
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.
1928 roff_evalstrcond(const char *v
, int *pos
)
1930 const char *s1
, *s2
, *s3
;
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 */
1938 if (NULL
== s3
) /* found no middle delimiter */
1941 while ('\0' != *++s3
) {
1942 if (*s2
!= *s3
) { /* mismatch */
1943 s3
= strchr(s3
, *s1
);
1946 if (*s3
== *s1
) { /* found the final delimiter */
1955 s3
= strchr(s2
, '\0');
1956 else if (*s3
!= '\0')
1963 * Evaluate an optionally negated single character, numerical,
1964 * or string condition.
1967 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
1971 int number
, savepos
, wanttrue
;
1973 if ('!' == v
[*pos
]) {
1994 cp
= name
= v
+ ++*pos
;
1995 sz
= roff_getname(r
, &cp
, ln
, *pos
);
1997 return (sz
&& roff_hasregn(r
, name
, sz
)) == wanttrue
;
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
;
2012 roff_line_ignore(ROFF_ARGS
)
2019 roff_insec(ROFF_ARGS
)
2022 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2023 ln
, ppos
, roff_name
[tok
]);
2028 roff_unsupp(ROFF_ARGS
)
2031 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2032 ln
, ppos
, roff_name
[tok
]);
2037 roff_cond(ROFF_ARGS
)
2040 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2043 * An `.el' has no conditional body: it will consume the value
2044 * of the current rstack entry set in prior `ie' calls or
2047 * If we're not an `el', however, then evaluate the conditional.
2050 r
->last
->rule
= tok
== ROFF_el
?
2051 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2052 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2055 * An if-else will put the NEGATION of the current evaluated
2056 * conditional into the stack of rules.
2059 if (tok
== ROFF_ie
) {
2060 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2062 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2063 r
->rstacksz
, sizeof(int));
2065 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2068 /* If the parent has false as its rule, then so do we. */
2070 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2075 * If there is nothing on the line after the conditional,
2076 * not even whitespace, use next-line scope.
2079 if (buf
->buf
[pos
] == '\0') {
2080 r
->last
->endspan
= 2;
2084 while (buf
->buf
[pos
] == ' ')
2087 /* An opening brace requests multiline scope. */
2089 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2090 r
->last
->endspan
= -1;
2092 while (buf
->buf
[pos
] == ' ')
2098 * Anything else following the conditional causes
2099 * single-line scope. Warn if the scope contains
2100 * nothing but trailing whitespace.
2103 if (buf
->buf
[pos
] == '\0')
2104 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2105 ln
, ppos
, roff_name
[tok
]);
2107 r
->last
->endspan
= 1;
2121 /* Ignore groff compatibility mode for now. */
2123 if (tok
== ROFF_ds1
)
2125 else if (tok
== ROFF_as1
)
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.
2134 name
= string
= buf
->buf
+ pos
;
2138 namesz
= roff_getname(r
, &string
, ln
, pos
);
2139 if (name
[namesz
] == '\\')
2142 /* Read past the initial double-quote, if any. */
2146 /* The rest is the value. */
2147 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
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.
2158 roff_getop(const char *v
, int *pos
, char *res
)
2173 switch (v
[*pos
+ 1]) {
2191 switch (v
[*pos
+ 1]) {
2205 if ('=' == v
[*pos
+ 1])
2217 * Evaluate either a parenthesized numeric expression
2218 * or a single signed integer number.
2221 roff_evalpar(struct roff
*r
, int ln
,
2222 const char *v
, int *pos
, int *res
, int flags
)
2226 return roff_getnum(v
, pos
, res
, flags
);
2229 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2233 * Omission of the closing parenthesis
2234 * is an error in validation mode,
2235 * but ignored in evaluation mode.
2240 else if (NULL
== res
)
2247 * Evaluate a complete numeric expression.
2248 * Proceed left to right, there is no concept of precedence.
2251 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2252 int *pos
, int *res
, int flags
)
2254 int mypos
, operand2
;
2262 if (flags
& ROFFNUM_WHITE
)
2263 while (isspace((unsigned char)v
[*pos
]))
2266 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2270 if (flags
& ROFFNUM_WHITE
)
2271 while (isspace((unsigned char)v
[*pos
]))
2274 if ( ! roff_getop(v
, pos
, &operator))
2277 if (flags
& ROFFNUM_WHITE
)
2278 while (isspace((unsigned char)v
[*pos
]))
2281 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2284 if (flags
& ROFFNUM_WHITE
)
2285 while (isspace((unsigned char)v
[*pos
]))
2302 if (operand2
== 0) {
2303 mandoc_msg(MANDOCERR_DIVZERO
,
2304 r
->parse
, ln
, *pos
, v
);
2311 if (operand2
== 0) {
2312 mandoc_msg(MANDOCERR_DIVZERO
,
2313 r
->parse
, ln
, *pos
, v
);
2320 *res
= *res
< operand2
;
2323 *res
= *res
> operand2
;
2326 *res
= *res
<= operand2
;
2329 *res
= *res
>= operand2
;
2332 *res
= *res
== operand2
;
2335 *res
= *res
!= operand2
;
2338 *res
= *res
&& operand2
;
2341 *res
= *res
|| operand2
;
2344 if (operand2
< *res
)
2348 if (operand2
> *res
)
2358 /* --- register management ------------------------------------------------ */
2361 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2363 struct roffreg
*reg
;
2365 /* Search for an existing register with the same name. */
2368 while (reg
&& strcmp(name
, reg
->key
.p
))
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
);
2377 reg
->next
= r
->regtab
;
2383 else if ('-' == sign
)
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.
2396 roff_getregro(const struct roff
*r
, const char *name
)
2400 case '$': /* Number of arguments of the last macro evaluated. */
2402 case 'A': /* ASCII approximation mode is always off. */
2404 case 'g': /* Groff compatibility mode is always on. */
2406 case 'H': /* Fixed horizontal resolution. */
2408 case 'j': /* Always adjust left margin only. */
2410 case 'T': /* Some output device is always defined. */
2412 case 'V': /* Fixed vertical resolution. */
2420 roff_getreg(const struct roff
*r
, const char *name
)
2422 struct roffreg
*reg
;
2425 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2426 val
= roff_getregro(r
, name
+ 1);
2431 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2432 if (0 == strcmp(name
, reg
->key
.p
))
2439 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2441 struct roffreg
*reg
;
2444 if ('.' == name
[0] && 2 == len
) {
2445 val
= roff_getregro(r
, name
+ 1);
2450 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2451 if (len
== reg
->key
.sz
&&
2452 0 == strncmp(name
, reg
->key
.p
, len
))
2459 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2461 struct roffreg
*reg
;
2464 if ('.' == name
[0] && 2 == len
) {
2465 val
= roff_getregro(r
, name
+ 1);
2470 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2471 if (len
== reg
->key
.sz
&&
2472 0 == strncmp(name
, reg
->key
.p
, len
))
2479 roff_freereg(struct roffreg
*reg
)
2481 struct roffreg
*old_reg
;
2483 while (NULL
!= reg
) {
2499 key
= val
= buf
->buf
+ pos
;
2503 keysz
= roff_getname(r
, &val
, ln
, pos
);
2504 if (key
[keysz
] == '\\')
2509 if (sign
== '+' || sign
== '-')
2512 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2513 roff_setreg(r
, key
, iv
, sign
);
2521 struct roffreg
*reg
, **prev
;
2525 name
= cp
= buf
->buf
+ pos
;
2528 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2529 name
[namesz
] = '\0';
2534 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2546 /* --- handler functions for roff requests -------------------------------- */
2555 cp
= buf
->buf
+ pos
;
2556 while (*cp
!= '\0') {
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
] == '\\')
2571 /* Parse the number of lines. */
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);
2579 while (isspace((unsigned char)buf
->buf
[pos
]))
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.
2589 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2590 strcmp(buf
->buf
+ pos
, "an-trap") ?
2591 buf
->buf
+ pos
: "br");
2598 const char *const *cp
;
2600 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2601 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2602 roff_setstr(r
, *cp
, NULL
, 0);
2605 r
->format
= MPARSE_MDOC
;
2613 const char *const *cp
;
2615 if ((r
->options
& MPARSE_QUICK
) == 0)
2616 for (cp
= __man_reserved
; *cp
; cp
++)
2617 roff_setstr(r
, *cp
, NULL
, 0);
2620 r
->format
= MPARSE_MAN
;
2630 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2632 else if ( ! tbl_end(&r
->tbl
)) {
2634 buf
->buf
= mandoc_strdup(".sp");
2636 return ROFF_REPARSE
;
2646 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2649 tbl_restart(ln
, ppos
, r
->tbl
);
2655 * Handle in-line equation delimiters.
2658 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2661 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
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.
2670 cp1
= buf
->buf
+ pos
;
2671 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2672 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2677 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2679 /* Handle preceding text, protecting whitespace. */
2681 if (*buf
->buf
!= '\0') {
2688 * Prepare replacing the delimiter with an equation macro
2689 * and drop leading white space from the equation.
2692 if (r
->eqn
== NULL
) {
2699 /* Handle following text, protecting whitespace. */
2707 /* Do the actual replacement. */
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;
2714 /* Toggle the in-line state of the eqn subsystem. */
2716 r
->eqn_inline
= r
->eqn
== NULL
;
2717 return ROFF_REPARSE
;
2725 assert(r
->eqn
== NULL
);
2726 e
= eqn_alloc(ppos
, ln
, r
->parse
);
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
;
2734 r
->first_eqn
= r
->last_eqn
= e
;
2736 r
->eqn
= r
->last_eqn
= e
;
2738 if (buf
->buf
[pos
] != '\0')
2739 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2740 ".EQ %s", buf
->buf
+ pos
);
2749 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2756 struct tbl_node
*tbl
;
2759 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2760 ln
, ppos
, "TS breaks TS");
2764 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2767 r
->last_tbl
->next
= tbl
;
2769 r
->first_tbl
= r
->last_tbl
= tbl
;
2771 r
->tbl
= r
->last_tbl
= tbl
;
2776 roff_onearg(ROFF_ARGS
)
2778 struct roff_node
*n
;
2781 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
2782 (tok
== ROFF_sp
|| tok
== ROFF_ti
))
2783 man_breakscope(r
->man
, tok
);
2785 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
2788 cp
= buf
->buf
+ pos
;
2790 while (*cp
!= '\0' && *cp
!= ' ')
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
);
2801 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
2803 r
->man
->next
= ROFF_NEXT_SIBLING
;
2808 roff_manyarg(ROFF_ARGS
)
2810 struct roff_node
*n
;
2813 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
2816 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
2817 while (*ep
!= '\0' && *ep
!= ' ')
2821 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
2824 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
2826 r
->man
->next
= ROFF_NEXT_SIBLING
;
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
;
2851 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2855 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2856 ln
, p
- buf
->buf
, "cc ... %s", p
);
2864 const char *p
, *first
, *second
;
2866 enum mandoc_esc esc
;
2871 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2875 while (*p
!= '\0') {
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
);
2886 fsz
= (size_t)(p
- first
);
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
);
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
);
2906 roff_setstrn(&r
->xmbtab
, first
, fsz
,
2911 if (r
->xtab
== NULL
)
2912 r
->xtab
= mandoc_calloc(128,
2913 sizeof(struct roffstr
));
2915 free(r
->xtab
[(int)*first
].p
);
2916 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
2917 r
->xtab
[(int)*first
].sz
= ssz
;
2928 name
= buf
->buf
+ pos
;
2929 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
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.
2938 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
2939 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
2941 buf
->sz
= mandoc_asprintf(&cp
,
2942 ".sp\nSee the file %s.\n.sp", name
) + 1;
2946 return ROFF_REPARSE
;
2953 /* --- user defined strings and macros ------------------------------------ */
2956 roff_userdef(ROFF_ARGS
)
2958 const char *arg
[9], *ap
;
2960 int expand_count
, i
, ib
, ie
;
2964 * Collect pointers to macro argument strings
2965 * and NUL-terminate them.
2969 cp
= buf
->buf
+ pos
;
2970 for (i
= 0; i
< 9; i
++) {
2974 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
2980 * Expand macro arguments.
2983 buf
->sz
= strlen(r
->current_string
) + 1;
2984 n1
= n2
= cp
= mandoc_malloc(buf
->sz
);
2985 memcpy(n1
, r
->current_string
, buf
->sz
);
2987 while (*cp
!= '\0') {
2989 /* Scan ahead for the next argument invocation. */
2995 if (*cp
== '*') { /* \\$* inserts all arguments */
2998 } else { /* \\$1 .. \\$9 insert one argument */
2999 ib
= ie
= *cp
- '1';
3000 if (ib
< 0 || ib
> 8)
3006 * Prevent infinite recursion.
3011 else if (++expand_count
> EXPAND_LIMIT
) {
3012 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
3013 ln
, (int)(cp
- n1
), NULL
);
3020 * Determine the size of the expanded argument,
3021 * taking escaping of quotes into account.
3024 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3025 for (i
= ib
; i
<= ie
; i
++) {
3026 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3035 * Determine the size of the rest of the
3036 * unexpanded macro, including the NUL.
3039 rsz
= buf
->sz
- (cp
- n1
) - 3;
3042 * When shrinking, move before
3043 * releasing the storage.
3047 memmove(cp
+ asz
, cp
+ 3, rsz
);
3050 * Resize the storage for the macro
3051 * and readjust the parse pointer.
3055 n2
= mandoc_realloc(n1
, buf
->sz
);
3056 cp
= n2
+ (cp
- n1
);
3060 * When growing, make room
3061 * for the expanded argument.
3065 memmove(cp
+ asz
, cp
+ 3, rsz
);
3068 /* Copy the expanded argument, escaping quotes. */
3071 for (i
= ib
; i
<= ie
; i
++) {
3072 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3074 memcpy(n2
, "\\(dq", 4);
3085 * Replace the macro invocation
3086 * by the expanded macro.
3093 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3094 ROFF_REPARSE
: ROFF_APPEND
;
3098 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3107 /* Read until end of name and terminate it with NUL. */
3108 for (cp
= name
; 1; cp
++) {
3109 if ('\0' == *cp
|| ' ' == *cp
) {
3116 if ('{' == cp
[1] || '}' == cp
[1])
3121 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3122 "%.*s", (int)(cp
- name
+ 1), name
);
3123 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3127 /* Read past spaces. */
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
3143 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3147 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3148 string
? strlen(string
) : 0, append
);
3152 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3153 const char *string
, size_t stringsz
, int append
)
3158 size_t oldch
, newch
;
3160 /* Search for an existing string with the same name. */
3163 while (n
&& (namesz
!= n
->key
.sz
||
3164 strncmp(n
->key
.p
, name
, namesz
)))
3168 /* Create a new string table entry. */
3169 n
= mandoc_malloc(sizeof(struct roffkv
));
3170 n
->key
.p
= mandoc_strndup(name
, namesz
);
3176 } else if (0 == append
) {
3186 * One additional byte for the '\n' in multiline mode,
3187 * and one for the terminating '\0'.
3189 newch
= stringsz
+ (1 < append
? 2u : 1u);
3191 if (NULL
== n
->val
.p
) {
3192 n
->val
.p
= mandoc_malloc(newch
);
3197 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3200 /* Skip existing content in the destination buffer. */
3201 c
= n
->val
.p
+ (int)oldch
;
3203 /* Append new content to the destination buffer. */
3205 while (i
< (int)stringsz
) {
3207 * Rudimentary roff copy mode:
3208 * Handle escaped backslashes.
3210 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3215 /* Append terminating bytes. */
3220 n
->val
.sz
= (int)(c
- n
->val
.p
);
3224 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3226 const struct roffkv
*n
;
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
])
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
;
3243 roff_freestr(struct roffkv
*r
)
3245 struct roffkv
*n
, *nn
;
3247 for (n
= r
; n
; n
= nn
) {
3255 /* --- accessors and utility functions ------------------------------------ */
3257 const struct tbl_span
*
3258 roff_span(const struct roff
*r
)
3261 return r
->tbl
? tbl_span(r
->tbl
) : NULL
;
3265 roff_eqn(const struct roff
*r
)
3268 return r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
;
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.
3277 roff_strdup(const struct roff
*r
, const char *p
)
3279 const struct roffkv
*cp
;
3283 enum mandoc_esc esc
;
3285 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3286 return mandoc_strdup(p
);
3287 else if ('\0' == *p
)
3288 return mandoc_strdup("");
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.
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
);
3310 } else if ('\\' != *p
) {
3311 res
= mandoc_realloc(res
, ssz
+ 2);
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
))
3323 * A match has been found.
3324 * Append the match to the array and move
3325 * forward by its keysize.
3327 res
= mandoc_realloc(res
,
3328 ssz
+ cp
->val
.sz
+ 1);
3329 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3331 p
+= (int)cp
->key
.sz
;
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.
3342 esc
= mandoc_escape(&p
, NULL
, NULL
);
3343 if (ESCAPE_ERROR
== esc
) {
3345 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3346 memcpy(res
+ ssz
, pp
, sz
);
3350 * We bail out on bad escapes.
3351 * No need to warn: we already did so when
3352 * roff_res() was called.
3355 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3356 memcpy(res
+ ssz
, pp
, sz
);
3360 res
[(int)ssz
] = '\0';
3365 roff_getformat(const struct roff
*r
)
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
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.
3382 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3388 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3390 else if (0 != r
->control
)
3392 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3394 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3399 while (' ' == cp
[pos
] || '\t' == cp
[pos
])