]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.174 2012/06/12 20:21:04 kristaps Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012 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
79 * A single register entity. If "set" is zero, the value of the
80 * register should be the default one, which is per-register.
81 * Registers are assumed to be unsigned ints for now.
84 int set
; /* whether set or not */
85 unsigned int u
; /* unsigned integer */
89 * An incredibly-simple string buffer.
92 char *p
; /* nil-terminated buffer */
93 size_t sz
; /* saved strlen(p) */
97 * A key-value roffstr pair as part of a singly-linked list.
102 struct roffkv
*next
; /* next in list */
106 struct mparse
*parse
; /* parse point */
107 struct roffnode
*last
; /* leaf of stack */
108 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
109 char control
; /* control character */
110 int rstackpos
; /* position in rstack */
111 struct reg regs
[REG__MAX
];
112 struct roffkv
*strtab
; /* user-defined strings & macros */
113 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
114 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
115 const char *current_string
; /* value of last called user macro */
116 struct tbl_node
*first_tbl
; /* first table parsed */
117 struct tbl_node
*last_tbl
; /* last table parsed */
118 struct tbl_node
*tbl
; /* current table being parsed */
119 struct eqn_node
*last_eqn
; /* last equation parsed */
120 struct eqn_node
*first_eqn
; /* first equation parsed */
121 struct eqn_node
*eqn
; /* current equation being parsed */
125 enum rofft tok
; /* type of node */
126 struct roffnode
*parent
; /* up one in stack */
127 int line
; /* parse line */
128 int col
; /* parse col */
129 char *name
; /* node name, e.g. macro name */
130 char *end
; /* end-rules: custom token */
131 int endspan
; /* end-rules: next-line or infty */
132 enum roffrule rule
; /* current evaluation rule */
135 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
136 enum rofft tok, /* tok of macro */ \
137 char **bufp, /* input buffer */ \
138 size_t *szp, /* size of input buffer */ \
139 int ln, /* parse line */ \
140 int ppos, /* original pos in buffer */ \
141 int pos, /* current pos in buffer */ \
142 int *offs /* reset offset of buffer data */
144 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
147 const char *name
; /* macro name */
148 roffproc proc
; /* process new macro */
149 roffproc text
; /* process as child text of macro */
150 roffproc sub
; /* process as child of macro */
152 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
153 struct roffmac
*next
;
157 const char *name
; /* predefined input name */
158 const char *str
; /* replacement symbol */
161 #define PREDEF(__name, __str) \
162 { (__name), (__str) },
164 static enum rofft
roffhash_find(const char *, size_t);
165 static void roffhash_init(void);
166 static void roffnode_cleanscope(struct roff
*);
167 static void roffnode_pop(struct roff
*);
168 static void roffnode_push(struct roff
*, enum rofft
,
169 const char *, int, int);
170 static enum rofferr
roff_block(ROFF_ARGS
);
171 static enum rofferr
roff_block_text(ROFF_ARGS
);
172 static enum rofferr
roff_block_sub(ROFF_ARGS
);
173 static enum rofferr
roff_cblock(ROFF_ARGS
);
174 static enum rofferr
roff_cc(ROFF_ARGS
);
175 static enum rofferr
roff_ccond(ROFF_ARGS
);
176 static enum rofferr
roff_cond(ROFF_ARGS
);
177 static enum rofferr
roff_cond_text(ROFF_ARGS
);
178 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
179 static enum rofferr
roff_ds(ROFF_ARGS
);
180 static enum roffrule
roff_evalcond(const char *, int *);
181 static void roff_free1(struct roff
*);
182 static void roff_freestr(struct roffkv
*);
183 static char *roff_getname(struct roff
*, char **, int, int);
184 static const char *roff_getstrn(const struct roff
*,
185 const char *, size_t);
186 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
187 static enum rofferr
roff_nr(ROFF_ARGS
);
188 static void roff_openeqn(struct roff
*, const char *,
189 int, int, const char *);
190 static enum rofft
roff_parse(struct roff
*, const char *, int *);
191 static enum rofferr
roff_parsetext(char *);
192 static enum rofferr
roff_res(struct roff
*,
193 char **, size_t *, int, int);
194 static enum rofferr
roff_rm(ROFF_ARGS
);
195 static void roff_setstr(struct roff
*,
196 const char *, const char *, int);
197 static void roff_setstrn(struct roffkv
**, const char *,
198 size_t, const char *, size_t, int);
199 static enum rofferr
roff_so(ROFF_ARGS
);
200 static enum rofferr
roff_tr(ROFF_ARGS
);
201 static enum rofferr
roff_TE(ROFF_ARGS
);
202 static enum rofferr
roff_TS(ROFF_ARGS
);
203 static enum rofferr
roff_EQ(ROFF_ARGS
);
204 static enum rofferr
roff_EN(ROFF_ARGS
);
205 static enum rofferr
roff_T_(ROFF_ARGS
);
206 static enum rofferr
roff_userdef(ROFF_ARGS
);
208 /* See roffhash_find() */
212 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
214 static struct roffmac
*hash
[HASHWIDTH
];
216 static struct roffmac roffs
[ROFF_MAX
] = {
217 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
218 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
219 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
220 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
221 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
222 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
223 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
224 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
225 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
226 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
227 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
228 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
229 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
230 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
231 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
232 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
233 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
234 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
235 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
236 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
237 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
238 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
239 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
240 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
241 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
242 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
243 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
244 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
245 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
246 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
247 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
248 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
251 /* Array of injected predefined strings. */
252 #define PREDEFS_MAX 38
253 static const struct predef predefs
[PREDEFS_MAX
] = {
254 #include "predefs.in"
257 /* See roffhash_find() */
258 #define ROFF_HASH(p) (p[0] - ASCII_LO)
266 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
267 assert(roffs
[i
].name
[0] >= ASCII_LO
);
268 assert(roffs
[i
].name
[0] <= ASCII_HI
);
270 buc
= ROFF_HASH(roffs
[i
].name
);
272 if (NULL
!= (n
= hash
[buc
])) {
273 for ( ; n
->next
; n
= n
->next
)
277 hash
[buc
] = &roffs
[i
];
282 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
283 * the nil-terminated string name could be found.
286 roffhash_find(const char *p
, size_t s
)
292 * libroff has an extremely simple hashtable, for the time
293 * being, which simply keys on the first character, which must
294 * be printable, then walks a chain. It works well enough until
298 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
303 if (NULL
== (n
= hash
[buc
]))
305 for ( ; n
; n
= n
->next
)
306 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
307 return((enum rofft
)(n
- roffs
));
314 * Pop the current node off of the stack of roff instructions currently
318 roffnode_pop(struct roff
*r
)
325 r
->last
= r
->last
->parent
;
333 * Push a roff node onto the instruction stack. This must later be
334 * removed with roffnode_pop().
337 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
342 p
= mandoc_calloc(1, sizeof(struct roffnode
));
345 p
->name
= mandoc_strdup(name
);
349 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
356 roff_free1(struct roff
*r
)
362 while (NULL
!= (t
= r
->first_tbl
)) {
363 r
->first_tbl
= t
->next
;
367 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
369 while (NULL
!= (e
= r
->first_eqn
)) {
370 r
->first_eqn
= e
->next
;
374 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
379 roff_freestr(r
->strtab
);
380 roff_freestr(r
->xmbtab
);
382 r
->strtab
= r
->xmbtab
= NULL
;
385 for (i
= 0; i
< 128; i
++)
393 roff_reset(struct roff
*r
)
400 memset(&r
->regs
, 0, sizeof(struct reg
) * REG__MAX
);
402 for (i
= 0; i
< PREDEFS_MAX
; i
++)
403 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
408 roff_free(struct roff
*r
)
417 roff_alloc(struct mparse
*parse
)
422 r
= mandoc_calloc(1, sizeof(struct roff
));
428 for (i
= 0; i
< PREDEFS_MAX
; i
++)
429 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
435 * Pre-filter each and every line for reserved words (one beginning with
436 * `\*', e.g., `\*(ab'). These must be handled before the actual line
438 * This also checks the syntax of regular escapes.
441 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
444 const char *stesc
; /* start of an escape sequence ('\\') */
445 const char *stnam
; /* start of the name, after "[(*" */
446 const char *cp
; /* end of the name, e.g. before ']' */
447 const char *res
; /* the string to be substituted */
448 int i
, maxl
, expand_count
;
456 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
460 * The second character must be an asterisk.
461 * If it isn't, skip it anyway: It is escaped,
462 * so it can't start another escape sequence.
470 esc
= mandoc_escape(&cp
, NULL
, NULL
);
471 if (ESCAPE_ERROR
!= esc
)
475 (MANDOCERR_BADESCAPE
, r
->parse
,
476 ln
, (int)(stesc
- *bufp
), NULL
);
483 * The third character decides the length
484 * of the name of the string.
485 * Save a pointer to the name.
505 /* Advance to the end of the name. */
507 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
510 (MANDOCERR_BADESCAPE
,
512 (int)(stesc
- *bufp
), NULL
);
515 if (0 == maxl
&& ']' == *cp
)
520 * Retrieve the replacement string; if it is
521 * undefined, resume searching for escapes.
524 res
= roff_getstrn(r
, stnam
, (size_t)i
);
528 (MANDOCERR_BADESCAPE
, r
->parse
,
529 ln
, (int)(stesc
- *bufp
), NULL
);
533 /* Replace the escape sequence by the string. */
537 nsz
= *szp
+ strlen(res
) + 1;
538 n
= mandoc_malloc(nsz
);
540 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
541 strlcat(n
, res
, nsz
);
542 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
549 if (EXPAND_LIMIT
>= ++expand_count
)
552 /* Just leave the string unexpanded. */
553 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
, ln
, pos
, NULL
);
560 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
563 roff_parsetext(char *p
)
572 sz
= strcspn(p
, "-\\");
579 /* Skip over escapes. */
582 ((const char **)&p
, NULL
, NULL
);
583 if (ESCAPE_ERROR
== esc
)
586 } else if (p
== start
) {
591 if (isalpha((unsigned char)p
[-1]) &&
592 isalpha((unsigned char)p
[1]))
601 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
602 size_t *szp
, int pos
, int *offs
)
609 * Run the reserved-word filter only if we have some reserved
613 e
= roff_res(r
, bufp
, szp
, ln
, pos
);
616 assert(ROFF_CONT
== e
);
619 ctl
= roff_getcontrol(r
, *bufp
, &pos
);
622 * First, if a scope is open and we're not a macro, pass the
623 * text through the macro's filter. If a scope isn't open and
624 * we're not a macro, just let it through.
625 * Finally, if there's an equation scope open, divert it into it
626 * no matter our state.
629 if (r
->last
&& ! ctl
) {
631 assert(roffs
[t
].text
);
633 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
634 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
638 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
640 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
641 return(roff_parsetext(*bufp
+ pos
));
644 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
646 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
647 return(roff_parsetext(*bufp
+ pos
));
649 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
652 * If a scope is open, go to the child handler for that macro,
653 * as it may want to preprocess before doing anything with it.
654 * Don't do so if an equation is open.
659 assert(roffs
[t
].sub
);
660 return((*roffs
[t
].sub
)
662 ln
, ppos
, pos
, offs
));
666 * Lastly, as we've no scope open, try to look up and execute
667 * the new macro. If no macro is found, simply return and let
668 * the compilers handle it.
671 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
674 assert(roffs
[t
].proc
);
675 return((*roffs
[t
].proc
)
677 ln
, ppos
, pos
, offs
));
682 roff_endparse(struct roff
*r
)
686 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
687 r
->last
->line
, r
->last
->col
, NULL
);
690 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
691 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
696 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
697 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
703 * Parse a roff node's type from the input buffer. This must be in the
704 * form of ".foo xxx" in the usual way.
707 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
713 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
714 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
718 * We stop the macro parse at an escape, tab, space, or nil.
719 * However, `\}' is also a valid macro, so make sure we don't
720 * clobber it by seeing the `\' as the end of token.
724 maclen
= strcspn(mac
+ 1, " \\\t\0") + 1;
726 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
727 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
731 while (buf
[*pos
] && ' ' == buf
[*pos
])
739 roff_cblock(ROFF_ARGS
)
743 * A block-close `..' should only be invoked as a child of an
744 * ignore macro, otherwise raise a warning and just ignore it.
747 if (NULL
== r
->last
) {
748 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
752 switch (r
->last
->tok
) {
760 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
767 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
772 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
775 roffnode_cleanscope(r
);
782 roffnode_cleanscope(struct roff
*r
)
786 if (--r
->last
->endspan
!= 0)
795 roff_ccond(ROFF_ARGS
)
798 if (NULL
== r
->last
) {
799 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
803 switch (r
->last
->tok
) {
811 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
815 if (r
->last
->endspan
> -1) {
816 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
821 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
824 roffnode_cleanscope(r
);
831 roff_block(ROFF_ARGS
)
839 if (ROFF_ig
!= tok
) {
840 if ('\0' == (*bufp
)[pos
]) {
841 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
846 * Re-write `de1', since we don't really care about
847 * groff's strange compatibility mode, into `de'.
855 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
858 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
861 while (isspace((unsigned char)(*bufp
)[pos
]))
862 (*bufp
)[pos
++] = '\0';
865 roffnode_push(r
, tok
, name
, ln
, ppos
);
868 * At the beginning of a `de' macro, clear the existing string
869 * with the same name, if there is one. New content will be
870 * added from roff_block_text() in multiline mode.
874 roff_setstr(r
, name
, "", 0);
876 if ('\0' == (*bufp
)[pos
])
879 /* If present, process the custom end-of-line marker. */
882 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
886 * Note: groff does NOT like escape characters in the input.
887 * Instead of detecting this, we're just going to let it fly and
892 sz
= (size_t)(pos
- sv
);
894 if (1 == sz
&& '.' == (*bufp
)[sv
])
897 r
->last
->end
= mandoc_malloc(sz
+ 1);
899 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
900 r
->last
->end
[(int)sz
] = '\0';
903 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
911 roff_block_sub(ROFF_ARGS
)
917 * First check whether a custom macro exists at this level. If
918 * it does, then check against it. This is some of groff's
919 * stranger behaviours. If we encountered a custom end-scope
920 * tag and that tag also happens to be a "real" macro, then we
921 * need to try interpreting it again as a real macro. If it's
922 * not, then return ignore. Else continue.
926 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
927 if ((*bufp
)[i
] != r
->last
->end
[j
])
930 if ('\0' == r
->last
->end
[j
] &&
931 ('\0' == (*bufp
)[i
] ||
933 '\t' == (*bufp
)[i
])) {
935 roffnode_cleanscope(r
);
937 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
941 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
948 * If we have no custom end-query or lookup failed, then try
949 * pulling it out of the hashtable.
952 t
= roff_parse(r
, *bufp
, &pos
);
955 * Macros other than block-end are only significant
956 * in `de' blocks; elsewhere, simply throw them away.
958 if (ROFF_cblock
!= t
) {
960 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
964 assert(roffs
[t
].proc
);
965 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
966 ln
, ppos
, pos
, offs
));
972 roff_block_text(ROFF_ARGS
)
976 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
984 roff_cond_sub(ROFF_ARGS
)
991 roffnode_cleanscope(r
);
994 * If the macro is unknown, first check if it contains a closing
995 * delimiter `\}'. If it does, close out our scope and return
996 * the currently-scoped rule (ignore or continue). Else, drop
997 * into the currently-scoped rule.
1000 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
1002 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1008 * Make the \} go away.
1009 * This is a little haphazard, as it's not quite
1010 * clear how nroff does this.
1011 * If we're at the end of line, then just chop
1012 * off the \} and resize the buffer.
1013 * If we aren't, then conver it to spaces.
1016 if ('\0' == *(ep
+ 1)) {
1020 *(ep
- 1) = *ep
= ' ';
1022 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1023 ln
, pos
, pos
+ 2, offs
);
1026 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1030 * A denied conditional must evaluate its children if and only
1031 * if they're either structurally required (such as loops and
1032 * conditionals) or a closing macro.
1035 if (ROFFRULE_DENY
== rr
)
1036 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
1037 if (ROFF_ccond
!= t
)
1040 assert(roffs
[t
].proc
);
1041 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1042 ln
, ppos
, pos
, offs
));
1047 roff_cond_text(ROFF_ARGS
)
1053 roffnode_cleanscope(r
);
1056 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1061 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1062 ln
, pos
, pos
+ 2, offs
);
1064 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1067 static enum roffrule
1068 roff_evalcond(const char *v
, int *pos
)
1074 return(ROFFRULE_ALLOW
);
1081 return(ROFFRULE_DENY
);
1086 while (v
[*pos
] && ' ' != v
[*pos
])
1088 return(ROFFRULE_DENY
);
1093 roff_line_ignore(ROFF_ARGS
)
1097 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
1104 roff_cond(ROFF_ARGS
)
1107 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1110 * An `.el' has no conditional body: it will consume the value
1111 * of the current rstack entry set in prior `ie' calls or
1114 * If we're not an `el', however, then evaluate the conditional.
1117 r
->last
->rule
= ROFF_el
== tok
?
1119 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
1120 roff_evalcond(*bufp
, &pos
);
1123 * An if-else will put the NEGATION of the current evaluated
1124 * conditional into the stack of rules.
1127 if (ROFF_ie
== tok
) {
1128 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1129 mandoc_msg(MANDOCERR_MEM
,
1130 r
->parse
, ln
, ppos
, NULL
);
1133 r
->rstack
[++r
->rstackpos
] =
1134 ROFFRULE_DENY
== r
->last
->rule
?
1135 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1138 /* If the parent has false as its rule, then so do we. */
1140 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1141 r
->last
->rule
= ROFFRULE_DENY
;
1145 * If there is nothing on the line after the conditional,
1146 * not even whitespace, use next-line scope.
1149 if ('\0' == (*bufp
)[pos
]) {
1150 r
->last
->endspan
= 2;
1154 while (' ' == (*bufp
)[pos
])
1157 /* An opening brace requests multiline scope. */
1159 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1160 r
->last
->endspan
= -1;
1166 * Anything else following the conditional causes
1167 * single-line scope. Warn if the scope contains
1168 * nothing but trailing whitespace.
1171 if ('\0' == (*bufp
)[pos
])
1172 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1174 r
->last
->endspan
= 1;
1186 char *name
, *string
;
1189 * A symbol is named by the first word following the macro
1190 * invocation up to a space. Its value is anything after the
1191 * name's trailing whitespace and optional double-quote. Thus,
1195 * will have `bar " ' as its value.
1198 string
= *bufp
+ pos
;
1199 name
= roff_getname(r
, &string
, ln
, pos
);
1203 /* Read past initial double-quote. */
1207 /* The rest is the value. */
1208 roff_setstr(r
, name
, string
, 0);
1213 roff_regisset(const struct roff
*r
, enum regs reg
)
1216 return(r
->regs
[(int)reg
].set
);
1220 roff_regget(const struct roff
*r
, enum regs reg
)
1223 return(r
->regs
[(int)reg
].u
);
1227 roff_regunset(struct roff
*r
, enum regs reg
)
1230 r
->regs
[(int)reg
].set
= 0;
1242 key
= roff_getname(r
, &val
, ln
, pos
);
1244 if (0 == strcmp(key
, "nS")) {
1245 r
->regs
[(int)REG_nS
].set
= 1;
1246 if ((iv
= mandoc_strntoi(val
, strlen(val
), 10)) >= 0)
1247 r
->regs
[(int)REG_nS
].u
= (unsigned)iv
;
1249 r
->regs
[(int)REG_nS
].u
= 0u;
1263 while ('\0' != *cp
) {
1264 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1266 roff_setstr(r
, name
, NULL
, 0);
1277 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1290 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1292 tbl_restart(ppos
, ln
, r
->tbl
);
1299 roff_closeeqn(struct roff
*r
)
1302 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1307 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1308 int offs
, const char *buf
)
1313 assert(NULL
== r
->eqn
);
1314 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1317 r
->last_eqn
->next
= e
;
1319 r
->first_eqn
= r
->last_eqn
= e
;
1321 r
->eqn
= r
->last_eqn
= e
;
1325 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1334 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1343 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1354 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1358 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1361 r
->last_tbl
->next
= t
;
1363 r
->first_tbl
= r
->last_tbl
= t
;
1365 r
->tbl
= r
->last_tbl
= t
;
1377 if ('\0' == *p
|| '.' == (r
->control
= *p
++))
1381 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1390 const char *p
, *first
, *second
;
1392 enum mandoc_esc esc
;
1397 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1401 while ('\0' != *p
) {
1405 if ('\\' == *first
) {
1406 esc
= mandoc_escape(&p
, NULL
, NULL
);
1407 if (ESCAPE_ERROR
== esc
) {
1409 (MANDOCERR_BADESCAPE
, r
->parse
,
1410 ln
, (int)(p
- *bufp
), NULL
);
1413 fsz
= (size_t)(p
- first
);
1417 if ('\\' == *second
) {
1418 esc
= mandoc_escape(&p
, NULL
, NULL
);
1419 if (ESCAPE_ERROR
== esc
) {
1421 (MANDOCERR_BADESCAPE
, r
->parse
,
1422 ln
, (int)(p
- *bufp
), NULL
);
1425 ssz
= (size_t)(p
- second
);
1426 } else if ('\0' == *second
) {
1427 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1428 ln
, (int)(p
- *bufp
), NULL
);
1434 roff_setstrn(&r
->xmbtab
, first
,
1435 fsz
, second
, ssz
, 0);
1439 if (NULL
== r
->xtab
)
1440 r
->xtab
= mandoc_calloc
1441 (128, sizeof(struct roffstr
));
1443 free(r
->xtab
[(int)*first
].p
);
1444 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1445 r
->xtab
[(int)*first
].sz
= ssz
;
1457 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1460 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1461 * opening anything that's not in our cwd or anything beneath
1462 * it. Thus, explicitly disallow traversing up the file-system
1463 * or using absolute paths.
1467 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1468 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1478 roff_userdef(ROFF_ARGS
)
1485 * Collect pointers to macro argument strings
1486 * and null-terminate them.
1489 for (i
= 0; i
< 9; i
++)
1490 arg
[i
] = '\0' == *cp
? "" :
1491 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1494 * Expand macro arguments.
1497 n1
= cp
= mandoc_strdup(r
->current_string
);
1498 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1500 if (0 > i
|| 8 < i
) {
1501 /* Not an argument invocation. */
1506 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1507 n2
= mandoc_malloc(*szp
);
1509 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1510 strlcat(n2
, arg
[i
], *szp
);
1511 strlcat(n2
, cp
+ 3, *szp
);
1513 cp
= n2
+ (cp
- n1
);
1519 * Replace the macro invocation
1520 * by the expanded macro.
1525 *szp
= strlen(*bufp
) + 1;
1527 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1528 ROFF_REPARSE
: ROFF_APPEND
);
1532 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1540 /* Read until end of name. */
1541 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1547 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1552 /* Nil-terminate name. */
1556 /* Read past spaces. */
1565 * Store *string into the user-defined string called *name.
1566 * In multiline mode, append to an existing entry and append '\n';
1567 * else replace the existing entry, if there is one.
1568 * To clear an existing entry, call with (*r, *name, NULL, 0).
1571 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1575 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
1576 string
? strlen(string
) : 0, multiline
);
1580 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
1581 const char *string
, size_t stringsz
, int multiline
)
1586 size_t oldch
, newch
;
1588 /* Search for an existing string with the same name. */
1591 while (n
&& strcmp(name
, n
->key
.p
))
1595 /* Create a new string table entry. */
1596 n
= mandoc_malloc(sizeof(struct roffkv
));
1597 n
->key
.p
= mandoc_strndup(name
, namesz
);
1603 } else if (0 == multiline
) {
1604 /* In multiline mode, append; else replace. */
1614 * One additional byte for the '\n' in multiline mode,
1615 * and one for the terminating '\0'.
1617 newch
= stringsz
+ (multiline
? 2u : 1u);
1619 if (NULL
== n
->val
.p
) {
1620 n
->val
.p
= mandoc_malloc(newch
);
1625 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
1628 /* Skip existing content in the destination buffer. */
1629 c
= n
->val
.p
+ (int)oldch
;
1631 /* Append new content to the destination buffer. */
1633 while (i
< (int)stringsz
) {
1635 * Rudimentary roff copy mode:
1636 * Handle escaped backslashes.
1638 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
1643 /* Append terminating bytes. */
1648 n
->val
.sz
= (int)(c
- n
->val
.p
);
1652 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1654 const struct roffkv
*n
;
1656 for (n
= r
->strtab
; n
; n
= n
->next
)
1657 if (0 == strncmp(name
, n
->key
.p
, len
) &&
1658 '\0' == n
->key
.p
[(int)len
])
1665 roff_freestr(struct roffkv
*r
)
1667 struct roffkv
*n
, *nn
;
1669 for (n
= r
; n
; n
= nn
) {
1677 const struct tbl_span
*
1678 roff_span(const struct roff
*r
)
1681 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1685 roff_eqn(const struct roff
*r
)
1688 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
1692 * Duplicate an input string, making the appropriate character
1693 * conversations (as stipulated by `tr') along the way.
1694 * Returns a heap-allocated string with all the replacements made.
1697 roff_strdup(const struct roff
*r
, const char *p
)
1699 const struct roffkv
*cp
;
1703 enum mandoc_esc esc
;
1705 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
1706 return(mandoc_strdup(p
));
1707 else if ('\0' == *p
)
1708 return(mandoc_strdup(""));
1711 * Step through each character looking for term matches
1712 * (remember that a `tr' can be invoked with an escape, which is
1713 * a glyph but the escape is multi-character).
1714 * We only do this if the character hash has been initialised
1715 * and the string is >0 length.
1721 while ('\0' != *p
) {
1722 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
1723 sz
= r
->xtab
[(int)*p
].sz
;
1724 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1725 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
1729 } else if ('\\' != *p
) {
1730 res
= mandoc_realloc(res
, ssz
+ 2);
1735 /* Search for term matches. */
1736 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
1737 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
1742 * A match has been found.
1743 * Append the match to the array and move
1744 * forward by its keysize.
1746 res
= mandoc_realloc
1747 (res
, ssz
+ cp
->val
.sz
+ 1);
1748 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
1750 p
+= (int)cp
->key
.sz
;
1755 * Handle escapes carefully: we need to copy
1756 * over just the escape itself, or else we might
1757 * do replacements within the escape itself.
1758 * Make sure to pass along the bogus string.
1761 esc
= mandoc_escape(&p
, NULL
, NULL
);
1762 if (ESCAPE_ERROR
== esc
) {
1764 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1765 memcpy(res
+ ssz
, pp
, sz
);
1769 * We bail out on bad escapes.
1770 * No need to warn: we already did so when
1771 * roff_res() was called.
1774 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1775 memcpy(res
+ ssz
, pp
, sz
);
1779 res
[(int)ssz
] = '\0';
1784 * Find out whether a line is a macro line or not.
1785 * If it is, adjust the current position and return one; if it isn't,
1786 * return zero and don't change the current position.
1787 * If the control character has been set with `.cc', then let that grain
1789 * This is slighly contrary to groff, where using the non-breaking
1790 * control character when `cc' has been invoked will cause the
1791 * non-breaking macro contents to be printed verbatim.
1794 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
1800 if (0 != r
->control
&& cp
[pos
] == r
->control
)
1802 else if (0 != r
->control
)
1804 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
1806 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
1811 while (' ' == cp
[pos
] || '\t' == cp
[pos
])