]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
35e0f883ef74eaa9ed41ab20c9eed25b2775b85f
1 /* $Id: roff.c,v 1.139 2011/05/24 14:00:39 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 #define RSTACK_MAX 128
63 ROFF_ccond
, /* FIXME: remove this. */
74 char *name
; /* key of symbol */
75 char *string
; /* current value */
76 struct roffstr
*next
; /* next in list */
80 struct mparse
*parse
; /* parse point */
81 struct roffnode
*last
; /* leaf of stack */
82 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
83 int rstackpos
; /* position in rstack */
84 struct regset
*regs
; /* read/writable registers */
85 struct roffstr
*first_string
; /* user-defined strings & macros */
86 const char *current_string
; /* value of last called user macro */
87 struct tbl_node
*first_tbl
; /* first table parsed */
88 struct tbl_node
*last_tbl
; /* last table parsed */
89 struct tbl_node
*tbl
; /* current table being parsed */
90 struct eqn_node
*last_eqn
; /* last equation parsed */
91 struct eqn_node
*first_eqn
; /* first equation parsed */
92 struct eqn_node
*eqn
; /* current equation being parsed */
96 enum rofft tok
; /* type of node */
97 struct roffnode
*parent
; /* up one in stack */
98 int line
; /* parse line */
99 int col
; /* parse col */
100 char *name
; /* node name, e.g. macro name */
101 char *end
; /* end-rules: custom token */
102 int endspan
; /* end-rules: next-line or infty */
103 enum roffrule rule
; /* current evaluation rule */
106 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
107 enum rofft tok, /* tok of macro */ \
108 char **bufp, /* input buffer */ \
109 size_t *szp, /* size of input buffer */ \
110 int ln, /* parse line */ \
111 int ppos, /* original pos in buffer */ \
112 int pos, /* current pos in buffer */ \
113 int *offs /* reset offset of buffer data */
115 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
118 const char *name
; /* macro name */
119 roffproc proc
; /* process new macro */
120 roffproc text
; /* process as child text of macro */
121 roffproc sub
; /* process as child of macro */
123 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
124 struct roffmac
*next
;
127 static enum rofferr
roff_block(ROFF_ARGS
);
128 static enum rofferr
roff_block_text(ROFF_ARGS
);
129 static enum rofferr
roff_block_sub(ROFF_ARGS
);
130 static enum rofferr
roff_cblock(ROFF_ARGS
);
131 static enum rofferr
roff_ccond(ROFF_ARGS
);
132 static enum rofferr
roff_cond(ROFF_ARGS
);
133 static enum rofferr
roff_cond_text(ROFF_ARGS
);
134 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
135 static enum rofferr
roff_ds(ROFF_ARGS
);
136 static enum roffrule
roff_evalcond(const char *, int *);
137 static void roff_freestr(struct roff
*);
138 static char *roff_getname(struct roff
*, char **, int, int);
139 static const char *roff_getstrn(const struct roff
*,
140 const char *, size_t);
141 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
142 static enum rofferr
roff_nr(ROFF_ARGS
);
143 static int roff_res(struct roff
*,
144 char **, size_t *, int);
145 static enum rofferr
roff_rm(ROFF_ARGS
);
146 static void roff_setstr(struct roff
*,
147 const char *, const char *, int);
148 static enum rofferr
roff_so(ROFF_ARGS
);
149 static enum rofferr
roff_TE(ROFF_ARGS
);
150 static enum rofferr
roff_TS(ROFF_ARGS
);
151 static enum rofferr
roff_EQ(ROFF_ARGS
);
152 static enum rofferr
roff_EN(ROFF_ARGS
);
153 static enum rofferr
roff_T_(ROFF_ARGS
);
154 static enum rofferr
roff_userdef(ROFF_ARGS
);
156 /* See roff_hash_find() */
160 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
162 static struct roffmac
*hash
[HASHWIDTH
];
164 static struct roffmac roffs
[ROFF_MAX
] = {
165 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
166 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
167 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
168 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
169 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
170 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
171 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
172 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
173 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
174 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
175 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
176 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
177 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
178 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
179 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
180 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
181 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
182 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
183 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
184 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
185 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
186 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
187 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
188 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
189 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
190 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
191 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
192 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
193 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
194 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
195 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
198 static void roff_free1(struct roff
*);
199 static enum rofft
roff_hash_find(const char *, size_t);
200 static void roff_hash_init(void);
201 static void roffnode_cleanscope(struct roff
*);
202 static void roffnode_push(struct roff
*, enum rofft
,
203 const char *, int, int);
204 static void roffnode_pop(struct roff
*);
205 static enum rofft
roff_parse(struct roff
*, const char *, int *);
207 /* See roff_hash_find() */
208 #define ROFF_HASH(p) (p[0] - ASCII_LO)
216 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
217 assert(roffs
[i
].name
[0] >= ASCII_LO
);
218 assert(roffs
[i
].name
[0] <= ASCII_HI
);
220 buc
= ROFF_HASH(roffs
[i
].name
);
222 if (NULL
!= (n
= hash
[buc
])) {
223 for ( ; n
->next
; n
= n
->next
)
227 hash
[buc
] = &roffs
[i
];
233 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
234 * the nil-terminated string name could be found.
237 roff_hash_find(const char *p
, size_t s
)
243 * libroff has an extremely simple hashtable, for the time
244 * being, which simply keys on the first character, which must
245 * be printable, then walks a chain. It works well enough until
249 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
254 if (NULL
== (n
= hash
[buc
]))
256 for ( ; n
; n
= n
->next
)
257 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
258 return((enum rofft
)(n
- roffs
));
265 * Pop the current node off of the stack of roff instructions currently
269 roffnode_pop(struct roff
*r
)
276 r
->last
= r
->last
->parent
;
284 * Push a roff node onto the instruction stack. This must later be
285 * removed with roffnode_pop().
288 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
293 p
= mandoc_calloc(1, sizeof(struct roffnode
));
296 p
->name
= mandoc_strdup(name
);
300 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
307 roff_free1(struct roff
*r
)
312 while (NULL
!= (t
= r
->first_tbl
)) {
313 r
->first_tbl
= t
->next
;
317 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
319 while (NULL
!= (e
= r
->first_eqn
)) {
320 r
->first_eqn
= e
->next
;
324 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
334 roff_reset(struct roff
*r
)
342 roff_free(struct roff
*r
)
351 roff_alloc(struct regset
*regs
, struct mparse
*parse
)
355 r
= mandoc_calloc(1, sizeof(struct roff
));
366 * Pre-filter each and every line for reserved words (one beginning with
367 * `\*', e.g., `\*(ab'). These must be handled before the actual line
371 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
373 const char *stesc
; /* start of an escape sequence ('\\') */
374 const char *stnam
; /* start of the name, after "[(*" */
375 const char *cp
; /* end of the name, e.g. before ']' */
376 const char *res
; /* the string to be substituted */
381 /* Search for a leading backslash and save a pointer to it. */
384 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
388 * The second character must be an asterisk.
389 * If it isn't, skip it anyway: It is escaped,
390 * so it can't start another escape sequence.
399 * The third character decides the length
400 * of the name of the string.
401 * Save a pointer to the name.
421 /* Advance to the end of the name. */
423 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
425 return(1); /* Error. */
426 if (0 == maxl
&& ']' == *cp
)
431 * Retrieve the replacement string; if it is
432 * undefined, resume searching for escapes.
435 res
= roff_getstrn(r
, stnam
, (size_t)i
);
442 /* Replace the escape sequence by the string. */
444 nsz
= *szp
+ strlen(res
) + 1;
445 n
= mandoc_malloc(nsz
);
447 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
448 strlcat(n
, res
, nsz
);
449 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
463 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
464 size_t *szp
, int pos
, int *offs
)
471 * Run the reserved-word filter only if we have some reserved
475 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
476 return(ROFF_REPARSE
);
479 ctl
= mandoc_getcontrol(*bufp
, &pos
);
482 * First, if a scope is open and we're not a macro, pass the
483 * text through the macro's filter. If a scope isn't open and
484 * we're not a macro, just let it through.
485 * Finally, if there's an equation scope open, divert it into it
486 * no matter our state.
489 if (r
->last
&& ! ctl
) {
491 assert(roffs
[t
].text
);
493 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
494 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
498 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
));
500 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
504 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
));
506 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
509 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
));
512 * If a scope is open, go to the child handler for that macro,
513 * as it may want to preprocess before doing anything with it.
514 * Don't do so if an equation is open.
519 assert(roffs
[t
].sub
);
520 return((*roffs
[t
].sub
)
522 ln
, ppos
, pos
, offs
));
526 * Lastly, as we've no scope open, try to look up and execute
527 * the new macro. If no macro is found, simply return and let
528 * the compilers handle it.
531 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
534 assert(roffs
[t
].proc
);
535 return((*roffs
[t
].proc
)
537 ln
, ppos
, pos
, offs
));
542 roff_endparse(struct roff
*r
)
546 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
547 r
->last
->line
, r
->last
->col
, NULL
);
550 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
551 r
->eqn
->eqn
.line
, r
->eqn
->eqn
.pos
, NULL
);
557 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
558 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
565 * Parse a roff node's type from the input buffer. This must be in the
566 * form of ".foo xxx" in the usual way.
569 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
575 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
])
579 maclen
= strcspn(mac
, " \\\t\0");
581 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
582 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
586 while (buf
[*pos
] && ' ' == buf
[*pos
])
594 roff_cblock(ROFF_ARGS
)
598 * A block-close `..' should only be invoked as a child of an
599 * ignore macro, otherwise raise a warning and just ignore it.
602 if (NULL
== r
->last
) {
603 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
607 switch (r
->last
->tok
) {
615 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
622 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
627 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
630 roffnode_cleanscope(r
);
637 roffnode_cleanscope(struct roff
*r
)
641 if (--r
->last
->endspan
< 0)
650 roff_ccond(ROFF_ARGS
)
653 if (NULL
== r
->last
) {
654 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
658 switch (r
->last
->tok
) {
666 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
670 if (r
->last
->endspan
> -1) {
671 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
676 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
679 roffnode_cleanscope(r
);
686 roff_block(ROFF_ARGS
)
694 if (ROFF_ig
!= tok
) {
695 if ('\0' == (*bufp
)[pos
]) {
696 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
701 * Re-write `de1', since we don't really care about
702 * groff's strange compatibility mode, into `de'.
710 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
713 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
716 while (isspace((unsigned char)(*bufp
)[pos
]))
717 (*bufp
)[pos
++] = '\0';
720 roffnode_push(r
, tok
, name
, ln
, ppos
);
723 * At the beginning of a `de' macro, clear the existing string
724 * with the same name, if there is one. New content will be
725 * added from roff_block_text() in multiline mode.
729 roff_setstr(r
, name
, "", 0);
731 if ('\0' == (*bufp
)[pos
])
734 /* If present, process the custom end-of-line marker. */
737 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
741 * Note: groff does NOT like escape characters in the input.
742 * Instead of detecting this, we're just going to let it fly and
747 sz
= (size_t)(pos
- sv
);
749 if (1 == sz
&& '.' == (*bufp
)[sv
])
752 r
->last
->end
= mandoc_malloc(sz
+ 1);
754 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
755 r
->last
->end
[(int)sz
] = '\0';
758 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
766 roff_block_sub(ROFF_ARGS
)
772 * First check whether a custom macro exists at this level. If
773 * it does, then check against it. This is some of groff's
774 * stranger behaviours. If we encountered a custom end-scope
775 * tag and that tag also happens to be a "real" macro, then we
776 * need to try interpreting it again as a real macro. If it's
777 * not, then return ignore. Else continue.
781 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
782 if ((*bufp
)[i
] != r
->last
->end
[j
])
785 if ('\0' == r
->last
->end
[j
] &&
786 ('\0' == (*bufp
)[i
] ||
788 '\t' == (*bufp
)[i
])) {
790 roffnode_cleanscope(r
);
792 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
796 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
803 * If we have no custom end-query or lookup failed, then try
804 * pulling it out of the hashtable.
807 t
= roff_parse(r
, *bufp
, &pos
);
810 * Macros other than block-end are only significant
811 * in `de' blocks; elsewhere, simply throw them away.
813 if (ROFF_cblock
!= t
) {
815 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
819 assert(roffs
[t
].proc
);
820 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
821 ln
, ppos
, pos
, offs
));
827 roff_block_text(ROFF_ARGS
)
831 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
839 roff_cond_sub(ROFF_ARGS
)
846 roffnode_cleanscope(r
);
849 * If the macro is unknown, first check if it contains a closing
850 * delimiter `\}'. If it does, close out our scope and return
851 * the currently-scoped rule (ignore or continue). Else, drop
852 * into the currently-scoped rule.
855 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
857 * Jump through hoops to detect a \}, because it could
858 * be (say) \\}, which is something completely
862 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
867 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
868 ln
, pos
, pos
+ 2, offs
);
871 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
875 * A denied conditional must evaluate its children if and only
876 * if they're either structurally required (such as loops and
877 * conditionals) or a closing macro.
880 if (ROFFRULE_DENY
== rr
)
881 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
885 assert(roffs
[t
].proc
);
886 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
887 ln
, ppos
, pos
, offs
));
893 roff_cond_text(ROFF_ARGS
)
901 * We display the value of the text if out current evaluation
902 * scope permits us to do so.
905 /* FIXME: use roff_ccond? */
908 if (NULL
== (ep
= strstr(st
, "\\}"))) {
909 roffnode_cleanscope(r
);
910 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
913 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
916 roffnode_cleanscope(r
);
917 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
922 roff_evalcond(const char *v
, int *pos
)
928 return(ROFFRULE_ALLOW
);
935 return(ROFFRULE_DENY
);
940 while (v
[*pos
] && ' ' != v
[*pos
])
942 return(ROFFRULE_DENY
);
947 roff_line_ignore(ROFF_ARGS
)
951 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
964 * An `.el' has no conditional body: it will consume the value
965 * of the current rstack entry set in prior `ie' calls or
968 * If we're not an `el', however, then evaluate the conditional.
971 rule
= ROFF_el
== tok
?
973 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
974 roff_evalcond(*bufp
, &pos
);
977 while (' ' == (*bufp
)[pos
])
981 * Roff is weird. If we have just white-space after the
982 * conditional, it's considered the BODY and we exit without
983 * really doing anything. Warn about this. It's probably
987 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
988 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
992 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
994 r
->last
->rule
= rule
;
997 * An if-else will put the NEGATION of the current evaluated
998 * conditional into the stack of rules.
1001 if (ROFF_ie
== tok
) {
1002 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1003 mandoc_msg(MANDOCERR_MEM
,
1004 r
->parse
, ln
, ppos
, NULL
);
1007 r
->rstack
[++r
->rstackpos
] =
1008 ROFFRULE_DENY
== r
->last
->rule
?
1009 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1012 /* If the parent has false as its rule, then so do we. */
1014 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1015 r
->last
->rule
= ROFFRULE_DENY
;
1018 * Determine scope. If we're invoked with "\{" trailing the
1019 * conditional, then we're in a multiline scope. Else our scope
1020 * expires on the next line.
1023 r
->last
->endspan
= 1;
1025 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1026 r
->last
->endspan
= -1;
1031 * If there are no arguments on the line, the next-line scope is
1035 if ('\0' == (*bufp
)[pos
])
1038 /* Otherwise re-run the roff parser after recalculating. */
1049 char *name
, *string
;
1052 * A symbol is named by the first word following the macro
1053 * invocation up to a space. Its value is anything after the
1054 * name's trailing whitespace and optional double-quote. Thus,
1058 * will have `bar " ' as its value.
1061 string
= *bufp
+ pos
;
1062 name
= roff_getname(r
, &string
, ln
, pos
);
1066 /* Read past initial double-quote. */
1070 /* The rest is the value. */
1071 roff_setstr(r
, name
, string
, 0);
1086 key
= roff_getname(r
, &val
, ln
, pos
);
1089 if (0 == strcmp(key
, "nS")) {
1090 rg
[(int)REG_nS
].set
= 1;
1091 if ((iv
= mandoc_strntou(val
, strlen(val
), 10)) >= 0)
1092 rg
[REG_nS
].v
.u
= (unsigned)iv
;
1094 rg
[(int)REG_nS
].v
.u
= 0u;
1108 while ('\0' != *cp
) {
1109 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1111 roff_setstr(r
, name
, NULL
, 0);
1122 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1136 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1138 tbl_restart(ppos
, ln
, r
->tbl
);
1149 assert(NULL
== r
->eqn
);
1150 e
= eqn_alloc(ppos
, ln
);
1153 r
->last_eqn
->next
= e
;
1155 r
->first_eqn
= r
->last_eqn
= e
;
1157 r
->eqn
= r
->last_eqn
= e
;
1166 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1177 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1181 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1184 r
->last_tbl
->next
= t
;
1186 r
->first_tbl
= r
->last_tbl
= t
;
1188 r
->tbl
= r
->last_tbl
= t
;
1198 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1201 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1202 * opening anything that's not in our cwd or anything beneath
1203 * it. Thus, explicitly disallow traversing up the file-system
1204 * or using absolute paths.
1208 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1209 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1219 roff_userdef(ROFF_ARGS
)
1226 * Collect pointers to macro argument strings
1227 * and null-terminate them.
1230 for (i
= 0; i
< 9; i
++)
1231 arg
[i
] = '\0' == *cp
? "" :
1232 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1235 * Expand macro arguments.
1238 n1
= cp
= mandoc_strdup(r
->current_string
);
1239 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1241 if (0 > i
|| 8 < i
) {
1242 /* Not an argument invocation. */
1247 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1248 n2
= mandoc_malloc(*szp
);
1250 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1251 strlcat(n2
, arg
[i
], *szp
);
1252 strlcat(n2
, cp
+ 3, *szp
);
1254 cp
= n2
+ (cp
- n1
);
1260 * Replace the macro invocation
1261 * by the expanded macro.
1266 *szp
= strlen(*bufp
) + 1;
1268 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1269 ROFF_REPARSE
: ROFF_APPEND
);
1273 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1281 /* Read until end of name. */
1282 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1288 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1293 /* Nil-terminate name. */
1297 /* Read past spaces. */
1306 * Store *string into the user-defined string called *name.
1307 * In multiline mode, append to an existing entry and append '\n';
1308 * else replace the existing entry, if there is one.
1309 * To clear an existing entry, call with (*r, *name, NULL, 0).
1312 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1317 size_t oldch
, newch
;
1319 /* Search for an existing string with the same name. */
1320 n
= r
->first_string
;
1321 while (n
&& strcmp(name
, n
->name
))
1325 /* Create a new string table entry. */
1326 n
= mandoc_malloc(sizeof(struct roffstr
));
1327 n
->name
= mandoc_strdup(name
);
1329 n
->next
= r
->first_string
;
1330 r
->first_string
= n
;
1331 } else if (0 == multiline
) {
1332 /* In multiline mode, append; else replace. */
1341 * One additional byte for the '\n' in multiline mode,
1342 * and one for the terminating '\0'.
1344 newch
= strlen(string
) + (multiline
? 2u : 1u);
1345 if (NULL
== n
->string
) {
1346 n
->string
= mandoc_malloc(newch
);
1350 oldch
= strlen(n
->string
);
1351 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1354 /* Skip existing content in the destination buffer. */
1355 c
= n
->string
+ (int)oldch
;
1357 /* Append new content to the destination buffer. */
1360 * Rudimentary roff copy mode:
1361 * Handle escaped backslashes.
1363 if ('\\' == *string
&& '\\' == *(string
+ 1))
1368 /* Append terminating bytes. */
1375 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1377 const struct roffstr
*n
;
1379 n
= r
->first_string
;
1380 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1383 return(n
? n
->string
: NULL
);
1387 roff_freestr(struct roff
*r
)
1389 struct roffstr
*n
, *nn
;
1391 for (n
= r
->first_string
; n
; n
= nn
) {
1398 r
->first_string
= NULL
;
1401 const struct tbl_span
*
1402 roff_span(const struct roff
*r
)
1405 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1409 roff_eqn(const struct roff
*r
)
1412 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);