]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.81 2010/05/17 00:46:35 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 ('.' == (c) || '\'' == (c))
51 struct roffnode
*last
; /* leaf of stack */
52 mandocmsg msg
; /* err/warn/fatal messages */
53 void *data
; /* privdata for messages */
62 enum rofft tok
; /* type of node */
63 struct roffnode
*parent
; /* up one in stack */
64 int line
; /* parse line */
65 int col
; /* parse col */
66 char *end
; /* end-rules: custom token */
67 int endspan
; /* end-rules: next-line or infty */
71 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
72 enum rofft tok, /* tok of macro */ \
73 char **bufp, /* input buffer */ \
74 size_t *szp, /* size of input buffer */ \
75 int ln, /* parse line */ \
76 int ppos, /* original pos in buffer */ \
77 int pos, /* current pos in buffer */ \
78 int *offs /* reset offset of buffer data */
80 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
83 const char *name
; /* macro name */
84 roffproc proc
; /* process new macro */
85 roffproc text
; /* process as child text of macro */
86 roffproc sub
; /* process as child of macro */
88 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
91 static enum rofferr
roff_block(ROFF_ARGS
);
92 static enum rofferr
roff_block_text(ROFF_ARGS
);
93 static enum rofferr
roff_block_sub(ROFF_ARGS
);
94 static enum rofferr
roff_cblock(ROFF_ARGS
);
95 static enum rofferr
roff_ccond(ROFF_ARGS
);
96 static enum rofferr
roff_if(ROFF_ARGS
);
97 static enum rofferr
roff_if_text(ROFF_ARGS
);
98 static enum rofferr
roff_if_sub(ROFF_ARGS
);
100 const struct roffmac roffs
[ROFF_MAX
] = {
101 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0 },
102 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0 },
103 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
104 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0 },
105 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0 },
106 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0 },
107 { "if", roff_if
, roff_if_text
, roff_if_sub
, ROFFMAC_STRUCT
},
108 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0 },
109 { ".", roff_cblock
, NULL
, NULL
, 0 },
110 { "\\}", roff_ccond
, NULL
, NULL
, 0 },
113 static void roff_free1(struct roff
*);
114 static enum rofft
roff_hash_find(const char *);
115 static void roffnode_cleanscope(struct roff
*);
116 static int roffnode_push(struct roff
*,
117 enum rofft
, int, int);
118 static void roffnode_pop(struct roff
*);
119 static enum rofft
roff_parse(const char *, int *);
123 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
124 * the nil-terminated string name could be found.
127 roff_hash_find(const char *p
)
131 /* FIXME: make this be fast and efficient. */
133 for (i
= 0; i
< (int)ROFF_MAX
; i
++)
134 if (0 == strcmp(roffs
[i
].name
, p
))
135 return((enum rofft
)i
);
142 * Pop the current node off of the stack of roff instructions currently
146 roffnode_pop(struct roff
*r
)
152 r
->last
= r
->last
->parent
;
160 * Push a roff node onto the instruction stack. This must later be
161 * removed with roffnode_pop().
164 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
168 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
169 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
177 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
185 roff_free1(struct roff
*r
)
194 roff_reset(struct roff
*r
)
202 roff_free(struct roff
*r
)
211 roff_alloc(const mandocmsg msg
, void *data
)
215 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
216 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
227 roff_parseln(struct roff
*r
, int ln
,
228 char **bufp
, size_t *szp
, int pos
, int *offs
)
234 * First, if a scope is open and we're not a macro, pass the
235 * text through the macro's filter. If a scope isn't open and
236 * we're not a macro, just let it through.
239 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
241 assert(roffs
[t
].text
);
242 return((*roffs
[t
].text
)
243 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
244 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
248 * If a scope is open, go to the child handler for that macro,
249 * as it may want to preprocess before doing anything with it.
254 assert(roffs
[t
].sub
);
255 return((*roffs
[t
].sub
)
256 (r
, t
, bufp
, szp
, ln
, pos
, pos
, offs
));
260 * Lastly, as we've no scope open, try to look up and execute
261 * the new macro. If no macro is found, simply return and let
262 * the compilers handle it.
266 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
269 assert(roffs
[t
].proc
);
270 return((*roffs
[t
].proc
)
271 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
276 roff_endparse(struct roff
*r
)
281 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
282 r
->last
->col
, NULL
));
287 * Parse a roff node's type from the input buffer. This must be in the
288 * form of ".foo xxx" in the usual way.
291 roff_parse(const char *buf
, int *pos
)
297 assert(ROFF_CTL(buf
[*pos
]));
300 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
303 if ('\0' == buf
[*pos
])
306 for (j
= 0; j
< 4; j
++, (*pos
)++)
307 if ('\0' == (mac
[j
] = buf
[*pos
]))
309 else if (' ' == buf
[*pos
])
317 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
320 while (buf
[*pos
] && ' ' == buf
[*pos
])
329 roff_cblock(ROFF_ARGS
)
333 * A block-close `..' should only be invoked as a child of an
334 * ignore macro, otherwise raise a warning and just ignore it.
337 if (NULL
== r
->last
) {
338 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
343 switch (r
->last
->tok
) {
359 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
365 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
369 roffnode_cleanscope(r
);
376 roffnode_cleanscope(struct roff
*r
)
380 if (--r
->last
->endspan
< 0)
389 roff_ccond(ROFF_ARGS
)
392 if (NULL
== r
->last
) {
393 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
398 if (ROFF_if
!= r
->last
->tok
) {
399 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
404 if (r
->last
->endspan
> -1) {
405 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
411 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
415 roffnode_cleanscope(r
);
422 roff_block(ROFF_ARGS
)
427 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
428 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
431 } else if (ROFF_ig
!= tok
) {
432 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
434 while (' ' == (*bufp
)[pos
])
438 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
441 if ('\0' == (*bufp
)[pos
])
445 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
446 '\t' != (*bufp
)[pos
])
450 * Note: groff does NOT like escape characters in the input.
451 * Instead of detecting this, we're just going to let it fly and
456 sz
= (size_t)(pos
- sv
);
458 if (1 == sz
&& '.' == (*bufp
)[sv
])
461 r
->last
->end
= malloc(sz
+ 1);
463 if (NULL
== r
->last
->end
) {
464 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
468 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
469 r
->last
->end
[(int)sz
] = '\0';
472 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
481 roff_if_sub(ROFF_ARGS
)
488 roffnode_cleanscope(r
);
490 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
491 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
494 * A denied conditional must evaluate its children if and only
495 * if they're either structurally required (such as loops and
496 * conditionals) or a closing macro.
498 if (ROFFRULE_DENY
== rr
)
499 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
503 assert(roffs
[t
].proc
);
504 return((*roffs
[t
].proc
)
505 (r
, t
, bufp
, szp
, ln
, ppos
, pos
, offs
));
511 roff_block_sub(ROFF_ARGS
)
517 * First check whether a custom macro exists at this level. If
518 * it does, then check against it. This is some of groff's
519 * stranger behaviours. If we encountered a custom end-scope
520 * tag and that tag also happens to be a "real" macro, then we
521 * need to try interpreting it again as a real macro. If it's
522 * not, then return ignore. Else continue.
527 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
530 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
531 if ((*bufp
)[i
] != r
->last
->end
[j
])
534 if ('\0' == r
->last
->end
[j
] &&
535 ('\0' == (*bufp
)[i
] ||
537 '\t' == (*bufp
)[i
])) {
539 roffnode_cleanscope(r
);
541 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
548 * If we have no custom end-query or lookup failed, then try
549 * pulling it out of the hashtable.
553 t
= roff_parse(*bufp
, &pos
);
555 /* If we're not a comment-end, then throw it away. */
556 if (ROFF_cblock
!= t
)
559 assert(roffs
[t
].proc
);
560 return((*roffs
[t
].proc
)(r
, t
, bufp
,
561 szp
, ln
, ppos
, pos
, offs
));
567 roff_block_text(ROFF_ARGS
)
576 roff_if_text(ROFF_ARGS
)
581 if (NULL
== (ep
= strstr(st
, "\\}"))) {
582 roffnode_cleanscope(r
);
586 if (ep
> st
&& '\\' != *(ep
- 1))
589 roffnode_cleanscope(r
);
601 * Read ahead past the conditional.
602 * FIXME: this does not work, as conditionals don't end on
603 * whitespace, but are parsed according to a formal grammar.
604 * It's good enough for now, however.
607 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
611 while (' ' == (*bufp
)[pos
])
615 * Roff is weird. If we have just white-space after the
616 * conditional, it's considered the BODY and we exit without
617 * really doing anything. Warn about this. It's probably
621 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
622 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
627 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
630 /* Don't evaluate: just assume NO. */
632 r
->last
->endspan
= 1;
634 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
635 r
->last
->endspan
= -1;
640 * If there are no arguments on the line, the next-line scope is
644 if ('\0' == (*bufp
)[pos
])
647 /* Otherwise re-run the roff parser after recalculating. */