]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1184c641415f18f41455be0aba3f884f528de591
1 /* $Id: mdoc.c,v 1.151 2010/06/27 16:36:22 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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>
34 #include "libmandoc.h"
36 const char *const __mdoc_macronames
[MDOC_MAX
] = {
37 "Ap", "Dd", "Dt", "Os",
38 "Sh", "Ss", "Pp", "D1",
39 "Dl", "Bd", "Ed", "Bl",
40 "El", "It", "Ad", "An",
41 "Ar", "Cd", "Cm", "Dv",
42 "Er", "Ev", "Ex", "Fa",
43 "Fd", "Fl", "Fn", "Ft",
44 "Ic", "In", "Li", "Nd",
45 "Nm", "Op", "Ot", "Pa",
46 "Rv", "St", "Va", "Vt",
48 "Xr", "%A", "%B", "%D",
50 "%I", "%J", "%N", "%O",
52 "%P", "%R", "%T", "%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
69 "Brc", "%C", "Es", "En",
71 "Dx", "%Q", "br", "sp",
76 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
77 "split", "nosplit", "ragged",
78 "unfilled", "literal", "file",
79 "offset", "bullet", "dash",
80 "hyphen", "item", "enum",
81 "tag", "diag", "hang",
82 "ohang", "inset", "column",
83 "width", "compact", "std",
84 "filled", "words", "emphasis",
85 "symbolic", "nested", "centered"
88 const char * const *mdoc_macronames
= __mdoc_macronames
;
89 const char * const *mdoc_argnames
= __mdoc_argnames
;
91 static void mdoc_node_free(struct mdoc_node
*);
92 static void mdoc_node_unlink(struct mdoc
*,
94 static void mdoc_free1(struct mdoc
*);
95 static void mdoc_alloc1(struct mdoc
*);
96 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
97 enum mdoct
, enum mdoc_type
);
98 static int node_append(struct mdoc
*,
100 static int mdoc_ptext(struct mdoc
*, int, char *, int);
101 static int mdoc_pmacro(struct mdoc
*, int, char *, int);
102 static int macrowarn(struct mdoc
*, int,
106 const struct mdoc_node
*
107 mdoc_node(const struct mdoc
*m
)
110 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
114 const struct mdoc_meta
*
115 mdoc_meta(const struct mdoc
*m
)
118 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
123 * Frees volatile resources (parse tree, meta-data, fields).
126 mdoc_free1(struct mdoc
*mdoc
)
130 mdoc_node_delete(mdoc
, mdoc
->first
);
131 if (mdoc
->meta
.title
)
132 free(mdoc
->meta
.title
);
136 free(mdoc
->meta
.name
);
138 free(mdoc
->meta
.arch
);
140 free(mdoc
->meta
.vol
);
142 free(mdoc
->meta
.msec
);
147 * Allocate all volatile resources (parse tree, meta-data, fields).
150 mdoc_alloc1(struct mdoc
*mdoc
)
153 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
155 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
156 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
157 mdoc
->first
= mdoc
->last
;
158 mdoc
->last
->type
= MDOC_ROOT
;
159 mdoc
->next
= MDOC_NEXT_CHILD
;
164 * Free up volatile resources (see mdoc_free1()) then re-initialises the
165 * data with mdoc_alloc1(). After invocation, parse data has been reset
166 * and the parser is ready for re-invocation on a new tree; however,
167 * cross-parse non-volatile data is kept intact.
170 mdoc_reset(struct mdoc
*mdoc
)
179 * Completely free up all volatile and non-volatile parse resources.
180 * After invocation, the pointer is no longer usable.
183 mdoc_free(struct mdoc
*mdoc
)
192 * Allocate volatile and non-volatile parse resources.
195 mdoc_alloc(struct regset
*regs
, void *data
,
196 int pflags
, mandocmsg msg
)
200 p
= mandoc_calloc(1, sizeof(struct mdoc
));
214 * Climb back up the parse tree, validating open scopes. Mostly calls
215 * through to macro_end() in macro.c.
218 mdoc_endparse(struct mdoc
*m
)
221 if (MDOC_HALT
& m
->flags
)
223 else if (mdoc_macroend(m
))
225 m
->flags
|= MDOC_HALT
;
231 * Main parse routine. Parses a single line -- really just hands off to
232 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
235 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
238 if (MDOC_HALT
& m
->flags
)
241 m
->flags
|= MDOC_NEWLINE
;
242 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
243 mdoc_pmacro(m
, ln
, buf
, offs
) :
244 mdoc_ptext(m
, ln
, buf
, offs
));
249 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
250 int ln
, int pos
, const char *fmt
, ...)
256 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
259 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
264 mdoc_macro(MACRO_PROT_ARGS
)
266 assert(tok
< MDOC_MAX
);
268 /* If we're in the body, deny prologue calls. */
270 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
271 MDOC_PBODY
& m
->flags
)
272 return(mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
));
274 /* If we're in the prologue, deny "body" macros. */
276 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
277 ! (MDOC_PBODY
& m
->flags
)) {
278 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
))
280 if (NULL
== m
->meta
.title
)
281 m
->meta
.title
= mandoc_strdup("UNKNOWN");
282 if (NULL
== m
->meta
.vol
)
283 m
->meta
.vol
= mandoc_strdup("LOCAL");
284 if (NULL
== m
->meta
.os
)
285 m
->meta
.os
= mandoc_strdup("LOCAL");
286 if (0 == m
->meta
.date
)
287 m
->meta
.date
= time(NULL
);
288 m
->flags
|= MDOC_PBODY
;
291 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
296 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
301 assert(MDOC_ROOT
!= p
->type
);
303 switch (mdoc
->next
) {
304 case (MDOC_NEXT_SIBLING
):
305 mdoc
->last
->next
= p
;
306 p
->prev
= mdoc
->last
;
307 p
->parent
= mdoc
->last
->parent
;
309 case (MDOC_NEXT_CHILD
):
310 mdoc
->last
->child
= p
;
311 p
->parent
= mdoc
->last
;
320 if ( ! mdoc_valid_pre(mdoc
, p
))
322 if ( ! mdoc_action_pre(mdoc
, p
))
327 assert(MDOC_BLOCK
== p
->parent
->type
);
331 assert(MDOC_BLOCK
== p
->parent
->type
);
335 assert(MDOC_BLOCK
== p
->parent
->type
);
346 if ( ! mdoc_valid_post(mdoc
))
348 if ( ! mdoc_action_post(mdoc
))
359 static struct mdoc_node
*
360 node_alloc(struct mdoc
*m
, int line
, int pos
,
361 enum mdoct tok
, enum mdoc_type type
)
365 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
374 if (MDOC_NEWLINE
& m
->flags
)
375 p
->flags
|= MDOC_LINE
;
376 m
->flags
&= ~MDOC_NEWLINE
;
378 /* Section analysis. */
380 if (SEC_SYNOPSIS
== p
->sec
)
381 p
->flags
|= MDOC_SYNPRETTY
;
383 /* Register analysis. */
385 if (m
->regs
->regs
[(int)REG_nS
].set
) {
386 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
387 p
->flags
|= MDOC_SYNPRETTY
;
389 p
->flags
&= ~MDOC_SYNPRETTY
;
397 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
401 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
402 if ( ! node_append(m
, p
))
404 m
->next
= MDOC_NEXT_CHILD
;
410 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
417 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
418 if ( ! node_append(m
, p
))
420 m
->next
= MDOC_NEXT_CHILD
;
426 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
430 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
431 if ( ! node_append(m
, p
))
433 m
->next
= MDOC_NEXT_CHILD
;
439 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
440 enum mdoct tok
, struct mdoc_arg
*args
)
444 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
448 if ( ! node_append(m
, p
))
450 m
->next
= MDOC_NEXT_CHILD
;
456 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
457 enum mdoct tok
, struct mdoc_arg
*args
)
461 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
465 if ( ! node_append(m
, p
))
467 m
->next
= MDOC_NEXT_CHILD
;
473 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
480 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
481 n
->string
= mandoc_malloc(len
+ 1);
482 sv
= strlcpy(n
->string
, p
, len
+ 1);
484 /* Prohibit truncation. */
485 assert(sv
< len
+ 1);
487 if ( ! node_append(m
, n
))
490 m
->next
= MDOC_NEXT_SIBLING
;
496 mdoc_node_free(struct mdoc_node
*p
)
502 mdoc_argv_free(p
->args
);
508 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
511 /* Adjust siblings. */
514 n
->prev
->next
= n
->next
;
516 n
->next
->prev
= n
->prev
;
522 if (n
->parent
->child
== n
)
523 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
526 /* Adjust parse point, if applicable. */
528 if (m
&& m
->last
== n
) {
531 m
->next
= MDOC_NEXT_SIBLING
;
534 m
->next
= MDOC_NEXT_CHILD
;
538 if (m
&& m
->first
== n
)
544 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
549 mdoc_node_delete(m
, p
->child
);
551 assert(0 == p
->nchild
);
553 mdoc_node_unlink(m
, p
);
559 * Parse free-form text, that is, a line that does not begin with the
563 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
568 /* Ignore bogus comments. */
570 if ('\\' == buf
[offs
] &&
571 '.' == buf
[offs
+ 1] &&
572 '"' == buf
[offs
+ 2])
573 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
575 /* No text before an initial macro. */
577 if (SEC_NONE
== m
->lastnamed
)
578 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
584 * Divert directly to list processing if we're encountering a
585 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
586 * (a MDOC_BODY means it's already open, in which case we should
587 * process within its context in the normal way).
590 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
591 LIST_column
== n
->data
.Bl
.type
) {
592 /* `Bl' is open without any children. */
593 m
->flags
|= MDOC_FREECOL
;
594 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
597 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
599 MDOC_Bl
== n
->parent
->tok
&&
600 LIST_column
== n
->parent
->data
.Bl
.type
) {
601 /* `Bl' has block-level `It' children. */
602 m
->flags
|= MDOC_FREECOL
;
603 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
607 * Search for the beginning of unescaped trailing whitespace (ws)
608 * and for the first character not to be output (end).
611 /* FIXME: replace with strcspn(). */
613 for (c
= end
= buf
+ offs
; *c
; c
++) {
616 if (mandoc_hyph(buf
+ offs
, c
))
626 * Always warn about trailing tabs,
627 * even outside literal context,
628 * where they should be put on the next line.
633 * Strip trailing tabs in literal context only;
634 * outside, they affect the next line.
636 if (MDOC_LITERAL
& m
->flags
)
640 /* Skip the escaped character, too, if any. */
653 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
656 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
657 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
661 * Insert a `Pp' in the case of a blank line. Technically,
662 * blank lines aren't allowed, but enough manuals assume this
663 * behaviour that we want to work around it.
665 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_Pp
, NULL
))
668 m
->next
= MDOC_NEXT_SIBLING
;
672 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
675 if (MDOC_LITERAL
& m
->flags
)
679 * End-of-sentence check. If the last character is an unescaped
680 * EOS character, then flag the node as being the end of a
681 * sentence. The front-end will know how to interpret this.
686 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
687 m
->last
->flags
|= MDOC_EOS
;
694 macrowarn(struct mdoc
*m
, int ln
, const char *buf
, int offs
)
698 rc
= mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
699 "unknown macro: %s%s",
700 buf
, strlen(buf
) > 3 ? "..." : "");
702 /* FIXME: logic should be in driver. */
703 /* FIXME: broken, will error out and not omit a message. */
704 return(MDOC_IGN_MACRO
& m
->pflags
? rc
: 0);
709 * Parse a macro line, that is, a line beginning with the control
713 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
720 /* Empty lines are ignored. */
724 if ('\0' == buf
[offs
])
729 /* Accept whitespace after the initial control char. */
733 while (buf
[i
] && ' ' == buf
[i
])
741 /* Copy the first word into a nil-terminated buffer. */
743 for (j
= 0; j
< 4; j
++, i
++) {
744 if ('\0' == (mac
[j
] = buf
[i
]))
746 else if (' ' == buf
[i
])
749 /* Check for invalid characters. */
751 if (isgraph((u_char
)buf
[i
]))
753 if ( ! mdoc_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
760 if (j
== 4 || j
< 2) {
761 if ( ! macrowarn(m
, ln
, mac
, sv
))
766 if (MDOC_MAX
== (tok
= mdoc_hash_find(mac
))) {
767 if ( ! macrowarn(m
, ln
, mac
, sv
))
772 /* The macro is sane. Jump to the next word. */
774 while (buf
[i
] && ' ' == buf
[i
])
778 * Trailing whitespace. Note that tabs are allowed to be passed
779 * into the parser as "text", so we only warn about spaces here.
782 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
783 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
787 * If an initial macro or a list invocation, divert directly
788 * into macro processing.
791 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
792 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
801 * If the first macro of a `Bl -column', open an `It' block
802 * context around the parsed macro.
805 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
806 LIST_column
== n
->data
.Bl
.type
) {
807 m
->flags
|= MDOC_FREECOL
;
808 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
814 * If we're following a block-level `It' within a `Bl -column'
815 * context (perhaps opened in the above block or in ptext()),
816 * then open an `It' block context around the parsed macro.
819 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
821 MDOC_Bl
== n
->parent
->tok
&&
822 LIST_column
== n
->parent
->data
.Bl
.type
) {
823 m
->flags
|= MDOC_FREECOL
;
824 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
829 /* Normal processing of a macro. */
831 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
836 err
: /* Error out. */
838 m
->flags
|= MDOC_HALT
;