]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.54 2009/03/08 12:40:27 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
30 * Main caller in the libmdoc library. This begins the parsing routine,
31 * handles allocation of data, and so forth. Most of the "work" is done
32 * in macro.c and validate.c.
35 static struct mdoc_node
*mdoc_node_alloc(const struct mdoc
*);
36 static int mdoc_node_append(struct mdoc
*,
39 static int parsetext(struct mdoc
*, int, char *);
40 static int parsemacro(struct mdoc
*, int, char *);
43 const char *const __mdoc_macronames
[MDOC_MAX
] = {
44 "\\\"", "Dd", "Dt", "Os",
45 "Sh", "Ss", "Pp", "D1",
46 "Dl", "Bd", "Ed", "Bl",
47 "El", "It", "Ad", "An",
48 "Ar", "Cd", "Cm", "Dv",
49 "Er", "Ev", "Ex", "Fa",
50 "Fd", "Fl", "Fn", "Ft",
51 "Ic", "In", "Li", "Nd",
52 "Nm", "Op", "Ot", "Pa",
53 "Rv", "St", "Va", "Vt",
55 "Xr", "\%A", "\%B", "\%D",
57 "\%I", "\%J", "\%N", "\%O",
59 "\%P", "\%R", "\%T", "\%V",
60 "Ac", "Ao", "Aq", "At",
61 "Bc", "Bf", "Bo", "Bq",
62 "Bsx", "Bx", "Db", "Dc",
63 "Do", "Dq", "Ec", "Ef",
64 "Em", "Eo", "Fx", "Ms",
65 "No", "Ns", "Nx", "Ox",
66 "Pc", "Pf", "Po", "Pq",
67 "Qc", "Ql", "Qo", "Qq",
68 "Re", "Rs", "Sc", "So",
69 "Sq", "Sm", "Sx", "Sy",
70 "Tn", "Ux", "Xc", "Xo",
71 "Fo", "Fc", "Oo", "Oc",
72 "Bk", "Ek", "Bt", "Hf",
76 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
77 "split", "nosplit", "ragged",
78 "unfilled", "literal", "file",
79 "offset", "bullet", "dash",
80 "hyphen", "item", "enum",
81 "tag", "diag", "hang",
82 "ohang", "inset", "column",
83 "width", "compact", "std",
84 "filled", "words", "emphasis",
88 const char * const *mdoc_macronames
= __mdoc_macronames
;
89 const char * const *mdoc_argnames
= __mdoc_argnames
;
92 const struct mdoc_node
*
93 mdoc_node(const struct mdoc
*mdoc
)
100 const struct mdoc_meta
*
101 mdoc_meta(const struct mdoc
*mdoc
)
109 mdoc_free(struct mdoc
*mdoc
)
113 mdoc_node_freelist(mdoc
->first
);
115 mdoc_tokhash_free(mdoc
->htab
);
116 if (mdoc
->meta
.title
)
117 free(mdoc
->meta
.title
);
121 free(mdoc
->meta
.name
);
123 free(mdoc
->meta
.arch
);
125 free(mdoc
->meta
.vol
);
132 mdoc_alloc(void *data
, const struct mdoc_cb
*cb
)
136 p
= xcalloc(1, sizeof(struct mdoc
));
140 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
142 p
->last
= xcalloc(1, sizeof(struct mdoc_node
));
143 p
->last
->type
= MDOC_ROOT
;
146 p
->next
= MDOC_NEXT_CHILD
;
147 p
->htab
= mdoc_tokhash_alloc();
154 mdoc_endparse(struct mdoc
*mdoc
)
157 if (MDOC_HALT
& mdoc
->flags
)
159 if (NULL
== mdoc
->first
)
163 if ( ! macro_end(mdoc
)) {
164 mdoc
->flags
|= MDOC_HALT
;
172 * Main parse routine. Parses a single line -- really just hands off to
173 * the macro or text parser.
176 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
179 /* If in error-mode, then we parse no more. */
181 if (MDOC_HALT
& m
->flags
)
184 return('.' == *buf
? parsemacro(m
, ln
, buf
) :
185 parsetext(m
, ln
, buf
));
190 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
195 if (NULL
== mdoc
->cb
.mdoc_msg
)
199 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
201 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
206 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
207 const char *fmt
, ...)
212 if (NULL
== mdoc
->cb
.mdoc_err
)
216 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
218 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
223 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
224 enum mdoc_warn type
, const char *fmt
, ...)
229 if (NULL
== mdoc
->cb
.mdoc_warn
)
233 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
235 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
240 mdoc_macro(struct mdoc
*m
, int tok
,
241 int ln
, int pp
, int *pos
, char *buf
)
244 /* FIXME - these should happen during validation. */
246 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
247 SEC_PROLOGUE
!= m
->lastnamed
)
248 return(mdoc_perr(m
, ln
, pp
,
249 "disallowed in document body"));
251 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
252 SEC_PROLOGUE
== m
->lastnamed
)
253 return(mdoc_perr(m
, ln
, pp
,
254 "disallowed in prologue"));
256 if (1 != pp
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
257 return(mdoc_perr(m
, ln
, pp
, "not callable"));
259 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
264 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
269 assert(MDOC_ROOT
!= p
->type
);
271 switch (mdoc
->next
) {
272 case (MDOC_NEXT_SIBLING
):
273 mdoc
->last
->next
= p
;
274 p
->prev
= mdoc
->last
;
275 p
->parent
= mdoc
->last
->parent
;
277 case (MDOC_NEXT_CHILD
):
278 mdoc
->last
->child
= p
;
279 p
->parent
= mdoc
->last
;
286 if ( ! mdoc_valid_pre(mdoc
, p
))
291 assert(MDOC_BLOCK
== p
->parent
->type
);
295 assert(MDOC_BLOCK
== p
->parent
->type
);
299 assert(MDOC_BLOCK
== p
->parent
->type
);
311 static struct mdoc_node
*
312 mdoc_node_alloc(const struct mdoc
*mdoc
)
316 p
= xcalloc(1, sizeof(struct mdoc_node
));
317 p
->sec
= mdoc
->lastsec
;
324 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
331 p
= mdoc_node_alloc(mdoc
);
338 return(mdoc_node_append(mdoc
, p
));
343 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
350 p
= mdoc_node_alloc(mdoc
);
357 return(mdoc_node_append(mdoc
, p
));
362 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
369 p
= mdoc_node_alloc(mdoc
);
376 return(mdoc_node_append(mdoc
, p
));
381 mdoc_root_alloc(struct mdoc
*mdoc
)
385 p
= mdoc_node_alloc(mdoc
);
389 return(mdoc_node_append(mdoc
, p
));
394 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
395 int tok
, struct mdoc_arg
*args
)
399 p
= mdoc_node_alloc(mdoc
);
403 p
->type
= MDOC_BLOCK
;
410 return(mdoc_node_append(mdoc
, p
));
415 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
416 int tok
, struct mdoc_arg
*args
)
420 p
= mdoc_node_alloc(mdoc
);
431 return(mdoc_node_append(mdoc
, p
));
436 mdoc_word_alloc(struct mdoc
*mdoc
,
437 int line
, int pos
, const char *word
)
441 p
= mdoc_node_alloc(mdoc
);
446 p
->string
= xstrdup(word
);
448 return(mdoc_node_append(mdoc
, p
));
453 mdoc_node_free(struct mdoc_node
*p
)
459 mdoc_argv_free(p
->args
);
465 mdoc_node_freelist(struct mdoc_node
*p
)
469 mdoc_node_freelist(p
->child
);
471 mdoc_node_freelist(p
->next
);
478 * Parse free-form text, that is, a line that does not begin with the
482 parsetext(struct mdoc
*mdoc
, int line
, char *buf
)
485 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
486 return(mdoc_perr(mdoc
, line
, 0,
487 "text disallowed in prologue"));
489 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
492 mdoc
->next
= MDOC_NEXT_SIBLING
;
498 * Parse a macro line, that is, a line beginning with the control
502 parsemacro(struct mdoc
*m
, int ln
, char *buf
)
507 /* Comments are quickly ignored. */
509 if (buf
[1] && '\\' == buf
[1])
510 if (buf
[2] && '\"' == buf
[2])
513 /* Copy the first word into a nil-terminated buffer. */
515 for (i
= 1; i
< 5; i
++) {
516 if (0 == (mac
[i
- 1] = buf
[i
]))
518 else if (isspace((unsigned char)buf
[i
]))
522 /* FIXME: be able to skip unknown macro lines! */
526 if (i
== 5 || i
<= 2) {
527 (void)mdoc_perr(m
, ln
, 1, "unknown macro: %s%s",
528 mac
, i
== 5 ? "..." : "");
532 if (MDOC_MAX
== (c
= mdoc_tokhash_find(m
->htab
, mac
))) {
533 (void)mdoc_perr(m
, ln
, 1, "unknown macro: %s", mac
);
537 /* The macro is sane. Jump to the next word. */
539 while (buf
[i
] && isspace((unsigned char)buf
[i
]))
542 /* Begin recursive parse sequence. */
544 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
549 err
: /* Error out. */
551 m
->flags
|= MDOC_HALT
;