]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.139 2010/05/26 09:35:35 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.
21 #include <sys/types.h>
33 #include "libmandoc.h"
35 const char *const __mdoc_macronames
[MDOC_MAX
] = {
36 "Ap", "Dd", "Dt", "Os",
37 "Sh", "Ss", "Pp", "D1",
38 "Dl", "Bd", "Ed", "Bl",
39 "El", "It", "Ad", "An",
40 "Ar", "Cd", "Cm", "Dv",
41 "Er", "Ev", "Ex", "Fa",
42 "Fd", "Fl", "Fn", "Ft",
43 "Ic", "In", "Li", "Nd",
44 "Nm", "Op", "Ot", "Pa",
45 "Rv", "St", "Va", "Vt",
47 "Xr", "%A", "%B", "%D",
49 "%I", "%J", "%N", "%O",
51 "%P", "%R", "%T", "%V",
52 "Ac", "Ao", "Aq", "At",
53 "Bc", "Bf", "Bo", "Bq",
54 "Bsx", "Bx", "Db", "Dc",
55 "Do", "Dq", "Ec", "Ef",
56 "Em", "Eo", "Fx", "Ms",
57 "No", "Ns", "Nx", "Ox",
58 "Pc", "Pf", "Po", "Pq",
59 "Qc", "Ql", "Qo", "Qq",
60 "Re", "Rs", "Sc", "So",
61 "Sq", "Sm", "Sx", "Sy",
62 "Tn", "Ux", "Xc", "Xo",
63 "Fo", "Fc", "Oo", "Oc",
64 "Bk", "Ek", "Bt", "Hf",
65 "Fr", "Ud", "Lb", "Lp",
66 "Lk", "Mt", "Brq", "Bro",
68 "Brc", "%C", "Es", "En",
70 "Dx", "%Q", "br", "sp",
75 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
76 "split", "nosplit", "ragged",
77 "unfilled", "literal", "file",
78 "offset", "bullet", "dash",
79 "hyphen", "item", "enum",
80 "tag", "diag", "hang",
81 "ohang", "inset", "column",
82 "width", "compact", "std",
83 "filled", "words", "emphasis",
84 "symbolic", "nested", "centered"
87 const char * const *mdoc_macronames
= __mdoc_macronames
;
88 const char * const *mdoc_argnames
= __mdoc_argnames
;
90 static void mdoc_node_free(struct mdoc_node
*);
91 static void mdoc_node_unlink(struct mdoc
*,
93 static void mdoc_free1(struct mdoc
*);
94 static void mdoc_alloc1(struct mdoc
*);
95 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
96 enum mdoct
, enum mdoc_type
);
97 static int node_append(struct mdoc
*,
99 static int mdoc_ptext(struct mdoc
*, int, char *, int);
100 static int mdoc_pmacro(struct mdoc
*, int, char *, int);
101 static int macrowarn(struct mdoc
*, int,
105 const struct mdoc_node
*
106 mdoc_node(const struct mdoc
*m
)
109 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
113 const struct mdoc_meta
*
114 mdoc_meta(const struct mdoc
*m
)
117 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
122 * Frees volatile resources (parse tree, meta-data, fields).
125 mdoc_free1(struct mdoc
*mdoc
)
129 mdoc_node_delete(mdoc
, mdoc
->first
);
130 if (mdoc
->meta
.title
)
131 free(mdoc
->meta
.title
);
135 free(mdoc
->meta
.name
);
137 free(mdoc
->meta
.arch
);
139 free(mdoc
->meta
.vol
);
141 free(mdoc
->meta
.msec
);
146 * Allocate all volatile resources (parse tree, meta-data, fields).
149 mdoc_alloc1(struct mdoc
*mdoc
)
152 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
154 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
155 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
156 mdoc
->first
= mdoc
->last
;
157 mdoc
->last
->type
= MDOC_ROOT
;
158 mdoc
->next
= MDOC_NEXT_CHILD
;
163 * Free up volatile resources (see mdoc_free1()) then re-initialises the
164 * data with mdoc_alloc1(). After invocation, parse data has been reset
165 * and the parser is ready for re-invocation on a new tree; however,
166 * cross-parse non-volatile data is kept intact.
169 mdoc_reset(struct mdoc
*mdoc
)
178 * Completely free up all volatile and non-volatile parse resources.
179 * After invocation, the pointer is no longer usable.
182 mdoc_free(struct mdoc
*mdoc
)
191 * Allocate volatile and non-volatile parse resources.
194 mdoc_alloc(void *data
, int pflags
, mandocmsg msg
)
198 p
= mandoc_calloc(1, sizeof(struct mdoc
));
211 * Climb back up the parse tree, validating open scopes. Mostly calls
212 * through to macro_end() in macro.c.
215 mdoc_endparse(struct mdoc
*m
)
218 if (MDOC_HALT
& m
->flags
)
220 else if (mdoc_macroend(m
))
222 m
->flags
|= MDOC_HALT
;
228 * Main parse routine. Parses a single line -- really just hands off to
229 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
232 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
235 if (MDOC_HALT
& m
->flags
)
238 m
->flags
|= MDOC_NEWLINE
;
239 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
240 mdoc_pmacro(m
, ln
, buf
, offs
) :
241 mdoc_ptext(m
, ln
, buf
, offs
));
246 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
247 int ln
, int pos
, const char *fmt
, ...)
253 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
256 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
261 mdoc_macro(struct mdoc
*m
, enum mdoct tok
,
262 int ln
, int pp
, int *pos
, char *buf
)
264 assert(tok
< MDOC_MAX
);
266 /* If we're in the body, deny prologue calls. */
268 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
269 MDOC_PBODY
& m
->flags
)
270 return(mdoc_pmsg(m
, ln
, pp
, MANDOCERR_BADBODY
));
272 /* If we're in the prologue, deny "body" macros. */
274 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
275 ! (MDOC_PBODY
& m
->flags
)) {
276 if ( ! mdoc_pmsg(m
, ln
, pp
, MANDOCERR_BADPROLOG
))
278 if (NULL
== m
->meta
.title
)
279 m
->meta
.title
= mandoc_strdup("unknown");
280 if (NULL
== m
->meta
.vol
)
281 m
->meta
.vol
= mandoc_strdup("local");
282 if (NULL
== m
->meta
.os
)
283 m
->meta
.os
= mandoc_strdup("local");
284 if (0 == m
->meta
.date
)
285 m
->meta
.date
= time(NULL
);
286 m
->flags
|= MDOC_PBODY
;
289 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
294 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
299 assert(MDOC_ROOT
!= p
->type
);
301 switch (mdoc
->next
) {
302 case (MDOC_NEXT_SIBLING
):
303 mdoc
->last
->next
= p
;
304 p
->prev
= mdoc
->last
;
305 p
->parent
= mdoc
->last
->parent
;
307 case (MDOC_NEXT_CHILD
):
308 mdoc
->last
->child
= p
;
309 p
->parent
= mdoc
->last
;
318 if ( ! mdoc_valid_pre(mdoc
, p
))
320 if ( ! mdoc_action_pre(mdoc
, p
))
325 assert(MDOC_BLOCK
== p
->parent
->type
);
329 assert(MDOC_BLOCK
== p
->parent
->type
);
333 assert(MDOC_BLOCK
== p
->parent
->type
);
344 if ( ! mdoc_valid_post(mdoc
))
346 if ( ! mdoc_action_post(mdoc
))
357 static struct mdoc_node
*
358 node_alloc(struct mdoc
*m
, int line
, int pos
,
359 enum mdoct tok
, enum mdoc_type type
)
363 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
369 if (MDOC_NEWLINE
& m
->flags
)
370 p
->flags
|= MDOC_LINE
;
371 m
->flags
&= ~MDOC_NEWLINE
;
377 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
381 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
382 if ( ! node_append(m
, p
))
384 m
->next
= MDOC_NEXT_CHILD
;
390 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
397 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
398 if ( ! node_append(m
, p
))
400 m
->next
= MDOC_NEXT_CHILD
;
406 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
410 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
411 if ( ! node_append(m
, p
))
413 m
->next
= MDOC_NEXT_CHILD
;
419 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
420 enum mdoct tok
, struct mdoc_arg
*args
)
424 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
428 if ( ! node_append(m
, p
))
430 m
->next
= MDOC_NEXT_CHILD
;
436 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
437 enum mdoct tok
, struct mdoc_arg
*args
)
441 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
445 if ( ! node_append(m
, p
))
447 m
->next
= MDOC_NEXT_CHILD
;
453 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
460 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
461 n
->string
= mandoc_malloc(len
+ 1);
462 sv
= strlcpy(n
->string
, p
, len
+ 1);
464 /* Prohibit truncation. */
465 assert(sv
< len
+ 1);
467 if ( ! node_append(m
, n
))
470 m
->next
= MDOC_NEXT_SIBLING
;
476 mdoc_node_free(struct mdoc_node
*p
)
482 mdoc_argv_free(p
->args
);
488 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
491 /* Adjust siblings. */
494 n
->prev
->next
= n
->next
;
496 n
->next
->prev
= n
->prev
;
502 if (n
->parent
->child
== n
)
503 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
506 /* Adjust parse point, if applicable. */
508 if (m
&& m
->last
== n
) {
511 m
->next
= MDOC_NEXT_SIBLING
;
514 m
->next
= MDOC_NEXT_CHILD
;
518 if (m
&& m
->first
== n
)
524 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
529 mdoc_node_delete(m
, p
->child
);
531 assert(0 == p
->nchild
);
533 mdoc_node_unlink(m
, p
);
539 * Parse free-form text, that is, a line that does not begin with the
543 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
547 /* Ignore bogus comments. */
549 if ('\\' == buf
[offs
] &&
550 '.' == buf
[offs
+ 1] &&
551 '"' == buf
[offs
+ 2])
552 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
554 /* No text before an initial macro. */
556 if (SEC_NONE
== m
->lastnamed
)
557 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
560 * Search for the beginning of unescaped trailing whitespace (ws)
561 * and for the first character not to be output (end).
564 /* FIXME: replace with strcspn(). */
566 for (c
= end
= buf
+ offs
; *c
; c
++) {
569 if (mandoc_hyph(buf
+ offs
, c
))
578 * Always warn about trailing tabs,
579 * even outside literal context,
580 * where they should be put on the next line.
585 * Strip trailing tabs in literal context only;
586 * outside, they affect the next line.
588 if (MDOC_LITERAL
& m
->flags
)
592 /* Skip the escaped character, too, if any. */
605 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
608 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
609 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
613 * Insert a `Pp' in the case of a blank line. Technically,
614 * blank lines aren't allowed, but enough manuals assume this
615 * behaviour that we want to work around it.
617 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_Pp
, NULL
))
620 m
->next
= MDOC_NEXT_SIBLING
;
624 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
627 if (MDOC_LITERAL
& m
->flags
)
631 * End-of-sentence check. If the last character is an unescaped
632 * EOS character, then flag the node as being the end of a
633 * sentence. The front-end will know how to interpret this.
638 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
639 m
->last
->flags
|= MDOC_EOS
;
646 macrowarn(struct mdoc
*m
, int ln
, const char *buf
, int offs
)
650 rc
= mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
651 "unknown macro: %s%s",
652 buf
, strlen(buf
) > 3 ? "..." : "");
654 /* FIXME: logic should be in driver. */
655 return(MDOC_IGN_MACRO
& m
->pflags
? rc
: 0);
660 * Parse a macro line, that is, a line beginning with the control
664 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
670 /* Empty lines are ignored. */
674 if ('\0' == buf
[offs
])
679 /* Accept whitespace after the initial control char. */
683 while (buf
[i
] && ' ' == buf
[i
])
691 /* Copy the first word into a nil-terminated buffer. */
693 for (j
= 0; j
< 4; j
++, i
++) {
694 if ('\0' == (mac
[j
] = buf
[i
]))
696 else if (' ' == buf
[i
])
699 /* Check for invalid characters. */
701 if (isgraph((u_char
)buf
[i
]))
703 if ( ! mdoc_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
710 if (j
== 4 || j
< 2) {
711 if ( ! macrowarn(m
, ln
, mac
, sv
))
716 if (MDOC_MAX
== (tok
= mdoc_hash_find(mac
))) {
717 if ( ! macrowarn(m
, ln
, mac
, sv
))
722 /* The macro is sane. Jump to the next word. */
724 while (buf
[i
] && ' ' == buf
[i
])
728 * Trailing whitespace. Note that tabs are allowed to be passed
729 * into the parser as "text", so we only warn about spaces here.
732 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
733 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
737 * Begin recursive parse sequence. Since we're at the start of
738 * the line, we don't need to do callable/parseable checks.
740 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
745 err
: /* Error out. */
747 m
->flags
|= MDOC_HALT
;