]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.154 2010/07/01 22:56:17 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #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
;
517 /* FIXME: put in mdoc_node_delete(). */
519 mdoc_node_free(struct mdoc_node
*p
)
522 if (MDOC_Bd
== p
->tok
&& MDOC_BLOCK
== p
->type
)
525 if (MDOC_Bl
== p
->tok
&& MDOC_BLOCK
== p
->type
)
532 mdoc_argv_free(p
->args
);
538 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
541 /* Adjust siblings. */
544 n
->prev
->next
= n
->next
;
546 n
->next
->prev
= n
->prev
;
552 if (n
->parent
->child
== n
)
553 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
556 /* Adjust parse point, if applicable. */
558 if (m
&& m
->last
== n
) {
561 m
->next
= MDOC_NEXT_SIBLING
;
564 m
->next
= MDOC_NEXT_CHILD
;
568 if (m
&& m
->first
== n
)
574 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
579 mdoc_node_delete(m
, p
->child
);
581 assert(0 == p
->nchild
);
583 mdoc_node_unlink(m
, p
);
589 * Parse free-form text, that is, a line that does not begin with the
593 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
, int offs
)
598 /* Ignore bogus comments. */
600 if ('\\' == buf
[offs
] &&
601 '.' == buf
[offs
+ 1] &&
602 '"' == buf
[offs
+ 2])
603 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
605 /* No text before an initial macro. */
607 if (SEC_NONE
== m
->lastnamed
)
608 return(mdoc_pmsg(m
, line
, offs
, MANDOCERR_NOTEXT
));
614 * Divert directly to list processing if we're encountering a
615 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
616 * (a MDOC_BODY means it's already open, in which case we should
617 * process within its context in the normal way).
620 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
621 LIST_column
== n
->data
.Bl
->type
) {
622 /* `Bl' is open without any children. */
623 m
->flags
|= MDOC_FREECOL
;
624 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
627 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
629 MDOC_Bl
== n
->parent
->tok
&&
630 LIST_column
== n
->parent
->data
.Bl
->type
) {
631 /* `Bl' has block-level `It' children. */
632 m
->flags
|= MDOC_FREECOL
;
633 return(mdoc_macro(m
, MDOC_It
, line
, offs
, &offs
, buf
));
637 * Search for the beginning of unescaped trailing whitespace (ws)
638 * and for the first character not to be output (end).
641 /* FIXME: replace with strcspn(). */
643 for (c
= end
= buf
+ offs
; *c
; c
++) {
646 if (mandoc_hyph(buf
+ offs
, c
))
656 * Always warn about trailing tabs,
657 * even outside literal context,
658 * where they should be put on the next line.
663 * Strip trailing tabs in literal context only;
664 * outside, they affect the next line.
666 if (MDOC_LITERAL
& m
->flags
)
670 /* Skip the escaped character, too, if any. */
683 if ( ! mdoc_pmsg(m
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
))
686 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& m
->flags
)) {
687 if ( ! mdoc_pmsg(m
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
))
691 * Insert a `Pp' in the case of a blank line. Technically,
692 * blank lines aren't allowed, but enough manuals assume this
693 * behaviour that we want to work around it.
695 if ( ! mdoc_elem_alloc(m
, line
, offs
, MDOC_Pp
, NULL
))
698 m
->next
= MDOC_NEXT_SIBLING
;
702 if ( ! mdoc_word_alloc(m
, line
, offs
, buf
+offs
))
705 if (MDOC_LITERAL
& m
->flags
)
709 * End-of-sentence check. If the last character is an unescaped
710 * EOS character, then flag the node as being the end of a
711 * sentence. The front-end will know how to interpret this.
716 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
717 m
->last
->flags
|= MDOC_EOS
;
724 macrowarn(struct mdoc
*m
, int ln
, const char *buf
, int offs
)
728 rc
= mdoc_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
729 "unknown macro: %s%s",
730 buf
, strlen(buf
) > 3 ? "..." : "");
732 /* FIXME: logic should be in driver. */
733 /* FIXME: broken, will error out and not omit a message. */
734 return(MDOC_IGN_MACRO
& m
->pflags
? rc
: 0);
739 * Parse a macro line, that is, a line beginning with the control
743 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
, int offs
)
750 /* Empty lines are ignored. */
754 if ('\0' == buf
[offs
])
759 /* Accept whitespace after the initial control char. */
763 while (buf
[i
] && ' ' == buf
[i
])
771 /* Copy the first word into a nil-terminated buffer. */
773 for (j
= 0; j
< 4; j
++, i
++) {
774 if ('\0' == (mac
[j
] = buf
[i
]))
776 else if (' ' == buf
[i
])
779 /* Check for invalid characters. */
781 if (isgraph((u_char
)buf
[i
]))
783 if ( ! mdoc_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
790 if (j
== 4 || j
< 2) {
791 if ( ! macrowarn(m
, ln
, mac
, sv
))
796 if (MDOC_MAX
== (tok
= mdoc_hash_find(mac
))) {
797 if ( ! macrowarn(m
, ln
, mac
, sv
))
802 /* The macro is sane. Jump to the next word. */
804 while (buf
[i
] && ' ' == buf
[i
])
808 * Trailing whitespace. Note that tabs are allowed to be passed
809 * into the parser as "text", so we only warn about spaces here.
812 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
813 if ( ! mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
817 * If an initial macro or a list invocation, divert directly
818 * into macro processing.
821 if (NULL
== m
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
822 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
831 * If the first macro of a `Bl -column', open an `It' block
832 * context around the parsed macro.
835 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
836 LIST_column
== n
->data
.Bl
->type
) {
837 m
->flags
|= MDOC_FREECOL
;
838 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
844 * If we're following a block-level `It' within a `Bl -column'
845 * context (perhaps opened in the above block or in ptext()),
846 * then open an `It' block context around the parsed macro.
849 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
851 MDOC_Bl
== n
->parent
->tok
&&
852 LIST_column
== n
->parent
->data
.Bl
->type
) {
853 m
->flags
|= MDOC_FREECOL
;
854 if ( ! mdoc_macro(m
, MDOC_It
, ln
, sv
, &sv
, buf
))
859 /* Normal processing of a macro. */
861 if ( ! mdoc_macro(m
, tok
, ln
, sv
, &i
, buf
))
866 err
: /* Error out. */
868 m
->flags
|= MDOC_HALT
;