]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.129 2011/03/22 09:50:11 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
37 ('.' == (c) || '\'' == (c))
69 ROFF_ccond
, /* FIXME: remove this. */
80 char *name
; /* key of symbol */
81 char *string
; /* current value */
82 struct roffstr
*next
; /* next in list */
86 struct mparse
*parse
; /* parse point */
87 struct roffnode
*last
; /* leaf of stack */
88 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
89 int rstackpos
; /* position in rstack */
90 struct regset
*regs
; /* read/writable registers */
91 struct roffstr
*first_string
; /* user-defined strings & macros */
92 const char *current_string
; /* value of last called user macro */
93 struct tbl_node
*first_tbl
; /* first table parsed */
94 struct tbl_node
*last_tbl
; /* last table parsed */
95 struct tbl_node
*tbl
; /* current table being parsed */
96 struct eqn_node
*last_eqn
; /* last equation parsed */
97 struct eqn_node
*first_eqn
; /* first equation parsed */
98 struct eqn_node
*eqn
; /* current equation being parsed */
102 enum rofft tok
; /* type of node */
103 struct roffnode
*parent
; /* up one in stack */
104 int line
; /* parse line */
105 int col
; /* parse col */
106 char *name
; /* node name, e.g. macro name */
107 char *end
; /* end-rules: custom token */
108 int endspan
; /* end-rules: next-line or infty */
109 enum roffrule rule
; /* current evaluation rule */
112 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
113 enum rofft tok, /* tok of macro */ \
114 char **bufp, /* input buffer */ \
115 size_t *szp, /* size of input buffer */ \
116 int ln, /* parse line */ \
117 int ppos, /* original pos in buffer */ \
118 int pos, /* current pos in buffer */ \
119 int *offs /* reset offset of buffer data */
121 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
124 const char *name
; /* macro name */
125 roffproc proc
; /* process new macro */
126 roffproc text
; /* process as child text of macro */
127 roffproc sub
; /* process as child of macro */
129 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
130 struct roffmac
*next
;
133 static enum rofferr
roff_block(ROFF_ARGS
);
134 static enum rofferr
roff_block_text(ROFF_ARGS
);
135 static enum rofferr
roff_block_sub(ROFF_ARGS
);
136 static enum rofferr
roff_cblock(ROFF_ARGS
);
137 static enum rofferr
roff_ccond(ROFF_ARGS
);
138 static enum rofferr
roff_cond(ROFF_ARGS
);
139 static enum rofferr
roff_cond_text(ROFF_ARGS
);
140 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
141 static enum rofferr
roff_ds(ROFF_ARGS
);
142 static enum roffrule
roff_evalcond(const char *, int *);
143 static void roff_freestr(struct roff
*);
144 static char *roff_getname(struct roff
*, char **, int, int);
145 static const char *roff_getstrn(const struct roff
*,
146 const char *, size_t);
147 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
148 static enum rofferr
roff_nr(ROFF_ARGS
);
149 static int roff_res(struct roff
*,
150 char **, size_t *, int);
151 static enum rofferr
roff_rm(ROFF_ARGS
);
152 static void roff_setstr(struct roff
*,
153 const char *, const char *, int);
154 static enum rofferr
roff_so(ROFF_ARGS
);
155 static enum rofferr
roff_TE(ROFF_ARGS
);
156 static enum rofferr
roff_TS(ROFF_ARGS
);
157 static enum rofferr
roff_EQ(ROFF_ARGS
);
158 static enum rofferr
roff_EN(ROFF_ARGS
);
159 static enum rofferr
roff_T_(ROFF_ARGS
);
160 static enum rofferr
roff_userdef(ROFF_ARGS
);
162 /* See roff_hash_find() */
166 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
168 static struct roffmac
*hash
[HASHWIDTH
];
170 static struct roffmac roffs
[ROFF_MAX
] = {
171 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
172 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
173 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
174 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
175 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
176 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
177 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
178 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
179 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
180 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
181 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
182 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
183 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
184 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
185 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
186 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
187 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
188 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
189 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
190 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
191 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
192 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
193 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
194 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
195 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
196 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
197 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
198 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
199 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
200 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
201 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
204 static void roff_free1(struct roff
*);
205 static enum rofft
roff_hash_find(const char *, size_t);
206 static void roff_hash_init(void);
207 static void roffnode_cleanscope(struct roff
*);
208 static void roffnode_push(struct roff
*, enum rofft
,
209 const char *, int, int);
210 static void roffnode_pop(struct roff
*);
211 static enum rofft
roff_parse(struct roff
*, const char *, int *);
212 static int roff_parse_nat(const char *, unsigned int *);
214 /* See roff_hash_find() */
215 #define ROFF_HASH(p) (p[0] - ASCII_LO)
223 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
224 assert(roffs
[i
].name
[0] >= ASCII_LO
);
225 assert(roffs
[i
].name
[0] <= ASCII_HI
);
227 buc
= ROFF_HASH(roffs
[i
].name
);
229 if (NULL
!= (n
= hash
[buc
])) {
230 for ( ; n
->next
; n
= n
->next
)
234 hash
[buc
] = &roffs
[i
];
240 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
241 * the nil-terminated string name could be found.
244 roff_hash_find(const char *p
, size_t s
)
250 * libroff has an extremely simple hashtable, for the time
251 * being, which simply keys on the first character, which must
252 * be printable, then walks a chain. It works well enough until
256 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
261 if (NULL
== (n
= hash
[buc
]))
263 for ( ; n
; n
= n
->next
)
264 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
265 return((enum rofft
)(n
- roffs
));
272 * Pop the current node off of the stack of roff instructions currently
276 roffnode_pop(struct roff
*r
)
283 if (ROFF_el
== p
->tok
)
284 if (r
->rstackpos
> -1)
287 r
->last
= r
->last
->parent
;
295 * Push a roff node onto the instruction stack. This must later be
296 * removed with roffnode_pop().
299 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
304 p
= mandoc_calloc(1, sizeof(struct roffnode
));
307 p
->name
= mandoc_strdup(name
);
311 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
318 roff_free1(struct roff
*r
)
323 while (NULL
!= (t
= r
->first_tbl
)) {
324 r
->first_tbl
= t
->next
;
328 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
330 while (NULL
!= (e
= r
->first_eqn
)) {
331 r
->first_eqn
= e
->next
;
335 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
345 roff_reset(struct roff
*r
)
353 roff_free(struct roff
*r
)
362 roff_alloc(struct regset
*regs
, struct mparse
*parse
)
366 r
= mandoc_calloc(1, sizeof(struct roff
));
377 * Pre-filter each and every line for reserved words (one beginning with
378 * `\*', e.g., `\*(ab'). These must be handled before the actual line
382 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
384 const char *stesc
; /* start of an escape sequence ('\\') */
385 const char *stnam
; /* start of the name, after "[(*" */
386 const char *cp
; /* end of the name, e.g. before ']' */
387 const char *res
; /* the string to be substituted */
392 /* Search for a leading backslash and save a pointer to it. */
395 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
399 * The second character must be an asterisk.
400 * If it isn't, skip it anyway: It is escaped,
401 * so it can't start another escape sequence.
410 * The third character decides the length
411 * of the name of the string.
412 * Save a pointer to the name.
432 /* Advance to the end of the name. */
434 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
436 return(1); /* Error. */
437 if (0 == maxl
&& ']' == *cp
)
442 * Retrieve the replacement string; if it is
443 * undefined, resume searching for escapes.
446 res
= roff_getstrn(r
, stnam
, (size_t)i
);
453 /* Replace the escape sequence by the string. */
455 nsz
= *szp
+ strlen(res
) + 1;
456 n
= mandoc_malloc(nsz
);
458 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
459 strlcat(n
, res
, nsz
);
460 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
474 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
475 size_t *szp
, int pos
, int *offs
)
482 * Run the reserved-word filter only if we have some reserved
486 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
487 return(ROFF_REPARSE
);
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
&& ! ROFF_CTL((*bufp
)[pos
])) {
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
, *offs
));
508 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
510 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
512 return(eqn_read(&r
->eqn
, ln
, *bufp
, *offs
));
514 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
517 return(eqn_read(&r
->eqn
, ln
, *bufp
, *offs
));
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
, pos
, 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.
540 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
543 assert(roffs
[t
].proc
);
544 return((*roffs
[t
].proc
)
546 ln
, ppos
, pos
, offs
));
551 roff_endparse(struct roff
*r
)
555 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
556 r
->last
->line
, r
->last
->col
, NULL
);
559 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
560 r
->eqn
->eqn
.line
, r
->eqn
->eqn
.pos
, NULL
);
566 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
567 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
575 * Parse a roff node's type from the input buffer. This must be in the
576 * form of ".foo xxx" in the usual way.
579 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
585 assert(ROFF_CTL(buf
[*pos
]));
588 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
591 if ('\0' == buf
[*pos
])
595 maclen
= strcspn(mac
, " \\\t\0");
597 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
598 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
601 while (buf
[*pos
] && ' ' == buf
[*pos
])
609 roff_parse_nat(const char *buf
, unsigned int *res
)
615 lval
= strtol(buf
, &ep
, 10);
616 if (buf
[0] == '\0' || *ep
!= '\0')
618 if ((errno
== ERANGE
&&
619 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
620 (lval
> INT_MAX
|| lval
< 0))
623 *res
= (unsigned int)lval
;
630 roff_cblock(ROFF_ARGS
)
634 * A block-close `..' should only be invoked as a child of an
635 * ignore macro, otherwise raise a warning and just ignore it.
638 if (NULL
== r
->last
) {
639 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
643 switch (r
->last
->tok
) {
651 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
658 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
663 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
666 roffnode_cleanscope(r
);
673 roffnode_cleanscope(struct roff
*r
)
677 if (--r
->last
->endspan
< 0)
686 roff_ccond(ROFF_ARGS
)
689 if (NULL
== r
->last
) {
690 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
694 switch (r
->last
->tok
) {
702 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
706 if (r
->last
->endspan
> -1) {
707 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
712 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
715 roffnode_cleanscope(r
);
722 roff_block(ROFF_ARGS
)
730 if (ROFF_ig
!= tok
) {
731 if ('\0' == (*bufp
)[pos
]) {
732 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
737 * Re-write `de1', since we don't really care about
738 * groff's strange compatibility mode, into `de'.
746 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
749 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
752 while (' ' == (*bufp
)[pos
])
753 (*bufp
)[pos
++] = '\0';
756 roffnode_push(r
, tok
, name
, ln
, ppos
);
759 * At the beginning of a `de' macro, clear the existing string
760 * with the same name, if there is one. New content will be
761 * added from roff_block_text() in multiline mode.
765 roff_setstr(r
, name
, "", 0);
767 if ('\0' == (*bufp
)[pos
])
770 /* If present, process the custom end-of-line marker. */
773 while ((*bufp
)[pos
] &&
774 ' ' != (*bufp
)[pos
] &&
775 '\t' != (*bufp
)[pos
])
779 * Note: groff does NOT like escape characters in the input.
780 * Instead of detecting this, we're just going to let it fly and
785 sz
= (size_t)(pos
- sv
);
787 if (1 == sz
&& '.' == (*bufp
)[sv
])
790 r
->last
->end
= mandoc_malloc(sz
+ 1);
792 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
793 r
->last
->end
[(int)sz
] = '\0';
796 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
804 roff_block_sub(ROFF_ARGS
)
810 * First check whether a custom macro exists at this level. If
811 * it does, then check against it. This is some of groff's
812 * stranger behaviours. If we encountered a custom end-scope
813 * tag and that tag also happens to be a "real" macro, then we
814 * need to try interpreting it again as a real macro. If it's
815 * not, then return ignore. Else continue.
820 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
823 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
824 if ((*bufp
)[i
] != r
->last
->end
[j
])
827 if ('\0' == r
->last
->end
[j
] &&
828 ('\0' == (*bufp
)[i
] ||
830 '\t' == (*bufp
)[i
])) {
832 roffnode_cleanscope(r
);
834 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
841 * If we have no custom end-query or lookup failed, then try
842 * pulling it out of the hashtable.
846 t
= roff_parse(r
, *bufp
, &pos
);
849 * Macros other than block-end are only significant
850 * in `de' blocks; elsewhere, simply throw them away.
852 if (ROFF_cblock
!= t
) {
854 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
858 assert(roffs
[t
].proc
);
859 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
860 ln
, ppos
, pos
, offs
));
866 roff_block_text(ROFF_ARGS
)
870 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
878 roff_cond_sub(ROFF_ARGS
)
887 * Clean out scope. If we've closed ourselves, then don't
891 roffnode_cleanscope(r
);
893 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
894 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
896 (r
, ROFF_ccond
, bufp
, szp
,
897 ln
, pos
, pos
+ 2, offs
));
898 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
902 * A denied conditional must evaluate its children if and only
903 * if they're either structurally required (such as loops and
904 * conditionals) or a closing macro.
906 if (ROFFRULE_DENY
== rr
)
907 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
911 assert(roffs
[t
].proc
);
912 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
913 ln
, ppos
, pos
, offs
));
919 roff_cond_text(ROFF_ARGS
)
927 * We display the value of the text if out current evaluation
928 * scope permits us to do so.
931 /* FIXME: use roff_ccond? */
934 if (NULL
== (ep
= strstr(st
, "\\}"))) {
935 roffnode_cleanscope(r
);
936 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
939 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
942 roffnode_cleanscope(r
);
943 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
948 roff_evalcond(const char *v
, int *pos
)
954 return(ROFFRULE_ALLOW
);
961 return(ROFFRULE_DENY
);
966 while (v
[*pos
] && ' ' != v
[*pos
])
968 return(ROFFRULE_DENY
);
973 roff_line_ignore(ROFF_ARGS
)
977 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
989 /* Stack overflow! */
991 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
992 mandoc_msg(MANDOCERR_MEM
, r
->parse
, ln
, ppos
, NULL
);
996 /* First, evaluate the conditional. */
998 if (ROFF_el
== tok
) {
1000 * An `.el' will get the value of the current rstack
1001 * entry set in prior `ie' calls or defaults to DENY.
1003 if (r
->rstackpos
< 0)
1004 rule
= ROFFRULE_DENY
;
1006 rule
= r
->rstack
[r
->rstackpos
];
1008 rule
= roff_evalcond(*bufp
, &pos
);
1012 while (' ' == (*bufp
)[pos
])
1016 * Roff is weird. If we have just white-space after the
1017 * conditional, it's considered the BODY and we exit without
1018 * really doing anything. Warn about this. It's probably
1022 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1023 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1027 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1029 r
->last
->rule
= rule
;
1031 if (ROFF_ie
== tok
) {
1033 * An if-else will put the NEGATION of the current
1034 * evaluated conditional into the stack.
1037 if (ROFFRULE_DENY
== r
->last
->rule
)
1038 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1040 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1043 /* If the parent has false as its rule, then so do we. */
1045 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1046 r
->last
->rule
= ROFFRULE_DENY
;
1049 * Determine scope. If we're invoked with "\{" trailing the
1050 * conditional, then we're in a multiline scope. Else our scope
1051 * expires on the next line.
1054 r
->last
->endspan
= 1;
1056 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1057 r
->last
->endspan
= -1;
1062 * If there are no arguments on the line, the next-line scope is
1066 if ('\0' == (*bufp
)[pos
])
1069 /* Otherwise re-run the roff parser after recalculating. */
1080 char *name
, *string
;
1083 * A symbol is named by the first word following the macro
1084 * invocation up to a space. Its value is anything after the
1085 * name's trailing whitespace and optional double-quote. Thus,
1089 * will have `bar " ' as its value.
1092 string
= *bufp
+ pos
;
1093 name
= roff_getname(r
, &string
, ln
, pos
);
1097 /* Read past initial double-quote. */
1101 /* The rest is the value. */
1102 roff_setstr(r
, name
, string
, 0);
1116 key
= roff_getname(r
, &val
, ln
, pos
);
1119 if (0 == strcmp(key
, "nS")) {
1120 rg
[(int)REG_nS
].set
= 1;
1121 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1122 rg
[(int)REG_nS
].v
.u
= 0;
1136 while ('\0' != *cp
) {
1137 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1139 roff_setstr(r
, name
, NULL
, 0);
1150 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1164 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1166 tbl_restart(ppos
, ln
, r
->tbl
);
1177 assert(NULL
== r
->eqn
);
1178 e
= eqn_alloc(ppos
, ln
);
1181 r
->last_eqn
->next
= e
;
1183 r
->first_eqn
= r
->last_eqn
= e
;
1185 r
->eqn
= r
->last_eqn
= e
;
1194 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1205 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1209 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1212 r
->last_tbl
->next
= t
;
1214 r
->first_tbl
= r
->last_tbl
= t
;
1216 r
->tbl
= r
->last_tbl
= t
;
1226 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1229 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1230 * opening anything that's not in our cwd or anything beneath
1231 * it. Thus, explicitly disallow traversing up the file-system
1232 * or using absolute paths.
1236 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1237 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1247 roff_userdef(ROFF_ARGS
)
1254 * Collect pointers to macro argument strings
1255 * and null-terminate them.
1258 for (i
= 0; i
< 9; i
++)
1259 arg
[i
] = '\0' == *cp
? "" :
1260 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1263 * Expand macro arguments.
1266 n1
= cp
= mandoc_strdup(r
->current_string
);
1267 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1269 if (0 > i
|| 8 < i
) {
1270 /* Not an argument invocation. */
1275 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1276 n2
= mandoc_malloc(*szp
);
1278 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1279 strlcat(n2
, arg
[i
], *szp
);
1280 strlcat(n2
, cp
+ 3, *szp
);
1282 cp
= n2
+ (cp
- n1
);
1288 * Replace the macro invocation
1289 * by the expanded macro.
1294 *szp
= strlen(*bufp
) + 1;
1296 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1297 ROFF_REPARSE
: ROFF_APPEND
);
1301 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1309 /* Read until end of name. */
1310 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1316 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1321 /* Nil-terminate name. */
1325 /* Read past spaces. */
1334 * Store *string into the user-defined string called *name.
1335 * In multiline mode, append to an existing entry and append '\n';
1336 * else replace the existing entry, if there is one.
1337 * To clear an existing entry, call with (*r, *name, NULL, 0).
1340 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1345 size_t oldch
, newch
;
1347 /* Search for an existing string with the same name. */
1348 n
= r
->first_string
;
1349 while (n
&& strcmp(name
, n
->name
))
1353 /* Create a new string table entry. */
1354 n
= mandoc_malloc(sizeof(struct roffstr
));
1355 n
->name
= mandoc_strdup(name
);
1357 n
->next
= r
->first_string
;
1358 r
->first_string
= n
;
1359 } else if (0 == multiline
) {
1360 /* In multiline mode, append; else replace. */
1369 * One additional byte for the '\n' in multiline mode,
1370 * and one for the terminating '\0'.
1372 newch
= strlen(string
) + (multiline
? 2u : 1u);
1373 if (NULL
== n
->string
) {
1374 n
->string
= mandoc_malloc(newch
);
1378 oldch
= strlen(n
->string
);
1379 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1382 /* Skip existing content in the destination buffer. */
1383 c
= n
->string
+ (int)oldch
;
1385 /* Append new content to the destination buffer. */
1388 * Rudimentary roff copy mode:
1389 * Handle escaped backslashes.
1391 if ('\\' == *string
&& '\\' == *(string
+ 1))
1396 /* Append terminating bytes. */
1403 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1405 const struct roffstr
*n
;
1407 n
= r
->first_string
;
1408 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1411 return(n
? n
->string
: NULL
);
1415 roff_freestr(struct roff
*r
)
1417 struct roffstr
*n
, *nn
;
1419 for (n
= r
->first_string
; n
; n
= nn
) {
1426 r
->first_string
= NULL
;
1429 const struct tbl_span
*
1430 roff_span(const struct roff
*r
)
1433 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1437 roff_eqn(const struct roff
*r
)
1440 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);