]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.74 2010/05/17 22:11:42 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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", "i", "sp",
41 "nf", "fi", "r", "RE",
42 "RS", "DT", "UC", "PD",
43 "Sp", "Vb", "Ve", "AT",
46 const char * const *man_macronames
= __man_macronames
;
48 static struct man_node
*man_node_alloc(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 macrowarn(struct man
*, int, const char *, int);
62 const struct man_node
*
63 man_node(const struct man
*m
)
66 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
70 const struct man_meta
*
71 man_meta(const struct man
*m
)
74 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
79 man_reset(struct man
*man
)
88 man_free(struct man
*man
)
97 man_alloc(void *data
, int pflags
, mandocmsg msg
)
101 p
= mandoc_calloc(1, sizeof(struct man
));
114 man_endparse(struct man
*m
)
117 if (MAN_HALT
& m
->flags
)
119 else if (man_macroend(m
))
121 m
->flags
|= MAN_HALT
;
127 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
130 if (MAN_HALT
& m
->flags
)
133 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
134 man_pmacro(m
, ln
, buf
, offs
) :
135 man_ptext(m
, ln
, buf
, offs
));
140 man_free1(struct man
*man
)
144 man_node_delete(man
, man
->first
);
146 free(man
->meta
.title
);
147 if (man
->meta
.source
)
148 free(man
->meta
.source
);
152 free(man
->meta
.msec
);
157 man_alloc1(struct man
*m
)
160 memset(&m
->meta
, 0, sizeof(struct man_meta
));
162 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
164 m
->last
->type
= MAN_ROOT
;
165 m
->last
->tok
= MAN_MAX
;
166 m
->next
= MAN_NEXT_CHILD
;
171 man_node_append(struct man
*man
, struct man_node
*p
)
176 assert(MAN_ROOT
!= p
->type
);
179 case (MAN_NEXT_SIBLING
):
182 p
->parent
= man
->last
->parent
;
184 case (MAN_NEXT_CHILD
):
185 man
->last
->child
= p
;
186 p
->parent
= man
->last
;
196 if ( ! man_valid_pre(man
, p
))
201 assert(MAN_BLOCK
== p
->parent
->type
);
205 assert(MAN_BLOCK
== p
->parent
->type
);
216 if ( ! man_valid_post(man
))
218 if ( ! man_action_post(man
))
229 static struct man_node
*
230 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
234 p
= mandoc_calloc(1, sizeof(struct man_node
));
244 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
248 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
249 if ( ! man_node_append(m
, p
))
251 m
->next
= MAN_NEXT_CHILD
;
257 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
261 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
262 if ( ! man_node_append(m
, p
))
264 m
->next
= MAN_NEXT_CHILD
;
270 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
274 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
275 if ( ! man_node_append(m
, p
))
277 m
->next
= MAN_NEXT_CHILD
;
283 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
287 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
288 if ( ! man_node_append(m
, p
))
290 m
->next
= MAN_NEXT_CHILD
;
296 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
303 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
304 n
->string
= mandoc_malloc(len
+ 1);
305 sv
= strlcpy(n
->string
, word
, len
+ 1);
307 /* Prohibit truncation. */
308 assert(sv
< len
+ 1);
310 if ( ! man_node_append(m
, n
))
313 m
->next
= MAN_NEXT_SIBLING
;
319 * Free all of the resources held by a node. This does NOT unlink a
320 * node from its context; for that, see man_node_unlink().
323 man_node_free(struct man_node
*p
)
333 man_node_delete(struct man
*m
, struct man_node
*p
)
337 man_node_delete(m
, p
->child
);
339 man_node_unlink(m
, p
);
345 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
349 /* Ignore bogus comments. */
351 if ('\\' == buf
[offs
] &&
352 '.' == buf
[offs
+ 1] &&
353 '"' == buf
[offs
+ 2])
354 return(man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
356 /* Literal free-form text whitespace is preserved. */
358 if (MAN_LITERAL
& m
->flags
) {
359 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
364 /* Pump blank lines directly into the backend. */
366 for (i
= offs
; ' ' == buf
[i
]; i
++)
367 /* Skip leading whitespace. */ ;
369 if ('\0' == buf
[i
]) {
370 /* Allocate a blank entry. */
371 if ( ! man_word_alloc(m
, line
, offs
, ""))
377 * Warn if the last un-escaped character is whitespace. Then
378 * strip away the remaining spaces (tabs stay!).
381 i
= (int)strlen(buf
);
384 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
385 if (i
> 1 && '\\' != buf
[i
- 2])
386 if ( ! man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
))
389 for (--i
; i
&& ' ' == buf
[i
]; i
--)
390 /* Spin back to non-space. */ ;
392 /* Jump ahead of escaped whitespace. */
393 i
+= '\\' == buf
[i
] ? 2 : 1;
398 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
402 * End-of-sentence check. If the last character is an unescaped
403 * EOS character, then flag the node as being the end of a
404 * sentence. The front-end will know how to interpret this.
408 if (mandoc_eos(buf
, (size_t)i
))
409 m
->last
->flags
|= MAN_EOS
;
413 * Co-ordinate what happens with having a next-line scope open:
414 * first close out the element scope (if applicable), then close
415 * out the block scope (also if applicable).
418 if (MAN_ELINE
& m
->flags
) {
419 m
->flags
&= ~MAN_ELINE
;
420 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
424 if ( ! (MAN_BLINE
& m
->flags
))
426 m
->flags
&= ~MAN_BLINE
;
428 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
430 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
435 macrowarn(struct man
*m
, int ln
, const char *buf
, int offs
)
439 rc
= man_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
440 "unknown macro: %s%s",
441 buf
, strlen(buf
) > 3 ? "..." : "");
443 return(MAN_IGN_MACRO
& m
->pflags
? rc
: 0);
448 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
455 /* Comments and empties are quickly ignored. */
459 if ('\0' == buf
[offs
])
465 * Skip whitespace between the control character and initial
466 * text. "Whitespace" is both spaces and tabs.
469 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
471 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
479 /* Copy the first word into a nil-terminated buffer. */
481 for (j
= 0; j
< 4; j
++, i
++) {
482 if ('\0' == (mac
[j
] = buf
[i
]))
484 else if (' ' == buf
[i
])
487 /* Check for invalid characters. */
489 if (isgraph((u_char
)buf
[i
]))
491 if ( ! man_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
498 if (j
== 4 || j
< 1) {
499 if ( ! macrowarn(m
, ln
, mac
, ppos
))
504 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
505 if ( ! macrowarn(m
, ln
, mac
, ppos
))
510 /* The macro is sane. Jump to the next word. */
512 while (buf
[i
] && ' ' == buf
[i
])
516 * Trailing whitespace. Note that tabs are allowed to be passed
517 * into the parser as "text", so we only warn about spaces here.
520 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
521 if ( ! man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
525 * Remove prior ELINE macro, as it's being clobbering by a new
526 * macro. Note that NSCOPED macros do not close out ELINE
527 * macros---they don't print text---so we let those slip by.
530 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
531 m
->flags
& MAN_ELINE
) {
532 assert(MAN_TEXT
!= m
->last
->type
);
535 * This occurs in the following construction:
541 * Flat-out disallow this madness.
543 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
) {
544 man_pmsg(m
, ln
, ppos
, MANDOCERR_SYNTLINESCOPE
);
551 assert(NULL
== n
->child
);
552 assert(0 == n
->nchild
);
554 if ( ! man_nmsg(m
, n
, MANDOCERR_LINESCOPE
))
557 man_node_delete(m
, n
);
558 m
->flags
&= ~MAN_ELINE
;
562 * Save the fact that we're in the next-line for a block. In
563 * this way, embedded roff instructions can "remember" state
567 if (MAN_BLINE
& m
->flags
)
568 m
->flags
|= MAN_BPLINE
;
570 /* Call to handler... */
572 assert(man_macros
[tok
].fp
);
573 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
578 * We weren't in a block-line scope when entering the
579 * above-parsed macro, so return.
582 if ( ! (MAN_BPLINE
& m
->flags
)) {
583 m
->flags
&= ~MAN_ILINE
;
586 m
->flags
&= ~MAN_BPLINE
;
589 * If we're in a block scope, then allow this macro to slip by
590 * without closing scope around it.
593 if (MAN_ILINE
& m
->flags
) {
594 m
->flags
&= ~MAN_ILINE
;
599 * If we've opened a new next-line element scope, then return
600 * now, as the next line will close out the block scope.
603 if (MAN_ELINE
& m
->flags
)
606 /* Close out the block scope opened in the prior line. */
608 assert(MAN_BLINE
& m
->flags
);
609 m
->flags
&= ~MAN_BLINE
;
611 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
613 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
615 err
: /* Error out. */
617 m
->flags
|= MAN_HALT
;
623 man_vmsg(struct man
*man
, enum mandocerr t
,
624 int ln
, int pos
, const char *fmt
, ...)
630 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
632 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
637 * Unlink a node from its context. If "m" is provided, the last parse
638 * point will also be adjusted accordingly.
641 man_node_unlink(struct man
*m
, struct man_node
*n
)
644 /* Adjust siblings. */
647 n
->prev
->next
= n
->next
;
649 n
->next
->prev
= n
->prev
;
655 if (n
->parent
->child
== n
)
656 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
659 /* Adjust parse point, if applicable. */
661 if (m
&& m
->last
== n
) {
662 /*XXX: this can occur when bailing from validation. */
663 /*assert(NULL == n->next);*/
666 m
->next
= MAN_NEXT_SIBLING
;
669 m
->next
= MAN_NEXT_CHILD
;
673 if (m
&& m
->first
== n
)