]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.107 2010/12/06 13:25:25 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.
32 #include "libmandoc.h"
34 #define RSTACK_MAX 128
37 ('.' == (c) || '\'' == (c))
40 #define ROFF_DEBUG(fmt, args...) \
41 do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
43 #define ROFF_DEBUG(fmt, args...) \
44 do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
68 ROFF_ccond
, /* FIXME: remove this. */
80 char *name
; /* key of symbol */
81 char *string
; /* current value */
82 struct roffstr
*next
; /* next in list */
86 struct roffnode
*last
; /* leaf of stack */
87 mandocmsg msg
; /* err/warn/fatal messages */
88 void *data
; /* privdata for messages */
89 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
90 int rstackpos
; /* position in rstack */
91 struct regset
*regs
; /* read/writable registers */
92 struct roffstr
*first_string
; /* user-defined strings & macros */
93 const char *current_string
; /* value of last called user macro */
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
;
128 static enum rofferr
roff_block(ROFF_ARGS
);
129 static enum rofferr
roff_block_text(ROFF_ARGS
);
130 static enum rofferr
roff_block_sub(ROFF_ARGS
);
131 static enum rofferr
roff_cblock(ROFF_ARGS
);
132 static enum rofferr
roff_ccond(ROFF_ARGS
);
133 static enum rofferr
roff_cond(ROFF_ARGS
);
134 static enum rofferr
roff_cond_text(ROFF_ARGS
);
135 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
136 static enum rofferr
roff_ds(ROFF_ARGS
);
137 static enum roffrule
roff_evalcond(const char *, int *);
138 static void roff_freestr(struct roff
*);
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_line_error(ROFF_ARGS
);
143 static enum rofferr
roff_nr(ROFF_ARGS
);
144 static int roff_res(struct roff
*,
145 char **, size_t *, int);
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_userdef(ROFF_ARGS
);
151 /* See roff_hash_find() */
155 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
157 static struct roffmac
*hash
[HASHWIDTH
];
159 static struct roffmac roffs
[ROFF_MAX
] = {
160 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
161 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
162 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
163 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
165 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
166 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
167 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
168 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
169 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
170 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
171 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
172 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
173 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
174 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
175 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
176 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
177 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
178 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
179 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
180 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
181 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
184 static void roff_free1(struct roff
*);
185 static enum rofft
roff_hash_find(const char *, size_t);
186 static void roff_hash_init(void);
187 static void roffnode_cleanscope(struct roff
*);
188 static void roffnode_push(struct roff
*, enum rofft
,
189 const char *, int, int);
190 static void roffnode_pop(struct roff
*);
191 static enum rofft
roff_parse(struct roff
*, const char *, int *);
192 static int roff_parse_nat(const char *, unsigned int *);
194 /* See roff_hash_find() */
195 #define ROFF_HASH(p) (p[0] - ASCII_LO)
203 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
204 assert(roffs
[i
].name
[0] >= ASCII_LO
);
205 assert(roffs
[i
].name
[0] <= ASCII_HI
);
207 buc
= ROFF_HASH(roffs
[i
].name
);
209 if (NULL
!= (n
= hash
[buc
])) {
210 for ( ; n
->next
; n
= n
->next
)
214 hash
[buc
] = &roffs
[i
];
220 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
221 * the nil-terminated string name could be found.
224 roff_hash_find(const char *p
, size_t s
)
230 * libroff has an extremely simple hashtable, for the time
231 * being, which simply keys on the first character, which must
232 * be printable, then walks a chain. It works well enough until
236 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
241 if (NULL
== (n
= hash
[buc
]))
243 for ( ; n
; n
= n
->next
)
244 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
245 return((enum rofft
)(n
- roffs
));
252 * Pop the current node off of the stack of roff instructions currently
256 roffnode_pop(struct roff
*r
)
263 if (ROFF_el
== p
->tok
)
264 if (r
->rstackpos
> -1)
267 ROFF_DEBUG("roff: popping scope\n");
268 r
->last
= r
->last
->parent
;
276 * Push a roff node onto the instruction stack. This must later be
277 * removed with roffnode_pop().
280 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
285 p
= mandoc_calloc(1, sizeof(struct roffnode
));
288 p
->name
= mandoc_strdup(name
);
292 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
299 roff_free1(struct roff
*r
)
309 roff_reset(struct roff
*r
)
317 roff_free(struct roff
*r
)
326 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
330 r
= mandoc_calloc(1, sizeof(struct roff
));
342 * Pre-filter each and every line for reserved words (one beginning with
343 * `\*', e.g., `\*(ab'). These must be handled before the actual line
347 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
349 const char *cp
, *cpp
, *st
, *res
;
355 for (cp
= &(*bufp
)[pos
]; (cpp
= strstr(cp
, "\\*")); cp
++) {
373 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
375 return(1); /* Error. */
376 if (0 == maxl
&& ']' == *cp
)
380 res
= roff_getstrn(r
, st
, (size_t)i
);
387 ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i
, st
);
389 nsz
= *szp
+ strlen(res
) + 1;
390 n
= mandoc_malloc(nsz
);
394 strlcat(n
, *bufp
, (size_t)(cpp
- *bufp
+ 1));
395 strlcat(n
, res
, nsz
);
396 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
410 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
411 size_t *szp
, int pos
, int *offs
)
417 * Run the reserved-word filter only if we have some reserved
421 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
422 return(ROFF_REPARSE
);
425 * First, if a scope is open and we're not a macro, pass the
426 * text through the macro's filter. If a scope isn't open and
427 * we're not a macro, just let it through.
430 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
432 assert(roffs
[t
].text
);
433 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
434 roffs
[t
].name
, &(*bufp
)[pos
]);
435 return((*roffs
[t
].text
)
437 ln
, pos
, pos
, offs
));
438 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
442 * If a scope is open, go to the child handler for that macro,
443 * as it may want to preprocess before doing anything with it.
448 assert(roffs
[t
].sub
);
449 ROFF_DEBUG("roff: intercept scoped context: %s, [%s]\n",
450 roffs
[t
].name
, &(*bufp
)[pos
]);
451 return((*roffs
[t
].sub
)
453 ln
, pos
, pos
, offs
));
457 * Lastly, as we've no scope open, try to look up and execute
458 * the new macro. If no macro is found, simply return and let
459 * the compilers handle it.
463 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
466 ROFF_DEBUG("roff: intercept new-scope: [%s], [%s]\n",
467 ROFF_USERDEF
== t
? r
->current_string
: roffs
[t
].name
,
470 assert(roffs
[t
].proc
);
471 return((*roffs
[t
].proc
)
473 ln
, ppos
, pos
, offs
));
478 roff_endparse(struct roff
*r
)
483 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
484 r
->last
->col
, NULL
));
489 * Parse a roff node's type from the input buffer. This must be in the
490 * form of ".foo xxx" in the usual way.
493 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
499 assert(ROFF_CTL(buf
[*pos
]));
502 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
505 if ('\0' == buf
[*pos
])
509 maclen
= strcspn(mac
, " \\\t\0");
511 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
512 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
515 while (buf
[*pos
] && ' ' == buf
[*pos
])
523 roff_parse_nat(const char *buf
, unsigned int *res
)
529 lval
= strtol(buf
, &ep
, 10);
530 if (buf
[0] == '\0' || *ep
!= '\0')
532 if ((errno
== ERANGE
&&
533 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
534 (lval
> INT_MAX
|| lval
< 0))
537 *res
= (unsigned int)lval
;
544 roff_cblock(ROFF_ARGS
)
548 * A block-close `..' should only be invoked as a child of an
549 * ignore macro, otherwise raise a warning and just ignore it.
552 if (NULL
== r
->last
) {
553 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
558 switch (r
->last
->tok
) {
572 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
578 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
582 roffnode_cleanscope(r
);
589 roffnode_cleanscope(struct roff
*r
)
593 if (--r
->last
->endspan
< 0)
602 roff_ccond(ROFF_ARGS
)
605 if (NULL
== r
->last
) {
606 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
611 switch (r
->last
->tok
) {
619 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
624 if (r
->last
->endspan
> -1) {
625 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
631 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
635 roffnode_cleanscope(r
);
642 roff_block(ROFF_ARGS
)
650 if (ROFF_ig
!= tok
) {
651 if ('\0' == (*bufp
)[pos
]) {
652 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
657 * Re-write `de1', since we don't really care about
658 * groff's strange compatibility mode, into `de'.
666 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
669 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
672 while (' ' == (*bufp
)[pos
])
673 (*bufp
)[pos
++] = '\0';
676 roffnode_push(r
, tok
, name
, ln
, ppos
);
679 * At the beginning of a `de' macro, clear the existing string
680 * with the same name, if there is one. New content will be
681 * added from roff_block_text() in multiline mode.
685 roff_setstr(r
, name
, NULL
, 0);
687 if ('\0' == (*bufp
)[pos
])
690 /* If present, process the custom end-of-line marker. */
693 while ((*bufp
)[pos
] &&
694 ' ' != (*bufp
)[pos
] &&
695 '\t' != (*bufp
)[pos
])
699 * Note: groff does NOT like escape characters in the input.
700 * Instead of detecting this, we're just going to let it fly and
705 sz
= (size_t)(pos
- sv
);
707 if (1 == sz
&& '.' == (*bufp
)[sv
])
710 r
->last
->end
= mandoc_malloc(sz
+ 1);
712 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
713 r
->last
->end
[(int)sz
] = '\0';
716 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
724 roff_block_sub(ROFF_ARGS
)
730 * First check whether a custom macro exists at this level. If
731 * it does, then check against it. This is some of groff's
732 * stranger behaviours. If we encountered a custom end-scope
733 * tag and that tag also happens to be a "real" macro, then we
734 * need to try interpreting it again as a real macro. If it's
735 * not, then return ignore. Else continue.
740 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
743 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
744 if ((*bufp
)[i
] != r
->last
->end
[j
])
747 if ('\0' == r
->last
->end
[j
] &&
748 ('\0' == (*bufp
)[i
] ||
750 '\t' == (*bufp
)[i
])) {
752 roffnode_cleanscope(r
);
754 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
761 * If we have no custom end-query or lookup failed, then try
762 * pulling it out of the hashtable.
766 t
= roff_parse(r
, *bufp
, &pos
);
769 * Macros other than block-end are only significant
770 * in `de' blocks; elsewhere, simply throw them away.
772 if (ROFF_cblock
!= t
) {
774 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
778 assert(roffs
[t
].proc
);
779 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
780 ln
, ppos
, pos
, offs
));
786 roff_block_text(ROFF_ARGS
)
790 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
798 roff_cond_sub(ROFF_ARGS
)
807 * Clean out scope. If we've closed ourselves, then don't
811 roffnode_cleanscope(r
);
813 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
814 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
816 (r
, ROFF_ccond
, bufp
, szp
,
817 ln
, pos
, pos
+ 2, offs
));
818 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
822 * A denied conditional must evaluate its children if and only
823 * if they're either structurally required (such as loops and
824 * conditionals) or a closing macro.
826 if (ROFFRULE_DENY
== rr
)
827 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
831 assert(roffs
[t
].proc
);
832 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
833 ln
, ppos
, pos
, offs
));
839 roff_cond_text(ROFF_ARGS
)
847 * We display the value of the text if out current evaluation
848 * scope permits us to do so.
851 /* FIXME: use roff_ccond? */
854 if (NULL
== (ep
= strstr(st
, "\\}"))) {
855 roffnode_cleanscope(r
);
856 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
859 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
862 roffnode_cleanscope(r
);
863 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
868 roff_evalcond(const char *v
, int *pos
)
874 return(ROFFRULE_ALLOW
);
881 return(ROFFRULE_DENY
);
886 while (v
[*pos
] && ' ' != v
[*pos
])
888 return(ROFFRULE_DENY
);
893 roff_line_ignore(ROFF_ARGS
)
901 roff_line_error(ROFF_ARGS
)
904 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
915 /* Stack overflow! */
917 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
918 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
922 /* First, evaluate the conditional. */
924 if (ROFF_el
== tok
) {
926 * An `.el' will get the value of the current rstack
927 * entry set in prior `ie' calls or defaults to DENY.
929 if (r
->rstackpos
< 0)
930 rule
= ROFFRULE_DENY
;
932 rule
= r
->rstack
[r
->rstackpos
];
934 rule
= roff_evalcond(*bufp
, &pos
);
938 while (' ' == (*bufp
)[pos
])
942 * Roff is weird. If we have just white-space after the
943 * conditional, it's considered the BODY and we exit without
944 * really doing anything. Warn about this. It's probably
948 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
949 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
953 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
955 r
->last
->rule
= rule
;
957 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
958 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
960 if (ROFF_ie
== tok
) {
962 * An if-else will put the NEGATION of the current
963 * evaluated conditional into the stack.
966 if (ROFFRULE_DENY
== r
->last
->rule
)
967 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
969 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
972 /* If the parent has false as its rule, then so do we. */
974 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
975 r
->last
->rule
= ROFFRULE_DENY
;
976 ROFF_DEBUG("roff: cond override: %s -> deny\n",
981 * Determine scope. If we're invoked with "\{" trailing the
982 * conditional, then we're in a multiline scope. Else our scope
983 * expires on the next line.
986 r
->last
->endspan
= 1;
988 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
989 r
->last
->endspan
= -1;
991 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
994 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
998 * If there are no arguments on the line, the next-line scope is
1002 if ('\0' == (*bufp
)[pos
])
1005 /* Otherwise re-run the roff parser after recalculating. */
1016 char *name
, *string
;
1019 * A symbol is named by the first word following the macro
1020 * invocation up to a space. Its value is anything after the
1021 * name's trailing whitespace and optional double-quote. Thus,
1025 * will have `bar " ' as its value.
1033 /* Read until end of name. */
1034 while (*string
&& ' ' != *string
)
1037 /* Nil-terminate name. */
1041 /* Read past spaces. */
1042 while (*string
&& ' ' == *string
)
1045 /* Read passed initial double-quote. */
1046 if (*string
&& '"' == *string
)
1049 /* The rest is the value. */
1050 roff_setstr(r
, name
, string
, 0);
1059 const char *key
, *val
;
1062 key
= &(*bufp
)[pos
];
1065 /* Parse register request. */
1066 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1070 * Set our nil terminator. Because this line is going to be
1071 * ignored anyway, we can munge it as we please.
1074 (*bufp
)[pos
++] = '\0';
1076 /* Skip whitespace to register token. */
1077 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1080 val
= &(*bufp
)[pos
];
1082 /* Process register token. */
1084 if (0 == strcmp(key
, "nS")) {
1085 rg
[(int)REG_nS
].set
= 1;
1086 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1087 rg
[(int)REG_nS
].v
.u
= 0;
1089 ROFF_DEBUG("roff: register nS: %u\n",
1090 rg
[(int)REG_nS
].v
.u
);
1092 ROFF_DEBUG("roff: ignoring register: %s\n", key
);
1103 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1106 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1107 * opening anything that's not in our cwd or anything beneath
1108 * it. Thus, explicitly disallow traversing up the file-system
1109 * or using absolute paths.
1113 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1114 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1124 roff_userdef(ROFF_ARGS
)
1128 int i
, quoted
, pairs
;
1131 * Collect pointers to macro argument strings
1132 * and null-terminate them.
1135 for (i
= 0; i
< 9; i
++) {
1136 /* Quoting can only start with a new word. */
1143 for (pairs
= 0; '\0' != *cp
; cp
++) {
1144 /* Unquoted arguments end at blanks. */
1150 /* After pairs of quotes, move left. */
1153 /* Pairs of quotes do not end words, ... */
1154 if ('"' == cp
[0] && '"' == cp
[1]) {
1159 /* ... but solitary quotes do. */
1167 /* Last argument; the remaining ones are empty strings. */
1170 /* Null-terminate argument and move to the next one. */
1177 * Expand macro arguments.
1180 n1
= cp
= mandoc_strdup(r
->current_string
);
1181 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1183 if (0 > i
|| 8 < i
) {
1184 /* Not an argument invocation. */
1189 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1190 n2
= mandoc_malloc(*szp
);
1192 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1193 strlcat(n2
, arg
[i
], *szp
);
1194 strlcat(n2
, cp
+ 3, *szp
);
1196 cp
= n2
+ (cp
- n1
);
1202 * Replace the macro invocation
1203 * by the expanded macro.
1208 *szp
= strlen(*bufp
) + 1;
1210 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1211 ROFF_REPARSE
: ROFF_APPEND
);
1215 * Store *string into the user-defined string called *name.
1216 * In multiline mode, append to an existing entry and append '\n';
1217 * else replace the existing entry, if there is one.
1218 * To clear an existing entry, call with (*r, *name, NULL, 0).
1221 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1226 size_t oldch
, newch
;
1228 /* Search for an existing string with the same name. */
1229 n
= r
->first_string
;
1230 while (n
&& strcmp(name
, n
->name
))
1234 /* Create a new string table entry. */
1235 n
= mandoc_malloc(sizeof(struct roffstr
));
1236 n
->name
= mandoc_strdup(name
);
1238 n
->next
= r
->first_string
;
1239 r
->first_string
= n
;
1240 } else if (0 == multiline
) {
1241 /* In multiline mode, append; else replace. */
1250 * One additional byte for the '\n' in multiline mode,
1251 * and one for the terminating '\0'.
1253 newch
= strlen(string
) + (multiline
? 2 : 1);
1254 if (NULL
== n
->string
) {
1255 n
->string
= mandoc_malloc(newch
);
1259 oldch
= strlen(n
->string
);
1260 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1263 /* Skip existing content in the destination buffer. */
1264 c
= n
->string
+ oldch
;
1266 /* Append new content to the destination buffer. */
1269 * Rudimentary roff copy mode:
1270 * Handle escaped backslashes.
1272 if ('\\' == *string
&& '\\' == *(string
+ 1))
1277 /* Append terminating bytes. */
1285 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1287 const struct roffstr
*n
;
1289 n
= r
->first_string
;
1290 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1293 return(n
? n
->string
: NULL
);
1298 roff_freestr(struct roff
*r
)
1300 struct roffstr
*n
, *nn
;
1302 for (n
= r
->first_string
; n
; n
= nn
) {
1309 r
->first_string
= NULL
;