]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.108 2010/12/21 01:46:44 schwarze Exp $ */
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include "libmandoc.h"
34 #define RSTACK_MAX 128
37 ('.' == (c) || '\'' == (c))
40 #define ROFF_DEBUG(fmt, args...) \
41 do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
43 #define ROFF_DEBUG(fmt, args...) \
44 do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
68 ROFF_ccond
, /* FIXME: remove this. */
80 char *name
; /* key of symbol */
81 char *string
; /* current value */
82 struct roffstr
*next
; /* next in list */
86 struct roffnode
*last
; /* leaf of stack */
87 mandocmsg msg
; /* err/warn/fatal messages */
88 void *data
; /* privdata for messages */
89 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
90 int rstackpos
; /* position in rstack */
91 struct regset
*regs
; /* read/writable registers */
92 struct roffstr
*first_string
; /* user-defined strings & macros */
93 const char *current_string
; /* value of last called user macro */
97 enum rofft tok
; /* type of node */
98 struct roffnode
*parent
; /* up one in stack */
99 int line
; /* parse line */
100 int col
; /* parse col */
101 char *name
; /* node name, e.g. macro name */
102 char *end
; /* end-rules: custom token */
103 int endspan
; /* end-rules: next-line or infty */
104 enum roffrule rule
; /* current evaluation rule */
107 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
108 enum rofft tok, /* tok of macro */ \
109 char **bufp, /* input buffer */ \
110 size_t *szp, /* size of input buffer */ \
111 int ln, /* parse line */ \
112 int ppos, /* original pos in buffer */ \
113 int pos, /* current pos in buffer */ \
114 int *offs /* reset offset of buffer data */
116 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
119 const char *name
; /* macro name */
120 roffproc proc
; /* process new macro */
121 roffproc text
; /* process as child text of macro */
122 roffproc sub
; /* process as child of macro */
124 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
125 struct roffmac
*next
;
128 static enum rofferr
roff_block(ROFF_ARGS
);
129 static enum rofferr
roff_block_text(ROFF_ARGS
);
130 static enum rofferr
roff_block_sub(ROFF_ARGS
);
131 static enum rofferr
roff_cblock(ROFF_ARGS
);
132 static enum rofferr
roff_ccond(ROFF_ARGS
);
133 static enum rofferr
roff_cond(ROFF_ARGS
);
134 static enum rofferr
roff_cond_text(ROFF_ARGS
);
135 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
136 static enum rofferr
roff_ds(ROFF_ARGS
);
137 static enum roffrule
roff_evalcond(const char *, int *);
138 static void roff_freestr(struct roff
*);
139 static const char *roff_getstrn(const struct roff
*,
140 const char *, size_t);
141 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
142 static enum rofferr
roff_line_error(ROFF_ARGS
);
143 static enum rofferr
roff_nr(ROFF_ARGS
);
144 static int roff_res(struct roff
*,
145 char **, size_t *, int);
146 static void roff_setstr(struct roff
*,
147 const char *, const char *, int);
148 static enum rofferr
roff_so(ROFF_ARGS
);
149 static enum rofferr
roff_userdef(ROFF_ARGS
);
151 /* See roff_hash_find() */
155 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
157 static struct roffmac
*hash
[HASHWIDTH
];
159 static struct roffmac roffs
[ROFF_MAX
] = {
160 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
161 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
162 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
163 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
164 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
165 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
166 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
167 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
168 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
169 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
170 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
171 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
172 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
173 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
174 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
175 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
176 { "rm", roff_line_error
, NULL
, NULL
, 0, NULL
},
177 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
178 { "tr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
179 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
180 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
181 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
184 static void roff_free1(struct roff
*);
185 static enum rofft
roff_hash_find(const char *, size_t);
186 static void roff_hash_init(void);
187 static void roffnode_cleanscope(struct roff
*);
188 static void roffnode_push(struct roff
*, enum rofft
,
189 const char *, int, int);
190 static void roffnode_pop(struct roff
*);
191 static enum rofft
roff_parse(struct roff
*, const char *, int *);
192 static int roff_parse_nat(const char *, unsigned int *);
194 /* See roff_hash_find() */
195 #define ROFF_HASH(p) (p[0] - ASCII_LO)
203 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
204 assert(roffs
[i
].name
[0] >= ASCII_LO
);
205 assert(roffs
[i
].name
[0] <= ASCII_HI
);
207 buc
= ROFF_HASH(roffs
[i
].name
);
209 if (NULL
!= (n
= hash
[buc
])) {
210 for ( ; n
->next
; n
= n
->next
)
214 hash
[buc
] = &roffs
[i
];
220 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
221 * the nil-terminated string name could be found.
224 roff_hash_find(const char *p
, size_t s
)
230 * libroff has an extremely simple hashtable, for the time
231 * being, which simply keys on the first character, which must
232 * be printable, then walks a chain. It works well enough until
236 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
241 if (NULL
== (n
= hash
[buc
]))
243 for ( ; n
; n
= n
->next
)
244 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
245 return((enum rofft
)(n
- roffs
));
252 * Pop the current node off of the stack of roff instructions currently
256 roffnode_pop(struct roff
*r
)
263 if (ROFF_el
== p
->tok
)
264 if (r
->rstackpos
> -1)
267 ROFF_DEBUG("roff: popping scope\n");
268 r
->last
= r
->last
->parent
;
276 * Push a roff node onto the instruction stack. This must later be
277 * removed with roffnode_pop().
280 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
285 p
= mandoc_calloc(1, sizeof(struct roffnode
));
288 p
->name
= mandoc_strdup(name
);
292 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
299 roff_free1(struct roff
*r
)
309 roff_reset(struct roff
*r
)
317 roff_free(struct roff
*r
)
326 roff_alloc(struct regset
*regs
, void *data
, const mandocmsg msg
)
330 r
= mandoc_calloc(1, sizeof(struct roff
));
342 * Pre-filter each and every line for reserved words (one beginning with
343 * `\*', e.g., `\*(ab'). These must be handled before the actual line
347 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
349 const char *stesc
; /* start of an escape sequence ('\\') */
350 const char *stnam
; /* start of the name, after "[(*" */
351 const char *cp
; /* end of the name, e.g. before ']' */
352 const char *res
; /* the string to be substituted */
357 /* Search for a leading backslash and save a pointer to it. */
360 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
364 * The second character must be an asterisk.
365 * If it isn't, skip it anyway: It is escaped,
366 * so it can't start another escape sequence.
375 * The third character decides the length
376 * of the name of the string.
377 * Save a pointer to the name.
397 /* Advance to the end of the name. */
399 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
401 return(1); /* Error. */
402 if (0 == maxl
&& ']' == *cp
)
407 * Retrieve the replacement string; if it is
408 * undefined, resume searching for escapes.
411 res
= roff_getstrn(r
, stnam
, (size_t)i
);
418 /* Replace the escape sequence by the string. */
420 ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i
, st
);
422 nsz
= *szp
+ strlen(res
) + 1;
423 n
= mandoc_malloc(nsz
);
425 strlcpy(n
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
426 strlcat(n
, res
, nsz
);
427 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
441 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
442 size_t *szp
, int pos
, int *offs
)
448 * Run the reserved-word filter only if we have some reserved
452 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
453 return(ROFF_REPARSE
);
456 * First, if a scope is open and we're not a macro, pass the
457 * text through the macro's filter. If a scope isn't open and
458 * we're not a macro, just let it through.
461 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
463 assert(roffs
[t
].text
);
464 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
465 roffs
[t
].name
, &(*bufp
)[pos
]);
466 return((*roffs
[t
].text
)
468 ln
, pos
, pos
, offs
));
469 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
473 * If a scope is open, go to the child handler for that macro,
474 * as it may want to preprocess before doing anything with it.
479 assert(roffs
[t
].sub
);
480 ROFF_DEBUG("roff: intercept scoped context: %s, [%s]\n",
481 roffs
[t
].name
, &(*bufp
)[pos
]);
482 return((*roffs
[t
].sub
)
484 ln
, pos
, pos
, offs
));
488 * Lastly, as we've no scope open, try to look up and execute
489 * the new macro. If no macro is found, simply return and let
490 * the compilers handle it.
494 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
497 ROFF_DEBUG("roff: intercept new-scope: [%s], [%s]\n",
498 ROFF_USERDEF
== t
? r
->current_string
: roffs
[t
].name
,
501 assert(roffs
[t
].proc
);
502 return((*roffs
[t
].proc
)
504 ln
, ppos
, pos
, offs
));
509 roff_endparse(struct roff
*r
)
514 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
515 r
->last
->col
, NULL
));
520 * Parse a roff node's type from the input buffer. This must be in the
521 * form of ".foo xxx" in the usual way.
524 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
530 assert(ROFF_CTL(buf
[*pos
]));
533 while (' ' == buf
[*pos
] || '\t' == buf
[*pos
])
536 if ('\0' == buf
[*pos
])
540 maclen
= strcspn(mac
, " \\\t\0");
542 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
543 ? ROFF_USERDEF
: roff_hash_find(mac
, maclen
);
546 while (buf
[*pos
] && ' ' == buf
[*pos
])
554 roff_parse_nat(const char *buf
, unsigned int *res
)
560 lval
= strtol(buf
, &ep
, 10);
561 if (buf
[0] == '\0' || *ep
!= '\0')
563 if ((errno
== ERANGE
&&
564 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
565 (lval
> INT_MAX
|| lval
< 0))
568 *res
= (unsigned int)lval
;
575 roff_cblock(ROFF_ARGS
)
579 * A block-close `..' should only be invoked as a child of an
580 * ignore macro, otherwise raise a warning and just ignore it.
583 if (NULL
== r
->last
) {
584 if ( ! (*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 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
610 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
614 roffnode_cleanscope(r
);
621 roffnode_cleanscope(struct roff
*r
)
625 if (--r
->last
->endspan
< 0)
634 roff_ccond(ROFF_ARGS
)
637 if (NULL
== r
->last
) {
638 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
643 switch (r
->last
->tok
) {
651 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
656 if (r
->last
->endspan
> -1) {
657 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
663 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
667 roffnode_cleanscope(r
);
674 roff_block(ROFF_ARGS
)
682 if (ROFF_ig
!= tok
) {
683 if ('\0' == (*bufp
)[pos
]) {
684 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
689 * Re-write `de1', since we don't really care about
690 * groff's strange compatibility mode, into `de'.
698 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
,
701 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
704 while (' ' == (*bufp
)[pos
])
705 (*bufp
)[pos
++] = '\0';
708 roffnode_push(r
, tok
, name
, ln
, ppos
);
711 * At the beginning of a `de' macro, clear the existing string
712 * with the same name, if there is one. New content will be
713 * added from roff_block_text() in multiline mode.
717 roff_setstr(r
, name
, "", 0);
719 if ('\0' == (*bufp
)[pos
])
722 /* If present, process the custom end-of-line marker. */
725 while ((*bufp
)[pos
] &&
726 ' ' != (*bufp
)[pos
] &&
727 '\t' != (*bufp
)[pos
])
731 * Note: groff does NOT like escape characters in the input.
732 * Instead of detecting this, we're just going to let it fly and
737 sz
= (size_t)(pos
- sv
);
739 if (1 == sz
&& '.' == (*bufp
)[sv
])
742 r
->last
->end
= mandoc_malloc(sz
+ 1);
744 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
745 r
->last
->end
[(int)sz
] = '\0';
748 (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
);
756 roff_block_sub(ROFF_ARGS
)
762 * First check whether a custom macro exists at this level. If
763 * it does, then check against it. This is some of groff's
764 * stranger behaviours. If we encountered a custom end-scope
765 * tag and that tag also happens to be a "real" macro, then we
766 * need to try interpreting it again as a real macro. If it's
767 * not, then return ignore. Else continue.
772 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
775 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
776 if ((*bufp
)[i
] != r
->last
->end
[j
])
779 if ('\0' == r
->last
->end
[j
] &&
780 ('\0' == (*bufp
)[i
] ||
782 '\t' == (*bufp
)[i
])) {
784 roffnode_cleanscope(r
);
786 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
793 * If we have no custom end-query or lookup failed, then try
794 * pulling it out of the hashtable.
798 t
= roff_parse(r
, *bufp
, &pos
);
801 * Macros other than block-end are only significant
802 * in `de' blocks; elsewhere, simply throw them away.
804 if (ROFF_cblock
!= t
) {
806 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 1);
810 assert(roffs
[t
].proc
);
811 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
812 ln
, ppos
, pos
, offs
));
818 roff_block_text(ROFF_ARGS
)
822 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 1);
830 roff_cond_sub(ROFF_ARGS
)
839 * Clean out scope. If we've closed ourselves, then don't
843 roffnode_cleanscope(r
);
845 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
))) {
846 if ('\\' == (*bufp
)[pos
] && '}' == (*bufp
)[pos
+ 1])
848 (r
, ROFF_ccond
, bufp
, szp
,
849 ln
, pos
, pos
+ 2, offs
));
850 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
854 * A denied conditional must evaluate its children if and only
855 * if they're either structurally required (such as loops and
856 * conditionals) or a closing macro.
858 if (ROFFRULE_DENY
== rr
)
859 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
863 assert(roffs
[t
].proc
);
864 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
865 ln
, ppos
, pos
, offs
));
871 roff_cond_text(ROFF_ARGS
)
879 * We display the value of the text if out current evaluation
880 * scope permits us to do so.
883 /* FIXME: use roff_ccond? */
886 if (NULL
== (ep
= strstr(st
, "\\}"))) {
887 roffnode_cleanscope(r
);
888 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
891 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
894 roffnode_cleanscope(r
);
895 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
900 roff_evalcond(const char *v
, int *pos
)
906 return(ROFFRULE_ALLOW
);
913 return(ROFFRULE_DENY
);
918 while (v
[*pos
] && ' ' != v
[*pos
])
920 return(ROFFRULE_DENY
);
925 roff_line_ignore(ROFF_ARGS
)
933 roff_line_error(ROFF_ARGS
)
936 (*r
->msg
)(MANDOCERR_REQUEST
, r
->data
, ln
, ppos
, roffs
[tok
].name
);
947 /* Stack overflow! */
949 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
950 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
954 /* First, evaluate the conditional. */
956 if (ROFF_el
== tok
) {
958 * An `.el' will get the value of the current rstack
959 * entry set in prior `ie' calls or defaults to DENY.
961 if (r
->rstackpos
< 0)
962 rule
= ROFFRULE_DENY
;
964 rule
= r
->rstack
[r
->rstackpos
];
966 rule
= roff_evalcond(*bufp
, &pos
);
970 while (' ' == (*bufp
)[pos
])
974 * Roff is weird. If we have just white-space after the
975 * conditional, it's considered the BODY and we exit without
976 * really doing anything. Warn about this. It's probably
980 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
981 (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
);
985 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
987 r
->last
->rule
= rule
;
989 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
990 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
992 if (ROFF_ie
== tok
) {
994 * An if-else will put the NEGATION of the current
995 * evaluated conditional into the stack.
998 if (ROFFRULE_DENY
== r
->last
->rule
)
999 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
1001 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
1004 /* If the parent has false as its rule, then so do we. */
1006 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
1007 r
->last
->rule
= ROFFRULE_DENY
;
1008 ROFF_DEBUG("roff: cond override: %s -> deny\n",
1013 * Determine scope. If we're invoked with "\{" trailing the
1014 * conditional, then we're in a multiline scope. Else our scope
1015 * expires on the next line.
1018 r
->last
->endspan
= 1;
1020 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1021 r
->last
->endspan
= -1;
1023 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
1026 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
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.
1065 /* Read until end of name. */
1066 while (*string
&& ' ' != *string
)
1069 /* Nil-terminate name. */
1073 /* Read past spaces. */
1074 while (*string
&& ' ' == *string
)
1077 /* Read passed initial double-quote. */
1078 if (*string
&& '"' == *string
)
1081 /* The rest is the value. */
1082 roff_setstr(r
, name
, string
, 0);
1091 const char *key
, *val
;
1094 key
= &(*bufp
)[pos
];
1097 /* Parse register request. */
1098 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1102 * Set our nil terminator. Because this line is going to be
1103 * ignored anyway, we can munge it as we please.
1106 (*bufp
)[pos
++] = '\0';
1108 /* Skip whitespace to register token. */
1109 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1112 val
= &(*bufp
)[pos
];
1114 /* Process register token. */
1116 if (0 == strcmp(key
, "nS")) {
1117 rg
[(int)REG_nS
].set
= 1;
1118 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1119 rg
[(int)REG_nS
].v
.u
= 0;
1121 ROFF_DEBUG("roff: register nS: %u\n",
1122 rg
[(int)REG_nS
].v
.u
);
1124 ROFF_DEBUG("roff: ignoring register: %s\n", key
);
1135 (*r
->msg
)(MANDOCERR_SO
, r
->data
, ln
, ppos
, NULL
);
1138 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1139 * opening anything that's not in our cwd or anything beneath
1140 * it. Thus, explicitly disallow traversing up the file-system
1141 * or using absolute paths.
1145 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1146 (*r
->msg
)(MANDOCERR_SOPATH
, r
->data
, ln
, pos
, NULL
);
1156 roff_userdef(ROFF_ARGS
)
1160 int i
, quoted
, pairs
;
1163 * Collect pointers to macro argument strings
1164 * and null-terminate them.
1167 for (i
= 0; i
< 9; i
++) {
1168 /* Quoting can only start with a new word. */
1175 for (pairs
= 0; '\0' != *cp
; cp
++) {
1176 /* Unquoted arguments end at blanks. */
1182 /* After pairs of quotes, move left. */
1185 /* Pairs of quotes do not end words, ... */
1186 if ('"' == cp
[0] && '"' == cp
[1]) {
1191 /* ... but solitary quotes do. */
1199 /* Last argument; the remaining ones are empty strings. */
1202 /* Null-terminate argument and move to the next one. */
1209 * Expand macro arguments.
1212 n1
= cp
= mandoc_strdup(r
->current_string
);
1213 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1215 if (0 > i
|| 8 < i
) {
1216 /* Not an argument invocation. */
1221 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1222 n2
= mandoc_malloc(*szp
);
1224 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1225 strlcat(n2
, arg
[i
], *szp
);
1226 strlcat(n2
, cp
+ 3, *szp
);
1228 cp
= n2
+ (cp
- n1
);
1234 * Replace the macro invocation
1235 * by the expanded macro.
1240 *szp
= strlen(*bufp
) + 1;
1242 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1243 ROFF_REPARSE
: ROFF_APPEND
);
1247 * Store *string into the user-defined string called *name.
1248 * In multiline mode, append to an existing entry and append '\n';
1249 * else replace the existing entry, if there is one.
1250 * To clear an existing entry, call with (*r, *name, NULL, 0).
1253 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1258 size_t oldch
, newch
;
1260 /* Search for an existing string with the same name. */
1261 n
= r
->first_string
;
1262 while (n
&& strcmp(name
, n
->name
))
1266 /* Create a new string table entry. */
1267 n
= mandoc_malloc(sizeof(struct roffstr
));
1268 n
->name
= mandoc_strdup(name
);
1270 n
->next
= r
->first_string
;
1271 r
->first_string
= n
;
1272 } else if (0 == multiline
) {
1273 /* In multiline mode, append; else replace. */
1282 * One additional byte for the '\n' in multiline mode,
1283 * and one for the terminating '\0'.
1285 newch
= strlen(string
) + (multiline
? 2 : 1);
1286 if (NULL
== n
->string
) {
1287 n
->string
= mandoc_malloc(newch
);
1291 oldch
= strlen(n
->string
);
1292 n
->string
= mandoc_realloc(n
->string
, oldch
+ newch
);
1295 /* Skip existing content in the destination buffer. */
1296 c
= n
->string
+ oldch
;
1298 /* Append new content to the destination buffer. */
1301 * Rudimentary roff copy mode:
1302 * Handle escaped backslashes.
1304 if ('\\' == *string
&& '\\' == *(string
+ 1))
1309 /* Append terminating bytes. */
1317 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1319 const struct roffstr
*n
;
1321 n
= r
->first_string
;
1322 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1325 return(n
? n
->string
: NULL
);
1330 roff_freestr(struct roff
*r
)
1332 struct roffstr
*n
, *nn
;
1334 for (n
= r
->first_string
; n
; n
= nn
) {
1341 r
->first_string
= NULL
;