]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.97 2009/07/29 08:46:06 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 const char *const __mdoc_merrnames
[MERRMAX
] = {
27 "trailing whitespace", /* ETAILWS */
28 "unexpected quoted parameter", /* EQUOTPARM */
29 "unterminated quoted parameter", /* EQUOTTERM */
30 "system: malloc error", /* EMALLOC */
31 "argument parameter suggested", /* EARGVAL */
32 "macro disallowed in prologue", /* EBODYPROL */
33 "macro disallowed in body", /* EPROLBODY */
34 "text disallowed in prologue", /* ETEXTPROL */
35 "blank line disallowed", /* ENOBLANK */
36 "text parameter too long", /* ETOOLONG */
37 "invalid escape sequence", /* EESCAPE */
38 "invalid character", /* EPRINT */
39 "document has no body", /* ENODAT */
40 "document has no prologue", /* ENOPROLOGUE */
41 "expected line arguments", /* ELINE */
42 "invalid AT&T argument", /* EATT */
43 "default name not yet set", /* ENAME */
44 "missing list type", /* ELISTTYPE */
45 "missing display type", /* EDISPTYPE */
46 "too many display types", /* EMULTIDISP */
47 "too many list types", /* EMULTILIST */
48 "NAME section must be first", /* ESECNAME */
49 "badly-formed NAME section", /* ENAMESECINC */
50 "argument repeated", /* EARGREP */
51 "expected boolean parameter", /* EBOOL */
52 "inconsistent column syntax", /* ECOLMIS */
53 "nested display invalid", /* ENESTDISP */
54 "width argument missing", /* EMISSWIDTH */
55 "invalid section for this manual section", /* EWRONGMSEC */
56 "section out of conventional order", /* ESECOOO */
57 "section repeated", /* ESECREP */
58 "invalid standard argument", /* EBADSTAND */
59 "multi-line arguments discouraged", /* ENOMULTILINE */
60 "multi-line arguments suggested", /* EMULTILINE */
61 "line arguments discouraged", /* ENOLINE */
62 "prologue macro out of conventional order", /* EPROLOOO */
63 "prologue macro repeated", /* EPROLREP */
64 "invalid manual section", /* EBADMSEC */
65 "invalid section", /* EBADSEC */
66 "invalid font mode", /* EFONT */
67 "invalid date syntax", /* EBADDATE */
68 "invalid number format", /* ENUMFMT */
69 "superfluous width argument", /* ENOWIDTH */
70 "system: utsname error", /* EUTSNAME */
71 "obsolete macro", /* EOBS */
72 "end-of-line scope violation", /* EIMPBRK */
73 "empty macro ignored", /* EIGNE */
74 "unclosed explicit scope", /* EOPEN */
75 "unterminated quoted phrase", /* EQUOTPHR */
76 "closure macro without prior context", /* ENOCTX */
77 "invalid whitespace after control character", /* ESPACE */
78 "no description found for library" /* ELIB */
81 const char *const __mdoc_macronames
[MDOC_MAX
] = {
82 "Ap", "Dd", "Dt", "Os",
83 "Sh", "Ss", "Pp", "D1",
84 "Dl", "Bd", "Ed", "Bl",
85 "El", "It", "Ad", "An",
86 "Ar", "Cd", "Cm", "Dv",
87 "Er", "Ev", "Ex", "Fa",
88 "Fd", "Fl", "Fn", "Ft",
89 "Ic", "In", "Li", "Nd",
90 "Nm", "Op", "Ot", "Pa",
91 "Rv", "St", "Va", "Vt",
93 "Xr", "\%A", "\%B", "\%D",
95 "\%I", "\%J", "\%N", "\%O",
97 "\%P", "\%R", "\%T", "\%V",
98 "Ac", "Ao", "Aq", "At",
99 "Bc", "Bf", "Bo", "Bq",
100 "Bsx", "Bx", "Db", "Dc",
101 "Do", "Dq", "Ec", "Ef",
102 "Em", "Eo", "Fx", "Ms",
103 "No", "Ns", "Nx", "Ox",
104 "Pc", "Pf", "Po", "Pq",
105 "Qc", "Ql", "Qo", "Qq",
106 "Re", "Rs", "Sc", "So",
107 "Sq", "Sm", "Sx", "Sy",
108 "Tn", "Ux", "Xc", "Xo",
109 "Fo", "Fc", "Oo", "Oc",
110 "Bk", "Ek", "Bt", "Hf",
111 "Fr", "Ud", "Lb", "Lp",
112 "Lk", "Mt", "Brq", "Bro",
114 "Brc", "\%C", "Es", "En",
116 "Dx", "\%Q", "br", "sp"
119 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
120 "split", "nosplit", "ragged",
121 "unfilled", "literal", "file",
122 "offset", "bullet", "dash",
123 "hyphen", "item", "enum",
124 "tag", "diag", "hang",
125 "ohang", "inset", "column",
126 "width", "compact", "std",
127 "filled", "words", "emphasis",
131 const char * const *mdoc_macronames
= __mdoc_macronames
;
132 const char * const *mdoc_argnames
= __mdoc_argnames
;
134 static void mdoc_free1(struct mdoc
*);
135 static int mdoc_alloc1(struct mdoc
*);
136 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
137 int, enum mdoc_type
);
138 static int node_append(struct mdoc
*,
140 static int parsetext(struct mdoc
*, int, char *);
141 static int parsemacro(struct mdoc
*, int, char *);
142 static int macrowarn(struct mdoc
*, int, const char *);
143 static int pstring(struct mdoc
*, int, int,
144 const char *, size_t);
147 extern size_t strlcpy(char *, const char *, size_t);
151 const struct mdoc_node
*
152 mdoc_node(const struct mdoc
*m
)
155 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
159 const struct mdoc_meta
*
160 mdoc_meta(const struct mdoc
*m
)
163 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
168 * Frees volatile resources (parse tree, meta-data, fields).
171 mdoc_free1(struct mdoc
*mdoc
)
175 mdoc_node_freelist(mdoc
->first
);
176 if (mdoc
->meta
.title
)
177 free(mdoc
->meta
.title
);
181 free(mdoc
->meta
.name
);
183 free(mdoc
->meta
.arch
);
185 free(mdoc
->meta
.vol
);
190 * Allocate all volatile resources (parse tree, meta-data, fields).
193 mdoc_alloc1(struct mdoc
*mdoc
)
196 bzero(&mdoc
->meta
, sizeof(struct mdoc_meta
));
198 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
199 mdoc
->last
= calloc(1, sizeof(struct mdoc_node
));
200 if (NULL
== mdoc
->last
)
203 mdoc
->first
= mdoc
->last
;
204 mdoc
->last
->type
= MDOC_ROOT
;
205 mdoc
->next
= MDOC_NEXT_CHILD
;
211 * Free up volatile resources (see mdoc_free1()) then re-initialises the
212 * data with mdoc_alloc1(). After invocation, parse data has been reset
213 * and the parser is ready for re-invocation on a new tree; however,
214 * cross-parse non-volatile data is kept intact.
217 mdoc_reset(struct mdoc
*mdoc
)
221 return(mdoc_alloc1(mdoc
));
226 * Completely free up all volatile and non-volatile parse resources.
227 * After invocation, the pointer is no longer usable.
230 mdoc_free(struct mdoc
*mdoc
)
235 mdoc_hash_free(mdoc
->htab
);
241 * Allocate volatile and non-volatile parse resources.
244 mdoc_alloc(void *data
, int pflags
, const struct mdoc_cb
*cb
)
248 if (NULL
== (p
= calloc(1, sizeof(struct mdoc
))))
251 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
256 if (NULL
== (p
->htab
= mdoc_hash_alloc())) {
259 } else if (mdoc_alloc1(p
))
268 * Climb back up the parse tree, validating open scopes. Mostly calls
269 * through to macro_end() in macro.c.
272 mdoc_endparse(struct mdoc
*m
)
275 if (MDOC_HALT
& m
->flags
)
277 else if (mdoc_macroend(m
))
279 m
->flags
|= MDOC_HALT
;
285 * Main parse routine. Parses a single line -- really just hands off to
286 * the macro (parsemacro()) or text parser (parsetext()).
289 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
292 if (MDOC_HALT
& m
->flags
)
295 return('.' == *buf
? parsemacro(m
, ln
, buf
) :
296 parsetext(m
, ln
, buf
));
301 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
302 const char *fmt
, ...)
307 if (NULL
== mdoc
->cb
.mdoc_err
)
311 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
314 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
319 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
324 if (NULL
== mdoc
->cb
.mdoc_warn
)
328 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
331 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, buf
));
336 mdoc_err(struct mdoc
*m
, int line
, int pos
, int iserr
, enum merr type
)
340 p
= __mdoc_merrnames
[(int)type
];
344 return(mdoc_verr(m
, line
, pos
, p
));
346 return(mdoc_vwarn(m
, line
, pos
, p
));
351 mdoc_macro(struct mdoc
*m
, int tok
,
352 int ln
, int pp
, int *pos
, char *buf
)
355 * If we're in the prologue, deny "body" macros. Similarly, if
356 * we're in the body, deny prologue calls.
358 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
359 MDOC_PBODY
& m
->flags
)
360 return(mdoc_perr(m
, ln
, pp
, EPROLBODY
));
361 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
362 ! (MDOC_PBODY
& m
->flags
))
363 return(mdoc_perr(m
, ln
, pp
, EBODYPROL
));
365 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
370 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
375 assert(MDOC_ROOT
!= p
->type
);
377 switch (mdoc
->next
) {
378 case (MDOC_NEXT_SIBLING
):
379 mdoc
->last
->next
= p
;
380 p
->prev
= mdoc
->last
;
381 p
->parent
= mdoc
->last
->parent
;
383 case (MDOC_NEXT_CHILD
):
384 mdoc
->last
->child
= p
;
385 p
->parent
= mdoc
->last
;
394 if ( ! mdoc_valid_pre(mdoc
, p
))
396 if ( ! mdoc_action_pre(mdoc
, p
))
401 assert(MDOC_BLOCK
== p
->parent
->type
);
405 assert(MDOC_BLOCK
== p
->parent
->type
);
409 assert(MDOC_BLOCK
== p
->parent
->type
);
420 if ( ! mdoc_valid_post(mdoc
))
422 if ( ! mdoc_action_post(mdoc
))
433 static struct mdoc_node
*
434 node_alloc(struct mdoc
*m
, int line
,
435 int pos
, int tok
, enum mdoc_type type
)
439 if (NULL
== (p
= calloc(1, sizeof(struct mdoc_node
)))) {
440 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
448 if (MDOC_TEXT
!= (p
->type
= type
))
456 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, int tok
)
460 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
463 return(node_append(m
, p
));
468 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, int tok
)
475 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
478 return(node_append(m
, p
));
483 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, int tok
)
487 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
490 return(node_append(m
, p
));
495 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
496 int tok
, struct mdoc_arg
*args
)
500 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
506 return(node_append(m
, p
));
511 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
512 int tok
, struct mdoc_arg
*args
)
516 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
522 return(node_append(m
, p
));
527 pstring(struct mdoc
*m
, int line
, int pos
, const char *p
, size_t len
)
532 n
= node_alloc(m
, line
, pos
, -1, MDOC_TEXT
);
534 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
536 n
->string
= malloc(len
+ 1);
537 if (NULL
== n
->string
) {
539 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
542 sv
= strlcpy(n
->string
, p
, len
+ 1);
544 /* Prohibit truncation. */
545 assert(sv
< len
+ 1);
547 return(node_append(m
, n
));
552 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
555 return(pstring(m
, line
, pos
, p
, strlen(p
)));
560 mdoc_node_free(struct mdoc_node
*p
)
568 mdoc_argv_free(p
->args
);
574 mdoc_node_freelist(struct mdoc_node
*p
)
578 mdoc_node_freelist(p
->child
);
580 mdoc_node_freelist(p
->next
);
582 assert(0 == p
->nchild
);
588 * Parse free-form text, that is, a line that does not begin with the
592 parsetext(struct mdoc
*m
, int line
, char *buf
)
596 if (SEC_NONE
== m
->lastnamed
)
597 return(mdoc_perr(m
, line
, 0, ETEXTPROL
));
600 * If in literal mode, then pass the buffer directly to the
601 * back-end, as it should be preserved as a single term.
604 if (MDOC_LITERAL
& m
->flags
) {
605 if ( ! mdoc_word_alloc(m
, line
, 0, buf
))
607 m
->next
= MDOC_NEXT_SIBLING
;
611 /* Disallow blank/white-space lines in non-literal mode. */
613 for (i
= 0; ' ' == buf
[i
]; i
++)
614 /* Skip leading whitespace. */ ;
616 return(mdoc_perr(m
, line
, 0, ENOBLANK
));
619 * Break apart a free-form line into tokens. Spaces are
620 * stripped out of the input.
623 for (j
= i
; buf
[i
]; i
++) {
627 /* Escaped whitespace. */
628 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
632 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
634 m
->next
= MDOC_NEXT_SIBLING
;
636 for ( ; ' ' == buf
[i
]; i
++)
637 /* Skip trailing whitespace. */ ;
644 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
647 m
->next
= MDOC_NEXT_SIBLING
;
655 macrowarn(struct mdoc
*m
, int ln
, const char *buf
)
657 if ( ! (MDOC_IGN_MACRO
& m
->pflags
))
658 return(mdoc_verr(m
, ln
, 0,
659 "unknown macro: %s%s",
660 buf
, strlen(buf
) > 3 ? "..." : ""));
661 return(mdoc_vwarn(m
, ln
, 0, "unknown macro: %s%s",
662 buf
, strlen(buf
) > 3 ? "..." : ""));
667 * Parse a macro line, that is, a line beginning with the control
671 parsemacro(struct mdoc
*m
, int ln
, char *buf
)
676 /* Empty lines are ignored. */
683 while (buf
[i
] && ' ' == buf
[i
])
687 return(mdoc_perr(m
, ln
, 1, ESPACE
));
690 /* Copy the first word into a nil-terminated buffer. */
692 for (i
= 1; i
< 5; i
++) {
693 if (0 == (mac
[i
- 1] = buf
[i
]))
695 else if (' ' == buf
[i
])
701 if (i
== 5 || i
<= 2) {
702 if ( ! macrowarn(m
, ln
, mac
))
707 if (MDOC_MAX
== (c
= mdoc_hash_find(m
->htab
, mac
))) {
708 if ( ! macrowarn(m
, ln
, mac
))
713 /* The macro is sane. Jump to the next word. */
715 while (buf
[i
] && ' ' == buf
[i
])
718 /* Begin recursive parse sequence. */
720 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
725 err
: /* Error out. */
727 m
->flags
|= MDOC_HALT
;