1 /* $Vendor-Id: roff.c,v 1.172 2011/10/24 21:41:45 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.
31 #include "libmandoc.h"
33 /* Maximum number of nested if-else conditionals. */
34 #define RSTACK_MAX 128
36 /* Maximum number of string expansions per line, to break infinite loops. */
37 #define EXPAND_LIMIT 1000
80 * A single register entity. If "set" is zero, the value of the
81 * register should be the default one, which is per-register.
82 * Registers are assumed to be unsigned ints for now.
85 int set
; /* whether set or not */
86 unsigned int u
; /* unsigned integer */
90 * An incredibly-simple string buffer.
93 char *p
; /* nil-terminated buffer */
94 size_t sz
; /* saved strlen(p) */
98 * A key-value roffstr pair as part of a singly-linked list.
103 struct roffkv
*next
; /* next in list */
107 struct mparse
*parse
; /* parse point */
108 struct roffnode
*last
; /* leaf of stack */
109 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
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 */
122 struct roff_nr
*nr
[64]; /* numbered register set */
126 enum rofft tok
; /* type of node */
127 struct roffnode
*parent
; /* up one in stack */
128 int line
; /* parse line */
129 int col
; /* parse col */
130 char *name
; /* node name, e.g. macro name */
131 char *end
; /* end-rules: custom token */
132 int endspan
; /* end-rules: next-line or infty */
133 enum roffrule rule
; /* current evaluation rule */
136 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
137 enum rofft tok, /* tok of macro */ \
138 char **bufp, /* input buffer */ \
139 size_t *szp, /* size of input buffer */ \
140 int ln, /* parse line */ \
141 int ppos, /* original pos in buffer */ \
142 int pos, /* current pos in buffer */ \
143 int *offs /* reset offset of buffer data */
145 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
148 const char *name
; /* macro name */
149 roffproc proc
; /* process new macro */
150 roffproc text
; /* process as child text of macro */
151 roffproc sub
; /* process as child of macro */
153 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
154 struct roffmac
*next
;
158 const char *name
; /* predefined input name */
159 const char *str
; /* replacement symbol */
162 #define PREDEF(__name, __str) \
163 { (__name), (__str) },
165 static enum rofft
roffhash_find(const char *, size_t);
166 static void roffhash_init(void);
167 static void roffnode_cleanscope(struct roff
*);
168 static void roffnode_pop(struct roff
*);
169 static void roffnode_push(struct roff
*, enum rofft
,
170 const char *, int, int);
171 static enum rofferr
roff_block(ROFF_ARGS
);
172 static enum rofferr
roff_block_text(ROFF_ARGS
);
173 static enum rofferr
roff_block_sub(ROFF_ARGS
);
174 static enum rofferr
roff_cblock(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 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
222 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
223 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
224 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
225 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
226 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
227 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
228 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
229 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
230 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
231 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
232 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
233 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
234 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
235 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
236 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
237 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
238 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
239 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
240 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
241 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
242 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
243 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
244 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
245 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
246 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
247 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
250 /* Array of injected predefined strings. */
251 #define PREDEFS_MAX 38
252 static const struct predef predefs
[PREDEFS_MAX
] = {
253 #include "predefs.in"
256 /* See roffhash_find() */
257 #define ROFF_HASH(p) (p[0] - ASCII_LO)
265 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
266 assert(roffs
[i
].name
[0] >= ASCII_LO
);
267 assert(roffs
[i
].name
[0] <= ASCII_HI
);
269 buc
= ROFF_HASH(roffs
[i
].name
);
271 if (NULL
!= (n
= hash
[buc
])) {
272 for ( ; n
->next
; n
= n
->next
)
276 hash
[buc
] = &roffs
[i
];
281 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
282 * the nil-terminated string name could be found.
285 roffhash_find(const char *p
, size_t s
)
291 * libroff has an extremely simple hashtable, for the time
292 * being, which simply keys on the first character, which must
293 * be printable, then walks a chain. It works well enough until
297 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
302 if (NULL
== (n
= hash
[buc
]))
304 for ( ; n
; n
= n
->next
)
305 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
306 return((enum rofft
)(n
- roffs
));
313 * Pop the current node off of the stack of roff instructions currently
317 roffnode_pop(struct roff
*r
)
324 r
->last
= r
->last
->parent
;
332 * Push a roff node onto the instruction stack. This must later be
333 * removed with roffnode_pop().
336 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
341 p
= mandoc_calloc(1, sizeof(struct roffnode
));
344 p
->name
= mandoc_strdup(name
);
348 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
355 roff_free1(struct roff
*r
)
361 while (NULL
!= (t
= r
->first_tbl
)) {
362 r
->first_tbl
= t
->next
;
366 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
368 while (NULL
!= (e
= r
->first_eqn
)) {
369 r
->first_eqn
= e
->next
;
373 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
378 roff_freestr(r
->strtab
);
379 roff_freestr(r
->xmbtab
);
381 r
->strtab
= r
->xmbtab
= NULL
;
384 for (i
= 0; i
< 128; i
++)
392 roff_reset(struct roff
*r
)
398 memset(&r
->regs
, 0, sizeof(r
->regs
));
399 memset(&r
->nr
, 0, sizeof(r
->nr
));
401 for (i
= 0; i
< PREDEFS_MAX
; i
++)
402 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
407 roff_free(struct roff
*r
)
416 roff_alloc(struct mparse
*parse
)
421 r
= mandoc_calloc(1, sizeof(struct roff
));
427 for (i
= 0; i
< PREDEFS_MAX
; i
++)
428 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
434 * Pre-filter each and every line for reserved words (one beginning with
435 * `\*', e.g., `\*(ab'). These must be handled before the actual line
437 * This also checks the syntax of regular escapes.
440 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
443 const char *stesc
; /* start of an escape sequence ('\\') */
444 const char *stnam
; /* start of the name, after "[(*" */
445 const char *cp
; /* end of the name, e.g. before ']' */
446 const char *res
; /* the string to be substituted */
447 int i
, maxl
, expand_count
;
455 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
459 * The second character must be an asterisk.
460 * If it isn't, skip it anyway: It is escaped,
461 * so it can't start another escape sequence.
469 esc
= mandoc_escape(&cp
, NULL
, NULL
);
470 if (ESCAPE_ERROR
!= esc
)
474 (MANDOCERR_BADESCAPE
, r
->parse
,
475 ln
, (int)(stesc
- *bufp
), NULL
);
482 * The third character decides the length
483 * of the name of the string.
484 * Save a pointer to the name.
504 /* Advance to the end of the name. */
506 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
509 (MANDOCERR_BADESCAPE
,
511 (int)(stesc
- *bufp
), NULL
);
514 if (0 == maxl
&& ']' == *cp
)
519 * Retrieve the replacement string; if it is
520 * undefined, resume searching for escapes.
523 res
= roff_getstrn(r
, stnam
, (size_t)i
);
527 (MANDOCERR_BADESCAPE
, r
->parse
,
528 ln
, (int)(stesc
- *bufp
), NULL
);
532 /* Replace the escape sequence by the string. */
536 nsz
= *szp
+ strlen(res
) + 1;
537 n
= mandoc_malloc(nsz
);
539 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
540 strlcat(n
, res
, nsz
);
541 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
548 if (EXPAND_LIMIT
>= ++expand_count
)
551 /* Just leave the string unexpanded. */
552 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
, ln
, pos
, NULL
);
559 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
562 roff_parsetext(char *p
)
571 sz
= strcspn(p
, "-\\");
578 /* Skip over escapes. */
581 ((const char **)/*XXX*/(void *)&p
, NULL
, NULL
);
582 if (ESCAPE_ERROR
== esc
)
585 } else if (p
== start
) {
590 if (isalpha((unsigned char)p
[-1]) &&
591 isalpha((unsigned char)p
[1]))
600 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
601 size_t *szp
, int pos
, int *offs
)
608 * Run the reserved-word filter only if we have some reserved
612 e
= roff_res(r
, bufp
, szp
, ln
, pos
);
615 assert(ROFF_CONT
== e
);
618 ctl
= mandoc_getcontrol(*bufp
, &pos
);
621 * First, if a scope is open and we're not a macro, pass the
622 * text through the macro's filter. If a scope isn't open and
623 * we're not a macro, just let it through.
624 * Finally, if there's an equation scope open, divert it into it
625 * no matter our state.
631 assert(roffs
[t
].text
);
633 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
634 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
639 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
641 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
642 return(roff_parsetext(*bufp
+ pos
));
644 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
647 * If a scope is open, go to the child handler for that macro,
648 * as it may want to preprocess before doing anything with it.
649 * Don't do so if an equation is open.
654 assert(roffs
[t
].sub
);
655 return((*roffs
[t
].sub
)
657 ln
, ppos
, pos
, offs
));
661 * Lastly, as we've no scope open, try to look up and execute
662 * the new macro. If no macro is found, simply return and let
663 * the compilers handle it.
666 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
669 assert(roffs
[t
].proc
);
670 return((*roffs
[t
].proc
)
672 ln
, ppos
, pos
, offs
));
677 roff_endparse(struct roff
*r
)
681 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
682 r
->last
->line
, r
->last
->col
, NULL
);
685 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
686 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
691 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
692 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
698 * Parse a roff node's type from the input buffer. This must be in the
699 * form of ".foo xxx" in the usual way.
702 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
708 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
709 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
713 * We stop the macro parse at an escape, tab, space, or nil.
714 * However, `\}' is also a valid macro, so make sure we don't
715 * clobber it by seeing the `\' as the end of token.
719 maclen
= strcspn(mac
+ 1, " \\\t\0") + 1;
721 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
722 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
726 while (buf
[*pos
] && ' ' == buf
[*pos
])
734 roff_cblock(ROFF_ARGS
)
738 * A block-close `..' should only be invoked as a child of an
739 * ignore macro, otherwise raise a warning and just ignore it.
742 if (NULL
== r
->last
) {
743 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
747 switch (r
->last
->tok
) {
755 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
762 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
767 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
770 roffnode_cleanscope(r
);
777 roffnode_cleanscope(struct roff
*r
)
781 if (--r
->last
->endspan
< 0)
790 roff_ccond(ROFF_ARGS
)
793 if (NULL
== r
->last
) {
794 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
798 switch (r
->last
->tok
) {
806 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
810 if (r
->last
->endspan
> -1) {
811 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
816 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
819 roffnode_cleanscope(r
);
826 roff_block(ROFF_ARGS
)
834 if (ROFF_ig
!= tok
) {
835 if ('\0' == (*bufp
)[pos
]) {
836 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
841 * Re-write `de1', since we don't really care about
842 * groff's strange compatibility mode, into `de'.
850 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
853 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
856 while (isspace((unsigned char)(*bufp
)[pos
]))
857 (*bufp
)[pos
++] = '\0';
860 roffnode_push(r
, tok
, name
, ln
, ppos
);
863 * At the beginning of a `de' macro, clear the existing string
864 * with the same name, if there is one. New content will be
865 * added from roff_block_text() in multiline mode.
869 roff_setstr(r
, name
, "", 0);
871 if ('\0' == (*bufp
)[pos
])
874 /* If present, process the custom end-of-line marker. */
877 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
881 * Note: groff does NOT like escape characters in the input.
882 * Instead of detecting this, we're just going to let it fly and
887 sz
= (size_t)(pos
- sv
);
889 if (1 == sz
&& '.' == (*bufp
)[sv
])
892 r
->last
->end
= mandoc_malloc(sz
+ 1);
894 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
895 r
->last
->end
[(int)sz
] = '\0';
898 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
906 roff_block_sub(ROFF_ARGS
)
912 * First check whether a custom macro exists at this level. If
913 * it does, then check against it. This is some of groff's
914 * stranger behaviours. If we encountered a custom end-scope
915 * tag and that tag also happens to be a "real" macro, then we
916 * need to try interpreting it again as a real macro. If it's
917 * not, then return ignore. Else continue.
921 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
922 if ((*bufp
)[i
] != r
->last
->end
[j
])
925 if ('\0' == r
->last
->end
[j
] &&
926 ('\0' == (*bufp
)[i
] ||
928 '\t' == (*bufp
)[i
])) {
930 roffnode_cleanscope(r
);
932 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
936 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
943 * If we have no custom end-query or lookup failed, then try
944 * pulling it out of the hashtable.
947 t
= roff_parse(r
, *bufp
, &pos
);
950 * Macros other than block-end are only significant
951 * in `de' blocks; elsewhere, simply throw them away.
953 if (ROFF_cblock
!= t
) {
955 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
959 assert(roffs
[t
].proc
);
960 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
961 ln
, ppos
, pos
, offs
));
967 roff_block_text(ROFF_ARGS
)
971 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
979 roff_cond_sub(ROFF_ARGS
)
986 roffnode_cleanscope(r
);
989 * If the macro is unknown, first check if it contains a closing
990 * delimiter `\}'. If it does, close out our scope and return
991 * the currently-scoped rule (ignore or continue). Else, drop
992 * into the currently-scoped rule.
995 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
997 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1003 * Make the \} go away.
1004 * This is a little haphazard, as it's not quite
1005 * clear how nroff does this.
1006 * If we're at the end of line, then just chop
1007 * off the \} and resize the buffer.
1008 * If we aren't, then conver it to spaces.
1011 if ('\0' == *(ep
+ 1)) {
1015 *(ep
- 1) = *ep
= ' ';
1017 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1018 ln
, pos
, pos
+ 2, offs
);
1021 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1025 * A denied conditional must evaluate its children if and only
1026 * if they're either structurally required (such as loops and
1027 * conditionals) or a closing macro.
1030 if (ROFFRULE_DENY
== rr
)
1031 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
1032 if (ROFF_ccond
!= t
)
1035 assert(roffs
[t
].proc
);
1036 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1037 ln
, ppos
, pos
, offs
));
1042 roff_cond_text(ROFF_ARGS
)
1048 roffnode_cleanscope(r
);
1051 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1056 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1057 ln
, pos
, pos
+ 2, offs
);
1059 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1063 roff_getnum(const char *v
, int *pos
, int *res
)
1067 if ((n
= (v
[*pos
] == '-')) != 0)
1071 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1072 *res
+= 10 * *res
+ v
[p
] - '0';
1084 roff_getop(const char *v
, int *pos
)
1087 switch (c
= v
[*pos
]) {
1093 if (v
[*pos
] == '=') {
1112 static enum roffrule
1113 roff_evalcond(const char *v
, int *pos
)
1120 return(ROFFRULE_ALLOW
);
1127 return(ROFFRULE_DENY
);
1135 if (!roff_getnum(v
, pos
, &lh
))
1136 return ROFFRULE_DENY
;
1137 if ((op
= roff_getop(v
, pos
)) == -1)
1139 if (!roff_getnum(v
, pos
, &rh
))
1140 return ROFFRULE_DENY
;
1161 return ROFFRULE_DENY
;
1166 return lh
? ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1171 roff_line_ignore(ROFF_ARGS
)
1175 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
1182 roff_cond(ROFF_ARGS
)
1188 * An `.el' has no conditional body: it will consume the value
1189 * of the current rstack entry set in prior `ie' calls or
1192 * If we're not an `el', however, then evaluate the conditional.
1195 rule
= ROFF_el
== tok
?
1197 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
1198 roff_evalcond(*bufp
, &pos
);
1201 while (' ' == (*bufp
)[pos
])
1205 * Roff is weird. If we have just white-space after the
1206 * conditional, it's considered the BODY and we exit without
1207 * really doing anything. Warn about this. It's probably
1211 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1212 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1216 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1218 r
->last
->rule
= rule
;
1221 * An if-else will put the NEGATION of the current evaluated
1222 * conditional into the stack of rules.
1225 if (ROFF_ie
== tok
) {
1226 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1227 mandoc_msg(MANDOCERR_MEM
,
1228 r
->parse
, ln
, ppos
, NULL
);
1231 r
->rstack
[++r
->rstackpos
] =
1232 ROFFRULE_DENY
== r
->last
->rule
?
1233 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1236 /* If the parent has false as its rule, then so do we. */
1238 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1239 r
->last
->rule
= ROFFRULE_DENY
;
1242 * Determine scope. If we're invoked with "\{" trailing the
1243 * conditional, then we're in a multiline scope. Else our scope
1244 * expires on the next line.
1247 r
->last
->endspan
= 1;
1249 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1250 r
->last
->endspan
= -1;
1255 * If there are no arguments on the line, the next-line scope is
1259 if ('\0' == (*bufp
)[pos
])
1262 /* Otherwise re-run the roff parser after recalculating. */
1273 char *name
, *string
;
1276 * A symbol is named by the first word following the macro
1277 * invocation up to a space. Its value is anything after the
1278 * name's trailing whitespace and optional double-quote. Thus,
1282 * will have `bar " ' as its value.
1285 string
= *bufp
+ pos
;
1286 name
= roff_getname(r
, &string
, ln
, pos
);
1290 /* Read past initial double-quote. */
1294 /* The rest is the value. */
1295 roff_setstr(r
, name
, string
, 0);
1300 roff_regisset(const struct roff
*r
, enum regs reg
)
1303 return(r
->regs
[(int)reg
].set
);
1307 roff_regget(const struct roff
*r
, enum regs reg
)
1310 return(r
->regs
[(int)reg
].u
);
1314 roff_regunset(struct roff
*r
, enum regs reg
)
1317 r
->regs
[(int)reg
].set
= 0;
1324 struct roff_nr
*next
;
1328 hash_str(const char *str
)
1330 const uint8_t *s
= (const uint8_t *)str
;
1333 while ((c
= *s
++) != '\0')
1334 hv
= hv
* 33 + c
; /* "perl": k=33, r=r+r/32 */
1335 return hv
+ (hv
>> 5);
1338 static struct roff_nr
*
1339 hash_find(struct roff
*r
, const char *str
, uint32_t *h
)
1342 *h
= hash_str(str
) % (sizeof(r
->nr
) / sizeof(r
->nr
[0]));
1344 for (e
= r
->nr
[*h
]; e
; e
= e
->next
)
1345 if (e
->hash
== *h
&& strcmp(e
->str
, str
) == 0)
1350 static struct roff_nr
*
1351 hash_insert(struct roff
*r
, const char *str
, uint32_t h
)
1355 e
= mandoc_malloc(sizeof(*e
));
1356 e
->str
= mandoc_strdup(str
);
1373 key
= roff_getname(r
, &val
, ln
, pos
);
1375 if ((h
= hash_find(r
, key
, &hv
)) == NULL
)
1376 h
= hash_insert(r
, key
, hv
);
1378 h
->val
= mandoc_strntoi(val
, strlen(val
), 10);
1380 if (0 == strcmp(key
, "nS")) {
1381 r
->regs
[(int)REG_nS
].set
= 1;
1383 r
->regs
[(int)REG_nS
].u
= (unsigned)h
->val
;
1385 r
->regs
[(int)REG_nS
].u
= 0u;
1392 roff_expand_nr(struct roff
*r
, const char *src
, int *sp
, size_t slen
,
1393 char **dst
, int *dp
, size_t *dlenp
)
1400 s
= *sp
+ 2; /* skip \\\n */
1402 if ('[' == src
[s
]) { /* XXX: Support builtins */
1408 for (l
= s
; src
[l
] && l
< (int)slen
; l
++) {
1413 if (!isalnum((unsigned char)src
[l
]))
1419 key
= mandoc_malloc(l
+ 1);
1420 memcpy(key
, src
+ s
, l
);
1423 if ((h
= hash_find(r
, key
, &hv
)) == NULL
) {
1427 if (*dst
== NULL
|| *dlenp
- *dp
< 256)
1428 *dst
= mandoc_realloc(*dst
, *dlenp
+= 256);
1430 /* XXX: support .af */
1431 *dp
+= snprintf(*dst
+ *dp
, *dlenp
- *dp
, "%jd", h
->val
);
1442 while ('\0' != *cp
) {
1443 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1445 roff_setstr(r
, name
, NULL
, 0);
1456 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1469 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1471 tbl_restart(ppos
, ln
, r
->tbl
);
1478 roff_closeeqn(struct roff
*r
)
1481 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1486 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1487 int offs
, const char *buf
)
1492 assert(NULL
== r
->eqn
);
1493 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1496 r
->last_eqn
->next
= e
;
1498 r
->first_eqn
= r
->last_eqn
= e
;
1500 r
->eqn
= r
->last_eqn
= e
;
1504 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1513 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1522 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1533 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1537 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1540 r
->last_tbl
->next
= t
;
1542 r
->first_tbl
= r
->last_tbl
= t
;
1544 r
->tbl
= r
->last_tbl
= t
;
1552 const char *p
, *first
, *second
;
1554 enum mandoc_esc esc
;
1559 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1563 while ('\0' != *p
) {
1567 if ('\\' == *first
) {
1568 esc
= mandoc_escape(&p
, NULL
, NULL
);
1569 if (ESCAPE_ERROR
== esc
) {
1571 (MANDOCERR_BADESCAPE
, r
->parse
,
1572 ln
, (int)(p
- *bufp
), NULL
);
1575 fsz
= (size_t)(p
- first
);
1579 if ('\\' == *second
) {
1580 esc
= mandoc_escape(&p
, NULL
, NULL
);
1581 if (ESCAPE_ERROR
== esc
) {
1583 (MANDOCERR_BADESCAPE
, r
->parse
,
1584 ln
, (int)(p
- *bufp
), NULL
);
1587 ssz
= (size_t)(p
- second
);
1588 } else if ('\0' == *second
) {
1589 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1590 ln
, (int)(p
- *bufp
), NULL
);
1596 roff_setstrn(&r
->xmbtab
, first
,
1597 fsz
, second
, ssz
, 0);
1601 if (NULL
== r
->xtab
)
1602 r
->xtab
= mandoc_calloc
1603 (128, sizeof(struct roffstr
));
1605 free(r
->xtab
[(int)*first
].p
);
1606 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1607 r
->xtab
[(int)*first
].sz
= ssz
;
1619 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1622 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1623 * opening anything that's not in our cwd or anything beneath
1624 * it. Thus, explicitly disallow traversing up the file-system
1625 * or using absolute paths.
1629 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1630 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1640 roff_userdef(ROFF_ARGS
)
1647 * Collect pointers to macro argument strings
1648 * and null-terminate them.
1651 for (i
= 0; i
< 9; i
++)
1652 arg
[i
] = '\0' == *cp
? "" :
1653 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1656 * Expand macro arguments.
1659 n1
= cp
= mandoc_strdup(r
->current_string
);
1660 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1662 if (0 > i
|| 8 < i
) {
1663 /* Not an argument invocation. */
1668 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1669 n2
= mandoc_malloc(*szp
);
1671 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1672 strlcat(n2
, arg
[i
], *szp
);
1673 strlcat(n2
, cp
+ 3, *szp
);
1675 cp
= n2
+ (cp
- n1
);
1681 * Replace the macro invocation
1682 * by the expanded macro.
1687 *szp
= strlen(*bufp
) + 1;
1689 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1690 ROFF_REPARSE
: ROFF_APPEND
);
1694 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1702 /* Read until end of name. */
1703 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1709 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1714 /* Nil-terminate name. */
1718 /* Read past spaces. */
1727 * Store *string into the user-defined string called *name.
1728 * In multiline mode, append to an existing entry and append '\n';
1729 * else replace the existing entry, if there is one.
1730 * To clear an existing entry, call with (*r, *name, NULL, 0).
1733 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1737 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
1738 string
? strlen(string
) : 0, multiline
);
1742 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
1743 const char *string
, size_t stringsz
, int multiline
)
1748 size_t oldch
, newch
;
1750 /* Search for an existing string with the same name. */
1753 while (n
&& strcmp(name
, n
->key
.p
))
1757 /* Create a new string table entry. */
1758 n
= mandoc_malloc(sizeof(struct roffkv
));
1759 n
->key
.p
= mandoc_strndup(name
, namesz
);
1765 } else if (0 == multiline
) {
1766 /* In multiline mode, append; else replace. */
1776 * One additional byte for the '\n' in multiline mode,
1777 * and one for the terminating '\0'.
1779 newch
= stringsz
+ (multiline
? 2u : 1u);
1781 if (NULL
== n
->val
.p
) {
1782 n
->val
.p
= mandoc_malloc(newch
);
1787 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
1790 /* Skip existing content in the destination buffer. */
1791 c
= n
->val
.p
+ (int)oldch
;
1793 /* Append new content to the destination buffer. */
1795 while (i
< (int)stringsz
) {
1797 * Rudimentary roff copy mode:
1798 * Handle escaped backslashes.
1800 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
1805 /* Append terminating bytes. */
1810 n
->val
.sz
= (int)(c
- n
->val
.p
);
1814 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1816 const struct roffkv
*n
;
1818 for (n
= r
->strtab
; n
; n
= n
->next
)
1819 if (0 == strncmp(name
, n
->key
.p
, len
) &&
1820 '\0' == n
->key
.p
[(int)len
])
1827 roff_freestr(struct roffkv
*r
)
1829 struct roffkv
*n
, *nn
;
1831 for (n
= r
; n
; n
= nn
) {
1839 const struct tbl_span
*
1840 roff_span(const struct roff
*r
)
1843 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1847 roff_eqn(const struct roff
*r
)
1850 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
1854 * Duplicate an input string, making the appropriate character
1855 * conversations (as stipulated by `tr') along the way.
1856 * Returns a heap-allocated string with all the replacements made.
1859 roff_strdup(const struct roff
*r
, const char *p
)
1861 const struct roffkv
*cp
;
1865 enum mandoc_esc esc
;
1867 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
1868 return(mandoc_strdup(p
));
1869 else if ('\0' == *p
)
1870 return(mandoc_strdup(""));
1873 * Step through each character looking for term matches
1874 * (remember that a `tr' can be invoked with an escape, which is
1875 * a glyph but the escape is multi-character).
1876 * We only do this if the character hash has been initialised
1877 * and the string is >0 length.
1883 while ('\0' != *p
) {
1884 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
1885 sz
= r
->xtab
[(int)*p
].sz
;
1886 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1887 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
1891 } else if ('\\' != *p
) {
1892 res
= mandoc_realloc(res
, ssz
+ 2);
1897 /* Search for term matches. */
1898 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
1899 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
1904 * A match has been found.
1905 * Append the match to the array and move
1906 * forward by its keysize.
1908 res
= mandoc_realloc
1909 (res
, ssz
+ cp
->val
.sz
+ 1);
1910 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
1912 p
+= (int)cp
->key
.sz
;
1917 * Handle escapes carefully: we need to copy
1918 * over just the escape itself, or else we might
1919 * do replacements within the escape itself.
1920 * Make sure to pass along the bogus string.
1923 esc
= mandoc_escape(&p
, NULL
, NULL
);
1924 if (ESCAPE_ERROR
== esc
) {
1926 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1927 memcpy(res
+ ssz
, pp
, sz
);
1931 * We bail out on bad escapes.
1932 * No need to warn: we already did so when
1933 * roff_res() was called.
1936 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
1937 memcpy(res
+ ssz
, pp
, sz
);
1941 res
[(int)ssz
] = '\0';