]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.343 2018/11/26 17:44:34 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>
32 #include "mandoc_aux.h"
33 #include "mandoc_ohash.h"
35 #include "libmandoc.h"
39 /* Maximum number of string expansions per line, to break infinite loops. */
40 #define EXPAND_LIMIT 1000
42 /* Types of definitions of macros and strings. */
43 #define ROFFDEF_USER (1 << 1) /* User-defined. */
44 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
45 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
46 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
47 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
48 ROFFDEF_REN | ROFFDEF_STD)
49 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
51 /* --- data types --------------------------------------------------------- */
54 * An incredibly-simple string buffer.
57 char *p
; /* nil-terminated buffer */
58 size_t sz
; /* saved strlen(p) */
62 * A key-value roffstr pair as part of a singly-linked list.
67 struct roffkv
*next
; /* next in list */
71 * A single number register as part of a singly-linked list.
81 * Association of request and macro names with token IDs.
89 * A macro processing context.
90 * More than one is needed when macro calls are nested.
99 struct mparse
*parse
; /* parse point */
100 struct roff_man
*man
; /* mdoc or man parser */
101 struct roffnode
*last
; /* leaf of stack */
102 struct mctx
*mstack
; /* stack of macro contexts */
103 int *rstack
; /* stack of inverted `ie' values */
104 struct ohash
*reqtab
; /* request lookup table */
105 struct roffreg
*regtab
; /* number registers */
106 struct roffkv
*strtab
; /* user-defined strings & macros */
107 struct roffkv
*rentab
; /* renamed strings & macros */
108 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
109 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
110 const char *current_string
; /* value of last called user macro */
111 struct tbl_node
*first_tbl
; /* first table parsed */
112 struct tbl_node
*last_tbl
; /* last table parsed */
113 struct tbl_node
*tbl
; /* current table being parsed */
114 struct eqn_node
*last_eqn
; /* equation parser */
115 struct eqn_node
*eqn
; /* active equation parser */
116 int eqn_inline
; /* current equation is inline */
117 int options
; /* parse options */
118 int mstacksz
; /* current size of mstack */
119 int mstackpos
; /* position in mstack */
120 int rstacksz
; /* current size limit of rstack */
121 int rstackpos
; /* position in rstack */
122 int format
; /* current file in mdoc or man format */
123 char control
; /* control character */
124 char escape
; /* escape character */
128 enum roff_tok tok
; /* type of node */
129 struct roffnode
*parent
; /* up one in stack */
130 int line
; /* parse line */
131 int col
; /* parse col */
132 char *name
; /* node name, e.g. macro name */
133 char *end
; /* end-rules: custom token */
134 int endspan
; /* end-rules: next-line or infty */
135 int rule
; /* current evaluation rule */
138 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
139 enum roff_tok tok, /* tok of macro */ \
140 struct buf *buf, /* input buffer */ \
141 int ln, /* parse line */ \
142 int ppos, /* original pos in buffer */ \
143 int pos, /* current pos in buffer */ \
144 int *offs /* reset offset of buffer data */
146 typedef int (*roffproc
)(ROFF_ARGS
);
149 roffproc proc
; /* process new macro */
150 roffproc text
; /* process as child text of macro */
151 roffproc sub
; /* process as child of macro */
153 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
157 const char *name
; /* predefined input name */
158 const char *str
; /* replacement symbol */
161 #define PREDEF(__name, __str) \
162 { (__name), (__str) },
164 /* --- function prototypes ------------------------------------------------ */
166 static int roffnode_cleanscope(struct roff
*);
167 static int roffnode_pop(struct roff
*);
168 static void roffnode_push(struct roff
*, enum roff_tok
,
169 const char *, int, int);
170 static void roff_addtbl(struct roff_man
*, struct tbl_node
*);
171 static int roff_als(ROFF_ARGS
);
172 static int roff_block(ROFF_ARGS
);
173 static int roff_block_text(ROFF_ARGS
);
174 static int roff_block_sub(ROFF_ARGS
);
175 static int roff_br(ROFF_ARGS
);
176 static int roff_cblock(ROFF_ARGS
);
177 static int roff_cc(ROFF_ARGS
);
178 static int roff_ccond(struct roff
*, int, int);
179 static int roff_char(ROFF_ARGS
);
180 static int roff_cond(ROFF_ARGS
);
181 static int roff_cond_text(ROFF_ARGS
);
182 static int roff_cond_sub(ROFF_ARGS
);
183 static int roff_ds(ROFF_ARGS
);
184 static int roff_ec(ROFF_ARGS
);
185 static int roff_eo(ROFF_ARGS
);
186 static int roff_eqndelim(struct roff
*, struct buf
*, int);
187 static int roff_evalcond(struct roff
*r
, int, char *, int *);
188 static int roff_evalnum(struct roff
*, int,
189 const char *, int *, int *, int);
190 static int roff_evalpar(struct roff
*, int,
191 const char *, int *, int *, int);
192 static int roff_evalstrcond(const char *, int *);
193 static void roff_free1(struct roff
*);
194 static void roff_freereg(struct roffreg
*);
195 static void roff_freestr(struct roffkv
*);
196 static size_t roff_getname(struct roff
*, char **, int, int);
197 static int roff_getnum(const char *, int *, int *, int);
198 static int roff_getop(const char *, int *, char *);
199 static int roff_getregn(struct roff
*,
200 const char *, size_t, char);
201 static int roff_getregro(const struct roff
*,
203 static const char *roff_getstrn(struct roff
*,
204 const char *, size_t, int *);
205 static int roff_hasregn(const struct roff
*,
206 const char *, size_t);
207 static int roff_insec(ROFF_ARGS
);
208 static int roff_it(ROFF_ARGS
);
209 static int roff_line_ignore(ROFF_ARGS
);
210 static void roff_man_alloc1(struct roff_man
*);
211 static void roff_man_free1(struct roff_man
*);
212 static int roff_manyarg(ROFF_ARGS
);
213 static int roff_nop(ROFF_ARGS
);
214 static int roff_nr(ROFF_ARGS
);
215 static int roff_onearg(ROFF_ARGS
);
216 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
218 static int roff_parsetext(struct roff
*, struct buf
*,
220 static int roff_renamed(ROFF_ARGS
);
221 static int roff_res(struct roff
*, struct buf
*, int, int);
222 static int roff_return(ROFF_ARGS
);
223 static int roff_rm(ROFF_ARGS
);
224 static int roff_rn(ROFF_ARGS
);
225 static int roff_rr(ROFF_ARGS
);
226 static void roff_setregn(struct roff
*, const char *,
227 size_t, int, char, int);
228 static void roff_setstr(struct roff
*,
229 const char *, const char *, int);
230 static void roff_setstrn(struct roffkv
**, const char *,
231 size_t, const char *, size_t, int);
232 static int roff_shift(ROFF_ARGS
);
233 static int roff_so(ROFF_ARGS
);
234 static int roff_tr(ROFF_ARGS
);
235 static int roff_Dd(ROFF_ARGS
);
236 static int roff_TE(ROFF_ARGS
);
237 static int roff_TS(ROFF_ARGS
);
238 static int roff_EQ(ROFF_ARGS
);
239 static int roff_EN(ROFF_ARGS
);
240 static int roff_T_(ROFF_ARGS
);
241 static int roff_unsupp(ROFF_ARGS
);
242 static int roff_userdef(ROFF_ARGS
);
244 /* --- constant data ------------------------------------------------------ */
246 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
247 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
249 const char *__roff_name
[MAN_MAX
+ 1] = {
250 "br", "ce", "ft", "ll",
251 "mc", "po", "rj", "sp",
253 "ab", "ad", "af", "aln",
254 "als", "am", "am1", "ami",
255 "ami1", "as", "as1", "asciify",
256 "backtrace", "bd", "bleedat", "blm",
257 "box", "boxa", "bp", "BP",
258 "break", "breakchar", "brnl", "brp",
260 "cf", "cflags", "ch", "char",
261 "chop", "class", "close", "CL",
262 "color", "composite", "continue", "cp",
263 "cropat", "cs", "cu", "da",
264 "dch", "Dd", "de", "de1",
265 "defcolor", "dei", "dei1", "device",
266 "devicem", "di", "do", "ds",
267 "ds1", "dwh", "dt", "ec",
268 "ecr", "ecs", "el", "em",
269 "EN", "eo", "EP", "EQ",
270 "errprint", "ev", "evc", "ex",
271 "fallback", "fam", "fc", "fchar",
272 "fcolor", "fdeferlig", "feature", "fkern",
273 "fl", "flig", "fp", "fps",
274 "fschar", "fspacewidth", "fspecial", "ftr",
275 "fzoom", "gcolor", "hc", "hcode",
276 "hidechar", "hla", "hlm", "hpf",
277 "hpfa", "hpfcode", "hw", "hy",
278 "hylang", "hylen", "hym", "hypp",
279 "hys", "ie", "if", "ig",
280 "index", "it", "itc", "IX",
281 "kern", "kernafter", "kernbefore", "kernpair",
282 "lc", "lc_ctype", "lds", "length",
283 "letadj", "lf", "lg", "lhang",
284 "linetabs", "lnr", "lnrf", "lpfx",
286 "mediasize", "minss", "mk", "mso",
287 "na", "ne", "nh", "nhychar",
288 "nm", "nn", "nop", "nr",
289 "nrf", "nroff", "ns", "nx",
290 "open", "opena", "os", "output",
291 "padj", "papersize", "pc", "pev",
292 "pi", "PI", "pl", "pm",
294 "psbb", "pshape", "pso", "ptr",
295 "pvs", "rchar", "rd", "recursionlimit",
296 "return", "rfschar", "rhang",
297 "rm", "rn", "rnn", "rr",
298 "rs", "rt", "schar", "sentchar",
299 "shc", "shift", "sizes", "so",
300 "spacewidth", "special", "spreadwarn", "ss",
301 "sty", "substring", "sv", "sy",
304 "tm", "tm1", "tmc", "tr",
305 "track", "transchar", "trf", "trimat",
306 "trin", "trnt", "troff", "TS",
307 "uf", "ul", "unformat", "unwatch",
308 "unwatchn", "vpt", "vs", "warn",
309 "warnscale", "watch", "watchlength", "watchn",
310 "wh", "while", "write", "writec",
311 "writem", "xflag", ".", NULL
,
313 "Dd", "Dt", "Os", "Sh",
314 "Ss", "Pp", "D1", "Dl",
315 "Bd", "Ed", "Bl", "El",
316 "It", "Ad", "An", "Ap",
317 "Ar", "Cd", "Cm", "Dv",
318 "Er", "Ev", "Ex", "Fa",
319 "Fd", "Fl", "Fn", "Ft",
320 "Ic", "In", "Li", "Nd",
321 "Nm", "Op", "Ot", "Pa",
322 "Rv", "St", "Va", "Vt",
323 "Xr", "%A", "%B", "%D",
324 "%I", "%J", "%N", "%O",
325 "%P", "%R", "%T", "%V",
326 "Ac", "Ao", "Aq", "At",
327 "Bc", "Bf", "Bo", "Bq",
328 "Bsx", "Bx", "Db", "Dc",
329 "Do", "Dq", "Ec", "Ef",
330 "Em", "Eo", "Fx", "Ms",
331 "No", "Ns", "Nx", "Ox",
332 "Pc", "Pf", "Po", "Pq",
333 "Qc", "Ql", "Qo", "Qq",
334 "Re", "Rs", "Sc", "So",
335 "Sq", "Sm", "Sx", "Sy",
336 "Tn", "Ux", "Xc", "Xo",
337 "Fo", "Fc", "Oo", "Oc",
338 "Bk", "Ek", "Bt", "Hf",
339 "Fr", "Ud", "Lb", "Lp",
340 "Lk", "Mt", "Brq", "Bro",
341 "Brc", "%C", "Es", "En",
342 "Dx", "%Q", "%U", "Ta",
344 "TH", "SH", "SS", "TP",
346 "LP", "PP", "P", "IP",
347 "HP", "SM", "SB", "BI",
348 "IB", "BR", "RB", "R",
349 "B", "I", "IR", "RI",
351 "RE", "RS", "DT", "UC",
355 "UE", "MT", "ME", NULL
357 const char *const *roff_name
= __roff_name
;
359 static struct roffmac roffs
[TOKEN_NONE
] = {
360 { roff_br
, NULL
, NULL
, 0 }, /* br */
361 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
362 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
363 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
364 { roff_onearg
, NULL
, NULL
, 0 }, /* mc */
365 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
366 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
367 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
368 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
369 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
370 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
371 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
372 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
373 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
374 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
375 { roff_als
, NULL
, NULL
, 0 }, /* als */
376 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
377 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
378 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
379 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
380 { roff_ds
, NULL
, NULL
, 0 }, /* as */
381 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
382 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
383 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
384 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
385 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
386 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
387 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
388 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
389 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
390 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
391 { roff_unsupp
, NULL
, NULL
, 0 }, /* break */
392 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
393 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
394 { roff_br
, NULL
, NULL
, 0 }, /* brp */
395 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
396 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
397 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
398 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
399 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
400 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
401 { roff_char
, NULL
, NULL
, 0 }, /* char */
402 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
403 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
404 { roff_insec
, NULL
, NULL
, 0 }, /* close */
405 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
406 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
407 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
408 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
409 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
411 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
412 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
413 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
414 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
415 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
416 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
417 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
419 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
420 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
421 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
422 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
423 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
424 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
425 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
426 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
427 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
428 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
429 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
430 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
431 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
432 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
433 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
434 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
435 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
436 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
437 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
438 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
440 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
441 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
442 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
444 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
445 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
446 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
447 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
448 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
449 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
450 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
451 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
452 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
453 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
454 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
455 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
456 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
457 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
458 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
459 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
460 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
461 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
462 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
464 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
465 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
466 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
469 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
470 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
475 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
476 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
477 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
478 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
479 { roff_it
, NULL
, NULL
, 0 }, /* it */
480 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
486 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
487 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
488 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
489 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
491 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
494 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
495 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
496 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
497 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
498 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
499 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
500 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
501 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
502 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
503 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
504 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
505 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
509 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
510 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
511 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
512 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
513 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
514 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
515 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
516 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
517 { roff_insec
, NULL
, NULL
, 0 }, /* open */
518 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
519 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
520 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
521 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
522 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
524 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
525 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
526 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
529 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
532 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
533 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
534 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
535 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
536 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
537 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
538 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
540 { roff_return
, NULL
, NULL
, 0 }, /* return */
541 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
542 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
543 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
544 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
545 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
546 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
549 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
550 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
551 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
552 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
554 { roff_so
, NULL
, NULL
, 0 }, /* so */
555 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
557 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
558 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
559 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
560 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
561 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
562 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
563 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
564 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
565 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
566 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
568 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
569 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
570 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
571 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
572 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
574 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
575 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
576 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
577 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
578 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
579 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
580 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
581 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
582 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
583 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
584 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
585 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
587 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
588 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
589 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
590 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
591 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
592 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
593 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
594 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
595 { roff_insec
, NULL
, NULL
, 0 }, /* write */
596 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
597 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
598 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
599 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
600 { roff_renamed
, NULL
, NULL
, 0 },
601 { roff_userdef
, NULL
, NULL
, 0 }
604 /* Array of injected predefined strings. */
605 #define PREDEFS_MAX 38
606 static const struct predef predefs
[PREDEFS_MAX
] = {
607 #include "predefs.in"
610 static int roffce_lines
; /* number of input lines to center */
611 static struct roff_node
*roffce_node
; /* active request */
612 static int roffit_lines
; /* number of lines to delay */
613 static char *roffit_macro
; /* nil-terminated macro line */
616 /* --- request table ------------------------------------------------------ */
619 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
627 htab
= mandoc_malloc(sizeof(*htab
));
628 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
630 for (tok
= mintok
; tok
< maxtok
; tok
++) {
631 if (roff_name
[tok
] == NULL
)
633 sz
= strlen(roff_name
[tok
]);
634 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
636 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
637 slot
= ohash_qlookup(htab
, req
->name
);
638 ohash_insert(htab
, slot
, req
);
644 roffhash_free(struct ohash
*htab
)
651 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
652 req
= ohash_next(htab
, &slot
))
659 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
666 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
668 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
669 return req
== NULL
? TOKEN_NONE
: req
->tok
;
672 /* --- stack of request blocks -------------------------------------------- */
675 * Pop the current node off of the stack of roff instructions currently
679 roffnode_pop(struct roff
*r
)
685 inloop
= p
->tok
== ROFF_while
;
694 * Push a roff node onto the instruction stack. This must later be
695 * removed with roffnode_pop().
698 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
703 p
= mandoc_calloc(1, sizeof(struct roffnode
));
706 p
->name
= mandoc_strdup(name
);
710 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
715 /* --- roff parser state data management ---------------------------------- */
718 roff_free1(struct roff
*r
)
720 struct tbl_node
*tbl
;
723 while (NULL
!= (tbl
= r
->first_tbl
)) {
724 r
->first_tbl
= tbl
->next
;
727 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
729 if (r
->last_eqn
!= NULL
)
730 eqn_free(r
->last_eqn
);
731 r
->last_eqn
= r
->eqn
= NULL
;
733 while (r
->mstackpos
>= 0)
744 roff_freereg(r
->regtab
);
747 roff_freestr(r
->strtab
);
748 roff_freestr(r
->rentab
);
749 roff_freestr(r
->xmbtab
);
750 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
753 for (i
= 0; i
< 128; i
++)
760 roff_reset(struct roff
*r
)
763 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
773 roff_free(struct roff
*r
)
778 for (i
= 0; i
< r
->mstacksz
; i
++)
779 free(r
->mstack
[i
].argv
);
781 roffhash_free(r
->reqtab
);
786 roff_alloc(struct mparse
*parse
, int options
)
790 r
= mandoc_calloc(1, sizeof(struct roff
));
792 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
793 r
->options
= options
;
794 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
801 /* --- syntax tree state data management ---------------------------------- */
804 roff_man_free1(struct roff_man
*man
)
807 if (man
->first
!= NULL
)
808 roff_node_delete(man
, man
->first
);
809 free(man
->meta
.msec
);
812 free(man
->meta
.arch
);
813 free(man
->meta
.title
);
814 free(man
->meta
.name
);
815 free(man
->meta
.date
);
819 roff_man_alloc1(struct roff_man
*man
)
822 memset(&man
->meta
, 0, sizeof(man
->meta
));
823 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
824 man
->first
->type
= ROFFT_ROOT
;
825 man
->last
= man
->first
;
828 man
->macroset
= MACROSET_NONE
;
829 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
830 man
->next
= ROFF_NEXT_CHILD
;
834 roff_man_reset(struct roff_man
*man
)
838 roff_man_alloc1(man
);
842 roff_man_free(struct roff_man
*man
)
850 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
851 const char *os_s
, int quick
)
853 struct roff_man
*man
;
855 man
= mandoc_calloc(1, sizeof(*man
));
860 roff_man_alloc1(man
);
865 /* --- syntax tree handling ----------------------------------------------- */
868 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
869 enum roff_type type
, int tok
)
873 n
= mandoc_calloc(1, sizeof(*n
));
878 n
->sec
= man
->lastsec
;
880 if (man
->flags
& MDOC_SYNOPSIS
)
881 n
->flags
|= NODE_SYNPRETTY
;
883 n
->flags
&= ~NODE_SYNPRETTY
;
884 if (man
->flags
& MDOC_NEWLINE
)
885 n
->flags
|= NODE_LINE
;
886 man
->flags
&= ~MDOC_NEWLINE
;
892 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
896 case ROFF_NEXT_SIBLING
:
897 if (man
->last
->next
!= NULL
) {
898 n
->next
= man
->last
->next
;
899 man
->last
->next
->prev
= n
;
901 man
->last
->parent
->last
= n
;
904 n
->parent
= man
->last
->parent
;
906 case ROFF_NEXT_CHILD
:
907 if (man
->last
->child
!= NULL
) {
908 n
->next
= man
->last
->child
;
909 man
->last
->child
->prev
= n
;
912 man
->last
->child
= n
;
913 n
->parent
= man
->last
;
925 if (n
->end
!= ENDBODY_NOT
)
937 * Copy over the normalised-data pointer of our parent. Not
938 * everybody has one, but copying a null pointer is fine.
941 n
->norm
= n
->parent
->norm
;
942 assert(n
->parent
->type
== ROFFT_BLOCK
);
946 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
950 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
951 n
->string
= roff_strdup(man
->roff
, word
);
952 roff_node_append(man
, n
);
953 n
->flags
|= NODE_VALID
| NODE_ENDED
;
954 man
->next
= ROFF_NEXT_SIBLING
;
958 roff_word_append(struct roff_man
*man
, const char *word
)
961 char *addstr
, *newstr
;
964 addstr
= roff_strdup(man
->roff
, word
);
965 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
969 man
->next
= ROFF_NEXT_SIBLING
;
973 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
977 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
978 roff_node_append(man
, n
);
979 man
->next
= ROFF_NEXT_CHILD
;
983 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
987 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
988 roff_node_append(man
, n
);
989 man
->next
= ROFF_NEXT_CHILD
;
994 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
998 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
999 roff_node_append(man
, n
);
1000 man
->next
= ROFF_NEXT_CHILD
;
1005 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1007 struct roff_node
*n
;
1009 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1010 roff_node_append(man
, n
);
1011 man
->next
= ROFF_NEXT_CHILD
;
1016 roff_addtbl(struct roff_man
*man
, struct tbl_node
*tbl
)
1018 struct roff_node
*n
;
1019 const struct tbl_span
*span
;
1021 if (man
->macroset
== MACROSET_MAN
)
1022 man_breakscope(man
, ROFF_TS
);
1023 while ((span
= tbl_span(tbl
)) != NULL
) {
1024 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1026 roff_node_append(man
, n
);
1027 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1028 man
->next
= ROFF_NEXT_SIBLING
;
1033 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1036 /* Adjust siblings. */
1039 n
->prev
->next
= n
->next
;
1041 n
->next
->prev
= n
->prev
;
1043 /* Adjust parent. */
1045 if (n
->parent
!= NULL
) {
1046 if (n
->parent
->child
== n
)
1047 n
->parent
->child
= n
->next
;
1048 if (n
->parent
->last
== n
)
1049 n
->parent
->last
= n
->prev
;
1052 /* Adjust parse point. */
1056 if (man
->last
== n
) {
1057 if (n
->prev
== NULL
) {
1058 man
->last
= n
->parent
;
1059 man
->next
= ROFF_NEXT_CHILD
;
1061 man
->last
= n
->prev
;
1062 man
->next
= ROFF_NEXT_SIBLING
;
1065 if (man
->first
== n
)
1070 roff_node_free(struct roff_node
*n
)
1073 if (n
->args
!= NULL
)
1074 mdoc_argv_free(n
->args
);
1075 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 char *ep
; /* end of comment string */
1158 const char *stnam
; /* start of the name, after "[(*" */
1159 const char *cp
; /* end of the name, e.g. before ']' */
1160 const char *res
; /* the string to be substituted */
1161 char *nbuf
; /* new buffer to copy buf->buf to */
1162 size_t maxl
; /* expected length of the escape name */
1163 size_t naml
; /* actual length of the escape name */
1164 size_t asz
; /* length of the replacement */
1165 size_t rsz
; /* length of the rest of the string */
1166 enum mandoc_esc esc
; /* type of the escape sequence */
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 char sign
; /* increment number register */
1176 char term
; /* character terminating the escape */
1178 /* Search forward for comments. */
1181 start
= buf
->buf
+ pos
;
1182 for (stesc
= buf
->buf
+ pos
; *stesc
!= '\0'; stesc
++) {
1183 if (stesc
[0] != r
->escape
|| stesc
[1] == '\0')
1186 if (*stesc
!= '"' && *stesc
!= '#')
1189 /* Comment found, look for RCS id. */
1192 if ((cp
= strstr(stesc
, "$" "OpenBSD")) != NULL
) {
1193 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1195 } else if ((cp
= strstr(stesc
, "$" "NetBSD")) != NULL
) {
1196 rcsid
= 1 << MANDOC_OS_NETBSD
;
1200 isalnum((unsigned char)*cp
) == 0 &&
1201 strchr(cp
, '$') != NULL
) {
1202 if (r
->man
->meta
.rcsids
& rcsid
)
1203 mandoc_msg(MANDOCERR_RCS_REP
, r
->parse
,
1204 ln
, stesc
+ 1 - buf
->buf
, stesc
+ 1);
1205 r
->man
->meta
.rcsids
|= rcsid
;
1208 /* Handle trailing whitespace. */
1210 ep
= strchr(stesc
--, '\0') - 1;
1215 if (*ep
== ' ' || *ep
== '\t')
1216 mandoc_msg(MANDOCERR_SPACE_EOL
, r
->parse
,
1217 ln
, ep
- buf
->buf
, NULL
);
1220 * Save comments preceding the title macro
1221 * in the syntax tree.
1224 if (r
->format
== 0) {
1225 while (*ep
== ' ' || *ep
== '\t')
1228 n
= roff_node_alloc(r
->man
,
1229 ln
, stesc
+ 1 - buf
->buf
,
1230 ROFFT_COMMENT
, TOKEN_NONE
);
1231 n
->string
= mandoc_strdup(stesc
+ 2);
1232 roff_node_append(r
->man
, n
);
1233 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1234 r
->man
->next
= ROFF_NEXT_SIBLING
;
1237 /* Line continuation with comment. */
1239 if (stesc
[1] == '#') {
1241 return ROFF_IGN
| ROFF_APPEND
;
1244 /* Discard normal comments. */
1246 while (stesc
> start
&& stesc
[-1] == ' ' &&
1247 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1256 /* Notice the end of the input. */
1258 if (*stesc
== '\n') {
1264 while (stesc
>= start
) {
1266 /* Search backwards for the next backslash. */
1268 if (*stesc
!= r
->escape
) {
1269 if (*stesc
== '\\') {
1271 buf
->sz
= mandoc_asprintf(&nbuf
, "%s\\e%s",
1272 buf
->buf
, stesc
+ 1) + 1;
1274 stesc
= nbuf
+ (stesc
- buf
->buf
);
1282 /* If it is escaped, skip it. */
1284 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1285 if (*cp
!= r
->escape
)
1288 if ((stesc
- cp
) % 2 == 0) {
1292 } else if (stesc
[1] != '\0') {
1299 return ROFF_IGN
| ROFF_APPEND
;
1302 /* Decide whether to expand or to check only. */
1317 if (sign
== '+' || sign
== '-')
1322 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1323 if (esc
== ESCAPE_ERROR
||
1324 (esc
== ESCAPE_SPECIAL
&&
1325 mchars_spec2cp(stnam
, inaml
) < 0))
1326 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1327 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1328 "%.*s", (int)(cp
- stesc
), stesc
);
1333 if (EXPAND_LIMIT
< ++expand_count
) {
1334 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1335 ln
, (int)(stesc
- buf
->buf
), NULL
);
1340 * The third character decides the length
1341 * of the name of the string or register.
1342 * Save a pointer to the name.
1369 /* Advance to the end of the name. */
1373 while (maxl
== 0 || naml
< maxl
) {
1375 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1376 ln
, (int)(stesc
- buf
->buf
), stesc
);
1380 if (maxl
== 0 && *cp
== term
) {
1384 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1388 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1389 case ESCAPE_SPECIAL
:
1390 case ESCAPE_UNICODE
:
1391 case ESCAPE_NUMBERED
:
1392 case ESCAPE_OVERSTRIKE
:
1401 * Retrieve the replacement string; if it is
1402 * undefined, resume searching for escapes.
1408 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1409 res
= roff_getstrn(r
, stnam
, naml
, &deftype
);
1412 * If not overriden, let \*(.T
1413 * through to the formatters.
1416 if (res
== NULL
&& naml
== 2 &&
1417 stnam
[0] == '.' && stnam
[1] == 'T') {
1418 roff_setstrn(&r
->strtab
,
1419 ".T", 2, NULL
, 0, 0);
1426 if (r
->mstackpos
< 0) {
1427 mandoc_vmsg(MANDOCERR_ARG_UNDEF
,
1428 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1432 ctx
= r
->mstack
+ r
->mstackpos
;
1433 npos
= stesc
[2] - '1';
1434 if (npos
>= 0 && npos
<= 8) {
1435 res
= npos
< ctx
->argc
?
1436 ctx
->argv
[npos
] : "";
1439 if (stesc
[2] == '*')
1441 else if (stesc
[2] == '@')
1444 mandoc_vmsg(MANDOCERR_ARG_NONUM
,
1445 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1450 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1454 asz
+= 2; /* quotes */
1455 asz
+= strlen(ctx
->argv
[npos
]);
1458 rsz
= buf
->sz
- (stesc
- buf
->buf
) - 3;
1460 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1462 nbuf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1464 stesc
= nbuf
+ (stesc
- buf
->buf
);
1467 memmove(stesc
+ asz
, stesc
+ 3, rsz
);
1469 for (npos
= 0; npos
< ctx
->argc
; npos
++) {
1474 cp
= ctx
->argv
[npos
];
1483 ubuf
[0] = arg_complete
&&
1484 roff_evalnum(r
, ln
, stnam
, &npos
,
1485 NULL
, ROFFNUM_SCALE
) &&
1486 stnam
+ npos
+ 1 == cp
? '1' : '0';
1491 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1492 roff_getregn(r
, stnam
, naml
, sign
));
1497 /* use even incomplete args */
1498 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1504 if (stesc
[1] == '*')
1505 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1506 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1507 "%.*s", (int)naml
, stnam
);
1509 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1510 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1511 ln
, (int)(stesc
- buf
->buf
), NULL
);
1515 /* Replace the escape sequence by the string. */
1518 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1519 buf
->buf
, res
, cp
) + 1;
1521 /* Prepare for the next replacement. */
1524 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1532 * Process text streams.
1535 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1541 enum mandoc_esc esc
;
1543 /* Spring the input line trap. */
1545 if (roffit_lines
== 1) {
1546 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1553 return ROFF_REPARSE
;
1554 } else if (roffit_lines
> 1)
1557 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1558 if (roffce_lines
< 1) {
1559 r
->man
->last
= roffce_node
;
1560 r
->man
->next
= ROFF_NEXT_SIBLING
;
1567 /* Convert all breakable hyphens into ASCII_HYPH. */
1569 start
= p
= buf
->buf
+ pos
;
1571 while (*p
!= '\0') {
1572 sz
= strcspn(p
, "-\\");
1579 /* Skip over escapes. */
1581 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1582 if (esc
== ESCAPE_ERROR
)
1587 } else if (p
== start
) {
1592 if (isalpha((unsigned char)p
[-1]) &&
1593 isalpha((unsigned char)p
[1]))
1601 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1605 int pos
; /* parse point */
1606 int spos
; /* saved parse point for messages */
1607 int ppos
; /* original offset in buf->buf */
1608 int ctl
; /* macro line (boolean) */
1612 /* Handle in-line equation delimiters. */
1614 if (r
->tbl
== NULL
&&
1615 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1616 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1617 e
= roff_eqndelim(r
, buf
, pos
);
1618 if (e
== ROFF_REPARSE
)
1620 assert(e
== ROFF_CONT
);
1623 /* Expand some escape sequences. */
1625 e
= roff_res(r
, buf
, ln
, pos
);
1626 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1628 assert(e
== ROFF_CONT
);
1630 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1633 * First, if a scope is open and we're not a macro, pass the
1634 * text through the macro's filter.
1635 * Equations process all content themselves.
1636 * Tables process almost all content themselves, but we want
1637 * to warn about macros before passing it there.
1640 if (r
->last
!= NULL
&& ! ctl
) {
1642 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1643 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1648 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1649 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1652 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1653 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1654 roff_addtbl(r
->man
, r
->tbl
);
1658 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1660 /* Skip empty request lines. */
1662 if (buf
->buf
[pos
] == '"') {
1663 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1666 } else if (buf
->buf
[pos
] == '\0')
1670 * If a scope is open, go to the child handler for that macro,
1671 * as it may want to preprocess before doing anything with it.
1672 * Don't do so if an equation is open.
1677 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1680 /* No scope is open. This is a new request or macro. */
1683 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1685 /* Tables ignore most macros. */
1687 if (r
->tbl
!= NULL
&& (t
== TOKEN_NONE
|| t
== ROFF_TS
||
1688 t
== ROFF_br
|| t
== ROFF_ce
|| t
== ROFF_rj
|| t
== ROFF_sp
)) {
1689 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1690 ln
, pos
, buf
->buf
+ spos
);
1691 if (t
!= TOKEN_NONE
)
1693 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1695 while (buf
->buf
[pos
] == ' ')
1697 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1698 roff_addtbl(r
->man
, r
->tbl
);
1702 /* For now, let high level macros abort .ce mode. */
1704 if (ctl
&& roffce_node
!= NULL
&&
1705 (t
== TOKEN_NONE
|| t
== ROFF_Dd
|| t
== ROFF_EQ
||
1706 t
== ROFF_TH
|| t
== ROFF_TS
)) {
1707 r
->man
->last
= roffce_node
;
1708 r
->man
->next
= ROFF_NEXT_SIBLING
;
1714 * This is neither a roff request nor a user-defined macro.
1715 * Let the standard macro set parsers handle it.
1718 if (t
== TOKEN_NONE
)
1721 /* Execute a roff request or a user defined macro. */
1723 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, spos
, pos
, offs
);
1727 * Internal interface function to tell the roff parser that execution
1728 * of the current macro ended. This is required because macro
1729 * definitions usually do not end with a .return request.
1732 roff_userret(struct roff
*r
)
1737 assert(r
->mstackpos
>= 0);
1738 ctx
= r
->mstack
+ r
->mstackpos
;
1739 for (i
= 0; i
< ctx
->argc
; i
++)
1746 roff_endparse(struct roff
*r
)
1748 if (r
->last
!= NULL
)
1749 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1750 r
->last
->line
, r
->last
->col
,
1751 roff_name
[r
->last
->tok
]);
1753 if (r
->eqn
!= NULL
) {
1754 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1755 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1760 if (r
->tbl
!= NULL
) {
1761 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1762 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1769 * Parse a roff node's type from the input buffer. This must be in the
1770 * form of ".foo xxx" in the usual way.
1772 static enum roff_tok
1773 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1783 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1787 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1789 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1790 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1799 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1802 if (t
!= TOKEN_NONE
)
1804 else if (deftype
== ROFFDEF_UNDEF
) {
1805 /* Using an undefined macro defines it to be empty. */
1806 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1807 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1812 /* --- handling of request blocks ----------------------------------------- */
1815 roff_cblock(ROFF_ARGS
)
1819 * A block-close `..' should only be invoked as a child of an
1820 * ignore macro, otherwise raise a warning and just ignore it.
1823 if (r
->last
== NULL
) {
1824 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1829 switch (r
->last
->tok
) {
1831 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1834 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1839 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1844 if (buf
->buf
[pos
] != '\0')
1845 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1846 ".. %s", buf
->buf
+ pos
);
1849 roffnode_cleanscope(r
);
1855 roffnode_cleanscope(struct roff
*r
)
1860 while (r
->last
!= NULL
) {
1861 if (--r
->last
->endspan
!= 0)
1863 inloop
+= roffnode_pop(r
);
1869 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1871 if (NULL
== r
->last
) {
1872 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1877 switch (r
->last
->tok
) {
1884 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1889 if (r
->last
->endspan
> -1) {
1890 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1895 return roffnode_pop(r
) + roffnode_cleanscope(r
);
1899 roff_block(ROFF_ARGS
)
1901 const char *name
, *value
;
1902 char *call
, *cp
, *iname
, *rname
;
1903 size_t csz
, namesz
, rsz
;
1906 /* Ignore groff compatibility mode for now. */
1908 if (tok
== ROFF_de1
)
1910 else if (tok
== ROFF_dei1
)
1912 else if (tok
== ROFF_am1
)
1914 else if (tok
== ROFF_ami1
)
1917 /* Parse the macro name argument. */
1919 cp
= buf
->buf
+ pos
;
1920 if (tok
== ROFF_ig
) {
1925 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1926 iname
[namesz
] = '\0';
1929 /* Resolve the macro name argument if it is indirect. */
1931 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1932 deftype
= ROFFDEF_USER
;
1933 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1935 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1936 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1937 "%.*s", (int)namesz
, iname
);
1940 namesz
= strlen(name
);
1944 if (namesz
== 0 && tok
!= ROFF_ig
) {
1945 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1946 ln
, ppos
, roff_name
[tok
]);
1950 roffnode_push(r
, tok
, name
, ln
, ppos
);
1953 * At the beginning of a `de' macro, clear the existing string
1954 * with the same name, if there is one. New content will be
1955 * appended from roff_block_text() in multiline mode.
1958 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
1959 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1960 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1961 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
1962 deftype
= ROFFDEF_ANY
;
1963 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
1964 switch (deftype
) { /* Before appending, ... */
1965 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
1966 roff_setstrn(&r
->strtab
, name
, namesz
,
1967 value
, strlen(value
), 0);
1969 case ROFFDEF_REN
: /* call original standard macro. */
1970 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1971 (int)strlen(value
), value
);
1972 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1973 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
1976 case ROFFDEF_STD
: /* rename and call standard macro. */
1977 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
1978 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
1979 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
1981 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
1993 /* Get the custom end marker. */
1996 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1998 /* Resolve the end marker if it is indirect. */
2000 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2001 deftype
= ROFFDEF_USER
;
2002 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2004 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
2005 r
->parse
, ln
, (int)(iname
- buf
->buf
),
2006 "%.*s", (int)namesz
, iname
);
2009 namesz
= strlen(name
);
2014 r
->last
->end
= mandoc_strndup(name
, namesz
);
2017 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2018 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2024 roff_block_sub(ROFF_ARGS
)
2030 * First check whether a custom macro exists at this level. If
2031 * it does, then check against it. This is some of groff's
2032 * stranger behaviours. If we encountered a custom end-scope
2033 * tag and that tag also happens to be a "real" macro, then we
2034 * need to try interpreting it again as a real macro. If it's
2035 * not, then return ignore. Else continue.
2039 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2040 if (buf
->buf
[i
] != r
->last
->end
[j
])
2043 if (r
->last
->end
[j
] == '\0' &&
2044 (buf
->buf
[i
] == '\0' ||
2045 buf
->buf
[i
] == ' ' ||
2046 buf
->buf
[i
] == '\t')) {
2048 roffnode_cleanscope(r
);
2050 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2054 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2062 * If we have no custom end-query or lookup failed, then try
2063 * pulling it out of the hashtable.
2066 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2068 if (t
!= ROFF_cblock
) {
2070 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2074 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2078 roff_block_text(ROFF_ARGS
)
2082 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2088 roff_cond_sub(ROFF_ARGS
)
2091 int endloop
, irc
, rr
;
2096 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2097 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2098 if (roffnode_cleanscope(r
))
2102 * If `\}' occurs on a macro line without a preceding macro,
2103 * drop the line completely.
2106 ep
= buf
->buf
+ pos
;
2107 if (ep
[0] == '\\' && ep
[1] == '}')
2111 * The closing delimiter `\}' rewinds the conditional scope
2112 * but is otherwise ignored when interpreting the line.
2115 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2118 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2119 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2132 * Fully handle known macros when they are structurally
2133 * required or when the conditional evaluated to true.
2136 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2137 irc
|= t
!= TOKEN_NONE
&& (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
) ?
2138 (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
) :
2139 rr
? ROFF_CONT
: ROFF_IGN
;
2144 roff_cond_text(ROFF_ARGS
)
2147 int endloop
, irc
, rr
;
2151 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2152 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2153 if (roffnode_cleanscope(r
))
2157 * If `\}' occurs on a text line with neither preceding
2158 * nor following characters, drop the line completely.
2161 ep
= buf
->buf
+ pos
;
2162 if (strcmp(ep
, "\\}") == 0)
2166 * The closing delimiter `\}' rewinds the conditional scope
2167 * but is otherwise ignored when interpreting the line.
2170 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2173 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2174 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2190 /* --- handling of numeric and conditional expressions -------------------- */
2193 * Parse a single signed integer number. Stop at the first non-digit.
2194 * If there is at least one digit, return success and advance the
2195 * parse point, else return failure and let the parse point unchanged.
2196 * Ignore overflows, treat them just like the C language.
2199 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2201 int myres
, scaled
, n
, p
;
2208 if (n
|| v
[p
] == '+')
2211 if (flags
& ROFFNUM_WHITE
)
2212 while (isspace((unsigned char)v
[p
]))
2215 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2216 *res
= 10 * *res
+ v
[p
] - '0';
2223 /* Each number may be followed by one optional scaling unit. */
2227 scaled
= *res
* 65536;
2230 scaled
= *res
* 240;
2233 scaled
= *res
* 240 / 2.54;
2244 scaled
= *res
* 10 / 3;
2250 scaled
= *res
* 6 / 25;
2257 if (flags
& ROFFNUM_SCALE
)
2265 * Evaluate a string comparison condition.
2266 * The first character is the delimiter.
2267 * Succeed if the string up to its second occurrence
2268 * matches the string up to its third occurence.
2269 * Advance the cursor after the third occurrence
2270 * or lacking that, to the end of the line.
2273 roff_evalstrcond(const char *v
, int *pos
)
2275 const char *s1
, *s2
, *s3
;
2279 s1
= v
+ *pos
; /* initial delimiter */
2280 s2
= s1
+ 1; /* for scanning the first string */
2281 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2283 if (NULL
== s3
) /* found no middle delimiter */
2286 while ('\0' != *++s3
) {
2287 if (*s2
!= *s3
) { /* mismatch */
2288 s3
= strchr(s3
, *s1
);
2291 if (*s3
== *s1
) { /* found the final delimiter */
2300 s3
= strchr(s2
, '\0');
2301 else if (*s3
!= '\0')
2308 * Evaluate an optionally negated single character, numerical,
2309 * or string condition.
2312 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2314 const char *start
, *end
;
2317 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2319 if ('!' == v
[*pos
]) {
2340 } while (v
[*pos
] == ' ');
2343 * Quirk for groff compatibility:
2344 * The horizontal tab is neither available nor unavailable.
2347 if (v
[*pos
] == '\t') {
2352 /* Printable ASCII characters are available. */
2354 if (v
[*pos
] != '\\') {
2360 switch (mandoc_escape(&end
, &start
, &len
)) {
2361 case ESCAPE_SPECIAL
:
2362 istrue
= mchars_spec2cp(start
, len
) != -1;
2364 case ESCAPE_UNICODE
:
2367 case ESCAPE_NUMBERED
:
2368 istrue
= mchars_num2char(start
, len
) != -1;
2375 return istrue
== wanttrue
;
2382 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2385 else if (v
[*pos
] == 'r')
2386 istrue
= roff_hasregn(r
, name
, sz
);
2388 deftype
= ROFFDEF_ANY
;
2389 roff_getstrn(r
, name
, sz
, &deftype
);
2393 return istrue
== wanttrue
;
2399 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2400 return (number
> 0) == wanttrue
;
2401 else if (*pos
== savepos
)
2402 return roff_evalstrcond(v
, pos
) == wanttrue
;
2408 roff_line_ignore(ROFF_ARGS
)
2415 roff_insec(ROFF_ARGS
)
2418 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2419 ln
, ppos
, roff_name
[tok
]);
2424 roff_unsupp(ROFF_ARGS
)
2427 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2428 ln
, ppos
, roff_name
[tok
]);
2433 roff_cond(ROFF_ARGS
)
2437 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2440 * An `.el' has no conditional body: it will consume the value
2441 * of the current rstack entry set in prior `ie' calls or
2444 * If we're not an `el', however, then evaluate the conditional.
2447 r
->last
->rule
= tok
== ROFF_el
?
2448 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2449 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2452 * An if-else will put the NEGATION of the current evaluated
2453 * conditional into the stack of rules.
2456 if (tok
== ROFF_ie
) {
2457 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2459 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2460 r
->rstacksz
, sizeof(int));
2462 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2465 /* If the parent has false as its rule, then so do we. */
2467 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2472 * If there is nothing on the line after the conditional,
2473 * not even whitespace, use next-line scope.
2474 * Except that .while does not support next-line scope.
2477 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2478 r
->last
->endspan
= 2;
2482 while (buf
->buf
[pos
] == ' ')
2485 /* An opening brace requests multiline scope. */
2487 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2488 r
->last
->endspan
= -1;
2490 while (buf
->buf
[pos
] == ' ')
2496 * Anything else following the conditional causes
2497 * single-line scope. Warn if the scope contains
2498 * nothing but trailing whitespace.
2501 if (buf
->buf
[pos
] == '\0')
2502 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2503 ln
, ppos
, roff_name
[tok
]);
2505 r
->last
->endspan
= 1;
2510 if (tok
== ROFF_while
)
2522 /* Ignore groff compatibility mode for now. */
2524 if (tok
== ROFF_ds1
)
2526 else if (tok
== ROFF_as1
)
2530 * The first word is the name of the string.
2531 * If it is empty or terminated by an escape sequence,
2532 * abort the `ds' request without defining anything.
2535 name
= string
= buf
->buf
+ pos
;
2539 namesz
= roff_getname(r
, &string
, ln
, pos
);
2540 if (name
[namesz
] == '\\')
2543 /* Read past the initial double-quote, if any. */
2547 /* The rest is the value. */
2548 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2550 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2555 * Parse a single operator, one or two characters long.
2556 * If the operator is recognized, return success and advance the
2557 * parse point, else return failure and let the parse point unchanged.
2560 roff_getop(const char *v
, int *pos
, char *res
)
2575 switch (v
[*pos
+ 1]) {
2593 switch (v
[*pos
+ 1]) {
2607 if ('=' == v
[*pos
+ 1])
2619 * Evaluate either a parenthesized numeric expression
2620 * or a single signed integer number.
2623 roff_evalpar(struct roff
*r
, int ln
,
2624 const char *v
, int *pos
, int *res
, int flags
)
2628 return roff_getnum(v
, pos
, res
, flags
);
2631 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2635 * Omission of the closing parenthesis
2636 * is an error in validation mode,
2637 * but ignored in evaluation mode.
2642 else if (NULL
== res
)
2649 * Evaluate a complete numeric expression.
2650 * Proceed left to right, there is no concept of precedence.
2653 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2654 int *pos
, int *res
, int flags
)
2656 int mypos
, operand2
;
2664 if (flags
& ROFFNUM_WHITE
)
2665 while (isspace((unsigned char)v
[*pos
]))
2668 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2672 if (flags
& ROFFNUM_WHITE
)
2673 while (isspace((unsigned char)v
[*pos
]))
2676 if ( ! roff_getop(v
, pos
, &operator))
2679 if (flags
& ROFFNUM_WHITE
)
2680 while (isspace((unsigned char)v
[*pos
]))
2683 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2686 if (flags
& ROFFNUM_WHITE
)
2687 while (isspace((unsigned char)v
[*pos
]))
2704 if (operand2
== 0) {
2705 mandoc_msg(MANDOCERR_DIVZERO
,
2706 r
->parse
, ln
, *pos
, v
);
2713 if (operand2
== 0) {
2714 mandoc_msg(MANDOCERR_DIVZERO
,
2715 r
->parse
, ln
, *pos
, v
);
2722 *res
= *res
< operand2
;
2725 *res
= *res
> operand2
;
2728 *res
= *res
<= operand2
;
2731 *res
= *res
>= operand2
;
2734 *res
= *res
== operand2
;
2737 *res
= *res
!= operand2
;
2740 *res
= *res
&& operand2
;
2743 *res
= *res
|| operand2
;
2746 if (operand2
< *res
)
2750 if (operand2
> *res
)
2760 /* --- register management ------------------------------------------------ */
2763 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2765 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2769 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2770 int val
, char sign
, int step
)
2772 struct roffreg
*reg
;
2774 /* Search for an existing register with the same name. */
2777 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2778 strncmp(reg
->key
.p
, name
, len
) != 0))
2782 /* Create a new register. */
2783 reg
= mandoc_malloc(sizeof(struct roffreg
));
2784 reg
->key
.p
= mandoc_strndup(name
, len
);
2788 reg
->next
= r
->regtab
;
2794 else if ('-' == sign
)
2798 if (step
!= INT_MIN
)
2803 * Handle some predefined read-only number registers.
2804 * For now, return -1 if the requested register is not predefined;
2805 * in case a predefined read-only register having the value -1
2806 * were to turn up, another special value would have to be chosen.
2809 roff_getregro(const struct roff
*r
, const char *name
)
2813 case '$': /* Number of arguments of the last macro evaluated. */
2814 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
2815 case 'A': /* ASCII approximation mode is always off. */
2817 case 'g': /* Groff compatibility mode is always on. */
2819 case 'H': /* Fixed horizontal resolution. */
2821 case 'j': /* Always adjust left margin only. */
2823 case 'T': /* Some output device is always defined. */
2825 case 'V': /* Fixed vertical resolution. */
2833 roff_getreg(struct roff
*r
, const char *name
)
2835 return roff_getregn(r
, name
, strlen(name
), '\0');
2839 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
2841 struct roffreg
*reg
;
2844 if ('.' == name
[0] && 2 == len
) {
2845 val
= roff_getregro(r
, name
+ 1);
2850 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
2851 if (len
== reg
->key
.sz
&&
2852 0 == strncmp(name
, reg
->key
.p
, len
)) {
2855 reg
->val
+= reg
->step
;
2858 reg
->val
-= reg
->step
;
2867 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
2872 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2874 struct roffreg
*reg
;
2877 if ('.' == name
[0] && 2 == len
) {
2878 val
= roff_getregro(r
, name
+ 1);
2883 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2884 if (len
== reg
->key
.sz
&&
2885 0 == strncmp(name
, reg
->key
.p
, len
))
2892 roff_freereg(struct roffreg
*reg
)
2894 struct roffreg
*old_reg
;
2896 while (NULL
!= reg
) {
2907 char *key
, *val
, *step
;
2912 key
= val
= buf
->buf
+ pos
;
2916 keysz
= roff_getname(r
, &val
, ln
, pos
);
2917 if (key
[keysz
] == '\\')
2921 if (sign
== '+' || sign
== '-')
2925 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
2929 while (isspace((unsigned char)*step
))
2931 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
2934 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
2941 struct roffreg
*reg
, **prev
;
2945 name
= cp
= buf
->buf
+ pos
;
2948 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2949 name
[namesz
] = '\0';
2954 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2966 /* --- handler functions for roff requests -------------------------------- */
2975 cp
= buf
->buf
+ pos
;
2976 while (*cp
!= '\0') {
2978 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2979 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2980 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2981 if (name
[namesz
] == '\\')
2992 /* Parse the number of lines. */
2994 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2995 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2996 ln
, ppos
, buf
->buf
+ 1);
3000 while (isspace((unsigned char)buf
->buf
[pos
]))
3004 * Arm the input line trap.
3005 * Special-casing "an-trap" is an ugly workaround to cope
3006 * with DocBook stupidly fiddling with man(7) internals.
3010 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3011 strcmp(buf
->buf
+ pos
, "an-trap") ?
3012 buf
->buf
+ pos
: "br");
3020 enum roff_tok t
, te
;
3027 r
->format
= MPARSE_MDOC
;
3028 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3034 r
->format
= MPARSE_MAN
;
3035 mask
= MPARSE_QUICK
;
3040 if ((r
->options
& mask
) == 0)
3041 for (t
= tok
; t
< te
; t
++)
3042 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3049 if (r
->tbl
== NULL
) {
3050 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3054 if (tbl_end(r
->tbl
) == 0) {
3057 buf
->buf
= mandoc_strdup(".sp");
3060 return ROFF_REPARSE
;
3071 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
3074 tbl_restart(ln
, ppos
, r
->tbl
);
3080 * Handle in-line equation delimiters.
3083 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3086 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3089 * Outside equations, look for an opening delimiter.
3090 * If we are inside an equation, we already know it is
3091 * in-line, or this function wouldn't have been called;
3092 * so look for a closing delimiter.
3095 cp1
= buf
->buf
+ pos
;
3096 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3097 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3102 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3104 /* Handle preceding text, protecting whitespace. */
3106 if (*buf
->buf
!= '\0') {
3113 * Prepare replacing the delimiter with an equation macro
3114 * and drop leading white space from the equation.
3117 if (r
->eqn
== NULL
) {
3124 /* Handle following text, protecting whitespace. */
3132 /* Do the actual replacement. */
3134 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3135 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3139 /* Toggle the in-line state of the eqn subsystem. */
3141 r
->eqn_inline
= r
->eqn
== NULL
;
3142 return ROFF_REPARSE
;
3148 struct roff_node
*n
;
3150 if (r
->man
->macroset
== MACROSET_MAN
)
3151 man_breakscope(r
->man
, ROFF_EQ
);
3152 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3153 if (ln
> r
->man
->last
->line
)
3154 n
->flags
|= NODE_LINE
;
3155 n
->eqn
= mandoc_calloc(1, sizeof(*n
->eqn
));
3156 n
->eqn
->expectargs
= UINT_MAX
;
3157 roff_node_append(r
->man
, n
);
3158 r
->man
->next
= ROFF_NEXT_SIBLING
;
3160 assert(r
->eqn
== NULL
);
3161 if (r
->last_eqn
== NULL
)
3162 r
->last_eqn
= eqn_alloc(r
->parse
);
3164 eqn_reset(r
->last_eqn
);
3165 r
->eqn
= r
->last_eqn
;
3168 if (buf
->buf
[pos
] != '\0')
3169 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3170 ".EQ %s", buf
->buf
+ pos
);
3178 if (r
->eqn
!= NULL
) {
3182 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
3183 if (buf
->buf
[pos
] != '\0')
3184 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3185 "EN %s", buf
->buf
+ pos
);
3192 if (r
->tbl
!= NULL
) {
3193 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
3194 ln
, ppos
, "TS breaks TS");
3197 r
->tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
3199 r
->last_tbl
->next
= r
->tbl
;
3201 r
->first_tbl
= r
->tbl
;
3202 r
->last_tbl
= r
->tbl
;
3207 roff_onearg(ROFF_ARGS
)
3209 struct roff_node
*n
;
3213 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3214 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3216 man_breakscope(r
->man
, tok
);
3218 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3219 r
->man
->last
= roffce_node
;
3220 r
->man
->next
= ROFF_NEXT_SIBLING
;
3223 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3226 cp
= buf
->buf
+ pos
;
3228 while (*cp
!= '\0' && *cp
!= ' ')
3233 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
3234 r
->parse
, ln
, cp
- buf
->buf
,
3235 "%s ... %s", roff_name
[tok
], cp
);
3236 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3239 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3240 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3241 roff_word_alloc(r
->man
, ln
, pos
, "1");
3242 r
->man
->last
->flags
|= NODE_NOSRC
;
3245 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3246 &roffce_lines
, 0) == 0) {
3247 mandoc_vmsg(MANDOCERR_CE_NONUM
,
3248 r
->parse
, ln
, pos
, "ce %s", buf
->buf
+ pos
);
3251 if (roffce_lines
< 1) {
3252 r
->man
->last
= r
->man
->last
->parent
;
3256 roffce_node
= r
->man
->last
->parent
;
3258 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3261 n
->flags
|= NODE_LINE
;
3262 r
->man
->next
= ROFF_NEXT_SIBLING
;
3267 roff_manyarg(ROFF_ARGS
)
3269 struct roff_node
*n
;
3272 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3275 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3276 while (*ep
!= '\0' && *ep
!= ' ')
3280 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3283 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3285 r
->man
->next
= ROFF_NEXT_SIBLING
;
3292 char *oldn
, *newn
, *end
, *value
;
3293 size_t oldsz
, newsz
, valsz
;
3295 newn
= oldn
= buf
->buf
+ pos
;
3299 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3300 if (newn
[newsz
] == '\\' || *oldn
== '\0')
3304 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3308 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3310 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3311 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3319 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3320 man_breakscope(r
->man
, ROFF_br
);
3321 roff_elem_alloc(r
->man
, ln
, ppos
, ROFF_br
);
3322 if (buf
->buf
[pos
] != '\0')
3323 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
3324 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3325 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3326 r
->man
->next
= ROFF_NEXT_SIBLING
;
3337 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3341 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3342 ln
, p
- buf
->buf
, "cc ... %s", p
);
3348 roff_char(ROFF_ARGS
)
3350 const char *p
, *kp
, *vp
;
3354 /* Parse the character to be replaced. */
3356 kp
= buf
->buf
+ pos
;
3358 if (*kp
== '\0' || (*kp
== '\\' &&
3359 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3360 (*p
!= ' ' && *p
!= '\0')) {
3361 mandoc_vmsg(MANDOCERR_CHAR_ARG
, r
->parse
,
3362 ln
, pos
, "char %s", kp
);
3370 * If the replacement string contains a font escape sequence,
3371 * we have to restore the font at the end.
3377 while (*p
!= '\0') {
3380 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3382 case ESCAPE_FONTROMAN
:
3383 case ESCAPE_FONTITALIC
:
3384 case ESCAPE_FONTBOLD
:
3387 case ESCAPE_FONTPREV
:
3395 mandoc_msg(MANDOCERR_CHAR_FONT
, r
->parse
,
3396 ln
, vp
- buf
->buf
, vp
);
3399 * Approximate the effect of .char using the .tr tables.
3400 * XXX In groff, .char and .tr interact differently.
3404 if (r
->xtab
== NULL
)
3405 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3406 assert((unsigned int)*kp
< 128);
3407 free(r
->xtab
[(int)*kp
].p
);
3408 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3409 "%s%s", vp
, font
? "\fP" : "");
3411 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3413 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3429 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
3430 ln
, p
- buf
->buf
, "ec ... %s", p
);
3439 if (buf
->buf
[pos
] != '\0')
3440 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
,
3441 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3448 while (buf
->buf
[pos
] == ' ')
3457 const char *p
, *first
, *second
;
3459 enum mandoc_esc esc
;
3464 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
3468 while (*p
!= '\0') {
3472 if (*first
== '\\') {
3473 esc
= mandoc_escape(&p
, NULL
, NULL
);
3474 if (esc
== ESCAPE_ERROR
) {
3475 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3476 ln
, (int)(p
- buf
->buf
), first
);
3479 fsz
= (size_t)(p
- first
);
3483 if (*second
== '\\') {
3484 esc
= mandoc_escape(&p
, NULL
, NULL
);
3485 if (esc
== ESCAPE_ERROR
) {
3486 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3487 ln
, (int)(p
- buf
->buf
), second
);
3490 ssz
= (size_t)(p
- second
);
3491 } else if (*second
== '\0') {
3492 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3493 ln
, first
- buf
->buf
, "tr %s", first
);
3499 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3504 if (r
->xtab
== NULL
)
3505 r
->xtab
= mandoc_calloc(128,
3506 sizeof(struct roffstr
));
3508 free(r
->xtab
[(int)*first
].p
);
3509 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3510 r
->xtab
[(int)*first
].sz
= ssz
;
3517 * Implementation of the .return request.
3518 * There is no need to call roff_userret() from here.
3519 * The read module will call that after rewinding the reader stack
3520 * to the place from where the current macro was called.
3523 roff_return(ROFF_ARGS
)
3525 if (r
->mstackpos
>= 0)
3526 return ROFF_IGN
| ROFF_USERRET
;
3528 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "return");
3536 char *oldn
, *newn
, *end
;
3537 size_t oldsz
, newsz
;
3540 oldn
= newn
= buf
->buf
+ pos
;
3544 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3545 if (oldn
[oldsz
] == '\\' || *newn
== '\0')
3549 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3553 deftype
= ROFFDEF_ANY
;
3554 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3557 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3558 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3559 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3562 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3563 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3566 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3567 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3568 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3571 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3572 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3575 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3576 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3583 roff_shift(ROFF_ARGS
)
3589 if (buf
->buf
[pos
] != '\0' &&
3590 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3591 mandoc_vmsg(MANDOCERR_CE_NONUM
, r
->parse
,
3592 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3595 if (r
->mstackpos
< 0) {
3596 mandoc_msg(MANDOCERR_REQ_NOMAC
, r
->parse
, ln
, ppos
, "shift");
3599 ctx
= r
->mstack
+ r
->mstackpos
;
3600 if (levels
> ctx
->argc
) {
3601 mandoc_vmsg(MANDOCERR_SHIFT
, r
->parse
,
3602 ln
, pos
, "%d, but max is %d", levels
, ctx
->argc
);
3607 for (i
= 0; i
< levels
; i
++)
3609 ctx
->argc
-= levels
;
3610 for (i
= 0; i
< ctx
->argc
; i
++)
3611 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3620 name
= buf
->buf
+ pos
;
3621 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3624 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3625 * opening anything that's not in our cwd or anything beneath
3626 * it. Thus, explicitly disallow traversing up the file-system
3627 * or using absolute paths.
3630 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3631 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3633 buf
->sz
= mandoc_asprintf(&cp
,
3634 ".sp\nSee the file %s.\n.sp", name
) + 1;
3638 return ROFF_REPARSE
;
3645 /* --- user defined strings and macros ------------------------------------ */
3648 roff_userdef(ROFF_ARGS
)
3651 char *arg
, *ap
, *dst
, *src
;
3654 /* Initialize a new macro stack context. */
3656 if (++r
->mstackpos
== r
->mstacksz
) {
3657 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3658 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3661 ctx
= r
->mstack
+ r
->mstackpos
;
3667 * Collect pointers to macro argument strings,
3668 * NUL-terminating them and escaping quotes.
3671 src
= buf
->buf
+ pos
;
3672 while (*src
!= '\0') {
3673 if (ctx
->argc
== ctx
->argsz
) {
3675 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3676 ctx
->argsz
, sizeof(*ctx
->argv
));
3678 arg
= mandoc_getarg(r
->parse
, &src
, ln
, &pos
);
3679 sz
= 1; /* For the terminating NUL. */
3680 for (ap
= arg
; *ap
!= '\0'; ap
++)
3681 sz
+= *ap
== '"' ? 4 : 1;
3682 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3683 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3685 memcpy(dst
, "\\(dq", 4);
3693 /* Replace the macro invocation by the macro definition. */
3696 buf
->buf
= mandoc_strdup(r
->current_string
);
3697 buf
->sz
= strlen(buf
->buf
) + 1;
3700 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3701 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3705 * Calling a high-level macro that was renamed with .rn.
3706 * r->current_string has already been set up by roff_parse().
3709 roff_renamed(ROFF_ARGS
)
3713 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3714 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3722 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3731 /* Read until end of name and terminate it with NUL. */
3732 for (cp
= name
; 1; cp
++) {
3733 if ('\0' == *cp
|| ' ' == *cp
) {
3740 if ('{' == cp
[1] || '}' == cp
[1])
3745 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3746 "%.*s", (int)(cp
- name
+ 1), name
);
3747 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3751 /* Read past spaces. */
3760 * Store *string into the user-defined string called *name.
3761 * To clear an existing entry, call with (*r, *name, NULL, 0).
3762 * append == 0: replace mode
3763 * append == 1: single-line append mode
3764 * append == 2: multiline append mode, append '\n' after each call
3767 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3772 namesz
= strlen(name
);
3773 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
3774 string
? strlen(string
) : 0, append
);
3775 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3779 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3780 const char *string
, size_t stringsz
, int append
)
3785 size_t oldch
, newch
;
3787 /* Search for an existing string with the same name. */
3790 while (n
&& (namesz
!= n
->key
.sz
||
3791 strncmp(n
->key
.p
, name
, namesz
)))
3795 /* Create a new string table entry. */
3796 n
= mandoc_malloc(sizeof(struct roffkv
));
3797 n
->key
.p
= mandoc_strndup(name
, namesz
);
3803 } else if (0 == append
) {
3813 * One additional byte for the '\n' in multiline mode,
3814 * and one for the terminating '\0'.
3816 newch
= stringsz
+ (1 < append
? 2u : 1u);
3818 if (NULL
== n
->val
.p
) {
3819 n
->val
.p
= mandoc_malloc(newch
);
3824 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3827 /* Skip existing content in the destination buffer. */
3828 c
= n
->val
.p
+ (int)oldch
;
3830 /* Append new content to the destination buffer. */
3832 while (i
< (int)stringsz
) {
3834 * Rudimentary roff copy mode:
3835 * Handle escaped backslashes.
3837 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3842 /* Append terminating bytes. */
3847 n
->val
.sz
= (int)(c
- n
->val
.p
);
3851 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
3854 const struct roffkv
*n
;
3859 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
3860 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3861 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3863 if (*deftype
& ROFFDEF_USER
) {
3864 *deftype
= ROFFDEF_USER
;
3871 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
3872 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
3873 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
3875 if (*deftype
& ROFFDEF_REN
) {
3876 *deftype
= ROFFDEF_REN
;
3883 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
3884 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
3885 predefs
[i
].name
[len
] != '\0')
3887 if (*deftype
& ROFFDEF_PRE
) {
3888 *deftype
= ROFFDEF_PRE
;
3889 return predefs
[i
].str
;
3895 if (r
->man
->macroset
!= MACROSET_MAN
) {
3896 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
3897 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3898 roff_name
[tok
][len
] != '\0')
3900 if (*deftype
& ROFFDEF_STD
) {
3901 *deftype
= ROFFDEF_STD
;
3909 if (r
->man
->macroset
!= MACROSET_MDOC
) {
3910 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
3911 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
3912 roff_name
[tok
][len
] != '\0')
3914 if (*deftype
& ROFFDEF_STD
) {
3915 *deftype
= ROFFDEF_STD
;
3924 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
3925 if (*deftype
& ROFFDEF_REN
) {
3927 * This might still be a request,
3928 * so do not treat it as undefined yet.
3930 *deftype
= ROFFDEF_UNDEF
;
3934 /* Using an undefined string defines it to be empty. */
3936 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
3937 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
3945 roff_freestr(struct roffkv
*r
)
3947 struct roffkv
*n
, *nn
;
3949 for (n
= r
; n
; n
= nn
) {
3957 /* --- accessors and utility functions ------------------------------------ */
3960 * Duplicate an input string, making the appropriate character
3961 * conversations (as stipulated by `tr') along the way.
3962 * Returns a heap-allocated string with all the replacements made.
3965 roff_strdup(const struct roff
*r
, const char *p
)
3967 const struct roffkv
*cp
;
3971 enum mandoc_esc esc
;
3973 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3974 return mandoc_strdup(p
);
3975 else if ('\0' == *p
)
3976 return mandoc_strdup("");
3979 * Step through each character looking for term matches
3980 * (remember that a `tr' can be invoked with an escape, which is
3981 * a glyph but the escape is multi-character).
3982 * We only do this if the character hash has been initialised
3983 * and the string is >0 length.
3989 while ('\0' != *p
) {
3990 assert((unsigned int)*p
< 128);
3991 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
3992 sz
= r
->xtab
[(int)*p
].sz
;
3993 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3994 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3998 } else if ('\\' != *p
) {
3999 res
= mandoc_realloc(res
, ssz
+ 2);
4004 /* Search for term matches. */
4005 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4006 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4011 * A match has been found.
4012 * Append the match to the array and move
4013 * forward by its keysize.
4015 res
= mandoc_realloc(res
,
4016 ssz
+ cp
->val
.sz
+ 1);
4017 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4019 p
+= (int)cp
->key
.sz
;
4024 * Handle escapes carefully: we need to copy
4025 * over just the escape itself, or else we might
4026 * do replacements within the escape itself.
4027 * Make sure to pass along the bogus string.
4030 esc
= mandoc_escape(&p
, NULL
, NULL
);
4031 if (ESCAPE_ERROR
== esc
) {
4033 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4034 memcpy(res
+ ssz
, pp
, sz
);
4038 * We bail out on bad escapes.
4039 * No need to warn: we already did so when
4040 * roff_res() was called.
4043 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4044 memcpy(res
+ ssz
, pp
, sz
);
4048 res
[(int)ssz
] = '\0';
4053 roff_getformat(const struct roff
*r
)
4060 * Find out whether a line is a macro line or not.
4061 * If it is, adjust the current position and return one; if it isn't,
4062 * return zero and don't change the current position.
4063 * If the control character has been set with `.cc', then let that grain
4065 * This is slighly contrary to groff, where using the non-breaking
4066 * control character when `cc' has been invoked will cause the
4067 * non-breaking macro contents to be printed verbatim.
4070 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4076 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4078 else if (r
->control
!= '\0')
4080 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4082 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4087 while (' ' == cp
[pos
] || '\t' == cp
[pos
])