]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
a9dbce22a58db061900e7d4a6c9778e45880463d
1 /* $Id: roff.c,v 1.141 2011/05/24 21:18:06 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 char *name
; /* key of symbol */
76 char *string
; /* current value */
77 struct roffstr
*next
; /* next in list */
81 struct mparse
*parse
; /* parse point */
82 struct roffnode
*last
; /* leaf of stack */
83 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
84 int rstackpos
; /* position in rstack */
85 struct regset
*regs
; /* read/writable registers */
86 struct roffstr
*first_string
; /* user-defined strings & macros */
87 const char *current_string
; /* value of last called user macro */
88 struct tbl_node
*first_tbl
; /* first table parsed */
89 struct tbl_node
*last_tbl
; /* last table parsed */
90 struct tbl_node
*tbl
; /* current table being parsed */
91 struct eqn_node
*last_eqn
; /* last equation parsed */
92 struct eqn_node
*first_eqn
; /* first equation parsed */
93 struct eqn_node
*eqn
; /* current equation being parsed */
97 enum rofft tok
; /* type of node */
98 struct roffnode
*parent
; /* up one in stack */
99 int line
; /* parse line */
100 int col
; /* parse col */
101 char *name
; /* node name, e.g. macro name */
102 char *end
; /* end-rules: custom token */
103 int endspan
; /* end-rules: next-line or infty */
104 enum roffrule rule
; /* current evaluation rule */
107 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
108 enum rofft tok, /* tok of macro */ \
109 char **bufp, /* input buffer */ \
110 size_t *szp, /* size of input buffer */ \
111 int ln, /* parse line */ \
112 int ppos, /* original pos in buffer */ \
113 int pos, /* current pos in buffer */ \
114 int *offs /* reset offset of buffer data */
116 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
119 const char *name
; /* macro name */
120 roffproc proc
; /* process new macro */
121 roffproc text
; /* process as child text of macro */
122 roffproc sub
; /* process as child of macro */
124 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
125 struct roffmac
*next
;
129 const char *name
; /* predefined input name */
130 const char *str
; /* replacement symbol */
133 #define PREDEF(__name, __str) \
134 { (__name), (__str) },
136 static enum rofferr
roff_block(ROFF_ARGS
);
137 static enum rofferr
roff_block_text(ROFF_ARGS
);
138 static enum rofferr
roff_block_sub(ROFF_ARGS
);
139 static enum rofferr
roff_cblock(ROFF_ARGS
);
140 static enum rofferr
roff_ccond(ROFF_ARGS
);
141 static enum rofferr
roff_cond(ROFF_ARGS
);
142 static enum rofferr
roff_cond_text(ROFF_ARGS
);
143 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
144 static enum rofferr
roff_ds(ROFF_ARGS
);
145 static enum roffrule
roff_evalcond(const char *, int *);
146 static void roff_freestr(struct roff
*);
147 static char *roff_getname(struct roff
*, char **, int, int);
148 static const char *roff_getstrn(const struct roff
*,
149 const char *, size_t);
150 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
151 static enum rofferr
roff_nr(ROFF_ARGS
);
152 static int roff_res(struct roff
*,
153 char **, size_t *, int);
154 static enum rofferr
roff_rm(ROFF_ARGS
);
155 static void roff_setstr(struct roff
*,
156 const char *, const char *, int);
157 static enum rofferr
roff_so(ROFF_ARGS
);
158 static enum rofferr
roff_TE(ROFF_ARGS
);
159 static enum rofferr
roff_TS(ROFF_ARGS
);
160 static enum rofferr
roff_EQ(ROFF_ARGS
);
161 static enum rofferr
roff_EN(ROFF_ARGS
);
162 static enum rofferr
roff_T_(ROFF_ARGS
);
163 static enum rofferr
roff_userdef(ROFF_ARGS
);
165 /* See roff_hash_find() */
169 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
171 static struct roffmac
*hash
[HASHWIDTH
];
173 static struct roffmac roffs
[ROFF_MAX
] = {
174 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
175 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
176 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
177 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
178 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
179 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
180 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
181 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
182 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
183 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
184 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
185 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
186 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
187 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
188 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
189 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
190 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
191 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
192 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
193 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
194 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
195 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
196 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
197 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
198 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
199 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
200 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
201 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
202 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
203 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
204 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
207 /* Array of injected predefined strings. */
208 #define PREDEFS_MAX 38
209 static const struct predef predefs
[PREDEFS_MAX
] = {
210 #include "predefs.in"
213 static void roff_free1(struct roff
*);
214 static enum rofft
roff_hash_find(const char *, size_t);
215 static void roff_hash_init(void);
216 static void roffnode_cleanscope(struct roff
*);
217 static void roffnode_push(struct roff
*, enum rofft
,
218 const char *, int, int);
219 static void roffnode_pop(struct roff
*);
220 static enum rofft
roff_parse(struct roff
*, const char *, int *);
222 /* See roff_hash_find() */
223 #define ROFF_HASH(p) (p[0] - ASCII_LO)
231 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
232 assert(roffs
[i
].name
[0] >= ASCII_LO
);
233 assert(roffs
[i
].name
[0] <= ASCII_HI
);
235 buc
= ROFF_HASH(roffs
[i
].name
);
237 if (NULL
!= (n
= hash
[buc
])) {
238 for ( ; n
->next
; n
= n
->next
)
242 hash
[buc
] = &roffs
[i
];
247 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
248 * the nil-terminated string name could be found.
251 roff_hash_find(const char *p
, size_t s
)
257 * libroff has an extremely simple hashtable, for the time
258 * being, which simply keys on the first character, which must
259 * be printable, then walks a chain. It works well enough until
263 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
268 if (NULL
== (n
= hash
[buc
]))
270 for ( ; n
; n
= n
->next
)
271 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
272 return((enum rofft
)(n
- roffs
));
279 * Pop the current node off of the stack of roff instructions currently
283 roffnode_pop(struct roff
*r
)
290 r
->last
= r
->last
->parent
;
298 * Push a roff node onto the instruction stack. This must later be
299 * removed with roffnode_pop().
302 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
307 p
= mandoc_calloc(1, sizeof(struct roffnode
));
310 p
->name
= mandoc_strdup(name
);
314 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
321 roff_free1(struct roff
*r
)
326 while (NULL
!= (t
= r
->first_tbl
)) {
327 r
->first_tbl
= t
->next
;
331 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
333 while (NULL
!= (e
= r
->first_eqn
)) {
334 r
->first_eqn
= e
->next
;
338 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
348 roff_reset(struct roff
*r
)
356 roff_free(struct roff
*r
)
365 roff_alloc(struct regset
*regs
, struct mparse
*parse
)
370 r
= mandoc_calloc(1, sizeof(struct roff
));
377 for (i
= 0; i
< PREDEFS_MAX
; i
++)
378 roff_setstr(r
, predefs
[i
].name
, predefs
[i
].str
, 0);
385 * Pre-filter each and every line for reserved words (one beginning with
386 * `\*', e.g., `\*(ab'). These must be handled before the actual line
390 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
392 const char *stesc
; /* start of an escape sequence ('\\') */
393 const char *stnam
; /* start of the name, after "[(*" */
394 const char *cp
; /* end of the name, e.g. before ']' */
395 const char *res
; /* the string to be substituted */
400 /* Search for a leading backslash and save a pointer to it. */
403 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
407 * The second character must be an asterisk.
408 * If it isn't, skip it anyway: It is escaped,
409 * so it can't start another escape sequence.
418 * The third character decides the length
419 * of the name of the string.
420 * Save a pointer to the name.
440 /* Advance to the end of the name. */
442 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
444 return(1); /* Error. */
445 if (0 == maxl
&& ']' == *cp
)
450 * Retrieve the replacement string; if it is
451 * undefined, resume searching for escapes.
454 res
= roff_getstrn(r
, stnam
, (size_t)i
);
461 /* Replace the escape sequence by the string. */
463 nsz
= *szp
+ strlen(res
) + 1;
464 n
= mandoc_malloc(nsz
);
466 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
467 strlcat(n
, res
, nsz
);
468 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
482 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
483 size_t *szp
, int pos
, int *offs
)
490 * Run the reserved-word filter only if we have some reserved
494 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
495 return(ROFF_REPARSE
);
498 ctl
= mandoc_getcontrol(*bufp
, &pos
);
501 * First, if a scope is open and we're not a macro, pass the
502 * text through the macro's filter. If a scope isn't open and
503 * we're not a macro, just let it through.
504 * Finally, if there's an equation scope open, divert it into it
505 * no matter our state.
508 if (r
->last
&& ! ctl
) {
510 assert(roffs
[t
].text
);
512 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
513 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
517 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
));
519 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
523 return(eqn_read(&r
->eqn
, ln
, *bufp
, pos
));
525 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
528 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
));
531 * If a scope is open, go to the child handler for that macro,
532 * as it may want to preprocess before doing anything with it.
533 * Don't do so if an equation is open.
538 assert(roffs
[t
].sub
);
539 return((*roffs
[t
].sub
)
541 ln
, ppos
, pos
, offs
));
545 * Lastly, as we've no scope open, try to look up and execute
546 * the new macro. If no macro is found, simply return and let
547 * the compilers handle it.
550 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
553 assert(roffs
[t
].proc
);
554 return((*roffs
[t
].proc
)
556 ln
, ppos
, pos
, offs
));
561 roff_endparse(struct roff
*r
)
565 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
566 r
->last
->line
, r
->last
->col
, NULL
);
569 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
570 r
->eqn
->eqn
.line
, r
->eqn
->eqn
.pos
, NULL
);
576 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
577 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
584 * Parse a roff node's type from the input buffer. This must be in the
585 * form of ".foo xxx" in the usual way.
588 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
594 if ('\0' == buf
[*pos
] || '"' == 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
);
605 while (buf
[*pos
] && ' ' == buf
[*pos
])
613 roff_cblock(ROFF_ARGS
)
617 * A block-close `..' should only be invoked as a child of an
618 * ignore macro, otherwise raise a warning and just ignore it.
621 if (NULL
== r
->last
) {
622 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
626 switch (r
->last
->tok
) {
634 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
641 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
646 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
649 roffnode_cleanscope(r
);
656 roffnode_cleanscope(struct roff
*r
)
660 if (--r
->last
->endspan
< 0)
669 roff_ccond(ROFF_ARGS
)
672 if (NULL
== r
->last
) {
673 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
677 switch (r
->last
->tok
) {
685 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
689 if (r
->last
->endspan
> -1) {
690 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
695 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
698 roffnode_cleanscope(r
);
705 roff_block(ROFF_ARGS
)
713 if (ROFF_ig
!= tok
) {
714 if ('\0' == (*bufp
)[pos
]) {
715 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
720 * Re-write `de1', since we don't really care about
721 * groff's strange compatibility mode, into `de'.
729 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
732 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
735 while (isspace((unsigned char)(*bufp
)[pos
]))
736 (*bufp
)[pos
++] = '\0';
739 roffnode_push(r
, tok
, name
, ln
, ppos
);
742 * At the beginning of a `de' macro, clear the existing string
743 * with the same name, if there is one. New content will be
744 * added from roff_block_text() in multiline mode.
748 roff_setstr(r
, name
, "", 0);
750 if ('\0' == (*bufp
)[pos
])
753 /* If present, process the custom end-of-line marker. */
756 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
760 * Note: groff does NOT like escape characters in the input.
761 * Instead of detecting this, we're just going to let it fly and
766 sz
= (size_t)(pos
- sv
);
768 if (1 == sz
&& '.' == (*bufp
)[sv
])
771 r
->last
->end
= mandoc_malloc(sz
+ 1);
773 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
774 r
->last
->end
[(int)sz
] = '\0';
777 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
785 roff_block_sub(ROFF_ARGS
)
791 * First check whether a custom macro exists at this level. If
792 * it does, then check against it. This is some of groff's
793 * stranger behaviours. If we encountered a custom end-scope
794 * tag and that tag also happens to be a "real" macro, then we
795 * need to try interpreting it again as a real macro. If it's
796 * not, then return ignore. Else continue.
800 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
801 if ((*bufp
)[i
] != r
->last
->end
[j
])
804 if ('\0' == r
->last
->end
[j
] &&
805 ('\0' == (*bufp
)[i
] ||
807 '\t' == (*bufp
)[i
])) {
809 roffnode_cleanscope(r
);
811 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
815 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
822 * If we have no custom end-query or lookup failed, then try
823 * pulling it out of the hashtable.
826 t
= roff_parse(r
, *bufp
, &pos
);
829 * Macros other than block-end are only significant
830 * in `de' blocks; elsewhere, simply throw them away.
832 if (ROFF_cblock
!= t
) {
834 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
838 assert(roffs
[t
].proc
);
839 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
840 ln
, ppos
, pos
, offs
));
846 roff_block_text(ROFF_ARGS
)
850 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
858 roff_cond_sub(ROFF_ARGS
)
865 roffnode_cleanscope(r
);
868 * If the macro is unknown, first check if it contains a closing
869 * delimiter `\}'. If it does, close out our scope and return
870 * the currently-scoped rule (ignore or continue). Else, drop
871 * into the currently-scoped rule.
874 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
876 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
881 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
882 ln
, pos
, pos
+ 2, offs
);
885 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
889 * A denied conditional must evaluate its children if and only
890 * if they're either structurally required (such as loops and
891 * conditionals) or a closing macro.
894 if (ROFFRULE_DENY
== rr
)
895 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
899 assert(roffs
[t
].proc
);
900 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
901 ln
, ppos
, pos
, offs
));
906 roff_cond_text(ROFF_ARGS
)
912 roffnode_cleanscope(r
);
915 for ( ; NULL
!= (ep
= strchr(ep
, '\\')); ep
++) {
920 roff_ccond(r
, ROFF_ccond
, bufp
, szp
,
921 ln
, pos
, pos
+ 2, offs
);
923 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
927 roff_evalcond(const char *v
, int *pos
)
933 return(ROFFRULE_ALLOW
);
940 return(ROFFRULE_DENY
);
945 while (v
[*pos
] && ' ' != v
[*pos
])
947 return(ROFFRULE_DENY
);
952 roff_line_ignore(ROFF_ARGS
)
956 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
, "it");
969 * An `.el' has no conditional body: it will consume the value
970 * of the current rstack entry set in prior `ie' calls or
973 * If we're not an `el', however, then evaluate the conditional.
976 rule
= ROFF_el
== tok
?
978 ROFFRULE_DENY
: r
->rstack
[r
->rstackpos
--]) :
979 roff_evalcond(*bufp
, &pos
);
982 while (' ' == (*bufp
)[pos
])
986 * Roff is weird. If we have just white-space after the
987 * conditional, it's considered the BODY and we exit without
988 * really doing anything. Warn about this. It's probably
992 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
993 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
997 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
999 r
->last
->rule
= rule
;
1002 * An if-else will put the NEGATION of the current evaluated
1003 * conditional into the stack of rules.
1006 if (ROFF_ie
== tok
) {
1007 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1008 mandoc_msg(MANDOCERR_MEM
,
1009 r
->parse
, ln
, ppos
, NULL
);
1012 r
->rstack
[++r
->rstackpos
] =
1013 ROFFRULE_DENY
== r
->last
->rule
?
1014 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
1017 /* If the parent has false as its rule, then so do we. */
1019 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1020 r
->last
->rule
= ROFFRULE_DENY
;
1023 * Determine scope. If we're invoked with "\{" trailing the
1024 * conditional, then we're in a multiline scope. Else our scope
1025 * expires on the next line.
1028 r
->last
->endspan
= 1;
1030 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1031 r
->last
->endspan
= -1;
1036 * If there are no arguments on the line, the next-line scope is
1040 if ('\0' == (*bufp
)[pos
])
1043 /* Otherwise re-run the roff parser after recalculating. */
1054 char *name
, *string
;
1057 * A symbol is named by the first word following the macro
1058 * invocation up to a space. Its value is anything after the
1059 * name's trailing whitespace and optional double-quote. Thus,
1063 * will have `bar " ' as its value.
1066 string
= *bufp
+ pos
;
1067 name
= roff_getname(r
, &string
, ln
, pos
);
1071 /* Read past initial double-quote. */
1075 /* The rest is the value. */
1076 roff_setstr(r
, name
, string
, 0);
1091 key
= roff_getname(r
, &val
, ln
, pos
);
1094 if (0 == strcmp(key
, "nS")) {
1095 rg
[(int)REG_nS
].set
= 1;
1096 if ((iv
= mandoc_strntou(val
, strlen(val
), 10)) >= 0)
1097 rg
[REG_nS
].v
.u
= (unsigned)iv
;
1099 rg
[(int)REG_nS
].v
.u
= 0u;
1113 while ('\0' != *cp
) {
1114 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1116 roff_setstr(r
, name
, NULL
, 0);
1127 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1141 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1143 tbl_restart(ppos
, ln
, r
->tbl
);
1154 assert(NULL
== r
->eqn
);
1155 e
= eqn_alloc(ppos
, ln
);
1158 r
->last_eqn
->next
= e
;
1160 r
->first_eqn
= r
->last_eqn
= e
;
1162 r
->eqn
= r
->last_eqn
= e
;
1171 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1182 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1186 t
= tbl_alloc(ppos
, ln
, r
->parse
);
1189 r
->last_tbl
->next
= t
;
1191 r
->first_tbl
= r
->last_tbl
= t
;
1193 r
->tbl
= r
->last_tbl
= t
;
1203 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1206 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1207 * opening anything that's not in our cwd or anything beneath
1208 * it. Thus, explicitly disallow traversing up the file-system
1209 * or using absolute paths.
1213 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1214 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1224 roff_userdef(ROFF_ARGS
)
1231 * Collect pointers to macro argument strings
1232 * and null-terminate them.
1235 for (i
= 0; i
< 9; i
++)
1236 arg
[i
] = '\0' == *cp
? "" :
1237 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1240 * Expand macro arguments.
1243 n1
= cp
= mandoc_strdup(r
->current_string
);
1244 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1246 if (0 > i
|| 8 < i
) {
1247 /* Not an argument invocation. */
1252 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1253 n2
= mandoc_malloc(*szp
);
1255 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1256 strlcat(n2
, arg
[i
], *szp
);
1257 strlcat(n2
, cp
+ 3, *szp
);
1259 cp
= n2
+ (cp
- n1
);
1265 * Replace the macro invocation
1266 * by the expanded macro.
1271 *szp
= strlen(*bufp
) + 1;
1273 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1274 ROFF_REPARSE
: ROFF_APPEND
);
1278 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1286 /* Read until end of name. */
1287 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1293 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1298 /* Nil-terminate name. */
1302 /* Read past spaces. */
1311 * Store *string into the user-defined string called *name.
1312 * In multiline mode, append to an existing entry and append '\n';
1313 * else replace the existing entry, if there is one.
1314 * To clear an existing entry, call with (*r, *name, NULL, 0).
1317 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1322 size_t oldch
, newch
;
1324 /* Search for an existing string with the same name. */
1325 n
= r
->first_string
;
1326 while (n
&& strcmp(name
, n
->name
))
1330 /* Create a new string table entry. */
1331 n
= mandoc_malloc(sizeof(struct roffstr
));
1332 n
->name
= mandoc_strdup(name
);
1334 n
->next
= r
->first_string
;
1335 r
->first_string
= n
;
1336 } else if (0 == multiline
) {
1337 /* In multiline mode, append; else replace. */
1346 * One additional byte for the '\n' in multiline mode,
1347 * and one for the terminating '\0'.
1349 newch
= strlen(string
) + (multiline
? 2u : 1u);
1350 if (NULL
== n
->string
) {
1351 n
->string
= mandoc_malloc(newch
);
1355 oldch
= strlen(n
->string
);
1356 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1359 /* Skip existing content in the destination buffer. */
1360 c
= n
->string
+ (int)oldch
;
1362 /* Append new content to the destination buffer. */
1365 * Rudimentary roff copy mode:
1366 * Handle escaped backslashes.
1368 if ('\\' == *string
&& '\\' == *(string
+ 1))
1373 /* Append terminating bytes. */
1380 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1382 const struct roffstr
*n
;
1384 n
= r
->first_string
;
1385 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1388 return(n
? n
->string
: NULL
);
1392 roff_freestr(struct roff
*r
)
1394 struct roffstr
*n
, *nn
;
1396 for (n
= r
->first_string
; n
; n
= nn
) {
1403 r
->first_string
= NULL
;
1406 const struct tbl_span
*
1407 roff_span(const struct roff
*r
)
1410 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1414 roff_eqn(const struct roff
*r
)
1417 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);