]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.388 2022/05/19 15:37:47 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 deftype
; /* type of definition to paste */
1379 int argi
; /* macro argument index */
1380 int quote_args
; /* true for \\$@, false for \\$* */
1381 int asz
; /* length of the replacement */
1382 int rsz
; /* length of the rest of the string */
1383 int npos
; /* position in numeric expression */
1384 int expand_count
; /* to avoid infinite loops */
1387 while (buf
->buf
[pos
] != '\0') {
1390 * Skip plain ASCII characters.
1391 * If we have a non-standard escape character,
1392 * escape literal backslashes because all processing in
1393 * subsequent functions uses the standard escaping rules.
1396 if (buf
->buf
[pos
] != ec
) {
1397 if (ec
!= ASCII_ESC
&& buf
->buf
[pos
] == '\\') {
1398 roff_expand_patch(buf
, pos
, "\\e", pos
+ 1);
1406 * Parse escape sequences,
1407 * issue diagnostic messages when appropriate,
1408 * and skip sequences that do not need expansion.
1409 * If we have a non-standard escape character, translate
1410 * it to backslashes and translate backslashes to \e.
1413 if (roff_escape(buf
->buf
, ln
, pos
,
1414 &iesc
, &iarg
, &iendarg
, &iend
) != ESCAPE_EXPAND
) {
1415 while (pos
< iend
) {
1416 if (buf
->buf
[pos
] == ec
) {
1417 buf
->buf
[pos
] = '\\';
1420 } else if (buf
->buf
[pos
] == '\\') {
1421 roff_expand_patch(buf
,
1422 pos
, "\\e", pos
+ 1);
1432 * Treat "\E" just like "\";
1433 * it only makes a difference in copy mode.
1437 while (buf
->buf
[inam
] == 'E')
1440 /* Handle expansion. */
1443 switch (buf
->buf
[inam
]) {
1445 if (iendarg
== iarg
)
1447 deftype
= ROFFDEF_USER
| ROFFDEF_PRE
;
1448 if ((res
= roff_getstrn(r
, buf
->buf
+ iarg
,
1449 iendarg
- iarg
, &deftype
)) != NULL
)
1454 * let \*(.T through to the formatters.
1457 if (iendarg
- iarg
== 2 &&
1458 buf
->buf
[iarg
] == '.' &&
1459 buf
->buf
[iarg
+ 1] == 'T') {
1460 roff_setstrn(&r
->strtab
, ".T", 2, NULL
, 0, 0);
1465 mandoc_msg(MANDOCERR_STR_UNDEF
, ln
, iesc
,
1466 "%.*s", iendarg
- iarg
, buf
->buf
+ iarg
);
1470 if (r
->mstackpos
< 0) {
1471 mandoc_msg(MANDOCERR_ARG_UNDEF
, ln
, iesc
,
1472 "%.*s", iend
- iesc
, buf
->buf
+ iesc
);
1475 ctx
= r
->mstack
+ r
->mstackpos
;
1476 argi
= buf
->buf
[iarg
] - '1';
1477 if (argi
>= 0 && argi
<= 8) {
1478 if (argi
< ctx
->argc
)
1479 res
= ctx
->argv
[argi
];
1482 if (buf
->buf
[iarg
] == '*')
1484 else if (buf
->buf
[iarg
] == '@')
1487 mandoc_msg(MANDOCERR_ARG_NONUM
, ln
, iesc
,
1488 "%.*s", iend
- iesc
, buf
->buf
+ iesc
);
1492 for (argi
= 0; argi
< ctx
->argc
; argi
++) {
1496 asz
+= 2; /* quotes */
1497 asz
+= strlen(ctx
->argv
[argi
]);
1499 if (asz
!= iend
- iesc
) {
1500 rsz
= buf
->sz
- iend
;
1501 if (asz
< iend
- iesc
)
1502 memmove(buf
->buf
+ iesc
+ asz
,
1503 buf
->buf
+ iend
, rsz
);
1504 buf
->sz
= iesc
+ asz
+ rsz
;
1505 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
1506 if (asz
> iend
- iesc
)
1507 memmove(buf
->buf
+ iesc
+ asz
,
1508 buf
->buf
+ iend
, rsz
);
1510 dst
= buf
->buf
+ iesc
;
1511 for (argi
= 0; argi
< ctx
->argc
; argi
++) {
1516 src
= ctx
->argv
[argi
];
1517 while (*src
!= '\0')
1525 ubuf
[0] = iendarg
> iarg
&& iend
> iendarg
&&
1526 roff_evalnum(r
, ln
, buf
->buf
+ iarg
, &npos
,
1527 NULL
, ROFFNUM_SCALE
) &&
1528 npos
== iendarg
- iarg
? '1' : '0';
1534 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1535 roff_getregn(r
, buf
->buf
+ iarg
,
1536 iendarg
- iarg
, buf
->buf
[inam
+ 1]));
1542 (void)snprintf(ubuf
, sizeof(ubuf
),
1543 "%d", (iendarg
- iarg
) * 24);
1551 if (++expand_count
> EXPAND_LIMIT
||
1552 buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1553 mandoc_msg(MANDOCERR_ROFFLOOP
, ln
, iesc
, NULL
);
1556 roff_expand_patch(buf
, iesc
, res
, iend
);
1562 * Replace the substring from the start position (inclusive)
1563 * to end position (exclusive) with the repl(acement) string.
1566 roff_expand_patch(struct buf
*buf
, int start
, const char *repl
, int end
)
1570 buf
->buf
[start
] = '\0';
1571 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s", buf
->buf
, repl
,
1572 buf
->buf
+ end
) + 1;
1578 * Parse a quoted or unquoted roff-style request or macro argument.
1579 * Return a pointer to the parsed argument, which is either the original
1580 * pointer or advanced by one byte in case the argument is quoted.
1581 * NUL-terminate the argument in place.
1582 * Collapse pairs of quotes inside quoted arguments.
1583 * Advance the argument pointer to the next argument,
1584 * or to the NUL byte terminating the argument line.
1587 roff_getarg(struct roff
*r
, char **cpp
, int ln
, int *pos
)
1591 int newesc
, pairs
, quoted
, white
;
1593 /* Quoting can only start with a new word. */
1596 if ('"' == *start
) {
1601 newesc
= pairs
= white
= 0;
1602 for (cp
= start
; '\0' != *cp
; cp
++) {
1605 * Move the following text left
1606 * after quoted quotes and after "\\" and "\t".
1611 if ('\\' == cp
[0]) {
1613 * In copy mode, translate double to single
1614 * backslashes and backslash-t to literal tabs.
1625 cp
[-pairs
] = ASCII_ESC
;
1630 /* Skip escaped blanks. */
1637 } else if (0 == quoted
) {
1639 /* Unescaped blanks end unquoted args. */
1643 } else if ('"' == cp
[0]) {
1645 /* Quoted quotes collapse. */
1649 /* Unquoted quotes end quoted args. */
1656 /* Quoted argument without a closing quote. */
1658 mandoc_msg(MANDOCERR_ARG_QUOTE
, ln
, *pos
, NULL
);
1660 /* NUL-terminate this argument and move to the next one. */
1668 *pos
+= (int)(cp
- start
) + (quoted
? 1 : 0);
1671 if ('\0' == *cp
&& (white
|| ' ' == cp
[-1]))
1672 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, *pos
, NULL
);
1674 start
= mandoc_strdup(start
);
1679 buf
.sz
= strlen(start
) + 1;
1681 if (roff_expand(r
, &buf
, ln
, 0, ASCII_ESC
) & ROFF_IGN
) {
1683 buf
.buf
= mandoc_strdup("");
1690 * Process text streams.
1693 roff_parsetext(struct roff
*r
, struct buf
*buf
, int pos
, int *offs
)
1699 enum mandoc_esc esc
;
1701 /* Spring the input line trap. */
1703 if (roffit_lines
== 1) {
1704 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1711 return ROFF_REPARSE
;
1712 } else if (roffit_lines
> 1)
1715 if (roffce_node
!= NULL
&& buf
->buf
[pos
] != '\0') {
1716 if (roffce_lines
< 1) {
1717 r
->man
->last
= roffce_node
;
1718 r
->man
->next
= ROFF_NEXT_SIBLING
;
1725 /* Convert all breakable hyphens into ASCII_HYPH. */
1727 start
= p
= buf
->buf
+ pos
;
1729 while (*p
!= '\0') {
1730 sz
= strcspn(p
, "-\\");
1737 /* Skip over escapes. */
1739 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1740 if (esc
== ESCAPE_ERROR
)
1745 } else if (p
== start
) {
1750 if (isalpha((unsigned char)p
[-1]) &&
1751 isalpha((unsigned char)p
[1]))
1759 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
, size_t len
)
1763 int pos
; /* parse point */
1764 int spos
; /* saved parse point for messages */
1765 int ppos
; /* original offset in buf->buf */
1766 int ctl
; /* macro line (boolean) */
1770 if (len
> 80 && r
->tbl
== NULL
&& r
->eqn
== NULL
&&
1771 (r
->man
->flags
& ROFF_NOFILL
) == 0 &&
1772 strchr(" .\\", buf
->buf
[pos
]) == NULL
&&
1773 buf
->buf
[pos
] != r
->control
&&
1774 strcspn(buf
->buf
, " ") < 80)
1775 mandoc_msg(MANDOCERR_TEXT_LONG
, ln
, (int)len
- 1,
1776 "%.20s...", buf
->buf
+ pos
);
1778 /* Handle in-line equation delimiters. */
1780 if (r
->tbl
== NULL
&&
1781 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1782 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1783 e
= roff_eqndelim(r
, buf
, pos
);
1784 if (e
== ROFF_REPARSE
)
1786 assert(e
== ROFF_CONT
);
1789 /* Handle comments and escape sequences. */
1791 e
= roff_parse_comment(r
, buf
, ln
, pos
, r
->escape
);
1792 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1794 assert(e
== ROFF_CONT
);
1796 e
= roff_expand(r
, buf
, ln
, pos
, r
->escape
);
1797 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1799 assert(e
== ROFF_CONT
);
1801 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1804 * First, if a scope is open and we're not a macro, pass the
1805 * text through the macro's filter.
1806 * Equations process all content themselves.
1807 * Tables process almost all content themselves, but we want
1808 * to warn about macros before passing it there.
1811 if (r
->last
!= NULL
&& ! ctl
) {
1813 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1814 if ((e
& ROFF_MASK
) == ROFF_IGN
)
1819 if (r
->eqn
!= NULL
&& strncmp(buf
->buf
+ ppos
, ".EN", 3)) {
1820 eqn_read(r
->eqn
, buf
->buf
+ ppos
);
1823 if (r
->tbl
!= NULL
&& (ctl
== 0 || buf
->buf
[pos
] == '\0')) {
1824 tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1825 roff_addtbl(r
->man
, ln
, r
->tbl
);
1829 r
->options
&= ~MPARSE_COMMENT
;
1830 return roff_parsetext(r
, buf
, pos
, offs
) | e
;
1833 /* Skip empty request lines. */
1835 if (buf
->buf
[pos
] == '"') {
1836 mandoc_msg(MANDOCERR_COMMENT_BAD
, ln
, pos
, NULL
);
1838 } else if (buf
->buf
[pos
] == '\0')
1842 * If a scope is open, go to the child handler for that macro,
1843 * as it may want to preprocess before doing anything with it.
1848 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1851 r
->options
&= ~MPARSE_COMMENT
;
1853 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1854 return roff_req_or_macro(r
, t
, buf
, ln
, spos
, pos
, offs
);
1858 * Handle a new request or macro.
1859 * May be called outside any scope or from inside a conditional scope.
1862 roff_req_or_macro(ROFF_ARGS
) {
1864 /* For now, tables ignore most macros and some request. */
1866 if (r
->tbl
!= NULL
&& (tok
== TOKEN_NONE
|| tok
== ROFF_TS
||
1867 tok
== ROFF_br
|| tok
== ROFF_ce
|| tok
== ROFF_rj
||
1869 mandoc_msg(MANDOCERR_TBLMACRO
,
1870 ln
, ppos
, "%s", buf
->buf
+ ppos
);
1871 if (tok
!= TOKEN_NONE
)
1873 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1875 while (buf
->buf
[pos
] == ' ')
1877 tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1878 roff_addtbl(r
->man
, ln
, r
->tbl
);
1882 /* For now, let high level macros abort .ce mode. */
1884 if (roffce_node
!= NULL
&&
1885 (tok
== TOKEN_NONE
|| tok
== ROFF_Dd
|| tok
== ROFF_EQ
||
1886 tok
== ROFF_TH
|| tok
== ROFF_TS
)) {
1887 r
->man
->last
= roffce_node
;
1888 r
->man
->next
= ROFF_NEXT_SIBLING
;
1894 * This is neither a roff request nor a user-defined macro.
1895 * Let the standard macro set parsers handle it.
1898 if (tok
== TOKEN_NONE
)
1901 /* Execute a roff request or a user-defined macro. */
1903 return (*roffs
[tok
].proc
)(r
, tok
, buf
, ln
, ppos
, pos
, offs
);
1907 * Internal interface function to tell the roff parser that execution
1908 * of the current macro ended. This is required because macro
1909 * definitions usually do not end with a .return request.
1912 roff_userret(struct roff
*r
)
1917 assert(r
->mstackpos
>= 0);
1918 ctx
= r
->mstack
+ r
->mstackpos
;
1919 for (i
= 0; i
< ctx
->argc
; i
++)
1926 roff_endparse(struct roff
*r
)
1928 if (r
->last
!= NULL
)
1929 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->last
->line
,
1930 r
->last
->col
, "%s", roff_name
[r
->last
->tok
]);
1932 if (r
->eqn
!= NULL
) {
1933 mandoc_msg(MANDOCERR_BLK_NOEND
,
1934 r
->eqn
->node
->line
, r
->eqn
->node
->pos
, "EQ");
1939 if (r
->tbl
!= NULL
) {
1946 * Parse the request or macro name at buf[*pos].
1947 * Return ROFF_RENAMED, ROFF_USERDEF, or a ROFF_* token value.
1948 * For empty, undefined, mdoc(7), and man(7) macros, return TOKEN_NONE.
1949 * As a side effect, set r->current_string to the definition or to NULL.
1951 static enum roff_tok
1952 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1962 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1966 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1968 deftype
= ROFFDEF_USER
| ROFFDEF_REN
;
1969 r
->current_string
= roff_getstrn(r
, mac
, maclen
, &deftype
);
1978 t
= roffhash_find(r
->reqtab
, mac
, maclen
);
1981 if (t
!= TOKEN_NONE
)
1983 else if (deftype
== ROFFDEF_UNDEF
) {
1984 /* Using an undefined macro defines it to be empty. */
1985 roff_setstrn(&r
->strtab
, mac
, maclen
, "", 0, 0);
1986 roff_setstrn(&r
->rentab
, mac
, maclen
, NULL
, 0, 0);
1991 /* --- handling of request blocks ----------------------------------------- */
1994 * Close a macro definition block or an "ignore" block.
1997 roff_cblock(ROFF_ARGS
)
2001 if (r
->last
== NULL
) {
2002 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2006 switch (r
->last
->tok
) {
2015 /* Remapped in roff_block(). */
2018 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "..");
2023 roffnode_cleanscope(r
);
2026 * If a conditional block with braces is still open,
2027 * check for "\}" block end markers.
2030 if (r
->last
!= NULL
&& r
->last
->endspan
< 0) {
2031 rr
= 1; /* If arguments follow "\}", warn about them. */
2032 roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2035 if (buf
->buf
[pos
] != '\0')
2036 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
2037 ".. %s", buf
->buf
+ pos
);
2043 * Pop all nodes ending at the end of the current input line.
2044 * Return the number of loops ended.
2047 roffnode_cleanscope(struct roff
*r
)
2052 while (r
->last
!= NULL
&& r
->last
->endspan
> 0) {
2053 if (--r
->last
->endspan
!= 0)
2055 inloop
+= roffnode_pop(r
);
2061 * Handle the closing "\}" of a conditional block.
2062 * Apart from generating warnings, this only pops nodes.
2063 * Return the number of loops ended.
2066 roff_ccond(struct roff
*r
, int ln
, int ppos
)
2068 if (NULL
== r
->last
) {
2069 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2073 switch (r
->last
->tok
) {
2080 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2084 if (r
->last
->endspan
> -1) {
2085 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "\\}");
2089 return roffnode_pop(r
) + roffnode_cleanscope(r
);
2093 roff_block(ROFF_ARGS
)
2095 const char *name
, *value
;
2096 char *call
, *cp
, *iname
, *rname
;
2097 size_t csz
, namesz
, rsz
;
2100 /* Ignore groff compatibility mode for now. */
2102 if (tok
== ROFF_de1
)
2104 else if (tok
== ROFF_dei1
)
2106 else if (tok
== ROFF_am1
)
2108 else if (tok
== ROFF_ami1
)
2111 /* Parse the macro name argument. */
2113 cp
= buf
->buf
+ pos
;
2114 if (tok
== ROFF_ig
) {
2119 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2120 iname
[namesz
] = '\0';
2123 /* Resolve the macro name argument if it is indirect. */
2125 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2126 deftype
= ROFFDEF_USER
;
2127 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2129 mandoc_msg(MANDOCERR_STR_UNDEF
,
2130 ln
, (int)(iname
- buf
->buf
),
2131 "%.*s", (int)namesz
, iname
);
2134 namesz
= strlen(name
);
2138 if (namesz
== 0 && tok
!= ROFF_ig
) {
2139 mandoc_msg(MANDOCERR_REQ_EMPTY
,
2140 ln
, ppos
, "%s", roff_name
[tok
]);
2144 roffnode_push(r
, tok
, name
, ln
, ppos
);
2147 * At the beginning of a `de' macro, clear the existing string
2148 * with the same name, if there is one. New content will be
2149 * appended from roff_block_text() in multiline mode.
2152 if (tok
== ROFF_de
|| tok
== ROFF_dei
) {
2153 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
2154 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2155 } else if (tok
== ROFF_am
|| tok
== ROFF_ami
) {
2156 deftype
= ROFFDEF_ANY
;
2157 value
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2158 switch (deftype
) { /* Before appending, ... */
2159 case ROFFDEF_PRE
: /* copy predefined to user-defined. */
2160 roff_setstrn(&r
->strtab
, name
, namesz
,
2161 value
, strlen(value
), 0);
2163 case ROFFDEF_REN
: /* call original standard macro. */
2164 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2165 (int)strlen(value
), value
);
2166 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2167 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2170 case ROFFDEF_STD
: /* rename and call standard macro. */
2171 rsz
= mandoc_asprintf(&rname
, "__%s_renamed", name
);
2172 roff_setstrn(&r
->rentab
, rname
, rsz
, name
, namesz
, 0);
2173 csz
= mandoc_asprintf(&call
, ".%.*s \\$* \\\"\n",
2175 roff_setstrn(&r
->strtab
, name
, namesz
, call
, csz
, 0);
2187 /* Get the custom end marker. */
2190 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
2192 /* Resolve the end marker if it is indirect. */
2194 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
2195 deftype
= ROFFDEF_USER
;
2196 name
= roff_getstrn(r
, iname
, namesz
, &deftype
);
2198 mandoc_msg(MANDOCERR_STR_UNDEF
,
2199 ln
, (int)(iname
- buf
->buf
),
2200 "%.*s", (int)namesz
, iname
);
2203 namesz
= strlen(name
);
2208 r
->last
->end
= mandoc_strndup(name
, namesz
);
2211 mandoc_msg(MANDOCERR_ARG_EXCESS
,
2212 ln
, pos
, ".%s ... %s", roff_name
[tok
], cp
);
2218 roff_block_sub(ROFF_ARGS
)
2224 * If a custom end marker is a user-defined or predefined macro
2225 * or a request, interpret it.
2229 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
2230 if (buf
->buf
[i
] != r
->last
->end
[j
])
2233 if (r
->last
->end
[j
] == '\0' &&
2234 (buf
->buf
[i
] == '\0' ||
2235 buf
->buf
[i
] == ' ' ||
2236 buf
->buf
[i
] == '\t')) {
2238 roffnode_cleanscope(r
);
2240 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
2244 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
2251 /* Handle the standard end marker. */
2253 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2254 if (t
== ROFF_cblock
)
2255 return roff_cblock(r
, t
, buf
, ln
, ppos
, pos
, offs
);
2257 /* Not an end marker, so append the line to the block. */
2260 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
2265 roff_block_text(ROFF_ARGS
)
2269 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
2275 * Check for a closing "\}" and handle it.
2276 * In this function, the final "int *offs" argument is used for
2277 * different purposes than elsewhere:
2278 * Input: *offs == 0: caller wants to discard arguments following \}
2279 * *offs == 1: caller wants to preserve text following \}
2280 * Output: *offs = 0: tell caller to discard input line
2281 * *offs = 1: tell caller to use input line
2284 roff_cond_checkend(ROFF_ARGS
)
2287 int endloop
, irc
, rr
;
2291 endloop
= tok
!= ROFF_while
? ROFF_IGN
:
2292 rr
? ROFF_LOOPCONT
: ROFF_LOOPEXIT
;
2293 if (roffnode_cleanscope(r
))
2297 * If "\}" occurs on a macro line without a preceding macro or
2298 * a text line contains nothing else, drop the line completely.
2301 ep
= buf
->buf
+ pos
;
2302 if (ep
[0] == '\\' && ep
[1] == '}' && (ep
[2] == '\0' || *offs
== 0))
2306 * The closing delimiter "\}" rewinds the conditional scope
2307 * but is otherwise ignored when interpreting the line.
2310 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2318 memmove(ep
, ep
+ 2, strlen(ep
+ 2) + 1);
2319 if (roff_ccond(r
, ln
, ep
- buf
->buf
))
2335 * Parse and process a request or macro line in conditional scope.
2338 roff_cond_sub(ROFF_ARGS
)
2340 struct roffnode
*bl
;
2344 rr
= 0; /* If arguments follow "\}", skip them. */
2345 irc
= roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2347 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
2350 * Handle requests and macros if the conditional evaluated
2351 * to true or if they are structurally required.
2352 * The .break request is always handled specially.
2355 if (t
== ROFF_break
) {
2356 if (irc
& ROFF_LOOPMASK
)
2357 irc
= ROFF_IGN
| ROFF_LOOPEXIT
;
2359 for (bl
= r
->last
; bl
!= NULL
; bl
= bl
->parent
) {
2361 if (bl
->tok
== ROFF_while
)
2365 } else if (rr
|| (t
< TOKEN_NONE
&& roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
2366 irc
|= roff_req_or_macro(r
, t
, buf
, ln
, spos
, pos
, offs
);
2367 if (irc
& ROFF_WHILE
)
2368 irc
&= ~(ROFF_LOOPCONT
| ROFF_LOOPEXIT
);
2374 * Parse and process a text line in conditional scope.
2377 roff_cond_text(ROFF_ARGS
)
2381 rr
= 1; /* If arguments follow "\}", preserve them. */
2382 irc
= roff_cond_checkend(r
, tok
, buf
, ln
, ppos
, pos
, &rr
);
2388 /* --- handling of numeric and conditional expressions -------------------- */
2391 * Parse a single signed integer number. Stop at the first non-digit.
2392 * If there is at least one digit, return success and advance the
2393 * parse point, else return failure and let the parse point unchanged.
2394 * Ignore overflows, treat them just like the C language.
2397 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2399 int myres
, scaled
, n
, p
;
2406 if (n
|| v
[p
] == '+')
2409 if (flags
& ROFFNUM_WHITE
)
2410 while (isspace((unsigned char)v
[p
]))
2413 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2414 *res
= 10 * *res
+ v
[p
] - '0';
2421 /* Each number may be followed by one optional scaling unit. */
2425 scaled
= *res
* 65536;
2428 scaled
= *res
* 240;
2431 scaled
= *res
* 240 / 2.54;
2442 scaled
= *res
* 10 / 3;
2448 scaled
= *res
* 6 / 25;
2455 if (flags
& ROFFNUM_SCALE
)
2463 * Evaluate a string comparison condition.
2464 * The first character is the delimiter.
2465 * Succeed if the string up to its second occurrence
2466 * matches the string up to its third occurence.
2467 * Advance the cursor after the third occurrence
2468 * or lacking that, to the end of the line.
2471 roff_evalstrcond(const char *v
, int *pos
)
2473 const char *s1
, *s2
, *s3
;
2477 s1
= v
+ *pos
; /* initial delimiter */
2478 s2
= s1
+ 1; /* for scanning the first string */
2479 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2481 if (NULL
== s3
) /* found no middle delimiter */
2484 while ('\0' != *++s3
) {
2485 if (*s2
!= *s3
) { /* mismatch */
2486 s3
= strchr(s3
, *s1
);
2489 if (*s3
== *s1
) { /* found the final delimiter */
2498 s3
= strchr(s2
, '\0');
2499 else if (*s3
!= '\0')
2506 * Evaluate an optionally negated single character, numerical,
2507 * or string condition.
2510 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2512 const char *start
, *end
;
2515 int deftype
, len
, number
, savepos
, istrue
, wanttrue
;
2517 if ('!' == v
[*pos
]) {
2538 } while (v
[*pos
] == ' ');
2541 * Quirk for groff compatibility:
2542 * The horizontal tab is neither available nor unavailable.
2545 if (v
[*pos
] == '\t') {
2550 /* Printable ASCII characters are available. */
2552 if (v
[*pos
] != '\\') {
2558 switch (mandoc_escape(&end
, &start
, &len
)) {
2559 case ESCAPE_SPECIAL
:
2560 istrue
= mchars_spec2cp(start
, len
) != -1;
2562 case ESCAPE_UNICODE
:
2565 case ESCAPE_NUMBERED
:
2566 istrue
= mchars_num2char(start
, len
) != -1;
2573 return istrue
== wanttrue
;
2580 sz
= roff_getname(r
, &cp
, ln
, cp
- v
);
2583 else if (v
[*pos
] == 'r')
2584 istrue
= roff_hasregn(r
, name
, sz
);
2586 deftype
= ROFFDEF_ANY
;
2587 roff_getstrn(r
, name
, sz
, &deftype
);
2590 *pos
= (name
+ sz
) - v
;
2591 return istrue
== wanttrue
;
2597 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2598 return (number
> 0) == wanttrue
;
2599 else if (*pos
== savepos
)
2600 return roff_evalstrcond(v
, pos
) == wanttrue
;
2606 roff_line_ignore(ROFF_ARGS
)
2613 roff_insec(ROFF_ARGS
)
2616 mandoc_msg(MANDOCERR_REQ_INSEC
, ln
, ppos
, "%s", roff_name
[tok
]);
2621 roff_unsupp(ROFF_ARGS
)
2624 mandoc_msg(MANDOCERR_REQ_UNSUPP
, ln
, ppos
, "%s", roff_name
[tok
]);
2629 roff_cond(ROFF_ARGS
)
2633 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2636 * An `.el' has no conditional body: it will consume the value
2637 * of the current rstack entry set in prior `ie' calls or
2640 * If we're not an `el', however, then evaluate the conditional.
2643 r
->last
->rule
= tok
== ROFF_el
?
2644 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2645 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2648 * An if-else will put the NEGATION of the current evaluated
2649 * conditional into the stack of rules.
2652 if (tok
== ROFF_ie
) {
2653 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2655 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2656 r
->rstacksz
, sizeof(int));
2658 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2661 /* If the parent has false as its rule, then so do we. */
2663 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2668 * If there is nothing on the line after the conditional,
2669 * not even whitespace, use next-line scope.
2670 * Except that .while does not support next-line scope.
2673 if (buf
->buf
[pos
] == '\0' && tok
!= ROFF_while
) {
2674 r
->last
->endspan
= 2;
2678 while (buf
->buf
[pos
] == ' ')
2681 /* An opening brace requests multiline scope. */
2683 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2684 r
->last
->endspan
= -1;
2686 while (buf
->buf
[pos
] == ' ')
2692 * Anything else following the conditional causes
2693 * single-line scope. Warn if the scope contains
2694 * nothing but trailing whitespace.
2697 if (buf
->buf
[pos
] == '\0')
2698 mandoc_msg(MANDOCERR_COND_EMPTY
,
2699 ln
, ppos
, "%s", roff_name
[tok
]);
2701 r
->last
->endspan
= 1;
2706 if (tok
== ROFF_while
)
2718 /* Ignore groff compatibility mode for now. */
2720 if (tok
== ROFF_ds1
)
2722 else if (tok
== ROFF_as1
)
2726 * The first word is the name of the string.
2727 * If it is empty or terminated by an escape sequence,
2728 * abort the `ds' request without defining anything.
2731 name
= string
= buf
->buf
+ pos
;
2735 namesz
= roff_getname(r
, &string
, ln
, pos
);
2736 switch (name
[namesz
]) {
2740 string
= buf
->buf
+ pos
+ namesz
;
2746 /* Read past the initial double-quote, if any. */
2750 /* The rest is the value. */
2751 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2753 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
2758 * Parse a single operator, one or two characters long.
2759 * If the operator is recognized, return success and advance the
2760 * parse point, else return failure and let the parse point unchanged.
2763 roff_getop(const char *v
, int *pos
, char *res
)
2778 switch (v
[*pos
+ 1]) {
2796 switch (v
[*pos
+ 1]) {
2810 if ('=' == v
[*pos
+ 1])
2822 * Evaluate either a parenthesized numeric expression
2823 * or a single signed integer number.
2826 roff_evalpar(struct roff
*r
, int ln
,
2827 const char *v
, int *pos
, int *res
, int flags
)
2831 return roff_getnum(v
, pos
, res
, flags
);
2834 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2838 * Omission of the closing parenthesis
2839 * is an error in validation mode,
2840 * but ignored in evaluation mode.
2845 else if (NULL
== res
)
2852 * Evaluate a complete numeric expression.
2853 * Proceed left to right, there is no concept of precedence.
2856 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2857 int *pos
, int *res
, int flags
)
2859 int mypos
, operand2
;
2867 if (flags
& ROFFNUM_WHITE
)
2868 while (isspace((unsigned char)v
[*pos
]))
2871 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2875 if (flags
& ROFFNUM_WHITE
)
2876 while (isspace((unsigned char)v
[*pos
]))
2879 if ( ! roff_getop(v
, pos
, &operator))
2882 if (flags
& ROFFNUM_WHITE
)
2883 while (isspace((unsigned char)v
[*pos
]))
2886 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2889 if (flags
& ROFFNUM_WHITE
)
2890 while (isspace((unsigned char)v
[*pos
]))
2907 if (operand2
== 0) {
2908 mandoc_msg(MANDOCERR_DIVZERO
,
2916 if (operand2
== 0) {
2917 mandoc_msg(MANDOCERR_DIVZERO
,
2925 *res
= *res
< operand2
;
2928 *res
= *res
> operand2
;
2931 *res
= *res
<= operand2
;
2934 *res
= *res
>= operand2
;
2937 *res
= *res
== operand2
;
2940 *res
= *res
!= operand2
;
2943 *res
= *res
&& operand2
;
2946 *res
= *res
|| operand2
;
2949 if (operand2
< *res
)
2953 if (operand2
> *res
)
2963 /* --- register management ------------------------------------------------ */
2966 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2968 roff_setregn(r
, name
, strlen(name
), val
, sign
, INT_MIN
);
2972 roff_setregn(struct roff
*r
, const char *name
, size_t len
,
2973 int val
, char sign
, int step
)
2975 struct roffreg
*reg
;
2977 /* Search for an existing register with the same name. */
2980 while (reg
!= NULL
&& (reg
->key
.sz
!= len
||
2981 strncmp(reg
->key
.p
, name
, len
) != 0))
2985 /* Create a new register. */
2986 reg
= mandoc_malloc(sizeof(struct roffreg
));
2987 reg
->key
.p
= mandoc_strndup(name
, len
);
2991 reg
->next
= r
->regtab
;
2997 else if ('-' == sign
)
3001 if (step
!= INT_MIN
)
3006 * Handle some predefined read-only number registers.
3007 * For now, return -1 if the requested register is not predefined;
3008 * in case a predefined read-only register having the value -1
3009 * were to turn up, another special value would have to be chosen.
3012 roff_getregro(const struct roff
*r
, const char *name
)
3016 case '$': /* Number of arguments of the last macro evaluated. */
3017 return r
->mstackpos
< 0 ? 0 : r
->mstack
[r
->mstackpos
].argc
;
3018 case 'A': /* ASCII approximation mode is always off. */
3020 case 'g': /* Groff compatibility mode is always on. */
3022 case 'H': /* Fixed horizontal resolution. */
3024 case 'j': /* Always adjust left margin only. */
3026 case 'T': /* Some output device is always defined. */
3028 case 'V': /* Fixed vertical resolution. */
3036 roff_getreg(struct roff
*r
, const char *name
)
3038 return roff_getregn(r
, name
, strlen(name
), '\0');
3042 roff_getregn(struct roff
*r
, const char *name
, size_t len
, char sign
)
3044 struct roffreg
*reg
;
3047 if ('.' == name
[0] && 2 == len
) {
3048 val
= roff_getregro(r
, name
+ 1);
3053 for (reg
= r
->regtab
; reg
; reg
= reg
->next
) {
3054 if (len
== reg
->key
.sz
&&
3055 0 == strncmp(name
, reg
->key
.p
, len
)) {
3058 reg
->val
+= reg
->step
;
3061 reg
->val
-= reg
->step
;
3070 roff_setregn(r
, name
, len
, 0, '\0', INT_MIN
);
3075 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
3077 struct roffreg
*reg
;
3080 if ('.' == name
[0] && 2 == len
) {
3081 val
= roff_getregro(r
, name
+ 1);
3086 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
3087 if (len
== reg
->key
.sz
&&
3088 0 == strncmp(name
, reg
->key
.p
, len
))
3095 roff_freereg(struct roffreg
*reg
)
3097 struct roffreg
*old_reg
;
3099 while (NULL
!= reg
) {
3110 char *key
, *val
, *step
;
3115 key
= val
= buf
->buf
+ pos
;
3119 keysz
= roff_getname(r
, &val
, ln
, pos
);
3120 if (key
[keysz
] == '\\' || key
[keysz
] == '\t')
3124 if (sign
== '+' || sign
== '-')
3128 if (roff_evalnum(r
, ln
, val
, &len
, &iv
, ROFFNUM_SCALE
) == 0)
3132 while (isspace((unsigned char)*step
))
3134 if (roff_evalnum(r
, ln
, step
, NULL
, &is
, 0) == 0)
3137 roff_setregn(r
, key
, keysz
, iv
, sign
, is
);
3144 struct roffreg
*reg
, **prev
;
3148 name
= cp
= buf
->buf
+ pos
;
3151 namesz
= roff_getname(r
, &cp
, ln
, pos
);
3152 name
[namesz
] = '\0';
3157 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
3169 /* --- handler functions for roff requests -------------------------------- */
3178 cp
= buf
->buf
+ pos
;
3179 while (*cp
!= '\0') {
3181 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
3182 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
3183 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
3184 if (name
[namesz
] == '\\' || name
[namesz
] == '\t')
3195 /* Parse the number of lines. */
3197 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
3198 mandoc_msg(MANDOCERR_IT_NONUM
,
3199 ln
, ppos
, "%s", buf
->buf
+ 1);
3203 while (isspace((unsigned char)buf
->buf
[pos
]))
3207 * Arm the input line trap.
3208 * Special-casing "an-trap" is an ugly workaround to cope
3209 * with DocBook stupidly fiddling with man(7) internals.
3213 roffit_macro
= mandoc_strdup(iv
!= 1 ||
3214 strcmp(buf
->buf
+ pos
, "an-trap") ?
3215 buf
->buf
+ pos
: "br");
3223 enum roff_tok t
, te
;
3230 r
->format
= MPARSE_MDOC
;
3231 mask
= MPARSE_MDOC
| MPARSE_QUICK
;
3237 r
->format
= MPARSE_MAN
;
3238 mask
= MPARSE_QUICK
;
3243 if ((r
->options
& mask
) == 0)
3244 for (t
= tok
; t
< te
; t
++)
3245 roff_setstr(r
, roff_name
[t
], NULL
, 0);
3252 r
->man
->flags
&= ~ROFF_NONOFILL
;
3253 if (r
->tbl
== NULL
) {
3254 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "TE");
3257 if (tbl_end(r
->tbl
, 0) == 0) {
3260 buf
->buf
= mandoc_strdup(".sp");
3263 return ROFF_REPARSE
;
3274 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "T&");
3276 tbl_restart(ln
, ppos
, r
->tbl
);
3282 * Handle in-line equation delimiters.
3285 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
3288 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
3291 * Outside equations, look for an opening delimiter.
3292 * If we are inside an equation, we already know it is
3293 * in-line, or this function wouldn't have been called;
3294 * so look for a closing delimiter.
3297 cp1
= buf
->buf
+ pos
;
3298 cp2
= strchr(cp1
, r
->eqn
== NULL
?
3299 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
3304 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
3306 /* Handle preceding text, protecting whitespace. */
3308 if (*buf
->buf
!= '\0') {
3315 * Prepare replacing the delimiter with an equation macro
3316 * and drop leading white space from the equation.
3319 if (r
->eqn
== NULL
) {
3326 /* Handle following text, protecting whitespace. */
3334 /* Do the actual replacement. */
3336 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
3337 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
3341 /* Toggle the in-line state of the eqn subsystem. */
3343 r
->eqn_inline
= r
->eqn
== NULL
;
3344 return ROFF_REPARSE
;
3350 struct roff_node
*n
;
3352 if (r
->man
->meta
.macroset
== MACROSET_MAN
)
3353 man_breakscope(r
->man
, ROFF_EQ
);
3354 n
= roff_node_alloc(r
->man
, ln
, ppos
, ROFFT_EQN
, TOKEN_NONE
);
3355 if (ln
> r
->man
->last
->line
)
3356 n
->flags
|= NODE_LINE
;
3357 n
->eqn
= eqn_box_new();
3358 roff_node_append(r
->man
, n
);
3359 r
->man
->next
= ROFF_NEXT_SIBLING
;
3361 assert(r
->eqn
== NULL
);
3362 if (r
->last_eqn
== NULL
)
3363 r
->last_eqn
= eqn_alloc();
3365 eqn_reset(r
->last_eqn
);
3366 r
->eqn
= r
->last_eqn
;
3369 if (buf
->buf
[pos
] != '\0')
3370 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3371 ".EQ %s", buf
->buf
+ pos
);
3379 if (r
->eqn
!= NULL
) {
3383 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, ppos
, "EN");
3384 if (buf
->buf
[pos
] != '\0')
3385 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3386 "EN %s", buf
->buf
+ pos
);
3393 if (r
->tbl
!= NULL
) {
3394 mandoc_msg(MANDOCERR_BLK_BROKEN
, ln
, ppos
, "TS breaks TS");
3397 r
->man
->flags
|= ROFF_NONOFILL
;
3398 r
->tbl
= tbl_alloc(ppos
, ln
, r
->last_tbl
);
3399 if (r
->last_tbl
== NULL
)
3400 r
->first_tbl
= r
->tbl
;
3401 r
->last_tbl
= r
->tbl
;
3406 roff_noarg(ROFF_ARGS
)
3408 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
))
3409 man_breakscope(r
->man
, tok
);
3410 if (tok
== ROFF_brp
)
3412 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3413 if (buf
->buf
[pos
] != '\0')
3414 mandoc_msg(MANDOCERR_ARG_SKIP
, ln
, pos
,
3415 "%s %s", roff_name
[tok
], buf
->buf
+ pos
);
3417 r
->man
->flags
|= ROFF_NOFILL
;
3418 else if (tok
== ROFF_fi
)
3419 r
->man
->flags
&= ~ROFF_NOFILL
;
3420 r
->man
->last
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3421 r
->man
->next
= ROFF_NEXT_SIBLING
;
3426 roff_onearg(ROFF_ARGS
)
3428 struct roff_node
*n
;
3432 if (r
->man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
3433 (tok
== ROFF_ce
|| tok
== ROFF_rj
|| tok
== ROFF_sp
||
3435 man_breakscope(r
->man
, tok
);
3437 if (roffce_node
!= NULL
&& (tok
== ROFF_ce
|| tok
== ROFF_rj
)) {
3438 r
->man
->last
= roffce_node
;
3439 r
->man
->next
= ROFF_NEXT_SIBLING
;
3442 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3445 cp
= buf
->buf
+ pos
;
3447 while (*cp
!= '\0' && *cp
!= ' ')
3452 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3453 ln
, (int)(cp
- buf
->buf
),
3454 "%s ... %s", roff_name
[tok
], cp
);
3455 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3458 if (tok
== ROFF_ce
|| tok
== ROFF_rj
) {
3459 if (r
->man
->last
->type
== ROFFT_ELEM
) {
3460 roff_word_alloc(r
->man
, ln
, pos
, "1");
3461 r
->man
->last
->flags
|= NODE_NOSRC
;
3464 if (roff_evalnum(r
, ln
, r
->man
->last
->string
, &npos
,
3465 &roffce_lines
, 0) == 0) {
3466 mandoc_msg(MANDOCERR_CE_NONUM
,
3467 ln
, pos
, "ce %s", buf
->buf
+ pos
);
3470 if (roffce_lines
< 1) {
3471 r
->man
->last
= r
->man
->last
->parent
;
3475 roffce_node
= r
->man
->last
->parent
;
3477 n
->flags
|= NODE_VALID
| NODE_ENDED
;
3480 n
->flags
|= NODE_LINE
;
3481 r
->man
->next
= ROFF_NEXT_SIBLING
;
3486 roff_manyarg(ROFF_ARGS
)
3488 struct roff_node
*n
;
3491 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3494 for (sp
= ep
= buf
->buf
+ pos
; *sp
!= '\0'; sp
= ep
) {
3495 while (*ep
!= '\0' && *ep
!= ' ')
3499 roff_word_alloc(r
->man
, ln
, sp
- buf
->buf
, sp
);
3502 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3504 r
->man
->next
= ROFF_NEXT_SIBLING
;
3511 char *oldn
, *newn
, *end
, *value
;
3512 size_t oldsz
, newsz
, valsz
;
3514 newn
= oldn
= buf
->buf
+ pos
;
3518 newsz
= roff_getname(r
, &oldn
, ln
, pos
);
3519 if (newn
[newsz
] == '\\' || newn
[newsz
] == '\t' || *oldn
== '\0')
3523 oldsz
= roff_getname(r
, &end
, ln
, oldn
- buf
->buf
);
3527 valsz
= mandoc_asprintf(&value
, ".%.*s \\$@\\\"\n",
3529 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, valsz
, 0);
3530 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3536 * The .break request only makes sense inside conditionals,
3537 * and that case is already handled in roff_cond_sub().
3540 roff_break(ROFF_ARGS
)
3542 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, ln
, pos
, "break");
3553 if (*p
== '\0' || (r
->control
= *p
++) == '.')
3557 mandoc_msg(MANDOCERR_ARG_EXCESS
,
3558 ln
, p
- buf
->buf
, "cc ... %s", p
);
3564 roff_char(ROFF_ARGS
)
3566 const char *p
, *kp
, *vp
;
3570 /* Parse the character to be replaced. */
3572 kp
= buf
->buf
+ pos
;
3574 if (*kp
== '\0' || (*kp
== '\\' &&
3575 mandoc_escape(&p
, NULL
, NULL
) != ESCAPE_SPECIAL
) ||
3576 (*p
!= ' ' && *p
!= '\0')) {
3577 mandoc_msg(MANDOCERR_CHAR_ARG
, ln
, pos
, "char %s", kp
);
3585 * If the replacement string contains a font escape sequence,
3586 * we have to restore the font at the end.
3592 while (*p
!= '\0') {
3595 switch (mandoc_escape(&p
, NULL
, NULL
)) {
3597 case ESCAPE_FONTROMAN
:
3598 case ESCAPE_FONTITALIC
:
3599 case ESCAPE_FONTBOLD
:
3604 case ESCAPE_FONTPREV
:
3612 mandoc_msg(MANDOCERR_CHAR_FONT
,
3613 ln
, (int)(vp
- buf
->buf
), "%s", vp
);
3616 * Approximate the effect of .char using the .tr tables.
3617 * XXX In groff, .char and .tr interact differently.
3621 if (r
->xtab
== NULL
)
3622 r
->xtab
= mandoc_calloc(128, sizeof(*r
->xtab
));
3623 assert((unsigned int)*kp
< 128);
3624 free(r
->xtab
[(int)*kp
].p
);
3625 r
->xtab
[(int)*kp
].sz
= mandoc_asprintf(&r
->xtab
[(int)*kp
].p
,
3626 "%s%s", vp
, font
? "\fP" : "");
3628 roff_setstrn(&r
->xmbtab
, kp
, ksz
, vp
, vsz
, 0);
3630 roff_setstrn(&r
->xmbtab
, kp
, ksz
, "\\fP", 3, 1);
3646 mandoc_msg(MANDOCERR_ARG_EXCESS
, ln
,
3647 (int)(p
- buf
->buf
), "ec ... %s", p
);
3656 if (buf
->buf
[pos
] != '\0')
3657 mandoc_msg(MANDOCERR_ARG_SKIP
,
3658 ln
, pos
, "eo %s", buf
->buf
+ pos
);
3665 struct roff_node
*n
;
3668 /* Parse the first argument. */
3670 cp
= buf
->buf
+ pos
;
3673 if (buf
->buf
[pos
] == '\\') {
3674 switch (mandoc_escape((const char **)&cp
, NULL
, NULL
)) {
3675 case ESCAPE_SPECIAL
:
3676 case ESCAPE_UNICODE
:
3677 case ESCAPE_NUMBERED
:
3681 mandoc_msg(MANDOCERR_MC_ESC
, ln
, pos
,
3682 "mc %s", buf
->buf
+ pos
);
3683 buf
->buf
[pos
] = '\0';
3688 /* Ignore additional arguments. */
3693 mandoc_msg(MANDOCERR_MC_DIST
, ln
, (int)(cp
- buf
->buf
),
3698 /* Create the .mc node. */
3700 roff_elem_alloc(r
->man
, ln
, ppos
, tok
);
3702 if (buf
->buf
[pos
] != '\0')
3703 roff_word_alloc(r
->man
, ln
, pos
, buf
->buf
+ pos
);
3704 n
->flags
|= NODE_LINE
| NODE_VALID
| NODE_ENDED
;
3706 r
->man
->next
= ROFF_NEXT_SIBLING
;
3713 while (buf
->buf
[pos
] == ' ')
3722 const char *p
, *first
, *second
;
3724 enum mandoc_esc esc
;
3729 mandoc_msg(MANDOCERR_REQ_EMPTY
, ln
, ppos
, "tr");
3733 while (*p
!= '\0') {
3737 if (*first
== '\\') {
3738 esc
= mandoc_escape(&p
, NULL
, NULL
);
3739 if (esc
== ESCAPE_ERROR
) {
3740 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3741 (int)(p
- buf
->buf
), "%s", first
);
3744 fsz
= (size_t)(p
- first
);
3748 if (*second
== '\\') {
3749 esc
= mandoc_escape(&p
, NULL
, NULL
);
3750 if (esc
== ESCAPE_ERROR
) {
3751 mandoc_msg(MANDOCERR_ESC_BAD
, ln
,
3752 (int)(p
- buf
->buf
), "%s", second
);
3755 ssz
= (size_t)(p
- second
);
3756 } else if (*second
== '\0') {
3757 mandoc_msg(MANDOCERR_TR_ODD
, ln
,
3758 (int)(first
- buf
->buf
), "tr %s", first
);
3764 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3769 if (r
->xtab
== NULL
)
3770 r
->xtab
= mandoc_calloc(128,
3771 sizeof(struct roffstr
));
3773 free(r
->xtab
[(int)*first
].p
);
3774 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3775 r
->xtab
[(int)*first
].sz
= ssz
;
3782 * Implementation of the .return request.
3783 * There is no need to call roff_userret() from here.
3784 * The read module will call that after rewinding the reader stack
3785 * to the place from where the current macro was called.
3788 roff_return(ROFF_ARGS
)
3790 if (r
->mstackpos
>= 0)
3791 return ROFF_IGN
| ROFF_USERRET
;
3793 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "return");
3801 char *oldn
, *newn
, *end
;
3802 size_t oldsz
, newsz
;
3805 oldn
= newn
= buf
->buf
+ pos
;
3809 oldsz
= roff_getname(r
, &newn
, ln
, pos
);
3810 if (oldn
[oldsz
] == '\\' || oldn
[oldsz
] == '\t' || *newn
== '\0')
3814 newsz
= roff_getname(r
, &end
, ln
, newn
- buf
->buf
);
3818 deftype
= ROFFDEF_ANY
;
3819 value
= roff_getstrn(r
, oldn
, oldsz
, &deftype
);
3822 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3823 roff_setstrn(&r
->strtab
, oldn
, oldsz
, NULL
, 0, 0);
3824 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3827 roff_setstrn(&r
->strtab
, newn
, newsz
, value
, strlen(value
), 0);
3828 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3831 roff_setstrn(&r
->rentab
, newn
, newsz
, value
, strlen(value
), 0);
3832 roff_setstrn(&r
->rentab
, oldn
, oldsz
, NULL
, 0, 0);
3833 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3836 roff_setstrn(&r
->rentab
, newn
, newsz
, oldn
, oldsz
, 0);
3837 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3840 roff_setstrn(&r
->strtab
, newn
, newsz
, NULL
, 0, 0);
3841 roff_setstrn(&r
->rentab
, newn
, newsz
, NULL
, 0, 0);
3848 roff_shift(ROFF_ARGS
)
3851 int argpos
, levels
, i
;
3855 if (buf
->buf
[pos
] != '\0' &&
3856 roff_evalnum(r
, ln
, buf
->buf
, &pos
, &levels
, 0) == 0) {
3857 mandoc_msg(MANDOCERR_CE_NONUM
,
3858 ln
, pos
, "shift %s", buf
->buf
+ pos
);
3861 if (r
->mstackpos
< 0) {
3862 mandoc_msg(MANDOCERR_REQ_NOMAC
, ln
, ppos
, "shift");
3865 ctx
= r
->mstack
+ r
->mstackpos
;
3866 if (levels
> ctx
->argc
) {
3867 mandoc_msg(MANDOCERR_SHIFT
,
3868 ln
, argpos
, "%d, but max is %d", levels
, ctx
->argc
);
3872 mandoc_msg(MANDOCERR_ARG_NEG
, ln
, argpos
, "shift %d", levels
);
3877 for (i
= 0; i
< levels
; i
++)
3879 ctx
->argc
-= levels
;
3880 for (i
= 0; i
< ctx
->argc
; i
++)
3881 ctx
->argv
[i
] = ctx
->argv
[i
+ levels
];
3890 name
= buf
->buf
+ pos
;
3891 mandoc_msg(MANDOCERR_SO
, ln
, ppos
, "so %s", name
);
3894 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3895 * opening anything that's not in our cwd or anything beneath
3896 * it. Thus, explicitly disallow traversing up the file-system
3897 * or using absolute paths.
3900 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3901 mandoc_msg(MANDOCERR_SO_PATH
, ln
, ppos
, ".so %s", name
);
3902 buf
->sz
= mandoc_asprintf(&cp
,
3903 ".sp\nSee the file %s.\n.sp", name
) + 1;
3907 return ROFF_REPARSE
;
3914 /* --- user defined strings and macros ------------------------------------ */
3917 roff_userdef(ROFF_ARGS
)
3920 char *arg
, *ap
, *dst
, *src
;
3923 /* If the macro is empty, ignore it altogether. */
3925 if (*r
->current_string
== '\0')
3928 /* Initialize a new macro stack context. */
3930 if (++r
->mstackpos
== r
->mstacksz
) {
3931 r
->mstack
= mandoc_recallocarray(r
->mstack
,
3932 r
->mstacksz
, r
->mstacksz
+ 8, sizeof(*r
->mstack
));
3935 ctx
= r
->mstack
+ r
->mstackpos
;
3939 * Collect pointers to macro argument strings,
3940 * NUL-terminating them and escaping quotes.
3943 src
= buf
->buf
+ pos
;
3944 while (*src
!= '\0') {
3945 if (ctx
->argc
== ctx
->argsz
) {
3947 ctx
->argv
= mandoc_reallocarray(ctx
->argv
,
3948 ctx
->argsz
, sizeof(*ctx
->argv
));
3950 arg
= roff_getarg(r
, &src
, ln
, &pos
);
3951 sz
= 1; /* For the terminating NUL. */
3952 for (ap
= arg
; *ap
!= '\0'; ap
++)
3953 sz
+= *ap
== '"' ? 4 : 1;
3954 ctx
->argv
[ctx
->argc
++] = dst
= mandoc_malloc(sz
);
3955 for (ap
= arg
; *ap
!= '\0'; ap
++) {
3957 memcpy(dst
, "\\(dq", 4);
3966 /* Replace the macro invocation by the macro definition. */
3969 buf
->buf
= mandoc_strdup(r
->current_string
);
3970 buf
->sz
= strlen(buf
->buf
) + 1;
3973 return buf
->buf
[buf
->sz
- 2] == '\n' ?
3974 ROFF_REPARSE
| ROFF_USERCALL
: ROFF_IGN
| ROFF_APPEND
;
3978 * Calling a high-level macro that was renamed with .rn.
3979 * r->current_string has already been set up by roff_parse().
3982 roff_renamed(ROFF_ARGS
)
3986 buf
->sz
= mandoc_asprintf(&nbuf
, ".%s%s%s", r
->current_string
,
3987 buf
->buf
[pos
] == '\0' ? "" : " ", buf
->buf
+ pos
) + 1;
3995 * Measure the length in bytes of the roff identifier at *cpp
3996 * and advance the pointer to the next word.
3999 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
4008 /* Advance cp to the byte after the end of the name. */
4010 for (cp
= name
; 1; cp
++) {
4014 if (*cp
== ' ' || *cp
== '\t') {
4020 if (cp
[1] == '{' || cp
[1] == '}')
4024 mandoc_msg(MANDOCERR_NAMESC
, ln
, pos
,
4025 "%.*s", (int)(cp
- name
+ 1), name
);
4026 mandoc_escape((const char **)&cp
, NULL
, NULL
);
4030 /* Read past spaces. */
4040 * Store *string into the user-defined string called *name.
4041 * To clear an existing entry, call with (*r, *name, NULL, 0).
4042 * append == 0: replace mode
4043 * append == 1: single-line append mode
4044 * append == 2: multiline append mode, append '\n' after each call
4047 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
4052 namesz
= strlen(name
);
4053 roff_setstrn(&r
->strtab
, name
, namesz
, string
,
4054 string
? strlen(string
) : 0, append
);
4055 roff_setstrn(&r
->rentab
, name
, namesz
, NULL
, 0, 0);
4059 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
4060 const char *string
, size_t stringsz
, int append
)
4065 size_t oldch
, newch
;
4067 /* Search for an existing string with the same name. */
4070 while (n
&& (namesz
!= n
->key
.sz
||
4071 strncmp(n
->key
.p
, name
, namesz
)))
4075 /* Create a new string table entry. */
4076 n
= mandoc_malloc(sizeof(struct roffkv
));
4077 n
->key
.p
= mandoc_strndup(name
, namesz
);
4083 } else if (0 == append
) {
4093 * One additional byte for the '\n' in multiline mode,
4094 * and one for the terminating '\0'.
4096 newch
= stringsz
+ (1 < append
? 2u : 1u);
4098 if (NULL
== n
->val
.p
) {
4099 n
->val
.p
= mandoc_malloc(newch
);
4104 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
4107 /* Skip existing content in the destination buffer. */
4108 c
= n
->val
.p
+ (int)oldch
;
4110 /* Append new content to the destination buffer. */
4112 while (i
< (int)stringsz
) {
4114 * Rudimentary roff copy mode:
4115 * Handle escaped backslashes.
4117 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
4122 /* Append terminating bytes. */
4127 n
->val
.sz
= (int)(c
- n
->val
.p
);
4131 roff_getstrn(struct roff
*r
, const char *name
, size_t len
,
4134 const struct roffkv
*n
;
4139 for (n
= r
->strtab
; n
!= NULL
; n
= n
->next
) {
4140 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4141 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4143 if (*deftype
& ROFFDEF_USER
) {
4144 *deftype
= ROFFDEF_USER
;
4151 for (n
= r
->rentab
; n
!= NULL
; n
= n
->next
) {
4152 if (strncmp(name
, n
->key
.p
, len
) != 0 ||
4153 n
->key
.p
[len
] != '\0' || n
->val
.p
== NULL
)
4155 if (*deftype
& ROFFDEF_REN
) {
4156 *deftype
= ROFFDEF_REN
;
4163 for (i
= 0; i
< PREDEFS_MAX
; i
++) {
4164 if (strncmp(name
, predefs
[i
].name
, len
) != 0 ||
4165 predefs
[i
].name
[len
] != '\0')
4167 if (*deftype
& ROFFDEF_PRE
) {
4168 *deftype
= ROFFDEF_PRE
;
4169 return predefs
[i
].str
;
4175 if (r
->man
->meta
.macroset
!= MACROSET_MAN
) {
4176 for (tok
= MDOC_Dd
; tok
< MDOC_MAX
; tok
++) {
4177 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4178 roff_name
[tok
][len
] != '\0')
4180 if (*deftype
& ROFFDEF_STD
) {
4181 *deftype
= ROFFDEF_STD
;
4189 if (r
->man
->meta
.macroset
!= MACROSET_MDOC
) {
4190 for (tok
= MAN_TH
; tok
< MAN_MAX
; tok
++) {
4191 if (strncmp(name
, roff_name
[tok
], len
) != 0 ||
4192 roff_name
[tok
][len
] != '\0')
4194 if (*deftype
& ROFFDEF_STD
) {
4195 *deftype
= ROFFDEF_STD
;
4204 if (found
== 0 && *deftype
!= ROFFDEF_ANY
) {
4205 if (*deftype
& ROFFDEF_REN
) {
4207 * This might still be a request,
4208 * so do not treat it as undefined yet.
4210 *deftype
= ROFFDEF_UNDEF
;
4214 /* Using an undefined string defines it to be empty. */
4216 roff_setstrn(&r
->strtab
, name
, len
, "", 0, 0);
4217 roff_setstrn(&r
->rentab
, name
, len
, NULL
, 0, 0);
4225 roff_freestr(struct roffkv
*r
)
4227 struct roffkv
*n
, *nn
;
4229 for (n
= r
; n
; n
= nn
) {
4237 /* --- accessors and utility functions ------------------------------------ */
4240 * Duplicate an input string, making the appropriate character
4241 * conversations (as stipulated by `tr') along the way.
4242 * Returns a heap-allocated string with all the replacements made.
4245 roff_strdup(const struct roff
*r
, const char *p
)
4247 const struct roffkv
*cp
;
4251 enum mandoc_esc esc
;
4253 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
4254 return mandoc_strdup(p
);
4255 else if ('\0' == *p
)
4256 return mandoc_strdup("");
4259 * Step through each character looking for term matches
4260 * (remember that a `tr' can be invoked with an escape, which is
4261 * a glyph but the escape is multi-character).
4262 * We only do this if the character hash has been initialised
4263 * and the string is >0 length.
4269 while ('\0' != *p
) {
4270 assert((unsigned int)*p
< 128);
4271 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(unsigned int)*p
].p
) {
4272 sz
= r
->xtab
[(int)*p
].sz
;
4273 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4274 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
4278 } else if ('\\' != *p
) {
4279 res
= mandoc_realloc(res
, ssz
+ 2);
4284 /* Search for term matches. */
4285 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
4286 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
4291 * A match has been found.
4292 * Append the match to the array and move
4293 * forward by its keysize.
4295 res
= mandoc_realloc(res
,
4296 ssz
+ cp
->val
.sz
+ 1);
4297 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
4299 p
+= (int)cp
->key
.sz
;
4304 * Handle escapes carefully: we need to copy
4305 * over just the escape itself, or else we might
4306 * do replacements within the escape itself.
4307 * Make sure to pass along the bogus string.
4310 esc
= mandoc_escape(&p
, NULL
, NULL
);
4311 if (ESCAPE_ERROR
== esc
) {
4313 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4314 memcpy(res
+ ssz
, pp
, sz
);
4318 * We bail out on bad escapes.
4319 * No need to warn: we already did so when
4320 * roff_expand() was called.
4323 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
4324 memcpy(res
+ ssz
, pp
, sz
);
4328 res
[(int)ssz
] = '\0';
4333 roff_getformat(const struct roff
*r
)
4340 * Find out whether a line is a macro line or not.
4341 * If it is, adjust the current position and return one; if it isn't,
4342 * return zero and don't change the current position.
4343 * If the control character has been set with `.cc', then let that grain
4345 * This is slighly contrary to groff, where using the non-breaking
4346 * control character when `cc' has been invoked will cause the
4347 * non-breaking macro contents to be printed verbatim.
4350 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
4356 if (r
->control
!= '\0' && cp
[pos
] == r
->control
)
4358 else if (r
->control
!= '\0')
4360 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
4362 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
4367 while (' ' == cp
[pos
] || '\t' == cp
[pos
])