]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.90 2010/06/27 15:52:41 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.
33 #define RSTACK_MAX 128
36 ('.' == (c) || '\'' == (c))
39 #define ROFF_DEBUG(fmt, args...) \
40 do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
42 #define ROFF_DEBUG(fmt, args...) \
43 do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
72 struct roffnode
*last
; /* leaf of stack */
73 mandocmsg msg
; /* err/warn/fatal messages */
74 void *data
; /* privdata for messages */
75 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
76 int rstackpos
; /* position in rstack */
77 struct regset
*regs
; /* read/writable registers */
81 enum rofft tok
; /* type of node */
82 struct roffnode
*parent
; /* up one in stack */
83 int line
; /* parse line */
84 int col
; /* parse col */
85 char *end
; /* end-rules: custom token */
86 int endspan
; /* end-rules: next-line or infty */
87 enum roffrule rule
; /* current evaluation rule */
90 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
91 enum rofft tok, /* tok of macro */ \
92 char **bufp, /* input buffer */ \
93 size_t *szp, /* size of input buffer */ \
94 int ln, /* parse line */ \
95 int ppos, /* original pos in buffer */ \
96 int pos, /* current pos in buffer */ \
97 int *offs /* reset offset of buffer data */
99 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
102 const char *name
; /* macro name */
103 roffproc proc
; /* process new macro */
104 roffproc text
; /* process as child text of macro */
105 roffproc sub
; /* process as child of macro */
107 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
108 struct roffmac
*next
;
111 static enum rofferr
roff_block(ROFF_ARGS
);
112 static enum rofferr
roff_block_text(ROFF_ARGS
);
113 static enum rofferr
roff_block_sub(ROFF_ARGS
);
114 static enum rofferr
roff_cblock(ROFF_ARGS
);
115 static enum rofferr
roff_ccond(ROFF_ARGS
);
116 static enum rofferr
roff_cond(ROFF_ARGS
);
117 static enum rofferr
roff_cond_text(ROFF_ARGS
);
118 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
119 static enum rofferr
roff_line(ROFF_ARGS
);
120 static enum rofferr
roff_nr(ROFF_ARGS
);
121 static enum roffrule
roff_evalcond(const char *, int *);
123 /* See roff_hash_find() */
127 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
129 static struct roffmac
*hash
[HASHWIDTH
];
131 static struct roffmac roffs
[ROFF_MAX
] = {
132 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
133 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
134 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
135 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
136 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
137 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
138 { "ds", roff_line
, NULL
, NULL
, 0, NULL
},
139 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
140 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
141 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
142 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
143 { "rm", roff_line
, NULL
, NULL
, 0, NULL
},
144 { "tr", roff_line
, NULL
, NULL
, 0, NULL
},
145 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
146 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
147 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
150 static void roff_free1(struct roff
*);
151 static enum rofft
roff_hash_find(const char *);
152 static void roff_hash_init(void);
153 static void roffnode_cleanscope(struct roff
*);
154 static int roffnode_push(struct roff
*,
155 enum rofft
, int, int);
156 static void roffnode_pop(struct roff
*);
157 static enum rofft
roff_parse(const char *, int *);
158 static int roff_parse_nat(const char *, int *);
160 /* See roff_hash_find() */
161 #define ROFF_HASH(p) (p[0] - ASCII_LO)
169 for (i
= 0; i
< (int)ROFF_MAX
; i
++) {
170 assert(roffs
[i
].name
[0] >= ASCII_LO
);
171 assert(roffs
[i
].name
[0] <= ASCII_HI
);
173 buc
= ROFF_HASH(roffs
[i
].name
);
175 if (NULL
!= (n
= hash
[buc
])) {
176 for ( ; n
->next
; n
= n
->next
)
180 hash
[buc
] = &roffs
[i
];
186 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
187 * the nil-terminated string name could be found.
190 roff_hash_find(const char *p
)
196 * libroff has an extremely simple hashtable, for the time
197 * being, which simply keys on the first character, which must
198 * be printable, then walks a chain. It works well enough until
202 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
207 if (NULL
== (n
= hash
[buc
]))
209 for ( ; n
; n
= n
->next
)
210 if (0 == strcmp(n
->name
, p
))
211 return((enum rofft
)(n
- roffs
));
218 * Pop the current node off of the stack of roff instructions currently
222 roffnode_pop(struct roff
*r
)
229 if (ROFF_el
== p
->tok
)
230 if (r
->rstackpos
> -1)
233 r
->last
= r
->last
->parent
;
241 * Push a roff node onto the instruction stack. This must later be
242 * removed with roffnode_pop().
245 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
249 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
250 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
258 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
266 roff_free1(struct roff
*r
)
275 roff_reset(struct roff
*r
)
283 roff_free(struct roff
*r
)
292 roff_alloc(struct regset
*regs
, const mandocmsg msg
, void *data
)
296 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
297 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
312 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
313 size_t *szp
, int pos
, int *offs
)
319 * First, if a scope is open and we're not a macro, pass the
320 * text through the macro's filter. If a scope isn't open and
321 * we're not a macro, just let it through.
324 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
326 assert(roffs
[t
].text
);
327 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
328 roffs
[t
].name
, &(*bufp
)[pos
]);
329 return((*roffs
[t
].text
)
331 ln
, pos
, pos
, offs
));
332 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
333 ROFF_DEBUG("roff: pass non-scoped text: [%s]\n",
339 * If a scope is open, go to the child handler for that macro,
340 * as it may want to preprocess before doing anything with it.
345 assert(roffs
[t
].sub
);
346 ROFF_DEBUG("roff: intercept scoped context: %s\n",
348 return((*roffs
[t
].sub
)
350 ln
, pos
, pos
, offs
));
354 * Lastly, as we've no scope open, try to look up and execute
355 * the new macro. If no macro is found, simply return and let
356 * the compilers handle it.
360 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
))) {
361 ROFF_DEBUG("roff: pass non-scoped non-macro: [%s]\n",
366 ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
367 roffs
[t
].name
, &(*bufp
)[pos
]);
368 assert(roffs
[t
].proc
);
369 return((*roffs
[t
].proc
)
371 ln
, ppos
, pos
, offs
));
376 roff_endparse(struct roff
*r
)
381 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
382 r
->last
->col
, NULL
));
387 * Parse a roff node's type from the input buffer. This must be in the
388 * form of ".foo xxx" in the usual way.
391 roff_parse(const char *buf
, int *pos
)
397 assert(ROFF_CTL(buf
[*pos
]));
400 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
403 if ('\0' == buf
[*pos
])
406 for (j
= 0; j
< 4; j
++, (*pos
)++)
407 if ('\0' == (mac
[j
] = buf
[*pos
]))
409 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
417 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
420 while (buf
[*pos
] && ' ' == buf
[*pos
])
428 roff_parse_nat(const char *buf
, int *res
)
434 lval
= strtol(buf
, &ep
, 10);
435 if (buf
[0] == '\0' || *ep
!= '\0')
437 if ((errno
== ERANGE
&&
438 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
439 (lval
> INT_MAX
|| lval
<= 0))
449 roff_cblock(ROFF_ARGS
)
453 * A block-close `..' should only be invoked as a child of an
454 * ignore macro, otherwise raise a warning and just ignore it.
457 if (NULL
== r
->last
) {
458 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
463 switch (r
->last
->tok
) {
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 roffnode_cleanscope(struct roff
*r
)
500 if (--r
->last
->endspan
< 0)
509 roff_ccond(ROFF_ARGS
)
512 if (NULL
== r
->last
) {
513 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
518 switch (r
->last
->tok
) {
526 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
531 if (r
->last
->endspan
> -1) {
532 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
538 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
542 roffnode_cleanscope(r
);
549 roff_block(ROFF_ARGS
)
554 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
555 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
558 } else if (ROFF_ig
!= tok
) {
559 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
561 while (' ' == (*bufp
)[pos
])
565 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
568 if ('\0' == (*bufp
)[pos
])
572 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
573 '\t' != (*bufp
)[pos
])
577 * Note: groff does NOT like escape characters in the input.
578 * Instead of detecting this, we're just going to let it fly and
583 sz
= (size_t)(pos
- sv
);
585 if (1 == sz
&& '.' == (*bufp
)[sv
])
588 r
->last
->end
= malloc(sz
+ 1);
590 if (NULL
== r
->last
->end
) {
591 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
595 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
596 r
->last
->end
[(int)sz
] = '\0';
599 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
608 roff_block_sub(ROFF_ARGS
)
614 * First check whether a custom macro exists at this level. If
615 * it does, then check against it. This is some of groff's
616 * stranger behaviours. If we encountered a custom end-scope
617 * tag and that tag also happens to be a "real" macro, then we
618 * need to try interpreting it again as a real macro. If it's
619 * not, then return ignore. Else continue.
624 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
627 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
628 if ((*bufp
)[i
] != r
->last
->end
[j
])
631 if ('\0' == r
->last
->end
[j
] &&
632 ('\0' == (*bufp
)[i
] ||
634 '\t' == (*bufp
)[i
])) {
636 roffnode_cleanscope(r
);
638 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
645 * If we have no custom end-query or lookup failed, then try
646 * pulling it out of the hashtable.
650 t
= roff_parse(*bufp
, &pos
);
652 /* If we're not a comment-end, then throw it away. */
653 if (ROFF_cblock
!= t
)
656 assert(roffs
[t
].proc
);
657 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
658 ln
, ppos
, pos
, offs
));
664 roff_block_text(ROFF_ARGS
)
673 roff_cond_sub(ROFF_ARGS
)
683 * Clean out scope. If we've closed ourselves, then don't
688 roffnode_cleanscope(r
);
691 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
693 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
694 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
697 * A denied conditional must evaluate its children if and only
698 * if they're either structurally required (such as loops and
699 * conditionals) or a closing macro.
701 if (ROFFRULE_DENY
== rr
)
702 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
706 assert(roffs
[t
].proc
);
707 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
708 ln
, ppos
, pos
, offs
));
714 roff_cond_text(ROFF_ARGS
)
722 * We display the value of the text if out current evaluation
723 * scope permits us to do so.
727 if (NULL
== (ep
= strstr(st
, "\\}"))) {
728 roffnode_cleanscope(r
);
729 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
732 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
735 roffnode_cleanscope(r
);
736 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
741 roff_evalcond(const char *v
, int *pos
)
747 return(ROFFRULE_ALLOW
);
754 return(ROFFRULE_DENY
);
759 while (v
[*pos
] && ' ' != v
[*pos
])
761 return(ROFFRULE_DENY
);
781 /* Stack overflow! */
783 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
784 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
788 /* First, evaluate the conditional. */
790 if (ROFF_el
== tok
) {
792 * An `.el' will get the value of the current rstack
793 * entry set in prior `ie' calls or defaults to DENY.
795 if (r
->rstackpos
< 0)
796 rule
= ROFFRULE_DENY
;
798 rule
= r
->rstack
[r
->rstackpos
];
800 rule
= roff_evalcond(*bufp
, &pos
);
804 while (' ' == (*bufp
)[pos
])
808 * Roff is weird. If we have just white-space after the
809 * conditional, it's considered the BODY and we exit without
810 * really doing anything. Warn about this. It's probably
814 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
815 if ((*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
820 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
823 r
->last
->rule
= rule
;
825 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
826 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
828 if (ROFF_ie
== tok
) {
830 * An if-else will put the NEGATION of the current
831 * evaluated conditional into the stack.
834 if (ROFFRULE_DENY
== r
->last
->rule
)
835 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
837 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
840 /* If the parent has false as its rule, then so do we. */
842 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
843 r
->last
->rule
= ROFFRULE_DENY
;
844 ROFF_DEBUG("roff: cond override: %s -> deny\n",
849 * Determine scope. If we're invoked with "\{" trailing the
850 * conditional, then we're in a multiline scope. Else our scope
851 * expires on the next line.
854 r
->last
->endspan
= 1;
856 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
857 r
->last
->endspan
= -1;
859 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
862 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
866 * If there are no arguments on the line, the next-line scope is
870 if ('\0' == (*bufp
)[pos
])
873 /* Otherwise re-run the roff parser after recalculating. */
884 const char *key
, *val
;
888 /* Parse register request. */
889 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
893 * Set our nil terminator. Because this line is going to be
894 * ignored anyway, we can munge it as we please.
897 (*bufp
)[pos
++] = '\0';
899 /* Skip whitespace to register token. */
900 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
905 /* Process register token. */
907 if (0 == strcmp(key
, "nS")) {
908 if ( ! roff_parse_nat(val
, &r
->regs
->regs
[(int)REG_nS
].i
))
909 r
->regs
->regs
[(int)REG_nS
].i
= 0;
911 ROFF_DEBUG("roff: register nS: %d\n",
912 r
->regs
->regs
[(int)REG_nS
].i
);
914 ROFF_DEBUG("roff: ignoring register: %s\n", key
);