]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
ddc2198cf60a1d404b4e8a352559a389a7349569
1 /* $Id: mdoc.c,v 1.179 2011/02/06 22:05:20 kristaps 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 mdoc_span_alloc(struct mdoc
*,
102 const struct tbl_span
*);
105 const struct mdoc_node
*
106 mdoc_node(const struct mdoc
*m
)
109 assert( ! (MDOC_HALT
& m
->flags
));
114 const struct mdoc_meta
*
115 mdoc_meta(const struct mdoc
*m
)
118 assert( ! (MDOC_HALT
& m
->flags
));
124 * Frees volatile resources (parse tree, meta-data, fields).
127 mdoc_free1(struct mdoc
*mdoc
)
131 mdoc_node_delete(mdoc
, mdoc
->first
);
132 if (mdoc
->meta
.title
)
133 free(mdoc
->meta
.title
);
137 free(mdoc
->meta
.name
);
139 free(mdoc
->meta
.arch
);
141 free(mdoc
->meta
.vol
);
143 free(mdoc
->meta
.msec
);
148 * Allocate all volatile resources (parse tree, meta-data, fields).
151 mdoc_alloc1(struct mdoc
*mdoc
)
154 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
156 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
157 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
158 mdoc
->first
= mdoc
->last
;
159 mdoc
->last
->type
= MDOC_ROOT
;
160 mdoc
->next
= MDOC_NEXT_CHILD
;
165 * Free up volatile resources (see mdoc_free1()) then re-initialises the
166 * data with mdoc_alloc1(). After invocation, parse data has been reset
167 * and the parser is ready for re-invocation on a new tree; however,
168 * cross-parse non-volatile data is kept intact.
171 mdoc_reset(struct mdoc
*mdoc
)
180 * Completely free up all volatile and non-volatile parse resources.
181 * After invocation, the pointer is no longer usable.
184 mdoc_free(struct mdoc
*mdoc
)
193 * Allocate volatile and non-volatile parse resources.
196 mdoc_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
200 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 assert( ! (MDOC_HALT
& m
->flags
));
221 if (mdoc_macroend(m
))
223 m
->flags
|= MDOC_HALT
;
228 mdoc_addspan(struct mdoc
*m
, const struct tbl_span
*sp
)
231 assert( ! (MDOC_HALT
& m
->flags
));
233 /* No text before an initial macro. */
235 if (SEC_NONE
== m
->lastnamed
) {
236 mdoc_pmsg(m
, sp
->line
, 0, MANDOCERR_NOTEXT
);
240 return(mdoc_span_alloc(m
, sp
));
245 * Main parse routine. Parses a single line -- really just hands off to
246 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
249 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
, int offs
)
252 assert( ! (MDOC_HALT
& m
->flags
));
254 m
->flags
|= MDOC_NEWLINE
;
257 * Let the roff nS register switch SYNOPSIS mode early,
258 * such that the parser knows at all times
259 * whether this mode is on or off.
260 * Note that this mode is also switched by the Sh macro.
262 if (m
->regs
->regs
[(int)REG_nS
].set
) {
263 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
264 m
->flags
|= MDOC_SYNOPSIS
;
266 m
->flags
&= ~MDOC_SYNOPSIS
;
269 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
270 mdoc_pmacro(m
, ln
, buf
, offs
) :
271 mdoc_ptext(m
, ln
, buf
, offs
));
276 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
277 int ln
, int pos
, const char *fmt
, ...)
283 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
286 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
291 mdoc_macro(MACRO_PROT_ARGS
)
293 assert(tok
< MDOC_MAX
);
295 /* If we're in the body, deny prologue calls. */
297 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
298 MDOC_PBODY
& m
->flags
) {
299 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
);
303 /* If we're in the prologue, deny "body" macros. */
305 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
306 ! (MDOC_PBODY
& m
->flags
)) {
307 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
);
308 if (NULL
== m
->meta
.msec
)
309 m
->meta
.msec
= mandoc_strdup("1");
310 if (NULL
== m
->meta
.title
)
311 m
->meta
.title
= mandoc_strdup("UNKNOWN");
312 if (NULL
== m
->meta
.vol
)
313 m
->meta
.vol
= mandoc_strdup("LOCAL");
314 if (NULL
== m
->meta
.os
)
315 m
->meta
.os
= mandoc_strdup("LOCAL");
316 if (0 == m
->meta
.date
)
317 m
->meta
.date
= time(NULL
);
318 m
->flags
|= MDOC_PBODY
;
321 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
326 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
331 assert(MDOC_ROOT
!= p
->type
);
333 switch (mdoc
->next
) {
334 case (MDOC_NEXT_SIBLING
):
335 mdoc
->last
->next
= p
;
336 p
->prev
= mdoc
->last
;
337 p
->parent
= mdoc
->last
->parent
;
339 case (MDOC_NEXT_CHILD
):
340 mdoc
->last
->child
= p
;
341 p
->parent
= mdoc
->last
;
351 * Copy over the normalised-data pointer of our parent. Not
352 * everybody has one, but copying a null pointer is fine.
361 p
->norm
= p
->parent
->norm
;
367 if ( ! mdoc_valid_pre(mdoc
, p
))
372 assert(MDOC_BLOCK
== p
->parent
->type
);
376 assert(MDOC_BLOCK
== p
->parent
->type
);
382 assert(MDOC_BLOCK
== p
->parent
->type
);
395 if ( ! mdoc_valid_post(mdoc
))
406 static struct mdoc_node
*
407 node_alloc(struct mdoc
*m
, int line
, int pos
,
408 enum mdoct tok
, enum mdoc_type type
)
412 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
421 if (MDOC_SYNOPSIS
& m
->flags
)
422 p
->flags
|= MDOC_SYNPRETTY
;
424 p
->flags
&= ~MDOC_SYNPRETTY
;
425 if (MDOC_NEWLINE
& m
->flags
)
426 p
->flags
|= MDOC_LINE
;
427 m
->flags
&= ~MDOC_NEWLINE
;
434 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
438 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
439 if ( ! node_append(m
, p
))
441 m
->next
= MDOC_NEXT_CHILD
;
447 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
454 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
455 if ( ! node_append(m
, p
))
457 m
->next
= MDOC_NEXT_CHILD
;
463 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
467 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
468 if ( ! node_append(m
, p
))
470 m
->next
= MDOC_NEXT_CHILD
;
476 mdoc_endbody_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
,
477 struct mdoc_node
*body
, enum mdoc_endbody end
)
481 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
484 if ( ! node_append(m
, p
))
486 m
->next
= MDOC_NEXT_SIBLING
;
492 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
493 enum mdoct tok
, struct mdoc_arg
*args
)
497 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
510 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
516 if ( ! node_append(m
, p
))
518 m
->next
= MDOC_NEXT_CHILD
;
524 mdoc_elem_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_ELEM
);
536 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
542 if ( ! node_append(m
, p
))
544 m
->next
= MDOC_NEXT_CHILD
;
549 mdoc_span_alloc(struct mdoc
*m
, const struct tbl_span
*sp
)
553 n
= node_alloc(m
, sp
->line
, 0, MDOC_MAX
, MDOC_TBL
);
556 if ( ! node_append(m
, n
))
559 m
->next
= MDOC_NEXT_SIBLING
;
565 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
572 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
573 n
->string
= mandoc_malloc(len
+ 1);
574 sv
= strlcpy(n
->string
, p
, len
+ 1);
576 /* Prohibit truncation. */
577 assert(sv
< len
+ 1);
579 if ( ! node_append(m
, n
))
582 m
->next
= MDOC_NEXT_SIBLING
;
588 mdoc_node_free(struct mdoc_node
*p
)
591 if (MDOC_BLOCK
== p
->type
|| MDOC_ELEM
== p
->type
)
596 mdoc_argv_free(p
->args
);
602 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
605 /* Adjust siblings. */
608 n
->prev
->next
= n
->next
;
610 n
->next
->prev
= n
->prev
;
616 if (n
->parent
->child
== n
)
617 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
618 if (n
->parent
->last
== n
)
619 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
622 /* Adjust parse point, if applicable. */
624 if (m
&& m
->last
== n
) {
627 m
->next
= MDOC_NEXT_SIBLING
;
630 m
->next
= MDOC_NEXT_CHILD
;
634 if (m
&& m
->first
== n
)
640 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
645 mdoc_node_delete(m
, p
->child
);
647 assert(0 == p
->nchild
);
649 mdoc_node_unlink(m
, p
);
655 * Parse free-form text, that is, a line that does not begin with the
659 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
664 /* Ignore bogus comments. */
666 if ('\\' == buf
[offs
] &&
667 '.' == buf
[offs
+ 1] &&
668 '"' == buf
[offs
+ 2]) {
669 mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
673 /* No text before an initial macro. */
675 if (SEC_NONE
== m
->lastnamed
) {
676 mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
);
684 * Divert directly to list processing if we're encountering a
685 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
686 * (a MDOC_BODY means it's already open, in which case we should
687 * process within its context in the normal way).
690 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
691 LIST_column
== n
->norm
->Bl
.type
) {
692 /* `Bl' is open without any children. */
693 m
->flags
|= MDOC_FREECOL
;
694 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
697 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
699 MDOC_Bl
== n
->parent
->tok
&&
700 LIST_column
== n
->parent
->norm
->Bl
.type
) {
701 /* `Bl' has block-level `It' children. */
702 m
->flags
|= MDOC_FREECOL
;
703 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
707 * Search for the beginning of unescaped trailing whitespace (ws)
708 * and for the first character not to be output (end).
711 /* FIXME: replace with strcspn(). */
713 for (c
= end
= buf
+ offs
; *c
; c
++) {
716 if (mandoc_hyph(buf
+ offs
, c
))
726 * Always warn about trailing tabs,
727 * even outside literal context,
728 * where they should be put on the next line.
733 * Strip trailing tabs in literal context only;
734 * outside, they affect the next line.
736 if (MDOC_LITERAL
& m
->flags
)
740 /* Skip the escaped character, too, if any. */
753 mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
);
755 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
756 mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
);
759 * Insert a `sp' in the case of a blank line. Technically,
760 * blank lines aren't allowed, but enough manuals assume this
761 * behaviour that we want to work around it.
763 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_sp
, NULL
))
766 m
->next
= MDOC_NEXT_SIBLING
;
770 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
773 if (MDOC_LITERAL
& m
->flags
)
777 * End-of-sentence check. If the last character is an unescaped
778 * EOS character, then flag the node as being the end of a
779 * sentence. The front-end will know how to interpret this.
784 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
785 m
->last
->flags
|= MDOC_EOS
;
792 * Parse a macro line, that is, a line beginning with the control
796 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
803 /* Empty lines are ignored. */
807 if ('\0' == buf
[offs
])
812 /* Accept tabs/whitespace after the initial control char. */
814 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
816 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
825 * Copy the first word into a nil-terminated buffer.
826 * Stop copying when a tab, space, or eoln is encountered.
830 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
834 tok
= (j
> 1 || j
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
835 if (MDOC_MAX
== tok
) {
836 mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, sv
, "%s", buf
+ sv
- 1);
840 /* Disregard the first trailing tab, if applicable. */
845 /* Jump to the next non-whitespace word. */
847 while (buf
[i
] && ' ' == buf
[i
])
851 * Trailing whitespace. Note that tabs are allowed to be passed
852 * into the parser as "text", so we only warn about spaces here.
855 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
856 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
859 * If an initial macro or a list invocation, divert directly
860 * into macro processing.
863 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
864 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
873 * If the first macro of a `Bl -column', open an `It' block
874 * context around the parsed macro.
877 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
878 LIST_column
== n
->norm
->Bl
.type
) {
879 m
->flags
|= MDOC_FREECOL
;
880 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
886 * If we're following a block-level `It' within a `Bl -column'
887 * context (perhaps opened in the above block or in ptext()),
888 * then open an `It' block context around the parsed macro.
891 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
893 MDOC_Bl
== n
->parent
->tok
&&
894 LIST_column
== n
->parent
->norm
->Bl
.type
) {
895 m
->flags
|= MDOC_FREECOL
;
896 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
901 /* Normal processing of a macro. */
903 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
908 err
: /* Error out. */
910 m
->flags
|= MDOC_HALT
;