]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
48785716e0f9bbe69fd4ce8a89c21b8a556688b9
1 /* $Id: mdoc.c,v 1.52 2009/03/06 14:13:47 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_arg
*argdup(size_t, const struct mdoc_arg
*);
36 static void argfree(size_t, struct mdoc_arg
*);
37 static void argcpy(struct mdoc_arg
*,
38 const struct mdoc_arg
*);
40 static struct mdoc_node
*mdoc_node_alloc(const struct mdoc
*);
41 static int mdoc_node_append(struct mdoc
*,
43 static void mdoc_elem_free(struct mdoc_elem
*);
44 static void mdoc_text_free(struct mdoc_text
*);
47 const char *const __mdoc_macronames
[MDOC_MAX
] = {
48 "\\\"", "Dd", "Dt", "Os",
49 "Sh", "Ss", "Pp", "D1",
50 "Dl", "Bd", "Ed", "Bl",
51 "El", "It", "Ad", "An",
52 "Ar", "Cd", "Cm", "Dv",
53 "Er", "Ev", "Ex", "Fa",
54 "Fd", "Fl", "Fn", "Ft",
55 "Ic", "In", "Li", "Nd",
56 "Nm", "Op", "Ot", "Pa",
57 "Rv", "St", "Va", "Vt",
59 "Xr", "\%A", "\%B", "\%D",
61 "\%I", "\%J", "\%N", "\%O",
63 "\%P", "\%R", "\%T", "\%V",
64 "Ac", "Ao", "Aq", "At",
65 "Bc", "Bf", "Bo", "Bq",
66 "Bsx", "Bx", "Db", "Dc",
67 "Do", "Dq", "Ec", "Ef",
68 "Em", "Eo", "Fx", "Ms",
69 "No", "Ns", "Nx", "Ox",
70 "Pc", "Pf", "Po", "Pq",
71 "Qc", "Ql", "Qo", "Qq",
72 "Re", "Rs", "Sc", "So",
73 "Sq", "Sm", "Sx", "Sy",
74 "Tn", "Ux", "Xc", "Xo",
75 "Fo", "Fc", "Oo", "Oc",
76 "Bk", "Ek", "Bt", "Hf",
80 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
81 "split", "nosplit", "ragged",
82 "unfilled", "literal", "file",
83 "offset", "bullet", "dash",
84 "hyphen", "item", "enum",
85 "tag", "diag", "hang",
86 "ohang", "inset", "column",
87 "width", "compact", "std",
88 "filled", "words", "emphasis",
92 const char * const *mdoc_macronames
= __mdoc_macronames
;
93 const char * const *mdoc_argnames
= __mdoc_argnames
;
96 const struct mdoc_node
*
97 mdoc_node(const struct mdoc
*mdoc
)
104 const struct mdoc_meta
*
105 mdoc_meta(const struct mdoc
*mdoc
)
113 mdoc_free(struct mdoc
*mdoc
)
117 mdoc_node_freelist(mdoc
->first
);
119 mdoc_tokhash_free(mdoc
->htab
);
120 if (mdoc
->meta
.title
)
121 free(mdoc
->meta
.title
);
125 free(mdoc
->meta
.name
);
127 free(mdoc
->meta
.arch
);
129 free(mdoc
->meta
.vol
);
136 mdoc_alloc(void *data
, const struct mdoc_cb
*cb
)
140 p
= xcalloc(1, sizeof(struct mdoc
));
144 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
146 p
->last
= xcalloc(1, sizeof(struct mdoc_node
));
147 p
->last
->type
= MDOC_ROOT
;
150 p
->next
= MDOC_NEXT_CHILD
;
151 p
->htab
= mdoc_tokhash_alloc();
158 mdoc_endparse(struct mdoc
*mdoc
)
161 if (MDOC_HALT
& mdoc
->flags
)
163 if (NULL
== mdoc
->first
)
167 if ( ! macro_end(mdoc
)) {
168 mdoc
->flags
|= MDOC_HALT
;
176 * Main line-parsing routine. If the line is a macro-line (started with
177 * a '.' control character), then pass along to the parser, which parses
178 * subsequent macros until the end of line. If normal text, simply
179 * append the entire line to the chain.
182 mdoc_parseln(struct mdoc
*mdoc
, int line
, char *buf
)
187 if (MDOC_HALT
& mdoc
->flags
)
194 * Free-form text. Not allowed in the prologue.
196 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
197 return(mdoc_perr(mdoc
, line
, 0,
198 "no text in prologue"));
200 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
202 mdoc
->next
= MDOC_NEXT_SIBLING
;
207 * Control-character detected. Begin the parsing sequence.
210 if (buf
[1] && '\\' == buf
[1])
211 if (buf
[2] && '\"' == buf
[2])
215 while (buf
[i
] && ! isspace((u_char
)buf
[i
]) &&
216 i
< (int)sizeof(tmp
))
219 if (i
== (int)sizeof(tmp
)) {
220 mdoc
->flags
|= MDOC_HALT
;
221 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
223 mdoc
->flags
|= MDOC_HALT
;
224 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
229 (void)memcpy(tmp
, buf
+ 1, (size_t)i
);
232 if (MDOC_MAX
== (c
= mdoc_find(mdoc
, tmp
))) {
233 mdoc
->flags
|= MDOC_HALT
;
234 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
237 while (buf
[i
] && isspace((u_char
)buf
[i
]))
240 if ( ! mdoc_macro(mdoc
, c
, line
, 1, &i
, buf
)) {
241 mdoc
->flags
|= MDOC_HALT
;
250 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
255 if (NULL
== mdoc
->cb
.mdoc_msg
)
259 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
261 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
266 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
267 const char *fmt
, ...)
272 if (NULL
== mdoc
->cb
.mdoc_err
)
276 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
278 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
283 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
284 enum mdoc_warn type
, const char *fmt
, ...)
289 if (NULL
== mdoc
->cb
.mdoc_warn
)
293 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
295 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
300 mdoc_macro(struct mdoc
*mdoc
, int tok
,
301 int ln
, int ppos
, int *pos
, char *buf
)
304 assert(mdoc_macros
[tok
].fp
);
306 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
307 SEC_PROLOGUE
!= mdoc
->lastnamed
)
308 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document body"));
309 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
310 SEC_PROLOGUE
== mdoc
->lastnamed
)
311 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document prologue"));
312 if (1 != ppos
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
313 return(mdoc_perr(mdoc
, ln
, ppos
, "macro not callable"));
314 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, ln
, ppos
, pos
, buf
));
319 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
324 assert(MDOC_ROOT
!= p
->type
);
326 /* See if we exceed the suggest line-max. */
340 /* This sort-of works (re-opening of text macros...). */
341 if (mdoc
->linetok
> MDOC_LINEARG_SOFTMAX
)
342 if ( ! mdoc_nwarn(mdoc
, p
, WARN_COMPAT
,
343 "suggested %d tokens per line exceeded (has %d)",
344 MDOC_LINEARG_SOFTMAX
, mdoc
->linetok
))
347 switch (mdoc
->next
) {
348 case (MDOC_NEXT_SIBLING
):
349 mdoc
->last
->next
= p
;
350 p
->prev
= mdoc
->last
;
351 p
->parent
= mdoc
->last
->parent
;
353 case (MDOC_NEXT_CHILD
):
354 mdoc
->last
->child
= p
;
355 p
->parent
= mdoc
->last
;
362 if ( ! mdoc_valid_pre(mdoc
, p
))
367 assert(MDOC_BLOCK
== p
->parent
->type
);
368 p
->parent
->data
.block
.head
= p
;
371 assert(MDOC_BLOCK
== p
->parent
->type
);
372 p
->parent
->data
.block
.tail
= p
;
375 assert(MDOC_BLOCK
== p
->parent
->type
);
376 p
->parent
->data
.block
.body
= p
;
387 static struct mdoc_node
*
388 mdoc_node_alloc(const struct mdoc
*mdoc
)
392 p
= xcalloc(1, sizeof(struct mdoc_node
));
393 p
->sec
= mdoc
->lastsec
;
400 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
407 p
= mdoc_node_alloc(mdoc
);
414 return(mdoc_node_append(mdoc
, p
));
419 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
426 p
= mdoc_node_alloc(mdoc
);
433 return(mdoc_node_append(mdoc
, p
));
438 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
445 p
= mdoc_node_alloc(mdoc
);
452 return(mdoc_node_append(mdoc
, p
));
457 mdoc_root_alloc(struct mdoc
*mdoc
)
461 p
= mdoc_node_alloc(mdoc
);
465 return(mdoc_node_append(mdoc
, p
));
470 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
471 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
475 p
= mdoc_node_alloc(mdoc
);
479 p
->type
= MDOC_BLOCK
;
481 p
->data
.block
.argc
= argsz
;
482 p
->data
.block
.argv
= argdup(argsz
, args
);
484 return(mdoc_node_append(mdoc
, p
));
489 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
490 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
494 p
= mdoc_node_alloc(mdoc
);
500 p
->data
.elem
.argc
= argsz
;
501 p
->data
.elem
.argv
= argdup(argsz
, args
);
503 return(mdoc_node_append(mdoc
, p
));
508 mdoc_word_alloc(struct mdoc
*mdoc
,
509 int line
, int pos
, const char *word
)
513 p
= mdoc_node_alloc(mdoc
);
518 p
->data
.text
.string
= xstrdup(word
);
520 return(mdoc_node_append(mdoc
, p
));
525 argfree(size_t sz
, struct mdoc_arg
*p
)
534 for (i
= 0; i
< (int)sz
; i
++)
538 for (j
= 0; j
< (int)p
[i
].sz
; j
++)
547 mdoc_elem_free(struct mdoc_elem
*p
)
550 argfree(p
->argc
, p
->argv
);
555 mdoc_block_free(struct mdoc_block
*p
)
558 argfree(p
->argc
, p
->argv
);
563 mdoc_text_free(struct mdoc_text
*p
)
572 mdoc_node_free(struct mdoc_node
*p
)
577 mdoc_text_free(&p
->data
.text
);
580 mdoc_elem_free(&p
->data
.elem
);
583 mdoc_block_free(&p
->data
.block
);
594 mdoc_node_freelist(struct mdoc_node
*p
)
598 mdoc_node_freelist(p
->child
);
600 mdoc_node_freelist(p
->next
);
607 mdoc_find(const struct mdoc
*mdoc
, const char *key
)
610 return(mdoc_tokhash_find(mdoc
->htab
, key
));
615 argcpy(struct mdoc_arg
*dst
, const struct mdoc_arg
*src
)
619 dst
->line
= src
->line
;
622 if (0 == (dst
->sz
= src
->sz
))
624 dst
->value
= xcalloc(dst
->sz
, sizeof(char *));
625 for (i
= 0; i
< (int)dst
->sz
; i
++)
626 dst
->value
[i
] = xstrdup(src
->value
[i
]);
630 static struct mdoc_arg
*
631 argdup(size_t argsz
, const struct mdoc_arg
*args
)
639 pp
= xcalloc((size_t)argsz
, sizeof(struct mdoc_arg
));
640 for (i
= 0; i
< (int)argsz
; i
++)
641 argcpy(&pp
[i
], &args
[i
]);