]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.183 2011/03/15 13:23:33 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 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);
102 const struct mdoc_node
*
103 mdoc_node(const struct mdoc
*m
)
106 assert( ! (MDOC_HALT
& m
->flags
));
111 const struct mdoc_meta
*
112 mdoc_meta(const struct mdoc
*m
)
115 assert( ! (MDOC_HALT
& m
->flags
));
121 * Frees volatile resources (parse tree, meta-data, fields).
124 mdoc_free1(struct mdoc
*mdoc
)
128 mdoc_node_delete(mdoc
, mdoc
->first
);
129 if (mdoc
->meta
.title
)
130 free(mdoc
->meta
.title
);
134 free(mdoc
->meta
.name
);
136 free(mdoc
->meta
.arch
);
138 free(mdoc
->meta
.vol
);
140 free(mdoc
->meta
.msec
);
142 free(mdoc
->meta
.date
);
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
, mandocmsg msg
)
199 p
= mandoc_calloc(1, sizeof(struct mdoc
));
212 * Climb back up the parse tree, validating open scopes. Mostly calls
213 * through to macro_end() in macro.c.
216 mdoc_endparse(struct mdoc
*m
)
219 assert( ! (MDOC_HALT
& m
->flags
));
220 if (mdoc_macroend(m
))
222 m
->flags
|= MDOC_HALT
;
227 mdoc_addeqn(struct mdoc
*m
, const struct eqn
*ep
)
231 assert( ! (MDOC_HALT
& m
->flags
));
233 /* No text before an initial macro. */
235 if (SEC_NONE
== m
->lastnamed
) {
236 mdoc_pmsg(m
, ep
->line
, ep
->pos
, MANDOCERR_NOTEXT
);
240 n
= node_alloc(m
, ep
->line
, ep
->pos
, MDOC_MAX
, MDOC_EQN
);
243 if ( ! node_append(m
, n
))
246 m
->next
= MDOC_NEXT_SIBLING
;
251 mdoc_addspan(struct mdoc
*m
, const struct tbl_span
*sp
)
255 assert( ! (MDOC_HALT
& m
->flags
));
257 /* No text before an initial macro. */
259 if (SEC_NONE
== m
->lastnamed
) {
260 mdoc_pmsg(m
, sp
->line
, 0, MANDOCERR_NOTEXT
);
264 n
= node_alloc(m
, sp
->line
, 0, MDOC_MAX
, MDOC_TBL
);
267 if ( ! node_append(m
, n
))
270 m
->next
= MDOC_NEXT_SIBLING
;
276 * Main parse routine. Parses a single line -- really just hands off to
277 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
280 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
283 assert( ! (MDOC_HALT
& m
->flags
));
285 m
->flags
|= MDOC_NEWLINE
;
288 * Let the roff nS register switch SYNOPSIS mode early,
289 * such that the parser knows at all times
290 * whether this mode is on or off.
291 * Note that this mode is also switched by the Sh macro.
293 if (m
->regs
->regs
[(int)REG_nS
].set
) {
294 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
295 m
->flags
|= MDOC_SYNOPSIS
;
297 m
->flags
&= ~MDOC_SYNOPSIS
;
300 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
301 mdoc_pmacro(m
, ln
, buf
, offs
) :
302 mdoc_ptext(m
, ln
, buf
, offs
));
307 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
308 int ln
, int pos
, const char *fmt
, ...)
314 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
317 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
322 mdoc_macro(MACRO_PROT_ARGS
)
324 assert(tok
< MDOC_MAX
);
326 /* If we're in the body, deny prologue calls. */
328 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
329 MDOC_PBODY
& m
->flags
) {
330 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
);
334 /* If we're in the prologue, deny "body" macros. */
336 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
337 ! (MDOC_PBODY
& m
->flags
)) {
338 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
);
339 if (NULL
== m
->meta
.msec
)
340 m
->meta
.msec
= mandoc_strdup("1");
341 if (NULL
== m
->meta
.title
)
342 m
->meta
.title
= mandoc_strdup("UNKNOWN");
343 if (NULL
== m
->meta
.vol
)
344 m
->meta
.vol
= mandoc_strdup("LOCAL");
345 if (NULL
== m
->meta
.os
)
346 m
->meta
.os
= mandoc_strdup("LOCAL");
347 if (NULL
== m
->meta
.date
)
348 m
->meta
.date
= mandoc_normdate(NULL
,
349 m
->msg
, m
->data
, line
, ppos
);
350 m
->flags
|= MDOC_PBODY
;
353 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
358 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
363 assert(MDOC_ROOT
!= p
->type
);
365 switch (mdoc
->next
) {
366 case (MDOC_NEXT_SIBLING
):
367 mdoc
->last
->next
= p
;
368 p
->prev
= mdoc
->last
;
369 p
->parent
= mdoc
->last
->parent
;
371 case (MDOC_NEXT_CHILD
):
372 mdoc
->last
->child
= p
;
373 p
->parent
= mdoc
->last
;
383 * Copy over the normalised-data pointer of our parent. Not
384 * everybody has one, but copying a null pointer is fine.
393 p
->norm
= p
->parent
->norm
;
399 if ( ! mdoc_valid_pre(mdoc
, p
))
404 assert(MDOC_BLOCK
== p
->parent
->type
);
408 assert(MDOC_BLOCK
== p
->parent
->type
);
414 assert(MDOC_BLOCK
== p
->parent
->type
);
427 if ( ! mdoc_valid_post(mdoc
))
438 static struct mdoc_node
*
439 node_alloc(struct mdoc
*m
, int line
, int pos
,
440 enum mdoct tok
, enum mdoc_type type
)
444 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
453 if (MDOC_SYNOPSIS
& m
->flags
)
454 p
->flags
|= MDOC_SYNPRETTY
;
456 p
->flags
&= ~MDOC_SYNPRETTY
;
457 if (MDOC_NEWLINE
& m
->flags
)
458 p
->flags
|= MDOC_LINE
;
459 m
->flags
&= ~MDOC_NEWLINE
;
466 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
470 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
471 if ( ! node_append(m
, p
))
473 m
->next
= MDOC_NEXT_CHILD
;
479 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
486 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
487 if ( ! node_append(m
, p
))
489 m
->next
= MDOC_NEXT_CHILD
;
495 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
499 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
500 if ( ! node_append(m
, p
))
502 m
->next
= MDOC_NEXT_CHILD
;
508 mdoc_endbody_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
,
509 struct mdoc_node
*body
, enum mdoc_endbody end
)
513 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
516 if ( ! node_append(m
, p
))
518 m
->next
= MDOC_NEXT_SIBLING
;
524 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
525 enum mdoct tok
, struct mdoc_arg
*args
)
529 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
542 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
548 if ( ! node_append(m
, p
))
550 m
->next
= MDOC_NEXT_CHILD
;
556 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
557 enum mdoct tok
, struct mdoc_arg
*args
)
561 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
568 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
574 if ( ! node_append(m
, p
))
576 m
->next
= MDOC_NEXT_CHILD
;
581 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
588 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
589 n
->string
= mandoc_malloc(len
+ 1);
590 sv
= strlcpy(n
->string
, p
, len
+ 1);
592 /* Prohibit truncation. */
593 assert(sv
< len
+ 1);
595 if ( ! node_append(m
, n
))
598 m
->next
= MDOC_NEXT_SIBLING
;
604 mdoc_node_free(struct mdoc_node
*p
)
607 if (MDOC_BLOCK
== p
->type
|| MDOC_ELEM
== p
->type
)
612 mdoc_argv_free(p
->args
);
618 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
621 /* Adjust siblings. */
624 n
->prev
->next
= n
->next
;
626 n
->next
->prev
= n
->prev
;
632 if (n
->parent
->child
== n
)
633 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
634 if (n
->parent
->last
== n
)
635 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
638 /* Adjust parse point, if applicable. */
640 if (m
&& m
->last
== n
) {
643 m
->next
= MDOC_NEXT_SIBLING
;
646 m
->next
= MDOC_NEXT_CHILD
;
650 if (m
&& m
->first
== n
)
656 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
661 mdoc_node_delete(m
, p
->child
);
663 assert(0 == p
->nchild
);
665 mdoc_node_unlink(m
, p
);
671 * Parse free-form text, that is, a line that does not begin with the
675 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
680 /* Ignore bogus comments. */
682 if ('\\' == buf
[offs
] &&
683 '.' == buf
[offs
+ 1] &&
684 '"' == buf
[offs
+ 2]) {
685 mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
689 /* No text before an initial macro. */
691 if (SEC_NONE
== m
->lastnamed
) {
692 mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
);
700 * Divert directly to list processing if we're encountering a
701 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
702 * (a MDOC_BODY means it's already open, in which case we should
703 * process within its context in the normal way).
706 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
707 LIST_column
== n
->norm
->Bl
.type
) {
708 /* `Bl' is open without any children. */
709 m
->flags
|= MDOC_FREECOL
;
710 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
713 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
715 MDOC_Bl
== n
->parent
->tok
&&
716 LIST_column
== n
->parent
->norm
->Bl
.type
) {
717 /* `Bl' has block-level `It' children. */
718 m
->flags
|= MDOC_FREECOL
;
719 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
723 * Search for the beginning of unescaped trailing whitespace (ws)
724 * and for the first character not to be output (end).
727 /* FIXME: replace with strcspn(). */
729 for (c
= end
= buf
+ offs
; *c
; c
++) {
732 if (mandoc_hyph(buf
+ offs
, c
))
742 * Always warn about trailing tabs,
743 * even outside literal context,
744 * where they should be put on the next line.
749 * Strip trailing tabs in literal context only;
750 * outside, they affect the next line.
752 if (MDOC_LITERAL
& m
->flags
)
756 /* Skip the escaped character, too, if any. */
769 mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
);
771 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
772 mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
);
775 * Insert a `sp' in the case of a blank line. Technically,
776 * blank lines aren't allowed, but enough manuals assume this
777 * behaviour that we want to work around it.
779 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_sp
, NULL
))
782 m
->next
= MDOC_NEXT_SIBLING
;
786 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
789 if (MDOC_LITERAL
& m
->flags
)
793 * End-of-sentence check. If the last character is an unescaped
794 * EOS character, then flag the node as being the end of a
795 * sentence. The front-end will know how to interpret this.
800 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
801 m
->last
->flags
|= MDOC_EOS
;
808 * Parse a macro line, that is, a line beginning with the control
812 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
819 /* Empty lines are ignored. */
823 if ('\0' == buf
[offs
])
828 /* Accept tabs/whitespace after the initial control char. */
830 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
832 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
841 * Copy the first word into a nil-terminated buffer.
842 * Stop copying when a tab, space, or eoln is encountered.
846 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
850 tok
= (j
> 1 || j
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
851 if (MDOC_MAX
== tok
) {
852 mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, sv
, "%s", buf
+ sv
- 1);
856 /* Disregard the first trailing tab, if applicable. */
861 /* Jump to the next non-whitespace word. */
863 while (buf
[i
] && ' ' == buf
[i
])
867 * Trailing whitespace. Note that tabs are allowed to be passed
868 * into the parser as "text", so we only warn about spaces here.
871 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
872 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
875 * If an initial macro or a list invocation, divert directly
876 * into macro processing.
879 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
880 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
889 * If the first macro of a `Bl -column', open an `It' block
890 * context around the parsed macro.
893 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
894 LIST_column
== n
->norm
->Bl
.type
) {
895 m
->flags
|= MDOC_FREECOL
;
896 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
902 * If we're following a block-level `It' within a `Bl -column'
903 * context (perhaps opened in the above block or in ptext()),
904 * then open an `It' block context around the parsed macro.
907 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
909 MDOC_Bl
== n
->parent
->tok
&&
910 LIST_column
== n
->parent
->norm
->Bl
.type
) {
911 m
->flags
|= MDOC_FREECOL
;
912 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
917 /* Normal processing of a macro. */
919 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
924 err
: /* Error out. */
926 m
->flags
|= MDOC_HALT
;