]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.361 2019/01/05 09:10:32 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017-2019 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>
31 #include "mandoc_aux.h"
32 #include "mandoc_ohash.h"
35 #include "mandoc_parse.h"
36 #include "libmandoc.h"
38 #include "tbl_parse.h"
39 #include "eqn_parse.h"
42 * ASCII_ESC is used to signal from roff_getarg() to roff_expand()
43 * that an escape sequence resulted from copy-in processing and
44 * needs to be checked or interpolated. As it is used nowhere
45 * else, it is defined here rather than in a header file.
49 /* Maximum number of string expansions per line, to break infinite loops. */
50 #define EXPAND_LIMIT 1000
52 /* Types of definitions of macros and strings. */
53 #define ROFFDEF_USER (1 << 1) /* User-defined. */
54 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
55 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
56 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
57 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
58 ROFFDEF_REN | ROFFDEF_STD)
59 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
61 /* --- data types --------------------------------------------------------- */
64 * An incredibly-simple string buffer.
67 char *p
; /* nil-terminated buffer */
68 size_t sz
; /* saved strlen(p) */
72 * A key-value roffstr pair as part of a singly-linked list.
77 struct roffkv
*next
; /* next in list */
81 * A single number register as part of a singly-linked list.
91 * Association of request and macro names with token IDs.
99 * A macro processing context.
100 * More than one is needed when macro calls are nested.
109 struct roff_man
*man
; /* mdoc or man parser */
110 struct roffnode
*last
; /* leaf of stack */
111 struct mctx
*mstack
; /* stack of macro contexts */
112 int *rstack
; /* stack of inverted `ie' values */
113 struct ohash
*reqtab
; /* request lookup table */
114 struct roffreg
*regtab
; /* number registers */
115 struct roffkv
*strtab
; /* user-defined strings & macros */
116 struct roffkv
*rentab
; /* renamed strings & macros */
117 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
118 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
119 const char *current_string
; /* value of last called user macro */
120 struct tbl_node
*first_tbl
; /* first table parsed */
121 struct tbl_node
*last_tbl
; /* last table parsed */
122 struct tbl_node
*tbl
; /* current table being parsed */
123 struct eqn_node
*last_eqn
; /* equation parser */
124 struct eqn_node
*eqn
; /* active equation parser */
125 int eqn_inline
; /* current equation is inline */
126 int options
; /* parse options */
127 int mstacksz
; /* current size of mstack */
128 int mstackpos
; /* position in mstack */
129 int rstacksz
; /* current size limit of rstack */
130 int rstackpos
; /* position in rstack */
131 int format
; /* current file in mdoc or man format */
132 char control
; /* control character */
133 char escape
; /* escape character */
137 enum roff_tok tok
; /* type of node */
138 struct roffnode
*parent
; /* up one in stack */
139 int line
; /* parse line */
140 int col
; /* parse col */
141 char *name
; /* node name, e.g. macro name */
142 char *end
; /* end-rules: custom token */
143 int endspan
; /* end-rules: next-line or infty */
144 int rule
; /* current evaluation rule */
147 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
148 enum roff_tok tok, /* tok of macro */ \
149 struct buf *buf, /* input buffer */ \
150 int ln, /* parse line */ \
151 int ppos, /* original pos in buffer */ \
152 int pos, /* current pos in buffer */ \
153 int *offs /* reset offset of buffer data */
155 typedef int (*roffproc
)(ROFF_ARGS
);
158 roffproc proc
; /* process new macro */
159 roffproc text
; /* process as child text of macro */
160 roffproc sub
; /* process as child of macro */
162 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
166 const char *name
; /* predefined input name */
167 const char *str
; /* replacement symbol */
170 #define PREDEF(__name, __str) \
171 { (__name), (__str) },
173 /* --- function prototypes ------------------------------------------------ */
175 static int roffnode_cleanscope(struct roff
*);
176 static int roffnode_pop(struct roff
*);
177 static void roffnode_push(struct roff
*, enum roff_tok
,
178 const char *, int, int);
179 static void roff_addtbl(struct roff_man
*, int, struct tbl_node
*);
180 static int roff_als(ROFF_ARGS
);
181 static int roff_block(ROFF_ARGS
);
182 static int roff_block_text(ROFF_ARGS
);
183 static int roff_block_sub(ROFF_ARGS
);
184 static int roff_cblock(ROFF_ARGS
);
185 static int roff_cc(ROFF_ARGS
);
186 static int roff_ccond(struct roff
*, int, int);
187 static int roff_char(ROFF_ARGS
);
188 static int roff_cond(ROFF_ARGS
);
189 static int roff_cond_text(ROFF_ARGS
);
190 static int roff_cond_sub(ROFF_ARGS
);
191 static int roff_ds(ROFF_ARGS
);
192 static int roff_ec(ROFF_ARGS
);
193 static int roff_eo(ROFF_ARGS
);
194 static int roff_eqndelim(struct roff
*, struct buf
*, int);
195 static int roff_evalcond(struct roff
*r
, int, char *, int *);
196 static int roff_evalnum(struct roff
*, int,
197 const char *, int *, int *, int);
198 static int roff_evalpar(struct roff
*, int,
199 const char *, int *, int *, int);
200 static int roff_evalstrcond(const char *, int *);
201 static int roff_expand(struct roff
*, struct buf
*,
203 static void roff_free1(struct roff
*);
204 static void roff_freereg(struct roffreg
*);
205 static void roff_freestr(struct roffkv
*);
206 static size_t roff_getname(struct roff
*, char **, int, int);
207 static int roff_getnum(const char *, int *, int *, int);
208 static int roff_getop(const char *, int *, char *);
209 static int roff_getregn(struct roff
*,
210 const char *, size_t, char);
211 static int roff_getregro(const struct roff
*,
213 static const char *roff_getstrn(struct roff
*,
214 const char *, size_t, int *);
215 static int roff_hasregn(const struct roff
*,
216 const char *, size_t);
217 static int roff_insec(ROFF_ARGS
);
218 static int roff_it(ROFF_ARGS
);
219 static int roff_line_ignore(ROFF_ARGS
);
220 static void roff_man_alloc1(struct roff_man
*);
221 static void roff_man_free1(struct roff_man
*);
222 static int roff_manyarg(ROFF_ARGS
);
223 static int roff_noarg(ROFF_ARGS
);
224 static int roff_nop(ROFF_ARGS
);
225 static int roff_nr(ROFF_ARGS
);
226 static int roff_onearg(ROFF_ARGS
);
227 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
229 static int roff_parsetext(struct roff
*, struct buf
*,
231 static int roff_renamed(ROFF_ARGS
);
232 static int roff_return(ROFF_ARGS
);
233 static int roff_rm(ROFF_ARGS
);
234 static int roff_rn(ROFF_ARGS
);
235 static int roff_rr(ROFF_ARGS
);
236 static void roff_setregn(struct roff
*, const char *,
237 size_t, int, char, int);
238 static void roff_setstr(struct roff
*,
239 const char *, const char *, int);
240 static void roff_setstrn(struct roffkv
**, const char *,
241 size_t, const char *, size_t, int);
242 static int roff_shift(ROFF_ARGS
);
243 static int roff_so(ROFF_ARGS
);
244 static int roff_tr(ROFF_ARGS
);
245 static int roff_Dd(ROFF_ARGS
);
246 static int roff_TE(ROFF_ARGS
);
247 static int roff_TS(ROFF_ARGS
);
248 static int roff_EQ(ROFF_ARGS
);
249 static int roff_EN(ROFF_ARGS
);
250 static int roff_T_(ROFF_ARGS
);
251 static int roff_unsupp(ROFF_ARGS
);
252 static int roff_userdef(ROFF_ARGS
);
254 /* --- constant data ------------------------------------------------------ */
256 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
257 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
259 const char *__roff_name
[MAN_MAX
+ 1] = {
260 "br", "ce", "fi", "ft",
264 "ab", "ad", "af", "aln",
265 "als", "am", "am1", "ami",
266 "ami1", "as", "as1", "asciify",
267 "backtrace", "bd", "bleedat", "blm",
268 "box", "boxa", "bp", "BP",
269 "break", "breakchar", "brnl", "brp",
271 "cf", "cflags", "ch", "char",
272 "chop", "class", "close", "CL",
273 "color", "composite", "continue", "cp",
274 "cropat", "cs", "cu", "da",
275 "dch", "Dd", "de", "de1",
276 "defcolor", "dei", "dei1", "device",
277 "devicem", "di", "do", "ds",
278 "ds1", "dwh", "dt", "ec",
279 "ecr", "ecs", "el", "em",
280 "EN", "eo", "EP", "EQ",
281 "errprint", "ev", "evc", "ex",
282 "fallback", "fam", "fc", "fchar",
283 "fcolor", "fdeferlig", "feature", "fkern",
284 "fl", "flig", "fp", "fps",
285 "fschar", "fspacewidth", "fspecial", "ftr",
286 "fzoom", "gcolor", "hc", "hcode",
287 "hidechar", "hla", "hlm", "hpf",
288 "hpfa", "hpfcode", "hw", "hy",
289 "hylang", "hylen", "hym", "hypp",
290 "hys", "ie", "if", "ig",
291 "index", "it", "itc", "IX",
292 "kern", "kernafter", "kernbefore", "kernpair",
293 "lc", "lc_ctype", "lds", "length",
294 "letadj", "lf", "lg", "lhang",
295 "linetabs", "lnr", "lnrf", "lpfx",
297 "mediasize", "minss", "mk", "mso",
298 "na", "ne", "nh", "nhychar",
299 "nm", "nn", "nop", "nr",
300 "nrf", "nroff", "ns", "nx",
301 "open", "opena", "os", "output",
302 "padj", "papersize", "pc", "pev",
303 "pi", "PI", "pl", "pm",
305 "psbb", "pshape", "pso", "ptr",
306 "pvs", "rchar", "rd", "recursionlimit",
307 "return", "rfschar", "rhang",
308 "rm", "rn", "rnn", "rr",
309 "rs", "rt", "schar", "sentchar",
310 "shc", "shift", "sizes", "so",
311 "spacewidth", "special", "spreadwarn", "ss",
312 "sty", "substring", "sv", "sy",
315 "tm", "tm1", "tmc", "tr",
316 "track", "transchar", "trf", "trimat",
317 "trin", "trnt", "troff", "TS",
318 "uf", "ul", "unformat", "unwatch",
319 "unwatchn", "vpt", "vs", "warn",
320 "warnscale", "watch", "watchlength", "watchn",
321 "wh", "while", "write", "writec",
322 "writem", "xflag", ".", NULL
,
324 "Dd", "Dt", "Os", "Sh",
325 "Ss", "Pp", "D1", "Dl",
326 "Bd", "Ed", "Bl", "El",
327 "It", "Ad", "An", "Ap",
328 "Ar", "Cd", "Cm", "Dv",
329 "Er", "Ev", "Ex", "Fa",
330 "Fd", "Fl", "Fn", "Ft",
331 "Ic", "In", "Li", "Nd",
332 "Nm", "Op", "Ot", "Pa",
333 "Rv", "St", "Va", "Vt",
334 "Xr", "%A", "%B", "%D",
335 "%I", "%J", "%N", "%O",
336 "%P", "%R", "%T", "%V",
337 "Ac", "Ao", "Aq", "At",
338 "Bc", "Bf", "Bo", "Bq",
339 "Bsx", "Bx", "Db", "Dc",
340 "Do", "Dq", "Ec", "Ef",
341 "Em", "Eo", "Fx", "Ms",
342 "No", "Ns", "Nx", "Ox",
343 "Pc", "Pf", "Po", "Pq",
344 "Qc", "Ql", "Qo", "Qq",
345 "Re", "Rs", "Sc", "So",
346 "Sq", "Sm", "Sx", "Sy",
347 "Tn", "Ux", "Xc", "Xo",
348 "Fo", "Fc", "Oo", "Oc",
349 "Bk", "Ek", "Bt", "Hf",
350 "Fr", "Ud", "Lb", "Lp",
351 "Lk", "Mt", "Brq", "Bro",
352 "Brc", "%C", "Es", "En",
353 "Dx", "%Q", "%U", "Ta",
355 "TH", "SH", "SS", "TP",
357 "LP", "PP", "P", "IP",
358 "HP", "SM", "SB", "BI",
359 "IB", "BR", "RB", "R",
360 "B", "I", "IR", "RI",
361 "RE", "RS", "DT", "UC",
365 "UE", "MT", "ME", NULL
367 const char *const *roff_name
= __roff_name
;
369 static struct roffmac roffs
[TOKEN_NONE
] = {
370 { roff_noarg
, NULL
, NULL
, 0 }, /* br */
371 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
372 { roff_noarg
, NULL
, NULL
, 0 }, /* fi */
373 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
374 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
375 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
376 { roff_noarg
, NULL
, NULL
, 0 }, /* nf */
377 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
378 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
379 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
380 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
381 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
382 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
383 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
385 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
386 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
387 { roff_als
, NULL
, NULL
, 0 }, /* als */
388 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
389 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
390 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
391 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
392 { roff_ds
, NULL
, NULL
, 0 }, /* as */
393 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
394 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
395 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
396 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
397 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
398 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
399 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
400 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
401 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
402 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
403 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
404 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
405 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
406 { roff_noarg
, NULL
, NULL
, 0 }, /* brp */
407 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
409 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
410 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
412 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
413 { roff_char
, NULL
, NULL
, 0 }, /* char */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
415 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
416 { roff_insec
, NULL
, NULL
, 0 }, /* close */
417 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
419 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
420 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
421 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
422 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
423 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
425 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
426 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
427 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
428 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
429 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
430 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
431 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
432 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
433 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
434 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
435 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
436 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
437 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
438 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
441 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
442 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
443 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
444 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
445 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
446 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
447 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
448 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
449 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
451 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
452 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
453 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
454 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
456 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
457 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
458 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
466 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
475 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
476 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
477 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
478 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
479 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
487 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
488 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
489 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
490 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
491 { roff_it
, NULL
, NULL
, 0 }, /* it */
492 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
494 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
495 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
496 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
497 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
498 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
499 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
500 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
501 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
503 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
504 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
506 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
507 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
508 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
509 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
510 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
511 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
512 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
513 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
515 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
516 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
517 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
518 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
519 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
521 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
522 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
523 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
524 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
525 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
526 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
528 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
529 { roff_insec
, NULL
, NULL
, 0 }, /* open */
530 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
532 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
534 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
536 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
537 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
538 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
540 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
541 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
542 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
543 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
544 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
545 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
546 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
549 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
550 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
552 { roff_return
, NULL
, NULL
, 0 }, /* return */
553 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
555 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
556 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
557 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
558 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
561 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
562 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
563 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
564 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
565 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
566 { roff_so
, NULL
, NULL
, 0 }, /* so */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
572 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
574 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
575 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
576 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
577 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
578 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
579 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
580 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
581 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
582 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
583 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
584 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
587 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
589 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
590 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
592 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
593 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
594 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
595 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
596 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
597 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
598 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
599 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
600 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
601 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
602 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
603 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
604 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
605 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
606 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
607 { roff_insec
, NULL
, NULL
, 0 }, /* write */
608 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
609 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
610 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
611 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
612 { roff_renamed
, NULL
, NULL
, 0 },
613 { roff_userdef
, NULL
, NULL
, 0 }
616 /* Array of injected predefined strings. */
617 #define PREDEFS_MAX 38
618 static const struct predef predefs
[PREDEFS_MAX
] = {
619 #include "predefs.in"
622 static int roffce_lines
; /* number of input lines to center */
623 static struct roff_node
*roffce_node
; /* active request */
624 static int roffit_lines
; /* number of lines to delay */
625 static char *roffit_macro
; /* nil-terminated macro line */
628 /* --- request table ------------------------------------------------------ */
631 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
639 htab
= mandoc_malloc(sizeof(*htab
));
640 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
642 for (tok
= mintok
; tok
< maxtok
; tok
++) {
643 if (roff_name
[tok
] == NULL
)
645 sz
= strlen(roff_name
[tok
]);
646 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
648 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
649 slot
= ohash_qlookup(htab
, req
->name
);
650 ohash_insert(htab
, slot
, req
);
656 roffhash_free(struct ohash
*htab
)
663 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
664 req
= ohash_next(htab
, &slot
))
671 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
678 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
680 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
681 return req
== NULL
? TOKEN_NONE
: req
->tok
;
684 /* --- stack of request blocks -------------------------------------------- */
687 * Pop the current node off of the stack of roff instructions currently
691 roffnode_pop(struct roff
*r
)
697 inloop
= p
->tok
== ROFF_while
;
706 * Push a roff node onto the instruction stack. This must later be
707 * removed with roffnode_pop().
710 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
715 p
= mandoc_calloc(1, sizeof(struct roffnode
));
718 p
->name
= mandoc_strdup(name
);
722 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
727 /* --- roff parser state data management ---------------------------------- */
730 roff_free1(struct roff
*r
)
734 tbl_free(r
->first_tbl
);
735 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
737 eqn_free(r
->last_eqn
);
738 r
->last_eqn
= r
->eqn
= NULL
;
740 while (r
->mstackpos
>= 0)
751 roff_freereg(r
->regtab
);
754 roff_freestr(r
->strtab
);
755 roff_freestr(r
->rentab
);
756 roff_freestr(r
->xmbtab
);
757 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
760 for (i
= 0; i
< 128; i
++)
767 roff_reset(struct roff
*r
)
770 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
780 roff_free(struct roff
*r
)
785 for (i
= 0; i
< r
->mstacksz
; i
++)
786 free(r
->mstack
[i
].argv
);
788 roffhash_free(r
->reqtab
);
793 roff_alloc(int options
)
797 r
= mandoc_calloc(1, sizeof(struct roff
));
798 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
799 r
->options
= options
;
800 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
807 /* --- syntax tree state data management ---------------------------------- */
810 roff_man_free1(struct roff_man
*man
)
812 if (man
->meta
.first
!= NULL
)
813 roff_node_delete(man
, man
->meta
.first
);
814 free(man
->meta
.msec
);
817 free(man
->meta
.arch
);
818 free(man
->meta
.title
);
819 free(man
->meta
.name
);
820 free(man
->meta
.date
);
821 free(man
->meta
.sodest
);
825 roff_state_reset(struct roff_man
*man
)
827 man
->last
= man
->meta
.first
;
830 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
831 man
->next
= ROFF_NEXT_CHILD
;
832 roff_setreg(man
->roff
, "nS", 0, '=');
836 roff_man_alloc1(struct roff_man
*man
)
838 memset(&man
->meta
, 0, sizeof(man
->meta
));
839 man
->meta
.first
= mandoc_calloc(1, sizeof(*man
->meta
.first
));
840 man
->meta
.first
->type
= ROFFT_ROOT
;
841 man
->meta
.macroset
= MACROSET_NONE
;
842 roff_state_reset(man
);
846 roff_man_reset(struct roff_man
*man
)
849 roff_man_alloc1(man
);
853 roff_man_free(struct roff_man
*man
)
860 roff_man_alloc(struct roff
*roff
, const char *os_s
, int quick
)
862 struct roff_man
*man
;
864 man
= mandoc_calloc(1, sizeof(*man
));
868 roff_man_alloc1(man
);
873 /* --- syntax tree handling ----------------------------------------------- */
876 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
877 enum roff_type type
, int tok
)
881 n
= mandoc_calloc(1, sizeof(*n
));
886 n
->sec
= man
->lastsec
;
888 if (man
->flags
& MDOC_SYNOPSIS
)
889 n
->flags
|= NODE_SYNPRETTY
;
891 n
->flags
&= ~NODE_SYNPRETTY
;
892 if ((man
->flags
& (ROFF_NOFILL
| ROFF_NONOFILL
)) == ROFF_NOFILL
)
893 n
->flags
|= NODE_NOFILL
;
895 n
->flags
&= ~NODE_NOFILL
;
896 if (man
->flags
& MDOC_NEWLINE
)
897 n
->flags
|= NODE_LINE
;
898 man
->flags
&= ~MDOC_NEWLINE
;
904 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
908 case ROFF_NEXT_SIBLING
:
909 if (man
->last
->next
!= NULL
) {
910 n
->next
= man
->last
->next
;
911 man
->last
->next
->prev
= n
;
913 man
->last
->parent
->last
= n
;
916 n
->parent
= man
->last
->parent
;
918 case ROFF_NEXT_CHILD
:
919 if (man
->last
->child
!= NULL
) {
920 n
->next
= man
->last
->child
;
921 man
->last
->child
->prev
= n
;
924 man
->last
->child
= n
;
925 n
->parent
= man
->last
;
937 if (n
->end
!= ENDBODY_NOT
)
949 * Copy over the normalised-data pointer of our parent. Not
950 * everybody has one, but copying a null pointer is fine.
953 n
->norm
= n
->parent
->norm
;
954 assert(n
->parent
->type
== ROFFT_BLOCK
);
958 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
962 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
963 n
->string
= roff_strdup(man
->roff
, word
);
964 roff_node_append(man
, n
);
965 n
->flags
|= NODE_VALID
| NODE_ENDED
;
966 man
->next
= ROFF_NEXT_SIBLING
;
970 roff_word_append(struct roff_man
*man
, const char *word
)
973 char *addstr
, *newstr
;
976 addstr
= roff_strdup(man
->roff
, word
);
977 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
981 man
->next
= ROFF_NEXT_SIBLING
;
985 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
989 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
990 roff_node_append(man
, n
);
991 man
->next
= ROFF_NEXT_CHILD
;
995 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
999 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1000 roff_node_append(man
, n
);
1001 man
->next
= ROFF_NEXT_CHILD
;
1006 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1008 struct roff_node
*n
;
1010 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1011 roff_node_append(man
, n
);
1012 man
->next
= ROFF_NEXT_CHILD
;
1017 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1019 struct roff_node
*n
;
1021 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1022 roff_node_append(man
, n
);
1023 man
->next
= ROFF_NEXT_CHILD
;
1028 roff_addtbl(struct roff_man
*man
, int line
, struct tbl_node
*tbl
)
1030 struct roff_node
*n
;
1031 struct tbl_span
*span
;
1033 if (man
->meta
.macroset
== MACROSET_MAN
)
1034 man_breakscope(man
, ROFF_TS
);
1035 while ((span
= tbl_span(tbl
)) != NULL
) {
1036 n
= roff_node_alloc(man
, line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1038 roff_node_append(man
, n
);
1039 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1040 man
->next
= ROFF_NEXT_SIBLING
;
1045 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1048 /* Adjust siblings. */
1051 n
->prev
->next
= n
->next
;
1053 n
->next
->prev
= n
->prev
;
1055 /* Adjust parent. */
1057 if (n
->parent
!= NULL
) {
1058 if (n
->parent
->child
== n
)
1059 n
->parent
->child
= n
->next
;
1060 if (n
->parent
->last
== n
)
1061 n
->parent
->last
= n
->prev
;
1064 /* Adjust parse point. */
1068 if (man
->last
== n
) {
1069 if (n
->prev
== NULL
) {
1070 man
->last
= n
->parent
;
1071 man
->next
= ROFF_NEXT_CHILD
;
1073 man
->last
= n
->prev
;
1074 man
->next
= ROFF_NEXT_SIBLING
;
1077 if (man
->meta
.first
== n
)
1078 man
->meta
.first
= NULL
;
1082 roff_node_relink(struct roff_man
*man
, struct roff_node
*n
)
1084 roff_node_unlink(man
, n
);
1085 n
->prev
= n
->next
= NULL
;
1086 roff_node_append(man
, n
);
1090 roff_node_free(struct roff_node
*n
)
1093 if (n
->args
!= NULL
)
1094 mdoc_argv_free(n
->args
);
1095 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1097 eqn_box_free(n
->eqn
);
1103 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1106 while (n
->child
!= NULL
)
1107 roff_node_delete(man
, n
->child
);
1108 roff_node_unlink(man
, n
);
1113 deroff(char **dest
, const struct roff_node
*n
)
1118 if (n
->type
!= ROFFT_TEXT
) {
1119 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1124 /* Skip leading whitespace. */
1126 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1127 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1128 strchr(" %&0^|~", cp
[1]) != NULL
)
1130 else if ( ! isspace((unsigned char)*cp
))
1134 /* Skip trailing backslash. */
1137 if (sz
> 0 && cp
[sz
- 1] == '\\')
1140 /* Skip trailing whitespace. */
1143 if ( ! isspace((unsigned char)cp
[sz
-1]))
1146 /* Skip empty strings. */
1151 if (*dest
== NULL
) {
1152 *dest
= mandoc_strndup(cp
, sz
);
1156 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1161 /* --- main functions of the roff parser ---------------------------------- */
1164 * In the current line, expand escape sequences that produce parsable
1165 * input text. Also check the syntax of the remaining escape sequences,
1166 * which typically produce output glyphs or change formatter state.
1169 roff_expand(struct roff
*r
, struct buf
*buf
, int ln
, int pos
, char newesc
)
1171 struct mctx
*ctx
; /* current macro call context */
1172 char ubuf
[24]; /* buffer to print the number */
1173 struct roff_node
*n
; /* used for header comments */
1174 const char *start
; /* start of the string to process */
1175 char *stesc
; /* start of an escape sequence ('\\') */
1176 const char *esct
; /* type of esccape sequence */
1177 char *ep
; /* end of comment string */
1178 const char *stnam
; /* start of the name, after "[(*" */
1179 const char *cp
; /* end of the name, e.g. before ']' */
1180 const char *res
; /* the string to be substituted */
1181 char *nbuf
; /* new buffer to copy buf->buf to */
1182 size_t maxl
; /* expected length of the escape name */
1183 size_t naml
; /* actual length of the escape name */
1184 size_t asz
; /* length of the replacement */
1185 size_t rsz
; /* length of the rest of the string */
1186 int inaml
; /* length returned from mandoc_escape() */
1187 int expand_count
; /* to avoid infinite loops */
1188 int npos
; /* position in numeric expression */
1189 int arg_complete
; /* argument not interrupted by eol */
1190 int quote_args
; /* true for \\$@, false for \\$* */
1191 int done
; /* no more input available */
1192 int deftype
; /* type of definition to paste */
1193 int rcsid
; /* kind of RCS id seen */
1194 enum mandocerr err
; /* for escape sequence problems */
1195 char sign
; /* increment number register */
1196 char term
; /* character terminating the escape */
1198 /* Search forward for comments. */
1201 start
= buf
->buf
+ pos
;
1202 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1203 if (stesc
[0] != newesc
|| stesc
[1] == '\0')
1206 if (*stesc
!= '"' && *stesc
!= '#')
1209 /* Comment found, look for RCS id. */
1212 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1213 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1215 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1216 rcsid
= 1 << MANDOC_OS_NETBSD
;
1220 isalnum((unsigned char)*cp
) == 0 &&
1221 strchr(cp
, '$') != NULL
) {
1222 if (r
->man
->meta
.rcsids
& rcsid
)
1223 mandoc_msg(MANDOCERR_RCS_REP
, ln
,
1224 (int)(stesc
- buf
->buf
) + 1,
1226 r
->man
->meta
.rcsids
|= rcsid
;
1229 /* Handle trailing whitespace. */
1231 ep
= strchr(stesc
--, '\0') - 1;
1236 if (*ep
== ' ' || *ep
== '\t')
1237 mandoc_msg(MANDOCERR_SPACE_EOL
,
1238 ln
, (int)(ep
- buf
->buf
), NULL
);
1241 * Save comments preceding the title macro
1242 * in the syntax tree.
1245 if (newesc
!= ASCII_ESC
&& r
->format
== 0) {
1246 while (*ep
== ' ' || *ep
== '\t')
1249 n
= roff_node_alloc(r
->man
,
1250 ln
, stesc
+ 1 - buf
->buf
,
1251 ROFFT_COMMENT
, TOKEN_NONE
);
1252 n
->string
= mandoc_strdup(stesc
+ 2);
1253 roff_node_append(r
->man
, n
);
1254 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1255 r
->man
->next
= ROFF_NEXT_SIBLING
;
1258 /* Line continuation with comment. */
1260 if (stesc
[1] == '#') {
1262 return ROFF_IGN
| ROFF_APPEND
;
1265 /* Discard normal comments. */
1267 while (stesc
> start
&& stesc
[-1] == ' ' &&
1268 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1277 /* Notice the end of the input. */
1279 if (*stesc
== '\n') {
1285 while (stesc
>= start
) {
1286 if (*stesc
!= newesc
) {
1289 * If we have a non-standard escape character,
1290 * escape literal backslashes because all
1291 * processing in subsequent functions uses
1292 * the standard escaping rules.
1295 if (newesc
!= ASCII_ESC
&& *stesc
== '\\') {
1297 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1298 buf
->buf
, stesc
+ 1) + 1;
1300 stesc
= nbuf
+ (stesc
- buf
->buf
);
1305 /* Search backwards for the next escape. */
1311 /* If it is escaped, skip it. */
1313 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1314 if (*cp
!= r
->escape
)
1317 if ((stesc
- cp
) % 2 == 0) {
1321 } else if (stesc
[1] != '\0') {
1328 return ROFF_IGN
| ROFF_APPEND
;
1331 /* Decide whether to expand or to check only. */
1349 if (sign
== '+' || sign
== '-')
1355 switch(mandoc_escape(&cp
, &stnam
, &inaml
)) {
1356 case ESCAPE_SPECIAL
:
1357 if (mchars_spec2cp(stnam
, inaml
) >= 0)
1361 err
= MANDOCERR_ESC_BAD
;
1364 err
= MANDOCERR_ESC_UNDEF
;
1367 err
= MANDOCERR_ESC_UNSUPP
;
1372 if (err
!= MANDOCERR_OK
)
1373 mandoc_msg(err
, ln
, (int)(stesc
- buf
->buf
),
1374 "%.*s", (int)(cp
- stesc
), stesc
);
1379 if (EXPAND_LIMIT
< ++expand_count
) {
1380 mandoc_msg(MANDOCERR_ROFFLOOP
,
1381 ln
, (int)(stesc
- buf
->buf
), NULL
);
1386 * The third character decides the length
1387 * of the name of the string or register.
1388 * Save a pointer to the name.
1415 /* Advance to the end of the name. */
1419 while (maxl
== 0 || naml
< maxl
) {
1421 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
1422 (int)(stesc
- buf
->buf
), "%s", stesc
);
1426 if (maxl
== 0 && *cp
== term
) {
1430 if (*cp
++ != '\\' || *esct
!= 'w') {
1434 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1435 case ESCAPE_SPECIAL
:
1436 case ESCAPE_UNICODE
:
1437 case ESCAPE_NUMBERED
:
1439 case ESCAPE_OVERSTRIKE
:
1448 * Retrieve the replacement string; if it is
1449 * undefined, resume searching for escapes.
1455 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1456 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1459 * If not overriden, let \*(.T
1460 * through to the formatters.
1463 if (res
== NULL
&& naml
== 2 &&
1464 stnam
[0] == '.' && stnam
[1] == 'T') {
1465 roff_setstrn(&r
->strtab
,
1466 ".T", 2, NULL
, 0, 0);
1473 if (r
->mstackpos
< 0) {
1474 mandoc_msg(MANDOCERR_ARG_UNDEF
, ln
,
1475 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1478 ctx
= r
->mstack
+ r
->mstackpos
;
1479 npos
= esct
[1] - '1';
1480 if (npos
>= 0 && npos
<= 8) {
1481 res
= npos
< ctx
->argc
?
1482 ctx
->argv
[npos
] : "";
1487 else if (esct
[1] == '@')
1490 mandoc_msg(MANDOCERR_ARG_NONUM
, ln
,
1491 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1495 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1499 asz
+= 2; /* quotes */
1500 asz
+= strlen(ctx
->argv
[npos
]);
1503 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1505 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1507 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1509 stesc
= nbuf
+ (stesc
- buf
->buf
);
1512 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1514 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1519 cp
= ctx
->argv
[npos
];
1528 ubuf
[0] = arg_complete
&&
1529 roff_evalnum(r
, ln
, stnam
, &npos
,
1530 NULL
, ROFFNUM_SCALE
) &&
1531 stnam
+ npos
+ 1 == cp
? '1' : '0';
1536 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1537 roff_getregn(r
, stnam
, naml
, sign
));
1542 /* use even incomplete args */
1543 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1550 mandoc_msg(MANDOCERR_STR_UNDEF
,
1551 ln
, (int)(stesc
- buf
->buf
),
1552 "%.*s", (int)naml
, stnam
);
1554 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1555 mandoc_msg(MANDOCERR_ROFFLOOP
,
1556 ln
, (int)(stesc
- buf
->buf
), NULL
);
1560 /* Replace the escape sequence by the string. */
1563 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1564 buf
->buf
, res
, cp
) + 1;
1566 /* Prepare for the next replacement. */
1569 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1577 * Parse a quoted or unquoted roff-style request or macro argument.
1578 * Return a pointer to the parsed argument, which is either the original
1579 * pointer or advanced by one byte in case the argument is quoted.
1580 * NUL-terminate the argument in place.
1581 * Collapse pairs of quotes inside quoted arguments.
1582 * Advance the argument pointer to the next argument,
1583 * or to the NUL byte terminating the argument line.
1586 roff_getarg(struct roff
*r
, char **cpp
, int ln
, int *pos
)
1590 int newesc
, pairs
, quoted
, white
;
1592 /* Quoting can only start with a new word. */
1595 if ('"' == *start
) {
1600 newesc
= pairs
= white
= 0;
1601 for (cp
= start
; '\0' != *cp
; cp
++) {
1604 * Move the following text left
1605 * after quoted quotes and after "\\" and "\t".
1610 if ('\\' == cp
[0]) {
1612 * In copy mode, translate double to single
1613 * backslashes and backslash-t to literal tabs.
1624 cp
[-pairs
] = ASCII_ESC
;
1629 /* Skip escaped blanks. */
1636 } else if (0 == quoted
) {
1638 /* Unescaped blanks end unquoted args. */
1642 } else if ('"' == cp
[0]) {
1644 /* Quoted quotes collapse. */
1648 /* Unquoted quotes end quoted args. */
1655 /* Quoted argument without a closing quote. */
1657 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
1659 /* NUL-terminate this argument and move to the next one. */
1667 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
1670 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
1671 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
1673 start
= mandoc_strdup(start
);
1678 buf
.sz
= strlen(start
) + 1;
1680 if (roff_expand(r
, &buf
, ln
, 0, ASCII_ESC
) & ROFF_IGN
) {
1682 buf
.buf
= mandoc_strdup("");
1689 * Process text streams.
1692 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1698 enum mandoc_esc esc
;
1700 /* Spring the input line trap. */
1702 if (roffit_lines
== 1) {
1703 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1710 return ROFF_REPARSE
;
1711 } else if (roffit_lines
> 1)
1714 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1715 if (roffce_lines
< 1) {
1716 r
->man
->last
= roffce_node
;
1717 r
->man
->next
= ROFF_NEXT_SIBLING
;
1724 /* Convert all breakable hyphens into ASCII_HYPH. */
1726 start
= p
= buf
->buf
+ pos
;
1728 while (*p
!= '\0') {
1729 sz
= strcspn(p
, "-\\");
1736 /* Skip over escapes. */
1738 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1739 if (esc
== ESCAPE_ERROR
)
1744 } else if (p
== start
) {
1749 if (isalpha((unsigned char)p
[-1]) &&
1750 isalpha((unsigned char)p
[1]))
1758 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1762 int pos
; /* parse point */
1763 int spos
; /* saved parse point for messages */
1764 int ppos
; /* original offset in buf->buf */
1765 int ctl
; /* macro line (boolean) */
1769 /* Handle in-line equation delimiters. */
1771 if (r
->tbl
== NULL
&&
1772 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1773 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1774 e
= roff_eqndelim(r
, buf
, pos
);
1775 if (e
== ROFF_REPARSE
)
1777 assert(e
== ROFF_CONT
);
1780 /* Expand some escape sequences. */
1782 e
= roff_expand(r
, buf
, ln
, pos
, r
->escape
);
1783 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1785 assert(e
== ROFF_CONT
);
1787 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1790 * First, if a scope is open and we're not a macro, pass the
1791 * text through the macro's filter.
1792 * Equations process all content themselves.
1793 * Tables process almost all content themselves, but we want
1794 * to warn about macros before passing it there.
1797 if (r
->last
!= NULL
&& ! ctl
) {
1799 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1800 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1805 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1806 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1809 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1810 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1811 roff_addtbl(r
->man
, ln
, r
->tbl
);
1815 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1817 /* Skip empty request lines. */
1819 if (buf
->buf
[pos
] == '"') {
1820 mandoc_msg(MANDOCERR_COMMENT_BAD
, ln
, pos
, NULL
);
1822 } else if (buf
->buf
[pos
] == '\0')
1826 * If a scope is open, go to the child handler for that macro,
1827 * as it may want to preprocess before doing anything with it.
1828 * Don't do so if an equation is open.
1833 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1836 /* No scope is open. This is a new request or macro. */
1839 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1841 /* Tables ignore most macros. */
1843 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1844 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1845 mandoc_msg(MANDOCERR_TBLMACRO
,
1846 ln
, pos
, "%s", buf
->buf
+ spos
);
1847 if (t
!= TOKEN_NONE
)
1849 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1851 while (buf
->buf
[pos
] == ' ')
1853 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1854 roff_addtbl(r
->man
, ln
, r
->tbl
);
1858 /* For now, let high level macros abort .ce mode. */
1860 if (ctl
&& roffce_node
!= NULL
&&
1861 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1862 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1863 r
->man
->last
= roffce_node
;
1864 r
->man
->next
= ROFF_NEXT_SIBLING
;
1870 * This is neither a roff request nor a user-defined macro.
1871 * Let the standard macro set parsers handle it.
1874 if (t
== TOKEN_NONE
)
1877 /* Execute a roff request or a user defined macro. */
1879 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1883 * Internal interface function to tell the roff parser that execution
1884 * of the current macro ended. This is required because macro
1885 * definitions usually do not end with a .return request.
1888 roff_userret(struct roff
*r
)
1893 assert(r
->mstackpos
>= 0);
1894 ctx
= r
->mstack
+ r
->mstackpos
;
1895 for (i
= 0; i
< ctx
->argc
; i
++)
1902 roff_endparse(struct roff
*r
)
1904 if (r
->last
!= NULL
)
1905 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->last
->line
,
1906 r
->last
->col
, "%s", roff_name
[r
->last
->tok
]);
1908 if (r
->eqn
!= NULL
) {
1909 mandoc_msg(MANDOCERR_BLK_NOEND
,
1910 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1915 if (r
->tbl
!= NULL
) {
1922 * Parse a roff node's type from the input buffer. This must be in the
1923 * form of ".foo xxx" in the usual way.
1925 static enum roff_tok
1926 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1936 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1940 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1942 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1943 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1952 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1955 if (t
!= TOKEN_NONE
)
1957 else if (deftype
== ROFFDEF_UNDEF
) {
1958 /* Using an undefined macro defines it to be empty. */
1959 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1960 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1965 /* --- handling of request blocks ----------------------------------------- */
1968 roff_cblock(ROFF_ARGS
)
1972 * A block-close `..' should only be invoked as a child of an
1973 * ignore macro, otherwise raise a warning and just ignore it.
1976 if (r
->last
== NULL
) {
1977 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
1981 switch (r
->last
->tok
) {
1983 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1986 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1991 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
1995 if (buf
->buf
[pos
] != '\0')
1996 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
1997 ".. %s", buf
->buf
+ pos
);
2000 roffnode_cleanscope(r
);
2006 roffnode_cleanscope(struct roff
*r
)
2011 while (r
->last
!= NULL
) {
2012 if (--r
->last
->endspan
!= 0)
2014 inloop
+= roffnode_pop(r
);
2020 roff_ccond(struct roff
*r
, int ln
, int ppos
)
2022 if (NULL
== r
->last
) {
2023 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2027 switch (r
->last
->tok
) {
2034 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2038 if (r
->last
->endspan
> -1) {
2039 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2043 return roffnode_pop(r
) + roffnode_cleanscope(r
);
2047 roff_block(ROFF_ARGS
)
2049 const char *name
, *value
;
2050 char *call
, *cp
, *iname
, *rname
;
2051 size_t csz
, namesz
, rsz
;
2054 /* Ignore groff compatibility mode for now. */
2056 if (tok
== ROFF_de1
)
2058 else if (tok
== ROFF_dei1
)
2060 else if (tok
== ROFF_am1
)
2062 else if (tok
== ROFF_ami1
)
2065 /* Parse the macro name argument. */
2067 cp
= buf
->buf
+ pos
;
2068 if (tok
== ROFF_ig
) {
2073 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2074 iname
[namesz
] = '\0';
2077 /* Resolve the macro name argument if it is indirect. */
2079 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2080 deftype
= ROFFDEF_USER
;
2081 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2083 mandoc_msg(MANDOCERR_STR_UNDEF
,
2084 ln
, (int)(iname
- buf
->buf
),
2085 "%.*s", (int)namesz
, iname
);
2088 namesz
= strlen(name
);
2092 if (namesz
== 0 && tok
!= ROFF_ig
) {
2093 mandoc_msg(MANDOCERR_REQ_EMPTY
,
2094 ln
, ppos
, "%s", roff_name
[tok
]);
2098 roffnode_push(r
, tok
, name
, ln
, ppos
);
2101 * At the beginning of a `de' macro, clear the existing string
2102 * with the same name, if there is one. New content will be
2103 * appended from roff_block_text() in multiline mode.
2106 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
2107 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
2108 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2109 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
2110 deftype
= ROFFDEF_ANY
;
2111 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2112 switch (deftype
) { /* Before appending, ... */
2113 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
2114 roff_setstrn(&r
->strtab
, name
, namesz
,
2115 value
, strlen(value
), 0);
2117 case ROFFDEF_REN
: /* call original standard macro. */
2118 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2119 (int)strlen(value
), value
);
2120 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2121 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2124 case ROFFDEF_STD
: /* rename and call standard macro. */
2125 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
2126 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
2127 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2129 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2141 /* Get the custom end marker. */
2144 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2146 /* Resolve the end marker if it is indirect. */
2148 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2149 deftype
= ROFFDEF_USER
;
2150 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2152 mandoc_msg(MANDOCERR_STR_UNDEF
,
2153 ln
, (int)(iname
- buf
->buf
),
2154 "%.*s", (int)namesz
, iname
);
2157 namesz
= strlen(name
);
2162 r
->last
->end
= mandoc_strndup(name
, namesz
);
2165 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2166 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2172 roff_block_sub(ROFF_ARGS
)
2178 * First check whether a custom macro exists at this level. If
2179 * it does, then check against it. This is some of groff's
2180 * stranger behaviours. If we encountered a custom end-scope
2181 * tag and that tag also happens to be a "real" macro, then we
2182 * need to try interpreting it again as a real macro. If it's
2183 * not, then return ignore. Else continue.
2187 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2188 if (buf
->buf
[i
] != r
->last
->end
[j
])
2191 if (r
->last
->end
[j
] == '\0' &&
2192 (buf
->buf
[i
] == '\0' ||
2193 buf
->buf
[i
] == ' ' ||
2194 buf
->buf
[i
] == '\t')) {
2196 roffnode_cleanscope(r
);
2198 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2202 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2210 * If we have no custom end-query or lookup failed, then try
2211 * pulling it out of the hashtable.
2214 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2216 if (t
!= ROFF_cblock
) {
2218 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2222 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2226 roff_block_text(ROFF_ARGS
)
2230 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2236 roff_cond_sub(ROFF_ARGS
)
2239 int endloop
, irc
, rr
;
2244 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2245 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2246 if (roffnode_cleanscope(r
))
2250 * If `\}' occurs on a macro line without a preceding macro,
2251 * drop the line completely.
2254 ep
= buf
->buf
+ pos
;
2255 if (ep
[0] == '\\' && ep
[1] == '}')
2259 * The closing delimiter `\}' rewinds the conditional scope
2260 * but is otherwise ignored when interpreting the line.
2263 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2266 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2267 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2280 * Fully handle known macros when they are structurally
2281 * required or when the conditional evaluated to true.
2284 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2285 irc
|= t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
) ?
2286 (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) :
2287 rr
? ROFF_CONT
: ROFF_IGN
;
2292 roff_cond_text(ROFF_ARGS
)
2295 int endloop
, irc
, rr
;
2299 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2300 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2301 if (roffnode_cleanscope(r
))
2305 * If `\}' occurs on a text line with neither preceding
2306 * nor following characters, drop the line completely.
2309 ep
= buf
->buf
+ pos
;
2310 if (strcmp(ep
, "\\}") == 0)
2314 * The closing delimiter `\}' rewinds the conditional scope
2315 * but is otherwise ignored when interpreting the line.
2318 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2321 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2322 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2338 /* --- handling of numeric and conditional expressions -------------------- */
2341 * Parse a single signed integer number. Stop at the first non-digit.
2342 * If there is at least one digit, return success and advance the
2343 * parse point, else return failure and let the parse point unchanged.
2344 * Ignore overflows, treat them just like the C language.
2347 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2349 int myres
, scaled
, n
, p
;
2356 if (n
|| v
[p
] == '+')
2359 if (flags
& ROFFNUM_WHITE
)
2360 while (isspace((unsigned char)v
[p
]))
2363 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2364 *res
= 10 * *res
+ v
[p
] - '0';
2371 /* Each number may be followed by one optional scaling unit. */
2375 scaled
= *res
* 65536;
2378 scaled
= *res
* 240;
2381 scaled
= *res
* 240 / 2.54;
2392 scaled
= *res
* 10 / 3;
2398 scaled
= *res
* 6 / 25;
2405 if (flags
& ROFFNUM_SCALE
)
2413 * Evaluate a string comparison condition.
2414 * The first character is the delimiter.
2415 * Succeed if the string up to its second occurrence
2416 * matches the string up to its third occurence.
2417 * Advance the cursor after the third occurrence
2418 * or lacking that, to the end of the line.
2421 roff_evalstrcond(const char *v
, int *pos
)
2423 const char *s1
, *s2
, *s3
;
2427 s1
= v
+ *pos
; /* initial delimiter */
2428 s2
= s1
+ 1; /* for scanning the first string */
2429 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2431 if (NULL
== s3
) /* found no middle delimiter */
2434 while ('\0' != *++s3
) {
2435 if (*s2
!= *s3
) { /* mismatch */
2436 s3
= strchr(s3
, *s1
);
2439 if (*s3
== *s1
) { /* found the final delimiter */
2448 s3
= strchr(s2
, '\0');
2449 else if (*s3
!= '\0')
2456 * Evaluate an optionally negated single character, numerical,
2457 * or string condition.
2460 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2462 const char *start
, *end
;
2465 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2467 if ('!' == v
[*pos
]) {
2488 } while (v
[*pos
] == ' ');
2491 * Quirk for groff compatibility:
2492 * The horizontal tab is neither available nor unavailable.
2495 if (v
[*pos
] == '\t') {
2500 /* Printable ASCII characters are available. */
2502 if (v
[*pos
] != '\\') {
2508 switch (mandoc_escape(&end
, &start
, &len
)) {
2509 case ESCAPE_SPECIAL
:
2510 istrue
= mchars_spec2cp(start
, len
) != -1;
2512 case ESCAPE_UNICODE
:
2515 case ESCAPE_NUMBERED
:
2516 istrue
= mchars_num2char(start
, len
) != -1;
2523 return istrue
== wanttrue
;
2530 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2533 else if (v
[*pos
] == 'r')
2534 istrue
= roff_hasregn(r
, name
, sz
);
2536 deftype
= ROFFDEF_ANY
;
2537 roff_getstrn(r
, name
, sz
, &deftype
);
2541 return istrue
== wanttrue
;
2547 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2548 return (number
> 0) == wanttrue
;
2549 else if (*pos
== savepos
)
2550 return roff_evalstrcond(v
, pos
) == wanttrue
;
2556 roff_line_ignore(ROFF_ARGS
)
2563 roff_insec(ROFF_ARGS
)
2566 mandoc_msg(MANDOCERR_REQ_INSEC
, ln
, ppos
, "%s", roff_name
[tok
]);
2571 roff_unsupp(ROFF_ARGS
)
2574 mandoc_msg(MANDOCERR_REQ_UNSUPP
, ln
, ppos
, "%s", roff_name
[tok
]);
2579 roff_cond(ROFF_ARGS
)
2583 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2586 * An `.el' has no conditional body: it will consume the value
2587 * of the current rstack entry set in prior `ie' calls or
2590 * If we're not an `el', however, then evaluate the conditional.
2593 r
->last
->rule
= tok
== ROFF_el
?
2594 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2595 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2598 * An if-else will put the NEGATION of the current evaluated
2599 * conditional into the stack of rules.
2602 if (tok
== ROFF_ie
) {
2603 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2605 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2606 r
->rstacksz
, sizeof(int));
2608 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2611 /* If the parent has false as its rule, then so do we. */
2613 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2618 * If there is nothing on the line after the conditional,
2619 * not even whitespace, use next-line scope.
2620 * Except that .while does not support next-line scope.
2623 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2624 r
->last
->endspan
= 2;
2628 while (buf
->buf
[pos
] == ' ')
2631 /* An opening brace requests multiline scope. */
2633 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2634 r
->last
->endspan
= -1;
2636 while (buf
->buf
[pos
] == ' ')
2642 * Anything else following the conditional causes
2643 * single-line scope. Warn if the scope contains
2644 * nothing but trailing whitespace.
2647 if (buf
->buf
[pos
] == '\0')
2648 mandoc_msg(MANDOCERR_COND_EMPTY
,
2649 ln
, ppos
, "%s", roff_name
[tok
]);
2651 r
->last
->endspan
= 1;
2656 if (tok
== ROFF_while
)
2668 /* Ignore groff compatibility mode for now. */
2670 if (tok
== ROFF_ds1
)
2672 else if (tok
== ROFF_as1
)
2676 * The first word is the name of the string.
2677 * If it is empty or terminated by an escape sequence,
2678 * abort the `ds' request without defining anything.
2681 name
= string
= buf
->buf
+ pos
;
2685 namesz
= roff_getname(r
, &string
, ln
, pos
);
2686 if (name
[namesz
] == '\\')
2689 /* Read past the initial double-quote, if any. */
2693 /* The rest is the value. */
2694 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2696 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2701 * Parse a single operator, one or two characters long.
2702 * If the operator is recognized, return success and advance the
2703 * parse point, else return failure and let the parse point unchanged.
2706 roff_getop(const char *v
, int *pos
, char *res
)
2721 switch (v
[*pos
+ 1]) {
2739 switch (v
[*pos
+ 1]) {
2753 if ('=' == v
[*pos
+ 1])
2765 * Evaluate either a parenthesized numeric expression
2766 * or a single signed integer number.
2769 roff_evalpar(struct roff
*r
, int ln
,
2770 const char *v
, int *pos
, int *res
, int flags
)
2774 return roff_getnum(v
, pos
, res
, flags
);
2777 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2781 * Omission of the closing parenthesis
2782 * is an error in validation mode,
2783 * but ignored in evaluation mode.
2788 else if (NULL
== res
)
2795 * Evaluate a complete numeric expression.
2796 * Proceed left to right, there is no concept of precedence.
2799 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2800 int *pos
, int *res
, int flags
)
2802 int mypos
, operand2
;
2810 if (flags
& ROFFNUM_WHITE
)
2811 while (isspace((unsigned char)v
[*pos
]))
2814 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2818 if (flags
& ROFFNUM_WHITE
)
2819 while (isspace((unsigned char)v
[*pos
]))
2822 if ( ! roff_getop(v
, pos
, &operator))
2825 if (flags
& ROFFNUM_WHITE
)
2826 while (isspace((unsigned char)v
[*pos
]))
2829 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2832 if (flags
& ROFFNUM_WHITE
)
2833 while (isspace((unsigned char)v
[*pos
]))
2850 if (operand2
== 0) {
2851 mandoc_msg(MANDOCERR_DIVZERO
,
2859 if (operand2
== 0) {
2860 mandoc_msg(MANDOCERR_DIVZERO
,
2868 *res
= *res
< operand2
;
2871 *res
= *res
> operand2
;
2874 *res
= *res
<= operand2
;
2877 *res
= *res
>= operand2
;
2880 *res
= *res
== operand2
;
2883 *res
= *res
!= operand2
;
2886 *res
= *res
&& operand2
;
2889 *res
= *res
|| operand2
;
2892 if (operand2
< *res
)
2896 if (operand2
> *res
)
2906 /* --- register management ------------------------------------------------ */
2909 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2911 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2915 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2916 int val
, char sign
, int step
)
2918 struct roffreg
*reg
;
2920 /* Search for an existing register with the same name. */
2923 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2924 strncmp(reg
->key
.p
, name
, len
) != 0))
2928 /* Create a new register. */
2929 reg
= mandoc_malloc(sizeof(struct roffreg
));
2930 reg
->key
.p
= mandoc_strndup(name
, len
);
2934 reg
->next
= r
->regtab
;
2940 else if ('-' == sign
)
2944 if (step
!= INT_MIN
)
2949 * Handle some predefined read-only number registers.
2950 * For now, return -1 if the requested register is not predefined;
2951 * in case a predefined read-only register having the value -1
2952 * were to turn up, another special value would have to be chosen.
2955 roff_getregro(const struct roff
*r
, const char *name
)
2959 case '$': /* Number of arguments of the last macro evaluated. */
2960 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
2961 case 'A': /* ASCII approximation mode is always off. */
2963 case 'g': /* Groff compatibility mode is always on. */
2965 case 'H': /* Fixed horizontal resolution. */
2967 case 'j': /* Always adjust left margin only. */
2969 case 'T': /* Some output device is always defined. */
2971 case 'V': /* Fixed vertical resolution. */
2979 roff_getreg(struct roff
*r
, const char *name
)
2981 return roff_getregn(r
, name
, strlen(name
), '\0');
2985 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2987 struct roffreg
*reg
;
2990 if ('.' == name
[0] && 2 == len
) {
2991 val
= roff_getregro(r
, name
+ 1);
2996 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2997 if (len
== reg
->key
.sz
&&
2998 0 == strncmp(name
, reg
->key
.p
, len
)) {
3001 reg
->val
+= reg
->step
;
3004 reg
->val
-= reg
->step
;
3013 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
3018 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
3020 struct roffreg
*reg
;
3023 if ('.' == name
[0] && 2 == len
) {
3024 val
= roff_getregro(r
, name
+ 1);
3029 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
3030 if (len
== reg
->key
.sz
&&
3031 0 == strncmp(name
, reg
->key
.p
, len
))
3038 roff_freereg(struct roffreg
*reg
)
3040 struct roffreg
*old_reg
;
3042 while (NULL
!= reg
) {
3053 char *key
, *val
, *step
;
3058 key
= val
= buf
->buf
+ pos
;
3062 keysz
= roff_getname(r
, &val
, ln
, pos
);
3063 if (key
[keysz
] == '\\')
3067 if (sign
== '+' || sign
== '-')
3071 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
3075 while (isspace((unsigned char)*step
))
3077 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
3080 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
3087 struct roffreg
*reg
, **prev
;
3091 name
= cp
= buf
->buf
+ pos
;
3094 namesz
= roff_getname(r
, &cp
, ln
, pos
);
3095 name
[namesz
] = '\0';
3100 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
3112 /* --- handler functions for roff requests -------------------------------- */
3121 cp
= buf
->buf
+ pos
;
3122 while (*cp
!= '\0') {
3124 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
3125 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
3126 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3127 if (name
[namesz
] == '\\')
3138 /* Parse the number of lines. */
3140 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
3141 mandoc_msg(MANDOCERR_IT_NONUM
,
3142 ln
, ppos
, "%s", buf
->buf
+ 1);
3146 while (isspace((unsigned char)buf
->buf
[pos
]))
3150 * Arm the input line trap.
3151 * Special-casing "an-trap" is an ugly workaround to cope
3152 * with DocBook stupidly fiddling with man(7) internals.
3156 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3157 strcmp(buf
->buf
+ pos
, "an-trap") ?
3158 buf
->buf
+ pos
: "br");
3166 enum roff_tok t
, te
;
3173 r
->format
= MPARSE_MDOC
;
3174 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3180 r
->format
= MPARSE_MAN
;
3181 mask
= MPARSE_QUICK
;
3186 if ((r
->options
& mask
) == 0)
3187 for (t
= tok
; t
< te
; t
++)
3188 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3195 r
->man
->flags
&= ~ROFF_NONOFILL
;
3196 if (r
->tbl
== NULL
) {
3197 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "TE");
3200 if (tbl_end(r
->tbl
, 0) == 0) {
3203 buf
->buf
= mandoc_strdup(".sp");
3206 return ROFF_REPARSE
;
3217 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "T&");
3219 tbl_restart(ln
, ppos
, r
->tbl
);
3225 * Handle in-line equation delimiters.
3228 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3231 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3234 * Outside equations, look for an opening delimiter.
3235 * If we are inside an equation, we already know it is
3236 * in-line, or this function wouldn't have been called;
3237 * so look for a closing delimiter.
3240 cp1
= buf
->buf
+ pos
;
3241 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3242 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3247 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3249 /* Handle preceding text, protecting whitespace. */
3251 if (*buf
->buf
!= '\0') {
3258 * Prepare replacing the delimiter with an equation macro
3259 * and drop leading white space from the equation.
3262 if (r
->eqn
== NULL
) {
3269 /* Handle following text, protecting whitespace. */
3277 /* Do the actual replacement. */
3279 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3280 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3284 /* Toggle the in-line state of the eqn subsystem. */
3286 r
->eqn_inline
= r
->eqn
== NULL
;
3287 return ROFF_REPARSE
;
3293 struct roff_node
*n
;
3295 if (r
->man
->meta
.macroset
== MACROSET_MAN
)
3296 man_breakscope(r
->man
, ROFF_EQ
);
3297 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3298 if (ln
> r
->man
->last
->line
)
3299 n
->flags
|= NODE_LINE
;
3300 n
->eqn
= eqn_box_new();
3301 roff_node_append(r
->man
, n
);
3302 r
->man
->next
= ROFF_NEXT_SIBLING
;
3304 assert(r
->eqn
== NULL
);
3305 if (r
->last_eqn
== NULL
)
3306 r
->last_eqn
= eqn_alloc();
3308 eqn_reset(r
->last_eqn
);
3309 r
->eqn
= r
->last_eqn
;
3312 if (buf
->buf
[pos
] != '\0')
3313 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3314 ".EQ %s", buf
->buf
+ pos
);
3322 if (r
->eqn
!= NULL
) {
3326 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "EN");
3327 if (buf
->buf
[pos
] != '\0')
3328 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3329 "EN %s", buf
->buf
+ pos
);
3336 if (r
->tbl
!= NULL
) {
3337 mandoc_msg(MANDOCERR_BLK_BROKEN
, ln
, ppos
, "TS breaks TS");
3340 r
->man
->flags
|= ROFF_NONOFILL
;
3341 r
->tbl
= tbl_alloc(ppos
, ln
, r
->last_tbl
);
3342 if (r
->last_tbl
== NULL
)
3343 r
->first_tbl
= r
->tbl
;
3344 r
->last_tbl
= r
->tbl
;
3349 roff_noarg(ROFF_ARGS
)
3351 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3352 man_breakscope(r
->man
, tok
);
3353 if (tok
== ROFF_brp
)
3355 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3356 if (buf
->buf
[pos
] != '\0')
3357 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3358 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3360 r
->man
->flags
|= ROFF_NOFILL
;
3361 else if (tok
== ROFF_fi
)
3362 r
->man
->flags
&= ~ROFF_NOFILL
;
3363 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3364 r
->man
->next
= ROFF_NEXT_SIBLING
;
3369 roff_onearg(ROFF_ARGS
)
3371 struct roff_node
*n
;
3375 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3376 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3378 man_breakscope(r
->man
, tok
);
3380 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3381 r
->man
->last
= roffce_node
;
3382 r
->man
->next
= ROFF_NEXT_SIBLING
;
3385 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3388 cp
= buf
->buf
+ pos
;
3390 while (*cp
!= '\0' && *cp
!= ' ')
3395 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3396 ln
, (int)(cp
- buf
->buf
),
3397 "%s ... %s", roff_name
[tok
], cp
);
3398 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3401 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3402 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3403 roff_word_alloc(r
->man
, ln
, pos
, "1");
3404 r
->man
->last
->flags
|= NODE_NOSRC
;
3407 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3408 &roffce_lines
, 0) == 0) {
3409 mandoc_msg(MANDOCERR_CE_NONUM
,
3410 ln
, pos
, "ce %s", buf
->buf
+ pos
);
3413 if (roffce_lines
< 1) {
3414 r
->man
->last
= r
->man
->last
->parent
;
3418 roffce_node
= r
->man
->last
->parent
;
3420 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3423 n
->flags
|= NODE_LINE
;
3424 r
->man
->next
= ROFF_NEXT_SIBLING
;
3429 roff_manyarg(ROFF_ARGS
)
3431 struct roff_node
*n
;
3434 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3437 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3438 while (*ep
!= '\0' && *ep
!= ' ')
3442 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3445 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3447 r
->man
->next
= ROFF_NEXT_SIBLING
;
3454 char *oldn
, *newn
, *end
, *value
;
3455 size_t oldsz
, newsz
, valsz
;
3457 newn
= oldn
= buf
->buf
+ pos
;
3461 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3462 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3466 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3470 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3472 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3473 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3485 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3489 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3490 ln
, p
- buf
->buf
, "cc ... %s", p
);
3496 roff_char(ROFF_ARGS
)
3498 const char *p
, *kp
, *vp
;
3502 /* Parse the character to be replaced. */
3504 kp
= buf
->buf
+ pos
;
3506 if (*kp
== '\0' || (*kp
== '\\' &&
3507 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3508 (*p
!= ' ' && *p
!= '\0')) {
3509 mandoc_msg(MANDOCERR_CHAR_ARG
, ln
, pos
, "char %s", kp
);
3517 * If the replacement string contains a font escape sequence,
3518 * we have to restore the font at the end.
3524 while (*p
!= '\0') {
3527 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3529 case ESCAPE_FONTROMAN
:
3530 case ESCAPE_FONTITALIC
:
3531 case ESCAPE_FONTBOLD
:
3534 case ESCAPE_FONTPREV
:
3542 mandoc_msg(MANDOCERR_CHAR_FONT
,
3543 ln
, (int)(vp
- buf
->buf
), "%s", vp
);
3546 * Approximate the effect of .char using the .tr tables.
3547 * XXX In groff, .char and .tr interact differently.
3551 if (r
->xtab
== NULL
)
3552 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3553 assert((unsigned int)*kp
< 128);
3554 free(r
->xtab
[(int)*kp
].p
);
3555 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3556 "%s%s", vp
, font
? "\fP" : "");
3558 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3560 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3576 mandoc_msg(MANDOCERR_ARG_EXCESS
, ln
,
3577 (int)(p
- buf
->buf
), "ec ... %s", p
);
3586 if (buf
->buf
[pos
] != '\0')
3587 mandoc_msg(MANDOCERR_ARG_SKIP
,
3588 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3595 while (buf
->buf
[pos
] == ' ')
3604 const char *p
, *first
, *second
;
3606 enum mandoc_esc esc
;
3611 mandoc_msg(MANDOCERR_REQ_EMPTY
, ln
, ppos
, "tr");
3615 while (*p
!= '\0') {
3619 if (*first
== '\\') {
3620 esc
= mandoc_escape(&p
, NULL
, NULL
);
3621 if (esc
== ESCAPE_ERROR
) {
3622 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3623 (int)(p
- buf
->buf
), "%s", first
);
3626 fsz
= (size_t)(p
- first
);
3630 if (*second
== '\\') {
3631 esc
= mandoc_escape(&p
, NULL
, NULL
);
3632 if (esc
== ESCAPE_ERROR
) {
3633 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3634 (int)(p
- buf
->buf
), "%s", second
);
3637 ssz
= (size_t)(p
- second
);
3638 } else if (*second
== '\0') {
3639 mandoc_msg(MANDOCERR_TR_ODD
, ln
,
3640 (int)(first
- buf
->buf
), "tr %s", first
);
3646 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3651 if (r
->xtab
== NULL
)
3652 r
->xtab
= mandoc_calloc(128,
3653 sizeof(struct roffstr
));
3655 free(r
->xtab
[(int)*first
].p
);
3656 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3657 r
->xtab
[(int)*first
].sz
= ssz
;
3664 * Implementation of the .return request.
3665 * There is no need to call roff_userret() from here.
3666 * The read module will call that after rewinding the reader stack
3667 * to the place from where the current macro was called.
3670 roff_return(ROFF_ARGS
)
3672 if (r
->mstackpos
>= 0)
3673 return ROFF_IGN
| ROFF_USERRET
;
3675 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "return");
3683 char *oldn
, *newn
, *end
;
3684 size_t oldsz
, newsz
;
3687 oldn
= newn
= buf
->buf
+ pos
;
3691 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3692 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3696 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3700 deftype
= ROFFDEF_ANY
;
3701 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3704 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3705 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3706 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3709 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3710 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3713 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3714 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3715 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3718 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3719 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3722 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3723 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3730 roff_shift(ROFF_ARGS
)
3736 if (buf
->buf
[pos
] != '\0' &&
3737 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3738 mandoc_msg(MANDOCERR_CE_NONUM
,
3739 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3742 if (r
->mstackpos
< 0) {
3743 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "shift");
3746 ctx
= r
->mstack
+ r
->mstackpos
;
3747 if (levels
> ctx
->argc
) {
3748 mandoc_msg(MANDOCERR_SHIFT
,
3749 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3754 for (i
= 0; i
< levels
; i
++)
3756 ctx
->argc
-= levels
;
3757 for (i
= 0; i
< ctx
->argc
; i
++)
3758 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3767 name
= buf
->buf
+ pos
;
3768 mandoc_msg(MANDOCERR_SO
, ln
, ppos
, "so %s", name
);
3771 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3772 * opening anything that's not in our cwd or anything beneath
3773 * it. Thus, explicitly disallow traversing up the file-system
3774 * or using absolute paths.
3777 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3778 mandoc_msg(MANDOCERR_SO_PATH
, ln
, ppos
, ".so %s", name
);
3779 buf
->sz
= mandoc_asprintf(&cp
,
3780 ".sp\nSee the file %s.\n.sp", name
) + 1;
3784 return ROFF_REPARSE
;
3791 /* --- user defined strings and macros ------------------------------------ */
3794 roff_userdef(ROFF_ARGS
)
3797 char *arg
, *ap
, *dst
, *src
;
3800 /* Initialize a new macro stack context. */
3802 if (++r
->mstackpos
== r
->mstacksz
) {
3803 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3804 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3807 ctx
= r
->mstack
+ r
->mstackpos
;
3813 * Collect pointers to macro argument strings,
3814 * NUL-terminating them and escaping quotes.
3817 src
= buf
->buf
+ pos
;
3818 while (*src
!= '\0') {
3819 if (ctx
->argc
== ctx
->argsz
) {
3821 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3822 ctx
->argsz
, sizeof(*ctx
->argv
));
3824 arg
= roff_getarg(r
, &src
, ln
, &pos
);
3825 sz
= 1; /* For the terminating NUL. */
3826 for (ap
= arg
; *ap
!= '\0'; ap
++)
3827 sz
+= *ap
== '"' ? 4 : 1;
3828 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3829 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3831 memcpy(dst
, "\\(dq", 4);
3840 /* Replace the macro invocation by the macro definition. */
3843 buf
->buf
= mandoc_strdup(r
->current_string
);
3844 buf
->sz
= strlen(buf
->buf
) + 1;
3847 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3848 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3852 * Calling a high-level macro that was renamed with .rn.
3853 * r->current_string has already been set up by roff_parse().
3856 roff_renamed(ROFF_ARGS
)
3860 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3861 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3869 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3878 /* Read until end of name and terminate it with NUL. */
3879 for (cp
= name
; 1; cp
++) {
3880 if ('\0' == *cp
|| ' ' == *cp
) {
3887 if ('{' == cp
[1] || '}' == cp
[1])
3892 mandoc_msg(MANDOCERR_NAMESC
, ln
, pos
,
3893 "%.*s", (int)(cp
- name
+ 1), name
);
3894 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3898 /* Read past spaces. */
3907 * Store *string into the user-defined string called *name.
3908 * To clear an existing entry, call with (*r, *name, NULL, 0).
3909 * append == 0: replace mode
3910 * append == 1: single-line append mode
3911 * append == 2: multiline append mode, append '\n' after each call
3914 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3919 namesz
= strlen(name
);
3920 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3921 string
? strlen(string
) : 0, append
);
3922 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3926 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3927 const char *string
, size_t stringsz
, int append
)
3932 size_t oldch
, newch
;
3934 /* Search for an existing string with the same name. */
3937 while (n
&& (namesz
!= n
->key
.sz
||
3938 strncmp(n
->key
.p
, name
, namesz
)))
3942 /* Create a new string table entry. */
3943 n
= mandoc_malloc(sizeof(struct roffkv
));
3944 n
->key
.p
= mandoc_strndup(name
, namesz
);
3950 } else if (0 == append
) {
3960 * One additional byte for the '\n' in multiline mode,
3961 * and one for the terminating '\0'.
3963 newch
= stringsz
+ (1 < append
? 2u : 1u);
3965 if (NULL
== n
->val
.p
) {
3966 n
->val
.p
= mandoc_malloc(newch
);
3971 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3974 /* Skip existing content in the destination buffer. */
3975 c
= n
->val
.p
+ (int)oldch
;
3977 /* Append new content to the destination buffer. */
3979 while (i
< (int)stringsz
) {
3981 * Rudimentary roff copy mode:
3982 * Handle escaped backslashes.
3984 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3989 /* Append terminating bytes. */
3994 n
->val
.sz
= (int)(c
- n
->val
.p
);
3998 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
4001 const struct roffkv
*n
;
4006 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
4007 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4008 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4010 if (*deftype
& ROFFDEF_USER
) {
4011 *deftype
= ROFFDEF_USER
;
4018 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
4019 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4020 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4022 if (*deftype
& ROFFDEF_REN
) {
4023 *deftype
= ROFFDEF_REN
;
4030 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
4031 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
4032 predefs
[i
].name
[len
] != '\0')
4034 if (*deftype
& ROFFDEF_PRE
) {
4035 *deftype
= ROFFDEF_PRE
;
4036 return predefs
[i
].str
;
4042 if (r
->man
->meta
.macroset
!= MACROSET_MAN
) {
4043 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
4044 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4045 roff_name
[tok
][len
] != '\0')
4047 if (*deftype
& ROFFDEF_STD
) {
4048 *deftype
= ROFFDEF_STD
;
4056 if (r
->man
->meta
.macroset
!= MACROSET_MDOC
) {
4057 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
4058 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4059 roff_name
[tok
][len
] != '\0')
4061 if (*deftype
& ROFFDEF_STD
) {
4062 *deftype
= ROFFDEF_STD
;
4071 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
4072 if (*deftype
& ROFFDEF_REN
) {
4074 * This might still be a request,
4075 * so do not treat it as undefined yet.
4077 *deftype
= ROFFDEF_UNDEF
;
4081 /* Using an undefined string defines it to be empty. */
4083 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
4084 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
4092 roff_freestr(struct roffkv
*r
)
4094 struct roffkv
*n
, *nn
;
4096 for (n
= r
; n
; n
= nn
) {
4104 /* --- accessors and utility functions ------------------------------------ */
4107 * Duplicate an input string, making the appropriate character
4108 * conversations (as stipulated by `tr') along the way.
4109 * Returns a heap-allocated string with all the replacements made.
4112 roff_strdup(const struct roff
*r
, const char *p
)
4114 const struct roffkv
*cp
;
4118 enum mandoc_esc esc
;
4120 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
4121 return mandoc_strdup(p
);
4122 else if ('\0' == *p
)
4123 return mandoc_strdup("");
4126 * Step through each character looking for term matches
4127 * (remember that a `tr' can be invoked with an escape, which is
4128 * a glyph but the escape is multi-character).
4129 * We only do this if the character hash has been initialised
4130 * and the string is >0 length.
4136 while ('\0' != *p
) {
4137 assert((unsigned int)*p
< 128);
4138 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
4139 sz
= r
->xtab
[(int)*p
].sz
;
4140 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4141 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
4145 } else if ('\\' != *p
) {
4146 res
= mandoc_realloc(res
, ssz
+ 2);
4151 /* Search for term matches. */
4152 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4153 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4158 * A match has been found.
4159 * Append the match to the array and move
4160 * forward by its keysize.
4162 res
= mandoc_realloc(res
,
4163 ssz
+ cp
->val
.sz
+ 1);
4164 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4166 p
+= (int)cp
->key
.sz
;
4171 * Handle escapes carefully: we need to copy
4172 * over just the escape itself, or else we might
4173 * do replacements within the escape itself.
4174 * Make sure to pass along the bogus string.
4177 esc
= mandoc_escape(&p
, NULL
, NULL
);
4178 if (ESCAPE_ERROR
== esc
) {
4180 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4181 memcpy(res
+ ssz
, pp
, sz
);
4185 * We bail out on bad escapes.
4186 * No need to warn: we already did so when
4187 * roff_expand() was called.
4190 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4191 memcpy(res
+ ssz
, pp
, sz
);
4195 res
[(int)ssz
] = '\0';
4200 roff_getformat(const struct roff
*r
)
4207 * Find out whether a line is a macro line or not.
4208 * If it is, adjust the current position and return one; if it isn't,
4209 * return zero and don't change the current position.
4210 * If the control character has been set with `.cc', then let that grain
4212 * This is slighly contrary to groff, where using the non-breaking
4213 * control character when `cc' has been invoked will cause the
4214 * non-breaking macro contents to be printed verbatim.
4217 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4223 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4225 else if (r
->control
!= '\0')
4227 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4229 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4234 while (' ' == cp
[pos
] || '\t' == cp
[pos
])