]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.161 2011/07/27 14:58:28 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.
29 #include "libmandoc.h"
31 /* Maximum number of nested if-else conditionals. */
32 #define RSTACK_MAX 128
75 * A single register entity. If "set" is zero, the value of the
76 * register should be the default one, which is per-register.
77 * Registers are assumed to be unsigned ints for now.
80 int set
; /* whether set or not */
81 unsigned int u
; /* unsigned integer */
85 char *key
; /* key of symbol */
86 char *val
; /* current value */
87 struct roffstr
*next
; /* next in list */
91 struct mparse
*parse
; /* parse point */
92 struct roffnode
*last
; /* leaf of stack */
93 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
94 int rstackpos
; /* position in rstack */
95 struct reg regs
[REG__MAX
];
96 struct roffstr
*first_string
; /* user-defined strings & macros */
97 const char *current_string
; /* value of last called user macro */
98 struct tbl_node
*first_tbl
; /* first table parsed */
99 struct tbl_node
*last_tbl
; /* last table parsed */
100 struct tbl_node
*tbl
; /* current table being parsed */
101 struct eqn_node
*last_eqn
; /* last equation parsed */
102 struct eqn_node
*first_eqn
; /* first equation parsed */
103 struct eqn_node
*eqn
; /* current equation being parsed */
107 enum rofft tok
; /* type of node */
108 struct roffnode
*parent
; /* up one in stack */
109 int line
; /* parse line */
110 int col
; /* parse col */
111 char *name
; /* node name, e.g. macro name */
112 char *end
; /* end-rules: custom token */
113 int endspan
; /* end-rules: next-line or infty */
114 enum roffrule rule
; /* current evaluation rule */
117 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
118 enum rofft tok, /* tok of macro */ \
119 char **bufp, /* input buffer */ \
120 size_t *szp, /* size of input buffer */ \
121 int ln, /* parse line */ \
122 int ppos, /* original pos in buffer */ \
123 int pos, /* current pos in buffer */ \
124 int *offs /* reset offset of buffer data */
126 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
129 const char *name
; /* macro name */
130 roffproc proc
; /* process new macro */
131 roffproc text
; /* process as child text of macro */
132 roffproc sub
; /* process as child of macro */
134 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
135 struct roffmac
*next
;
139 const char *name
; /* predefined input name */
140 const char *str
; /* replacement symbol */
143 #define PREDEF(__name, __str) \
144 { (__name), (__str) },
146 static enum rofft
roffhash_find(const char *, size_t);
147 static void roffhash_init(void);
148 static void roffnode_cleanscope(struct roff
*);
149 static void roffnode_pop(struct roff
*);
150 static void roffnode_push(struct roff
*, enum rofft
,
151 const char *, int, int);
152 static enum rofferr
roff_block(ROFF_ARGS
);
153 static enum rofferr
roff_block_text(ROFF_ARGS
);
154 static enum rofferr
roff_block_sub(ROFF_ARGS
);
155 static enum rofferr
roff_cblock(ROFF_ARGS
);
156 static enum rofferr
roff_ccond(ROFF_ARGS
);
157 static enum rofferr
roff_cond(ROFF_ARGS
);
158 static enum rofferr
roff_cond_text(ROFF_ARGS
);
159 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
160 static enum rofferr
roff_ds(ROFF_ARGS
);
161 static enum roffrule
roff_evalcond(const char *, int *);
162 static void roff_free1(struct roff
*);
163 static void roff_freestr(struct roff
*);
164 static char *roff_getname(struct roff
*, char **, int, int);
165 static const char *roff_getstrn(const struct roff
*,
166 const char *, size_t);
167 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
168 static enum rofferr
roff_nr(ROFF_ARGS
);
169 static void roff_openeqn(struct roff
*, const char *,
170 int, int, const char *);
171 static enum rofft
roff_parse(struct roff
*, const char *, int *);
172 static enum rofferr
roff_parsetext(char *);
173 static void roff_res(struct roff
*,
174 char **, size_t *, int, int);
175 static enum rofferr
roff_rm(ROFF_ARGS
);
176 static void roff_setstr(struct roff
*,
177 const char *, const char *, int);
178 static enum rofferr
roff_so(ROFF_ARGS
);
179 static enum rofferr
roff_TE(ROFF_ARGS
);
180 static enum rofferr
roff_TS(ROFF_ARGS
);
181 static enum rofferr
roff_EQ(ROFF_ARGS
);
182 static enum rofferr
roff_EN(ROFF_ARGS
);
183 static enum rofferr
roff_T_(ROFF_ARGS
);
184 static enum rofferr
roff_userdef(ROFF_ARGS
);
186 /* See roffhash_find() */
190 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
192 static struct roffmac
*hash
[HASHWIDTH
];
194 static struct roffmac roffs
[ROFF_MAX
] = {
195 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
196 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
197 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
198 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
199 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
200 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
201 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
202 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
203 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
204 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
205 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
206 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
207 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
208 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
209 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
210 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
211 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
212 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
213 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
214 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
215 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
216 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
217 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
218 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
219 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
220 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
221 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
222 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
223 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
224 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
225 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
228 /* Array of injected predefined strings. */
229 #define PREDEFS_MAX 38
230 static const struct predef predefs
[PREDEFS_MAX
] = {
231 #include "predefs.in"
234 /* See roffhash_find() */
235 #define ROFF_HASH(p) (p[0] - ASCII_LO)
243 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
244 assert(roffs
[i
].name
[0] >= ASCII_LO
);
245 assert(roffs
[i
].name
[0] <= ASCII_HI
);
247 buc
= ROFF_HASH(roffs
[i
].name
);
249 if (NULL
!= (n
= hash
[buc
])) {
250 for ( ; n
->next
; n
= n
->next
)
254 hash
[buc
] = &roffs
[i
];
259 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
260 * the nil-terminated string name could be found.
263 roffhash_find(const char *p
, size_t s
)
269 * libroff has an extremely simple hashtable, for the time
270 * being, which simply keys on the first character, which must
271 * be printable, then walks a chain. It works well enough until
275 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
280 if (NULL
== (n
= hash
[buc
]))
282 for ( ; n
; n
= n
->next
)
283 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
284 return((enum rofft
)(n
- roffs
));
291 * Pop the current node off of the stack of roff instructions currently
295 roffnode_pop(struct roff
*r
)
302 r
->last
= r
->last
->parent
;
310 * Push a roff node onto the instruction stack. This must later be
311 * removed with roffnode_pop().
314 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
319 p
= mandoc_calloc(1, sizeof(struct roffnode
));
322 p
->name
= mandoc_strdup(name
);
326 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
333 roff_free1(struct roff
*r
)
338 while (NULL
!= (t
= r
->first_tbl
)) {
339 r
->first_tbl
= t
->next
;
343 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
345 while (NULL
!= (e
= r
->first_eqn
)) {
346 r
->first_eqn
= e
->next
;
350 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
360 roff_reset(struct roff
*r
)
366 memset(&r
->regs
, 0, sizeof(struct reg
) * REG__MAX
);
368 for (i
= 0; i
< PREDEFS_MAX
; i
++)
369 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
374 roff_free(struct roff
*r
)
383 roff_alloc(struct mparse
*parse
)
388 r
= mandoc_calloc(1, sizeof(struct roff
));
394 for (i
= 0; i
< PREDEFS_MAX
; i
++)
395 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
401 * Pre-filter each and every line for reserved words (one beginning with
402 * `\*', e.g., `\*(ab'). These must be handled before the actual line
404 * This also checks the syntax of regular escapes.
407 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
410 const char *stesc
; /* start of an escape sequence ('\\') */
411 const char *stnam
; /* start of the name, after "[(*" */
412 const char *cp
; /* end of the name, e.g. before ']' */
413 const char *res
; /* the string to be substituted */
420 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
424 * The second character must be an asterisk.
425 * If it isn't, skip it anyway: It is escaped,
426 * so it can't start another escape sequence.
434 esc
= mandoc_escape(&cp
, NULL
, NULL
);
435 if (ESCAPE_ERROR
!= esc
)
439 (MANDOCERR_BADESCAPE
, r
->parse
,
440 ln
, (int)(stesc
- *bufp
), NULL
);
447 * The third character decides the length
448 * of the name of the string.
449 * Save a pointer to the name.
469 /* Advance to the end of the name. */
471 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
474 (MANDOCERR_BADESCAPE
,
476 (int)(stesc
- *bufp
), NULL
);
479 if (0 == maxl
&& ']' == *cp
)
484 * Retrieve the replacement string; if it is
485 * undefined, resume searching for escapes.
488 res
= roff_getstrn(r
, stnam
, (size_t)i
);
492 (MANDOCERR_BADESCAPE
, r
->parse
,
493 ln
, (int)(stesc
- *bufp
), NULL
);
497 /* Replace the escape sequence by the string. */
501 nsz
= *szp
+ strlen(res
) + 1;
502 n
= mandoc_malloc(nsz
);
504 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
505 strlcat(n
, res
, nsz
);
506 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
517 * Process text streams: convert all breakable hyphens into ASCII_HYPH.
520 roff_parsetext(char *p
)
530 sz
= strcspn(p
, "-\\");
537 /* Skip over escapes. */
540 ((const char **)&p
, NULL
, NULL
);
541 if (ESCAPE_ERROR
== esc
)
544 } else if (p
== start
) {
552 '\t' != r
&& '\t' != l
&&
553 ' ' != r
&& ' ' != l
&&
554 '-' != r
&& '-' != l
&&
555 ! isdigit((unsigned char)l
) &&
556 ! isdigit((unsigned char)r
))
565 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
566 size_t *szp
, int pos
, int *offs
)
573 * Run the reserved-word filter only if we have some reserved
577 roff_res(r
, bufp
, szp
, ln
, pos
);
580 ctl
= mandoc_getcontrol(*bufp
, &pos
);
583 * First, if a scope is open and we're not a macro, pass the
584 * text through the macro's filter. If a scope isn't open and
585 * we're not a macro, just let it through.
586 * Finally, if there's an equation scope open, divert it into it
587 * no matter our state.
590 if (r
->last
&& ! ctl
) {
592 assert(roffs
[t
].text
);
594 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
595 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
599 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
601 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
602 return(roff_parsetext(*bufp
+ pos
));
605 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
, offs
));
607 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
608 return(roff_parsetext(*bufp
+ pos
));
610 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
613 * If a scope is open, go to the child handler for that macro,
614 * as it may want to preprocess before doing anything with it.
615 * Don't do so if an equation is open.
620 assert(roffs
[t
].sub
);
621 return((*roffs
[t
].sub
)
623 ln
, ppos
, pos
, offs
));
627 * Lastly, as we've no scope open, try to look up and execute
628 * the new macro. If no macro is found, simply return and let
629 * the compilers handle it.
632 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
635 assert(roffs
[t
].proc
);
636 return((*roffs
[t
].proc
)
638 ln
, ppos
, pos
, offs
));
643 roff_endparse(struct roff
*r
)
647 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
648 r
->last
->line
, r
->last
->col
, NULL
);
651 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
652 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
657 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
658 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
664 * Parse a roff node's type from the input buffer. This must be in the
665 * form of ".foo xxx" in the usual way.
668 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
674 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
675 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
679 * We stop the macro parse at an escape, tab, space, or nil.
680 * However, `\}' is also a valid macro, so make sure we don't
681 * clobber it by seeing the `\' as the end of token.
685 maclen
= strcspn(mac
+ 1, " \\\t\0") + 1;
687 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
688 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
692 while (buf
[*pos
] && ' ' == buf
[*pos
])
700 roff_cblock(ROFF_ARGS
)
704 * A block-close `..' should only be invoked as a child of an
705 * ignore macro, otherwise raise a warning and just ignore it.
708 if (NULL
== r
->last
) {
709 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
713 switch (r
->last
->tok
) {
721 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
728 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
733 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
736 roffnode_cleanscope(r
);
743 roffnode_cleanscope(struct roff
*r
)
747 if (--r
->last
->endspan
< 0)
756 roff_ccond(ROFF_ARGS
)
759 if (NULL
== r
->last
) {
760 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
764 switch (r
->last
->tok
) {
772 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
776 if (r
->last
->endspan
> -1) {
777 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
782 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
785 roffnode_cleanscope(r
);
792 roff_block(ROFF_ARGS
)
800 if (ROFF_ig
!= tok
) {
801 if ('\0' == (*bufp
)[pos
]) {
802 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
807 * Re-write `de1', since we don't really care about
808 * groff's strange compatibility mode, into `de'.
816 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
819 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
822 while (isspace((unsigned char)(*bufp
)[pos
]))
823 (*bufp
)[pos
++] = '\0';
826 roffnode_push(r
, tok
, name
, ln
, ppos
);
829 * At the beginning of a `de' macro, clear the existing string
830 * with the same name, if there is one. New content will be
831 * added from roff_block_text() in multiline mode.
835 roff_setstr(r
, name
, "", 0);
837 if ('\0' == (*bufp
)[pos
])
840 /* If present, process the custom end-of-line marker. */
843 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
847 * Note: groff does NOT like escape characters in the input.
848 * Instead of detecting this, we're just going to let it fly and
853 sz
= (size_t)(pos
- sv
);
855 if (1 == sz
&& '.' == (*bufp
)[sv
])
858 r
->last
->end
= mandoc_malloc(sz
+ 1);
860 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
861 r
->last
->end
[(int)sz
] = '\0';
864 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
872 roff_block_sub(ROFF_ARGS
)
878 * First check whether a custom macro exists at this level. If
879 * it does, then check against it. This is some of groff's
880 * stranger behaviours. If we encountered a custom end-scope
881 * tag and that tag also happens to be a "real" macro, then we
882 * need to try interpreting it again as a real macro. If it's
883 * not, then return ignore. Else continue.
887 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
888 if ((*bufp
)[i
] != r
->last
->end
[j
])
891 if ('\0' == r
->last
->end
[j
] &&
892 ('\0' == (*bufp
)[i
] ||
894 '\t' == (*bufp
)[i
])) {
896 roffnode_cleanscope(r
);
898 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
902 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
909 * If we have no custom end-query or lookup failed, then try
910 * pulling it out of the hashtable.
913 t
= roff_parse(r
, *bufp
, &pos
);
916 * Macros other than block-end are only significant
917 * in `de' blocks; elsewhere, simply throw them away.
919 if (ROFF_cblock
!= t
) {
921 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
925 assert(roffs
[t
].proc
);
926 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
927 ln
, ppos
, pos
, offs
));
933 roff_block_text(ROFF_ARGS
)
937 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
945 roff_cond_sub(ROFF_ARGS
)
952 roffnode_cleanscope(r
);
955 * If the macro is unknown, first check if it contains a closing
956 * delimiter `\}'. If it does, close out our scope and return
957 * the currently-scoped rule (ignore or continue). Else, drop
958 * into the currently-scoped rule.
961 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
963 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
969 * Make the \} go away.
970 * This is a little haphazard, as it's not quite
971 * clear how nroff does this.
972 * If we're at the end of line, then just chop
973 * off the \} and resize the buffer.
974 * If we aren't, then conver it to spaces.
977 if ('\0' == *(ep
+ 1)) {
981 *(ep
- 1) = *ep
= ' ';
983 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
984 ln
, pos
, pos
+ 2, offs
);
987 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
991 * A denied conditional must evaluate its children if and only
992 * if they're either structurally required (such as loops and
993 * conditionals) or a closing macro.
996 if (ROFFRULE_DENY
== rr
)
997 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
1001 assert(roffs
[t
].proc
);
1002 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1003 ln
, ppos
, pos
, offs
));
1008 roff_cond_text(ROFF_ARGS
)
1014 roffnode_cleanscope(r
);
1017 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
1022 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
1023 ln
, pos
, pos
+ 2, offs
);
1025 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
1028 static enum roffrule
1029 roff_evalcond(const char *v
, int *pos
)
1035 return(ROFFRULE_ALLOW
);
1042 return(ROFFRULE_DENY
);
1047 while (v
[*pos
] && ' ' != v
[*pos
])
1049 return(ROFFRULE_DENY
);
1054 roff_line_ignore(ROFF_ARGS
)
1058 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
1065 roff_cond(ROFF_ARGS
)
1071 * An `.el' has no conditional body: it will consume the value
1072 * of the current rstack entry set in prior `ie' calls or
1075 * If we're not an `el', however, then evaluate the conditional.
1078 rule
= ROFF_el
== tok
?
1080 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
1081 roff_evalcond(*bufp
, &pos
);
1084 while (' ' == (*bufp
)[pos
])
1088 * Roff is weird. If we have just white-space after the
1089 * conditional, it's considered the BODY and we exit without
1090 * really doing anything. Warn about this. It's probably
1094 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1095 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1099 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1101 r
->last
->rule
= rule
;
1104 * An if-else will put the NEGATION of the current evaluated
1105 * conditional into the stack of rules.
1108 if (ROFF_ie
== tok
) {
1109 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1110 mandoc_msg(MANDOCERR_MEM
,
1111 r
->parse
, ln
, ppos
, NULL
);
1114 r
->rstack
[++r
->rstackpos
] =
1115 ROFFRULE_DENY
== r
->last
->rule
?
1116 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1119 /* If the parent has false as its rule, then so do we. */
1121 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1122 r
->last
->rule
= ROFFRULE_DENY
;
1125 * Determine scope. If we're invoked with "\{" trailing the
1126 * conditional, then we're in a multiline scope. Else our scope
1127 * expires on the next line.
1130 r
->last
->endspan
= 1;
1132 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1133 r
->last
->endspan
= -1;
1138 * If there are no arguments on the line, the next-line scope is
1142 if ('\0' == (*bufp
)[pos
])
1145 /* Otherwise re-run the roff parser after recalculating. */
1156 char *name
, *string
;
1159 * A symbol is named by the first word following the macro
1160 * invocation up to a space. Its value is anything after the
1161 * name's trailing whitespace and optional double-quote. Thus,
1165 * will have `bar " ' as its value.
1168 string
= *bufp
+ pos
;
1169 name
= roff_getname(r
, &string
, ln
, pos
);
1173 /* Read past initial double-quote. */
1177 /* The rest is the value. */
1178 roff_setstr(r
, name
, string
, 0);
1183 roff_regisset(const struct roff
*r
, enum regs reg
)
1186 return(r
->regs
[(int)reg
].set
);
1190 roff_regget(const struct roff
*r
, enum regs reg
)
1193 return(r
->regs
[(int)reg
].u
);
1197 roff_regunset(struct roff
*r
, enum regs reg
)
1200 r
->regs
[(int)reg
].set
= 0;
1212 key
= roff_getname(r
, &val
, ln
, pos
);
1214 if (0 == strcmp(key
, "nS")) {
1215 r
->regs
[(int)REG_nS
].set
= 1;
1216 if ((iv
= mandoc_strntoi(val
, strlen(val
), 10)) >= 0)
1217 r
->regs
[(int)REG_nS
].u
= (unsigned)iv
;
1219 r
->regs
[(int)REG_nS
].u
= 0u;
1233 while ('\0' != *cp
) {
1234 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1236 roff_setstr(r
, name
, NULL
, 0);
1247 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1260 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1262 tbl_restart(ppos
, ln
, r
->tbl
);
1269 roff_closeeqn(struct roff
*r
)
1272 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1277 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1278 int offs
, const char *buf
)
1283 assert(NULL
== r
->eqn
);
1284 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1287 r
->last_eqn
->next
= e
;
1289 r
->first_eqn
= r
->last_eqn
= e
;
1291 r
->eqn
= r
->last_eqn
= e
;
1295 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1304 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1313 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1324 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1328 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1331 r
->last_tbl
->next
= t
;
1333 r
->first_tbl
= r
->last_tbl
= t
;
1335 r
->tbl
= r
->last_tbl
= t
;
1345 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1348 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1349 * opening anything that's not in our cwd or anything beneath
1350 * it. Thus, explicitly disallow traversing up the file-system
1351 * or using absolute paths.
1355 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1356 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1366 roff_userdef(ROFF_ARGS
)
1373 * Collect pointers to macro argument strings
1374 * and null-terminate them.
1377 for (i
= 0; i
< 9; i
++)
1378 arg
[i
] = '\0' == *cp
? "" :
1379 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1382 * Expand macro arguments.
1385 n1
= cp
= mandoc_strdup(r
->current_string
);
1386 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1388 if (0 > i
|| 8 < i
) {
1389 /* Not an argument invocation. */
1394 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1395 n2
= mandoc_malloc(*szp
);
1397 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1398 strlcat(n2
, arg
[i
], *szp
);
1399 strlcat(n2
, cp
+ 3, *szp
);
1401 cp
= n2
+ (cp
- n1
);
1407 * Replace the macro invocation
1408 * by the expanded macro.
1413 *szp
= strlen(*bufp
) + 1;
1415 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1416 ROFF_REPARSE
: ROFF_APPEND
);
1420 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1428 /* Read until end of name. */
1429 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1435 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1440 /* Nil-terminate name. */
1444 /* Read past spaces. */
1453 * Store *string into the user-defined string called *name.
1454 * In multiline mode, append to an existing entry and append '\n';
1455 * else replace the existing entry, if there is one.
1456 * To clear an existing entry, call with (*r, *name, NULL, 0).
1459 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1464 size_t oldch
, newch
;
1466 /* Search for an existing string with the same name. */
1467 n
= r
->first_string
;
1468 while (n
&& strcmp(name
, n
->key
))
1472 /* Create a new string table entry. */
1473 n
= mandoc_malloc(sizeof(struct roffstr
));
1474 n
->key
= mandoc_strdup(name
);
1476 n
->next
= r
->first_string
;
1477 r
->first_string
= n
;
1478 } else if (0 == multiline
) {
1479 /* In multiline mode, append; else replace. */
1488 * One additional byte for the '\n' in multiline mode,
1489 * and one for the terminating '\0'.
1491 newch
= strlen(string
) + (multiline
? 2u : 1u);
1492 if (NULL
== n
->val
) {
1493 n
->val
= mandoc_malloc(newch
);
1497 oldch
= strlen(n
->val
);
1498 n
->val
= mandoc_realloc(n
->val
, oldch
+ newch
);
1501 /* Skip existing content in the destination buffer. */
1502 c
= n
->val
+ (int)oldch
;
1504 /* Append new content to the destination buffer. */
1507 * Rudimentary roff copy mode:
1508 * Handle escaped backslashes.
1510 if ('\\' == *string
&& '\\' == *(string
+ 1))
1515 /* Append terminating bytes. */
1522 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1524 const struct roffstr
*n
;
1526 for (n
= r
->first_string
; n
; n
= n
->next
)
1527 if (0 == strncmp(name
, n
->key
, len
) &&
1528 '\0' == n
->key
[(int)len
])
1535 roff_freestr(struct roff
*r
)
1537 struct roffstr
*n
, *nn
;
1539 for (n
= r
->first_string
; n
; n
= nn
) {
1546 r
->first_string
= NULL
;
1549 const struct tbl_span
*
1550 roff_span(const struct roff
*r
)
1553 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1557 roff_eqn(const struct roff
*r
)
1560 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
1564 roff_eqndelim(const struct roff
*r
)