]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.238 2015/02/12 13:00:52 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 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 void node_append(struct mdoc
*, struct mdoc_node
*);
93 static int mdoc_ptext(struct mdoc
*, int, char *, int);
94 static int mdoc_pmacro(struct mdoc
*, int, char *, int);
97 const struct mdoc_node
*
98 mdoc_node(const struct mdoc
*mdoc
)
104 const struct mdoc_meta
*
105 mdoc_meta(const struct mdoc
*mdoc
)
112 * Frees volatile resources (parse tree, meta-data, fields).
115 mdoc_free1(struct mdoc
*mdoc
)
119 mdoc_node_delete(mdoc
, mdoc
->first
);
120 free(mdoc
->meta
.msec
);
121 free(mdoc
->meta
.vol
);
122 free(mdoc
->meta
.arch
);
123 free(mdoc
->meta
.date
);
124 free(mdoc
->meta
.title
);
126 free(mdoc
->meta
.name
);
130 * Allocate all volatile resources (parse tree, meta-data, fields).
133 mdoc_alloc1(struct mdoc
*mdoc
)
136 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
138 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
139 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
140 mdoc
->first
= mdoc
->last
;
141 mdoc
->last
->type
= MDOC_ROOT
;
142 mdoc
->last
->tok
= MDOC_MAX
;
143 mdoc
->next
= MDOC_NEXT_CHILD
;
147 * Free up volatile resources (see mdoc_free1()) then re-initialises the
148 * data with mdoc_alloc1(). After invocation, parse data has been reset
149 * and the parser is ready for re-invocation on a new tree; however,
150 * cross-parse non-volatile data is kept intact.
153 mdoc_reset(struct mdoc
*mdoc
)
161 * Completely free up all volatile and non-volatile parse resources.
162 * After invocation, the pointer is no longer usable.
165 mdoc_free(struct mdoc
*mdoc
)
173 * Allocate volatile and non-volatile parse resources.
176 mdoc_alloc(struct roff
*roff
, struct mparse
*parse
,
177 const char *defos
, int quick
)
181 p
= mandoc_calloc(1, sizeof(struct mdoc
));
194 mdoc_endparse(struct mdoc
*mdoc
)
201 mdoc_addeqn(struct mdoc
*mdoc
, const struct eqn
*ep
)
205 n
= node_alloc(mdoc
, ep
->ln
, ep
->pos
, MDOC_MAX
, MDOC_EQN
);
207 if (ep
->ln
> mdoc
->last
->line
)
208 n
->flags
|= MDOC_LINE
;
209 node_append(mdoc
, n
);
210 mdoc
->next
= MDOC_NEXT_SIBLING
;
214 mdoc_addspan(struct mdoc
*mdoc
, const struct tbl_span
*sp
)
218 n
= node_alloc(mdoc
, sp
->line
, 0, MDOC_MAX
, MDOC_TBL
);
220 node_append(mdoc
, n
);
221 mdoc
->next
= MDOC_NEXT_SIBLING
;
225 * Main parse routine. Parses a single line -- really just hands off to
226 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
229 mdoc_parseln(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
232 if (mdoc
->last
->type
!= MDOC_EQN
|| ln
> mdoc
->last
->line
)
233 mdoc
->flags
|= MDOC_NEWLINE
;
236 * Let the roff nS register switch SYNOPSIS mode early,
237 * such that the parser knows at all times
238 * whether this mode is on or off.
239 * Note that this mode is also switched by the Sh macro.
241 if (roff_getreg(mdoc
->roff
, "nS"))
242 mdoc
->flags
|= MDOC_SYNOPSIS
;
244 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
246 return(roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
247 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
248 mdoc_ptext(mdoc
, ln
, buf
, offs
));
252 mdoc_macro(MACRO_PROT_ARGS
)
254 assert(tok
< MDOC_MAX
);
256 if (mdoc
->flags
& MDOC_PBODY
) {
257 if (tok
== MDOC_Dt
) {
258 mandoc_vmsg(MANDOCERR_DT_LATE
,
259 mdoc
->parse
, line
, ppos
,
260 "Dt %s", buf
+ *pos
);
263 } else if ( ! (mdoc_macros
[tok
].flags
& MDOC_PROLOGUE
)) {
264 if (mdoc
->meta
.title
== NULL
) {
265 mandoc_vmsg(MANDOCERR_DT_NOTITLE
,
266 mdoc
->parse
, line
, ppos
, "%s %s",
267 mdoc_macronames
[tok
], buf
+ *pos
);
268 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
270 if (NULL
== mdoc
->meta
.vol
)
271 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
272 mdoc
->flags
|= MDOC_PBODY
;
274 (*mdoc_macros
[tok
].fp
)(mdoc
, tok
, line
, ppos
, pos
, buf
);
279 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
284 assert(MDOC_ROOT
!= p
->type
);
286 switch (mdoc
->next
) {
287 case MDOC_NEXT_SIBLING
:
288 mdoc
->last
->next
= p
;
289 p
->prev
= mdoc
->last
;
290 p
->parent
= mdoc
->last
->parent
;
292 case MDOC_NEXT_CHILD
:
293 mdoc
->last
->child
= p
;
294 p
->parent
= mdoc
->last
;
304 * Copy over the normalised-data pointer of our parent. Not
305 * everybody has one, but copying a null pointer is fine.
310 if (ENDBODY_NOT
!= p
->end
)
316 p
->norm
= p
->parent
->norm
;
322 mdoc_valid_pre(mdoc
, p
);
326 assert(MDOC_BLOCK
== p
->parent
->type
);
330 assert(MDOC_BLOCK
== p
->parent
->type
);
336 assert(MDOC_BLOCK
== p
->parent
->type
);
349 mdoc_valid_post(mdoc
);
356 static struct mdoc_node
*
357 node_alloc(struct mdoc
*mdoc
, int line
, int pos
,
358 enum mdoct tok
, enum mdoc_type type
)
362 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
363 p
->sec
= mdoc
->lastsec
;
371 if (MDOC_SYNOPSIS
& mdoc
->flags
)
372 p
->flags
|= MDOC_SYNPRETTY
;
374 p
->flags
&= ~MDOC_SYNPRETTY
;
375 if (MDOC_NEWLINE
& mdoc
->flags
)
376 p
->flags
|= MDOC_LINE
;
377 mdoc
->flags
&= ~MDOC_NEWLINE
;
383 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
387 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_TAIL
);
388 node_append(mdoc
, p
);
389 mdoc
->next
= MDOC_NEXT_CHILD
;
393 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
399 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_HEAD
);
400 node_append(mdoc
, p
);
401 mdoc
->next
= MDOC_NEXT_CHILD
;
406 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
)
410 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
411 node_append(mdoc
, p
);
412 mdoc
->next
= MDOC_NEXT_CHILD
;
417 mdoc_endbody_alloc(struct mdoc
*mdoc
, int line
, int pos
, enum mdoct tok
,
418 struct mdoc_node
*body
, enum mdoc_endbody end
)
422 body
->flags
|= MDOC_ENDED
;
423 body
->parent
->flags
|= MDOC_ENDED
;
424 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
426 p
->norm
= body
->norm
;
428 node_append(mdoc
, p
);
429 mdoc
->next
= MDOC_NEXT_SIBLING
;
434 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
435 enum mdoct tok
, struct mdoc_arg
*args
)
439 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BLOCK
);
454 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
459 node_append(mdoc
, p
);
460 mdoc
->next
= MDOC_NEXT_CHILD
;
465 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
466 enum mdoct tok
, struct mdoc_arg
*args
)
470 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_ELEM
);
477 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
482 node_append(mdoc
, p
);
483 mdoc
->next
= MDOC_NEXT_CHILD
;
487 mdoc_word_alloc(struct mdoc
*mdoc
, int line
, int pos
, const char *p
)
491 n
= node_alloc(mdoc
, line
, pos
, MDOC_MAX
, MDOC_TEXT
);
492 n
->string
= roff_strdup(mdoc
->roff
, p
);
493 node_append(mdoc
, n
);
494 mdoc
->next
= MDOC_NEXT_SIBLING
;
498 mdoc_word_append(struct mdoc
*mdoc
, const char *p
)
501 char *addstr
, *newstr
;
504 addstr
= roff_strdup(mdoc
->roff
, p
);
505 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
509 mdoc
->next
= MDOC_NEXT_SIBLING
;
513 mdoc_node_free(struct mdoc_node
*p
)
516 if (MDOC_BLOCK
== p
->type
|| MDOC_ELEM
== p
->type
)
521 mdoc_argv_free(p
->args
);
526 mdoc_node_unlink(struct mdoc
*mdoc
, struct mdoc_node
*n
)
529 /* Adjust siblings. */
532 n
->prev
->next
= n
->next
;
534 n
->next
->prev
= n
->prev
;
540 if (n
->parent
->child
== n
)
541 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
542 if (n
->parent
->last
== n
)
543 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
546 /* Adjust parse point, if applicable. */
548 if (mdoc
&& mdoc
->last
== n
) {
550 mdoc
->last
= n
->prev
;
551 mdoc
->next
= MDOC_NEXT_SIBLING
;
553 mdoc
->last
= n
->parent
;
554 mdoc
->next
= MDOC_NEXT_CHILD
;
558 if (mdoc
&& mdoc
->first
== n
)
563 mdoc_node_delete(struct mdoc
*mdoc
, struct mdoc_node
*p
)
568 mdoc_node_delete(mdoc
, p
->child
);
570 assert(0 == p
->nchild
);
572 mdoc_node_unlink(mdoc
, p
);
577 mdoc_node_relink(struct mdoc
*mdoc
, struct mdoc_node
*p
)
580 mdoc_node_unlink(mdoc
, p
);
581 node_append(mdoc
, p
);
585 * Parse free-form text, that is, a line that does not begin with the
589 mdoc_ptext(struct mdoc
*mdoc
, int line
, char *buf
, int offs
)
598 * Divert directly to list processing if we're encountering a
599 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
600 * (a MDOC_BODY means it's already open, in which case we should
601 * process within its context in the normal way).
604 if (n
->tok
== MDOC_Bl
&& n
->type
== MDOC_BODY
&&
605 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
606 /* `Bl' is open without any children. */
607 mdoc
->flags
|= MDOC_FREECOL
;
608 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
612 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
614 MDOC_Bl
== n
->parent
->tok
&&
615 LIST_column
== n
->parent
->norm
->Bl
.type
) {
616 /* `Bl' has block-level `It' children. */
617 mdoc
->flags
|= MDOC_FREECOL
;
618 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
623 * Search for the beginning of unescaped trailing whitespace (ws)
624 * and for the first character not to be output (end).
627 /* FIXME: replace with strcspn(). */
629 for (c
= end
= buf
+ offs
; *c
; c
++) {
637 * Always warn about trailing tabs,
638 * even outside literal context,
639 * where they should be put on the next line.
644 * Strip trailing tabs in literal context only;
645 * outside, they affect the next line.
647 if (MDOC_LITERAL
& mdoc
->flags
)
651 /* Skip the escaped character, too, if any. */
664 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
665 line
, (int)(ws
-buf
), NULL
);
667 if (buf
[offs
] == '\0' && ! (mdoc
->flags
& MDOC_LITERAL
)) {
668 mandoc_msg(MANDOCERR_FI_BLANK
, mdoc
->parse
,
669 line
, (int)(c
- buf
), NULL
);
672 * Insert a `sp' in the case of a blank line. Technically,
673 * blank lines aren't allowed, but enough manuals assume this
674 * behaviour that we want to work around it.
676 mdoc_elem_alloc(mdoc
, line
, offs
, MDOC_sp
, NULL
);
677 mdoc
->next
= MDOC_NEXT_SIBLING
;
678 mdoc_valid_post(mdoc
);
682 mdoc_word_alloc(mdoc
, line
, offs
, buf
+offs
);
684 if (mdoc
->flags
& MDOC_LITERAL
)
688 * End-of-sentence check. If the last character is an unescaped
689 * EOS character, then flag the node as being the end of a
690 * sentence. The front-end will know how to interpret this.
695 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
696 mdoc
->last
->flags
|= MDOC_EOS
;
701 * Parse a macro line, that is, a line beginning with the control
705 mdoc_pmacro(struct mdoc
*mdoc
, int ln
, char *buf
, int offs
)
716 * Copy the first word into a nil-terminated buffer.
717 * Stop when a space, tab, escape, or eoln is encountered.
721 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
722 mac
[i
++] = buf
[offs
++];
726 tok
= (i
> 1 && i
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
728 if (tok
== MDOC_MAX
) {
729 mandoc_msg(MANDOCERR_MACRO
, mdoc
->parse
,
730 ln
, sv
, buf
+ sv
- 1);
734 /* Skip a leading escape sequence or tab. */
739 mandoc_escape(&cp
, NULL
, NULL
);
749 /* Jump to the next non-whitespace word. */
751 while (buf
[offs
] && ' ' == buf
[offs
])
755 * Trailing whitespace. Note that tabs are allowed to be passed
756 * into the parser as "text", so we only warn about spaces here.
759 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
760 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
764 * If an initial macro or a list invocation, divert directly
765 * into macro processing.
768 if (NULL
== mdoc
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
769 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
777 * If the first macro of a `Bl -column', open an `It' block
778 * context around the parsed macro.
781 if (n
->tok
== MDOC_Bl
&& n
->type
== MDOC_BODY
&&
782 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
783 mdoc
->flags
|= MDOC_FREECOL
;
784 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
789 * If we're following a block-level `It' within a `Bl -column'
790 * context (perhaps opened in the above block or in ptext()),
791 * then open an `It' block context around the parsed macro.
794 if (MDOC_It
== n
->tok
&& MDOC_BLOCK
== n
->type
&&
796 MDOC_Bl
== n
->parent
->tok
&&
797 LIST_column
== n
->parent
->norm
->Bl
.type
) {
798 mdoc
->flags
|= MDOC_FREECOL
;
799 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
803 /* Normal processing of a macro. */
805 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
807 /* In quick mode (for mandocdb), abort after the NAME section. */
809 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
810 SEC_NAME
!= mdoc
->last
->sec
)
817 mdoc_isdelim(const char *p
)
830 return(DELIM_MIDDLE
);
854 if (0 == strcmp(p
+ 1, "."))
856 if (0 == strcmp(p
+ 1, "fR|\\fP"))
857 return(DELIM_MIDDLE
);
863 mdoc_deroff(char **dest
, const struct mdoc_node
*n
)
868 if (MDOC_TEXT
!= n
->type
) {
869 for (n
= n
->child
; n
; n
= n
->next
)
870 mdoc_deroff(dest
, n
);
874 /* Skip leading whitespace. */
876 for (cp
= n
->string
; '\0' != *cp
; cp
++)
877 if (0 == isspace((unsigned char)*cp
))
880 /* Skip trailing whitespace. */
882 for (sz
= strlen(cp
); sz
; sz
--)
883 if (0 == isspace((unsigned char)cp
[sz
-1]))
886 /* Skip empty strings. */
892 *dest
= mandoc_strndup(cp
, sz
);
896 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);