]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.242 2014/12/16 03:53:43 schwarze Exp $ */
3 * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 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.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
30 #include "libmandoc.h"
33 /* Maximum number of nested if-else conditionals. */
34 #define RSTACK_MAX 128
36 /* Maximum number of string expansions per line, to break infinite loops. */
37 #define EXPAND_LIMIT 1000
83 * An incredibly-simple string buffer.
86 char *p
; /* nil-terminated buffer */
87 size_t sz
; /* saved strlen(p) */
91 * A key-value roffstr pair as part of a singly-linked list.
96 struct roffkv
*next
; /* next in list */
100 * A single number register as part of a singly-linked list.
105 struct roffreg
*next
;
109 struct mparse
*parse
; /* parse point */
110 const struct mchars
*mchars
; /* character table */
111 struct roffnode
*last
; /* leaf of stack */
112 int *rstack
; /* stack of inverted `ie' values */
113 struct roffreg
*regtab
; /* number registers */
114 struct roffkv
*strtab
; /* user-defined strings & macros */
115 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
116 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
117 const char *current_string
; /* value of last called user macro */
118 struct tbl_node
*first_tbl
; /* first table parsed */
119 struct tbl_node
*last_tbl
; /* last table parsed */
120 struct tbl_node
*tbl
; /* current table being parsed */
121 struct eqn_node
*last_eqn
; /* last equation parsed */
122 struct eqn_node
*first_eqn
; /* first equation parsed */
123 struct eqn_node
*eqn
; /* current equation being parsed */
124 int eqn_inline
; /* current equation is inline */
125 int options
; /* parse options */
126 int rstacksz
; /* current size limit of rstack */
127 int rstackpos
; /* position in rstack */
128 int format
; /* current file in mdoc or man format */
129 char control
; /* control character */
133 enum rofft tok
; /* type of node */
134 struct roffnode
*parent
; /* up one in stack */
135 int line
; /* parse line */
136 int col
; /* parse col */
137 char *name
; /* node name, e.g. macro name */
138 char *end
; /* end-rules: custom token */
139 int endspan
; /* end-rules: next-line or infty */
140 int rule
; /* current evaluation rule */
143 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
144 enum rofft tok, /* tok of macro */ \
145 struct buf *buf, /* input buffer */ \
146 int ln, /* parse line */ \
147 int ppos, /* original pos in buffer */ \
148 int pos, /* current pos in buffer */ \
149 int *offs /* reset offset of buffer data */
151 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
154 const char *name
; /* macro name */
155 roffproc proc
; /* process new macro */
156 roffproc text
; /* process as child text of macro */
157 roffproc sub
; /* process as child of macro */
159 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
160 struct roffmac
*next
;
164 const char *name
; /* predefined input name */
165 const char *str
; /* replacement symbol */
168 #define PREDEF(__name, __str) \
169 { (__name), (__str) },
171 static enum rofft
roffhash_find(const char *, size_t);
172 static void roffhash_init(void);
173 static void roffnode_cleanscope(struct roff
*);
174 static void roffnode_pop(struct roff
*);
175 static void roffnode_push(struct roff
*, enum rofft
,
176 const char *, int, int);
177 static enum rofferr
roff_block(ROFF_ARGS
);
178 static enum rofferr
roff_block_text(ROFF_ARGS
);
179 static enum rofferr
roff_block_sub(ROFF_ARGS
);
180 static enum rofferr
roff_cblock(ROFF_ARGS
);
181 static enum rofferr
roff_cc(ROFF_ARGS
);
182 static void roff_ccond(struct roff
*, int, int);
183 static enum rofferr
roff_cond(ROFF_ARGS
);
184 static enum rofferr
roff_cond_text(ROFF_ARGS
);
185 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
186 static enum rofferr
roff_ds(ROFF_ARGS
);
187 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
188 static int roff_evalcond(struct roff
*r
, int,
189 const char *, int *);
190 static int roff_evalnum(struct roff
*, int,
191 const char *, int *, int *, int);
192 static int roff_evalpar(struct roff
*, int,
193 const char *, int *, int *);
194 static int roff_evalstrcond(const char *, int *);
195 static void roff_free1(struct roff
*);
196 static void roff_freereg(struct roffreg
*);
197 static void roff_freestr(struct roffkv
*);
198 static size_t roff_getname(struct roff
*, char **, int, int);
199 static int roff_getnum(const char *, int *, int *);
200 static int roff_getop(const char *, int *, char *);
201 static int roff_getregn(const struct roff
*,
202 const char *, size_t);
203 static int roff_getregro(const char *name
);
204 static const char *roff_getstrn(const struct roff
*,
205 const char *, size_t);
206 static enum rofferr
roff_it(ROFF_ARGS
);
207 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
208 static enum rofferr
roff_nr(ROFF_ARGS
);
209 static enum rofft
roff_parse(struct roff
*, char *, int *,
211 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
212 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
213 static enum rofferr
roff_rm(ROFF_ARGS
);
214 static enum rofferr
roff_rr(ROFF_ARGS
);
215 static void roff_setstr(struct roff
*,
216 const char *, const char *, int);
217 static void roff_setstrn(struct roffkv
**, const char *,
218 size_t, const char *, size_t, int);
219 static enum rofferr
roff_so(ROFF_ARGS
);
220 static enum rofferr
roff_tr(ROFF_ARGS
);
221 static enum rofferr
roff_Dd(ROFF_ARGS
);
222 static enum rofferr
roff_TH(ROFF_ARGS
);
223 static enum rofferr
roff_TE(ROFF_ARGS
);
224 static enum rofferr
roff_TS(ROFF_ARGS
);
225 static enum rofferr
roff_EQ(ROFF_ARGS
);
226 static enum rofferr
roff_EN(ROFF_ARGS
);
227 static enum rofferr
roff_T_(ROFF_ARGS
);
228 static enum rofferr
roff_userdef(ROFF_ARGS
);
230 /* See roffhash_find() */
234 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
236 static struct roffmac
*hash
[HASHWIDTH
];
238 static struct roffmac roffs
[ROFF_MAX
] = {
239 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
240 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
241 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
242 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
243 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
244 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
245 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
246 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
247 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
248 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
249 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
250 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
251 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
252 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
253 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
254 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
255 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
256 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
257 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
258 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
259 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
260 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
261 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
262 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
263 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
264 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
265 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
266 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
267 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
268 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
269 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
270 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
271 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
272 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
273 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
274 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
275 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
276 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
277 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
280 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
281 const char *const __mdoc_reserved
[] = {
282 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
283 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
284 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
285 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
286 "Dt", "Dv", "Dx", "D1",
287 "Ec", "Ed", "Ef", "Ek", "El", "Em",
288 "En", "Eo", "Er", "Es", "Ev", "Ex",
289 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
290 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
291 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
292 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
293 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
294 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
295 "Sc", "Sh", "Sm", "So", "Sq",
296 "Ss", "St", "Sx", "Sy",
297 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
298 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
299 "%P", "%Q", "%R", "%T", "%U", "%V",
303 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
304 const char *const __man_reserved
[] = {
305 "AT", "B", "BI", "BR", "DT",
306 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
307 "LP", "OP", "P", "PD", "PP",
308 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
309 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
313 /* Array of injected predefined strings. */
314 #define PREDEFS_MAX 38
315 static const struct predef predefs
[PREDEFS_MAX
] = {
316 #include "predefs.in"
319 /* See roffhash_find() */
320 #define ROFF_HASH(p) (p[0] - ASCII_LO)
322 static int roffit_lines
; /* number of lines to delay */
323 static char *roffit_macro
; /* nil-terminated macro line */
332 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
333 assert(roffs
[i
].name
[0] >= ASCII_LO
);
334 assert(roffs
[i
].name
[0] <= ASCII_HI
);
336 buc
= ROFF_HASH(roffs
[i
].name
);
338 if (NULL
!= (n
= hash
[buc
])) {
339 for ( ; n
->next
; n
= n
->next
)
343 hash
[buc
] = &roffs
[i
];
348 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
349 * the nil-terminated string name could be found.
352 roffhash_find(const char *p
, size_t s
)
358 * libroff has an extremely simple hashtable, for the time
359 * being, which simply keys on the first character, which must
360 * be printable, then walks a chain. It works well enough until
364 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
369 if (NULL
== (n
= hash
[buc
]))
371 for ( ; n
; n
= n
->next
)
372 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
373 return((enum rofft
)(n
- roffs
));
379 * Pop the current node off of the stack of roff instructions currently
383 roffnode_pop(struct roff
*r
)
390 r
->last
= r
->last
->parent
;
397 * Push a roff node onto the instruction stack. This must later be
398 * removed with roffnode_pop().
401 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
406 p
= mandoc_calloc(1, sizeof(struct roffnode
));
409 p
->name
= mandoc_strdup(name
);
413 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
419 roff_free1(struct roff
*r
)
421 struct tbl_node
*tbl
;
425 while (NULL
!= (tbl
= r
->first_tbl
)) {
426 r
->first_tbl
= tbl
->next
;
429 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
431 while (NULL
!= (e
= r
->first_eqn
)) {
432 r
->first_eqn
= e
->next
;
435 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
445 roff_freereg(r
->regtab
);
448 roff_freestr(r
->strtab
);
449 roff_freestr(r
->xmbtab
);
450 r
->strtab
= r
->xmbtab
= NULL
;
453 for (i
= 0; i
< 128; i
++)
460 roff_reset(struct roff
*r
)
464 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
469 roff_free(struct roff
*r
)
477 roff_alloc(struct mparse
*parse
, const struct mchars
*mchars
, int options
)
481 r
= mandoc_calloc(1, sizeof(struct roff
));
484 r
->options
= options
;
485 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
494 * In the current line, expand escape sequences that tend to get
495 * used in numerical expressions and conditional requests.
496 * Also check the syntax of the remaining escape sequences.
499 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
501 char ubuf
[24]; /* buffer to print the number */
502 const char *start
; /* start of the string to process */
503 char *stesc
; /* start of an escape sequence ('\\') */
504 const char *stnam
; /* start of the name, after "[(*" */
505 const char *cp
; /* end of the name, e.g. before ']' */
506 const char *res
; /* the string to be substituted */
507 char *nbuf
; /* new buffer to copy buf->buf to */
508 size_t maxl
; /* expected length of the escape name */
509 size_t naml
; /* actual length of the escape name */
510 enum mandoc_esc esc
; /* type of the escape sequence */
511 int inaml
; /* length returned from mandoc_escape() */
512 int expand_count
; /* to avoid infinite loops */
513 int npos
; /* position in numeric expression */
514 int arg_complete
; /* argument not interrupted by eol */
515 char term
; /* character terminating the escape */
518 start
= buf
->buf
+ pos
;
519 stesc
= strchr(start
, '\0') - 1;
520 while (stesc
-- > start
) {
522 /* Search backwards for the next backslash. */
527 /* If it is escaped, skip it. */
529 for (cp
= stesc
- 1; cp
>= start
; cp
--)
533 if ((stesc
- cp
) % 2 == 0) {
538 /* Decide whether to expand or to check only. */
555 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
556 if (esc
== ESCAPE_ERROR
||
557 (esc
== ESCAPE_SPECIAL
&&
558 mchars_spec2cp(r
->mchars
, stnam
, inaml
) < 0))
559 mandoc_vmsg(MANDOCERR_ESC_BAD
,
560 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
561 "%.*s", (int)(cp
- stesc
), stesc
);
565 if (EXPAND_LIMIT
< ++expand_count
) {
566 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
567 ln
, (int)(stesc
- buf
->buf
), NULL
);
572 * The third character decides the length
573 * of the name of the string or register.
574 * Save a pointer to the name.
601 /* Advance to the end of the name. */
604 for (naml
= 0; maxl
== 0 || naml
< maxl
; naml
++, cp
++) {
606 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
607 ln
, (int)(stesc
- buf
->buf
), stesc
);
611 if (maxl
== 0 && *cp
== term
) {
618 * Retrieve the replacement string; if it is
619 * undefined, resume searching for escapes.
625 res
= roff_getstrn(r
, stnam
, naml
);
629 ubuf
[0] = arg_complete
&&
630 roff_evalnum(r
, ln
, stnam
, &npos
, NULL
, 0) &&
631 stnam
+ npos
+ 1 == cp
? '1' : '0';
636 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
637 roff_getregn(r
, stnam
, naml
));
642 /* use even incomplete args */
643 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
649 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
650 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
651 "%.*s", (int)naml
, stnam
);
655 /* Replace the escape sequence by the string. */
658 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
659 buf
->buf
, res
, cp
) + 1;
661 /* Prepare for the next replacement. */
664 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
672 * Process text streams:
673 * Convert all breakable hyphens into ASCII_HYPH.
674 * Decrement and spring input line trap.
677 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
685 start
= p
= buf
->buf
+ pos
;
688 sz
= strcspn(p
, "-\\");
695 /* Skip over escapes. */
697 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
698 if (esc
== ESCAPE_ERROR
)
701 } else if (p
== start
) {
706 if (isalpha((unsigned char)p
[-1]) &&
707 isalpha((unsigned char)p
[1]))
712 /* Spring the input line trap. */
713 if (roffit_lines
== 1) {
714 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
721 return(ROFF_REPARSE
);
722 } else if (roffit_lines
> 1)
728 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
732 int pos
; /* parse point */
733 int ppos
; /* original offset in buf->buf */
734 int ctl
; /* macro line (boolean) */
738 /* Handle in-line equation delimiters. */
740 if (r
->tbl
== NULL
&&
741 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
742 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
743 e
= roff_eqndelim(r
, buf
, pos
);
744 if (e
== ROFF_REPARSE
)
746 assert(e
== ROFF_CONT
);
749 /* Expand some escape sequences. */
751 e
= roff_res(r
, buf
, ln
, pos
);
754 assert(e
== ROFF_CONT
);
756 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
759 * First, if a scope is open and we're not a macro, pass the
760 * text through the macro's filter. If a scope isn't open and
761 * we're not a macro, just let it through.
762 * Finally, if there's an equation scope open, divert it into it
763 * no matter our state.
766 if (r
->last
&& ! ctl
) {
768 assert(roffs
[t
].text
);
769 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
770 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
775 return(eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
));
778 return(tbl_read(r
->tbl
, ln
, buf
->buf
, pos
));
779 return(roff_parsetext(buf
, pos
, offs
));
782 /* Skip empty request lines. */
784 if (buf
->buf
[pos
] == '"') {
785 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
788 } else if (buf
->buf
[pos
] == '\0')
792 * If a scope is open, go to the child handler for that macro,
793 * as it may want to preprocess before doing anything with it.
794 * Don't do so if an equation is open.
799 assert(roffs
[t
].sub
);
800 return((*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
804 * Lastly, as we've no scope open, try to look up and execute
805 * the new macro. If no macro is found, simply return and let
806 * the compilers handle it.
809 if ((t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
)) == ROFF_MAX
)
812 assert(roffs
[t
].proc
);
813 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
817 roff_endparse(struct roff
*r
)
821 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
822 r
->last
->line
, r
->last
->col
,
823 roffs
[r
->last
->tok
].name
);
826 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
827 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
832 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
833 r
->tbl
->line
, r
->tbl
->pos
, "TS");
839 * Parse a roff node's type from the input buffer. This must be in the
840 * form of ".foo xxx" in the usual way.
843 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
852 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
856 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
858 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
859 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
868 roff_cblock(ROFF_ARGS
)
872 * A block-close `..' should only be invoked as a child of an
873 * ignore macro, otherwise raise a warning and just ignore it.
876 if (r
->last
== NULL
) {
877 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
882 switch (r
->last
->tok
) {
884 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
889 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
896 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
901 if (buf
->buf
[pos
] != '\0')
902 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
903 ".. %s", buf
->buf
+ pos
);
906 roffnode_cleanscope(r
);
912 roffnode_cleanscope(struct roff
*r
)
916 if (--r
->last
->endspan
!= 0)
923 roff_ccond(struct roff
*r
, int ln
, int ppos
)
926 if (NULL
== r
->last
) {
927 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
932 switch (r
->last
->tok
) {
940 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
945 if (r
->last
->endspan
> -1) {
946 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
952 roffnode_cleanscope(r
);
957 roff_block(ROFF_ARGS
)
963 /* Ignore groff compatibility mode for now. */
967 else if (tok
== ROFF_am1
)
970 /* Parse the macro name argument. */
973 if (tok
== ROFF_ig
) {
978 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
979 iname
[namesz
] = '\0';
982 /* Resolve the macro name argument if it is indirect. */
984 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
985 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
986 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
987 r
->parse
, ln
, (int)(iname
- buf
->buf
),
988 "%.*s", (int)namesz
, iname
);
991 namesz
= strlen(name
);
995 if (namesz
== 0 && tok
!= ROFF_ig
) {
996 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
997 ln
, ppos
, roffs
[tok
].name
);
1001 roffnode_push(r
, tok
, name
, ln
, ppos
);
1004 * At the beginning of a `de' macro, clear the existing string
1005 * with the same name, if there is one. New content will be
1006 * appended from roff_block_text() in multiline mode.
1009 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1010 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1015 /* Get the custom end marker. */
1018 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1020 /* Resolve the end marker if it is indirect. */
1022 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1023 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1024 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1025 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1026 "%.*s", (int)namesz
, iname
);
1029 namesz
= strlen(name
);
1034 r
->last
->end
= mandoc_strndup(name
, namesz
);
1037 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1038 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1044 roff_block_sub(ROFF_ARGS
)
1050 * First check whether a custom macro exists at this level. If
1051 * it does, then check against it. This is some of groff's
1052 * stranger behaviours. If we encountered a custom end-scope
1053 * tag and that tag also happens to be a "real" macro, then we
1054 * need to try interpreting it again as a real macro. If it's
1055 * not, then return ignore. Else continue.
1059 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1060 if (buf
->buf
[i
] != r
->last
->end
[j
])
1063 if (r
->last
->end
[j
] == '\0' &&
1064 (buf
->buf
[i
] == '\0' ||
1065 buf
->buf
[i
] == ' ' ||
1066 buf
->buf
[i
] == '\t')) {
1068 roffnode_cleanscope(r
);
1070 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1074 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1082 * If we have no custom end-query or lookup failed, then try
1083 * pulling it out of the hashtable.
1086 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1088 if (t
!= ROFF_cblock
) {
1090 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1094 assert(roffs
[t
].proc
);
1095 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1099 roff_block_text(ROFF_ARGS
)
1103 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1109 roff_cond_sub(ROFF_ARGS
)
1116 roffnode_cleanscope(r
);
1117 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1120 * Fully handle known macros when they are structurally
1121 * required or when the conditional evaluated to true.
1124 if ((t
!= ROFF_MAX
) &&
1125 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1126 assert(roffs
[t
].proc
);
1127 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1131 * If `\}' occurs on a macro line without a preceding macro,
1132 * drop the line completely.
1135 ep
= buf
->buf
+ pos
;
1136 if (ep
[0] == '\\' && ep
[1] == '}')
1139 /* Always check for the closing delimiter `\}'. */
1141 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1142 if (*(++ep
) == '}') {
1144 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1148 return(rr
? ROFF_CONT
: ROFF_IGN
);
1152 roff_cond_text(ROFF_ARGS
)
1158 roffnode_cleanscope(r
);
1160 ep
= buf
->buf
+ pos
;
1161 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1162 if (*(++ep
) == '}') {
1164 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1168 return(rr
? ROFF_CONT
: ROFF_IGN
);
1172 * Parse a single signed integer number. Stop at the first non-digit.
1173 * If there is at least one digit, return success and advance the
1174 * parse point, else return failure and let the parse point unchanged.
1175 * Ignore overflows, treat them just like the C language.
1178 roff_getnum(const char *v
, int *pos
, int *res
)
1190 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1191 *res
= 10 * *res
+ v
[p
] - '0';
1203 * Evaluate a string comparison condition.
1204 * The first character is the delimiter.
1205 * Succeed if the string up to its second occurrence
1206 * matches the string up to its third occurence.
1207 * Advance the cursor after the third occurrence
1208 * or lacking that, to the end of the line.
1211 roff_evalstrcond(const char *v
, int *pos
)
1213 const char *s1
, *s2
, *s3
;
1217 s1
= v
+ *pos
; /* initial delimiter */
1218 s2
= s1
+ 1; /* for scanning the first string */
1219 s3
= strchr(s2
, *s1
); /* for scanning the second string */
1221 if (NULL
== s3
) /* found no middle delimiter */
1224 while ('\0' != *++s3
) {
1225 if (*s2
!= *s3
) { /* mismatch */
1226 s3
= strchr(s3
, *s1
);
1229 if (*s3
== *s1
) { /* found the final delimiter */
1238 s3
= strchr(s2
, '\0');
1239 else if (*s3
!= '\0')
1246 * Evaluate an optionally negated single character, numerical,
1247 * or string condition.
1250 roff_evalcond(struct roff
*r
, int ln
, const char *v
, int *pos
)
1252 int number
, savepos
, wanttrue
;
1254 if ('!' == v
[*pos
]) {
1286 if (roff_evalnum(r
, ln
, v
, pos
, &number
, 0))
1287 return((number
> 0) == wanttrue
);
1288 else if (*pos
== savepos
)
1289 return(roff_evalstrcond(v
, pos
) == wanttrue
);
1295 roff_line_ignore(ROFF_ARGS
)
1302 roff_cond(ROFF_ARGS
)
1305 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1308 * An `.el' has no conditional body: it will consume the value
1309 * of the current rstack entry set in prior `ie' calls or
1312 * If we're not an `el', however, then evaluate the conditional.
1315 r
->last
->rule
= tok
== ROFF_el
?
1316 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
1317 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
1320 * An if-else will put the NEGATION of the current evaluated
1321 * conditional into the stack of rules.
1324 if (tok
== ROFF_ie
) {
1325 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
1327 r
->rstack
= mandoc_reallocarray(r
->rstack
,
1328 r
->rstacksz
, sizeof(int));
1330 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
1333 /* If the parent has false as its rule, then so do we. */
1335 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
1340 * If there is nothing on the line after the conditional,
1341 * not even whitespace, use next-line scope.
1344 if (buf
->buf
[pos
] == '\0') {
1345 r
->last
->endspan
= 2;
1349 while (buf
->buf
[pos
] == ' ')
1352 /* An opening brace requests multiline scope. */
1354 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
1355 r
->last
->endspan
= -1;
1361 * Anything else following the conditional causes
1362 * single-line scope. Warn if the scope contains
1363 * nothing but trailing whitespace.
1366 if (buf
->buf
[pos
] == '\0')
1367 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
1368 ln
, ppos
, roffs
[tok
].name
);
1370 r
->last
->endspan
= 1;
1385 * The first word is the name of the string.
1386 * If it is empty or terminated by an escape sequence,
1387 * abort the `ds' request without defining anything.
1390 name
= string
= buf
->buf
+ pos
;
1394 namesz
= roff_getname(r
, &string
, ln
, pos
);
1395 if (name
[namesz
] == '\\')
1398 /* Read past the initial double-quote, if any. */
1402 /* The rest is the value. */
1403 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
1409 * Parse a single operator, one or two characters long.
1410 * If the operator is recognized, return success and advance the
1411 * parse point, else return failure and let the parse point unchanged.
1414 roff_getop(const char *v
, int *pos
, char *res
)
1435 switch (v
[*pos
+ 1]) {
1453 switch (v
[*pos
+ 1]) {
1467 if ('=' == v
[*pos
+ 1])
1479 * Evaluate either a parenthesized numeric expression
1480 * or a single signed integer number.
1483 roff_evalpar(struct roff
*r
, int ln
,
1484 const char *v
, int *pos
, int *res
)
1488 return(roff_getnum(v
, pos
, res
));
1491 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, 1))
1495 * Omission of the closing parenthesis
1496 * is an error in validation mode,
1497 * but ignored in evaluation mode.
1502 else if (NULL
== res
)
1509 * Evaluate a complete numeric expression.
1510 * Proceed left to right, there is no concept of precedence.
1513 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
1514 int *pos
, int *res
, int skipwhite
)
1516 int mypos
, operand2
;
1525 while (isspace((unsigned char)v
[*pos
]))
1528 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
))
1533 while (isspace((unsigned char)v
[*pos
]))
1536 if ( ! roff_getop(v
, pos
, &operator))
1540 while (isspace((unsigned char)v
[*pos
]))
1543 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
))
1547 while (isspace((unsigned char)v
[*pos
]))
1564 if (0 == operand2
) {
1565 mandoc_msg(MANDOCERR_DIVZERO
,
1566 r
->parse
, ln
, *pos
, v
);
1576 *res
= *res
< operand2
;
1579 *res
= *res
> operand2
;
1582 *res
= *res
<= operand2
;
1585 *res
= *res
>= operand2
;
1588 *res
= *res
== operand2
;
1591 *res
= *res
!= operand2
;
1594 *res
= *res
&& operand2
;
1597 *res
= *res
|| operand2
;
1600 if (operand2
< *res
)
1604 if (operand2
> *res
)
1615 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
1617 struct roffreg
*reg
;
1619 /* Search for an existing register with the same name. */
1622 while (reg
&& strcmp(name
, reg
->key
.p
))
1626 /* Create a new register. */
1627 reg
= mandoc_malloc(sizeof(struct roffreg
));
1628 reg
->key
.p
= mandoc_strdup(name
);
1629 reg
->key
.sz
= strlen(name
);
1631 reg
->next
= r
->regtab
;
1637 else if ('-' == sign
)
1644 * Handle some predefined read-only number registers.
1645 * For now, return -1 if the requested register is not predefined;
1646 * in case a predefined read-only register having the value -1
1647 * were to turn up, another special value would have to be chosen.
1650 roff_getregro(const char *name
)
1654 case 'A': /* ASCII approximation mode is always off. */
1656 case 'g': /* Groff compatibility mode is always on. */
1658 case 'H': /* Fixed horizontal resolution. */
1660 case 'j': /* Always adjust left margin only. */
1662 case 'T': /* Some output device is always defined. */
1664 case 'V': /* Fixed vertical resolution. */
1672 roff_getreg(const struct roff
*r
, const char *name
)
1674 struct roffreg
*reg
;
1677 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
1678 val
= roff_getregro(name
+ 1);
1683 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1684 if (0 == strcmp(name
, reg
->key
.p
))
1691 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
1693 struct roffreg
*reg
;
1696 if ('.' == name
[0] && 2 == len
) {
1697 val
= roff_getregro(name
+ 1);
1702 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1703 if (len
== reg
->key
.sz
&&
1704 0 == strncmp(name
, reg
->key
.p
, len
))
1711 roff_freereg(struct roffreg
*reg
)
1713 struct roffreg
*old_reg
;
1715 while (NULL
!= reg
) {
1731 key
= val
= buf
->buf
+ pos
;
1735 keysz
= roff_getname(r
, &val
, ln
, pos
);
1736 if (key
[keysz
] == '\\')
1741 if (sign
== '+' || sign
== '-')
1744 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, 0))
1745 roff_setreg(r
, key
, iv
, sign
);
1753 struct roffreg
*reg
, **prev
;
1757 name
= cp
= buf
->buf
+ pos
;
1760 namesz
= roff_getname(r
, &cp
, ln
, pos
);
1761 name
[namesz
] = '\0';
1766 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
1785 cp
= buf
->buf
+ pos
;
1786 while (*cp
!= '\0') {
1788 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
1789 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
1790 if (name
[namesz
] == '\\')
1803 /* Parse the number of lines. */
1804 cp
= buf
->buf
+ pos
;
1805 len
= strcspn(cp
, " \t");
1807 if ((iv
= mandoc_strntoi(cp
, len
, 10)) <= 0) {
1808 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
1809 ln
, ppos
, buf
->buf
+ 1);
1814 /* Arm the input line trap. */
1816 roffit_macro
= mandoc_strdup(cp
);
1823 const char *const *cp
;
1825 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
1826 for (cp
= __mdoc_reserved
; *cp
; cp
++)
1827 roff_setstr(r
, *cp
, NULL
, 0);
1830 r
->format
= MPARSE_MDOC
;
1838 const char *const *cp
;
1840 if ((r
->options
& MPARSE_QUICK
) == 0)
1841 for (cp
= __man_reserved
; *cp
; cp
++)
1842 roff_setstr(r
, *cp
, NULL
, 0);
1845 r
->format
= MPARSE_MAN
;
1855 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1868 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1871 tbl_restart(ppos
, ln
, r
->tbl
);
1877 * Handle in-line equation delimiters.
1880 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
1883 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
1886 * Outside equations, look for an opening delimiter.
1887 * If we are inside an equation, we already know it is
1888 * in-line, or this function wouldn't have been called;
1889 * so look for a closing delimiter.
1892 cp1
= buf
->buf
+ pos
;
1893 cp2
= strchr(cp1
, r
->eqn
== NULL
?
1894 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
1899 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
1901 /* Handle preceding text, protecting whitespace. */
1903 if (*buf
->buf
!= '\0') {
1910 * Prepare replacing the delimiter with an equation macro
1911 * and drop leading white space from the equation.
1914 if (r
->eqn
== NULL
) {
1921 /* Handle following text, protecting whitespace. */
1929 /* Do the actual replacement. */
1931 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
1932 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
1936 /* Toggle the in-line state of the eqn subsystem. */
1938 r
->eqn_inline
= r
->eqn
== NULL
;
1939 return(ROFF_REPARSE
);
1947 assert(r
->eqn
== NULL
);
1948 e
= eqn_alloc(ppos
, ln
, r
->parse
);
1951 r
->last_eqn
->next
= e
;
1952 e
->delim
= r
->last_eqn
->delim
;
1953 e
->odelim
= r
->last_eqn
->odelim
;
1954 e
->cdelim
= r
->last_eqn
->cdelim
;
1956 r
->first_eqn
= r
->last_eqn
= e
;
1958 r
->eqn
= r
->last_eqn
= e
;
1960 if (buf
->buf
[pos
] != '\0')
1961 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1962 ".EQ %s", buf
->buf
+ pos
);
1971 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
1978 struct tbl_node
*tbl
;
1981 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
1982 ln
, ppos
, "TS breaks TS");
1986 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
1989 r
->last_tbl
->next
= tbl
;
1991 r
->first_tbl
= r
->last_tbl
= tbl
;
1993 r
->tbl
= r
->last_tbl
= tbl
;
2004 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2008 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
2016 const char *p
, *first
, *second
;
2018 enum mandoc_esc esc
;
2023 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
2027 while (*p
!= '\0') {
2031 if (*first
== '\\') {
2032 esc
= mandoc_escape(&p
, NULL
, NULL
);
2033 if (esc
== ESCAPE_ERROR
) {
2034 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2035 ln
, (int)(p
- buf
->buf
), first
);
2038 fsz
= (size_t)(p
- first
);
2042 if (*second
== '\\') {
2043 esc
= mandoc_escape(&p
, NULL
, NULL
);
2044 if (esc
== ESCAPE_ERROR
) {
2045 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2046 ln
, (int)(p
- buf
->buf
), second
);
2049 ssz
= (size_t)(p
- second
);
2050 } else if (*second
== '\0') {
2051 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
2052 ln
, (int)(p
- buf
->buf
), NULL
);
2058 roff_setstrn(&r
->xmbtab
, first
, fsz
,
2063 if (r
->xtab
== NULL
)
2064 r
->xtab
= mandoc_calloc(128,
2065 sizeof(struct roffstr
));
2067 free(r
->xtab
[(int)*first
].p
);
2068 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
2069 r
->xtab
[(int)*first
].sz
= ssz
;
2080 name
= buf
->buf
+ pos
;
2081 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
2084 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2085 * opening anything that's not in our cwd or anything beneath
2086 * it. Thus, explicitly disallow traversing up the file-system
2087 * or using absolute paths.
2090 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
2091 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
2101 roff_userdef(ROFF_ARGS
)
2108 * Collect pointers to macro argument strings
2109 * and NUL-terminate them.
2111 cp
= buf
->buf
+ pos
;
2112 for (i
= 0; i
< 9; i
++)
2113 arg
[i
] = *cp
== '\0' ? "" :
2114 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
2117 * Expand macro arguments.
2120 n1
= cp
= mandoc_strdup(r
->current_string
);
2121 while ((cp
= strstr(cp
, "\\$")) != NULL
) {
2123 if (0 > i
|| 8 < i
) {
2124 /* Not an argument invocation. */
2129 buf
->sz
= mandoc_asprintf(&n2
, "%s%s%s",
2130 n1
, arg
[i
], cp
+ 3) + 1;
2131 cp
= n2
+ (cp
- n1
);
2137 * Replace the macro invocation
2138 * by the expanded macro.
2143 buf
->sz
= strlen(buf
->buf
) + 1;
2145 return(buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
2146 ROFF_REPARSE
: ROFF_APPEND
);
2150 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
2159 /* Read until end of name and terminate it with NUL. */
2160 for (cp
= name
; 1; cp
++) {
2161 if ('\0' == *cp
|| ' ' == *cp
) {
2168 if ('{' == cp
[1] || '}' == cp
[1])
2173 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
2174 "%.*s", (int)(cp
- name
+ 1), name
);
2175 mandoc_escape((const char **)&cp
, NULL
, NULL
);
2179 /* Read past spaces. */
2188 * Store *string into the user-defined string called *name.
2189 * To clear an existing entry, call with (*r, *name, NULL, 0).
2190 * append == 0: replace mode
2191 * append == 1: single-line append mode
2192 * append == 2: multiline append mode, append '\n' after each call
2195 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
2199 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
2200 string
? strlen(string
) : 0, append
);
2204 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
2205 const char *string
, size_t stringsz
, int append
)
2210 size_t oldch
, newch
;
2212 /* Search for an existing string with the same name. */
2215 while (n
&& (namesz
!= n
->key
.sz
||
2216 strncmp(n
->key
.p
, name
, namesz
)))
2220 /* Create a new string table entry. */
2221 n
= mandoc_malloc(sizeof(struct roffkv
));
2222 n
->key
.p
= mandoc_strndup(name
, namesz
);
2228 } else if (0 == append
) {
2238 * One additional byte for the '\n' in multiline mode,
2239 * and one for the terminating '\0'.
2241 newch
= stringsz
+ (1 < append
? 2u : 1u);
2243 if (NULL
== n
->val
.p
) {
2244 n
->val
.p
= mandoc_malloc(newch
);
2249 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
2252 /* Skip existing content in the destination buffer. */
2253 c
= n
->val
.p
+ (int)oldch
;
2255 /* Append new content to the destination buffer. */
2257 while (i
< (int)stringsz
) {
2259 * Rudimentary roff copy mode:
2260 * Handle escaped backslashes.
2262 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
2267 /* Append terminating bytes. */
2272 n
->val
.sz
= (int)(c
- n
->val
.p
);
2276 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
2278 const struct roffkv
*n
;
2281 for (n
= r
->strtab
; n
; n
= n
->next
)
2282 if (0 == strncmp(name
, n
->key
.p
, len
) &&
2283 '\0' == n
->key
.p
[(int)len
])
2286 for (i
= 0; i
< PREDEFS_MAX
; i
++)
2287 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
2288 '\0' == predefs
[i
].name
[(int)len
])
2289 return(predefs
[i
].str
);
2295 roff_freestr(struct roffkv
*r
)
2297 struct roffkv
*n
, *nn
;
2299 for (n
= r
; n
; n
= nn
) {
2307 const struct tbl_span
*
2308 roff_span(const struct roff
*r
)
2311 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
2315 roff_eqn(const struct roff
*r
)
2318 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
2322 * Duplicate an input string, making the appropriate character
2323 * conversations (as stipulated by `tr') along the way.
2324 * Returns a heap-allocated string with all the replacements made.
2327 roff_strdup(const struct roff
*r
, const char *p
)
2329 const struct roffkv
*cp
;
2333 enum mandoc_esc esc
;
2335 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
2336 return(mandoc_strdup(p
));
2337 else if ('\0' == *p
)
2338 return(mandoc_strdup(""));
2341 * Step through each character looking for term matches
2342 * (remember that a `tr' can be invoked with an escape, which is
2343 * a glyph but the escape is multi-character).
2344 * We only do this if the character hash has been initialised
2345 * and the string is >0 length.
2351 while ('\0' != *p
) {
2352 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
2353 sz
= r
->xtab
[(int)*p
].sz
;
2354 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2355 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
2359 } else if ('\\' != *p
) {
2360 res
= mandoc_realloc(res
, ssz
+ 2);
2365 /* Search for term matches. */
2366 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
2367 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
2372 * A match has been found.
2373 * Append the match to the array and move
2374 * forward by its keysize.
2376 res
= mandoc_realloc(res
,
2377 ssz
+ cp
->val
.sz
+ 1);
2378 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
2380 p
+= (int)cp
->key
.sz
;
2385 * Handle escapes carefully: we need to copy
2386 * over just the escape itself, or else we might
2387 * do replacements within the escape itself.
2388 * Make sure to pass along the bogus string.
2391 esc
= mandoc_escape(&p
, NULL
, NULL
);
2392 if (ESCAPE_ERROR
== esc
) {
2394 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2395 memcpy(res
+ ssz
, pp
, sz
);
2399 * We bail out on bad escapes.
2400 * No need to warn: we already did so when
2401 * roff_res() was called.
2404 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2405 memcpy(res
+ ssz
, pp
, sz
);
2409 res
[(int)ssz
] = '\0';
2414 roff_getformat(const struct roff
*r
)
2421 * Find out whether a line is a macro line or not.
2422 * If it is, adjust the current position and return one; if it isn't,
2423 * return zero and don't change the current position.
2424 * If the control character has been set with `.cc', then let that grain
2426 * This is slighly contrary to groff, where using the non-breaking
2427 * control character when `cc' has been invoked will cause the
2428 * non-breaking macro contents to be printed verbatim.
2431 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
2437 if (0 != r
->control
&& cp
[pos
] == r
->control
)
2439 else if (0 != r
->control
)
2441 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
2443 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
2448 while (' ' == cp
[pos
] || '\t' == cp
[pos
])