]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.123 2014/01/05 20:26:36 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
33 #include "libmandoc.h"
35 const char *const __man_macronames
[MAN_MAX
] = {
36 "br", "TH", "SH", "SS",
37 "TP", "LP", "PP", "P",
38 "IP", "HP", "SM", "SB",
39 "BI", "IB", "BR", "RB",
41 "RI", "na", "sp", "nf",
42 "fi", "RE", "RS", "DT",
43 "UC", "PD", "AT", "in",
44 "ft", "OP", "EX", "EE",
48 const char * const *man_macronames
= __man_macronames
;
50 static struct man_node
*man_node_alloc(struct man
*, int, int,
51 enum man_type
, enum mant
);
52 static int man_node_append(struct man
*,
54 static void man_node_free(struct man_node
*);
55 static void man_node_unlink(struct man
*,
57 static int man_ptext(struct man
*, int, char *, int);
58 static int man_pmacro(struct man
*, int, char *, int);
59 static void man_free1(struct man
*);
60 static void man_alloc1(struct man
*);
61 static int man_descope(struct man
*, int, int);
64 const struct man_node
*
65 man_node(const struct man
*man
)
68 assert( ! (MAN_HALT
& man
->flags
));
73 const struct man_meta
*
74 man_meta(const struct man
*man
)
77 assert( ! (MAN_HALT
& man
->flags
));
83 man_reset(struct man
*man
)
92 man_free(struct man
*man
)
101 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
105 p
= mandoc_calloc(1, sizeof(struct man
));
118 man_endparse(struct man
*man
)
121 assert( ! (MAN_HALT
& man
->flags
));
122 if (man_macroend(man
))
124 man
->flags
|= MAN_HALT
;
130 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
133 man
->flags
|= MAN_NEWLINE
;
135 assert( ! (MAN_HALT
& man
->flags
));
137 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
138 man_pmacro(man
, ln
, buf
, offs
) :
139 man_ptext(man
, ln
, buf
, offs
));
144 man_free1(struct man
*man
)
148 man_node_delete(man
, man
->first
);
150 free(man
->meta
.title
);
151 if (man
->meta
.source
)
152 free(man
->meta
.source
);
154 free(man
->meta
.date
);
158 free(man
->meta
.msec
);
163 man_alloc1(struct man
*man
)
166 memset(&man
->meta
, 0, sizeof(struct man_meta
));
168 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
169 man
->first
= man
->last
;
170 man
->last
->type
= MAN_ROOT
;
171 man
->last
->tok
= MAN_MAX
;
172 man
->next
= MAN_NEXT_CHILD
;
177 man_node_append(struct man
*man
, struct man_node
*p
)
182 assert(MAN_ROOT
!= p
->type
);
185 case (MAN_NEXT_SIBLING
):
188 p
->parent
= man
->last
->parent
;
190 case (MAN_NEXT_CHILD
):
191 man
->last
->child
= p
;
192 p
->parent
= man
->last
;
202 if ( ! man_valid_pre(man
, p
))
207 assert(MAN_BLOCK
== p
->parent
->type
);
211 assert(MAN_BLOCK
== p
->parent
->type
);
215 assert(MAN_BLOCK
== p
->parent
->type
);
228 if ( ! man_valid_post(man
))
239 static struct man_node
*
240 man_node_alloc(struct man
*man
, int line
, int pos
,
241 enum man_type type
, enum mant tok
)
245 p
= mandoc_calloc(1, sizeof(struct man_node
));
251 if (MAN_NEWLINE
& man
->flags
)
252 p
->flags
|= MAN_LINE
;
253 man
->flags
&= ~MAN_NEWLINE
;
259 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
263 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
264 if ( ! man_node_append(man
, p
))
266 man
->next
= MAN_NEXT_CHILD
;
272 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
276 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
277 if ( ! man_node_append(man
, p
))
279 man
->next
= MAN_NEXT_CHILD
;
285 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
289 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
290 if ( ! man_node_append(man
, p
))
292 man
->next
= MAN_NEXT_CHILD
;
298 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
302 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
303 if ( ! man_node_append(man
, p
))
305 man
->next
= MAN_NEXT_CHILD
;
311 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
315 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
316 if ( ! man_node_append(man
, p
))
318 man
->next
= MAN_NEXT_CHILD
;
323 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
327 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
328 n
->string
= roff_strdup(man
->roff
, word
);
330 if ( ! man_node_append(man
, n
))
333 man
->next
= MAN_NEXT_SIBLING
;
339 * Free all of the resources held by a node. This does NOT unlink a
340 * node from its context; for that, see man_node_unlink().
343 man_node_free(struct man_node
*p
)
353 man_node_delete(struct man
*man
, struct man_node
*p
)
357 man_node_delete(man
, p
->child
);
359 man_node_unlink(man
, p
);
364 man_addeqn(struct man
*man
, const struct eqn
*ep
)
368 assert( ! (MAN_HALT
& man
->flags
));
370 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
373 if ( ! man_node_append(man
, n
))
376 man
->next
= MAN_NEXT_SIBLING
;
377 return(man_descope(man
, ep
->ln
, ep
->pos
));
381 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
385 assert( ! (MAN_HALT
& man
->flags
));
387 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
390 if ( ! man_node_append(man
, n
))
393 man
->next
= MAN_NEXT_SIBLING
;
394 return(man_descope(man
, sp
->line
, 0));
398 man_descope(struct man
*man
, int line
, int offs
)
401 * Co-ordinate what happens with having a next-line scope open:
402 * first close out the element scope (if applicable), then close
403 * out the block scope (also if applicable).
406 if (MAN_ELINE
& man
->flags
) {
407 man
->flags
&= ~MAN_ELINE
;
408 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
412 if ( ! (MAN_BLINE
& man
->flags
))
414 man
->flags
&= ~MAN_BLINE
;
416 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
418 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
422 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
426 /* Literal free-form text whitespace is preserved. */
428 if (MAN_LITERAL
& man
->flags
) {
429 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
431 return(man_descope(man
, line
, offs
));
434 for (i
= offs
; ' ' == buf
[i
]; i
++)
435 /* Skip leading whitespace. */ ;
438 * Blank lines are ignored right after headings
439 * but add a single vertical space elsewhere.
442 if ('\0' == buf
[i
]) {
443 /* Allocate a blank entry. */
444 if (MAN_SH
!= man
->last
->tok
&&
445 MAN_SS
!= man
->last
->tok
) {
446 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
448 man
->next
= MAN_NEXT_SIBLING
;
454 * Warn if the last un-escaped character is whitespace. Then
455 * strip away the remaining spaces (tabs stay!).
458 i
= (int)strlen(buf
);
461 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
462 if (i
> 1 && '\\' != buf
[i
- 2])
463 man_pmsg(man
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
465 for (--i
; i
&& ' ' == buf
[i
]; i
--)
466 /* Spin back to non-space. */ ;
468 /* Jump ahead of escaped whitespace. */
469 i
+= '\\' == buf
[i
] ? 2 : 1;
474 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
478 * End-of-sentence check. If the last character is an unescaped
479 * EOS character, then flag the node as being the end of a
480 * sentence. The front-end will know how to interpret this.
484 if (mandoc_eos(buf
, (size_t)i
))
485 man
->last
->flags
|= MAN_EOS
;
487 return(man_descope(man
, line
, offs
));
491 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
498 if ('"' == buf
[offs
]) {
499 man_pmsg(man
, ln
, offs
, MANDOCERR_BADCOMMENT
);
501 } else if ('\0' == buf
[offs
])
507 * Copy the first word into a nil-terminated buffer.
508 * Stop copying when a tab, space, or eoln is encountered.
512 while (i
< 4 && '\0' != buf
[offs
] &&
513 ' ' != buf
[offs
] && '\t' != buf
[offs
])
514 mac
[i
++] = buf
[offs
++];
518 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
520 if (MAN_MAX
== tok
) {
521 mandoc_vmsg(MANDOCERR_MACRO
, man
->parse
, ln
,
522 ppos
, "%s", buf
+ ppos
- 1);
526 /* The macro is sane. Jump to the next word. */
528 while (buf
[offs
] && ' ' == buf
[offs
])
532 * Trailing whitespace. Note that tabs are allowed to be passed
533 * into the parser as "text", so we only warn about spaces here.
536 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
537 man_pmsg(man
, ln
, offs
- 1, MANDOCERR_EOLNSPACE
);
540 * Remove prior ELINE macro, as it's being clobbered by a new
541 * macro. Note that NSCOPED macros do not close out ELINE
542 * macros---they don't print text---so we let those slip by.
545 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
546 man
->flags
& MAN_ELINE
) {
548 assert(MAN_TEXT
!= n
->type
);
550 /* Remove repeated NSCOPED macros causing ELINE. */
552 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
555 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
556 n
->pos
, "%s breaks %s", man_macronames
[tok
],
557 man_macronames
[n
->tok
]);
559 man_node_delete(man
, n
);
560 man
->flags
&= ~MAN_ELINE
;
564 * Remove prior BLINE macro that is being clobbered.
566 if ((man
->flags
& MAN_BLINE
) &&
567 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
570 /* Might be a text node like 8 in
574 if (MAN_TEXT
== n
->type
)
577 /* Remove element that didn't end BLINE, if any. */
578 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
581 assert(MAN_HEAD
== n
->type
);
583 assert(MAN_BLOCK
== n
->type
);
584 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
586 mandoc_vmsg(MANDOCERR_LINESCOPE
, man
->parse
, n
->line
,
587 n
->pos
, "%s breaks %s", man_macronames
[tok
],
588 man_macronames
[n
->tok
]);
590 man_node_delete(man
, n
);
591 man
->flags
&= ~MAN_BLINE
;
595 * Save the fact that we're in the next-line for a block. In
596 * this way, embedded roff instructions can "remember" state
600 if (MAN_BLINE
& man
->flags
)
601 man
->flags
|= MAN_BPLINE
;
603 /* Call to handler... */
605 assert(man_macros
[tok
].fp
);
606 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
609 /* In quick mode (for mandocdb), abort after the NAME section. */
611 if (man
->quick
&& MAN_SH
== tok
&&
612 strcmp(man
->last
->prev
->child
->string
, "NAME"))
616 * We weren't in a block-line scope when entering the
617 * above-parsed macro, so return.
620 if ( ! (MAN_BPLINE
& man
->flags
)) {
621 man
->flags
&= ~MAN_ILINE
;
624 man
->flags
&= ~MAN_BPLINE
;
627 * If we're in a block scope, then allow this macro to slip by
628 * without closing scope around it.
631 if (MAN_ILINE
& man
->flags
) {
632 man
->flags
&= ~MAN_ILINE
;
637 * If we've opened a new next-line element scope, then return
638 * now, as the next line will close out the block scope.
641 if (MAN_ELINE
& man
->flags
)
644 /* Close out the block scope opened in the prior line. */
646 assert(MAN_BLINE
& man
->flags
);
647 man
->flags
&= ~MAN_BLINE
;
649 if ( ! man_unscope(man
, man
->last
->parent
, MANDOCERR_MAX
))
651 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
653 err
: /* Error out. */
655 man
->flags
|= MAN_HALT
;
660 * Unlink a node from its context. If "man" is provided, the last parse
661 * point will also be adjusted accordingly.
664 man_node_unlink(struct man
*man
, struct man_node
*n
)
667 /* Adjust siblings. */
670 n
->prev
->next
= n
->next
;
672 n
->next
->prev
= n
->prev
;
678 if (n
->parent
->child
== n
)
679 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
682 /* Adjust parse point, if applicable. */
684 if (man
&& man
->last
== n
) {
685 /*XXX: this can occur when bailing from validation. */
686 /*assert(NULL == n->next);*/
689 man
->next
= MAN_NEXT_SIBLING
;
691 man
->last
= n
->parent
;
692 man
->next
= MAN_NEXT_CHILD
;
696 if (man
&& man
->first
== n
)
700 const struct mparse
*
701 man_mparse(const struct man
*man
)
704 assert(man
&& man
->parse
);