]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.86 2010/06/01 11:47:28 kristaps Exp $ */
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #define RSTACK_MAX 128
33 ('.' == (c) || '\'' == (c))
60 struct roffnode
*last
; /* leaf of stack */
61 mandocmsg msg
; /* err/warn/fatal messages */
62 void *data
; /* privdata for messages */
63 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
64 int rstackpos
; /* position in rstack */
68 enum rofft tok
; /* type of node */
69 struct roffnode
*parent
; /* up one in stack */
70 int line
; /* parse line */
71 int col
; /* parse col */
72 char *end
; /* end-rules: custom token */
73 int endspan
; /* end-rules: next-line or infty */
74 enum roffrule rule
; /* current evaluation rule */
77 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
78 enum rofft tok, /* tok of macro */ \
79 char **bufp, /* input buffer */ \
80 size_t *szp, /* size of input buffer */ \
81 int ln, /* parse line */ \
82 int ppos, /* original pos in buffer */ \
83 int pos, /* current pos in buffer */ \
84 int *offs /* reset offset of buffer data */
86 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
89 const char *name
; /* macro name */
90 roffproc proc
; /* process new macro */
91 roffproc text
; /* process as child text of macro */
92 roffproc sub
; /* process as child of macro */
94 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
98 static enum rofferr
roff_block(ROFF_ARGS
);
99 static enum rofferr
roff_block_text(ROFF_ARGS
);
100 static enum rofferr
roff_block_sub(ROFF_ARGS
);
101 static enum rofferr
roff_cblock(ROFF_ARGS
);
102 static enum rofferr
roff_ccond(ROFF_ARGS
);
103 static enum rofferr
roff_cond(ROFF_ARGS
);
104 static enum rofferr
roff_cond_text(ROFF_ARGS
);
105 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
106 static enum rofferr
roff_line(ROFF_ARGS
);
108 /* See roff_hash_find() */
112 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
114 static struct roffmac
*hash
[HASHWIDTH
];
116 static struct roffmac roffs
[ROFF_MAX
] = {
117 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
118 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
119 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
120 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
121 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
122 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
123 { "ds", roff_line
, NULL
, NULL
, 0, NULL
},
124 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
125 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
126 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
127 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
128 { "rm", roff_line
, NULL
, NULL
, 0, NULL
},
129 { "tr", roff_line
, NULL
, NULL
, 0, NULL
},
130 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
131 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
134 static void roff_free1(struct roff
*);
135 static enum rofft
roff_hash_find(const char *);
136 static void roff_hash_init(void);
137 static void roffnode_cleanscope(struct roff
*);
138 static int roffnode_push(struct roff
*,
139 enum rofft
, int, int);
140 static void roffnode_pop(struct roff
*);
141 static enum rofft
roff_parse(const char *, int *);
143 /* See roff_hash_find() */
144 #define ROFF_HASH(p) (p[0] - ASCII_LO)
152 for (i
= 0; i
< (int)ROFF_MAX
; i
++) {
153 assert(roffs
[i
].name
[0] >= ASCII_LO
);
154 assert(roffs
[i
].name
[0] <= ASCII_HI
);
156 buc
= ROFF_HASH(roffs
[i
].name
);
158 if (NULL
!= (n
= hash
[buc
])) {
159 for ( ; n
->next
; n
= n
->next
)
163 hash
[buc
] = &roffs
[i
];
169 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
170 * the nil-terminated string name could be found.
173 roff_hash_find(const char *p
)
179 * libroff has an extremely simple hashtable, for the time
180 * being, which simply keys on the first character, which must
181 * be printable, then walks a chain. It works well enough until
185 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
190 if (NULL
== (n
= hash
[buc
]))
192 for ( ; n
; n
= n
->next
)
193 if (0 == strcmp(n
->name
, p
))
194 return((enum rofft
)(n
- roffs
));
201 * Pop the current node off of the stack of roff instructions currently
205 roffnode_pop(struct roff
*r
)
212 if (ROFF_el
== p
->tok
)
213 if (r
->rstackpos
> -1)
216 r
->last
= r
->last
->parent
;
224 * Push a roff node onto the instruction stack. This must later be
225 * removed with roffnode_pop().
228 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
232 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
233 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
241 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
249 roff_free1(struct roff
*r
)
258 roff_reset(struct roff
*r
)
266 roff_free(struct roff
*r
)
275 roff_alloc(const mandocmsg msg
, void *data
)
279 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
280 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
294 roff_parseln(struct roff
*r
, int ln
,
295 char **bufp
, size_t *szp
, int pos
, int *offs
)
301 * First, if a scope is open and we're not a macro, pass the
302 * text through the macro's filter. If a scope isn't open and
303 * we're not a macro, just let it through.
306 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
308 assert(roffs
[t
].text
);
309 return((*roffs
[t
].text
)
310 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
311 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
315 * If a scope is open, go to the child handler for that macro,
316 * as it may want to preprocess before doing anything with it.
321 assert(roffs
[t
].sub
);
322 return((*roffs
[t
].sub
)
323 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
327 * Lastly, as we've no scope open, try to look up and execute
328 * the new macro. If no macro is found, simply return and let
329 * the compilers handle it.
333 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
336 assert(roffs
[t
].proc
);
337 return((*roffs
[t
].proc
)
338 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
343 roff_endparse(struct roff
*r
)
348 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
349 r
->last
->col
, NULL
));
354 * Parse a roff node's type from the input buffer. This must be in the
355 * form of ".foo xxx" in the usual way.
358 roff_parse(const char *buf
, int *pos
)
364 assert(ROFF_CTL(buf
[*pos
]));
367 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
370 if ('\0' == buf
[*pos
])
373 for (j
= 0; j
< 4; j
++, (*pos
)++)
374 if ('\0' == (mac
[j
] = buf
[*pos
]))
376 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
384 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
387 while (buf
[*pos
] && ' ' == buf
[*pos
])
396 roff_cblock(ROFF_ARGS
)
400 * A block-close `..' should only be invoked as a child of an
401 * ignore macro, otherwise raise a warning and just ignore it.
404 if (NULL
== r
->last
) {
405 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
410 switch (r
->last
->tok
) {
426 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
432 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
436 roffnode_cleanscope(r
);
443 roffnode_cleanscope(struct roff
*r
)
447 if (--r
->last
->endspan
< 0)
456 roff_ccond(ROFF_ARGS
)
459 if (NULL
== r
->last
) {
460 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
465 switch (r
->last
->tok
) {
473 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
478 if (r
->last
->endspan
> -1) {
479 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
485 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
489 roffnode_cleanscope(r
);
496 roff_block(ROFF_ARGS
)
501 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
502 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
505 } else if (ROFF_ig
!= tok
) {
506 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
508 while (' ' == (*bufp
)[pos
])
512 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
515 if ('\0' == (*bufp
)[pos
])
519 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
520 '\t' != (*bufp
)[pos
])
524 * Note: groff does NOT like escape characters in the input.
525 * Instead of detecting this, we're just going to let it fly and
530 sz
= (size_t)(pos
- sv
);
532 if (1 == sz
&& '.' == (*bufp
)[sv
])
535 r
->last
->end
= malloc(sz
+ 1);
537 if (NULL
== r
->last
->end
) {
538 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
542 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
543 r
->last
->end
[(int)sz
] = '\0';
546 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
555 roff_block_sub(ROFF_ARGS
)
561 * First check whether a custom macro exists at this level. If
562 * it does, then check against it. This is some of groff's
563 * stranger behaviours. If we encountered a custom end-scope
564 * tag and that tag also happens to be a "real" macro, then we
565 * need to try interpreting it again as a real macro. If it's
566 * not, then return ignore. Else continue.
571 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
574 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
575 if ((*bufp
)[i
] != r
->last
->end
[j
])
578 if ('\0' == r
->last
->end
[j
] &&
579 ('\0' == (*bufp
)[i
] ||
581 '\t' == (*bufp
)[i
])) {
583 roffnode_cleanscope(r
);
585 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
592 * If we have no custom end-query or lookup failed, then try
593 * pulling it out of the hashtable.
597 t
= roff_parse(*bufp
, &pos
);
599 /* If we're not a comment-end, then throw it away. */
600 if (ROFF_cblock
!= t
)
603 assert(roffs
[t
].proc
);
604 return((*roffs
[t
].proc
)(r
, t
, bufp
,
605 szp
, ln
, ppos
, pos
, offs
));
611 roff_block_text(ROFF_ARGS
)
620 roff_cond_sub(ROFF_ARGS
)
628 roff_cond_text(r
, tok
, bufp
, szp
, ln
, ppos
, pos
, offs
);
630 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
631 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
634 * A denied conditional must evaluate its children if and only
635 * if they're either structurally required (such as loops and
636 * conditionals) or a closing macro.
638 if (ROFFRULE_DENY
== rr
)
639 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
643 assert(roffs
[t
].proc
);
644 return((*roffs
[t
].proc
)
645 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
651 roff_cond_text(ROFF_ARGS
)
659 * We display the value of the text if out current evaluation
660 * scope permits us to do so.
664 if (NULL
== (ep
= strstr(st
, "\\}"))) {
665 roffnode_cleanscope(r
);
666 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
669 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
672 roffnode_cleanscope(r
);
673 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
681 int cpos
; /* position of the condition */
684 /* Stack overflow! */
686 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
687 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
693 if (ROFF_if
== tok
|| ROFF_ie
== tok
) {
695 * Read ahead past the conditional. FIXME: this does
696 * not work, as conditionals don't end on whitespace,
697 * but are parsed according to a formal grammar. It's
698 * good enough for now, however.
700 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
705 while (' ' == (*bufp
)[pos
])
709 * Roff is weird. If we have just white-space after the
710 * conditional, it's considered the BODY and we exit without
711 * really doing anything. Warn about this. It's probably
714 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
715 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
720 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
723 /* XXX: Implement more conditionals. */
725 if (ROFF_if
== tok
|| ROFF_ie
== tok
)
726 r
->last
->rule
= 'n' == (*bufp
)[cpos
] ?
727 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
728 else if (ROFF_el
== tok
) {
730 * An `.el' will get the value of the current rstack
731 * entry set in prior `ie' calls or defaults to DENY.
733 if (r
->rstackpos
< 0)
734 r
->last
->rule
= ROFFRULE_DENY
;
736 r
->last
->rule
= r
->rstack
[r
->rstackpos
];
738 if (ROFF_ie
== tok
) {
740 * An if-else will put the NEGATION of the current
741 * evaluated conditional into the stack.
744 if (ROFFRULE_DENY
== r
->last
->rule
)
745 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
747 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
749 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
750 r
->last
->rule
= ROFFRULE_DENY
;
752 r
->last
->endspan
= 1;
754 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
755 r
->last
->endspan
= -1;
760 * If there are no arguments on the line, the next-line scope is
764 if ('\0' == (*bufp
)[pos
])
767 /* Otherwise re-run the roff parser after recalculating. */