]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.129 2014/04/20 16:46:04 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/types.h>
34 #include "mandoc_aux.h"
36 #include "libmandoc.h"
38 const char *const __man_macronames
[MAN_MAX
] = {
39 "br", "TH", "SH", "SS",
40 "TP", "LP", "PP", "P",
41 "IP", "HP", "SM", "SB",
42 "BI", "IB", "BR", "RB",
44 "RI", "na", "sp", "nf",
45 "fi", "RE", "RS", "DT",
46 "UC", "PD", "AT", "in",
47 "ft", "OP", "EX", "EE",
51 const char * const *man_macronames
= __man_macronames
;
53 static struct man_node
*man_node_alloc(struct man
*, int, int,
54 enum man_type
, enum mant
);
55 static int man_node_append(struct man
*,
57 static void man_node_free(struct man_node
*);
58 static void man_node_unlink(struct man
*,
60 static int man_ptext(struct man
*, int, char *, int);
61 static int man_pmacro(struct man
*, int, char *, int);
62 static void man_free1(struct man
*);
63 static void man_alloc1(struct man
*);
64 static int man_descope(struct man
*, int, int);
67 const struct man_node
*
68 man_node(const struct man
*man
)
71 assert( ! (MAN_HALT
& man
->flags
));
75 const struct man_meta
*
76 man_meta(const struct man
*man
)
79 assert( ! (MAN_HALT
& man
->flags
));
84 man_reset(struct man
*man
)
92 man_free(struct man
*man
)
100 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
104 p
= mandoc_calloc(1, sizeof(struct man
));
116 man_endparse(struct man
*man
)
119 assert( ! (MAN_HALT
& man
->flags
));
120 if (man_macroend(man
))
122 man
->flags
|= MAN_HALT
;
127 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
130 man
->flags
|= MAN_NEWLINE
;
132 assert( ! (MAN_HALT
& man
->flags
));
134 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
135 man_pmacro(man
, ln
, buf
, offs
) :
136 man_ptext(man
, ln
, buf
, offs
));
140 man_free1(struct man
*man
)
144 man_node_delete(man
, man
->first
);
146 free(man
->meta
.title
);
147 if (man
->meta
.source
)
148 free(man
->meta
.source
);
150 free(man
->meta
.date
);
154 free(man
->meta
.msec
);
158 man_alloc1(struct man
*man
)
161 memset(&man
->meta
, 0, sizeof(struct man_meta
));
163 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
164 man
->first
= man
->last
;
165 man
->last
->type
= MAN_ROOT
;
166 man
->last
->tok
= MAN_MAX
;
167 man
->next
= MAN_NEXT_CHILD
;
172 man_node_append(struct man
*man
, struct man_node
*p
)
177 assert(MAN_ROOT
!= p
->type
);
180 case MAN_NEXT_SIBLING
:
183 p
->parent
= man
->last
->parent
;
186 man
->last
->child
= p
;
187 p
->parent
= man
->last
;
197 if ( ! man_valid_pre(man
, p
))
202 assert(MAN_BLOCK
== p
->parent
->type
);
206 assert(MAN_BLOCK
== p
->parent
->type
);
210 assert(MAN_BLOCK
== p
->parent
->type
);
223 if ( ! man_valid_post(man
))
233 static struct man_node
*
234 man_node_alloc(struct man
*man
, int line
, int pos
,
235 enum man_type type
, enum mant tok
)
239 p
= mandoc_calloc(1, sizeof(struct man_node
));
245 if (MAN_NEWLINE
& man
->flags
)
246 p
->flags
|= MAN_LINE
;
247 man
->flags
&= ~MAN_NEWLINE
;
252 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
256 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
257 if ( ! man_node_append(man
, p
))
259 man
->next
= MAN_NEXT_CHILD
;
264 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
268 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
269 if ( ! man_node_append(man
, p
))
271 man
->next
= MAN_NEXT_CHILD
;
276 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
280 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
281 if ( ! man_node_append(man
, p
))
283 man
->next
= MAN_NEXT_CHILD
;
288 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
292 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
293 if ( ! man_node_append(man
, p
))
295 man
->next
= MAN_NEXT_CHILD
;
300 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
304 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
305 if ( ! man_node_append(man
, p
))
307 man
->next
= MAN_NEXT_CHILD
;
312 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
316 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
317 n
->string
= roff_strdup(man
->roff
, word
);
319 if ( ! man_node_append(man
, n
))
322 man
->next
= MAN_NEXT_SIBLING
;
327 * Free all of the resources held by a node. This does NOT unlink a
328 * node from its context; for that, see man_node_unlink().
331 man_node_free(struct man_node
*p
)
340 man_node_delete(struct man
*man
, struct man_node
*p
)
344 man_node_delete(man
, p
->child
);
346 man_node_unlink(man
, p
);
351 man_addeqn(struct man
*man
, const struct eqn
*ep
)
355 assert( ! (MAN_HALT
& man
->flags
));
357 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
360 if ( ! man_node_append(man
, n
))
363 man
->next
= MAN_NEXT_SIBLING
;
364 return(man_descope(man
, ep
->ln
, ep
->pos
));
368 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
372 assert( ! (MAN_HALT
& man
->flags
));
374 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
377 if ( ! man_node_append(man
, n
))
380 man
->next
= MAN_NEXT_SIBLING
;
381 return(man_descope(man
, sp
->line
, 0));
385 man_descope(struct man
*man
, int line
, int offs
)
388 * Co-ordinate what happens with having a next-line scope open:
389 * first close out the element scope (if applicable), then close
390 * out the block scope (also if applicable).
393 if (MAN_ELINE
& man
->flags
) {
394 man
->flags
&= ~MAN_ELINE
;
395 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
399 if ( ! (MAN_BLINE
& man
->flags
))
401 man
->flags
&= ~MAN_BLINE
;
403 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
405 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
409 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
413 /* Literal free-form text whitespace is preserved. */
415 if (MAN_LITERAL
& man
->flags
) {
416 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
418 return(man_descope(man
, line
, offs
));
421 for (i
= offs
; ' ' == buf
[i
]; i
++)
422 /* Skip leading whitespace. */ ;
425 * Blank lines are ignored right after headings
426 * but add a single vertical space elsewhere.
429 if ('\0' == buf
[i
]) {
430 /* Allocate a blank entry. */
431 if (MAN_SH
!= man
->last
->tok
&&
432 MAN_SS
!= man
->last
->tok
) {
433 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
435 man
->next
= MAN_NEXT_SIBLING
;
441 * Warn if the last un-escaped character is whitespace. Then
442 * strip away the remaining spaces (tabs stay!).
445 i
= (int)strlen(buf
);
448 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
449 if (i
> 1 && '\\' != buf
[i
- 2])
450 man_pmsg(man
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
452 for (--i
; i
&& ' ' == buf
[i
]; i
--)
453 /* Spin back to non-space. */ ;
455 /* Jump ahead of escaped whitespace. */
456 i
+= '\\' == buf
[i
] ? 2 : 1;
461 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
465 * End-of-sentence check. If the last character is an unescaped
466 * EOS character, then flag the node as being the end of a
467 * sentence. The front-end will know how to interpret this.
471 if (mandoc_eos(buf
, (size_t)i
))
472 man
->last
->flags
|= MAN_EOS
;
474 return(man_descope(man
, line
, offs
));
478 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
485 if ('"' == buf
[offs
]) {
486 man_pmsg(man
, ln
, offs
, MANDOCERR_BADCOMMENT
);
488 } else if ('\0' == buf
[offs
])
494 * Copy the first word into a nil-terminated buffer.
495 * Stop copying when a tab, space, or eoln is encountered.
499 while (i
< 4 && '\0' != buf
[offs
] && ' ' != buf
[offs
] &&
501 mac
[i
++] = buf
[offs
++];
505 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
507 if (MAN_MAX
== tok
) {
508 mandoc_vmsg(MANDOCERR_MACRO
, man
->parse
, ln
, ppos
,
509 "%s", buf
+ ppos
- 1);
513 /* The macro is sane. Jump to the next word. */
515 while (buf
[offs
] && ' ' == buf
[offs
])
519 * Trailing whitespace. Note that tabs are allowed to be passed
520 * into the parser as "text", so we only warn about spaces here.
523 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
524 man_pmsg(man
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
527 * Remove prior ELINE macro, as it's being clobbered by a new
528 * macro. Note that NSCOPED macros do not close out ELINE
529 * macros---they don't print text---so we let those slip by.
532 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
533 man
->flags
& MAN_ELINE
) {
535 assert(MAN_TEXT
!= n
->type
);
537 /* Remove repeated NSCOPED macros causing ELINE. */
539 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
542 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
543 n
->pos
, "%s breaks %s", man_macronames
[tok
],
544 man_macronames
[n
->tok
]);
546 man_node_delete(man
, n
);
547 man
->flags
&= ~MAN_ELINE
;
551 * Remove prior BLINE macro that is being clobbered.
553 if ((man
->flags
& MAN_BLINE
) &&
554 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
557 /* Might be a text node like 8 in
561 if (MAN_TEXT
== n
->type
)
564 /* Remove element that didn't end BLINE, if any. */
565 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
568 assert(MAN_HEAD
== n
->type
);
570 assert(MAN_BLOCK
== n
->type
);
571 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
573 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
574 n
->pos
, "%s breaks %s", man_macronames
[tok
],
575 man_macronames
[n
->tok
]);
577 man_node_delete(man
, n
);
578 man
->flags
&= ~MAN_BLINE
;
582 * Save the fact that we're in the next-line for a block. In
583 * this way, embedded roff instructions can "remember" state
587 if (MAN_BLINE
& man
->flags
)
588 man
->flags
|= MAN_BPLINE
;
590 /* Call to handler... */
592 assert(man_macros
[tok
].fp
);
593 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
596 /* In quick mode (for mandocdb), abort after the NAME section. */
598 if (man
->quick
&& MAN_SH
== tok
&&
599 strcmp(man
->last
->prev
->child
->string
, "NAME"))
603 * We weren't in a block-line scope when entering the
604 * above-parsed macro, so return.
607 if ( ! (MAN_BPLINE
& man
->flags
)) {
608 man
->flags
&= ~MAN_ILINE
;
611 man
->flags
&= ~MAN_BPLINE
;
614 * If we're in a block scope, then allow this macro to slip by
615 * without closing scope around it.
618 if (MAN_ILINE
& man
->flags
) {
619 man
->flags
&= ~MAN_ILINE
;
624 * If we've opened a new next-line element scope, then return
625 * now, as the next line will close out the block scope.
628 if (MAN_ELINE
& man
->flags
)
631 /* Close out the block scope opened in the prior line. */
633 assert(MAN_BLINE
& man
->flags
);
634 man
->flags
&= ~MAN_BLINE
;
636 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
638 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
640 err
: /* Error out. */
642 man
->flags
|= MAN_HALT
;
647 * Unlink a node from its context. If "man" is provided, the last parse
648 * point will also be adjusted accordingly.
651 man_node_unlink(struct man
*man
, struct man_node
*n
)
654 /* Adjust siblings. */
657 n
->prev
->next
= n
->next
;
659 n
->next
->prev
= n
->prev
;
665 if (n
->parent
->child
== n
)
666 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
669 /* Adjust parse point, if applicable. */
671 if (man
&& man
->last
== n
) {
672 /*XXX: this can occur when bailing from validation. */
673 /*assert(NULL == n->next);*/
676 man
->next
= MAN_NEXT_SIBLING
;
678 man
->last
= n
->parent
;
679 man
->next
= MAN_NEXT_CHILD
;
683 if (man
&& man
->first
== n
)
687 const struct mparse
*
688 man_mparse(const struct man
*man
)
691 assert(man
&& man
->parse
);
696 man_deroff(char **dest
, const struct man_node
*n
)
701 if (MAN_TEXT
!= n
->type
) {
702 for (n
= n
->child
; n
; n
= n
->next
)
707 /* Skip leading whitespace and escape sequences. */
710 while ('\0' != *cp
) {
713 mandoc_escape((const char **)&cp
, NULL
, NULL
);
714 } else if (isspace((unsigned char)*cp
))
720 /* Skip trailing whitespace. */
722 for (sz
= strlen(cp
); sz
; sz
--)
723 if (0 == isspace((unsigned char)cp
[sz
-1]))
726 /* Skip empty strings. */
732 *dest
= mandoc_strndup(cp
, sz
);
736 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);