]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.128 2014/03/30 19:47:48 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
));
76 const struct man_meta
*
77 man_meta(const struct man
*man
)
80 assert( ! (MAN_HALT
& man
->flags
));
86 man_reset(struct man
*man
)
95 man_free(struct man
*man
)
104 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
108 p
= mandoc_calloc(1, sizeof(struct man
));
121 man_endparse(struct man
*man
)
124 assert( ! (MAN_HALT
& man
->flags
));
125 if (man_macroend(man
))
127 man
->flags
|= MAN_HALT
;
133 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
136 man
->flags
|= MAN_NEWLINE
;
138 assert( ! (MAN_HALT
& man
->flags
));
140 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
141 man_pmacro(man
, ln
, buf
, offs
) :
142 man_ptext(man
, ln
, buf
, offs
));
147 man_free1(struct man
*man
)
151 man_node_delete(man
, man
->first
);
153 free(man
->meta
.title
);
154 if (man
->meta
.source
)
155 free(man
->meta
.source
);
157 free(man
->meta
.date
);
161 free(man
->meta
.msec
);
166 man_alloc1(struct man
*man
)
169 memset(&man
->meta
, 0, sizeof(struct man_meta
));
171 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
172 man
->first
= man
->last
;
173 man
->last
->type
= MAN_ROOT
;
174 man
->last
->tok
= MAN_MAX
;
175 man
->next
= MAN_NEXT_CHILD
;
180 man_node_append(struct man
*man
, struct man_node
*p
)
185 assert(MAN_ROOT
!= p
->type
);
188 case (MAN_NEXT_SIBLING
):
191 p
->parent
= man
->last
->parent
;
193 case (MAN_NEXT_CHILD
):
194 man
->last
->child
= p
;
195 p
->parent
= man
->last
;
205 if ( ! man_valid_pre(man
, p
))
210 assert(MAN_BLOCK
== p
->parent
->type
);
214 assert(MAN_BLOCK
== p
->parent
->type
);
218 assert(MAN_BLOCK
== p
->parent
->type
);
231 if ( ! man_valid_post(man
))
242 static struct man_node
*
243 man_node_alloc(struct man
*man
, int line
, int pos
,
244 enum man_type type
, enum mant tok
)
248 p
= mandoc_calloc(1, sizeof(struct man_node
));
254 if (MAN_NEWLINE
& man
->flags
)
255 p
->flags
|= MAN_LINE
;
256 man
->flags
&= ~MAN_NEWLINE
;
262 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
266 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
267 if ( ! man_node_append(man
, p
))
269 man
->next
= MAN_NEXT_CHILD
;
275 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
279 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
280 if ( ! man_node_append(man
, p
))
282 man
->next
= MAN_NEXT_CHILD
;
288 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
292 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
293 if ( ! man_node_append(man
, p
))
295 man
->next
= MAN_NEXT_CHILD
;
301 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
305 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
306 if ( ! man_node_append(man
, p
))
308 man
->next
= MAN_NEXT_CHILD
;
314 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
318 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
319 if ( ! man_node_append(man
, p
))
321 man
->next
= MAN_NEXT_CHILD
;
326 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
330 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
331 n
->string
= roff_strdup(man
->roff
, word
);
333 if ( ! man_node_append(man
, n
))
336 man
->next
= MAN_NEXT_SIBLING
;
342 * Free all of the resources held by a node. This does NOT unlink a
343 * node from its context; for that, see man_node_unlink().
346 man_node_free(struct man_node
*p
)
356 man_node_delete(struct man
*man
, struct man_node
*p
)
360 man_node_delete(man
, p
->child
);
362 man_node_unlink(man
, p
);
367 man_addeqn(struct man
*man
, const struct eqn
*ep
)
371 assert( ! (MAN_HALT
& man
->flags
));
373 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
376 if ( ! man_node_append(man
, n
))
379 man
->next
= MAN_NEXT_SIBLING
;
380 return(man_descope(man
, ep
->ln
, ep
->pos
));
384 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
388 assert( ! (MAN_HALT
& man
->flags
));
390 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
393 if ( ! man_node_append(man
, n
))
396 man
->next
= MAN_NEXT_SIBLING
;
397 return(man_descope(man
, sp
->line
, 0));
401 man_descope(struct man
*man
, int line
, int offs
)
404 * Co-ordinate what happens with having a next-line scope open:
405 * first close out the element scope (if applicable), then close
406 * out the block scope (also if applicable).
409 if (MAN_ELINE
& man
->flags
) {
410 man
->flags
&= ~MAN_ELINE
;
411 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
415 if ( ! (MAN_BLINE
& man
->flags
))
417 man
->flags
&= ~MAN_BLINE
;
419 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
421 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
425 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
429 /* Literal free-form text whitespace is preserved. */
431 if (MAN_LITERAL
& man
->flags
) {
432 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
434 return(man_descope(man
, line
, offs
));
437 for (i
= offs
; ' ' == buf
[i
]; i
++)
438 /* Skip leading whitespace. */ ;
441 * Blank lines are ignored right after headings
442 * but add a single vertical space elsewhere.
445 if ('\0' == buf
[i
]) {
446 /* Allocate a blank entry. */
447 if (MAN_SH
!= man
->last
->tok
&&
448 MAN_SS
!= man
->last
->tok
) {
449 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
451 man
->next
= MAN_NEXT_SIBLING
;
457 * Warn if the last un-escaped character is whitespace. Then
458 * strip away the remaining spaces (tabs stay!).
461 i
= (int)strlen(buf
);
464 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
465 if (i
> 1 && '\\' != buf
[i
- 2])
466 man_pmsg(man
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
468 for (--i
; i
&& ' ' == buf
[i
]; i
--)
469 /* Spin back to non-space. */ ;
471 /* Jump ahead of escaped whitespace. */
472 i
+= '\\' == buf
[i
] ? 2 : 1;
477 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
481 * End-of-sentence check. If the last character is an unescaped
482 * EOS character, then flag the node as being the end of a
483 * sentence. The front-end will know how to interpret this.
487 if (mandoc_eos(buf
, (size_t)i
))
488 man
->last
->flags
|= MAN_EOS
;
490 return(man_descope(man
, line
, offs
));
494 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
501 if ('"' == buf
[offs
]) {
502 man_pmsg(man
, ln
, offs
, MANDOCERR_BADCOMMENT
);
504 } else if ('\0' == buf
[offs
])
510 * Copy the first word into a nil-terminated buffer.
511 * Stop copying when a tab, space, or eoln is encountered.
515 while (i
< 4 && '\0' != buf
[offs
] &&
516 ' ' != buf
[offs
] && '\t' != buf
[offs
])
517 mac
[i
++] = buf
[offs
++];
521 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
523 if (MAN_MAX
== tok
) {
524 mandoc_vmsg(MANDOCERR_MACRO
, man
->parse
, ln
,
525 ppos
, "%s", buf
+ ppos
- 1);
529 /* The macro is sane. Jump to the next word. */
531 while (buf
[offs
] && ' ' == buf
[offs
])
535 * Trailing whitespace. Note that tabs are allowed to be passed
536 * into the parser as "text", so we only warn about spaces here.
539 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
540 man_pmsg(man
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
543 * Remove prior ELINE macro, as it's being clobbered by a new
544 * macro. Note that NSCOPED macros do not close out ELINE
545 * macros---they don't print text---so we let those slip by.
548 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
549 man
->flags
& MAN_ELINE
) {
551 assert(MAN_TEXT
!= n
->type
);
553 /* Remove repeated NSCOPED macros causing ELINE. */
555 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
558 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
559 n
->pos
, "%s breaks %s", man_macronames
[tok
],
560 man_macronames
[n
->tok
]);
562 man_node_delete(man
, n
);
563 man
->flags
&= ~MAN_ELINE
;
567 * Remove prior BLINE macro that is being clobbered.
569 if ((man
->flags
& MAN_BLINE
) &&
570 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
573 /* Might be a text node like 8 in
577 if (MAN_TEXT
== n
->type
)
580 /* Remove element that didn't end BLINE, if any. */
581 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
584 assert(MAN_HEAD
== n
->type
);
586 assert(MAN_BLOCK
== n
->type
);
587 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
589 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
590 n
->pos
, "%s breaks %s", man_macronames
[tok
],
591 man_macronames
[n
->tok
]);
593 man_node_delete(man
, n
);
594 man
->flags
&= ~MAN_BLINE
;
598 * Save the fact that we're in the next-line for a block. In
599 * this way, embedded roff instructions can "remember" state
603 if (MAN_BLINE
& man
->flags
)
604 man
->flags
|= MAN_BPLINE
;
606 /* Call to handler... */
608 assert(man_macros
[tok
].fp
);
609 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
612 /* In quick mode (for mandocdb), abort after the NAME section. */
614 if (man
->quick
&& MAN_SH
== tok
&&
615 strcmp(man
->last
->prev
->child
->string
, "NAME"))
619 * We weren't in a block-line scope when entering the
620 * above-parsed macro, so return.
623 if ( ! (MAN_BPLINE
& man
->flags
)) {
624 man
->flags
&= ~MAN_ILINE
;
627 man
->flags
&= ~MAN_BPLINE
;
630 * If we're in a block scope, then allow this macro to slip by
631 * without closing scope around it.
634 if (MAN_ILINE
& man
->flags
) {
635 man
->flags
&= ~MAN_ILINE
;
640 * If we've opened a new next-line element scope, then return
641 * now, as the next line will close out the block scope.
644 if (MAN_ELINE
& man
->flags
)
647 /* Close out the block scope opened in the prior line. */
649 assert(MAN_BLINE
& man
->flags
);
650 man
->flags
&= ~MAN_BLINE
;
652 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
654 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
656 err
: /* Error out. */
658 man
->flags
|= MAN_HALT
;
663 * Unlink a node from its context. If "man" is provided, the last parse
664 * point will also be adjusted accordingly.
667 man_node_unlink(struct man
*man
, struct man_node
*n
)
670 /* Adjust siblings. */
673 n
->prev
->next
= n
->next
;
675 n
->next
->prev
= n
->prev
;
681 if (n
->parent
->child
== n
)
682 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
685 /* Adjust parse point, if applicable. */
687 if (man
&& man
->last
== n
) {
688 /*XXX: this can occur when bailing from validation. */
689 /*assert(NULL == n->next);*/
692 man
->next
= MAN_NEXT_SIBLING
;
694 man
->last
= n
->parent
;
695 man
->next
= MAN_NEXT_CHILD
;
699 if (man
&& man
->first
== n
)
703 const struct mparse
*
704 man_mparse(const struct man
*man
)
707 assert(man
&& man
->parse
);
712 man_deroff(char **dest
, const struct man_node
*n
)
717 if (MAN_TEXT
!= n
->type
) {
718 for (n
= n
->child
; n
; n
= n
->next
)
723 /* Skip leading whitespace and escape sequences. */
726 while ('\0' != *cp
) {
729 mandoc_escape((const char **)&cp
, NULL
, NULL
);
730 } else if (isspace((unsigned char)*cp
))
736 /* Skip trailing whitespace. */
738 for (sz
= strlen(cp
); sz
; sz
--)
739 if (0 == isspace((unsigned char)cp
[sz
-1]))
742 /* Skip empty strings. */
748 *dest
= mandoc_strndup(cp
, sz
);
752 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);