]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.374 2020/04/08 11:56:03 schwarze Exp $ */
3 * Copyright (c) 2010-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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.
18 * Implementation of the roff(7) parser for mandoc(1).
22 #include <sys/types.h>
33 #include "mandoc_aux.h"
34 #include "mandoc_ohash.h"
37 #include "mandoc_parse.h"
38 #include "libmandoc.h"
40 #include "tbl_parse.h"
41 #include "eqn_parse.h"
44 * ASCII_ESC is used to signal from roff_getarg() to roff_expand()
45 * that an escape sequence resulted from copy-in processing and
46 * needs to be checked or interpolated. As it is used nowhere
47 * else, it is defined here rather than in a header file.
51 /* Maximum number of string expansions per line, to break infinite loops. */
52 #define EXPAND_LIMIT 1000
54 /* Types of definitions of macros and strings. */
55 #define ROFFDEF_USER (1 << 1) /* User-defined. */
56 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
57 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
58 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
59 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
60 ROFFDEF_REN | ROFFDEF_STD)
61 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
63 /* --- data types --------------------------------------------------------- */
66 * An incredibly-simple string buffer.
69 char *p
; /* nil-terminated buffer */
70 size_t sz
; /* saved strlen(p) */
74 * A key-value roffstr pair as part of a singly-linked list.
79 struct roffkv
*next
; /* next in list */
83 * A single number register as part of a singly-linked list.
93 * Association of request and macro names with token IDs.
101 * A macro processing context.
102 * More than one is needed when macro calls are nested.
111 struct roff_man
*man
; /* mdoc or man parser */
112 struct roffnode
*last
; /* leaf of stack */
113 struct mctx
*mstack
; /* stack of macro contexts */
114 int *rstack
; /* stack of inverted `ie' values */
115 struct ohash
*reqtab
; /* request lookup table */
116 struct roffreg
*regtab
; /* number registers */
117 struct roffkv
*strtab
; /* user-defined strings & macros */
118 struct roffkv
*rentab
; /* renamed strings & macros */
119 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
120 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
121 const char *current_string
; /* value of last called user macro */
122 struct tbl_node
*first_tbl
; /* first table parsed */
123 struct tbl_node
*last_tbl
; /* last table parsed */
124 struct tbl_node
*tbl
; /* current table being parsed */
125 struct eqn_node
*last_eqn
; /* equation parser */
126 struct eqn_node
*eqn
; /* active equation parser */
127 int eqn_inline
; /* current equation is inline */
128 int options
; /* parse options */
129 int mstacksz
; /* current size of mstack */
130 int mstackpos
; /* position in mstack */
131 int rstacksz
; /* current size limit of rstack */
132 int rstackpos
; /* position in rstack */
133 int format
; /* current file in mdoc or man format */
134 char control
; /* control character */
135 char escape
; /* escape character */
139 * A macro definition, condition, or ignored block.
142 enum roff_tok tok
; /* type of node */
143 struct roffnode
*parent
; /* up one in stack */
144 int line
; /* parse line */
145 int col
; /* parse col */
146 char *name
; /* node name, e.g. macro name */
147 char *end
; /* custom end macro of the block */
148 int endspan
; /* scope to: 1=eol 2=next line -1=\} */
149 int rule
; /* content is: 1=evaluated 0=skipped */
152 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
153 enum roff_tok tok, /* tok of macro */ \
154 struct buf *buf, /* input buffer */ \
155 int ln, /* parse line */ \
156 int ppos, /* original pos in buffer */ \
157 int pos, /* current pos in buffer */ \
158 int *offs /* reset offset of buffer data */
160 typedef int (*roffproc
)(ROFF_ARGS
);
163 roffproc proc
; /* process new macro */
164 roffproc text
; /* process as child text of macro */
165 roffproc sub
; /* process as child of macro */
167 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
171 const char *name
; /* predefined input name */
172 const char *str
; /* replacement symbol */
175 #define PREDEF(__name, __str) \
176 { (__name), (__str) },
178 /* --- function prototypes ------------------------------------------------ */
180 static int roffnode_cleanscope(struct roff
*);
181 static int roffnode_pop(struct roff
*);
182 static void roffnode_push(struct roff
*, enum roff_tok
,
183 const char *, int, int);
184 static void roff_addtbl(struct roff_man
*, int, struct tbl_node
*);
185 static int roff_als(ROFF_ARGS
);
186 static int roff_block(ROFF_ARGS
);
187 static int roff_block_text(ROFF_ARGS
);
188 static int roff_block_sub(ROFF_ARGS
);
189 static int roff_break(ROFF_ARGS
);
190 static int roff_cblock(ROFF_ARGS
);
191 static int roff_cc(ROFF_ARGS
);
192 static int roff_ccond(struct roff
*, int, int);
193 static int roff_char(ROFF_ARGS
);
194 static int roff_cond(ROFF_ARGS
);
195 static int roff_cond_text(ROFF_ARGS
);
196 static int roff_cond_sub(ROFF_ARGS
);
197 static int roff_ds(ROFF_ARGS
);
198 static int roff_ec(ROFF_ARGS
);
199 static int roff_eo(ROFF_ARGS
);
200 static int roff_eqndelim(struct roff
*, struct buf
*, int);
201 static int roff_evalcond(struct roff
*, int, char *, int *);
202 static int roff_evalnum(struct roff
*, int,
203 const char *, int *, int *, int);
204 static int roff_evalpar(struct roff
*, int,
205 const char *, int *, int *, int);
206 static int roff_evalstrcond(const char *, int *);
207 static int roff_expand(struct roff
*, struct buf
*,
209 static void roff_free1(struct roff
*);
210 static void roff_freereg(struct roffreg
*);
211 static void roff_freestr(struct roffkv
*);
212 static size_t roff_getname(struct roff
*, char **, int, int);
213 static int roff_getnum(const char *, int *, int *, int);
214 static int roff_getop(const char *, int *, char *);
215 static int roff_getregn(struct roff
*,
216 const char *, size_t, char);
217 static int roff_getregro(const struct roff
*,
219 static const char *roff_getstrn(struct roff
*,
220 const char *, size_t, int *);
221 static int roff_hasregn(const struct roff
*,
222 const char *, size_t);
223 static int roff_insec(ROFF_ARGS
);
224 static int roff_it(ROFF_ARGS
);
225 static int roff_line_ignore(ROFF_ARGS
);
226 static void roff_man_alloc1(struct roff_man
*);
227 static void roff_man_free1(struct roff_man
*);
228 static int roff_manyarg(ROFF_ARGS
);
229 static int roff_noarg(ROFF_ARGS
);
230 static int roff_nop(ROFF_ARGS
);
231 static int roff_nr(ROFF_ARGS
);
232 static int roff_onearg(ROFF_ARGS
);
233 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
235 static int roff_parsetext(struct roff
*, struct buf
*,
237 static int roff_renamed(ROFF_ARGS
);
238 static int roff_return(ROFF_ARGS
);
239 static int roff_rm(ROFF_ARGS
);
240 static int roff_rn(ROFF_ARGS
);
241 static int roff_rr(ROFF_ARGS
);
242 static void roff_setregn(struct roff
*, const char *,
243 size_t, int, char, int);
244 static void roff_setstr(struct roff
*,
245 const char *, const char *, int);
246 static void roff_setstrn(struct roffkv
**, const char *,
247 size_t, const char *, size_t, int);
248 static int roff_shift(ROFF_ARGS
);
249 static int roff_so(ROFF_ARGS
);
250 static int roff_tr(ROFF_ARGS
);
251 static int roff_Dd(ROFF_ARGS
);
252 static int roff_TE(ROFF_ARGS
);
253 static int roff_TS(ROFF_ARGS
);
254 static int roff_EQ(ROFF_ARGS
);
255 static int roff_EN(ROFF_ARGS
);
256 static int roff_T_(ROFF_ARGS
);
257 static int roff_unsupp(ROFF_ARGS
);
258 static int roff_userdef(ROFF_ARGS
);
260 /* --- constant data ------------------------------------------------------ */
262 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
263 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
265 const char *__roff_name
[MAN_MAX
+ 1] = {
266 "br", "ce", "fi", "ft",
270 "ab", "ad", "af", "aln",
271 "als", "am", "am1", "ami",
272 "ami1", "as", "as1", "asciify",
273 "backtrace", "bd", "bleedat", "blm",
274 "box", "boxa", "bp", "BP",
275 "break", "breakchar", "brnl", "brp",
277 "cf", "cflags", "ch", "char",
278 "chop", "class", "close", "CL",
279 "color", "composite", "continue", "cp",
280 "cropat", "cs", "cu", "da",
281 "dch", "Dd", "de", "de1",
282 "defcolor", "dei", "dei1", "device",
283 "devicem", "di", "do", "ds",
284 "ds1", "dwh", "dt", "ec",
285 "ecr", "ecs", "el", "em",
286 "EN", "eo", "EP", "EQ",
287 "errprint", "ev", "evc", "ex",
288 "fallback", "fam", "fc", "fchar",
289 "fcolor", "fdeferlig", "feature", "fkern",
290 "fl", "flig", "fp", "fps",
291 "fschar", "fspacewidth", "fspecial", "ftr",
292 "fzoom", "gcolor", "hc", "hcode",
293 "hidechar", "hla", "hlm", "hpf",
294 "hpfa", "hpfcode", "hw", "hy",
295 "hylang", "hylen", "hym", "hypp",
296 "hys", "ie", "if", "ig",
297 "index", "it", "itc", "IX",
298 "kern", "kernafter", "kernbefore", "kernpair",
299 "lc", "lc_ctype", "lds", "length",
300 "letadj", "lf", "lg", "lhang",
301 "linetabs", "lnr", "lnrf", "lpfx",
303 "mediasize", "minss", "mk", "mso",
304 "na", "ne", "nh", "nhychar",
305 "nm", "nn", "nop", "nr",
306 "nrf", "nroff", "ns", "nx",
307 "open", "opena", "os", "output",
308 "padj", "papersize", "pc", "pev",
309 "pi", "PI", "pl", "pm",
311 "psbb", "pshape", "pso", "ptr",
312 "pvs", "rchar", "rd", "recursionlimit",
313 "return", "rfschar", "rhang",
314 "rm", "rn", "rnn", "rr",
315 "rs", "rt", "schar", "sentchar",
316 "shc", "shift", "sizes", "so",
317 "spacewidth", "special", "spreadwarn", "ss",
318 "sty", "substring", "sv", "sy",
321 "tm", "tm1", "tmc", "tr",
322 "track", "transchar", "trf", "trimat",
323 "trin", "trnt", "troff", "TS",
324 "uf", "ul", "unformat", "unwatch",
325 "unwatchn", "vpt", "vs", "warn",
326 "warnscale", "watch", "watchlength", "watchn",
327 "wh", "while", "write", "writec",
328 "writem", "xflag", ".", NULL
,
330 "Dd", "Dt", "Os", "Sh",
331 "Ss", "Pp", "D1", "Dl",
332 "Bd", "Ed", "Bl", "El",
333 "It", "Ad", "An", "Ap",
334 "Ar", "Cd", "Cm", "Dv",
335 "Er", "Ev", "Ex", "Fa",
336 "Fd", "Fl", "Fn", "Ft",
337 "Ic", "In", "Li", "Nd",
338 "Nm", "Op", "Ot", "Pa",
339 "Rv", "St", "Va", "Vt",
340 "Xr", "%A", "%B", "%D",
341 "%I", "%J", "%N", "%O",
342 "%P", "%R", "%T", "%V",
343 "Ac", "Ao", "Aq", "At",
344 "Bc", "Bf", "Bo", "Bq",
345 "Bsx", "Bx", "Db", "Dc",
346 "Do", "Dq", "Ec", "Ef",
347 "Em", "Eo", "Fx", "Ms",
348 "No", "Ns", "Nx", "Ox",
349 "Pc", "Pf", "Po", "Pq",
350 "Qc", "Ql", "Qo", "Qq",
351 "Re", "Rs", "Sc", "So",
352 "Sq", "Sm", "Sx", "Sy",
353 "Tn", "Ux", "Xc", "Xo",
354 "Fo", "Fc", "Oo", "Oc",
355 "Bk", "Ek", "Bt", "Hf",
356 "Fr", "Ud", "Lb", "Lp",
357 "Lk", "Mt", "Brq", "Bro",
358 "Brc", "%C", "Es", "En",
359 "Dx", "%Q", "%U", "Ta",
361 "TH", "SH", "SS", "TP",
363 "LP", "PP", "P", "IP",
364 "HP", "SM", "SB", "BI",
365 "IB", "BR", "RB", "R",
366 "B", "I", "IR", "RI",
367 "RE", "RS", "DT", "UC",
371 "UE", "MT", "ME", NULL
373 const char *const *roff_name
= __roff_name
;
375 static struct roffmac roffs
[TOKEN_NONE
] = {
376 { roff_noarg
, NULL
, NULL
, 0 }, /* br */
377 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
378 { roff_noarg
, NULL
, NULL
, 0 }, /* fi */
379 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
380 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
381 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
382 { roff_noarg
, NULL
, NULL
, 0 }, /* nf */
383 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
384 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
385 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
386 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
387 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
388 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
389 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
390 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
391 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
392 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
393 { roff_als
, NULL
, NULL
, 0 }, /* als */
394 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
395 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
396 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
397 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
398 { roff_ds
, NULL
, NULL
, 0 }, /* as */
399 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
400 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
401 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
402 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
403 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
404 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
405 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
406 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
407 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
409 { roff_break
, NULL
, NULL
, 0 }, /* break */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
412 { roff_noarg
, NULL
, NULL
, 0 }, /* brp */
413 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
415 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
416 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
417 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
419 { roff_char
, NULL
, NULL
, 0 }, /* char */
420 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
421 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
422 { roff_insec
, NULL
, NULL
, 0 }, /* close */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
425 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
426 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
427 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
428 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
429 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
430 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
431 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
432 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
433 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
434 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
435 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
436 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
437 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
438 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
441 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
442 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
443 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
444 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
445 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
446 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
447 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
448 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
449 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
450 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
451 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
452 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
453 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
454 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
455 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
457 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
458 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
459 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
462 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
463 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
472 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
475 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
476 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
477 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
478 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
479 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
487 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
488 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
489 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
491 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
493 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
494 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
495 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
496 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
497 { roff_it
, NULL
, NULL
, 0 }, /* it */
498 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
504 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
505 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
506 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
507 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
509 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
510 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
511 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
512 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
513 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
514 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
515 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
516 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
517 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
518 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
519 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
521 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
522 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
524 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
525 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
526 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
527 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
528 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
529 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
530 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
531 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
532 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
534 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
535 { roff_insec
, NULL
, NULL
, 0 }, /* open */
536 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
537 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
538 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
540 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
541 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
542 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
543 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
544 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
545 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
546 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
550 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
551 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
552 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
555 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
558 { roff_return
, NULL
, NULL
, 0 }, /* return */
559 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
561 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
562 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
563 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
564 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
565 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
566 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
567 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
570 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
572 { roff_so
, NULL
, NULL
, 0 }, /* so */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
574 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
575 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
576 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
577 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
578 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
579 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
580 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
581 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
582 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
583 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
584 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
586 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
587 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
589 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
590 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
592 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
593 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
594 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
595 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
596 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
597 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
598 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
599 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
600 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
601 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
602 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
603 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
604 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
605 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
606 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
607 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
608 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
609 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
610 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
611 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
612 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
613 { roff_insec
, NULL
, NULL
, 0 }, /* write */
614 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
615 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
616 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
617 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
618 { roff_renamed
, NULL
, NULL
, 0 },
619 { roff_userdef
, NULL
, NULL
, 0 }
622 /* Array of injected predefined strings. */
623 #define PREDEFS_MAX 38
624 static const struct predef predefs
[PREDEFS_MAX
] = {
625 #include "predefs.in"
628 static int roffce_lines
; /* number of input lines to center */
629 static struct roff_node
*roffce_node
; /* active request */
630 static int roffit_lines
; /* number of lines to delay */
631 static char *roffit_macro
; /* nil-terminated macro line */
634 /* --- request table ------------------------------------------------------ */
637 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
645 htab
= mandoc_malloc(sizeof(*htab
));
646 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
648 for (tok
= mintok
; tok
< maxtok
; tok
++) {
649 if (roff_name
[tok
] == NULL
)
651 sz
= strlen(roff_name
[tok
]);
652 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
654 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
655 slot
= ohash_qlookup(htab
, req
->name
);
656 ohash_insert(htab
, slot
, req
);
662 roffhash_free(struct ohash
*htab
)
669 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
670 req
= ohash_next(htab
, &slot
))
677 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
684 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
686 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
687 return req
== NULL
? TOKEN_NONE
: req
->tok
;
690 /* --- stack of request blocks -------------------------------------------- */
693 * Pop the current node off of the stack of roff instructions currently
694 * pending. Return 1 if it is a loop or 0 otherwise.
697 roffnode_pop(struct roff
*r
)
703 inloop
= p
->tok
== ROFF_while
;
712 * Push a roff node onto the instruction stack. This must later be
713 * removed with roffnode_pop().
716 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
721 p
= mandoc_calloc(1, sizeof(struct roffnode
));
724 p
->name
= mandoc_strdup(name
);
728 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
733 /* --- roff parser state data management ---------------------------------- */
736 roff_free1(struct roff
*r
)
740 tbl_free(r
->first_tbl
);
741 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
743 eqn_free(r
->last_eqn
);
744 r
->last_eqn
= r
->eqn
= NULL
;
746 while (r
->mstackpos
>= 0)
757 roff_freereg(r
->regtab
);
760 roff_freestr(r
->strtab
);
761 roff_freestr(r
->rentab
);
762 roff_freestr(r
->xmbtab
);
763 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
766 for (i
= 0; i
< 128; i
++)
773 roff_reset(struct roff
*r
)
776 r
->options
|= MPARSE_COMMENT
;
777 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
787 roff_free(struct roff
*r
)
792 for (i
= 0; i
< r
->mstacksz
; i
++)
793 free(r
->mstack
[i
].argv
);
795 roffhash_free(r
->reqtab
);
800 roff_alloc(int options
)
804 r
= mandoc_calloc(1, sizeof(struct roff
));
805 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
806 r
->options
= options
| MPARSE_COMMENT
;
807 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
814 /* --- syntax tree state data management ---------------------------------- */
817 roff_man_free1(struct roff_man
*man
)
819 if (man
->meta
.first
!= NULL
)
820 roff_node_delete(man
, man
->meta
.first
);
821 free(man
->meta
.msec
);
824 free(man
->meta
.arch
);
825 free(man
->meta
.title
);
826 free(man
->meta
.name
);
827 free(man
->meta
.date
);
828 free(man
->meta
.sodest
);
832 roff_state_reset(struct roff_man
*man
)
834 man
->last
= man
->meta
.first
;
837 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
838 man
->next
= ROFF_NEXT_CHILD
;
839 roff_setreg(man
->roff
, "nS", 0, '=');
843 roff_man_alloc1(struct roff_man
*man
)
845 memset(&man
->meta
, 0, sizeof(man
->meta
));
846 man
->meta
.first
= mandoc_calloc(1, sizeof(*man
->meta
.first
));
847 man
->meta
.first
->type
= ROFFT_ROOT
;
848 man
->meta
.macroset
= MACROSET_NONE
;
849 roff_state_reset(man
);
853 roff_man_reset(struct roff_man
*man
)
856 roff_man_alloc1(man
);
860 roff_man_free(struct roff_man
*man
)
867 roff_man_alloc(struct roff
*roff
, const char *os_s
, int quick
)
869 struct roff_man
*man
;
871 man
= mandoc_calloc(1, sizeof(*man
));
875 roff_man_alloc1(man
);
880 /* --- syntax tree handling ----------------------------------------------- */
883 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
884 enum roff_type type
, int tok
)
888 n
= mandoc_calloc(1, sizeof(*n
));
893 n
->sec
= man
->lastsec
;
895 if (man
->flags
& MDOC_SYNOPSIS
)
896 n
->flags
|= NODE_SYNPRETTY
;
898 n
->flags
&= ~NODE_SYNPRETTY
;
899 if ((man
->flags
& (ROFF_NOFILL
| ROFF_NONOFILL
)) == ROFF_NOFILL
)
900 n
->flags
|= NODE_NOFILL
;
902 n
->flags
&= ~NODE_NOFILL
;
903 if (man
->flags
& MDOC_NEWLINE
)
904 n
->flags
|= NODE_LINE
;
905 man
->flags
&= ~MDOC_NEWLINE
;
911 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
915 case ROFF_NEXT_SIBLING
:
916 if (man
->last
->next
!= NULL
) {
917 n
->next
= man
->last
->next
;
918 man
->last
->next
->prev
= n
;
920 man
->last
->parent
->last
= n
;
923 n
->parent
= man
->last
->parent
;
925 case ROFF_NEXT_CHILD
:
926 if (man
->last
->child
!= NULL
) {
927 n
->next
= man
->last
->child
;
928 man
->last
->child
->prev
= n
;
931 man
->last
->child
= n
;
932 n
->parent
= man
->last
;
944 if (n
->end
!= ENDBODY_NOT
)
956 * Copy over the normalised-data pointer of our parent. Not
957 * everybody has one, but copying a null pointer is fine.
960 n
->norm
= n
->parent
->norm
;
961 assert(n
->parent
->type
== ROFFT_BLOCK
);
965 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
969 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
970 n
->string
= roff_strdup(man
->roff
, word
);
971 roff_node_append(man
, n
);
972 n
->flags
|= NODE_VALID
| NODE_ENDED
;
973 man
->next
= ROFF_NEXT_SIBLING
;
977 roff_word_append(struct roff_man
*man
, const char *word
)
980 char *addstr
, *newstr
;
983 addstr
= roff_strdup(man
->roff
, word
);
984 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
988 man
->next
= ROFF_NEXT_SIBLING
;
992 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
996 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
997 roff_node_append(man
, n
);
998 man
->next
= ROFF_NEXT_CHILD
;
1002 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1004 struct roff_node
*n
;
1006 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1007 roff_node_append(man
, n
);
1008 man
->next
= ROFF_NEXT_CHILD
;
1013 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1015 struct roff_node
*n
;
1017 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1018 roff_node_append(man
, n
);
1019 man
->next
= ROFF_NEXT_CHILD
;
1024 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1026 struct roff_node
*n
;
1028 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1029 roff_node_append(man
, n
);
1030 man
->next
= ROFF_NEXT_CHILD
;
1035 roff_addtbl(struct roff_man
*man
, int line
, struct tbl_node
*tbl
)
1037 struct roff_node
*n
;
1038 struct tbl_span
*span
;
1040 if (man
->meta
.macroset
== MACROSET_MAN
)
1041 man_breakscope(man
, ROFF_TS
);
1042 while ((span
= tbl_span(tbl
)) != NULL
) {
1043 n
= roff_node_alloc(man
, line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1045 roff_node_append(man
, n
);
1046 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1047 man
->next
= ROFF_NEXT_SIBLING
;
1052 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1055 /* Adjust siblings. */
1058 n
->prev
->next
= n
->next
;
1060 n
->next
->prev
= n
->prev
;
1062 /* Adjust parent. */
1064 if (n
->parent
!= NULL
) {
1065 if (n
->parent
->child
== n
)
1066 n
->parent
->child
= n
->next
;
1067 if (n
->parent
->last
== n
)
1068 n
->parent
->last
= n
->prev
;
1071 /* Adjust parse point. */
1075 if (man
->last
== n
) {
1076 if (n
->prev
== NULL
) {
1077 man
->last
= n
->parent
;
1078 man
->next
= ROFF_NEXT_CHILD
;
1080 man
->last
= n
->prev
;
1081 man
->next
= ROFF_NEXT_SIBLING
;
1084 if (man
->meta
.first
== n
)
1085 man
->meta
.first
= NULL
;
1089 roff_node_relink(struct roff_man
*man
, struct roff_node
*n
)
1091 roff_node_unlink(man
, n
);
1092 n
->prev
= n
->next
= NULL
;
1093 roff_node_append(man
, n
);
1097 roff_node_free(struct roff_node
*n
)
1100 if (n
->args
!= NULL
)
1101 mdoc_argv_free(n
->args
);
1102 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1104 eqn_box_free(n
->eqn
);
1111 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1114 while (n
->child
!= NULL
)
1115 roff_node_delete(man
, n
->child
);
1116 roff_node_unlink(man
, n
);
1121 roff_node_transparent(struct roff_node
*n
)
1125 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
1127 return roff_tok_transparent(n
->tok
);
1131 roff_tok_transparent(enum roff_tok tok
)
1154 roff_node_child(struct roff_node
*n
)
1156 for (n
= n
->child
; roff_node_transparent(n
); n
= n
->next
)
1162 roff_node_prev(struct roff_node
*n
)
1166 } while (roff_node_transparent(n
));
1171 roff_node_next(struct roff_node
*n
)
1175 } while (roff_node_transparent(n
));
1180 deroff(char **dest
, const struct roff_node
*n
)
1185 if (n
->string
== NULL
) {
1186 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1191 /* Skip leading whitespace. */
1193 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1194 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1195 strchr(" %&0^|~", cp
[1]) != NULL
)
1197 else if ( ! isspace((unsigned char)*cp
))
1201 /* Skip trailing backslash. */
1204 if (sz
> 0 && cp
[sz
- 1] == '\\')
1207 /* Skip trailing whitespace. */
1210 if ( ! isspace((unsigned char)cp
[sz
-1]))
1213 /* Skip empty strings. */
1218 if (*dest
== NULL
) {
1219 *dest
= mandoc_strndup(cp
, sz
);
1223 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1228 /* --- main functions of the roff parser ---------------------------------- */
1231 * In the current line, expand escape sequences that produce parsable
1232 * input text. Also check the syntax of the remaining escape sequences,
1233 * which typically produce output glyphs or change formatter state.
1236 roff_expand(struct roff
*r
, struct buf
*buf
, int ln
, int pos
, char newesc
)
1238 struct mctx
*ctx
; /* current macro call context */
1239 char ubuf
[24]; /* buffer to print the number */
1240 struct roff_node
*n
; /* used for header comments */
1241 const char *start
; /* start of the string to process */
1242 char *stesc
; /* start of an escape sequence ('\\') */
1243 const char *esct
; /* type of esccape sequence */
1244 char *ep
; /* end of comment string */
1245 const char *stnam
; /* start of the name, after "[(*" */
1246 const char *cp
; /* end of the name, e.g. before ']' */
1247 const char *res
; /* the string to be substituted */
1248 char *nbuf
; /* new buffer to copy buf->buf to */
1249 size_t maxl
; /* expected length of the escape name */
1250 size_t naml
; /* actual length of the escape name */
1251 size_t asz
; /* length of the replacement */
1252 size_t rsz
; /* length of the rest of the string */
1253 int inaml
; /* length returned from mandoc_escape() */
1254 int expand_count
; /* to avoid infinite loops */
1255 int npos
; /* position in numeric expression */
1256 int arg_complete
; /* argument not interrupted by eol */
1257 int quote_args
; /* true for \\$@, false for \\$* */
1258 int done
; /* no more input available */
1259 int deftype
; /* type of definition to paste */
1260 int rcsid
; /* kind of RCS id seen */
1261 enum mandocerr err
; /* for escape sequence problems */
1262 char sign
; /* increment number register */
1263 char term
; /* character terminating the escape */
1265 /* Search forward for comments. */
1268 start
= buf
->buf
+ pos
;
1269 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1270 if (stesc
[0] != newesc
|| stesc
[1] == '\0')
1273 if (*stesc
!= '"' && *stesc
!= '#')
1276 /* Comment found, look for RCS id. */
1279 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1280 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1282 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1283 rcsid
= 1 << MANDOC_OS_NETBSD
;
1287 isalnum((unsigned char)*cp
) == 0 &&
1288 strchr(cp
, '$') != NULL
) {
1289 if (r
->man
->meta
.rcsids
& rcsid
)
1290 mandoc_msg(MANDOCERR_RCS_REP
, ln
,
1291 (int)(stesc
- buf
->buf
) + 1,
1293 r
->man
->meta
.rcsids
|= rcsid
;
1296 /* Handle trailing whitespace. */
1298 ep
= strchr(stesc
--, '\0') - 1;
1303 if (*ep
== ' ' || *ep
== '\t')
1304 mandoc_msg(MANDOCERR_SPACE_EOL
,
1305 ln
, (int)(ep
- buf
->buf
), NULL
);
1308 * Save comments preceding the title macro
1309 * in the syntax tree.
1312 if (newesc
!= ASCII_ESC
&& r
->options
& MPARSE_COMMENT
) {
1313 while (*ep
== ' ' || *ep
== '\t')
1316 n
= roff_node_alloc(r
->man
,
1317 ln
, stesc
+ 1 - buf
->buf
,
1318 ROFFT_COMMENT
, TOKEN_NONE
);
1319 n
->string
= mandoc_strdup(stesc
+ 2);
1320 roff_node_append(r
->man
, n
);
1321 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1322 r
->man
->next
= ROFF_NEXT_SIBLING
;
1325 /* Line continuation with comment. */
1327 if (stesc
[1] == '#') {
1329 return ROFF_IGN
| ROFF_APPEND
;
1332 /* Discard normal comments. */
1334 while (stesc
> start
&& stesc
[-1] == ' ' &&
1335 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1344 /* Notice the end of the input. */
1346 if (*stesc
== '\n') {
1352 while (stesc
>= start
) {
1353 if (*stesc
!= newesc
) {
1356 * If we have a non-standard escape character,
1357 * escape literal backslashes because all
1358 * processing in subsequent functions uses
1359 * the standard escaping rules.
1362 if (newesc
!= ASCII_ESC
&& *stesc
== '\\') {
1364 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1365 buf
->buf
, stesc
+ 1) + 1;
1367 stesc
= nbuf
+ (stesc
- buf
->buf
);
1372 /* Search backwards for the next escape. */
1378 /* If it is escaped, skip it. */
1380 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1381 if (*cp
!= r
->escape
)
1384 if ((stesc
- cp
) % 2 == 0) {
1388 } else if (stesc
[1] != '\0') {
1395 return ROFF_IGN
| ROFF_APPEND
;
1398 /* Decide whether to expand or to check only. */
1416 if (sign
== '+' || sign
== '-')
1422 switch(mandoc_escape(&cp
, &stnam
, &inaml
)) {
1423 case ESCAPE_SPECIAL
:
1424 if (mchars_spec2cp(stnam
, inaml
) >= 0)
1428 err
= MANDOCERR_ESC_BAD
;
1431 err
= MANDOCERR_ESC_UNDEF
;
1434 err
= MANDOCERR_ESC_UNSUPP
;
1439 if (err
!= MANDOCERR_OK
)
1440 mandoc_msg(err
, ln
, (int)(stesc
- buf
->buf
),
1441 "%.*s", (int)(cp
- stesc
), stesc
);
1446 if (EXPAND_LIMIT
< ++expand_count
) {
1447 mandoc_msg(MANDOCERR_ROFFLOOP
,
1448 ln
, (int)(stesc
- buf
->buf
), NULL
);
1453 * The third character decides the length
1454 * of the name of the string or register.
1455 * Save a pointer to the name.
1482 /* Advance to the end of the name. */
1486 while (maxl
== 0 || naml
< maxl
) {
1488 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
1489 (int)(stesc
- buf
->buf
), "%s", stesc
);
1493 if (maxl
== 0 && *cp
== term
) {
1497 if (*cp
++ != '\\' || *esct
!= 'w') {
1501 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1502 case ESCAPE_SPECIAL
:
1503 case ESCAPE_UNICODE
:
1504 case ESCAPE_NUMBERED
:
1506 case ESCAPE_OVERSTRIKE
:
1515 * Retrieve the replacement string; if it is
1516 * undefined, resume searching for escapes.
1522 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1523 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1526 * If not overriden, let \*(.T
1527 * through to the formatters.
1530 if (res
== NULL
&& naml
== 2 &&
1531 stnam
[0] == '.' && stnam
[1] == 'T') {
1532 roff_setstrn(&r
->strtab
,
1533 ".T", 2, NULL
, 0, 0);
1540 if (r
->mstackpos
< 0) {
1541 mandoc_msg(MANDOCERR_ARG_UNDEF
, ln
,
1542 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1545 ctx
= r
->mstack
+ r
->mstackpos
;
1546 npos
= esct
[1] - '1';
1547 if (npos
>= 0 && npos
<= 8) {
1548 res
= npos
< ctx
->argc
?
1549 ctx
->argv
[npos
] : "";
1554 else if (esct
[1] == '@')
1557 mandoc_msg(MANDOCERR_ARG_NONUM
, ln
,
1558 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1562 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1566 asz
+= 2; /* quotes */
1567 asz
+= strlen(ctx
->argv
[npos
]);
1570 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1572 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1574 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1576 stesc
= nbuf
+ (stesc
- buf
->buf
);
1579 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1581 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1586 cp
= ctx
->argv
[npos
];
1595 ubuf
[0] = arg_complete
&&
1596 roff_evalnum(r
, ln
, stnam
, &npos
,
1597 NULL
, ROFFNUM_SCALE
) &&
1598 stnam
+ npos
+ 1 == cp
? '1' : '0';
1603 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1604 roff_getregn(r
, stnam
, naml
, sign
));
1609 /* use even incomplete args */
1610 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1617 mandoc_msg(MANDOCERR_STR_UNDEF
,
1618 ln
, (int)(stesc
- buf
->buf
),
1619 "%.*s", (int)naml
, stnam
);
1621 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1622 mandoc_msg(MANDOCERR_ROFFLOOP
,
1623 ln
, (int)(stesc
- buf
->buf
), NULL
);
1627 /* Replace the escape sequence by the string. */
1630 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1631 buf
->buf
, res
, cp
) + 1;
1633 /* Prepare for the next replacement. */
1636 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1644 * Parse a quoted or unquoted roff-style request or macro argument.
1645 * Return a pointer to the parsed argument, which is either the original
1646 * pointer or advanced by one byte in case the argument is quoted.
1647 * NUL-terminate the argument in place.
1648 * Collapse pairs of quotes inside quoted arguments.
1649 * Advance the argument pointer to the next argument,
1650 * or to the NUL byte terminating the argument line.
1653 roff_getarg(struct roff
*r
, char **cpp
, int ln
, int *pos
)
1657 int newesc
, pairs
, quoted
, white
;
1659 /* Quoting can only start with a new word. */
1662 if ('"' == *start
) {
1667 newesc
= pairs
= white
= 0;
1668 for (cp
= start
; '\0' != *cp
; cp
++) {
1671 * Move the following text left
1672 * after quoted quotes and after "\\" and "\t".
1677 if ('\\' == cp
[0]) {
1679 * In copy mode, translate double to single
1680 * backslashes and backslash-t to literal tabs.
1691 cp
[-pairs
] = ASCII_ESC
;
1696 /* Skip escaped blanks. */
1703 } else if (0 == quoted
) {
1705 /* Unescaped blanks end unquoted args. */
1709 } else if ('"' == cp
[0]) {
1711 /* Quoted quotes collapse. */
1715 /* Unquoted quotes end quoted args. */
1722 /* Quoted argument without a closing quote. */
1724 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
1726 /* NUL-terminate this argument and move to the next one. */
1734 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
1737 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
1738 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
1740 start
= mandoc_strdup(start
);
1745 buf
.sz
= strlen(start
) + 1;
1747 if (roff_expand(r
, &buf
, ln
, 0, ASCII_ESC
) & ROFF_IGN
) {
1749 buf
.buf
= mandoc_strdup("");
1756 * Process text streams.
1759 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1765 enum mandoc_esc esc
;
1767 /* Spring the input line trap. */
1769 if (roffit_lines
== 1) {
1770 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1777 return ROFF_REPARSE
;
1778 } else if (roffit_lines
> 1)
1781 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1782 if (roffce_lines
< 1) {
1783 r
->man
->last
= roffce_node
;
1784 r
->man
->next
= ROFF_NEXT_SIBLING
;
1791 /* Convert all breakable hyphens into ASCII_HYPH. */
1793 start
= p
= buf
->buf
+ pos
;
1795 while (*p
!= '\0') {
1796 sz
= strcspn(p
, "-\\");
1803 /* Skip over escapes. */
1805 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1806 if (esc
== ESCAPE_ERROR
)
1811 } else if (p
== start
) {
1816 if (isalpha((unsigned char)p
[-1]) &&
1817 isalpha((unsigned char)p
[1]))
1825 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1829 int pos
; /* parse point */
1830 int spos
; /* saved parse point for messages */
1831 int ppos
; /* original offset in buf->buf */
1832 int ctl
; /* macro line (boolean) */
1836 /* Handle in-line equation delimiters. */
1838 if (r
->tbl
== NULL
&&
1839 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1840 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1841 e
= roff_eqndelim(r
, buf
, pos
);
1842 if (e
== ROFF_REPARSE
)
1844 assert(e
== ROFF_CONT
);
1847 /* Expand some escape sequences. */
1849 e
= roff_expand(r
, buf
, ln
, pos
, r
->escape
);
1850 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1852 assert(e
== ROFF_CONT
);
1854 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1857 * First, if a scope is open and we're not a macro, pass the
1858 * text through the macro's filter.
1859 * Equations process all content themselves.
1860 * Tables process almost all content themselves, but we want
1861 * to warn about macros before passing it there.
1864 if (r
->last
!= NULL
&& ! ctl
) {
1866 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1867 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1872 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1873 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1876 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1877 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1878 roff_addtbl(r
->man
, ln
, r
->tbl
);
1882 r
->options
&= ~MPARSE_COMMENT
;
1883 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1886 /* Skip empty request lines. */
1888 if (buf
->buf
[pos
] == '"') {
1889 mandoc_msg(MANDOCERR_COMMENT_BAD
, ln
, pos
, NULL
);
1891 } else if (buf
->buf
[pos
] == '\0')
1895 * If a scope is open, go to the child handler for that macro,
1896 * as it may want to preprocess before doing anything with it.
1897 * Don't do so if an equation is open.
1902 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1905 /* No scope is open. This is a new request or macro. */
1907 r
->options
&= ~MPARSE_COMMENT
;
1909 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1911 /* Tables ignore most macros. */
1913 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1914 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1915 mandoc_msg(MANDOCERR_TBLMACRO
,
1916 ln
, pos
, "%s", buf
->buf
+ spos
);
1917 if (t
!= TOKEN_NONE
)
1919 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1921 while (buf
->buf
[pos
] == ' ')
1923 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1924 roff_addtbl(r
->man
, ln
, r
->tbl
);
1928 /* For now, let high level macros abort .ce mode. */
1930 if (ctl
&& roffce_node
!= NULL
&&
1931 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1932 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1933 r
->man
->last
= roffce_node
;
1934 r
->man
->next
= ROFF_NEXT_SIBLING
;
1940 * This is neither a roff request nor a user-defined macro.
1941 * Let the standard macro set parsers handle it.
1944 if (t
== TOKEN_NONE
)
1947 /* Execute a roff request or a user defined macro. */
1949 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1953 * Internal interface function to tell the roff parser that execution
1954 * of the current macro ended. This is required because macro
1955 * definitions usually do not end with a .return request.
1958 roff_userret(struct roff
*r
)
1963 assert(r
->mstackpos
>= 0);
1964 ctx
= r
->mstack
+ r
->mstackpos
;
1965 for (i
= 0; i
< ctx
->argc
; i
++)
1972 roff_endparse(struct roff
*r
)
1974 if (r
->last
!= NULL
)
1975 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->last
->line
,
1976 r
->last
->col
, "%s", roff_name
[r
->last
->tok
]);
1978 if (r
->eqn
!= NULL
) {
1979 mandoc_msg(MANDOCERR_BLK_NOEND
,
1980 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1985 if (r
->tbl
!= NULL
) {
1992 * Parse a roff node's type from the input buffer. This must be in the
1993 * form of ".foo xxx" in the usual way.
1995 static enum roff_tok
1996 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
2006 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
2010 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
2012 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
2013 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
2022 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
2025 if (t
!= TOKEN_NONE
)
2027 else if (deftype
== ROFFDEF_UNDEF
) {
2028 /* Using an undefined macro defines it to be empty. */
2029 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
2030 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
2035 /* --- handling of request blocks ----------------------------------------- */
2038 roff_cblock(ROFF_ARGS
)
2042 * A block-close `..' should only be invoked as a child of an
2043 * ignore macro, otherwise raise a warning and just ignore it.
2046 if (r
->last
== NULL
) {
2047 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2051 switch (r
->last
->tok
) {
2053 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
2056 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
2061 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2065 if (buf
->buf
[pos
] != '\0')
2066 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
2067 ".. %s", buf
->buf
+ pos
);
2070 roffnode_cleanscope(r
);
2076 * Pop all nodes ending at the end of the current input line.
2077 * Return the number of loops ended.
2080 roffnode_cleanscope(struct roff
*r
)
2085 while (r
->last
!= NULL
) {
2086 if (--r
->last
->endspan
!= 0)
2088 inloop
+= roffnode_pop(r
);
2094 * Handle the closing \} of a conditional block.
2095 * Apart from generating warnings, this only pops nodes.
2096 * Return the number of loops ended.
2099 roff_ccond(struct roff
*r
, int ln
, int ppos
)
2101 if (NULL
== r
->last
) {
2102 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2106 switch (r
->last
->tok
) {
2113 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2117 if (r
->last
->endspan
> -1) {
2118 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2122 return roffnode_pop(r
) + roffnode_cleanscope(r
);
2126 roff_block(ROFF_ARGS
)
2128 const char *name
, *value
;
2129 char *call
, *cp
, *iname
, *rname
;
2130 size_t csz
, namesz
, rsz
;
2133 /* Ignore groff compatibility mode for now. */
2135 if (tok
== ROFF_de1
)
2137 else if (tok
== ROFF_dei1
)
2139 else if (tok
== ROFF_am1
)
2141 else if (tok
== ROFF_ami1
)
2144 /* Parse the macro name argument. */
2146 cp
= buf
->buf
+ pos
;
2147 if (tok
== ROFF_ig
) {
2152 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2153 iname
[namesz
] = '\0';
2156 /* Resolve the macro name argument if it is indirect. */
2158 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2159 deftype
= ROFFDEF_USER
;
2160 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2162 mandoc_msg(MANDOCERR_STR_UNDEF
,
2163 ln
, (int)(iname
- buf
->buf
),
2164 "%.*s", (int)namesz
, iname
);
2167 namesz
= strlen(name
);
2171 if (namesz
== 0 && tok
!= ROFF_ig
) {
2172 mandoc_msg(MANDOCERR_REQ_EMPTY
,
2173 ln
, ppos
, "%s", roff_name
[tok
]);
2177 roffnode_push(r
, tok
, name
, ln
, ppos
);
2180 * At the beginning of a `de' macro, clear the existing string
2181 * with the same name, if there is one. New content will be
2182 * appended from roff_block_text() in multiline mode.
2185 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
2186 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
2187 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2188 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
2189 deftype
= ROFFDEF_ANY
;
2190 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2191 switch (deftype
) { /* Before appending, ... */
2192 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
2193 roff_setstrn(&r
->strtab
, name
, namesz
,
2194 value
, strlen(value
), 0);
2196 case ROFFDEF_REN
: /* call original standard macro. */
2197 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2198 (int)strlen(value
), value
);
2199 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2200 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2203 case ROFFDEF_STD
: /* rename and call standard macro. */
2204 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
2205 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
2206 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2208 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2220 /* Get the custom end marker. */
2223 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2225 /* Resolve the end marker if it is indirect. */
2227 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2228 deftype
= ROFFDEF_USER
;
2229 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2231 mandoc_msg(MANDOCERR_STR_UNDEF
,
2232 ln
, (int)(iname
- buf
->buf
),
2233 "%.*s", (int)namesz
, iname
);
2236 namesz
= strlen(name
);
2241 r
->last
->end
= mandoc_strndup(name
, namesz
);
2244 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2245 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2251 roff_block_sub(ROFF_ARGS
)
2257 * First check whether a custom macro exists at this level. If
2258 * it does, then check against it. This is some of groff's
2259 * stranger behaviours. If we encountered a custom end-scope
2260 * tag and that tag also happens to be a "real" macro, then we
2261 * need to try interpreting it again as a real macro. If it's
2262 * not, then return ignore. Else continue.
2266 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2267 if (buf
->buf
[i
] != r
->last
->end
[j
])
2270 if (r
->last
->end
[j
] == '\0' &&
2271 (buf
->buf
[i
] == '\0' ||
2272 buf
->buf
[i
] == ' ' ||
2273 buf
->buf
[i
] == '\t')) {
2275 roffnode_cleanscope(r
);
2277 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2281 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2289 * If we have no custom end-query or lookup failed, then try
2290 * pulling it out of the hashtable.
2293 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2295 if (t
!= ROFF_cblock
) {
2297 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2301 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2305 roff_block_text(ROFF_ARGS
)
2309 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2315 roff_cond_sub(ROFF_ARGS
)
2317 struct roffnode
*bl
;
2319 int endloop
, irc
, rr
;
2324 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2325 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2326 if (roffnode_cleanscope(r
))
2330 * If `\}' occurs on a macro line without a preceding macro,
2331 * drop the line completely.
2334 ep
= buf
->buf
+ pos
;
2335 if (ep
[0] == '\\' && ep
[1] == '}')
2339 * The closing delimiter `\}' rewinds the conditional scope
2340 * but is otherwise ignored when interpreting the line.
2343 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2346 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2347 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2359 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2361 /* For now, let high level macros abort .ce mode. */
2363 if (roffce_node
!= NULL
&&
2364 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
2365 t
== ROFF_TH
|| t
== ROFF_TS
)) {
2366 r
->man
->last
= roffce_node
;
2367 r
->man
->next
= ROFF_NEXT_SIBLING
;
2373 * Fully handle known macros when they are structurally
2374 * required or when the conditional evaluated to true.
2377 if (t
== ROFF_break
) {
2378 if (irc
& ROFF_LOOPMASK
)
2379 irc
= ROFF_IGN
| ROFF_LOOPEXIT
;
2381 for (bl
= r
->last
; bl
!= NULL
; bl
= bl
->parent
) {
2383 if (bl
->tok
== ROFF_while
)
2387 } else if (t
!= TOKEN_NONE
&&
2388 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
))
2389 irc
|= (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2391 irc
|= rr
? ROFF_CONT
: ROFF_IGN
;
2396 roff_cond_text(ROFF_ARGS
)
2399 int endloop
, irc
, rr
;
2403 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2404 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2405 if (roffnode_cleanscope(r
))
2409 * If `\}' occurs on a text line with neither preceding
2410 * nor following characters, drop the line completely.
2413 ep
= buf
->buf
+ pos
;
2414 if (strcmp(ep
, "\\}") == 0)
2418 * The closing delimiter `\}' rewinds the conditional scope
2419 * but is otherwise ignored when interpreting the line.
2422 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2425 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2426 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2442 /* --- handling of numeric and conditional expressions -------------------- */
2445 * Parse a single signed integer number. Stop at the first non-digit.
2446 * If there is at least one digit, return success and advance the
2447 * parse point, else return failure and let the parse point unchanged.
2448 * Ignore overflows, treat them just like the C language.
2451 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2453 int myres
, scaled
, n
, p
;
2460 if (n
|| v
[p
] == '+')
2463 if (flags
& ROFFNUM_WHITE
)
2464 while (isspace((unsigned char)v
[p
]))
2467 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2468 *res
= 10 * *res
+ v
[p
] - '0';
2475 /* Each number may be followed by one optional scaling unit. */
2479 scaled
= *res
* 65536;
2482 scaled
= *res
* 240;
2485 scaled
= *res
* 240 / 2.54;
2496 scaled
= *res
* 10 / 3;
2502 scaled
= *res
* 6 / 25;
2509 if (flags
& ROFFNUM_SCALE
)
2517 * Evaluate a string comparison condition.
2518 * The first character is the delimiter.
2519 * Succeed if the string up to its second occurrence
2520 * matches the string up to its third occurence.
2521 * Advance the cursor after the third occurrence
2522 * or lacking that, to the end of the line.
2525 roff_evalstrcond(const char *v
, int *pos
)
2527 const char *s1
, *s2
, *s3
;
2531 s1
= v
+ *pos
; /* initial delimiter */
2532 s2
= s1
+ 1; /* for scanning the first string */
2533 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2535 if (NULL
== s3
) /* found no middle delimiter */
2538 while ('\0' != *++s3
) {
2539 if (*s2
!= *s3
) { /* mismatch */
2540 s3
= strchr(s3
, *s1
);
2543 if (*s3
== *s1
) { /* found the final delimiter */
2552 s3
= strchr(s2
, '\0');
2553 else if (*s3
!= '\0')
2560 * Evaluate an optionally negated single character, numerical,
2561 * or string condition.
2564 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2566 const char *start
, *end
;
2569 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2571 if ('!' == v
[*pos
]) {
2592 } while (v
[*pos
] == ' ');
2595 * Quirk for groff compatibility:
2596 * The horizontal tab is neither available nor unavailable.
2599 if (v
[*pos
] == '\t') {
2604 /* Printable ASCII characters are available. */
2606 if (v
[*pos
] != '\\') {
2612 switch (mandoc_escape(&end
, &start
, &len
)) {
2613 case ESCAPE_SPECIAL
:
2614 istrue
= mchars_spec2cp(start
, len
) != -1;
2616 case ESCAPE_UNICODE
:
2619 case ESCAPE_NUMBERED
:
2620 istrue
= mchars_num2char(start
, len
) != -1;
2627 return istrue
== wanttrue
;
2634 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2637 else if (v
[*pos
] == 'r')
2638 istrue
= roff_hasregn(r
, name
, sz
);
2640 deftype
= ROFFDEF_ANY
;
2641 roff_getstrn(r
, name
, sz
, &deftype
);
2644 *pos
= (name
+ sz
) - v
;
2645 return istrue
== wanttrue
;
2651 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2652 return (number
> 0) == wanttrue
;
2653 else if (*pos
== savepos
)
2654 return roff_evalstrcond(v
, pos
) == wanttrue
;
2660 roff_line_ignore(ROFF_ARGS
)
2667 roff_insec(ROFF_ARGS
)
2670 mandoc_msg(MANDOCERR_REQ_INSEC
, ln
, ppos
, "%s", roff_name
[tok
]);
2675 roff_unsupp(ROFF_ARGS
)
2678 mandoc_msg(MANDOCERR_REQ_UNSUPP
, ln
, ppos
, "%s", roff_name
[tok
]);
2683 roff_cond(ROFF_ARGS
)
2687 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2690 * An `.el' has no conditional body: it will consume the value
2691 * of the current rstack entry set in prior `ie' calls or
2694 * If we're not an `el', however, then evaluate the conditional.
2697 r
->last
->rule
= tok
== ROFF_el
?
2698 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2699 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2702 * An if-else will put the NEGATION of the current evaluated
2703 * conditional into the stack of rules.
2706 if (tok
== ROFF_ie
) {
2707 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2709 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2710 r
->rstacksz
, sizeof(int));
2712 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2715 /* If the parent has false as its rule, then so do we. */
2717 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2722 * If there is nothing on the line after the conditional,
2723 * not even whitespace, use next-line scope.
2724 * Except that .while does not support next-line scope.
2727 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2728 r
->last
->endspan
= 2;
2732 while (buf
->buf
[pos
] == ' ')
2735 /* An opening brace requests multiline scope. */
2737 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2738 r
->last
->endspan
= -1;
2740 while (buf
->buf
[pos
] == ' ')
2746 * Anything else following the conditional causes
2747 * single-line scope. Warn if the scope contains
2748 * nothing but trailing whitespace.
2751 if (buf
->buf
[pos
] == '\0')
2752 mandoc_msg(MANDOCERR_COND_EMPTY
,
2753 ln
, ppos
, "%s", roff_name
[tok
]);
2755 r
->last
->endspan
= 1;
2760 if (tok
== ROFF_while
)
2772 /* Ignore groff compatibility mode for now. */
2774 if (tok
== ROFF_ds1
)
2776 else if (tok
== ROFF_as1
)
2780 * The first word is the name of the string.
2781 * If it is empty or terminated by an escape sequence,
2782 * abort the `ds' request without defining anything.
2785 name
= string
= buf
->buf
+ pos
;
2789 namesz
= roff_getname(r
, &string
, ln
, pos
);
2790 switch (name
[namesz
]) {
2794 string
= buf
->buf
+ pos
+ namesz
;
2800 /* Read past the initial double-quote, if any. */
2804 /* The rest is the value. */
2805 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2807 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2812 * Parse a single operator, one or two characters long.
2813 * If the operator is recognized, return success and advance the
2814 * parse point, else return failure and let the parse point unchanged.
2817 roff_getop(const char *v
, int *pos
, char *res
)
2832 switch (v
[*pos
+ 1]) {
2850 switch (v
[*pos
+ 1]) {
2864 if ('=' == v
[*pos
+ 1])
2876 * Evaluate either a parenthesized numeric expression
2877 * or a single signed integer number.
2880 roff_evalpar(struct roff
*r
, int ln
,
2881 const char *v
, int *pos
, int *res
, int flags
)
2885 return roff_getnum(v
, pos
, res
, flags
);
2888 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2892 * Omission of the closing parenthesis
2893 * is an error in validation mode,
2894 * but ignored in evaluation mode.
2899 else if (NULL
== res
)
2906 * Evaluate a complete numeric expression.
2907 * Proceed left to right, there is no concept of precedence.
2910 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2911 int *pos
, int *res
, int flags
)
2913 int mypos
, operand2
;
2921 if (flags
& ROFFNUM_WHITE
)
2922 while (isspace((unsigned char)v
[*pos
]))
2925 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2929 if (flags
& ROFFNUM_WHITE
)
2930 while (isspace((unsigned char)v
[*pos
]))
2933 if ( ! roff_getop(v
, pos
, &operator))
2936 if (flags
& ROFFNUM_WHITE
)
2937 while (isspace((unsigned char)v
[*pos
]))
2940 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2943 if (flags
& ROFFNUM_WHITE
)
2944 while (isspace((unsigned char)v
[*pos
]))
2961 if (operand2
== 0) {
2962 mandoc_msg(MANDOCERR_DIVZERO
,
2970 if (operand2
== 0) {
2971 mandoc_msg(MANDOCERR_DIVZERO
,
2979 *res
= *res
< operand2
;
2982 *res
= *res
> operand2
;
2985 *res
= *res
<= operand2
;
2988 *res
= *res
>= operand2
;
2991 *res
= *res
== operand2
;
2994 *res
= *res
!= operand2
;
2997 *res
= *res
&& operand2
;
3000 *res
= *res
|| operand2
;
3003 if (operand2
< *res
)
3007 if (operand2
> *res
)
3017 /* --- register management ------------------------------------------------ */
3020 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
3022 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
3026 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
3027 int val
, char sign
, int step
)
3029 struct roffreg
*reg
;
3031 /* Search for an existing register with the same name. */
3034 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
3035 strncmp(reg
->key
.p
, name
, len
) != 0))
3039 /* Create a new register. */
3040 reg
= mandoc_malloc(sizeof(struct roffreg
));
3041 reg
->key
.p
= mandoc_strndup(name
, len
);
3045 reg
->next
= r
->regtab
;
3051 else if ('-' == sign
)
3055 if (step
!= INT_MIN
)
3060 * Handle some predefined read-only number registers.
3061 * For now, return -1 if the requested register is not predefined;
3062 * in case a predefined read-only register having the value -1
3063 * were to turn up, another special value would have to be chosen.
3066 roff_getregro(const struct roff
*r
, const char *name
)
3070 case '$': /* Number of arguments of the last macro evaluated. */
3071 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
3072 case 'A': /* ASCII approximation mode is always off. */
3074 case 'g': /* Groff compatibility mode is always on. */
3076 case 'H': /* Fixed horizontal resolution. */
3078 case 'j': /* Always adjust left margin only. */
3080 case 'T': /* Some output device is always defined. */
3082 case 'V': /* Fixed vertical resolution. */
3090 roff_getreg(struct roff
*r
, const char *name
)
3092 return roff_getregn(r
, name
, strlen(name
), '\0');
3096 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
3098 struct roffreg
*reg
;
3101 if ('.' == name
[0] && 2 == len
) {
3102 val
= roff_getregro(r
, name
+ 1);
3107 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
3108 if (len
== reg
->key
.sz
&&
3109 0 == strncmp(name
, reg
->key
.p
, len
)) {
3112 reg
->val
+= reg
->step
;
3115 reg
->val
-= reg
->step
;
3124 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
3129 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
3131 struct roffreg
*reg
;
3134 if ('.' == name
[0] && 2 == len
) {
3135 val
= roff_getregro(r
, name
+ 1);
3140 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
3141 if (len
== reg
->key
.sz
&&
3142 0 == strncmp(name
, reg
->key
.p
, len
))
3149 roff_freereg(struct roffreg
*reg
)
3151 struct roffreg
*old_reg
;
3153 while (NULL
!= reg
) {
3164 char *key
, *val
, *step
;
3169 key
= val
= buf
->buf
+ pos
;
3173 keysz
= roff_getname(r
, &val
, ln
, pos
);
3174 if (key
[keysz
] == '\\' || key
[keysz
] == '\t')
3178 if (sign
== '+' || sign
== '-')
3182 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
3186 while (isspace((unsigned char)*step
))
3188 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
3191 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
3198 struct roffreg
*reg
, **prev
;
3202 name
= cp
= buf
->buf
+ pos
;
3205 namesz
= roff_getname(r
, &cp
, ln
, pos
);
3206 name
[namesz
] = '\0';
3211 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
3223 /* --- handler functions for roff requests -------------------------------- */
3232 cp
= buf
->buf
+ pos
;
3233 while (*cp
!= '\0') {
3235 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
3236 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
3237 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3238 if (name
[namesz
] == '\\' || name
[namesz
] == '\t')
3249 /* Parse the number of lines. */
3251 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
3252 mandoc_msg(MANDOCERR_IT_NONUM
,
3253 ln
, ppos
, "%s", buf
->buf
+ 1);
3257 while (isspace((unsigned char)buf
->buf
[pos
]))
3261 * Arm the input line trap.
3262 * Special-casing "an-trap" is an ugly workaround to cope
3263 * with DocBook stupidly fiddling with man(7) internals.
3267 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3268 strcmp(buf
->buf
+ pos
, "an-trap") ?
3269 buf
->buf
+ pos
: "br");
3277 enum roff_tok t
, te
;
3284 r
->format
= MPARSE_MDOC
;
3285 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3291 r
->format
= MPARSE_MAN
;
3292 mask
= MPARSE_QUICK
;
3297 if ((r
->options
& mask
) == 0)
3298 for (t
= tok
; t
< te
; t
++)
3299 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3306 r
->man
->flags
&= ~ROFF_NONOFILL
;
3307 if (r
->tbl
== NULL
) {
3308 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "TE");
3311 if (tbl_end(r
->tbl
, 0) == 0) {
3314 buf
->buf
= mandoc_strdup(".sp");
3317 return ROFF_REPARSE
;
3328 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "T&");
3330 tbl_restart(ln
, ppos
, r
->tbl
);
3336 * Handle in-line equation delimiters.
3339 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3342 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3345 * Outside equations, look for an opening delimiter.
3346 * If we are inside an equation, we already know it is
3347 * in-line, or this function wouldn't have been called;
3348 * so look for a closing delimiter.
3351 cp1
= buf
->buf
+ pos
;
3352 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3353 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3358 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3360 /* Handle preceding text, protecting whitespace. */
3362 if (*buf
->buf
!= '\0') {
3369 * Prepare replacing the delimiter with an equation macro
3370 * and drop leading white space from the equation.
3373 if (r
->eqn
== NULL
) {
3380 /* Handle following text, protecting whitespace. */
3388 /* Do the actual replacement. */
3390 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3391 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3395 /* Toggle the in-line state of the eqn subsystem. */
3397 r
->eqn_inline
= r
->eqn
== NULL
;
3398 return ROFF_REPARSE
;
3404 struct roff_node
*n
;
3406 if (r
->man
->meta
.macroset
== MACROSET_MAN
)
3407 man_breakscope(r
->man
, ROFF_EQ
);
3408 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3409 if (ln
> r
->man
->last
->line
)
3410 n
->flags
|= NODE_LINE
;
3411 n
->eqn
= eqn_box_new();
3412 roff_node_append(r
->man
, n
);
3413 r
->man
->next
= ROFF_NEXT_SIBLING
;
3415 assert(r
->eqn
== NULL
);
3416 if (r
->last_eqn
== NULL
)
3417 r
->last_eqn
= eqn_alloc();
3419 eqn_reset(r
->last_eqn
);
3420 r
->eqn
= r
->last_eqn
;
3423 if (buf
->buf
[pos
] != '\0')
3424 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3425 ".EQ %s", buf
->buf
+ pos
);
3433 if (r
->eqn
!= NULL
) {
3437 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "EN");
3438 if (buf
->buf
[pos
] != '\0')
3439 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3440 "EN %s", buf
->buf
+ pos
);
3447 if (r
->tbl
!= NULL
) {
3448 mandoc_msg(MANDOCERR_BLK_BROKEN
, ln
, ppos
, "TS breaks TS");
3451 r
->man
->flags
|= ROFF_NONOFILL
;
3452 r
->tbl
= tbl_alloc(ppos
, ln
, r
->last_tbl
);
3453 if (r
->last_tbl
== NULL
)
3454 r
->first_tbl
= r
->tbl
;
3455 r
->last_tbl
= r
->tbl
;
3460 roff_noarg(ROFF_ARGS
)
3462 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3463 man_breakscope(r
->man
, tok
);
3464 if (tok
== ROFF_brp
)
3466 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3467 if (buf
->buf
[pos
] != '\0')
3468 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3469 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3471 r
->man
->flags
|= ROFF_NOFILL
;
3472 else if (tok
== ROFF_fi
)
3473 r
->man
->flags
&= ~ROFF_NOFILL
;
3474 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3475 r
->man
->next
= ROFF_NEXT_SIBLING
;
3480 roff_onearg(ROFF_ARGS
)
3482 struct roff_node
*n
;
3486 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3487 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3489 man_breakscope(r
->man
, tok
);
3491 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3492 r
->man
->last
= roffce_node
;
3493 r
->man
->next
= ROFF_NEXT_SIBLING
;
3496 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3499 cp
= buf
->buf
+ pos
;
3501 while (*cp
!= '\0' && *cp
!= ' ')
3506 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3507 ln
, (int)(cp
- buf
->buf
),
3508 "%s ... %s", roff_name
[tok
], cp
);
3509 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3512 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3513 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3514 roff_word_alloc(r
->man
, ln
, pos
, "1");
3515 r
->man
->last
->flags
|= NODE_NOSRC
;
3518 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3519 &roffce_lines
, 0) == 0) {
3520 mandoc_msg(MANDOCERR_CE_NONUM
,
3521 ln
, pos
, "ce %s", buf
->buf
+ pos
);
3524 if (roffce_lines
< 1) {
3525 r
->man
->last
= r
->man
->last
->parent
;
3529 roffce_node
= r
->man
->last
->parent
;
3531 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3534 n
->flags
|= NODE_LINE
;
3535 r
->man
->next
= ROFF_NEXT_SIBLING
;
3540 roff_manyarg(ROFF_ARGS
)
3542 struct roff_node
*n
;
3545 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3548 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3549 while (*ep
!= '\0' && *ep
!= ' ')
3553 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3556 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3558 r
->man
->next
= ROFF_NEXT_SIBLING
;
3565 char *oldn
, *newn
, *end
, *value
;
3566 size_t oldsz
, newsz
, valsz
;
3568 newn
= oldn
= buf
->buf
+ pos
;
3572 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3573 if (newn
[newsz
] == '\\' || newn
[newsz
] == '\t' || *oldn
== '\0')
3577 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3581 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3583 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3584 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3590 * The .break request only makes sense inside conditionals,
3591 * and that case is already handled in roff_cond_sub().
3594 roff_break(ROFF_ARGS
)
3596 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, pos
, "break");
3607 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3611 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3612 ln
, p
- buf
->buf
, "cc ... %s", p
);
3618 roff_char(ROFF_ARGS
)
3620 const char *p
, *kp
, *vp
;
3624 /* Parse the character to be replaced. */
3626 kp
= buf
->buf
+ pos
;
3628 if (*kp
== '\0' || (*kp
== '\\' &&
3629 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3630 (*p
!= ' ' && *p
!= '\0')) {
3631 mandoc_msg(MANDOCERR_CHAR_ARG
, ln
, pos
, "char %s", kp
);
3639 * If the replacement string contains a font escape sequence,
3640 * we have to restore the font at the end.
3646 while (*p
!= '\0') {
3649 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3651 case ESCAPE_FONTROMAN
:
3652 case ESCAPE_FONTITALIC
:
3653 case ESCAPE_FONTBOLD
:
3656 case ESCAPE_FONTPREV
:
3664 mandoc_msg(MANDOCERR_CHAR_FONT
,
3665 ln
, (int)(vp
- buf
->buf
), "%s", vp
);
3668 * Approximate the effect of .char using the .tr tables.
3669 * XXX In groff, .char and .tr interact differently.
3673 if (r
->xtab
== NULL
)
3674 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3675 assert((unsigned int)*kp
< 128);
3676 free(r
->xtab
[(int)*kp
].p
);
3677 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3678 "%s%s", vp
, font
? "\fP" : "");
3680 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3682 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3698 mandoc_msg(MANDOCERR_ARG_EXCESS
, ln
,
3699 (int)(p
- buf
->buf
), "ec ... %s", p
);
3708 if (buf
->buf
[pos
] != '\0')
3709 mandoc_msg(MANDOCERR_ARG_SKIP
,
3710 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3717 while (buf
->buf
[pos
] == ' ')
3726 const char *p
, *first
, *second
;
3728 enum mandoc_esc esc
;
3733 mandoc_msg(MANDOCERR_REQ_EMPTY
, ln
, ppos
, "tr");
3737 while (*p
!= '\0') {
3741 if (*first
== '\\') {
3742 esc
= mandoc_escape(&p
, NULL
, NULL
);
3743 if (esc
== ESCAPE_ERROR
) {
3744 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3745 (int)(p
- buf
->buf
), "%s", first
);
3748 fsz
= (size_t)(p
- first
);
3752 if (*second
== '\\') {
3753 esc
= mandoc_escape(&p
, NULL
, NULL
);
3754 if (esc
== ESCAPE_ERROR
) {
3755 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3756 (int)(p
- buf
->buf
), "%s", second
);
3759 ssz
= (size_t)(p
- second
);
3760 } else if (*second
== '\0') {
3761 mandoc_msg(MANDOCERR_TR_ODD
, ln
,
3762 (int)(first
- buf
->buf
), "tr %s", first
);
3768 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3773 if (r
->xtab
== NULL
)
3774 r
->xtab
= mandoc_calloc(128,
3775 sizeof(struct roffstr
));
3777 free(r
->xtab
[(int)*first
].p
);
3778 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3779 r
->xtab
[(int)*first
].sz
= ssz
;
3786 * Implementation of the .return request.
3787 * There is no need to call roff_userret() from here.
3788 * The read module will call that after rewinding the reader stack
3789 * to the place from where the current macro was called.
3792 roff_return(ROFF_ARGS
)
3794 if (r
->mstackpos
>= 0)
3795 return ROFF_IGN
| ROFF_USERRET
;
3797 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "return");
3805 char *oldn
, *newn
, *end
;
3806 size_t oldsz
, newsz
;
3809 oldn
= newn
= buf
->buf
+ pos
;
3813 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3814 if (oldn
[oldsz
] == '\\' || oldn
[oldsz
] == '\t' || *newn
== '\0')
3818 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3822 deftype
= ROFFDEF_ANY
;
3823 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3826 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3827 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3828 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3831 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3832 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3835 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3836 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3837 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3840 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3841 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3844 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3845 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3852 roff_shift(ROFF_ARGS
)
3858 if (buf
->buf
[pos
] != '\0' &&
3859 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3860 mandoc_msg(MANDOCERR_CE_NONUM
,
3861 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3864 if (r
->mstackpos
< 0) {
3865 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "shift");
3868 ctx
= r
->mstack
+ r
->mstackpos
;
3869 if (levels
> ctx
->argc
) {
3870 mandoc_msg(MANDOCERR_SHIFT
,
3871 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3876 for (i
= 0; i
< levels
; i
++)
3878 ctx
->argc
-= levels
;
3879 for (i
= 0; i
< ctx
->argc
; i
++)
3880 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3889 name
= buf
->buf
+ pos
;
3890 mandoc_msg(MANDOCERR_SO
, ln
, ppos
, "so %s", name
);
3893 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3894 * opening anything that's not in our cwd or anything beneath
3895 * it. Thus, explicitly disallow traversing up the file-system
3896 * or using absolute paths.
3899 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3900 mandoc_msg(MANDOCERR_SO_PATH
, ln
, ppos
, ".so %s", name
);
3901 buf
->sz
= mandoc_asprintf(&cp
,
3902 ".sp\nSee the file %s.\n.sp", name
) + 1;
3906 return ROFF_REPARSE
;
3913 /* --- user defined strings and macros ------------------------------------ */
3916 roff_userdef(ROFF_ARGS
)
3919 char *arg
, *ap
, *dst
, *src
;
3922 /* If the macro is empty, ignore it altogether. */
3924 if (*r
->current_string
== '\0')
3927 /* Initialize a new macro stack context. */
3929 if (++r
->mstackpos
== r
->mstacksz
) {
3930 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3931 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3934 ctx
= r
->mstack
+ r
->mstackpos
;
3940 * Collect pointers to macro argument strings,
3941 * NUL-terminating them and escaping quotes.
3944 src
= buf
->buf
+ pos
;
3945 while (*src
!= '\0') {
3946 if (ctx
->argc
== ctx
->argsz
) {
3948 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3949 ctx
->argsz
, sizeof(*ctx
->argv
));
3951 arg
= roff_getarg(r
, &src
, ln
, &pos
);
3952 sz
= 1; /* For the terminating NUL. */
3953 for (ap
= arg
; *ap
!= '\0'; ap
++)
3954 sz
+= *ap
== '"' ? 4 : 1;
3955 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3956 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3958 memcpy(dst
, "\\(dq", 4);
3967 /* Replace the macro invocation by the macro definition. */
3970 buf
->buf
= mandoc_strdup(r
->current_string
);
3971 buf
->sz
= strlen(buf
->buf
) + 1;
3974 return buf
->buf
[buf
->sz
- 2] == '\n' ?
3975 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3979 * Calling a high-level macro that was renamed with .rn.
3980 * r->current_string has already been set up by roff_parse().
3983 roff_renamed(ROFF_ARGS
)
3987 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3988 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3996 * Measure the length in bytes of the roff identifier at *cpp
3997 * and advance the pointer to the next word.
4000 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
4009 /* Advance cp to the byte after the end of the name. */
4011 for (cp
= name
; 1; cp
++) {
4015 if (*cp
== ' ' || *cp
== '\t') {
4021 if (cp
[1] == '{' || cp
[1] == '}')
4025 mandoc_msg(MANDOCERR_NAMESC
, ln
, pos
,
4026 "%.*s", (int)(cp
- name
+ 1), name
);
4027 mandoc_escape((const char **)&cp
, NULL
, NULL
);
4031 /* Read past spaces. */
4041 * Store *string into the user-defined string called *name.
4042 * To clear an existing entry, call with (*r, *name, NULL, 0).
4043 * append == 0: replace mode
4044 * append == 1: single-line append mode
4045 * append == 2: multiline append mode, append '\n' after each call
4048 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
4053 namesz
= strlen(name
);
4054 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
4055 string
? strlen(string
) : 0, append
);
4056 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
4060 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
4061 const char *string
, size_t stringsz
, int append
)
4066 size_t oldch
, newch
;
4068 /* Search for an existing string with the same name. */
4071 while (n
&& (namesz
!= n
->key
.sz
||
4072 strncmp(n
->key
.p
, name
, namesz
)))
4076 /* Create a new string table entry. */
4077 n
= mandoc_malloc(sizeof(struct roffkv
));
4078 n
->key
.p
= mandoc_strndup(name
, namesz
);
4084 } else if (0 == append
) {
4094 * One additional byte for the '\n' in multiline mode,
4095 * and one for the terminating '\0'.
4097 newch
= stringsz
+ (1 < append
? 2u : 1u);
4099 if (NULL
== n
->val
.p
) {
4100 n
->val
.p
= mandoc_malloc(newch
);
4105 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
4108 /* Skip existing content in the destination buffer. */
4109 c
= n
->val
.p
+ (int)oldch
;
4111 /* Append new content to the destination buffer. */
4113 while (i
< (int)stringsz
) {
4115 * Rudimentary roff copy mode:
4116 * Handle escaped backslashes.
4118 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
4123 /* Append terminating bytes. */
4128 n
->val
.sz
= (int)(c
- n
->val
.p
);
4132 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
4135 const struct roffkv
*n
;
4140 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
4141 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4142 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4144 if (*deftype
& ROFFDEF_USER
) {
4145 *deftype
= ROFFDEF_USER
;
4152 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
4153 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4154 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4156 if (*deftype
& ROFFDEF_REN
) {
4157 *deftype
= ROFFDEF_REN
;
4164 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
4165 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
4166 predefs
[i
].name
[len
] != '\0')
4168 if (*deftype
& ROFFDEF_PRE
) {
4169 *deftype
= ROFFDEF_PRE
;
4170 return predefs
[i
].str
;
4176 if (r
->man
->meta
.macroset
!= MACROSET_MAN
) {
4177 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
4178 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4179 roff_name
[tok
][len
] != '\0')
4181 if (*deftype
& ROFFDEF_STD
) {
4182 *deftype
= ROFFDEF_STD
;
4190 if (r
->man
->meta
.macroset
!= MACROSET_MDOC
) {
4191 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
4192 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4193 roff_name
[tok
][len
] != '\0')
4195 if (*deftype
& ROFFDEF_STD
) {
4196 *deftype
= ROFFDEF_STD
;
4205 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
4206 if (*deftype
& ROFFDEF_REN
) {
4208 * This might still be a request,
4209 * so do not treat it as undefined yet.
4211 *deftype
= ROFFDEF_UNDEF
;
4215 /* Using an undefined string defines it to be empty. */
4217 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
4218 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
4226 roff_freestr(struct roffkv
*r
)
4228 struct roffkv
*n
, *nn
;
4230 for (n
= r
; n
; n
= nn
) {
4238 /* --- accessors and utility functions ------------------------------------ */
4241 * Duplicate an input string, making the appropriate character
4242 * conversations (as stipulated by `tr') along the way.
4243 * Returns a heap-allocated string with all the replacements made.
4246 roff_strdup(const struct roff
*r
, const char *p
)
4248 const struct roffkv
*cp
;
4252 enum mandoc_esc esc
;
4254 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
4255 return mandoc_strdup(p
);
4256 else if ('\0' == *p
)
4257 return mandoc_strdup("");
4260 * Step through each character looking for term matches
4261 * (remember that a `tr' can be invoked with an escape, which is
4262 * a glyph but the escape is multi-character).
4263 * We only do this if the character hash has been initialised
4264 * and the string is >0 length.
4270 while ('\0' != *p
) {
4271 assert((unsigned int)*p
< 128);
4272 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
4273 sz
= r
->xtab
[(int)*p
].sz
;
4274 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4275 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
4279 } else if ('\\' != *p
) {
4280 res
= mandoc_realloc(res
, ssz
+ 2);
4285 /* Search for term matches. */
4286 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4287 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4292 * A match has been found.
4293 * Append the match to the array and move
4294 * forward by its keysize.
4296 res
= mandoc_realloc(res
,
4297 ssz
+ cp
->val
.sz
+ 1);
4298 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4300 p
+= (int)cp
->key
.sz
;
4305 * Handle escapes carefully: we need to copy
4306 * over just the escape itself, or else we might
4307 * do replacements within the escape itself.
4308 * Make sure to pass along the bogus string.
4311 esc
= mandoc_escape(&p
, NULL
, NULL
);
4312 if (ESCAPE_ERROR
== esc
) {
4314 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4315 memcpy(res
+ ssz
, pp
, sz
);
4319 * We bail out on bad escapes.
4320 * No need to warn: we already did so when
4321 * roff_expand() was called.
4324 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4325 memcpy(res
+ ssz
, pp
, sz
);
4329 res
[(int)ssz
] = '\0';
4334 roff_getformat(const struct roff
*r
)
4341 * Find out whether a line is a macro line or not.
4342 * If it is, adjust the current position and return one; if it isn't,
4343 * return zero and don't change the current position.
4344 * If the control character has been set with `.cc', then let that grain
4346 * This is slighly contrary to groff, where using the non-breaking
4347 * control character when `cc' has been invoked will cause the
4348 * non-breaking macro contents to be printed verbatim.
4351 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4357 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4359 else if (r
->control
!= '\0')
4361 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4363 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4368 while (' ' == cp
[pos
] || '\t' == cp
[pos
])