]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.225 2014/09/06 23:24:32 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 const char *const __mdoc_macronames
[MDOC_MAX
+ 1] = {
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",
47 "Xr", "%A", "%B", "%D",
48 "%I", "%J", "%N", "%O",
49 "%P", "%R", "%T", "%V",
50 "Ac", "Ao", "Aq", "At",
51 "Bc", "Bf", "Bo", "Bq",
52 "Bsx", "Bx", "Db", "Dc",
53 "Do", "Dq", "Ec", "Ef",
54 "Em", "Eo", "Fx", "Ms",
55 "No", "Ns", "Nx", "Ox",
56 "Pc", "Pf", "Po", "Pq",
57 "Qc", "Ql", "Qo", "Qq",
58 "Re", "Rs", "Sc", "So",
59 "Sq", "Sm", "Sx", "Sy",
60 "Tn", "Ux", "Xc", "Xo",
61 "Fo", "Fc", "Oo", "Oc",
62 "Bk", "Ek", "Bt", "Hf",
63 "Fr", "Ud", "Lb", "Lp",
64 "Lk", "Mt", "Brq", "Bro",
65 "Brc", "%C", "Es", "En",
66 "Dx", "%Q", "br", "sp",
67 "%U", "Ta", "ll", "text",
70 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
71 "split", "nosplit", "ragged",
72 "unfilled", "literal", "file",
73 "offset", "bullet", "dash",
74 "hyphen", "item", "enum",
75 "tag", "diag", "hang",
76 "ohang", "inset", "column",
77 "width", "compact", "std",
78 "filled", "words", "emphasis",
79 "symbolic", "nested", "centered"
82 const char * const *mdoc_macronames
= __mdoc_macronames
;
83 const char * const *mdoc_argnames
= __mdoc_argnames
;
85 static void mdoc_node_free(struct mdoc_node
*);
86 static void mdoc_node_unlink(struct mdoc
*,
88 static void mdoc_free1(struct mdoc
*);
89 static void mdoc_alloc1(struct mdoc
*);
90 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
91 enum mdoct
, enum mdoc_type
);
92 static int node_append(struct mdoc
*,
95 static int mdoc_preptext(struct mdoc
*, int, char *, int);
97 static int mdoc_ptext(struct mdoc
*, int, char *, int);
98 static int mdoc_pmacro(struct mdoc
*, int, char *, int);
101 const struct mdoc_node
*
102 mdoc_node(const struct mdoc
*mdoc
)
108 const struct mdoc_meta
*
109 mdoc_meta(const struct mdoc
*mdoc
)
116 * Frees volatile resources (parse tree, meta-data, fields).
119 mdoc_free1(struct mdoc
*mdoc
)
123 mdoc_node_delete(mdoc
, mdoc
->first
);
124 free(mdoc
->meta
.msec
);
125 free(mdoc
->meta
.vol
);
126 free(mdoc
->meta
.arch
);
127 free(mdoc
->meta
.date
);
128 free(mdoc
->meta
.title
);
130 free(mdoc
->meta
.name
);
134 * Allocate all volatile resources (parse tree, meta-data, fields).
137 mdoc_alloc1(struct mdoc
*mdoc
)
140 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
142 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
143 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
144 mdoc
->first
= mdoc
->last
;
145 mdoc
->last
->type
= MDOC_ROOT
;
146 mdoc
->last
->tok
= MDOC_MAX
;
147 mdoc
->next
= MDOC_NEXT_CHILD
;
151 * Free up volatile resources (see mdoc_free1()) then re-initialises the
152 * data with mdoc_alloc1(). After invocation, parse data has been reset
153 * and the parser is ready for re-invocation on a new tree; however,
154 * cross-parse non-volatile data is kept intact.
157 mdoc_reset(struct mdoc
*mdoc
)
165 * Completely free up all volatile and non-volatile parse resources.
166 * After invocation, the pointer is no longer usable.
169 mdoc_free(struct mdoc
*mdoc
)
177 * Allocate volatile and non-volatile parse resources.
180 mdoc_alloc(struct roff
*roff
, struct mparse
*parse
,
181 const char *defos
, int quick
)
185 p
= mandoc_calloc(1, sizeof(struct mdoc
));
198 mdoc_endparse(struct mdoc
*mdoc
)
201 return(mdoc_macroend(mdoc
));
205 mdoc_addeqn(struct mdoc
*mdoc
, const struct eqn
*ep
)
209 n
= node_alloc(mdoc
, ep
->ln
, ep
->pos
, MDOC_MAX
, MDOC_EQN
);
212 if ( ! node_append(mdoc
, n
))
215 mdoc
->next
= MDOC_NEXT_SIBLING
;
220 mdoc_addspan(struct mdoc
*mdoc
, const struct tbl_span
*sp
)
224 n
= node_alloc(mdoc
, sp
->line
, 0, MDOC_MAX
, MDOC_TBL
);
227 if ( ! node_append(mdoc
, n
))
230 mdoc
->next
= MDOC_NEXT_SIBLING
;
235 * Main parse routine. Parses a single line -- really just hands off to
236 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
239 mdoc_parseln(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
242 mdoc
->flags
|= MDOC_NEWLINE
;
245 * Let the roff nS register switch SYNOPSIS mode early,
246 * such that the parser knows at all times
247 * whether this mode is on or off.
248 * Note that this mode is also switched by the Sh macro.
250 if (roff_getreg(mdoc
->roff
, "nS"))
251 mdoc
->flags
|= MDOC_SYNOPSIS
;
253 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
255 return(roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
256 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
257 mdoc_ptext(mdoc
, ln
, buf
, offs
));
261 mdoc_macro(MACRO_PROT_ARGS
)
263 assert(tok
< MDOC_MAX
);
265 if (mdoc
->flags
& MDOC_PBODY
) {
266 if (tok
== MDOC_Dt
) {
267 mandoc_vmsg(MANDOCERR_DT_LATE
,
268 mdoc
->parse
, line
, ppos
,
269 "Dt %s", buf
+ *pos
);
272 } else if ( ! (mdoc_macros
[tok
].flags
& MDOC_PROLOGUE
)) {
273 if (mdoc
->meta
.title
== NULL
) {
274 mandoc_vmsg(MANDOCERR_DT_NOTITLE
,
275 mdoc
->parse
, line
, ppos
, "%s %s",
276 mdoc_macronames
[tok
], buf
+ *pos
);
277 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
279 if (NULL
== mdoc
->meta
.vol
)
280 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
281 mdoc
->flags
|= MDOC_PBODY
;
284 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, line
, ppos
, pos
, buf
));
289 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
294 assert(MDOC_ROOT
!= p
->type
);
296 switch (mdoc
->next
) {
297 case MDOC_NEXT_SIBLING
:
298 mdoc
->last
->next
= p
;
299 p
->prev
= mdoc
->last
;
300 p
->parent
= mdoc
->last
->parent
;
302 case MDOC_NEXT_CHILD
:
303 mdoc
->last
->child
= p
;
304 p
->parent
= mdoc
->last
;
314 * Copy over the normalised-data pointer of our parent. Not
315 * everybody has one, but copying a null pointer is fine.
320 if (ENDBODY_NOT
!= p
->end
)
326 p
->norm
= p
->parent
->norm
;
332 if ( ! mdoc_valid_pre(mdoc
, p
))
337 assert(MDOC_BLOCK
== p
->parent
->type
);
341 assert(MDOC_BLOCK
== p
->parent
->type
);
347 assert(MDOC_BLOCK
== p
->parent
->type
);
360 if ( ! mdoc_valid_post(mdoc
))
370 static struct mdoc_node
*
371 node_alloc(struct mdoc
*mdoc
, int line
, int pos
,
372 enum mdoct tok
, enum mdoc_type type
)
376 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
377 p
->sec
= mdoc
->lastsec
;
386 if (MDOC_SYNOPSIS
& mdoc
->flags
)
387 p
->flags
|= MDOC_SYNPRETTY
;
389 p
->flags
&= ~MDOC_SYNPRETTY
;
390 if (MDOC_NEWLINE
& mdoc
->flags
)
391 p
->flags
|= MDOC_LINE
;
392 mdoc
->flags
&= ~MDOC_NEWLINE
;
398 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
402 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_TAIL
);
403 if ( ! node_append(mdoc
, p
))
405 mdoc
->next
= MDOC_NEXT_CHILD
;
410 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
417 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_HEAD
);
418 if ( ! node_append(mdoc
, p
))
420 mdoc
->next
= MDOC_NEXT_CHILD
;
425 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
429 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
430 if ( ! node_append(mdoc
, p
))
432 mdoc
->next
= MDOC_NEXT_CHILD
;
437 mdoc_endbody_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
,
438 struct mdoc_node
*body
, enum mdoc_endbody end
)
442 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
444 p
->norm
= body
->norm
;
446 if ( ! node_append(mdoc
, p
))
448 mdoc
->next
= MDOC_NEXT_SIBLING
;
453 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
454 enum mdoct tok
, struct mdoc_arg
*args
)
458 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BLOCK
);
473 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
479 if ( ! node_append(mdoc
, p
))
481 mdoc
->next
= MDOC_NEXT_CHILD
;
486 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
487 enum mdoct tok
, struct mdoc_arg
*args
)
491 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_ELEM
);
498 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
504 if ( ! node_append(mdoc
, p
))
506 mdoc
->next
= MDOC_NEXT_CHILD
;
511 mdoc_word_alloc(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
515 n
= node_alloc(mdoc
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
516 n
->string
= roff_strdup(mdoc
->roff
, p
);
518 if ( ! node_append(mdoc
, n
))
521 mdoc
->next
= MDOC_NEXT_SIBLING
;
526 mdoc_word_append(struct mdoc
*mdoc
, const char *p
)
529 char *addstr
, *newstr
;
532 addstr
= roff_strdup(mdoc
->roff
, p
);
533 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
537 mdoc
->next
= MDOC_NEXT_SIBLING
;
541 mdoc_node_free(struct mdoc_node
*p
)
544 if (MDOC_BLOCK
== p
->type
|| MDOC_ELEM
== p
->type
)
549 mdoc_argv_free(p
->args
);
554 mdoc_node_unlink(struct mdoc
*mdoc
, struct mdoc_node
*n
)
557 /* Adjust siblings. */
560 n
->prev
->next
= n
->next
;
562 n
->next
->prev
= n
->prev
;
568 if (n
->parent
->child
== n
)
569 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
570 if (n
->parent
->last
== n
)
571 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
574 /* Adjust parse point, if applicable. */
576 if (mdoc
&& mdoc
->last
== n
) {
578 mdoc
->last
= n
->prev
;
579 mdoc
->next
= MDOC_NEXT_SIBLING
;
581 mdoc
->last
= n
->parent
;
582 mdoc
->next
= MDOC_NEXT_CHILD
;
586 if (mdoc
&& mdoc
->first
== n
)
591 mdoc_node_delete(struct mdoc
*mdoc
, struct mdoc_node
*p
)
596 mdoc_node_delete(mdoc
, p
->child
);
598 assert(0 == p
->nchild
);
600 mdoc_node_unlink(mdoc
, p
);
605 mdoc_node_relink(struct mdoc
*mdoc
, struct mdoc_node
*p
)
608 mdoc_node_unlink(mdoc
, p
);
609 return(node_append(mdoc
, p
));
614 * Pre-treat a text line.
615 * Text lines can consist of equations, which must be handled apart from
617 * Thus, use this function to step through a line checking if it has any
618 * equations embedded in it.
619 * This must handle multiple equations AND equations that do not end at
620 * the end-of-line, i.e., will re-enter in the next roff parse.
623 mdoc_preptext(struct mdoc
*mdoc
, int line
, char *buf
, int offs
)
628 while ('\0' != buf
[offs
]) {
629 /* Mark starting position if eqn is set. */
631 if ('\0' != (delim
= roff_eqndelim(mdoc
->roff
)))
632 if (NULL
!= (start
= strchr(buf
+ offs
, delim
)))
635 /* Parse text as normal. */
636 if ( ! mdoc_ptext(mdoc
, line
, buf
, offs
))
639 /* Continue only if an equation exists. */
643 /* Read past the end of the equation. */
644 offs
+= start
- (buf
+ offs
);
645 assert(start
== &buf
[offs
]);
646 if (NULL
!= (end
= strchr(buf
+ offs
, delim
))) {
652 /* Parse the equation itself. */
653 roff_openeqn(mdoc
->roff
, NULL
, line
, offs
, buf
);
655 /* Process a finished equation? */
656 if (roff_closeeqn(mdoc
->roff
))
657 if ( ! mdoc_addeqn(mdoc
, roff_eqn(mdoc
->roff
)))
659 offs
+= (end
- (buf
+ offs
));
667 * Parse free-form text, that is, a line that does not begin with the
671 mdoc_ptext(struct mdoc
*mdoc
, int line
, char *buf
, int offs
)
680 * Divert directly to list processing if we're encountering a
681 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
682 * (a MDOC_BODY means it's already open, in which case we should
683 * process within its context in the normal way).
686 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
687 LIST_column
== n
->norm
->Bl
.type
) {
688 /* `Bl' is open without any children. */
689 mdoc
->flags
|= MDOC_FREECOL
;
690 return(mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
));
693 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
695 MDOC_Bl
== n
->parent
->tok
&&
696 LIST_column
== n
->parent
->norm
->Bl
.type
) {
697 /* `Bl' has block-level `It' children. */
698 mdoc
->flags
|= MDOC_FREECOL
;
699 return(mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
));
703 * Search for the beginning of unescaped trailing whitespace (ws)
704 * and for the first character not to be output (end).
707 /* FIXME: replace with strcspn(). */
709 for (c
= end
= buf
+ offs
; *c
; c
++) {
717 * Always warn about trailing tabs,
718 * even outside literal context,
719 * where they should be put on the next line.
724 * Strip trailing tabs in literal context only;
725 * outside, they affect the next line.
727 if (MDOC_LITERAL
& mdoc
->flags
)
731 /* Skip the escaped character, too, if any. */
744 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
745 line
, (int)(ws
-buf
), NULL
);
747 if ('\0' == buf
[offs
] && ! (MDOC_LITERAL
& mdoc
->flags
)) {
748 mandoc_msg(MANDOCERR_FI_BLANK
, mdoc
->parse
,
749 line
, (int)(c
- buf
), NULL
);
752 * Insert a `sp' in the case of a blank line. Technically,
753 * blank lines aren't allowed, but enough manuals assume this
754 * behaviour that we want to work around it.
756 if ( ! mdoc_elem_alloc(mdoc
, line
, offs
, MDOC_sp
, NULL
))
759 mdoc
->next
= MDOC_NEXT_SIBLING
;
761 return(mdoc_valid_post(mdoc
));
764 if ( ! mdoc_word_alloc(mdoc
, line
, offs
, buf
+offs
))
767 if (MDOC_LITERAL
& mdoc
->flags
)
771 * End-of-sentence check. If the last character is an unescaped
772 * EOS character, then flag the node as being the end of a
773 * sentence. The front-end will know how to interpret this.
778 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
779 mdoc
->last
->flags
|= MDOC_EOS
;
785 * Parse a macro line, that is, a line beginning with the control
789 mdoc_pmacro(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
799 * Copy the first word into a nil-terminated buffer.
800 * Stop copying when a tab, space, or eoln is encountered.
804 while (i
< 4 && '\0' != buf
[offs
] && ' ' != buf
[offs
] &&
806 mac
[i
++] = buf
[offs
++];
810 tok
= (i
> 1 && i
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
812 if (MDOC_MAX
== tok
) {
813 mandoc_msg(MANDOCERR_MACRO
, mdoc
->parse
,
814 ln
, sv
, buf
+ sv
- 1);
818 /* Disregard the first trailing tab, if applicable. */
820 if ('\t' == buf
[offs
])
823 /* Jump to the next non-whitespace word. */
825 while (buf
[offs
] && ' ' == buf
[offs
])
829 * Trailing whitespace. Note that tabs are allowed to be passed
830 * into the parser as "text", so we only warn about spaces here.
833 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
834 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
838 * If an initial macro or a list invocation, divert directly
839 * into macro processing.
842 if (NULL
== mdoc
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
)
843 return(mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
));
849 * If the first macro of a `Bl -column', open an `It' block
850 * context around the parsed macro.
853 if (MDOC_Bl
== n
->tok
&& MDOC_BODY
== n
->type
&&
854 LIST_column
== n
->norm
->Bl
.type
) {
855 mdoc
->flags
|= MDOC_FREECOL
;
856 return(mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
));
860 * If we're following a block-level `It' within a `Bl -column'
861 * context (perhaps opened in the above block or in ptext()),
862 * then open an `It' block context around the parsed macro.
865 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
867 MDOC_Bl
== n
->parent
->tok
&&
868 LIST_column
== n
->parent
->norm
->Bl
.type
) {
869 mdoc
->flags
|= MDOC_FREECOL
;
870 return(mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
));
873 /* Normal processing of a macro. */
875 if ( ! mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
))
878 /* In quick mode (for mandocdb), abort after the NAME section. */
880 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
881 SEC_NAME
!= mdoc
->last
->sec
)
888 mdoc_isdelim(const char *p
)
901 return(DELIM_MIDDLE
);
925 if (0 == strcmp(p
+ 1, "."))
927 if (0 == strcmp(p
+ 1, "fR|\\fP"))
928 return(DELIM_MIDDLE
);
934 mdoc_deroff(char **dest
, const struct mdoc_node
*n
)
939 if (MDOC_TEXT
!= n
->type
) {
940 for (n
= n
->child
; n
; n
= n
->next
)
941 mdoc_deroff(dest
, n
);
945 /* Skip leading whitespace. */
947 for (cp
= n
->string
; '\0' != *cp
; cp
++)
948 if (0 == isspace((unsigned char)*cp
))
951 /* Skip trailing whitespace. */
953 for (sz
= strlen(cp
); sz
; sz
--)
954 if (0 == isspace((unsigned char)cp
[sz
-1]))
957 /* Skip empty strings. */
963 *dest
= mandoc_strndup(cp
, sz
);
967 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);