]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.124 2011/01/25 01:12:02 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))
68 ROFF_ccond
, /* FIXME: remove this. */
79 char *name
; /* key of symbol */
80 char *string
; /* current value */
81 struct roffstr
*next
; /* next in list */
85 struct roffnode
*last
; /* leaf of stack */
86 mandocmsg msg
; /* err/warn/fatal messages */
87 void *data
; /* privdata for messages */
88 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
89 int rstackpos
; /* position in rstack */
90 struct regset
*regs
; /* read/writable registers */
91 struct roffstr
*first_string
; /* user-defined strings & macros */
92 const char *current_string
; /* value of last called user macro */
93 struct tbl_node
*first_tbl
; /* first table parsed */
94 struct tbl_node
*last_tbl
; /* last table parsed */
95 struct tbl_node
*tbl
; /* current table being parsed */
99 enum rofft tok
; /* type of node */
100 struct roffnode
*parent
; /* up one in stack */
101 int line
; /* parse line */
102 int col
; /* parse col */
103 char *name
; /* node name, e.g. macro name */
104 char *end
; /* end-rules: custom token */
105 int endspan
; /* end-rules: next-line or infty */
106 enum roffrule rule
; /* current evaluation rule */
109 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
110 enum rofft tok, /* tok of macro */ \
111 char **bufp, /* input buffer */ \
112 size_t *szp, /* size of input buffer */ \
113 int ln, /* parse line */ \
114 int ppos, /* original pos in buffer */ \
115 int pos, /* current pos in buffer */ \
116 int *offs /* reset offset of buffer data */
118 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
121 const char *name
; /* macro name */
122 roffproc proc
; /* process new macro */
123 roffproc text
; /* process as child text of macro */
124 roffproc sub
; /* process as child of macro */
126 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
127 struct roffmac
*next
;
130 static enum rofferr
roff_block(ROFF_ARGS
);
131 static enum rofferr
roff_block_text(ROFF_ARGS
);
132 static enum rofferr
roff_block_sub(ROFF_ARGS
);
133 static enum rofferr
roff_cblock(ROFF_ARGS
);
134 static enum rofferr
roff_ccond(ROFF_ARGS
);
135 static enum rofferr
roff_cond(ROFF_ARGS
);
136 static enum rofferr
roff_cond_text(ROFF_ARGS
);
137 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
138 static enum rofferr
roff_ds(ROFF_ARGS
);
139 static enum roffrule
roff_evalcond(const char *, int *);
140 static void roff_freestr(struct roff
*);
141 static char *roff_getname(struct roff
*, char **, int, int);
142 static const char *roff_getstrn(const struct roff
*,
143 const char *, size_t);
144 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
145 static enum rofferr
roff_nr(ROFF_ARGS
);
146 static int roff_res(struct roff
*,
147 char **, size_t *, int);
148 static enum rofferr
roff_rm(ROFF_ARGS
);
149 static void roff_setstr(struct roff
*,
150 const char *, const char *, int);
151 static enum rofferr
roff_so(ROFF_ARGS
);
152 static enum rofferr
roff_TE(ROFF_ARGS
);
153 static enum rofferr
roff_TS(ROFF_ARGS
);
154 static enum rofferr
roff_T_(ROFF_ARGS
);
155 static enum rofferr
roff_userdef(ROFF_ARGS
);
157 /* See roff_hash_find() */
161 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
163 static struct roffmac
*hash
[HASHWIDTH
];
165 static struct roffmac roffs
[ROFF_MAX
] = {
166 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
167 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
168 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
169 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
170 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
171 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
172 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
173 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
174 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
175 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
176 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
177 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
178 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
179 { "it", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
180 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
181 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
182 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
183 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
184 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
185 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
186 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
187 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
188 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
189 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
190 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
191 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
192 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
193 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
194 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
197 static void roff_free1(struct roff
*);
198 static enum rofft
roff_hash_find(const char *, size_t);
199 static void roff_hash_init(void);
200 static void roffnode_cleanscope(struct roff
*);
201 static void roffnode_push(struct roff
*, enum rofft
,
202 const char *, int, int);
203 static void roffnode_pop(struct roff
*);
204 static enum rofft
roff_parse(struct roff
*, const char *, int *);
205 static int roff_parse_nat(const char *, unsigned int *);
207 /* See roff_hash_find() */
208 #define ROFF_HASH(p) (p[0] - ASCII_LO)
216 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
217 assert(roffs
[i
].name
[0] >= ASCII_LO
);
218 assert(roffs
[i
].name
[0] <= ASCII_HI
);
220 buc
= ROFF_HASH(roffs
[i
].name
);
222 if (NULL
!= (n
= hash
[buc
])) {
223 for ( ; n
->next
; n
= n
->next
)
227 hash
[buc
] = &roffs
[i
];
233 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
234 * the nil-terminated string name could be found.
237 roff_hash_find(const char *p
, size_t s
)
243 * libroff has an extremely simple hashtable, for the time
244 * being, which simply keys on the first character, which must
245 * be printable, then walks a chain. It works well enough until
249 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
254 if (NULL
== (n
= hash
[buc
]))
256 for ( ; n
; n
= n
->next
)
257 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
258 return((enum rofft
)(n
- roffs
));
265 * Pop the current node off of the stack of roff instructions currently
269 roffnode_pop(struct roff
*r
)
276 if (ROFF_el
== p
->tok
)
277 if (r
->rstackpos
> -1)
280 r
->last
= r
->last
->parent
;
288 * Push a roff node onto the instruction stack. This must later be
289 * removed with roffnode_pop().
292 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
297 p
= mandoc_calloc(1, sizeof(struct roffnode
));
300 p
->name
= mandoc_strdup(name
);
304 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
311 roff_free1(struct roff
*r
)
315 while (r
->first_tbl
) {
317 r
->first_tbl
= t
->next
;
321 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
331 roff_reset(struct roff
*r
)
339 roff_free(struct roff
*r
)
348 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
352 r
= mandoc_calloc(1, sizeof(struct roff
));
364 * Pre-filter each and every line for reserved words (one beginning with
365 * `\*', e.g., `\*(ab'). These must be handled before the actual line
369 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
371 const char *stesc
; /* start of an escape sequence ('\\') */
372 const char *stnam
; /* start of the name, after "[(*" */
373 const char *cp
; /* end of the name, e.g. before ']' */
374 const char *res
; /* the string to be substituted */
379 /* Search for a leading backslash and save a pointer to it. */
382 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
386 * The second character must be an asterisk.
387 * If it isn't, skip it anyway: It is escaped,
388 * so it can't start another escape sequence.
397 * The third character decides the length
398 * of the name of the string.
399 * Save a pointer to the name.
419 /* Advance to the end of the name. */
421 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
423 return(1); /* Error. */
424 if (0 == maxl
&& ']' == *cp
)
429 * Retrieve the replacement string; if it is
430 * undefined, resume searching for escapes.
433 res
= roff_getstrn(r
, stnam
, (size_t)i
);
440 /* Replace the escape sequence by the string. */
442 nsz
= *szp
+ strlen(res
) + 1;
443 n
= mandoc_malloc(nsz
);
445 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
446 strlcat(n
, res
, nsz
);
447 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
461 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
462 size_t *szp
, int pos
, int *offs
)
469 * Run the reserved-word filter only if we have some reserved
473 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
474 return(ROFF_REPARSE
);
477 * First, if a scope is open and we're not a macro, pass the
478 * text through the macro's filter. If a scope isn't open and
479 * we're not a macro, just let it through.
482 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
484 assert(roffs
[t
].text
);
486 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
487 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
488 if (ROFF_CONT
== e
&& r
->tbl
)
489 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
491 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
493 return(tbl_read(r
->tbl
, ln
, *bufp
, *offs
));
498 * If a scope is open, go to the child handler for that macro,
499 * as it may want to preprocess before doing anything with it.
504 assert(roffs
[t
].sub
);
505 return((*roffs
[t
].sub
)
507 ln
, pos
, pos
, offs
));
511 * Lastly, as we've no scope open, try to look up and execute
512 * the new macro. If no macro is found, simply return and let
513 * the compilers handle it.
517 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
520 assert(roffs
[t
].proc
);
521 return((*roffs
[t
].proc
)
523 ln
, ppos
, pos
, offs
));
528 roff_endparse(struct roff
*r
)
532 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
533 r
->last
->line
, r
->last
->col
, NULL
);
536 (*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
537 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
545 * Parse a roff node's type from the input buffer. This must be in the
546 * form of ".foo xxx" in the usual way.
549 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
555 assert(ROFF_CTL(buf
[*pos
]));
558 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
561 if ('\0' == buf
[*pos
])
565 maclen
= strcspn(mac
, " \\\t\0");
567 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
568 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
571 while (buf
[*pos
] && ' ' == buf
[*pos
])
579 roff_parse_nat(const char *buf
, unsigned int *res
)
585 lval
= strtol(buf
, &ep
, 10);
586 if (buf
[0] == '\0' || *ep
!= '\0')
588 if ((errno
== ERANGE
&&
589 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
590 (lval
> INT_MAX
|| lval
< 0))
593 *res
= (unsigned int)lval
;
600 roff_cblock(ROFF_ARGS
)
604 * A block-close `..' should only be invoked as a child of an
605 * ignore macro, otherwise raise a warning and just ignore it.
608 if (NULL
== r
->last
) {
609 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
613 switch (r
->last
->tok
) {
621 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
628 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
633 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
636 roffnode_cleanscope(r
);
643 roffnode_cleanscope(struct roff
*r
)
647 if (--r
->last
->endspan
< 0)
656 roff_ccond(ROFF_ARGS
)
659 if (NULL
== r
->last
) {
660 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
664 switch (r
->last
->tok
) {
672 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
676 if (r
->last
->endspan
> -1) {
677 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
682 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
685 roffnode_cleanscope(r
);
692 roff_block(ROFF_ARGS
)
700 if (ROFF_ig
!= tok
) {
701 if ('\0' == (*bufp
)[pos
]) {
702 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
707 * Re-write `de1', since we don't really care about
708 * groff's strange compatibility mode, into `de'.
716 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
719 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
722 while (' ' == (*bufp
)[pos
])
723 (*bufp
)[pos
++] = '\0';
726 roffnode_push(r
, tok
, name
, ln
, ppos
);
729 * At the beginning of a `de' macro, clear the existing string
730 * with the same name, if there is one. New content will be
731 * added from roff_block_text() in multiline mode.
735 roff_setstr(r
, name
, "", 0);
737 if ('\0' == (*bufp
)[pos
])
740 /* If present, process the custom end-of-line marker. */
743 while ((*bufp
)[pos
] &&
744 ' ' != (*bufp
)[pos
] &&
745 '\t' != (*bufp
)[pos
])
749 * Note: groff does NOT like escape characters in the input.
750 * Instead of detecting this, we're just going to let it fly and
755 sz
= (size_t)(pos
- sv
);
757 if (1 == sz
&& '.' == (*bufp
)[sv
])
760 r
->last
->end
= mandoc_malloc(sz
+ 1);
762 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
763 r
->last
->end
[(int)sz
] = '\0';
766 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
774 roff_block_sub(ROFF_ARGS
)
780 * First check whether a custom macro exists at this level. If
781 * it does, then check against it. This is some of groff's
782 * stranger behaviours. If we encountered a custom end-scope
783 * tag and that tag also happens to be a "real" macro, then we
784 * need to try interpreting it again as a real macro. If it's
785 * not, then return ignore. Else continue.
790 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
793 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
794 if ((*bufp
)[i
] != r
->last
->end
[j
])
797 if ('\0' == r
->last
->end
[j
] &&
798 ('\0' == (*bufp
)[i
] ||
800 '\t' == (*bufp
)[i
])) {
802 roffnode_cleanscope(r
);
804 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
811 * If we have no custom end-query or lookup failed, then try
812 * pulling it out of the hashtable.
816 t
= roff_parse(r
, *bufp
, &pos
);
819 * Macros other than block-end are only significant
820 * in `de' blocks; elsewhere, simply throw them away.
822 if (ROFF_cblock
!= t
) {
824 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
828 assert(roffs
[t
].proc
);
829 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
830 ln
, ppos
, pos
, offs
));
836 roff_block_text(ROFF_ARGS
)
840 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
848 roff_cond_sub(ROFF_ARGS
)
857 * Clean out scope. If we've closed ourselves, then don't
861 roffnode_cleanscope(r
);
863 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
864 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
866 (r
, ROFF_ccond
, bufp
, szp
,
867 ln
, pos
, pos
+ 2, offs
));
868 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
872 * A denied conditional must evaluate its children if and only
873 * if they're either structurally required (such as loops and
874 * conditionals) or a closing macro.
876 if (ROFFRULE_DENY
== rr
)
877 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
881 assert(roffs
[t
].proc
);
882 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
883 ln
, ppos
, pos
, offs
));
889 roff_cond_text(ROFF_ARGS
)
897 * We display the value of the text if out current evaluation
898 * scope permits us to do so.
901 /* FIXME: use roff_ccond? */
904 if (NULL
== (ep
= strstr(st
, "\\}"))) {
905 roffnode_cleanscope(r
);
906 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
909 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
912 roffnode_cleanscope(r
);
913 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
918 roff_evalcond(const char *v
, int *pos
)
924 return(ROFFRULE_ALLOW
);
931 return(ROFFRULE_DENY
);
936 while (v
[*pos
] && ' ' != v
[*pos
])
938 return(ROFFRULE_DENY
);
943 roff_line_ignore(ROFF_ARGS
)
947 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, "it");
959 /* Stack overflow! */
961 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
962 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
966 /* First, evaluate the conditional. */
968 if (ROFF_el
== tok
) {
970 * An `.el' will get the value of the current rstack
971 * entry set in prior `ie' calls or defaults to DENY.
973 if (r
->rstackpos
< 0)
974 rule
= ROFFRULE_DENY
;
976 rule
= r
->rstack
[r
->rstackpos
];
978 rule
= roff_evalcond(*bufp
, &pos
);
982 while (' ' == (*bufp
)[pos
])
986 * Roff is weird. If we have just white-space after the
987 * conditional, it's considered the BODY and we exit without
988 * really doing anything. Warn about this. It's probably
992 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
993 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
997 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
999 r
->last
->rule
= rule
;
1001 if (ROFF_ie
== tok
) {
1003 * An if-else will put the NEGATION of the current
1004 * evaluated conditional into the stack.
1007 if (ROFFRULE_DENY
== r
->last
->rule
)
1008 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1010 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1013 /* If the parent has false as its rule, then so do we. */
1015 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
1016 r
->last
->rule
= ROFFRULE_DENY
;
1019 * Determine scope. If we're invoked with "\{" trailing the
1020 * conditional, then we're in a multiline scope. Else our scope
1021 * expires on the next line.
1024 r
->last
->endspan
= 1;
1026 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1027 r
->last
->endspan
= -1;
1032 * If there are no arguments on the line, the next-line scope is
1036 if ('\0' == (*bufp
)[pos
])
1039 /* Otherwise re-run the roff parser after recalculating. */
1050 char *name
, *string
;
1053 * A symbol is named by the first word following the macro
1054 * invocation up to a space. Its value is anything after the
1055 * name's trailing whitespace and optional double-quote. Thus,
1059 * will have `bar " ' as its value.
1062 string
= *bufp
+ pos
;
1063 name
= roff_getname(r
, &string
, ln
, pos
);
1067 /* Read past initial double-quote. */
1071 /* The rest is the value. */
1072 roff_setstr(r
, name
, string
, 0);
1086 key
= roff_getname(r
, &val
, ln
, pos
);
1089 if (0 == strcmp(key
, "nS")) {
1090 rg
[(int)REG_nS
].set
= 1;
1091 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1092 rg
[(int)REG_nS
].v
.u
= 0;
1106 while ('\0' != *cp
) {
1107 name
= roff_getname(r
, &cp
, ln
, cp
- *bufp
);
1109 roff_setstr(r
, name
, NULL
, 0);
1120 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1134 (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
);
1136 tbl_restart(ppos
, ln
, r
->tbl
);
1148 (*r
->msg
)(MANDOCERR_SCOPEBROKEN
, r
->data
, ln
, ppos
, NULL
);
1152 t
= tbl_alloc(ppos
, ln
, r
->data
, r
->msg
);
1155 r
->last_tbl
->next
= t
;
1157 r
->first_tbl
= r
->last_tbl
= t
;
1159 r
->tbl
= r
->last_tbl
= t
;
1169 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1172 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1173 * opening anything that's not in our cwd or anything beneath
1174 * it. Thus, explicitly disallow traversing up the file-system
1175 * or using absolute paths.
1179 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1180 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1190 roff_userdef(ROFF_ARGS
)
1197 * Collect pointers to macro argument strings
1198 * and null-terminate them.
1201 for (i
= 0; i
< 9; i
++)
1202 arg
[i
] = '\0' == *cp
? "" :
1203 mandoc_getarg(&cp
, r
->msg
, r
->data
, ln
, &pos
);
1206 * Expand macro arguments.
1209 n1
= cp
= mandoc_strdup(r
->current_string
);
1210 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1212 if (0 > i
|| 8 < i
) {
1213 /* Not an argument invocation. */
1218 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1219 n2
= mandoc_malloc(*szp
);
1221 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1222 strlcat(n2
, arg
[i
], *szp
);
1223 strlcat(n2
, cp
+ 3, *szp
);
1225 cp
= n2
+ (cp
- n1
);
1231 * Replace the macro invocation
1232 * by the expanded macro.
1237 *szp
= strlen(*bufp
) + 1;
1239 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1240 ROFF_REPARSE
: ROFF_APPEND
);
1245 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1253 /* Read until end of name. */
1254 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1260 (*r
->msg
)(MANDOCERR_NAMESC
, r
->data
, ln
, pos
, NULL
);
1265 /* Nil-terminate name. */
1269 /* Read past spaces. */
1279 * Store *string into the user-defined string called *name.
1280 * In multiline mode, append to an existing entry and append '\n';
1281 * else replace the existing entry, if there is one.
1282 * To clear an existing entry, call with (*r, *name, NULL, 0).
1285 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1290 size_t oldch
, newch
;
1292 /* Search for an existing string with the same name. */
1293 n
= r
->first_string
;
1294 while (n
&& strcmp(name
, n
->name
))
1298 /* Create a new string table entry. */
1299 n
= mandoc_malloc(sizeof(struct roffstr
));
1300 n
->name
= mandoc_strdup(name
);
1302 n
->next
= r
->first_string
;
1303 r
->first_string
= n
;
1304 } else if (0 == multiline
) {
1305 /* In multiline mode, append; else replace. */
1314 * One additional byte for the '\n' in multiline mode,
1315 * and one for the terminating '\0'.
1317 newch
= strlen(string
) + (multiline
? 2 : 1);
1318 if (NULL
== n
->string
) {
1319 n
->string
= mandoc_malloc(newch
);
1323 oldch
= strlen(n
->string
);
1324 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1327 /* Skip existing content in the destination buffer. */
1328 c
= n
->string
+ oldch
;
1330 /* Append new content to the destination buffer. */
1333 * Rudimentary roff copy mode:
1334 * Handle escaped backslashes.
1336 if ('\\' == *string
&& '\\' == *(string
+ 1))
1341 /* Append terminating bytes. */
1349 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1351 const struct roffstr
*n
;
1353 n
= r
->first_string
;
1354 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1357 return(n
? n
->string
: NULL
);
1362 roff_freestr(struct roff
*r
)
1364 struct roffstr
*n
, *nn
;
1366 for (n
= r
->first_string
; n
; n
= nn
) {
1373 r
->first_string
= NULL
;
1376 const struct tbl_span
*
1377 roff_span(const struct roff
*r
)
1380 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);