xref: /titanic_44/usr/src/cmd/mandoc/roff.c (revision 698f87a48e2e945bfe5493ce168e0d0ae1cedd5c)
1*698f87a4SGarrett D'Amore /*	$Id: roff.c,v 1.189 2013/12/30 18:44:06 schwarze Exp $ */
295c635efSGarrett D'Amore /*
3*698f87a4SGarrett D'Amore  * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4*698f87a4SGarrett D'Amore  * Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
595c635efSGarrett D'Amore  *
695c635efSGarrett D'Amore  * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore  * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore  * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore  *
1095c635efSGarrett D'Amore  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1195c635efSGarrett D'Amore  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1295c635efSGarrett D'Amore  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1395c635efSGarrett D'Amore  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1795c635efSGarrett D'Amore  */
1895c635efSGarrett D'Amore #ifdef HAVE_CONFIG_H
1995c635efSGarrett D'Amore #include "config.h"
2095c635efSGarrett D'Amore #endif
2195c635efSGarrett D'Amore 
2295c635efSGarrett D'Amore #include <assert.h>
2395c635efSGarrett D'Amore #include <ctype.h>
24*698f87a4SGarrett D'Amore #include <stdio.h>
2595c635efSGarrett D'Amore #include <stdlib.h>
2695c635efSGarrett D'Amore #include <string.h>
2795c635efSGarrett D'Amore 
2895c635efSGarrett D'Amore #include "mandoc.h"
2995c635efSGarrett D'Amore #include "libroff.h"
3095c635efSGarrett D'Amore #include "libmandoc.h"
3195c635efSGarrett D'Amore 
3295c635efSGarrett D'Amore /* Maximum number of nested if-else conditionals. */
3395c635efSGarrett D'Amore #define	RSTACK_MAX	128
3495c635efSGarrett D'Amore 
3595c635efSGarrett D'Amore /* Maximum number of string expansions per line, to break infinite loops. */
3695c635efSGarrett D'Amore #define	EXPAND_LIMIT	1000
3795c635efSGarrett D'Amore 
3895c635efSGarrett D'Amore enum	rofft {
3995c635efSGarrett D'Amore 	ROFF_ad,
4095c635efSGarrett D'Amore 	ROFF_am,
4195c635efSGarrett D'Amore 	ROFF_ami,
4295c635efSGarrett D'Amore 	ROFF_am1,
43*698f87a4SGarrett D'Amore 	ROFF_cc,
4495c635efSGarrett D'Amore 	ROFF_de,
4595c635efSGarrett D'Amore 	ROFF_dei,
4695c635efSGarrett D'Amore 	ROFF_de1,
4795c635efSGarrett D'Amore 	ROFF_ds,
4895c635efSGarrett D'Amore 	ROFF_el,
49*698f87a4SGarrett D'Amore 	ROFF_fam,
50*698f87a4SGarrett D'Amore 	ROFF_hw,
5195c635efSGarrett D'Amore 	ROFF_hy,
5295c635efSGarrett D'Amore 	ROFF_ie,
5395c635efSGarrett D'Amore 	ROFF_if,
5495c635efSGarrett D'Amore 	ROFF_ig,
5595c635efSGarrett D'Amore 	ROFF_it,
5695c635efSGarrett D'Amore 	ROFF_ne,
5795c635efSGarrett D'Amore 	ROFF_nh,
5895c635efSGarrett D'Amore 	ROFF_nr,
5995c635efSGarrett D'Amore 	ROFF_ns,
6095c635efSGarrett D'Amore 	ROFF_ps,
6195c635efSGarrett D'Amore 	ROFF_rm,
6295c635efSGarrett D'Amore 	ROFF_so,
6395c635efSGarrett D'Amore 	ROFF_ta,
6495c635efSGarrett D'Amore 	ROFF_tr,
65*698f87a4SGarrett D'Amore 	ROFF_Dd,
66*698f87a4SGarrett D'Amore 	ROFF_TH,
6795c635efSGarrett D'Amore 	ROFF_TS,
6895c635efSGarrett D'Amore 	ROFF_TE,
6995c635efSGarrett D'Amore 	ROFF_T_,
7095c635efSGarrett D'Amore 	ROFF_EQ,
7195c635efSGarrett D'Amore 	ROFF_EN,
7295c635efSGarrett D'Amore 	ROFF_cblock,
7395c635efSGarrett D'Amore 	ROFF_ccond,
7495c635efSGarrett D'Amore 	ROFF_USERDEF,
7595c635efSGarrett D'Amore 	ROFF_MAX
7695c635efSGarrett D'Amore };
7795c635efSGarrett D'Amore 
7895c635efSGarrett D'Amore enum	roffrule {
79*698f87a4SGarrett D'Amore 	ROFFRULE_DENY,
80*698f87a4SGarrett D'Amore 	ROFFRULE_ALLOW
8195c635efSGarrett D'Amore };
8295c635efSGarrett D'Amore 
8395c635efSGarrett D'Amore /*
8495c635efSGarrett D'Amore  * An incredibly-simple string buffer.
8595c635efSGarrett D'Amore  */
8695c635efSGarrett D'Amore struct	roffstr {
8795c635efSGarrett D'Amore 	char		*p; /* nil-terminated buffer */
8895c635efSGarrett D'Amore 	size_t		 sz; /* saved strlen(p) */
8995c635efSGarrett D'Amore };
9095c635efSGarrett D'Amore 
9195c635efSGarrett D'Amore /*
9295c635efSGarrett D'Amore  * A key-value roffstr pair as part of a singly-linked list.
9395c635efSGarrett D'Amore  */
9495c635efSGarrett D'Amore struct	roffkv {
9595c635efSGarrett D'Amore 	struct roffstr	 key;
9695c635efSGarrett D'Amore 	struct roffstr	 val;
9795c635efSGarrett D'Amore 	struct roffkv	*next; /* next in list */
9895c635efSGarrett D'Amore };
9995c635efSGarrett D'Amore 
100*698f87a4SGarrett D'Amore /*
101*698f87a4SGarrett D'Amore  * A single number register as part of a singly-linked list.
102*698f87a4SGarrett D'Amore  */
103*698f87a4SGarrett D'Amore struct	roffreg {
104*698f87a4SGarrett D'Amore 	struct roffstr	 key;
105*698f87a4SGarrett D'Amore 	int		 val;
106*698f87a4SGarrett D'Amore 	struct roffreg	*next;
107*698f87a4SGarrett D'Amore };
108*698f87a4SGarrett D'Amore 
10995c635efSGarrett D'Amore struct	roff {
110*698f87a4SGarrett D'Amore 	enum mparset	 parsetype; /* requested parse type */
11195c635efSGarrett D'Amore 	struct mparse	*parse; /* parse point */
11295c635efSGarrett D'Amore 	struct roffnode	*last; /* leaf of stack */
11395c635efSGarrett D'Amore 	enum roffrule	 rstack[RSTACK_MAX]; /* stack of !`ie' rules */
114*698f87a4SGarrett D'Amore 	char		 control; /* control character */
11595c635efSGarrett D'Amore 	int		 rstackpos; /* position in rstack */
116*698f87a4SGarrett D'Amore 	struct roffreg	*regtab; /* number registers */
11795c635efSGarrett D'Amore 	struct roffkv	*strtab; /* user-defined strings & macros */
11895c635efSGarrett D'Amore 	struct roffkv	*xmbtab; /* multi-byte trans table (`tr') */
11995c635efSGarrett D'Amore 	struct roffstr	*xtab; /* single-byte trans table (`tr') */
12095c635efSGarrett D'Amore 	const char	*current_string; /* value of last called user macro */
12195c635efSGarrett D'Amore 	struct tbl_node	*first_tbl; /* first table parsed */
12295c635efSGarrett D'Amore 	struct tbl_node	*last_tbl; /* last table parsed */
12395c635efSGarrett D'Amore 	struct tbl_node	*tbl; /* current table being parsed */
12495c635efSGarrett D'Amore 	struct eqn_node	*last_eqn; /* last equation parsed */
12595c635efSGarrett D'Amore 	struct eqn_node	*first_eqn; /* first equation parsed */
12695c635efSGarrett D'Amore 	struct eqn_node	*eqn; /* current equation being parsed */
12795c635efSGarrett D'Amore };
12895c635efSGarrett D'Amore 
12995c635efSGarrett D'Amore struct	roffnode {
13095c635efSGarrett D'Amore 	enum rofft	 tok; /* type of node */
13195c635efSGarrett D'Amore 	struct roffnode	*parent; /* up one in stack */
13295c635efSGarrett D'Amore 	int		 line; /* parse line */
13395c635efSGarrett D'Amore 	int		 col; /* parse col */
13495c635efSGarrett D'Amore 	char		*name; /* node name, e.g. macro name */
13595c635efSGarrett D'Amore 	char		*end; /* end-rules: custom token */
13695c635efSGarrett D'Amore 	int		 endspan; /* end-rules: next-line or infty */
13795c635efSGarrett D'Amore 	enum roffrule	 rule; /* current evaluation rule */
13895c635efSGarrett D'Amore };
13995c635efSGarrett D'Amore 
14095c635efSGarrett D'Amore #define	ROFF_ARGS	 struct roff *r, /* parse ctx */ \
14195c635efSGarrett D'Amore 			 enum rofft tok, /* tok of macro */ \
14295c635efSGarrett D'Amore 		 	 char **bufp, /* input buffer */ \
14395c635efSGarrett D'Amore 			 size_t *szp, /* size of input buffer */ \
14495c635efSGarrett D'Amore 			 int ln, /* parse line */ \
14595c635efSGarrett D'Amore 			 int ppos, /* original pos in buffer */ \
14695c635efSGarrett D'Amore 			 int pos, /* current pos in buffer */ \
14795c635efSGarrett D'Amore 			 int *offs /* reset offset of buffer data */
14895c635efSGarrett D'Amore 
14995c635efSGarrett D'Amore typedef	enum rofferr (*roffproc)(ROFF_ARGS);
15095c635efSGarrett D'Amore 
15195c635efSGarrett D'Amore struct	roffmac {
15295c635efSGarrett D'Amore 	const char	*name; /* macro name */
15395c635efSGarrett D'Amore 	roffproc	 proc; /* process new macro */
15495c635efSGarrett D'Amore 	roffproc	 text; /* process as child text of macro */
15595c635efSGarrett D'Amore 	roffproc	 sub; /* process as child of macro */
15695c635efSGarrett D'Amore 	int		 flags;
15795c635efSGarrett D'Amore #define	ROFFMAC_STRUCT	(1 << 0) /* always interpret */
15895c635efSGarrett D'Amore 	struct roffmac	*next;
15995c635efSGarrett D'Amore };
16095c635efSGarrett D'Amore 
16195c635efSGarrett D'Amore struct	predef {
16295c635efSGarrett D'Amore 	const char	*name; /* predefined input name */
16395c635efSGarrett D'Amore 	const char	*str; /* replacement symbol */
16495c635efSGarrett D'Amore };
16595c635efSGarrett D'Amore 
16695c635efSGarrett D'Amore #define	PREDEF(__name, __str) \
16795c635efSGarrett D'Amore 	{ (__name), (__str) },
16895c635efSGarrett D'Amore 
16995c635efSGarrett D'Amore static	enum rofft	 roffhash_find(const char *, size_t);
17095c635efSGarrett D'Amore static	void		 roffhash_init(void);
17195c635efSGarrett D'Amore static	void		 roffnode_cleanscope(struct roff *);
17295c635efSGarrett D'Amore static	void		 roffnode_pop(struct roff *);
17395c635efSGarrett D'Amore static	void		 roffnode_push(struct roff *, enum rofft,
17495c635efSGarrett D'Amore 				const char *, int, int);
17595c635efSGarrett D'Amore static	enum rofferr	 roff_block(ROFF_ARGS);
17695c635efSGarrett D'Amore static	enum rofferr	 roff_block_text(ROFF_ARGS);
17795c635efSGarrett D'Amore static	enum rofferr	 roff_block_sub(ROFF_ARGS);
17895c635efSGarrett D'Amore static	enum rofferr	 roff_cblock(ROFF_ARGS);
179*698f87a4SGarrett D'Amore static	enum rofferr	 roff_cc(ROFF_ARGS);
18095c635efSGarrett D'Amore static	enum rofferr	 roff_ccond(ROFF_ARGS);
18195c635efSGarrett D'Amore static	enum rofferr	 roff_cond(ROFF_ARGS);
18295c635efSGarrett D'Amore static	enum rofferr	 roff_cond_text(ROFF_ARGS);
18395c635efSGarrett D'Amore static	enum rofferr	 roff_cond_sub(ROFF_ARGS);
18495c635efSGarrett D'Amore static	enum rofferr	 roff_ds(ROFF_ARGS);
18595c635efSGarrett D'Amore static	enum roffrule	 roff_evalcond(const char *, int *);
18695c635efSGarrett D'Amore static	void		 roff_free1(struct roff *);
187*698f87a4SGarrett D'Amore static	void		 roff_freereg(struct roffreg *);
18895c635efSGarrett D'Amore static	void		 roff_freestr(struct roffkv *);
18995c635efSGarrett D'Amore static	char		*roff_getname(struct roff *, char **, int, int);
190*698f87a4SGarrett D'Amore static	int		 roff_getnum(const char *, int *, int *);
191*698f87a4SGarrett D'Amore static	int		 roff_getop(const char *, int *, char *);
192*698f87a4SGarrett D'Amore static	int		 roff_getregn(const struct roff *,
193*698f87a4SGarrett D'Amore 				const char *, size_t);
19495c635efSGarrett D'Amore static	const char	*roff_getstrn(const struct roff *,
19595c635efSGarrett D'Amore 				const char *, size_t);
196*698f87a4SGarrett D'Amore static	enum rofferr	 roff_it(ROFF_ARGS);
19795c635efSGarrett D'Amore static	enum rofferr	 roff_line_ignore(ROFF_ARGS);
19895c635efSGarrett D'Amore static	enum rofferr	 roff_nr(ROFF_ARGS);
19995c635efSGarrett D'Amore static	void		 roff_openeqn(struct roff *, const char *,
20095c635efSGarrett D'Amore 				int, int, const char *);
20195c635efSGarrett D'Amore static	enum rofft	 roff_parse(struct roff *, const char *, int *);
202*698f87a4SGarrett D'Amore static	enum rofferr	 roff_parsetext(char **, size_t *, int, int *);
20395c635efSGarrett D'Amore static	enum rofferr	 roff_res(struct roff *,
20495c635efSGarrett D'Amore 				char **, size_t *, int, int);
20595c635efSGarrett D'Amore static	enum rofferr	 roff_rm(ROFF_ARGS);
20695c635efSGarrett D'Amore static	void		 roff_setstr(struct roff *,
20795c635efSGarrett D'Amore 				const char *, const char *, int);
20895c635efSGarrett D'Amore static	void		 roff_setstrn(struct roffkv **, const char *,
20995c635efSGarrett D'Amore 				size_t, const char *, size_t, int);
21095c635efSGarrett D'Amore static	enum rofferr	 roff_so(ROFF_ARGS);
21195c635efSGarrett D'Amore static	enum rofferr	 roff_tr(ROFF_ARGS);
212*698f87a4SGarrett D'Amore static	enum rofferr	 roff_Dd(ROFF_ARGS);
213*698f87a4SGarrett D'Amore static	enum rofferr	 roff_TH(ROFF_ARGS);
21495c635efSGarrett D'Amore static	enum rofferr	 roff_TE(ROFF_ARGS);
21595c635efSGarrett D'Amore static	enum rofferr	 roff_TS(ROFF_ARGS);
21695c635efSGarrett D'Amore static	enum rofferr	 roff_EQ(ROFF_ARGS);
21795c635efSGarrett D'Amore static	enum rofferr	 roff_EN(ROFF_ARGS);
21895c635efSGarrett D'Amore static	enum rofferr	 roff_T_(ROFF_ARGS);
21995c635efSGarrett D'Amore static	enum rofferr	 roff_userdef(ROFF_ARGS);
22095c635efSGarrett D'Amore 
22195c635efSGarrett D'Amore /* See roffhash_find() */
22295c635efSGarrett D'Amore 
22395c635efSGarrett D'Amore #define	ASCII_HI	 126
22495c635efSGarrett D'Amore #define	ASCII_LO	 33
22595c635efSGarrett D'Amore #define	HASHWIDTH	(ASCII_HI - ASCII_LO + 1)
22695c635efSGarrett D'Amore 
22795c635efSGarrett D'Amore static	struct roffmac	*hash[HASHWIDTH];
22895c635efSGarrett D'Amore 
22995c635efSGarrett D'Amore static	struct roffmac	 roffs[ROFF_MAX] = {
23095c635efSGarrett D'Amore 	{ "ad", roff_line_ignore, NULL, NULL, 0, NULL },
23195c635efSGarrett D'Amore 	{ "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
23295c635efSGarrett D'Amore 	{ "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
23395c635efSGarrett D'Amore 	{ "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
234*698f87a4SGarrett D'Amore 	{ "cc", roff_cc, NULL, NULL, 0, NULL },
23595c635efSGarrett D'Amore 	{ "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
23695c635efSGarrett D'Amore 	{ "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
23795c635efSGarrett D'Amore 	{ "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
23895c635efSGarrett D'Amore 	{ "ds", roff_ds, NULL, NULL, 0, NULL },
23995c635efSGarrett D'Amore 	{ "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
240*698f87a4SGarrett D'Amore 	{ "fam", roff_line_ignore, NULL, NULL, 0, NULL },
241*698f87a4SGarrett D'Amore 	{ "hw", roff_line_ignore, NULL, NULL, 0, NULL },
24295c635efSGarrett D'Amore 	{ "hy", roff_line_ignore, NULL, NULL, 0, NULL },
24395c635efSGarrett D'Amore 	{ "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
24495c635efSGarrett D'Amore 	{ "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
24595c635efSGarrett D'Amore 	{ "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
246*698f87a4SGarrett D'Amore 	{ "it", roff_it, NULL, NULL, 0, NULL },
24795c635efSGarrett D'Amore 	{ "ne", roff_line_ignore, NULL, NULL, 0, NULL },
24895c635efSGarrett D'Amore 	{ "nh", roff_line_ignore, NULL, NULL, 0, NULL },
24995c635efSGarrett D'Amore 	{ "nr", roff_nr, NULL, NULL, 0, NULL },
25095c635efSGarrett D'Amore 	{ "ns", roff_line_ignore, NULL, NULL, 0, NULL },
25195c635efSGarrett D'Amore 	{ "ps", roff_line_ignore, NULL, NULL, 0, NULL },
25295c635efSGarrett D'Amore 	{ "rm", roff_rm, NULL, NULL, 0, NULL },
25395c635efSGarrett D'Amore 	{ "so", roff_so, NULL, NULL, 0, NULL },
25495c635efSGarrett D'Amore 	{ "ta", roff_line_ignore, NULL, NULL, 0, NULL },
25595c635efSGarrett D'Amore 	{ "tr", roff_tr, NULL, NULL, 0, NULL },
256*698f87a4SGarrett D'Amore 	{ "Dd", roff_Dd, NULL, NULL, 0, NULL },
257*698f87a4SGarrett D'Amore 	{ "TH", roff_TH, NULL, NULL, 0, NULL },
25895c635efSGarrett D'Amore 	{ "TS", roff_TS, NULL, NULL, 0, NULL },
25995c635efSGarrett D'Amore 	{ "TE", roff_TE, NULL, NULL, 0, NULL },
26095c635efSGarrett D'Amore 	{ "T&", roff_T_, NULL, NULL, 0, NULL },
26195c635efSGarrett D'Amore 	{ "EQ", roff_EQ, NULL, NULL, 0, NULL },
26295c635efSGarrett D'Amore 	{ "EN", roff_EN, NULL, NULL, 0, NULL },
26395c635efSGarrett D'Amore 	{ ".", roff_cblock, NULL, NULL, 0, NULL },
26495c635efSGarrett D'Amore 	{ "\\}", roff_ccond, NULL, NULL, 0, NULL },
26595c635efSGarrett D'Amore 	{ NULL, roff_userdef, NULL, NULL, 0, NULL },
26695c635efSGarrett D'Amore };
26795c635efSGarrett D'Amore 
268*698f87a4SGarrett D'Amore const	char *const __mdoc_reserved[] = {
269*698f87a4SGarrett D'Amore 	"Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
270*698f87a4SGarrett D'Amore 	"Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
271*698f87a4SGarrett D'Amore 	"Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
272*698f87a4SGarrett D'Amore 	"Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
273*698f87a4SGarrett D'Amore 	"Ds", "Dt", "Dv", "Dx", "D1",
274*698f87a4SGarrett D'Amore 	"Ec", "Ed", "Ef", "Ek", "El", "Em", "em",
275*698f87a4SGarrett D'Amore 	"En", "Eo", "Eq", "Er", "Es", "Ev", "Ex",
276*698f87a4SGarrett D'Amore 	"Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
277*698f87a4SGarrett D'Amore 	"Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp", "LP",
278*698f87a4SGarrett D'Amore 	"Me", "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
279*698f87a4SGarrett D'Amore 	"Oc", "Oo", "Op", "Os", "Ot", "Ox",
280*698f87a4SGarrett D'Amore 	"Pa", "Pc", "Pf", "Po", "Pp", "PP", "pp", "Pq",
281*698f87a4SGarrett D'Amore 	"Qc", "Ql", "Qo", "Qq", "Or", "Rd", "Re", "Rs", "Rv",
282*698f87a4SGarrett D'Amore 	"Sc", "Sf", "Sh", "SH", "Sm", "So", "Sq",
283*698f87a4SGarrett D'Amore 	"Ss", "St", "Sx", "Sy",
284*698f87a4SGarrett D'Amore 	"Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
285*698f87a4SGarrett D'Amore 	"%A", "%B", "%D", "%I", "%J", "%N", "%O",
286*698f87a4SGarrett D'Amore 	"%P", "%Q", "%R", "%T", "%U", "%V",
287*698f87a4SGarrett D'Amore 	NULL
288*698f87a4SGarrett D'Amore };
289*698f87a4SGarrett D'Amore 
290*698f87a4SGarrett D'Amore const	char *const __man_reserved[] = {
291*698f87a4SGarrett D'Amore 	"AT", "B", "BI", "BR", "BT", "DE", "DS", "DT",
292*698f87a4SGarrett D'Amore 	"EE", "EN", "EQ", "EX", "HF", "HP", "I", "IB", "IP", "IR",
293*698f87a4SGarrett D'Amore 	"LP", "ME", "MT", "OP", "P", "PD", "PP", "PT",
294*698f87a4SGarrett D'Amore 	"R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS", "SY",
295*698f87a4SGarrett D'Amore 	"TE", "TH", "TP", "TQ", "TS", "T&", "UC", "UE", "UR", "YS",
296*698f87a4SGarrett D'Amore 	NULL
297*698f87a4SGarrett D'Amore };
298*698f87a4SGarrett D'Amore 
29995c635efSGarrett D'Amore /* Array of injected predefined strings. */
30095c635efSGarrett D'Amore #define	PREDEFS_MAX	 38
30195c635efSGarrett D'Amore static	const struct predef predefs[PREDEFS_MAX] = {
30295c635efSGarrett D'Amore #include "predefs.in"
30395c635efSGarrett D'Amore };
30495c635efSGarrett D'Amore 
30595c635efSGarrett D'Amore /* See roffhash_find() */
30695c635efSGarrett D'Amore #define	ROFF_HASH(p)	(p[0] - ASCII_LO)
30795c635efSGarrett D'Amore 
308*698f87a4SGarrett D'Amore static	int	 roffit_lines;  /* number of lines to delay */
309*698f87a4SGarrett D'Amore static	char	*roffit_macro;  /* nil-terminated macro line */
310*698f87a4SGarrett D'Amore 
31195c635efSGarrett D'Amore static void
roffhash_init(void)31295c635efSGarrett D'Amore roffhash_init(void)
31395c635efSGarrett D'Amore {
31495c635efSGarrett D'Amore 	struct roffmac	 *n;
31595c635efSGarrett D'Amore 	int		  buc, i;
31695c635efSGarrett D'Amore 
31795c635efSGarrett D'Amore 	for (i = 0; i < (int)ROFF_USERDEF; i++) {
31895c635efSGarrett D'Amore 		assert(roffs[i].name[0] >= ASCII_LO);
31995c635efSGarrett D'Amore 		assert(roffs[i].name[0] <= ASCII_HI);
32095c635efSGarrett D'Amore 
32195c635efSGarrett D'Amore 		buc = ROFF_HASH(roffs[i].name);
32295c635efSGarrett D'Amore 
32395c635efSGarrett D'Amore 		if (NULL != (n = hash[buc])) {
32495c635efSGarrett D'Amore 			for ( ; n->next; n = n->next)
32595c635efSGarrett D'Amore 				/* Do nothing. */ ;
32695c635efSGarrett D'Amore 			n->next = &roffs[i];
32795c635efSGarrett D'Amore 		} else
32895c635efSGarrett D'Amore 			hash[buc] = &roffs[i];
32995c635efSGarrett D'Amore 	}
33095c635efSGarrett D'Amore }
33195c635efSGarrett D'Amore 
33295c635efSGarrett D'Amore /*
33395c635efSGarrett D'Amore  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
33495c635efSGarrett D'Amore  * the nil-terminated string name could be found.
33595c635efSGarrett D'Amore  */
33695c635efSGarrett D'Amore static enum rofft
roffhash_find(const char * p,size_t s)33795c635efSGarrett D'Amore roffhash_find(const char *p, size_t s)
33895c635efSGarrett D'Amore {
33995c635efSGarrett D'Amore 	int		 buc;
34095c635efSGarrett D'Amore 	struct roffmac	*n;
34195c635efSGarrett D'Amore 
34295c635efSGarrett D'Amore 	/*
34395c635efSGarrett D'Amore 	 * libroff has an extremely simple hashtable, for the time
34495c635efSGarrett D'Amore 	 * being, which simply keys on the first character, which must
34595c635efSGarrett D'Amore 	 * be printable, then walks a chain.  It works well enough until
34695c635efSGarrett D'Amore 	 * optimised.
34795c635efSGarrett D'Amore 	 */
34895c635efSGarrett D'Amore 
34995c635efSGarrett D'Amore 	if (p[0] < ASCII_LO || p[0] > ASCII_HI)
35095c635efSGarrett D'Amore 		return(ROFF_MAX);
35195c635efSGarrett D'Amore 
35295c635efSGarrett D'Amore 	buc = ROFF_HASH(p);
35395c635efSGarrett D'Amore 
35495c635efSGarrett D'Amore 	if (NULL == (n = hash[buc]))
35595c635efSGarrett D'Amore 		return(ROFF_MAX);
35695c635efSGarrett D'Amore 	for ( ; n; n = n->next)
35795c635efSGarrett D'Amore 		if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
35895c635efSGarrett D'Amore 			return((enum rofft)(n - roffs));
35995c635efSGarrett D'Amore 
36095c635efSGarrett D'Amore 	return(ROFF_MAX);
36195c635efSGarrett D'Amore }
36295c635efSGarrett D'Amore 
36395c635efSGarrett D'Amore 
36495c635efSGarrett D'Amore /*
36595c635efSGarrett D'Amore  * Pop the current node off of the stack of roff instructions currently
36695c635efSGarrett D'Amore  * pending.
36795c635efSGarrett D'Amore  */
36895c635efSGarrett D'Amore static void
roffnode_pop(struct roff * r)36995c635efSGarrett D'Amore roffnode_pop(struct roff *r)
37095c635efSGarrett D'Amore {
37195c635efSGarrett D'Amore 	struct roffnode	*p;
37295c635efSGarrett D'Amore 
37395c635efSGarrett D'Amore 	assert(r->last);
37495c635efSGarrett D'Amore 	p = r->last;
37595c635efSGarrett D'Amore 
37695c635efSGarrett D'Amore 	r->last = r->last->parent;
37795c635efSGarrett D'Amore 	free(p->name);
37895c635efSGarrett D'Amore 	free(p->end);
37995c635efSGarrett D'Amore 	free(p);
38095c635efSGarrett D'Amore }
38195c635efSGarrett D'Amore 
38295c635efSGarrett D'Amore 
38395c635efSGarrett D'Amore /*
38495c635efSGarrett D'Amore  * Push a roff node onto the instruction stack.  This must later be
38595c635efSGarrett D'Amore  * removed with roffnode_pop().
38695c635efSGarrett D'Amore  */
38795c635efSGarrett D'Amore static void
roffnode_push(struct roff * r,enum rofft tok,const char * name,int line,int col)38895c635efSGarrett D'Amore roffnode_push(struct roff *r, enum rofft tok, const char *name,
38995c635efSGarrett D'Amore 		int line, int col)
39095c635efSGarrett D'Amore {
39195c635efSGarrett D'Amore 	struct roffnode	*p;
39295c635efSGarrett D'Amore 
39395c635efSGarrett D'Amore 	p = mandoc_calloc(1, sizeof(struct roffnode));
39495c635efSGarrett D'Amore 	p->tok = tok;
39595c635efSGarrett D'Amore 	if (name)
39695c635efSGarrett D'Amore 		p->name = mandoc_strdup(name);
39795c635efSGarrett D'Amore 	p->parent = r->last;
39895c635efSGarrett D'Amore 	p->line = line;
39995c635efSGarrett D'Amore 	p->col = col;
40095c635efSGarrett D'Amore 	p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
40195c635efSGarrett D'Amore 
40295c635efSGarrett D'Amore 	r->last = p;
40395c635efSGarrett D'Amore }
40495c635efSGarrett D'Amore 
40595c635efSGarrett D'Amore 
40695c635efSGarrett D'Amore static void
roff_free1(struct roff * r)40795c635efSGarrett D'Amore roff_free1(struct roff *r)
40895c635efSGarrett D'Amore {
409*698f87a4SGarrett D'Amore 	struct tbl_node	*tbl;
41095c635efSGarrett D'Amore 	struct eqn_node	*e;
41195c635efSGarrett D'Amore 	int		 i;
41295c635efSGarrett D'Amore 
413*698f87a4SGarrett D'Amore 	while (NULL != (tbl = r->first_tbl)) {
414*698f87a4SGarrett D'Amore 		r->first_tbl = tbl->next;
415*698f87a4SGarrett D'Amore 		tbl_free(tbl);
41695c635efSGarrett D'Amore 	}
41795c635efSGarrett D'Amore 
41895c635efSGarrett D'Amore 	r->first_tbl = r->last_tbl = r->tbl = NULL;
41995c635efSGarrett D'Amore 
42095c635efSGarrett D'Amore 	while (NULL != (e = r->first_eqn)) {
42195c635efSGarrett D'Amore 		r->first_eqn = e->next;
42295c635efSGarrett D'Amore 		eqn_free(e);
42395c635efSGarrett D'Amore 	}
42495c635efSGarrett D'Amore 
42595c635efSGarrett D'Amore 	r->first_eqn = r->last_eqn = r->eqn = NULL;
42695c635efSGarrett D'Amore 
42795c635efSGarrett D'Amore 	while (r->last)
42895c635efSGarrett D'Amore 		roffnode_pop(r);
42995c635efSGarrett D'Amore 
43095c635efSGarrett D'Amore 	roff_freestr(r->strtab);
43195c635efSGarrett D'Amore 	roff_freestr(r->xmbtab);
43295c635efSGarrett D'Amore 
43395c635efSGarrett D'Amore 	r->strtab = r->xmbtab = NULL;
43495c635efSGarrett D'Amore 
435*698f87a4SGarrett D'Amore 	roff_freereg(r->regtab);
436*698f87a4SGarrett D'Amore 
437*698f87a4SGarrett D'Amore 	r->regtab = NULL;
438*698f87a4SGarrett D'Amore 
43995c635efSGarrett D'Amore 	if (r->xtab)
44095c635efSGarrett D'Amore 		for (i = 0; i < 128; i++)
44195c635efSGarrett D'Amore 			free(r->xtab[i].p);
44295c635efSGarrett D'Amore 
44395c635efSGarrett D'Amore 	free(r->xtab);
44495c635efSGarrett D'Amore 	r->xtab = NULL;
44595c635efSGarrett D'Amore }
44695c635efSGarrett D'Amore 
44795c635efSGarrett D'Amore void
roff_reset(struct roff * r)44895c635efSGarrett D'Amore roff_reset(struct roff *r)
44995c635efSGarrett D'Amore {
45095c635efSGarrett D'Amore 	int		 i;
45195c635efSGarrett D'Amore 
45295c635efSGarrett D'Amore 	roff_free1(r);
45395c635efSGarrett D'Amore 
454*698f87a4SGarrett D'Amore 	r->control = 0;
45595c635efSGarrett D'Amore 
45695c635efSGarrett D'Amore 	for (i = 0; i < PREDEFS_MAX; i++)
45795c635efSGarrett D'Amore 		roff_setstr(r, predefs[i].name, predefs[i].str, 0);
45895c635efSGarrett D'Amore }
45995c635efSGarrett D'Amore 
46095c635efSGarrett D'Amore 
46195c635efSGarrett D'Amore void
roff_free(struct roff * r)46295c635efSGarrett D'Amore roff_free(struct roff *r)
46395c635efSGarrett D'Amore {
46495c635efSGarrett D'Amore 
46595c635efSGarrett D'Amore 	roff_free1(r);
46695c635efSGarrett D'Amore 	free(r);
46795c635efSGarrett D'Amore }
46895c635efSGarrett D'Amore 
46995c635efSGarrett D'Amore 
47095c635efSGarrett D'Amore struct roff *
roff_alloc(enum mparset type,struct mparse * parse)471*698f87a4SGarrett D'Amore roff_alloc(enum mparset type, struct mparse *parse)
47295c635efSGarrett D'Amore {
47395c635efSGarrett D'Amore 	struct roff	*r;
47495c635efSGarrett D'Amore 	int		 i;
47595c635efSGarrett D'Amore 
47695c635efSGarrett D'Amore 	r = mandoc_calloc(1, sizeof(struct roff));
477*698f87a4SGarrett D'Amore 	r->parsetype = type;
47895c635efSGarrett D'Amore 	r->parse = parse;
47995c635efSGarrett D'Amore 	r->rstackpos = -1;
48095c635efSGarrett D'Amore 
48195c635efSGarrett D'Amore 	roffhash_init();
48295c635efSGarrett D'Amore 
48395c635efSGarrett D'Amore 	for (i = 0; i < PREDEFS_MAX; i++)
48495c635efSGarrett D'Amore 		roff_setstr(r, predefs[i].name, predefs[i].str, 0);
48595c635efSGarrett D'Amore 
48695c635efSGarrett D'Amore 	return(r);
48795c635efSGarrett D'Amore }
48895c635efSGarrett D'Amore 
48995c635efSGarrett D'Amore /*
490*698f87a4SGarrett D'Amore  * In the current line, expand user-defined strings ("\*")
491*698f87a4SGarrett D'Amore  * and references to number registers ("\n").
492*698f87a4SGarrett D'Amore  * Also check the syntax of other escape sequences.
49395c635efSGarrett D'Amore  */
49495c635efSGarrett D'Amore static enum rofferr
roff_res(struct roff * r,char ** bufp,size_t * szp,int ln,int pos)49595c635efSGarrett D'Amore roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
49695c635efSGarrett D'Amore {
497*698f87a4SGarrett D'Amore 	char		 ubuf[12]; /* buffer to print the number */
49895c635efSGarrett D'Amore 	const char	*stesc;	/* start of an escape sequence ('\\') */
49995c635efSGarrett D'Amore 	const char	*stnam;	/* start of the name, after "[(*" */
50095c635efSGarrett D'Amore 	const char	*cp;	/* end of the name, e.g. before ']' */
50195c635efSGarrett D'Amore 	const char	*res;	/* the string to be substituted */
502*698f87a4SGarrett D'Amore 	char		*nbuf;	/* new buffer to copy bufp to */
503*698f87a4SGarrett D'Amore 	size_t		 nsz;	/* size of the new buffer */
504*698f87a4SGarrett D'Amore 	size_t		 maxl;  /* expected length of the escape name */
505*698f87a4SGarrett D'Amore 	size_t		 naml;	/* actual length of the escape name */
506*698f87a4SGarrett D'Amore 	int		 expand_count;	/* to avoid infinite loops */
50795c635efSGarrett D'Amore 
50895c635efSGarrett D'Amore 	expand_count = 0;
50995c635efSGarrett D'Amore 
51095c635efSGarrett D'Amore again:
51195c635efSGarrett D'Amore 	cp = *bufp + pos;
51295c635efSGarrett D'Amore 	while (NULL != (cp = strchr(cp, '\\'))) {
51395c635efSGarrett D'Amore 		stesc = cp++;
51495c635efSGarrett D'Amore 
51595c635efSGarrett D'Amore 		/*
516*698f87a4SGarrett D'Amore 		 * The second character must be an asterisk or an n.
51795c635efSGarrett D'Amore 		 * If it isn't, skip it anyway:  It is escaped,
51895c635efSGarrett D'Amore 		 * so it can't start another escape sequence.
51995c635efSGarrett D'Amore 		 */
52095c635efSGarrett D'Amore 
52195c635efSGarrett D'Amore 		if ('\0' == *cp)
52295c635efSGarrett D'Amore 			return(ROFF_CONT);
52395c635efSGarrett D'Amore 
524*698f87a4SGarrett D'Amore 		switch (*cp) {
525*698f87a4SGarrett D'Amore 		case ('*'):
526*698f87a4SGarrett D'Amore 			res = NULL;
527*698f87a4SGarrett D'Amore 			break;
528*698f87a4SGarrett D'Amore 		case ('n'):
529*698f87a4SGarrett D'Amore 			res = ubuf;
530*698f87a4SGarrett D'Amore 			break;
531*698f87a4SGarrett D'Amore 		default:
532*698f87a4SGarrett D'Amore 			if (ESCAPE_ERROR != mandoc_escape(&cp, NULL, NULL))
53395c635efSGarrett D'Amore 				continue;
53495c635efSGarrett D'Amore 			mandoc_msg
53595c635efSGarrett D'Amore 				(MANDOCERR_BADESCAPE, r->parse,
53695c635efSGarrett D'Amore 				 ln, (int)(stesc - *bufp), NULL);
53795c635efSGarrett D'Amore 			return(ROFF_CONT);
53895c635efSGarrett D'Amore 		}
53995c635efSGarrett D'Amore 
54095c635efSGarrett D'Amore 		cp++;
54195c635efSGarrett D'Amore 
54295c635efSGarrett D'Amore 		/*
54395c635efSGarrett D'Amore 		 * The third character decides the length
544*698f87a4SGarrett D'Amore 		 * of the name of the string or register.
54595c635efSGarrett D'Amore 		 * Save a pointer to the name.
54695c635efSGarrett D'Amore 		 */
54795c635efSGarrett D'Amore 
54895c635efSGarrett D'Amore 		switch (*cp) {
54995c635efSGarrett D'Amore 		case ('\0'):
55095c635efSGarrett D'Amore 			return(ROFF_CONT);
55195c635efSGarrett D'Amore 		case ('('):
55295c635efSGarrett D'Amore 			cp++;
55395c635efSGarrett D'Amore 			maxl = 2;
55495c635efSGarrett D'Amore 			break;
55595c635efSGarrett D'Amore 		case ('['):
55695c635efSGarrett D'Amore 			cp++;
55795c635efSGarrett D'Amore 			maxl = 0;
55895c635efSGarrett D'Amore 			break;
55995c635efSGarrett D'Amore 		default:
56095c635efSGarrett D'Amore 			maxl = 1;
56195c635efSGarrett D'Amore 			break;
56295c635efSGarrett D'Amore 		}
56395c635efSGarrett D'Amore 		stnam = cp;
56495c635efSGarrett D'Amore 
56595c635efSGarrett D'Amore 		/* Advance to the end of the name. */
56695c635efSGarrett D'Amore 
567*698f87a4SGarrett D'Amore 		for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
56895c635efSGarrett D'Amore 			if ('\0' == *cp) {
56995c635efSGarrett D'Amore 				mandoc_msg
57095c635efSGarrett D'Amore 					(MANDOCERR_BADESCAPE,
57195c635efSGarrett D'Amore 					 r->parse, ln,
57295c635efSGarrett D'Amore 					 (int)(stesc - *bufp), NULL);
57395c635efSGarrett D'Amore 				return(ROFF_CONT);
57495c635efSGarrett D'Amore 			}
57595c635efSGarrett D'Amore 			if (0 == maxl && ']' == *cp)
57695c635efSGarrett D'Amore 				break;
57795c635efSGarrett D'Amore 		}
57895c635efSGarrett D'Amore 
57995c635efSGarrett D'Amore 		/*
58095c635efSGarrett D'Amore 		 * Retrieve the replacement string; if it is
58195c635efSGarrett D'Amore 		 * undefined, resume searching for escapes.
58295c635efSGarrett D'Amore 		 */
58395c635efSGarrett D'Amore 
584*698f87a4SGarrett D'Amore 		if (NULL == res)
585*698f87a4SGarrett D'Amore 			res = roff_getstrn(r, stnam, naml);
586*698f87a4SGarrett D'Amore 		else
587*698f87a4SGarrett D'Amore 			snprintf(ubuf, sizeof(ubuf), "%d",
588*698f87a4SGarrett D'Amore 			    roff_getregn(r, stnam, naml));
58995c635efSGarrett D'Amore 
59095c635efSGarrett D'Amore 		if (NULL == res) {
59195c635efSGarrett D'Amore 			mandoc_msg
59295c635efSGarrett D'Amore 				(MANDOCERR_BADESCAPE, r->parse,
59395c635efSGarrett D'Amore 				 ln, (int)(stesc - *bufp), NULL);
59495c635efSGarrett D'Amore 			res = "";
59595c635efSGarrett D'Amore 		}
59695c635efSGarrett D'Amore 
59795c635efSGarrett D'Amore 		/* Replace the escape sequence by the string. */
59895c635efSGarrett D'Amore 
59995c635efSGarrett D'Amore 		pos = stesc - *bufp;
60095c635efSGarrett D'Amore 
60195c635efSGarrett D'Amore 		nsz = *szp + strlen(res) + 1;
602*698f87a4SGarrett D'Amore 		nbuf = mandoc_malloc(nsz);
60395c635efSGarrett D'Amore 
604*698f87a4SGarrett D'Amore 		strlcpy(nbuf, *bufp, (size_t)(stesc - *bufp + 1));
605*698f87a4SGarrett D'Amore 		strlcat(nbuf, res, nsz);
606*698f87a4SGarrett D'Amore 		strlcat(nbuf, cp + (maxl ? 0 : 1), nsz);
60795c635efSGarrett D'Amore 
60895c635efSGarrett D'Amore 		free(*bufp);
60995c635efSGarrett D'Amore 
610*698f87a4SGarrett D'Amore 		*bufp = nbuf;
61195c635efSGarrett D'Amore 		*szp = nsz;
61295c635efSGarrett D'Amore 
61395c635efSGarrett D'Amore 		if (EXPAND_LIMIT >= ++expand_count)
61495c635efSGarrett D'Amore 			goto again;
61595c635efSGarrett D'Amore 
61695c635efSGarrett D'Amore 		/* Just leave the string unexpanded. */
61795c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
61895c635efSGarrett D'Amore 		return(ROFF_IGN);
61995c635efSGarrett D'Amore 	}
62095c635efSGarrett D'Amore 	return(ROFF_CONT);
62195c635efSGarrett D'Amore }
62295c635efSGarrett D'Amore 
62395c635efSGarrett D'Amore /*
624*698f87a4SGarrett D'Amore  * Process text streams:
625*698f87a4SGarrett D'Amore  * Convert all breakable hyphens into ASCII_HYPH.
626*698f87a4SGarrett D'Amore  * Decrement and spring input line trap.
62795c635efSGarrett D'Amore  */
62895c635efSGarrett D'Amore static enum rofferr
roff_parsetext(char ** bufp,size_t * szp,int pos,int * offs)629*698f87a4SGarrett D'Amore roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
63095c635efSGarrett D'Amore {
63195c635efSGarrett D'Amore 	size_t		 sz;
63295c635efSGarrett D'Amore 	const char	*start;
633*698f87a4SGarrett D'Amore 	char		*p;
634*698f87a4SGarrett D'Amore 	int		 isz;
63595c635efSGarrett D'Amore 	enum mandoc_esc	 esc;
63695c635efSGarrett D'Amore 
637*698f87a4SGarrett D'Amore 	start = p = *bufp + pos;
63895c635efSGarrett D'Amore 
63995c635efSGarrett D'Amore 	while ('\0' != *p) {
64095c635efSGarrett D'Amore 		sz = strcspn(p, "-\\");
64195c635efSGarrett D'Amore 		p += sz;
64295c635efSGarrett D'Amore 
64395c635efSGarrett D'Amore 		if ('\0' == *p)
64495c635efSGarrett D'Amore 			break;
64595c635efSGarrett D'Amore 
64695c635efSGarrett D'Amore 		if ('\\' == *p) {
64795c635efSGarrett D'Amore 			/* Skip over escapes. */
64895c635efSGarrett D'Amore 			p++;
649*698f87a4SGarrett D'Amore 			esc = mandoc_escape((const char **)&p, NULL, NULL);
65095c635efSGarrett D'Amore 			if (ESCAPE_ERROR == esc)
65195c635efSGarrett D'Amore 				break;
65295c635efSGarrett D'Amore 			continue;
65395c635efSGarrett D'Amore 		} else if (p == start) {
65495c635efSGarrett D'Amore 			p++;
65595c635efSGarrett D'Amore 			continue;
65695c635efSGarrett D'Amore 		}
65795c635efSGarrett D'Amore 
65895c635efSGarrett D'Amore 		if (isalpha((unsigned char)p[-1]) &&
65995c635efSGarrett D'Amore 		    isalpha((unsigned char)p[1]))
66095c635efSGarrett D'Amore 			*p = ASCII_HYPH;
66195c635efSGarrett D'Amore 		p++;
66295c635efSGarrett D'Amore 	}
66395c635efSGarrett D'Amore 
664*698f87a4SGarrett D'Amore 	/* Spring the input line trap. */
665*698f87a4SGarrett D'Amore 	if (1 == roffit_lines) {
666*698f87a4SGarrett D'Amore 		isz = asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
667*698f87a4SGarrett D'Amore 		if (-1 == isz) {
668*698f87a4SGarrett D'Amore 			perror(NULL);
669*698f87a4SGarrett D'Amore 			exit((int)MANDOCLEVEL_SYSERR);
670*698f87a4SGarrett D'Amore 		}
671*698f87a4SGarrett D'Amore 		free(*bufp);
672*698f87a4SGarrett D'Amore 		*bufp = p;
673*698f87a4SGarrett D'Amore 		*szp = isz + 1;
674*698f87a4SGarrett D'Amore 		*offs = 0;
675*698f87a4SGarrett D'Amore 		free(roffit_macro);
676*698f87a4SGarrett D'Amore 		roffit_lines = 0;
677*698f87a4SGarrett D'Amore 		return(ROFF_REPARSE);
678*698f87a4SGarrett D'Amore 	} else if (1 < roffit_lines)
679*698f87a4SGarrett D'Amore 		--roffit_lines;
68095c635efSGarrett D'Amore 	return(ROFF_CONT);
68195c635efSGarrett D'Amore }
68295c635efSGarrett D'Amore 
68395c635efSGarrett D'Amore enum rofferr
roff_parseln(struct roff * r,int ln,char ** bufp,size_t * szp,int pos,int * offs)68495c635efSGarrett D'Amore roff_parseln(struct roff *r, int ln, char **bufp,
68595c635efSGarrett D'Amore 		size_t *szp, int pos, int *offs)
68695c635efSGarrett D'Amore {
68795c635efSGarrett D'Amore 	enum rofft	 t;
68895c635efSGarrett D'Amore 	enum rofferr	 e;
68995c635efSGarrett D'Amore 	int		 ppos, ctl;
69095c635efSGarrett D'Amore 
69195c635efSGarrett D'Amore 	/*
69295c635efSGarrett D'Amore 	 * Run the reserved-word filter only if we have some reserved
69395c635efSGarrett D'Amore 	 * words to fill in.
69495c635efSGarrett D'Amore 	 */
69595c635efSGarrett D'Amore 
69695c635efSGarrett D'Amore 	e = roff_res(r, bufp, szp, ln, pos);
69795c635efSGarrett D'Amore 	if (ROFF_IGN == e)
69895c635efSGarrett D'Amore 		return(e);
69995c635efSGarrett D'Amore 	assert(ROFF_CONT == e);
70095c635efSGarrett D'Amore 
70195c635efSGarrett D'Amore 	ppos = pos;
702*698f87a4SGarrett D'Amore 	ctl = roff_getcontrol(r, *bufp, &pos);
70395c635efSGarrett D'Amore 
70495c635efSGarrett D'Amore 	/*
70595c635efSGarrett D'Amore 	 * First, if a scope is open and we're not a macro, pass the
70695c635efSGarrett D'Amore 	 * text through the macro's filter.  If a scope isn't open and
70795c635efSGarrett D'Amore 	 * we're not a macro, just let it through.
70895c635efSGarrett D'Amore 	 * Finally, if there's an equation scope open, divert it into it
70995c635efSGarrett D'Amore 	 * no matter our state.
71095c635efSGarrett D'Amore 	 */
71195c635efSGarrett D'Amore 
71295c635efSGarrett D'Amore 	if (r->last && ! ctl) {
71395c635efSGarrett D'Amore 		t = r->last->tok;
71495c635efSGarrett D'Amore 		assert(roffs[t].text);
71595c635efSGarrett D'Amore 		e = (*roffs[t].text)
71695c635efSGarrett D'Amore 			(r, t, bufp, szp, ln, pos, pos, offs);
71795c635efSGarrett D'Amore 		assert(ROFF_IGN == e || ROFF_CONT == e);
71895c635efSGarrett D'Amore 		if (ROFF_CONT != e)
71995c635efSGarrett D'Amore 			return(e);
720*698f87a4SGarrett D'Amore 	}
72195c635efSGarrett D'Amore 	if (r->eqn)
72295c635efSGarrett D'Amore 		return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
723*698f87a4SGarrett D'Amore 	if ( ! ctl) {
724*698f87a4SGarrett D'Amore 		if (r->tbl)
725*698f87a4SGarrett D'Amore 			return(tbl_read(r->tbl, ln, *bufp, pos));
726*698f87a4SGarrett D'Amore 		return(roff_parsetext(bufp, szp, pos, offs));
727*698f87a4SGarrett D'Amore 	}
72895c635efSGarrett D'Amore 
72995c635efSGarrett D'Amore 	/*
73095c635efSGarrett D'Amore 	 * If a scope is open, go to the child handler for that macro,
73195c635efSGarrett D'Amore 	 * as it may want to preprocess before doing anything with it.
73295c635efSGarrett D'Amore 	 * Don't do so if an equation is open.
73395c635efSGarrett D'Amore 	 */
73495c635efSGarrett D'Amore 
73595c635efSGarrett D'Amore 	if (r->last) {
73695c635efSGarrett D'Amore 		t = r->last->tok;
73795c635efSGarrett D'Amore 		assert(roffs[t].sub);
73895c635efSGarrett D'Amore 		return((*roffs[t].sub)
73995c635efSGarrett D'Amore 				(r, t, bufp, szp,
74095c635efSGarrett D'Amore 				 ln, ppos, pos, offs));
74195c635efSGarrett D'Amore 	}
74295c635efSGarrett D'Amore 
74395c635efSGarrett D'Amore 	/*
74495c635efSGarrett D'Amore 	 * Lastly, as we've no scope open, try to look up and execute
74595c635efSGarrett D'Amore 	 * the new macro.  If no macro is found, simply return and let
74695c635efSGarrett D'Amore 	 * the compilers handle it.
74795c635efSGarrett D'Amore 	 */
74895c635efSGarrett D'Amore 
74995c635efSGarrett D'Amore 	if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
75095c635efSGarrett D'Amore 		return(ROFF_CONT);
75195c635efSGarrett D'Amore 
75295c635efSGarrett D'Amore 	assert(roffs[t].proc);
75395c635efSGarrett D'Amore 	return((*roffs[t].proc)
75495c635efSGarrett D'Amore 			(r, t, bufp, szp,
75595c635efSGarrett D'Amore 			 ln, ppos, pos, offs));
75695c635efSGarrett D'Amore }
75795c635efSGarrett D'Amore 
75895c635efSGarrett D'Amore 
75995c635efSGarrett D'Amore void
roff_endparse(struct roff * r)76095c635efSGarrett D'Amore roff_endparse(struct roff *r)
76195c635efSGarrett D'Amore {
76295c635efSGarrett D'Amore 
76395c635efSGarrett D'Amore 	if (r->last)
76495c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
76595c635efSGarrett D'Amore 				r->last->line, r->last->col, NULL);
76695c635efSGarrett D'Amore 
76795c635efSGarrett D'Amore 	if (r->eqn) {
76895c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
76995c635efSGarrett D'Amore 				r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
77095c635efSGarrett D'Amore 		eqn_end(&r->eqn);
77195c635efSGarrett D'Amore 	}
77295c635efSGarrett D'Amore 
77395c635efSGarrett D'Amore 	if (r->tbl) {
77495c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
77595c635efSGarrett D'Amore 				r->tbl->line, r->tbl->pos, NULL);
77695c635efSGarrett D'Amore 		tbl_end(&r->tbl);
77795c635efSGarrett D'Amore 	}
77895c635efSGarrett D'Amore }
77995c635efSGarrett D'Amore 
78095c635efSGarrett D'Amore /*
78195c635efSGarrett D'Amore  * Parse a roff node's type from the input buffer.  This must be in the
78295c635efSGarrett D'Amore  * form of ".foo xxx" in the usual way.
78395c635efSGarrett D'Amore  */
78495c635efSGarrett D'Amore static enum rofft
roff_parse(struct roff * r,const char * buf,int * pos)78595c635efSGarrett D'Amore roff_parse(struct roff *r, const char *buf, int *pos)
78695c635efSGarrett D'Amore {
78795c635efSGarrett D'Amore 	const char	*mac;
78895c635efSGarrett D'Amore 	size_t		 maclen;
78995c635efSGarrett D'Amore 	enum rofft	 t;
79095c635efSGarrett D'Amore 
79195c635efSGarrett D'Amore 	if ('\0' == buf[*pos] || '"' == buf[*pos] ||
79295c635efSGarrett D'Amore 			'\t' == buf[*pos] || ' ' == buf[*pos])
79395c635efSGarrett D'Amore 		return(ROFF_MAX);
79495c635efSGarrett D'Amore 
79595c635efSGarrett D'Amore 	/*
79695c635efSGarrett D'Amore 	 * We stop the macro parse at an escape, tab, space, or nil.
79795c635efSGarrett D'Amore 	 * However, `\}' is also a valid macro, so make sure we don't
79895c635efSGarrett D'Amore 	 * clobber it by seeing the `\' as the end of token.
79995c635efSGarrett D'Amore 	 */
80095c635efSGarrett D'Amore 
80195c635efSGarrett D'Amore 	mac = buf + *pos;
80295c635efSGarrett D'Amore 	maclen = strcspn(mac + 1, " \\\t\0") + 1;
80395c635efSGarrett D'Amore 
80495c635efSGarrett D'Amore 	t = (r->current_string = roff_getstrn(r, mac, maclen))
80595c635efSGarrett D'Amore 	    ? ROFF_USERDEF : roffhash_find(mac, maclen);
80695c635efSGarrett D'Amore 
80795c635efSGarrett D'Amore 	*pos += (int)maclen;
80895c635efSGarrett D'Amore 
80995c635efSGarrett D'Amore 	while (buf[*pos] && ' ' == buf[*pos])
81095c635efSGarrett D'Amore 		(*pos)++;
81195c635efSGarrett D'Amore 
81295c635efSGarrett D'Amore 	return(t);
81395c635efSGarrett D'Amore }
81495c635efSGarrett D'Amore 
81595c635efSGarrett D'Amore /* ARGSUSED */
81695c635efSGarrett D'Amore static enum rofferr
roff_cblock(ROFF_ARGS)81795c635efSGarrett D'Amore roff_cblock(ROFF_ARGS)
81895c635efSGarrett D'Amore {
81995c635efSGarrett D'Amore 
82095c635efSGarrett D'Amore 	/*
82195c635efSGarrett D'Amore 	 * A block-close `..' should only be invoked as a child of an
82295c635efSGarrett D'Amore 	 * ignore macro, otherwise raise a warning and just ignore it.
82395c635efSGarrett D'Amore 	 */
82495c635efSGarrett D'Amore 
82595c635efSGarrett D'Amore 	if (NULL == r->last) {
82695c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
82795c635efSGarrett D'Amore 		return(ROFF_IGN);
82895c635efSGarrett D'Amore 	}
82995c635efSGarrett D'Amore 
83095c635efSGarrett D'Amore 	switch (r->last->tok) {
83195c635efSGarrett D'Amore 	case (ROFF_am):
83295c635efSGarrett D'Amore 		/* FALLTHROUGH */
83395c635efSGarrett D'Amore 	case (ROFF_ami):
83495c635efSGarrett D'Amore 		/* FALLTHROUGH */
83595c635efSGarrett D'Amore 	case (ROFF_am1):
83695c635efSGarrett D'Amore 		/* FALLTHROUGH */
83795c635efSGarrett D'Amore 	case (ROFF_de):
83895c635efSGarrett D'Amore 		/* ROFF_de1 is remapped to ROFF_de in roff_block(). */
83995c635efSGarrett D'Amore 		/* FALLTHROUGH */
84095c635efSGarrett D'Amore 	case (ROFF_dei):
84195c635efSGarrett D'Amore 		/* FALLTHROUGH */
84295c635efSGarrett D'Amore 	case (ROFF_ig):
84395c635efSGarrett D'Amore 		break;
84495c635efSGarrett D'Amore 	default:
84595c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
84695c635efSGarrett D'Amore 		return(ROFF_IGN);
84795c635efSGarrett D'Amore 	}
84895c635efSGarrett D'Amore 
84995c635efSGarrett D'Amore 	if ((*bufp)[pos])
85095c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
85195c635efSGarrett D'Amore 
85295c635efSGarrett D'Amore 	roffnode_pop(r);
85395c635efSGarrett D'Amore 	roffnode_cleanscope(r);
85495c635efSGarrett D'Amore 	return(ROFF_IGN);
85595c635efSGarrett D'Amore 
85695c635efSGarrett D'Amore }
85795c635efSGarrett D'Amore 
85895c635efSGarrett D'Amore 
85995c635efSGarrett D'Amore static void
roffnode_cleanscope(struct roff * r)86095c635efSGarrett D'Amore roffnode_cleanscope(struct roff *r)
86195c635efSGarrett D'Amore {
86295c635efSGarrett D'Amore 
86395c635efSGarrett D'Amore 	while (r->last) {
864*698f87a4SGarrett D'Amore 		if (--r->last->endspan != 0)
86595c635efSGarrett D'Amore 			break;
86695c635efSGarrett D'Amore 		roffnode_pop(r);
86795c635efSGarrett D'Amore 	}
86895c635efSGarrett D'Amore }
86995c635efSGarrett D'Amore 
87095c635efSGarrett D'Amore 
87195c635efSGarrett D'Amore /* ARGSUSED */
87295c635efSGarrett D'Amore static enum rofferr
roff_ccond(ROFF_ARGS)87395c635efSGarrett D'Amore roff_ccond(ROFF_ARGS)
87495c635efSGarrett D'Amore {
87595c635efSGarrett D'Amore 
87695c635efSGarrett D'Amore 	if (NULL == r->last) {
87795c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
87895c635efSGarrett D'Amore 		return(ROFF_IGN);
87995c635efSGarrett D'Amore 	}
88095c635efSGarrett D'Amore 
88195c635efSGarrett D'Amore 	switch (r->last->tok) {
88295c635efSGarrett D'Amore 	case (ROFF_el):
88395c635efSGarrett D'Amore 		/* FALLTHROUGH */
88495c635efSGarrett D'Amore 	case (ROFF_ie):
88595c635efSGarrett D'Amore 		/* FALLTHROUGH */
88695c635efSGarrett D'Amore 	case (ROFF_if):
88795c635efSGarrett D'Amore 		break;
88895c635efSGarrett D'Amore 	default:
88995c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
89095c635efSGarrett D'Amore 		return(ROFF_IGN);
89195c635efSGarrett D'Amore 	}
89295c635efSGarrett D'Amore 
89395c635efSGarrett D'Amore 	if (r->last->endspan > -1) {
89495c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
89595c635efSGarrett D'Amore 		return(ROFF_IGN);
89695c635efSGarrett D'Amore 	}
89795c635efSGarrett D'Amore 
89895c635efSGarrett D'Amore 	if ((*bufp)[pos])
89995c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
90095c635efSGarrett D'Amore 
90195c635efSGarrett D'Amore 	roffnode_pop(r);
90295c635efSGarrett D'Amore 	roffnode_cleanscope(r);
90395c635efSGarrett D'Amore 	return(ROFF_IGN);
90495c635efSGarrett D'Amore }
90595c635efSGarrett D'Amore 
90695c635efSGarrett D'Amore 
90795c635efSGarrett D'Amore /* ARGSUSED */
90895c635efSGarrett D'Amore static enum rofferr
roff_block(ROFF_ARGS)90995c635efSGarrett D'Amore roff_block(ROFF_ARGS)
91095c635efSGarrett D'Amore {
91195c635efSGarrett D'Amore 	int		sv;
91295c635efSGarrett D'Amore 	size_t		sz;
91395c635efSGarrett D'Amore 	char		*name;
91495c635efSGarrett D'Amore 
91595c635efSGarrett D'Amore 	name = NULL;
91695c635efSGarrett D'Amore 
91795c635efSGarrett D'Amore 	if (ROFF_ig != tok) {
91895c635efSGarrett D'Amore 		if ('\0' == (*bufp)[pos]) {
91995c635efSGarrett D'Amore 			mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
92095c635efSGarrett D'Amore 			return(ROFF_IGN);
92195c635efSGarrett D'Amore 		}
92295c635efSGarrett D'Amore 
92395c635efSGarrett D'Amore 		/*
92495c635efSGarrett D'Amore 		 * Re-write `de1', since we don't really care about
92595c635efSGarrett D'Amore 		 * groff's strange compatibility mode, into `de'.
92695c635efSGarrett D'Amore 		 */
92795c635efSGarrett D'Amore 
92895c635efSGarrett D'Amore 		if (ROFF_de1 == tok)
92995c635efSGarrett D'Amore 			tok = ROFF_de;
93095c635efSGarrett D'Amore 		if (ROFF_de == tok)
93195c635efSGarrett D'Amore 			name = *bufp + pos;
93295c635efSGarrett D'Amore 		else
93395c635efSGarrett D'Amore 			mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
93495c635efSGarrett D'Amore 			    roffs[tok].name);
93595c635efSGarrett D'Amore 
93695c635efSGarrett D'Amore 		while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
93795c635efSGarrett D'Amore 			pos++;
93895c635efSGarrett D'Amore 
93995c635efSGarrett D'Amore 		while (isspace((unsigned char)(*bufp)[pos]))
94095c635efSGarrett D'Amore 			(*bufp)[pos++] = '\0';
94195c635efSGarrett D'Amore 	}
94295c635efSGarrett D'Amore 
94395c635efSGarrett D'Amore 	roffnode_push(r, tok, name, ln, ppos);
94495c635efSGarrett D'Amore 
94595c635efSGarrett D'Amore 	/*
94695c635efSGarrett D'Amore 	 * At the beginning of a `de' macro, clear the existing string
94795c635efSGarrett D'Amore 	 * with the same name, if there is one.  New content will be
94895c635efSGarrett D'Amore 	 * added from roff_block_text() in multiline mode.
94995c635efSGarrett D'Amore 	 */
95095c635efSGarrett D'Amore 
95195c635efSGarrett D'Amore 	if (ROFF_de == tok)
95295c635efSGarrett D'Amore 		roff_setstr(r, name, "", 0);
95395c635efSGarrett D'Amore 
95495c635efSGarrett D'Amore 	if ('\0' == (*bufp)[pos])
95595c635efSGarrett D'Amore 		return(ROFF_IGN);
95695c635efSGarrett D'Amore 
95795c635efSGarrett D'Amore 	/* If present, process the custom end-of-line marker. */
95895c635efSGarrett D'Amore 
95995c635efSGarrett D'Amore 	sv = pos;
96095c635efSGarrett D'Amore 	while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
96195c635efSGarrett D'Amore 		pos++;
96295c635efSGarrett D'Amore 
96395c635efSGarrett D'Amore 	/*
96495c635efSGarrett D'Amore 	 * Note: groff does NOT like escape characters in the input.
96595c635efSGarrett D'Amore 	 * Instead of detecting this, we're just going to let it fly and
96695c635efSGarrett D'Amore 	 * to hell with it.
96795c635efSGarrett D'Amore 	 */
96895c635efSGarrett D'Amore 
96995c635efSGarrett D'Amore 	assert(pos > sv);
97095c635efSGarrett D'Amore 	sz = (size_t)(pos - sv);
97195c635efSGarrett D'Amore 
97295c635efSGarrett D'Amore 	if (1 == sz && '.' == (*bufp)[sv])
97395c635efSGarrett D'Amore 		return(ROFF_IGN);
97495c635efSGarrett D'Amore 
97595c635efSGarrett D'Amore 	r->last->end = mandoc_malloc(sz + 1);
97695c635efSGarrett D'Amore 
97795c635efSGarrett D'Amore 	memcpy(r->last->end, *bufp + sv, sz);
97895c635efSGarrett D'Amore 	r->last->end[(int)sz] = '\0';
97995c635efSGarrett D'Amore 
98095c635efSGarrett D'Amore 	if ((*bufp)[pos])
98195c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
98295c635efSGarrett D'Amore 
98395c635efSGarrett D'Amore 	return(ROFF_IGN);
98495c635efSGarrett D'Amore }
98595c635efSGarrett D'Amore 
98695c635efSGarrett D'Amore 
98795c635efSGarrett D'Amore /* ARGSUSED */
98895c635efSGarrett D'Amore static enum rofferr
roff_block_sub(ROFF_ARGS)98995c635efSGarrett D'Amore roff_block_sub(ROFF_ARGS)
99095c635efSGarrett D'Amore {
99195c635efSGarrett D'Amore 	enum rofft	t;
99295c635efSGarrett D'Amore 	int		i, j;
99395c635efSGarrett D'Amore 
99495c635efSGarrett D'Amore 	/*
99595c635efSGarrett D'Amore 	 * First check whether a custom macro exists at this level.  If
99695c635efSGarrett D'Amore 	 * it does, then check against it.  This is some of groff's
99795c635efSGarrett D'Amore 	 * stranger behaviours.  If we encountered a custom end-scope
99895c635efSGarrett D'Amore 	 * tag and that tag also happens to be a "real" macro, then we
99995c635efSGarrett D'Amore 	 * need to try interpreting it again as a real macro.  If it's
100095c635efSGarrett D'Amore 	 * not, then return ignore.  Else continue.
100195c635efSGarrett D'Amore 	 */
100295c635efSGarrett D'Amore 
100395c635efSGarrett D'Amore 	if (r->last->end) {
100495c635efSGarrett D'Amore 		for (i = pos, j = 0; r->last->end[j]; j++, i++)
100595c635efSGarrett D'Amore 			if ((*bufp)[i] != r->last->end[j])
100695c635efSGarrett D'Amore 				break;
100795c635efSGarrett D'Amore 
100895c635efSGarrett D'Amore 		if ('\0' == r->last->end[j] &&
100995c635efSGarrett D'Amore 				('\0' == (*bufp)[i] ||
101095c635efSGarrett D'Amore 				 ' ' == (*bufp)[i] ||
101195c635efSGarrett D'Amore 				 '\t' == (*bufp)[i])) {
101295c635efSGarrett D'Amore 			roffnode_pop(r);
101395c635efSGarrett D'Amore 			roffnode_cleanscope(r);
101495c635efSGarrett D'Amore 
101595c635efSGarrett D'Amore 			while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
101695c635efSGarrett D'Amore 				i++;
101795c635efSGarrett D'Amore 
101895c635efSGarrett D'Amore 			pos = i;
101995c635efSGarrett D'Amore 			if (ROFF_MAX != roff_parse(r, *bufp, &pos))
102095c635efSGarrett D'Amore 				return(ROFF_RERUN);
102195c635efSGarrett D'Amore 			return(ROFF_IGN);
102295c635efSGarrett D'Amore 		}
102395c635efSGarrett D'Amore 	}
102495c635efSGarrett D'Amore 
102595c635efSGarrett D'Amore 	/*
102695c635efSGarrett D'Amore 	 * If we have no custom end-query or lookup failed, then try
102795c635efSGarrett D'Amore 	 * pulling it out of the hashtable.
102895c635efSGarrett D'Amore 	 */
102995c635efSGarrett D'Amore 
103095c635efSGarrett D'Amore 	t = roff_parse(r, *bufp, &pos);
103195c635efSGarrett D'Amore 
103295c635efSGarrett D'Amore 	/*
103395c635efSGarrett D'Amore 	 * Macros other than block-end are only significant
103495c635efSGarrett D'Amore 	 * in `de' blocks; elsewhere, simply throw them away.
103595c635efSGarrett D'Amore 	 */
103695c635efSGarrett D'Amore 	if (ROFF_cblock != t) {
103795c635efSGarrett D'Amore 		if (ROFF_de == tok)
103895c635efSGarrett D'Amore 			roff_setstr(r, r->last->name, *bufp + ppos, 1);
103995c635efSGarrett D'Amore 		return(ROFF_IGN);
104095c635efSGarrett D'Amore 	}
104195c635efSGarrett D'Amore 
104295c635efSGarrett D'Amore 	assert(roffs[t].proc);
104395c635efSGarrett D'Amore 	return((*roffs[t].proc)(r, t, bufp, szp,
104495c635efSGarrett D'Amore 				ln, ppos, pos, offs));
104595c635efSGarrett D'Amore }
104695c635efSGarrett D'Amore 
104795c635efSGarrett D'Amore 
104895c635efSGarrett D'Amore /* ARGSUSED */
104995c635efSGarrett D'Amore static enum rofferr
roff_block_text(ROFF_ARGS)105095c635efSGarrett D'Amore roff_block_text(ROFF_ARGS)
105195c635efSGarrett D'Amore {
105295c635efSGarrett D'Amore 
105395c635efSGarrett D'Amore 	if (ROFF_de == tok)
105495c635efSGarrett D'Amore 		roff_setstr(r, r->last->name, *bufp + pos, 1);
105595c635efSGarrett D'Amore 
105695c635efSGarrett D'Amore 	return(ROFF_IGN);
105795c635efSGarrett D'Amore }
105895c635efSGarrett D'Amore 
105995c635efSGarrett D'Amore 
106095c635efSGarrett D'Amore /* ARGSUSED */
106195c635efSGarrett D'Amore static enum rofferr
roff_cond_sub(ROFF_ARGS)106295c635efSGarrett D'Amore roff_cond_sub(ROFF_ARGS)
106395c635efSGarrett D'Amore {
106495c635efSGarrett D'Amore 	enum rofft	 t;
106595c635efSGarrett D'Amore 	enum roffrule	 rr;
106695c635efSGarrett D'Amore 	char		*ep;
106795c635efSGarrett D'Amore 
106895c635efSGarrett D'Amore 	rr = r->last->rule;
106995c635efSGarrett D'Amore 	roffnode_cleanscope(r);
1070*698f87a4SGarrett D'Amore 	t = roff_parse(r, *bufp, &pos);
107195c635efSGarrett D'Amore 
107295c635efSGarrett D'Amore 	/*
1073*698f87a4SGarrett D'Amore 	 * Fully handle known macros when they are structurally
1074*698f87a4SGarrett D'Amore 	 * required or when the conditional evaluated to true.
107595c635efSGarrett D'Amore 	 */
107695c635efSGarrett D'Amore 
1077*698f87a4SGarrett D'Amore 	if ((ROFF_MAX != t) &&
1078*698f87a4SGarrett D'Amore 	    (ROFF_ccond == t || ROFFRULE_ALLOW == rr ||
1079*698f87a4SGarrett D'Amore 	     ROFFMAC_STRUCT & roffs[t].flags)) {
1080*698f87a4SGarrett D'Amore 		assert(roffs[t].proc);
1081*698f87a4SGarrett D'Amore 		return((*roffs[t].proc)(r, t, bufp, szp,
1082*698f87a4SGarrett D'Amore 					ln, ppos, pos, offs));
1083*698f87a4SGarrett D'Amore 	}
1084*698f87a4SGarrett D'Amore 
1085*698f87a4SGarrett D'Amore 	/* Always check for the closing delimiter `\}'. */
1086*698f87a4SGarrett D'Amore 
108795c635efSGarrett D'Amore 	ep = &(*bufp)[pos];
1088*698f87a4SGarrett D'Amore 	while (NULL != (ep = strchr(ep, '\\'))) {
1089*698f87a4SGarrett D'Amore 		if ('}' != *(++ep))
109095c635efSGarrett D'Amore 			continue;
109195c635efSGarrett D'Amore 
109295c635efSGarrett D'Amore 		/*
109395c635efSGarrett D'Amore 		 * If we're at the end of line, then just chop
109495c635efSGarrett D'Amore 		 * off the \} and resize the buffer.
1095*698f87a4SGarrett D'Amore 		 * If we aren't, then convert it to spaces.
109695c635efSGarrett D'Amore 		 */
109795c635efSGarrett D'Amore 
109895c635efSGarrett D'Amore 		if ('\0' == *(ep + 1)) {
109995c635efSGarrett D'Amore 			*--ep = '\0';
110095c635efSGarrett D'Amore 			*szp -= 2;
110195c635efSGarrett D'Amore 		} else
110295c635efSGarrett D'Amore 			*(ep - 1) = *ep = ' ';
110395c635efSGarrett D'Amore 
110495c635efSGarrett D'Amore 		roff_ccond(r, ROFF_ccond, bufp, szp,
110595c635efSGarrett D'Amore 				ln, pos, pos + 2, offs);
110695c635efSGarrett D'Amore 		break;
110795c635efSGarrett D'Amore 	}
110895c635efSGarrett D'Amore 	return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
110995c635efSGarrett D'Amore }
111095c635efSGarrett D'Amore 
111195c635efSGarrett D'Amore /* ARGSUSED */
111295c635efSGarrett D'Amore static enum rofferr
roff_cond_text(ROFF_ARGS)111395c635efSGarrett D'Amore roff_cond_text(ROFF_ARGS)
111495c635efSGarrett D'Amore {
111595c635efSGarrett D'Amore 	char		*ep;
111695c635efSGarrett D'Amore 	enum roffrule	 rr;
111795c635efSGarrett D'Amore 
111895c635efSGarrett D'Amore 	rr = r->last->rule;
111995c635efSGarrett D'Amore 	roffnode_cleanscope(r);
112095c635efSGarrett D'Amore 
112195c635efSGarrett D'Amore 	ep = &(*bufp)[pos];
112295c635efSGarrett D'Amore 	for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
112395c635efSGarrett D'Amore 		ep++;
112495c635efSGarrett D'Amore 		if ('}' != *ep)
112595c635efSGarrett D'Amore 			continue;
112695c635efSGarrett D'Amore 		*ep = '&';
112795c635efSGarrett D'Amore 		roff_ccond(r, ROFF_ccond, bufp, szp,
112895c635efSGarrett D'Amore 				ln, pos, pos + 2, offs);
112995c635efSGarrett D'Amore 	}
113095c635efSGarrett D'Amore 	return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
113195c635efSGarrett D'Amore }
113295c635efSGarrett D'Amore 
1133*698f87a4SGarrett D'Amore static int
roff_getnum(const char * v,int * pos,int * res)1134*698f87a4SGarrett D'Amore roff_getnum(const char *v, int *pos, int *res)
1135*698f87a4SGarrett D'Amore {
1136*698f87a4SGarrett D'Amore 	int p, n;
1137*698f87a4SGarrett D'Amore 
1138*698f87a4SGarrett D'Amore 	p = *pos;
1139*698f87a4SGarrett D'Amore 	n = v[p] == '-';
1140*698f87a4SGarrett D'Amore 	if (n)
1141*698f87a4SGarrett D'Amore 		p++;
1142*698f87a4SGarrett D'Amore 
1143*698f87a4SGarrett D'Amore 	for (*res = 0; isdigit((unsigned char)v[p]); p++)
1144*698f87a4SGarrett D'Amore 		*res += 10 * *res + v[p] - '0';
1145*698f87a4SGarrett D'Amore 	if (p == *pos + n)
1146*698f87a4SGarrett D'Amore 		return 0;
1147*698f87a4SGarrett D'Amore 
1148*698f87a4SGarrett D'Amore 	if (n)
1149*698f87a4SGarrett D'Amore 		*res = -*res;
1150*698f87a4SGarrett D'Amore 
1151*698f87a4SGarrett D'Amore 	*pos = p;
1152*698f87a4SGarrett D'Amore 	return 1;
1153*698f87a4SGarrett D'Amore }
1154*698f87a4SGarrett D'Amore 
1155*698f87a4SGarrett D'Amore static int
roff_getop(const char * v,int * pos,char * res)1156*698f87a4SGarrett D'Amore roff_getop(const char *v, int *pos, char *res)
1157*698f87a4SGarrett D'Amore {
1158*698f87a4SGarrett D'Amore 	int e;
1159*698f87a4SGarrett D'Amore 
1160*698f87a4SGarrett D'Amore 	*res = v[*pos];
1161*698f87a4SGarrett D'Amore 	e = v[*pos + 1] == '=';
1162*698f87a4SGarrett D'Amore 
1163*698f87a4SGarrett D'Amore 	switch (*res) {
1164*698f87a4SGarrett D'Amore 	case '=':
1165*698f87a4SGarrett D'Amore 		break;
1166*698f87a4SGarrett D'Amore 	case '>':
1167*698f87a4SGarrett D'Amore 		if (e)
1168*698f87a4SGarrett D'Amore 			*res = 'g';
1169*698f87a4SGarrett D'Amore 		break;
1170*698f87a4SGarrett D'Amore 	case '<':
1171*698f87a4SGarrett D'Amore 		if (e)
1172*698f87a4SGarrett D'Amore 			*res = 'l';
1173*698f87a4SGarrett D'Amore 		break;
1174*698f87a4SGarrett D'Amore 	default:
1175*698f87a4SGarrett D'Amore 		return(0);
1176*698f87a4SGarrett D'Amore 	}
1177*698f87a4SGarrett D'Amore 
1178*698f87a4SGarrett D'Amore 	*pos += 1 + e;
1179*698f87a4SGarrett D'Amore 
1180*698f87a4SGarrett D'Amore 	return(*res);
1181*698f87a4SGarrett D'Amore }
1182*698f87a4SGarrett D'Amore 
118395c635efSGarrett D'Amore static enum roffrule
roff_evalcond(const char * v,int * pos)118495c635efSGarrett D'Amore roff_evalcond(const char *v, int *pos)
118595c635efSGarrett D'Amore {
1186*698f87a4SGarrett D'Amore 	int	 not, lh, rh;
1187*698f87a4SGarrett D'Amore 	char	 op;
118895c635efSGarrett D'Amore 
118995c635efSGarrett D'Amore 	switch (v[*pos]) {
119095c635efSGarrett D'Amore 	case ('n'):
119195c635efSGarrett D'Amore 		(*pos)++;
119295c635efSGarrett D'Amore 		return(ROFFRULE_ALLOW);
119395c635efSGarrett D'Amore 	case ('e'):
119495c635efSGarrett D'Amore 		/* FALLTHROUGH */
119595c635efSGarrett D'Amore 	case ('o'):
119695c635efSGarrett D'Amore 		/* FALLTHROUGH */
119795c635efSGarrett D'Amore 	case ('t'):
119895c635efSGarrett D'Amore 		(*pos)++;
119995c635efSGarrett D'Amore 		return(ROFFRULE_DENY);
1200*698f87a4SGarrett D'Amore 	case ('!'):
1201*698f87a4SGarrett D'Amore 		(*pos)++;
1202*698f87a4SGarrett D'Amore 		not = 1;
1203*698f87a4SGarrett D'Amore 		break;
120495c635efSGarrett D'Amore 	default:
1205*698f87a4SGarrett D'Amore 		not = 0;
120695c635efSGarrett D'Amore 		break;
120795c635efSGarrett D'Amore 	}
120895c635efSGarrett D'Amore 
1209*698f87a4SGarrett D'Amore 	if (!roff_getnum(v, pos, &lh))
1210*698f87a4SGarrett D'Amore 		return ROFFRULE_DENY;
1211*698f87a4SGarrett D'Amore 	if (!roff_getop(v, pos, &op)) {
1212*698f87a4SGarrett D'Amore 		if (lh < 0)
1213*698f87a4SGarrett D'Amore 			lh = 0;
1214*698f87a4SGarrett D'Amore 		goto out;
1215*698f87a4SGarrett D'Amore 	}
1216*698f87a4SGarrett D'Amore 	if (!roff_getnum(v, pos, &rh))
1217*698f87a4SGarrett D'Amore 		return ROFFRULE_DENY;
1218*698f87a4SGarrett D'Amore 	switch (op) {
1219*698f87a4SGarrett D'Amore 	case 'g':
1220*698f87a4SGarrett D'Amore 		lh = lh >= rh;
1221*698f87a4SGarrett D'Amore 		break;
1222*698f87a4SGarrett D'Amore 	case 'l':
1223*698f87a4SGarrett D'Amore 		lh = lh <= rh;
1224*698f87a4SGarrett D'Amore 		break;
1225*698f87a4SGarrett D'Amore 	case '=':
1226*698f87a4SGarrett D'Amore 		lh = lh == rh;
1227*698f87a4SGarrett D'Amore 		break;
1228*698f87a4SGarrett D'Amore 	case '>':
1229*698f87a4SGarrett D'Amore 		lh = lh > rh;
1230*698f87a4SGarrett D'Amore 		break;
1231*698f87a4SGarrett D'Amore 	case '<':
1232*698f87a4SGarrett D'Amore 		lh = lh < rh;
1233*698f87a4SGarrett D'Amore 		break;
1234*698f87a4SGarrett D'Amore 	default:
1235*698f87a4SGarrett D'Amore 		return ROFFRULE_DENY;
1236*698f87a4SGarrett D'Amore 	}
1237*698f87a4SGarrett D'Amore out:
1238*698f87a4SGarrett D'Amore 	if (not)
1239*698f87a4SGarrett D'Amore 		lh = !lh;
1240*698f87a4SGarrett D'Amore 	return lh ? ROFFRULE_ALLOW : ROFFRULE_DENY;
124195c635efSGarrett D'Amore }
124295c635efSGarrett D'Amore 
124395c635efSGarrett D'Amore /* ARGSUSED */
124495c635efSGarrett D'Amore static enum rofferr
roff_line_ignore(ROFF_ARGS)124595c635efSGarrett D'Amore roff_line_ignore(ROFF_ARGS)
124695c635efSGarrett D'Amore {
124795c635efSGarrett D'Amore 
124895c635efSGarrett D'Amore 	return(ROFF_IGN);
124995c635efSGarrett D'Amore }
125095c635efSGarrett D'Amore 
125195c635efSGarrett D'Amore /* ARGSUSED */
125295c635efSGarrett D'Amore static enum rofferr
roff_cond(ROFF_ARGS)125395c635efSGarrett D'Amore roff_cond(ROFF_ARGS)
125495c635efSGarrett D'Amore {
1255*698f87a4SGarrett D'Amore 
1256*698f87a4SGarrett D'Amore 	roffnode_push(r, tok, NULL, ln, ppos);
125795c635efSGarrett D'Amore 
125895c635efSGarrett D'Amore 	/*
125995c635efSGarrett D'Amore 	 * An `.el' has no conditional body: it will consume the value
126095c635efSGarrett D'Amore 	 * of the current rstack entry set in prior `ie' calls or
126195c635efSGarrett D'Amore 	 * defaults to DENY.
126295c635efSGarrett D'Amore 	 *
126395c635efSGarrett D'Amore 	 * If we're not an `el', however, then evaluate the conditional.
126495c635efSGarrett D'Amore 	 */
126595c635efSGarrett D'Amore 
1266*698f87a4SGarrett D'Amore 	r->last->rule = ROFF_el == tok ?
126795c635efSGarrett D'Amore 		(r->rstackpos < 0 ?
126895c635efSGarrett D'Amore 		 ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
126995c635efSGarrett D'Amore 		roff_evalcond(*bufp, &pos);
127095c635efSGarrett D'Amore 
127195c635efSGarrett D'Amore 	/*
127295c635efSGarrett D'Amore 	 * An if-else will put the NEGATION of the current evaluated
127395c635efSGarrett D'Amore 	 * conditional into the stack of rules.
127495c635efSGarrett D'Amore 	 */
127595c635efSGarrett D'Amore 
127695c635efSGarrett D'Amore 	if (ROFF_ie == tok) {
127795c635efSGarrett D'Amore 		if (r->rstackpos == RSTACK_MAX - 1) {
127895c635efSGarrett D'Amore 			mandoc_msg(MANDOCERR_MEM,
127995c635efSGarrett D'Amore 				r->parse, ln, ppos, NULL);
128095c635efSGarrett D'Amore 			return(ROFF_ERR);
128195c635efSGarrett D'Amore 		}
128295c635efSGarrett D'Amore 		r->rstack[++r->rstackpos] =
128395c635efSGarrett D'Amore 			ROFFRULE_DENY == r->last->rule ?
128495c635efSGarrett D'Amore 			ROFFRULE_ALLOW : ROFFRULE_DENY;
128595c635efSGarrett D'Amore 	}
128695c635efSGarrett D'Amore 
128795c635efSGarrett D'Amore 	/* If the parent has false as its rule, then so do we. */
128895c635efSGarrett D'Amore 
128995c635efSGarrett D'Amore 	if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
129095c635efSGarrett D'Amore 		r->last->rule = ROFFRULE_DENY;
129195c635efSGarrett D'Amore 
129295c635efSGarrett D'Amore 	/*
1293*698f87a4SGarrett D'Amore 	 * Determine scope.
1294*698f87a4SGarrett D'Amore 	 * If there is nothing on the line after the conditional,
1295*698f87a4SGarrett D'Amore 	 * not even whitespace, use next-line scope.
129695c635efSGarrett D'Amore 	 */
129795c635efSGarrett D'Amore 
1298*698f87a4SGarrett D'Amore 	if ('\0' == (*bufp)[pos]) {
1299*698f87a4SGarrett D'Amore 		r->last->endspan = 2;
1300*698f87a4SGarrett D'Amore 		goto out;
1301*698f87a4SGarrett D'Amore 	}
1302*698f87a4SGarrett D'Amore 
1303*698f87a4SGarrett D'Amore 	while (' ' == (*bufp)[pos])
1304*698f87a4SGarrett D'Amore 		pos++;
1305*698f87a4SGarrett D'Amore 
1306*698f87a4SGarrett D'Amore 	/* An opening brace requests multiline scope. */
130795c635efSGarrett D'Amore 
130895c635efSGarrett D'Amore 	if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
130995c635efSGarrett D'Amore 		r->last->endspan = -1;
131095c635efSGarrett D'Amore 		pos += 2;
1311*698f87a4SGarrett D'Amore 		goto out;
131295c635efSGarrett D'Amore 	}
131395c635efSGarrett D'Amore 
131495c635efSGarrett D'Amore 	/*
1315*698f87a4SGarrett D'Amore 	 * Anything else following the conditional causes
1316*698f87a4SGarrett D'Amore 	 * single-line scope.  Warn if the scope contains
1317*698f87a4SGarrett D'Amore 	 * nothing but trailing whitespace.
131895c635efSGarrett D'Amore 	 */
131995c635efSGarrett D'Amore 
132095c635efSGarrett D'Amore 	if ('\0' == (*bufp)[pos])
1321*698f87a4SGarrett D'Amore 		mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
132295c635efSGarrett D'Amore 
1323*698f87a4SGarrett D'Amore 	r->last->endspan = 1;
132495c635efSGarrett D'Amore 
1325*698f87a4SGarrett D'Amore out:
132695c635efSGarrett D'Amore 	*offs = pos;
132795c635efSGarrett D'Amore 	return(ROFF_RERUN);
132895c635efSGarrett D'Amore }
132995c635efSGarrett D'Amore 
133095c635efSGarrett D'Amore 
133195c635efSGarrett D'Amore /* ARGSUSED */
133295c635efSGarrett D'Amore static enum rofferr
roff_ds(ROFF_ARGS)133395c635efSGarrett D'Amore roff_ds(ROFF_ARGS)
133495c635efSGarrett D'Amore {
133595c635efSGarrett D'Amore 	char		*name, *string;
133695c635efSGarrett D'Amore 
133795c635efSGarrett D'Amore 	/*
133895c635efSGarrett D'Amore 	 * A symbol is named by the first word following the macro
133995c635efSGarrett D'Amore 	 * invocation up to a space.  Its value is anything after the
134095c635efSGarrett D'Amore 	 * name's trailing whitespace and optional double-quote.  Thus,
134195c635efSGarrett D'Amore 	 *
134295c635efSGarrett D'Amore 	 *  [.ds foo "bar  "     ]
134395c635efSGarrett D'Amore 	 *
134495c635efSGarrett D'Amore 	 * will have `bar  "     ' as its value.
134595c635efSGarrett D'Amore 	 */
134695c635efSGarrett D'Amore 
134795c635efSGarrett D'Amore 	string = *bufp + pos;
134895c635efSGarrett D'Amore 	name = roff_getname(r, &string, ln, pos);
134995c635efSGarrett D'Amore 	if ('\0' == *name)
135095c635efSGarrett D'Amore 		return(ROFF_IGN);
135195c635efSGarrett D'Amore 
135295c635efSGarrett D'Amore 	/* Read past initial double-quote. */
135395c635efSGarrett D'Amore 	if ('"' == *string)
135495c635efSGarrett D'Amore 		string++;
135595c635efSGarrett D'Amore 
135695c635efSGarrett D'Amore 	/* The rest is the value. */
135795c635efSGarrett D'Amore 	roff_setstr(r, name, string, 0);
135895c635efSGarrett D'Amore 	return(ROFF_IGN);
135995c635efSGarrett D'Amore }
136095c635efSGarrett D'Amore 
136195c635efSGarrett D'Amore void
roff_setreg(struct roff * r,const char * name,int val,char sign)1362*698f87a4SGarrett D'Amore roff_setreg(struct roff *r, const char *name, int val, char sign)
136395c635efSGarrett D'Amore {
1364*698f87a4SGarrett D'Amore 	struct roffreg	*reg;
136595c635efSGarrett D'Amore 
1366*698f87a4SGarrett D'Amore 	/* Search for an existing register with the same name. */
1367*698f87a4SGarrett D'Amore 	reg = r->regtab;
1368*698f87a4SGarrett D'Amore 
1369*698f87a4SGarrett D'Amore 	while (reg && strcmp(name, reg->key.p))
1370*698f87a4SGarrett D'Amore 		reg = reg->next;
1371*698f87a4SGarrett D'Amore 
1372*698f87a4SGarrett D'Amore 	if (NULL == reg) {
1373*698f87a4SGarrett D'Amore 		/* Create a new register. */
1374*698f87a4SGarrett D'Amore 		reg = mandoc_malloc(sizeof(struct roffreg));
1375*698f87a4SGarrett D'Amore 		reg->key.p = mandoc_strdup(name);
1376*698f87a4SGarrett D'Amore 		reg->key.sz = strlen(name);
1377*698f87a4SGarrett D'Amore 		reg->val = 0;
1378*698f87a4SGarrett D'Amore 		reg->next = r->regtab;
1379*698f87a4SGarrett D'Amore 		r->regtab = reg;
1380*698f87a4SGarrett D'Amore 	}
1381*698f87a4SGarrett D'Amore 
1382*698f87a4SGarrett D'Amore 	if ('+' == sign)
1383*698f87a4SGarrett D'Amore 		reg->val += val;
1384*698f87a4SGarrett D'Amore 	else if ('-' == sign)
1385*698f87a4SGarrett D'Amore 		reg->val -= val;
1386*698f87a4SGarrett D'Amore 	else
1387*698f87a4SGarrett D'Amore 		reg->val = val;
1388*698f87a4SGarrett D'Amore }
1389*698f87a4SGarrett D'Amore 
1390*698f87a4SGarrett D'Amore int
roff_getreg(const struct roff * r,const char * name)1391*698f87a4SGarrett D'Amore roff_getreg(const struct roff *r, const char *name)
1392*698f87a4SGarrett D'Amore {
1393*698f87a4SGarrett D'Amore 	struct roffreg	*reg;
1394*698f87a4SGarrett D'Amore 
1395*698f87a4SGarrett D'Amore 	for (reg = r->regtab; reg; reg = reg->next)
1396*698f87a4SGarrett D'Amore 		if (0 == strcmp(name, reg->key.p))
1397*698f87a4SGarrett D'Amore 			return(reg->val);
1398*698f87a4SGarrett D'Amore 
1399*698f87a4SGarrett D'Amore 	return(0);
1400*698f87a4SGarrett D'Amore }
1401*698f87a4SGarrett D'Amore 
1402*698f87a4SGarrett D'Amore static int
roff_getregn(const struct roff * r,const char * name,size_t len)1403*698f87a4SGarrett D'Amore roff_getregn(const struct roff *r, const char *name, size_t len)
1404*698f87a4SGarrett D'Amore {
1405*698f87a4SGarrett D'Amore 	struct roffreg	*reg;
1406*698f87a4SGarrett D'Amore 
1407*698f87a4SGarrett D'Amore 	for (reg = r->regtab; reg; reg = reg->next)
1408*698f87a4SGarrett D'Amore 		if (len == reg->key.sz &&
1409*698f87a4SGarrett D'Amore 		    0 == strncmp(name, reg->key.p, len))
1410*698f87a4SGarrett D'Amore 			return(reg->val);
1411*698f87a4SGarrett D'Amore 
1412*698f87a4SGarrett D'Amore 	return(0);
1413*698f87a4SGarrett D'Amore }
1414*698f87a4SGarrett D'Amore 
1415*698f87a4SGarrett D'Amore static void
roff_freereg(struct roffreg * reg)1416*698f87a4SGarrett D'Amore roff_freereg(struct roffreg *reg)
1417*698f87a4SGarrett D'Amore {
1418*698f87a4SGarrett D'Amore 	struct roffreg	*old_reg;
1419*698f87a4SGarrett D'Amore 
1420*698f87a4SGarrett D'Amore 	while (NULL != reg) {
1421*698f87a4SGarrett D'Amore 		free(reg->key.p);
1422*698f87a4SGarrett D'Amore 		old_reg = reg;
1423*698f87a4SGarrett D'Amore 		reg = reg->next;
1424*698f87a4SGarrett D'Amore 		free(old_reg);
1425*698f87a4SGarrett D'Amore 	}
142695c635efSGarrett D'Amore }
142795c635efSGarrett D'Amore 
142895c635efSGarrett D'Amore /* ARGSUSED */
142995c635efSGarrett D'Amore static enum rofferr
roff_nr(ROFF_ARGS)143095c635efSGarrett D'Amore roff_nr(ROFF_ARGS)
143195c635efSGarrett D'Amore {
143295c635efSGarrett D'Amore 	const char	*key;
143395c635efSGarrett D'Amore 	char		*val;
1434*698f87a4SGarrett D'Amore 	size_t		 sz;
143595c635efSGarrett D'Amore 	int		 iv;
1436*698f87a4SGarrett D'Amore 	char		 sign;
143795c635efSGarrett D'Amore 
143895c635efSGarrett D'Amore 	val = *bufp + pos;
143995c635efSGarrett D'Amore 	key = roff_getname(r, &val, ln, pos);
144095c635efSGarrett D'Amore 
1441*698f87a4SGarrett D'Amore 	sign = *val;
1442*698f87a4SGarrett D'Amore 	if ('+' == sign || '-' == sign)
1443*698f87a4SGarrett D'Amore 		val++;
1444*698f87a4SGarrett D'Amore 
1445*698f87a4SGarrett D'Amore 	sz = strspn(val, "0123456789");
1446*698f87a4SGarrett D'Amore 	iv = sz ? mandoc_strntoi(val, sz, 10) : 0;
1447*698f87a4SGarrett D'Amore 
1448*698f87a4SGarrett D'Amore 	roff_setreg(r, key, iv, sign);
144995c635efSGarrett D'Amore 
145095c635efSGarrett D'Amore 	return(ROFF_IGN);
145195c635efSGarrett D'Amore }
145295c635efSGarrett D'Amore 
145395c635efSGarrett D'Amore /* ARGSUSED */
145495c635efSGarrett D'Amore static enum rofferr
roff_rm(ROFF_ARGS)145595c635efSGarrett D'Amore roff_rm(ROFF_ARGS)
145695c635efSGarrett D'Amore {
145795c635efSGarrett D'Amore 	const char	 *name;
145895c635efSGarrett D'Amore 	char		 *cp;
145995c635efSGarrett D'Amore 
146095c635efSGarrett D'Amore 	cp = *bufp + pos;
146195c635efSGarrett D'Amore 	while ('\0' != *cp) {
146295c635efSGarrett D'Amore 		name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
146395c635efSGarrett D'Amore 		if ('\0' != *name)
146495c635efSGarrett D'Amore 			roff_setstr(r, name, NULL, 0);
146595c635efSGarrett D'Amore 	}
146695c635efSGarrett D'Amore 	return(ROFF_IGN);
146795c635efSGarrett D'Amore }
146895c635efSGarrett D'Amore 
146995c635efSGarrett D'Amore /* ARGSUSED */
147095c635efSGarrett D'Amore static enum rofferr
roff_it(ROFF_ARGS)1471*698f87a4SGarrett D'Amore roff_it(ROFF_ARGS)
1472*698f87a4SGarrett D'Amore {
1473*698f87a4SGarrett D'Amore 	char		*cp;
1474*698f87a4SGarrett D'Amore 	size_t		 len;
1475*698f87a4SGarrett D'Amore 	int		 iv;
1476*698f87a4SGarrett D'Amore 
1477*698f87a4SGarrett D'Amore 	/* Parse the number of lines. */
1478*698f87a4SGarrett D'Amore 	cp = *bufp + pos;
1479*698f87a4SGarrett D'Amore 	len = strcspn(cp, " \t");
1480*698f87a4SGarrett D'Amore 	cp[len] = '\0';
1481*698f87a4SGarrett D'Amore 	if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
1482*698f87a4SGarrett D'Amore 		mandoc_msg(MANDOCERR_NUMERIC, r->parse,
1483*698f87a4SGarrett D'Amore 				ln, ppos, *bufp + 1);
1484*698f87a4SGarrett D'Amore 		return(ROFF_IGN);
1485*698f87a4SGarrett D'Amore 	}
1486*698f87a4SGarrett D'Amore 	cp += len + 1;
1487*698f87a4SGarrett D'Amore 
1488*698f87a4SGarrett D'Amore 	/* Arm the input line trap. */
1489*698f87a4SGarrett D'Amore 	roffit_lines = iv;
1490*698f87a4SGarrett D'Amore 	roffit_macro = mandoc_strdup(cp);
1491*698f87a4SGarrett D'Amore 	return(ROFF_IGN);
1492*698f87a4SGarrett D'Amore }
1493*698f87a4SGarrett D'Amore 
1494*698f87a4SGarrett D'Amore /* ARGSUSED */
1495*698f87a4SGarrett D'Amore static enum rofferr
roff_Dd(ROFF_ARGS)1496*698f87a4SGarrett D'Amore roff_Dd(ROFF_ARGS)
1497*698f87a4SGarrett D'Amore {
1498*698f87a4SGarrett D'Amore 	const char *const	*cp;
1499*698f87a4SGarrett D'Amore 
1500*698f87a4SGarrett D'Amore 	if (MPARSE_MDOC != r->parsetype)
1501*698f87a4SGarrett D'Amore 		for (cp = __mdoc_reserved; *cp; cp++)
1502*698f87a4SGarrett D'Amore 			roff_setstr(r, *cp, NULL, 0);
1503*698f87a4SGarrett D'Amore 
1504*698f87a4SGarrett D'Amore 	return(ROFF_CONT);
1505*698f87a4SGarrett D'Amore }
1506*698f87a4SGarrett D'Amore 
1507*698f87a4SGarrett D'Amore /* ARGSUSED */
1508*698f87a4SGarrett D'Amore static enum rofferr
roff_TH(ROFF_ARGS)1509*698f87a4SGarrett D'Amore roff_TH(ROFF_ARGS)
1510*698f87a4SGarrett D'Amore {
1511*698f87a4SGarrett D'Amore 	const char *const	*cp;
1512*698f87a4SGarrett D'Amore 
1513*698f87a4SGarrett D'Amore 	if (MPARSE_MDOC != r->parsetype)
1514*698f87a4SGarrett D'Amore 		for (cp = __man_reserved; *cp; cp++)
1515*698f87a4SGarrett D'Amore 			roff_setstr(r, *cp, NULL, 0);
1516*698f87a4SGarrett D'Amore 
1517*698f87a4SGarrett D'Amore 	return(ROFF_CONT);
1518*698f87a4SGarrett D'Amore }
1519*698f87a4SGarrett D'Amore 
1520*698f87a4SGarrett D'Amore /* ARGSUSED */
1521*698f87a4SGarrett D'Amore static enum rofferr
roff_TE(ROFF_ARGS)152295c635efSGarrett D'Amore roff_TE(ROFF_ARGS)
152395c635efSGarrett D'Amore {
152495c635efSGarrett D'Amore 
152595c635efSGarrett D'Amore 	if (NULL == r->tbl)
152695c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
152795c635efSGarrett D'Amore 	else
152895c635efSGarrett D'Amore 		tbl_end(&r->tbl);
152995c635efSGarrett D'Amore 
153095c635efSGarrett D'Amore 	return(ROFF_IGN);
153195c635efSGarrett D'Amore }
153295c635efSGarrett D'Amore 
153395c635efSGarrett D'Amore /* ARGSUSED */
153495c635efSGarrett D'Amore static enum rofferr
roff_T_(ROFF_ARGS)153595c635efSGarrett D'Amore roff_T_(ROFF_ARGS)
153695c635efSGarrett D'Amore {
153795c635efSGarrett D'Amore 
153895c635efSGarrett D'Amore 	if (NULL == r->tbl)
153995c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
154095c635efSGarrett D'Amore 	else
154195c635efSGarrett D'Amore 		tbl_restart(ppos, ln, r->tbl);
154295c635efSGarrett D'Amore 
154395c635efSGarrett D'Amore 	return(ROFF_IGN);
154495c635efSGarrett D'Amore }
154595c635efSGarrett D'Amore 
154695c635efSGarrett D'Amore #if 0
154795c635efSGarrett D'Amore static int
154895c635efSGarrett D'Amore roff_closeeqn(struct roff *r)
154995c635efSGarrett D'Amore {
155095c635efSGarrett D'Amore 
155195c635efSGarrett D'Amore 	return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
155295c635efSGarrett D'Amore }
155395c635efSGarrett D'Amore #endif
155495c635efSGarrett D'Amore 
155595c635efSGarrett D'Amore static void
roff_openeqn(struct roff * r,const char * name,int line,int offs,const char * buf)155695c635efSGarrett D'Amore roff_openeqn(struct roff *r, const char *name, int line,
155795c635efSGarrett D'Amore 		int offs, const char *buf)
155895c635efSGarrett D'Amore {
155995c635efSGarrett D'Amore 	struct eqn_node *e;
156095c635efSGarrett D'Amore 	int		 poff;
156195c635efSGarrett D'Amore 
156295c635efSGarrett D'Amore 	assert(NULL == r->eqn);
156395c635efSGarrett D'Amore 	e = eqn_alloc(name, offs, line, r->parse);
156495c635efSGarrett D'Amore 
156595c635efSGarrett D'Amore 	if (r->last_eqn)
156695c635efSGarrett D'Amore 		r->last_eqn->next = e;
156795c635efSGarrett D'Amore 	else
156895c635efSGarrett D'Amore 		r->first_eqn = r->last_eqn = e;
156995c635efSGarrett D'Amore 
157095c635efSGarrett D'Amore 	r->eqn = r->last_eqn = e;
157195c635efSGarrett D'Amore 
157295c635efSGarrett D'Amore 	if (buf) {
157395c635efSGarrett D'Amore 		poff = 0;
157495c635efSGarrett D'Amore 		eqn_read(&r->eqn, line, buf, offs, &poff);
157595c635efSGarrett D'Amore 	}
157695c635efSGarrett D'Amore }
157795c635efSGarrett D'Amore 
157895c635efSGarrett D'Amore /* ARGSUSED */
157995c635efSGarrett D'Amore static enum rofferr
roff_EQ(ROFF_ARGS)158095c635efSGarrett D'Amore roff_EQ(ROFF_ARGS)
158195c635efSGarrett D'Amore {
158295c635efSGarrett D'Amore 
158395c635efSGarrett D'Amore 	roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
158495c635efSGarrett D'Amore 	return(ROFF_IGN);
158595c635efSGarrett D'Amore }
158695c635efSGarrett D'Amore 
158795c635efSGarrett D'Amore /* ARGSUSED */
158895c635efSGarrett D'Amore static enum rofferr
roff_EN(ROFF_ARGS)158995c635efSGarrett D'Amore roff_EN(ROFF_ARGS)
159095c635efSGarrett D'Amore {
159195c635efSGarrett D'Amore 
159295c635efSGarrett D'Amore 	mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
159395c635efSGarrett D'Amore 	return(ROFF_IGN);
159495c635efSGarrett D'Amore }
159595c635efSGarrett D'Amore 
159695c635efSGarrett D'Amore /* ARGSUSED */
159795c635efSGarrett D'Amore static enum rofferr
roff_TS(ROFF_ARGS)159895c635efSGarrett D'Amore roff_TS(ROFF_ARGS)
159995c635efSGarrett D'Amore {
1600*698f87a4SGarrett D'Amore 	struct tbl_node	*tbl;
160195c635efSGarrett D'Amore 
160295c635efSGarrett D'Amore 	if (r->tbl) {
160395c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
160495c635efSGarrett D'Amore 		tbl_end(&r->tbl);
160595c635efSGarrett D'Amore 	}
160695c635efSGarrett D'Amore 
1607*698f87a4SGarrett D'Amore 	tbl = tbl_alloc(ppos, ln, r->parse);
160895c635efSGarrett D'Amore 
160995c635efSGarrett D'Amore 	if (r->last_tbl)
1610*698f87a4SGarrett D'Amore 		r->last_tbl->next = tbl;
161195c635efSGarrett D'Amore 	else
1612*698f87a4SGarrett D'Amore 		r->first_tbl = r->last_tbl = tbl;
161395c635efSGarrett D'Amore 
1614*698f87a4SGarrett D'Amore 	r->tbl = r->last_tbl = tbl;
1615*698f87a4SGarrett D'Amore 	return(ROFF_IGN);
1616*698f87a4SGarrett D'Amore }
1617*698f87a4SGarrett D'Amore 
1618*698f87a4SGarrett D'Amore /* ARGSUSED */
1619*698f87a4SGarrett D'Amore static enum rofferr
roff_cc(ROFF_ARGS)1620*698f87a4SGarrett D'Amore roff_cc(ROFF_ARGS)
1621*698f87a4SGarrett D'Amore {
1622*698f87a4SGarrett D'Amore 	const char	*p;
1623*698f87a4SGarrett D'Amore 
1624*698f87a4SGarrett D'Amore 	p = *bufp + pos;
1625*698f87a4SGarrett D'Amore 
1626*698f87a4SGarrett D'Amore 	if ('\0' == *p || '.' == (r->control = *p++))
1627*698f87a4SGarrett D'Amore 		r->control = 0;
1628*698f87a4SGarrett D'Amore 
1629*698f87a4SGarrett D'Amore 	if ('\0' != *p)
1630*698f87a4SGarrett D'Amore 		mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
1631*698f87a4SGarrett D'Amore 
163295c635efSGarrett D'Amore 	return(ROFF_IGN);
163395c635efSGarrett D'Amore }
163495c635efSGarrett D'Amore 
163595c635efSGarrett D'Amore /* ARGSUSED */
163695c635efSGarrett D'Amore static enum rofferr
roff_tr(ROFF_ARGS)163795c635efSGarrett D'Amore roff_tr(ROFF_ARGS)
163895c635efSGarrett D'Amore {
163995c635efSGarrett D'Amore 	const char	*p, *first, *second;
164095c635efSGarrett D'Amore 	size_t		 fsz, ssz;
164195c635efSGarrett D'Amore 	enum mandoc_esc	 esc;
164295c635efSGarrett D'Amore 
164395c635efSGarrett D'Amore 	p = *bufp + pos;
164495c635efSGarrett D'Amore 
164595c635efSGarrett D'Amore 	if ('\0' == *p) {
164695c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
164795c635efSGarrett D'Amore 		return(ROFF_IGN);
164895c635efSGarrett D'Amore 	}
164995c635efSGarrett D'Amore 
165095c635efSGarrett D'Amore 	while ('\0' != *p) {
165195c635efSGarrett D'Amore 		fsz = ssz = 1;
165295c635efSGarrett D'Amore 
165395c635efSGarrett D'Amore 		first = p++;
165495c635efSGarrett D'Amore 		if ('\\' == *first) {
165595c635efSGarrett D'Amore 			esc = mandoc_escape(&p, NULL, NULL);
165695c635efSGarrett D'Amore 			if (ESCAPE_ERROR == esc) {
165795c635efSGarrett D'Amore 				mandoc_msg
165895c635efSGarrett D'Amore 					(MANDOCERR_BADESCAPE, r->parse,
165995c635efSGarrett D'Amore 					 ln, (int)(p - *bufp), NULL);
166095c635efSGarrett D'Amore 				return(ROFF_IGN);
166195c635efSGarrett D'Amore 			}
166295c635efSGarrett D'Amore 			fsz = (size_t)(p - first);
166395c635efSGarrett D'Amore 		}
166495c635efSGarrett D'Amore 
166595c635efSGarrett D'Amore 		second = p++;
166695c635efSGarrett D'Amore 		if ('\\' == *second) {
166795c635efSGarrett D'Amore 			esc = mandoc_escape(&p, NULL, NULL);
166895c635efSGarrett D'Amore 			if (ESCAPE_ERROR == esc) {
166995c635efSGarrett D'Amore 				mandoc_msg
167095c635efSGarrett D'Amore 					(MANDOCERR_BADESCAPE, r->parse,
167195c635efSGarrett D'Amore 					 ln, (int)(p - *bufp), NULL);
167295c635efSGarrett D'Amore 				return(ROFF_IGN);
167395c635efSGarrett D'Amore 			}
167495c635efSGarrett D'Amore 			ssz = (size_t)(p - second);
167595c635efSGarrett D'Amore 		} else if ('\0' == *second) {
167695c635efSGarrett D'Amore 			mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
167795c635efSGarrett D'Amore 					ln, (int)(p - *bufp), NULL);
167895c635efSGarrett D'Amore 			second = " ";
167995c635efSGarrett D'Amore 			p--;
168095c635efSGarrett D'Amore 		}
168195c635efSGarrett D'Amore 
168295c635efSGarrett D'Amore 		if (fsz > 1) {
168395c635efSGarrett D'Amore 			roff_setstrn(&r->xmbtab, first,
168495c635efSGarrett D'Amore 					fsz, second, ssz, 0);
168595c635efSGarrett D'Amore 			continue;
168695c635efSGarrett D'Amore 		}
168795c635efSGarrett D'Amore 
168895c635efSGarrett D'Amore 		if (NULL == r->xtab)
168995c635efSGarrett D'Amore 			r->xtab = mandoc_calloc
169095c635efSGarrett D'Amore 				(128, sizeof(struct roffstr));
169195c635efSGarrett D'Amore 
169295c635efSGarrett D'Amore 		free(r->xtab[(int)*first].p);
169395c635efSGarrett D'Amore 		r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
169495c635efSGarrett D'Amore 		r->xtab[(int)*first].sz = ssz;
169595c635efSGarrett D'Amore 	}
169695c635efSGarrett D'Amore 
169795c635efSGarrett D'Amore 	return(ROFF_IGN);
169895c635efSGarrett D'Amore }
169995c635efSGarrett D'Amore 
170095c635efSGarrett D'Amore /* ARGSUSED */
170195c635efSGarrett D'Amore static enum rofferr
roff_so(ROFF_ARGS)170295c635efSGarrett D'Amore roff_so(ROFF_ARGS)
170395c635efSGarrett D'Amore {
170495c635efSGarrett D'Amore 	char *name;
170595c635efSGarrett D'Amore 
170695c635efSGarrett D'Amore 	mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
170795c635efSGarrett D'Amore 
170895c635efSGarrett D'Amore 	/*
170995c635efSGarrett D'Amore 	 * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
171095c635efSGarrett D'Amore 	 * opening anything that's not in our cwd or anything beneath
171195c635efSGarrett D'Amore 	 * it.  Thus, explicitly disallow traversing up the file-system
171295c635efSGarrett D'Amore 	 * or using absolute paths.
171395c635efSGarrett D'Amore 	 */
171495c635efSGarrett D'Amore 
171595c635efSGarrett D'Amore 	name = *bufp + pos;
171695c635efSGarrett D'Amore 	if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
171795c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
171895c635efSGarrett D'Amore 		return(ROFF_ERR);
171995c635efSGarrett D'Amore 	}
172095c635efSGarrett D'Amore 
172195c635efSGarrett D'Amore 	*offs = pos;
172295c635efSGarrett D'Amore 	return(ROFF_SO);
172395c635efSGarrett D'Amore }
172495c635efSGarrett D'Amore 
172595c635efSGarrett D'Amore /* ARGSUSED */
172695c635efSGarrett D'Amore static enum rofferr
roff_userdef(ROFF_ARGS)172795c635efSGarrett D'Amore roff_userdef(ROFF_ARGS)
172895c635efSGarrett D'Amore {
172995c635efSGarrett D'Amore 	const char	 *arg[9];
173095c635efSGarrett D'Amore 	char		 *cp, *n1, *n2;
173195c635efSGarrett D'Amore 	int		  i;
173295c635efSGarrett D'Amore 
173395c635efSGarrett D'Amore 	/*
173495c635efSGarrett D'Amore 	 * Collect pointers to macro argument strings
1735*698f87a4SGarrett D'Amore 	 * and NUL-terminate them.
173695c635efSGarrett D'Amore 	 */
173795c635efSGarrett D'Amore 	cp = *bufp + pos;
173895c635efSGarrett D'Amore 	for (i = 0; i < 9; i++)
173995c635efSGarrett D'Amore 		arg[i] = '\0' == *cp ? "" :
174095c635efSGarrett D'Amore 		    mandoc_getarg(r->parse, &cp, ln, &pos);
174195c635efSGarrett D'Amore 
174295c635efSGarrett D'Amore 	/*
174395c635efSGarrett D'Amore 	 * Expand macro arguments.
174495c635efSGarrett D'Amore 	 */
174595c635efSGarrett D'Amore 	*szp = 0;
174695c635efSGarrett D'Amore 	n1 = cp = mandoc_strdup(r->current_string);
174795c635efSGarrett D'Amore 	while (NULL != (cp = strstr(cp, "\\$"))) {
174895c635efSGarrett D'Amore 		i = cp[2] - '1';
174995c635efSGarrett D'Amore 		if (0 > i || 8 < i) {
175095c635efSGarrett D'Amore 			/* Not an argument invocation. */
175195c635efSGarrett D'Amore 			cp += 2;
175295c635efSGarrett D'Amore 			continue;
175395c635efSGarrett D'Amore 		}
175495c635efSGarrett D'Amore 
175595c635efSGarrett D'Amore 		*szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
175695c635efSGarrett D'Amore 		n2 = mandoc_malloc(*szp);
175795c635efSGarrett D'Amore 
175895c635efSGarrett D'Amore 		strlcpy(n2, n1, (size_t)(cp - n1 + 1));
175995c635efSGarrett D'Amore 		strlcat(n2, arg[i], *szp);
176095c635efSGarrett D'Amore 		strlcat(n2, cp + 3, *szp);
176195c635efSGarrett D'Amore 
176295c635efSGarrett D'Amore 		cp = n2 + (cp - n1);
176395c635efSGarrett D'Amore 		free(n1);
176495c635efSGarrett D'Amore 		n1 = n2;
176595c635efSGarrett D'Amore 	}
176695c635efSGarrett D'Amore 
176795c635efSGarrett D'Amore 	/*
176895c635efSGarrett D'Amore 	 * Replace the macro invocation
176995c635efSGarrett D'Amore 	 * by the expanded macro.
177095c635efSGarrett D'Amore 	 */
177195c635efSGarrett D'Amore 	free(*bufp);
177295c635efSGarrett D'Amore 	*bufp = n1;
177395c635efSGarrett D'Amore 	if (0 == *szp)
177495c635efSGarrett D'Amore 		*szp = strlen(*bufp) + 1;
177595c635efSGarrett D'Amore 
177695c635efSGarrett D'Amore 	return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
177795c635efSGarrett D'Amore 	   ROFF_REPARSE : ROFF_APPEND);
177895c635efSGarrett D'Amore }
177995c635efSGarrett D'Amore 
178095c635efSGarrett D'Amore static char *
roff_getname(struct roff * r,char ** cpp,int ln,int pos)178195c635efSGarrett D'Amore roff_getname(struct roff *r, char **cpp, int ln, int pos)
178295c635efSGarrett D'Amore {
178395c635efSGarrett D'Amore 	char	 *name, *cp;
178495c635efSGarrett D'Amore 
178595c635efSGarrett D'Amore 	name = *cpp;
178695c635efSGarrett D'Amore 	if ('\0' == *name)
178795c635efSGarrett D'Amore 		return(name);
178895c635efSGarrett D'Amore 
178995c635efSGarrett D'Amore 	/* Read until end of name. */
179095c635efSGarrett D'Amore 	for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
179195c635efSGarrett D'Amore 		if ('\\' != *cp)
179295c635efSGarrett D'Amore 			continue;
179395c635efSGarrett D'Amore 		cp++;
179495c635efSGarrett D'Amore 		if ('\\' == *cp)
179595c635efSGarrett D'Amore 			continue;
179695c635efSGarrett D'Amore 		mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
179795c635efSGarrett D'Amore 		*cp = '\0';
179895c635efSGarrett D'Amore 		name = cp;
179995c635efSGarrett D'Amore 	}
180095c635efSGarrett D'Amore 
180195c635efSGarrett D'Amore 	/* Nil-terminate name. */
180295c635efSGarrett D'Amore 	if ('\0' != *cp)
180395c635efSGarrett D'Amore 		*(cp++) = '\0';
180495c635efSGarrett D'Amore 
180595c635efSGarrett D'Amore 	/* Read past spaces. */
180695c635efSGarrett D'Amore 	while (' ' == *cp)
180795c635efSGarrett D'Amore 		cp++;
180895c635efSGarrett D'Amore 
180995c635efSGarrett D'Amore 	*cpp = cp;
181095c635efSGarrett D'Amore 	return(name);
181195c635efSGarrett D'Amore }
181295c635efSGarrett D'Amore 
181395c635efSGarrett D'Amore /*
181495c635efSGarrett D'Amore  * Store *string into the user-defined string called *name.
181595c635efSGarrett D'Amore  * In multiline mode, append to an existing entry and append '\n';
181695c635efSGarrett D'Amore  * else replace the existing entry, if there is one.
181795c635efSGarrett D'Amore  * To clear an existing entry, call with (*r, *name, NULL, 0).
181895c635efSGarrett D'Amore  */
181995c635efSGarrett D'Amore static void
roff_setstr(struct roff * r,const char * name,const char * string,int multiline)182095c635efSGarrett D'Amore roff_setstr(struct roff *r, const char *name, const char *string,
182195c635efSGarrett D'Amore 	int multiline)
182295c635efSGarrett D'Amore {
182395c635efSGarrett D'Amore 
182495c635efSGarrett D'Amore 	roff_setstrn(&r->strtab, name, strlen(name), string,
182595c635efSGarrett D'Amore 			string ? strlen(string) : 0, multiline);
182695c635efSGarrett D'Amore }
182795c635efSGarrett D'Amore 
182895c635efSGarrett D'Amore static void
roff_setstrn(struct roffkv ** r,const char * name,size_t namesz,const char * string,size_t stringsz,int multiline)182995c635efSGarrett D'Amore roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
183095c635efSGarrett D'Amore 		const char *string, size_t stringsz, int multiline)
183195c635efSGarrett D'Amore {
183295c635efSGarrett D'Amore 	struct roffkv	*n;
183395c635efSGarrett D'Amore 	char		*c;
183495c635efSGarrett D'Amore 	int		 i;
183595c635efSGarrett D'Amore 	size_t		 oldch, newch;
183695c635efSGarrett D'Amore 
183795c635efSGarrett D'Amore 	/* Search for an existing string with the same name. */
183895c635efSGarrett D'Amore 	n = *r;
183995c635efSGarrett D'Amore 
184095c635efSGarrett D'Amore 	while (n && strcmp(name, n->key.p))
184195c635efSGarrett D'Amore 		n = n->next;
184295c635efSGarrett D'Amore 
184395c635efSGarrett D'Amore 	if (NULL == n) {
184495c635efSGarrett D'Amore 		/* Create a new string table entry. */
184595c635efSGarrett D'Amore 		n = mandoc_malloc(sizeof(struct roffkv));
184695c635efSGarrett D'Amore 		n->key.p = mandoc_strndup(name, namesz);
184795c635efSGarrett D'Amore 		n->key.sz = namesz;
184895c635efSGarrett D'Amore 		n->val.p = NULL;
184995c635efSGarrett D'Amore 		n->val.sz = 0;
185095c635efSGarrett D'Amore 		n->next = *r;
185195c635efSGarrett D'Amore 		*r = n;
185295c635efSGarrett D'Amore 	} else if (0 == multiline) {
185395c635efSGarrett D'Amore 		/* In multiline mode, append; else replace. */
185495c635efSGarrett D'Amore 		free(n->val.p);
185595c635efSGarrett D'Amore 		n->val.p = NULL;
185695c635efSGarrett D'Amore 		n->val.sz = 0;
185795c635efSGarrett D'Amore 	}
185895c635efSGarrett D'Amore 
185995c635efSGarrett D'Amore 	if (NULL == string)
186095c635efSGarrett D'Amore 		return;
186195c635efSGarrett D'Amore 
186295c635efSGarrett D'Amore 	/*
186395c635efSGarrett D'Amore 	 * One additional byte for the '\n' in multiline mode,
186495c635efSGarrett D'Amore 	 * and one for the terminating '\0'.
186595c635efSGarrett D'Amore 	 */
186695c635efSGarrett D'Amore 	newch = stringsz + (multiline ? 2u : 1u);
186795c635efSGarrett D'Amore 
186895c635efSGarrett D'Amore 	if (NULL == n->val.p) {
186995c635efSGarrett D'Amore 		n->val.p = mandoc_malloc(newch);
187095c635efSGarrett D'Amore 		*n->val.p = '\0';
187195c635efSGarrett D'Amore 		oldch = 0;
187295c635efSGarrett D'Amore 	} else {
187395c635efSGarrett D'Amore 		oldch = n->val.sz;
187495c635efSGarrett D'Amore 		n->val.p = mandoc_realloc(n->val.p, oldch + newch);
187595c635efSGarrett D'Amore 	}
187695c635efSGarrett D'Amore 
187795c635efSGarrett D'Amore 	/* Skip existing content in the destination buffer. */
187895c635efSGarrett D'Amore 	c = n->val.p + (int)oldch;
187995c635efSGarrett D'Amore 
188095c635efSGarrett D'Amore 	/* Append new content to the destination buffer. */
188195c635efSGarrett D'Amore 	i = 0;
188295c635efSGarrett D'Amore 	while (i < (int)stringsz) {
188395c635efSGarrett D'Amore 		/*
188495c635efSGarrett D'Amore 		 * Rudimentary roff copy mode:
188595c635efSGarrett D'Amore 		 * Handle escaped backslashes.
188695c635efSGarrett D'Amore 		 */
188795c635efSGarrett D'Amore 		if ('\\' == string[i] && '\\' == string[i + 1])
188895c635efSGarrett D'Amore 			i++;
188995c635efSGarrett D'Amore 		*c++ = string[i++];
189095c635efSGarrett D'Amore 	}
189195c635efSGarrett D'Amore 
189295c635efSGarrett D'Amore 	/* Append terminating bytes. */
189395c635efSGarrett D'Amore 	if (multiline)
189495c635efSGarrett D'Amore 		*c++ = '\n';
189595c635efSGarrett D'Amore 
189695c635efSGarrett D'Amore 	*c = '\0';
189795c635efSGarrett D'Amore 	n->val.sz = (int)(c - n->val.p);
189895c635efSGarrett D'Amore }
189995c635efSGarrett D'Amore 
190095c635efSGarrett D'Amore static const char *
roff_getstrn(const struct roff * r,const char * name,size_t len)190195c635efSGarrett D'Amore roff_getstrn(const struct roff *r, const char *name, size_t len)
190295c635efSGarrett D'Amore {
190395c635efSGarrett D'Amore 	const struct roffkv *n;
190495c635efSGarrett D'Amore 
190595c635efSGarrett D'Amore 	for (n = r->strtab; n; n = n->next)
190695c635efSGarrett D'Amore 		if (0 == strncmp(name, n->key.p, len) &&
190795c635efSGarrett D'Amore 				'\0' == n->key.p[(int)len])
190895c635efSGarrett D'Amore 			return(n->val.p);
190995c635efSGarrett D'Amore 
191095c635efSGarrett D'Amore 	return(NULL);
191195c635efSGarrett D'Amore }
191295c635efSGarrett D'Amore 
191395c635efSGarrett D'Amore static void
roff_freestr(struct roffkv * r)191495c635efSGarrett D'Amore roff_freestr(struct roffkv *r)
191595c635efSGarrett D'Amore {
191695c635efSGarrett D'Amore 	struct roffkv	 *n, *nn;
191795c635efSGarrett D'Amore 
191895c635efSGarrett D'Amore 	for (n = r; n; n = nn) {
191995c635efSGarrett D'Amore 		free(n->key.p);
192095c635efSGarrett D'Amore 		free(n->val.p);
192195c635efSGarrett D'Amore 		nn = n->next;
192295c635efSGarrett D'Amore 		free(n);
192395c635efSGarrett D'Amore 	}
192495c635efSGarrett D'Amore }
192595c635efSGarrett D'Amore 
192695c635efSGarrett D'Amore const struct tbl_span *
roff_span(const struct roff * r)192795c635efSGarrett D'Amore roff_span(const struct roff *r)
192895c635efSGarrett D'Amore {
192995c635efSGarrett D'Amore 
193095c635efSGarrett D'Amore 	return(r->tbl ? tbl_span(r->tbl) : NULL);
193195c635efSGarrett D'Amore }
193295c635efSGarrett D'Amore 
193395c635efSGarrett D'Amore const struct eqn *
roff_eqn(const struct roff * r)193495c635efSGarrett D'Amore roff_eqn(const struct roff *r)
193595c635efSGarrett D'Amore {
193695c635efSGarrett D'Amore 
193795c635efSGarrett D'Amore 	return(r->last_eqn ? &r->last_eqn->eqn : NULL);
193895c635efSGarrett D'Amore }
193995c635efSGarrett D'Amore 
194095c635efSGarrett D'Amore /*
194195c635efSGarrett D'Amore  * Duplicate an input string, making the appropriate character
194295c635efSGarrett D'Amore  * conversations (as stipulated by `tr') along the way.
194395c635efSGarrett D'Amore  * Returns a heap-allocated string with all the replacements made.
194495c635efSGarrett D'Amore  */
194595c635efSGarrett D'Amore char *
roff_strdup(const struct roff * r,const char * p)194695c635efSGarrett D'Amore roff_strdup(const struct roff *r, const char *p)
194795c635efSGarrett D'Amore {
194895c635efSGarrett D'Amore 	const struct roffkv *cp;
194995c635efSGarrett D'Amore 	char		*res;
195095c635efSGarrett D'Amore 	const char	*pp;
195195c635efSGarrett D'Amore 	size_t		 ssz, sz;
195295c635efSGarrett D'Amore 	enum mandoc_esc	 esc;
195395c635efSGarrett D'Amore 
195495c635efSGarrett D'Amore 	if (NULL == r->xmbtab && NULL == r->xtab)
195595c635efSGarrett D'Amore 		return(mandoc_strdup(p));
195695c635efSGarrett D'Amore 	else if ('\0' == *p)
195795c635efSGarrett D'Amore 		return(mandoc_strdup(""));
195895c635efSGarrett D'Amore 
195995c635efSGarrett D'Amore 	/*
196095c635efSGarrett D'Amore 	 * Step through each character looking for term matches
196195c635efSGarrett D'Amore 	 * (remember that a `tr' can be invoked with an escape, which is
196295c635efSGarrett D'Amore 	 * a glyph but the escape is multi-character).
196395c635efSGarrett D'Amore 	 * We only do this if the character hash has been initialised
196495c635efSGarrett D'Amore 	 * and the string is >0 length.
196595c635efSGarrett D'Amore 	 */
196695c635efSGarrett D'Amore 
196795c635efSGarrett D'Amore 	res = NULL;
196895c635efSGarrett D'Amore 	ssz = 0;
196995c635efSGarrett D'Amore 
197095c635efSGarrett D'Amore 	while ('\0' != *p) {
197195c635efSGarrett D'Amore 		if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
197295c635efSGarrett D'Amore 			sz = r->xtab[(int)*p].sz;
197395c635efSGarrett D'Amore 			res = mandoc_realloc(res, ssz + sz + 1);
197495c635efSGarrett D'Amore 			memcpy(res + ssz, r->xtab[(int)*p].p, sz);
197595c635efSGarrett D'Amore 			ssz += sz;
197695c635efSGarrett D'Amore 			p++;
197795c635efSGarrett D'Amore 			continue;
197895c635efSGarrett D'Amore 		} else if ('\\' != *p) {
197995c635efSGarrett D'Amore 			res = mandoc_realloc(res, ssz + 2);
198095c635efSGarrett D'Amore 			res[ssz++] = *p++;
198195c635efSGarrett D'Amore 			continue;
198295c635efSGarrett D'Amore 		}
198395c635efSGarrett D'Amore 
198495c635efSGarrett D'Amore 		/* Search for term matches. */
198595c635efSGarrett D'Amore 		for (cp = r->xmbtab; cp; cp = cp->next)
198695c635efSGarrett D'Amore 			if (0 == strncmp(p, cp->key.p, cp->key.sz))
198795c635efSGarrett D'Amore 				break;
198895c635efSGarrett D'Amore 
198995c635efSGarrett D'Amore 		if (NULL != cp) {
199095c635efSGarrett D'Amore 			/*
199195c635efSGarrett D'Amore 			 * A match has been found.
199295c635efSGarrett D'Amore 			 * Append the match to the array and move
199395c635efSGarrett D'Amore 			 * forward by its keysize.
199495c635efSGarrett D'Amore 			 */
199595c635efSGarrett D'Amore 			res = mandoc_realloc
199695c635efSGarrett D'Amore 				(res, ssz + cp->val.sz + 1);
199795c635efSGarrett D'Amore 			memcpy(res + ssz, cp->val.p, cp->val.sz);
199895c635efSGarrett D'Amore 			ssz += cp->val.sz;
199995c635efSGarrett D'Amore 			p += (int)cp->key.sz;
200095c635efSGarrett D'Amore 			continue;
200195c635efSGarrett D'Amore 		}
200295c635efSGarrett D'Amore 
200395c635efSGarrett D'Amore 		/*
200495c635efSGarrett D'Amore 		 * Handle escapes carefully: we need to copy
200595c635efSGarrett D'Amore 		 * over just the escape itself, or else we might
200695c635efSGarrett D'Amore 		 * do replacements within the escape itself.
200795c635efSGarrett D'Amore 		 * Make sure to pass along the bogus string.
200895c635efSGarrett D'Amore 		 */
200995c635efSGarrett D'Amore 		pp = p++;
201095c635efSGarrett D'Amore 		esc = mandoc_escape(&p, NULL, NULL);
201195c635efSGarrett D'Amore 		if (ESCAPE_ERROR == esc) {
201295c635efSGarrett D'Amore 			sz = strlen(pp);
201395c635efSGarrett D'Amore 			res = mandoc_realloc(res, ssz + sz + 1);
201495c635efSGarrett D'Amore 			memcpy(res + ssz, pp, sz);
201595c635efSGarrett D'Amore 			break;
201695c635efSGarrett D'Amore 		}
201795c635efSGarrett D'Amore 		/*
201895c635efSGarrett D'Amore 		 * We bail out on bad escapes.
201995c635efSGarrett D'Amore 		 * No need to warn: we already did so when
202095c635efSGarrett D'Amore 		 * roff_res() was called.
202195c635efSGarrett D'Amore 		 */
202295c635efSGarrett D'Amore 		sz = (int)(p - pp);
202395c635efSGarrett D'Amore 		res = mandoc_realloc(res, ssz + sz + 1);
202495c635efSGarrett D'Amore 		memcpy(res + ssz, pp, sz);
202595c635efSGarrett D'Amore 		ssz += sz;
202695c635efSGarrett D'Amore 	}
202795c635efSGarrett D'Amore 
202895c635efSGarrett D'Amore 	res[(int)ssz] = '\0';
202995c635efSGarrett D'Amore 	return(res);
203095c635efSGarrett D'Amore }
2031*698f87a4SGarrett D'Amore 
2032*698f87a4SGarrett D'Amore /*
2033*698f87a4SGarrett D'Amore  * Find out whether a line is a macro line or not.
2034*698f87a4SGarrett D'Amore  * If it is, adjust the current position and return one; if it isn't,
2035*698f87a4SGarrett D'Amore  * return zero and don't change the current position.
2036*698f87a4SGarrett D'Amore  * If the control character has been set with `.cc', then let that grain
2037*698f87a4SGarrett D'Amore  * precedence.
2038*698f87a4SGarrett D'Amore  * This is slighly contrary to groff, where using the non-breaking
2039*698f87a4SGarrett D'Amore  * control character when `cc' has been invoked will cause the
2040*698f87a4SGarrett D'Amore  * non-breaking macro contents to be printed verbatim.
2041*698f87a4SGarrett D'Amore  */
2042*698f87a4SGarrett D'Amore int
roff_getcontrol(const struct roff * r,const char * cp,int * ppos)2043*698f87a4SGarrett D'Amore roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
2044*698f87a4SGarrett D'Amore {
2045*698f87a4SGarrett D'Amore 	int		pos;
2046*698f87a4SGarrett D'Amore 
2047*698f87a4SGarrett D'Amore 	pos = *ppos;
2048*698f87a4SGarrett D'Amore 
2049*698f87a4SGarrett D'Amore 	if (0 != r->control && cp[pos] == r->control)
2050*698f87a4SGarrett D'Amore 		pos++;
2051*698f87a4SGarrett D'Amore 	else if (0 != r->control)
2052*698f87a4SGarrett D'Amore 		return(0);
2053*698f87a4SGarrett D'Amore 	else if ('\\' == cp[pos] && '.' == cp[pos + 1])
2054*698f87a4SGarrett D'Amore 		pos += 2;
2055*698f87a4SGarrett D'Amore 	else if ('.' == cp[pos] || '\'' == cp[pos])
2056*698f87a4SGarrett D'Amore 		pos++;
2057*698f87a4SGarrett D'Amore 	else
2058*698f87a4SGarrett D'Amore 		return(0);
2059*698f87a4SGarrett D'Amore 
2060*698f87a4SGarrett D'Amore 	while (' ' == cp[pos] || '\t' == cp[pos])
2061*698f87a4SGarrett D'Amore 		pos++;
2062*698f87a4SGarrett D'Amore 
2063*698f87a4SGarrett D'Amore 	*ppos = pos;
2064*698f87a4SGarrett D'Amore 	return(1);
2065*698f87a4SGarrett D'Amore }
2066