]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
af8a7fcce8802cbf3d24cc12ff02881db4c0b06d
1 /* $Id: mdoc.c,v 1.49 2009/03/01 23:14:15 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 * FIXME: should puke on whitespace in non-literal displays.
199 if (SEC_PROLOGUE
== mdoc
->lastnamed
)
200 return(mdoc_perr(mdoc
, line
, 0,
201 "no text in document prologue"));
202 if ( ! mdoc_word_alloc(mdoc
, line
, 0, buf
))
204 mdoc
->next
= MDOC_NEXT_SIBLING
;
208 if (buf
[1] && '\\' == buf
[1])
209 if (buf
[2] && '\"' == buf
[2])
213 while (buf
[i
] && ! isspace((int)buf
[i
]) && i
< (int)sizeof(tmp
))
216 if (i
== (int)sizeof(tmp
)) {
217 mdoc
->flags
|= MDOC_HALT
;
218 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
220 mdoc
->flags
|= MDOC_HALT
;
221 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
226 (void)memcpy(tmp
, buf
+ 1, (size_t)i
);
229 if (MDOC_MAX
== (c
= mdoc_find(mdoc
, tmp
))) {
230 mdoc
->flags
|= MDOC_HALT
;
231 return(mdoc_perr(mdoc
, line
, 1, "unknown macro"));
234 while (buf
[i
] && isspace((int)buf
[i
]))
237 if ( ! mdoc_macro(mdoc
, c
, line
, 1, &i
, buf
)) {
238 mdoc
->flags
|= MDOC_HALT
;
246 mdoc_vmsg(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
251 if (NULL
== mdoc
->cb
.mdoc_msg
)
255 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
257 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, ln
, pos
, buf
);
262 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
263 const char *fmt
, ...)
268 if (NULL
== mdoc
->cb
.mdoc_err
)
272 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
274 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
279 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
,
280 enum mdoc_warn type
, const char *fmt
, ...)
285 if (NULL
== mdoc
->cb
.mdoc_warn
)
289 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
291 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, type
, buf
));
296 mdoc_macro(struct mdoc
*mdoc
, int tok
,
297 int ln
, int ppos
, int *pos
, char *buf
)
300 assert(mdoc_macros
[tok
].fp
);
302 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
303 SEC_PROLOGUE
!= mdoc
->lastnamed
)
304 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document body"));
305 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
306 SEC_PROLOGUE
== mdoc
->lastnamed
)
307 return(mdoc_perr(mdoc
, ln
, ppos
, "macro disallowed in document prologue"));
308 if (1 != ppos
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
309 return(mdoc_perr(mdoc
, ln
, ppos
, "macro not callable"));
310 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, ln
, ppos
, pos
, buf
));
315 mdoc_node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
317 const char *nn
, *nt
, *on
, *ot
, *act
;
321 assert(MDOC_ROOT
!= p
->type
);
323 /* See if we exceed the suggest line-max. */
337 /* This sort-of works (re-opening of text macros...). */
338 if (mdoc
->linetok
> MDOC_LINEARG_SOFTMAX
)
339 if ( ! mdoc_nwarn(mdoc
, p
, WARN_COMPAT
,
340 "suggested %d tokens per line exceeded (has %d)",
341 MDOC_LINEARG_SOFTMAX
, mdoc
->linetok
))
344 if (MDOC_TEXT
== mdoc
->last
->type
)
346 else if (MDOC_ROOT
== mdoc
->last
->type
)
349 on
= mdoc_macronames
[mdoc
->last
->tok
];
351 if (MDOC_TEXT
== p
->type
)
353 else if (MDOC_ROOT
== p
->type
)
356 nn
= mdoc_macronames
[p
->tok
];
358 ot
= mdoc_type2a(mdoc
->last
->type
);
359 nt
= mdoc_type2a(p
->type
);
361 switch (mdoc
->next
) {
362 case (MDOC_NEXT_SIBLING
):
363 mdoc
->last
->next
= p
;
364 p
->prev
= mdoc
->last
;
365 p
->parent
= mdoc
->last
->parent
;
368 case (MDOC_NEXT_CHILD
):
369 mdoc
->last
->child
= p
;
370 p
->parent
= mdoc
->last
;
378 if ( ! mdoc_valid_pre(mdoc
, p
))
383 assert(MDOC_BLOCK
== p
->parent
->type
);
384 p
->parent
->data
.block
.head
= p
;
387 assert(MDOC_BLOCK
== p
->parent
->type
);
388 p
->parent
->data
.block
.tail
= p
;
391 assert(MDOC_BLOCK
== p
->parent
->type
);
392 p
->parent
->data
.block
.body
= p
;
399 mdoc_msg(mdoc
, "parse: %s `%s' %s of %s `%s'",
400 nt
, nn
, act
, ot
, on
);
405 static struct mdoc_node
*
406 mdoc_node_alloc(const struct mdoc
*mdoc
)
410 p
= xcalloc(1, sizeof(struct mdoc_node
));
411 p
->sec
= mdoc
->lastsec
;
418 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
425 p
= mdoc_node_alloc(mdoc
);
432 return(mdoc_node_append(mdoc
, p
));
437 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
444 p
= mdoc_node_alloc(mdoc
);
451 return(mdoc_node_append(mdoc
, p
));
456 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
463 p
= mdoc_node_alloc(mdoc
);
470 return(mdoc_node_append(mdoc
, p
));
475 mdoc_root_alloc(struct mdoc
*mdoc
)
479 p
= mdoc_node_alloc(mdoc
);
483 return(mdoc_node_append(mdoc
, p
));
488 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
489 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
493 p
= mdoc_node_alloc(mdoc
);
497 p
->type
= MDOC_BLOCK
;
499 p
->data
.block
.argc
= argsz
;
500 p
->data
.block
.argv
= argdup(argsz
, args
);
502 return(mdoc_node_append(mdoc
, p
));
507 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
508 int tok
, size_t argsz
, const struct mdoc_arg
*args
)
512 p
= mdoc_node_alloc(mdoc
);
518 p
->data
.elem
.argc
= argsz
;
519 p
->data
.elem
.argv
= argdup(argsz
, args
);
521 return(mdoc_node_append(mdoc
, p
));
526 mdoc_word_alloc(struct mdoc
*mdoc
,
527 int line
, int pos
, const char *word
)
531 p
= mdoc_node_alloc(mdoc
);
536 p
->data
.text
.string
= xstrdup(word
);
538 return(mdoc_node_append(mdoc
, p
));
543 argfree(size_t sz
, struct mdoc_arg
*p
)
552 for (i
= 0; i
< (int)sz
; i
++)
556 for (j
= 0; j
< (int)p
[i
].sz
; j
++)
565 mdoc_elem_free(struct mdoc_elem
*p
)
568 argfree(p
->argc
, p
->argv
);
573 mdoc_block_free(struct mdoc_block
*p
)
576 argfree(p
->argc
, p
->argv
);
581 mdoc_text_free(struct mdoc_text
*p
)
590 mdoc_node_free(struct mdoc_node
*p
)
595 mdoc_text_free(&p
->data
.text
);
598 mdoc_elem_free(&p
->data
.elem
);
601 mdoc_block_free(&p
->data
.block
);
612 mdoc_node_freelist(struct mdoc_node
*p
)
616 mdoc_node_freelist(p
->child
);
618 mdoc_node_freelist(p
->next
);
625 mdoc_find(const struct mdoc
*mdoc
, const char *key
)
628 return(mdoc_tokhash_find(mdoc
->htab
, key
));
633 argcpy(struct mdoc_arg
*dst
, const struct mdoc_arg
*src
)
637 dst
->line
= src
->line
;
640 if (0 == (dst
->sz
= src
->sz
))
642 dst
->value
= xcalloc(dst
->sz
, sizeof(char *));
643 for (i
= 0; i
< (int)dst
->sz
; i
++)
644 dst
->value
[i
] = xstrdup(src
->value
[i
]);
648 static struct mdoc_arg
*
649 argdup(size_t argsz
, const struct mdoc_arg
*args
)
657 pp
= xcalloc((size_t)argsz
, sizeof(struct mdoc_arg
));
658 for (i
= 0; i
< (int)argsz
; i
++)
659 argcpy(&pp
[i
], &args
[i
]);
665 /* FIXME: deprecate. */
667 mdoc_node2a(struct mdoc_node
*node
)
674 (void)xstrlcat(buf
, mdoc_type2a(node
->type
), 64);
675 if (MDOC_ROOT
== node
->type
)
677 (void)xstrlcat(buf
, " `", 64);
678 if (MDOC_TEXT
== node
->type
)
679 (void)xstrlcat(buf
, node
->data
.text
.string
, 64);
681 (void)xstrlcat(buf
, mdoc_macronames
[node
->tok
], 64);
682 (void)xstrlcat(buf
, "'", 64);