]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.97 2010/07/27 19:56:50 kristaps 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.
32 #include "libmandoc.h"
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)
74 char *name
; /* key of symbol */
75 char *string
; /* current value */
76 struct roffstr
*next
; /* next in list */
80 struct roffnode
*last
; /* leaf of stack */
81 mandocmsg msg
; /* err/warn/fatal messages */
82 void *data
; /* privdata for messages */
83 enum roffrule rstack
[RSTACK_MAX
]; /* stack of !`ie' rules */
84 int rstackpos
; /* position in rstack */
85 struct regset
*regs
; /* read/writable registers */
86 struct roffstr
*first_string
;
90 enum rofft tok
; /* type of node */
91 struct roffnode
*parent
; /* up one in stack */
92 int line
; /* parse line */
93 int col
; /* parse col */
94 char *end
; /* end-rules: custom token */
95 int endspan
; /* end-rules: next-line or infty */
96 enum roffrule rule
; /* current evaluation rule */
99 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
100 enum rofft tok, /* tok of macro */ \
101 char **bufp, /* input buffer */ \
102 size_t *szp, /* size of input buffer */ \
103 int ln, /* parse line */ \
104 int ppos, /* original pos in buffer */ \
105 int pos, /* current pos in buffer */ \
106 int *offs /* reset offset of buffer data */
108 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
111 const char *name
; /* macro name */
112 roffproc proc
; /* process new macro */
113 roffproc text
; /* process as child text of macro */
114 roffproc sub
; /* process as child of macro */
116 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
117 struct roffmac
*next
;
120 static enum rofferr
roff_block(ROFF_ARGS
);
121 static enum rofferr
roff_block_text(ROFF_ARGS
);
122 static enum rofferr
roff_block_sub(ROFF_ARGS
);
123 static enum rofferr
roff_cblock(ROFF_ARGS
);
124 static enum rofferr
roff_ccond(ROFF_ARGS
);
125 static enum rofferr
roff_cond(ROFF_ARGS
);
126 static enum rofferr
roff_cond_text(ROFF_ARGS
);
127 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
128 static enum rofferr
roff_ds(ROFF_ARGS
);
129 static enum roffrule
roff_evalcond(const char *, int *);
130 static void roff_freestr(struct roff
*);
131 static const char *roff_getstrn(const struct roff
*,
132 const char *, size_t);
133 static enum rofferr
roff_line(ROFF_ARGS
);
134 static enum rofferr
roff_nr(ROFF_ARGS
);
135 static int roff_res(struct roff
*,
136 char **, size_t *, int);
137 static void roff_setstr(struct roff
*,
138 const char *, const char *);
140 /* See roff_hash_find() */
144 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
146 static struct roffmac
*hash
[HASHWIDTH
];
148 static struct roffmac roffs
[ROFF_MAX
] = {
149 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
150 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
151 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
152 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
153 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
154 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
155 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
156 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
157 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
158 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
159 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
160 { "rm", roff_line
, NULL
, NULL
, 0, NULL
},
161 { "tr", roff_line
, NULL
, NULL
, 0, NULL
},
162 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
163 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
164 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
167 static void roff_free1(struct roff
*);
168 static enum rofft
roff_hash_find(const char *);
169 static void roff_hash_init(void);
170 static void roffnode_cleanscope(struct roff
*);
171 static int roffnode_push(struct roff
*,
172 enum rofft
, int, int);
173 static void roffnode_pop(struct roff
*);
174 static enum rofft
roff_parse(const char *, int *);
175 static int roff_parse_nat(const char *, unsigned int *);
177 /* See roff_hash_find() */
178 #define ROFF_HASH(p) (p[0] - ASCII_LO)
186 for (i
= 0; i
< (int)ROFF_MAX
; i
++) {
187 assert(roffs
[i
].name
[0] >= ASCII_LO
);
188 assert(roffs
[i
].name
[0] <= ASCII_HI
);
190 buc
= ROFF_HASH(roffs
[i
].name
);
192 if (NULL
!= (n
= hash
[buc
])) {
193 for ( ; n
->next
; n
= n
->next
)
197 hash
[buc
] = &roffs
[i
];
203 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
204 * the nil-terminated string name could be found.
207 roff_hash_find(const char *p
)
213 * libroff has an extremely simple hashtable, for the time
214 * being, which simply keys on the first character, which must
215 * be printable, then walks a chain. It works well enough until
219 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
224 if (NULL
== (n
= hash
[buc
]))
226 for ( ; n
; n
= n
->next
)
227 if (0 == strcmp(n
->name
, p
))
228 return((enum rofft
)(n
- roffs
));
235 * Pop the current node off of the stack of roff instructions currently
239 roffnode_pop(struct roff
*r
)
246 if (ROFF_el
== p
->tok
)
247 if (r
->rstackpos
> -1)
250 r
->last
= r
->last
->parent
;
258 * Push a roff node onto the instruction stack. This must later be
259 * removed with roffnode_pop().
262 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
266 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
267 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
275 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
283 roff_free1(struct roff
*r
)
293 roff_reset(struct roff
*r
)
301 roff_free(struct roff
*r
)
310 roff_alloc(struct regset
*regs
, const mandocmsg msg
, void *data
)
314 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
315 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
330 * Pre-filter each and every line for reserved words (one beginning with
331 * `\*', e.g., `\*(ab'). These must be handled before the actual line
335 roff_res(struct roff
*r
, char **bufp
, size_t *szp
, int pos
)
337 const char *cp
, *cpp
, *st
, *res
;
343 for (cp
= &(*bufp
)[pos
]; (cpp
= strstr(cp
, "\\*")); cp
++) {
361 for (i
= 0; 0 == maxl
|| i
< maxl
; i
++, cp
++) {
363 return(1); /* Error. */
364 if (0 == maxl
&& ']' == *cp
)
368 res
= roff_getstrn(r
, st
, (size_t)i
);
375 ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i
, st
);
377 nsz
= *szp
+ strlen(res
) + 1;
378 n
= mandoc_malloc(nsz
);
382 strlcat(n
, *bufp
, (size_t)(cpp
- *bufp
+ 1));
383 strlcat(n
, res
, nsz
);
384 strlcat(n
, cp
+ (maxl
? 0 : 1), nsz
);
398 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
399 size_t *szp
, int pos
, int *offs
)
405 * Run the reserved-word filter only if we have some reserved
409 if (r
->first_string
&& ! roff_res(r
, bufp
, szp
, pos
))
413 * First, if a scope is open and we're not a macro, pass the
414 * text through the macro's filter. If a scope isn't open and
415 * we're not a macro, just let it through.
418 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
420 assert(roffs
[t
].text
);
421 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
422 roffs
[t
].name
, &(*bufp
)[pos
]);
423 return((*roffs
[t
].text
)
425 ln
, pos
, pos
, offs
));
426 } else if ( ! ROFF_CTL((*bufp
)[pos
]))
430 * If a scope is open, go to the child handler for that macro,
431 * as it may want to preprocess before doing anything with it.
436 assert(roffs
[t
].sub
);
437 ROFF_DEBUG("roff: intercept scoped context: %s\n",
439 return((*roffs
[t
].sub
)
441 ln
, pos
, pos
, offs
));
445 * Lastly, as we've no scope open, try to look up and execute
446 * the new macro. If no macro is found, simply return and let
447 * the compilers handle it.
451 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
454 ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
455 roffs
[t
].name
, &(*bufp
)[pos
]);
456 assert(roffs
[t
].proc
);
457 return((*roffs
[t
].proc
)
459 ln
, ppos
, pos
, offs
));
464 roff_endparse(struct roff
*r
)
469 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
470 r
->last
->col
, NULL
));
475 * Parse a roff node's type from the input buffer. This must be in the
476 * form of ".foo xxx" in the usual way.
479 roff_parse(const char *buf
, int *pos
)
485 assert(ROFF_CTL(buf
[*pos
]));
488 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
491 if ('\0' == buf
[*pos
])
494 for (j
= 0; j
< 4; j
++, (*pos
)++)
495 if ('\0' == (mac
[j
] = buf
[*pos
]))
497 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
505 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
508 while (buf
[*pos
] && ' ' == buf
[*pos
])
516 roff_parse_nat(const char *buf
, unsigned int *res
)
522 lval
= strtol(buf
, &ep
, 10);
523 if (buf
[0] == '\0' || *ep
!= '\0')
525 if ((errno
== ERANGE
&&
526 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
527 (lval
> INT_MAX
|| lval
< 0))
530 *res
= (unsigned int)lval
;
537 roff_cblock(ROFF_ARGS
)
541 * A block-close `..' should only be invoked as a child of an
542 * ignore macro, otherwise raise a warning and just ignore it.
545 if (NULL
== r
->last
) {
546 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
551 switch (r
->last
->tok
) {
567 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
573 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
577 roffnode_cleanscope(r
);
584 roffnode_cleanscope(struct roff
*r
)
588 if (--r
->last
->endspan
< 0)
597 roff_ccond(ROFF_ARGS
)
600 if (NULL
== r
->last
) {
601 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
606 switch (r
->last
->tok
) {
614 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
619 if (r
->last
->endspan
> -1) {
620 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
626 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
630 roffnode_cleanscope(r
);
637 roff_block(ROFF_ARGS
)
642 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
643 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
646 } else if (ROFF_ig
!= tok
) {
647 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
649 while (' ' == (*bufp
)[pos
])
653 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
656 if ('\0' == (*bufp
)[pos
])
660 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
661 '\t' != (*bufp
)[pos
])
665 * Note: groff does NOT like escape characters in the input.
666 * Instead of detecting this, we're just going to let it fly and
671 sz
= (size_t)(pos
- sv
);
673 if (1 == sz
&& '.' == (*bufp
)[sv
])
676 r
->last
->end
= malloc(sz
+ 1);
678 if (NULL
== r
->last
->end
) {
679 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
683 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
684 r
->last
->end
[(int)sz
] = '\0';
687 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
696 roff_block_sub(ROFF_ARGS
)
702 * First check whether a custom macro exists at this level. If
703 * it does, then check against it. This is some of groff's
704 * stranger behaviours. If we encountered a custom end-scope
705 * tag and that tag also happens to be a "real" macro, then we
706 * need to try interpreting it again as a real macro. If it's
707 * not, then return ignore. Else continue.
712 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
715 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
716 if ((*bufp
)[i
] != r
->last
->end
[j
])
719 if ('\0' == r
->last
->end
[j
] &&
720 ('\0' == (*bufp
)[i
] ||
722 '\t' == (*bufp
)[i
])) {
724 roffnode_cleanscope(r
);
726 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
733 * If we have no custom end-query or lookup failed, then try
734 * pulling it out of the hashtable.
738 t
= roff_parse(*bufp
, &pos
);
740 /* If we're not a comment-end, then throw it away. */
741 if (ROFF_cblock
!= t
)
744 assert(roffs
[t
].proc
);
745 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
746 ln
, ppos
, pos
, offs
));
752 roff_block_text(ROFF_ARGS
)
761 roff_cond_sub(ROFF_ARGS
)
771 * Clean out scope. If we've closed ourselves, then don't
776 roffnode_cleanscope(r
);
779 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
781 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
782 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
785 * A denied conditional must evaluate its children if and only
786 * if they're either structurally required (such as loops and
787 * conditionals) or a closing macro.
789 if (ROFFRULE_DENY
== rr
)
790 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
794 assert(roffs
[t
].proc
);
795 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
796 ln
, ppos
, pos
, offs
));
802 roff_cond_text(ROFF_ARGS
)
810 * We display the value of the text if out current evaluation
811 * scope permits us to do so.
815 if (NULL
== (ep
= strstr(st
, "\\}"))) {
816 roffnode_cleanscope(r
);
817 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
820 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
823 roffnode_cleanscope(r
);
824 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
829 roff_evalcond(const char *v
, int *pos
)
835 return(ROFFRULE_ALLOW
);
842 return(ROFFRULE_DENY
);
847 while (v
[*pos
] && ' ' != v
[*pos
])
849 return(ROFFRULE_DENY
);
869 /* Stack overflow! */
871 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
872 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
876 /* First, evaluate the conditional. */
878 if (ROFF_el
== tok
) {
880 * An `.el' will get the value of the current rstack
881 * entry set in prior `ie' calls or defaults to DENY.
883 if (r
->rstackpos
< 0)
884 rule
= ROFFRULE_DENY
;
886 rule
= r
->rstack
[r
->rstackpos
];
888 rule
= roff_evalcond(*bufp
, &pos
);
892 while (' ' == (*bufp
)[pos
])
896 * Roff is weird. If we have just white-space after the
897 * conditional, it's considered the BODY and we exit without
898 * really doing anything. Warn about this. It's probably
902 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
903 if ((*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
908 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
911 r
->last
->rule
= rule
;
913 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
914 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
916 if (ROFF_ie
== tok
) {
918 * An if-else will put the NEGATION of the current
919 * evaluated conditional into the stack.
922 if (ROFFRULE_DENY
== r
->last
->rule
)
923 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
925 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
928 /* If the parent has false as its rule, then so do we. */
930 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
931 r
->last
->rule
= ROFFRULE_DENY
;
932 ROFF_DEBUG("roff: cond override: %s -> deny\n",
937 * Determine scope. If we're invoked with "\{" trailing the
938 * conditional, then we're in a multiline scope. Else our scope
939 * expires on the next line.
942 r
->last
->endspan
= 1;
944 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
945 r
->last
->endspan
= -1;
947 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
950 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
954 * If there are no arguments on the line, the next-line scope is
958 if ('\0' == (*bufp
)[pos
])
961 /* Otherwise re-run the roff parser after recalculating. */
975 * A symbol is named by the first word following the macro
976 * invocation up to a space. Its value is anything after the
977 * name's trailing whitespace and optional double-quote. Thus,
981 * will have `bar " ' as its value.
989 /* Read until end of name. */
990 while (*string
&& ' ' != *string
)
993 /* Nil-terminate name. */
997 /* Read past spaces. */
998 while (*string
&& ' ' == *string
)
1001 /* Read passed initial double-quote. */
1002 if (*string
&& '"' == *string
)
1005 /* The rest is the value. */
1006 roff_setstr(r
, name
, string
);
1015 const char *key
, *val
;
1018 key
= &(*bufp
)[pos
];
1021 /* Parse register request. */
1022 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
1026 * Set our nil terminator. Because this line is going to be
1027 * ignored anyway, we can munge it as we please.
1030 (*bufp
)[pos
++] = '\0';
1032 /* Skip whitespace to register token. */
1033 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
1036 val
= &(*bufp
)[pos
];
1038 /* Process register token. */
1040 if (0 == strcmp(key
, "nS")) {
1041 rg
[(int)REG_nS
].set
= 1;
1042 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
1043 rg
[(int)REG_nS
].v
.u
= 0;
1045 ROFF_DEBUG("roff: register nS: %u\n",
1046 rg
[(int)REG_nS
].v
.u
);
1048 ROFF_DEBUG("roff: ignoring register: %s\n", key
);
1055 roff_setstr(struct roff
*r
, const char *name
, const char *string
)
1060 n
= r
->first_string
;
1061 while (n
&& strcmp(name
, n
->name
))
1065 namecopy
= mandoc_strdup(name
);
1066 n
= mandoc_malloc(sizeof(struct roffstr
));
1068 n
->next
= r
->first_string
;
1069 r
->first_string
= n
;
1073 ROFF_DEBUG("roff: new symbol: [%s] = [%s]\n", name
, string
);
1074 n
->string
= string
? strdup(string
) : NULL
;
1079 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
1081 const struct roffstr
*n
;
1083 n
= r
->first_string
;
1084 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[(int)len
]))
1087 return(n
? n
->string
: NULL
);
1092 roff_freestr(struct roff
*r
)
1094 struct roffstr
*n
, *nn
;
1096 for (n
= r
->first_string
; n
; n
= nn
) {
1103 r
->first_string
= NULL
;