]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.109 2010/12/28 10:51:03 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))
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 roffnode
*last
; /* leaf of stack */
81 mandocmsg msg
; /* err/warn/fatal messages */
82 void *data
; /* privdata for messages */
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 */
92 enum rofft tok
; /* type of node */
93 struct roffnode
*parent
; /* up one in stack */
94 int line
; /* parse line */
95 int col
; /* parse col */
96 char *name
; /* node name, e.g. macro name */
97 char *end
; /* end-rules: custom token */
98 int endspan
; /* end-rules: next-line or infty */
99 enum roffrule rule
; /* current evaluation rule */
102 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
103 enum rofft tok, /* tok of macro */ \
104 char **bufp, /* input buffer */ \
105 size_t *szp, /* size of input buffer */ \
106 int ln, /* parse line */ \
107 int ppos, /* original pos in buffer */ \
108 int pos, /* current pos in buffer */ \
109 int *offs /* reset offset of buffer data */
111 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
114 const char *name
; /* macro name */
115 roffproc proc
; /* process new macro */
116 roffproc text
; /* process as child text of macro */
117 roffproc sub
; /* process as child of macro */
119 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
120 struct roffmac
*next
;
123 static enum rofferr
roff_block(ROFF_ARGS
);
124 static enum rofferr
roff_block_text(ROFF_ARGS
);
125 static enum rofferr
roff_block_sub(ROFF_ARGS
);
126 static enum rofferr
roff_cblock(ROFF_ARGS
);
127 static enum rofferr
roff_ccond(ROFF_ARGS
);
128 static enum rofferr
roff_cond(ROFF_ARGS
);
129 static enum rofferr
roff_cond_text(ROFF_ARGS
);
130 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
131 static enum rofferr
roff_ds(ROFF_ARGS
);
132 static enum roffrule
roff_evalcond(const char *, int *);
133 static void roff_freestr(struct roff
*);
134 static const char *roff_getstrn(const struct roff
*,
135 const char *, size_t);
136 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
137 static enum rofferr
roff_line_error(ROFF_ARGS
);
138 static enum rofferr
roff_nr(ROFF_ARGS
);
139 static int roff_res(struct roff
*,
140 char **, size_t *, int);
141 static void roff_setstr(struct roff
*,
142 const char *, const char *, int);
143 static enum rofferr
roff_so(ROFF_ARGS
);
144 static enum rofferr
roff_TE(ROFF_ARGS
);
145 static enum rofferr
roff_TS(ROFF_ARGS
);
146 static enum rofferr
roff_userdef(ROFF_ARGS
);
148 /* See roff_hash_find() */
152 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
154 static struct roffmac
*hash
[HASHWIDTH
];
156 static struct roffmac roffs
[ROFF_MAX
] = {
157 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
158 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
159 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
160 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
161 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
162 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
163 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
165 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
166 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
167 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
168 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
169 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
170 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
171 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
172 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
173 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
174 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
175 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
176 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
177 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
178 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
179 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
180 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
183 static void roff_free1(struct roff
*);
184 static enum rofft
roff_hash_find(const char *, size_t);
185 static void roff_hash_init(void);
186 static void roffnode_cleanscope(struct roff
*);
187 static void roffnode_push(struct roff
*, enum rofft
,
188 const char *, int, int);
189 static void roffnode_pop(struct roff
*);
190 static enum rofft
roff_parse(struct roff
*, const char *, int *);
191 static int roff_parse_nat(const char *, unsigned int *);
193 /* See roff_hash_find() */
194 #define ROFF_HASH(p) (p[0] - ASCII_LO)
202 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
203 assert(roffs
[i
].name
[0] >= ASCII_LO
);
204 assert(roffs
[i
].name
[0] <= ASCII_HI
);
206 buc
= ROFF_HASH(roffs
[i
].name
);
208 if (NULL
!= (n
= hash
[buc
])) {
209 for ( ; n
->next
; n
= n
->next
)
213 hash
[buc
] = &roffs
[i
];
219 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
220 * the nil-terminated string name could be found.
223 roff_hash_find(const char *p
, size_t s
)
229 * libroff has an extremely simple hashtable, for the time
230 * being, which simply keys on the first character, which must
231 * be printable, then walks a chain. It works well enough until
235 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
240 if (NULL
== (n
= hash
[buc
]))
242 for ( ; n
; n
= n
->next
)
243 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
244 return((enum rofft
)(n
- roffs
));
251 * Pop the current node off of the stack of roff instructions currently
255 roffnode_pop(struct roff
*r
)
262 if (ROFF_el
== p
->tok
)
263 if (r
->rstackpos
> -1)
266 r
->last
= r
->last
->parent
;
274 * Push a roff node onto the instruction stack. This must later be
275 * removed with roffnode_pop().
278 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
283 p
= mandoc_calloc(1, sizeof(struct roffnode
));
286 p
->name
= mandoc_strdup(name
);
290 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
297 roff_free1(struct roff
*r
)
313 roff_reset(struct roff
*r
)
321 roff_free(struct roff
*r
)
330 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
334 r
= mandoc_calloc(1, sizeof(struct roff
));
346 * Pre-filter each and every line for reserved words (one beginning with
347 * `\*', e.g., `\*(ab'). These must be handled before the actual line
351 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
353 const char *stesc
; /* start of an escape sequence ('\\') */
354 const char *stnam
; /* start of the name, after "[(*" */
355 const char *cp
; /* end of the name, e.g. before ']' */
356 const char *res
; /* the string to be substituted */
361 /* Search for a leading backslash and save a pointer to it. */
364 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
368 * The second character must be an asterisk.
369 * If it isn't, skip it anyway: It is escaped,
370 * so it can't start another escape sequence.
379 * The third character decides the length
380 * of the name of the string.
381 * Save a pointer to the name.
401 /* Advance to the end of the name. */
403 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
405 return(1); /* Error. */
406 if (0 == maxl
&& ']' == *cp
)
411 * Retrieve the replacement string; if it is
412 * undefined, resume searching for escapes.
415 res
= roff_getstrn(r
, stnam
, (size_t)i
);
422 /* Replace the escape sequence by the string. */
424 nsz
= *szp
+ strlen(res
) + 1;
425 n
= mandoc_malloc(nsz
);
427 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
428 strlcat(n
, res
, nsz
);
429 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
443 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
444 size_t *szp
, int pos
, int *offs
)
451 * Run the reserved-word filter only if we have some reserved
455 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
456 return(ROFF_REPARSE
);
459 * First, if a scope is open and we're not a macro, pass the
460 * text through the macro's filter. If a scope isn't open and
461 * we're not a macro, just let it through.
464 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
466 assert(roffs
[t
].text
);
468 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
469 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
470 if (ROFF_CONT
== e
&& r
->tbl
)
471 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
473 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
475 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
480 * If a scope is open, go to the child handler for that macro,
481 * as it may want to preprocess before doing anything with it.
486 assert(roffs
[t
].sub
);
487 return((*roffs
[t
].sub
)
489 ln
, pos
, pos
, offs
));
493 * Lastly, as we've no scope open, try to look up and execute
494 * the new macro. If no macro is found, simply return and let
495 * the compilers handle it.
499 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
502 assert(roffs
[t
].proc
);
503 return((*roffs
[t
].proc
)
505 ln
, ppos
, pos
, offs
));
510 roff_endparse(struct roff
*r
)
513 if (r
->last
|| r
->tbl
)
514 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
515 r
->last
->line
, r
->last
->col
, NULL
);
521 * Parse a roff node's type from the input buffer. This must be in the
522 * form of ".foo xxx" in the usual way.
525 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
531 assert(ROFF_CTL(buf
[*pos
]));
534 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
537 if ('\0' == buf
[*pos
])
541 maclen
= strcspn(mac
, " \\\t\0");
543 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
544 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
547 while (buf
[*pos
] && ' ' == buf
[*pos
])
555 roff_parse_nat(const char *buf
, unsigned int *res
)
561 lval
= strtol(buf
, &ep
, 10);
562 if (buf
[0] == '\0' || *ep
!= '\0')
564 if ((errno
== ERANGE
&&
565 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
566 (lval
> INT_MAX
|| lval
< 0))
569 *res
= (unsigned int)lval
;
576 roff_cblock(ROFF_ARGS
)
580 * A block-close `..' should only be invoked as a child of an
581 * ignore macro, otherwise raise a warning and just ignore it.
584 if (NULL
== r
->last
) {
585 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
589 switch (r
->last
->tok
) {
597 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
604 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
609 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
612 roffnode_cleanscope(r
);
619 roffnode_cleanscope(struct roff
*r
)
623 if (--r
->last
->endspan
< 0)
632 roff_ccond(ROFF_ARGS
)
635 if (NULL
== r
->last
) {
636 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
640 switch (r
->last
->tok
) {
648 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
652 if (r
->last
->endspan
> -1) {
653 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
658 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
661 roffnode_cleanscope(r
);
668 roff_block(ROFF_ARGS
)
676 if (ROFF_ig
!= tok
) {
677 if ('\0' == (*bufp
)[pos
]) {
678 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
683 * Re-write `de1', since we don't really care about
684 * groff's strange compatibility mode, into `de'.
692 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
695 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
698 while (' ' == (*bufp
)[pos
])
699 (*bufp
)[pos
++] = '\0';
702 roffnode_push(r
, tok
, name
, ln
, ppos
);
705 * At the beginning of a `de' macro, clear the existing string
706 * with the same name, if there is one. New content will be
707 * added from roff_block_text() in multiline mode.
711 roff_setstr(r
, name
, "", 0);
713 if ('\0' == (*bufp
)[pos
])
716 /* If present, process the custom end-of-line marker. */
719 while ((*bufp
)[pos
] &&
720 ' ' != (*bufp
)[pos
] &&
721 '\t' != (*bufp
)[pos
])
725 * Note: groff does NOT like escape characters in the input.
726 * Instead of detecting this, we're just going to let it fly and
731 sz
= (size_t)(pos
- sv
);
733 if (1 == sz
&& '.' == (*bufp
)[sv
])
736 r
->last
->end
= mandoc_malloc(sz
+ 1);
738 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
739 r
->last
->end
[(int)sz
] = '\0';
742 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
750 roff_block_sub(ROFF_ARGS
)
756 * First check whether a custom macro exists at this level. If
757 * it does, then check against it. This is some of groff's
758 * stranger behaviours. If we encountered a custom end-scope
759 * tag and that tag also happens to be a "real" macro, then we
760 * need to try interpreting it again as a real macro. If it's
761 * not, then return ignore. Else continue.
766 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
769 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
770 if ((*bufp
)[i
] != r
->last
->end
[j
])
773 if ('\0' == r
->last
->end
[j
] &&
774 ('\0' == (*bufp
)[i
] ||
776 '\t' == (*bufp
)[i
])) {
778 roffnode_cleanscope(r
);
780 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
787 * If we have no custom end-query or lookup failed, then try
788 * pulling it out of the hashtable.
792 t
= roff_parse(r
, *bufp
, &pos
);
795 * Macros other than block-end are only significant
796 * in `de' blocks; elsewhere, simply throw them away.
798 if (ROFF_cblock
!= t
) {
800 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
804 assert(roffs
[t
].proc
);
805 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
806 ln
, ppos
, pos
, offs
));
812 roff_block_text(ROFF_ARGS
)
816 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
824 roff_cond_sub(ROFF_ARGS
)
833 * Clean out scope. If we've closed ourselves, then don't
837 roffnode_cleanscope(r
);
839 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
840 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
842 (r
, ROFF_ccond
, bufp
, szp
,
843 ln
, pos
, pos
+ 2, offs
));
844 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
848 * A denied conditional must evaluate its children if and only
849 * if they're either structurally required (such as loops and
850 * conditionals) or a closing macro.
852 if (ROFFRULE_DENY
== rr
)
853 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
857 assert(roffs
[t
].proc
);
858 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
859 ln
, ppos
, pos
, offs
));
865 roff_cond_text(ROFF_ARGS
)
873 * We display the value of the text if out current evaluation
874 * scope permits us to do so.
877 /* FIXME: use roff_ccond? */
880 if (NULL
== (ep
= strstr(st
, "\\}"))) {
881 roffnode_cleanscope(r
);
882 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
885 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
888 roffnode_cleanscope(r
);
889 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
894 roff_evalcond(const char *v
, int *pos
)
900 return(ROFFRULE_ALLOW
);
907 return(ROFFRULE_DENY
);
912 while (v
[*pos
] && ' ' != v
[*pos
])
914 return(ROFFRULE_DENY
);
919 roff_line_ignore(ROFF_ARGS
)
927 roff_line_error(ROFF_ARGS
)
930 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
941 /* Stack overflow! */
943 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
944 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
948 /* First, evaluate the conditional. */
950 if (ROFF_el
== tok
) {
952 * An `.el' will get the value of the current rstack
953 * entry set in prior `ie' calls or defaults to DENY.
955 if (r
->rstackpos
< 0)
956 rule
= ROFFRULE_DENY
;
958 rule
= r
->rstack
[r
->rstackpos
];
960 rule
= roff_evalcond(*bufp
, &pos
);
964 while (' ' == (*bufp
)[pos
])
968 * Roff is weird. If we have just white-space after the
969 * conditional, it's considered the BODY and we exit without
970 * really doing anything. Warn about this. It's probably
974 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
975 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
979 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
981 r
->last
->rule
= rule
;
983 if (ROFF_ie
== tok
) {
985 * An if-else will put the NEGATION of the current
986 * evaluated conditional into the stack.
989 if (ROFFRULE_DENY
== r
->last
->rule
)
990 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
992 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
995 /* If the parent has false as its rule, then so do we. */
997 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
998 r
->last
->rule
= ROFFRULE_DENY
;
1001 * Determine scope. If we're invoked with "\{" trailing the
1002 * conditional, then we're in a multiline scope. Else our scope
1003 * expires on the next line.
1006 r
->last
->endspan
= 1;
1008 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1009 r
->last
->endspan
= -1;
1014 * If there are no arguments on the line, the next-line scope is
1018 if ('\0' == (*bufp
)[pos
])
1021 /* Otherwise re-run the roff parser after recalculating. */
1032 char *name
, *string
;
1035 * A symbol is named by the first word following the macro
1036 * invocation up to a space. Its value is anything after the
1037 * name's trailing whitespace and optional double-quote. Thus,
1041 * will have `bar " ' as its value.
1049 /* Read until end of name. */
1050 while (*string
&& ' ' != *string
)
1053 /* Nil-terminate name. */
1057 /* Read past spaces. */
1058 while (*string
&& ' ' == *string
)
1061 /* Read passed initial double-quote. */
1062 if (*string
&& '"' == *string
)
1065 /* The rest is the value. */
1066 roff_setstr(r
, name
, string
, 0);
1075 const char *key
, *val
;
1078 key
= &(*bufp
)[pos
];
1081 /* Parse register request. */
1082 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1086 * Set our nil terminator. Because this line is going to be
1087 * ignored anyway, we can munge it as we please.
1090 (*bufp
)[pos
++] = '\0';
1092 /* Skip whitespace to register token. */
1093 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1096 val
= &(*bufp
)[pos
];
1098 /* Process register token. */
1100 if (0 == strcmp(key
, "nS")) {
1101 rg
[(int)REG_nS
].set
= 1;
1102 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1103 rg
[(int)REG_nS
].v
.u
= 0;
1115 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1129 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1132 r
->tbl
= tbl_alloc();
1143 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1146 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1147 * opening anything that's not in our cwd or anything beneath
1148 * it. Thus, explicitly disallow traversing up the file-system
1149 * or using absolute paths.
1153 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1154 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1164 roff_userdef(ROFF_ARGS
)
1168 int i
, quoted
, pairs
;
1171 * Collect pointers to macro argument strings
1172 * and null-terminate them.
1175 for (i
= 0; i
< 9; i
++) {
1176 /* Quoting can only start with a new word. */
1183 for (pairs
= 0; '\0' != *cp
; cp
++) {
1184 /* Unquoted arguments end at blanks. */
1190 /* After pairs of quotes, move left. */
1193 /* Pairs of quotes do not end words, ... */
1194 if ('"' == cp
[0] && '"' == cp
[1]) {
1199 /* ... but solitary quotes do. */
1207 /* Last argument; the remaining ones are empty strings. */
1210 /* Null-terminate argument and move to the next one. */
1217 * Expand macro arguments.
1220 n1
= cp
= mandoc_strdup(r
->current_string
);
1221 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1223 if (0 > i
|| 8 < i
) {
1224 /* Not an argument invocation. */
1229 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1230 n2
= mandoc_malloc(*szp
);
1232 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1233 strlcat(n2
, arg
[i
], *szp
);
1234 strlcat(n2
, cp
+ 3, *szp
);
1236 cp
= n2
+ (cp
- n1
);
1242 * Replace the macro invocation
1243 * by the expanded macro.
1248 *szp
= strlen(*bufp
) + 1;
1250 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1251 ROFF_REPARSE
: ROFF_APPEND
);
1255 * Store *string into the user-defined string called *name.
1256 * In multiline mode, append to an existing entry and append '\n';
1257 * else replace the existing entry, if there is one.
1258 * To clear an existing entry, call with (*r, *name, NULL, 0).
1261 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1266 size_t oldch
, newch
;
1268 /* Search for an existing string with the same name. */
1269 n
= r
->first_string
;
1270 while (n
&& strcmp(name
, n
->name
))
1274 /* Create a new string table entry. */
1275 n
= mandoc_malloc(sizeof(struct roffstr
));
1276 n
->name
= mandoc_strdup(name
);
1278 n
->next
= r
->first_string
;
1279 r
->first_string
= n
;
1280 } else if (0 == multiline
) {
1281 /* In multiline mode, append; else replace. */
1290 * One additional byte for the '\n' in multiline mode,
1291 * and one for the terminating '\0'.
1293 newch
= strlen(string
) + (multiline
? 2 : 1);
1294 if (NULL
== n
->string
) {
1295 n
->string
= mandoc_malloc(newch
);
1299 oldch
= strlen(n
->string
);
1300 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1303 /* Skip existing content in the destination buffer. */
1304 c
= n
->string
+ oldch
;
1306 /* Append new content to the destination buffer. */
1309 * Rudimentary roff copy mode:
1310 * Handle escaped backslashes.
1312 if ('\\' == *string
&& '\\' == *(string
+ 1))
1317 /* Append terminating bytes. */
1325 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1327 const struct roffstr
*n
;
1329 n
= r
->first_string
;
1330 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1333 return(n
? n
->string
: NULL
);
1338 roff_freestr(struct roff
*r
)
1340 struct roffstr
*n
, *nn
;
1342 for (n
= r
->first_string
; n
; n
= nn
) {
1349 r
->first_string
= NULL
;