]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
7c46e9a593a96f9613227097720500374f585a3a
1 /* $Id: roff.c,v 1.200 2014/03/20 02:57:28 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.
30 #include "libmandoc.h"
32 /* Maximum number of nested if-else conditionals. */
33 #define RSTACK_MAX 128
35 /* Maximum number of string expansions per line, to break infinite loops. */
36 #define EXPAND_LIMIT 1000
80 * An incredibly-simple string buffer.
83 char *p
; /* nil-terminated buffer */
84 size_t sz
; /* saved strlen(p) */
88 * A key-value roffstr pair as part of a singly-linked list.
93 struct roffkv
*next
; /* next in list */
97 * A single number register as part of a singly-linked list.
102 struct roffreg
*next
;
106 struct mparse
*parse
; /* parse point */
107 int options
; /* parse options */
108 struct roffnode
*last
; /* leaf of stack */
109 int rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
110 char control
; /* control character */
111 int rstackpos
; /* position in rstack */
112 struct roffreg
*regtab
; /* number registers */
113 struct roffkv
*strtab
; /* user-defined strings & macros */
114 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
115 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
116 const char *current_string
; /* value of last called user macro */
117 struct tbl_node
*first_tbl
; /* first table parsed */
118 struct tbl_node
*last_tbl
; /* last table parsed */
119 struct tbl_node
*tbl
; /* current table being parsed */
120 struct eqn_node
*last_eqn
; /* last equation parsed */
121 struct eqn_node
*first_eqn
; /* first equation parsed */
122 struct eqn_node
*eqn
; /* current equation being parsed */
126 enum rofft tok
; /* type of node */
127 struct roffnode
*parent
; /* up one in stack */
128 int line
; /* parse line */
129 int col
; /* parse col */
130 char *name
; /* node name, e.g. macro name */
131 char *end
; /* end-rules: custom token */
132 int endspan
; /* end-rules: next-line or infty */
133 int rule
; /* current evaluation rule */
136 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
137 enum rofft tok, /* tok of macro */ \
138 char **bufp, /* input buffer */ \
139 size_t *szp, /* size of input buffer */ \
140 int ln, /* parse line */ \
141 int ppos, /* original pos in buffer */ \
142 int pos, /* current pos in buffer */ \
143 int *offs /* reset offset of buffer data */
145 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
148 const char *name
; /* macro name */
149 roffproc proc
; /* process new macro */
150 roffproc text
; /* process as child text of macro */
151 roffproc sub
; /* process as child of macro */
153 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
154 struct roffmac
*next
;
158 const char *name
; /* predefined input name */
159 const char *str
; /* replacement symbol */
162 #define PREDEF(__name, __str) \
163 { (__name), (__str) },
165 static enum rofft
roffhash_find(const char *, size_t);
166 static void roffhash_init(void);
167 static void roffnode_cleanscope(struct roff
*);
168 static void roffnode_pop(struct roff
*);
169 static void roffnode_push(struct roff
*, enum rofft
,
170 const char *, int, int);
171 static enum rofferr
roff_block(ROFF_ARGS
);
172 static enum rofferr
roff_block_text(ROFF_ARGS
);
173 static enum rofferr
roff_block_sub(ROFF_ARGS
);
174 static enum rofferr
roff_cblock(ROFF_ARGS
);
175 static enum rofferr
roff_cc(ROFF_ARGS
);
176 static void roff_ccond(struct roff
*, int, int);
177 static enum rofferr
roff_cond(ROFF_ARGS
);
178 static enum rofferr
roff_cond_text(ROFF_ARGS
);
179 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
180 static enum rofferr
roff_ds(ROFF_ARGS
);
181 static int roff_evalcond(const char *, int *);
182 static int roff_evalstrcond(const char *, int *);
183 static void roff_free1(struct roff
*);
184 static void roff_freereg(struct roffreg
*);
185 static void roff_freestr(struct roffkv
*);
186 static char *roff_getname(struct roff
*, char **, int, int);
187 static int roff_getnum(const char *, int *, int *);
188 static int roff_getop(const char *, int *, char *);
189 static int roff_getregn(const struct roff
*,
190 const char *, size_t);
191 static int roff_getregro(const char *name
);
192 static const char *roff_getstrn(const struct roff
*,
193 const char *, size_t);
194 static enum rofferr
roff_it(ROFF_ARGS
);
195 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
196 static enum rofferr
roff_nr(ROFF_ARGS
);
197 static void roff_openeqn(struct roff
*, const char *,
198 int, int, const char *);
199 static enum rofft
roff_parse(struct roff
*, const char *, int *);
200 static enum rofferr
roff_parsetext(char **, size_t *, int, int *);
201 static enum rofferr
roff_res(struct roff
*,
202 char **, size_t *, int, int);
203 static enum rofferr
roff_rm(ROFF_ARGS
);
204 static void roff_setstr(struct roff
*,
205 const char *, const char *, int);
206 static void roff_setstrn(struct roffkv
**, const char *,
207 size_t, const char *, size_t, int);
208 static enum rofferr
roff_so(ROFF_ARGS
);
209 static enum rofferr
roff_tr(ROFF_ARGS
);
210 static enum rofferr
roff_Dd(ROFF_ARGS
);
211 static enum rofferr
roff_TH(ROFF_ARGS
);
212 static enum rofferr
roff_TE(ROFF_ARGS
);
213 static enum rofferr
roff_TS(ROFF_ARGS
);
214 static enum rofferr
roff_EQ(ROFF_ARGS
);
215 static enum rofferr
roff_EN(ROFF_ARGS
);
216 static enum rofferr
roff_T_(ROFF_ARGS
);
217 static enum rofferr
roff_userdef(ROFF_ARGS
);
219 /* See roffhash_find() */
223 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
225 static struct roffmac
*hash
[HASHWIDTH
];
227 static struct roffmac roffs
[ROFF_MAX
] = {
228 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
229 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
230 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
231 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
232 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
233 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
234 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
235 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
236 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
237 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
238 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
239 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
240 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
241 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
242 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
243 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
244 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
245 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
246 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
247 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
248 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
249 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
250 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
251 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
252 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
253 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
254 { "ta", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
255 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
256 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
257 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
258 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
259 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
260 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
261 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
262 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
263 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
264 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
267 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
268 const char *const __mdoc_reserved
[] = {
269 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
270 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
271 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
272 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
273 "Dt", "Dv", "Dx", "D1",
274 "Ec", "Ed", "Ef", "Ek", "El", "Em",
275 "En", "Eo", "Er", "Es", "Ev", "Ex",
276 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
277 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
278 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
279 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
280 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
281 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
282 "Sc", "Sh", "Sm", "So", "Sq",
283 "Ss", "St", "Sx", "Sy",
284 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
285 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
286 "%P", "%Q", "%R", "%T", "%U", "%V",
290 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
291 const char *const __man_reserved
[] = {
292 "AT", "B", "BI", "BR", "DT",
293 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
294 "LP", "OP", "P", "PD", "PP",
295 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
296 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
300 /* Array of injected predefined strings. */
301 #define PREDEFS_MAX 38
302 static const struct predef predefs
[PREDEFS_MAX
] = {
303 #include "predefs.in"
306 /* See roffhash_find() */
307 #define ROFF_HASH(p) (p[0] - ASCII_LO)
309 static int roffit_lines
; /* number of lines to delay */
310 static char *roffit_macro
; /* nil-terminated macro line */
318 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
319 assert(roffs
[i
].name
[0] >= ASCII_LO
);
320 assert(roffs
[i
].name
[0] <= ASCII_HI
);
322 buc
= ROFF_HASH(roffs
[i
].name
);
324 if (NULL
!= (n
= hash
[buc
])) {
325 for ( ; n
->next
; n
= n
->next
)
329 hash
[buc
] = &roffs
[i
];
334 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
335 * the nil-terminated string name could be found.
338 roffhash_find(const char *p
, size_t s
)
344 * libroff has an extremely simple hashtable, for the time
345 * being, which simply keys on the first character, which must
346 * be printable, then walks a chain. It works well enough until
350 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
355 if (NULL
== (n
= hash
[buc
]))
357 for ( ; n
; n
= n
->next
)
358 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
359 return((enum rofft
)(n
- roffs
));
366 * Pop the current node off of the stack of roff instructions currently
370 roffnode_pop(struct roff
*r
)
377 r
->last
= r
->last
->parent
;
385 * Push a roff node onto the instruction stack. This must later be
386 * removed with roffnode_pop().
389 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
394 p
= mandoc_calloc(1, sizeof(struct roffnode
));
397 p
->name
= mandoc_strdup(name
);
401 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
408 roff_free1(struct roff
*r
)
410 struct tbl_node
*tbl
;
414 while (NULL
!= (tbl
= r
->first_tbl
)) {
415 r
->first_tbl
= tbl
->next
;
419 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
421 while (NULL
!= (e
= r
->first_eqn
)) {
422 r
->first_eqn
= e
->next
;
426 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
431 roff_freestr(r
->strtab
);
432 roff_freestr(r
->xmbtab
);
434 r
->strtab
= r
->xmbtab
= NULL
;
436 roff_freereg(r
->regtab
);
441 for (i
= 0; i
< 128; i
++)
449 roff_reset(struct roff
*r
)
458 roff_free(struct roff
*r
)
467 roff_alloc(struct mparse
*parse
, int options
)
471 r
= mandoc_calloc(1, sizeof(struct roff
));
473 r
->options
= options
;
482 * In the current line, expand user-defined strings ("\*")
483 * and references to number registers ("\n").
484 * Also check the syntax of other escape sequences.
487 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int ln
, int pos
)
489 char ubuf
[12]; /* buffer to print the number */
490 const char *stesc
; /* start of an escape sequence ('\\') */
491 const char *stnam
; /* start of the name, after "[(*" */
492 const char *cp
; /* end of the name, e.g. before ']' */
493 const char *res
; /* the string to be substituted */
494 char *nbuf
; /* new buffer to copy bufp to */
495 size_t nsz
; /* size of the new buffer */
496 size_t maxl
; /* expected length of the escape name */
497 size_t naml
; /* actual length of the escape name */
498 int expand_count
; /* to avoid infinite loops */
504 while (NULL
!= (cp
= strchr(cp
, '\\'))) {
508 * The second character must be an asterisk or an n.
509 * If it isn't, skip it anyway: It is escaped,
510 * so it can't start another escape sequence.
524 if (ESCAPE_ERROR
!= mandoc_escape(&cp
, NULL
, NULL
))
527 (MANDOCERR_BADESCAPE
, r
->parse
,
528 ln
, (int)(stesc
- *bufp
), NULL
);
535 * The third character decides the length
536 * of the name of the string or register.
537 * Save a pointer to the name.
557 /* Advance to the end of the name. */
559 for (naml
= 0; 0 == maxl
|| naml
< maxl
; naml
++, cp
++) {
562 (MANDOCERR_BADESCAPE
,
564 (int)(stesc
- *bufp
), NULL
);
567 if (0 == maxl
&& ']' == *cp
)
572 * Retrieve the replacement string; if it is
573 * undefined, resume searching for escapes.
577 res
= roff_getstrn(r
, stnam
, naml
);
579 snprintf(ubuf
, sizeof(ubuf
), "%d",
580 roff_getregn(r
, stnam
, naml
));
584 (MANDOCERR_BADESCAPE
, r
->parse
,
585 ln
, (int)(stesc
- *bufp
), NULL
);
589 /* Replace the escape sequence by the string. */
593 nsz
= *szp
+ strlen(res
) + 1;
594 nbuf
= mandoc_malloc(nsz
);
596 strlcpy(nbuf
, *bufp
, (size_t)(stesc
- *bufp
+ 1));
597 strlcat(nbuf
, res
, nsz
);
598 strlcat(nbuf
, cp
+ (maxl
? 0 : 1), nsz
);
605 if (EXPAND_LIMIT
>= ++expand_count
)
608 /* Just leave the string unexpanded. */
609 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
, ln
, pos
, NULL
);
616 * Process text streams:
617 * Convert all breakable hyphens into ASCII_HYPH.
618 * Decrement and spring input line trap.
621 roff_parsetext(char **bufp
, size_t *szp
, int pos
, int *offs
)
629 start
= p
= *bufp
+ pos
;
632 sz
= strcspn(p
, "-\\");
639 /* Skip over escapes. */
641 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
642 if (ESCAPE_ERROR
== esc
)
645 } else if (p
== start
) {
650 if (isalpha((unsigned char)p
[-1]) &&
651 isalpha((unsigned char)p
[1]))
656 /* Spring the input line trap. */
657 if (1 == roffit_lines
) {
658 isz
= asprintf(&p
, "%s\n.%s", *bufp
, roffit_macro
);
661 exit((int)MANDOCLEVEL_SYSERR
);
669 return(ROFF_REPARSE
);
670 } else if (1 < roffit_lines
)
676 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
677 size_t *szp
, int pos
, int *offs
)
684 * Run the reserved-word filter only if we have some reserved
688 e
= roff_res(r
, bufp
, szp
, ln
, pos
);
691 assert(ROFF_CONT
== e
);
694 ctl
= roff_getcontrol(r
, *bufp
, &pos
);
697 * First, if a scope is open and we're not a macro, pass the
698 * text through the macro's filter. If a scope isn't open and
699 * we're not a macro, just let it through.
700 * Finally, if there's an equation scope open, divert it into it
701 * no matter our state.
704 if (r
->last
&& ! ctl
) {
706 assert(roffs
[t
].text
);
708 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
);
709 assert(ROFF_IGN
== e
|| ROFF_CONT
== e
);
714 return(eqn_read(&r
->eqn
, ln
, *bufp
, ppos
, offs
));
717 return(tbl_read(r
->tbl
, ln
, *bufp
, pos
));
718 return(roff_parsetext(bufp
, szp
, pos
, offs
));
722 * If a scope is open, go to the child handler for that macro,
723 * as it may want to preprocess before doing anything with it.
724 * Don't do so if an equation is open.
729 assert(roffs
[t
].sub
);
730 return((*roffs
[t
].sub
)
732 ln
, ppos
, pos
, offs
));
736 * Lastly, as we've no scope open, try to look up and execute
737 * the new macro. If no macro is found, simply return and let
738 * the compilers handle it.
741 if (ROFF_MAX
== (t
= roff_parse(r
, *bufp
, &pos
)))
744 assert(roffs
[t
].proc
);
745 return((*roffs
[t
].proc
)
747 ln
, ppos
, pos
, offs
));
752 roff_endparse(struct roff
*r
)
756 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
757 r
->last
->line
, r
->last
->col
, NULL
);
760 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
761 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, NULL
);
766 mandoc_msg(MANDOCERR_SCOPEEXIT
, r
->parse
,
767 r
->tbl
->line
, r
->tbl
->pos
, NULL
);
773 * Parse a roff node's type from the input buffer. This must be in the
774 * form of ".foo xxx" in the usual way.
777 roff_parse(struct roff
*r
, const char *buf
, int *pos
)
783 if ('\0' == buf
[*pos
] || '"' == buf
[*pos
] ||
784 '\t' == buf
[*pos
] || ' ' == buf
[*pos
])
787 /* We stop the macro parse at an escape, tab, space, or nil. */
790 maclen
= strcspn(mac
, " \\\t\0");
792 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
793 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
797 while (buf
[*pos
] && ' ' == buf
[*pos
])
805 roff_cblock(ROFF_ARGS
)
809 * A block-close `..' should only be invoked as a child of an
810 * ignore macro, otherwise raise a warning and just ignore it.
813 if (NULL
== r
->last
) {
814 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
818 switch (r
->last
->tok
) {
826 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
833 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
838 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
841 roffnode_cleanscope(r
);
848 roffnode_cleanscope(struct roff
*r
)
852 if (--r
->last
->endspan
!= 0)
860 roff_ccond(struct roff
*r
, int ln
, int ppos
)
863 if (NULL
== r
->last
) {
864 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
868 switch (r
->last
->tok
) {
876 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
880 if (r
->last
->endspan
> -1) {
881 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
886 roffnode_cleanscope(r
);
893 roff_block(ROFF_ARGS
)
901 if (ROFF_ig
!= tok
) {
902 if ('\0' == (*bufp
)[pos
]) {
903 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
908 * Re-write `de1', since we don't really care about
909 * groff's strange compatibility mode, into `de'.
917 mandoc_msg(MANDOCERR_REQUEST
, r
->parse
, ln
, ppos
,
920 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
923 while (isspace((unsigned char)(*bufp
)[pos
]))
924 (*bufp
)[pos
++] = '\0';
927 roffnode_push(r
, tok
, name
, ln
, ppos
);
930 * At the beginning of a `de' macro, clear the existing string
931 * with the same name, if there is one. New content will be
932 * appended from roff_block_text() in multiline mode.
936 roff_setstr(r
, name
, "", 0);
938 if ('\0' == (*bufp
)[pos
])
941 /* If present, process the custom end-of-line marker. */
944 while ((*bufp
)[pos
] && ! isspace((unsigned char)(*bufp
)[pos
]))
948 * Note: groff does NOT like escape characters in the input.
949 * Instead of detecting this, we're just going to let it fly and
954 sz
= (size_t)(pos
- sv
);
956 if (1 == sz
&& '.' == (*bufp
)[sv
])
959 r
->last
->end
= mandoc_malloc(sz
+ 1);
961 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
962 r
->last
->end
[(int)sz
] = '\0';
965 mandoc_msg(MANDOCERR_ARGSLOST
, r
->parse
, ln
, pos
, NULL
);
973 roff_block_sub(ROFF_ARGS
)
979 * First check whether a custom macro exists at this level. If
980 * it does, then check against it. This is some of groff's
981 * stranger behaviours. If we encountered a custom end-scope
982 * tag and that tag also happens to be a "real" macro, then we
983 * need to try interpreting it again as a real macro. If it's
984 * not, then return ignore. Else continue.
988 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
989 if ((*bufp
)[i
] != r
->last
->end
[j
])
992 if ('\0' == r
->last
->end
[j
] &&
993 ('\0' == (*bufp
)[i
] ||
995 '\t' == (*bufp
)[i
])) {
997 roffnode_cleanscope(r
);
999 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
1003 if (ROFF_MAX
!= roff_parse(r
, *bufp
, &pos
))
1010 * If we have no custom end-query or lookup failed, then try
1011 * pulling it out of the hashtable.
1014 t
= roff_parse(r
, *bufp
, &pos
);
1017 * Macros other than block-end are only significant
1018 * in `de' blocks; elsewhere, simply throw them away.
1020 if (ROFF_cblock
!= t
) {
1022 roff_setstr(r
, r
->last
->name
, *bufp
+ ppos
, 2);
1026 assert(roffs
[t
].proc
);
1027 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1028 ln
, ppos
, pos
, offs
));
1034 roff_block_text(ROFF_ARGS
)
1038 roff_setstr(r
, r
->last
->name
, *bufp
+ pos
, 2);
1046 roff_cond_sub(ROFF_ARGS
)
1053 roffnode_cleanscope(r
);
1054 t
= roff_parse(r
, *bufp
, &pos
);
1057 * Fully handle known macros when they are structurally
1058 * required or when the conditional evaluated to true.
1061 if ((ROFF_MAX
!= t
) &&
1062 (rr
|| ROFFMAC_STRUCT
& roffs
[t
].flags
)) {
1063 assert(roffs
[t
].proc
);
1064 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
1065 ln
, ppos
, pos
, offs
));
1069 * If `\}' occurs on a macro line without a preceding macro,
1070 * drop the line completely.
1074 if ('\\' == ep
[0] && '}' == ep
[1])
1077 /* Always check for the closing delimiter `\}'. */
1079 while (NULL
!= (ep
= strchr(ep
, '\\'))) {
1080 if ('}' == *(++ep
)) {
1082 roff_ccond(r
, ln
, ep
- *bufp
- 1);
1086 return(rr
? ROFF_CONT
: ROFF_IGN
);
1091 roff_cond_text(ROFF_ARGS
)
1097 roffnode_cleanscope(r
);
1100 while (NULL
!= (ep
= strchr(ep
, '\\'))) {
1101 if ('}' == *(++ep
)) {
1103 roff_ccond(r
, ln
, ep
- *bufp
- 1);
1107 return(rr
? ROFF_CONT
: ROFF_IGN
);
1111 roff_getnum(const char *v
, int *pos
, int *res
)
1120 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1121 *res
+= 10 * *res
+ v
[p
] - '0';
1133 roff_getop(const char *v
, int *pos
, char *res
)
1138 e
= v
[*pos
+ 1] == '=';
1161 * Evaluate a string comparison condition.
1162 * The first character is the delimiter.
1163 * Succeed if the string up to its second occurrence
1164 * matches the string up to its third occurence.
1165 * Advance the cursor after the third occurrence
1166 * or lacking that, to the end of the line.
1169 roff_evalstrcond(const char *v
, int *pos
)
1171 const char *s1
, *s2
, *s3
;
1175 s1
= v
+ *pos
; /* initial delimiter */
1176 s2
= s1
+ 1; /* for scanning the first string */
1177 s3
= strchr(s2
, *s1
); /* for scanning the second string */
1179 if (NULL
== s3
) /* found no middle delimiter */
1182 while ('\0' != *++s3
) {
1183 if (*s2
!= *s3
) { /* mismatch */
1184 s3
= strchr(s3
, *s1
);
1187 if (*s3
== *s1
) { /* found the final delimiter */
1196 s3
= strchr(s2
, '\0');
1204 roff_evalcond(const char *v
, int *pos
)
1206 int wanttrue
, lh
, rh
;
1209 if ('!' == v
[*pos
]) {
1236 if (!roff_getnum(v
, pos
, &lh
))
1237 return(roff_evalstrcond(v
, pos
) == wanttrue
);
1238 if (!roff_getop(v
, pos
, &op
))
1239 return((lh
> 0) == wanttrue
);
1240 if (!roff_getnum(v
, pos
, &rh
))
1245 return((lh
>= rh
) == wanttrue
);
1247 return((lh
<= rh
) == wanttrue
);
1249 return((lh
== rh
) == wanttrue
);
1251 return((lh
> rh
) == wanttrue
);
1253 return((lh
< rh
) == wanttrue
);
1261 roff_line_ignore(ROFF_ARGS
)
1269 roff_cond(ROFF_ARGS
)
1272 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1275 * An `.el' has no conditional body: it will consume the value
1276 * of the current rstack entry set in prior `ie' calls or
1279 * If we're not an `el', however, then evaluate the conditional.
1282 r
->last
->rule
= ROFF_el
== tok
?
1283 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
1284 roff_evalcond(*bufp
, &pos
);
1287 * An if-else will put the NEGATION of the current evaluated
1288 * conditional into the stack of rules.
1291 if (ROFF_ie
== tok
) {
1292 if (r
->rstackpos
== RSTACK_MAX
- 1) {
1293 mandoc_msg(MANDOCERR_MEM
,
1294 r
->parse
, ln
, ppos
, NULL
);
1297 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
1300 /* If the parent has false as its rule, then so do we. */
1302 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
1307 * If there is nothing on the line after the conditional,
1308 * not even whitespace, use next-line scope.
1311 if ('\0' == (*bufp
)[pos
]) {
1312 r
->last
->endspan
= 2;
1316 while (' ' == (*bufp
)[pos
])
1319 /* An opening brace requests multiline scope. */
1321 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
1322 r
->last
->endspan
= -1;
1328 * Anything else following the conditional causes
1329 * single-line scope. Warn if the scope contains
1330 * nothing but trailing whitespace.
1333 if ('\0' == (*bufp
)[pos
])
1334 mandoc_msg(MANDOCERR_NOARGS
, r
->parse
, ln
, ppos
, NULL
);
1336 r
->last
->endspan
= 1;
1348 char *name
, *string
;
1351 * A symbol is named by the first word following the macro
1352 * invocation up to a space. Its value is anything after the
1353 * name's trailing whitespace and optional double-quote. Thus,
1357 * will have `bar " ' as its value.
1360 string
= *bufp
+ pos
;
1361 name
= roff_getname(r
, &string
, ln
, pos
);
1365 /* Read past initial double-quote. */
1369 /* The rest is the value. */
1370 roff_setstr(r
, name
, string
, ROFF_as
== tok
);
1375 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
1377 struct roffreg
*reg
;
1379 /* Search for an existing register with the same name. */
1382 while (reg
&& strcmp(name
, reg
->key
.p
))
1386 /* Create a new register. */
1387 reg
= mandoc_malloc(sizeof(struct roffreg
));
1388 reg
->key
.p
= mandoc_strdup(name
);
1389 reg
->key
.sz
= strlen(name
);
1391 reg
->next
= r
->regtab
;
1397 else if ('-' == sign
)
1404 * Handle some predefined read-only number registers.
1405 * For now, return -1 if the requested register is not predefined;
1406 * in case a predefined read-only register having the value -1
1407 * were to turn up, another special value would have to be chosen.
1410 roff_getregro(const char *name
)
1414 case ('A'): /* ASCII approximation mode is always off. */
1416 case ('g'): /* Groff compatibility mode is always on. */
1418 case ('H'): /* Fixed horizontal resolution. */
1420 case ('j'): /* Always adjust left margin only. */
1422 case ('T'): /* Some output device is always defined. */
1424 case ('V'): /* Fixed vertical resolution. */
1432 roff_getreg(const struct roff
*r
, const char *name
)
1434 struct roffreg
*reg
;
1437 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
1438 val
= roff_getregro(name
+ 1);
1443 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1444 if (0 == strcmp(name
, reg
->key
.p
))
1451 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
1453 struct roffreg
*reg
;
1456 if ('.' == name
[0] && 2 == len
) {
1457 val
= roff_getregro(name
+ 1);
1462 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
1463 if (len
== reg
->key
.sz
&&
1464 0 == strncmp(name
, reg
->key
.p
, len
))
1471 roff_freereg(struct roffreg
*reg
)
1473 struct roffreg
*old_reg
;
1475 while (NULL
!= reg
) {
1494 key
= roff_getname(r
, &val
, ln
, pos
);
1497 if ('+' == sign
|| '-' == sign
)
1500 sz
= strspn(val
, "0123456789");
1501 iv
= sz
? mandoc_strntoi(val
, sz
, 10) : 0;
1503 roff_setreg(r
, key
, iv
, sign
);
1516 while ('\0' != *cp
) {
1517 name
= roff_getname(r
, &cp
, ln
, (int)(cp
- *bufp
));
1519 roff_setstr(r
, name
, NULL
, 0);
1532 /* Parse the number of lines. */
1534 len
= strcspn(cp
, " \t");
1536 if ((iv
= mandoc_strntoi(cp
, len
, 10)) <= 0) {
1537 mandoc_msg(MANDOCERR_NUMERIC
, r
->parse
,
1538 ln
, ppos
, *bufp
+ 1);
1543 /* Arm the input line trap. */
1545 roffit_macro
= mandoc_strdup(cp
);
1553 const char *const *cp
;
1555 if (0 == ((MPARSE_MDOC
| MPARSE_QUICK
) & r
->options
))
1556 for (cp
= __mdoc_reserved
; *cp
; cp
++)
1557 roff_setstr(r
, *cp
, NULL
, 0);
1566 const char *const *cp
;
1568 if (0 == (MPARSE_QUICK
& r
->options
))
1569 for (cp
= __man_reserved
; *cp
; cp
++)
1570 roff_setstr(r
, *cp
, NULL
, 0);
1581 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1594 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1596 tbl_restart(ppos
, ln
, r
->tbl
);
1603 roff_closeeqn(struct roff
*r
)
1606 return(r
->eqn
&& ROFF_EQN
== eqn_end(&r
->eqn
) ? 1 : 0);
1611 roff_openeqn(struct roff
*r
, const char *name
, int line
,
1612 int offs
, const char *buf
)
1617 assert(NULL
== r
->eqn
);
1618 e
= eqn_alloc(name
, offs
, line
, r
->parse
);
1621 r
->last_eqn
->next
= e
;
1623 r
->first_eqn
= r
->last_eqn
= e
;
1625 r
->eqn
= r
->last_eqn
= e
;
1629 eqn_read(&r
->eqn
, line
, buf
, offs
, &poff
);
1638 roff_openeqn(r
, *bufp
+ pos
, ln
, ppos
, NULL
);
1647 mandoc_msg(MANDOCERR_NOSCOPE
, r
->parse
, ln
, ppos
, NULL
);
1655 struct tbl_node
*tbl
;
1658 mandoc_msg(MANDOCERR_SCOPEBROKEN
, r
->parse
, ln
, ppos
, NULL
);
1662 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
1665 r
->last_tbl
->next
= tbl
;
1667 r
->first_tbl
= r
->last_tbl
= tbl
;
1669 r
->tbl
= r
->last_tbl
= tbl
;
1681 if ('\0' == *p
|| '.' == (r
->control
= *p
++))
1685 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1694 const char *p
, *first
, *second
;
1696 enum mandoc_esc esc
;
1701 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
, ln
, ppos
, NULL
);
1705 while ('\0' != *p
) {
1709 if ('\\' == *first
) {
1710 esc
= mandoc_escape(&p
, NULL
, NULL
);
1711 if (ESCAPE_ERROR
== esc
) {
1713 (MANDOCERR_BADESCAPE
, r
->parse
,
1714 ln
, (int)(p
- *bufp
), NULL
);
1717 fsz
= (size_t)(p
- first
);
1721 if ('\\' == *second
) {
1722 esc
= mandoc_escape(&p
, NULL
, NULL
);
1723 if (ESCAPE_ERROR
== esc
) {
1725 (MANDOCERR_BADESCAPE
, r
->parse
,
1726 ln
, (int)(p
- *bufp
), NULL
);
1729 ssz
= (size_t)(p
- second
);
1730 } else if ('\0' == *second
) {
1731 mandoc_msg(MANDOCERR_ARGCOUNT
, r
->parse
,
1732 ln
, (int)(p
- *bufp
), NULL
);
1738 roff_setstrn(&r
->xmbtab
, first
,
1739 fsz
, second
, ssz
, 0);
1743 if (NULL
== r
->xtab
)
1744 r
->xtab
= mandoc_calloc
1745 (128, sizeof(struct roffstr
));
1747 free(r
->xtab
[(int)*first
].p
);
1748 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
1749 r
->xtab
[(int)*first
].sz
= ssz
;
1761 mandoc_msg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, NULL
);
1764 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
1765 * opening anything that's not in our cwd or anything beneath
1766 * it. Thus, explicitly disallow traversing up the file-system
1767 * or using absolute paths.
1771 if ('/' == *name
|| strstr(name
, "../") || strstr(name
, "/..")) {
1772 mandoc_msg(MANDOCERR_SOPATH
, r
->parse
, ln
, pos
, NULL
);
1782 roff_userdef(ROFF_ARGS
)
1789 * Collect pointers to macro argument strings
1790 * and NUL-terminate them.
1793 for (i
= 0; i
< 9; i
++)
1794 arg
[i
] = '\0' == *cp
? "" :
1795 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
1798 * Expand macro arguments.
1801 n1
= cp
= mandoc_strdup(r
->current_string
);
1802 while (NULL
!= (cp
= strstr(cp
, "\\$"))) {
1804 if (0 > i
|| 8 < i
) {
1805 /* Not an argument invocation. */
1810 *szp
= strlen(n1
) - 3 + strlen(arg
[i
]) + 1;
1811 n2
= mandoc_malloc(*szp
);
1813 strlcpy(n2
, n1
, (size_t)(cp
- n1
+ 1));
1814 strlcat(n2
, arg
[i
], *szp
);
1815 strlcat(n2
, cp
+ 3, *szp
);
1817 cp
= n2
+ (cp
- n1
);
1823 * Replace the macro invocation
1824 * by the expanded macro.
1829 *szp
= strlen(*bufp
) + 1;
1831 return(*szp
> 1 && '\n' == (*bufp
)[(int)*szp
- 2] ?
1832 ROFF_REPARSE
: ROFF_APPEND
);
1836 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
1844 /* Read until end of name. */
1845 for (cp
= name
; '\0' != *cp
&& ' ' != *cp
; cp
++) {
1851 mandoc_msg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
, NULL
);
1856 /* Nil-terminate name. */
1860 /* Read past spaces. */
1869 * Store *string into the user-defined string called *name.
1870 * To clear an existing entry, call with (*r, *name, NULL, 0).
1871 * append == 0: replace mode
1872 * append == 1: single-line append mode
1873 * append == 2: multiline append mode, append '\n' after each call
1876 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
1880 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
1881 string
? strlen(string
) : 0, append
);
1885 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
1886 const char *string
, size_t stringsz
, int append
)
1891 size_t oldch
, newch
;
1893 /* Search for an existing string with the same name. */
1896 while (n
&& strcmp(name
, n
->key
.p
))
1900 /* Create a new string table entry. */
1901 n
= mandoc_malloc(sizeof(struct roffkv
));
1902 n
->key
.p
= mandoc_strndup(name
, namesz
);
1908 } else if (0 == append
) {
1918 * One additional byte for the '\n' in multiline mode,
1919 * and one for the terminating '\0'.
1921 newch
= stringsz
+ (1 < append
? 2u : 1u);
1923 if (NULL
== n
->val
.p
) {
1924 n
->val
.p
= mandoc_malloc(newch
);
1929 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
1932 /* Skip existing content in the destination buffer. */
1933 c
= n
->val
.p
+ (int)oldch
;
1935 /* Append new content to the destination buffer. */
1937 while (i
< (int)stringsz
) {
1939 * Rudimentary roff copy mode:
1940 * Handle escaped backslashes.
1942 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
1947 /* Append terminating bytes. */
1952 n
->val
.sz
= (int)(c
- n
->val
.p
);
1956 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1958 const struct roffkv
*n
;
1961 for (n
= r
->strtab
; n
; n
= n
->next
)
1962 if (0 == strncmp(name
, n
->key
.p
, len
) &&
1963 '\0' == n
->key
.p
[(int)len
])
1966 for (i
= 0; i
< PREDEFS_MAX
; i
++)
1967 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
1968 '\0' == predefs
[i
].name
[(int)len
])
1969 return(predefs
[i
].str
);
1975 roff_freestr(struct roffkv
*r
)
1977 struct roffkv
*n
, *nn
;
1979 for (n
= r
; n
; n
= nn
) {
1987 const struct tbl_span
*
1988 roff_span(const struct roff
*r
)
1991 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
1995 roff_eqn(const struct roff
*r
)
1998 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
2002 * Duplicate an input string, making the appropriate character
2003 * conversations (as stipulated by `tr') along the way.
2004 * Returns a heap-allocated string with all the replacements made.
2007 roff_strdup(const struct roff
*r
, const char *p
)
2009 const struct roffkv
*cp
;
2013 enum mandoc_esc esc
;
2015 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
2016 return(mandoc_strdup(p
));
2017 else if ('\0' == *p
)
2018 return(mandoc_strdup(""));
2021 * Step through each character looking for term matches
2022 * (remember that a `tr' can be invoked with an escape, which is
2023 * a glyph but the escape is multi-character).
2024 * We only do this if the character hash has been initialised
2025 * and the string is >0 length.
2031 while ('\0' != *p
) {
2032 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
2033 sz
= r
->xtab
[(int)*p
].sz
;
2034 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2035 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
2039 } else if ('\\' != *p
) {
2040 res
= mandoc_realloc(res
, ssz
+ 2);
2045 /* Search for term matches. */
2046 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
2047 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
2052 * A match has been found.
2053 * Append the match to the array and move
2054 * forward by its keysize.
2056 res
= mandoc_realloc
2057 (res
, ssz
+ cp
->val
.sz
+ 1);
2058 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
2060 p
+= (int)cp
->key
.sz
;
2065 * Handle escapes carefully: we need to copy
2066 * over just the escape itself, or else we might
2067 * do replacements within the escape itself.
2068 * Make sure to pass along the bogus string.
2071 esc
= mandoc_escape(&p
, NULL
, NULL
);
2072 if (ESCAPE_ERROR
== esc
) {
2074 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2075 memcpy(res
+ ssz
, pp
, sz
);
2079 * We bail out on bad escapes.
2080 * No need to warn: we already did so when
2081 * roff_res() was called.
2084 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2085 memcpy(res
+ ssz
, pp
, sz
);
2089 res
[(int)ssz
] = '\0';
2094 * Find out whether a line is a macro line or not.
2095 * If it is, adjust the current position and return one; if it isn't,
2096 * return zero and don't change the current position.
2097 * If the control character has been set with `.cc', then let that grain
2099 * This is slighly contrary to groff, where using the non-breaking
2100 * control character when `cc' has been invoked will cause the
2101 * non-breaking macro contents to be printed verbatim.
2104 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
2110 if (0 != r
->control
&& cp
[pos
] == r
->control
)
2112 else if (0 != r
->control
)
2114 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
2116 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
2121 while (' ' == cp
[pos
] || '\t' == cp
[pos
])