1 /* $Id: roff.c,v 1.130 2011/03/29 09:00:48 kristaps Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include "libmandoc.h"
34 #define RSTACK_MAX 128
66 ROFF_ccond, /* FIXME: remove this. */
77 char *name; /* key of symbol */
78 char *string; /* current value */
79 struct roffstr *next; /* next in list */
83 struct mparse *parse; /* parse point */
84 struct roffnode *last; /* leaf of stack */
85 enum roffrule rstack[RSTACK_MAX]; /* stack of !`ie' rules */
86 int rstackpos; /* position in rstack */
87 struct regset *regs; /* read/writable registers */
88 struct roffstr *first_string; /* user-defined strings & macros */
89 const char *current_string; /* value of last called user macro */
90 struct tbl_node *first_tbl; /* first table parsed */
91 struct tbl_node *last_tbl; /* last table parsed */
92 struct tbl_node *tbl; /* current table being parsed */
93 struct eqn_node *last_eqn; /* last equation parsed */
94 struct eqn_node *first_eqn; /* first equation parsed */
95 struct eqn_node *eqn; /* current equation being parsed */
99 enum rofft tok; /* type of node */
100 struct roffnode *parent; /* up one in stack */
101 int line; /* parse line */
102 int col; /* parse col */
103 char *name; /* node name, e.g. macro name */
104 char *end; /* end-rules: custom token */
105 int endspan; /* end-rules: next-line or infty */
106 enum roffrule rule; /* current evaluation rule */
109 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
110 enum rofft tok, /* tok of macro */ \
111 char **bufp, /* input buffer */ \
112 size_t *szp, /* size of input buffer */ \
113 int ln, /* parse line */ \
114 int ppos, /* original pos in buffer */ \
115 int pos, /* current pos in buffer */ \
116 int *offs /* reset offset of buffer data */
118 typedef enum rofferr (*roffproc)(ROFF_ARGS);
121 const char *name; /* macro name */
122 roffproc proc; /* process new macro */
123 roffproc text; /* process as child text of macro */
124 roffproc sub; /* process as child of macro */
126 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
127 struct roffmac *next;
130 static enum rofferr roff_block(ROFF_ARGS);
131 static enum rofferr roff_block_text(ROFF_ARGS);
132 static enum rofferr roff_block_sub(ROFF_ARGS);
133 static enum rofferr roff_cblock(ROFF_ARGS);
134 static enum rofferr roff_ccond(ROFF_ARGS);
135 static enum rofferr roff_cond(ROFF_ARGS);
136 static enum rofferr roff_cond_text(ROFF_ARGS);
137 static enum rofferr roff_cond_sub(ROFF_ARGS);
138 static enum rofferr roff_ds(ROFF_ARGS);
139 static enum roffrule roff_evalcond(const char *, int *);
140 static void roff_freestr(struct roff *);
141 static char *roff_getname(struct roff *, char **, int, int);
142 static const char *roff_getstrn(const struct roff *,
143 const char *, size_t);
144 static enum rofferr roff_line_ignore(ROFF_ARGS);
145 static enum rofferr roff_nr(ROFF_ARGS);
146 static int roff_res(struct roff *,
147 char **, size_t *, int);
148 static enum rofferr roff_rm(ROFF_ARGS);
149 static void roff_setstr(struct roff *,
150 const char *, const char *, int);
151 static enum rofferr roff_so(ROFF_ARGS);
152 static enum rofferr roff_TE(ROFF_ARGS);
153 static enum rofferr roff_TS(ROFF_ARGS);
154 static enum rofferr roff_EQ(ROFF_ARGS);
155 static enum rofferr roff_EN(ROFF_ARGS);
156 static enum rofferr roff_T_(ROFF_ARGS);
157 static enum rofferr roff_userdef(ROFF_ARGS);
159 /* See roff_hash_find() */
163 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
165 static struct roffmac *hash[HASHWIDTH];
167 static struct roffmac roffs[ROFF_MAX] = {
168 { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
169 { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
170 { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
171 { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
172 { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
173 { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
174 { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
175 { "ds", roff_ds, NULL, NULL, 0, NULL },
176 { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
177 { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
178 { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
179 { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
180 { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
181 { "it", roff_line_ignore, NULL, NULL, 0, NULL },
182 { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
183 { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
184 { "nr", roff_nr, NULL, NULL, 0, NULL },
185 { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
186 { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
187 { "rm", roff_rm, NULL, NULL, 0, NULL },
188 { "so", roff_so, NULL, NULL, 0, NULL },
189 { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
190 { "tr", roff_line_ignore, NULL, NULL, 0, NULL },
191 { "TS", roff_TS, NULL, NULL, 0, NULL },
192 { "TE", roff_TE, NULL, NULL, 0, NULL },
193 { "T&", roff_T_, NULL, NULL, 0, NULL },
194 { "EQ", roff_EQ, NULL, NULL, 0, NULL },
195 { "EN", roff_EN, NULL, NULL, 0, NULL },
196 { ".", roff_cblock, NULL, NULL, 0, NULL },
197 { "\\}", roff_ccond, NULL, NULL, 0, NULL },
198 { NULL, roff_userdef, NULL, NULL, 0, NULL },
201 static void roff_free1(struct roff *);
202 static enum rofft roff_hash_find(const char *, size_t);
203 static void roff_hash_init(void);
204 static void roffnode_cleanscope(struct roff *);
205 static void roffnode_push(struct roff *, enum rofft,
206 const char *, int, int);
207 static void roffnode_pop(struct roff *);
208 static enum rofft roff_parse(struct roff *, const char *, int *);
209 static int roff_parse_nat(const char *, unsigned int *);
211 /* See roff_hash_find() */
212 #define ROFF_HASH(p) (p[0] - ASCII_LO)
220 for (i = 0; i < (int)ROFF_USERDEF; i++) {
221 assert(roffs[i].name[0] >= ASCII_LO);
222 assert(roffs[i].name[0] <= ASCII_HI);
224 buc = ROFF_HASH(roffs[i].name);
226 if (NULL != (n = hash[buc])) {
227 for ( ; n->next; n = n->next)
231 hash[buc] = &roffs[i];
237 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
238 * the nil-terminated string name could be found.
241 roff_hash_find(const char *p, size_t s)
247 * libroff has an extremely simple hashtable, for the time
248 * being, which simply keys on the first character, which must
249 * be printable, then walks a chain. It works well enough until
253 if (p[0] < ASCII_LO || p[0] > ASCII_HI)
258 if (NULL == (n = hash[buc]))
260 for ( ; n; n = n->next)
261 if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
262 return((enum rofft)(n - roffs));
269 * Pop the current node off of the stack of roff instructions currently
273 roffnode_pop(struct roff *r)
280 if (ROFF_el == p->tok)
281 if (r->rstackpos > -1)
284 r->last = r->last->parent;
292 * Push a roff node onto the instruction stack. This must later be
293 * removed with roffnode_pop().
296 roffnode_push(struct roff *r, enum rofft tok, const char *name,
301 p = mandoc_calloc(1, sizeof(struct roffnode));
304 p->name = mandoc_strdup(name);
308 p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
315 roff_free1(struct roff *r)
320 while (NULL != (t = r->first_tbl)) {
321 r->first_tbl = t->next;
325 r->first_tbl = r->last_tbl = r->tbl = NULL;
327 while (NULL != (e = r->first_eqn)) {
328 r->first_eqn = e->next;
332 r->first_eqn = r->last_eqn = r->eqn = NULL;
342 roff_reset(struct roff *r)
350 roff_free(struct roff *r)
359 roff_alloc(struct regset *regs, struct mparse *parse)
363 r = mandoc_calloc(1, sizeof(struct roff));
374 * Pre-filter each and every line for reserved words (one beginning with
375 * `\*', e.g., `\*(ab'). These must be handled before the actual line
379 roff_res(struct roff *r, char **bufp, size_t *szp, int pos)
381 const char *stesc; /* start of an escape sequence ('\\') */
382 const char *stnam; /* start of the name, after "[(*" */
383 const char *cp; /* end of the name, e.g. before ']' */
384 const char *res; /* the string to be substituted */
389 /* Search for a leading backslash and save a pointer to it. */
392 while (NULL != (cp = strchr(cp, '\\'))) {
396 * The second character must be an asterisk.
397 * If it isn't, skip it anyway: It is escaped,
398 * so it can't start another escape sequence.
407 * The third character decides the length
408 * of the name of the string.
409 * Save a pointer to the name.
429 /* Advance to the end of the name. */
431 for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
433 return(1); /* Error. */
434 if (0 == maxl && ']' == *cp)
439 * Retrieve the replacement string; if it is
440 * undefined, resume searching for escapes.
443 res = roff_getstrn(r, stnam, (size_t)i);
450 /* Replace the escape sequence by the string. */
452 nsz = *szp + strlen(res) + 1;
453 n = mandoc_malloc(nsz);
455 strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
456 strlcat(n, res, nsz);
457 strlcat(n, cp + (maxl ? 0 : 1), nsz);
471 roff_parseln(struct roff *r, int ln, char **bufp,
472 size_t *szp, int pos, int *offs)
479 * Run the reserved-word filter only if we have some reserved
483 if (r->first_string && ! roff_res(r, bufp, szp, pos))
484 return(ROFF_REPARSE);
487 ctl = mandoc_getcontrol(*bufp, &pos);
490 * First, if a scope is open and we're not a macro, pass the
491 * text through the macro's filter. If a scope isn't open and
492 * we're not a macro, just let it through.
493 * Finally, if there's an equation scope open, divert it into it
494 * no matter our state.
497 if (r->last && ! ctl) {
499 assert(roffs[t].text);
501 (r, t, bufp, szp, ln, pos, pos, offs);
502 assert(ROFF_IGN == e || ROFF_CONT == e);
506 return(eqn_read(&r->eqn, ln, *bufp, pos));
508 return(tbl_read(r->tbl, ln, *bufp, pos));
512 return(eqn_read(&r->eqn, ln, *bufp, pos));
514 return(tbl_read(r->tbl, ln, *bufp, pos));
517 return(eqn_read(&r->eqn, ln, *bufp, ppos));
520 * If a scope is open, go to the child handler for that macro,
521 * as it may want to preprocess before doing anything with it.
522 * Don't do so if an equation is open.
527 assert(roffs[t].sub);
528 return((*roffs[t].sub)
530 ln, ppos, pos, offs));
534 * Lastly, as we've no scope open, try to look up and execute
535 * the new macro. If no macro is found, simply return and let
536 * the compilers handle it.
539 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
542 assert(roffs[t].proc);
543 return((*roffs[t].proc)
545 ln, ppos, pos, offs));
550 roff_endparse(struct roff *r)
554 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
555 r->last->line, r->last->col, NULL);
558 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
559 r->eqn->eqn.line, r->eqn->eqn.pos, NULL);
565 mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
566 r->tbl->line, r->tbl->pos, NULL);
573 * Parse a roff node's type from the input buffer. This must be in the
574 * form of ".foo xxx" in the usual way.
577 roff_parse(struct roff *r, const char *buf, int *pos)
583 if ('\0' == buf[*pos] || '"' == buf[*pos])
587 maclen = strcspn(mac, " \\\t\0");
589 t = (r->current_string = roff_getstrn(r, mac, maclen))
590 ? ROFF_USERDEF : roff_hash_find(mac, maclen);
594 while (buf[*pos] && ' ' == buf[*pos])
602 roff_parse_nat(const char *buf, unsigned int *res)
608 lval = strtol(buf, &ep, 10);
609 if (buf[0] == '\0' || *ep != '\0')
611 if ((errno == ERANGE &&
612 (lval == LONG_MAX || lval == LONG_MIN)) ||
613 (lval > INT_MAX || lval < 0))
616 *res = (unsigned int)lval;
623 roff_cblock(ROFF_ARGS)
627 * A block-close `..' should only be invoked as a child of an
628 * ignore macro, otherwise raise a warning and just ignore it.
631 if (NULL == r->last) {
632 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
636 switch (r->last->tok) {
644 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
651 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
656 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
659 roffnode_cleanscope(r);
666 roffnode_cleanscope(struct roff *r)
670 if (--r->last->endspan < 0)
679 roff_ccond(ROFF_ARGS)
682 if (NULL == r->last) {
683 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
687 switch (r->last->tok) {
695 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
699 if (r->last->endspan > -1) {
700 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
705 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
708 roffnode_cleanscope(r);
715 roff_block(ROFF_ARGS)
723 if (ROFF_ig != tok) {
724 if ('\0' == (*bufp)[pos]) {
725 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
730 * Re-write `de1', since we don't really care about
731 * groff's strange compatibility mode, into `de'.
739 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
742 while ((*bufp)[pos] && ' ' != (*bufp)[pos])
745 while (' ' == (*bufp)[pos])
746 (*bufp)[pos++] = '\0';
749 roffnode_push(r, tok, name, ln, ppos);
752 * At the beginning of a `de' macro, clear the existing string
753 * with the same name, if there is one. New content will be
754 * added from roff_block_text() in multiline mode.
758 roff_setstr(r, name, "", 0);
760 if ('\0' == (*bufp)[pos])
763 /* If present, process the custom end-of-line marker. */
766 while ((*bufp)[pos] &&
767 ' ' != (*bufp)[pos] &&
768 '\t' != (*bufp)[pos])
772 * Note: groff does NOT like escape characters in the input.
773 * Instead of detecting this, we're just going to let it fly and
778 sz = (size_t)(pos - sv);
780 if (1 == sz && '.' == (*bufp)[sv])
783 r->last->end = mandoc_malloc(sz + 1);
785 memcpy(r->last->end, *bufp + sv, sz);
786 r->last->end[(int)sz] = '\0';
789 mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
797 roff_block_sub(ROFF_ARGS)
803 * First check whether a custom macro exists at this level. If
804 * it does, then check against it. This is some of groff's
805 * stranger behaviours. If we encountered a custom end-scope
806 * tag and that tag also happens to be a "real" macro, then we
807 * need to try interpreting it again as a real macro. If it's
808 * not, then return ignore. Else continue.
812 for (i = pos, j = 0; r->last->end[j]; j++, i++)
813 if ((*bufp)[i] != r->last->end[j])
816 if ('\0' == r->last->end[j] &&
817 ('\0' == (*bufp)[i] ||
819 '\t' == (*bufp)[i])) {
821 roffnode_cleanscope(r);
823 while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
827 if (ROFF_MAX != roff_parse(r, *bufp, &pos))
834 * If we have no custom end-query or lookup failed, then try
835 * pulling it out of the hashtable.
838 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
842 * Macros other than block-end are only significant
843 * in `de' blocks; elsewhere, simply throw them away.
845 if (ROFF_cblock != t) {
847 roff_setstr(r, r->last->name, *bufp + ppos, 1);
851 assert(roffs[t].proc);
852 return((*roffs[t].proc)(r, t, bufp, szp,
853 ln, ppos, pos, offs));
859 roff_block_text(ROFF_ARGS)
863 roff_setstr(r, r->last->name, *bufp + pos, 1);
871 roff_cond_sub(ROFF_ARGS)
879 * Clean out scope. If we've closed ourselves, then don't
883 roffnode_cleanscope(r);
885 if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
886 if ('\\' == (*bufp)[pos] && '}' == (*bufp)[pos + 1])
888 (r, ROFF_ccond, bufp, szp,
889 ln, pos, pos + 2, offs));
890 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
894 * A denied conditional must evaluate its children if and only
895 * if they're either structurally required (such as loops and
896 * conditionals) or a closing macro.
898 if (ROFFRULE_DENY == rr)
899 if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
903 assert(roffs[t].proc);
904 return((*roffs[t].proc)(r, t, bufp, szp,
905 ln, ppos, pos, offs));
911 roff_cond_text(ROFF_ARGS)
919 * We display the value of the text if out current evaluation
920 * scope permits us to do so.
923 /* FIXME: use roff_ccond? */
926 if (NULL == (ep = strstr(st, "\\}"))) {
927 roffnode_cleanscope(r);
928 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
931 if (ep == st || (ep > st && '\\' != *(ep - 1)))
934 roffnode_cleanscope(r);
935 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
940 roff_evalcond(const char *v, int *pos)
946 return(ROFFRULE_ALLOW);
953 return(ROFFRULE_DENY);
958 while (v[*pos] && ' ' != v[*pos])
960 return(ROFFRULE_DENY);
965 roff_line_ignore(ROFF_ARGS)
969 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
981 /* Stack overflow! */
983 if (ROFF_ie == tok && r->rstackpos == RSTACK_MAX - 1) {
984 mandoc_msg(MANDOCERR_MEM, r->parse, ln, ppos, NULL);
988 /* First, evaluate the conditional. */
990 if (ROFF_el == tok) {
992 * An `.el' will get the value of the current rstack
993 * entry set in prior `ie' calls or defaults to DENY.
995 if (r->rstackpos < 0)
996 rule = ROFFRULE_DENY;
998 rule = r->rstack[r->rstackpos];
1000 rule = roff_evalcond(*bufp, &pos);
1004 while (' ' == (*bufp)[pos])
1008 * Roff is weird. If we have just white-space after the
1009 * conditional, it's considered the BODY and we exit without
1010 * really doing anything. Warn about this. It's probably
1014 if ('\0' == (*bufp)[pos] && sv != pos) {
1015 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1019 roffnode_push(r, tok, NULL, ln, ppos);
1021 r->last->rule = rule;
1023 if (ROFF_ie == tok) {
1025 * An if-else will put the NEGATION of the current
1026 * evaluated conditional into the stack.
1029 if (ROFFRULE_DENY == r->last->rule)
1030 r->rstack[r->rstackpos] = ROFFRULE_ALLOW;
1032 r->rstack[r->rstackpos] = ROFFRULE_DENY;
1035 /* If the parent has false as its rule, then so do we. */
1037 if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1038 r->last->rule = ROFFRULE_DENY;
1041 * Determine scope. If we're invoked with "\{" trailing the
1042 * conditional, then we're in a multiline scope. Else our scope
1043 * expires on the next line.
1046 r->last->endspan = 1;
1048 if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
1049 r->last->endspan = -1;
1054 * If there are no arguments on the line, the next-line scope is
1058 if ('\0' == (*bufp)[pos])
1061 /* Otherwise re-run the roff parser after recalculating. */
1072 char *name, *string;
1075 * A symbol is named by the first word following the macro
1076 * invocation up to a space. Its value is anything after the
1077 * name's trailing whitespace and optional double-quote. Thus,
1081 * will have `bar " ' as its value.
1084 string = *bufp + pos;
1085 name = roff_getname(r, &string, ln, pos);
1089 /* Read past initial double-quote. */
1093 /* The rest is the value. */
1094 roff_setstr(r, name, string, 0);
1108 key = roff_getname(r, &val, ln, pos);
1111 if (0 == strcmp(key, "nS")) {
1112 rg[(int)REG_nS].set = 1;
1113 if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u))
1114 rg[(int)REG_nS].v.u = 0;
1128 while ('\0' != *cp) {
1129 name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1131 roff_setstr(r, name, NULL, 0);
1142 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1156 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1158 tbl_restart(ppos, ln, r->tbl);
1169 assert(NULL == r->eqn);
1170 e = eqn_alloc(ppos, ln);
1173 r->last_eqn->next = e;
1175 r->first_eqn = r->last_eqn = e;
1177 r->eqn = r->last_eqn = e;
1186 mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1197 mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1201 t = tbl_alloc(ppos, ln, r->parse);
1204 r->last_tbl->next = t;
1206 r->first_tbl = r->last_tbl = t;
1208 r->tbl = r->last_tbl = t;
1218 mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1221 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1222 * opening anything that's not in our cwd or anything beneath
1223 * it. Thus, explicitly disallow traversing up the file-system
1224 * or using absolute paths.
1228 if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1229 mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1239 roff_userdef(ROFF_ARGS)
1246 * Collect pointers to macro argument strings
1247 * and null-terminate them.
1250 for (i = 0; i < 9; i++)
1251 arg[i] = '\0' == *cp ? "" :
1252 mandoc_getarg(r->parse, &cp, ln, &pos);
1255 * Expand macro arguments.
1258 n1 = cp = mandoc_strdup(r->current_string);
1259 while (NULL != (cp = strstr(cp, "\\$"))) {
1261 if (0 > i || 8 < i) {
1262 /* Not an argument invocation. */
1267 *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
1268 n2 = mandoc_malloc(*szp);
1270 strlcpy(n2, n1, (size_t)(cp - n1 + 1));
1271 strlcat(n2, arg[i], *szp);
1272 strlcat(n2, cp + 3, *szp);
1274 cp = n2 + (cp - n1);
1280 * Replace the macro invocation
1281 * by the expanded macro.
1286 *szp = strlen(*bufp) + 1;
1288 return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
1289 ROFF_REPARSE : ROFF_APPEND);
1293 roff_getname(struct roff *r, char **cpp, int ln, int pos)
1301 /* Read until end of name. */
1302 for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
1308 mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1313 /* Nil-terminate name. */
1317 /* Read past spaces. */
1326 * Store *string into the user-defined string called *name.
1327 * In multiline mode, append to an existing entry and append '\n';
1328 * else replace the existing entry, if there is one.
1329 * To clear an existing entry, call with (*r, *name, NULL, 0).
1332 roff_setstr(struct roff *r, const char *name, const char *string,
1337 size_t oldch, newch;
1339 /* Search for an existing string with the same name. */
1340 n = r->first_string;
1341 while (n && strcmp(name, n->name))
1345 /* Create a new string table entry. */
1346 n = mandoc_malloc(sizeof(struct roffstr));
1347 n->name = mandoc_strdup(name);
1349 n->next = r->first_string;
1350 r->first_string = n;
1351 } else if (0 == multiline) {
1352 /* In multiline mode, append; else replace. */
1361 * One additional byte for the '\n' in multiline mode,
1362 * and one for the terminating '\0'.
1364 newch = strlen(string) + (multiline ? 2u : 1u);
1365 if (NULL == n->string) {
1366 n->string = mandoc_malloc(newch);
1370 oldch = strlen(n->string);
1371 n->string = mandoc_realloc(n->string, oldch + newch);
1374 /* Skip existing content in the destination buffer. */
1375 c = n->string + (int)oldch;
1377 /* Append new content to the destination buffer. */
1380 * Rudimentary roff copy mode:
1381 * Handle escaped backslashes.
1383 if ('\\' == *string && '\\' == *(string + 1))
1388 /* Append terminating bytes. */
1395 roff_getstrn(const struct roff *r, const char *name, size_t len)
1397 const struct roffstr *n;
1399 n = r->first_string;
1400 while (n && (strncmp(name, n->name, len) || '\0' != n->name[(int)len]))
1403 return(n ? n->string : NULL);
1407 roff_freestr(struct roff *r)
1409 struct roffstr *n, *nn;
1411 for (n = r->first_string; n; n = nn) {
1418 r->first_string = NULL;
1421 const struct tbl_span *
1422 roff_span(const struct roff *r)
1425 return(r->tbl ? tbl_span(r->tbl) : NULL);
1429 roff_eqn(const struct roff *r)
1432 return(r->last_eqn ? &r->last_eqn->eqn : NULL);