]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.205 2014/04/07 21:00:08 schwarze Exp $ */
3 * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "mandoc_aux.h"
31 #include "libmandoc.h"
33 /* Maximum number of nested if-else conditionals. */
34 #define RSTACK_MAX 128
36 /* Maximum number of string expansions per line, to break infinite loops. */
37 #define EXPAND_LIMIT 1000
82 * An incredibly-simple string buffer.
85 char *p
; /* nil-terminated buffer */
86 size_t sz
; /* saved strlen(p) */
90 * A key-value roffstr pair as part of a singly-linked list.
95 struct roffkv
*next
; /* next in list */
99 * A single number register as part of a singly-linked list.
104 struct roffreg
*next
;
108 struct mparse
*parse
; /* parse point */
109 int options
; /* parse options */
110 struct roffnode
*last
; /* leaf of stack */
111 int rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
112 char control
; /* control character */
113 int rstackpos
; /* position in rstack */
114 struct roffreg
*regtab
; /* number registers */
115 struct roffkv
*strtab
; /* user-defined strings & macros */
116 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
117 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
118 const char *current_string
; /* value of last called user macro */
119 struct tbl_node
*first_tbl
; /* first table parsed */
120 struct tbl_node
*last_tbl
; /* last table parsed */
121 struct tbl_node
*tbl
; /* current table being parsed */
122 struct eqn_node
*last_eqn
; /* last equation parsed */
123 struct eqn_node
*first_eqn
; /* first equation parsed */
124 struct eqn_node
*eqn
; /* current equation being parsed */
128 enum rofft tok
; /* type of node */
129 struct roffnode
*parent
; /* up one in stack */
130 int line
; /* parse line */
131 int col
; /* parse col */
132 char *name
; /* node name, e.g. macro name */
133 char *end
; /* end-rules: custom token */
134 int endspan
; /* end-rules: next-line or infty */
135 int rule
; /* current evaluation rule */
138 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
139 enum rofft tok, /* tok of macro */ \
140 char **bufp, /* input buffer */ \
141 size_t *szp, /* size of input buffer */ \
142 int ln, /* parse line */ \
143 int ppos, /* original pos in buffer */ \
144 int pos, /* current pos in buffer */ \
145 int *offs /* reset offset of buffer data */
147 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
150 const char *name
; /* macro name */
151 roffproc proc
; /* process new macro */
152 roffproc text
; /* process as child text of macro */
153 roffproc sub
; /* process as child of macro */
155 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
156 struct roffmac
*next
;
160 const char *name
; /* predefined input name */
161 const char *str
; /* replacement symbol */
164 #define PREDEF(__name, __str) \
165 { (__name), (__str) },
167 static enum rofft
roffhash_find(const char *, size_t);
168 static void roffhash_init(void);
169 static void roffnode_cleanscope(struct roff
*);
170 static void roffnode_pop(struct roff
*);
171 static void roffnode_push(struct roff
*, enum rofft
,
172 const char *, int, int);
173 static enum rofferr
roff_block(ROFF_ARGS
);
174 static enum rofferr
roff_block_text(ROFF_ARGS
);
175 static enum rofferr
roff_block_sub(ROFF_ARGS
);
176 static enum rofferr
roff_cblock(ROFF_ARGS
);
177 static enum rofferr
roff_cc(ROFF_ARGS
);
178 static void roff_ccond(struct roff
*, int, int);
179 static enum rofferr
roff_cond(ROFF_ARGS
);
180 static enum rofferr
roff_cond_text(ROFF_ARGS
);
181 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
182 static enum rofferr
roff_ds(ROFF_ARGS
);
183 static int roff_evalcond(const char *, int *);
184 static int roff_evalnum(const char *, int *, int *, int);
185 static int roff_evalpar(const char *, int *, int *);
186 static int roff_evalstrcond(const char *, int *);
187 static void roff_free1(struct roff
*);
188 static void roff_freereg(struct roffreg
*);
189 static void roff_freestr(struct roffkv
*);
190 static char *roff_getname(struct roff
*, char **, int, int);
191 static int roff_getnum(const char *, int *, int *);
192 static int roff_getop(const char *, int *, char *);
193 static int roff_getregn(const struct roff
*,
194 const char *, size_t);
195 static int roff_getregro(const char *name
);
196 static const char *roff_getstrn(const struct roff
*,
197 const char *, size_t);
198 static enum rofferr
roff_it(ROFF_ARGS
);
199 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
200 static enum rofferr
roff_nr(ROFF_ARGS
);
201 static void roff_openeqn(struct roff
*, const char *,
202 int, int, const char *);
203 static enum rofft
roff_parse(struct roff
*, const char *, int *);
204 static enum rofferr
roff_parsetext(char **, size_t *, int, int *);
205 static enum rofferr
roff_res(struct roff
*,
206 char **, size_t *, int, int);
207 static enum rofferr
roff_rm(ROFF_ARGS
);
208 static enum rofferr
roff_rr(ROFF_ARGS
);
209 static void roff_setstr(struct roff
*,
210 const char *, const char *, int);
211 static void roff_setstrn(struct roffkv
**, const char *,
212 size_t, const char *, size_t, int);
213 static enum rofferr
roff_so(ROFF_ARGS
);
214 static enum rofferr
roff_tr(ROFF_ARGS
);
215 static enum rofferr
roff_Dd(ROFF_ARGS
);
216 static enum rofferr
roff_TH(ROFF_ARGS
);
217 static enum rofferr
roff_TE(ROFF_ARGS
);
218 static enum rofferr
roff_TS(ROFF_ARGS
);
219 static enum rofferr
roff_EQ(ROFF_ARGS
);
220 static enum rofferr
roff_EN(ROFF_ARGS
);
221 static enum rofferr
roff_T_(ROFF_ARGS
);
222 static enum rofferr
roff_userdef(ROFF_ARGS
);
224 /* See roffhash_find() */
228 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
230 static struct roffmac
*hash
[HASHWIDTH
];
232 static struct roffmac roffs
[ROFF_MAX
] = {
233 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
234 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
235 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
236 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
237 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
238 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
239 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
240 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
241 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
242 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
243 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
244 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
245 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
246 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
247 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
248 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
249 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
250 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
251 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
252 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
253 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
254 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
255 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
256 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
257 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
258 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
259 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
260 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
261 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
262 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
263 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
264 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
265 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
266 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
267 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
268 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
269 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
270 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
273 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
274 const char *const __mdoc_reserved
[] = {
275 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
276 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
277 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
278 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
279 "Dt", "Dv", "Dx", "D1",
280 "Ec", "Ed", "Ef", "Ek", "El", "Em",
281 "En", "Eo", "Er", "Es", "Ev", "Ex",
282 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
283 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
284 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
285 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
286 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
287 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
288 "Sc", "Sh", "Sm", "So", "Sq",
289 "Ss", "St", "Sx", "Sy",
290 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
291 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
292 "%P", "%Q", "%R", "%T", "%U", "%V",
296 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
297 const char *const __man_reserved
[] = {
298 "AT", "B", "BI", "BR", "DT",
299 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
300 "LP", "OP", "P", "PD", "PP",
301 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
302 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
306 /* Array of injected predefined strings. */
307 #define PREDEFS_MAX 38
308 static const struct predef predefs
[PREDEFS_MAX
] = {
309 #include "predefs.in"
312 /* See roffhash_find() */
313 #define ROFF_HASH(p) (p[0] - ASCII_LO)
315 static int roffit_lines
; /* number of lines to delay */
316 static char *roffit_macro
; /* nil-terminated macro line */
324 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
325 assert(roffs
[i
].name
[0] >= ASCII_LO
);
326 assert(roffs
[i
].name
[0] <= ASCII_HI
);
328 buc
= ROFF_HASH(roffs
[i
].name
);
330 if (NULL
!= (n
= hash
[buc
])) {
331 for ( ; n
->next
; n
= n
->next
)
335 hash
[buc
] = &roffs
[i
];
340 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
341 * the nil-terminated string name could be found.
344 roffhash_find(const char *p
, size_t s
)
350 * libroff has an extremely simple hashtable, for the time
351 * being, which simply keys on the first character, which must
352 * be printable, then walks a chain. It works well enough until
356 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
361 if (NULL
== (n
= hash
[buc
]))
363 for ( ; n
; n
= n
->next
)
364 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
365 return((enum rofft
)(n
- roffs
));
372 * Pop the current node off of the stack of roff instructions currently
376 roffnode_pop(struct roff
*r
)
383 r
->last
= r
->last
->parent
;
391 * Push a roff node onto the instruction stack. This must later be
392 * removed with roffnode_pop().
395 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
400 p
= mandoc_calloc(1, sizeof(struct roffnode
));
403 p
->name
= mandoc_strdup(name
);
407 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
414 roff_free1(struct roff
*r
)
416 struct tbl_node
*tbl
;
420 while (NULL
!= (tbl
= r
->first_tbl
)) {
421 r
->first_tbl
= tbl
->next
;
425 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
427 while (NULL
!= (e
= r
->first_eqn
)) {
428 r
->first_eqn
= e
->next
;
432 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
437 roff_freestr(r
->strtab
);
438 roff_freestr(r
->xmbtab
);
440 r
->strtab
= r
->xmbtab
= NULL
;
442 roff_freereg(r
->regtab
);
447 for (i
= 0; i
< 128; i
++)
455 roff_reset(struct roff
*r
)
464 roff_free(struct roff
*r
)
473 roff_alloc(struct mparse
*parse
, int options
)
477 r
= mandoc_calloc(1, sizeof(struct roff
));
479 r
->options
= options
;
488 * In the current line, expand user-defined strings ("\*")
489 * and references to number registers ("\n").
490 * Also check the syntax of other escape sequences.
493 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
495 char ubuf
[12]; /* buffer to print the number */
496 const char *start
; /* start of the string to process */
497 const char *stesc
; /* start of an escape sequence ('\\') */
498 const char *stnam
; /* start of the name, after "[(*" */
499 const char *cp
; /* end of the name, e.g. before ']' */
500 const char *res
; /* the string to be substituted */
501 char *nbuf
; /* new buffer to copy bufp to */
502 size_t maxl
; /* expected length of the escape name */
503 size_t naml
; /* actual length of the escape name */
504 size_t ressz
; /* size of the replacement string */
505 int expand_count
; /* to avoid infinite loops */
509 stesc
= strchr(start
, '\0') - 1;
510 while (stesc
-- > start
) {
512 /* Search backwards for the next backslash. */
517 /* If it is escaped, skip it. */
519 for (cp
= stesc
- 1; cp
>= start
; cp
--)
523 if (0 == (stesc
- cp
) % 2) {
529 * Everything except user-defined strings and number
530 * registers is only checked, not expanded.
542 if (ESCAPE_ERROR
== mandoc_escape(&cp
, NULL
, NULL
))
543 mandoc_msg(MANDOCERR_BADESCAPE
, r
->parse
,
544 ln
, (int)(stesc
- *bufp
), NULL
);
548 if (EXPAND_LIMIT
< ++expand_count
) {
549 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
550 ln
, (int)(stesc
- *bufp
), NULL
);
555 * The third character decides the length
556 * of the name of the string or register.
557 * Save a pointer to the name.
577 /* Advance to the end of the name. */
579 for (naml
= 0; 0 == maxl
|| naml
< maxl
; naml
++, cp
++) {
582 (MANDOCERR_BADESCAPE
,
584 (int)(stesc
- *bufp
), NULL
);
587 if (0 == maxl
&& ']' == *cp
)
592 * Retrieve the replacement string; if it is
593 * undefined, resume searching for escapes.
597 res
= roff_getstrn(r
, stnam
, naml
);
599 snprintf(ubuf
, sizeof(ubuf
), "%d",
600 roff_getregn(r
, stnam
, naml
));
604 (MANDOCERR_BADESCAPE
, r
->parse
,
605 ln
, (int)(stesc
- *bufp
), NULL
);
610 /* Replace the escape sequence by the string. */
613 nbuf
= mandoc_malloc(*szp
);
615 strlcpy(nbuf
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
616 strlcat(nbuf
, res
, *szp
);
617 strlcat(nbuf
, cp
+ (maxl
? 0 : 1), *szp
);
619 /* Prepare for the next replacement. */
622 stesc
= nbuf
+ (stesc
- *bufp
) + ressz
;
630 * Process text streams:
631 * Convert all breakable hyphens into ASCII_HYPH.
632 * Decrement and spring input line trap.
635 roff_parsetext(char **bufp
, size_t *szp
, int pos
, int *offs
)
643 start
= p
= *bufp
+ pos
;
646 sz
= strcspn(p
, "-\\");
653 /* Skip over escapes. */
655 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
656 if (ESCAPE_ERROR
== esc
)
659 } else if (p
== start
) {
664 if (isalpha((unsigned char)p
[-1]) &&
665 isalpha((unsigned char)p
[1]))
670 /* Spring the input line trap. */
671 if (1 == roffit_lines
) {
672 isz
= mandoc_asprintf(&p
, "%s\n.%s", *bufp
, roffit_macro
);
679 return(ROFF_REPARSE
);
680 } else if (1 < roffit_lines
)
686 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
687 size_t *szp
, int pos
, int *offs
)
694 * Run the reserved-word filter only if we have some reserved
698 e
= roff_res(r
, bufp
, szp
, ln
, pos
);
701 assert(ROFF_CONT
== e
);
704 ctl
= roff_getcontrol(r
, *bufp
, &pos
);
707 * First, if a scope is open and we're not a macro, pass the
708 * text through the macro's filter. If a scope isn't open and
709 * we're not a macro, just let it through.
710 * Finally, if there's an equation scope open, divert it into it
711 * no matter our state.
714 if (r
->last
&& ! ctl
) {
716 assert(roffs
[t
].text
);
718 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
719 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
724 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
727 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
728 return(roff_parsetext(bufp
, szp
, pos
, offs
));
732 * If a scope is open, go to the child handler for that macro,
733 * as it may want to preprocess before doing anything with it.
734 * Don't do so if an equation is open.
739 assert(roffs
[t
].sub
);
740 return((*roffs
[t
].sub
)
742 ln
, ppos
, pos
, offs
));
746 * Lastly, as we've no scope open, try to look up and execute
747 * the new macro. If no macro is found, simply return and let
748 * the compilers handle it.
751 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
754 assert(roffs
[t
].proc
);
755 return((*roffs
[t
].proc
)
757 ln
, ppos
, pos
, offs
));
762 roff_endparse(struct roff
*r
)
766 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
767 r
->last
->line
, r
->last
->col
, NULL
);
770 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
771 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
776 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
777 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
783 * Parse a roff node's type from the input buffer. This must be in the
784 * form of ".foo xxx" in the usual way.
787 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
793 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
794 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
797 /* We stop the macro parse at an escape, tab, space, or nil. */
800 maclen
= strcspn(mac
, " \\\t\0");
802 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
803 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
807 while (buf
[*pos
] && ' ' == buf
[*pos
])
815 roff_cblock(ROFF_ARGS
)
819 * A block-close `..' should only be invoked as a child of an
820 * ignore macro, otherwise raise a warning and just ignore it.
823 if (NULL
== r
->last
) {
824 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
828 switch (r
->last
->tok
) {
836 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
843 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
848 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
851 roffnode_cleanscope(r
);
858 roffnode_cleanscope(struct roff
*r
)
862 if (--r
->last
->endspan
!= 0)
870 roff_ccond(struct roff
*r
, int ln
, int ppos
)
873 if (NULL
== r
->last
) {
874 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
878 switch (r
->last
->tok
) {
886 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
890 if (r
->last
->endspan
> -1) {
891 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
896 roffnode_cleanscope(r
);
903 roff_block(ROFF_ARGS
)
911 if (ROFF_ig
!= tok
) {
912 if ('\0' == (*bufp
)[pos
]) {
913 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
918 * Re-write `de1', since we don't really care about
919 * groff's strange compatibility mode, into `de'.
927 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
930 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
933 while (isspace((unsigned char)(*bufp
)[pos
]))
934 (*bufp
)[pos
++] = '\0';
937 roffnode_push(r
, tok
, name
, ln
, ppos
);
940 * At the beginning of a `de' macro, clear the existing string
941 * with the same name, if there is one. New content will be
942 * appended from roff_block_text() in multiline mode.
946 roff_setstr(r
, name
, "", 0);
948 if ('\0' == (*bufp
)[pos
])
951 /* If present, process the custom end-of-line marker. */
954 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
958 * Note: groff does NOT like escape characters in the input.
959 * Instead of detecting this, we're just going to let it fly and
964 sz
= (size_t)(pos
- sv
);
966 if (1 == sz
&& '.' == (*bufp
)[sv
])
969 r
->last
->end
= mandoc_malloc(sz
+ 1);
971 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
972 r
->last
->end
[(int)sz
] = '\0';
975 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
983 roff_block_sub(ROFF_ARGS
)
989 * First check whether a custom macro exists at this level. If
990 * it does, then check against it. This is some of groff's
991 * stranger behaviours. If we encountered a custom end-scope
992 * tag and that tag also happens to be a "real" macro, then we
993 * need to try interpreting it again as a real macro. If it's
994 * not, then return ignore. Else continue.
998 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
999 if ((*bufp
)[i
] != r
->last
->end
[j
])
1002 if ('\0' == r
->last
->end
[j
] &&
1003 ('\0' == (*bufp
)[i
] ||
1004 ' ' == (*bufp
)[i
] ||
1005 '\t' == (*bufp
)[i
])) {
1007 roffnode_cleanscope(r
);
1009 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
1013 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
1020 * If we have no custom end-query or lookup failed, then try
1021 * pulling it out of the hashtable.
1024 t
= roff_parse(r
, *bufp
, &pos
);
1027 * Macros other than block-end are only significant
1028 * in `de' blocks; elsewhere, simply throw them away.
1030 if (ROFF_cblock
!= t
) {
1032 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 2);
1036 assert(roffs
[t
].proc
);
1037 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1038 ln
, ppos
, pos
, offs
));
1044 roff_block_text(ROFF_ARGS
)
1048 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 2);
1056 roff_cond_sub(ROFF_ARGS
)
1063 roffnode_cleanscope(r
);
1064 t
= roff_parse(r
, *bufp
, &pos
);
1067 * Fully handle known macros when they are structurally
1068 * required or when the conditional evaluated to true.
1071 if ((ROFF_MAX
!= t
) &&
1072 (rr
|| ROFFMAC_STRUCT
& roffs
[t
].flags
)) {
1073 assert(roffs
[t
].proc
);
1074 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1075 ln
, ppos
, pos
, offs
));
1079 * If `\}' occurs on a macro line without a preceding macro,
1080 * drop the line completely.
1084 if ('\\' == ep
[0] && '}' == ep
[1])
1087 /* Always check for the closing delimiter `\}'. */
1089 while (NULL
!= (ep
= strchr(ep
, '\\'))) {
1090 if ('}' == *(++ep
)) {
1092 roff_ccond(r
, ln
, ep
- *bufp
- 1);
1096 return(rr
? ROFF_CONT
: ROFF_IGN
);
1101 roff_cond_text(ROFF_ARGS
)
1107 roffnode_cleanscope(r
);
1110 while (NULL
!= (ep
= strchr(ep
, '\\'))) {
1111 if ('}' == *(++ep
)) {
1113 roff_ccond(r
, ln
, ep
- *bufp
- 1);
1117 return(rr
? ROFF_CONT
: ROFF_IGN
);
1121 * Parse a single signed integer number. Stop at the first non-digit.
1122 * If there is at least one digit, return success and advance the
1123 * parse point, else return failure and let the parse point unchanged.
1124 * Ignore overflows, treat them just like the C language.
1127 roff_getnum(const char *v
, int *pos
, int *res
)
1136 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1137 *res
= 10 * *res
+ v
[p
] - '0';
1149 * Evaluate a string comparison condition.
1150 * The first character is the delimiter.
1151 * Succeed if the string up to its second occurrence
1152 * matches the string up to its third occurence.
1153 * Advance the cursor after the third occurrence
1154 * or lacking that, to the end of the line.
1157 roff_evalstrcond(const char *v
, int *pos
)
1159 const char *s1
, *s2
, *s3
;
1163 s1
= v
+ *pos
; /* initial delimiter */
1164 s2
= s1
+ 1; /* for scanning the first string */
1165 s3
= strchr(s2
, *s1
); /* for scanning the second string */
1167 if (NULL
== s3
) /* found no middle delimiter */
1170 while ('\0' != *++s3
) {
1171 if (*s2
!= *s3
) { /* mismatch */
1172 s3
= strchr(s3
, *s1
);
1175 if (*s3
== *s1
) { /* found the final delimiter */
1184 s3
= strchr(s2
, '\0');
1192 * Evaluate an optionally negated single character, numerical,
1193 * or string condition.
1196 roff_evalcond(const char *v
, int *pos
)
1198 int wanttrue
, number
;
1200 if ('!' == v
[*pos
]) {
1227 if (roff_evalnum(v
, pos
, &number
, 0))
1228 return((number
> 0) == wanttrue
);
1230 return(roff_evalstrcond(v
, pos
) == wanttrue
);
1235 roff_line_ignore(ROFF_ARGS
)
1243 roff_cond(ROFF_ARGS
)
1246 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1249 * An `.el' has no conditional body: it will consume the value
1250 * of the current rstack entry set in prior `ie' calls or
1253 * If we're not an `el', however, then evaluate the conditional.
1256 r
->last
->rule
= ROFF_el
== tok
?
1257 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
1258 roff_evalcond(*bufp
, &pos
);
1261 * An if-else will put the NEGATION of the current evaluated
1262 * conditional into the stack of rules.
1265 if (ROFF_ie
== tok
) {
1266 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1267 mandoc_msg(MANDOCERR_MEM
,
1268 r
->parse
, ln
, ppos
, NULL
);
1271 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
1274 /* If the parent has false as its rule, then so do we. */
1276 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
1281 * If there is nothing on the line after the conditional,
1282 * not even whitespace, use next-line scope.
1285 if ('\0' == (*bufp
)[pos
]) {
1286 r
->last
->endspan
= 2;
1290 while (' ' == (*bufp
)[pos
])
1293 /* An opening brace requests multiline scope. */
1295 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1296 r
->last
->endspan
= -1;
1302 * Anything else following the conditional causes
1303 * single-line scope. Warn if the scope contains
1304 * nothing but trailing whitespace.
1307 if ('\0' == (*bufp
)[pos
])
1308 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1310 r
->last
->endspan
= 1;
1322 char *name
, *string
;
1325 * A symbol is named by the first word following the macro
1326 * invocation up to a space. Its value is anything after the
1327 * name's trailing whitespace and optional double-quote. Thus,
1331 * will have `bar " ' as its value.
1334 string
= *bufp
+ pos
;
1335 name
= roff_getname(r
, &string
, ln
, pos
);
1339 /* Read past initial double-quote. */
1343 /* The rest is the value. */
1344 roff_setstr(r
, name
, string
, ROFF_as
== tok
);
1349 * Parse a single operator, one or two characters long.
1350 * If the operator is recognized, return success and advance the
1351 * parse point, else return failure and let the parse point unchanged.
1354 roff_getop(const char *v
, int *pos
, char *res
)
1375 switch (v
[*pos
+ 1]) {
1393 switch (v
[*pos
+ 1]) {
1407 if ('=' == v
[*pos
+ 1])
1419 * Evaluate either a parenthesized numeric expression
1420 * or a single signed integer number.
1423 roff_evalpar(const char *v
, int *pos
, int *res
)
1427 return(roff_getnum(v
, pos
, res
));
1430 if ( ! roff_evalnum(v
, pos
, res
, 1))
1433 /* If the trailing parenthesis is missing, ignore the error. */
1441 * Evaluate a complete numeric expression.
1442 * Proceed left to right, there is no concept of precedence.
1445 roff_evalnum(const char *v
, int *pos
, int *res
, int skipwhite
)
1447 int mypos
, operand2
;
1456 while (isspace((unsigned char)v
[*pos
]))
1459 if ( ! roff_evalpar(v
, pos
, res
))
1464 while (isspace((unsigned char)v
[*pos
]))
1467 if ( ! roff_getop(v
, pos
, &operator))
1471 while (isspace((unsigned char)v
[*pos
]))
1474 if ( ! roff_evalpar(v
, pos
, &operand2
))
1478 while (isspace((unsigned char)v
[*pos
]))
1498 *res
= *res
< operand2
;
1501 *res
= *res
> operand2
;
1504 *res
= *res
<= operand2
;
1507 *res
= *res
>= operand2
;
1510 *res
= *res
== operand2
;
1513 *res
= *res
!= operand2
;
1516 *res
= *res
&& operand2
;
1519 *res
= *res
|| operand2
;
1522 if (operand2
< *res
)
1526 if (operand2
> *res
)
1537 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
1539 struct roffreg
*reg
;
1541 /* Search for an existing register with the same name. */
1544 while (reg
&& strcmp(name
, reg
->key
.p
))
1548 /* Create a new register. */
1549 reg
= mandoc_malloc(sizeof(struct roffreg
));
1550 reg
->key
.p
= mandoc_strdup(name
);
1551 reg
->key
.sz
= strlen(name
);
1553 reg
->next
= r
->regtab
;
1559 else if ('-' == sign
)
1566 * Handle some predefined read-only number registers.
1567 * For now, return -1 if the requested register is not predefined;
1568 * in case a predefined read-only register having the value -1
1569 * were to turn up, another special value would have to be chosen.
1572 roff_getregro(const char *name
)
1576 case ('A'): /* ASCII approximation mode is always off. */
1578 case ('g'): /* Groff compatibility mode is always on. */
1580 case ('H'): /* Fixed horizontal resolution. */
1582 case ('j'): /* Always adjust left margin only. */
1584 case ('T'): /* Some output device is always defined. */
1586 case ('V'): /* Fixed vertical resolution. */
1594 roff_getreg(const struct roff
*r
, const char *name
)
1596 struct roffreg
*reg
;
1599 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
1600 val
= roff_getregro(name
+ 1);
1605 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1606 if (0 == strcmp(name
, reg
->key
.p
))
1613 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
1615 struct roffreg
*reg
;
1618 if ('.' == name
[0] && 2 == len
) {
1619 val
= roff_getregro(name
+ 1);
1624 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1625 if (len
== reg
->key
.sz
&&
1626 0 == strncmp(name
, reg
->key
.p
, len
))
1633 roff_freereg(struct roffreg
*reg
)
1635 struct roffreg
*old_reg
;
1637 while (NULL
!= reg
) {
1654 key
= roff_getname(r
, &val
, ln
, pos
);
1657 if ('+' == sign
|| '-' == sign
)
1660 if (roff_evalnum(val
, NULL
, &iv
, 0))
1661 roff_setreg(r
, key
, iv
, sign
);
1669 struct roffreg
*reg
, **prev
;
1674 name
= roff_getname(r
, &cp
, ln
, pos
);
1679 if (NULL
== reg
|| !strcmp(name
, reg
->key
.p
))
1699 while ('\0' != *cp
) {
1700 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1702 roff_setstr(r
, name
, NULL
, 0);
1715 /* Parse the number of lines. */
1717 len
= strcspn(cp
, " \t");
1719 if ((iv
= mandoc_strntoi(cp
, len
, 10)) <= 0) {
1720 mandoc_msg(MANDOCERR_NUMERIC
, r
->parse
,
1721 ln
, ppos
, *bufp
+ 1);
1726 /* Arm the input line trap. */
1728 roffit_macro
= mandoc_strdup(cp
);
1736 const char *const *cp
;
1738 if (0 == ((MPARSE_MDOC
| MPARSE_QUICK
) & r
->options
))
1739 for (cp
= __mdoc_reserved
; *cp
; cp
++)
1740 roff_setstr(r
, *cp
, NULL
, 0);
1749 const char *const *cp
;
1751 if (0 == (MPARSE_QUICK
& r
->options
))
1752 for (cp
= __man_reserved
; *cp
; cp
++)
1753 roff_setstr(r
, *cp
, NULL
, 0);
1764 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1777 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1779 tbl_restart(ppos
, ln
, r
->tbl
);
1786 roff_closeeqn(struct roff
*r
)
1789 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1794 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1795 int offs
, const char *buf
)
1800 assert(NULL
== r
->eqn
);
1801 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1804 r
->last_eqn
->next
= e
;
1806 r
->first_eqn
= r
->last_eqn
= e
;
1808 r
->eqn
= r
->last_eqn
= e
;
1812 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1821 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1830 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1838 struct tbl_node
*tbl
;
1841 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1845 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
1848 r
->last_tbl
->next
= tbl
;
1850 r
->first_tbl
= r
->last_tbl
= tbl
;
1852 r
->tbl
= r
->last_tbl
= tbl
;
1864 if ('\0' == *p
|| '.' == (r
->control
= *p
++))
1868 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1877 const char *p
, *first
, *second
;
1879 enum mandoc_esc esc
;
1884 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1888 while ('\0' != *p
) {
1892 if ('\\' == *first
) {
1893 esc
= mandoc_escape(&p
, NULL
, NULL
);
1894 if (ESCAPE_ERROR
== esc
) {
1896 (MANDOCERR_BADESCAPE
, r
->parse
,
1897 ln
, (int)(p
- *bufp
), NULL
);
1900 fsz
= (size_t)(p
- first
);
1904 if ('\\' == *second
) {
1905 esc
= mandoc_escape(&p
, NULL
, NULL
);
1906 if (ESCAPE_ERROR
== esc
) {
1908 (MANDOCERR_BADESCAPE
, r
->parse
,
1909 ln
, (int)(p
- *bufp
), NULL
);
1912 ssz
= (size_t)(p
- second
);
1913 } else if ('\0' == *second
) {
1914 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1915 ln
, (int)(p
- *bufp
), NULL
);
1921 roff_setstrn(&r
->xmbtab
, first
,
1922 fsz
, second
, ssz
, 0);
1926 if (NULL
== r
->xtab
)
1927 r
->xtab
= mandoc_calloc
1928 (128, sizeof(struct roffstr
));
1930 free(r
->xtab
[(int)*first
].p
);
1931 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1932 r
->xtab
[(int)*first
].sz
= ssz
;
1944 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1947 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1948 * opening anything that's not in our cwd or anything beneath
1949 * it. Thus, explicitly disallow traversing up the file-system
1950 * or using absolute paths.
1954 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1955 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1965 roff_userdef(ROFF_ARGS
)
1972 * Collect pointers to macro argument strings
1973 * and NUL-terminate them.
1976 for (i
= 0; i
< 9; i
++)
1977 arg
[i
] = '\0' == *cp
? "" :
1978 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1981 * Expand macro arguments.
1984 n1
= cp
= mandoc_strdup(r
->current_string
);
1985 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1987 if (0 > i
|| 8 < i
) {
1988 /* Not an argument invocation. */
1993 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1994 n2
= mandoc_malloc(*szp
);
1996 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1997 strlcat(n2
, arg
[i
], *szp
);
1998 strlcat(n2
, cp
+ 3, *szp
);
2000 cp
= n2
+ (cp
- n1
);
2006 * Replace the macro invocation
2007 * by the expanded macro.
2012 *szp
= strlen(*bufp
) + 1;
2014 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
2015 ROFF_REPARSE
: ROFF_APPEND
);
2019 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
2027 /* Read until end of name. */
2028 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
2034 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
2039 /* Nil-terminate name. */
2043 /* Read past spaces. */
2052 * Store *string into the user-defined string called *name.
2053 * To clear an existing entry, call with (*r, *name, NULL, 0).
2054 * append == 0: replace mode
2055 * append == 1: single-line append mode
2056 * append == 2: multiline append mode, append '\n' after each call
2059 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
2063 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
2064 string
? strlen(string
) : 0, append
);
2068 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
2069 const char *string
, size_t stringsz
, int append
)
2074 size_t oldch
, newch
;
2076 /* Search for an existing string with the same name. */
2079 while (n
&& strcmp(name
, n
->key
.p
))
2083 /* Create a new string table entry. */
2084 n
= mandoc_malloc(sizeof(struct roffkv
));
2085 n
->key
.p
= mandoc_strndup(name
, namesz
);
2091 } else if (0 == append
) {
2101 * One additional byte for the '\n' in multiline mode,
2102 * and one for the terminating '\0'.
2104 newch
= stringsz
+ (1 < append
? 2u : 1u);
2106 if (NULL
== n
->val
.p
) {
2107 n
->val
.p
= mandoc_malloc(newch
);
2112 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
2115 /* Skip existing content in the destination buffer. */
2116 c
= n
->val
.p
+ (int)oldch
;
2118 /* Append new content to the destination buffer. */
2120 while (i
< (int)stringsz
) {
2122 * Rudimentary roff copy mode:
2123 * Handle escaped backslashes.
2125 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
2130 /* Append terminating bytes. */
2135 n
->val
.sz
= (int)(c
- n
->val
.p
);
2139 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
2141 const struct roffkv
*n
;
2144 for (n
= r
->strtab
; n
; n
= n
->next
)
2145 if (0 == strncmp(name
, n
->key
.p
, len
) &&
2146 '\0' == n
->key
.p
[(int)len
])
2149 for (i
= 0; i
< PREDEFS_MAX
; i
++)
2150 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
2151 '\0' == predefs
[i
].name
[(int)len
])
2152 return(predefs
[i
].str
);
2158 roff_freestr(struct roffkv
*r
)
2160 struct roffkv
*n
, *nn
;
2162 for (n
= r
; n
; n
= nn
) {
2170 const struct tbl_span
*
2171 roff_span(const struct roff
*r
)
2174 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
2178 roff_eqn(const struct roff
*r
)
2181 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
2185 * Duplicate an input string, making the appropriate character
2186 * conversations (as stipulated by `tr') along the way.
2187 * Returns a heap-allocated string with all the replacements made.
2190 roff_strdup(const struct roff
*r
, const char *p
)
2192 const struct roffkv
*cp
;
2196 enum mandoc_esc esc
;
2198 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
2199 return(mandoc_strdup(p
));
2200 else if ('\0' == *p
)
2201 return(mandoc_strdup(""));
2204 * Step through each character looking for term matches
2205 * (remember that a `tr' can be invoked with an escape, which is
2206 * a glyph but the escape is multi-character).
2207 * We only do this if the character hash has been initialised
2208 * and the string is >0 length.
2214 while ('\0' != *p
) {
2215 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
2216 sz
= r
->xtab
[(int)*p
].sz
;
2217 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2218 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
2222 } else if ('\\' != *p
) {
2223 res
= mandoc_realloc(res
, ssz
+ 2);
2228 /* Search for term matches. */
2229 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
2230 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
2235 * A match has been found.
2236 * Append the match to the array and move
2237 * forward by its keysize.
2239 res
= mandoc_realloc
2240 (res
, ssz
+ cp
->val
.sz
+ 1);
2241 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
2243 p
+= (int)cp
->key
.sz
;
2248 * Handle escapes carefully: we need to copy
2249 * over just the escape itself, or else we might
2250 * do replacements within the escape itself.
2251 * Make sure to pass along the bogus string.
2254 esc
= mandoc_escape(&p
, NULL
, NULL
);
2255 if (ESCAPE_ERROR
== esc
) {
2257 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2258 memcpy(res
+ ssz
, pp
, sz
);
2262 * We bail out on bad escapes.
2263 * No need to warn: we already did so when
2264 * roff_res() was called.
2267 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2268 memcpy(res
+ ssz
, pp
, sz
);
2272 res
[(int)ssz
] = '\0';
2277 * Find out whether a line is a macro line or not.
2278 * If it is, adjust the current position and return one; if it isn't,
2279 * return zero and don't change the current position.
2280 * If the control character has been set with `.cc', then let that grain
2282 * This is slighly contrary to groff, where using the non-breaking
2283 * control character when `cc' has been invoked will cause the
2284 * non-breaking macro contents to be printed verbatim.
2287 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
2293 if (0 != r
->control
&& cp
[pos
] == r
->control
)
2295 else if (0 != r
->control
)
2297 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
2299 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
2304 while (' ' == cp
[pos
] || '\t' == cp
[pos
])