]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.133 2011/04/13 10:01:27 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
] && ! isspace((unsigned char)(*bufp
)[pos
]))
745 while (isspace((unsigned char)(*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
] && ! isspace((unsigned char)(*bufp
)[pos
]))
770 * Note: groff does NOT like escape characters in the input.
771 * Instead of detecting this, we're just going to let it fly and
776 sz
= (size_t)(pos
- sv
);
778 if (1 == sz
&& '.' == (*bufp
)[sv
])
781 r
->last
->end
= mandoc_malloc(sz
+ 1);
783 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
784 r
->last
->end
[(int)sz
] = '\0';
787 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
795 roff_block_sub(ROFF_ARGS
)
801 * First check whether a custom macro exists at this level. If
802 * it does, then check against it. This is some of groff's
803 * stranger behaviours. If we encountered a custom end-scope
804 * tag and that tag also happens to be a "real" macro, then we
805 * need to try interpreting it again as a real macro. If it's
806 * not, then return ignore. Else continue.
810 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
811 if ((*bufp
)[i
] != r
->last
->end
[j
])
814 if ('\0' == r
->last
->end
[j
] &&
815 ('\0' == (*bufp
)[i
] ||
817 '\t' == (*bufp
)[i
])) {
819 roffnode_cleanscope(r
);
821 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
825 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
832 * If we have no custom end-query or lookup failed, then try
833 * pulling it out of the hashtable.
836 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
840 * Macros other than block-end are only significant
841 * in `de' blocks; elsewhere, simply throw them away.
843 if (ROFF_cblock
!= t
) {
845 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
849 assert(roffs
[t
].proc
);
850 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
851 ln
, ppos
, pos
, offs
));
857 roff_block_text(ROFF_ARGS
)
861 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
869 roff_cond_sub(ROFF_ARGS
)
877 * Clean out scope. If we've closed ourselves, then don't
881 roffnode_cleanscope(r
);
883 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
884 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
886 (r
, ROFF_ccond
, bufp
, szp
,
887 ln
, pos
, pos
+ 2, offs
));
888 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
892 * A denied conditional must evaluate its children if and only
893 * if they're either structurally required (such as loops and
894 * conditionals) or a closing macro.
896 if (ROFFRULE_DENY
== rr
)
897 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
901 assert(roffs
[t
].proc
);
902 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
903 ln
, ppos
, pos
, offs
));
909 roff_cond_text(ROFF_ARGS
)
917 * We display the value of the text if out current evaluation
918 * scope permits us to do so.
921 /* FIXME: use roff_ccond? */
924 if (NULL
== (ep
= strstr(st
, "\\}"))) {
925 roffnode_cleanscope(r
);
926 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
929 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
932 roffnode_cleanscope(r
);
933 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
938 roff_evalcond(const char *v
, int *pos
)
944 return(ROFFRULE_ALLOW
);
951 return(ROFFRULE_DENY
);
956 while (v
[*pos
] && ' ' != v
[*pos
])
958 return(ROFFRULE_DENY
);
963 roff_line_ignore(ROFF_ARGS
)
967 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
979 /* Stack overflow! */
981 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
982 mandoc_msg(MANDOCERR_MEM
, r
->parse
, ln
, ppos
, NULL
);
986 /* First, evaluate the conditional. */
988 if (ROFF_el
== tok
) {
990 * An `.el' will get the value of the current rstack
991 * entry set in prior `ie' calls or defaults to DENY.
993 if (r
->rstackpos
< 0)
994 rule
= ROFFRULE_DENY
;
996 rule
= r
->rstack
[r
->rstackpos
];
998 rule
= roff_evalcond(*bufp
, &pos
);
1002 while (' ' == (*bufp
)[pos
])
1006 * Roff is weird. If we have just white-space after the
1007 * conditional, it's considered the BODY and we exit without
1008 * really doing anything. Warn about this. It's probably
1012 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1013 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1017 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1019 r
->last
->rule
= rule
;
1021 if (ROFF_ie
== tok
) {
1023 * An if-else will put the NEGATION of the current
1024 * evaluated conditional into the stack.
1027 if (ROFFRULE_DENY
== r
->last
->rule
)
1028 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1030 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1033 /* If the parent has false as its rule, then so do we. */
1035 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1036 r
->last
->rule
= ROFFRULE_DENY
;
1039 * Determine scope. If we're invoked with "\{" trailing the
1040 * conditional, then we're in a multiline scope. Else our scope
1041 * expires on the next line.
1044 r
->last
->endspan
= 1;
1046 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1047 r
->last
->endspan
= -1;
1052 * If there are no arguments on the line, the next-line scope is
1056 if ('\0' == (*bufp
)[pos
])
1059 /* Otherwise re-run the roff parser after recalculating. */
1070 char *name
, *string
;
1073 * A symbol is named by the first word following the macro
1074 * invocation up to a space. Its value is anything after the
1075 * name's trailing whitespace and optional double-quote. Thus,
1079 * will have `bar " ' as its value.
1082 string
= *bufp
+ pos
;
1083 name
= roff_getname(r
, &string
, ln
, pos
);
1087 /* Read past initial double-quote. */
1091 /* The rest is the value. */
1092 roff_setstr(r
, name
, string
, 0);
1106 key
= roff_getname(r
, &val
, ln
, pos
);
1109 if (0 == strcmp(key
, "nS")) {
1110 rg
[(int)REG_nS
].set
= 1;
1111 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1112 rg
[(int)REG_nS
].v
.u
= 0;
1126 while ('\0' != *cp
) {
1127 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1129 roff_setstr(r
, name
, NULL
, 0);
1140 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1154 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1156 tbl_restart(ppos
, ln
, r
->tbl
);
1167 assert(NULL
== r
->eqn
);
1168 e
= eqn_alloc(ppos
, ln
);
1171 r
->last_eqn
->next
= e
;
1173 r
->first_eqn
= r
->last_eqn
= e
;
1175 r
->eqn
= r
->last_eqn
= e
;
1184 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1195 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1199 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1202 r
->last_tbl
->next
= t
;
1204 r
->first_tbl
= r
->last_tbl
= t
;
1206 r
->tbl
= r
->last_tbl
= t
;
1216 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1219 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1220 * opening anything that's not in our cwd or anything beneath
1221 * it. Thus, explicitly disallow traversing up the file-system
1222 * or using absolute paths.
1226 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1227 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1237 roff_userdef(ROFF_ARGS
)
1244 * Collect pointers to macro argument strings
1245 * and null-terminate them.
1248 for (i
= 0; i
< 9; i
++)
1249 arg
[i
] = '\0' == *cp
? "" :
1250 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1253 * Expand macro arguments.
1256 n1
= cp
= mandoc_strdup(r
->current_string
);
1257 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1259 if (0 > i
|| 8 < i
) {
1260 /* Not an argument invocation. */
1265 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1266 n2
= mandoc_malloc(*szp
);
1268 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1269 strlcat(n2
, arg
[i
], *szp
);
1270 strlcat(n2
, cp
+ 3, *szp
);
1272 cp
= n2
+ (cp
- n1
);
1278 * Replace the macro invocation
1279 * by the expanded macro.
1284 *szp
= strlen(*bufp
) + 1;
1286 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1287 ROFF_REPARSE
: ROFF_APPEND
);
1291 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1299 /* Read until end of name. */
1300 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1306 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1311 /* Nil-terminate name. */
1315 /* Read past spaces. */
1324 * Store *string into the user-defined string called *name.
1325 * In multiline mode, append to an existing entry and append '\n';
1326 * else replace the existing entry, if there is one.
1327 * To clear an existing entry, call with (*r, *name, NULL, 0).
1330 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1335 size_t oldch
, newch
;
1337 /* Search for an existing string with the same name. */
1338 n
= r
->first_string
;
1339 while (n
&& strcmp(name
, n
->name
))
1343 /* Create a new string table entry. */
1344 n
= mandoc_malloc(sizeof(struct roffstr
));
1345 n
->name
= mandoc_strdup(name
);
1347 n
->next
= r
->first_string
;
1348 r
->first_string
= n
;
1349 } else if (0 == multiline
) {
1350 /* In multiline mode, append; else replace. */
1359 * One additional byte for the '\n' in multiline mode,
1360 * and one for the terminating '\0'.
1362 newch
= strlen(string
) + (multiline
? 2u : 1u);
1363 if (NULL
== n
->string
) {
1364 n
->string
= mandoc_malloc(newch
);
1368 oldch
= strlen(n
->string
);
1369 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1372 /* Skip existing content in the destination buffer. */
1373 c
= n
->string
+ (int)oldch
;
1375 /* Append new content to the destination buffer. */
1378 * Rudimentary roff copy mode:
1379 * Handle escaped backslashes.
1381 if ('\\' == *string
&& '\\' == *(string
+ 1))
1386 /* Append terminating bytes. */
1393 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1395 const struct roffstr
*n
;
1397 n
= r
->first_string
;
1398 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1401 return(n
? n
->string
: NULL
);
1405 roff_freestr(struct roff
*r
)
1407 struct roffstr
*n
, *nn
;
1409 for (n
= r
->first_string
; n
; n
= nn
) {
1416 r
->first_string
= NULL
;
1419 const struct tbl_span
*
1420 roff_span(const struct roff
*r
)
1423 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1427 roff_eqn(const struct roff
*r
)
1430 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);