]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.138 2014/08/10 23:54:41 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 man
->flags
|= MAN_NEWLINE
;
124 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
125 man_pmacro(man
, ln
, buf
, offs
) :
126 man_ptext(man
, ln
, buf
, offs
));
130 man_free1(struct man
*man
)
134 man_node_delete(man
, man
->first
);
136 free(man
->meta
.title
);
137 if (man
->meta
.source
)
138 free(man
->meta
.source
);
140 free(man
->meta
.date
);
144 free(man
->meta
.msec
);
148 man_alloc1(struct man
*man
)
151 memset(&man
->meta
, 0, sizeof(struct man_meta
));
153 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
154 man
->first
= man
->last
;
155 man
->last
->type
= MAN_ROOT
;
156 man
->last
->tok
= MAN_MAX
;
157 man
->next
= MAN_NEXT_CHILD
;
162 man_node_append(struct man
*man
, struct man_node
*p
)
167 assert(MAN_ROOT
!= p
->type
);
170 case MAN_NEXT_SIBLING
:
173 p
->parent
= man
->last
->parent
;
176 man
->last
->child
= p
;
177 p
->parent
= man
->last
;
189 if (p
->tok
== MAN_SH
|| p
->tok
== MAN_SS
)
190 man
->flags
&= ~MAN_LITERAL
;
193 assert(MAN_BLOCK
== p
->parent
->type
);
197 assert(MAN_BLOCK
== p
->parent
->type
);
201 assert(MAN_BLOCK
== p
->parent
->type
);
214 if ( ! man_valid_post(man
))
224 static struct man_node
*
225 man_node_alloc(struct man
*man
, int line
, int pos
,
226 enum man_type type
, enum mant tok
)
230 p
= mandoc_calloc(1, sizeof(struct man_node
));
236 if (MAN_NEWLINE
& man
->flags
)
237 p
->flags
|= MAN_LINE
;
238 man
->flags
&= ~MAN_NEWLINE
;
243 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
247 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
248 if ( ! man_node_append(man
, p
))
250 man
->next
= MAN_NEXT_CHILD
;
255 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
259 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
260 if ( ! man_node_append(man
, p
))
262 man
->next
= MAN_NEXT_CHILD
;
267 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
271 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
272 if ( ! man_node_append(man
, p
))
274 man
->next
= MAN_NEXT_CHILD
;
279 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
283 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
284 if ( ! man_node_append(man
, p
))
286 man
->next
= MAN_NEXT_CHILD
;
291 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
295 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
296 if ( ! man_node_append(man
, p
))
298 man
->next
= MAN_NEXT_CHILD
;
303 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
307 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
308 n
->string
= roff_strdup(man
->roff
, word
);
310 if ( ! man_node_append(man
, n
))
313 man
->next
= MAN_NEXT_SIBLING
;
318 * Free all of the resources held by a node. This does NOT unlink a
319 * node from its context; for that, see man_node_unlink().
322 man_node_free(struct man_node
*p
)
331 man_node_delete(struct man
*man
, struct man_node
*p
)
335 man_node_delete(man
, p
->child
);
337 man_node_unlink(man
, p
);
342 man_addeqn(struct man
*man
, const struct eqn
*ep
)
346 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
349 if ( ! man_node_append(man
, n
))
352 man
->next
= MAN_NEXT_SIBLING
;
353 return(man_descope(man
, ep
->ln
, ep
->pos
));
357 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
361 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
364 if ( ! man_node_append(man
, n
))
367 man
->next
= MAN_NEXT_SIBLING
;
368 return(man_descope(man
, sp
->line
, 0));
372 man_descope(struct man
*man
, int line
, int offs
)
375 * Co-ordinate what happens with having a next-line scope open:
376 * first close out the element scope (if applicable), then close
377 * out the block scope (also if applicable).
380 if (MAN_ELINE
& man
->flags
) {
381 man
->flags
&= ~MAN_ELINE
;
382 if ( ! man_unscope(man
, man
->last
->parent
))
386 if ( ! (MAN_BLINE
& man
->flags
))
388 man
->flags
&= ~MAN_BLINE
;
390 if ( ! man_unscope(man
, man
->last
->parent
))
392 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
396 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
400 /* Literal free-form text whitespace is preserved. */
402 if (MAN_LITERAL
& man
->flags
) {
403 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
405 return(man_descope(man
, line
, offs
));
408 for (i
= offs
; ' ' == buf
[i
]; i
++)
409 /* Skip leading whitespace. */ ;
412 * Blank lines are ignored right after headings
413 * but add a single vertical space elsewhere.
416 if ('\0' == buf
[i
]) {
417 /* Allocate a blank entry. */
418 if (MAN_SH
!= man
->last
->tok
&&
419 MAN_SS
!= man
->last
->tok
) {
420 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
422 man
->next
= MAN_NEXT_SIBLING
;
428 * Warn if the last un-escaped character is whitespace. Then
429 * strip away the remaining spaces (tabs stay!).
432 i
= (int)strlen(buf
);
435 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
436 if (i
> 1 && '\\' != buf
[i
- 2])
437 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
440 for (--i
; i
&& ' ' == buf
[i
]; i
--)
441 /* Spin back to non-space. */ ;
443 /* Jump ahead of escaped whitespace. */
444 i
+= '\\' == buf
[i
] ? 2 : 1;
449 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
453 * End-of-sentence check. If the last character is an unescaped
454 * EOS character, then flag the node as being the end of a
455 * sentence. The front-end will know how to interpret this.
459 if (mandoc_eos(buf
, (size_t)i
))
460 man
->last
->flags
|= MAN_EOS
;
462 return(man_descope(man
, line
, offs
));
466 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
474 if ('"' == buf
[offs
]) {
475 mandoc_msg(MANDOCERR_COMMENT_BAD
, man
->parse
,
478 } else if ('\0' == buf
[offs
])
484 * Copy the first word into a nil-terminated buffer.
485 * Stop copying when a tab, space, or eoln is encountered.
489 while (i
< 4 && '\0' != buf
[offs
] && ' ' != buf
[offs
] &&
491 mac
[i
++] = buf
[offs
++];
495 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
497 if (MAN_MAX
== tok
) {
498 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
499 ln
, ppos
, buf
+ ppos
- 1);
503 /* The macro is sane. Jump to the next word. */
505 while (buf
[offs
] && ' ' == buf
[offs
])
509 * Trailing whitespace. Note that tabs are allowed to be passed
510 * into the parser as "text", so we only warn about spaces here.
513 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
514 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
518 * Remove prior ELINE macro, as it's being clobbered by a new
519 * macro. Note that NSCOPED macros do not close out ELINE
520 * macros---they don't print text---so we let those slip by.
523 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
524 man
->flags
& MAN_ELINE
) {
526 assert(MAN_TEXT
!= n
->type
);
528 /* Remove repeated NSCOPED macros causing ELINE. */
530 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
533 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
534 n
->pos
, "%s breaks %s", man_macronames
[tok
],
535 man_macronames
[n
->tok
]);
537 man_node_delete(man
, n
);
538 man
->flags
&= ~MAN_ELINE
;
542 * Remove prior BLINE macro that is being clobbered.
544 if ((man
->flags
& MAN_BLINE
) &&
545 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
548 /* Might be a text node like 8 in
552 if (MAN_TEXT
== n
->type
)
555 /* Remove element that didn't end BLINE, if any. */
556 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
559 assert(MAN_HEAD
== n
->type
);
561 assert(MAN_BLOCK
== n
->type
);
562 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
564 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
565 n
->pos
, "%s breaks %s", man_macronames
[tok
],
566 man_macronames
[n
->tok
]);
568 man_node_delete(man
, n
);
569 man
->flags
&= ~MAN_BLINE
;
572 /* Remember whether we are in next-line scope for a block head. */
574 bline
= man
->flags
& MAN_BLINE
;
576 /* Call to handler... */
578 assert(man_macros
[tok
].fp
);
579 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
582 /* In quick mode (for mandocdb), abort after the NAME section. */
584 if (man
->quick
&& MAN_SH
== tok
) {
586 if (MAN_BODY
== n
->type
&&
587 strcmp(n
->prev
->child
->string
, "NAME"))
592 * If we are in a next-line scope for a block head,
593 * close it out now and switch to the body,
594 * unless the next-line scope is allowed to continue.
597 if ( ! bline
|| man
->flags
& MAN_ELINE
||
598 man_macros
[tok
].flags
& MAN_NSCOPED
)
601 assert(MAN_BLINE
& man
->flags
);
602 man
->flags
&= ~MAN_BLINE
;
604 if ( ! man_unscope(man
, man
->last
->parent
))
606 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
610 * Unlink a node from its context. If "man" is provided, the last parse
611 * point will also be adjusted accordingly.
614 man_node_unlink(struct man
*man
, struct man_node
*n
)
617 /* Adjust siblings. */
620 n
->prev
->next
= n
->next
;
622 n
->next
->prev
= n
->prev
;
628 if (n
->parent
->child
== n
)
629 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
632 /* Adjust parse point, if applicable. */
634 if (man
&& man
->last
== n
) {
635 /*XXX: this can occur when bailing from validation. */
636 /*assert(NULL == n->next);*/
639 man
->next
= MAN_NEXT_SIBLING
;
641 man
->last
= n
->parent
;
642 man
->next
= MAN_NEXT_CHILD
;
646 if (man
&& man
->first
== n
)
650 const struct mparse
*
651 man_mparse(const struct man
*man
)
654 assert(man
&& man
->parse
);
659 man_deroff(char **dest
, const struct man_node
*n
)
664 if (MAN_TEXT
!= n
->type
) {
665 for (n
= n
->child
; n
; n
= n
->next
)
670 /* Skip leading whitespace and escape sequences. */
673 while ('\0' != *cp
) {
676 mandoc_escape((const char **)&cp
, NULL
, NULL
);
677 } else if (isspace((unsigned char)*cp
))
683 /* Skip trailing whitespace. */
685 for (sz
= strlen(cp
); sz
; sz
--)
686 if (0 == isspace((unsigned char)cp
[sz
-1]))
689 /* Skip empty strings. */
695 *dest
= mandoc_strndup(cp
, sz
);
699 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);