]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.246 2015/04/18 17:53:21 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
30 #include "mandoc_aux.h"
34 #include "libmandoc.h"
37 const char *const __mdoc_macronames
[MDOC_MAX
+ 1] = {
38 "Ap", "Dd", "Dt", "Os",
39 "Sh", "Ss", "Pp", "D1",
40 "Dl", "Bd", "Ed", "Bl",
41 "El", "It", "Ad", "An",
42 "Ar", "Cd", "Cm", "Dv",
43 "Er", "Ev", "Ex", "Fa",
44 "Fd", "Fl", "Fn", "Ft",
45 "Ic", "In", "Li", "Nd",
46 "Nm", "Op", "Ot", "Pa",
47 "Rv", "St", "Va", "Vt",
48 "Xr", "%A", "%B", "%D",
49 "%I", "%J", "%N", "%O",
50 "%P", "%R", "%T", "%V",
51 "Ac", "Ao", "Aq", "At",
52 "Bc", "Bf", "Bo", "Bq",
53 "Bsx", "Bx", "Db", "Dc",
54 "Do", "Dq", "Ec", "Ef",
55 "Em", "Eo", "Fx", "Ms",
56 "No", "Ns", "Nx", "Ox",
57 "Pc", "Pf", "Po", "Pq",
58 "Qc", "Ql", "Qo", "Qq",
59 "Re", "Rs", "Sc", "So",
60 "Sq", "Sm", "Sx", "Sy",
61 "Tn", "Ux", "Xc", "Xo",
62 "Fo", "Fc", "Oo", "Oc",
63 "Bk", "Ek", "Bt", "Hf",
64 "Fr", "Ud", "Lb", "Lp",
65 "Lk", "Mt", "Brq", "Bro",
66 "Brc", "%C", "Es", "En",
67 "Dx", "%Q", "br", "sp",
68 "%U", "Ta", "ll", "text",
71 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
72 "split", "nosplit", "ragged",
73 "unfilled", "literal", "file",
74 "offset", "bullet", "dash",
75 "hyphen", "item", "enum",
76 "tag", "diag", "hang",
77 "ohang", "inset", "column",
78 "width", "compact", "std",
79 "filled", "words", "emphasis",
80 "symbolic", "nested", "centered"
83 const char * const *mdoc_macronames
= __mdoc_macronames
;
84 const char * const *mdoc_argnames
= __mdoc_argnames
;
86 static void mdoc_node_free(struct roff_node
*);
87 static void mdoc_node_unlink(struct roff_man
*,
89 static struct roff_node
*node_alloc(struct roff_man
*, int, int,
91 static void node_append(struct roff_man
*, struct roff_node
*);
92 static int mdoc_ptext(struct roff_man
*, int, char *, int);
93 static int mdoc_pmacro(struct roff_man
*, int, char *, int);
97 mdoc_endparse(struct roff_man
*mdoc
)
104 mdoc_addeqn(struct roff_man
*mdoc
, const struct eqn
*ep
)
108 n
= node_alloc(mdoc
, ep
->ln
, ep
->pos
, MDOC_MAX
, ROFFT_EQN
);
110 if (ep
->ln
> mdoc
->last
->line
)
111 n
->flags
|= MDOC_LINE
;
112 node_append(mdoc
, n
);
113 mdoc
->next
= ROFF_NEXT_SIBLING
;
117 mdoc_addspan(struct roff_man
*mdoc
, const struct tbl_span
*sp
)
121 n
= node_alloc(mdoc
, sp
->line
, 0, MDOC_MAX
, ROFFT_TBL
);
123 node_append(mdoc
, n
);
124 mdoc
->next
= ROFF_NEXT_SIBLING
;
128 * Main parse routine. Parses a single line -- really just hands off to
129 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
132 mdoc_parseln(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
135 if (mdoc
->last
->type
!= ROFFT_EQN
|| ln
> mdoc
->last
->line
)
136 mdoc
->flags
|= MDOC_NEWLINE
;
139 * Let the roff nS register switch SYNOPSIS mode early,
140 * such that the parser knows at all times
141 * whether this mode is on or off.
142 * Note that this mode is also switched by the Sh macro.
144 if (roff_getreg(mdoc
->roff
, "nS"))
145 mdoc
->flags
|= MDOC_SYNOPSIS
;
147 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
149 return(roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
150 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
151 mdoc_ptext(mdoc
, ln
, buf
, offs
));
155 mdoc_macro(MACRO_PROT_ARGS
)
157 assert(tok
< MDOC_MAX
);
159 if (mdoc
->flags
& MDOC_PBODY
) {
160 if (tok
== MDOC_Dt
) {
161 mandoc_vmsg(MANDOCERR_DT_LATE
,
162 mdoc
->parse
, line
, ppos
,
163 "Dt %s", buf
+ *pos
);
166 } else if ( ! (mdoc_macros
[tok
].flags
& MDOC_PROLOGUE
)) {
167 if (mdoc
->meta
.title
== NULL
) {
168 mandoc_vmsg(MANDOCERR_DT_NOTITLE
,
169 mdoc
->parse
, line
, ppos
, "%s %s",
170 mdoc_macronames
[tok
], buf
+ *pos
);
171 mdoc
->meta
.title
= mandoc_strdup("UNTITLED");
173 if (NULL
== mdoc
->meta
.vol
)
174 mdoc
->meta
.vol
= mandoc_strdup("LOCAL");
175 mdoc
->flags
|= MDOC_PBODY
;
177 (*mdoc_macros
[tok
].fp
)(mdoc
, tok
, line
, ppos
, pos
, buf
);
182 node_append(struct roff_man
*mdoc
, struct roff_node
*p
)
187 assert(p
->type
!= ROFFT_ROOT
);
189 switch (mdoc
->next
) {
190 case ROFF_NEXT_SIBLING
:
191 mdoc
->last
->next
= p
;
192 p
->prev
= mdoc
->last
;
193 p
->parent
= mdoc
->last
->parent
;
195 case ROFF_NEXT_CHILD
:
196 mdoc
->last
->child
= p
;
197 p
->parent
= mdoc
->last
;
207 * Copy over the normalised-data pointer of our parent. Not
208 * everybody has one, but copying a null pointer is fine.
213 if (ENDBODY_NOT
!= p
->end
)
219 p
->norm
= p
->parent
->norm
;
225 mdoc_valid_pre(mdoc
, p
);
229 assert(p
->parent
->type
== ROFFT_BLOCK
);
233 assert(p
->parent
->type
== ROFFT_BLOCK
);
239 assert(p
->parent
->type
== ROFFT_BLOCK
);
252 mdoc_valid_post(mdoc
);
259 static struct roff_node
*
260 node_alloc(struct roff_man
*mdoc
, int line
, int pos
,
261 int tok
, enum roff_type type
)
265 p
= mandoc_calloc(1, sizeof(*p
));
266 p
->sec
= mdoc
->lastsec
;
274 if (MDOC_SYNOPSIS
& mdoc
->flags
)
275 p
->flags
|= MDOC_SYNPRETTY
;
277 p
->flags
&= ~MDOC_SYNPRETTY
;
278 if (MDOC_NEWLINE
& mdoc
->flags
)
279 p
->flags
|= MDOC_LINE
;
280 mdoc
->flags
&= ~MDOC_NEWLINE
;
286 mdoc_tail_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
)
290 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_TAIL
);
291 node_append(mdoc
, p
);
292 mdoc
->next
= ROFF_NEXT_CHILD
;
296 mdoc_head_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
)
302 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_HEAD
);
303 node_append(mdoc
, p
);
304 mdoc
->next
= ROFF_NEXT_CHILD
;
309 mdoc_body_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
)
313 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_BODY
);
314 node_append(mdoc
, p
);
315 mdoc
->next
= ROFF_NEXT_CHILD
;
320 mdoc_endbody_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
,
321 struct roff_node
*body
, enum mdoc_endbody end
)
325 body
->flags
|= MDOC_ENDED
;
326 body
->parent
->flags
|= MDOC_ENDED
;
327 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_BODY
);
329 p
->norm
= body
->norm
;
331 node_append(mdoc
, p
);
332 mdoc
->next
= ROFF_NEXT_SIBLING
;
337 mdoc_block_alloc(struct roff_man
*mdoc
, int line
, int pos
,
338 int tok
, struct mdoc_arg
*args
)
342 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_BLOCK
);
357 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
362 node_append(mdoc
, p
);
363 mdoc
->next
= ROFF_NEXT_CHILD
;
368 mdoc_elem_alloc(struct roff_man
*mdoc
, int line
, int pos
,
369 int tok
, struct mdoc_arg
*args
)
373 p
= node_alloc(mdoc
, line
, pos
, tok
, ROFFT_ELEM
);
380 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
385 node_append(mdoc
, p
);
386 mdoc
->next
= ROFF_NEXT_CHILD
;
390 mdoc_word_alloc(struct roff_man
*mdoc
, int line
, int pos
, const char *p
)
394 n
= node_alloc(mdoc
, line
, pos
, MDOC_MAX
, ROFFT_TEXT
);
395 n
->string
= roff_strdup(mdoc
->roff
, p
);
396 node_append(mdoc
, n
);
397 mdoc
->next
= ROFF_NEXT_SIBLING
;
401 mdoc_word_append(struct roff_man
*mdoc
, const char *p
)
404 char *addstr
, *newstr
;
407 addstr
= roff_strdup(mdoc
->roff
, p
);
408 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
412 mdoc
->next
= ROFF_NEXT_SIBLING
;
416 mdoc_node_free(struct roff_node
*p
)
419 if (p
->type
== ROFFT_BLOCK
|| p
->type
== ROFFT_ELEM
)
424 mdoc_argv_free(p
->args
);
429 mdoc_node_unlink(struct roff_man
*mdoc
, struct roff_node
*n
)
432 /* Adjust siblings. */
435 n
->prev
->next
= n
->next
;
437 n
->next
->prev
= n
->prev
;
443 if (n
->parent
->child
== n
)
444 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
445 if (n
->parent
->last
== n
)
446 n
->parent
->last
= n
->prev
? n
->prev
: NULL
;
449 /* Adjust parse point, if applicable. */
451 if (mdoc
&& mdoc
->last
== n
) {
453 mdoc
->last
= n
->prev
;
454 mdoc
->next
= ROFF_NEXT_SIBLING
;
456 mdoc
->last
= n
->parent
;
457 mdoc
->next
= ROFF_NEXT_CHILD
;
461 if (mdoc
&& mdoc
->first
== n
)
466 mdoc_node_delete(struct roff_man
*mdoc
, struct roff_node
*p
)
471 mdoc_node_delete(mdoc
, p
->child
);
473 assert(0 == p
->nchild
);
475 mdoc_node_unlink(mdoc
, p
);
480 mdoc_node_relink(struct roff_man
*mdoc
, struct roff_node
*p
)
483 mdoc_node_unlink(mdoc
, p
);
484 node_append(mdoc
, p
);
488 * Parse free-form text, that is, a line that does not begin with the
492 mdoc_ptext(struct roff_man
*mdoc
, int line
, char *buf
, int offs
)
501 * Divert directly to list processing if we're encountering a
502 * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
503 * (a ROFFT_BODY means it's already open, in which case we should
504 * process within its context in the normal way).
507 if (n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
508 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
509 /* `Bl' is open without any children. */
510 mdoc
->flags
|= MDOC_FREECOL
;
511 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
515 if (n
->tok
== MDOC_It
&& n
->type
== ROFFT_BLOCK
&&
517 MDOC_Bl
== n
->parent
->tok
&&
518 LIST_column
== n
->parent
->norm
->Bl
.type
) {
519 /* `Bl' has block-level `It' children. */
520 mdoc
->flags
|= MDOC_FREECOL
;
521 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
526 * Search for the beginning of unescaped trailing whitespace (ws)
527 * and for the first character not to be output (end).
530 /* FIXME: replace with strcspn(). */
532 for (c
= end
= buf
+ offs
; *c
; c
++) {
540 * Always warn about trailing tabs,
541 * even outside literal context,
542 * where they should be put on the next line.
547 * Strip trailing tabs in literal context only;
548 * outside, they affect the next line.
550 if (MDOC_LITERAL
& mdoc
->flags
)
554 /* Skip the escaped character, too, if any. */
567 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
568 line
, (int)(ws
-buf
), NULL
);
570 if (buf
[offs
] == '\0' && ! (mdoc
->flags
& MDOC_LITERAL
)) {
571 mandoc_msg(MANDOCERR_FI_BLANK
, mdoc
->parse
,
572 line
, (int)(c
- buf
), NULL
);
575 * Insert a `sp' in the case of a blank line. Technically,
576 * blank lines aren't allowed, but enough manuals assume this
577 * behaviour that we want to work around it.
579 mdoc_elem_alloc(mdoc
, line
, offs
, MDOC_sp
, NULL
);
580 mdoc
->next
= ROFF_NEXT_SIBLING
;
581 mdoc_valid_post(mdoc
);
585 mdoc_word_alloc(mdoc
, line
, offs
, buf
+offs
);
587 if (mdoc
->flags
& MDOC_LITERAL
)
591 * End-of-sentence check. If the last character is an unescaped
592 * EOS character, then flag the node as being the end of a
593 * sentence. The front-end will know how to interpret this.
598 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
599 mdoc
->last
->flags
|= MDOC_EOS
;
604 * Parse a macro line, that is, a line beginning with the control
608 mdoc_pmacro(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
619 * Copy the first word into a nil-terminated buffer.
620 * Stop when a space, tab, escape, or eoln is encountered.
624 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
625 mac
[i
++] = buf
[offs
++];
629 tok
= (i
> 1 && i
< 4) ? mdoc_hash_find(mac
) : MDOC_MAX
;
631 if (tok
== MDOC_MAX
) {
632 mandoc_msg(MANDOCERR_MACRO
, mdoc
->parse
,
633 ln
, sv
, buf
+ sv
- 1);
637 /* Skip a leading escape sequence or tab. */
642 mandoc_escape(&cp
, NULL
, NULL
);
652 /* Jump to the next non-whitespace word. */
654 while (buf
[offs
] && ' ' == buf
[offs
])
658 * Trailing whitespace. Note that tabs are allowed to be passed
659 * into the parser as "text", so we only warn about spaces here.
662 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
663 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
667 * If an initial macro or a list invocation, divert directly
668 * into macro processing.
671 if (NULL
== mdoc
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
672 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
680 * If the first macro of a `Bl -column', open an `It' block
681 * context around the parsed macro.
684 if (n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
685 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
686 mdoc
->flags
|= MDOC_FREECOL
;
687 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
692 * If we're following a block-level `It' within a `Bl -column'
693 * context (perhaps opened in the above block or in ptext()),
694 * then open an `It' block context around the parsed macro.
697 if (n
->tok
== MDOC_It
&& n
->type
== ROFFT_BLOCK
&&
699 MDOC_Bl
== n
->parent
->tok
&&
700 LIST_column
== n
->parent
->norm
->Bl
.type
) {
701 mdoc
->flags
|= MDOC_FREECOL
;
702 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
706 /* Normal processing of a macro. */
708 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
710 /* In quick mode (for mandocdb), abort after the NAME section. */
712 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
713 SEC_NAME
!= mdoc
->last
->sec
)
720 mdoc_isdelim(const char *p
)
733 return(DELIM_MIDDLE
);
757 if (0 == strcmp(p
+ 1, "."))
759 if (0 == strcmp(p
+ 1, "fR|\\fP"))
760 return(DELIM_MIDDLE
);
766 mdoc_deroff(char **dest
, const struct roff_node
*n
)
771 if (n
->type
!= ROFFT_TEXT
) {
772 for (n
= n
->child
; n
; n
= n
->next
)
773 mdoc_deroff(dest
, n
);
777 /* Skip leading whitespace. */
779 for (cp
= n
->string
; '\0' != *cp
; cp
++)
780 if (0 == isspace((unsigned char)*cp
))
783 /* Skip trailing whitespace. */
785 for (sz
= strlen(cp
); sz
; sz
--)
786 if (0 == isspace((unsigned char)cp
[sz
-1]))
789 /* Skip empty strings. */
795 *dest
= mandoc_strndup(cp
, sz
);
799 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);