]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.204 2013/10/05 22:08:12 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012 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
*,
101 static int mdoc_preptext(struct mdoc
*, int, char *, int);
103 static int mdoc_ptext(struct mdoc
*, int, char *, int);
104 static int mdoc_pmacro(struct mdoc
*, int, char *, int);
106 const struct mdoc_node
*
107 mdoc_node(const struct mdoc
*mdoc
)
110 assert( ! (MDOC_HALT
& mdoc
->flags
));
115 const struct mdoc_meta
*
116 mdoc_meta(const struct mdoc
*mdoc
)
119 assert( ! (MDOC_HALT
& mdoc
->flags
));
125 * Frees volatile resources (parse tree, meta-data, fields).
128 mdoc_free1(struct mdoc
*mdoc
)
132 mdoc_node_delete(mdoc
, mdoc
->first
);
133 if (mdoc
->meta
.title
)
134 free(mdoc
->meta
.title
);
138 free(mdoc
->meta
.name
);
140 free(mdoc
->meta
.arch
);
142 free(mdoc
->meta
.vol
);
144 free(mdoc
->meta
.msec
);
146 free(mdoc
->meta
.date
);
151 * Allocate all volatile resources (parse tree, meta-data, fields).
154 mdoc_alloc1(struct mdoc
*mdoc
)
157 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
159 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
160 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
161 mdoc
->first
= mdoc
->last
;
162 mdoc
->last
->type
= MDOC_ROOT
;
163 mdoc
->last
->tok
= MDOC_MAX
;
164 mdoc
->next
= MDOC_NEXT_CHILD
;
169 * Free up volatile resources (see mdoc_free1()) then re-initialises the
170 * data with mdoc_alloc1(). After invocation, parse data has been reset
171 * and the parser is ready for re-invocation on a new tree; however,
172 * cross-parse non-volatile data is kept intact.
175 mdoc_reset(struct mdoc
*mdoc
)
184 * Completely free up all volatile and non-volatile parse resources.
185 * After invocation, the pointer is no longer usable.
188 mdoc_free(struct mdoc
*mdoc
)
197 * Allocate volatile and non-volatile parse resources.
200 mdoc_alloc(struct roff
*roff
, struct mparse
*parse
, char *defos
)
204 p
= mandoc_calloc(1, sizeof(struct mdoc
));
217 * Climb back up the parse tree, validating open scopes. Mostly calls
218 * through to macro_end() in macro.c.
221 mdoc_endparse(struct mdoc
*mdoc
)
224 assert( ! (MDOC_HALT
& mdoc
->flags
));
225 if (mdoc_macroend(mdoc
))
227 mdoc
->flags
|= MDOC_HALT
;
232 mdoc_addeqn(struct mdoc
*mdoc
, const struct eqn
*ep
)
236 assert( ! (MDOC_HALT
& mdoc
->flags
));
238 /* No text before an initial macro. */
240 if (SEC_NONE
== mdoc
->lastnamed
) {
241 mdoc_pmsg(mdoc
, ep
->ln
, ep
->pos
, MANDOCERR_NOTEXT
);
245 n
= node_alloc(mdoc
, ep
->ln
, ep
->pos
, MDOC_MAX
, MDOC_EQN
);
248 if ( ! node_append(mdoc
, n
))
251 mdoc
->next
= MDOC_NEXT_SIBLING
;
256 mdoc_addspan(struct mdoc
*mdoc
, const struct tbl_span
*sp
)
260 assert( ! (MDOC_HALT
& mdoc
->flags
));
262 /* No text before an initial macro. */
264 if (SEC_NONE
== mdoc
->lastnamed
) {
265 mdoc_pmsg(mdoc
, sp
->line
, 0, MANDOCERR_NOTEXT
);
269 n
= node_alloc(mdoc
, sp
->line
, 0, MDOC_MAX
, MDOC_TBL
);
272 if ( ! node_append(mdoc
, n
))
275 mdoc
->next
= MDOC_NEXT_SIBLING
;
281 * Main parse routine. Parses a single line -- really just hands off to
282 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
285 mdoc_parseln(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
288 assert( ! (MDOC_HALT
& mdoc
->flags
));
290 mdoc
->flags
|= MDOC_NEWLINE
;
293 * Let the roff nS register switch SYNOPSIS mode early,
294 * such that the parser knows at all times
295 * whether this mode is on or off.
296 * Note that this mode is also switched by the Sh macro.
298 if (roff_getreg(mdoc
->roff
, "nS"))
299 mdoc
->flags
|= MDOC_SYNOPSIS
;
301 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
303 return(roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
304 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
305 mdoc_ptext(mdoc
, ln
, buf
, offs
));
309 mdoc_macro(MACRO_PROT_ARGS
)
311 assert(tok
< MDOC_MAX
);
313 /* If we're in the body, deny prologue calls. */
315 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
316 MDOC_PBODY
& mdoc
->flags
) {
317 mdoc_pmsg(mdoc
, line
, ppos
, MANDOCERR_BADBODY
);
321 /* If we're in the prologue, deny "body" macros. */
323 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
324 ! (MDOC_PBODY
& mdoc
->flags
)) {
325 mdoc_pmsg(mdoc
, line
, ppos
, MANDOCERR_BADPROLOG
);
326 if (NULL
== mdoc
->meta
.msec
)
327 mdoc
->meta
.msec
= mandoc_strdup("1");
328 if (NULL
== mdoc
->meta
.title
)
329 mdoc
->meta
.title
= mandoc_strdup("UNKNOWN");
330 if (NULL
== mdoc
->meta
.vol
)
331 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
332 if (NULL
== mdoc
->meta
.os
)
333 mdoc
->meta
.os
= mandoc_strdup("LOCAL");
334 if (NULL
== mdoc
->meta
.date
)
335 mdoc
->meta
.date
= mandoc_normdate
336 (mdoc
->parse
, NULL
, line
, ppos
);
337 mdoc
->flags
|= MDOC_PBODY
;
340 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, line
, ppos
, pos
, buf
));
345 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
350 assert(MDOC_ROOT
!= p
->type
);
352 switch (mdoc
->next
) {
353 case (MDOC_NEXT_SIBLING
):
354 mdoc
->last
->next
= p
;
355 p
->prev
= mdoc
->last
;
356 p
->parent
= mdoc
->last
->parent
;
358 case (MDOC_NEXT_CHILD
):
359 mdoc
->last
->child
= p
;
360 p
->parent
= mdoc
->last
;
370 * Copy over the normalised-data pointer of our parent. Not
371 * everybody has one, but copying a null pointer is fine.
376 if (ENDBODY_NOT
!= p
->end
)
382 p
->norm
= p
->parent
->norm
;
388 if ( ! mdoc_valid_pre(mdoc
, p
))
393 assert(MDOC_BLOCK
== p
->parent
->type
);
397 assert(MDOC_BLOCK
== p
->parent
->type
);
403 assert(MDOC_BLOCK
== p
->parent
->type
);
416 if ( ! mdoc_valid_post(mdoc
))
427 static struct mdoc_node
*
428 node_alloc(struct mdoc
*mdoc
, int line
, int pos
,
429 enum mdoct tok
, enum mdoc_type type
)
433 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
434 p
->sec
= mdoc
->lastsec
;
442 if (MDOC_SYNOPSIS
& mdoc
->flags
)
443 p
->flags
|= MDOC_SYNPRETTY
;
445 p
->flags
&= ~MDOC_SYNPRETTY
;
446 if (MDOC_NEWLINE
& mdoc
->flags
)
447 p
->flags
|= MDOC_LINE
;
448 mdoc
->flags
&= ~MDOC_NEWLINE
;
455 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
459 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_TAIL
);
460 if ( ! node_append(mdoc
, p
))
462 mdoc
->next
= MDOC_NEXT_CHILD
;
468 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
475 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_HEAD
);
476 if ( ! node_append(mdoc
, p
))
478 mdoc
->next
= MDOC_NEXT_CHILD
;
484 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
488 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
489 if ( ! node_append(mdoc
, p
))
491 mdoc
->next
= MDOC_NEXT_CHILD
;
497 mdoc_endbody_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
,
498 struct mdoc_node
*body
, enum mdoc_endbody end
)
502 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
504 p
->norm
= body
->norm
;
506 if ( ! node_append(mdoc
, p
))
508 mdoc
->next
= MDOC_NEXT_SIBLING
;
514 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
515 enum mdoct tok
, struct mdoc_arg
*args
)
519 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BLOCK
);
532 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
538 if ( ! node_append(mdoc
, p
))
540 mdoc
->next
= MDOC_NEXT_CHILD
;
546 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
547 enum mdoct tok
, struct mdoc_arg
*args
)
551 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_ELEM
);
558 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
564 if ( ! node_append(mdoc
, p
))
566 mdoc
->next
= MDOC_NEXT_CHILD
;
571 mdoc_word_alloc(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
575 n
= node_alloc(mdoc
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
576 n
->string
= roff_strdup(mdoc
->roff
, p
);
578 if ( ! node_append(mdoc
, n
))
581 mdoc
->next
= MDOC_NEXT_SIBLING
;
587 mdoc_node_free(struct mdoc_node
*p
)
590 if (MDOC_BLOCK
== p
->type
|| MDOC_ELEM
== p
->type
)
595 mdoc_argv_free(p
->args
);
601 mdoc_node_unlink(struct mdoc
*mdoc
, struct mdoc_node
*n
)
604 /* Adjust siblings. */
607 n
->prev
->next
= n
->next
;
609 n
->next
->prev
= n
->prev
;
615 if (n
->parent
->child
== n
)
616 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
617 if (n
->parent
->last
== n
)
618 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
621 /* Adjust parse point, if applicable. */
623 if (mdoc
&& mdoc
->last
== n
) {
625 mdoc
->last
= n
->prev
;
626 mdoc
->next
= MDOC_NEXT_SIBLING
;
628 mdoc
->last
= n
->parent
;
629 mdoc
->next
= MDOC_NEXT_CHILD
;
633 if (mdoc
&& mdoc
->first
== n
)
639 mdoc_node_delete(struct mdoc
*mdoc
, struct mdoc_node
*p
)
644 mdoc_node_delete(mdoc
, p
->child
);
646 assert(0 == p
->nchild
);
648 mdoc_node_unlink(mdoc
, p
);
653 mdoc_node_relink(struct mdoc
*mdoc
, struct mdoc_node
*p
)
656 mdoc_node_unlink(mdoc
, p
);
657 return(node_append(mdoc
, p
));
662 * Pre-treat a text line.
663 * Text lines can consist of equations, which must be handled apart from
665 * Thus, use this function to step through a line checking if it has any
666 * equations embedded in it.
667 * This must handle multiple equations AND equations that do not end at
668 * the end-of-line, i.e., will re-enter in the next roff parse.
671 mdoc_preptext(struct mdoc
*mdoc
, int line
, char *buf
, int offs
)
676 while ('\0' != buf
[offs
]) {
677 /* Mark starting position if eqn is set. */
679 if ('\0' != (delim
= roff_eqndelim(mdoc
->roff
)))
680 if (NULL
!= (start
= strchr(buf
+ offs
, delim
)))
683 /* Parse text as normal. */
684 if ( ! mdoc_ptext(mdoc
, line
, buf
, offs
))
687 /* Continue only if an equation exists. */
691 /* Read past the end of the equation. */
692 offs
+= start
- (buf
+ offs
);
693 assert(start
== &buf
[offs
]);
694 if (NULL
!= (end
= strchr(buf
+ offs
, delim
))) {
700 /* Parse the equation itself. */
701 roff_openeqn(mdoc
->roff
, NULL
, line
, offs
, buf
);
703 /* Process a finished equation? */
704 if (roff_closeeqn(mdoc
->roff
))
705 if ( ! mdoc_addeqn(mdoc
, roff_eqn(mdoc
->roff
)))
707 offs
+= (end
- (buf
+ offs
));
715 * Parse free-form text, that is, a line that does not begin with the
719 mdoc_ptext(struct mdoc
*mdoc
, int line
, char *buf
, int offs
)
724 /* No text before an initial macro. */
726 if (SEC_NONE
== mdoc
->lastnamed
) {
727 mdoc_pmsg(mdoc
, line
, offs
, MANDOCERR_NOTEXT
);
735 * Divert directly to list processing if we're encountering a
736 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
737 * (a MDOC_BODY means it's already open, in which case we should
738 * process within its context in the normal way).
741 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
742 LIST_column
== n
->norm
->Bl
.type
) {
743 /* `Bl' is open without any children. */
744 mdoc
->flags
|= MDOC_FREECOL
;
745 return(mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
));
748 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
750 MDOC_Bl
== n
->parent
->tok
&&
751 LIST_column
== n
->parent
->norm
->Bl
.type
) {
752 /* `Bl' has block-level `It' children. */
753 mdoc
->flags
|= MDOC_FREECOL
;
754 return(mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
));
758 * Search for the beginning of unescaped trailing whitespace (ws)
759 * and for the first character not to be output (end).
762 /* FIXME: replace with strcspn(). */
764 for (c
= end
= buf
+ offs
; *c
; c
++) {
772 * Always warn about trailing tabs,
773 * even outside literal context,
774 * where they should be put on the next line.
779 * Strip trailing tabs in literal context only;
780 * outside, they affect the next line.
782 if (MDOC_LITERAL
& mdoc
->flags
)
786 /* Skip the escaped character, too, if any. */
799 mdoc_pmsg(mdoc
, line
, (int)(ws
-buf
), MANDOCERR_EOLNSPACE
);
801 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& mdoc
->flags
)) {
802 mdoc_pmsg(mdoc
, line
, (int)(c
-buf
), MANDOCERR_NOBLANKLN
);
805 * Insert a `sp' in the case of a blank line. Technically,
806 * blank lines aren't allowed, but enough manuals assume this
807 * behaviour that we want to work around it.
809 if ( ! mdoc_elem_alloc(mdoc
, line
, offs
, MDOC_sp
, NULL
))
812 mdoc
->next
= MDOC_NEXT_SIBLING
;
814 return(mdoc_valid_post(mdoc
));
817 if ( ! mdoc_word_alloc(mdoc
, line
, offs
, buf
+offs
))
820 if (MDOC_LITERAL
& mdoc
->flags
)
824 * End-of-sentence check. If the last character is an unescaped
825 * EOS character, then flag the node as being the end of a
826 * sentence. The front-end will know how to interpret this.
831 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
), 0))
832 mdoc
->last
->flags
|= MDOC_EOS
;
839 * Parse a macro line, that is, a line beginning with the control
843 mdoc_pmacro(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
850 /* Empty post-control lines are ignored. */
852 if ('"' == buf
[offs
]) {
853 mdoc_pmsg(mdoc
, ln
, offs
, MANDOCERR_BADCOMMENT
);
855 } else if ('\0' == buf
[offs
])
861 * Copy the first word into a nil-terminated buffer.
862 * Stop copying when a tab, space, or eoln is encountered.
866 while (i
< 4 && '\0' != buf
[offs
] &&
867 ' ' != buf
[offs
] && '\t' != buf
[offs
])
868 mac
[i
++] = buf
[offs
++];
872 tok
= (i
> 1 || i
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
874 if (MDOC_MAX
== tok
) {
875 mandoc_vmsg(MANDOCERR_MACRO
, mdoc
->parse
,
876 ln
, sv
, "%s", buf
+ sv
- 1);
880 /* Disregard the first trailing tab, if applicable. */
882 if ('\t' == buf
[offs
])
885 /* Jump to the next non-whitespace word. */
887 while (buf
[offs
] && ' ' == buf
[offs
])
891 * Trailing whitespace. Note that tabs are allowed to be passed
892 * into the parser as "text", so we only warn about spaces here.
895 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
896 mdoc_pmsg(mdoc
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
899 * If an initial macro or a list invocation, divert directly
900 * into macro processing.
903 if (NULL
== mdoc
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
904 if ( ! mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
))
913 * If the first macro of a `Bl -column', open an `It' block
914 * context around the parsed macro.
917 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
918 LIST_column
== n
->norm
->Bl
.type
) {
919 mdoc
->flags
|= MDOC_FREECOL
;
920 if ( ! mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
))
926 * If we're following a block-level `It' within a `Bl -column'
927 * context (perhaps opened in the above block or in ptext()),
928 * then open an `It' block context around the parsed macro.
931 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
933 MDOC_Bl
== n
->parent
->tok
&&
934 LIST_column
== n
->parent
->norm
->Bl
.type
) {
935 mdoc
->flags
|= MDOC_FREECOL
;
936 if ( ! mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
))
941 /* Normal processing of a macro. */
943 if ( ! mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
))
948 err
: /* Error out. */
950 mdoc
->flags
|= MDOC_HALT
;
955 mdoc_isdelim(const char *p
)
968 return(DELIM_MIDDLE
);
992 if (0 == strcmp(p
+ 1, "."))
994 if (0 == strcmp(p
+ 1, "fR|\\fP"))
995 return(DELIM_MIDDLE
);