]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
b78ef59ee3245fb9e6e4c0cbdde5911512ee7ea4
1 /* $Id: roff.c,v 1.393 2022/06/03 12:15:55 schwarze Exp $ */
3 * Copyright (c) 2010-2015, 2017-2022 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Implementation of the roff(7) parser for mandoc(1).
22 #include <sys/types.h>
33 #include "mandoc_aux.h"
34 #include "mandoc_ohash.h"
37 #include "mandoc_parse.h"
38 #include "libmandoc.h"
40 #include "tbl_parse.h"
41 #include "eqn_parse.h"
44 * ASCII_ESC is used to signal from roff_getarg() to roff_expand()
45 * that an escape sequence resulted from copy-in processing and
46 * needs to be checked or interpolated. As it is used nowhere
47 * else, it is defined here rather than in a header file.
51 /* Maximum number of string expansions per line, to break infinite loops. */
52 #define EXPAND_LIMIT 1000
54 /* Types of definitions of macros and strings. */
55 #define ROFFDEF_USER (1 << 1) /* User-defined. */
56 #define ROFFDEF_PRE (1 << 2) /* Predefined. */
57 #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
58 #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
59 #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
60 ROFFDEF_REN | ROFFDEF_STD)
61 #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
63 /* --- data types --------------------------------------------------------- */
66 * An incredibly-simple string buffer.
69 char *p
; /* nil-terminated buffer */
70 size_t sz
; /* saved strlen(p) */
74 * A key-value roffstr pair as part of a singly-linked list.
79 struct roffkv
*next
; /* next in list */
83 * A single number register as part of a singly-linked list.
93 * Association of request and macro names with token IDs.
101 * A macro processing context.
102 * More than one is needed when macro calls are nested.
111 struct roff_man
*man
; /* mdoc or man parser */
112 struct roffnode
*last
; /* leaf of stack */
113 struct mctx
*mstack
; /* stack of macro contexts */
114 int *rstack
; /* stack of inverted `ie' values */
115 struct ohash
*reqtab
; /* request lookup table */
116 struct roffreg
*regtab
; /* number registers */
117 struct roffkv
*strtab
; /* user-defined strings & macros */
118 struct roffkv
*rentab
; /* renamed strings & macros */
119 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
120 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
121 const char *current_string
; /* value of last called user macro */
122 struct tbl_node
*first_tbl
; /* first table parsed */
123 struct tbl_node
*last_tbl
; /* last table parsed */
124 struct tbl_node
*tbl
; /* current table being parsed */
125 struct eqn_node
*last_eqn
; /* equation parser */
126 struct eqn_node
*eqn
; /* active equation parser */
127 int eqn_inline
; /* current equation is inline */
128 int options
; /* parse options */
129 int mstacksz
; /* current size of mstack */
130 int mstackpos
; /* position in mstack */
131 int rstacksz
; /* current size limit of rstack */
132 int rstackpos
; /* position in rstack */
133 int format
; /* current file in mdoc or man format */
134 char control
; /* control character */
135 char escape
; /* escape character */
139 * A macro definition, condition, or ignored block.
142 enum roff_tok tok
; /* type of node */
143 struct roffnode
*parent
; /* up one in stack */
144 int line
; /* parse line */
145 int col
; /* parse col */
146 char *name
; /* node name, e.g. macro name */
147 char *end
; /* custom end macro of the block */
148 int endspan
; /* scope to: 1=eol 2=next line -1=\} */
149 int rule
; /* content is: 1=evaluated 0=skipped */
152 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
153 enum roff_tok tok, /* tok of macro */ \
154 struct buf *buf, /* input buffer */ \
155 int ln, /* parse line */ \
156 int ppos, /* original pos in buffer */ \
157 int pos, /* current pos in buffer */ \
158 int *offs /* reset offset of buffer data */
160 typedef int (*roffproc
)(ROFF_ARGS
);
163 roffproc proc
; /* process new macro */
164 roffproc text
; /* process as child text of macro */
165 roffproc sub
; /* process as child of macro */
167 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
171 const char *name
; /* predefined input name */
172 const char *str
; /* replacement symbol */
175 #define PREDEF(__name, __str) \
176 { (__name), (__str) },
178 /* --- function prototypes ------------------------------------------------ */
180 static int roffnode_cleanscope(struct roff
*);
181 static int roffnode_pop(struct roff
*);
182 static void roffnode_push(struct roff
*, enum roff_tok
,
183 const char *, int, int);
184 static void roff_addtbl(struct roff_man
*, int, struct tbl_node
*);
185 static int roff_als(ROFF_ARGS
);
186 static int roff_block(ROFF_ARGS
);
187 static int roff_block_text(ROFF_ARGS
);
188 static int roff_block_sub(ROFF_ARGS
);
189 static int roff_break(ROFF_ARGS
);
190 static int roff_cblock(ROFF_ARGS
);
191 static int roff_cc(ROFF_ARGS
);
192 static int roff_ccond(struct roff
*, int, int);
193 static int roff_char(ROFF_ARGS
);
194 static int roff_cond(ROFF_ARGS
);
195 static int roff_cond_checkend(ROFF_ARGS
);
196 static int roff_cond_text(ROFF_ARGS
);
197 static int roff_cond_sub(ROFF_ARGS
);
198 static int roff_ds(ROFF_ARGS
);
199 static int roff_ec(ROFF_ARGS
);
200 static int roff_eo(ROFF_ARGS
);
201 static int roff_eqndelim(struct roff
*, struct buf
*, int);
202 static int roff_evalcond(struct roff
*, int, char *, int *);
203 static int roff_evalnum(struct roff
*, int,
204 const char *, int *, int *, int);
205 static int roff_evalpar(struct roff
*, int,
206 const char *, int *, int *, int);
207 static int roff_evalstrcond(const char *, int *);
208 static int roff_expand(struct roff
*, struct buf
*,
210 static void roff_expand_patch(struct buf
*, int,
212 static void roff_free1(struct roff
*);
213 static void roff_freereg(struct roffreg
*);
214 static void roff_freestr(struct roffkv
*);
215 static size_t roff_getname(struct roff
*, char **, int, int);
216 static int roff_getnum(const char *, int *, int *, int);
217 static int roff_getop(const char *, int *, char *);
218 static int roff_getregn(struct roff
*,
219 const char *, size_t, char);
220 static int roff_getregro(const struct roff
*,
222 static const char *roff_getstrn(struct roff
*,
223 const char *, size_t, int *);
224 static int roff_hasregn(const struct roff
*,
225 const char *, size_t);
226 static int roff_insec(ROFF_ARGS
);
227 static int roff_it(ROFF_ARGS
);
228 static int roff_line_ignore(ROFF_ARGS
);
229 static void roff_man_alloc1(struct roff_man
*);
230 static void roff_man_free1(struct roff_man
*);
231 static int roff_manyarg(ROFF_ARGS
);
232 static int roff_mc(ROFF_ARGS
);
233 static int roff_noarg(ROFF_ARGS
);
234 static int roff_nop(ROFF_ARGS
);
235 static int roff_nr(ROFF_ARGS
);
236 static int roff_onearg(ROFF_ARGS
);
237 static enum roff_tok
roff_parse(struct roff
*, char *, int *,
239 static int roff_parse_comment(struct roff
*, struct buf
*,
241 static int roff_parsetext(struct roff
*, struct buf
*,
243 static int roff_renamed(ROFF_ARGS
);
244 static int roff_req_or_macro(ROFF_ARGS
);
245 static int roff_return(ROFF_ARGS
);
246 static int roff_rm(ROFF_ARGS
);
247 static int roff_rn(ROFF_ARGS
);
248 static int roff_rr(ROFF_ARGS
);
249 static void roff_setregn(struct roff
*, const char *,
250 size_t, int, char, int);
251 static void roff_setstr(struct roff
*,
252 const char *, const char *, int);
253 static void roff_setstrn(struct roffkv
**, const char *,
254 size_t, const char *, size_t, int);
255 static int roff_shift(ROFF_ARGS
);
256 static int roff_so(ROFF_ARGS
);
257 static int roff_tr(ROFF_ARGS
);
258 static int roff_Dd(ROFF_ARGS
);
259 static int roff_TE(ROFF_ARGS
);
260 static int roff_TS(ROFF_ARGS
);
261 static int roff_EQ(ROFF_ARGS
);
262 static int roff_EN(ROFF_ARGS
);
263 static int roff_T_(ROFF_ARGS
);
264 static int roff_unsupp(ROFF_ARGS
);
265 static int roff_userdef(ROFF_ARGS
);
267 /* --- constant data ------------------------------------------------------ */
269 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
270 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
272 const char *__roff_name
[MAN_MAX
+ 1] = {
273 "br", "ce", "fi", "ft",
277 "ab", "ad", "af", "aln",
278 "als", "am", "am1", "ami",
279 "ami1", "as", "as1", "asciify",
280 "backtrace", "bd", "bleedat", "blm",
281 "box", "boxa", "bp", "BP",
282 "break", "breakchar", "brnl", "brp",
284 "cf", "cflags", "ch", "char",
285 "chop", "class", "close", "CL",
286 "color", "composite", "continue", "cp",
287 "cropat", "cs", "cu", "da",
288 "dch", "Dd", "de", "de1",
289 "defcolor", "dei", "dei1", "device",
290 "devicem", "di", "do", "ds",
291 "ds1", "dwh", "dt", "ec",
292 "ecr", "ecs", "el", "em",
293 "EN", "eo", "EP", "EQ",
294 "errprint", "ev", "evc", "ex",
295 "fallback", "fam", "fc", "fchar",
296 "fcolor", "fdeferlig", "feature", "fkern",
297 "fl", "flig", "fp", "fps",
298 "fschar", "fspacewidth", "fspecial", "ftr",
299 "fzoom", "gcolor", "hc", "hcode",
300 "hidechar", "hla", "hlm", "hpf",
301 "hpfa", "hpfcode", "hw", "hy",
302 "hylang", "hylen", "hym", "hypp",
303 "hys", "ie", "if", "ig",
304 "index", "it", "itc", "IX",
305 "kern", "kernafter", "kernbefore", "kernpair",
306 "lc", "lc_ctype", "lds", "length",
307 "letadj", "lf", "lg", "lhang",
308 "linetabs", "lnr", "lnrf", "lpfx",
310 "mediasize", "minss", "mk", "mso",
311 "na", "ne", "nh", "nhychar",
312 "nm", "nn", "nop", "nr",
313 "nrf", "nroff", "ns", "nx",
314 "open", "opena", "os", "output",
315 "padj", "papersize", "pc", "pev",
316 "pi", "PI", "pl", "pm",
318 "psbb", "pshape", "pso", "ptr",
319 "pvs", "rchar", "rd", "recursionlimit",
320 "return", "rfschar", "rhang",
321 "rm", "rn", "rnn", "rr",
322 "rs", "rt", "schar", "sentchar",
323 "shc", "shift", "sizes", "so",
324 "spacewidth", "special", "spreadwarn", "ss",
325 "sty", "substring", "sv", "sy",
328 "tm", "tm1", "tmc", "tr",
329 "track", "transchar", "trf", "trimat",
330 "trin", "trnt", "troff", "TS",
331 "uf", "ul", "unformat", "unwatch",
332 "unwatchn", "vpt", "vs", "warn",
333 "warnscale", "watch", "watchlength", "watchn",
334 "wh", "while", "write", "writec",
335 "writem", "xflag", ".", NULL
,
337 "Dd", "Dt", "Os", "Sh",
338 "Ss", "Pp", "D1", "Dl",
339 "Bd", "Ed", "Bl", "El",
340 "It", "Ad", "An", "Ap",
341 "Ar", "Cd", "Cm", "Dv",
342 "Er", "Ev", "Ex", "Fa",
343 "Fd", "Fl", "Fn", "Ft",
344 "Ic", "In", "Li", "Nd",
345 "Nm", "Op", "Ot", "Pa",
346 "Rv", "St", "Va", "Vt",
347 "Xr", "%A", "%B", "%D",
348 "%I", "%J", "%N", "%O",
349 "%P", "%R", "%T", "%V",
350 "Ac", "Ao", "Aq", "At",
351 "Bc", "Bf", "Bo", "Bq",
352 "Bsx", "Bx", "Db", "Dc",
353 "Do", "Dq", "Ec", "Ef",
354 "Em", "Eo", "Fx", "Ms",
355 "No", "Ns", "Nx", "Ox",
356 "Pc", "Pf", "Po", "Pq",
357 "Qc", "Ql", "Qo", "Qq",
358 "Re", "Rs", "Sc", "So",
359 "Sq", "Sm", "Sx", "Sy",
360 "Tn", "Ux", "Xc", "Xo",
361 "Fo", "Fc", "Oo", "Oc",
362 "Bk", "Ek", "Bt", "Hf",
363 "Fr", "Ud", "Lb", "Lp",
364 "Lk", "Mt", "Brq", "Bro",
365 "Brc", "%C", "Es", "En",
366 "Dx", "%Q", "%U", "Ta",
368 "TH", "SH", "SS", "TP",
370 "LP", "PP", "P", "IP",
371 "HP", "SM", "SB", "BI",
372 "IB", "BR", "RB", "R",
373 "B", "I", "IR", "RI",
374 "RE", "RS", "DT", "UC",
378 "UE", "MT", "ME", NULL
380 const char *const *roff_name
= __roff_name
;
382 static struct roffmac roffs
[TOKEN_NONE
] = {
383 { roff_noarg
, NULL
, NULL
, 0 }, /* br */
384 { roff_onearg
, NULL
, NULL
, 0 }, /* ce */
385 { roff_noarg
, NULL
, NULL
, 0 }, /* fi */
386 { roff_onearg
, NULL
, NULL
, 0 }, /* ft */
387 { roff_onearg
, NULL
, NULL
, 0 }, /* ll */
388 { roff_mc
, NULL
, NULL
, 0 }, /* mc */
389 { roff_noarg
, NULL
, NULL
, 0 }, /* nf */
390 { roff_onearg
, NULL
, NULL
, 0 }, /* po */
391 { roff_onearg
, NULL
, NULL
, 0 }, /* rj */
392 { roff_onearg
, NULL
, NULL
, 0 }, /* sp */
393 { roff_manyarg
, NULL
, NULL
, 0 }, /* ta */
394 { roff_onearg
, NULL
, NULL
, 0 }, /* ti */
395 { NULL
, NULL
, NULL
, 0 }, /* ROFF_MAX */
396 { roff_unsupp
, NULL
, NULL
, 0 }, /* ab */
397 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ad */
398 { roff_line_ignore
, NULL
, NULL
, 0 }, /* af */
399 { roff_unsupp
, NULL
, NULL
, 0 }, /* aln */
400 { roff_als
, NULL
, NULL
, 0 }, /* als */
401 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am */
402 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* am1 */
403 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami */
404 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ami1 */
405 { roff_ds
, NULL
, NULL
, 0 }, /* as */
406 { roff_ds
, NULL
, NULL
, 0 }, /* as1 */
407 { roff_unsupp
, NULL
, NULL
, 0 }, /* asciify */
408 { roff_line_ignore
, NULL
, NULL
, 0 }, /* backtrace */
409 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bd */
410 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bleedat */
411 { roff_unsupp
, NULL
, NULL
, 0 }, /* blm */
412 { roff_unsupp
, NULL
, NULL
, 0 }, /* box */
413 { roff_unsupp
, NULL
, NULL
, 0 }, /* boxa */
414 { roff_line_ignore
, NULL
, NULL
, 0 }, /* bp */
415 { roff_unsupp
, NULL
, NULL
, 0 }, /* BP */
416 { roff_break
, NULL
, NULL
, 0 }, /* break */
417 { roff_line_ignore
, NULL
, NULL
, 0 }, /* breakchar */
418 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brnl */
419 { roff_noarg
, NULL
, NULL
, 0 }, /* brp */
420 { roff_line_ignore
, NULL
, NULL
, 0 }, /* brpnl */
421 { roff_unsupp
, NULL
, NULL
, 0 }, /* c2 */
422 { roff_cc
, NULL
, NULL
, 0 }, /* cc */
423 { roff_insec
, NULL
, NULL
, 0 }, /* cf */
424 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cflags */
425 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ch */
426 { roff_char
, NULL
, NULL
, 0 }, /* char */
427 { roff_unsupp
, NULL
, NULL
, 0 }, /* chop */
428 { roff_line_ignore
, NULL
, NULL
, 0 }, /* class */
429 { roff_insec
, NULL
, NULL
, 0 }, /* close */
430 { roff_unsupp
, NULL
, NULL
, 0 }, /* CL */
431 { roff_line_ignore
, NULL
, NULL
, 0 }, /* color */
432 { roff_unsupp
, NULL
, NULL
, 0 }, /* composite */
433 { roff_unsupp
, NULL
, NULL
, 0 }, /* continue */
434 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cp */
435 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cropat */
436 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cs */
437 { roff_line_ignore
, NULL
, NULL
, 0 }, /* cu */
438 { roff_unsupp
, NULL
, NULL
, 0 }, /* da */
439 { roff_unsupp
, NULL
, NULL
, 0 }, /* dch */
440 { roff_Dd
, NULL
, NULL
, 0 }, /* Dd */
441 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de */
442 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* de1 */
443 { roff_line_ignore
, NULL
, NULL
, 0 }, /* defcolor */
444 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei */
445 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* dei1 */
446 { roff_unsupp
, NULL
, NULL
, 0 }, /* device */
447 { roff_unsupp
, NULL
, NULL
, 0 }, /* devicem */
448 { roff_unsupp
, NULL
, NULL
, 0 }, /* di */
449 { roff_unsupp
, NULL
, NULL
, 0 }, /* do */
450 { roff_ds
, NULL
, NULL
, 0 }, /* ds */
451 { roff_ds
, NULL
, NULL
, 0 }, /* ds1 */
452 { roff_unsupp
, NULL
, NULL
, 0 }, /* dwh */
453 { roff_unsupp
, NULL
, NULL
, 0 }, /* dt */
454 { roff_ec
, NULL
, NULL
, 0 }, /* ec */
455 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecr */
456 { roff_unsupp
, NULL
, NULL
, 0 }, /* ecs */
457 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* el */
458 { roff_unsupp
, NULL
, NULL
, 0 }, /* em */
459 { roff_EN
, NULL
, NULL
, 0 }, /* EN */
460 { roff_eo
, NULL
, NULL
, 0 }, /* eo */
461 { roff_unsupp
, NULL
, NULL
, 0 }, /* EP */
462 { roff_EQ
, NULL
, NULL
, 0 }, /* EQ */
463 { roff_line_ignore
, NULL
, NULL
, 0 }, /* errprint */
464 { roff_unsupp
, NULL
, NULL
, 0 }, /* ev */
465 { roff_unsupp
, NULL
, NULL
, 0 }, /* evc */
466 { roff_unsupp
, NULL
, NULL
, 0 }, /* ex */
467 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fallback */
468 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fam */
469 { roff_unsupp
, NULL
, NULL
, 0 }, /* fc */
470 { roff_unsupp
, NULL
, NULL
, 0 }, /* fchar */
471 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fcolor */
472 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fdeferlig */
473 { roff_line_ignore
, NULL
, NULL
, 0 }, /* feature */
474 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fkern */
475 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fl */
476 { roff_line_ignore
, NULL
, NULL
, 0 }, /* flig */
477 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fp */
478 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fps */
479 { roff_unsupp
, NULL
, NULL
, 0 }, /* fschar */
480 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspacewidth */
481 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fspecial */
482 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ftr */
483 { roff_line_ignore
, NULL
, NULL
, 0 }, /* fzoom */
484 { roff_line_ignore
, NULL
, NULL
, 0 }, /* gcolor */
485 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hc */
486 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hcode */
487 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hidechar */
488 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hla */
489 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hlm */
490 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpf */
491 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfa */
492 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hpfcode */
493 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hw */
494 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hy */
495 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylang */
496 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hylen */
497 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hym */
498 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hypp */
499 { roff_line_ignore
, NULL
, NULL
, 0 }, /* hys */
500 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* ie */
501 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /* if */
502 { roff_block
, roff_block_text
, roff_block_sub
, 0 }, /* ig */
503 { roff_unsupp
, NULL
, NULL
, 0 }, /* index */
504 { roff_it
, NULL
, NULL
, 0 }, /* it */
505 { roff_unsupp
, NULL
, NULL
, 0 }, /* itc */
506 { roff_line_ignore
, NULL
, NULL
, 0 }, /* IX */
507 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kern */
508 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernafter */
509 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernbefore */
510 { roff_line_ignore
, NULL
, NULL
, 0 }, /* kernpair */
511 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc */
512 { roff_unsupp
, NULL
, NULL
, 0 }, /* lc_ctype */
513 { roff_unsupp
, NULL
, NULL
, 0 }, /* lds */
514 { roff_unsupp
, NULL
, NULL
, 0 }, /* length */
515 { roff_line_ignore
, NULL
, NULL
, 0 }, /* letadj */
516 { roff_insec
, NULL
, NULL
, 0 }, /* lf */
517 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lg */
518 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lhang */
519 { roff_unsupp
, NULL
, NULL
, 0 }, /* linetabs */
520 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnr */
521 { roff_unsupp
, NULL
, NULL
, 0 }, /* lnrf */
522 { roff_unsupp
, NULL
, NULL
, 0 }, /* lpfx */
523 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ls */
524 { roff_unsupp
, NULL
, NULL
, 0 }, /* lsm */
525 { roff_line_ignore
, NULL
, NULL
, 0 }, /* lt */
526 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mediasize */
527 { roff_line_ignore
, NULL
, NULL
, 0 }, /* minss */
528 { roff_line_ignore
, NULL
, NULL
, 0 }, /* mk */
529 { roff_insec
, NULL
, NULL
, 0 }, /* mso */
530 { roff_line_ignore
, NULL
, NULL
, 0 }, /* na */
531 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ne */
532 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nh */
533 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nhychar */
534 { roff_unsupp
, NULL
, NULL
, 0 }, /* nm */
535 { roff_unsupp
, NULL
, NULL
, 0 }, /* nn */
536 { roff_nop
, NULL
, NULL
, 0 }, /* nop */
537 { roff_nr
, NULL
, NULL
, 0 }, /* nr */
538 { roff_unsupp
, NULL
, NULL
, 0 }, /* nrf */
539 { roff_line_ignore
, NULL
, NULL
, 0 }, /* nroff */
540 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ns */
541 { roff_insec
, NULL
, NULL
, 0 }, /* nx */
542 { roff_insec
, NULL
, NULL
, 0 }, /* open */
543 { roff_insec
, NULL
, NULL
, 0 }, /* opena */
544 { roff_line_ignore
, NULL
, NULL
, 0 }, /* os */
545 { roff_unsupp
, NULL
, NULL
, 0 }, /* output */
546 { roff_line_ignore
, NULL
, NULL
, 0 }, /* padj */
547 { roff_line_ignore
, NULL
, NULL
, 0 }, /* papersize */
548 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pc */
549 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pev */
550 { roff_insec
, NULL
, NULL
, 0 }, /* pi */
551 { roff_unsupp
, NULL
, NULL
, 0 }, /* PI */
552 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pl */
553 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pm */
554 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pn */
555 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pnr */
556 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ps */
557 { roff_unsupp
, NULL
, NULL
, 0 }, /* psbb */
558 { roff_unsupp
, NULL
, NULL
, 0 }, /* pshape */
559 { roff_insec
, NULL
, NULL
, 0 }, /* pso */
560 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ptr */
561 { roff_line_ignore
, NULL
, NULL
, 0 }, /* pvs */
562 { roff_unsupp
, NULL
, NULL
, 0 }, /* rchar */
563 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rd */
564 { roff_line_ignore
, NULL
, NULL
, 0 }, /* recursionlimit */
565 { roff_return
, NULL
, NULL
, 0 }, /* return */
566 { roff_unsupp
, NULL
, NULL
, 0 }, /* rfschar */
567 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rhang */
568 { roff_rm
, NULL
, NULL
, 0 }, /* rm */
569 { roff_rn
, NULL
, NULL
, 0 }, /* rn */
570 { roff_unsupp
, NULL
, NULL
, 0 }, /* rnn */
571 { roff_rr
, NULL
, NULL
, 0 }, /* rr */
572 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rs */
573 { roff_line_ignore
, NULL
, NULL
, 0 }, /* rt */
574 { roff_unsupp
, NULL
, NULL
, 0 }, /* schar */
575 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sentchar */
576 { roff_line_ignore
, NULL
, NULL
, 0 }, /* shc */
577 { roff_shift
, NULL
, NULL
, 0 }, /* shift */
578 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sizes */
579 { roff_so
, NULL
, NULL
, 0 }, /* so */
580 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spacewidth */
581 { roff_line_ignore
, NULL
, NULL
, 0 }, /* special */
582 { roff_line_ignore
, NULL
, NULL
, 0 }, /* spreadwarn */
583 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ss */
584 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sty */
585 { roff_unsupp
, NULL
, NULL
, 0 }, /* substring */
586 { roff_line_ignore
, NULL
, NULL
, 0 }, /* sv */
587 { roff_insec
, NULL
, NULL
, 0 }, /* sy */
588 { roff_T_
, NULL
, NULL
, 0 }, /* T& */
589 { roff_unsupp
, NULL
, NULL
, 0 }, /* tc */
590 { roff_TE
, NULL
, NULL
, 0 }, /* TE */
591 { roff_Dd
, NULL
, NULL
, 0 }, /* TH */
592 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tkf */
593 { roff_unsupp
, NULL
, NULL
, 0 }, /* tl */
594 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm */
595 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tm1 */
596 { roff_line_ignore
, NULL
, NULL
, 0 }, /* tmc */
597 { roff_tr
, NULL
, NULL
, 0 }, /* tr */
598 { roff_line_ignore
, NULL
, NULL
, 0 }, /* track */
599 { roff_line_ignore
, NULL
, NULL
, 0 }, /* transchar */
600 { roff_insec
, NULL
, NULL
, 0 }, /* trf */
601 { roff_line_ignore
, NULL
, NULL
, 0 }, /* trimat */
602 { roff_unsupp
, NULL
, NULL
, 0 }, /* trin */
603 { roff_unsupp
, NULL
, NULL
, 0 }, /* trnt */
604 { roff_line_ignore
, NULL
, NULL
, 0 }, /* troff */
605 { roff_TS
, NULL
, NULL
, 0 }, /* TS */
606 { roff_line_ignore
, NULL
, NULL
, 0 }, /* uf */
607 { roff_line_ignore
, NULL
, NULL
, 0 }, /* ul */
608 { roff_unsupp
, NULL
, NULL
, 0 }, /* unformat */
609 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatch */
610 { roff_line_ignore
, NULL
, NULL
, 0 }, /* unwatchn */
611 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vpt */
612 { roff_line_ignore
, NULL
, NULL
, 0 }, /* vs */
613 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warn */
614 { roff_line_ignore
, NULL
, NULL
, 0 }, /* warnscale */
615 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watch */
616 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchlength */
617 { roff_line_ignore
, NULL
, NULL
, 0 }, /* watchn */
618 { roff_unsupp
, NULL
, NULL
, 0 }, /* wh */
619 { roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
}, /*while*/
620 { roff_insec
, NULL
, NULL
, 0 }, /* write */
621 { roff_insec
, NULL
, NULL
, 0 }, /* writec */
622 { roff_insec
, NULL
, NULL
, 0 }, /* writem */
623 { roff_line_ignore
, NULL
, NULL
, 0 }, /* xflag */
624 { roff_cblock
, NULL
, NULL
, 0 }, /* . */
625 { roff_renamed
, NULL
, NULL
, 0 },
626 { roff_userdef
, NULL
, NULL
, 0 }
629 /* Array of injected predefined strings. */
630 #define PREDEFS_MAX 38
631 static const struct predef predefs
[PREDEFS_MAX
] = {
632 #include "predefs.in"
635 static int roffce_lines
; /* number of input lines to center */
636 static struct roff_node
*roffce_node
; /* active request */
637 static int roffit_lines
; /* number of lines to delay */
638 static char *roffit_macro
; /* nil-terminated macro line */
641 /* --- request table ------------------------------------------------------ */
644 roffhash_alloc(enum roff_tok mintok
, enum roff_tok maxtok
)
652 htab
= mandoc_malloc(sizeof(*htab
));
653 mandoc_ohash_init(htab
, 8, offsetof(struct roffreq
, name
));
655 for (tok
= mintok
; tok
< maxtok
; tok
++) {
656 if (roff_name
[tok
] == NULL
)
658 sz
= strlen(roff_name
[tok
]);
659 req
= mandoc_malloc(sizeof(*req
) + sz
+ 1);
661 memcpy(req
->name
, roff_name
[tok
], sz
+ 1);
662 slot
= ohash_qlookup(htab
, req
->name
);
663 ohash_insert(htab
, slot
, req
);
669 roffhash_free(struct ohash
*htab
)
676 for (req
= ohash_first(htab
, &slot
); req
!= NULL
;
677 req
= ohash_next(htab
, &slot
))
684 roffhash_find(struct ohash
*htab
, const char *name
, size_t sz
)
691 req
= ohash_find(htab
, ohash_qlookupi(htab
, name
, &end
));
693 req
= ohash_find(htab
, ohash_qlookup(htab
, name
));
694 return req
== NULL
? TOKEN_NONE
: req
->tok
;
697 /* --- stack of request blocks -------------------------------------------- */
700 * Pop the current node off of the stack of roff instructions currently
701 * pending. Return 1 if it is a loop or 0 otherwise.
704 roffnode_pop(struct roff
*r
)
710 inloop
= p
->tok
== ROFF_while
;
719 * Push a roff node onto the instruction stack. This must later be
720 * removed with roffnode_pop().
723 roffnode_push(struct roff
*r
, enum roff_tok tok
, const char *name
,
728 p
= mandoc_calloc(1, sizeof(struct roffnode
));
731 p
->name
= mandoc_strdup(name
);
735 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
740 /* --- roff parser state data management ---------------------------------- */
743 roff_free1(struct roff
*r
)
747 tbl_free(r
->first_tbl
);
748 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
750 eqn_free(r
->last_eqn
);
751 r
->last_eqn
= r
->eqn
= NULL
;
753 while (r
->mstackpos
>= 0)
764 roff_freereg(r
->regtab
);
767 roff_freestr(r
->strtab
);
768 roff_freestr(r
->rentab
);
769 roff_freestr(r
->xmbtab
);
770 r
->strtab
= r
->rentab
= r
->xmbtab
= NULL
;
773 for (i
= 0; i
< 128; i
++)
780 roff_reset(struct roff
*r
)
783 r
->options
|= MPARSE_COMMENT
;
784 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
794 roff_free(struct roff
*r
)
799 for (i
= 0; i
< r
->mstacksz
; i
++)
800 free(r
->mstack
[i
].argv
);
802 roffhash_free(r
->reqtab
);
807 roff_alloc(int options
)
811 r
= mandoc_calloc(1, sizeof(struct roff
));
812 r
->reqtab
= roffhash_alloc(0, ROFF_RENAMED
);
813 r
->options
= options
| MPARSE_COMMENT
;
814 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
821 /* --- syntax tree state data management ---------------------------------- */
824 roff_man_free1(struct roff_man
*man
)
826 if (man
->meta
.first
!= NULL
)
827 roff_node_delete(man
, man
->meta
.first
);
828 free(man
->meta
.msec
);
831 free(man
->meta
.arch
);
832 free(man
->meta
.title
);
833 free(man
->meta
.name
);
834 free(man
->meta
.date
);
835 free(man
->meta
.sodest
);
839 roff_state_reset(struct roff_man
*man
)
841 man
->last
= man
->meta
.first
;
844 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
845 man
->next
= ROFF_NEXT_CHILD
;
846 roff_setreg(man
->roff
, "nS", 0, '=');
850 roff_man_alloc1(struct roff_man
*man
)
852 memset(&man
->meta
, 0, sizeof(man
->meta
));
853 man
->meta
.first
= mandoc_calloc(1, sizeof(*man
->meta
.first
));
854 man
->meta
.first
->type
= ROFFT_ROOT
;
855 man
->meta
.macroset
= MACROSET_NONE
;
856 roff_state_reset(man
);
860 roff_man_reset(struct roff_man
*man
)
863 roff_man_alloc1(man
);
867 roff_man_free(struct roff_man
*man
)
875 roff_man_alloc(struct roff
*roff
, const char *os_s
, int quick
)
877 struct roff_man
*man
;
879 man
= mandoc_calloc(1, sizeof(*man
));
883 roff_man_alloc1(man
);
888 /* --- syntax tree handling ----------------------------------------------- */
891 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
892 enum roff_type type
, int tok
)
896 n
= mandoc_calloc(1, sizeof(*n
));
901 n
->sec
= man
->lastsec
;
903 if (man
->flags
& MDOC_SYNOPSIS
)
904 n
->flags
|= NODE_SYNPRETTY
;
906 n
->flags
&= ~NODE_SYNPRETTY
;
907 if ((man
->flags
& (ROFF_NOFILL
| ROFF_NONOFILL
)) == ROFF_NOFILL
)
908 n
->flags
|= NODE_NOFILL
;
910 n
->flags
&= ~NODE_NOFILL
;
911 if (man
->flags
& MDOC_NEWLINE
)
912 n
->flags
|= NODE_LINE
;
913 man
->flags
&= ~MDOC_NEWLINE
;
919 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
923 case ROFF_NEXT_SIBLING
:
924 if (man
->last
->next
!= NULL
) {
925 n
->next
= man
->last
->next
;
926 man
->last
->next
->prev
= n
;
928 man
->last
->parent
->last
= n
;
931 n
->parent
= man
->last
->parent
;
933 case ROFF_NEXT_CHILD
:
934 if (man
->last
->child
!= NULL
) {
935 n
->next
= man
->last
->child
;
936 man
->last
->child
->prev
= n
;
939 man
->last
->child
= n
;
940 n
->parent
= man
->last
;
952 if (n
->end
!= ENDBODY_NOT
)
964 * Copy over the normalised-data pointer of our parent. Not
965 * everybody has one, but copying a null pointer is fine.
968 n
->norm
= n
->parent
->norm
;
969 assert(n
->parent
->type
== ROFFT_BLOCK
);
973 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
977 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
978 n
->string
= roff_strdup(man
->roff
, word
);
979 roff_node_append(man
, n
);
980 n
->flags
|= NODE_VALID
| NODE_ENDED
;
981 man
->next
= ROFF_NEXT_SIBLING
;
985 roff_word_append(struct roff_man
*man
, const char *word
)
988 char *addstr
, *newstr
;
991 addstr
= roff_strdup(man
->roff
, word
);
992 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
996 man
->next
= ROFF_NEXT_SIBLING
;
1000 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1002 struct roff_node
*n
;
1004 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
1005 roff_node_append(man
, n
);
1006 man
->next
= ROFF_NEXT_CHILD
;
1010 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1012 struct roff_node
*n
;
1014 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1015 roff_node_append(man
, n
);
1016 man
->next
= ROFF_NEXT_CHILD
;
1021 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1023 struct roff_node
*n
;
1025 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1026 roff_node_append(man
, n
);
1027 man
->next
= ROFF_NEXT_CHILD
;
1032 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1034 struct roff_node
*n
;
1036 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1037 roff_node_append(man
, n
);
1038 man
->next
= ROFF_NEXT_CHILD
;
1043 roff_addtbl(struct roff_man
*man
, int line
, struct tbl_node
*tbl
)
1045 struct roff_node
*n
;
1046 struct tbl_span
*span
;
1048 if (man
->meta
.macroset
== MACROSET_MAN
)
1049 man_breakscope(man
, ROFF_TS
);
1050 while ((span
= tbl_span(tbl
)) != NULL
) {
1051 n
= roff_node_alloc(man
, line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1053 roff_node_append(man
, n
);
1054 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1055 man
->next
= ROFF_NEXT_SIBLING
;
1060 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1063 /* Adjust siblings. */
1066 n
->prev
->next
= n
->next
;
1068 n
->next
->prev
= n
->prev
;
1070 /* Adjust parent. */
1072 if (n
->parent
!= NULL
) {
1073 if (n
->parent
->child
== n
)
1074 n
->parent
->child
= n
->next
;
1075 if (n
->parent
->last
== n
)
1076 n
->parent
->last
= n
->prev
;
1079 /* Adjust parse point. */
1083 if (man
->last
== n
) {
1084 if (n
->prev
== NULL
) {
1085 man
->last
= n
->parent
;
1086 man
->next
= ROFF_NEXT_CHILD
;
1088 man
->last
= n
->prev
;
1089 man
->next
= ROFF_NEXT_SIBLING
;
1092 if (man
->meta
.first
== n
)
1093 man
->meta
.first
= NULL
;
1097 roff_node_relink(struct roff_man
*man
, struct roff_node
*n
)
1099 roff_node_unlink(man
, n
);
1100 n
->prev
= n
->next
= NULL
;
1101 roff_node_append(man
, n
);
1105 roff_node_free(struct roff_node
*n
)
1108 if (n
->args
!= NULL
)
1109 mdoc_argv_free(n
->args
);
1110 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1112 eqn_box_free(n
->eqn
);
1119 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1122 while (n
->child
!= NULL
)
1123 roff_node_delete(man
, n
->child
);
1124 roff_node_unlink(man
, n
);
1129 roff_node_transparent(struct roff_node
*n
)
1133 if (n
->type
== ROFFT_COMMENT
|| n
->flags
& NODE_NOPRT
)
1135 return roff_tok_transparent(n
->tok
);
1139 roff_tok_transparent(enum roff_tok tok
)
1162 roff_node_child(struct roff_node
*n
)
1164 for (n
= n
->child
; roff_node_transparent(n
); n
= n
->next
)
1170 roff_node_prev(struct roff_node
*n
)
1174 } while (roff_node_transparent(n
));
1179 roff_node_next(struct roff_node
*n
)
1183 } while (roff_node_transparent(n
));
1188 deroff(char **dest
, const struct roff_node
*n
)
1193 if (n
->string
== NULL
) {
1194 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1199 /* Skip leading whitespace. */
1201 for (cp
= n
->string
; *cp
!= '\0'; cp
++) {
1202 if (cp
[0] == '\\' && cp
[1] != '\0' &&
1203 strchr(" %&0^|~", cp
[1]) != NULL
)
1205 else if ( ! isspace((unsigned char)*cp
))
1209 /* Skip trailing backslash. */
1212 if (sz
> 0 && cp
[sz
- 1] == '\\')
1215 /* Skip trailing whitespace. */
1218 if ( ! isspace((unsigned char)cp
[sz
-1]))
1221 /* Skip empty strings. */
1226 if (*dest
== NULL
) {
1227 *dest
= mandoc_strndup(cp
, sz
);
1231 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1236 /* --- main functions of the roff parser ---------------------------------- */
1239 * Save comments preceding the title macro, for example in order to
1240 * preserve Copyright and license headers in HTML output,
1241 * provide diagnostics about RCS ids and trailing whitespace in comments,
1242 * then discard comments including preceding whitespace.
1243 * This function also handles input line continuation.
1246 roff_parse_comment(struct roff
*r
, struct buf
*buf
, int ln
, int pos
, char ec
)
1248 struct roff_node
*n
; /* used for header comments */
1249 const char *start
; /* start of the string to process */
1250 const char *cp
; /* for RCS id parsing */
1251 char *stesc
; /* start of an escape sequence ('\\') */
1252 char *ep
; /* end of comment string */
1253 int rcsid
; /* kind of RCS id seen */
1255 for (start
= stesc
= buf
->buf
+ pos
;; stesc
++) {
1257 * XXX Ugly hack: Remove the newline character that
1258 * mparse_buf_r() appended to mark the end of input
1259 * if it is not preceded by an escape character.
1261 if (stesc
[0] == '\n') {
1262 assert(stesc
[1] == '\0');
1266 /* The line ends without continuation or comment. */
1267 if (stesc
[0] == '\0')
1270 /* Unescaped byte: skip it. */
1275 * XXX Ugly hack: Do not attempt to append another line
1276 * if the function mparse_buf_r() appended a newline
1277 * character to indicate the end of input.
1279 if (stesc
[1] == '\n') {
1280 assert(stesc
[2] == '\0');
1286 * An escape character at the end of an input line
1287 * requests line continuation.
1289 if (stesc
[1] == '\0') {
1291 return ROFF_IGN
| ROFF_APPEND
;
1294 /* Found a comment: process it. */
1295 if (stesc
[1] == '"' || stesc
[1] == '#')
1298 /* Escaped escape character: skip them both. */
1303 /* Look for an RCS id in the comment. */
1306 if ((cp
= strstr(stesc
+ 2, "$" "OpenBSD")) != NULL
) {
1307 rcsid
= 1 << MANDOC_OS_OPENBSD
;
1309 } else if ((cp
= strstr(stesc
+ 2, "$" "NetBSD")) != NULL
) {
1310 rcsid
= 1 << MANDOC_OS_NETBSD
;
1313 if (cp
!= NULL
&& isalnum((unsigned char)*cp
) == 0 &&
1314 strchr(cp
, '$') != NULL
) {
1315 if (r
->man
->meta
.rcsids
& rcsid
)
1316 mandoc_msg(MANDOCERR_RCS_REP
, ln
,
1317 (int)(stesc
- buf
->buf
) + 2, "%s", stesc
+ 1);
1318 r
->man
->meta
.rcsids
|= rcsid
;
1321 /* Warn about trailing whitespace at the end of the comment. */
1323 ep
= strchr(stesc
+ 2, '\0') - 1;
1326 if (*ep
== ' ' || *ep
== '\t')
1327 mandoc_msg(MANDOCERR_SPACE_EOL
,
1328 ln
, (int)(ep
- buf
->buf
), NULL
);
1330 /* Save comments preceding the title macro in the syntax tree. */
1332 if (r
->options
& MPARSE_COMMENT
) {
1333 while (*ep
== ' ' || *ep
== '\t')
1336 n
= roff_node_alloc(r
->man
, ln
, stesc
+ 1 - buf
->buf
,
1337 ROFFT_COMMENT
, TOKEN_NONE
);
1338 n
->string
= mandoc_strdup(stesc
+ 2);
1339 roff_node_append(r
->man
, n
);
1340 n
->flags
|= NODE_VALID
| NODE_ENDED
;
1341 r
->man
->next
= ROFF_NEXT_SIBLING
;
1344 /* The comment requests line continuation. */
1346 if (stesc
[1] == '#') {
1348 return ROFF_IGN
| ROFF_APPEND
;
1351 /* Discard the comment including preceding whitespace. */
1353 while (stesc
> start
&& stesc
[-1] == ' ' &&
1354 (stesc
== start
+ 1 || stesc
[-2] != '\\'))
1361 * In the current line, expand escape sequences that produce parsable
1362 * input text. Also check the syntax of the remaining escape sequences,
1363 * which typically produce output glyphs or change formatter state.
1366 roff_expand(struct roff
*r
, struct buf
*buf
, int ln
, int pos
, char ec
)
1368 char ubuf
[24]; /* buffer to print a number */
1369 struct mctx
*ctx
; /* current macro call context */
1370 const char *res
; /* the string to be pasted */
1371 const char *src
; /* source for copying */
1372 char *dst
; /* destination for copying */
1373 int iesc
; /* index of leading escape char */
1374 int inam
; /* index of the escape name */
1375 int iarg
; /* index beginning the argument */
1376 int iendarg
; /* index right after the argument */
1377 int iend
; /* index right after the sequence */
1378 int isrc
, idst
; /* to reduce \\ and \. in names */
1379 int deftype
; /* type of definition to paste */
1380 int argi
; /* macro argument index */
1381 int quote_args
; /* true for \\$@, false for \\$* */
1382 int asz
; /* length of the replacement */
1383 int rsz
; /* length of the rest of the string */
1384 int npos
; /* position in numeric expression */
1385 int expand_count
; /* to avoid infinite loops */
1388 while (buf
->buf
[pos
] != '\0') {
1391 * Skip plain ASCII characters.
1392 * If we have a non-standard escape character,
1393 * escape literal backslashes because all processing in
1394 * subsequent functions uses the standard escaping rules.
1397 if (buf
->buf
[pos
] != ec
) {
1398 if (ec
!= ASCII_ESC
&& buf
->buf
[pos
] == '\\') {
1399 roff_expand_patch(buf
, pos
, "\\e", pos
+ 1);
1407 * Parse escape sequences,
1408 * issue diagnostic messages when appropriate,
1409 * and skip sequences that do not need expansion.
1410 * If we have a non-standard escape character, translate
1411 * it to backslashes and translate backslashes to \e.
1414 if (roff_escape(buf
->buf
, ln
, pos
, &iesc
, &inam
,
1415 &iarg
, &iendarg
, &iend
) != ESCAPE_EXPAND
) {
1416 while (pos
< iend
) {
1417 if (buf
->buf
[pos
] == ec
) {
1418 buf
->buf
[pos
] = '\\';
1421 } else if (buf
->buf
[pos
] == '\\') {
1422 roff_expand_patch(buf
,
1423 pos
, "\\e", pos
+ 1);
1432 /* Reduce \\ and \. in names. */
1434 if (buf
->buf
[inam
] == '*' || buf
->buf
[inam
] == 'n') {
1436 while (isrc
< iendarg
) {
1437 if (isrc
+ 1 < iendarg
&&
1438 buf
->buf
[isrc
] == '\\' &&
1439 (buf
->buf
[isrc
+ 1] == '\\' ||
1440 buf
->buf
[isrc
+ 1] == '.'))
1442 buf
->buf
[idst
++] = buf
->buf
[isrc
++];
1444 iendarg
-= isrc
- idst
;
1447 /* Handle expansion. */
1450 switch (buf
->buf
[inam
]) {
1452 if (iendarg
== iarg
)
1454 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1455 if ((res
= roff_getstrn(r
, buf
->buf
+ iarg
,
1456 iendarg
- iarg
, &deftype
)) != NULL
)
1461 * let \*(.T through to the formatters.
1464 if (iendarg
- iarg
== 2 &&
1465 buf
->buf
[iarg
] == '.' &&
1466 buf
->buf
[iarg
+ 1] == 'T') {
1467 roff_setstrn(&r
->strtab
, ".T", 2, NULL
, 0, 0);
1472 mandoc_msg(MANDOCERR_STR_UNDEF
, ln
, iesc
,
1473 "%.*s", iendarg
- iarg
, buf
->buf
+ iarg
);
1477 if (r
->mstackpos
< 0) {
1478 mandoc_msg(MANDOCERR_ARG_UNDEF
, ln
, iesc
,
1479 "%.*s", iend
- iesc
, buf
->buf
+ iesc
);
1482 ctx
= r
->mstack
+ r
->mstackpos
;
1483 argi
= buf
->buf
[iarg
] - '1';
1484 if (argi
>= 0 && argi
<= 8) {
1485 if (argi
< ctx
->argc
)
1486 res
= ctx
->argv
[argi
];
1489 if (buf
->buf
[iarg
] == '*')
1491 else if (buf
->buf
[iarg
] == '@')
1494 mandoc_msg(MANDOCERR_ARG_NONUM
, ln
, iesc
,
1495 "%.*s", iend
- iesc
, buf
->buf
+ iesc
);
1499 for (argi
= 0; argi
< ctx
->argc
; argi
++) {
1503 asz
+= 2; /* quotes */
1504 asz
+= strlen(ctx
->argv
[argi
]);
1506 if (asz
!= iend
- iesc
) {
1507 rsz
= buf
->sz
- iend
;
1508 if (asz
< iend
- iesc
)
1509 memmove(buf
->buf
+ iesc
+ asz
,
1510 buf
->buf
+ iend
, rsz
);
1511 buf
->sz
= iesc
+ asz
+ rsz
;
1512 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1513 if (asz
> iend
- iesc
)
1514 memmove(buf
->buf
+ iesc
+ asz
,
1515 buf
->buf
+ iend
, rsz
);
1517 dst
= buf
->buf
+ iesc
;
1518 for (argi
= 0; argi
< ctx
->argc
; argi
++) {
1523 src
= ctx
->argv
[argi
];
1524 while (*src
!= '\0')
1531 ubuf
[0] = iendarg
> iarg
? '1' : '0';
1537 ubuf
[0] = iendarg
> iarg
&& iend
> iendarg
&&
1538 roff_evalnum(r
, ln
, buf
->buf
+ iarg
, &npos
,
1539 NULL
, ROFFNUM_SCALE
) &&
1540 npos
== iendarg
- iarg
? '1' : '0';
1545 mandoc_msg(MANDOCERR_UNSUPP
, ln
, iesc
,
1546 "%.*s", iend
- iesc
, buf
->buf
+ iesc
);
1547 roff_expand_patch(buf
, iendarg
, "}", iend
);
1548 roff_expand_patch(buf
, iesc
, "${", iarg
);
1554 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1555 roff_getregn(r
, buf
->buf
+ iarg
,
1556 iendarg
- iarg
, buf
->buf
[inam
+ 1]));
1562 (void)snprintf(ubuf
, sizeof(ubuf
),
1563 "%d", (iendarg
- iarg
) * 24);
1571 if (++expand_count
> EXPAND_LIMIT
||
1572 buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1573 mandoc_msg(MANDOCERR_ROFFLOOP
, ln
, iesc
, NULL
);
1576 roff_expand_patch(buf
, iesc
, res
, iend
);
1582 * Replace the substring from the start position (inclusive)
1583 * to end position (exclusive) with the repl(acement) string.
1586 roff_expand_patch(struct buf
*buf
, int start
, const char *repl
, int end
)
1590 buf
->sz
= mandoc_asprintf(&nbuf
, "%.*s%s%s", start
, buf
->buf
,
1591 repl
, buf
->buf
+ end
) + 1;
1597 * Parse a quoted or unquoted roff-style request or macro argument.
1598 * Return a pointer to the parsed argument, which is either the original
1599 * pointer or advanced by one byte in case the argument is quoted.
1600 * NUL-terminate the argument in place.
1601 * Collapse pairs of quotes inside quoted arguments.
1602 * Advance the argument pointer to the next argument,
1603 * or to the NUL byte terminating the argument line.
1606 roff_getarg(struct roff
*r
, char **cpp
, int ln
, int *pos
)
1610 int newesc
, pairs
, quoted
, white
;
1612 /* Quoting can only start with a new word. */
1615 if ('"' == *start
) {
1620 newesc
= pairs
= white
= 0;
1621 for (cp
= start
; '\0' != *cp
; cp
++) {
1624 * Move the following text left
1625 * after quoted quotes and after "\\" and "\t".
1630 if ('\\' == cp
[0]) {
1632 * In copy mode, translate double to single
1633 * backslashes and backslash-t to literal tabs.
1644 cp
[-pairs
] = ASCII_ESC
;
1649 /* Skip escaped blanks. */
1656 } else if (0 == quoted
) {
1658 /* Unescaped blanks end unquoted args. */
1662 } else if ('"' == cp
[0]) {
1664 /* Quoted quotes collapse. */
1668 /* Unquoted quotes end quoted args. */
1675 /* Quoted argument without a closing quote. */
1677 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
1679 /* NUL-terminate this argument and move to the next one. */
1687 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
1690 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
1691 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
1693 start
= mandoc_strdup(start
);
1698 buf
.sz
= strlen(start
) + 1;
1700 if (roff_expand(r
, &buf
, ln
, 0, ASCII_ESC
) & ROFF_IGN
) {
1702 buf
.buf
= mandoc_strdup("");
1709 * Process text streams.
1712 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1718 enum mandoc_esc esc
;
1720 /* Spring the input line trap. */
1722 if (roffit_lines
== 1) {
1723 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1730 return ROFF_REPARSE
;
1731 } else if (roffit_lines
> 1)
1734 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1735 if (roffce_lines
< 1) {
1736 r
->man
->last
= roffce_node
;
1737 r
->man
->next
= ROFF_NEXT_SIBLING
;
1744 /* Convert all breakable hyphens into ASCII_HYPH. */
1746 start
= p
= buf
->buf
+ pos
;
1748 while (*p
!= '\0') {
1749 sz
= strcspn(p
, "-\\");
1756 /* Skip over escapes. */
1758 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1759 if (esc
== ESCAPE_ERROR
)
1764 } else if (p
== start
) {
1769 if (isalpha((unsigned char)p
[-1]) &&
1770 isalpha((unsigned char)p
[1]))
1778 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
, size_t len
)
1782 int pos
; /* parse point */
1783 int spos
; /* saved parse point for messages */
1784 int ppos
; /* original offset in buf->buf */
1785 int ctl
; /* macro line (boolean) */
1789 if (len
> 80 && r
->tbl
== NULL
&& r
->eqn
== NULL
&&
1790 (r
->man
->flags
& ROFF_NOFILL
) == 0 &&
1791 strchr(" .\\", buf
->buf
[pos
]) == NULL
&&
1792 buf
->buf
[pos
] != r
->control
&&
1793 strcspn(buf
->buf
, " ") < 80)
1794 mandoc_msg(MANDOCERR_TEXT_LONG
, ln
, (int)len
- 1,
1795 "%.20s...", buf
->buf
+ pos
);
1797 /* Handle in-line equation delimiters. */
1799 if (r
->tbl
== NULL
&&
1800 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1801 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1802 e
= roff_eqndelim(r
, buf
, pos
);
1803 if (e
== ROFF_REPARSE
)
1805 assert(e
== ROFF_CONT
);
1808 /* Handle comments and escape sequences. */
1810 e
= roff_parse_comment(r
, buf
, ln
, pos
, r
->escape
);
1811 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1813 assert(e
== ROFF_CONT
);
1815 e
= roff_expand(r
, buf
, ln
, pos
, r
->escape
);
1816 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1818 assert(e
== ROFF_CONT
);
1820 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1823 * First, if a scope is open and we're not a macro, pass the
1824 * text through the macro's filter.
1825 * Equations process all content themselves.
1826 * Tables process almost all content themselves, but we want
1827 * to warn about macros before passing it there.
1830 if (r
->last
!= NULL
&& ! ctl
) {
1832 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1833 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1838 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1839 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1842 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1843 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1844 roff_addtbl(r
->man
, ln
, r
->tbl
);
1848 r
->options
&= ~MPARSE_COMMENT
;
1849 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1852 /* Skip empty request lines. */
1854 if (buf
->buf
[pos
] == '"') {
1855 mandoc_msg(MANDOCERR_COMMENT_BAD
, ln
, pos
, NULL
);
1857 } else if (buf
->buf
[pos
] == '\0')
1861 * If a scope is open, go to the child handler for that macro,
1862 * as it may want to preprocess before doing anything with it.
1867 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1870 r
->options
&= ~MPARSE_COMMENT
;
1872 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1873 return roff_req_or_macro(r
, t
, buf
, ln
, spos
, pos
, offs
);
1877 * Handle a new request or macro.
1878 * May be called outside any scope or from inside a conditional scope.
1881 roff_req_or_macro(ROFF_ARGS
) {
1883 /* For now, tables ignore most macros and some request. */
1885 if (r
->tbl
!= NULL
&& (tok
== TOKEN_NONE
|| tok
== ROFF_TS
||
1886 tok
== ROFF_br
|| tok
== ROFF_ce
|| tok
== ROFF_rj
||
1888 mandoc_msg(MANDOCERR_TBLMACRO
,
1889 ln
, ppos
, "%s", buf
->buf
+ ppos
);
1890 if (tok
!= TOKEN_NONE
)
1892 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1894 while (buf
->buf
[pos
] == ' ')
1896 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1897 roff_addtbl(r
->man
, ln
, r
->tbl
);
1901 /* For now, let high level macros abort .ce mode. */
1903 if (roffce_node
!= NULL
&&
1904 (tok
== TOKEN_NONE
|| tok
== ROFF_Dd
|| tok
== ROFF_EQ
||
1905 tok
== ROFF_TH
|| tok
== ROFF_TS
)) {
1906 r
->man
->last
= roffce_node
;
1907 r
->man
->next
= ROFF_NEXT_SIBLING
;
1913 * This is neither a roff request nor a user-defined macro.
1914 * Let the standard macro set parsers handle it.
1917 if (tok
== TOKEN_NONE
)
1920 /* Execute a roff request or a user-defined macro. */
1922 return (*roffs
[tok
].proc
)(r
, tok
, buf
, ln
, ppos
, pos
, offs
);
1926 * Internal interface function to tell the roff parser that execution
1927 * of the current macro ended. This is required because macro
1928 * definitions usually do not end with a .return request.
1931 roff_userret(struct roff
*r
)
1936 assert(r
->mstackpos
>= 0);
1937 ctx
= r
->mstack
+ r
->mstackpos
;
1938 for (i
= 0; i
< ctx
->argc
; i
++)
1945 roff_endparse(struct roff
*r
)
1947 if (r
->last
!= NULL
)
1948 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->last
->line
,
1949 r
->last
->col
, "%s", roff_name
[r
->last
->tok
]);
1951 if (r
->eqn
!= NULL
) {
1952 mandoc_msg(MANDOCERR_BLK_NOEND
,
1953 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1958 if (r
->tbl
!= NULL
) {
1965 * Parse the request or macro name at buf[*pos].
1966 * Return ROFF_RENAMED, ROFF_USERDEF, or a ROFF_* token value.
1967 * For empty, undefined, mdoc(7), and man(7) macros, return TOKEN_NONE.
1968 * As a side effect, set r->current_string to the definition or to NULL.
1970 static enum roff_tok
1971 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1981 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1985 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1987 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1988 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1997 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
2000 if (t
!= TOKEN_NONE
)
2002 else if (deftype
== ROFFDEF_UNDEF
) {
2003 /* Using an undefined macro defines it to be empty. */
2004 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
2005 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
2010 /* --- handling of request blocks ----------------------------------------- */
2013 * Close a macro definition block or an "ignore" block.
2016 roff_cblock(ROFF_ARGS
)
2020 if (r
->last
== NULL
) {
2021 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2025 switch (r
->last
->tok
) {
2034 /* Remapped in roff_block(). */
2037 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2042 roffnode_cleanscope(r
);
2045 * If a conditional block with braces is still open,
2046 * check for "\}" block end markers.
2049 if (r
->last
!= NULL
&& r
->last
->endspan
< 0) {
2050 rr
= 1; /* If arguments follow "\}", warn about them. */
2051 roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2054 if (buf
->buf
[pos
] != '\0')
2055 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
2056 ".. %s", buf
->buf
+ pos
);
2062 * Pop all nodes ending at the end of the current input line.
2063 * Return the number of loops ended.
2066 roffnode_cleanscope(struct roff
*r
)
2071 while (r
->last
!= NULL
&& r
->last
->endspan
> 0) {
2072 if (--r
->last
->endspan
!= 0)
2074 inloop
+= roffnode_pop(r
);
2080 * Handle the closing "\}" of a conditional block.
2081 * Apart from generating warnings, this only pops nodes.
2082 * Return the number of loops ended.
2085 roff_ccond(struct roff
*r
, int ln
, int ppos
)
2087 if (NULL
== r
->last
) {
2088 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2092 switch (r
->last
->tok
) {
2099 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2103 if (r
->last
->endspan
> -1) {
2104 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2108 return roffnode_pop(r
) + roffnode_cleanscope(r
);
2112 roff_block(ROFF_ARGS
)
2114 const char *name
, *value
;
2115 char *call
, *cp
, *iname
, *rname
;
2116 size_t csz
, namesz
, rsz
;
2119 /* Ignore groff compatibility mode for now. */
2121 if (tok
== ROFF_de1
)
2123 else if (tok
== ROFF_dei1
)
2125 else if (tok
== ROFF_am1
)
2127 else if (tok
== ROFF_ami1
)
2130 /* Parse the macro name argument. */
2132 cp
= buf
->buf
+ pos
;
2133 if (tok
== ROFF_ig
) {
2138 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2139 iname
[namesz
] = '\0';
2142 /* Resolve the macro name argument if it is indirect. */
2144 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2145 deftype
= ROFFDEF_USER
;
2146 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2148 mandoc_msg(MANDOCERR_STR_UNDEF
,
2149 ln
, (int)(iname
- buf
->buf
),
2150 "%.*s", (int)namesz
, iname
);
2153 namesz
= strlen(name
);
2157 if (namesz
== 0 && tok
!= ROFF_ig
) {
2158 mandoc_msg(MANDOCERR_REQ_EMPTY
,
2159 ln
, ppos
, "%s", roff_name
[tok
]);
2163 roffnode_push(r
, tok
, name
, ln
, ppos
);
2166 * At the beginning of a `de' macro, clear the existing string
2167 * with the same name, if there is one. New content will be
2168 * appended from roff_block_text() in multiline mode.
2171 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
2172 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
2173 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2174 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
2175 deftype
= ROFFDEF_ANY
;
2176 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2177 switch (deftype
) { /* Before appending, ... */
2178 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
2179 roff_setstrn(&r
->strtab
, name
, namesz
,
2180 value
, strlen(value
), 0);
2182 case ROFFDEF_REN
: /* call original standard macro. */
2183 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2184 (int)strlen(value
), value
);
2185 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2186 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2189 case ROFFDEF_STD
: /* rename and call standard macro. */
2190 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
2191 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
2192 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2194 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2206 /* Get the custom end marker. */
2209 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2211 /* Resolve the end marker if it is indirect. */
2213 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2214 deftype
= ROFFDEF_USER
;
2215 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2217 mandoc_msg(MANDOCERR_STR_UNDEF
,
2218 ln
, (int)(iname
- buf
->buf
),
2219 "%.*s", (int)namesz
, iname
);
2222 namesz
= strlen(name
);
2227 r
->last
->end
= mandoc_strndup(name
, namesz
);
2230 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2231 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2237 roff_block_sub(ROFF_ARGS
)
2243 * If a custom end marker is a user-defined or predefined macro
2244 * or a request, interpret it.
2248 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2249 if (buf
->buf
[i
] != r
->last
->end
[j
])
2252 if (r
->last
->end
[j
] == '\0' &&
2253 (buf
->buf
[i
] == '\0' ||
2254 buf
->buf
[i
] == ' ' ||
2255 buf
->buf
[i
] == '\t')) {
2257 roffnode_cleanscope(r
);
2259 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2263 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2270 /* Handle the standard end marker. */
2272 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2273 if (t
== ROFF_cblock
)
2274 return roff_cblock(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2276 /* Not an end marker, so append the line to the block. */
2279 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2284 roff_block_text(ROFF_ARGS
)
2288 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2294 * Check for a closing "\}" and handle it.
2295 * In this function, the final "int *offs" argument is used for
2296 * different purposes than elsewhere:
2297 * Input: *offs == 0: caller wants to discard arguments following \}
2298 * *offs == 1: caller wants to preserve text following \}
2299 * Output: *offs = 0: tell caller to discard input line
2300 * *offs = 1: tell caller to use input line
2303 roff_cond_checkend(ROFF_ARGS
)
2306 int endloop
, irc
, rr
;
2310 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2311 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2312 if (roffnode_cleanscope(r
))
2316 * If "\}" occurs on a macro line without a preceding macro or
2317 * a text line contains nothing else, drop the line completely.
2320 ep
= buf
->buf
+ pos
;
2321 if (ep
[0] == '\\' && ep
[1] == '}' && (ep
[2] == '\0' || *offs
== 0))
2325 * The closing delimiter "\}" rewinds the conditional scope
2326 * but is otherwise ignored when interpreting the line.
2329 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2337 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2338 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2354 * Parse and process a request or macro line in conditional scope.
2357 roff_cond_sub(ROFF_ARGS
)
2359 struct roffnode
*bl
;
2363 rr
= 0; /* If arguments follow "\}", skip them. */
2364 irc
= roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2366 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2369 * Handle requests and macros if the conditional evaluated
2370 * to true or if they are structurally required.
2371 * The .break request is always handled specially.
2374 if (t
== ROFF_break
) {
2375 if (irc
& ROFF_LOOPMASK
)
2376 irc
= ROFF_IGN
| ROFF_LOOPEXIT
;
2378 for (bl
= r
->last
; bl
!= NULL
; bl
= bl
->parent
) {
2380 if (bl
->tok
== ROFF_while
)
2384 } else if (rr
|| (t
< TOKEN_NONE
&& roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
2385 irc
|= roff_req_or_macro(r
, t
, buf
, ln
, spos
, pos
, offs
);
2386 if (irc
& ROFF_WHILE
)
2387 irc
&= ~(ROFF_LOOPCONT
| ROFF_LOOPEXIT
);
2393 * Parse and process a text line in conditional scope.
2396 roff_cond_text(ROFF_ARGS
)
2400 rr
= 1; /* If arguments follow "\}", preserve them. */
2401 irc
= roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2407 /* --- handling of numeric and conditional expressions -------------------- */
2410 * Parse a single signed integer number. Stop at the first non-digit.
2411 * If there is at least one digit, return success and advance the
2412 * parse point, else return failure and let the parse point unchanged.
2413 * Ignore overflows, treat them just like the C language.
2416 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2418 int myres
, scaled
, n
, p
;
2425 if (n
|| v
[p
] == '+')
2428 if (flags
& ROFFNUM_WHITE
)
2429 while (isspace((unsigned char)v
[p
]))
2432 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2433 *res
= 10 * *res
+ v
[p
] - '0';
2440 /* Each number may be followed by one optional scaling unit. */
2444 scaled
= *res
* 65536;
2447 scaled
= *res
* 240;
2450 scaled
= *res
* 240 / 2.54;
2461 scaled
= *res
* 10 / 3;
2467 scaled
= *res
* 6 / 25;
2474 if (flags
& ROFFNUM_SCALE
)
2482 * Evaluate a string comparison condition.
2483 * The first character is the delimiter.
2484 * Succeed if the string up to its second occurrence
2485 * matches the string up to its third occurence.
2486 * Advance the cursor after the third occurrence
2487 * or lacking that, to the end of the line.
2490 roff_evalstrcond(const char *v
, int *pos
)
2492 const char *s1
, *s2
, *s3
;
2496 s1
= v
+ *pos
; /* initial delimiter */
2497 s2
= s1
+ 1; /* for scanning the first string */
2498 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2500 if (NULL
== s3
) /* found no middle delimiter */
2503 while ('\0' != *++s3
) {
2504 if (*s2
!= *s3
) { /* mismatch */
2505 s3
= strchr(s3
, *s1
);
2508 if (*s3
== *s1
) { /* found the final delimiter */
2517 s3
= strchr(s2
, '\0');
2518 else if (*s3
!= '\0')
2525 * Evaluate an optionally negated single character, numerical,
2526 * or string condition.
2529 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2531 const char *start
, *end
;
2534 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2536 if ('!' == v
[*pos
]) {
2557 } while (v
[*pos
] == ' ');
2560 * Quirk for groff compatibility:
2561 * The horizontal tab is neither available nor unavailable.
2564 if (v
[*pos
] == '\t') {
2569 /* Printable ASCII characters are available. */
2571 if (v
[*pos
] != '\\') {
2577 switch (mandoc_escape(&end
, &start
, &len
)) {
2578 case ESCAPE_SPECIAL
:
2579 istrue
= mchars_spec2cp(start
, len
) != -1;
2581 case ESCAPE_UNICODE
:
2584 case ESCAPE_NUMBERED
:
2585 istrue
= mchars_num2char(start
, len
) != -1;
2592 return istrue
== wanttrue
;
2599 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2602 else if (v
[*pos
] == 'r')
2603 istrue
= roff_hasregn(r
, name
, sz
);
2605 deftype
= ROFFDEF_ANY
;
2606 roff_getstrn(r
, name
, sz
, &deftype
);
2609 *pos
= (name
+ sz
) - v
;
2610 return istrue
== wanttrue
;
2616 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2617 return (number
> 0) == wanttrue
;
2618 else if (*pos
== savepos
)
2619 return roff_evalstrcond(v
, pos
) == wanttrue
;
2625 roff_line_ignore(ROFF_ARGS
)
2632 roff_insec(ROFF_ARGS
)
2635 mandoc_msg(MANDOCERR_REQ_INSEC
, ln
, ppos
, "%s", roff_name
[tok
]);
2640 roff_unsupp(ROFF_ARGS
)
2643 mandoc_msg(MANDOCERR_REQ_UNSUPP
, ln
, ppos
, "%s", roff_name
[tok
]);
2648 roff_cond(ROFF_ARGS
)
2652 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2655 * An `.el' has no conditional body: it will consume the value
2656 * of the current rstack entry set in prior `ie' calls or
2659 * If we're not an `el', however, then evaluate the conditional.
2662 r
->last
->rule
= tok
== ROFF_el
?
2663 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2664 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2667 * An if-else will put the NEGATION of the current evaluated
2668 * conditional into the stack of rules.
2671 if (tok
== ROFF_ie
) {
2672 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2674 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2675 r
->rstacksz
, sizeof(int));
2677 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2680 /* If the parent has false as its rule, then so do we. */
2682 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2687 * If there is nothing on the line after the conditional,
2688 * not even whitespace, use next-line scope.
2689 * Except that .while does not support next-line scope.
2692 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2693 r
->last
->endspan
= 2;
2697 while (buf
->buf
[pos
] == ' ')
2700 /* An opening brace requests multiline scope. */
2702 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2703 r
->last
->endspan
= -1;
2705 while (buf
->buf
[pos
] == ' ')
2711 * Anything else following the conditional causes
2712 * single-line scope. Warn if the scope contains
2713 * nothing but trailing whitespace.
2716 if (buf
->buf
[pos
] == '\0')
2717 mandoc_msg(MANDOCERR_COND_EMPTY
,
2718 ln
, ppos
, "%s", roff_name
[tok
]);
2720 r
->last
->endspan
= 1;
2725 if (tok
== ROFF_while
)
2737 /* Ignore groff compatibility mode for now. */
2739 if (tok
== ROFF_ds1
)
2741 else if (tok
== ROFF_as1
)
2745 * The first word is the name of the string.
2746 * If it is empty or terminated by an escape sequence,
2747 * abort the `ds' request without defining anything.
2750 name
= string
= buf
->buf
+ pos
;
2754 namesz
= roff_getname(r
, &string
, ln
, pos
);
2755 switch (name
[namesz
]) {
2759 string
= buf
->buf
+ pos
+ namesz
;
2765 /* Read past the initial double-quote, if any. */
2769 /* The rest is the value. */
2770 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2772 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2777 * Parse a single operator, one or two characters long.
2778 * If the operator is recognized, return success and advance the
2779 * parse point, else return failure and let the parse point unchanged.
2782 roff_getop(const char *v
, int *pos
, char *res
)
2797 switch (v
[*pos
+ 1]) {
2815 switch (v
[*pos
+ 1]) {
2829 if ('=' == v
[*pos
+ 1])
2841 * Evaluate either a parenthesized numeric expression
2842 * or a single signed integer number.
2845 roff_evalpar(struct roff
*r
, int ln
,
2846 const char *v
, int *pos
, int *res
, int flags
)
2850 return roff_getnum(v
, pos
, res
, flags
);
2853 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2857 * Omission of the closing parenthesis
2858 * is an error in validation mode,
2859 * but ignored in evaluation mode.
2864 else if (NULL
== res
)
2871 * Evaluate a complete numeric expression.
2872 * Proceed left to right, there is no concept of precedence.
2875 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2876 int *pos
, int *res
, int flags
)
2878 int mypos
, operand2
;
2886 if (flags
& ROFFNUM_WHITE
)
2887 while (isspace((unsigned char)v
[*pos
]))
2890 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2894 if (flags
& ROFFNUM_WHITE
)
2895 while (isspace((unsigned char)v
[*pos
]))
2898 if ( ! roff_getop(v
, pos
, &operator))
2901 if (flags
& ROFFNUM_WHITE
)
2902 while (isspace((unsigned char)v
[*pos
]))
2905 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2908 if (flags
& ROFFNUM_WHITE
)
2909 while (isspace((unsigned char)v
[*pos
]))
2926 if (operand2
== 0) {
2927 mandoc_msg(MANDOCERR_DIVZERO
,
2935 if (operand2
== 0) {
2936 mandoc_msg(MANDOCERR_DIVZERO
,
2944 *res
= *res
< operand2
;
2947 *res
= *res
> operand2
;
2950 *res
= *res
<= operand2
;
2953 *res
= *res
>= operand2
;
2956 *res
= *res
== operand2
;
2959 *res
= *res
!= operand2
;
2962 *res
= *res
&& operand2
;
2965 *res
= *res
|| operand2
;
2968 if (operand2
< *res
)
2972 if (operand2
> *res
)
2982 /* --- register management ------------------------------------------------ */
2985 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2987 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2991 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2992 int val
, char sign
, int step
)
2994 struct roffreg
*reg
;
2996 /* Search for an existing register with the same name. */
2999 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
3000 strncmp(reg
->key
.p
, name
, len
) != 0))
3004 /* Create a new register. */
3005 reg
= mandoc_malloc(sizeof(struct roffreg
));
3006 reg
->key
.p
= mandoc_strndup(name
, len
);
3010 reg
->next
= r
->regtab
;
3016 else if ('-' == sign
)
3020 if (step
!= INT_MIN
)
3025 * Handle some predefined read-only number registers.
3026 * For now, return -1 if the requested register is not predefined;
3027 * in case a predefined read-only register having the value -1
3028 * were to turn up, another special value would have to be chosen.
3031 roff_getregro(const struct roff
*r
, const char *name
)
3035 case '$': /* Number of arguments of the last macro evaluated. */
3036 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
3037 case 'A': /* ASCII approximation mode is always off. */
3039 case 'g': /* Groff compatibility mode is always on. */
3041 case 'H': /* Fixed horizontal resolution. */
3043 case 'j': /* Always adjust left margin only. */
3045 case 'T': /* Some output device is always defined. */
3047 case 'V': /* Fixed vertical resolution. */
3055 roff_getreg(struct roff
*r
, const char *name
)
3057 return roff_getregn(r
, name
, strlen(name
), '\0');
3061 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
3063 struct roffreg
*reg
;
3066 if ('.' == name
[0] && 2 == len
) {
3067 val
= roff_getregro(r
, name
+ 1);
3072 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
3073 if (len
== reg
->key
.sz
&&
3074 0 == strncmp(name
, reg
->key
.p
, len
)) {
3077 reg
->val
+= reg
->step
;
3080 reg
->val
-= reg
->step
;
3089 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
3094 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
3096 struct roffreg
*reg
;
3099 if ('.' == name
[0] && 2 == len
) {
3100 val
= roff_getregro(r
, name
+ 1);
3105 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
3106 if (len
== reg
->key
.sz
&&
3107 0 == strncmp(name
, reg
->key
.p
, len
))
3114 roff_freereg(struct roffreg
*reg
)
3116 struct roffreg
*old_reg
;
3118 while (NULL
!= reg
) {
3129 char *key
, *val
, *step
;
3134 key
= val
= buf
->buf
+ pos
;
3138 keysz
= roff_getname(r
, &val
, ln
, pos
);
3139 if (key
[keysz
] == '\\' || key
[keysz
] == '\t')
3143 if (sign
== '+' || sign
== '-')
3147 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
3151 while (isspace((unsigned char)*step
))
3153 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
3156 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
3163 struct roffreg
*reg
, **prev
;
3167 name
= cp
= buf
->buf
+ pos
;
3170 namesz
= roff_getname(r
, &cp
, ln
, pos
);
3171 name
[namesz
] = '\0';
3176 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
3188 /* --- handler functions for roff requests -------------------------------- */
3197 cp
= buf
->buf
+ pos
;
3198 while (*cp
!= '\0') {
3200 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
3201 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
3202 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3203 if (name
[namesz
] == '\\' || name
[namesz
] == '\t')
3214 /* Parse the number of lines. */
3216 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
3217 mandoc_msg(MANDOCERR_IT_NONUM
,
3218 ln
, ppos
, "%s", buf
->buf
+ 1);
3222 while (isspace((unsigned char)buf
->buf
[pos
]))
3226 * Arm the input line trap.
3227 * Special-casing "an-trap" is an ugly workaround to cope
3228 * with DocBook stupidly fiddling with man(7) internals.
3232 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3233 strcmp(buf
->buf
+ pos
, "an-trap") ?
3234 buf
->buf
+ pos
: "br");
3242 enum roff_tok t
, te
;
3249 r
->format
= MPARSE_MDOC
;
3250 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3256 r
->format
= MPARSE_MAN
;
3257 mask
= MPARSE_QUICK
;
3262 if ((r
->options
& mask
) == 0)
3263 for (t
= tok
; t
< te
; t
++)
3264 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3271 r
->man
->flags
&= ~ROFF_NONOFILL
;
3272 if (r
->tbl
== NULL
) {
3273 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "TE");
3276 if (tbl_end(r
->tbl
, 0) == 0) {
3279 buf
->buf
= mandoc_strdup(".sp");
3282 return ROFF_REPARSE
;
3293 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "T&");
3295 tbl_restart(ln
, ppos
, r
->tbl
);
3301 * Handle in-line equation delimiters.
3304 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3307 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3310 * Outside equations, look for an opening delimiter.
3311 * If we are inside an equation, we already know it is
3312 * in-line, or this function wouldn't have been called;
3313 * so look for a closing delimiter.
3316 cp1
= buf
->buf
+ pos
;
3317 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3318 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3323 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3325 /* Handle preceding text, protecting whitespace. */
3327 if (*buf
->buf
!= '\0') {
3334 * Prepare replacing the delimiter with an equation macro
3335 * and drop leading white space from the equation.
3338 if (r
->eqn
== NULL
) {
3345 /* Handle following text, protecting whitespace. */
3353 /* Do the actual replacement. */
3355 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3356 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3360 /* Toggle the in-line state of the eqn subsystem. */
3362 r
->eqn_inline
= r
->eqn
== NULL
;
3363 return ROFF_REPARSE
;
3369 struct roff_node
*n
;
3371 if (r
->man
->meta
.macroset
== MACROSET_MAN
)
3372 man_breakscope(r
->man
, ROFF_EQ
);
3373 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3374 if (ln
> r
->man
->last
->line
)
3375 n
->flags
|= NODE_LINE
;
3376 n
->eqn
= eqn_box_new();
3377 roff_node_append(r
->man
, n
);
3378 r
->man
->next
= ROFF_NEXT_SIBLING
;
3380 assert(r
->eqn
== NULL
);
3381 if (r
->last_eqn
== NULL
)
3382 r
->last_eqn
= eqn_alloc();
3384 eqn_reset(r
->last_eqn
);
3385 r
->eqn
= r
->last_eqn
;
3388 if (buf
->buf
[pos
] != '\0')
3389 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3390 ".EQ %s", buf
->buf
+ pos
);
3398 if (r
->eqn
!= NULL
) {
3402 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "EN");
3403 if (buf
->buf
[pos
] != '\0')
3404 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3405 "EN %s", buf
->buf
+ pos
);
3412 if (r
->tbl
!= NULL
) {
3413 mandoc_msg(MANDOCERR_BLK_BROKEN
, ln
, ppos
, "TS breaks TS");
3416 r
->man
->flags
|= ROFF_NONOFILL
;
3417 r
->tbl
= tbl_alloc(ppos
, ln
, r
->last_tbl
);
3418 if (r
->last_tbl
== NULL
)
3419 r
->first_tbl
= r
->tbl
;
3420 r
->last_tbl
= r
->tbl
;
3425 roff_noarg(ROFF_ARGS
)
3427 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3428 man_breakscope(r
->man
, tok
);
3429 if (tok
== ROFF_brp
)
3431 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3432 if (buf
->buf
[pos
] != '\0')
3433 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3434 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3436 r
->man
->flags
|= ROFF_NOFILL
;
3437 else if (tok
== ROFF_fi
)
3438 r
->man
->flags
&= ~ROFF_NOFILL
;
3439 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3440 r
->man
->next
= ROFF_NEXT_SIBLING
;
3445 roff_onearg(ROFF_ARGS
)
3447 struct roff_node
*n
;
3451 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3452 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3454 man_breakscope(r
->man
, tok
);
3456 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3457 r
->man
->last
= roffce_node
;
3458 r
->man
->next
= ROFF_NEXT_SIBLING
;
3461 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3464 cp
= buf
->buf
+ pos
;
3466 while (*cp
!= '\0' && *cp
!= ' ')
3471 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3472 ln
, (int)(cp
- buf
->buf
),
3473 "%s ... %s", roff_name
[tok
], cp
);
3474 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3477 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3478 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3479 roff_word_alloc(r
->man
, ln
, pos
, "1");
3480 r
->man
->last
->flags
|= NODE_NOSRC
;
3483 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3484 &roffce_lines
, 0) == 0) {
3485 mandoc_msg(MANDOCERR_CE_NONUM
,
3486 ln
, pos
, "ce %s", buf
->buf
+ pos
);
3489 if (roffce_lines
< 1) {
3490 r
->man
->last
= r
->man
->last
->parent
;
3494 roffce_node
= r
->man
->last
->parent
;
3496 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3499 n
->flags
|= NODE_LINE
;
3500 r
->man
->next
= ROFF_NEXT_SIBLING
;
3505 roff_manyarg(ROFF_ARGS
)
3507 struct roff_node
*n
;
3510 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3513 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3514 while (*ep
!= '\0' && *ep
!= ' ')
3518 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3521 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3523 r
->man
->next
= ROFF_NEXT_SIBLING
;
3530 char *oldn
, *newn
, *end
, *value
;
3531 size_t oldsz
, newsz
, valsz
;
3533 newn
= oldn
= buf
->buf
+ pos
;
3537 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3538 if (newn
[newsz
] == '\\' || newn
[newsz
] == '\t' || *oldn
== '\0')
3542 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3546 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3548 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3549 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3555 * The .break request only makes sense inside conditionals,
3556 * and that case is already handled in roff_cond_sub().
3559 roff_break(ROFF_ARGS
)
3561 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, pos
, "break");
3572 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3576 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3577 ln
, p
- buf
->buf
, "cc ... %s", p
);
3583 roff_char(ROFF_ARGS
)
3585 const char *p
, *kp
, *vp
;
3589 /* Parse the character to be replaced. */
3591 kp
= buf
->buf
+ pos
;
3593 if (*kp
== '\0' || (*kp
== '\\' &&
3594 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3595 (*p
!= ' ' && *p
!= '\0')) {
3596 mandoc_msg(MANDOCERR_CHAR_ARG
, ln
, pos
, "char %s", kp
);
3604 * If the replacement string contains a font escape sequence,
3605 * we have to restore the font at the end.
3611 while (*p
!= '\0') {
3614 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3616 case ESCAPE_FONTROMAN
:
3617 case ESCAPE_FONTITALIC
:
3618 case ESCAPE_FONTBOLD
:
3623 case ESCAPE_FONTPREV
:
3631 mandoc_msg(MANDOCERR_CHAR_FONT
,
3632 ln
, (int)(vp
- buf
->buf
), "%s", vp
);
3635 * Approximate the effect of .char using the .tr tables.
3636 * XXX In groff, .char and .tr interact differently.
3640 if (r
->xtab
== NULL
)
3641 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3642 assert((unsigned int)*kp
< 128);
3643 free(r
->xtab
[(int)*kp
].p
);
3644 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3645 "%s%s", vp
, font
? "\fP" : "");
3647 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3649 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3665 mandoc_msg(MANDOCERR_ARG_EXCESS
, ln
,
3666 (int)(p
- buf
->buf
), "ec ... %s", p
);
3675 if (buf
->buf
[pos
] != '\0')
3676 mandoc_msg(MANDOCERR_ARG_SKIP
,
3677 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3684 struct roff_node
*n
;
3687 /* Parse the first argument. */
3689 cp
= buf
->buf
+ pos
;
3692 if (buf
->buf
[pos
] == '\\') {
3693 switch (mandoc_escape((const char **)&cp
, NULL
, NULL
)) {
3694 case ESCAPE_SPECIAL
:
3695 case ESCAPE_UNICODE
:
3696 case ESCAPE_NUMBERED
:
3700 mandoc_msg(MANDOCERR_MC_ESC
, ln
, pos
,
3701 "mc %s", buf
->buf
+ pos
);
3702 buf
->buf
[pos
] = '\0';
3707 /* Ignore additional arguments. */
3712 mandoc_msg(MANDOCERR_MC_DIST
, ln
, (int)(cp
- buf
->buf
),
3717 /* Create the .mc node. */
3719 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3721 if (buf
->buf
[pos
] != '\0')
3722 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3723 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3725 r
->man
->next
= ROFF_NEXT_SIBLING
;
3732 while (buf
->buf
[pos
] == ' ')
3741 const char *p
, *first
, *second
;
3743 enum mandoc_esc esc
;
3748 mandoc_msg(MANDOCERR_REQ_EMPTY
, ln
, ppos
, "tr");
3752 while (*p
!= '\0') {
3756 if (*first
== '\\') {
3757 esc
= mandoc_escape(&p
, NULL
, NULL
);
3758 if (esc
== ESCAPE_ERROR
) {
3759 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3760 (int)(p
- buf
->buf
), "%s", first
);
3763 fsz
= (size_t)(p
- first
);
3767 if (*second
== '\\') {
3768 esc
= mandoc_escape(&p
, NULL
, NULL
);
3769 if (esc
== ESCAPE_ERROR
) {
3770 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3771 (int)(p
- buf
->buf
), "%s", second
);
3774 ssz
= (size_t)(p
- second
);
3775 } else if (*second
== '\0') {
3776 mandoc_msg(MANDOCERR_TR_ODD
, ln
,
3777 (int)(first
- buf
->buf
), "tr %s", first
);
3783 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3788 if (r
->xtab
== NULL
)
3789 r
->xtab
= mandoc_calloc(128,
3790 sizeof(struct roffstr
));
3792 free(r
->xtab
[(int)*first
].p
);
3793 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3794 r
->xtab
[(int)*first
].sz
= ssz
;
3801 * Implementation of the .return request.
3802 * There is no need to call roff_userret() from here.
3803 * The read module will call that after rewinding the reader stack
3804 * to the place from where the current macro was called.
3807 roff_return(ROFF_ARGS
)
3809 if (r
->mstackpos
>= 0)
3810 return ROFF_IGN
| ROFF_USERRET
;
3812 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "return");
3820 char *oldn
, *newn
, *end
;
3821 size_t oldsz
, newsz
;
3824 oldn
= newn
= buf
->buf
+ pos
;
3828 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3829 if (oldn
[oldsz
] == '\\' || oldn
[oldsz
] == '\t' || *newn
== '\0')
3833 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3837 deftype
= ROFFDEF_ANY
;
3838 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3841 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3842 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3843 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3846 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3847 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3850 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3851 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3852 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3855 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3856 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3859 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3860 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3867 roff_shift(ROFF_ARGS
)
3870 int argpos
, levels
, i
;
3874 if (buf
->buf
[pos
] != '\0' &&
3875 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3876 mandoc_msg(MANDOCERR_CE_NONUM
,
3877 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3880 if (r
->mstackpos
< 0) {
3881 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "shift");
3884 ctx
= r
->mstack
+ r
->mstackpos
;
3885 if (levels
> ctx
->argc
) {
3886 mandoc_msg(MANDOCERR_SHIFT
,
3887 ln
, argpos
, "%d, but max is %d", levels
, ctx
->argc
);
3891 mandoc_msg(MANDOCERR_ARG_NEG
, ln
, argpos
, "shift %d", levels
);
3896 for (i
= 0; i
< levels
; i
++)
3898 ctx
->argc
-= levels
;
3899 for (i
= 0; i
< ctx
->argc
; i
++)
3900 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3909 name
= buf
->buf
+ pos
;
3910 mandoc_msg(MANDOCERR_SO
, ln
, ppos
, "so %s", name
);
3913 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3914 * opening anything that's not in our cwd or anything beneath
3915 * it. Thus, explicitly disallow traversing up the file-system
3916 * or using absolute paths.
3919 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3920 mandoc_msg(MANDOCERR_SO_PATH
, ln
, ppos
, ".so %s", name
);
3921 buf
->sz
= mandoc_asprintf(&cp
,
3922 ".sp\nSee the file %s.\n.sp", name
) + 1;
3926 return ROFF_REPARSE
;
3933 /* --- user defined strings and macros ------------------------------------ */
3936 roff_userdef(ROFF_ARGS
)
3939 char *arg
, *ap
, *dst
, *src
;
3942 /* If the macro is empty, ignore it altogether. */
3944 if (*r
->current_string
== '\0')
3947 /* Initialize a new macro stack context. */
3949 if (++r
->mstackpos
== r
->mstacksz
) {
3950 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3951 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3954 ctx
= r
->mstack
+ r
->mstackpos
;
3958 * Collect pointers to macro argument strings,
3959 * NUL-terminating them and escaping quotes.
3962 src
= buf
->buf
+ pos
;
3963 while (*src
!= '\0') {
3964 if (ctx
->argc
== ctx
->argsz
) {
3966 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3967 ctx
->argsz
, sizeof(*ctx
->argv
));
3969 arg
= roff_getarg(r
, &src
, ln
, &pos
);
3970 sz
= 1; /* For the terminating NUL. */
3971 for (ap
= arg
; *ap
!= '\0'; ap
++)
3972 sz
+= *ap
== '"' ? 4 : 1;
3973 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3974 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3976 memcpy(dst
, "\\(dq", 4);
3985 /* Replace the macro invocation by the macro definition. */
3988 buf
->buf
= mandoc_strdup(r
->current_string
);
3989 buf
->sz
= strlen(buf
->buf
) + 1;
3992 return buf
->buf
[buf
->sz
- 2] == '\n' ?
3993 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3997 * Calling a high-level macro that was renamed with .rn.
3998 * r->current_string has already been set up by roff_parse().
4001 roff_renamed(ROFF_ARGS
)
4005 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
4006 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
4014 * Measure the length in bytes of the roff identifier at *cpp
4015 * and advance the pointer to the next word.
4018 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
4021 int namesz
, inam
, iend
;
4027 /* Advance cp to the byte after the end of the name. */
4034 if (*cp
== ' ' || *cp
== '\t') {
4039 if (name
+ namesz
< cp
) {
4047 if (cp
[1] == '{' || cp
[1] == '}')
4049 if (roff_escape(cp
, 0, 0, NULL
, &inam
,
4050 NULL
, NULL
, &iend
) != ESCAPE_UNDEF
) {
4051 mandoc_msg(MANDOCERR_NAMESC
, ln
, pos
,
4052 "%.*s%.*s", namesz
, name
, iend
, cp
);
4058 * In an identifier, \\, \., \G and so on
4059 * are reduced to \, ., G and so on,
4060 * vaguely similar to copy mode.
4063 name
[namesz
++] = cp
[inam
];
4065 if (cp
>= name
+ namesz
)
4071 /* Read past spaces. */
4081 * Store *string into the user-defined string called *name.
4082 * To clear an existing entry, call with (*r, *name, NULL, 0).
4083 * append == 0: replace mode
4084 * append == 1: single-line append mode
4085 * append == 2: multiline append mode, append '\n' after each call
4088 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
4093 namesz
= strlen(name
);
4094 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
4095 string
? strlen(string
) : 0, append
);
4096 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
4100 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
4101 const char *string
, size_t stringsz
, int append
)
4106 size_t oldch
, newch
;
4108 /* Search for an existing string with the same name. */
4111 while (n
&& (namesz
!= n
->key
.sz
||
4112 strncmp(n
->key
.p
, name
, namesz
)))
4116 /* Create a new string table entry. */
4117 n
= mandoc_malloc(sizeof(struct roffkv
));
4118 n
->key
.p
= mandoc_strndup(name
, namesz
);
4124 } else if (0 == append
) {
4134 * One additional byte for the '\n' in multiline mode,
4135 * and one for the terminating '\0'.
4137 newch
= stringsz
+ (1 < append
? 2u : 1u);
4139 if (NULL
== n
->val
.p
) {
4140 n
->val
.p
= mandoc_malloc(newch
);
4145 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
4148 /* Skip existing content in the destination buffer. */
4149 c
= n
->val
.p
+ (int)oldch
;
4151 /* Append new content to the destination buffer. */
4153 while (i
< (int)stringsz
) {
4155 * Rudimentary roff copy mode:
4156 * Handle escaped backslashes.
4158 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
4163 /* Append terminating bytes. */
4168 n
->val
.sz
= (int)(c
- n
->val
.p
);
4172 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
4175 const struct roffkv
*n
;
4180 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
4181 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4182 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4184 if (*deftype
& ROFFDEF_USER
) {
4185 *deftype
= ROFFDEF_USER
;
4192 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
4193 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4194 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4196 if (*deftype
& ROFFDEF_REN
) {
4197 *deftype
= ROFFDEF_REN
;
4204 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
4205 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
4206 predefs
[i
].name
[len
] != '\0')
4208 if (*deftype
& ROFFDEF_PRE
) {
4209 *deftype
= ROFFDEF_PRE
;
4210 return predefs
[i
].str
;
4216 if (r
->man
->meta
.macroset
!= MACROSET_MAN
) {
4217 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
4218 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4219 roff_name
[tok
][len
] != '\0')
4221 if (*deftype
& ROFFDEF_STD
) {
4222 *deftype
= ROFFDEF_STD
;
4230 if (r
->man
->meta
.macroset
!= MACROSET_MDOC
) {
4231 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
4232 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4233 roff_name
[tok
][len
] != '\0')
4235 if (*deftype
& ROFFDEF_STD
) {
4236 *deftype
= ROFFDEF_STD
;
4245 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
4246 if (*deftype
& ROFFDEF_REN
) {
4248 * This might still be a request,
4249 * so do not treat it as undefined yet.
4251 *deftype
= ROFFDEF_UNDEF
;
4255 /* Using an undefined string defines it to be empty. */
4257 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
4258 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
4266 roff_freestr(struct roffkv
*r
)
4268 struct roffkv
*n
, *nn
;
4270 for (n
= r
; n
; n
= nn
) {
4278 /* --- accessors and utility functions ------------------------------------ */
4281 * Duplicate an input string, making the appropriate character
4282 * conversations (as stipulated by `tr') along the way.
4283 * Returns a heap-allocated string with all the replacements made.
4286 roff_strdup(const struct roff
*r
, const char *p
)
4288 const struct roffkv
*cp
;
4292 enum mandoc_esc esc
;
4294 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
4295 return mandoc_strdup(p
);
4296 else if ('\0' == *p
)
4297 return mandoc_strdup("");
4300 * Step through each character looking for term matches
4301 * (remember that a `tr' can be invoked with an escape, which is
4302 * a glyph but the escape is multi-character).
4303 * We only do this if the character hash has been initialised
4304 * and the string is >0 length.
4310 while ('\0' != *p
) {
4311 assert((unsigned int)*p
< 128);
4312 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
4313 sz
= r
->xtab
[(int)*p
].sz
;
4314 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4315 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
4319 } else if ('\\' != *p
) {
4320 res
= mandoc_realloc(res
, ssz
+ 2);
4325 /* Search for term matches. */
4326 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4327 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4332 * A match has been found.
4333 * Append the match to the array and move
4334 * forward by its keysize.
4336 res
= mandoc_realloc(res
,
4337 ssz
+ cp
->val
.sz
+ 1);
4338 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4340 p
+= (int)cp
->key
.sz
;
4345 * Handle escapes carefully: we need to copy
4346 * over just the escape itself, or else we might
4347 * do replacements within the escape itself.
4348 * Make sure to pass along the bogus string.
4351 esc
= mandoc_escape(&p
, NULL
, NULL
);
4352 if (ESCAPE_ERROR
== esc
) {
4354 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4355 memcpy(res
+ ssz
, pp
, sz
);
4359 * We bail out on bad escapes.
4360 * No need to warn: we already did so when
4361 * roff_expand() was called.
4364 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4365 memcpy(res
+ ssz
, pp
, sz
);
4369 res
[(int)ssz
] = '\0';
4374 roff_getformat(const struct roff
*r
)
4381 * Find out whether a line is a macro line or not.
4382 * If it is, adjust the current position and return one; if it isn't,
4383 * return zero and don't change the current position.
4384 * If the control character has been set with `.cc', then let that grain
4386 * This is slighly contrary to groff, where using the non-breaking
4387 * control character when `cc' has been invoked will cause the
4388 * non-breaking macro contents to be printed verbatim.
4391 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4397 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4399 else if (r
->control
!= '\0')
4401 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4403 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4408 while (' ' == cp
[pos
] || '\t' == cp
[pos
])