1 /* $Id: man.c,v 1.119 2012/11/17 00:26:33 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
32 #include "libmandoc.h"
34 const char *const __man_macronames
[MAN_MAX
] = {
35 "br", "TH", "SH", "SS",
36 "TP", "LP", "PP", "P",
37 "IP", "HP", "SM", "SB",
38 "BI", "IB", "BR", "RB",
40 "RI", "na", "sp", "nf",
41 "fi", "RE", "RS", "DT",
42 "UC", "PD", "AT", "in",
43 "ft", "OP", "EX", "EE"
46 const char * const *man_macronames
= __man_macronames
;
48 static struct man_node
*man_node_alloc(struct man
*, int, int,
49 enum man_type
, enum mant
);
50 static int man_node_append(struct man
*,
52 static void man_node_free(struct man_node
*);
53 static void man_node_unlink(struct man
*,
55 static int man_ptext(struct man
*, int, char *, int);
56 static int man_pmacro(struct man
*, int, char *, int);
57 static void man_free1(struct man
*);
58 static void man_alloc1(struct man
*);
59 static int man_descope(struct man
*, int, int);
62 const struct man_node
*
63 man_node(const struct man
*man
)
66 assert( ! (MAN_HALT
& man
->flags
));
71 const struct man_meta
*
72 man_meta(const struct man
*man
)
75 assert( ! (MAN_HALT
& man
->flags
));
81 man_reset(struct man
*man
)
90 man_free(struct man
*man
)
99 man_alloc(struct roff
*roff
, struct mparse
*parse
)
103 p
= mandoc_calloc(1, sizeof(struct man
));
115 man_endparse(struct man
*man
)
118 assert( ! (MAN_HALT
& man
->flags
));
119 if (man_macroend(man
))
121 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
));
141 man_free1(struct man
*man
)
145 man_node_delete(man
, man
->first
);
147 free(man
->meta
.title
);
148 if (man
->meta
.source
)
149 free(man
->meta
.source
);
151 free(man
->meta
.date
);
155 free(man
->meta
.msec
);
160 man_alloc1(struct man
*man
)
163 memset(&man
->meta
, 0, sizeof(struct man_meta
));
165 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
166 man
->first
= man
->last
;
167 man
->last
->type
= MAN_ROOT
;
168 man
->last
->tok
= MAN_MAX
;
169 man
->next
= MAN_NEXT_CHILD
;
174 man_node_append(struct man
*man
, struct man_node
*p
)
179 assert(MAN_ROOT
!= p
->type
);
182 case (MAN_NEXT_SIBLING
):
185 p
->parent
= man
->last
->parent
;
187 case (MAN_NEXT_CHILD
):
188 man
->last
->child
= p
;
189 p
->parent
= man
->last
;
199 if ( ! man_valid_pre(man
, p
))
204 assert(MAN_BLOCK
== p
->parent
->type
);
208 assert(MAN_BLOCK
== p
->parent
->type
);
212 assert(MAN_BLOCK
== p
->parent
->type
);
225 if ( ! man_valid_post(man
))
236 static struct man_node
*
237 man_node_alloc(struct man
*man
, int line
, int pos
,
238 enum man_type type
, enum mant tok
)
242 p
= mandoc_calloc(1, sizeof(struct man_node
));
248 if (MAN_NEWLINE
& man
->flags
)
249 p
->flags
|= MAN_LINE
;
250 man
->flags
&= ~MAN_NEWLINE
;
256 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
260 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
261 if ( ! man_node_append(man
, p
))
263 man
->next
= MAN_NEXT_CHILD
;
269 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
273 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
274 if ( ! man_node_append(man
, p
))
276 man
->next
= MAN_NEXT_CHILD
;
282 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
286 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
287 if ( ! man_node_append(man
, p
))
289 man
->next
= MAN_NEXT_CHILD
;
295 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
299 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
300 if ( ! man_node_append(man
, p
))
302 man
->next
= MAN_NEXT_CHILD
;
308 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
312 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
313 if ( ! man_node_append(man
, p
))
315 man
->next
= MAN_NEXT_CHILD
;
320 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
324 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
325 n
->string
= roff_strdup(man
->roff
, word
);
327 if ( ! man_node_append(man
, n
))
330 man
->next
= MAN_NEXT_SIBLING
;
336 * Free all of the resources held by a node. This does NOT unlink a
337 * node from its context; for that, see man_node_unlink().
340 man_node_free(struct man_node
*p
)
350 man_node_delete(struct man
*man
, struct man_node
*p
)
354 man_node_delete(man
, p
->child
);
356 man_node_unlink(man
, p
);
361 man_addeqn(struct man
*man
, const struct eqn
*ep
)
365 assert( ! (MAN_HALT
& man
->flags
));
367 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
370 if ( ! man_node_append(man
, n
))
373 man
->next
= MAN_NEXT_SIBLING
;
374 return(man_descope(man
, ep
->ln
, ep
->pos
));
378 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
382 assert( ! (MAN_HALT
& man
->flags
));
384 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
387 if ( ! man_node_append(man
, n
))
390 man
->next
= MAN_NEXT_SIBLING
;
391 return(man_descope(man
, sp
->line
, 0));
395 man_descope(struct man
*man
, int line
, int offs
)
398 * Co-ordinate what happens with having a next-line scope open:
399 * first close out the element scope (if applicable), then close
400 * out the block scope (also if applicable).
403 if (MAN_ELINE
& man
->flags
) {
404 man
->flags
&= ~MAN_ELINE
;
405 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
409 if ( ! (MAN_BLINE
& man
->flags
))
411 man
->flags
&= ~MAN_BLINE
;
413 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
415 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
419 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
423 /* Literal free-form text whitespace is preserved. */
425 if (MAN_LITERAL
& man
->flags
) {
426 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
428 return(man_descope(man
, line
, offs
));
431 /* Pump blank lines directly into the backend. */
433 for (i
= offs
; ' ' == buf
[i
]; i
++)
434 /* Skip leading whitespace. */ ;
436 if ('\0' == buf
[i
]) {
437 /* Allocate a blank entry. */
438 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
440 man
->next
= MAN_NEXT_SIBLING
;
445 * Warn if the last un-escaped character is whitespace. Then
446 * strip away the remaining spaces (tabs stay!).
449 i
= (int)strlen(buf
);
452 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
453 if (i
> 1 && '\\' != buf
[i
- 2])
454 man_pmsg(man
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
456 for (--i
; i
&& ' ' == buf
[i
]; i
--)
457 /* Spin back to non-space. */ ;
459 /* Jump ahead of escaped whitespace. */
460 i
+= '\\' == buf
[i
] ? 2 : 1;
465 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
469 * End-of-sentence check. If the last character is an unescaped
470 * EOS character, then flag the node as being the end of a
471 * sentence. The front-end will know how to interpret this.
475 if (mandoc_eos(buf
, (size_t)i
, 0))
476 man
->last
->flags
|= MAN_EOS
;
478 return(man_descope(man
, line
, offs
));
482 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
489 if ('"' == buf
[offs
]) {
490 man_pmsg(man
, ln
, offs
, MANDOCERR_BADCOMMENT
);
492 } else if ('\0' == buf
[offs
])
498 * Copy the first word into a nil-terminated buffer.
499 * Stop copying when a tab, space, or eoln is encountered.
503 while (i
< 4 && '\0' != buf
[offs
] &&
504 ' ' != buf
[offs
] && '\t' != buf
[offs
])
505 mac
[i
++] = buf
[offs
++];
509 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
511 if (MAN_MAX
== tok
) {
512 mandoc_vmsg(MANDOCERR_MACRO
, man
->parse
, ln
,
513 ppos
, "%s", buf
+ ppos
- 1);
517 /* The macro is sane. Jump to the next word. */
519 while (buf
[offs
] && ' ' == buf
[offs
])
523 * Trailing whitespace. Note that tabs are allowed to be passed
524 * into the parser as "text", so we only warn about spaces here.
527 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
528 man_pmsg(man
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
531 * Remove prior ELINE macro, as it's being clobbered by a new
532 * macro. Note that NSCOPED macros do not close out ELINE
533 * macros---they don't print text---so we let those slip by.
536 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
537 man
->flags
& MAN_ELINE
) {
539 assert(MAN_TEXT
!= n
->type
);
541 /* Remove repeated NSCOPED macros causing ELINE. */
543 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
546 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
547 n
->pos
, "%s breaks %s", man_macronames
[tok
],
548 man_macronames
[n
->tok
]);
550 man_node_delete(man
, n
);
551 man
->flags
&= ~MAN_ELINE
;
555 * Remove prior BLINE macro that is being clobbered.
557 if ((man
->flags
& MAN_BLINE
) &&
558 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
561 /* Might be a text node like 8 in
565 if (MAN_TEXT
== n
->type
)
568 /* Remove element that didn't end BLINE, if any. */
569 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
572 assert(MAN_HEAD
== n
->type
);
574 assert(MAN_BLOCK
== n
->type
);
575 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
577 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
578 n
->pos
, "%s breaks %s", man_macronames
[tok
],
579 man_macronames
[n
->tok
]);
581 man_node_delete(man
, n
);
582 man
->flags
&= ~MAN_BLINE
;
586 * Save the fact that we're in the next-line for a block. In
587 * this way, embedded roff instructions can "remember" state
591 if (MAN_BLINE
& man
->flags
)
592 man
->flags
|= MAN_BPLINE
;
594 /* Call to handler... */
596 assert(man_macros
[tok
].fp
);
597 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
601 * We weren't in a block-line scope when entering the
602 * above-parsed macro, so return.
605 if ( ! (MAN_BPLINE
& man
->flags
)) {
606 man
->flags
&= ~MAN_ILINE
;
609 man
->flags
&= ~MAN_BPLINE
;
612 * If we're in a block scope, then allow this macro to slip by
613 * without closing scope around it.
616 if (MAN_ILINE
& man
->flags
) {
617 man
->flags
&= ~MAN_ILINE
;
622 * If we've opened a new next-line element scope, then return
623 * now, as the next line will close out the block scope.
626 if (MAN_ELINE
& man
->flags
)
629 /* Close out the block scope opened in the prior line. */
631 assert(MAN_BLINE
& man
->flags
);
632 man
->flags
&= ~MAN_BLINE
;
634 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
636 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
638 err
: /* Error out. */
640 man
->flags
|= MAN_HALT
;
645 * Unlink a node from its context. If "man" is provided, the last parse
646 * point will also be adjusted accordingly.
649 man_node_unlink(struct man
*man
, struct man_node
*n
)
652 /* Adjust siblings. */
655 n
->prev
->next
= n
->next
;
657 n
->next
->prev
= n
->prev
;
663 if (n
->parent
->child
== n
)
664 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
667 /* Adjust parse point, if applicable. */
669 if (man
&& man
->last
== n
) {
670 /*XXX: this can occur when bailing from validation. */
671 /*assert(NULL == n->next);*/
674 man
->next
= MAN_NEXT_SIBLING
;
676 man
->last
= n
->parent
;
677 man
->next
= MAN_NEXT_CHILD
;
681 if (man
&& man
->first
== n
)
685 const struct mparse
*
686 man_mparse(const struct man
*man
)
689 assert(man
&& man
->parse
);