]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.353 2018/12/18 22:00:02 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017, 2018 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"
41 /* Maximum number of string expansions per line, to break infinite loops. */
42 #define EXPAND_LIMIT 1000
44 /* Types of definitions of macros and strings. */
45 #define ROFFDEF_USER (1 << 1) /* User-defined. */
46 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
47 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
48 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
49 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
50 ROFFDEF_REN | ROFFDEF_STD)
51 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
53 /* --- data types --------------------------------------------------------- */
56 * An incredibly-simple string buffer.
59 char *p
; /* nil-terminated buffer */
60 size_t sz
; /* saved strlen(p) */
64 * A key-value roffstr pair as part of a singly-linked list.
69 struct roffkv
*next
; /* next in list */
73 * A single number register as part of a singly-linked list.
83 * Association of request and macro names with token IDs.
91 * A macro processing context.
92 * More than one is needed when macro calls are nested.
101 struct roff_man
*man
; /* mdoc or man parser */
102 struct roffnode
*last
; /* leaf of stack */
103 struct mctx
*mstack
; /* stack of macro contexts */
104 int *rstack
; /* stack of inverted `ie' values */
105 struct ohash
*reqtab
; /* request lookup table */
106 struct roffreg
*regtab
; /* number registers */
107 struct roffkv
*strtab
; /* user-defined strings & macros */
108 struct roffkv
*rentab
; /* renamed strings & macros */
109 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
110 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
111 const char *current_string
; /* value of last called user macro */
112 struct tbl_node
*first_tbl
; /* first table parsed */
113 struct tbl_node
*last_tbl
; /* last table parsed */
114 struct tbl_node
*tbl
; /* current table being parsed */
115 struct eqn_node
*last_eqn
; /* equation parser */
116 struct eqn_node
*eqn
; /* active equation parser */
117 int eqn_inline
; /* current equation is inline */
118 int options
; /* parse options */
119 int mstacksz
; /* current size of mstack */
120 int mstackpos
; /* position in mstack */
121 int rstacksz
; /* current size limit of rstack */
122 int rstackpos
; /* position in rstack */
123 int format
; /* current file in mdoc or man format */
124 char control
; /* control character */
125 char escape
; /* escape character */
129 enum roff_tok tok
; /* type of node */
130 struct roffnode
*parent
; /* up one in stack */
131 int line
; /* parse line */
132 int col
; /* parse col */
133 char *name
; /* node name, e.g. macro name */
134 char *end
; /* end-rules: custom token */
135 int endspan
; /* end-rules: next-line or infty */
136 int rule
; /* current evaluation rule */
139 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
140 enum roff_tok tok, /* tok of macro */ \
141 struct buf *buf, /* input buffer */ \
142 int ln, /* parse line */ \
143 int ppos, /* original pos in buffer */ \
144 int pos, /* current pos in buffer */ \
145 int *offs /* reset offset of buffer data */
147 typedef int (*roffproc
)(ROFF_ARGS
);
150 roffproc proc
; /* process new macro */
151 roffproc text
; /* process as child text of macro */
152 roffproc sub
; /* process as child of macro */
154 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
158 const char *name
; /* predefined input name */
159 const char *str
; /* replacement symbol */
162 #define PREDEF(__name, __str) \
163 { (__name), (__str) },
165 /* --- function prototypes ------------------------------------------------ */
167 static int roffnode_cleanscope(struct roff
*);
168 static int roffnode_pop(struct roff
*);
169 static void roffnode_push(struct roff
*, enum roff_tok
,
170 const char *, int, int);
171 static void roff_addtbl(struct roff_man
*, int, struct tbl_node
*);
172 static int roff_als(ROFF_ARGS
);
173 static int roff_block(ROFF_ARGS
);
174 static int roff_block_text(ROFF_ARGS
);
175 static int roff_block_sub(ROFF_ARGS
);
176 static int roff_br(ROFF_ARGS
);
177 static int roff_cblock(ROFF_ARGS
);
178 static int roff_cc(ROFF_ARGS
);
179 static int roff_ccond(struct roff
*, int, int);
180 static int roff_char(ROFF_ARGS
);
181 static int roff_cond(ROFF_ARGS
);
182 static int roff_cond_text(ROFF_ARGS
);
183 static int roff_cond_sub(ROFF_ARGS
);
184 static int roff_ds(ROFF_ARGS
);
185 static int roff_ec(ROFF_ARGS
);
186 static int roff_eo(ROFF_ARGS
);
187 static int roff_eqndelim(struct roff
*, struct buf
*, int);
188 static int roff_evalcond(struct roff
*r
, int, char *, int *);
189 static int roff_evalnum(struct roff
*, int,
190 const char *, int *, int *, int);
191 static int roff_evalpar(struct roff
*, int,
192 const char *, int *, int *, int);
193 static int roff_evalstrcond(const char *, int *);
194 static void roff_free1(struct roff
*);
195 static void roff_freereg(struct roffreg
*);
196 static void roff_freestr(struct roffkv
*);
197 static size_t roff_getname(struct roff
*, char **, int, int);
198 static int roff_getnum(const char *, int *, int *, int);
199 static int roff_getop(const char *, int *, char *);
200 static int roff_getregn(struct roff
*,
201 const char *, size_t, char);
202 static int roff_getregro(const struct roff
*,
204 static const char *roff_getstrn(struct roff
*,
205 const char *, size_t, int *);
206 static int roff_hasregn(const struct roff
*,
207 const char *, size_t);
208 static int roff_insec(ROFF_ARGS
);
209 static int roff_it(ROFF_ARGS
);
210 static int roff_line_ignore(ROFF_ARGS
);
211 static void roff_man_alloc1(struct roff_man
*);
212 static void roff_man_free1(struct roff_man
*);
213 static int roff_manyarg(ROFF_ARGS
);
214 static int roff_nop(ROFF_ARGS
);
215 static int roff_nr(ROFF_ARGS
);
216 static int roff_onearg(ROFF_ARGS
);
217 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
219 static int roff_parsetext(struct roff
*, struct buf
*,
221 static int roff_renamed(ROFF_ARGS
);
222 static int roff_res(struct roff
*, struct buf
*, int, int);
223 static int roff_return(ROFF_ARGS
);
224 static int roff_rm(ROFF_ARGS
);
225 static int roff_rn(ROFF_ARGS
);
226 static int roff_rr(ROFF_ARGS
);
227 static void roff_setregn(struct roff
*, const char *,
228 size_t, int, char, int);
229 static void roff_setstr(struct roff
*,
230 const char *, const char *, int);
231 static void roff_setstrn(struct roffkv
**, const char *,
232 size_t, const char *, size_t, int);
233 static int roff_shift(ROFF_ARGS
);
234 static int roff_so(ROFF_ARGS
);
235 static int roff_tr(ROFF_ARGS
);
236 static int roff_Dd(ROFF_ARGS
);
237 static int roff_TE(ROFF_ARGS
);
238 static int roff_TS(ROFF_ARGS
);
239 static int roff_EQ(ROFF_ARGS
);
240 static int roff_EN(ROFF_ARGS
);
241 static int roff_T_(ROFF_ARGS
);
242 static int roff_unsupp(ROFF_ARGS
);
243 static int roff_userdef(ROFF_ARGS
);
245 /* --- constant data ------------------------------------------------------ */
247 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
248 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
250 const char *__roff_name
[MAN_MAX
+ 1] = {
251 "br", "ce", "ft", "ll",
252 "mc", "po", "rj", "sp",
254 "ab", "ad", "af", "aln",
255 "als", "am", "am1", "ami",
256 "ami1", "as", "as1", "asciify",
257 "backtrace", "bd", "bleedat", "blm",
258 "box", "boxa", "bp", "BP",
259 "break", "breakchar", "brnl", "brp",
261 "cf", "cflags", "ch", "char",
262 "chop", "class", "close", "CL",
263 "color", "composite", "continue", "cp",
264 "cropat", "cs", "cu", "da",
265 "dch", "Dd", "de", "de1",
266 "defcolor", "dei", "dei1", "device",
267 "devicem", "di", "do", "ds",
268 "ds1", "dwh", "dt", "ec",
269 "ecr", "ecs", "el", "em",
270 "EN", "eo", "EP", "EQ",
271 "errprint", "ev", "evc", "ex",
272 "fallback", "fam", "fc", "fchar",
273 "fcolor", "fdeferlig", "feature", "fkern",
274 "fl", "flig", "fp", "fps",
275 "fschar", "fspacewidth", "fspecial", "ftr",
276 "fzoom", "gcolor", "hc", "hcode",
277 "hidechar", "hla", "hlm", "hpf",
278 "hpfa", "hpfcode", "hw", "hy",
279 "hylang", "hylen", "hym", "hypp",
280 "hys", "ie", "if", "ig",
281 "index", "it", "itc", "IX",
282 "kern", "kernafter", "kernbefore", "kernpair",
283 "lc", "lc_ctype", "lds", "length",
284 "letadj", "lf", "lg", "lhang",
285 "linetabs", "lnr", "lnrf", "lpfx",
287 "mediasize", "minss", "mk", "mso",
288 "na", "ne", "nh", "nhychar",
289 "nm", "nn", "nop", "nr",
290 "nrf", "nroff", "ns", "nx",
291 "open", "opena", "os", "output",
292 "padj", "papersize", "pc", "pev",
293 "pi", "PI", "pl", "pm",
295 "psbb", "pshape", "pso", "ptr",
296 "pvs", "rchar", "rd", "recursionlimit",
297 "return", "rfschar", "rhang",
298 "rm", "rn", "rnn", "rr",
299 "rs", "rt", "schar", "sentchar",
300 "shc", "shift", "sizes", "so",
301 "spacewidth", "special", "spreadwarn", "ss",
302 "sty", "substring", "sv", "sy",
305 "tm", "tm1", "tmc", "tr",
306 "track", "transchar", "trf", "trimat",
307 "trin", "trnt", "troff", "TS",
308 "uf", "ul", "unformat", "unwatch",
309 "unwatchn", "vpt", "vs", "warn",
310 "warnscale", "watch", "watchlength", "watchn",
311 "wh", "while", "write", "writec",
312 "writem", "xflag", ".", NULL
,
314 "Dd", "Dt", "Os", "Sh",
315 "Ss", "Pp", "D1", "Dl",
316 "Bd", "Ed", "Bl", "El",
317 "It", "Ad", "An", "Ap",
318 "Ar", "Cd", "Cm", "Dv",
319 "Er", "Ev", "Ex", "Fa",
320 "Fd", "Fl", "Fn", "Ft",
321 "Ic", "In", "Li", "Nd",
322 "Nm", "Op", "Ot", "Pa",
323 "Rv", "St", "Va", "Vt",
324 "Xr", "%A", "%B", "%D",
325 "%I", "%J", "%N", "%O",
326 "%P", "%R", "%T", "%V",
327 "Ac", "Ao", "Aq", "At",
328 "Bc", "Bf", "Bo", "Bq",
329 "Bsx", "Bx", "Db", "Dc",
330 "Do", "Dq", "Ec", "Ef",
331 "Em", "Eo", "Fx", "Ms",
332 "No", "Ns", "Nx", "Ox",
333 "Pc", "Pf", "Po", "Pq",
334 "Qc", "Ql", "Qo", "Qq",
335 "Re", "Rs", "Sc", "So",
336 "Sq", "Sm", "Sx", "Sy",
337 "Tn", "Ux", "Xc", "Xo",
338 "Fo", "Fc", "Oo", "Oc",
339 "Bk", "Ek", "Bt", "Hf",
340 "Fr", "Ud", "Lb", "Lp",
341 "Lk", "Mt", "Brq", "Bro",
342 "Brc", "%C", "Es", "En",
343 "Dx", "%Q", "%U", "Ta",
345 "TH", "SH", "SS", "TP",
347 "LP", "PP", "P", "IP",
348 "HP", "SM", "SB", "BI",
349 "IB", "BR", "RB", "R",
350 "B", "I", "IR", "RI",
352 "RE", "RS", "DT", "UC",
356 "UE", "MT", "ME", NULL
358 const char *const *roff_name
= __roff_name
;
360 static struct roffmac roffs
[TOKEN_NONE
] = {
361 { roff_br
, NULL
, NULL
, 0 }, /* br */
362 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
363 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
364 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
365 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
366 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
367 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
368 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
369 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
370 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
371 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
372 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
373 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
374 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
375 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
376 { roff_als
, NULL
, NULL
, 0 }, /* als */
377 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
378 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
379 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
380 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
381 { roff_ds
, NULL
, NULL
, 0 }, /* as */
382 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
383 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
385 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
386 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
387 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
388 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
389 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
390 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
391 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
392 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
393 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
394 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
395 { roff_br
, NULL
, NULL
, 0 }, /* brp */
396 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
397 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
398 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
399 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
400 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
401 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
402 { roff_char
, NULL
, NULL
, 0 }, /* char */
403 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
404 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
405 { roff_insec
, NULL
, NULL
, 0 }, /* close */
406 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
407 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
409 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
412 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
413 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
415 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
416 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
417 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
418 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
419 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
420 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
421 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
424 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
425 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
426 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
427 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
428 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
429 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
430 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
431 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
432 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
433 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
434 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
435 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
436 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
437 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
438 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
439 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
441 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
442 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
444 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
445 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
446 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
453 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
454 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
455 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
457 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
458 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
475 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
476 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
477 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
478 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
479 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
480 { roff_it
, NULL
, NULL
, 0 }, /* it */
481 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
487 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
488 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
489 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
490 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
491 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
492 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
494 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
495 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
496 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
497 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
498 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
500 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
504 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
505 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
509 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
510 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
511 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
512 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
513 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
514 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
515 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
516 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
517 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
518 { roff_insec
, NULL
, NULL
, 0 }, /* open */
519 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
520 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
521 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
522 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
524 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
525 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
526 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
527 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
532 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
533 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
534 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
535 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
536 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
537 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
538 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
540 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
541 { roff_return
, NULL
, NULL
, 0 }, /* return */
542 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
543 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
544 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
545 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
546 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
547 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
550 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
552 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
553 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
555 { roff_so
, NULL
, NULL
, 0 }, /* so */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
561 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
562 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
563 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
564 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
565 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
566 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
567 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
568 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
569 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
572 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
573 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
574 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
575 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
576 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
577 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
578 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
579 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
580 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
581 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
582 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
583 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
584 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
587 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
589 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
590 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
592 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
593 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
594 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
595 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
596 { roff_insec
, NULL
, NULL
, 0 }, /* write */
597 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
598 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
599 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
600 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
601 { roff_renamed
, NULL
, NULL
, 0 },
602 { roff_userdef
, NULL
, NULL
, 0 }
605 /* Array of injected predefined strings. */
606 #define PREDEFS_MAX 38
607 static const struct predef predefs
[PREDEFS_MAX
] = {
608 #include "predefs.in"
611 static int roffce_lines
; /* number of input lines to center */
612 static struct roff_node
*roffce_node
; /* active request */
613 static int roffit_lines
; /* number of lines to delay */
614 static char *roffit_macro
; /* nil-terminated macro line */
617 /* --- request table ------------------------------------------------------ */
620 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
628 htab
= mandoc_malloc(sizeof(*htab
));
629 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
631 for (tok
= mintok
; tok
< maxtok
; tok
++) {
632 if (roff_name
[tok
] == NULL
)
634 sz
= strlen(roff_name
[tok
]);
635 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
637 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
638 slot
= ohash_qlookup(htab
, req
->name
);
639 ohash_insert(htab
, slot
, req
);
645 roffhash_free(struct ohash
*htab
)
652 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
653 req
= ohash_next(htab
, &slot
))
660 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
667 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
669 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
670 return req
== NULL
? TOKEN_NONE
: req
->tok
;
673 /* --- stack of request blocks -------------------------------------------- */
676 * Pop the current node off of the stack of roff instructions currently
680 roffnode_pop(struct roff
*r
)
686 inloop
= p
->tok
== ROFF_while
;
695 * Push a roff node onto the instruction stack. This must later be
696 * removed with roffnode_pop().
699 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
704 p
= mandoc_calloc(1, sizeof(struct roffnode
));
707 p
->name
= mandoc_strdup(name
);
711 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
716 /* --- roff parser state data management ---------------------------------- */
719 roff_free1(struct roff
*r
)
723 tbl_free(r
->first_tbl
);
724 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
726 eqn_free(r
->last_eqn
);
727 r
->last_eqn
= r
->eqn
= NULL
;
729 while (r
->mstackpos
>= 0)
740 roff_freereg(r
->regtab
);
743 roff_freestr(r
->strtab
);
744 roff_freestr(r
->rentab
);
745 roff_freestr(r
->xmbtab
);
746 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
749 for (i
= 0; i
< 128; i
++)
756 roff_reset(struct roff
*r
)
759 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
769 roff_free(struct roff
*r
)
774 for (i
= 0; i
< r
->mstacksz
; i
++)
775 free(r
->mstack
[i
].argv
);
777 roffhash_free(r
->reqtab
);
782 roff_alloc(int options
)
786 r
= mandoc_calloc(1, sizeof(struct roff
));
787 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
788 r
->options
= options
;
789 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
796 /* --- syntax tree state data management ---------------------------------- */
799 roff_man_free1(struct roff_man
*man
)
802 if (man
->first
!= NULL
)
803 roff_node_delete(man
, man
->first
);
804 free(man
->meta
.msec
);
807 free(man
->meta
.arch
);
808 free(man
->meta
.title
);
809 free(man
->meta
.name
);
810 free(man
->meta
.date
);
814 roff_man_alloc1(struct roff_man
*man
)
817 memset(&man
->meta
, 0, sizeof(man
->meta
));
818 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
819 man
->first
->type
= ROFFT_ROOT
;
820 man
->last
= man
->first
;
823 man
->macroset
= MACROSET_NONE
;
824 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
825 man
->next
= ROFF_NEXT_CHILD
;
829 roff_man_reset(struct roff_man
*man
)
833 roff_man_alloc1(man
);
837 roff_man_free(struct roff_man
*man
)
845 roff_man_alloc(struct roff
*roff
, const char *os_s
, int quick
)
847 struct roff_man
*man
;
849 man
= mandoc_calloc(1, sizeof(*man
));
853 roff_man_alloc1(man
);
858 /* --- syntax tree handling ----------------------------------------------- */
861 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
862 enum roff_type type
, int tok
)
866 n
= mandoc_calloc(1, sizeof(*n
));
871 n
->sec
= man
->lastsec
;
873 if (man
->flags
& MDOC_SYNOPSIS
)
874 n
->flags
|= NODE_SYNPRETTY
;
876 n
->flags
&= ~NODE_SYNPRETTY
;
877 if (man
->flags
& MDOC_NEWLINE
)
878 n
->flags
|= NODE_LINE
;
879 man
->flags
&= ~MDOC_NEWLINE
;
885 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
889 case ROFF_NEXT_SIBLING
:
890 if (man
->last
->next
!= NULL
) {
891 n
->next
= man
->last
->next
;
892 man
->last
->next
->prev
= n
;
894 man
->last
->parent
->last
= n
;
897 n
->parent
= man
->last
->parent
;
899 case ROFF_NEXT_CHILD
:
900 if (man
->last
->child
!= NULL
) {
901 n
->next
= man
->last
->child
;
902 man
->last
->child
->prev
= n
;
905 man
->last
->child
= n
;
906 n
->parent
= man
->last
;
918 if (n
->end
!= ENDBODY_NOT
)
930 * Copy over the normalised-data pointer of our parent. Not
931 * everybody has one, but copying a null pointer is fine.
934 n
->norm
= n
->parent
->norm
;
935 assert(n
->parent
->type
== ROFFT_BLOCK
);
939 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
943 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
944 n
->string
= roff_strdup(man
->roff
, word
);
945 roff_node_append(man
, n
);
946 n
->flags
|= NODE_VALID
| NODE_ENDED
;
947 man
->next
= ROFF_NEXT_SIBLING
;
951 roff_word_append(struct roff_man
*man
, const char *word
)
954 char *addstr
, *newstr
;
957 addstr
= roff_strdup(man
->roff
, word
);
958 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
962 man
->next
= ROFF_NEXT_SIBLING
;
966 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
970 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
971 roff_node_append(man
, n
);
972 man
->next
= ROFF_NEXT_CHILD
;
976 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
980 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
981 roff_node_append(man
, n
);
982 man
->next
= ROFF_NEXT_CHILD
;
987 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
991 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
992 roff_node_append(man
, n
);
993 man
->next
= ROFF_NEXT_CHILD
;
998 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1000 struct roff_node
*n
;
1002 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1003 roff_node_append(man
, n
);
1004 man
->next
= ROFF_NEXT_CHILD
;
1009 roff_addtbl(struct roff_man
*man
, int line
, struct tbl_node
*tbl
)
1011 struct roff_node
*n
;
1012 struct tbl_span
*span
;
1014 if (man
->macroset
== MACROSET_MAN
)
1015 man_breakscope(man
, ROFF_TS
);
1016 while ((span
= tbl_span(tbl
)) != NULL
) {
1017 n
= roff_node_alloc(man
, line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1019 roff_node_append(man
, n
);
1020 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1021 man
->next
= ROFF_NEXT_SIBLING
;
1026 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1029 /* Adjust siblings. */
1032 n
->prev
->next
= n
->next
;
1034 n
->next
->prev
= n
->prev
;
1036 /* Adjust parent. */
1038 if (n
->parent
!= NULL
) {
1039 if (n
->parent
->child
== n
)
1040 n
->parent
->child
= n
->next
;
1041 if (n
->parent
->last
== n
)
1042 n
->parent
->last
= n
->prev
;
1045 /* Adjust parse point. */
1049 if (man
->last
== n
) {
1050 if (n
->prev
== NULL
) {
1051 man
->last
= n
->parent
;
1052 man
->next
= ROFF_NEXT_CHILD
;
1054 man
->last
= n
->prev
;
1055 man
->next
= ROFF_NEXT_SIBLING
;
1058 if (man
->first
== n
)
1063 roff_node_relink(struct roff_man
*man
, struct roff_node
*n
)
1065 roff_node_unlink(man
, n
);
1066 n
->prev
= n
->next
= NULL
;
1067 roff_node_append(man
, n
);
1071 roff_node_free(struct roff_node
*n
)
1074 if (n
->args
!= NULL
)
1075 mdoc_argv_free(n
->args
);
1076 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1078 eqn_box_free(n
->eqn
);
1084 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1087 while (n
->child
!= NULL
)
1088 roff_node_delete(man
, n
->child
);
1089 roff_node_unlink(man
, n
);
1094 deroff(char **dest
, const struct roff_node
*n
)
1099 if (n
->type
!= ROFFT_TEXT
) {
1100 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1105 /* Skip leading whitespace. */
1107 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1108 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1109 strchr(" %&0^|~", cp
[1]) != NULL
)
1111 else if ( ! isspace((unsigned char)*cp
))
1115 /* Skip trailing backslash. */
1118 if (sz
> 0 && cp
[sz
- 1] == '\\')
1121 /* Skip trailing whitespace. */
1124 if ( ! isspace((unsigned char)cp
[sz
-1]))
1127 /* Skip empty strings. */
1132 if (*dest
== NULL
) {
1133 *dest
= mandoc_strndup(cp
, sz
);
1137 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1142 /* --- main functions of the roff parser ---------------------------------- */
1145 * In the current line, expand escape sequences that tend to get
1146 * used in numerical expressions and conditional requests.
1147 * Also check the syntax of the remaining escape sequences.
1150 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1152 struct mctx
*ctx
; /* current macro call context */
1153 char ubuf
[24]; /* buffer to print the number */
1154 struct roff_node
*n
; /* used for header comments */
1155 const char *start
; /* start of the string to process */
1156 char *stesc
; /* start of an escape sequence ('\\') */
1157 const char *esct
; /* type of esccape sequence */
1158 char *ep
; /* end of comment string */
1159 const char *stnam
; /* start of the name, after "[(*" */
1160 const char *cp
; /* end of the name, e.g. before ']' */
1161 const char *res
; /* the string to be substituted */
1162 char *nbuf
; /* new buffer to copy buf->buf to */
1163 size_t maxl
; /* expected length of the escape name */
1164 size_t naml
; /* actual length of the escape name */
1165 size_t asz
; /* length of the replacement */
1166 size_t rsz
; /* length of the rest of the string */
1167 int inaml
; /* length returned from mandoc_escape() */
1168 int expand_count
; /* to avoid infinite loops */
1169 int npos
; /* position in numeric expression */
1170 int arg_complete
; /* argument not interrupted by eol */
1171 int quote_args
; /* true for \\$@, false for \\$* */
1172 int done
; /* no more input available */
1173 int deftype
; /* type of definition to paste */
1174 int rcsid
; /* kind of RCS id seen */
1175 enum mandocerr err
; /* for escape sequence problems */
1176 char sign
; /* increment number register */
1177 char term
; /* character terminating the escape */
1179 /* Search forward for comments. */
1182 start
= buf
->buf
+ pos
;
1183 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1184 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1187 if (*stesc
!= '"' && *stesc
!= '#')
1190 /* Comment found, look for RCS id. */
1193 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1194 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1196 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1197 rcsid
= 1 << MANDOC_OS_NETBSD
;
1201 isalnum((unsigned char)*cp
) == 0 &&
1202 strchr(cp
, '$') != NULL
) {
1203 if (r
->man
->meta
.rcsids
& rcsid
)
1204 mandoc_msg(MANDOCERR_RCS_REP
, ln
,
1205 (int)(stesc
- buf
->buf
) + 1,
1207 r
->man
->meta
.rcsids
|= rcsid
;
1210 /* Handle trailing whitespace. */
1212 ep
= strchr(stesc
--, '\0') - 1;
1217 if (*ep
== ' ' || *ep
== '\t')
1218 mandoc_msg(MANDOCERR_SPACE_EOL
,
1219 ln
, (int)(ep
- buf
->buf
), NULL
);
1222 * Save comments preceding the title macro
1223 * in the syntax tree.
1226 if (r
->format
== 0) {
1227 while (*ep
== ' ' || *ep
== '\t')
1230 n
= roff_node_alloc(r
->man
,
1231 ln
, stesc
+ 1 - buf
->buf
,
1232 ROFFT_COMMENT
, TOKEN_NONE
);
1233 n
->string
= mandoc_strdup(stesc
+ 2);
1234 roff_node_append(r
->man
, n
);
1235 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1236 r
->man
->next
= ROFF_NEXT_SIBLING
;
1239 /* Line continuation with comment. */
1241 if (stesc
[1] == '#') {
1243 return ROFF_IGN
| ROFF_APPEND
;
1246 /* Discard normal comments. */
1248 while (stesc
> start
&& stesc
[-1] == ' ' &&
1249 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1258 /* Notice the end of the input. */
1260 if (*stesc
== '\n') {
1266 while (stesc
>= start
) {
1268 /* Search backwards for the next backslash. */
1270 if (*stesc
!= r
->escape
) {
1271 if (*stesc
== '\\') {
1273 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1274 buf
->buf
, stesc
+ 1) + 1;
1276 stesc
= nbuf
+ (stesc
- buf
->buf
);
1284 /* If it is escaped, skip it. */
1286 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1287 if (*cp
!= r
->escape
)
1290 if ((stesc
- cp
) % 2 == 0) {
1294 } else if (stesc
[1] != '\0') {
1301 return ROFF_IGN
| ROFF_APPEND
;
1304 /* Decide whether to expand or to check only. */
1322 if (sign
== '+' || sign
== '-')
1328 switch(mandoc_escape(&cp
, &stnam
, &inaml
)) {
1329 case ESCAPE_SPECIAL
:
1330 if (mchars_spec2cp(stnam
, inaml
) >= 0)
1334 err
= MANDOCERR_ESC_BAD
;
1337 err
= MANDOCERR_ESC_UNDEF
;
1340 err
= MANDOCERR_ESC_UNSUPP
;
1345 if (err
!= MANDOCERR_OK
)
1346 mandoc_msg(err
, ln
, (int)(stesc
- buf
->buf
),
1347 "%.*s", (int)(cp
- stesc
), stesc
);
1352 if (EXPAND_LIMIT
< ++expand_count
) {
1353 mandoc_msg(MANDOCERR_ROFFLOOP
,
1354 ln
, (int)(stesc
- buf
->buf
), NULL
);
1359 * The third character decides the length
1360 * of the name of the string or register.
1361 * Save a pointer to the name.
1388 /* Advance to the end of the name. */
1392 while (maxl
== 0 || naml
< maxl
) {
1394 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
1395 (int)(stesc
- buf
->buf
), "%s", stesc
);
1399 if (maxl
== 0 && *cp
== term
) {
1403 if (*cp
++ != '\\' || *esct
!= 'w') {
1407 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1408 case ESCAPE_SPECIAL
:
1409 case ESCAPE_UNICODE
:
1410 case ESCAPE_NUMBERED
:
1412 case ESCAPE_OVERSTRIKE
:
1421 * Retrieve the replacement string; if it is
1422 * undefined, resume searching for escapes.
1428 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1429 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1432 * If not overriden, let \*(.T
1433 * through to the formatters.
1436 if (res
== NULL
&& naml
== 2 &&
1437 stnam
[0] == '.' && stnam
[1] == 'T') {
1438 roff_setstrn(&r
->strtab
,
1439 ".T", 2, NULL
, 0, 0);
1446 if (r
->mstackpos
< 0) {
1447 mandoc_msg(MANDOCERR_ARG_UNDEF
, ln
,
1448 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1451 ctx
= r
->mstack
+ r
->mstackpos
;
1452 npos
= esct
[1] - '1';
1453 if (npos
>= 0 && npos
<= 8) {
1454 res
= npos
< ctx
->argc
?
1455 ctx
->argv
[npos
] : "";
1460 else if (esct
[1] == '@')
1463 mandoc_msg(MANDOCERR_ARG_NONUM
, ln
,
1464 (int)(stesc
- buf
->buf
), "%.3s", stesc
);
1468 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1472 asz
+= 2; /* quotes */
1473 asz
+= strlen(ctx
->argv
[npos
]);
1476 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1478 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1480 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1482 stesc
= nbuf
+ (stesc
- buf
->buf
);
1485 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1487 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1492 cp
= ctx
->argv
[npos
];
1501 ubuf
[0] = arg_complete
&&
1502 roff_evalnum(r
, ln
, stnam
, &npos
,
1503 NULL
, ROFFNUM_SCALE
) &&
1504 stnam
+ npos
+ 1 == cp
? '1' : '0';
1509 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1510 roff_getregn(r
, stnam
, naml
, sign
));
1515 /* use even incomplete args */
1516 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1523 mandoc_msg(MANDOCERR_STR_UNDEF
,
1524 ln
, (int)(stesc
- buf
->buf
),
1525 "%.*s", (int)naml
, stnam
);
1527 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1528 mandoc_msg(MANDOCERR_ROFFLOOP
,
1529 ln
, (int)(stesc
- buf
->buf
), NULL
);
1533 /* Replace the escape sequence by the string. */
1536 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1537 buf
->buf
, res
, cp
) + 1;
1539 /* Prepare for the next replacement. */
1542 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1550 * Parse a quoted or unquoted roff-style request or macro argument.
1551 * Return a pointer to the parsed argument, which is either the original
1552 * pointer or advanced by one byte in case the argument is quoted.
1553 * NUL-terminate the argument in place.
1554 * Collapse pairs of quotes inside quoted arguments.
1555 * Advance the argument pointer to the next argument,
1556 * or to the NUL byte terminating the argument line.
1559 mandoc_getarg(char **cpp
, int ln
, int *pos
)
1562 int quoted
, pairs
, white
;
1564 /* Quoting can only start with a new word. */
1567 if ('"' == *start
) {
1574 for (cp
= start
; '\0' != *cp
; cp
++) {
1577 * Move the following text left
1578 * after quoted quotes and after "\\" and "\t".
1583 if ('\\' == cp
[0]) {
1585 * In copy mode, translate double to single
1586 * backslashes and backslash-t to literal tabs.
1598 /* Skip escaped blanks. */
1605 } else if (0 == quoted
) {
1607 /* Unescaped blanks end unquoted args. */
1611 } else if ('"' == cp
[0]) {
1613 /* Quoted quotes collapse. */
1617 /* Unquoted quotes end quoted args. */
1624 /* Quoted argument without a closing quote. */
1626 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
1628 /* NUL-terminate this argument and move to the next one. */
1636 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
1639 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
1640 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
1647 * Process text streams.
1650 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1656 enum mandoc_esc esc
;
1658 /* Spring the input line trap. */
1660 if (roffit_lines
== 1) {
1661 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1668 return ROFF_REPARSE
;
1669 } else if (roffit_lines
> 1)
1672 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1673 if (roffce_lines
< 1) {
1674 r
->man
->last
= roffce_node
;
1675 r
->man
->next
= ROFF_NEXT_SIBLING
;
1682 /* Convert all breakable hyphens into ASCII_HYPH. */
1684 start
= p
= buf
->buf
+ pos
;
1686 while (*p
!= '\0') {
1687 sz
= strcspn(p
, "-\\");
1694 /* Skip over escapes. */
1696 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1697 if (esc
== ESCAPE_ERROR
)
1702 } else if (p
== start
) {
1707 if (isalpha((unsigned char)p
[-1]) &&
1708 isalpha((unsigned char)p
[1]))
1716 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1720 int pos
; /* parse point */
1721 int spos
; /* saved parse point for messages */
1722 int ppos
; /* original offset in buf->buf */
1723 int ctl
; /* macro line (boolean) */
1727 /* Handle in-line equation delimiters. */
1729 if (r
->tbl
== NULL
&&
1730 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1731 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1732 e
= roff_eqndelim(r
, buf
, pos
);
1733 if (e
== ROFF_REPARSE
)
1735 assert(e
== ROFF_CONT
);
1738 /* Expand some escape sequences. */
1740 e
= roff_res(r
, buf
, ln
, pos
);
1741 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1743 assert(e
== ROFF_CONT
);
1745 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1748 * First, if a scope is open and we're not a macro, pass the
1749 * text through the macro's filter.
1750 * Equations process all content themselves.
1751 * Tables process almost all content themselves, but we want
1752 * to warn about macros before passing it there.
1755 if (r
->last
!= NULL
&& ! ctl
) {
1757 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1758 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1763 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1764 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1767 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1768 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1769 roff_addtbl(r
->man
, ln
, r
->tbl
);
1773 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1775 /* Skip empty request lines. */
1777 if (buf
->buf
[pos
] == '"') {
1778 mandoc_msg(MANDOCERR_COMMENT_BAD
, ln
, pos
, NULL
);
1780 } else if (buf
->buf
[pos
] == '\0')
1784 * If a scope is open, go to the child handler for that macro,
1785 * as it may want to preprocess before doing anything with it.
1786 * Don't do so if an equation is open.
1791 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1794 /* No scope is open. This is a new request or macro. */
1797 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1799 /* Tables ignore most macros. */
1801 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1802 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1803 mandoc_msg(MANDOCERR_TBLMACRO
,
1804 ln
, pos
, "%s", buf
->buf
+ spos
);
1805 if (t
!= TOKEN_NONE
)
1807 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1809 while (buf
->buf
[pos
] == ' ')
1811 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1812 roff_addtbl(r
->man
, ln
, r
->tbl
);
1816 /* For now, let high level macros abort .ce mode. */
1818 if (ctl
&& roffce_node
!= NULL
&&
1819 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1820 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1821 r
->man
->last
= roffce_node
;
1822 r
->man
->next
= ROFF_NEXT_SIBLING
;
1828 * This is neither a roff request nor a user-defined macro.
1829 * Let the standard macro set parsers handle it.
1832 if (t
== TOKEN_NONE
)
1835 /* Execute a roff request or a user defined macro. */
1837 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1841 * Internal interface function to tell the roff parser that execution
1842 * of the current macro ended. This is required because macro
1843 * definitions usually do not end with a .return request.
1846 roff_userret(struct roff
*r
)
1851 assert(r
->mstackpos
>= 0);
1852 ctx
= r
->mstack
+ r
->mstackpos
;
1853 for (i
= 0; i
< ctx
->argc
; i
++)
1860 roff_endparse(struct roff
*r
)
1862 if (r
->last
!= NULL
)
1863 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->last
->line
,
1864 r
->last
->col
, "%s", roff_name
[r
->last
->tok
]);
1866 if (r
->eqn
!= NULL
) {
1867 mandoc_msg(MANDOCERR_BLK_NOEND
,
1868 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1873 if (r
->tbl
!= NULL
) {
1880 * Parse a roff node's type from the input buffer. This must be in the
1881 * form of ".foo xxx" in the usual way.
1883 static enum roff_tok
1884 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1894 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1898 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1900 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1901 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1910 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1913 if (t
!= TOKEN_NONE
)
1915 else if (deftype
== ROFFDEF_UNDEF
) {
1916 /* Using an undefined macro defines it to be empty. */
1917 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1918 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1923 /* --- handling of request blocks ----------------------------------------- */
1926 roff_cblock(ROFF_ARGS
)
1930 * A block-close `..' should only be invoked as a child of an
1931 * ignore macro, otherwise raise a warning and just ignore it.
1934 if (r
->last
== NULL
) {
1935 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
1939 switch (r
->last
->tok
) {
1941 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1944 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1949 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
1953 if (buf
->buf
[pos
] != '\0')
1954 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
1955 ".. %s", buf
->buf
+ pos
);
1958 roffnode_cleanscope(r
);
1964 roffnode_cleanscope(struct roff
*r
)
1969 while (r
->last
!= NULL
) {
1970 if (--r
->last
->endspan
!= 0)
1972 inloop
+= roffnode_pop(r
);
1978 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1980 if (NULL
== r
->last
) {
1981 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
1985 switch (r
->last
->tok
) {
1992 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
1996 if (r
->last
->endspan
> -1) {
1997 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2001 return roffnode_pop(r
) + roffnode_cleanscope(r
);
2005 roff_block(ROFF_ARGS
)
2007 const char *name
, *value
;
2008 char *call
, *cp
, *iname
, *rname
;
2009 size_t csz
, namesz
, rsz
;
2012 /* Ignore groff compatibility mode for now. */
2014 if (tok
== ROFF_de1
)
2016 else if (tok
== ROFF_dei1
)
2018 else if (tok
== ROFF_am1
)
2020 else if (tok
== ROFF_ami1
)
2023 /* Parse the macro name argument. */
2025 cp
= buf
->buf
+ pos
;
2026 if (tok
== ROFF_ig
) {
2031 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2032 iname
[namesz
] = '\0';
2035 /* Resolve the macro name argument if it is indirect. */
2037 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2038 deftype
= ROFFDEF_USER
;
2039 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2041 mandoc_msg(MANDOCERR_STR_UNDEF
,
2042 ln
, (int)(iname
- buf
->buf
),
2043 "%.*s", (int)namesz
, iname
);
2046 namesz
= strlen(name
);
2050 if (namesz
== 0 && tok
!= ROFF_ig
) {
2051 mandoc_msg(MANDOCERR_REQ_EMPTY
,
2052 ln
, ppos
, "%s", roff_name
[tok
]);
2056 roffnode_push(r
, tok
, name
, ln
, ppos
);
2059 * At the beginning of a `de' macro, clear the existing string
2060 * with the same name, if there is one. New content will be
2061 * appended from roff_block_text() in multiline mode.
2064 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
2065 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
2066 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2067 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
2068 deftype
= ROFFDEF_ANY
;
2069 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2070 switch (deftype
) { /* Before appending, ... */
2071 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
2072 roff_setstrn(&r
->strtab
, name
, namesz
,
2073 value
, strlen(value
), 0);
2075 case ROFFDEF_REN
: /* call original standard macro. */
2076 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2077 (int)strlen(value
), value
);
2078 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2079 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2082 case ROFFDEF_STD
: /* rename and call standard macro. */
2083 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
2084 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
2085 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2087 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2099 /* Get the custom end marker. */
2102 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2104 /* Resolve the end marker if it is indirect. */
2106 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2107 deftype
= ROFFDEF_USER
;
2108 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2110 mandoc_msg(MANDOCERR_STR_UNDEF
,
2111 ln
, (int)(iname
- buf
->buf
),
2112 "%.*s", (int)namesz
, iname
);
2115 namesz
= strlen(name
);
2120 r
->last
->end
= mandoc_strndup(name
, namesz
);
2123 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2124 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2130 roff_block_sub(ROFF_ARGS
)
2136 * First check whether a custom macro exists at this level. If
2137 * it does, then check against it. This is some of groff's
2138 * stranger behaviours. If we encountered a custom end-scope
2139 * tag and that tag also happens to be a "real" macro, then we
2140 * need to try interpreting it again as a real macro. If it's
2141 * not, then return ignore. Else continue.
2145 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2146 if (buf
->buf
[i
] != r
->last
->end
[j
])
2149 if (r
->last
->end
[j
] == '\0' &&
2150 (buf
->buf
[i
] == '\0' ||
2151 buf
->buf
[i
] == ' ' ||
2152 buf
->buf
[i
] == '\t')) {
2154 roffnode_cleanscope(r
);
2156 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2160 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2168 * If we have no custom end-query or lookup failed, then try
2169 * pulling it out of the hashtable.
2172 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2174 if (t
!= ROFF_cblock
) {
2176 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2180 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2184 roff_block_text(ROFF_ARGS
)
2188 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2194 roff_cond_sub(ROFF_ARGS
)
2197 int endloop
, irc
, rr
;
2202 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2203 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2204 if (roffnode_cleanscope(r
))
2208 * If `\}' occurs on a macro line without a preceding macro,
2209 * drop the line completely.
2212 ep
= buf
->buf
+ pos
;
2213 if (ep
[0] == '\\' && ep
[1] == '}')
2217 * The closing delimiter `\}' rewinds the conditional scope
2218 * but is otherwise ignored when interpreting the line.
2221 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2224 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2225 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2238 * Fully handle known macros when they are structurally
2239 * required or when the conditional evaluated to true.
2242 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2243 irc
|= t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
) ?
2244 (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) :
2245 rr
? ROFF_CONT
: ROFF_IGN
;
2250 roff_cond_text(ROFF_ARGS
)
2253 int endloop
, irc
, rr
;
2257 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2258 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2259 if (roffnode_cleanscope(r
))
2263 * If `\}' occurs on a text line with neither preceding
2264 * nor following characters, drop the line completely.
2267 ep
= buf
->buf
+ pos
;
2268 if (strcmp(ep
, "\\}") == 0)
2272 * The closing delimiter `\}' rewinds the conditional scope
2273 * but is otherwise ignored when interpreting the line.
2276 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2279 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2280 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2296 /* --- handling of numeric and conditional expressions -------------------- */
2299 * Parse a single signed integer number. Stop at the first non-digit.
2300 * If there is at least one digit, return success and advance the
2301 * parse point, else return failure and let the parse point unchanged.
2302 * Ignore overflows, treat them just like the C language.
2305 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2307 int myres
, scaled
, n
, p
;
2314 if (n
|| v
[p
] == '+')
2317 if (flags
& ROFFNUM_WHITE
)
2318 while (isspace((unsigned char)v
[p
]))
2321 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2322 *res
= 10 * *res
+ v
[p
] - '0';
2329 /* Each number may be followed by one optional scaling unit. */
2333 scaled
= *res
* 65536;
2336 scaled
= *res
* 240;
2339 scaled
= *res
* 240 / 2.54;
2350 scaled
= *res
* 10 / 3;
2356 scaled
= *res
* 6 / 25;
2363 if (flags
& ROFFNUM_SCALE
)
2371 * Evaluate a string comparison condition.
2372 * The first character is the delimiter.
2373 * Succeed if the string up to its second occurrence
2374 * matches the string up to its third occurence.
2375 * Advance the cursor after the third occurrence
2376 * or lacking that, to the end of the line.
2379 roff_evalstrcond(const char *v
, int *pos
)
2381 const char *s1
, *s2
, *s3
;
2385 s1
= v
+ *pos
; /* initial delimiter */
2386 s2
= s1
+ 1; /* for scanning the first string */
2387 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2389 if (NULL
== s3
) /* found no middle delimiter */
2392 while ('\0' != *++s3
) {
2393 if (*s2
!= *s3
) { /* mismatch */
2394 s3
= strchr(s3
, *s1
);
2397 if (*s3
== *s1
) { /* found the final delimiter */
2406 s3
= strchr(s2
, '\0');
2407 else if (*s3
!= '\0')
2414 * Evaluate an optionally negated single character, numerical,
2415 * or string condition.
2418 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2420 const char *start
, *end
;
2423 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2425 if ('!' == v
[*pos
]) {
2446 } while (v
[*pos
] == ' ');
2449 * Quirk for groff compatibility:
2450 * The horizontal tab is neither available nor unavailable.
2453 if (v
[*pos
] == '\t') {
2458 /* Printable ASCII characters are available. */
2460 if (v
[*pos
] != '\\') {
2466 switch (mandoc_escape(&end
, &start
, &len
)) {
2467 case ESCAPE_SPECIAL
:
2468 istrue
= mchars_spec2cp(start
, len
) != -1;
2470 case ESCAPE_UNICODE
:
2473 case ESCAPE_NUMBERED
:
2474 istrue
= mchars_num2char(start
, len
) != -1;
2481 return istrue
== wanttrue
;
2488 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2491 else if (v
[*pos
] == 'r')
2492 istrue
= roff_hasregn(r
, name
, sz
);
2494 deftype
= ROFFDEF_ANY
;
2495 roff_getstrn(r
, name
, sz
, &deftype
);
2499 return istrue
== wanttrue
;
2505 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2506 return (number
> 0) == wanttrue
;
2507 else if (*pos
== savepos
)
2508 return roff_evalstrcond(v
, pos
) == wanttrue
;
2514 roff_line_ignore(ROFF_ARGS
)
2521 roff_insec(ROFF_ARGS
)
2524 mandoc_msg(MANDOCERR_REQ_INSEC
, ln
, ppos
, "%s", roff_name
[tok
]);
2529 roff_unsupp(ROFF_ARGS
)
2532 mandoc_msg(MANDOCERR_REQ_UNSUPP
, ln
, ppos
, "%s", roff_name
[tok
]);
2537 roff_cond(ROFF_ARGS
)
2541 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2544 * An `.el' has no conditional body: it will consume the value
2545 * of the current rstack entry set in prior `ie' calls or
2548 * If we're not an `el', however, then evaluate the conditional.
2551 r
->last
->rule
= tok
== ROFF_el
?
2552 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2553 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2556 * An if-else will put the NEGATION of the current evaluated
2557 * conditional into the stack of rules.
2560 if (tok
== ROFF_ie
) {
2561 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2563 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2564 r
->rstacksz
, sizeof(int));
2566 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2569 /* If the parent has false as its rule, then so do we. */
2571 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2576 * If there is nothing on the line after the conditional,
2577 * not even whitespace, use next-line scope.
2578 * Except that .while does not support next-line scope.
2581 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2582 r
->last
->endspan
= 2;
2586 while (buf
->buf
[pos
] == ' ')
2589 /* An opening brace requests multiline scope. */
2591 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2592 r
->last
->endspan
= -1;
2594 while (buf
->buf
[pos
] == ' ')
2600 * Anything else following the conditional causes
2601 * single-line scope. Warn if the scope contains
2602 * nothing but trailing whitespace.
2605 if (buf
->buf
[pos
] == '\0')
2606 mandoc_msg(MANDOCERR_COND_EMPTY
,
2607 ln
, ppos
, "%s", roff_name
[tok
]);
2609 r
->last
->endspan
= 1;
2614 if (tok
== ROFF_while
)
2626 /* Ignore groff compatibility mode for now. */
2628 if (tok
== ROFF_ds1
)
2630 else if (tok
== ROFF_as1
)
2634 * The first word is the name of the string.
2635 * If it is empty or terminated by an escape sequence,
2636 * abort the `ds' request without defining anything.
2639 name
= string
= buf
->buf
+ pos
;
2643 namesz
= roff_getname(r
, &string
, ln
, pos
);
2644 if (name
[namesz
] == '\\')
2647 /* Read past the initial double-quote, if any. */
2651 /* The rest is the value. */
2652 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2654 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2659 * Parse a single operator, one or two characters long.
2660 * If the operator is recognized, return success and advance the
2661 * parse point, else return failure and let the parse point unchanged.
2664 roff_getop(const char *v
, int *pos
, char *res
)
2679 switch (v
[*pos
+ 1]) {
2697 switch (v
[*pos
+ 1]) {
2711 if ('=' == v
[*pos
+ 1])
2723 * Evaluate either a parenthesized numeric expression
2724 * or a single signed integer number.
2727 roff_evalpar(struct roff
*r
, int ln
,
2728 const char *v
, int *pos
, int *res
, int flags
)
2732 return roff_getnum(v
, pos
, res
, flags
);
2735 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2739 * Omission of the closing parenthesis
2740 * is an error in validation mode,
2741 * but ignored in evaluation mode.
2746 else if (NULL
== res
)
2753 * Evaluate a complete numeric expression.
2754 * Proceed left to right, there is no concept of precedence.
2757 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2758 int *pos
, int *res
, int flags
)
2760 int mypos
, operand2
;
2768 if (flags
& ROFFNUM_WHITE
)
2769 while (isspace((unsigned char)v
[*pos
]))
2772 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2776 if (flags
& ROFFNUM_WHITE
)
2777 while (isspace((unsigned char)v
[*pos
]))
2780 if ( ! roff_getop(v
, pos
, &operator))
2783 if (flags
& ROFFNUM_WHITE
)
2784 while (isspace((unsigned char)v
[*pos
]))
2787 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2790 if (flags
& ROFFNUM_WHITE
)
2791 while (isspace((unsigned char)v
[*pos
]))
2808 if (operand2
== 0) {
2809 mandoc_msg(MANDOCERR_DIVZERO
,
2817 if (operand2
== 0) {
2818 mandoc_msg(MANDOCERR_DIVZERO
,
2826 *res
= *res
< operand2
;
2829 *res
= *res
> operand2
;
2832 *res
= *res
<= operand2
;
2835 *res
= *res
>= operand2
;
2838 *res
= *res
== operand2
;
2841 *res
= *res
!= operand2
;
2844 *res
= *res
&& operand2
;
2847 *res
= *res
|| operand2
;
2850 if (operand2
< *res
)
2854 if (operand2
> *res
)
2864 /* --- register management ------------------------------------------------ */
2867 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2869 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2873 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2874 int val
, char sign
, int step
)
2876 struct roffreg
*reg
;
2878 /* Search for an existing register with the same name. */
2881 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2882 strncmp(reg
->key
.p
, name
, len
) != 0))
2886 /* Create a new register. */
2887 reg
= mandoc_malloc(sizeof(struct roffreg
));
2888 reg
->key
.p
= mandoc_strndup(name
, len
);
2892 reg
->next
= r
->regtab
;
2898 else if ('-' == sign
)
2902 if (step
!= INT_MIN
)
2907 * Handle some predefined read-only number registers.
2908 * For now, return -1 if the requested register is not predefined;
2909 * in case a predefined read-only register having the value -1
2910 * were to turn up, another special value would have to be chosen.
2913 roff_getregro(const struct roff
*r
, const char *name
)
2917 case '$': /* Number of arguments of the last macro evaluated. */
2918 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
2919 case 'A': /* ASCII approximation mode is always off. */
2921 case 'g': /* Groff compatibility mode is always on. */
2923 case 'H': /* Fixed horizontal resolution. */
2925 case 'j': /* Always adjust left margin only. */
2927 case 'T': /* Some output device is always defined. */
2929 case 'V': /* Fixed vertical resolution. */
2937 roff_getreg(struct roff
*r
, const char *name
)
2939 return roff_getregn(r
, name
, strlen(name
), '\0');
2943 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2945 struct roffreg
*reg
;
2948 if ('.' == name
[0] && 2 == len
) {
2949 val
= roff_getregro(r
, name
+ 1);
2954 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2955 if (len
== reg
->key
.sz
&&
2956 0 == strncmp(name
, reg
->key
.p
, len
)) {
2959 reg
->val
+= reg
->step
;
2962 reg
->val
-= reg
->step
;
2971 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
2976 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2978 struct roffreg
*reg
;
2981 if ('.' == name
[0] && 2 == len
) {
2982 val
= roff_getregro(r
, name
+ 1);
2987 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2988 if (len
== reg
->key
.sz
&&
2989 0 == strncmp(name
, reg
->key
.p
, len
))
2996 roff_freereg(struct roffreg
*reg
)
2998 struct roffreg
*old_reg
;
3000 while (NULL
!= reg
) {
3011 char *key
, *val
, *step
;
3016 key
= val
= buf
->buf
+ pos
;
3020 keysz
= roff_getname(r
, &val
, ln
, pos
);
3021 if (key
[keysz
] == '\\')
3025 if (sign
== '+' || sign
== '-')
3029 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
3033 while (isspace((unsigned char)*step
))
3035 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
3038 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
3045 struct roffreg
*reg
, **prev
;
3049 name
= cp
= buf
->buf
+ pos
;
3052 namesz
= roff_getname(r
, &cp
, ln
, pos
);
3053 name
[namesz
] = '\0';
3058 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
3070 /* --- handler functions for roff requests -------------------------------- */
3079 cp
= buf
->buf
+ pos
;
3080 while (*cp
!= '\0') {
3082 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
3083 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
3084 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3085 if (name
[namesz
] == '\\')
3096 /* Parse the number of lines. */
3098 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
3099 mandoc_msg(MANDOCERR_IT_NONUM
,
3100 ln
, ppos
, "%s", buf
->buf
+ 1);
3104 while (isspace((unsigned char)buf
->buf
[pos
]))
3108 * Arm the input line trap.
3109 * Special-casing "an-trap" is an ugly workaround to cope
3110 * with DocBook stupidly fiddling with man(7) internals.
3114 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3115 strcmp(buf
->buf
+ pos
, "an-trap") ?
3116 buf
->buf
+ pos
: "br");
3124 enum roff_tok t
, te
;
3131 r
->format
= MPARSE_MDOC
;
3132 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3138 r
->format
= MPARSE_MAN
;
3139 mask
= MPARSE_QUICK
;
3144 if ((r
->options
& mask
) == 0)
3145 for (t
= tok
; t
< te
; t
++)
3146 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3153 if (r
->tbl
== NULL
) {
3154 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "TE");
3157 if (tbl_end(r
->tbl
, 0) == 0) {
3160 buf
->buf
= mandoc_strdup(".sp");
3163 return ROFF_REPARSE
;
3174 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "T&");
3176 tbl_restart(ln
, ppos
, r
->tbl
);
3182 * Handle in-line equation delimiters.
3185 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3188 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3191 * Outside equations, look for an opening delimiter.
3192 * If we are inside an equation, we already know it is
3193 * in-line, or this function wouldn't have been called;
3194 * so look for a closing delimiter.
3197 cp1
= buf
->buf
+ pos
;
3198 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3199 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3204 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3206 /* Handle preceding text, protecting whitespace. */
3208 if (*buf
->buf
!= '\0') {
3215 * Prepare replacing the delimiter with an equation macro
3216 * and drop leading white space from the equation.
3219 if (r
->eqn
== NULL
) {
3226 /* Handle following text, protecting whitespace. */
3234 /* Do the actual replacement. */
3236 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3237 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3241 /* Toggle the in-line state of the eqn subsystem. */
3243 r
->eqn_inline
= r
->eqn
== NULL
;
3244 return ROFF_REPARSE
;
3250 struct roff_node
*n
;
3252 if (r
->man
->macroset
== MACROSET_MAN
)
3253 man_breakscope(r
->man
, ROFF_EQ
);
3254 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3255 if (ln
> r
->man
->last
->line
)
3256 n
->flags
|= NODE_LINE
;
3257 n
->eqn
= eqn_box_new();
3258 roff_node_append(r
->man
, n
);
3259 r
->man
->next
= ROFF_NEXT_SIBLING
;
3261 assert(r
->eqn
== NULL
);
3262 if (r
->last_eqn
== NULL
)
3263 r
->last_eqn
= eqn_alloc();
3265 eqn_reset(r
->last_eqn
);
3266 r
->eqn
= r
->last_eqn
;
3269 if (buf
->buf
[pos
] != '\0')
3270 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3271 ".EQ %s", buf
->buf
+ pos
);
3279 if (r
->eqn
!= NULL
) {
3283 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "EN");
3284 if (buf
->buf
[pos
] != '\0')
3285 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3286 "EN %s", buf
->buf
+ pos
);
3293 if (r
->tbl
!= NULL
) {
3294 mandoc_msg(MANDOCERR_BLK_BROKEN
, ln
, ppos
, "TS breaks TS");
3297 r
->tbl
= tbl_alloc(ppos
, ln
, r
->last_tbl
);
3298 if (r
->last_tbl
== NULL
)
3299 r
->first_tbl
= r
->tbl
;
3300 r
->last_tbl
= r
->tbl
;
3305 roff_onearg(ROFF_ARGS
)
3307 struct roff_node
*n
;
3311 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3312 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3314 man_breakscope(r
->man
, tok
);
3316 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3317 r
->man
->last
= roffce_node
;
3318 r
->man
->next
= ROFF_NEXT_SIBLING
;
3321 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3324 cp
= buf
->buf
+ pos
;
3326 while (*cp
!= '\0' && *cp
!= ' ')
3331 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3332 ln
, (int)(cp
- buf
->buf
),
3333 "%s ... %s", roff_name
[tok
], cp
);
3334 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3337 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3338 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3339 roff_word_alloc(r
->man
, ln
, pos
, "1");
3340 r
->man
->last
->flags
|= NODE_NOSRC
;
3343 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3344 &roffce_lines
, 0) == 0) {
3345 mandoc_msg(MANDOCERR_CE_NONUM
,
3346 ln
, pos
, "ce %s", buf
->buf
+ pos
);
3349 if (roffce_lines
< 1) {
3350 r
->man
->last
= r
->man
->last
->parent
;
3354 roffce_node
= r
->man
->last
->parent
;
3356 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3359 n
->flags
|= NODE_LINE
;
3360 r
->man
->next
= ROFF_NEXT_SIBLING
;
3365 roff_manyarg(ROFF_ARGS
)
3367 struct roff_node
*n
;
3370 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3373 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3374 while (*ep
!= '\0' && *ep
!= ' ')
3378 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3381 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3383 r
->man
->next
= ROFF_NEXT_SIBLING
;
3390 char *oldn
, *newn
, *end
, *value
;
3391 size_t oldsz
, newsz
, valsz
;
3393 newn
= oldn
= buf
->buf
+ pos
;
3397 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3398 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3402 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3406 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3408 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3409 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3417 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3418 man_breakscope(r
->man
, ROFF_br
);
3419 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3420 if (buf
->buf
[pos
] != '\0')
3421 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3422 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3423 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3424 r
->man
->next
= ROFF_NEXT_SIBLING
;
3435 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3439 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3440 ln
, p
- buf
->buf
, "cc ... %s", p
);
3446 roff_char(ROFF_ARGS
)
3448 const char *p
, *kp
, *vp
;
3452 /* Parse the character to be replaced. */
3454 kp
= buf
->buf
+ pos
;
3456 if (*kp
== '\0' || (*kp
== '\\' &&
3457 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3458 (*p
!= ' ' && *p
!= '\0')) {
3459 mandoc_msg(MANDOCERR_CHAR_ARG
, ln
, pos
, "char %s", kp
);
3467 * If the replacement string contains a font escape sequence,
3468 * we have to restore the font at the end.
3474 while (*p
!= '\0') {
3477 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3479 case ESCAPE_FONTROMAN
:
3480 case ESCAPE_FONTITALIC
:
3481 case ESCAPE_FONTBOLD
:
3484 case ESCAPE_FONTPREV
:
3492 mandoc_msg(MANDOCERR_CHAR_FONT
,
3493 ln
, (int)(vp
- buf
->buf
), "%s", vp
);
3496 * Approximate the effect of .char using the .tr tables.
3497 * XXX In groff, .char and .tr interact differently.
3501 if (r
->xtab
== NULL
)
3502 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3503 assert((unsigned int)*kp
< 128);
3504 free(r
->xtab
[(int)*kp
].p
);
3505 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3506 "%s%s", vp
, font
? "\fP" : "");
3508 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3510 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3526 mandoc_msg(MANDOCERR_ARG_EXCESS
, ln
,
3527 (int)(p
- buf
->buf
), "ec ... %s", p
);
3536 if (buf
->buf
[pos
] != '\0')
3537 mandoc_msg(MANDOCERR_ARG_SKIP
,
3538 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3545 while (buf
->buf
[pos
] == ' ')
3554 const char *p
, *first
, *second
;
3556 enum mandoc_esc esc
;
3561 mandoc_msg(MANDOCERR_REQ_EMPTY
, ln
, ppos
, "tr");
3565 while (*p
!= '\0') {
3569 if (*first
== '\\') {
3570 esc
= mandoc_escape(&p
, NULL
, NULL
);
3571 if (esc
== ESCAPE_ERROR
) {
3572 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3573 (int)(p
- buf
->buf
), "%s", first
);
3576 fsz
= (size_t)(p
- first
);
3580 if (*second
== '\\') {
3581 esc
= mandoc_escape(&p
, NULL
, NULL
);
3582 if (esc
== ESCAPE_ERROR
) {
3583 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3584 (int)(p
- buf
->buf
), "%s", second
);
3587 ssz
= (size_t)(p
- second
);
3588 } else if (*second
== '\0') {
3589 mandoc_msg(MANDOCERR_TR_ODD
, ln
,
3590 (int)(first
- buf
->buf
), "tr %s", first
);
3596 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3601 if (r
->xtab
== NULL
)
3602 r
->xtab
= mandoc_calloc(128,
3603 sizeof(struct roffstr
));
3605 free(r
->xtab
[(int)*first
].p
);
3606 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3607 r
->xtab
[(int)*first
].sz
= ssz
;
3614 * Implementation of the .return request.
3615 * There is no need to call roff_userret() from here.
3616 * The read module will call that after rewinding the reader stack
3617 * to the place from where the current macro was called.
3620 roff_return(ROFF_ARGS
)
3622 if (r
->mstackpos
>= 0)
3623 return ROFF_IGN
| ROFF_USERRET
;
3625 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "return");
3633 char *oldn
, *newn
, *end
;
3634 size_t oldsz
, newsz
;
3637 oldn
= newn
= buf
->buf
+ pos
;
3641 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3642 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3646 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3650 deftype
= ROFFDEF_ANY
;
3651 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3654 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3655 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3656 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3659 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3660 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3663 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3664 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3665 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3668 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3669 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3672 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3673 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3680 roff_shift(ROFF_ARGS
)
3686 if (buf
->buf
[pos
] != '\0' &&
3687 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3688 mandoc_msg(MANDOCERR_CE_NONUM
,
3689 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3692 if (r
->mstackpos
< 0) {
3693 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "shift");
3696 ctx
= r
->mstack
+ r
->mstackpos
;
3697 if (levels
> ctx
->argc
) {
3698 mandoc_msg(MANDOCERR_SHIFT
,
3699 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3704 for (i
= 0; i
< levels
; i
++)
3706 ctx
->argc
-= levels
;
3707 for (i
= 0; i
< ctx
->argc
; i
++)
3708 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3717 name
= buf
->buf
+ pos
;
3718 mandoc_msg(MANDOCERR_SO
, ln
, ppos
, "so %s", name
);
3721 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3722 * opening anything that's not in our cwd or anything beneath
3723 * it. Thus, explicitly disallow traversing up the file-system
3724 * or using absolute paths.
3727 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3728 mandoc_msg(MANDOCERR_SO_PATH
, ln
, ppos
, ".so %s", name
);
3729 buf
->sz
= mandoc_asprintf(&cp
,
3730 ".sp\nSee the file %s.\n.sp", name
) + 1;
3734 return ROFF_REPARSE
;
3741 /* --- user defined strings and macros ------------------------------------ */
3744 roff_userdef(ROFF_ARGS
)
3747 char *arg
, *ap
, *dst
, *src
;
3750 /* Initialize a new macro stack context. */
3752 if (++r
->mstackpos
== r
->mstacksz
) {
3753 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3754 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3757 ctx
= r
->mstack
+ r
->mstackpos
;
3763 * Collect pointers to macro argument strings,
3764 * NUL-terminating them and escaping quotes.
3767 src
= buf
->buf
+ pos
;
3768 while (*src
!= '\0') {
3769 if (ctx
->argc
== ctx
->argsz
) {
3771 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3772 ctx
->argsz
, sizeof(*ctx
->argv
));
3774 arg
= mandoc_getarg(&src
, ln
, &pos
);
3775 sz
= 1; /* For the terminating NUL. */
3776 for (ap
= arg
; *ap
!= '\0'; ap
++)
3777 sz
+= *ap
== '"' ? 4 : 1;
3778 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3779 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3781 memcpy(dst
, "\\(dq", 4);
3789 /* Replace the macro invocation by the macro definition. */
3792 buf
->buf
= mandoc_strdup(r
->current_string
);
3793 buf
->sz
= strlen(buf
->buf
) + 1;
3796 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3797 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3801 * Calling a high-level macro that was renamed with .rn.
3802 * r->current_string has already been set up by roff_parse().
3805 roff_renamed(ROFF_ARGS
)
3809 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3810 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3818 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3827 /* Read until end of name and terminate it with NUL. */
3828 for (cp
= name
; 1; cp
++) {
3829 if ('\0' == *cp
|| ' ' == *cp
) {
3836 if ('{' == cp
[1] || '}' == cp
[1])
3841 mandoc_msg(MANDOCERR_NAMESC
, ln
, pos
,
3842 "%.*s", (int)(cp
- name
+ 1), name
);
3843 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3847 /* Read past spaces. */
3856 * Store *string into the user-defined string called *name.
3857 * To clear an existing entry, call with (*r, *name, NULL, 0).
3858 * append == 0: replace mode
3859 * append == 1: single-line append mode
3860 * append == 2: multiline append mode, append '\n' after each call
3863 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3868 namesz
= strlen(name
);
3869 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3870 string
? strlen(string
) : 0, append
);
3871 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3875 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3876 const char *string
, size_t stringsz
, int append
)
3881 size_t oldch
, newch
;
3883 /* Search for an existing string with the same name. */
3886 while (n
&& (namesz
!= n
->key
.sz
||
3887 strncmp(n
->key
.p
, name
, namesz
)))
3891 /* Create a new string table entry. */
3892 n
= mandoc_malloc(sizeof(struct roffkv
));
3893 n
->key
.p
= mandoc_strndup(name
, namesz
);
3899 } else if (0 == append
) {
3909 * One additional byte for the '\n' in multiline mode,
3910 * and one for the terminating '\0'.
3912 newch
= stringsz
+ (1 < append
? 2u : 1u);
3914 if (NULL
== n
->val
.p
) {
3915 n
->val
.p
= mandoc_malloc(newch
);
3920 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3923 /* Skip existing content in the destination buffer. */
3924 c
= n
->val
.p
+ (int)oldch
;
3926 /* Append new content to the destination buffer. */
3928 while (i
< (int)stringsz
) {
3930 * Rudimentary roff copy mode:
3931 * Handle escaped backslashes.
3933 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3938 /* Append terminating bytes. */
3943 n
->val
.sz
= (int)(c
- n
->val
.p
);
3947 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
3950 const struct roffkv
*n
;
3955 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
3956 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3957 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3959 if (*deftype
& ROFFDEF_USER
) {
3960 *deftype
= ROFFDEF_USER
;
3967 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
3968 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3969 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3971 if (*deftype
& ROFFDEF_REN
) {
3972 *deftype
= ROFFDEF_REN
;
3979 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3980 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
3981 predefs
[i
].name
[len
] != '\0')
3983 if (*deftype
& ROFFDEF_PRE
) {
3984 *deftype
= ROFFDEF_PRE
;
3985 return predefs
[i
].str
;
3991 if (r
->man
->macroset
!= MACROSET_MAN
) {
3992 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
3993 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3994 roff_name
[tok
][len
] != '\0')
3996 if (*deftype
& ROFFDEF_STD
) {
3997 *deftype
= ROFFDEF_STD
;
4005 if (r
->man
->macroset
!= MACROSET_MDOC
) {
4006 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
4007 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4008 roff_name
[tok
][len
] != '\0')
4010 if (*deftype
& ROFFDEF_STD
) {
4011 *deftype
= ROFFDEF_STD
;
4020 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
4021 if (*deftype
& ROFFDEF_REN
) {
4023 * This might still be a request,
4024 * so do not treat it as undefined yet.
4026 *deftype
= ROFFDEF_UNDEF
;
4030 /* Using an undefined string defines it to be empty. */
4032 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
4033 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
4041 roff_freestr(struct roffkv
*r
)
4043 struct roffkv
*n
, *nn
;
4045 for (n
= r
; n
; n
= nn
) {
4053 /* --- accessors and utility functions ------------------------------------ */
4056 * Duplicate an input string, making the appropriate character
4057 * conversations (as stipulated by `tr') along the way.
4058 * Returns a heap-allocated string with all the replacements made.
4061 roff_strdup(const struct roff
*r
, const char *p
)
4063 const struct roffkv
*cp
;
4067 enum mandoc_esc esc
;
4069 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
4070 return mandoc_strdup(p
);
4071 else if ('\0' == *p
)
4072 return mandoc_strdup("");
4075 * Step through each character looking for term matches
4076 * (remember that a `tr' can be invoked with an escape, which is
4077 * a glyph but the escape is multi-character).
4078 * We only do this if the character hash has been initialised
4079 * and the string is >0 length.
4085 while ('\0' != *p
) {
4086 assert((unsigned int)*p
< 128);
4087 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
4088 sz
= r
->xtab
[(int)*p
].sz
;
4089 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4090 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
4094 } else if ('\\' != *p
) {
4095 res
= mandoc_realloc(res
, ssz
+ 2);
4100 /* Search for term matches. */
4101 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4102 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4107 * A match has been found.
4108 * Append the match to the array and move
4109 * forward by its keysize.
4111 res
= mandoc_realloc(res
,
4112 ssz
+ cp
->val
.sz
+ 1);
4113 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4115 p
+= (int)cp
->key
.sz
;
4120 * Handle escapes carefully: we need to copy
4121 * over just the escape itself, or else we might
4122 * do replacements within the escape itself.
4123 * Make sure to pass along the bogus string.
4126 esc
= mandoc_escape(&p
, NULL
, NULL
);
4127 if (ESCAPE_ERROR
== esc
) {
4129 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4130 memcpy(res
+ ssz
, pp
, sz
);
4134 * We bail out on bad escapes.
4135 * No need to warn: we already did so when
4136 * roff_res() was called.
4139 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4140 memcpy(res
+ ssz
, pp
, sz
);
4144 res
[(int)ssz
] = '\0';
4149 roff_getformat(const struct roff
*r
)
4156 * Find out whether a line is a macro line or not.
4157 * If it is, adjust the current position and return one; if it isn't,
4158 * return zero and don't change the current position.
4159 * If the control character has been set with `.cc', then let that grain
4161 * This is slighly contrary to groff, where using the non-breaking
4162 * control character when `cc' has been invoked will cause the
4163 * non-breaking macro contents to be printed verbatim.
4166 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4172 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4174 else if (r
->control
!= '\0')
4176 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4178 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4183 while (' ' == cp
[pos
] || '\t' == cp
[pos
])