]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.48 2009/02/23 12:45:19 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 "p1003.1-88", "p1003.1-90", "p1003.1-96",
89 "p1003.1-2001", "p1003.1-2004", "p1003.1",
90 "p1003.1b", "p1003.1b-93", "p1003.1c-95",
91 "p1003.1g-2000", "p1003.2-92", "p1387.2-95",
92 "p1003.2", "p1387.2", "isoC-90",
93 "isoC-amd1", "isoC-tcor1", "isoC-tcor2",
94 "isoC-99", "ansiC", "ansiC-89",
95 "ansiC-99", "ieee754", "iso8802-3",
96 "xpg3", "xpg4", "xpg4.2",
97 "xpg4.3", "xbd5", "xcu5",
98 "xsh5", "xns5", "xns5.2d2.0",
99 "xcurses4.2", "susv2", "susv3",
100 "svid4", "filled", "words",
101 "emphasis", "symbolic",
104 const char * const *mdoc_macronames
= __mdoc_macronames
;
105 const char * const *mdoc_argnames
= __mdoc_argnames
;
108 const struct mdoc_node
*
109 mdoc_node(const struct mdoc
*mdoc
)
116 const struct mdoc_meta
*
117 mdoc_meta(const struct mdoc
*mdoc
)
125 mdoc_free(struct mdoc
*mdoc
)
129 mdoc_node_freelist(mdoc
->first
);
131 mdoc_tokhash_free(mdoc
->htab
);
132 if (mdoc
->meta
.title
)
133 free(mdoc
->meta
.title
);
137 free(mdoc
->meta
.name
);
144 mdoc_alloc(void *data
, const struct mdoc_cb
*cb
)
148 p
= xcalloc(1, sizeof(struct mdoc
));
152 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
154 p
->last
= xcalloc(1, sizeof(struct mdoc_node
));
155 p
->last
->type
= MDOC_ROOT
;
158 p
->next
= MDOC_NEXT_CHILD
;
159 p
->htab
= mdoc_tokhash_alloc();
166 mdoc_endparse(struct mdoc
*mdoc
)
169 if (MDOC_HALT
& mdoc
->flags
)
171 if (NULL
== mdoc
->first
)
175 if ( ! macro_end(mdoc
)) {
176 mdoc
->flags
|= MDOC_HALT
;
184 mdoc_parseln(struct mdoc
*mdoc
, int line
, char *buf
)
189 if (MDOC_HALT
& mdoc
->flags
)
195 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
196 return(mdoc_perr(mdoc
, line
, 0, "text disallowed in document prologue"));
197 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
199 mdoc
->next
= MDOC_NEXT_SIBLING
;
203 if (buf
[1] && '\\' == buf
[1])
204 if (buf
[2] && '\"' == buf
[2])
208 while (buf
[i
] && ! isspace((int)buf
[i
]) && i
< (int)sizeof(tmp
))
211 if (i
== (int)sizeof(tmp
)) {
212 mdoc
->flags
|= MDOC_HALT
;
213 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
215 mdoc
->flags
|= MDOC_HALT
;
216 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
221 (void)memcpy(tmp
, buf
+ 1, (size_t)i
);
224 if (MDOC_MAX
== (c
= mdoc_find(mdoc
, tmp
))) {
225 mdoc
->flags
|= MDOC_HALT
;
226 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
229 while (buf
[i
] && isspace((int)buf
[i
]))
232 if ( ! mdoc_macro(mdoc
, c
, line
, 1, &i
, buf
)) {
233 mdoc
->flags
|= MDOC_HALT
;
241 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
246 if (NULL
== mdoc
->cb
.mdoc_msg
)
250 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
252 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
257 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
258 const char *fmt
, ...)
263 if (NULL
== mdoc
->cb
.mdoc_err
)
267 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
269 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
274 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
275 enum mdoc_warn type
, const char *fmt
, ...)
280 if (NULL
== mdoc
->cb
.mdoc_warn
)
284 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
286 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
291 mdoc_macro(struct mdoc
*mdoc
, int tok
,
292 int ln
, int ppos
, int *pos
, char *buf
)
295 assert(mdoc_macros
[tok
].fp
);
297 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
298 SEC_PROLOGUE
!= mdoc
->lastnamed
)
299 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document body"));
300 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
301 SEC_PROLOGUE
== mdoc
->lastnamed
)
302 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document prologue"));
303 if (1 != ppos
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
304 return(mdoc_perr(mdoc
, ln
, ppos
, "macro not callable"));
305 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, ln
, ppos
, pos
, buf
));
310 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
312 const char *nn
, *nt
, *on
, *ot
, *act
;
316 assert(MDOC_ROOT
!= p
->type
);
318 /* See if we exceed the suggest line-max. */
332 /* This sort-of works (re-opening of text macros...). */
333 if (mdoc
->linetok
> MDOC_LINEARG_SOFTMAX
)
334 if ( ! mdoc_nwarn(mdoc
, p
, WARN_COMPAT
,
335 "suggested %d tokens per line exceeded (has %d)",
336 MDOC_LINEARG_SOFTMAX
, mdoc
->linetok
))
339 if (MDOC_TEXT
== mdoc
->last
->type
)
341 else if (MDOC_ROOT
== mdoc
->last
->type
)
344 on
= mdoc_macronames
[mdoc
->last
->tok
];
346 if (MDOC_TEXT
== p
->type
)
348 else if (MDOC_ROOT
== p
->type
)
351 nn
= mdoc_macronames
[p
->tok
];
353 ot
= mdoc_type2a(mdoc
->last
->type
);
354 nt
= mdoc_type2a(p
->type
);
356 switch (mdoc
->next
) {
357 case (MDOC_NEXT_SIBLING
):
358 mdoc
->last
->next
= p
;
359 p
->prev
= mdoc
->last
;
360 p
->parent
= mdoc
->last
->parent
;
363 case (MDOC_NEXT_CHILD
):
364 mdoc
->last
->child
= p
;
365 p
->parent
= mdoc
->last
;
373 if ( ! mdoc_valid_pre(mdoc
, p
))
378 assert(MDOC_BLOCK
== p
->parent
->type
);
379 p
->parent
->data
.block
.head
= p
;
382 assert(MDOC_BLOCK
== p
->parent
->type
);
383 p
->parent
->data
.block
.tail
= p
;
386 assert(MDOC_BLOCK
== p
->parent
->type
);
387 p
->parent
->data
.block
.body
= p
;
394 mdoc_msg(mdoc
, "parse: %s `%s' %s of %s `%s'",
395 nt
, nn
, act
, ot
, on
);
400 static struct mdoc_node
*
401 mdoc_node_alloc(const struct mdoc
*mdoc
)
405 p
= xcalloc(1, sizeof(struct mdoc_node
));
406 p
->sec
= mdoc
->lastsec
;
413 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
420 p
= mdoc_node_alloc(mdoc
);
427 return(mdoc_node_append(mdoc
, p
));
432 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
439 p
= mdoc_node_alloc(mdoc
);
446 return(mdoc_node_append(mdoc
, p
));
451 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
458 p
= mdoc_node_alloc(mdoc
);
465 return(mdoc_node_append(mdoc
, p
));
470 mdoc_root_alloc(struct mdoc
*mdoc
)
474 p
= mdoc_node_alloc(mdoc
);
478 return(mdoc_node_append(mdoc
, p
));
483 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
484 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
488 p
= mdoc_node_alloc(mdoc
);
492 p
->type
= MDOC_BLOCK
;
494 p
->data
.block
.argc
= argsz
;
495 p
->data
.block
.argv
= argdup(argsz
, args
);
497 return(mdoc_node_append(mdoc
, p
));
502 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
503 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
507 p
= mdoc_node_alloc(mdoc
);
513 p
->data
.elem
.argc
= argsz
;
514 p
->data
.elem
.argv
= argdup(argsz
, args
);
516 return(mdoc_node_append(mdoc
, p
));
521 mdoc_word_alloc(struct mdoc
*mdoc
,
522 int line
, int pos
, const char *word
)
526 p
= mdoc_node_alloc(mdoc
);
531 p
->data
.text
.string
= xstrdup(word
);
533 return(mdoc_node_append(mdoc
, p
));
538 argfree(size_t sz
, struct mdoc_arg
*p
)
547 for (i
= 0; i
< (int)sz
; i
++)
551 for (j
= 0; j
< (int)p
[i
].sz
; j
++)
560 mdoc_elem_free(struct mdoc_elem
*p
)
563 argfree(p
->argc
, p
->argv
);
568 mdoc_block_free(struct mdoc_block
*p
)
571 argfree(p
->argc
, p
->argv
);
576 mdoc_text_free(struct mdoc_text
*p
)
585 mdoc_node_free(struct mdoc_node
*p
)
590 mdoc_text_free(&p
->data
.text
);
593 mdoc_elem_free(&p
->data
.elem
);
596 mdoc_block_free(&p
->data
.block
);
607 mdoc_node_freelist(struct mdoc_node
*p
)
611 mdoc_node_freelist(p
->child
);
613 mdoc_node_freelist(p
->next
);
620 mdoc_find(const struct mdoc
*mdoc
, const char *key
)
623 return(mdoc_tokhash_find(mdoc
->htab
, key
));
628 argcpy(struct mdoc_arg
*dst
, const struct mdoc_arg
*src
)
632 dst
->line
= src
->line
;
635 if (0 == (dst
->sz
= src
->sz
))
637 dst
->value
= xcalloc(dst
->sz
, sizeof(char *));
638 for (i
= 0; i
< (int)dst
->sz
; i
++)
639 dst
->value
[i
] = xstrdup(src
->value
[i
]);
643 static struct mdoc_arg
*
644 argdup(size_t argsz
, const struct mdoc_arg
*args
)
652 pp
= xcalloc((size_t)argsz
, sizeof(struct mdoc_arg
));
653 for (i
= 0; i
< (int)argsz
; i
++)
654 argcpy(&pp
[i
], &args
[i
]);
660 /* FIXME: deprecate. */
662 mdoc_node2a(struct mdoc_node
*node
)
669 (void)xstrlcat(buf
, mdoc_type2a(node
->type
), 64);
670 if (MDOC_ROOT
== node
->type
)
672 (void)xstrlcat(buf
, " `", 64);
673 if (MDOC_TEXT
== node
->type
)
674 (void)xstrlcat(buf
, node
->data
.text
.string
, 64);
676 (void)xstrlcat(buf
, mdoc_macronames
[node
->tok
], 64);
677 (void)xstrlcat(buf
, "'", 64);