]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.121 2011/01/11 00:11:45 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 char *roff_getname(struct roff
*, char **, int, int);
138 static const char *roff_getstrn(const struct roff
*,
139 const char *, size_t);
140 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
141 static enum rofferr
roff_line_error(ROFF_ARGS
);
142 static enum rofferr
roff_nr(ROFF_ARGS
);
143 static int roff_res(struct roff
*,
144 char **, size_t *, int);
145 static void roff_setstr(struct roff
*,
146 const char *, const char *, int);
147 static enum rofferr
roff_so(ROFF_ARGS
);
148 static enum rofferr
roff_TE(ROFF_ARGS
);
149 static enum rofferr
roff_TS(ROFF_ARGS
);
150 static enum rofferr
roff_T_(ROFF_ARGS
);
151 static enum rofferr
roff_userdef(ROFF_ARGS
);
153 /* See roff_hash_find() */
157 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
159 static struct roffmac
*hash
[HASHWIDTH
];
161 static struct roffmac roffs
[ROFF_MAX
] = {
162 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
163 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
165 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
166 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
167 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
168 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
169 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
170 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
171 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
172 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
173 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
174 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
175 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
176 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
177 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
178 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
179 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
180 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
181 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
182 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
183 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
184 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
185 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
186 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
189 static void roff_free1(struct roff
*);
190 static enum rofft
roff_hash_find(const char *, size_t);
191 static void roff_hash_init(void);
192 static void roffnode_cleanscope(struct roff
*);
193 static void roffnode_push(struct roff
*, enum rofft
,
194 const char *, int, int);
195 static void roffnode_pop(struct roff
*);
196 static enum rofft
roff_parse(struct roff
*, const char *, int *);
197 static int roff_parse_nat(const char *, unsigned int *);
199 /* See roff_hash_find() */
200 #define ROFF_HASH(p) (p[0] - ASCII_LO)
208 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
209 assert(roffs
[i
].name
[0] >= ASCII_LO
);
210 assert(roffs
[i
].name
[0] <= ASCII_HI
);
212 buc
= ROFF_HASH(roffs
[i
].name
);
214 if (NULL
!= (n
= hash
[buc
])) {
215 for ( ; n
->next
; n
= n
->next
)
219 hash
[buc
] = &roffs
[i
];
225 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
226 * the nil-terminated string name could be found.
229 roff_hash_find(const char *p
, size_t s
)
235 * libroff has an extremely simple hashtable, for the time
236 * being, which simply keys on the first character, which must
237 * be printable, then walks a chain. It works well enough until
241 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
246 if (NULL
== (n
= hash
[buc
]))
248 for ( ; n
; n
= n
->next
)
249 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
250 return((enum rofft
)(n
- roffs
));
257 * Pop the current node off of the stack of roff instructions currently
261 roffnode_pop(struct roff
*r
)
268 if (ROFF_el
== p
->tok
)
269 if (r
->rstackpos
> -1)
272 r
->last
= r
->last
->parent
;
280 * Push a roff node onto the instruction stack. This must later be
281 * removed with roffnode_pop().
284 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
289 p
= mandoc_calloc(1, sizeof(struct roffnode
));
292 p
->name
= mandoc_strdup(name
);
296 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
303 roff_free1(struct roff
*r
)
307 while (r
->first_tbl
) {
309 r
->first_tbl
= t
->next
;
313 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
323 roff_reset(struct roff
*r
)
331 roff_free(struct roff
*r
)
340 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
344 r
= mandoc_calloc(1, sizeof(struct roff
));
356 * Pre-filter each and every line for reserved words (one beginning with
357 * `\*', e.g., `\*(ab'). These must be handled before the actual line
361 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
363 const char *stesc
; /* start of an escape sequence ('\\') */
364 const char *stnam
; /* start of the name, after "[(*" */
365 const char *cp
; /* end of the name, e.g. before ']' */
366 const char *res
; /* the string to be substituted */
371 /* Search for a leading backslash and save a pointer to it. */
374 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
378 * The second character must be an asterisk.
379 * If it isn't, skip it anyway: It is escaped,
380 * so it can't start another escape sequence.
389 * The third character decides the length
390 * of the name of the string.
391 * Save a pointer to the name.
411 /* Advance to the end of the name. */
413 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
415 return(1); /* Error. */
416 if (0 == maxl
&& ']' == *cp
)
421 * Retrieve the replacement string; if it is
422 * undefined, resume searching for escapes.
425 res
= roff_getstrn(r
, stnam
, (size_t)i
);
432 /* Replace the escape sequence by the string. */
434 nsz
= *szp
+ strlen(res
) + 1;
435 n
= mandoc_malloc(nsz
);
437 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
438 strlcat(n
, res
, nsz
);
439 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
453 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
454 size_t *szp
, int pos
, int *offs
)
461 * Run the reserved-word filter only if we have some reserved
465 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
466 return(ROFF_REPARSE
);
469 * First, if a scope is open and we're not a macro, pass the
470 * text through the macro's filter. If a scope isn't open and
471 * we're not a macro, just let it through.
474 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
476 assert(roffs
[t
].text
);
478 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
479 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
480 if (ROFF_CONT
== e
&& r
->tbl
)
481 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
483 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
485 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
490 * If a scope is open, go to the child handler for that macro,
491 * as it may want to preprocess before doing anything with it.
496 assert(roffs
[t
].sub
);
497 return((*roffs
[t
].sub
)
499 ln
, pos
, pos
, offs
));
503 * Lastly, as we've no scope open, try to look up and execute
504 * the new macro. If no macro is found, simply return and let
505 * the compilers handle it.
509 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
512 assert(roffs
[t
].proc
);
513 return((*roffs
[t
].proc
)
515 ln
, ppos
, pos
, offs
));
520 roff_endparse(struct roff
*r
)
524 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
525 r
->last
->line
, r
->last
->col
, NULL
);
528 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
529 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
537 * Parse a roff node's type from the input buffer. This must be in the
538 * form of ".foo xxx" in the usual way.
541 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
547 assert(ROFF_CTL(buf
[*pos
]));
550 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
553 if ('\0' == buf
[*pos
])
557 maclen
= strcspn(mac
, " \\\t\0");
559 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
560 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
563 while (buf
[*pos
] && ' ' == buf
[*pos
])
571 roff_parse_nat(const char *buf
, unsigned int *res
)
577 lval
= strtol(buf
, &ep
, 10);
578 if (buf
[0] == '\0' || *ep
!= '\0')
580 if ((errno
== ERANGE
&&
581 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
582 (lval
> INT_MAX
|| lval
< 0))
585 *res
= (unsigned int)lval
;
592 roff_cblock(ROFF_ARGS
)
596 * A block-close `..' should only be invoked as a child of an
597 * ignore macro, otherwise raise a warning and just ignore it.
600 if (NULL
== r
->last
) {
601 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
605 switch (r
->last
->tok
) {
613 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
620 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
625 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
628 roffnode_cleanscope(r
);
635 roffnode_cleanscope(struct roff
*r
)
639 if (--r
->last
->endspan
< 0)
648 roff_ccond(ROFF_ARGS
)
651 if (NULL
== r
->last
) {
652 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
656 switch (r
->last
->tok
) {
664 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
668 if (r
->last
->endspan
> -1) {
669 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
674 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
677 roffnode_cleanscope(r
);
684 roff_block(ROFF_ARGS
)
692 if (ROFF_ig
!= tok
) {
693 if ('\0' == (*bufp
)[pos
]) {
694 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
699 * Re-write `de1', since we don't really care about
700 * groff's strange compatibility mode, into `de'.
708 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
711 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
714 while (' ' == (*bufp
)[pos
])
715 (*bufp
)[pos
++] = '\0';
718 roffnode_push(r
, tok
, name
, ln
, ppos
);
721 * At the beginning of a `de' macro, clear the existing string
722 * with the same name, if there is one. New content will be
723 * added from roff_block_text() in multiline mode.
727 roff_setstr(r
, name
, "", 0);
729 if ('\0' == (*bufp
)[pos
])
732 /* If present, process the custom end-of-line marker. */
735 while ((*bufp
)[pos
] &&
736 ' ' != (*bufp
)[pos
] &&
737 '\t' != (*bufp
)[pos
])
741 * Note: groff does NOT like escape characters in the input.
742 * Instead of detecting this, we're just going to let it fly and
747 sz
= (size_t)(pos
- sv
);
749 if (1 == sz
&& '.' == (*bufp
)[sv
])
752 r
->last
->end
= mandoc_malloc(sz
+ 1);
754 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
755 r
->last
->end
[(int)sz
] = '\0';
758 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
766 roff_block_sub(ROFF_ARGS
)
772 * First check whether a custom macro exists at this level. If
773 * it does, then check against it. This is some of groff's
774 * stranger behaviours. If we encountered a custom end-scope
775 * tag and that tag also happens to be a "real" macro, then we
776 * need to try interpreting it again as a real macro. If it's
777 * not, then return ignore. Else continue.
782 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
785 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
786 if ((*bufp
)[i
] != r
->last
->end
[j
])
789 if ('\0' == r
->last
->end
[j
] &&
790 ('\0' == (*bufp
)[i
] ||
792 '\t' == (*bufp
)[i
])) {
794 roffnode_cleanscope(r
);
796 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
803 * If we have no custom end-query or lookup failed, then try
804 * pulling it out of the hashtable.
808 t
= roff_parse(r
, *bufp
, &pos
);
811 * Macros other than block-end are only significant
812 * in `de' blocks; elsewhere, simply throw them away.
814 if (ROFF_cblock
!= t
) {
816 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
820 assert(roffs
[t
].proc
);
821 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
822 ln
, ppos
, pos
, offs
));
828 roff_block_text(ROFF_ARGS
)
832 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
840 roff_cond_sub(ROFF_ARGS
)
849 * Clean out scope. If we've closed ourselves, then don't
853 roffnode_cleanscope(r
);
855 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
856 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
858 (r
, ROFF_ccond
, bufp
, szp
,
859 ln
, pos
, pos
+ 2, offs
));
860 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
864 * A denied conditional must evaluate its children if and only
865 * if they're either structurally required (such as loops and
866 * conditionals) or a closing macro.
868 if (ROFFRULE_DENY
== rr
)
869 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
873 assert(roffs
[t
].proc
);
874 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
875 ln
, ppos
, pos
, offs
));
881 roff_cond_text(ROFF_ARGS
)
889 * We display the value of the text if out current evaluation
890 * scope permits us to do so.
893 /* FIXME: use roff_ccond? */
896 if (NULL
== (ep
= strstr(st
, "\\}"))) {
897 roffnode_cleanscope(r
);
898 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
901 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
904 roffnode_cleanscope(r
);
905 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
910 roff_evalcond(const char *v
, int *pos
)
916 return(ROFFRULE_ALLOW
);
923 return(ROFFRULE_DENY
);
928 while (v
[*pos
] && ' ' != v
[*pos
])
930 return(ROFFRULE_DENY
);
935 roff_line_ignore(ROFF_ARGS
)
943 roff_line_error(ROFF_ARGS
)
946 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
957 /* Stack overflow! */
959 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
960 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
964 /* First, evaluate the conditional. */
966 if (ROFF_el
== tok
) {
968 * An `.el' will get the value of the current rstack
969 * entry set in prior `ie' calls or defaults to DENY.
971 if (r
->rstackpos
< 0)
972 rule
= ROFFRULE_DENY
;
974 rule
= r
->rstack
[r
->rstackpos
];
976 rule
= roff_evalcond(*bufp
, &pos
);
980 while (' ' == (*bufp
)[pos
])
984 * Roff is weird. If we have just white-space after the
985 * conditional, it's considered the BODY and we exit without
986 * really doing anything. Warn about this. It's probably
990 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
991 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
995 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
997 r
->last
->rule
= rule
;
999 if (ROFF_ie
== tok
) {
1001 * An if-else will put the NEGATION of the current
1002 * evaluated conditional into the stack.
1005 if (ROFFRULE_DENY
== r
->last
->rule
)
1006 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1008 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1011 /* If the parent has false as its rule, then so do we. */
1013 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1014 r
->last
->rule
= ROFFRULE_DENY
;
1017 * Determine scope. If we're invoked with "\{" trailing the
1018 * conditional, then we're in a multiline scope. Else our scope
1019 * expires on the next line.
1022 r
->last
->endspan
= 1;
1024 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1025 r
->last
->endspan
= -1;
1030 * If there are no arguments on the line, the next-line scope is
1034 if ('\0' == (*bufp
)[pos
])
1037 /* Otherwise re-run the roff parser after recalculating. */
1048 char *name
, *string
;
1051 * A symbol is named by the first word following the macro
1052 * invocation up to a space. Its value is anything after the
1053 * name's trailing whitespace and optional double-quote. Thus,
1057 * will have `bar " ' as its value.
1060 string
= *bufp
+ pos
;
1061 name
= roff_getname(r
, &string
, ln
, pos
);
1065 /* Read past initial double-quote. */
1069 /* The rest is the value. */
1070 roff_setstr(r
, name
, string
, 0);
1084 key
= roff_getname(r
, &val
, ln
, pos
);
1087 if (0 == strcmp(key
, "nS")) {
1088 rg
[(int)REG_nS
].set
= 1;
1089 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1090 rg
[(int)REG_nS
].v
.u
= 0;
1102 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1116 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1118 tbl_restart(ppos
, ln
, r
->tbl
);
1130 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1134 t
= tbl_alloc(ppos
, ln
, r
->data
, r
->msg
);
1137 r
->last_tbl
->next
= t
;
1139 r
->first_tbl
= r
->last_tbl
= t
;
1141 r
->tbl
= r
->last_tbl
= t
;
1151 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1154 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1155 * opening anything that's not in our cwd or anything beneath
1156 * it. Thus, explicitly disallow traversing up the file-system
1157 * or using absolute paths.
1161 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1162 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1172 roff_userdef(ROFF_ARGS
)
1179 * Collect pointers to macro argument strings
1180 * and null-terminate them.
1183 for (i
= 0; i
< 9; i
++)
1184 arg
[i
] = '\0' == *cp
? "" :
1185 mandoc_getarg(&cp
, r
->msg
, r
->data
, ln
, &pos
);
1188 * Expand macro arguments.
1191 n1
= cp
= mandoc_strdup(r
->current_string
);
1192 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1194 if (0 > i
|| 8 < i
) {
1195 /* Not an argument invocation. */
1200 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1201 n2
= mandoc_malloc(*szp
);
1203 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1204 strlcat(n2
, arg
[i
], *szp
);
1205 strlcat(n2
, cp
+ 3, *szp
);
1207 cp
= n2
+ (cp
- n1
);
1213 * Replace the macro invocation
1214 * by the expanded macro.
1219 *szp
= strlen(*bufp
) + 1;
1221 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1222 ROFF_REPARSE
: ROFF_APPEND
);
1227 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1235 /* Read until end of name. */
1236 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1242 (*r
->msg
)(MANDOCERR_NAMESC
, r
->data
, ln
, pos
, NULL
);
1247 /* Nil-terminate name. */
1251 /* Read past spaces. */
1261 * Store *string into the user-defined string called *name.
1262 * In multiline mode, append to an existing entry and append '\n';
1263 * else replace the existing entry, if there is one.
1264 * To clear an existing entry, call with (*r, *name, NULL, 0).
1267 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1272 size_t oldch
, newch
;
1274 /* Search for an existing string with the same name. */
1275 n
= r
->first_string
;
1276 while (n
&& strcmp(name
, n
->name
))
1280 /* Create a new string table entry. */
1281 n
= mandoc_malloc(sizeof(struct roffstr
));
1282 n
->name
= mandoc_strdup(name
);
1284 n
->next
= r
->first_string
;
1285 r
->first_string
= n
;
1286 } else if (0 == multiline
) {
1287 /* In multiline mode, append; else replace. */
1296 * One additional byte for the '\n' in multiline mode,
1297 * and one for the terminating '\0'.
1299 newch
= strlen(string
) + (multiline
? 2 : 1);
1300 if (NULL
== n
->string
) {
1301 n
->string
= mandoc_malloc(newch
);
1305 oldch
= strlen(n
->string
);
1306 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1309 /* Skip existing content in the destination buffer. */
1310 c
= n
->string
+ oldch
;
1312 /* Append new content to the destination buffer. */
1315 * Rudimentary roff copy mode:
1316 * Handle escaped backslashes.
1318 if ('\\' == *string
&& '\\' == *(string
+ 1))
1323 /* Append terminating bytes. */
1331 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1333 const struct roffstr
*n
;
1335 n
= r
->first_string
;
1336 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1339 return(n
? n
->string
: NULL
);
1344 roff_freestr(struct roff
*r
)
1346 struct roffstr
*n
, *nn
;
1348 for (n
= r
->first_string
; n
; n
= nn
) {
1355 r
->first_string
= NULL
;
1358 const struct tbl_span
*
1359 roff_span(const struct roff
*r
)
1362 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);