]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
a644bc851c5eee4f15c6e57fa2f2fb51db886bbe
1 /* $Id: roff.c,v 1.92 2010/07/03 16:02:12 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.
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
;
114 struct roffstr
*next
;
117 static enum rofferr
roff_block(ROFF_ARGS
);
118 static enum rofferr
roff_block_text(ROFF_ARGS
);
119 static enum rofferr
roff_block_sub(ROFF_ARGS
);
120 static enum rofferr
roff_cblock(ROFF_ARGS
);
121 static enum rofferr
roff_ccond(ROFF_ARGS
);
122 static enum rofferr
roff_cond(ROFF_ARGS
);
123 static enum rofferr
roff_cond_text(ROFF_ARGS
);
124 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
125 static enum rofferr
roff_ds(ROFF_ARGS
);
126 static enum rofferr
roff_line(ROFF_ARGS
);
127 static enum rofferr
roff_nr(ROFF_ARGS
);
128 static enum roffrule
roff_evalcond(const char *, int *);
130 /* See roff_hash_find() */
134 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
136 static struct roffmac
*hash
[HASHWIDTH
];
138 static struct roffmac roffs
[ROFF_MAX
] = {
139 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
140 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
141 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
142 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
143 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
144 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
145 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
146 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
147 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
148 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
149 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
150 { "rm", roff_line
, NULL
, NULL
, 0, NULL
},
151 { "tr", roff_line
, NULL
, NULL
, 0, NULL
},
152 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
153 { "\\}", roff_ccond
, NULL
, NULL
, 0, NULL
},
154 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
157 static void roff_free1(struct roff
*);
158 static enum rofft
roff_hash_find(const char *);
159 static void roff_hash_init(void);
160 static void roffnode_cleanscope(struct roff
*);
161 static int roffnode_push(struct roff
*,
162 enum rofft
, int, int);
163 static void roffnode_pop(struct roff
*);
164 static enum rofft
roff_parse(const char *, int *);
165 static int roff_parse_nat(const char *, unsigned int *);
167 /* See roff_hash_find() */
168 #define ROFF_HASH(p) (p[0] - ASCII_LO)
176 for (i
= 0; i
< (int)ROFF_MAX
; i
++) {
177 assert(roffs
[i
].name
[0] >= ASCII_LO
);
178 assert(roffs
[i
].name
[0] <= ASCII_HI
);
180 buc
= ROFF_HASH(roffs
[i
].name
);
182 if (NULL
!= (n
= hash
[buc
])) {
183 for ( ; n
->next
; n
= n
->next
)
187 hash
[buc
] = &roffs
[i
];
193 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
194 * the nil-terminated string name could be found.
197 roff_hash_find(const char *p
)
203 * libroff has an extremely simple hashtable, for the time
204 * being, which simply keys on the first character, which must
205 * be printable, then walks a chain. It works well enough until
209 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
214 if (NULL
== (n
= hash
[buc
]))
216 for ( ; n
; n
= n
->next
)
217 if (0 == strcmp(n
->name
, p
))
218 return((enum rofft
)(n
- roffs
));
225 * Pop the current node off of the stack of roff instructions currently
229 roffnode_pop(struct roff
*r
)
236 if (ROFF_el
== p
->tok
)
237 if (r
->rstackpos
> -1)
240 r
->last
= r
->last
->parent
;
248 * Push a roff node onto the instruction stack. This must later be
249 * removed with roffnode_pop().
252 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
256 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
257 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
265 p
->rule
= p
->parent
? p
->parent
->rule
: ROFFRULE_DENY
;
273 roff_free1(struct roff
*r
)
283 roff_reset(struct roff
*r
)
291 roff_free(struct roff
*r
)
300 roff_alloc(struct regset
*regs
, const mandocmsg msg
, void *data
)
304 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
305 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
320 roff_parseln(struct roff
*r
, int ln
, char **bufp
,
321 size_t *szp
, int pos
, int *offs
)
327 * First, if a scope is open and we're not a macro, pass the
328 * text through the macro's filter. If a scope isn't open and
329 * we're not a macro, just let it through.
332 if (r
->last
&& ! ROFF_CTL((*bufp
)[pos
])) {
334 assert(roffs
[t
].text
);
335 ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
336 roffs
[t
].name
, &(*bufp
)[pos
]);
337 return((*roffs
[t
].text
)
339 ln
, pos
, pos
, offs
));
340 } else if ( ! ROFF_CTL((*bufp
)[pos
])) {
341 ROFF_DEBUG("roff: pass non-scoped text: [%s]\n",
347 * If a scope is open, go to the child handler for that macro,
348 * as it may want to preprocess before doing anything with it.
353 assert(roffs
[t
].sub
);
354 ROFF_DEBUG("roff: intercept scoped context: %s\n",
356 return((*roffs
[t
].sub
)
358 ln
, pos
, pos
, offs
));
362 * Lastly, as we've no scope open, try to look up and execute
363 * the new macro. If no macro is found, simply return and let
364 * the compilers handle it.
368 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
))) {
369 ROFF_DEBUG("roff: pass non-scoped non-macro: [%s]\n",
374 ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
375 roffs
[t
].name
, &(*bufp
)[pos
]);
376 assert(roffs
[t
].proc
);
377 return((*roffs
[t
].proc
)
379 ln
, ppos
, pos
, offs
));
384 roff_endparse(struct roff
*r
)
389 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
, r
->last
->line
,
390 r
->last
->col
, NULL
));
395 * Parse a roff node's type from the input buffer. This must be in the
396 * form of ".foo xxx" in the usual way.
399 roff_parse(const char *buf
, int *pos
)
405 assert(ROFF_CTL(buf
[*pos
]));
408 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
411 if ('\0' == buf
[*pos
])
414 for (j
= 0; j
< 4; j
++, (*pos
)++)
415 if ('\0' == (mac
[j
] = buf
[*pos
]))
417 else if (' ' == buf
[*pos
] || (j
&& '\\' == buf
[*pos
]))
425 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
428 while (buf
[*pos
] && ' ' == buf
[*pos
])
436 roff_parse_nat(const char *buf
, unsigned int *res
)
442 lval
= strtol(buf
, &ep
, 10);
443 if (buf
[0] == '\0' || *ep
!= '\0')
445 if ((errno
== ERANGE
&&
446 (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
447 (lval
> INT_MAX
|| lval
< 0))
450 *res
= (unsigned int)lval
;
457 roff_cblock(ROFF_ARGS
)
461 * A block-close `..' should only be invoked as a child of an
462 * ignore macro, otherwise raise a warning and just ignore it.
465 if (NULL
== r
->last
) {
466 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
471 switch (r
->last
->tok
) {
487 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
493 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
497 roffnode_cleanscope(r
);
504 roffnode_cleanscope(struct roff
*r
)
508 if (--r
->last
->endspan
< 0)
517 roff_ccond(ROFF_ARGS
)
520 if (NULL
== r
->last
) {
521 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
526 switch (r
->last
->tok
) {
534 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
539 if (r
->last
->endspan
> -1) {
540 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
546 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
550 roffnode_cleanscope(r
);
557 roff_block(ROFF_ARGS
)
562 if (ROFF_ig
!= tok
&& '\0' == (*bufp
)[pos
]) {
563 if ( ! (*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
566 } else if (ROFF_ig
!= tok
) {
567 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
569 while (' ' == (*bufp
)[pos
])
573 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
576 if ('\0' == (*bufp
)[pos
])
580 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
] &&
581 '\t' != (*bufp
)[pos
])
585 * Note: groff does NOT like escape characters in the input.
586 * Instead of detecting this, we're just going to let it fly and
591 sz
= (size_t)(pos
- sv
);
593 if (1 == sz
&& '.' == (*bufp
)[sv
])
596 r
->last
->end
= malloc(sz
+ 1);
598 if (NULL
== r
->last
->end
) {
599 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, pos
, NULL
);
603 memcpy(r
->last
->end
, *bufp
+ sv
, sz
);
604 r
->last
->end
[(int)sz
] = '\0';
607 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, pos
, NULL
))
616 roff_block_sub(ROFF_ARGS
)
622 * First check whether a custom macro exists at this level. If
623 * it does, then check against it. This is some of groff's
624 * stranger behaviours. If we encountered a custom end-scope
625 * tag and that tag also happens to be a "real" macro, then we
626 * need to try interpreting it again as a real macro. If it's
627 * not, then return ignore. Else continue.
632 while (' ' == (*bufp
)[i
] || '\t' == (*bufp
)[i
])
635 for (j
= 0; r
->last
->end
[j
]; j
++, i
++)
636 if ((*bufp
)[i
] != r
->last
->end
[j
])
639 if ('\0' == r
->last
->end
[j
] &&
640 ('\0' == (*bufp
)[i
] ||
642 '\t' == (*bufp
)[i
])) {
644 roffnode_cleanscope(r
);
646 if (ROFF_MAX
!= roff_parse(*bufp
, &pos
))
653 * If we have no custom end-query or lookup failed, then try
654 * pulling it out of the hashtable.
658 t
= roff_parse(*bufp
, &pos
);
660 /* If we're not a comment-end, then throw it away. */
661 if (ROFF_cblock
!= t
)
664 assert(roffs
[t
].proc
);
665 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
666 ln
, ppos
, pos
, offs
));
672 roff_block_text(ROFF_ARGS
)
681 roff_cond_sub(ROFF_ARGS
)
691 * Clean out scope. If we've closed ourselves, then don't
696 roffnode_cleanscope(r
);
699 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
701 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &pos
)))
702 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
705 * A denied conditional must evaluate its children if and only
706 * if they're either structurally required (such as loops and
707 * conditionals) or a closing macro.
709 if (ROFFRULE_DENY
== rr
)
710 if ( ! (ROFFMAC_STRUCT
& roffs
[t
].flags
))
714 assert(roffs
[t
].proc
);
715 return((*roffs
[t
].proc
)(r
, t
, bufp
, szp
,
716 ln
, ppos
, pos
, offs
));
722 roff_cond_text(ROFF_ARGS
)
730 * We display the value of the text if out current evaluation
731 * scope permits us to do so.
735 if (NULL
== (ep
= strstr(st
, "\\}"))) {
736 roffnode_cleanscope(r
);
737 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
740 if (ep
== st
|| (ep
> st
&& '\\' != *(ep
- 1)))
743 roffnode_cleanscope(r
);
744 return(ROFFRULE_DENY
== rr
? ROFF_IGN
: ROFF_CONT
);
749 roff_evalcond(const char *v
, int *pos
)
755 return(ROFFRULE_ALLOW
);
762 return(ROFFRULE_DENY
);
767 while (v
[*pos
] && ' ' != v
[*pos
])
769 return(ROFFRULE_DENY
);
789 /* Stack overflow! */
791 if (ROFF_ie
== tok
&& r
->rstackpos
== RSTACK_MAX
- 1) {
792 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
796 /* First, evaluate the conditional. */
798 if (ROFF_el
== tok
) {
800 * An `.el' will get the value of the current rstack
801 * entry set in prior `ie' calls or defaults to DENY.
803 if (r
->rstackpos
< 0)
804 rule
= ROFFRULE_DENY
;
806 rule
= r
->rstack
[r
->rstackpos
];
808 rule
= roff_evalcond(*bufp
, &pos
);
812 while (' ' == (*bufp
)[pos
])
816 * Roff is weird. If we have just white-space after the
817 * conditional, it's considered the BODY and we exit without
818 * really doing anything. Warn about this. It's probably
822 if ('\0' == (*bufp
)[pos
] && sv
!= pos
) {
823 if ((*r
->msg
)(MANDOCERR_NOARGS
, r
->data
, ln
, ppos
, NULL
))
828 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
831 r
->last
->rule
= rule
;
833 ROFF_DEBUG("roff: cond: %s -> %s\n", roffs
[tok
].name
,
834 ROFFRULE_ALLOW
== rule
? "allow" : "deny");
836 if (ROFF_ie
== tok
) {
838 * An if-else will put the NEGATION of the current
839 * evaluated conditional into the stack.
842 if (ROFFRULE_DENY
== r
->last
->rule
)
843 r
->rstack
[r
->rstackpos
] = ROFFRULE_ALLOW
;
845 r
->rstack
[r
->rstackpos
] = ROFFRULE_DENY
;
848 /* If the parent has false as its rule, then so do we. */
850 if (r
->last
->parent
&& ROFFRULE_DENY
== r
->last
->parent
->rule
) {
851 r
->last
->rule
= ROFFRULE_DENY
;
852 ROFF_DEBUG("roff: cond override: %s -> deny\n",
857 * Determine scope. If we're invoked with "\{" trailing the
858 * conditional, then we're in a multiline scope. Else our scope
859 * expires on the next line.
862 r
->last
->endspan
= 1;
864 if ('\\' == (*bufp
)[pos
] && '{' == (*bufp
)[pos
+ 1]) {
865 r
->last
->endspan
= -1;
867 ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
870 ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
874 * If there are no arguments on the line, the next-line scope is
878 if ('\0' == (*bufp
)[pos
])
881 /* Otherwise re-run the roff parser after recalculating. */
892 char *name
, *string
, *end
;
899 while (*string
&& ' ' != *string
)
903 if (*string
&& '"' == *string
)
905 while (*string
&& ' ' == *string
)
916 roff_setstr(name
, string
);
925 const char *key
, *val
;
931 /* Parse register request. */
932 while ((*bufp
)[pos
] && ' ' != (*bufp
)[pos
])
936 * Set our nil terminator. Because this line is going to be
937 * ignored anyway, we can munge it as we please.
940 (*bufp
)[pos
++] = '\0';
942 /* Skip whitespace to register token. */
943 while ((*bufp
)[pos
] && ' ' == (*bufp
)[pos
])
948 /* Process register token. */
950 if (0 == strcmp(key
, "nS")) {
951 rg
[(int)REG_nS
].set
= 1;
952 if ( ! roff_parse_nat(val
, &rg
[(int)REG_nS
].v
.u
))
953 rg
[(int)REG_nS
].v
.u
= 0;
955 ROFF_DEBUG("roff: register nS: %u\n",
956 rg
[(int)REG_nS
].v
.u
);
958 ROFF_DEBUG("roff: ignoring register: %s\n", key
);
965 roff_setstr(const char *name
, const char *string
)
971 while (n
&& strcmp(name
, n
->name
))
976 if (NULL
== (namecopy
= strdup(name
)))
978 if (NULL
== (n
= malloc(sizeof(struct roffstr
)))) {
983 n
->next
= first_string
;
987 n
->string
= strdup(string
);
994 roff_getstr(const char *name
)
999 while (n
&& strcmp(name
, n
->name
))
1008 roff_getstrn(const char *name
, size_t len
)
1013 while (n
&& (strncmp(name
, n
->name
, len
) || '\0' != n
->name
[len
]))
1024 struct roffstr
*n
, *nn
;
1026 for (n
= first_string
; n
; n
= nn
) {
1032 first_string
= NULL
;