]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.162 2010/08/08 14:51:32 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);
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(struct regset
*regs
, void *data
,
195 int pflags
, mandocmsg msg
)
199 p
= mandoc_calloc(1, sizeof(struct mdoc
));
213 * Climb back up the parse tree, validating open scopes. Mostly calls
214 * through to macro_end() in macro.c.
217 mdoc_endparse(struct mdoc
*m
)
220 if (MDOC_HALT
& m
->flags
)
222 else if (mdoc_macroend(m
))
224 m
->flags
|= MDOC_HALT
;
230 * Main parse routine. Parses a single line -- really just hands off to
231 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
234 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
237 if (MDOC_HALT
& m
->flags
)
240 m
->flags
|= MDOC_NEWLINE
;
243 * Let the roff nS register switch SYNOPSIS mode early,
244 * such that the parser knows at all times
245 * whether this mode is on or off.
246 * Note that this mode is also switched by the Sh macro.
248 if (m
->regs
->regs
[(int)REG_nS
].set
) {
249 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
250 m
->flags
|= MDOC_SYNOPSIS
;
252 m
->flags
&= ~MDOC_SYNOPSIS
;
255 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
256 mdoc_pmacro(m
, ln
, buf
, offs
) :
257 mdoc_ptext(m
, ln
, buf
, offs
));
262 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
263 int ln
, int pos
, const char *fmt
, ...)
269 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
272 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
277 mdoc_macro(MACRO_PROT_ARGS
)
279 assert(tok
< MDOC_MAX
);
281 /* If we're in the body, deny prologue calls. */
283 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
284 MDOC_PBODY
& m
->flags
)
285 return(mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
));
287 /* If we're in the prologue, deny "body" macros. */
289 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
290 ! (MDOC_PBODY
& m
->flags
)) {
291 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
))
293 if (NULL
== m
->meta
.title
)
294 m
->meta
.title
= mandoc_strdup("UNKNOWN");
295 if (NULL
== m
->meta
.vol
)
296 m
->meta
.vol
= mandoc_strdup("LOCAL");
297 if (NULL
== m
->meta
.os
)
298 m
->meta
.os
= mandoc_strdup("LOCAL");
299 if (0 == m
->meta
.date
)
300 m
->meta
.date
= time(NULL
);
301 m
->flags
|= MDOC_PBODY
;
304 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
309 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
314 assert(MDOC_ROOT
!= p
->type
);
316 switch (mdoc
->next
) {
317 case (MDOC_NEXT_SIBLING
):
318 mdoc
->last
->next
= p
;
319 p
->prev
= mdoc
->last
;
320 p
->parent
= mdoc
->last
->parent
;
322 case (MDOC_NEXT_CHILD
):
323 mdoc
->last
->child
= p
;
324 p
->parent
= mdoc
->last
;
333 if ( ! mdoc_valid_pre(mdoc
, p
))
335 if ( ! mdoc_action_pre(mdoc
, p
))
340 assert(MDOC_BLOCK
== p
->parent
->type
);
344 assert(MDOC_BLOCK
== p
->parent
->type
);
350 assert(MDOC_BLOCK
== p
->parent
->type
);
361 if ( ! mdoc_valid_post(mdoc
))
363 if ( ! mdoc_action_post(mdoc
))
374 static struct mdoc_node
*
375 node_alloc(struct mdoc
*m
, int line
, int pos
,
376 enum mdoct tok
, enum mdoc_type type
)
380 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
389 if (MDOC_SYNOPSIS
& m
->flags
)
390 p
->flags
|= MDOC_SYNPRETTY
;
392 p
->flags
&= ~MDOC_SYNPRETTY
;
393 if (MDOC_NEWLINE
& m
->flags
)
394 p
->flags
|= MDOC_LINE
;
395 m
->flags
&= ~MDOC_NEWLINE
;
402 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
406 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
407 if ( ! node_append(m
, p
))
409 m
->next
= MDOC_NEXT_CHILD
;
415 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
422 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
423 if ( ! node_append(m
, p
))
425 m
->next
= MDOC_NEXT_CHILD
;
431 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
435 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
436 if ( ! node_append(m
, p
))
438 m
->next
= MDOC_NEXT_CHILD
;
444 mdoc_endbody_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
,
445 struct mdoc_node
*body
, enum mdoc_endbody end
)
449 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
452 if ( ! node_append(m
, p
))
454 m
->next
= MDOC_NEXT_SIBLING
;
460 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
461 enum mdoct tok
, struct mdoc_arg
*args
)
465 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
469 if ( ! node_append(m
, p
))
471 m
->next
= MDOC_NEXT_CHILD
;
477 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
478 enum mdoct tok
, struct mdoc_arg
*args
)
482 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
486 if ( ! node_append(m
, p
))
488 m
->next
= MDOC_NEXT_CHILD
;
494 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
501 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
502 n
->string
= mandoc_malloc(len
+ 1);
503 sv
= strlcpy(n
->string
, p
, len
+ 1);
505 /* Prohibit truncation. */
506 assert(sv
< len
+ 1);
508 if ( ! node_append(m
, n
))
511 m
->next
= MDOC_NEXT_SIBLING
;
517 mdoc_node_free(struct mdoc_node
*p
)
521 * XXX: if these end up being problematic in terms of memory
522 * management and dereferencing freed blocks, then make them
523 * into reference-counted double-pointers.
526 if (MDOC_Bd
== p
->tok
&& MDOC_BLOCK
== p
->type
)
529 if (MDOC_Bl
== p
->tok
&& MDOC_BLOCK
== p
->type
)
532 if (MDOC_Bf
== p
->tok
&& MDOC_HEAD
== p
->type
)
539 mdoc_argv_free(p
->args
);
545 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
548 /* Adjust siblings. */
551 n
->prev
->next
= n
->next
;
553 n
->next
->prev
= n
->prev
;
559 if (n
->parent
->child
== n
)
560 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
563 /* Adjust parse point, if applicable. */
565 if (m
&& m
->last
== n
) {
568 m
->next
= MDOC_NEXT_SIBLING
;
571 m
->next
= MDOC_NEXT_CHILD
;
575 if (m
&& m
->first
== n
)
581 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
586 mdoc_node_delete(m
, p
->child
);
588 assert(0 == p
->nchild
);
590 mdoc_node_unlink(m
, p
);
596 * Parse free-form text, that is, a line that does not begin with the
600 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
605 /* Ignore bogus comments. */
607 if ('\\' == buf
[offs
] &&
608 '.' == buf
[offs
+ 1] &&
609 '"' == buf
[offs
+ 2])
610 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
612 /* No text before an initial macro. */
614 if (SEC_NONE
== m
->lastnamed
)
615 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
621 * Divert directly to list processing if we're encountering a
622 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
623 * (a MDOC_BODY means it's already open, in which case we should
624 * process within its context in the normal way).
627 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
628 LIST_column
== n
->data
.Bl
->type
) {
629 /* `Bl' is open without any children. */
630 m
->flags
|= MDOC_FREECOL
;
631 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
634 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
636 MDOC_Bl
== n
->parent
->tok
&&
637 LIST_column
== n
->parent
->data
.Bl
->type
) {
638 /* `Bl' has block-level `It' children. */
639 m
->flags
|= MDOC_FREECOL
;
640 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
644 * Search for the beginning of unescaped trailing whitespace (ws)
645 * and for the first character not to be output (end).
648 /* FIXME: replace with strcspn(). */
650 for (c
= end
= buf
+ offs
; *c
; c
++) {
653 if (mandoc_hyph(buf
+ offs
, c
))
663 * Always warn about trailing tabs,
664 * even outside literal context,
665 * where they should be put on the next line.
670 * Strip trailing tabs in literal context only;
671 * outside, they affect the next line.
673 if (MDOC_LITERAL
& m
->flags
)
677 /* Skip the escaped character, too, if any. */
690 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
693 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
694 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
698 * Insert a `Pp' in the case of a blank line. Technically,
699 * blank lines aren't allowed, but enough manuals assume this
700 * behaviour that we want to work around it.
702 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_Pp
, NULL
))
705 m
->next
= MDOC_NEXT_SIBLING
;
709 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
712 if (MDOC_LITERAL
& m
->flags
)
716 * End-of-sentence check. If the last character is an unescaped
717 * EOS character, then flag the node as being the end of a
718 * sentence. The front-end will know how to interpret this.
723 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
724 m
->last
->flags
|= MDOC_EOS
;
731 macrowarn(struct mdoc
*m
, int ln
, const char *buf
, int offs
)
735 rc
= mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
736 "unknown macro: %s%s",
737 buf
, strlen(buf
) > 3 ? "..." : "");
739 /* FIXME: logic should be in driver. */
740 /* FIXME: broken, will error out and not omit a message. */
741 return(MDOC_IGN_MACRO
& m
->pflags
? rc
: 0);
746 * Parse a macro line, that is, a line beginning with the control
750 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
757 /* Empty lines are ignored. */
761 if ('\0' == buf
[offs
])
766 /* Accept tabs/whitespace after the initial control char. */
768 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
770 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
779 * Copy the first word into a nil-terminated buffer.
780 * Stop copying when a tab, space, or eoln is encountered.
784 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
788 if (j
== 4 || j
< 2) {
789 if ( ! macrowarn(m
, ln
, mac
, sv
))
794 if (MDOC_MAX
== (tok
= mdoc_hash_find(mac
))) {
795 if ( ! macrowarn(m
, ln
, mac
, sv
))
800 /* Disregard the first trailing tab, if applicable. */
805 /* Jump to the next non-whitespace word. */
807 while (buf
[i
] && ' ' == buf
[i
])
811 * Trailing whitespace. Note that tabs are allowed to be passed
812 * into the parser as "text", so we only warn about spaces here.
815 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
816 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
820 * If an initial macro or a list invocation, divert directly
821 * into macro processing.
824 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
825 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
834 * If the first macro of a `Bl -column', open an `It' block
835 * context around the parsed macro.
838 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
839 LIST_column
== n
->data
.Bl
->type
) {
840 m
->flags
|= MDOC_FREECOL
;
841 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
847 * If we're following a block-level `It' within a `Bl -column'
848 * context (perhaps opened in the above block or in ptext()),
849 * then open an `It' block context around the parsed macro.
852 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
854 MDOC_Bl
== n
->parent
->tok
&&
855 LIST_column
== n
->parent
->data
.Bl
->type
) {
856 m
->flags
|= MDOC_FREECOL
;
857 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
862 /* Normal processing of a macro. */
864 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
869 err
: /* Error out. */
871 m
->flags
|= MDOC_HALT
;