]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.170 2011/09/18 23:51:31 schwarze 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
34 /* Maximum number of string expansions per line, to break infinite loops. */
35 #define EXPAND_LIMIT 1000
78 * A single register entity. If "set" is zero, the value of the
79 * register should be the default one, which is per-register.
80 * Registers are assumed to be unsigned ints for now.
83 int set
; /* whether set or not */
84 unsigned int u
; /* unsigned integer */
88 * An incredibly-simple string buffer.
91 char *p
; /* nil-terminated buffer */
92 size_t sz
; /* saved strlen(p) */
96 * A key-value roffstr pair as part of a singly-linked list.
101 struct roffkv
*next
; /* next in list */
105 struct mparse
*parse
; /* parse point */
106 struct roffnode
*last
; /* leaf of stack */
107 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
108 int rstackpos
; /* position in rstack */
109 struct reg regs
[REG__MAX
];
110 struct roffkv
*strtab
; /* user-defined strings & macros */
111 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
112 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
113 const char *current_string
; /* value of last called user macro */
114 struct tbl_node
*first_tbl
; /* first table parsed */
115 struct tbl_node
*last_tbl
; /* last table parsed */
116 struct tbl_node
*tbl
; /* current table being parsed */
117 struct eqn_node
*last_eqn
; /* last equation parsed */
118 struct eqn_node
*first_eqn
; /* first equation parsed */
119 struct eqn_node
*eqn
; /* current equation being parsed */
123 enum rofft tok
; /* type of node */
124 struct roffnode
*parent
; /* up one in stack */
125 int line
; /* parse line */
126 int col
; /* parse col */
127 char *name
; /* node name, e.g. macro name */
128 char *end
; /* end-rules: custom token */
129 int endspan
; /* end-rules: next-line or infty */
130 enum roffrule rule
; /* current evaluation rule */
133 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
134 enum rofft tok, /* tok of macro */ \
135 char **bufp, /* input buffer */ \
136 size_t *szp, /* size of input buffer */ \
137 int ln, /* parse line */ \
138 int ppos, /* original pos in buffer */ \
139 int pos, /* current pos in buffer */ \
140 int *offs /* reset offset of buffer data */
142 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
145 const char *name
; /* macro name */
146 roffproc proc
; /* process new macro */
147 roffproc text
; /* process as child text of macro */
148 roffproc sub
; /* process as child of macro */
150 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
151 struct roffmac
*next
;
155 const char *name
; /* predefined input name */
156 const char *str
; /* replacement symbol */
159 #define PREDEF(__name, __str) \
160 { (__name), (__str) },
162 static enum rofft
roffhash_find(const char *, size_t);
163 static void roffhash_init(void);
164 static void roffnode_cleanscope(struct roff
*);
165 static void roffnode_pop(struct roff
*);
166 static void roffnode_push(struct roff
*, enum rofft
,
167 const char *, int, int);
168 static enum rofferr
roff_block(ROFF_ARGS
);
169 static enum rofferr
roff_block_text(ROFF_ARGS
);
170 static enum rofferr
roff_block_sub(ROFF_ARGS
);
171 static enum rofferr
roff_cblock(ROFF_ARGS
);
172 static enum rofferr
roff_ccond(ROFF_ARGS
);
173 static enum rofferr
roff_cond(ROFF_ARGS
);
174 static enum rofferr
roff_cond_text(ROFF_ARGS
);
175 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
176 static enum rofferr
roff_ds(ROFF_ARGS
);
177 static enum roffrule
roff_evalcond(const char *, int *);
178 static void roff_free1(struct roff
*);
179 static void roff_freestr(struct roffkv
*);
180 static char *roff_getname(struct roff
*, char **, int, int);
181 static const char *roff_getstrn(const struct roff
*,
182 const char *, size_t);
183 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
184 static enum rofferr
roff_nr(ROFF_ARGS
);
185 static void roff_openeqn(struct roff
*, const char *,
186 int, int, const char *);
187 static enum rofft
roff_parse(struct roff
*, const char *, int *);
188 static enum rofferr
roff_parsetext(char *);
189 static void roff_res(struct roff
*,
190 char **, size_t *, int, int);
191 static enum rofferr
roff_rm(ROFF_ARGS
);
192 static void roff_setstr(struct roff
*,
193 const char *, const char *, int);
194 static void roff_setstrn(struct roffkv
**, const char *,
195 size_t, const char *, size_t, int);
196 static enum rofferr
roff_so(ROFF_ARGS
);
197 static enum rofferr
roff_tr(ROFF_ARGS
);
198 static enum rofferr
roff_TE(ROFF_ARGS
);
199 static enum rofferr
roff_TS(ROFF_ARGS
);
200 static enum rofferr
roff_EQ(ROFF_ARGS
);
201 static enum rofferr
roff_EN(ROFF_ARGS
);
202 static enum rofferr
roff_T_(ROFF_ARGS
);
203 static enum rofferr
roff_userdef(ROFF_ARGS
);
205 /* See roffhash_find() */
209 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
211 static struct roffmac
*hash
[HASHWIDTH
];
213 static struct roffmac roffs
[ROFF_MAX
] = {
214 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
215 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
216 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
217 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
218 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
219 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
220 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
221 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
222 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
223 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
224 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
225 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
226 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
227 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
228 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
229 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
230 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
231 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
232 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
233 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
234 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
235 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
236 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
237 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
238 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
239 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
240 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
241 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
242 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
243 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
244 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
247 /* Array of injected predefined strings. */
248 #define PREDEFS_MAX 38
249 static const struct predef predefs
[PREDEFS_MAX
] = {
250 #include "predefs.in"
253 /* See roffhash_find() */
254 #define ROFF_HASH(p) (p[0] - ASCII_LO)
262 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
263 assert(roffs
[i
].name
[0] >= ASCII_LO
);
264 assert(roffs
[i
].name
[0] <= ASCII_HI
);
266 buc
= ROFF_HASH(roffs
[i
].name
);
268 if (NULL
!= (n
= hash
[buc
])) {
269 for ( ; n
->next
; n
= n
->next
)
273 hash
[buc
] = &roffs
[i
];
278 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
279 * the nil-terminated string name could be found.
282 roffhash_find(const char *p
, size_t s
)
288 * libroff has an extremely simple hashtable, for the time
289 * being, which simply keys on the first character, which must
290 * be printable, then walks a chain. It works well enough until
294 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
299 if (NULL
== (n
= hash
[buc
]))
301 for ( ; n
; n
= n
->next
)
302 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
303 return((enum rofft
)(n
- roffs
));
310 * Pop the current node off of the stack of roff instructions currently
314 roffnode_pop(struct roff
*r
)
321 r
->last
= r
->last
->parent
;
329 * Push a roff node onto the instruction stack. This must later be
330 * removed with roffnode_pop().
333 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
338 p
= mandoc_calloc(1, sizeof(struct roffnode
));
341 p
->name
= mandoc_strdup(name
);
345 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
352 roff_free1(struct roff
*r
)
358 while (NULL
!= (t
= r
->first_tbl
)) {
359 r
->first_tbl
= t
->next
;
363 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
365 while (NULL
!= (e
= r
->first_eqn
)) {
366 r
->first_eqn
= e
->next
;
370 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
375 roff_freestr(r
->strtab
);
376 roff_freestr(r
->xmbtab
);
378 r
->strtab
= r
->xmbtab
= NULL
;
381 for (i
= 0; i
< 128; i
++)
389 roff_reset(struct roff
*r
)
395 memset(&r
->regs
, 0, sizeof(struct reg
) * REG__MAX
);
397 for (i
= 0; i
< PREDEFS_MAX
; i
++)
398 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
403 roff_free(struct roff
*r
)
412 roff_alloc(struct mparse
*parse
)
417 r
= mandoc_calloc(1, sizeof(struct roff
));
423 for (i
= 0; i
< PREDEFS_MAX
; i
++)
424 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
430 * Pre-filter each and every line for reserved words (one beginning with
431 * `\*', e.g., `\*(ab'). These must be handled before the actual line
433 * This also checks the syntax of regular escapes.
436 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
439 const char *stesc
; /* start of an escape sequence ('\\') */
440 const char *stnam
; /* start of the name, after "[(*" */
441 const char *cp
; /* end of the name, e.g. before ']' */
442 const char *res
; /* the string to be substituted */
443 int i
, maxl
, expand_count
;
451 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
455 * The second character must be an asterisk.
456 * If it isn't, skip it anyway: It is escaped,
457 * so it can't start another escape sequence.
465 esc
= mandoc_escape(&cp
, NULL
, NULL
);
466 if (ESCAPE_ERROR
!= esc
)
470 (MANDOCERR_BADESCAPE
, r
->parse
,
471 ln
, (int)(stesc
- *bufp
), NULL
);
478 * The third character decides the length
479 * of the name of the string.
480 * Save a pointer to the name.
500 /* Advance to the end of the name. */
502 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
505 (MANDOCERR_BADESCAPE
,
507 (int)(stesc
- *bufp
), NULL
);
510 if (0 == maxl
&& ']' == *cp
)
515 * Retrieve the replacement string; if it is
516 * undefined, resume searching for escapes.
519 res
= roff_getstrn(r
, stnam
, (size_t)i
);
523 (MANDOCERR_BADESCAPE
, r
->parse
,
524 ln
, (int)(stesc
- *bufp
), NULL
);
528 /* Replace the escape sequence by the string. */
532 nsz
= *szp
+ strlen(res
) + 1;
533 n
= mandoc_malloc(nsz
);
535 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
536 strlcat(n
, res
, nsz
);
537 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
544 if (EXPAND_LIMIT
>= ++expand_count
)
547 /* Just leave the string unexpanded. */
548 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
, ln
, pos
, NULL
);
554 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
557 roff_parsetext(char *p
)
567 sz
= strcspn(p
, "-\\");
574 /* Skip over escapes. */
577 ((const char **)&p
, NULL
, NULL
);
578 if (ESCAPE_ERROR
== esc
)
581 } else if (p
== start
) {
589 '\t' != r
&& '\t' != l
&&
590 ' ' != r
&& ' ' != l
&&
591 '-' != r
&& '-' != l
&&
592 ! isdigit((unsigned char)l
) &&
593 ! isdigit((unsigned char)r
))
602 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
603 size_t *szp
, int pos
, int *offs
)
610 * Run the reserved-word filter only if we have some reserved
614 roff_res(r
, bufp
, szp
, ln
, pos
);
617 ctl
= mandoc_getcontrol(*bufp
, &pos
);
620 * First, if a scope is open and we're not a macro, pass the
621 * text through the macro's filter. If a scope isn't open and
622 * we're not a macro, just let it through.
623 * Finally, if there's an equation scope open, divert it into it
624 * no matter our state.
627 if (r
->last
&& ! ctl
) {
629 assert(roffs
[t
].text
);
631 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
632 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
636 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
638 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
639 return(roff_parsetext(*bufp
+ pos
));
642 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
644 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
645 return(roff_parsetext(*bufp
+ pos
));
647 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
650 * If a scope is open, go to the child handler for that macro,
651 * as it may want to preprocess before doing anything with it.
652 * Don't do so if an equation is open.
657 assert(roffs
[t
].sub
);
658 return((*roffs
[t
].sub
)
660 ln
, ppos
, pos
, offs
));
664 * Lastly, as we've no scope open, try to look up and execute
665 * the new macro. If no macro is found, simply return and let
666 * the compilers handle it.
669 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
672 assert(roffs
[t
].proc
);
673 return((*roffs
[t
].proc
)
675 ln
, ppos
, pos
, offs
));
680 roff_endparse(struct roff
*r
)
684 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
685 r
->last
->line
, r
->last
->col
, NULL
);
688 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
689 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
694 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
695 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
701 * Parse a roff node's type from the input buffer. This must be in the
702 * form of ".foo xxx" in the usual way.
705 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
711 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
712 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
716 * We stop the macro parse at an escape, tab, space, or nil.
717 * However, `\}' is also a valid macro, so make sure we don't
718 * clobber it by seeing the `\' as the end of token.
722 maclen
= strcspn(mac
+ 1, " \\\t\0") + 1;
724 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
725 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
729 while (buf
[*pos
] && ' ' == buf
[*pos
])
737 roff_cblock(ROFF_ARGS
)
741 * A block-close `..' should only be invoked as a child of an
742 * ignore macro, otherwise raise a warning and just ignore it.
745 if (NULL
== r
->last
) {
746 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
750 switch (r
->last
->tok
) {
758 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
765 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
770 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
773 roffnode_cleanscope(r
);
780 roffnode_cleanscope(struct roff
*r
)
784 if (--r
->last
->endspan
< 0)
793 roff_ccond(ROFF_ARGS
)
796 if (NULL
== r
->last
) {
797 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
801 switch (r
->last
->tok
) {
809 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
813 if (r
->last
->endspan
> -1) {
814 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
819 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
822 roffnode_cleanscope(r
);
829 roff_block(ROFF_ARGS
)
837 if (ROFF_ig
!= tok
) {
838 if ('\0' == (*bufp
)[pos
]) {
839 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
844 * Re-write `de1', since we don't really care about
845 * groff's strange compatibility mode, into `de'.
853 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
856 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
859 while (isspace((unsigned char)(*bufp
)[pos
]))
860 (*bufp
)[pos
++] = '\0';
863 roffnode_push(r
, tok
, name
, ln
, ppos
);
866 * At the beginning of a `de' macro, clear the existing string
867 * with the same name, if there is one. New content will be
868 * added from roff_block_text() in multiline mode.
872 roff_setstr(r
, name
, "", 0);
874 if ('\0' == (*bufp
)[pos
])
877 /* If present, process the custom end-of-line marker. */
880 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
884 * Note: groff does NOT like escape characters in the input.
885 * Instead of detecting this, we're just going to let it fly and
890 sz
= (size_t)(pos
- sv
);
892 if (1 == sz
&& '.' == (*bufp
)[sv
])
895 r
->last
->end
= mandoc_malloc(sz
+ 1);
897 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
898 r
->last
->end
[(int)sz
] = '\0';
901 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
909 roff_block_sub(ROFF_ARGS
)
915 * First check whether a custom macro exists at this level. If
916 * it does, then check against it. This is some of groff's
917 * stranger behaviours. If we encountered a custom end-scope
918 * tag and that tag also happens to be a "real" macro, then we
919 * need to try interpreting it again as a real macro. If it's
920 * not, then return ignore. Else continue.
924 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
925 if ((*bufp
)[i
] != r
->last
->end
[j
])
928 if ('\0' == r
->last
->end
[j
] &&
929 ('\0' == (*bufp
)[i
] ||
931 '\t' == (*bufp
)[i
])) {
933 roffnode_cleanscope(r
);
935 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
939 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
946 * If we have no custom end-query or lookup failed, then try
947 * pulling it out of the hashtable.
950 t
= roff_parse(r
, *bufp
, &pos
);
953 * Macros other than block-end are only significant
954 * in `de' blocks; elsewhere, simply throw them away.
956 if (ROFF_cblock
!= t
) {
958 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
962 assert(roffs
[t
].proc
);
963 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
964 ln
, ppos
, pos
, offs
));
970 roff_block_text(ROFF_ARGS
)
974 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
982 roff_cond_sub(ROFF_ARGS
)
989 roffnode_cleanscope(r
);
992 * If the macro is unknown, first check if it contains a closing
993 * delimiter `\}'. If it does, close out our scope and return
994 * the currently-scoped rule (ignore or continue). Else, drop
995 * into the currently-scoped rule.
998 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
1000 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1006 * Make the \} go away.
1007 * This is a little haphazard, as it's not quite
1008 * clear how nroff does this.
1009 * If we're at the end of line, then just chop
1010 * off the \} and resize the buffer.
1011 * If we aren't, then conver it to spaces.
1014 if ('\0' == *(ep
+ 1)) {
1018 *(ep
- 1) = *ep
= ' ';
1020 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1021 ln
, pos
, pos
+ 2, offs
);
1024 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1028 * A denied conditional must evaluate its children if and only
1029 * if they're either structurally required (such as loops and
1030 * conditionals) or a closing macro.
1033 if (ROFFRULE_DENY
== rr
)
1034 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
1035 if (ROFF_ccond
!= t
)
1038 assert(roffs
[t
].proc
);
1039 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1040 ln
, ppos
, pos
, offs
));
1045 roff_cond_text(ROFF_ARGS
)
1051 roffnode_cleanscope(r
);
1054 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1059 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1060 ln
, pos
, pos
+ 2, offs
);
1062 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1065 static enum roffrule
1066 roff_evalcond(const char *v
, int *pos
)
1072 return(ROFFRULE_ALLOW
);
1079 return(ROFFRULE_DENY
);
1084 while (v
[*pos
] && ' ' != v
[*pos
])
1086 return(ROFFRULE_DENY
);
1091 roff_line_ignore(ROFF_ARGS
)
1095 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
1102 roff_cond(ROFF_ARGS
)
1108 * An `.el' has no conditional body: it will consume the value
1109 * of the current rstack entry set in prior `ie' calls or
1112 * If we're not an `el', however, then evaluate the conditional.
1115 rule
= ROFF_el
== tok
?
1117 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
1118 roff_evalcond(*bufp
, &pos
);
1121 while (' ' == (*bufp
)[pos
])
1125 * Roff is weird. If we have just white-space after the
1126 * conditional, it's considered the BODY and we exit without
1127 * really doing anything. Warn about this. It's probably
1131 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1132 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1136 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1138 r
->last
->rule
= rule
;
1141 * An if-else will put the NEGATION of the current evaluated
1142 * conditional into the stack of rules.
1145 if (ROFF_ie
== tok
) {
1146 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1147 mandoc_msg(MANDOCERR_MEM
,
1148 r
->parse
, ln
, ppos
, NULL
);
1151 r
->rstack
[++r
->rstackpos
] =
1152 ROFFRULE_DENY
== r
->last
->rule
?
1153 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1156 /* If the parent has false as its rule, then so do we. */
1158 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1159 r
->last
->rule
= ROFFRULE_DENY
;
1162 * Determine scope. If we're invoked with "\{" trailing the
1163 * conditional, then we're in a multiline scope. Else our scope
1164 * expires on the next line.
1167 r
->last
->endspan
= 1;
1169 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1170 r
->last
->endspan
= -1;
1175 * If there are no arguments on the line, the next-line scope is
1179 if ('\0' == (*bufp
)[pos
])
1182 /* Otherwise re-run the roff parser after recalculating. */
1193 char *name
, *string
;
1196 * A symbol is named by the first word following the macro
1197 * invocation up to a space. Its value is anything after the
1198 * name's trailing whitespace and optional double-quote. Thus,
1202 * will have `bar " ' as its value.
1205 string
= *bufp
+ pos
;
1206 name
= roff_getname(r
, &string
, ln
, pos
);
1210 /* Read past initial double-quote. */
1214 /* The rest is the value. */
1215 roff_setstr(r
, name
, string
, 0);
1220 roff_regisset(const struct roff
*r
, enum regs reg
)
1223 return(r
->regs
[(int)reg
].set
);
1227 roff_regget(const struct roff
*r
, enum regs reg
)
1230 return(r
->regs
[(int)reg
].u
);
1234 roff_regunset(struct roff
*r
, enum regs reg
)
1237 r
->regs
[(int)reg
].set
= 0;
1249 key
= roff_getname(r
, &val
, ln
, pos
);
1251 if (0 == strcmp(key
, "nS")) {
1252 r
->regs
[(int)REG_nS
].set
= 1;
1253 if ((iv
= mandoc_strntoi(val
, strlen(val
), 10)) >= 0)
1254 r
->regs
[(int)REG_nS
].u
= (unsigned)iv
;
1256 r
->regs
[(int)REG_nS
].u
= 0u;
1270 while ('\0' != *cp
) {
1271 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1273 roff_setstr(r
, name
, NULL
, 0);
1284 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1297 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1299 tbl_restart(ppos
, ln
, r
->tbl
);
1306 roff_closeeqn(struct roff
*r
)
1309 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1314 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1315 int offs
, const char *buf
)
1320 assert(NULL
== r
->eqn
);
1321 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1324 r
->last_eqn
->next
= e
;
1326 r
->first_eqn
= r
->last_eqn
= e
;
1328 r
->eqn
= r
->last_eqn
= e
;
1332 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1341 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1350 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1361 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1365 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1368 r
->last_tbl
->next
= t
;
1370 r
->first_tbl
= r
->last_tbl
= t
;
1372 r
->tbl
= r
->last_tbl
= t
;
1380 const char *p
, *first
, *second
;
1382 enum mandoc_esc esc
;
1387 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1391 while ('\0' != *p
) {
1395 if ('\\' == *first
) {
1396 esc
= mandoc_escape(&p
, NULL
, NULL
);
1397 if (ESCAPE_ERROR
== esc
) {
1399 (MANDOCERR_BADESCAPE
, r
->parse
,
1400 ln
, (int)(p
- *bufp
), NULL
);
1403 fsz
= (size_t)(p
- first
);
1407 if ('\\' == *second
) {
1408 esc
= mandoc_escape(&p
, NULL
, NULL
);
1409 if (ESCAPE_ERROR
== esc
) {
1411 (MANDOCERR_BADESCAPE
, r
->parse
,
1412 ln
, (int)(p
- *bufp
), NULL
);
1415 ssz
= (size_t)(p
- second
);
1416 } else if ('\0' == *second
) {
1417 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1418 ln
, (int)(p
- *bufp
), NULL
);
1424 roff_setstrn(&r
->xmbtab
, first
,
1425 fsz
, second
, ssz
, 0);
1429 if (NULL
== r
->xtab
)
1430 r
->xtab
= mandoc_calloc
1431 (128, sizeof(struct roffstr
));
1433 free(r
->xtab
[(int)*first
].p
);
1434 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1435 r
->xtab
[(int)*first
].sz
= ssz
;
1447 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1450 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1451 * opening anything that's not in our cwd or anything beneath
1452 * it. Thus, explicitly disallow traversing up the file-system
1453 * or using absolute paths.
1457 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1458 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1468 roff_userdef(ROFF_ARGS
)
1475 * Collect pointers to macro argument strings
1476 * and null-terminate them.
1479 for (i
= 0; i
< 9; i
++)
1480 arg
[i
] = '\0' == *cp
? "" :
1481 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1484 * Expand macro arguments.
1487 n1
= cp
= mandoc_strdup(r
->current_string
);
1488 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1490 if (0 > i
|| 8 < i
) {
1491 /* Not an argument invocation. */
1496 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1497 n2
= mandoc_malloc(*szp
);
1499 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1500 strlcat(n2
, arg
[i
], *szp
);
1501 strlcat(n2
, cp
+ 3, *szp
);
1503 cp
= n2
+ (cp
- n1
);
1509 * Replace the macro invocation
1510 * by the expanded macro.
1515 *szp
= strlen(*bufp
) + 1;
1517 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1518 ROFF_REPARSE
: ROFF_APPEND
);
1522 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1530 /* Read until end of name. */
1531 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1537 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1542 /* Nil-terminate name. */
1546 /* Read past spaces. */
1555 * Store *string into the user-defined string called *name.
1556 * In multiline mode, append to an existing entry and append '\n';
1557 * else replace the existing entry, if there is one.
1558 * To clear an existing entry, call with (*r, *name, NULL, 0).
1561 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1565 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
1566 string
? strlen(string
) : 0, multiline
);
1570 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
1571 const char *string
, size_t stringsz
, int multiline
)
1576 size_t oldch
, newch
;
1578 /* Search for an existing string with the same name. */
1581 while (n
&& strcmp(name
, n
->key
.p
))
1585 /* Create a new string table entry. */
1586 n
= mandoc_malloc(sizeof(struct roffkv
));
1587 n
->key
.p
= mandoc_strndup(name
, namesz
);
1593 } else if (0 == multiline
) {
1594 /* In multiline mode, append; else replace. */
1604 * One additional byte for the '\n' in multiline mode,
1605 * and one for the terminating '\0'.
1607 newch
= stringsz
+ (multiline
? 2u : 1u);
1609 if (NULL
== n
->val
.p
) {
1610 n
->val
.p
= mandoc_malloc(newch
);
1615 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
1618 /* Skip existing content in the destination buffer. */
1619 c
= n
->val
.p
+ (int)oldch
;
1621 /* Append new content to the destination buffer. */
1623 while (i
< (int)stringsz
) {
1625 * Rudimentary roff copy mode:
1626 * Handle escaped backslashes.
1628 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
1633 /* Append terminating bytes. */
1638 n
->val
.sz
= (int)(c
- n
->val
.p
);
1642 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1644 const struct roffkv
*n
;
1646 for (n
= r
->strtab
; n
; n
= n
->next
)
1647 if (0 == strncmp(name
, n
->key
.p
, len
) &&
1648 '\0' == n
->key
.p
[(int)len
])
1655 roff_freestr(struct roffkv
*r
)
1657 struct roffkv
*n
, *nn
;
1659 for (n
= r
; n
; n
= nn
) {
1667 const struct tbl_span
*
1668 roff_span(const struct roff
*r
)
1671 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1675 roff_eqn(const struct roff
*r
)
1678 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
1682 * Duplicate an input string, making the appropriate character
1683 * conversations (as stipulated by `tr') along the way.
1684 * Returns a heap-allocated string with all the replacements made.
1687 roff_strdup(const struct roff
*r
, const char *p
)
1689 const struct roffkv
*cp
;
1693 enum mandoc_esc esc
;
1695 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
1696 return(mandoc_strdup(p
));
1697 else if ('\0' == *p
)
1698 return(mandoc_strdup(""));
1701 * Step through each character looking for term matches
1702 * (remember that a `tr' can be invoked with an escape, which is
1703 * a glyph but the escape is multi-character).
1704 * We only do this if the character hash has been initialised
1705 * and the string is >0 length.
1711 while ('\0' != *p
) {
1712 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
1713 sz
= r
->xtab
[(int)*p
].sz
;
1714 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1715 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
1719 } else if ('\\' != *p
) {
1720 res
= mandoc_realloc(res
, ssz
+ 2);
1725 /* Search for term matches. */
1726 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
1727 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
1732 * A match has been found.
1733 * Append the match to the array and move
1734 * forward by its keysize.
1736 res
= mandoc_realloc
1737 (res
, ssz
+ cp
->val
.sz
+ 1);
1738 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
1740 p
+= (int)cp
->key
.sz
;
1745 * Handle escapes carefully: we need to copy
1746 * over just the escape itself, or else we might
1747 * do replacements within the escape itself.
1748 * Make sure to pass along the bogus string.
1751 esc
= mandoc_escape(&p
, NULL
, NULL
);
1752 if (ESCAPE_ERROR
== esc
) {
1754 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1755 memcpy(res
+ ssz
, pp
, sz
);
1759 * We bail out on bad escapes.
1760 * No need to warn: we already did so when
1761 * roff_res() was called.
1764 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1765 memcpy(res
+ ssz
, pp
, sz
);
1769 res
[(int)ssz
] = '\0';