]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.61 2009/03/11 00:39:58 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 *);
41 static int macrowarn(struct mdoc
*, int, const char *);
44 const char *const __mdoc_macronames
[MDOC_MAX
] = {
45 "\\\"", "Dd", "Dt", "Os",
46 "Sh", "Ss", "Pp", "D1",
47 "Dl", "Bd", "Ed", "Bl",
48 "El", "It", "Ad", "An",
49 "Ar", "Cd", "Cm", "Dv",
50 "Er", "Ev", "Ex", "Fa",
51 "Fd", "Fl", "Fn", "Ft",
52 "Ic", "In", "Li", "Nd",
53 "Nm", "Op", "Ot", "Pa",
54 "Rv", "St", "Va", "Vt",
56 "Xr", "\%A", "\%B", "\%D",
58 "\%I", "\%J", "\%N", "\%O",
60 "\%P", "\%R", "\%T", "\%V",
61 "Ac", "Ao", "Aq", "At",
62 "Bc", "Bf", "Bo", "Bq",
63 "Bsx", "Bx", "Db", "Dc",
64 "Do", "Dq", "Ec", "Ef",
65 "Em", "Eo", "Fx", "Ms",
66 "No", "Ns", "Nx", "Ox",
67 "Pc", "Pf", "Po", "Pq",
68 "Qc", "Ql", "Qo", "Qq",
69 "Re", "Rs", "Sc", "So",
70 "Sq", "Sm", "Sx", "Sy",
71 "Tn", "Ux", "Xc", "Xo",
72 "Fo", "Fc", "Oo", "Oc",
73 "Bk", "Ek", "Bt", "Hf",
74 "Fr", "Ud", "Lb", "Ap",
75 "Lp", "Lk", "Mt", "Brq",
79 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
80 "split", "nosplit", "ragged",
81 "unfilled", "literal", "file",
82 "offset", "bullet", "dash",
83 "hyphen", "item", "enum",
84 "tag", "diag", "hang",
85 "ohang", "inset", "column",
86 "width", "compact", "std",
87 "filled", "words", "emphasis",
91 const char * const *mdoc_macronames
= __mdoc_macronames
;
92 const char * const *mdoc_argnames
= __mdoc_argnames
;
95 const struct mdoc_node
*
96 mdoc_node(const struct mdoc
*mdoc
)
103 const struct mdoc_meta
*
104 mdoc_meta(const struct mdoc
*mdoc
)
112 mdoc_free(struct mdoc
*mdoc
)
116 mdoc_node_freelist(mdoc
->first
);
118 mdoc_tokhash_free(mdoc
->htab
);
119 if (mdoc
->meta
.title
)
120 free(mdoc
->meta
.title
);
124 free(mdoc
->meta
.name
);
126 free(mdoc
->meta
.arch
);
128 free(mdoc
->meta
.vol
);
135 mdoc_alloc(void *data
, int pflags
, const struct mdoc_cb
*cb
)
139 p
= xcalloc(1, sizeof(struct mdoc
));
143 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
145 p
->last
= xcalloc(1, sizeof(struct mdoc_node
));
146 p
->last
->type
= MDOC_ROOT
;
149 p
->next
= MDOC_NEXT_CHILD
;
150 p
->htab
= mdoc_tokhash_alloc();
157 mdoc_endparse(struct mdoc
*mdoc
)
160 if (MDOC_HALT
& mdoc
->flags
)
162 if (NULL
== mdoc
->first
)
166 if ( ! macro_end(mdoc
)) {
167 mdoc
->flags
|= MDOC_HALT
;
175 * Main parse routine. Parses a single line -- really just hands off to
176 * the macro or text parser.
179 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
182 /* If in error-mode, then we parse no more. */
184 if (MDOC_HALT
& m
->flags
)
187 return('.' == *buf
? parsemacro(m
, ln
, buf
) :
188 parsetext(m
, ln
, buf
));
193 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
198 if (NULL
== mdoc
->cb
.mdoc_msg
)
202 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
204 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
209 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
210 const char *fmt
, ...)
215 if (NULL
== mdoc
->cb
.mdoc_err
)
219 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
221 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
226 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
227 enum mdoc_warn type
, const char *fmt
, ...)
232 if (NULL
== mdoc
->cb
.mdoc_warn
)
236 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
238 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
243 mdoc_macro(struct mdoc
*m
, int tok
,
244 int ln
, int pp
, int *pos
, char *buf
)
247 /* FIXME - these should happen during validation. */
249 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
250 SEC_PROLOGUE
!= m
->lastnamed
)
251 return(mdoc_perr(m
, ln
, pp
,
252 "disallowed in document body"));
254 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
255 SEC_PROLOGUE
== m
->lastnamed
)
256 return(mdoc_perr(m
, ln
, pp
,
257 "disallowed in prologue"));
259 if (1 != pp
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
260 return(mdoc_perr(m
, ln
, pp
, "not callable"));
262 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
267 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
272 assert(MDOC_ROOT
!= p
->type
);
274 switch (mdoc
->next
) {
275 case (MDOC_NEXT_SIBLING
):
276 mdoc
->last
->next
= p
;
277 p
->prev
= mdoc
->last
;
278 p
->parent
= mdoc
->last
->parent
;
280 case (MDOC_NEXT_CHILD
):
281 mdoc
->last
->child
= p
;
282 p
->parent
= mdoc
->last
;
289 if ( ! mdoc_valid_pre(mdoc
, p
))
294 assert(MDOC_BLOCK
== p
->parent
->type
);
298 assert(MDOC_BLOCK
== p
->parent
->type
);
302 assert(MDOC_BLOCK
== p
->parent
->type
);
314 static struct mdoc_node
*
315 mdoc_node_alloc(const struct mdoc
*mdoc
)
319 p
= xcalloc(1, sizeof(struct mdoc_node
));
320 p
->sec
= mdoc
->lastsec
;
327 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
334 p
= mdoc_node_alloc(mdoc
);
341 return(mdoc_node_append(mdoc
, p
));
346 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
353 p
= mdoc_node_alloc(mdoc
);
360 return(mdoc_node_append(mdoc
, p
));
365 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
372 p
= mdoc_node_alloc(mdoc
);
379 return(mdoc_node_append(mdoc
, p
));
384 mdoc_root_alloc(struct mdoc
*mdoc
)
388 p
= mdoc_node_alloc(mdoc
);
392 return(mdoc_node_append(mdoc
, p
));
397 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
398 int tok
, struct mdoc_arg
*args
)
402 p
= mdoc_node_alloc(mdoc
);
406 p
->type
= MDOC_BLOCK
;
413 return(mdoc_node_append(mdoc
, p
));
418 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
419 int tok
, struct mdoc_arg
*args
)
423 p
= mdoc_node_alloc(mdoc
);
434 return(mdoc_node_append(mdoc
, p
));
439 mdoc_word_alloc(struct mdoc
*mdoc
,
440 int line
, int pos
, const char *word
)
444 p
= mdoc_node_alloc(mdoc
);
449 p
->string
= xstrdup(word
);
451 return(mdoc_node_append(mdoc
, p
));
456 mdoc_node_free(struct mdoc_node
*p
)
462 mdoc_argv_free(p
->args
);
468 mdoc_node_freelist(struct mdoc_node
*p
)
472 mdoc_node_freelist(p
->child
);
474 mdoc_node_freelist(p
->next
);
481 * Parse free-form text, that is, a line that does not begin with the
485 parsetext(struct mdoc
*mdoc
, int line
, char *buf
)
488 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
489 return(mdoc_perr(mdoc
, line
, 0,
490 "text disallowed in prologue"));
492 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
495 mdoc
->next
= MDOC_NEXT_SIBLING
;
501 macrowarn(struct mdoc
*m
, int ln
, const char *buf
)
503 if ( ! (MDOC_IGN_MACRO
& m
->pflags
))
504 return(mdoc_perr(m
, ln
, 1, "unknown macro: %s%s",
505 buf
, strlen(buf
) > 3 ? "..." : ""));
506 return(mdoc_pwarn(m
, ln
, 1, WARN_SYNTAX
,
507 "unknown macro: %s%s",
508 buf
, strlen(buf
) > 3 ? "..." : ""));
514 * Parse a macro line, that is, a line beginning with the control
518 parsemacro(struct mdoc
*m
, int ln
, char *buf
)
523 /* Comments are quickly ignored. */
525 if (buf
[1] && '\\' == buf
[1])
526 if (buf
[2] && '\"' == buf
[2])
529 /* Copy the first word into a nil-terminated buffer. */
531 for (i
= 1; i
< 5; i
++) {
532 if (0 == (mac
[i
- 1] = buf
[i
]))
534 else if (isspace((unsigned char)buf
[i
]))
538 /* FIXME: be able to skip unknown macro lines! */
542 if (i
== 5 || i
<= 2) {
543 if ( ! macrowarn(m
, ln
, mac
))
548 if (MDOC_MAX
== (c
= mdoc_tokhash_find(m
->htab
, mac
))) {
549 if ( ! macrowarn(m
, ln
, mac
))
554 /* The macro is sane. Jump to the next word. */
556 while (buf
[i
] && isspace((unsigned char)buf
[i
]))
559 /* Begin recursive parse sequence. */
561 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
566 err
: /* Error out. */
568 m
->flags
|= MDOC_HALT
;