]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.120 2011/01/03 23:24:16 schwarze 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.
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 */
89 struct tbl_node
*first_tbl
; /* first table parsed */
90 struct tbl_node
*last_tbl
; /* last table parsed */
91 struct tbl_node
*tbl
; /* current table being parsed */
95 enum rofft tok
; /* type of node */
96 struct roffnode
*parent
; /* up one in stack */
97 int line
; /* parse line */
98 int col
; /* parse col */
99 char *name
; /* node name, e.g. macro name */
100 char *end
; /* end-rules: custom token */
101 int endspan
; /* end-rules: next-line or infty */
102 enum roffrule rule
; /* current evaluation rule */
105 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
106 enum rofft tok, /* tok of macro */ \
107 char **bufp, /* input buffer */ \
108 size_t *szp, /* size of input buffer */ \
109 int ln, /* parse line */ \
110 int ppos, /* original pos in buffer */ \
111 int pos, /* current pos in buffer */ \
112 int *offs /* reset offset of buffer data */
114 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
117 const char *name
; /* macro name */
118 roffproc proc
; /* process new macro */
119 roffproc text
; /* process as child text of macro */
120 roffproc sub
; /* process as child of macro */
122 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
123 struct roffmac
*next
;
126 static enum rofferr
roff_block(ROFF_ARGS
);
127 static enum rofferr
roff_block_text(ROFF_ARGS
);
128 static enum rofferr
roff_block_sub(ROFF_ARGS
);
129 static enum rofferr
roff_cblock(ROFF_ARGS
);
130 static enum rofferr
roff_ccond(ROFF_ARGS
);
131 static enum rofferr
roff_cond(ROFF_ARGS
);
132 static enum rofferr
roff_cond_text(ROFF_ARGS
);
133 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
134 static enum rofferr
roff_ds(ROFF_ARGS
);
135 static enum roffrule
roff_evalcond(const char *, int *);
136 static void roff_freestr(struct roff
*);
137 static const char *roff_getstrn(const struct roff
*,
138 const char *, size_t);
139 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
140 static enum rofferr
roff_line_error(ROFF_ARGS
);
141 static enum rofferr
roff_nr(ROFF_ARGS
);
142 static int roff_res(struct roff
*,
143 char **, size_t *, int);
144 static void roff_setstr(struct roff
*,
145 const char *, const char *, int);
146 static enum rofferr
roff_so(ROFF_ARGS
);
147 static enum rofferr
roff_TE(ROFF_ARGS
);
148 static enum rofferr
roff_TS(ROFF_ARGS
);
149 static enum rofferr
roff_T_(ROFF_ARGS
);
150 static enum rofferr
roff_userdef(ROFF_ARGS
);
152 /* See roff_hash_find() */
156 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
158 static struct roffmac
*hash
[HASHWIDTH
];
160 static struct roffmac roffs
[ROFF_MAX
] = {
161 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
162 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
163 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
165 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
166 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
167 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
168 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
169 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
170 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
171 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
172 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
173 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
174 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
175 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
176 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
177 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
178 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
179 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
180 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
181 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
182 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
183 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
184 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
185 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
188 static void roff_free1(struct roff
*);
189 static enum rofft
roff_hash_find(const char *, size_t);
190 static void roff_hash_init(void);
191 static void roffnode_cleanscope(struct roff
*);
192 static void roffnode_push(struct roff
*, enum rofft
,
193 const char *, int, int);
194 static void roffnode_pop(struct roff
*);
195 static enum rofft
roff_parse(struct roff
*, const char *, int *);
196 static int roff_parse_nat(const char *, unsigned int *);
198 /* See roff_hash_find() */
199 #define ROFF_HASH(p) (p[0] - ASCII_LO)
207 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
208 assert(roffs
[i
].name
[0] >= ASCII_LO
);
209 assert(roffs
[i
].name
[0] <= ASCII_HI
);
211 buc
= ROFF_HASH(roffs
[i
].name
);
213 if (NULL
!= (n
= hash
[buc
])) {
214 for ( ; n
->next
; n
= n
->next
)
218 hash
[buc
] = &roffs
[i
];
224 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
225 * the nil-terminated string name could be found.
228 roff_hash_find(const char *p
, size_t s
)
234 * libroff has an extremely simple hashtable, for the time
235 * being, which simply keys on the first character, which must
236 * be printable, then walks a chain. It works well enough until
240 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
245 if (NULL
== (n
= hash
[buc
]))
247 for ( ; n
; n
= n
->next
)
248 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
249 return((enum rofft
)(n
- roffs
));
256 * Pop the current node off of the stack of roff instructions currently
260 roffnode_pop(struct roff
*r
)
267 if (ROFF_el
== p
->tok
)
268 if (r
->rstackpos
> -1)
271 r
->last
= r
->last
->parent
;
279 * Push a roff node onto the instruction stack. This must later be
280 * removed with roffnode_pop().
283 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
288 p
= mandoc_calloc(1, sizeof(struct roffnode
));
291 p
->name
= mandoc_strdup(name
);
295 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
302 roff_free1(struct roff
*r
)
306 while (r
->first_tbl
) {
308 r
->first_tbl
= t
->next
;
312 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
322 roff_reset(struct roff
*r
)
330 roff_free(struct roff
*r
)
339 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
343 r
= mandoc_calloc(1, sizeof(struct roff
));
355 * Pre-filter each and every line for reserved words (one beginning with
356 * `\*', e.g., `\*(ab'). These must be handled before the actual line
360 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
362 const char *stesc
; /* start of an escape sequence ('\\') */
363 const char *stnam
; /* start of the name, after "[(*" */
364 const char *cp
; /* end of the name, e.g. before ']' */
365 const char *res
; /* the string to be substituted */
370 /* Search for a leading backslash and save a pointer to it. */
373 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
377 * The second character must be an asterisk.
378 * If it isn't, skip it anyway: It is escaped,
379 * so it can't start another escape sequence.
388 * The third character decides the length
389 * of the name of the string.
390 * Save a pointer to the name.
410 /* Advance to the end of the name. */
412 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
414 return(1); /* Error. */
415 if (0 == maxl
&& ']' == *cp
)
420 * Retrieve the replacement string; if it is
421 * undefined, resume searching for escapes.
424 res
= roff_getstrn(r
, stnam
, (size_t)i
);
431 /* Replace the escape sequence by the string. */
433 nsz
= *szp
+ strlen(res
) + 1;
434 n
= mandoc_malloc(nsz
);
436 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
437 strlcat(n
, res
, nsz
);
438 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
452 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
453 size_t *szp
, int pos
, int *offs
)
460 * Run the reserved-word filter only if we have some reserved
464 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
465 return(ROFF_REPARSE
);
468 * First, if a scope is open and we're not a macro, pass the
469 * text through the macro's filter. If a scope isn't open and
470 * we're not a macro, just let it through.
473 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
475 assert(roffs
[t
].text
);
477 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
478 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
479 if (ROFF_CONT
== e
&& r
->tbl
)
480 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
482 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
484 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
489 * If a scope is open, go to the child handler for that macro,
490 * as it may want to preprocess before doing anything with it.
495 assert(roffs
[t
].sub
);
496 return((*roffs
[t
].sub
)
498 ln
, pos
, pos
, offs
));
502 * Lastly, as we've no scope open, try to look up and execute
503 * the new macro. If no macro is found, simply return and let
504 * the compilers handle it.
508 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
511 assert(roffs
[t
].proc
);
512 return((*roffs
[t
].proc
)
514 ln
, ppos
, pos
, offs
));
519 roff_endparse(struct roff
*r
)
523 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
524 r
->last
->line
, r
->last
->col
, NULL
);
527 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
528 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
536 * Parse a roff node's type from the input buffer. This must be in the
537 * form of ".foo xxx" in the usual way.
540 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
546 assert(ROFF_CTL(buf
[*pos
]));
549 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
552 if ('\0' == buf
[*pos
])
556 maclen
= strcspn(mac
, " \\\t\0");
558 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
559 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
562 while (buf
[*pos
] && ' ' == buf
[*pos
])
570 roff_parse_nat(const char *buf
, unsigned int *res
)
576 lval
= strtol(buf
, &ep
, 10);
577 if (buf
[0] == '\0' || *ep
!= '\0')
579 if ((errno
== ERANGE
&&
580 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
581 (lval
> INT_MAX
|| lval
< 0))
584 *res
= (unsigned int)lval
;
591 roff_cblock(ROFF_ARGS
)
595 * A block-close `..' should only be invoked as a child of an
596 * ignore macro, otherwise raise a warning and just ignore it.
599 if (NULL
== r
->last
) {
600 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
604 switch (r
->last
->tok
) {
612 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
619 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
624 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
627 roffnode_cleanscope(r
);
634 roffnode_cleanscope(struct roff
*r
)
638 if (--r
->last
->endspan
< 0)
647 roff_ccond(ROFF_ARGS
)
650 if (NULL
== r
->last
) {
651 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
655 switch (r
->last
->tok
) {
663 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
667 if (r
->last
->endspan
> -1) {
668 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
673 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
676 roffnode_cleanscope(r
);
683 roff_block(ROFF_ARGS
)
691 if (ROFF_ig
!= tok
) {
692 if ('\0' == (*bufp
)[pos
]) {
693 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
698 * Re-write `de1', since we don't really care about
699 * groff's strange compatibility mode, into `de'.
707 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
710 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
713 while (' ' == (*bufp
)[pos
])
714 (*bufp
)[pos
++] = '\0';
717 roffnode_push(r
, tok
, name
, ln
, ppos
);
720 * At the beginning of a `de' macro, clear the existing string
721 * with the same name, if there is one. New content will be
722 * added from roff_block_text() in multiline mode.
726 roff_setstr(r
, name
, "", 0);
728 if ('\0' == (*bufp
)[pos
])
731 /* If present, process the custom end-of-line marker. */
734 while ((*bufp
)[pos
] &&
735 ' ' != (*bufp
)[pos
] &&
736 '\t' != (*bufp
)[pos
])
740 * Note: groff does NOT like escape characters in the input.
741 * Instead of detecting this, we're just going to let it fly and
746 sz
= (size_t)(pos
- sv
);
748 if (1 == sz
&& '.' == (*bufp
)[sv
])
751 r
->last
->end
= mandoc_malloc(sz
+ 1);
753 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
754 r
->last
->end
[(int)sz
] = '\0';
757 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
765 roff_block_sub(ROFF_ARGS
)
771 * First check whether a custom macro exists at this level. If
772 * it does, then check against it. This is some of groff's
773 * stranger behaviours. If we encountered a custom end-scope
774 * tag and that tag also happens to be a "real" macro, then we
775 * need to try interpreting it again as a real macro. If it's
776 * not, then return ignore. Else continue.
781 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
784 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
785 if ((*bufp
)[i
] != r
->last
->end
[j
])
788 if ('\0' == r
->last
->end
[j
] &&
789 ('\0' == (*bufp
)[i
] ||
791 '\t' == (*bufp
)[i
])) {
793 roffnode_cleanscope(r
);
795 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
802 * If we have no custom end-query or lookup failed, then try
803 * pulling it out of the hashtable.
807 t
= roff_parse(r
, *bufp
, &pos
);
810 * Macros other than block-end are only significant
811 * in `de' blocks; elsewhere, simply throw them away.
813 if (ROFF_cblock
!= t
) {
815 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
819 assert(roffs
[t
].proc
);
820 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
821 ln
, ppos
, pos
, offs
));
827 roff_block_text(ROFF_ARGS
)
831 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
839 roff_cond_sub(ROFF_ARGS
)
848 * Clean out scope. If we've closed ourselves, then don't
852 roffnode_cleanscope(r
);
854 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
855 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
857 (r
, ROFF_ccond
, bufp
, szp
,
858 ln
, pos
, pos
+ 2, offs
));
859 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
863 * A denied conditional must evaluate its children if and only
864 * if they're either structurally required (such as loops and
865 * conditionals) or a closing macro.
867 if (ROFFRULE_DENY
== rr
)
868 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
872 assert(roffs
[t
].proc
);
873 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
874 ln
, ppos
, pos
, offs
));
880 roff_cond_text(ROFF_ARGS
)
888 * We display the value of the text if out current evaluation
889 * scope permits us to do so.
892 /* FIXME: use roff_ccond? */
895 if (NULL
== (ep
= strstr(st
, "\\}"))) {
896 roffnode_cleanscope(r
);
897 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
900 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
903 roffnode_cleanscope(r
);
904 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
909 roff_evalcond(const char *v
, int *pos
)
915 return(ROFFRULE_ALLOW
);
922 return(ROFFRULE_DENY
);
927 while (v
[*pos
] && ' ' != v
[*pos
])
929 return(ROFFRULE_DENY
);
934 roff_line_ignore(ROFF_ARGS
)
942 roff_line_error(ROFF_ARGS
)
945 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
956 /* Stack overflow! */
958 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
959 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
963 /* First, evaluate the conditional. */
965 if (ROFF_el
== tok
) {
967 * An `.el' will get the value of the current rstack
968 * entry set in prior `ie' calls or defaults to DENY.
970 if (r
->rstackpos
< 0)
971 rule
= ROFFRULE_DENY
;
973 rule
= r
->rstack
[r
->rstackpos
];
975 rule
= roff_evalcond(*bufp
, &pos
);
979 while (' ' == (*bufp
)[pos
])
983 * Roff is weird. If we have just white-space after the
984 * conditional, it's considered the BODY and we exit without
985 * really doing anything. Warn about this. It's probably
989 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
990 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
994 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
996 r
->last
->rule
= rule
;
998 if (ROFF_ie
== tok
) {
1000 * An if-else will put the NEGATION of the current
1001 * evaluated conditional into the stack.
1004 if (ROFFRULE_DENY
== r
->last
->rule
)
1005 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1007 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1010 /* If the parent has false as its rule, then so do we. */
1012 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1013 r
->last
->rule
= ROFFRULE_DENY
;
1016 * Determine scope. If we're invoked with "\{" trailing the
1017 * conditional, then we're in a multiline scope. Else our scope
1018 * expires on the next line.
1021 r
->last
->endspan
= 1;
1023 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1024 r
->last
->endspan
= -1;
1029 * If there are no arguments on the line, the next-line scope is
1033 if ('\0' == (*bufp
)[pos
])
1036 /* Otherwise re-run the roff parser after recalculating. */
1047 char *name
, *string
;
1050 * A symbol is named by the first word following the macro
1051 * invocation up to a space. Its value is anything after the
1052 * name's trailing whitespace and optional double-quote. Thus,
1056 * will have `bar " ' as its value.
1064 /* Read until end of name. */
1065 while (*string
&& ' ' != *string
)
1068 /* Nil-terminate name. */
1072 /* Read past spaces. */
1073 while (*string
&& ' ' == *string
)
1076 /* Read passed initial double-quote. */
1077 if (*string
&& '"' == *string
)
1080 /* The rest is the value. */
1081 roff_setstr(r
, name
, string
, 0);
1090 const char *key
, *val
;
1093 key
= &(*bufp
)[pos
];
1096 /* Parse register request. */
1097 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1101 * Set our nil terminator. Because this line is going to be
1102 * ignored anyway, we can munge it as we please.
1105 (*bufp
)[pos
++] = '\0';
1107 /* Skip whitespace to register token. */
1108 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1111 val
= &(*bufp
)[pos
];
1113 /* Process register token. */
1115 if (0 == strcmp(key
, "nS")) {
1116 rg
[(int)REG_nS
].set
= 1;
1117 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1118 rg
[(int)REG_nS
].v
.u
= 0;
1130 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1144 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1146 tbl_restart(ppos
, ln
, r
->tbl
);
1158 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1162 t
= tbl_alloc(ppos
, ln
, r
->data
, r
->msg
);
1165 r
->last_tbl
->next
= t
;
1167 r
->first_tbl
= r
->last_tbl
= t
;
1169 r
->tbl
= r
->last_tbl
= t
;
1179 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1182 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1183 * opening anything that's not in our cwd or anything beneath
1184 * it. Thus, explicitly disallow traversing up the file-system
1185 * or using absolute paths.
1189 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1190 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1200 roff_userdef(ROFF_ARGS
)
1207 * Collect pointers to macro argument strings
1208 * and null-terminate them.
1211 for (i
= 0; i
< 9; i
++)
1212 arg
[i
] = '\0' == *cp
? "" :
1213 mandoc_getarg(&cp
, r
->msg
, r
->data
, ln
, &pos
);
1216 * Expand macro arguments.
1219 n1
= cp
= mandoc_strdup(r
->current_string
);
1220 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1222 if (0 > i
|| 8 < i
) {
1223 /* Not an argument invocation. */
1228 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1229 n2
= mandoc_malloc(*szp
);
1231 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1232 strlcat(n2
, arg
[i
], *szp
);
1233 strlcat(n2
, cp
+ 3, *szp
);
1235 cp
= n2
+ (cp
- n1
);
1241 * Replace the macro invocation
1242 * by the expanded macro.
1247 *szp
= strlen(*bufp
) + 1;
1249 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1250 ROFF_REPARSE
: ROFF_APPEND
);
1254 * Store *string into the user-defined string called *name.
1255 * In multiline mode, append to an existing entry and append '\n';
1256 * else replace the existing entry, if there is one.
1257 * To clear an existing entry, call with (*r, *name, NULL, 0).
1260 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1265 size_t oldch
, newch
;
1267 /* Search for an existing string with the same name. */
1268 n
= r
->first_string
;
1269 while (n
&& strcmp(name
, n
->name
))
1273 /* Create a new string table entry. */
1274 n
= mandoc_malloc(sizeof(struct roffstr
));
1275 n
->name
= mandoc_strdup(name
);
1277 n
->next
= r
->first_string
;
1278 r
->first_string
= n
;
1279 } else if (0 == multiline
) {
1280 /* In multiline mode, append; else replace. */
1289 * One additional byte for the '\n' in multiline mode,
1290 * and one for the terminating '\0'.
1292 newch
= strlen(string
) + (multiline
? 2 : 1);
1293 if (NULL
== n
->string
) {
1294 n
->string
= mandoc_malloc(newch
);
1298 oldch
= strlen(n
->string
);
1299 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1302 /* Skip existing content in the destination buffer. */
1303 c
= n
->string
+ oldch
;
1305 /* Append new content to the destination buffer. */
1308 * Rudimentary roff copy mode:
1309 * Handle escaped backslashes.
1311 if ('\\' == *string
&& '\\' == *(string
+ 1))
1316 /* Append terminating bytes. */
1324 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1326 const struct roffstr
*n
;
1328 n
= r
->first_string
;
1329 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1332 return(n
? n
->string
: NULL
);
1337 roff_freestr(struct roff
*r
)
1339 struct roffstr
*n
, *nn
;
1341 for (n
= r
->first_string
; n
; n
= nn
) {
1348 r
->first_string
= NULL
;
1351 const struct tbl_span
*
1352 roff_span(const struct roff
*r
)
1355 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);