]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.125 2011/02/06 20:36:36 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.
33 #include "libmandoc.h"
35 #define RSTACK_MAX 128
38 ('.' == (c) || '\'' == (c))
70 ROFF_ccond
, /* FIXME: remove this. */
81 char *name
; /* key of symbol */
82 char *string
; /* current value */
83 struct roffstr
*next
; /* next in list */
87 struct roffnode
*last
; /* leaf of stack */
88 mandocmsg msg
; /* err/warn/fatal messages */
89 void *data
; /* privdata for messages */
90 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
91 int rstackpos
; /* position in rstack */
92 struct regset
*regs
; /* read/writable registers */
93 struct roffstr
*first_string
; /* user-defined strings & macros */
94 const char *current_string
; /* value of last called user macro */
95 struct tbl_node
*first_tbl
; /* first table parsed */
96 struct tbl_node
*last_tbl
; /* last table parsed */
97 struct tbl_node
*tbl
; /* current table being parsed */
98 struct eqn_node
*last_eqn
; /* last equation parsed */
99 struct eqn_node
*first_eqn
; /* first equation parsed */
100 struct eqn_node
*eqn
; /* current equation being parsed */
104 enum rofft tok
; /* type of node */
105 struct roffnode
*parent
; /* up one in stack */
106 int line
; /* parse line */
107 int col
; /* parse col */
108 char *name
; /* node name, e.g. macro name */
109 char *end
; /* end-rules: custom token */
110 int endspan
; /* end-rules: next-line or infty */
111 enum roffrule rule
; /* current evaluation rule */
114 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
115 enum rofft tok, /* tok of macro */ \
116 char **bufp, /* input buffer */ \
117 size_t *szp, /* size of input buffer */ \
118 int ln, /* parse line */ \
119 int ppos, /* original pos in buffer */ \
120 int pos, /* current pos in buffer */ \
121 int *offs /* reset offset of buffer data */
123 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
126 const char *name
; /* macro name */
127 roffproc proc
; /* process new macro */
128 roffproc text
; /* process as child text of macro */
129 roffproc sub
; /* process as child of macro */
131 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
132 struct roffmac
*next
;
135 static enum rofferr
roff_block(ROFF_ARGS
);
136 static enum rofferr
roff_block_text(ROFF_ARGS
);
137 static enum rofferr
roff_block_sub(ROFF_ARGS
);
138 static enum rofferr
roff_cblock(ROFF_ARGS
);
139 static enum rofferr
roff_ccond(ROFF_ARGS
);
140 static enum rofferr
roff_cond(ROFF_ARGS
);
141 static enum rofferr
roff_cond_text(ROFF_ARGS
);
142 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
143 static enum rofferr
roff_ds(ROFF_ARGS
);
144 static enum roffrule
roff_evalcond(const char *, int *);
145 static void roff_freestr(struct roff
*);
146 static char *roff_getname(struct roff
*, char **, int, int);
147 static const char *roff_getstrn(const struct roff
*,
148 const char *, size_t);
149 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
150 static enum rofferr
roff_nr(ROFF_ARGS
);
151 static int roff_res(struct roff
*,
152 char **, size_t *, int);
153 static enum rofferr
roff_rm(ROFF_ARGS
);
154 static void roff_setstr(struct roff
*,
155 const char *, const char *, int);
156 static enum rofferr
roff_so(ROFF_ARGS
);
157 static enum rofferr
roff_TE(ROFF_ARGS
);
158 static enum rofferr
roff_TS(ROFF_ARGS
);
159 static enum rofferr
roff_EQ(ROFF_ARGS
);
160 static enum rofferr
roff_EN(ROFF_ARGS
);
161 static enum rofferr
roff_T_(ROFF_ARGS
);
162 static enum rofferr
roff_userdef(ROFF_ARGS
);
164 /* See roff_hash_find() */
168 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
170 static struct roffmac
*hash
[HASHWIDTH
];
172 static struct roffmac roffs
[ROFF_MAX
] = {
173 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
174 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
175 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
176 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
177 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
178 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
179 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
180 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
181 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
182 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
183 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
184 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
185 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
186 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
187 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
188 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
189 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
190 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
191 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
192 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
193 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
194 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
195 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
196 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
197 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
198 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
199 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
200 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
201 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
202 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
203 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
206 static void roff_free1(struct roff
*);
207 static enum rofft
roff_hash_find(const char *, size_t);
208 static void roff_hash_init(void);
209 static void roffnode_cleanscope(struct roff
*);
210 static void roffnode_push(struct roff
*, enum rofft
,
211 const char *, int, int);
212 static void roffnode_pop(struct roff
*);
213 static enum rofft
roff_parse(struct roff
*, const char *, int *);
214 static int roff_parse_nat(const char *, unsigned int *);
216 /* See roff_hash_find() */
217 #define ROFF_HASH(p) (p[0] - ASCII_LO)
225 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
226 assert(roffs
[i
].name
[0] >= ASCII_LO
);
227 assert(roffs
[i
].name
[0] <= ASCII_HI
);
229 buc
= ROFF_HASH(roffs
[i
].name
);
231 if (NULL
!= (n
= hash
[buc
])) {
232 for ( ; n
->next
; n
= n
->next
)
236 hash
[buc
] = &roffs
[i
];
242 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
243 * the nil-terminated string name could be found.
246 roff_hash_find(const char *p
, size_t s
)
252 * libroff has an extremely simple hashtable, for the time
253 * being, which simply keys on the first character, which must
254 * be printable, then walks a chain. It works well enough until
258 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
263 if (NULL
== (n
= hash
[buc
]))
265 for ( ; n
; n
= n
->next
)
266 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
267 return((enum rofft
)(n
- roffs
));
274 * Pop the current node off of the stack of roff instructions currently
278 roffnode_pop(struct roff
*r
)
285 if (ROFF_el
== p
->tok
)
286 if (r
->rstackpos
> -1)
289 r
->last
= r
->last
->parent
;
297 * Push a roff node onto the instruction stack. This must later be
298 * removed with roffnode_pop().
301 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
306 p
= mandoc_calloc(1, sizeof(struct roffnode
));
309 p
->name
= mandoc_strdup(name
);
313 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
320 roff_free1(struct roff
*r
)
325 while (NULL
!= (t
= r
->first_tbl
)) {
326 r
->first_tbl
= t
->next
;
330 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
332 while (NULL
!= (e
= r
->first_eqn
)) {
333 r
->first_eqn
= e
->next
;
337 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
347 roff_reset(struct roff
*r
)
355 roff_free(struct roff
*r
)
364 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
368 r
= mandoc_calloc(1, sizeof(struct roff
));
380 * Pre-filter each and every line for reserved words (one beginning with
381 * `\*', e.g., `\*(ab'). These must be handled before the actual line
385 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
387 const char *stesc
; /* start of an escape sequence ('\\') */
388 const char *stnam
; /* start of the name, after "[(*" */
389 const char *cp
; /* end of the name, e.g. before ']' */
390 const char *res
; /* the string to be substituted */
395 /* Search for a leading backslash and save a pointer to it. */
398 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
402 * The second character must be an asterisk.
403 * If it isn't, skip it anyway: It is escaped,
404 * so it can't start another escape sequence.
413 * The third character decides the length
414 * of the name of the string.
415 * Save a pointer to the name.
435 /* Advance to the end of the name. */
437 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
439 return(1); /* Error. */
440 if (0 == maxl
&& ']' == *cp
)
445 * Retrieve the replacement string; if it is
446 * undefined, resume searching for escapes.
449 res
= roff_getstrn(r
, stnam
, (size_t)i
);
456 /* Replace the escape sequence by the string. */
458 nsz
= *szp
+ strlen(res
) + 1;
459 n
= mandoc_malloc(nsz
);
461 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
462 strlcat(n
, res
, nsz
);
463 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
477 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
478 size_t *szp
, int pos
, int *offs
)
485 * Run the reserved-word filter only if we have some reserved
489 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
490 return(ROFF_REPARSE
);
493 * First, if a scope is open and we're not a macro, pass the
494 * text through the macro's filter. If a scope isn't open and
495 * we're not a macro, just let it through.
496 * Finally, if there's an equation scope open, divert it into it
497 * no matter our state.
500 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
502 assert(roffs
[t
].text
);
504 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
505 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
509 return(eqn_read(&r
->eqn
, ln
, *bufp
, *offs
));
511 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
513 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
515 return(eqn_read(&r
->eqn
, ln
, *bufp
, *offs
));
517 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
520 return(eqn_read(&r
->eqn
, ln
, *bufp
, *offs
));
523 * If a scope is open, go to the child handler for that macro,
524 * as it may want to preprocess before doing anything with it.
525 * Don't do so if an equation is open.
530 assert(roffs
[t
].sub
);
531 return((*roffs
[t
].sub
)
533 ln
, pos
, pos
, offs
));
537 * Lastly, as we've no scope open, try to look up and execute
538 * the new macro. If no macro is found, simply return and let
539 * the compilers handle it.
543 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
546 assert(roffs
[t
].proc
);
547 return((*roffs
[t
].proc
)
549 ln
, ppos
, pos
, offs
));
554 roff_endparse(struct roff
*r
)
558 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
559 r
->last
->line
, r
->last
->col
, NULL
);
562 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
563 r
->eqn
->line
, r
->eqn
->pos
, NULL
);
569 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
570 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
578 * Parse a roff node's type from the input buffer. This must be in the
579 * form of ".foo xxx" in the usual way.
582 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
588 assert(ROFF_CTL(buf
[*pos
]));
591 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
594 if ('\0' == buf
[*pos
])
598 maclen
= strcspn(mac
, " \\\t\0");
600 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
601 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
604 while (buf
[*pos
] && ' ' == buf
[*pos
])
612 roff_parse_nat(const char *buf
, unsigned int *res
)
618 lval
= strtol(buf
, &ep
, 10);
619 if (buf
[0] == '\0' || *ep
!= '\0')
621 if ((errno
== ERANGE
&&
622 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
623 (lval
> INT_MAX
|| lval
< 0))
626 *res
= (unsigned int)lval
;
633 roff_cblock(ROFF_ARGS
)
637 * A block-close `..' should only be invoked as a child of an
638 * ignore macro, otherwise raise a warning and just ignore it.
641 if (NULL
== r
->last
) {
642 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
646 switch (r
->last
->tok
) {
654 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
661 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
666 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
669 roffnode_cleanscope(r
);
676 roffnode_cleanscope(struct roff
*r
)
680 if (--r
->last
->endspan
< 0)
689 roff_ccond(ROFF_ARGS
)
692 if (NULL
== r
->last
) {
693 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
697 switch (r
->last
->tok
) {
705 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
709 if (r
->last
->endspan
> -1) {
710 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
715 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
718 roffnode_cleanscope(r
);
725 roff_block(ROFF_ARGS
)
733 if (ROFF_ig
!= tok
) {
734 if ('\0' == (*bufp
)[pos
]) {
735 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
740 * Re-write `de1', since we don't really care about
741 * groff's strange compatibility mode, into `de'.
749 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
752 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
755 while (' ' == (*bufp
)[pos
])
756 (*bufp
)[pos
++] = '\0';
759 roffnode_push(r
, tok
, name
, ln
, ppos
);
762 * At the beginning of a `de' macro, clear the existing string
763 * with the same name, if there is one. New content will be
764 * added from roff_block_text() in multiline mode.
768 roff_setstr(r
, name
, "", 0);
770 if ('\0' == (*bufp
)[pos
])
773 /* If present, process the custom end-of-line marker. */
776 while ((*bufp
)[pos
] &&
777 ' ' != (*bufp
)[pos
] &&
778 '\t' != (*bufp
)[pos
])
782 * Note: groff does NOT like escape characters in the input.
783 * Instead of detecting this, we're just going to let it fly and
788 sz
= (size_t)(pos
- sv
);
790 if (1 == sz
&& '.' == (*bufp
)[sv
])
793 r
->last
->end
= mandoc_malloc(sz
+ 1);
795 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
796 r
->last
->end
[(int)sz
] = '\0';
799 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
807 roff_block_sub(ROFF_ARGS
)
813 * First check whether a custom macro exists at this level. If
814 * it does, then check against it. This is some of groff's
815 * stranger behaviours. If we encountered a custom end-scope
816 * tag and that tag also happens to be a "real" macro, then we
817 * need to try interpreting it again as a real macro. If it's
818 * not, then return ignore. Else continue.
823 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
826 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
827 if ((*bufp
)[i
] != r
->last
->end
[j
])
830 if ('\0' == r
->last
->end
[j
] &&
831 ('\0' == (*bufp
)[i
] ||
833 '\t' == (*bufp
)[i
])) {
835 roffnode_cleanscope(r
);
837 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
844 * If we have no custom end-query or lookup failed, then try
845 * pulling it out of the hashtable.
849 t
= roff_parse(r
, *bufp
, &pos
);
852 * Macros other than block-end are only significant
853 * in `de' blocks; elsewhere, simply throw them away.
855 if (ROFF_cblock
!= t
) {
857 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
861 assert(roffs
[t
].proc
);
862 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
863 ln
, ppos
, pos
, offs
));
869 roff_block_text(ROFF_ARGS
)
873 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
881 roff_cond_sub(ROFF_ARGS
)
890 * Clean out scope. If we've closed ourselves, then don't
894 roffnode_cleanscope(r
);
896 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
897 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
899 (r
, ROFF_ccond
, bufp
, szp
,
900 ln
, pos
, pos
+ 2, offs
));
901 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
905 * A denied conditional must evaluate its children if and only
906 * if they're either structurally required (such as loops and
907 * conditionals) or a closing macro.
909 if (ROFFRULE_DENY
== rr
)
910 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
914 assert(roffs
[t
].proc
);
915 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
916 ln
, ppos
, pos
, offs
));
922 roff_cond_text(ROFF_ARGS
)
930 * We display the value of the text if out current evaluation
931 * scope permits us to do so.
934 /* FIXME: use roff_ccond? */
937 if (NULL
== (ep
= strstr(st
, "\\}"))) {
938 roffnode_cleanscope(r
);
939 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
942 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
945 roffnode_cleanscope(r
);
946 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
951 roff_evalcond(const char *v
, int *pos
)
957 return(ROFFRULE_ALLOW
);
964 return(ROFFRULE_DENY
);
969 while (v
[*pos
] && ' ' != v
[*pos
])
971 return(ROFFRULE_DENY
);
976 roff_line_ignore(ROFF_ARGS
)
980 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, "it");
992 /* Stack overflow! */
994 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
995 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
999 /* First, evaluate the conditional. */
1001 if (ROFF_el
== tok
) {
1003 * An `.el' will get the value of the current rstack
1004 * entry set in prior `ie' calls or defaults to DENY.
1006 if (r
->rstackpos
< 0)
1007 rule
= ROFFRULE_DENY
;
1009 rule
= r
->rstack
[r
->rstackpos
];
1011 rule
= roff_evalcond(*bufp
, &pos
);
1015 while (' ' == (*bufp
)[pos
])
1019 * Roff is weird. If we have just white-space after the
1020 * conditional, it's considered the BODY and we exit without
1021 * really doing anything. Warn about this. It's probably
1025 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
1026 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
1030 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1032 r
->last
->rule
= rule
;
1034 if (ROFF_ie
== tok
) {
1036 * An if-else will put the NEGATION of the current
1037 * evaluated conditional into the stack.
1040 if (ROFFRULE_DENY
== r
->last
->rule
)
1041 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1043 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1046 /* If the parent has false as its rule, then so do we. */
1048 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1049 r
->last
->rule
= ROFFRULE_DENY
;
1052 * Determine scope. If we're invoked with "\{" trailing the
1053 * conditional, then we're in a multiline scope. Else our scope
1054 * expires on the next line.
1057 r
->last
->endspan
= 1;
1059 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1060 r
->last
->endspan
= -1;
1065 * If there are no arguments on the line, the next-line scope is
1069 if ('\0' == (*bufp
)[pos
])
1072 /* Otherwise re-run the roff parser after recalculating. */
1083 char *name
, *string
;
1086 * A symbol is named by the first word following the macro
1087 * invocation up to a space. Its value is anything after the
1088 * name's trailing whitespace and optional double-quote. Thus,
1092 * will have `bar " ' as its value.
1095 string
= *bufp
+ pos
;
1096 name
= roff_getname(r
, &string
, ln
, pos
);
1100 /* Read past initial double-quote. */
1104 /* The rest is the value. */
1105 roff_setstr(r
, name
, string
, 0);
1119 key
= roff_getname(r
, &val
, ln
, pos
);
1122 if (0 == strcmp(key
, "nS")) {
1123 rg
[(int)REG_nS
].set
= 1;
1124 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1125 rg
[(int)REG_nS
].v
.u
= 0;
1139 while ('\0' != *cp
) {
1140 name
= roff_getname(r
, &cp
, ln
, cp
- *bufp
);
1142 roff_setstr(r
, name
, NULL
, 0);
1153 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1167 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1169 tbl_restart(ppos
, ln
, r
->tbl
);
1180 assert(NULL
== r
->eqn
);
1181 e
= eqn_alloc(ppos
, ln
);
1184 r
->last_eqn
->next
= e
;
1186 r
->first_eqn
= r
->last_eqn
= e
;
1188 r
->eqn
= r
->last_eqn
= e
;
1197 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1208 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1212 t
= tbl_alloc(ppos
, ln
, r
->data
, r
->msg
);
1215 r
->last_tbl
->next
= t
;
1217 r
->first_tbl
= r
->last_tbl
= t
;
1219 r
->tbl
= r
->last_tbl
= t
;
1229 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1232 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1233 * opening anything that's not in our cwd or anything beneath
1234 * it. Thus, explicitly disallow traversing up the file-system
1235 * or using absolute paths.
1239 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1240 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1250 roff_userdef(ROFF_ARGS
)
1257 * Collect pointers to macro argument strings
1258 * and null-terminate them.
1261 for (i
= 0; i
< 9; i
++)
1262 arg
[i
] = '\0' == *cp
? "" :
1263 mandoc_getarg(&cp
, r
->msg
, r
->data
, ln
, &pos
);
1266 * Expand macro arguments.
1269 n1
= cp
= mandoc_strdup(r
->current_string
);
1270 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1272 if (0 > i
|| 8 < i
) {
1273 /* Not an argument invocation. */
1278 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1279 n2
= mandoc_malloc(*szp
);
1281 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1282 strlcat(n2
, arg
[i
], *szp
);
1283 strlcat(n2
, cp
+ 3, *szp
);
1285 cp
= n2
+ (cp
- n1
);
1291 * Replace the macro invocation
1292 * by the expanded macro.
1297 *szp
= strlen(*bufp
) + 1;
1299 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1300 ROFF_REPARSE
: ROFF_APPEND
);
1304 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1312 /* Read until end of name. */
1313 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1319 (*r
->msg
)(MANDOCERR_NAMESC
, r
->data
, ln
, pos
, NULL
);
1324 /* Nil-terminate name. */
1328 /* Read past spaces. */
1337 * Store *string into the user-defined string called *name.
1338 * In multiline mode, append to an existing entry and append '\n';
1339 * else replace the existing entry, if there is one.
1340 * To clear an existing entry, call with (*r, *name, NULL, 0).
1343 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1348 size_t oldch
, newch
;
1350 /* Search for an existing string with the same name. */
1351 n
= r
->first_string
;
1352 while (n
&& strcmp(name
, n
->name
))
1356 /* Create a new string table entry. */
1357 n
= mandoc_malloc(sizeof(struct roffstr
));
1358 n
->name
= mandoc_strdup(name
);
1360 n
->next
= r
->first_string
;
1361 r
->first_string
= n
;
1362 } else if (0 == multiline
) {
1363 /* In multiline mode, append; else replace. */
1372 * One additional byte for the '\n' in multiline mode,
1373 * and one for the terminating '\0'.
1375 newch
= strlen(string
) + (multiline
? 2 : 1);
1376 if (NULL
== n
->string
) {
1377 n
->string
= mandoc_malloc(newch
);
1381 oldch
= strlen(n
->string
);
1382 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1385 /* Skip existing content in the destination buffer. */
1386 c
= n
->string
+ oldch
;
1388 /* Append new content to the destination buffer. */
1391 * Rudimentary roff copy mode:
1392 * Handle escaped backslashes.
1394 if ('\\' == *string
&& '\\' == *(string
+ 1))
1399 /* Append terminating bytes. */
1406 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1408 const struct roffstr
*n
;
1410 n
= r
->first_string
;
1411 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1414 return(n
? n
->string
: NULL
);
1418 roff_freestr(struct roff
*r
)
1420 struct roffstr
*n
, *nn
;
1422 for (n
= r
->first_string
; n
; n
= nn
) {
1429 r
->first_string
= NULL
;
1432 const struct tbl_span
*
1433 roff_span(const struct roff
*r
)
1436 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1440 roff_eqn(const struct roff
*r
)
1443 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);