]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.84 2010/05/24 23:54:18 schwarze 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))
59 struct roffnode
*last
; /* leaf of stack */
60 mandocmsg msg
; /* err/warn/fatal messages */
61 void *data
; /* privdata for messages */
62 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
63 int rstackpos
; /* position in rstack */
67 enum rofft tok
; /* type of node */
68 struct roffnode
*parent
; /* up one in stack */
69 int line
; /* parse line */
70 int col
; /* parse col */
71 char *end
; /* end-rules: custom token */
72 int endspan
; /* end-rules: next-line or infty */
73 enum roffrule rule
; /* current evaluation rule */
76 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
77 enum rofft tok, /* tok of macro */ \
78 char **bufp, /* input buffer */ \
79 size_t *szp, /* size of input buffer */ \
80 int ln, /* parse line */ \
81 int ppos, /* original pos in buffer */ \
82 int pos, /* current pos in buffer */ \
83 int *offs /* reset offset of buffer data */
85 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
88 const char *name
; /* macro name */
89 roffproc proc
; /* process new macro */
90 roffproc text
; /* process as child text of macro */
91 roffproc sub
; /* process as child of macro */
93 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
96 static enum rofferr
roff_block(ROFF_ARGS
);
97 static enum rofferr
roff_block_text(ROFF_ARGS
);
98 static enum rofferr
roff_block_sub(ROFF_ARGS
);
99 static enum rofferr
roff_cblock(ROFF_ARGS
);
100 static enum rofferr
roff_ccond(ROFF_ARGS
);
101 static enum rofferr
roff_cond(ROFF_ARGS
);
102 static enum rofferr
roff_cond_text(ROFF_ARGS
);
103 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
104 static enum rofferr
roff_line(ROFF_ARGS
);
106 const struct roffmac roffs
[ROFF_MAX
] = {
107 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0 },
108 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0 },
109 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
110 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0 },
111 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0 },
112 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
113 { "ds", roff_line
, NULL
, NULL
, 0 },
114 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
115 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
116 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
},
117 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0 },
118 { "rm", roff_line
, NULL
, NULL
, 0 },
119 { "tr", roff_line
, NULL
, NULL
, 0 },
120 { ".", roff_cblock
, NULL
, NULL
, 0 },
121 { "\\}", roff_ccond
, NULL
, NULL
, 0 },
124 static void roff_free1(struct roff
*);
125 static enum rofft
roff_hash_find(const char *);
126 static void roffnode_cleanscope(struct roff
*);
127 static int roffnode_push(struct roff
*,
128 enum rofft
, int, int);
129 static void roffnode_pop(struct roff
*);
130 static enum rofft
roff_parse(const char *, int *);
134 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
135 * the nil-terminated string name could be found.
138 roff_hash_find(const char *p
)
142 /* FIXME: make this be fast and efficient. */
144 for (i
= 0; i
< (int)ROFF_MAX
; i
++)
145 if (0 == strcmp(roffs
[i
].name
, p
))
146 return((enum rofft
)i
);
153 * Pop the current node off of the stack of roff instructions currently
157 roffnode_pop(struct roff
*r
)
164 if (ROFF_el
== p
->tok
)
165 if (r
->rstackpos
> -1)
168 r
->last
= r
->last
->parent
;
176 * Push a roff node onto the instruction stack. This must later be
177 * removed with roffnode_pop().
180 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
184 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
185 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
193 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
201 roff_free1(struct roff
*r
)
210 roff_reset(struct roff
*r
)
218 roff_free(struct roff
*r
)
227 roff_alloc(const mandocmsg msg
, void *data
)
231 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
232 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
244 roff_parseln(struct roff
*r
, int ln
,
245 char **bufp
, size_t *szp
, int pos
, int *offs
)
251 * First, if a scope is open and we're not a macro, pass the
252 * text through the macro's filter. If a scope isn't open and
253 * we're not a macro, just let it through.
256 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
258 assert(roffs
[t
].text
);
259 return((*roffs
[t
].text
)
260 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
261 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
265 * If a scope is open, go to the child handler for that macro,
266 * as it may want to preprocess before doing anything with it.
271 assert(roffs
[t
].sub
);
272 return((*roffs
[t
].sub
)
273 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
277 * Lastly, as we've no scope open, try to look up and execute
278 * the new macro. If no macro is found, simply return and let
279 * the compilers handle it.
283 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
286 assert(roffs
[t
].proc
);
287 return((*roffs
[t
].proc
)
288 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
293 roff_endparse(struct roff
*r
)
298 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
299 r
->last
->col
, NULL
));
304 * Parse a roff node's type from the input buffer. This must be in the
305 * form of ".foo xxx" in the usual way.
308 roff_parse(const char *buf
, int *pos
)
314 assert(ROFF_CTL(buf
[*pos
]));
317 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
320 if ('\0' == buf
[*pos
])
323 for (j
= 0; j
< 4; j
++, (*pos
)++)
324 if ('\0' == (mac
[j
] = buf
[*pos
]))
326 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
334 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
337 while (buf
[*pos
] && ' ' == buf
[*pos
])
346 roff_cblock(ROFF_ARGS
)
350 * A block-close `..' should only be invoked as a child of an
351 * ignore macro, otherwise raise a warning and just ignore it.
354 if (NULL
== r
->last
) {
355 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
360 switch (r
->last
->tok
) {
376 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
382 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
386 roffnode_cleanscope(r
);
393 roffnode_cleanscope(struct roff
*r
)
397 if (--r
->last
->endspan
< 0)
406 roff_ccond(ROFF_ARGS
)
409 if (NULL
== r
->last
) {
410 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
415 switch (r
->last
->tok
) {
423 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
428 if (r
->last
->endspan
> -1) {
429 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
435 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
439 roffnode_cleanscope(r
);
446 roff_block(ROFF_ARGS
)
451 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
452 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
455 } else if (ROFF_ig
!= tok
) {
456 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
458 while (' ' == (*bufp
)[pos
])
462 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
465 if ('\0' == (*bufp
)[pos
])
469 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
470 '\t' != (*bufp
)[pos
])
474 * Note: groff does NOT like escape characters in the input.
475 * Instead of detecting this, we're just going to let it fly and
480 sz
= (size_t)(pos
- sv
);
482 if (1 == sz
&& '.' == (*bufp
)[sv
])
485 r
->last
->end
= malloc(sz
+ 1);
487 if (NULL
== r
->last
->end
) {
488 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
492 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
493 r
->last
->end
[(int)sz
] = '\0';
496 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
505 roff_block_sub(ROFF_ARGS
)
511 * First check whether a custom macro exists at this level. If
512 * it does, then check against it. This is some of groff's
513 * stranger behaviours. If we encountered a custom end-scope
514 * tag and that tag also happens to be a "real" macro, then we
515 * need to try interpreting it again as a real macro. If it's
516 * not, then return ignore. Else continue.
521 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
524 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
525 if ((*bufp
)[i
] != r
->last
->end
[j
])
528 if ('\0' == r
->last
->end
[j
] &&
529 ('\0' == (*bufp
)[i
] ||
531 '\t' == (*bufp
)[i
])) {
533 roffnode_cleanscope(r
);
535 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
542 * If we have no custom end-query or lookup failed, then try
543 * pulling it out of the hashtable.
547 t
= roff_parse(*bufp
, &pos
);
549 /* If we're not a comment-end, then throw it away. */
550 if (ROFF_cblock
!= t
)
553 assert(roffs
[t
].proc
);
554 return((*roffs
[t
].proc
)(r
, t
, bufp
,
555 szp
, ln
, ppos
, pos
, offs
));
561 roff_block_text(ROFF_ARGS
)
570 roff_cond_sub(ROFF_ARGS
)
578 roff_cond_text(r
, tok
, bufp
, szp
, ln
, ppos
, pos
, offs
);
580 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
581 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
584 * A denied conditional must evaluate its children if and only
585 * if they're either structurally required (such as loops and
586 * conditionals) or a closing macro.
588 if (ROFFRULE_DENY
== rr
)
589 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
593 assert(roffs
[t
].proc
);
594 return((*roffs
[t
].proc
)
595 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
601 roff_cond_text(ROFF_ARGS
)
609 * We display the value of the text if out current evaluation
610 * scope permits us to do so.
614 if (NULL
== (ep
= strstr(st
, "\\}"))) {
615 roffnode_cleanscope(r
);
616 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
619 if (ep
> st
&& '\\' != *(ep
- 1)) {
624 roffnode_cleanscope(r
);
625 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
633 int cpos
; /* position of the condition */
636 /* Stack overflow! */
638 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
639 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
645 if (ROFF_if
== tok
|| ROFF_ie
== tok
) {
647 * Read ahead past the conditional. FIXME: this does
648 * not work, as conditionals don't end on whitespace,
649 * but are parsed according to a formal grammar. It's
650 * good enough for now, however.
652 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
657 while (' ' == (*bufp
)[pos
])
661 * Roff is weird. If we have just white-space after the
662 * conditional, it's considered the BODY and we exit without
663 * really doing anything. Warn about this. It's probably
666 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
667 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
672 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
675 /* XXX: Implement more conditionals. */
677 if (ROFF_if
== tok
|| ROFF_ie
== tok
)
678 r
->last
->rule
= 'n' == (*bufp
)[cpos
] ?
679 ROFFRULE_ALLOW
: ROFFRULE_DENY
;
680 else if (ROFF_el
== tok
) {
682 * An `.el' will get the value of the current rstack
683 * entry set in prior `ie' calls or defaults to DENY.
685 if (r
->rstackpos
< 0)
686 r
->last
->rule
= ROFFRULE_DENY
;
688 r
->last
->rule
= r
->rstack
[r
->rstackpos
];
690 if (ROFF_ie
== tok
) {
692 * An if-else will put the NEGATION of the current
693 * evaluated conditional into the stack.
696 if (ROFFRULE_DENY
== r
->last
->rule
)
697 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
699 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
701 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
)
702 r
->last
->rule
= ROFFRULE_DENY
;
704 r
->last
->endspan
= 1;
706 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
707 r
->last
->endspan
= -1;
712 * If there are no arguments on the line, the next-line scope is
716 if ('\0' == (*bufp
)[pos
])
719 /* Otherwise re-run the roff parser after recalculating. */