]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.82 2010/05/17 02:01:05 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.
29 #define RSTACK_MAX 128
32 ('.' == (c) || '\'' == (c))
56 struct roffnode
*last
; /* leaf of stack */
57 mandocmsg msg
; /* err/warn/fatal messages */
58 void *data
; /* privdata for messages */
59 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
60 int rstackpos
; /* position in rstack */
64 enum rofft tok
; /* type of node */
65 struct roffnode
*parent
; /* up one in stack */
66 int line
; /* parse line */
67 int col
; /* parse col */
68 char *end
; /* end-rules: custom token */
69 int endspan
; /* end-rules: next-line or infty */
70 enum roffrule rule
; /* current evaluation rule */
73 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
74 enum rofft tok, /* tok of macro */ \
75 char **bufp, /* input buffer */ \
76 size_t *szp, /* size of input buffer */ \
77 int ln, /* parse line */ \
78 int ppos, /* original pos in buffer */ \
79 int pos, /* current pos in buffer */ \
80 int *offs /* reset offset of buffer data */
82 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
85 const char *name
; /* macro name */
86 roffproc proc
; /* process new macro */
87 roffproc text
; /* process as child text of macro */
88 roffproc sub
; /* process as child of macro */
90 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
93 static enum rofferr
roff_block(ROFF_ARGS
);
94 static enum rofferr
roff_block_text(ROFF_ARGS
);
95 static enum rofferr
roff_block_sub(ROFF_ARGS
);
96 static enum rofferr
roff_cblock(ROFF_ARGS
);
97 static enum rofferr
roff_ccond(ROFF_ARGS
);
98 static enum rofferr
roff_cond(ROFF_ARGS
);
99 static enum rofferr
roff_cond_text(ROFF_ARGS
);
100 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
102 const struct roffmac roffs
[ROFF_MAX
] = {
103 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0 },
104 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0 },
105 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
106 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0 },
107 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0 },
108 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
109 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
110 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
111 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
112 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0 },
113 { ".", roff_cblock
, NULL
, NULL
, 0 },
114 { "\\}", roff_ccond
, NULL
, NULL
, 0 },
117 static void roff_free1(struct roff
*);
118 static enum rofft
roff_hash_find(const char *);
119 static void roffnode_cleanscope(struct roff
*);
120 static int roffnode_push(struct roff
*,
121 enum rofft
, int, int);
122 static void roffnode_pop(struct roff
*);
123 static enum rofft
roff_parse(const char *, int *);
127 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
128 * the nil-terminated string name could be found.
131 roff_hash_find(const char *p
)
135 /* FIXME: make this be fast and efficient. */
137 for (i
= 0; i
< (int)ROFF_MAX
; i
++)
138 if (0 == strcmp(roffs
[i
].name
, p
))
139 return((enum rofft
)i
);
146 * Pop the current node off of the stack of roff instructions currently
150 roffnode_pop(struct roff
*r
)
157 if (ROFF_el
== p
->tok
)
158 if (r
->rstackpos
> -1)
161 r
->last
= r
->last
->parent
;
169 * Push a roff node onto the instruction stack. This must later be
170 * removed with roffnode_pop().
173 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
177 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
178 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
186 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
194 roff_free1(struct roff
*r
)
203 roff_reset(struct roff
*r
)
211 roff_free(struct roff
*r
)
220 roff_alloc(const mandocmsg msg
, void *data
)
224 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
225 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
237 roff_parseln(struct roff
*r
, int ln
,
238 char **bufp
, size_t *szp
, int pos
, int *offs
)
244 * First, if a scope is open and we're not a macro, pass the
245 * text through the macro's filter. If a scope isn't open and
246 * we're not a macro, just let it through.
249 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
251 assert(roffs
[t
].text
);
252 return((*roffs
[t
].text
)
253 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
254 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
258 * If a scope is open, go to the child handler for that macro,
259 * as it may want to preprocess before doing anything with it.
264 assert(roffs
[t
].sub
);
265 return((*roffs
[t
].sub
)
266 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
270 * Lastly, as we've no scope open, try to look up and execute
271 * the new macro. If no macro is found, simply return and let
272 * the compilers handle it.
276 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
279 assert(roffs
[t
].proc
);
280 return((*roffs
[t
].proc
)
281 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
286 roff_endparse(struct roff
*r
)
291 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
292 r
->last
->col
, NULL
));
297 * Parse a roff node's type from the input buffer. This must be in the
298 * form of ".foo xxx" in the usual way.
301 roff_parse(const char *buf
, int *pos
)
307 assert(ROFF_CTL(buf
[*pos
]));
310 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
313 if ('\0' == buf
[*pos
])
316 for (j
= 0; j
< 4; j
++, (*pos
)++)
317 if ('\0' == (mac
[j
] = buf
[*pos
]))
319 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
327 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
330 while (buf
[*pos
] && ' ' == buf
[*pos
])
339 roff_cblock(ROFF_ARGS
)
343 * A block-close `..' should only be invoked as a child of an
344 * ignore macro, otherwise raise a warning and just ignore it.
347 if (NULL
== r
->last
) {
348 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
353 switch (r
->last
->tok
) {
369 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
375 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
379 roffnode_cleanscope(r
);
386 roffnode_cleanscope(struct roff
*r
)
390 if (--r
->last
->endspan
< 0)
399 roff_ccond(ROFF_ARGS
)
402 if (NULL
== r
->last
) {
403 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
408 switch (r
->last
->tok
) {
416 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
421 if (r
->last
->endspan
> -1) {
422 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
428 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
432 roffnode_cleanscope(r
);
439 roff_block(ROFF_ARGS
)
444 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
445 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
448 } else if (ROFF_ig
!= tok
) {
449 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
451 while (' ' == (*bufp
)[pos
])
455 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
458 if ('\0' == (*bufp
)[pos
])
462 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
463 '\t' != (*bufp
)[pos
])
467 * Note: groff does NOT like escape characters in the input.
468 * Instead of detecting this, we're just going to let it fly and
473 sz
= (size_t)(pos
- sv
);
475 if (1 == sz
&& '.' == (*bufp
)[sv
])
478 r
->last
->end
= malloc(sz
+ 1);
480 if (NULL
== r
->last
->end
) {
481 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
485 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
486 r
->last
->end
[(int)sz
] = '\0';
489 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
498 roff_block_sub(ROFF_ARGS
)
504 * First check whether a custom macro exists at this level. If
505 * it does, then check against it. This is some of groff's
506 * stranger behaviours. If we encountered a custom end-scope
507 * tag and that tag also happens to be a "real" macro, then we
508 * need to try interpreting it again as a real macro. If it's
509 * not, then return ignore. Else continue.
514 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
517 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
518 if ((*bufp
)[i
] != r
->last
->end
[j
])
521 if ('\0' == r
->last
->end
[j
] &&
522 ('\0' == (*bufp
)[i
] ||
524 '\t' == (*bufp
)[i
])) {
526 roffnode_cleanscope(r
);
528 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
535 * If we have no custom end-query or lookup failed, then try
536 * pulling it out of the hashtable.
540 t
= roff_parse(*bufp
, &pos
);
542 /* If we're not a comment-end, then throw it away. */
543 if (ROFF_cblock
!= t
)
546 assert(roffs
[t
].proc
);
547 return((*roffs
[t
].proc
)(r
, t
, bufp
,
548 szp
, ln
, ppos
, pos
, offs
));
554 roff_block_text(ROFF_ARGS
)
563 roff_cond_sub(ROFF_ARGS
)
571 roffnode_cleanscope(r
);
573 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
574 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
577 * A denied conditional must evaluate its children if and only
578 * if they're either structurally required (such as loops and
579 * conditionals) or a closing macro.
581 if (ROFFRULE_DENY
== rr
)
582 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
586 assert(roffs
[t
].proc
);
587 return((*roffs
[t
].proc
)
588 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
594 roff_cond_text(ROFF_ARGS
)
602 * We display the value of the text if out current evaluation
603 * scope permits us to do so.
607 if (NULL
== (ep
= strstr(st
, "\\}"))) {
608 roffnode_cleanscope(r
);
609 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
612 if (ep
> st
&& '\\' != *(ep
- 1))
615 roffnode_cleanscope(r
);
616 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
626 /* Stack overflow! */
628 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
629 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
633 if (ROFF_if
== tok
|| ROFF_ie
== tok
) {
635 * Read ahead past the conditional. FIXME: this does
636 * not work, as conditionals don't end on whitespace,
637 * but are parsed according to a formal grammar. It's
638 * good enough for now, however.
640 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
645 while (' ' == (*bufp
)[pos
])
649 * Roff is weird. If we have just white-space after the
650 * conditional, it's considered the BODY and we exit without
651 * really doing anything. Warn about this. It's probably
654 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
655 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
660 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
663 /* TODO: here we would evaluate the conditional. */
665 if (ROFF_el
== tok
) {
667 * An `.el' will get the value of the current rstack
668 * entry set in prior `ie' calls or defaults to DENY.
670 if (r
->rstackpos
< 0)
671 r
->last
->rule
= ROFFRULE_DENY
;
673 r
->last
->rule
= r
->rstack
[r
->rstackpos
];
674 } else if (ROFF_ie
== tok
) {
676 * An if-else will put the NEGATION of the current
677 * evaluated conditional into the stack.
680 if (ROFFRULE_DENY
== r
->last
->rule
)
681 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
683 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
686 r
->last
->endspan
= 1;
688 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
689 r
->last
->endspan
= -1;
694 * If there are no arguments on the line, the next-line scope is
698 if ('\0' == (*bufp
)[pos
])
701 /* Otherwise re-run the roff parser after recalculating. */