]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.93 2010/07/04 22:04:04 schwarze Exp $ */
3 * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 #define RSTACK_MAX 128
37 ('.' == (c) || '\'' == (c))
40 #define ROFF_DEBUG(fmt, args...) \
41 do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
43 #define ROFF_DEBUG(fmt, args...) \
44 do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
73 struct roffnode
*last
; /* leaf of stack */
74 mandocmsg msg
; /* err/warn/fatal messages */
75 void *data
; /* privdata for messages */
76 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
77 int rstackpos
; /* position in rstack */
78 struct regset
*regs
; /* read/writable registers */
82 enum rofft tok
; /* type of node */
83 struct roffnode
*parent
; /* up one in stack */
84 int line
; /* parse line */
85 int col
; /* parse col */
86 char *end
; /* end-rules: custom token */
87 int endspan
; /* end-rules: next-line or infty */
88 enum roffrule rule
; /* current evaluation rule */
91 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
92 enum rofft tok, /* tok of macro */ \
93 char **bufp, /* input buffer */ \
94 size_t *szp, /* size of input buffer */ \
95 int ln, /* parse line */ \
96 int ppos, /* original pos in buffer */ \
97 int pos, /* current pos in buffer */ \
98 int *offs /* reset offset of buffer data */
100 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
103 const char *name
; /* macro name */
104 roffproc proc
; /* process new macro */
105 roffproc text
; /* process as child text of macro */
106 roffproc sub
; /* process as child of macro */
108 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
109 struct roffmac
*next
;
115 struct roffstr
*next
;
118 static enum rofferr
roff_block(ROFF_ARGS
);
119 static enum rofferr
roff_block_text(ROFF_ARGS
);
120 static enum rofferr
roff_block_sub(ROFF_ARGS
);
121 static enum rofferr
roff_cblock(ROFF_ARGS
);
122 static enum rofferr
roff_ccond(ROFF_ARGS
);
123 static enum rofferr
roff_cond(ROFF_ARGS
);
124 static enum rofferr
roff_cond_text(ROFF_ARGS
);
125 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
126 static enum rofferr
roff_ds(ROFF_ARGS
);
127 static enum rofferr
roff_line(ROFF_ARGS
);
128 static enum rofferr
roff_nr(ROFF_ARGS
);
129 static enum roffrule
roff_evalcond(const char *, int *);
131 /* See roff_hash_find() */
135 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
137 static struct roffmac
*hash
[HASHWIDTH
];
139 static struct roffmac roffs
[ROFF_MAX
] = {
140 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
141 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
142 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
143 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
144 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
145 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
146 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
147 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
148 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
149 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
150 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
151 { "rm", roff_line
, NULL
, NULL
, 0, NULL
},
152 { "tr", roff_line
, NULL
, NULL
, 0, NULL
},
153 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
154 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
155 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
158 static void roff_free1(struct roff
*);
159 static enum rofft
roff_hash_find(const char *);
160 static void roff_hash_init(void);
161 static void roffnode_cleanscope(struct roff
*);
162 static int roffnode_push(struct roff
*,
163 enum rofft
, int, int);
164 static void roffnode_pop(struct roff
*);
165 static enum rofft
roff_parse(const char *, int *);
166 static int roff_parse_nat(const char *, unsigned int *);
168 /* See roff_hash_find() */
169 #define ROFF_HASH(p) (p[0] - ASCII_LO)
177 for (i
= 0; i
< (int)ROFF_MAX
; i
++) {
178 assert(roffs
[i
].name
[0] >= ASCII_LO
);
179 assert(roffs
[i
].name
[0] <= ASCII_HI
);
181 buc
= ROFF_HASH(roffs
[i
].name
);
183 if (NULL
!= (n
= hash
[buc
])) {
184 for ( ; n
->next
; n
= n
->next
)
188 hash
[buc
] = &roffs
[i
];
194 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
195 * the nil-terminated string name could be found.
198 roff_hash_find(const char *p
)
204 * libroff has an extremely simple hashtable, for the time
205 * being, which simply keys on the first character, which must
206 * be printable, then walks a chain. It works well enough until
210 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
215 if (NULL
== (n
= hash
[buc
]))
217 for ( ; n
; n
= n
->next
)
218 if (0 == strcmp(n
->name
, p
))
219 return((enum rofft
)(n
- roffs
));
226 * Pop the current node off of the stack of roff instructions currently
230 roffnode_pop(struct roff
*r
)
237 if (ROFF_el
== p
->tok
)
238 if (r
->rstackpos
> -1)
241 r
->last
= r
->last
->parent
;
249 * Push a roff node onto the instruction stack. This must later be
250 * removed with roffnode_pop().
253 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
257 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
258 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
266 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
274 roff_free1(struct roff
*r
)
284 roff_reset(struct roff
*r
)
292 roff_free(struct roff
*r
)
301 roff_alloc(struct regset
*regs
, const mandocmsg msg
, void *data
)
305 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
306 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
321 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
322 size_t *szp
, int pos
, int *offs
)
328 * First, if a scope is open and we're not a macro, pass the
329 * text through the macro's filter. If a scope isn't open and
330 * we're not a macro, just let it through.
333 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
335 assert(roffs
[t
].text
);
336 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
337 roffs
[t
].name
, &(*bufp
)[pos
]);
338 return((*roffs
[t
].text
)
340 ln
, pos
, pos
, offs
));
341 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
342 ROFF_DEBUG("roff: pass non-scoped text: [%s]\n",
348 * If a scope is open, go to the child handler for that macro,
349 * as it may want to preprocess before doing anything with it.
354 assert(roffs
[t
].sub
);
355 ROFF_DEBUG("roff: intercept scoped context: %s\n",
357 return((*roffs
[t
].sub
)
359 ln
, pos
, pos
, offs
));
363 * Lastly, as we've no scope open, try to look up and execute
364 * the new macro. If no macro is found, simply return and let
365 * the compilers handle it.
369 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
))) {
370 ROFF_DEBUG("roff: pass non-scoped non-macro: [%s]\n",
375 ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
376 roffs
[t
].name
, &(*bufp
)[pos
]);
377 assert(roffs
[t
].proc
);
378 return((*roffs
[t
].proc
)
380 ln
, ppos
, pos
, offs
));
385 roff_endparse(struct roff
*r
)
390 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
391 r
->last
->col
, NULL
));
396 * Parse a roff node's type from the input buffer. This must be in the
397 * form of ".foo xxx" in the usual way.
400 roff_parse(const char *buf
, int *pos
)
406 assert(ROFF_CTL(buf
[*pos
]));
409 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
412 if ('\0' == buf
[*pos
])
415 for (j
= 0; j
< 4; j
++, (*pos
)++)
416 if ('\0' == (mac
[j
] = buf
[*pos
]))
418 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
426 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
429 while (buf
[*pos
] && ' ' == buf
[*pos
])
437 roff_parse_nat(const char *buf
, unsigned int *res
)
443 lval
= strtol(buf
, &ep
, 10);
444 if (buf
[0] == '\0' || *ep
!= '\0')
446 if ((errno
== ERANGE
&&
447 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
448 (lval
> INT_MAX
|| lval
< 0))
451 *res
= (unsigned int)lval
;
458 roff_cblock(ROFF_ARGS
)
462 * A block-close `..' should only be invoked as a child of an
463 * ignore macro, otherwise raise a warning and just ignore it.
466 if (NULL
== r
->last
) {
467 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
472 switch (r
->last
->tok
) {
488 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
494 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
498 roffnode_cleanscope(r
);
505 roffnode_cleanscope(struct roff
*r
)
509 if (--r
->last
->endspan
< 0)
518 roff_ccond(ROFF_ARGS
)
521 if (NULL
== r
->last
) {
522 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
527 switch (r
->last
->tok
) {
535 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
540 if (r
->last
->endspan
> -1) {
541 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
547 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
551 roffnode_cleanscope(r
);
558 roff_block(ROFF_ARGS
)
563 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
564 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
567 } else if (ROFF_ig
!= tok
) {
568 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
570 while (' ' == (*bufp
)[pos
])
574 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
577 if ('\0' == (*bufp
)[pos
])
581 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
582 '\t' != (*bufp
)[pos
])
586 * Note: groff does NOT like escape characters in the input.
587 * Instead of detecting this, we're just going to let it fly and
592 sz
= (size_t)(pos
- sv
);
594 if (1 == sz
&& '.' == (*bufp
)[sv
])
597 r
->last
->end
= malloc(sz
+ 1);
599 if (NULL
== r
->last
->end
) {
600 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
604 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
605 r
->last
->end
[(int)sz
] = '\0';
608 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
617 roff_block_sub(ROFF_ARGS
)
623 * First check whether a custom macro exists at this level. If
624 * it does, then check against it. This is some of groff's
625 * stranger behaviours. If we encountered a custom end-scope
626 * tag and that tag also happens to be a "real" macro, then we
627 * need to try interpreting it again as a real macro. If it's
628 * not, then return ignore. Else continue.
633 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
636 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
637 if ((*bufp
)[i
] != r
->last
->end
[j
])
640 if ('\0' == r
->last
->end
[j
] &&
641 ('\0' == (*bufp
)[i
] ||
643 '\t' == (*bufp
)[i
])) {
645 roffnode_cleanscope(r
);
647 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
654 * If we have no custom end-query or lookup failed, then try
655 * pulling it out of the hashtable.
659 t
= roff_parse(*bufp
, &pos
);
661 /* If we're not a comment-end, then throw it away. */
662 if (ROFF_cblock
!= t
)
665 assert(roffs
[t
].proc
);
666 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
667 ln
, ppos
, pos
, offs
));
673 roff_block_text(ROFF_ARGS
)
682 roff_cond_sub(ROFF_ARGS
)
692 * Clean out scope. If we've closed ourselves, then don't
697 roffnode_cleanscope(r
);
700 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
702 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
703 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
706 * A denied conditional must evaluate its children if and only
707 * if they're either structurally required (such as loops and
708 * conditionals) or a closing macro.
710 if (ROFFRULE_DENY
== rr
)
711 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
715 assert(roffs
[t
].proc
);
716 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
717 ln
, ppos
, pos
, offs
));
723 roff_cond_text(ROFF_ARGS
)
731 * We display the value of the text if out current evaluation
732 * scope permits us to do so.
736 if (NULL
== (ep
= strstr(st
, "\\}"))) {
737 roffnode_cleanscope(r
);
738 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
741 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
744 roffnode_cleanscope(r
);
745 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
750 roff_evalcond(const char *v
, int *pos
)
756 return(ROFFRULE_ALLOW
);
763 return(ROFFRULE_DENY
);
768 while (v
[*pos
] && ' ' != v
[*pos
])
770 return(ROFFRULE_DENY
);
790 /* Stack overflow! */
792 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
793 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
797 /* First, evaluate the conditional. */
799 if (ROFF_el
== tok
) {
801 * An `.el' will get the value of the current rstack
802 * entry set in prior `ie' calls or defaults to DENY.
804 if (r
->rstackpos
< 0)
805 rule
= ROFFRULE_DENY
;
807 rule
= r
->rstack
[r
->rstackpos
];
809 rule
= roff_evalcond(*bufp
, &pos
);
813 while (' ' == (*bufp
)[pos
])
817 * Roff is weird. If we have just white-space after the
818 * conditional, it's considered the BODY and we exit without
819 * really doing anything. Warn about this. It's probably
823 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
824 if ((*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
829 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
832 r
->last
->rule
= rule
;
834 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
835 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
837 if (ROFF_ie
== tok
) {
839 * An if-else will put the NEGATION of the current
840 * evaluated conditional into the stack.
843 if (ROFFRULE_DENY
== r
->last
->rule
)
844 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
846 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
849 /* If the parent has false as its rule, then so do we. */
851 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
852 r
->last
->rule
= ROFFRULE_DENY
;
853 ROFF_DEBUG("roff: cond override: %s -> deny\n",
858 * Determine scope. If we're invoked with "\{" trailing the
859 * conditional, then we're in a multiline scope. Else our scope
860 * expires on the next line.
863 r
->last
->endspan
= 1;
865 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
866 r
->last
->endspan
= -1;
868 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
871 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
875 * If there are no arguments on the line, the next-line scope is
879 if ('\0' == (*bufp
)[pos
])
882 /* Otherwise re-run the roff parser after recalculating. */
893 char *name
, *string
, *end
;
900 while (*string
&& ' ' != *string
)
904 if (*string
&& '"' == *string
)
906 while (*string
&& ' ' == *string
)
917 roff_setstr(name
, string
);
926 const char *key
, *val
;
932 /* Parse register request. */
933 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
937 * Set our nil terminator. Because this line is going to be
938 * ignored anyway, we can munge it as we please.
941 (*bufp
)[pos
++] = '\0';
943 /* Skip whitespace to register token. */
944 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
949 /* Process register token. */
951 if (0 == strcmp(key
, "nS")) {
952 rg
[(int)REG_nS
].set
= 1;
953 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
954 rg
[(int)REG_nS
].v
.u
= 0;
956 ROFF_DEBUG("roff: register nS: %u\n",
957 rg
[(int)REG_nS
].v
.u
);
959 ROFF_DEBUG("roff: ignoring register: %s\n", key
);
966 roff_setstr(const char *name
, const char *string
)
972 while (n
&& strcmp(name
, n
->name
))
977 if (NULL
== (namecopy
= strdup(name
)))
979 if (NULL
== (n
= malloc(sizeof(struct roffstr
)))) {
984 n
->next
= first_string
;
988 n
->string
= strdup(string
);
995 roff_getstr(const char *name
)
1000 while (n
&& strcmp(name
, n
->name
))
1009 roff_getstrn(const char *name
, size_t len
)
1014 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[len
]))
1025 struct roffstr
*n
, *nn
;
1027 for (n
= first_string
; n
; n
= nn
) {
1033 first_string
= NULL
;