]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
89c3fdd406805c8b6a20fb200a052e0b1199e8c3
1 /* $Id: roff.c,v 1.72 2010/05/15 22:22:51 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.
39 struct roffnode
*last
; /* leaf of stack */
40 mandocmsg msg
; /* err/warn/fatal messages */
41 void *data
; /* privdata for messages */
45 enum rofft tok
; /* type of node */
46 struct roffnode
*parent
; /* up one in stack */
47 char *end
; /* custom end-token */
48 int line
; /* parse line */
49 int col
; /* parse col */
52 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
53 enum rofft tok, /* tok of macro */ \
54 char **bufp, /* input buffer */ \
55 size_t *szp, /* size of input buffer */ \
56 int ln, /* parse line */ \
57 int ppos /* current pos in buffer */
59 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
62 const char *name
; /* macro name */
63 roffproc sub
; /* child of control black */
64 roffproc
new; /* root of stack (type = ROFF_MAX) */
67 static enum rofferr
roff_new_close(ROFF_ARGS
);
68 static enum rofferr
roff_new_ig(ROFF_ARGS
);
69 static enum rofferr
roff_sub_ig(ROFF_ARGS
);
71 const struct roffmac roffs
[ROFF_MAX
] = {
72 { "de", roff_sub_ig
, roff_new_ig
},
73 { "dei", roff_sub_ig
, roff_new_ig
},
74 { "am", roff_sub_ig
, roff_new_ig
},
75 { "ami", roff_sub_ig
, roff_new_ig
},
76 { "ig", roff_sub_ig
, roff_new_ig
},
77 { ".", NULL
, roff_new_close
},
80 static void roff_free1(struct roff
*);
81 static enum rofft
roff_hash_find(const char *);
82 static int roffnode_push(struct roff
*,
83 enum rofft
, int, int);
84 static void roffnode_pop(struct roff
*);
85 static enum rofft
roff_parse(const char *, int *);
89 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
90 * the nil-terminated string name could be found.
93 roff_hash_find(const char *p
)
97 /* FIXME: make this be fast and efficient. */
99 for (i
= 0; i
< (int)ROFF_MAX
; i
++)
100 if (0 == strcmp(roffs
[i
].name
, p
))
101 return((enum rofft
)i
);
108 * Pop the current node off of the stack of roff instructions currently
112 roffnode_pop(struct roff
*r
)
116 if (NULL
== (p
= r
->last
))
124 * Push a roff node onto the instruction stack. This must later be
125 * removed with roffnode_pop().
128 roffnode_push(struct roff
*r
, enum rofft tok
, int line
, int col
)
132 if (NULL
== (p
= calloc(1, sizeof(struct roffnode
)))) {
133 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, line
, col
, NULL
);
148 roff_free1(struct roff
*r
)
157 roff_reset(struct roff
*r
)
165 roff_free(struct roff
*r
)
174 roff_alloc(const mandocmsg msg
, void *data
)
178 if (NULL
== (r
= calloc(1, sizeof(struct roff
)))) {
179 (*msg
)(MANDOCERR_MEM
, data
, 0, 0, NULL
);
190 roff_parseln(struct roff
*r
, int ln
, char **bufp
, size_t *szp
)
195 if (NULL
!= r
->last
) {
197 * If there's a node on the stack, then jump directly
198 * into its processing function.
201 assert(roffs
[t
].sub
);
202 return((*roffs
[t
].sub
)(r
, t
, bufp
, szp
, ln
, 0));
203 } else if ('.' != (*bufp
)[0] && NULL
== r
->last
)
204 /* Return when in free text without a context. */
207 /* There's nothing on the stack: make us anew. */
209 if (ROFF_MAX
== (t
= roff_parse(*bufp
, &ppos
)))
212 assert(roffs
[t
].new);
213 return((*roffs
[t
].new)(r
, t
, bufp
, szp
, ln
, ppos
));
218 * Parse a roff node's type from the input buffer. This must be in the
219 * form of ".foo xxx" in the usual way.
222 roff_parse(const char *buf
, int *pos
)
228 assert('.' == buf
[0]);
231 while (buf
[*pos
] && (' ' == buf
[*pos
] || '\t' == buf
[*pos
]))
234 if ('\0' == buf
[*pos
])
237 for (j
= 0; j
< 4; j
++, (*pos
)++)
238 if ('\0' == (mac
[j
] = buf
[*pos
]))
240 else if (' ' == buf
[*pos
])
248 if (ROFF_MAX
== (t
= roff_hash_find(mac
)))
251 while (buf
[*pos
] && ' ' == buf
[*pos
])
260 roff_sub_ig(ROFF_ARGS
)
264 /* Ignore free-text lines. */
266 if ('.' != (*bufp
)[ppos
])
272 while ((*bufp
)[i
] && ' ' == (*bufp
)[i
])
275 for (j
= 0; r
->last
->end
[j
]; i
++, j
++)
276 if ((*bufp
)[i
] != r
->last
->end
[j
])
281 if ((*bufp
)[i
] && ' ' != (*bufp
)[i
])
284 while (' ' == (*bufp
)[i
])
287 } else if (ROFF_close
!= roff_parse(*bufp
, &i
))
292 if ('\0' == (*bufp
)[i
])
294 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, i
, NULL
))
303 roff_new_close(ROFF_ARGS
)
306 if ( ! (*r
->msg
)(MANDOCERR_NOSCOPE
, r
->data
, ln
, ppos
, NULL
))
315 roff_new_ig(ROFF_ARGS
)
319 if ( ! roffnode_push(r
, tok
, ln
, ppos
))
322 if (ROFF_ig
!= tok
) {
323 while ((*bufp
)[ppos
] && ' ' != (*bufp
)[ppos
])
325 while (' ' == (*bufp
)[ppos
])
331 while ((*bufp
)[i
] && ' ' != (*bufp
)[i
])
338 if ( ! (*r
->msg
)(MANDOCERR_ARGSLOST
, r
->data
, ln
, i
, NULL
))
342 * If the macro has arguments, the first argument (up to the
343 * next whitespace) is interpreted as an argument marking the
344 * macro close. Thus, `.ig foo' will close at `.foo'.
346 * NOTE: the closing macro `.foo' in the above case is not
347 * allowed to have leading spaces with old groff! Thus `.foo'
348 * != `. foo'. Oh yeah, everything after the `.foo' is lost.
349 * Merry fucking Christmas.
352 r
->last
->end
= malloc((size_t)i
- ppos
+ 1);
353 if (NULL
== r
->last
->end
) {
354 (*r
->msg
)(MANDOCERR_MEM
, r
->data
, ln
, ppos
, NULL
);
358 memcpy(r
->last
->end
, &(*bufp
)[ppos
], (size_t)i
- ppos
);
359 r
->last
->end
[(size_t)i
- ppos
] = '\0';
366 roff_endparse(struct roff
*r
)
371 return((*r
->msg
)(MANDOCERR_SCOPEEXIT
, r
->data
,
372 r
->last
->line
, r
->last
->col
, NULL
));