]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.167 2011/07/29 10:16:59 kristaps Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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 "libmandoc.h"
31 /* Maximum number of nested if-else conditionals. */
32 #define RSTACK_MAX 128
75 * A single register entity. If "set" is zero, the value of the
76 * register should be the default one, which is per-register.
77 * Registers are assumed to be unsigned ints for now.
80 int set
; /* whether set or not */
81 unsigned int u
; /* unsigned integer */
85 * An incredibly-simple string buffer.
88 char *p
; /* nil-terminated buffer */
89 size_t sz
; /* saved strlen(p) */
93 * A key-value roffstr pair as part of a singly-linked list.
98 struct roffkv
*next
; /* next in list */
102 struct mparse
*parse
; /* parse point */
103 struct roffnode
*last
; /* leaf of stack */
104 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
105 int rstackpos
; /* position in rstack */
106 struct reg regs
[REG__MAX
];
107 struct roffkv
*strtab
; /* user-defined strings & macros */
108 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
109 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
110 const char *current_string
; /* value of last called user macro */
111 struct tbl_node
*first_tbl
; /* first table parsed */
112 struct tbl_node
*last_tbl
; /* last table parsed */
113 struct tbl_node
*tbl
; /* current table being parsed */
114 struct eqn_node
*last_eqn
; /* last equation parsed */
115 struct eqn_node
*first_eqn
; /* first equation parsed */
116 struct eqn_node
*eqn
; /* current equation being parsed */
120 enum rofft tok
; /* type of node */
121 struct roffnode
*parent
; /* up one in stack */
122 int line
; /* parse line */
123 int col
; /* parse col */
124 char *name
; /* node name, e.g. macro name */
125 char *end
; /* end-rules: custom token */
126 int endspan
; /* end-rules: next-line or infty */
127 enum roffrule rule
; /* current evaluation rule */
130 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
131 enum rofft tok, /* tok of macro */ \
132 char **bufp, /* input buffer */ \
133 size_t *szp, /* size of input buffer */ \
134 int ln, /* parse line */ \
135 int ppos, /* original pos in buffer */ \
136 int pos, /* current pos in buffer */ \
137 int *offs /* reset offset of buffer data */
139 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
142 const char *name
; /* macro name */
143 roffproc proc
; /* process new macro */
144 roffproc text
; /* process as child text of macro */
145 roffproc sub
; /* process as child of macro */
147 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
148 struct roffmac
*next
;
152 const char *name
; /* predefined input name */
153 const char *str
; /* replacement symbol */
156 #define PREDEF(__name, __str) \
157 { (__name), (__str) },
159 static enum rofft
roffhash_find(const char *, size_t);
160 static void roffhash_init(void);
161 static void roffnode_cleanscope(struct roff
*);
162 static void roffnode_pop(struct roff
*);
163 static void roffnode_push(struct roff
*, enum rofft
,
164 const char *, int, int);
165 static enum rofferr
roff_block(ROFF_ARGS
);
166 static enum rofferr
roff_block_text(ROFF_ARGS
);
167 static enum rofferr
roff_block_sub(ROFF_ARGS
);
168 static enum rofferr
roff_cblock(ROFF_ARGS
);
169 static enum rofferr
roff_ccond(ROFF_ARGS
);
170 static enum rofferr
roff_cond(ROFF_ARGS
);
171 static enum rofferr
roff_cond_text(ROFF_ARGS
);
172 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
173 static enum rofferr
roff_ds(ROFF_ARGS
);
174 static enum roffrule
roff_evalcond(const char *, int *);
175 static void roff_free1(struct roff
*);
176 static void roff_freestr(struct roffkv
*);
177 static char *roff_getname(struct roff
*, char **, int, int);
178 static const char *roff_getstrn(const struct roff
*,
179 const char *, size_t);
180 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
181 static enum rofferr
roff_nr(ROFF_ARGS
);
182 static void roff_openeqn(struct roff
*, const char *,
183 int, int, const char *);
184 static enum rofft
roff_parse(struct roff
*, const char *, int *);
185 static enum rofferr
roff_parsetext(char *);
186 static void roff_res(struct roff
*,
187 char **, size_t *, int, int);
188 static enum rofferr
roff_rm(ROFF_ARGS
);
189 static void roff_setstr(struct roff
*,
190 const char *, const char *, int);
191 static void roff_setstrn(struct roffkv
**, const char *,
192 size_t, const char *, size_t, int);
193 static enum rofferr
roff_so(ROFF_ARGS
);
194 static enum rofferr
roff_tr(ROFF_ARGS
);
195 static enum rofferr
roff_TE(ROFF_ARGS
);
196 static enum rofferr
roff_TS(ROFF_ARGS
);
197 static enum rofferr
roff_EQ(ROFF_ARGS
);
198 static enum rofferr
roff_EN(ROFF_ARGS
);
199 static enum rofferr
roff_T_(ROFF_ARGS
);
200 static enum rofferr
roff_userdef(ROFF_ARGS
);
202 /* See roffhash_find() */
206 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
208 static struct roffmac
*hash
[HASHWIDTH
];
210 static struct roffmac roffs
[ROFF_MAX
] = {
211 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
212 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
213 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
214 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
215 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
216 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
217 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
218 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
219 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
220 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
221 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
222 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
223 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
224 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
225 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
226 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
227 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
228 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
229 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
230 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
231 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
232 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
233 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
234 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
235 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
236 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
237 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
238 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
239 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
240 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
241 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
244 /* Array of injected predefined strings. */
245 #define PREDEFS_MAX 38
246 static const struct predef predefs
[PREDEFS_MAX
] = {
247 #include "predefs.in"
250 /* See roffhash_find() */
251 #define ROFF_HASH(p) (p[0] - ASCII_LO)
259 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
260 assert(roffs
[i
].name
[0] >= ASCII_LO
);
261 assert(roffs
[i
].name
[0] <= ASCII_HI
);
263 buc
= ROFF_HASH(roffs
[i
].name
);
265 if (NULL
!= (n
= hash
[buc
])) {
266 for ( ; n
->next
; n
= n
->next
)
270 hash
[buc
] = &roffs
[i
];
275 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
276 * the nil-terminated string name could be found.
279 roffhash_find(const char *p
, size_t s
)
285 * libroff has an extremely simple hashtable, for the time
286 * being, which simply keys on the first character, which must
287 * be printable, then walks a chain. It works well enough until
291 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
296 if (NULL
== (n
= hash
[buc
]))
298 for ( ; n
; n
= n
->next
)
299 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
300 return((enum rofft
)(n
- roffs
));
307 * Pop the current node off of the stack of roff instructions currently
311 roffnode_pop(struct roff
*r
)
318 r
->last
= r
->last
->parent
;
326 * Push a roff node onto the instruction stack. This must later be
327 * removed with roffnode_pop().
330 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
335 p
= mandoc_calloc(1, sizeof(struct roffnode
));
338 p
->name
= mandoc_strdup(name
);
342 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
349 roff_free1(struct roff
*r
)
355 while (NULL
!= (t
= r
->first_tbl
)) {
356 r
->first_tbl
= t
->next
;
360 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
362 while (NULL
!= (e
= r
->first_eqn
)) {
363 r
->first_eqn
= e
->next
;
367 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
372 roff_freestr(r
->strtab
);
373 roff_freestr(r
->xmbtab
);
375 r
->strtab
= r
->xmbtab
= NULL
;
378 for (i
= 0; i
< 128; i
++)
386 roff_reset(struct roff
*r
)
392 memset(&r
->regs
, 0, sizeof(struct reg
) * REG__MAX
);
394 for (i
= 0; i
< PREDEFS_MAX
; i
++)
395 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
400 roff_free(struct roff
*r
)
409 roff_alloc(struct mparse
*parse
)
414 r
= mandoc_calloc(1, sizeof(struct roff
));
420 for (i
= 0; i
< PREDEFS_MAX
; i
++)
421 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
427 * Pre-filter each and every line for reserved words (one beginning with
428 * `\*', e.g., `\*(ab'). These must be handled before the actual line
430 * This also checks the syntax of regular escapes.
433 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
436 const char *stesc
; /* start of an escape sequence ('\\') */
437 const char *stnam
; /* start of the name, after "[(*" */
438 const char *cp
; /* end of the name, e.g. before ']' */
439 const char *res
; /* the string to be substituted */
446 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
450 * The second character must be an asterisk.
451 * If it isn't, skip it anyway: It is escaped,
452 * so it can't start another escape sequence.
460 esc
= mandoc_escape(&cp
, NULL
, NULL
);
461 if (ESCAPE_ERROR
!= esc
)
465 (MANDOCERR_BADESCAPE
, r
->parse
,
466 ln
, (int)(stesc
- *bufp
), NULL
);
473 * The third character decides the length
474 * of the name of the string.
475 * Save a pointer to the name.
495 /* Advance to the end of the name. */
497 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
500 (MANDOCERR_BADESCAPE
,
502 (int)(stesc
- *bufp
), NULL
);
505 if (0 == maxl
&& ']' == *cp
)
510 * Retrieve the replacement string; if it is
511 * undefined, resume searching for escapes.
514 res
= roff_getstrn(r
, stnam
, (size_t)i
);
518 (MANDOCERR_BADESCAPE
, r
->parse
,
519 ln
, (int)(stesc
- *bufp
), NULL
);
523 /* Replace the escape sequence by the string. */
527 nsz
= *szp
+ strlen(res
) + 1;
528 n
= mandoc_malloc(nsz
);
530 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
531 strlcat(n
, res
, nsz
);
532 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
543 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
546 roff_parsetext(char *p
)
556 sz
= strcspn(p
, "-\\");
563 /* Skip over escapes. */
566 ((const char **)&p
, NULL
, NULL
);
567 if (ESCAPE_ERROR
== esc
)
570 } else if (p
== start
) {
578 '\t' != r
&& '\t' != l
&&
579 ' ' != r
&& ' ' != l
&&
580 '-' != r
&& '-' != l
&&
581 ! isdigit((unsigned char)l
) &&
582 ! isdigit((unsigned char)r
))
591 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
592 size_t *szp
, int pos
, int *offs
)
599 * Run the reserved-word filter only if we have some reserved
603 roff_res(r
, bufp
, szp
, ln
, pos
);
606 ctl
= mandoc_getcontrol(*bufp
, &pos
);
609 * First, if a scope is open and we're not a macro, pass the
610 * text through the macro's filter. If a scope isn't open and
611 * we're not a macro, just let it through.
612 * Finally, if there's an equation scope open, divert it into it
613 * no matter our state.
616 if (r
->last
&& ! ctl
) {
618 assert(roffs
[t
].text
);
620 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
621 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
625 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
627 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
628 return(roff_parsetext(*bufp
+ pos
));
631 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
633 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
634 return(roff_parsetext(*bufp
+ pos
));
636 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
639 * If a scope is open, go to the child handler for that macro,
640 * as it may want to preprocess before doing anything with it.
641 * Don't do so if an equation is open.
646 assert(roffs
[t
].sub
);
647 return((*roffs
[t
].sub
)
649 ln
, ppos
, pos
, offs
));
653 * Lastly, as we've no scope open, try to look up and execute
654 * the new macro. If no macro is found, simply return and let
655 * the compilers handle it.
658 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
661 assert(roffs
[t
].proc
);
662 return((*roffs
[t
].proc
)
664 ln
, ppos
, pos
, offs
));
669 roff_endparse(struct roff
*r
)
673 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
674 r
->last
->line
, r
->last
->col
, NULL
);
677 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
678 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
683 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
684 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
690 * Parse a roff node's type from the input buffer. This must be in the
691 * form of ".foo xxx" in the usual way.
694 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
700 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
701 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
705 * We stop the macro parse at an escape, tab, space, or nil.
706 * However, `\}' is also a valid macro, so make sure we don't
707 * clobber it by seeing the `\' as the end of token.
711 maclen
= strcspn(mac
+ 1, " \\\t\0") + 1;
713 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
714 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
718 while (buf
[*pos
] && ' ' == buf
[*pos
])
726 roff_cblock(ROFF_ARGS
)
730 * A block-close `..' should only be invoked as a child of an
731 * ignore macro, otherwise raise a warning and just ignore it.
734 if (NULL
== r
->last
) {
735 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
739 switch (r
->last
->tok
) {
747 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
754 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
759 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
762 roffnode_cleanscope(r
);
769 roffnode_cleanscope(struct roff
*r
)
773 if (--r
->last
->endspan
< 0)
782 roff_ccond(ROFF_ARGS
)
785 if (NULL
== r
->last
) {
786 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
790 switch (r
->last
->tok
) {
798 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
802 if (r
->last
->endspan
> -1) {
803 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
808 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
811 roffnode_cleanscope(r
);
818 roff_block(ROFF_ARGS
)
826 if (ROFF_ig
!= tok
) {
827 if ('\0' == (*bufp
)[pos
]) {
828 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
833 * Re-write `de1', since we don't really care about
834 * groff's strange compatibility mode, into `de'.
842 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
845 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
848 while (isspace((unsigned char)(*bufp
)[pos
]))
849 (*bufp
)[pos
++] = '\0';
852 roffnode_push(r
, tok
, name
, ln
, ppos
);
855 * At the beginning of a `de' macro, clear the existing string
856 * with the same name, if there is one. New content will be
857 * added from roff_block_text() in multiline mode.
861 roff_setstr(r
, name
, "", 0);
863 if ('\0' == (*bufp
)[pos
])
866 /* If present, process the custom end-of-line marker. */
869 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
873 * Note: groff does NOT like escape characters in the input.
874 * Instead of detecting this, we're just going to let it fly and
879 sz
= (size_t)(pos
- sv
);
881 if (1 == sz
&& '.' == (*bufp
)[sv
])
884 r
->last
->end
= mandoc_malloc(sz
+ 1);
886 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
887 r
->last
->end
[(int)sz
] = '\0';
890 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
898 roff_block_sub(ROFF_ARGS
)
904 * First check whether a custom macro exists at this level. If
905 * it does, then check against it. This is some of groff's
906 * stranger behaviours. If we encountered a custom end-scope
907 * tag and that tag also happens to be a "real" macro, then we
908 * need to try interpreting it again as a real macro. If it's
909 * not, then return ignore. Else continue.
913 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
914 if ((*bufp
)[i
] != r
->last
->end
[j
])
917 if ('\0' == r
->last
->end
[j
] &&
918 ('\0' == (*bufp
)[i
] ||
920 '\t' == (*bufp
)[i
])) {
922 roffnode_cleanscope(r
);
924 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
928 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
935 * If we have no custom end-query or lookup failed, then try
936 * pulling it out of the hashtable.
939 t
= roff_parse(r
, *bufp
, &pos
);
942 * Macros other than block-end are only significant
943 * in `de' blocks; elsewhere, simply throw them away.
945 if (ROFF_cblock
!= t
) {
947 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
951 assert(roffs
[t
].proc
);
952 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
953 ln
, ppos
, pos
, offs
));
959 roff_block_text(ROFF_ARGS
)
963 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
971 roff_cond_sub(ROFF_ARGS
)
978 roffnode_cleanscope(r
);
981 * If the macro is unknown, first check if it contains a closing
982 * delimiter `\}'. If it does, close out our scope and return
983 * the currently-scoped rule (ignore or continue). Else, drop
984 * into the currently-scoped rule.
987 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
989 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
995 * Make the \} go away.
996 * This is a little haphazard, as it's not quite
997 * clear how nroff does this.
998 * If we're at the end of line, then just chop
999 * off the \} and resize the buffer.
1000 * If we aren't, then conver it to spaces.
1003 if ('\0' == *(ep
+ 1)) {
1007 *(ep
- 1) = *ep
= ' ';
1009 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1010 ln
, pos
, pos
+ 2, offs
);
1013 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1017 * A denied conditional must evaluate its children if and only
1018 * if they're either structurally required (such as loops and
1019 * conditionals) or a closing macro.
1022 if (ROFFRULE_DENY
== rr
)
1023 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
1024 if (ROFF_ccond
!= t
)
1027 assert(roffs
[t
].proc
);
1028 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1029 ln
, ppos
, pos
, offs
));
1034 roff_cond_text(ROFF_ARGS
)
1040 roffnode_cleanscope(r
);
1043 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1048 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1049 ln
, pos
, pos
+ 2, offs
);
1051 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1054 static enum roffrule
1055 roff_evalcond(const char *v
, int *pos
)
1061 return(ROFFRULE_ALLOW
);
1068 return(ROFFRULE_DENY
);
1073 while (v
[*pos
] && ' ' != v
[*pos
])
1075 return(ROFFRULE_DENY
);
1080 roff_line_ignore(ROFF_ARGS
)
1084 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
1091 roff_cond(ROFF_ARGS
)
1097 * An `.el' has no conditional body: it will consume the value
1098 * of the current rstack entry set in prior `ie' calls or
1101 * If we're not an `el', however, then evaluate the conditional.
1104 rule
= ROFF_el
== tok
?
1106 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
1107 roff_evalcond(*bufp
, &pos
);
1110 while (' ' == (*bufp
)[pos
])
1114 * Roff is weird. If we have just white-space after the
1115 * conditional, it's considered the BODY and we exit without
1116 * really doing anything. Warn about this. It's probably
1120 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1121 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1125 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1127 r
->last
->rule
= rule
;
1130 * An if-else will put the NEGATION of the current evaluated
1131 * conditional into the stack of rules.
1134 if (ROFF_ie
== tok
) {
1135 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1136 mandoc_msg(MANDOCERR_MEM
,
1137 r
->parse
, ln
, ppos
, NULL
);
1140 r
->rstack
[++r
->rstackpos
] =
1141 ROFFRULE_DENY
== r
->last
->rule
?
1142 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1145 /* If the parent has false as its rule, then so do we. */
1147 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1148 r
->last
->rule
= ROFFRULE_DENY
;
1151 * Determine scope. If we're invoked with "\{" trailing the
1152 * conditional, then we're in a multiline scope. Else our scope
1153 * expires on the next line.
1156 r
->last
->endspan
= 1;
1158 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1159 r
->last
->endspan
= -1;
1164 * If there are no arguments on the line, the next-line scope is
1168 if ('\0' == (*bufp
)[pos
])
1171 /* Otherwise re-run the roff parser after recalculating. */
1182 char *name
, *string
;
1185 * A symbol is named by the first word following the macro
1186 * invocation up to a space. Its value is anything after the
1187 * name's trailing whitespace and optional double-quote. Thus,
1191 * will have `bar " ' as its value.
1194 string
= *bufp
+ pos
;
1195 name
= roff_getname(r
, &string
, ln
, pos
);
1199 /* Read past initial double-quote. */
1203 /* The rest is the value. */
1204 roff_setstr(r
, name
, string
, 0);
1209 roff_regisset(const struct roff
*r
, enum regs reg
)
1212 return(r
->regs
[(int)reg
].set
);
1216 roff_regget(const struct roff
*r
, enum regs reg
)
1219 return(r
->regs
[(int)reg
].u
);
1223 roff_regunset(struct roff
*r
, enum regs reg
)
1226 r
->regs
[(int)reg
].set
= 0;
1238 key
= roff_getname(r
, &val
, ln
, pos
);
1240 if (0 == strcmp(key
, "nS")) {
1241 r
->regs
[(int)REG_nS
].set
= 1;
1242 if ((iv
= mandoc_strntoi(val
, strlen(val
), 10)) >= 0)
1243 r
->regs
[(int)REG_nS
].u
= (unsigned)iv
;
1245 r
->regs
[(int)REG_nS
].u
= 0u;
1259 while ('\0' != *cp
) {
1260 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1262 roff_setstr(r
, name
, NULL
, 0);
1273 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1286 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1288 tbl_restart(ppos
, ln
, r
->tbl
);
1295 roff_closeeqn(struct roff
*r
)
1298 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1303 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1304 int offs
, const char *buf
)
1309 assert(NULL
== r
->eqn
);
1310 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1313 r
->last_eqn
->next
= e
;
1315 r
->first_eqn
= r
->last_eqn
= e
;
1317 r
->eqn
= r
->last_eqn
= e
;
1321 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1330 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1339 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1350 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1354 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1357 r
->last_tbl
->next
= t
;
1359 r
->first_tbl
= r
->last_tbl
= t
;
1361 r
->tbl
= r
->last_tbl
= t
;
1369 const char *p
, *first
, *second
;
1371 enum mandoc_esc esc
;
1376 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1380 while ('\0' != *p
) {
1384 if ('\\' == *first
) {
1385 esc
= mandoc_escape(&p
, NULL
, NULL
);
1386 if (ESCAPE_ERROR
== esc
) {
1388 (MANDOCERR_BADESCAPE
, r
->parse
,
1389 ln
, (int)(p
- *bufp
), NULL
);
1392 fsz
= (size_t)(p
- first
);
1396 if ('\\' == *second
) {
1397 esc
= mandoc_escape(&p
, NULL
, NULL
);
1398 if (ESCAPE_ERROR
== esc
) {
1400 (MANDOCERR_BADESCAPE
, r
->parse
,
1401 ln
, (int)(p
- *bufp
), NULL
);
1404 ssz
= (size_t)(p
- second
);
1405 } else if ('\0' == *second
) {
1406 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1407 ln
, (int)(p
- *bufp
), NULL
);
1413 roff_setstrn(&r
->xmbtab
, first
,
1414 fsz
, second
, ssz
, 0);
1418 if (NULL
== r
->xtab
)
1419 r
->xtab
= mandoc_calloc
1420 (128, sizeof(struct roffstr
));
1422 free(r
->xtab
[(int)*first
].p
);
1423 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1424 r
->xtab
[(int)*first
].sz
= ssz
;
1436 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1439 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1440 * opening anything that's not in our cwd or anything beneath
1441 * it. Thus, explicitly disallow traversing up the file-system
1442 * or using absolute paths.
1446 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1447 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1457 roff_userdef(ROFF_ARGS
)
1464 * Collect pointers to macro argument strings
1465 * and null-terminate them.
1468 for (i
= 0; i
< 9; i
++)
1469 arg
[i
] = '\0' == *cp
? "" :
1470 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1473 * Expand macro arguments.
1476 n1
= cp
= mandoc_strdup(r
->current_string
);
1477 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1479 if (0 > i
|| 8 < i
) {
1480 /* Not an argument invocation. */
1485 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1486 n2
= mandoc_malloc(*szp
);
1488 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1489 strlcat(n2
, arg
[i
], *szp
);
1490 strlcat(n2
, cp
+ 3, *szp
);
1492 cp
= n2
+ (cp
- n1
);
1498 * Replace the macro invocation
1499 * by the expanded macro.
1504 *szp
= strlen(*bufp
) + 1;
1506 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1507 ROFF_REPARSE
: ROFF_APPEND
);
1511 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1519 /* Read until end of name. */
1520 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1526 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1531 /* Nil-terminate name. */
1535 /* Read past spaces. */
1544 * Store *string into the user-defined string called *name.
1545 * In multiline mode, append to an existing entry and append '\n';
1546 * else replace the existing entry, if there is one.
1547 * To clear an existing entry, call with (*r, *name, NULL, 0).
1550 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1554 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
1555 string
? strlen(string
) : 0, multiline
);
1559 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
1560 const char *string
, size_t stringsz
, int multiline
)
1565 size_t oldch
, newch
;
1567 /* Search for an existing string with the same name. */
1570 while (n
&& strcmp(name
, n
->key
.p
))
1574 /* Create a new string table entry. */
1575 n
= mandoc_malloc(sizeof(struct roffkv
));
1576 n
->key
.p
= mandoc_strndup(name
, namesz
);
1582 } else if (0 == multiline
) {
1583 /* In multiline mode, append; else replace. */
1593 * One additional byte for the '\n' in multiline mode,
1594 * and one for the terminating '\0'.
1596 newch
= stringsz
+ (multiline
? 2u : 1u);
1598 if (NULL
== n
->val
.p
) {
1599 n
->val
.p
= mandoc_malloc(newch
);
1604 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
1607 /* Skip existing content in the destination buffer. */
1608 c
= n
->val
.p
+ (int)oldch
;
1610 /* Append new content to the destination buffer. */
1612 while (i
< (int)stringsz
) {
1614 * Rudimentary roff copy mode:
1615 * Handle escaped backslashes.
1617 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
1622 /* Append terminating bytes. */
1627 n
->val
.sz
= (int)(c
- n
->val
.p
);
1631 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1633 const struct roffkv
*n
;
1635 for (n
= r
->strtab
; n
; n
= n
->next
)
1636 if (0 == strncmp(name
, n
->key
.p
, len
) &&
1637 '\0' == n
->key
.p
[(int)len
])
1644 roff_freestr(struct roffkv
*r
)
1646 struct roffkv
*n
, *nn
;
1648 for (n
= r
; n
; n
= nn
) {
1656 const struct tbl_span
*
1657 roff_span(const struct roff
*r
)
1660 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1664 roff_eqn(const struct roff
*r
)
1667 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
1671 roff_eqndelim(const struct roff
*r
)
1678 * Duplicate an input string, making the appropriate character
1679 * conversations (as stipulated by `tr') along the way.
1680 * Returns a heap-allocated string with all the replacements made.
1683 roff_strdup(const struct roff
*r
, const char *p
)
1685 const struct roffkv
*cp
;
1689 enum mandoc_esc esc
;
1691 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
1692 return(mandoc_strdup(p
));
1693 else if ('\0' == *p
)
1694 return(mandoc_strdup(""));
1697 * Step through each character looking for term matches
1698 * (remember that a `tr' can be invoked with an escape, which is
1699 * a glyph but the escape is multi-character).
1700 * We only do this if the character hash has been initialised
1701 * and the string is >0 length.
1707 while ('\0' != *p
) {
1708 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
1709 sz
= r
->xtab
[(int)*p
].sz
;
1710 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1711 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
1715 } else if ('\\' != *p
) {
1716 res
= mandoc_realloc(res
, ssz
+ 2);
1721 /* Search for term matches. */
1722 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
1723 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
1728 * A match has been found.
1729 * Append the match to the array and move
1730 * forward by its keysize.
1732 res
= mandoc_realloc
1733 (res
, ssz
+ cp
->val
.sz
+ 1);
1734 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
1736 p
+= (int)cp
->key
.sz
;
1741 * Handle escapes carefully: we need to copy
1742 * over just the escape itself, or else we might
1743 * do replacements within the escape itself.
1744 * Make sure to pass along the bogus string.
1747 esc
= mandoc_escape(&p
, NULL
, NULL
);
1748 if (ESCAPE_ERROR
== esc
) {
1750 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1751 memcpy(res
+ ssz
, pp
, sz
);
1755 * We bail out on bad escapes.
1756 * No need to warn: we already did so when
1757 * roff_res() was called.
1760 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1761 memcpy(res
+ ssz
, pp
, sz
);
1765 res
[(int)ssz
] = '\0';