]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.124 2014/01/06 00:53:33 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 "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
)
69 assert( ! (MAN_HALT
& man
->flags
));
74 const struct man_meta
*
75 man_meta(const struct man
*man
)
78 assert( ! (MAN_HALT
& man
->flags
));
84 man_reset(struct man
*man
)
93 man_free(struct man
*man
)
102 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
106 p
= mandoc_calloc(1, sizeof(struct man
));
119 man_endparse(struct man
*man
)
122 assert( ! (MAN_HALT
& man
->flags
));
123 if (man_macroend(man
))
125 man
->flags
|= MAN_HALT
;
131 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
134 man
->flags
|= MAN_NEWLINE
;
136 assert( ! (MAN_HALT
& man
->flags
));
138 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
139 man_pmacro(man
, ln
, buf
, offs
) :
140 man_ptext(man
, ln
, buf
, offs
));
145 man_free1(struct man
*man
)
149 man_node_delete(man
, man
->first
);
151 free(man
->meta
.title
);
152 if (man
->meta
.source
)
153 free(man
->meta
.source
);
155 free(man
->meta
.date
);
159 free(man
->meta
.msec
);
164 man_alloc1(struct man
*man
)
167 memset(&man
->meta
, 0, sizeof(struct man_meta
));
169 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
170 man
->first
= man
->last
;
171 man
->last
->type
= MAN_ROOT
;
172 man
->last
->tok
= MAN_MAX
;
173 man
->next
= MAN_NEXT_CHILD
;
178 man_node_append(struct man
*man
, struct man_node
*p
)
183 assert(MAN_ROOT
!= p
->type
);
186 case (MAN_NEXT_SIBLING
):
189 p
->parent
= man
->last
->parent
;
191 case (MAN_NEXT_CHILD
):
192 man
->last
->child
= p
;
193 p
->parent
= man
->last
;
203 if ( ! man_valid_pre(man
, p
))
208 assert(MAN_BLOCK
== p
->parent
->type
);
212 assert(MAN_BLOCK
== p
->parent
->type
);
216 assert(MAN_BLOCK
== p
->parent
->type
);
229 if ( ! man_valid_post(man
))
240 static struct man_node
*
241 man_node_alloc(struct man
*man
, int line
, int pos
,
242 enum man_type type
, enum mant tok
)
246 p
= mandoc_calloc(1, sizeof(struct man_node
));
252 if (MAN_NEWLINE
& man
->flags
)
253 p
->flags
|= MAN_LINE
;
254 man
->flags
&= ~MAN_NEWLINE
;
260 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
264 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
265 if ( ! man_node_append(man
, p
))
267 man
->next
= MAN_NEXT_CHILD
;
273 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
277 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
278 if ( ! man_node_append(man
, p
))
280 man
->next
= MAN_NEXT_CHILD
;
286 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
290 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
291 if ( ! man_node_append(man
, p
))
293 man
->next
= MAN_NEXT_CHILD
;
299 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
303 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
304 if ( ! man_node_append(man
, p
))
306 man
->next
= MAN_NEXT_CHILD
;
312 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
316 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
317 if ( ! man_node_append(man
, p
))
319 man
->next
= MAN_NEXT_CHILD
;
324 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
328 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
329 n
->string
= roff_strdup(man
->roff
, word
);
331 if ( ! man_node_append(man
, n
))
334 man
->next
= MAN_NEXT_SIBLING
;
340 * Free all of the resources held by a node. This does NOT unlink a
341 * node from its context; for that, see man_node_unlink().
344 man_node_free(struct man_node
*p
)
354 man_node_delete(struct man
*man
, struct man_node
*p
)
358 man_node_delete(man
, p
->child
);
360 man_node_unlink(man
, p
);
365 man_addeqn(struct man
*man
, const struct eqn
*ep
)
369 assert( ! (MAN_HALT
& man
->flags
));
371 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
374 if ( ! man_node_append(man
, n
))
377 man
->next
= MAN_NEXT_SIBLING
;
378 return(man_descope(man
, ep
->ln
, ep
->pos
));
382 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
386 assert( ! (MAN_HALT
& man
->flags
));
388 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
391 if ( ! man_node_append(man
, n
))
394 man
->next
= MAN_NEXT_SIBLING
;
395 return(man_descope(man
, sp
->line
, 0));
399 man_descope(struct man
*man
, int line
, int offs
)
402 * Co-ordinate what happens with having a next-line scope open:
403 * first close out the element scope (if applicable), then close
404 * out the block scope (also if applicable).
407 if (MAN_ELINE
& man
->flags
) {
408 man
->flags
&= ~MAN_ELINE
;
409 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
413 if ( ! (MAN_BLINE
& man
->flags
))
415 man
->flags
&= ~MAN_BLINE
;
417 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
419 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
423 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
427 /* Literal free-form text whitespace is preserved. */
429 if (MAN_LITERAL
& man
->flags
) {
430 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
432 return(man_descope(man
, line
, offs
));
435 for (i
= offs
; ' ' == buf
[i
]; i
++)
436 /* Skip leading whitespace. */ ;
439 * Blank lines are ignored right after headings
440 * but add a single vertical space elsewhere.
443 if ('\0' == buf
[i
]) {
444 /* Allocate a blank entry. */
445 if (MAN_SH
!= man
->last
->tok
&&
446 MAN_SS
!= man
->last
->tok
) {
447 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
449 man
->next
= MAN_NEXT_SIBLING
;
455 * Warn if the last un-escaped character is whitespace. Then
456 * strip away the remaining spaces (tabs stay!).
459 i
= (int)strlen(buf
);
462 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
463 if (i
> 1 && '\\' != buf
[i
- 2])
464 man_pmsg(man
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
466 for (--i
; i
&& ' ' == buf
[i
]; i
--)
467 /* Spin back to non-space. */ ;
469 /* Jump ahead of escaped whitespace. */
470 i
+= '\\' == buf
[i
] ? 2 : 1;
475 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
479 * End-of-sentence check. If the last character is an unescaped
480 * EOS character, then flag the node as being the end of a
481 * sentence. The front-end will know how to interpret this.
485 if (mandoc_eos(buf
, (size_t)i
))
486 man
->last
->flags
|= MAN_EOS
;
488 return(man_descope(man
, line
, offs
));
492 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
499 if ('"' == buf
[offs
]) {
500 man_pmsg(man
, ln
, offs
, MANDOCERR_BADCOMMENT
);
502 } else if ('\0' == buf
[offs
])
508 * Copy the first word into a nil-terminated buffer.
509 * Stop copying when a tab, space, or eoln is encountered.
513 while (i
< 4 && '\0' != buf
[offs
] &&
514 ' ' != buf
[offs
] && '\t' != buf
[offs
])
515 mac
[i
++] = buf
[offs
++];
519 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
521 if (MAN_MAX
== tok
) {
522 mandoc_vmsg(MANDOCERR_MACRO
, man
->parse
, ln
,
523 ppos
, "%s", buf
+ ppos
- 1);
527 /* The macro is sane. Jump to the next word. */
529 while (buf
[offs
] && ' ' == buf
[offs
])
533 * Trailing whitespace. Note that tabs are allowed to be passed
534 * into the parser as "text", so we only warn about spaces here.
537 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
538 man_pmsg(man
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
541 * Remove prior ELINE macro, as it's being clobbered by a new
542 * macro. Note that NSCOPED macros do not close out ELINE
543 * macros---they don't print text---so we let those slip by.
546 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
547 man
->flags
& MAN_ELINE
) {
549 assert(MAN_TEXT
!= n
->type
);
551 /* Remove repeated NSCOPED macros causing ELINE. */
553 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
556 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
557 n
->pos
, "%s breaks %s", man_macronames
[tok
],
558 man_macronames
[n
->tok
]);
560 man_node_delete(man
, n
);
561 man
->flags
&= ~MAN_ELINE
;
565 * Remove prior BLINE macro that is being clobbered.
567 if ((man
->flags
& MAN_BLINE
) &&
568 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
571 /* Might be a text node like 8 in
575 if (MAN_TEXT
== n
->type
)
578 /* Remove element that didn't end BLINE, if any. */
579 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
582 assert(MAN_HEAD
== n
->type
);
584 assert(MAN_BLOCK
== n
->type
);
585 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
587 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
588 n
->pos
, "%s breaks %s", man_macronames
[tok
],
589 man_macronames
[n
->tok
]);
591 man_node_delete(man
, n
);
592 man
->flags
&= ~MAN_BLINE
;
596 * Save the fact that we're in the next-line for a block. In
597 * this way, embedded roff instructions can "remember" state
601 if (MAN_BLINE
& man
->flags
)
602 man
->flags
|= MAN_BPLINE
;
604 /* Call to handler... */
606 assert(man_macros
[tok
].fp
);
607 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
610 /* In quick mode (for mandocdb), abort after the NAME section. */
612 if (man
->quick
&& MAN_SH
== tok
&&
613 strcmp(man
->last
->prev
->child
->string
, "NAME"))
617 * We weren't in a block-line scope when entering the
618 * above-parsed macro, so return.
621 if ( ! (MAN_BPLINE
& man
->flags
)) {
622 man
->flags
&= ~MAN_ILINE
;
625 man
->flags
&= ~MAN_BPLINE
;
628 * If we're in a block scope, then allow this macro to slip by
629 * without closing scope around it.
632 if (MAN_ILINE
& man
->flags
) {
633 man
->flags
&= ~MAN_ILINE
;
638 * If we've opened a new next-line element scope, then return
639 * now, as the next line will close out the block scope.
642 if (MAN_ELINE
& man
->flags
)
645 /* Close out the block scope opened in the prior line. */
647 assert(MAN_BLINE
& man
->flags
);
648 man
->flags
&= ~MAN_BLINE
;
650 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
652 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
654 err
: /* Error out. */
656 man
->flags
|= MAN_HALT
;
661 * Unlink a node from its context. If "man" is provided, the last parse
662 * point will also be adjusted accordingly.
665 man_node_unlink(struct man
*man
, struct man_node
*n
)
668 /* Adjust siblings. */
671 n
->prev
->next
= n
->next
;
673 n
->next
->prev
= n
->prev
;
679 if (n
->parent
->child
== n
)
680 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
683 /* Adjust parse point, if applicable. */
685 if (man
&& man
->last
== n
) {
686 /*XXX: this can occur when bailing from validation. */
687 /*assert(NULL == n->next);*/
690 man
->next
= MAN_NEXT_SIBLING
;
692 man
->last
= n
->parent
;
693 man
->next
= MAN_NEXT_CHILD
;
697 if (man
&& man
->first
== n
)
701 const struct mparse
*
702 man_mparse(const struct man
*man
)
705 assert(man
&& man
->parse
);