]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.165 2010/09/27 23:03:44 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #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);
103 const struct mdoc_node
*
104 mdoc_node(const struct mdoc
*m
)
107 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
111 const struct mdoc_meta
*
112 mdoc_meta(const struct mdoc
*m
)
115 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
120 * Frees volatile resources (parse tree, meta-data, fields).
123 mdoc_free1(struct mdoc
*mdoc
)
127 mdoc_node_delete(mdoc
, mdoc
->first
);
128 if (mdoc
->meta
.title
)
129 free(mdoc
->meta
.title
);
133 free(mdoc
->meta
.name
);
135 free(mdoc
->meta
.arch
);
137 free(mdoc
->meta
.vol
);
139 free(mdoc
->meta
.msec
);
144 * Allocate all volatile resources (parse tree, meta-data, fields).
147 mdoc_alloc1(struct mdoc
*mdoc
)
150 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
152 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
153 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
154 mdoc
->first
= mdoc
->last
;
155 mdoc
->last
->type
= MDOC_ROOT
;
156 mdoc
->next
= MDOC_NEXT_CHILD
;
161 * Free up volatile resources (see mdoc_free1()) then re-initialises the
162 * data with mdoc_alloc1(). After invocation, parse data has been reset
163 * and the parser is ready for re-invocation on a new tree; however,
164 * cross-parse non-volatile data is kept intact.
167 mdoc_reset(struct mdoc
*mdoc
)
176 * Completely free up all volatile and non-volatile parse resources.
177 * After invocation, the pointer is no longer usable.
180 mdoc_free(struct mdoc
*mdoc
)
189 * Allocate volatile and non-volatile parse resources.
192 mdoc_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
196 p
= mandoc_calloc(1, sizeof(struct mdoc
));
209 * Climb back up the parse tree, validating open scopes. Mostly calls
210 * through to macro_end() in macro.c.
213 mdoc_endparse(struct mdoc
*m
)
216 if (MDOC_HALT
& m
->flags
)
218 else if (mdoc_macroend(m
))
220 m
->flags
|= MDOC_HALT
;
226 * Main parse routine. Parses a single line -- really just hands off to
227 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
230 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
233 if (MDOC_HALT
& m
->flags
)
236 m
->flags
|= MDOC_NEWLINE
;
239 * Let the roff nS register switch SYNOPSIS mode early,
240 * such that the parser knows at all times
241 * whether this mode is on or off.
242 * Note that this mode is also switched by the Sh macro.
244 if (m
->regs
->regs
[(int)REG_nS
].set
) {
245 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
246 m
->flags
|= MDOC_SYNOPSIS
;
248 m
->flags
&= ~MDOC_SYNOPSIS
;
251 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
252 mdoc_pmacro(m
, ln
, buf
, offs
) :
253 mdoc_ptext(m
, ln
, buf
, offs
));
258 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
259 int ln
, int pos
, const char *fmt
, ...)
265 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
268 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
273 mdoc_macro(MACRO_PROT_ARGS
)
275 assert(tok
< MDOC_MAX
);
277 /* If we're in the body, deny prologue calls. */
279 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
280 MDOC_PBODY
& m
->flags
)
281 return(mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
));
283 /* If we're in the prologue, deny "body" macros. */
285 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
286 ! (MDOC_PBODY
& m
->flags
)) {
287 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
))
289 if (NULL
== m
->meta
.title
)
290 m
->meta
.title
= mandoc_strdup("UNKNOWN");
291 if (NULL
== m
->meta
.vol
)
292 m
->meta
.vol
= mandoc_strdup("LOCAL");
293 if (NULL
== m
->meta
.os
)
294 m
->meta
.os
= mandoc_strdup("LOCAL");
295 if (0 == m
->meta
.date
)
296 m
->meta
.date
= time(NULL
);
297 m
->flags
|= MDOC_PBODY
;
300 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
305 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
310 assert(MDOC_ROOT
!= p
->type
);
312 switch (mdoc
->next
) {
313 case (MDOC_NEXT_SIBLING
):
314 mdoc
->last
->next
= p
;
315 p
->prev
= mdoc
->last
;
316 p
->parent
= mdoc
->last
->parent
;
318 case (MDOC_NEXT_CHILD
):
319 mdoc
->last
->child
= p
;
320 p
->parent
= mdoc
->last
;
329 if ( ! mdoc_valid_pre(mdoc
, p
))
331 if ( ! mdoc_action_pre(mdoc
, p
))
336 assert(MDOC_BLOCK
== p
->parent
->type
);
340 assert(MDOC_BLOCK
== p
->parent
->type
);
346 assert(MDOC_BLOCK
== p
->parent
->type
);
357 if ( ! mdoc_valid_post(mdoc
))
359 if ( ! mdoc_action_post(mdoc
))
370 static struct mdoc_node
*
371 node_alloc(struct mdoc
*m
, int line
, int pos
,
372 enum mdoct tok
, enum mdoc_type type
)
376 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
385 if (MDOC_SYNOPSIS
& m
->flags
)
386 p
->flags
|= MDOC_SYNPRETTY
;
388 p
->flags
&= ~MDOC_SYNPRETTY
;
389 if (MDOC_NEWLINE
& m
->flags
)
390 p
->flags
|= MDOC_LINE
;
391 m
->flags
&= ~MDOC_NEWLINE
;
398 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
402 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
403 if ( ! node_append(m
, p
))
405 m
->next
= MDOC_NEXT_CHILD
;
411 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
418 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
419 if ( ! node_append(m
, p
))
421 m
->next
= MDOC_NEXT_CHILD
;
427 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
431 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
432 if ( ! node_append(m
, p
))
434 m
->next
= MDOC_NEXT_CHILD
;
440 mdoc_endbody_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
,
441 struct mdoc_node
*body
, enum mdoc_endbody end
)
445 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
448 if ( ! node_append(m
, p
))
450 m
->next
= MDOC_NEXT_SIBLING
;
456 mdoc_block_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_BLOCK
);
465 if ( ! node_append(m
, p
))
467 m
->next
= MDOC_NEXT_CHILD
;
473 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
474 enum mdoct tok
, struct mdoc_arg
*args
)
478 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
482 if ( ! node_append(m
, p
))
484 m
->next
= MDOC_NEXT_CHILD
;
490 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
497 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
498 n
->string
= mandoc_malloc(len
+ 1);
499 sv
= strlcpy(n
->string
, p
, len
+ 1);
501 /* Prohibit truncation. */
502 assert(sv
< len
+ 1);
504 if ( ! node_append(m
, n
))
507 m
->next
= MDOC_NEXT_SIBLING
;
513 mdoc_node_free(struct mdoc_node
*p
)
517 * XXX: if these end up being problematic in terms of memory
518 * management and dereferencing freed blocks, then make them
519 * into reference-counted double-pointers.
522 if (MDOC_Bd
== p
->tok
&& MDOC_BLOCK
== p
->type
)
525 if (MDOC_Bl
== p
->tok
&& MDOC_BLOCK
== p
->type
)
528 if (MDOC_Bf
== p
->tok
&& MDOC_HEAD
== p
->type
)
535 mdoc_argv_free(p
->args
);
541 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
544 /* Adjust siblings. */
547 n
->prev
->next
= n
->next
;
549 n
->next
->prev
= n
->prev
;
555 if (n
->parent
->child
== n
)
556 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
559 /* Adjust parse point, if applicable. */
561 if (m
&& m
->last
== n
) {
564 m
->next
= MDOC_NEXT_SIBLING
;
567 m
->next
= MDOC_NEXT_CHILD
;
571 if (m
&& m
->first
== n
)
577 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
582 mdoc_node_delete(m
, p
->child
);
584 assert(0 == p
->nchild
);
586 mdoc_node_unlink(m
, p
);
592 * Parse free-form text, that is, a line that does not begin with the
596 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
601 /* Ignore bogus comments. */
603 if ('\\' == buf
[offs
] &&
604 '.' == buf
[offs
+ 1] &&
605 '"' == buf
[offs
+ 2])
606 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
608 /* No text before an initial macro. */
610 if (SEC_NONE
== m
->lastnamed
)
611 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
617 * Divert directly to list processing if we're encountering a
618 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
619 * (a MDOC_BODY means it's already open, in which case we should
620 * process within its context in the normal way).
623 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
624 LIST_column
== n
->data
.Bl
->type
) {
625 /* `Bl' is open without any children. */
626 m
->flags
|= MDOC_FREECOL
;
627 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
630 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
632 MDOC_Bl
== n
->parent
->tok
&&
633 LIST_column
== n
->parent
->data
.Bl
->type
) {
634 /* `Bl' has block-level `It' children. */
635 m
->flags
|= MDOC_FREECOL
;
636 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
640 * Search for the beginning of unescaped trailing whitespace (ws)
641 * and for the first character not to be output (end).
644 /* FIXME: replace with strcspn(). */
646 for (c
= end
= buf
+ offs
; *c
; c
++) {
649 if (mandoc_hyph(buf
+ offs
, c
))
659 * Always warn about trailing tabs,
660 * even outside literal context,
661 * where they should be put on the next line.
666 * Strip trailing tabs in literal context only;
667 * outside, they affect the next line.
669 if (MDOC_LITERAL
& m
->flags
)
673 /* Skip the escaped character, too, if any. */
686 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
689 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
690 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
694 * Insert a `sp' in the case of a blank line. Technically,
695 * blank lines aren't allowed, but enough manuals assume this
696 * behaviour that we want to work around it.
698 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_sp
, NULL
))
701 m
->next
= MDOC_NEXT_SIBLING
;
705 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
708 if (MDOC_LITERAL
& m
->flags
)
712 * End-of-sentence check. If the last character is an unescaped
713 * EOS character, then flag the node as being the end of a
714 * sentence. The front-end will know how to interpret this.
719 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
720 m
->last
->flags
|= MDOC_EOS
;
727 * Parse a macro line, that is, a line beginning with the control
731 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
738 /* Empty lines are ignored. */
742 if ('\0' == buf
[offs
])
747 /* Accept tabs/whitespace after the initial control char. */
749 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
751 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
760 * Copy the first word into a nil-terminated buffer.
761 * Stop copying when a tab, space, or eoln is encountered.
765 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
769 tok
= (j
> 1 || j
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
770 if (MDOC_MAX
== tok
) {
771 mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, sv
,
772 "unknown macro: %s%s",
773 buf
, strlen(buf
) > 3 ? "..." : "");
777 /* Disregard the first trailing tab, if applicable. */
782 /* Jump to the next non-whitespace word. */
784 while (buf
[i
] && ' ' == buf
[i
])
788 * Trailing whitespace. Note that tabs are allowed to be passed
789 * into the parser as "text", so we only warn about spaces here.
792 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
793 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
797 * If an initial macro or a list invocation, divert directly
798 * into macro processing.
801 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
802 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
811 * If the first macro of a `Bl -column', open an `It' block
812 * context around the parsed macro.
815 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
816 LIST_column
== n
->data
.Bl
->type
) {
817 m
->flags
|= MDOC_FREECOL
;
818 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
824 * If we're following a block-level `It' within a `Bl -column'
825 * context (perhaps opened in the above block or in ptext()),
826 * then open an `It' block context around the parsed macro.
829 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
831 MDOC_Bl
== n
->parent
->tok
&&
832 LIST_column
== n
->parent
->data
.Bl
->type
) {
833 m
->flags
|= MDOC_FREECOL
;
834 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
839 /* Normal processing of a macro. */
841 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
846 err
: /* Error out. */
848 m
->flags
|= MDOC_HALT
;