]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.86 2010/08/08 14:51:32 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>
31 #include "libmandoc.h"
33 const char *const __man_macronames
[MAN_MAX
] = {
34 "br", "TH", "SH", "SS",
35 "TP", "LP", "PP", "P",
36 "IP", "HP", "SM", "SB",
37 "BI", "IB", "BR", "RB",
39 "RI", "na", "i", "sp",
40 "nf", "fi", "r", "RE",
41 "RS", "DT", "UC", "PD",
42 "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
]))
484 * Copy the first word into a nil-terminated buffer.
485 * Stop copying when a tab, space, or eoln is encountered.
489 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
493 if (j
== 4 || j
< 1) {
494 if ( ! macrowarn(m
, ln
, mac
, ppos
))
499 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
500 if ( ! macrowarn(m
, ln
, mac
, ppos
))
505 /* The macro is sane. Jump to the next word. */
507 while (buf
[i
] && ' ' == buf
[i
])
511 * Trailing whitespace. Note that tabs are allowed to be passed
512 * into the parser as "text", so we only warn about spaces here.
515 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
516 if ( ! man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
520 * Remove prior ELINE macro, as it's being clobbering by a new
521 * macro. Note that NSCOPED macros do not close out ELINE
522 * macros---they don't print text---so we let those slip by.
525 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
526 m
->flags
& MAN_ELINE
) {
527 assert(MAN_TEXT
!= m
->last
->type
);
530 * This occurs in the following construction:
536 * Flat-out disallow this madness.
538 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
) {
539 man_pmsg(m
, ln
, ppos
, MANDOCERR_SYNTLINESCOPE
);
546 assert(NULL
== n
->child
);
547 assert(0 == n
->nchild
);
549 if ( ! man_nmsg(m
, n
, MANDOCERR_LINESCOPE
))
552 man_node_delete(m
, n
);
553 m
->flags
&= ~MAN_ELINE
;
557 * Save the fact that we're in the next-line for a block. In
558 * this way, embedded roff instructions can "remember" state
562 if (MAN_BLINE
& m
->flags
)
563 m
->flags
|= MAN_BPLINE
;
565 /* Call to handler... */
567 assert(man_macros
[tok
].fp
);
568 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
573 * We weren't in a block-line scope when entering the
574 * above-parsed macro, so return.
577 if ( ! (MAN_BPLINE
& m
->flags
)) {
578 m
->flags
&= ~MAN_ILINE
;
581 m
->flags
&= ~MAN_BPLINE
;
584 * If we're in a block scope, then allow this macro to slip by
585 * without closing scope around it.
588 if (MAN_ILINE
& m
->flags
) {
589 m
->flags
&= ~MAN_ILINE
;
594 * If we've opened a new next-line element scope, then return
595 * now, as the next line will close out the block scope.
598 if (MAN_ELINE
& m
->flags
)
601 /* Close out the block scope opened in the prior line. */
603 assert(MAN_BLINE
& m
->flags
);
604 m
->flags
&= ~MAN_BLINE
;
606 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
608 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
610 err
: /* Error out. */
612 m
->flags
|= MAN_HALT
;
618 man_vmsg(struct man
*man
, enum mandocerr t
,
619 int ln
, int pos
, const char *fmt
, ...)
625 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
627 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
632 * Unlink a node from its context. If "m" is provided, the last parse
633 * point will also be adjusted accordingly.
636 man_node_unlink(struct man
*m
, struct man_node
*n
)
639 /* Adjust siblings. */
642 n
->prev
->next
= n
->next
;
644 n
->next
->prev
= n
->prev
;
650 if (n
->parent
->child
== n
)
651 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
654 /* Adjust parse point, if applicable. */
656 if (m
&& m
->last
== n
) {
657 /*XXX: this can occur when bailing from validation. */
658 /*assert(NULL == n->next);*/
661 m
->next
= MAN_NEXT_SIBLING
;
664 m
->next
= MAN_NEXT_CHILD
;
668 if (m
&& m
->first
== n
)