]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.141 2014/10/20 15:50:24 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.
21 #include <sys/types.h>
32 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 const char *const __man_macronames
[MAN_MAX
] = {
37 "br", "TH", "SH", "SS",
38 "TP", "LP", "PP", "P",
39 "IP", "HP", "SM", "SB",
40 "BI", "IB", "BR", "RB",
42 "RI", "na", "sp", "nf",
43 "fi", "RE", "RS", "DT",
44 "UC", "PD", "AT", "in",
45 "ft", "OP", "EX", "EE",
49 const char * const *man_macronames
= __man_macronames
;
51 static struct man_node
*man_node_alloc(struct man
*, int, int,
52 enum man_type
, enum mant
);
53 static int man_node_append(struct man
*,
55 static void man_node_free(struct man_node
*);
56 static void man_node_unlink(struct man
*,
58 static int man_ptext(struct man
*, int, char *, int);
59 static int man_pmacro(struct man
*, int, char *, int);
60 static void man_free1(struct man
*);
61 static void man_alloc1(struct man
*);
62 static int man_descope(struct man
*, int, int);
65 const struct man_node
*
66 man_node(const struct man
*man
)
72 const struct man_meta
*
73 man_meta(const struct man
*man
)
80 man_reset(struct man
*man
)
88 man_free(struct man
*man
)
96 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
100 p
= mandoc_calloc(1, sizeof(struct man
));
112 man_endparse(struct man
*man
)
115 return(man_macroend(man
));
119 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
122 if (man
->last
->type
!= MAN_EQN
|| ln
> man
->last
->line
)
123 man
->flags
|= MAN_NEWLINE
;
125 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
126 man_pmacro(man
, ln
, buf
, offs
) :
127 man_ptext(man
, ln
, buf
, offs
));
131 man_free1(struct man
*man
)
135 man_node_delete(man
, man
->first
);
137 free(man
->meta
.title
);
138 if (man
->meta
.source
)
139 free(man
->meta
.source
);
141 free(man
->meta
.date
);
145 free(man
->meta
.msec
);
149 man_alloc1(struct man
*man
)
152 memset(&man
->meta
, 0, sizeof(struct man_meta
));
154 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
155 man
->first
= man
->last
;
156 man
->last
->type
= MAN_ROOT
;
157 man
->last
->tok
= MAN_MAX
;
158 man
->next
= MAN_NEXT_CHILD
;
163 man_node_append(struct man
*man
, struct man_node
*p
)
168 assert(MAN_ROOT
!= p
->type
);
171 case MAN_NEXT_SIBLING
:
174 p
->parent
= man
->last
->parent
;
177 man
->last
->child
= p
;
178 p
->parent
= man
->last
;
190 if (p
->tok
== MAN_SH
|| p
->tok
== MAN_SS
)
191 man
->flags
&= ~MAN_LITERAL
;
194 assert(MAN_BLOCK
== p
->parent
->type
);
198 assert(MAN_BLOCK
== p
->parent
->type
);
202 assert(MAN_BLOCK
== p
->parent
->type
);
215 if ( ! man_valid_post(man
))
225 static struct man_node
*
226 man_node_alloc(struct man
*man
, int line
, int pos
,
227 enum man_type type
, enum mant tok
)
231 p
= mandoc_calloc(1, sizeof(struct man_node
));
237 if (MAN_NEWLINE
& man
->flags
)
238 p
->flags
|= MAN_LINE
;
239 man
->flags
&= ~MAN_NEWLINE
;
244 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
248 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
249 if ( ! man_node_append(man
, p
))
251 man
->next
= MAN_NEXT_CHILD
;
256 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
260 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
261 if ( ! man_node_append(man
, p
))
263 man
->next
= MAN_NEXT_CHILD
;
268 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
272 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
273 if ( ! man_node_append(man
, p
))
275 man
->next
= MAN_NEXT_CHILD
;
280 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
284 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
285 if ( ! man_node_append(man
, p
))
287 man
->next
= MAN_NEXT_CHILD
;
292 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
296 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
297 if ( ! man_node_append(man
, p
))
299 man
->next
= MAN_NEXT_CHILD
;
304 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
308 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
309 n
->string
= roff_strdup(man
->roff
, word
);
311 if ( ! man_node_append(man
, n
))
314 man
->next
= MAN_NEXT_SIBLING
;
319 * Free all of the resources held by a node. This does NOT unlink a
320 * node from its context; for that, see man_node_unlink().
323 man_node_free(struct man_node
*p
)
332 man_node_delete(struct man
*man
, struct man_node
*p
)
336 man_node_delete(man
, p
->child
);
338 man_node_unlink(man
, p
);
343 man_addeqn(struct man
*man
, const struct eqn
*ep
)
347 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
349 if (ep
->ln
> man
->last
->line
)
350 n
->flags
|= MAN_LINE
;
352 if ( ! man_node_append(man
, n
))
355 man
->next
= MAN_NEXT_SIBLING
;
356 return(man_descope(man
, ep
->ln
, ep
->pos
));
360 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
364 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
367 if ( ! man_node_append(man
, n
))
370 man
->next
= MAN_NEXT_SIBLING
;
371 return(man_descope(man
, sp
->line
, 0));
375 man_descope(struct man
*man
, int line
, int offs
)
378 * Co-ordinate what happens with having a next-line scope open:
379 * first close out the element scope (if applicable), then close
380 * out the block scope (also if applicable).
383 if (MAN_ELINE
& man
->flags
) {
384 man
->flags
&= ~MAN_ELINE
;
385 if ( ! man_unscope(man
, man
->last
->parent
))
389 if ( ! (MAN_BLINE
& man
->flags
))
391 man
->flags
&= ~MAN_BLINE
;
393 if ( ! man_unscope(man
, man
->last
->parent
))
395 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
399 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
403 /* Literal free-form text whitespace is preserved. */
405 if (MAN_LITERAL
& man
->flags
) {
406 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
408 return(man_descope(man
, line
, offs
));
411 for (i
= offs
; ' ' == buf
[i
]; i
++)
412 /* Skip leading whitespace. */ ;
415 * Blank lines are ignored right after headings
416 * but add a single vertical space elsewhere.
419 if ('\0' == buf
[i
]) {
420 /* Allocate a blank entry. */
421 if (MAN_SH
!= man
->last
->tok
&&
422 MAN_SS
!= man
->last
->tok
) {
423 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
425 man
->next
= MAN_NEXT_SIBLING
;
431 * Warn if the last un-escaped character is whitespace. Then
432 * strip away the remaining spaces (tabs stay!).
435 i
= (int)strlen(buf
);
438 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
439 if (i
> 1 && '\\' != buf
[i
- 2])
440 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
443 for (--i
; i
&& ' ' == buf
[i
]; i
--)
444 /* Spin back to non-space. */ ;
446 /* Jump ahead of escaped whitespace. */
447 i
+= '\\' == buf
[i
] ? 2 : 1;
452 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
456 * End-of-sentence check. If the last character is an unescaped
457 * EOS character, then flag the node as being the end of a
458 * sentence. The front-end will know how to interpret this.
462 if (mandoc_eos(buf
, (size_t)i
))
463 man
->last
->flags
|= MAN_EOS
;
465 return(man_descope(man
, line
, offs
));
469 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
480 * Copy the first word into a nil-terminated buffer.
481 * Stop copying when a tab, space, or eoln is encountered.
485 while (i
< 4 && '\0' != buf
[offs
] && ' ' != buf
[offs
] &&
487 mac
[i
++] = buf
[offs
++];
491 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
493 if (MAN_MAX
== tok
) {
494 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
495 ln
, ppos
, buf
+ ppos
- 1);
499 /* The macro is sane. Jump to the next word. */
501 while (buf
[offs
] && ' ' == buf
[offs
])
505 * Trailing whitespace. Note that tabs are allowed to be passed
506 * into the parser as "text", so we only warn about spaces here.
509 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
510 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
514 * Remove prior ELINE macro, as it's being clobbered by a new
515 * macro. Note that NSCOPED macros do not close out ELINE
516 * macros---they don't print text---so we let those slip by.
519 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
520 man
->flags
& MAN_ELINE
) {
522 assert(MAN_TEXT
!= n
->type
);
524 /* Remove repeated NSCOPED macros causing ELINE. */
526 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
529 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
530 n
->pos
, "%s breaks %s", man_macronames
[tok
],
531 man_macronames
[n
->tok
]);
533 man_node_delete(man
, n
);
534 man
->flags
&= ~MAN_ELINE
;
538 * Remove prior BLINE macro that is being clobbered.
540 if ((man
->flags
& MAN_BLINE
) &&
541 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
544 /* Might be a text node like 8 in
548 if (MAN_TEXT
== n
->type
)
551 /* Remove element that didn't end BLINE, if any. */
552 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
555 assert(MAN_HEAD
== n
->type
);
557 assert(MAN_BLOCK
== n
->type
);
558 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
560 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
561 n
->pos
, "%s breaks %s", man_macronames
[tok
],
562 man_macronames
[n
->tok
]);
564 man_node_delete(man
, n
);
565 man
->flags
&= ~MAN_BLINE
;
568 /* Remember whether we are in next-line scope for a block head. */
570 bline
= man
->flags
& MAN_BLINE
;
572 /* Call to handler... */
574 assert(man_macros
[tok
].fp
);
575 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
578 /* In quick mode (for mandocdb), abort after the NAME section. */
580 if (man
->quick
&& MAN_SH
== tok
) {
582 if (MAN_BODY
== n
->type
&&
583 strcmp(n
->prev
->child
->string
, "NAME"))
588 * If we are in a next-line scope for a block head,
589 * close it out now and switch to the body,
590 * unless the next-line scope is allowed to continue.
593 if ( ! bline
|| man
->flags
& MAN_ELINE
||
594 man_macros
[tok
].flags
& MAN_NSCOPED
)
597 assert(MAN_BLINE
& man
->flags
);
598 man
->flags
&= ~MAN_BLINE
;
600 if ( ! man_unscope(man
, man
->last
->parent
))
602 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
606 * Unlink a node from its context. If "man" is provided, the last parse
607 * point will also be adjusted accordingly.
610 man_node_unlink(struct man
*man
, struct man_node
*n
)
613 /* Adjust siblings. */
616 n
->prev
->next
= n
->next
;
618 n
->next
->prev
= n
->prev
;
624 if (n
->parent
->child
== n
)
625 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
628 /* Adjust parse point, if applicable. */
630 if (man
&& man
->last
== n
) {
631 /*XXX: this can occur when bailing from validation. */
632 /*assert(NULL == n->next);*/
635 man
->next
= MAN_NEXT_SIBLING
;
637 man
->last
= n
->parent
;
638 man
->next
= MAN_NEXT_CHILD
;
642 if (man
&& man
->first
== n
)
646 const struct mparse
*
647 man_mparse(const struct man
*man
)
650 assert(man
&& man
->parse
);
655 man_deroff(char **dest
, const struct man_node
*n
)
660 if (MAN_TEXT
!= n
->type
) {
661 for (n
= n
->child
; n
; n
= n
->next
)
666 /* Skip leading whitespace and escape sequences. */
669 while ('\0' != *cp
) {
672 mandoc_escape((const char **)&cp
, NULL
, NULL
);
673 } else if (isspace((unsigned char)*cp
))
679 /* Skip trailing whitespace. */
681 for (sz
= strlen(cp
); sz
; sz
--)
682 if (0 == isspace((unsigned char)cp
[sz
-1]))
685 /* Skip empty strings. */
691 *dest
= mandoc_strndup(cp
, sz
);
695 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);