]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
7525bc4107b1fea2da23e866471745fbbb5af9c1
1 /* $Id: mdoc.c,v 1.57 2009/03/08 20:57:35 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",
73 "Fr", "Ud", "Lb", "Ap",
77 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
78 "split", "nosplit", "ragged",
79 "unfilled", "literal", "file",
80 "offset", "bullet", "dash",
81 "hyphen", "item", "enum",
82 "tag", "diag", "hang",
83 "ohang", "inset", "column",
84 "width", "compact", "std",
85 "filled", "words", "emphasis",
89 const char * const *mdoc_macronames
= __mdoc_macronames
;
90 const char * const *mdoc_argnames
= __mdoc_argnames
;
93 const struct mdoc_node
*
94 mdoc_node(const struct mdoc
*mdoc
)
101 const struct mdoc_meta
*
102 mdoc_meta(const struct mdoc
*mdoc
)
110 mdoc_free(struct mdoc
*mdoc
)
114 mdoc_node_freelist(mdoc
->first
);
116 mdoc_tokhash_free(mdoc
->htab
);
117 if (mdoc
->meta
.title
)
118 free(mdoc
->meta
.title
);
122 free(mdoc
->meta
.name
);
124 free(mdoc
->meta
.arch
);
126 free(mdoc
->meta
.vol
);
133 mdoc_alloc(void *data
, int pflags
, const struct mdoc_cb
*cb
)
137 p
= xcalloc(1, sizeof(struct mdoc
));
141 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
143 p
->last
= xcalloc(1, sizeof(struct mdoc_node
));
144 p
->last
->type
= MDOC_ROOT
;
147 p
->next
= MDOC_NEXT_CHILD
;
148 p
->htab
= mdoc_tokhash_alloc();
155 mdoc_endparse(struct mdoc
*mdoc
)
158 if (MDOC_HALT
& mdoc
->flags
)
160 if (NULL
== mdoc
->first
)
164 if ( ! macro_end(mdoc
)) {
165 mdoc
->flags
|= MDOC_HALT
;
173 * Main parse routine. Parses a single line -- really just hands off to
174 * the macro or text parser.
177 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
180 /* If in error-mode, then we parse no more. */
182 if (MDOC_HALT
& m
->flags
)
185 return('.' == *buf
? parsemacro(m
, ln
, buf
) :
186 parsetext(m
, ln
, buf
));
191 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
196 if (NULL
== mdoc
->cb
.mdoc_msg
)
200 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
202 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
207 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
208 const char *fmt
, ...)
213 if (NULL
== mdoc
->cb
.mdoc_err
)
217 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
219 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
224 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
225 enum mdoc_warn type
, const char *fmt
, ...)
230 if (NULL
== mdoc
->cb
.mdoc_warn
)
234 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
236 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
241 mdoc_macro(struct mdoc
*m
, int tok
,
242 int ln
, int pp
, int *pos
, char *buf
)
245 /* FIXME - these should happen during validation. */
247 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
248 SEC_PROLOGUE
!= m
->lastnamed
)
249 return(mdoc_perr(m
, ln
, pp
,
250 "disallowed in document body"));
252 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
253 SEC_PROLOGUE
== m
->lastnamed
)
254 return(mdoc_perr(m
, ln
, pp
,
255 "disallowed in prologue"));
257 if (1 != pp
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
258 return(mdoc_perr(m
, ln
, pp
, "not callable"));
260 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
265 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
270 assert(MDOC_ROOT
!= p
->type
);
272 switch (mdoc
->next
) {
273 case (MDOC_NEXT_SIBLING
):
274 mdoc
->last
->next
= p
;
275 p
->prev
= mdoc
->last
;
276 p
->parent
= mdoc
->last
->parent
;
278 case (MDOC_NEXT_CHILD
):
279 mdoc
->last
->child
= p
;
280 p
->parent
= mdoc
->last
;
287 if ( ! mdoc_valid_pre(mdoc
, p
))
292 assert(MDOC_BLOCK
== p
->parent
->type
);
296 assert(MDOC_BLOCK
== p
->parent
->type
);
300 assert(MDOC_BLOCK
== p
->parent
->type
);
312 static struct mdoc_node
*
313 mdoc_node_alloc(const struct mdoc
*mdoc
)
317 p
= xcalloc(1, sizeof(struct mdoc_node
));
318 p
->sec
= mdoc
->lastsec
;
325 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
332 p
= mdoc_node_alloc(mdoc
);
339 return(mdoc_node_append(mdoc
, p
));
344 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
351 p
= mdoc_node_alloc(mdoc
);
358 return(mdoc_node_append(mdoc
, p
));
363 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
370 p
= mdoc_node_alloc(mdoc
);
377 return(mdoc_node_append(mdoc
, p
));
382 mdoc_root_alloc(struct mdoc
*mdoc
)
386 p
= mdoc_node_alloc(mdoc
);
390 return(mdoc_node_append(mdoc
, p
));
395 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
396 int tok
, struct mdoc_arg
*args
)
400 p
= mdoc_node_alloc(mdoc
);
404 p
->type
= MDOC_BLOCK
;
411 return(mdoc_node_append(mdoc
, p
));
416 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
417 int tok
, struct mdoc_arg
*args
)
421 p
= mdoc_node_alloc(mdoc
);
432 return(mdoc_node_append(mdoc
, p
));
437 mdoc_word_alloc(struct mdoc
*mdoc
,
438 int line
, int pos
, const char *word
)
442 p
= mdoc_node_alloc(mdoc
);
447 p
->string
= xstrdup(word
);
449 return(mdoc_node_append(mdoc
, p
));
454 mdoc_node_free(struct mdoc_node
*p
)
460 mdoc_argv_free(p
->args
);
466 mdoc_node_freelist(struct mdoc_node
*p
)
470 mdoc_node_freelist(p
->child
);
472 mdoc_node_freelist(p
->next
);
479 * Parse free-form text, that is, a line that does not begin with the
483 parsetext(struct mdoc
*mdoc
, int line
, char *buf
)
486 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
487 return(mdoc_perr(mdoc
, line
, 0,
488 "text disallowed in prologue"));
490 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
493 mdoc
->next
= MDOC_NEXT_SIBLING
;
499 * Parse a macro line, that is, a line beginning with the control
503 parsemacro(struct mdoc
*m
, int ln
, char *buf
)
508 /* Comments are quickly ignored. */
510 if (buf
[1] && '\\' == buf
[1])
511 if (buf
[2] && '\"' == buf
[2])
514 /* Copy the first word into a nil-terminated buffer. */
516 for (i
= 1; i
< 5; i
++) {
517 if (0 == (mac
[i
- 1] = buf
[i
]))
519 else if (isspace((unsigned char)buf
[i
]))
523 /* FIXME: be able to skip unknown macro lines! */
527 if (i
== 5 || i
<= 2) {
528 (void)mdoc_perr(m
, ln
, 1, "unknown macro: %s%s",
529 mac
, i
== 5 ? "..." : "");
533 if (MDOC_MAX
== (c
= mdoc_tokhash_find(m
->htab
, mac
))) {
534 (void)mdoc_perr(m
, ln
, 1, "unknown macro: %s", mac
);
538 /* The macro is sane. Jump to the next word. */
540 while (buf
[i
] && isspace((unsigned char)buf
[i
]))
543 /* Begin recursive parse sequence. */
545 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
550 err
: /* Error out. */
552 m
->flags
|= MDOC_HALT
;