]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.83 2010/07/18 17:00:26 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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", "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(struct regset
*regs
, void *data
,
98 int pflags
, mandocmsg msg
)
102 p
= mandoc_calloc(1, sizeof(struct man
));
116 man_endparse(struct man
*m
)
119 if (MAN_HALT
& m
->flags
)
121 else if (man_macroend(m
))
123 m
->flags
|= MAN_HALT
;
129 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
132 if (MAN_HALT
& m
->flags
)
135 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
136 man_pmacro(m
, ln
, buf
, offs
) :
137 man_ptext(m
, ln
, buf
, offs
));
142 man_free1(struct man
*man
)
146 man_node_delete(man
, man
->first
);
148 free(man
->meta
.title
);
149 if (man
->meta
.source
)
150 free(man
->meta
.source
);
151 if (man
->meta
.rawdate
)
152 free(man
->meta
.rawdate
);
156 free(man
->meta
.msec
);
161 man_alloc1(struct man
*m
)
164 memset(&m
->meta
, 0, sizeof(struct man_meta
));
166 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
168 m
->last
->type
= MAN_ROOT
;
169 m
->last
->tok
= MAN_MAX
;
170 m
->next
= MAN_NEXT_CHILD
;
175 man_node_append(struct man
*man
, struct man_node
*p
)
180 assert(MAN_ROOT
!= p
->type
);
183 case (MAN_NEXT_SIBLING
):
186 p
->parent
= man
->last
->parent
;
188 case (MAN_NEXT_CHILD
):
189 man
->last
->child
= p
;
190 p
->parent
= man
->last
;
200 if ( ! man_valid_pre(man
, p
))
205 assert(MAN_BLOCK
== p
->parent
->type
);
209 assert(MAN_BLOCK
== p
->parent
->type
);
220 if ( ! man_valid_post(man
))
222 if ( ! man_action_post(man
))
233 static struct man_node
*
234 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
238 p
= mandoc_calloc(1, sizeof(struct man_node
));
248 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
252 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
253 if ( ! man_node_append(m
, p
))
255 m
->next
= MAN_NEXT_CHILD
;
261 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
265 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
266 if ( ! man_node_append(m
, p
))
268 m
->next
= MAN_NEXT_CHILD
;
274 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
278 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
279 if ( ! man_node_append(m
, p
))
281 m
->next
= MAN_NEXT_CHILD
;
287 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
291 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
292 if ( ! man_node_append(m
, p
))
294 m
->next
= MAN_NEXT_CHILD
;
300 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
307 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
308 n
->string
= mandoc_malloc(len
+ 1);
309 sv
= strlcpy(n
->string
, word
, len
+ 1);
311 /* Prohibit truncation. */
312 assert(sv
< len
+ 1);
314 if ( ! man_node_append(m
, n
))
317 m
->next
= MAN_NEXT_SIBLING
;
323 * Free all of the resources held by a node. This does NOT unlink a
324 * node from its context; for that, see man_node_unlink().
327 man_node_free(struct man_node
*p
)
337 man_node_delete(struct man
*m
, struct man_node
*p
)
341 man_node_delete(m
, p
->child
);
343 man_node_unlink(m
, p
);
349 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
353 /* Ignore bogus comments. */
355 if ('\\' == buf
[offs
] &&
356 '.' == buf
[offs
+ 1] &&
357 '"' == buf
[offs
+ 2])
358 return(man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
360 /* Literal free-form text whitespace is preserved. */
362 if (MAN_LITERAL
& m
->flags
) {
363 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
368 /* Pump blank lines directly into the backend. */
370 for (i
= offs
; ' ' == buf
[i
]; i
++)
371 /* Skip leading whitespace. */ ;
373 if ('\0' == buf
[i
]) {
374 /* Allocate a blank entry. */
375 if ( ! man_word_alloc(m
, line
, offs
, ""))
381 * Warn if the last un-escaped character is whitespace. Then
382 * strip away the remaining spaces (tabs stay!).
385 i
= (int)strlen(buf
);
388 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
389 if (i
> 1 && '\\' != buf
[i
- 2])
390 if ( ! man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
))
393 for (--i
; i
&& ' ' == buf
[i
]; i
--)
394 /* Spin back to non-space. */ ;
396 /* Jump ahead of escaped whitespace. */
397 i
+= '\\' == buf
[i
] ? 2 : 1;
402 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
406 * End-of-sentence check. If the last character is an unescaped
407 * EOS character, then flag the node as being the end of a
408 * sentence. The front-end will know how to interpret this.
412 if (mandoc_eos(buf
, (size_t)i
, 0))
413 m
->last
->flags
|= MAN_EOS
;
417 * Co-ordinate what happens with having a next-line scope open:
418 * first close out the element scope (if applicable), then close
419 * out the block scope (also if applicable).
422 if (MAN_ELINE
& m
->flags
) {
423 m
->flags
&= ~MAN_ELINE
;
424 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
428 if ( ! (MAN_BLINE
& m
->flags
))
430 m
->flags
&= ~MAN_BLINE
;
432 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
434 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
439 macrowarn(struct man
*m
, int ln
, const char *buf
, int offs
)
443 rc
= man_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
444 "unknown macro: %s%s",
445 buf
, strlen(buf
) > 3 ? "..." : "");
447 return(MAN_IGN_MACRO
& m
->pflags
? rc
: 0);
452 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
459 /* Comments and empties are quickly ignored. */
463 if ('\0' == buf
[offs
])
469 * Skip whitespace between the control character and initial
470 * text. "Whitespace" is both spaces and tabs.
473 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
475 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
483 /* Copy the first word into a nil-terminated buffer. */
485 for (j
= 0; j
< 4; j
++, i
++) {
486 if ('\0' == (mac
[j
] = buf
[i
]))
488 else if (' ' == buf
[i
])
491 /* Check for invalid characters. */
493 if (isgraph((u_char
)buf
[i
]))
495 if ( ! man_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
502 if (j
== 4 || j
< 1) {
503 if ( ! macrowarn(m
, ln
, mac
, ppos
))
508 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
509 if ( ! macrowarn(m
, ln
, mac
, ppos
))
514 /* The macro is sane. Jump to the next word. */
516 while (buf
[i
] && ' ' == buf
[i
])
520 * Trailing whitespace. Note that tabs are allowed to be passed
521 * into the parser as "text", so we only warn about spaces here.
524 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
525 if ( ! man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
529 * Remove prior ELINE macro, as it's being clobbering by a new
530 * macro. Note that NSCOPED macros do not close out ELINE
531 * macros---they don't print text---so we let those slip by.
534 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
535 m
->flags
& MAN_ELINE
) {
536 assert(MAN_TEXT
!= m
->last
->type
);
539 * This occurs in the following construction:
545 * Flat-out disallow this madness.
547 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
) {
548 man_pmsg(m
, ln
, ppos
, MANDOCERR_SYNTLINESCOPE
);
555 assert(NULL
== n
->child
);
556 assert(0 == n
->nchild
);
558 if ( ! man_nmsg(m
, n
, MANDOCERR_LINESCOPE
))
561 man_node_delete(m
, n
);
562 m
->flags
&= ~MAN_ELINE
;
566 * Save the fact that we're in the next-line for a block. In
567 * this way, embedded roff instructions can "remember" state
571 if (MAN_BLINE
& m
->flags
)
572 m
->flags
|= MAN_BPLINE
;
574 /* Call to handler... */
576 assert(man_macros
[tok
].fp
);
577 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
582 * We weren't in a block-line scope when entering the
583 * above-parsed macro, so return.
586 if ( ! (MAN_BPLINE
& m
->flags
)) {
587 m
->flags
&= ~MAN_ILINE
;
590 m
->flags
&= ~MAN_BPLINE
;
593 * If we're in a block scope, then allow this macro to slip by
594 * without closing scope around it.
597 if (MAN_ILINE
& m
->flags
) {
598 m
->flags
&= ~MAN_ILINE
;
603 * If we've opened a new next-line element scope, then return
604 * now, as the next line will close out the block scope.
607 if (MAN_ELINE
& m
->flags
)
610 /* Close out the block scope opened in the prior line. */
612 assert(MAN_BLINE
& m
->flags
);
613 m
->flags
&= ~MAN_BLINE
;
615 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
617 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
619 err
: /* Error out. */
621 m
->flags
|= MAN_HALT
;
627 man_vmsg(struct man
*man
, enum mandocerr t
,
628 int ln
, int pos
, const char *fmt
, ...)
634 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
636 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
641 * Unlink a node from its context. If "m" is provided, the last parse
642 * point will also be adjusted accordingly.
645 man_node_unlink(struct man
*m
, struct man_node
*n
)
648 /* Adjust siblings. */
651 n
->prev
->next
= n
->next
;
653 n
->next
->prev
= n
->prev
;
659 if (n
->parent
->child
== n
)
660 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
663 /* Adjust parse point, if applicable. */
665 if (m
&& m
->last
== n
) {
666 /*XXX: this can occur when bailing from validation. */
667 /*assert(NULL == n->next);*/
670 m
->next
= MAN_NEXT_SIBLING
;
673 m
->next
= MAN_NEXT_CHILD
;
677 if (m
&& m
->first
== n
)