]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.160 2010/08/07 20:33:55 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>
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
;
244 * Let the roff nS register switch SYNOPSIS mode early,
245 * such that the parser knows at all times
246 * whether this mode is on or off.
247 * Note that this mode is also switched by the Sh macro.
249 if (m
->regs
->regs
[(int)REG_nS
].set
) {
250 if (m
->regs
->regs
[(int)REG_nS
].v
.u
)
251 m
->flags
|= MDOC_SYNOPSIS
;
253 m
->flags
&= ~MDOC_SYNOPSIS
;
256 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
257 mdoc_pmacro(m
, ln
, buf
, offs
) :
258 mdoc_ptext(m
, ln
, buf
, offs
));
263 mdoc_vmsg(struct mdoc
*mdoc
, enum mandocerr t
,
264 int ln
, int pos
, const char *fmt
, ...)
270 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
273 return((*mdoc
->msg
)(t
, mdoc
->data
, ln
, pos
, buf
));
278 mdoc_macro(MACRO_PROT_ARGS
)
280 assert(tok
< MDOC_MAX
);
282 /* If we're in the body, deny prologue calls. */
284 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
285 MDOC_PBODY
& m
->flags
)
286 return(mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADBODY
));
288 /* If we're in the prologue, deny "body" macros. */
290 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
291 ! (MDOC_PBODY
& m
->flags
)) {
292 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_BADPROLOG
))
294 if (NULL
== m
->meta
.title
)
295 m
->meta
.title
= mandoc_strdup("UNKNOWN");
296 if (NULL
== m
->meta
.vol
)
297 m
->meta
.vol
= mandoc_strdup("LOCAL");
298 if (NULL
== m
->meta
.os
)
299 m
->meta
.os
= mandoc_strdup("LOCAL");
300 if (0 == m
->meta
.date
)
301 m
->meta
.date
= time(NULL
);
302 m
->flags
|= MDOC_PBODY
;
305 return((*mdoc_macros
[tok
].fp
)(m
, tok
, line
, ppos
, pos
, buf
));
310 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
315 assert(MDOC_ROOT
!= p
->type
);
317 switch (mdoc
->next
) {
318 case (MDOC_NEXT_SIBLING
):
319 mdoc
->last
->next
= p
;
320 p
->prev
= mdoc
->last
;
321 p
->parent
= mdoc
->last
->parent
;
323 case (MDOC_NEXT_CHILD
):
324 mdoc
->last
->child
= p
;
325 p
->parent
= mdoc
->last
;
334 if ( ! mdoc_valid_pre(mdoc
, p
))
336 if ( ! mdoc_action_pre(mdoc
, p
))
341 assert(MDOC_BLOCK
== p
->parent
->type
);
345 assert(MDOC_BLOCK
== p
->parent
->type
);
351 assert(MDOC_BLOCK
== p
->parent
->type
);
362 if ( ! mdoc_valid_post(mdoc
))
364 if ( ! mdoc_action_post(mdoc
))
375 static struct mdoc_node
*
376 node_alloc(struct mdoc
*m
, int line
, int pos
,
377 enum mdoct tok
, enum mdoc_type type
)
381 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
390 if (MDOC_SYNOPSIS
& m
->flags
)
391 p
->flags
|= MDOC_SYNPRETTY
;
393 p
->flags
&= ~MDOC_SYNPRETTY
;
394 if (MDOC_NEWLINE
& m
->flags
)
395 p
->flags
|= MDOC_LINE
;
396 m
->flags
&= ~MDOC_NEWLINE
;
403 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
407 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
408 if ( ! node_append(m
, p
))
410 m
->next
= MDOC_NEXT_CHILD
;
416 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
423 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
424 if ( ! node_append(m
, p
))
426 m
->next
= MDOC_NEXT_CHILD
;
432 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
436 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
437 if ( ! node_append(m
, p
))
439 m
->next
= MDOC_NEXT_CHILD
;
445 mdoc_endbody_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
,
446 struct mdoc_node
*body
, enum mdoc_endbody end
)
450 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
453 if ( ! node_append(m
, p
))
455 m
->next
= MDOC_NEXT_SIBLING
;
461 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
462 enum mdoct tok
, struct mdoc_arg
*args
)
466 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
470 if ( ! node_append(m
, p
))
472 m
->next
= MDOC_NEXT_CHILD
;
478 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
479 enum mdoct tok
, struct mdoc_arg
*args
)
483 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
487 if ( ! node_append(m
, p
))
489 m
->next
= MDOC_NEXT_CHILD
;
495 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
502 n
= node_alloc(m
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
503 n
->string
= mandoc_malloc(len
+ 1);
504 sv
= strlcpy(n
->string
, p
, len
+ 1);
506 /* Prohibit truncation. */
507 assert(sv
< len
+ 1);
509 if ( ! node_append(m
, n
))
512 m
->next
= MDOC_NEXT_SIBLING
;
518 mdoc_node_free(struct mdoc_node
*p
)
522 * XXX: if these end up being problematic in terms of memory
523 * management and dereferencing freed blocks, then make them
524 * into reference-counted double-pointers.
527 if (MDOC_Bd
== p
->tok
&& MDOC_BLOCK
== p
->type
)
530 if (MDOC_Bl
== p
->tok
&& MDOC_BLOCK
== p
->type
)
533 if (MDOC_Bf
== p
->tok
&& MDOC_HEAD
== p
->type
)
540 mdoc_argv_free(p
->args
);
546 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
549 /* Adjust siblings. */
552 n
->prev
->next
= n
->next
;
554 n
->next
->prev
= n
->prev
;
560 if (n
->parent
->child
== n
)
561 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
564 /* Adjust parse point, if applicable. */
566 if (m
&& m
->last
== n
) {
569 m
->next
= MDOC_NEXT_SIBLING
;
572 m
->next
= MDOC_NEXT_CHILD
;
576 if (m
&& m
->first
== n
)
582 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
587 mdoc_node_delete(m
, p
->child
);
589 assert(0 == p
->nchild
);
591 mdoc_node_unlink(m
, p
);
597 * Parse free-form text, that is, a line that does not begin with the
601 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
606 /* Ignore bogus comments. */
608 if ('\\' == buf
[offs
] &&
609 '.' == buf
[offs
+ 1] &&
610 '"' == buf
[offs
+ 2])
611 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
613 /* No text before an initial macro. */
615 if (SEC_NONE
== m
->lastnamed
)
616 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
622 * Divert directly to list processing if we're encountering a
623 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
624 * (a MDOC_BODY means it's already open, in which case we should
625 * process within its context in the normal way).
628 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
629 LIST_column
== n
->data
.Bl
->type
) {
630 /* `Bl' is open without any children. */
631 m
->flags
|= MDOC_FREECOL
;
632 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
635 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
637 MDOC_Bl
== n
->parent
->tok
&&
638 LIST_column
== n
->parent
->data
.Bl
->type
) {
639 /* `Bl' has block-level `It' children. */
640 m
->flags
|= MDOC_FREECOL
;
641 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
645 * Search for the beginning of unescaped trailing whitespace (ws)
646 * and for the first character not to be output (end).
649 /* FIXME: replace with strcspn(). */
651 for (c
= end
= buf
+ offs
; *c
; c
++) {
654 if (mandoc_hyph(buf
+ offs
, c
))
664 * Always warn about trailing tabs,
665 * even outside literal context,
666 * where they should be put on the next line.
671 * Strip trailing tabs in literal context only;
672 * outside, they affect the next line.
674 if (MDOC_LITERAL
& m
->flags
)
678 /* Skip the escaped character, too, if any. */
691 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
694 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
695 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
699 * Insert a `Pp' in the case of a blank line. Technically,
700 * blank lines aren't allowed, but enough manuals assume this
701 * behaviour that we want to work around it.
703 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_Pp
, NULL
))
706 m
->next
= MDOC_NEXT_SIBLING
;
710 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
713 if (MDOC_LITERAL
& m
->flags
)
717 * End-of-sentence check. If the last character is an unescaped
718 * EOS character, then flag the node as being the end of a
719 * sentence. The front-end will know how to interpret this.
724 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
725 m
->last
->flags
|= MDOC_EOS
;
732 macrowarn(struct mdoc
*m
, int ln
, const char *buf
, int offs
)
736 rc
= mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
737 "unknown macro: %s%s",
738 buf
, strlen(buf
) > 3 ? "..." : "");
740 /* FIXME: logic should be in driver. */
741 /* FIXME: broken, will error out and not omit a message. */
742 return(MDOC_IGN_MACRO
& m
->pflags
? rc
: 0);
747 * Parse a macro line, that is, a line beginning with the control
751 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
758 /* Empty lines are ignored. */
762 if ('\0' == buf
[offs
])
767 /* Accept tabs/whitespace after the initial control char. */
769 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
771 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
780 * Copy the first word into a nil-terminated buffer. Stop
781 * copying when a tab, space, or eoln is encountered.
784 for (j
= 0; j
< 4; j
++, i
++) {
785 if ('\0' == (mac
[j
] = buf
[i
]))
787 else if (' ' == buf
[i
] || '\t' == buf
[i
])
790 /* Check for invalid characters. */
791 /* TODO: remove me, already done in main.c. */
793 if (isgraph((u_char
)buf
[i
]))
795 if ( ! mdoc_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
802 if (j
== 4 || j
< 2) {
803 if ( ! macrowarn(m
, ln
, mac
, sv
))
808 if (MDOC_MAX
== (tok
= mdoc_hash_find(mac
))) {
809 if ( ! macrowarn(m
, ln
, mac
, sv
))
814 /* Disregard the first trailing tab, if applicable. */
819 /* Jump to the next non-whitespace word. */
821 while (buf
[i
] && ' ' == buf
[i
])
825 * Trailing whitespace. Note that tabs are allowed to be passed
826 * into the parser as "text", so we only warn about spaces here.
829 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
830 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
834 * If an initial macro or a list invocation, divert directly
835 * into macro processing.
838 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
839 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
848 * If the first macro of a `Bl -column', open an `It' block
849 * context around the parsed macro.
852 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
853 LIST_column
== n
->data
.Bl
->type
) {
854 m
->flags
|= MDOC_FREECOL
;
855 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
861 * If we're following a block-level `It' within a `Bl -column'
862 * context (perhaps opened in the above block or in ptext()),
863 * then open an `It' block context around the parsed macro.
866 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
868 MDOC_Bl
== n
->parent
->tok
&&
869 LIST_column
== n
->parent
->data
.Bl
->type
) {
870 m
->flags
|= MDOC_FREECOL
;
871 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
876 /* Normal processing of a macro. */
878 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
883 err
: /* Error out. */
885 m
->flags
|= MDOC_HALT
;