]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.112 2010/12/29 14:53:31 kristaps Exp $ */
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 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))
64 ROFF_ccond
, /* FIXME: remove this. */
75 char *name
; /* key of symbol */
76 char *string
; /* current value */
77 struct roffstr
*next
; /* next in list */
81 struct roffnode
*last
; /* leaf of stack */
82 mandocmsg msg
; /* err/warn/fatal messages */
83 void *data
; /* privdata for messages */
84 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
85 int rstackpos
; /* position in rstack */
86 struct regset
*regs
; /* read/writable registers */
87 struct roffstr
*first_string
; /* user-defined strings & macros */
88 const char *current_string
; /* value of last called user macro */
93 enum rofft tok
; /* type of node */
94 struct roffnode
*parent
; /* up one in stack */
95 int line
; /* parse line */
96 int col
; /* parse col */
97 char *name
; /* node name, e.g. macro name */
98 char *end
; /* end-rules: custom token */
99 int endspan
; /* end-rules: next-line or infty */
100 enum roffrule rule
; /* current evaluation rule */
103 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
104 enum rofft tok, /* tok of macro */ \
105 char **bufp, /* input buffer */ \
106 size_t *szp, /* size of input buffer */ \
107 int ln, /* parse line */ \
108 int ppos, /* original pos in buffer */ \
109 int pos, /* current pos in buffer */ \
110 int *offs /* reset offset of buffer data */
112 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
115 const char *name
; /* macro name */
116 roffproc proc
; /* process new macro */
117 roffproc text
; /* process as child text of macro */
118 roffproc sub
; /* process as child of macro */
120 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
121 struct roffmac
*next
;
124 static enum rofferr
roff_block(ROFF_ARGS
);
125 static enum rofferr
roff_block_text(ROFF_ARGS
);
126 static enum rofferr
roff_block_sub(ROFF_ARGS
);
127 static enum rofferr
roff_cblock(ROFF_ARGS
);
128 static enum rofferr
roff_ccond(ROFF_ARGS
);
129 static enum rofferr
roff_cond(ROFF_ARGS
);
130 static enum rofferr
roff_cond_text(ROFF_ARGS
);
131 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
132 static enum rofferr
roff_ds(ROFF_ARGS
);
133 static enum roffrule
roff_evalcond(const char *, int *);
134 static void roff_freestr(struct roff
*);
135 static const char *roff_getstrn(const struct roff
*,
136 const char *, size_t);
137 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
138 static enum rofferr
roff_line_error(ROFF_ARGS
);
139 static enum rofferr
roff_nr(ROFF_ARGS
);
140 static int roff_res(struct roff
*,
141 char **, size_t *, int);
142 static void roff_setstr(struct roff
*,
143 const char *, const char *, int);
144 static enum rofferr
roff_so(ROFF_ARGS
);
145 static enum rofferr
roff_TE(ROFF_ARGS
);
146 static enum rofferr
roff_TS(ROFF_ARGS
);
147 static enum rofferr
roff_T_(ROFF_ARGS
);
148 static enum rofferr
roff_userdef(ROFF_ARGS
);
150 /* See roff_hash_find() */
154 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
156 static struct roffmac
*hash
[HASHWIDTH
];
158 static struct roffmac roffs
[ROFF_MAX
] = {
159 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
160 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
161 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
162 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
163 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
165 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
166 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
167 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
168 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
169 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
170 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
171 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
172 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
173 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
174 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
175 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
176 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
177 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
178 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
179 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
180 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
181 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
182 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
183 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
186 static void roff_free1(struct roff
*);
187 static enum rofft
roff_hash_find(const char *, size_t);
188 static void roff_hash_init(void);
189 static void roffnode_cleanscope(struct roff
*);
190 static void roffnode_push(struct roff
*, enum rofft
,
191 const char *, int, int);
192 static void roffnode_pop(struct roff
*);
193 static enum rofft
roff_parse(struct roff
*, const char *, int *);
194 static int roff_parse_nat(const char *, unsigned int *);
196 /* See roff_hash_find() */
197 #define ROFF_HASH(p) (p[0] - ASCII_LO)
205 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
206 assert(roffs
[i
].name
[0] >= ASCII_LO
);
207 assert(roffs
[i
].name
[0] <= ASCII_HI
);
209 buc
= ROFF_HASH(roffs
[i
].name
);
211 if (NULL
!= (n
= hash
[buc
])) {
212 for ( ; n
->next
; n
= n
->next
)
216 hash
[buc
] = &roffs
[i
];
222 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
223 * the nil-terminated string name could be found.
226 roff_hash_find(const char *p
, size_t s
)
232 * libroff has an extremely simple hashtable, for the time
233 * being, which simply keys on the first character, which must
234 * be printable, then walks a chain. It works well enough until
238 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
243 if (NULL
== (n
= hash
[buc
]))
245 for ( ; n
; n
= n
->next
)
246 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
247 return((enum rofft
)(n
- roffs
));
254 * Pop the current node off of the stack of roff instructions currently
258 roffnode_pop(struct roff
*r
)
265 if (ROFF_el
== p
->tok
)
266 if (r
->rstackpos
> -1)
269 r
->last
= r
->last
->parent
;
277 * Push a roff node onto the instruction stack. This must later be
278 * removed with roffnode_pop().
281 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
286 p
= mandoc_calloc(1, sizeof(struct roffnode
));
289 p
->name
= mandoc_strdup(name
);
293 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
300 roff_free1(struct roff
*r
)
316 roff_reset(struct roff
*r
)
324 roff_free(struct roff
*r
)
333 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
337 r
= mandoc_calloc(1, sizeof(struct roff
));
349 * Pre-filter each and every line for reserved words (one beginning with
350 * `\*', e.g., `\*(ab'). These must be handled before the actual line
354 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
356 const char *stesc
; /* start of an escape sequence ('\\') */
357 const char *stnam
; /* start of the name, after "[(*" */
358 const char *cp
; /* end of the name, e.g. before ']' */
359 const char *res
; /* the string to be substituted */
364 /* Search for a leading backslash and save a pointer to it. */
367 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
371 * The second character must be an asterisk.
372 * If it isn't, skip it anyway: It is escaped,
373 * so it can't start another escape sequence.
382 * The third character decides the length
383 * of the name of the string.
384 * Save a pointer to the name.
404 /* Advance to the end of the name. */
406 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
408 return(1); /* Error. */
409 if (0 == maxl
&& ']' == *cp
)
414 * Retrieve the replacement string; if it is
415 * undefined, resume searching for escapes.
418 res
= roff_getstrn(r
, stnam
, (size_t)i
);
425 /* Replace the escape sequence by the string. */
427 nsz
= *szp
+ strlen(res
) + 1;
428 n
= mandoc_malloc(nsz
);
430 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
431 strlcat(n
, res
, nsz
);
432 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
446 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
447 size_t *szp
, int pos
, int *offs
)
454 * Run the reserved-word filter only if we have some reserved
458 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
459 return(ROFF_REPARSE
);
462 * First, if a scope is open and we're not a macro, pass the
463 * text through the macro's filter. If a scope isn't open and
464 * we're not a macro, just let it through.
467 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
469 assert(roffs
[t
].text
);
471 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
472 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
473 if (ROFF_CONT
== e
&& r
->tbl
)
474 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
476 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
478 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
483 * If a scope is open, go to the child handler for that macro,
484 * as it may want to preprocess before doing anything with it.
489 assert(roffs
[t
].sub
);
490 return((*roffs
[t
].sub
)
492 ln
, pos
, pos
, offs
));
496 * Lastly, as we've no scope open, try to look up and execute
497 * the new macro. If no macro is found, simply return and let
498 * the compilers handle it.
502 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
505 assert(roffs
[t
].proc
);
506 return((*roffs
[t
].proc
)
508 ln
, ppos
, pos
, offs
));
513 roff_endparse(struct roff
*r
)
516 /* FIXME: if r->tbl */
518 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
519 r
->last
->line
, r
->last
->col
, NULL
);
525 * Parse a roff node's type from the input buffer. This must be in the
526 * form of ".foo xxx" in the usual way.
529 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
535 assert(ROFF_CTL(buf
[*pos
]));
538 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
541 if ('\0' == buf
[*pos
])
545 maclen
= strcspn(mac
, " \\\t\0");
547 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
548 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
551 while (buf
[*pos
] && ' ' == buf
[*pos
])
559 roff_parse_nat(const char *buf
, unsigned int *res
)
565 lval
= strtol(buf
, &ep
, 10);
566 if (buf
[0] == '\0' || *ep
!= '\0')
568 if ((errno
== ERANGE
&&
569 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
570 (lval
> INT_MAX
|| lval
< 0))
573 *res
= (unsigned int)lval
;
580 roff_cblock(ROFF_ARGS
)
584 * A block-close `..' should only be invoked as a child of an
585 * ignore macro, otherwise raise a warning and just ignore it.
588 if (NULL
== r
->last
) {
589 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
593 switch (r
->last
->tok
) {
601 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
608 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
613 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
616 roffnode_cleanscope(r
);
623 roffnode_cleanscope(struct roff
*r
)
627 if (--r
->last
->endspan
< 0)
636 roff_ccond(ROFF_ARGS
)
639 if (NULL
== r
->last
) {
640 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
644 switch (r
->last
->tok
) {
652 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
656 if (r
->last
->endspan
> -1) {
657 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
662 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
665 roffnode_cleanscope(r
);
672 roff_block(ROFF_ARGS
)
680 if (ROFF_ig
!= tok
) {
681 if ('\0' == (*bufp
)[pos
]) {
682 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
687 * Re-write `de1', since we don't really care about
688 * groff's strange compatibility mode, into `de'.
696 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
699 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
702 while (' ' == (*bufp
)[pos
])
703 (*bufp
)[pos
++] = '\0';
706 roffnode_push(r
, tok
, name
, ln
, ppos
);
709 * At the beginning of a `de' macro, clear the existing string
710 * with the same name, if there is one. New content will be
711 * added from roff_block_text() in multiline mode.
715 roff_setstr(r
, name
, "", 0);
717 if ('\0' == (*bufp
)[pos
])
720 /* If present, process the custom end-of-line marker. */
723 while ((*bufp
)[pos
] &&
724 ' ' != (*bufp
)[pos
] &&
725 '\t' != (*bufp
)[pos
])
729 * Note: groff does NOT like escape characters in the input.
730 * Instead of detecting this, we're just going to let it fly and
735 sz
= (size_t)(pos
- sv
);
737 if (1 == sz
&& '.' == (*bufp
)[sv
])
740 r
->last
->end
= mandoc_malloc(sz
+ 1);
742 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
743 r
->last
->end
[(int)sz
] = '\0';
746 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
754 roff_block_sub(ROFF_ARGS
)
760 * First check whether a custom macro exists at this level. If
761 * it does, then check against it. This is some of groff's
762 * stranger behaviours. If we encountered a custom end-scope
763 * tag and that tag also happens to be a "real" macro, then we
764 * need to try interpreting it again as a real macro. If it's
765 * not, then return ignore. Else continue.
770 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
773 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
774 if ((*bufp
)[i
] != r
->last
->end
[j
])
777 if ('\0' == r
->last
->end
[j
] &&
778 ('\0' == (*bufp
)[i
] ||
780 '\t' == (*bufp
)[i
])) {
782 roffnode_cleanscope(r
);
784 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
791 * If we have no custom end-query or lookup failed, then try
792 * pulling it out of the hashtable.
796 t
= roff_parse(r
, *bufp
, &pos
);
799 * Macros other than block-end are only significant
800 * in `de' blocks; elsewhere, simply throw them away.
802 if (ROFF_cblock
!= t
) {
804 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
808 assert(roffs
[t
].proc
);
809 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
810 ln
, ppos
, pos
, offs
));
816 roff_block_text(ROFF_ARGS
)
820 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
828 roff_cond_sub(ROFF_ARGS
)
837 * Clean out scope. If we've closed ourselves, then don't
841 roffnode_cleanscope(r
);
843 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
844 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
846 (r
, ROFF_ccond
, bufp
, szp
,
847 ln
, pos
, pos
+ 2, offs
));
848 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
852 * A denied conditional must evaluate its children if and only
853 * if they're either structurally required (such as loops and
854 * conditionals) or a closing macro.
856 if (ROFFRULE_DENY
== rr
)
857 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
861 assert(roffs
[t
].proc
);
862 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
863 ln
, ppos
, pos
, offs
));
869 roff_cond_text(ROFF_ARGS
)
877 * We display the value of the text if out current evaluation
878 * scope permits us to do so.
881 /* FIXME: use roff_ccond? */
884 if (NULL
== (ep
= strstr(st
, "\\}"))) {
885 roffnode_cleanscope(r
);
886 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
889 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
892 roffnode_cleanscope(r
);
893 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
898 roff_evalcond(const char *v
, int *pos
)
904 return(ROFFRULE_ALLOW
);
911 return(ROFFRULE_DENY
);
916 while (v
[*pos
] && ' ' != v
[*pos
])
918 return(ROFFRULE_DENY
);
923 roff_line_ignore(ROFF_ARGS
)
931 roff_line_error(ROFF_ARGS
)
934 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
945 /* Stack overflow! */
947 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
948 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
952 /* First, evaluate the conditional. */
954 if (ROFF_el
== tok
) {
956 * An `.el' will get the value of the current rstack
957 * entry set in prior `ie' calls or defaults to DENY.
959 if (r
->rstackpos
< 0)
960 rule
= ROFFRULE_DENY
;
962 rule
= r
->rstack
[r
->rstackpos
];
964 rule
= roff_evalcond(*bufp
, &pos
);
968 while (' ' == (*bufp
)[pos
])
972 * Roff is weird. If we have just white-space after the
973 * conditional, it's considered the BODY and we exit without
974 * really doing anything. Warn about this. It's probably
978 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
979 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
983 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
985 r
->last
->rule
= rule
;
987 if (ROFF_ie
== tok
) {
989 * An if-else will put the NEGATION of the current
990 * evaluated conditional into the stack.
993 if (ROFFRULE_DENY
== r
->last
->rule
)
994 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
996 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
999 /* If the parent has false as its rule, then so do we. */
1001 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1002 r
->last
->rule
= ROFFRULE_DENY
;
1005 * Determine scope. If we're invoked with "\{" trailing the
1006 * conditional, then we're in a multiline scope. Else our scope
1007 * expires on the next line.
1010 r
->last
->endspan
= 1;
1012 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1013 r
->last
->endspan
= -1;
1018 * If there are no arguments on the line, the next-line scope is
1022 if ('\0' == (*bufp
)[pos
])
1025 /* Otherwise re-run the roff parser after recalculating. */
1036 char *name
, *string
;
1039 * A symbol is named by the first word following the macro
1040 * invocation up to a space. Its value is anything after the
1041 * name's trailing whitespace and optional double-quote. Thus,
1045 * will have `bar " ' as its value.
1053 /* Read until end of name. */
1054 while (*string
&& ' ' != *string
)
1057 /* Nil-terminate name. */
1061 /* Read past spaces. */
1062 while (*string
&& ' ' == *string
)
1065 /* Read passed initial double-quote. */
1066 if (*string
&& '"' == *string
)
1069 /* The rest is the value. */
1070 roff_setstr(r
, name
, string
, 0);
1079 const char *key
, *val
;
1082 key
= &(*bufp
)[pos
];
1085 /* Parse register request. */
1086 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1090 * Set our nil terminator. Because this line is going to be
1091 * ignored anyway, we can munge it as we please.
1094 (*bufp
)[pos
++] = '\0';
1096 /* Skip whitespace to register token. */
1097 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1100 val
= &(*bufp
)[pos
];
1102 /* Process register token. */
1104 if (0 == strcmp(key
, "nS")) {
1105 rg
[(int)REG_nS
].set
= 1;
1106 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1107 rg
[(int)REG_nS
].v
.u
= 0;
1119 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1133 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1135 tbl_restart(r
->tbl
);
1146 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1149 r
->tbl
= tbl_alloc(r
->data
, r
->msg
);
1160 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1163 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1164 * opening anything that's not in our cwd or anything beneath
1165 * it. Thus, explicitly disallow traversing up the file-system
1166 * or using absolute paths.
1170 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1171 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1181 roff_userdef(ROFF_ARGS
)
1185 int i
, quoted
, pairs
;
1188 * Collect pointers to macro argument strings
1189 * and null-terminate them.
1192 for (i
= 0; i
< 9; i
++) {
1193 /* Quoting can only start with a new word. */
1200 for (pairs
= 0; '\0' != *cp
; cp
++) {
1201 /* Unquoted arguments end at blanks. */
1207 /* After pairs of quotes, move left. */
1210 /* Pairs of quotes do not end words, ... */
1211 if ('"' == cp
[0] && '"' == cp
[1]) {
1216 /* ... but solitary quotes do. */
1224 /* Last argument; the remaining ones are empty strings. */
1227 /* Null-terminate argument and move to the next one. */
1234 * Expand macro arguments.
1237 n1
= cp
= mandoc_strdup(r
->current_string
);
1238 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1240 if (0 > i
|| 8 < i
) {
1241 /* Not an argument invocation. */
1246 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1247 n2
= mandoc_malloc(*szp
);
1249 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1250 strlcat(n2
, arg
[i
], *szp
);
1251 strlcat(n2
, cp
+ 3, *szp
);
1253 cp
= n2
+ (cp
- n1
);
1259 * Replace the macro invocation
1260 * by the expanded macro.
1265 *szp
= strlen(*bufp
) + 1;
1267 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1268 ROFF_REPARSE
: ROFF_APPEND
);
1272 * Store *string into the user-defined string called *name.
1273 * In multiline mode, append to an existing entry and append '\n';
1274 * else replace the existing entry, if there is one.
1275 * To clear an existing entry, call with (*r, *name, NULL, 0).
1278 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1283 size_t oldch
, newch
;
1285 /* Search for an existing string with the same name. */
1286 n
= r
->first_string
;
1287 while (n
&& strcmp(name
, n
->name
))
1291 /* Create a new string table entry. */
1292 n
= mandoc_malloc(sizeof(struct roffstr
));
1293 n
->name
= mandoc_strdup(name
);
1295 n
->next
= r
->first_string
;
1296 r
->first_string
= n
;
1297 } else if (0 == multiline
) {
1298 /* In multiline mode, append; else replace. */
1307 * One additional byte for the '\n' in multiline mode,
1308 * and one for the terminating '\0'.
1310 newch
= strlen(string
) + (multiline
? 2 : 1);
1311 if (NULL
== n
->string
) {
1312 n
->string
= mandoc_malloc(newch
);
1316 oldch
= strlen(n
->string
);
1317 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1320 /* Skip existing content in the destination buffer. */
1321 c
= n
->string
+ oldch
;
1323 /* Append new content to the destination buffer. */
1326 * Rudimentary roff copy mode:
1327 * Handle escaped backslashes.
1329 if ('\\' == *string
&& '\\' == *(string
+ 1))
1334 /* Append terminating bytes. */
1342 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1344 const struct roffstr
*n
;
1346 n
= r
->first_string
;
1347 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1350 return(n
? n
->string
: NULL
);
1355 roff_freestr(struct roff
*r
)
1357 struct roffstr
*n
, *nn
;
1359 for (n
= r
->first_string
; n
; n
= nn
) {
1366 r
->first_string
= NULL
;