]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.87 2010/08/20 01:02:07 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
*);
61 const struct man_node
*
62 man_node(const struct man
*m
)
65 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
69 const struct man_meta
*
70 man_meta(const struct man
*m
)
73 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
78 man_reset(struct man
*man
)
87 man_free(struct man
*man
)
96 man_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
100 p
= mandoc_calloc(1, sizeof(struct man
));
113 man_endparse(struct man
*m
)
116 if (MAN_HALT
& m
->flags
)
118 else if (man_macroend(m
))
120 m
->flags
|= MAN_HALT
;
126 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
129 if (MAN_HALT
& m
->flags
)
132 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
133 man_pmacro(m
, ln
, buf
, offs
) :
134 man_ptext(m
, ln
, buf
, offs
));
139 man_free1(struct man
*man
)
143 man_node_delete(man
, man
->first
);
145 free(man
->meta
.title
);
146 if (man
->meta
.source
)
147 free(man
->meta
.source
);
148 if (man
->meta
.rawdate
)
149 free(man
->meta
.rawdate
);
153 free(man
->meta
.msec
);
158 man_alloc1(struct man
*m
)
161 memset(&m
->meta
, 0, sizeof(struct man_meta
));
163 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
165 m
->last
->type
= MAN_ROOT
;
166 m
->last
->tok
= MAN_MAX
;
167 m
->next
= MAN_NEXT_CHILD
;
172 man_node_append(struct man
*man
, struct man_node
*p
)
177 assert(MAN_ROOT
!= p
->type
);
180 case (MAN_NEXT_SIBLING
):
183 p
->parent
= man
->last
->parent
;
185 case (MAN_NEXT_CHILD
):
186 man
->last
->child
= p
;
187 p
->parent
= man
->last
;
197 if ( ! man_valid_pre(man
, p
))
202 assert(MAN_BLOCK
== p
->parent
->type
);
206 assert(MAN_BLOCK
== p
->parent
->type
);
217 if ( ! man_valid_post(man
))
219 if ( ! man_action_post(man
))
230 static struct man_node
*
231 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
235 p
= mandoc_calloc(1, sizeof(struct man_node
));
245 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
249 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
250 if ( ! man_node_append(m
, p
))
252 m
->next
= MAN_NEXT_CHILD
;
258 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
262 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
263 if ( ! man_node_append(m
, p
))
265 m
->next
= MAN_NEXT_CHILD
;
271 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
275 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
276 if ( ! man_node_append(m
, p
))
278 m
->next
= MAN_NEXT_CHILD
;
284 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
288 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
289 if ( ! man_node_append(m
, p
))
291 m
->next
= MAN_NEXT_CHILD
;
297 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
304 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
305 n
->string
= mandoc_malloc(len
+ 1);
306 sv
= strlcpy(n
->string
, word
, len
+ 1);
308 /* Prohibit truncation. */
309 assert(sv
< len
+ 1);
311 if ( ! man_node_append(m
, n
))
314 m
->next
= MAN_NEXT_SIBLING
;
320 * Free all of the resources held by a node. This does NOT unlink a
321 * node from its context; for that, see man_node_unlink().
324 man_node_free(struct man_node
*p
)
334 man_node_delete(struct man
*m
, struct man_node
*p
)
338 man_node_delete(m
, p
->child
);
340 man_node_unlink(m
, p
);
346 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
350 /* Ignore bogus comments. */
352 if ('\\' == buf
[offs
] &&
353 '.' == buf
[offs
+ 1] &&
354 '"' == buf
[offs
+ 2])
355 return(man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
357 /* Literal free-form text whitespace is preserved. */
359 if (MAN_LITERAL
& m
->flags
) {
360 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
365 /* Pump blank lines directly into the backend. */
367 for (i
= offs
; ' ' == buf
[i
]; i
++)
368 /* Skip leading whitespace. */ ;
370 if ('\0' == buf
[i
]) {
371 /* Allocate a blank entry. */
372 if ( ! man_word_alloc(m
, line
, offs
, ""))
378 * Warn if the last un-escaped character is whitespace. Then
379 * strip away the remaining spaces (tabs stay!).
382 i
= (int)strlen(buf
);
385 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
386 if (i
> 1 && '\\' != buf
[i
- 2])
387 if ( ! man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
))
390 for (--i
; i
&& ' ' == buf
[i
]; i
--)
391 /* Spin back to non-space. */ ;
393 /* Jump ahead of escaped whitespace. */
394 i
+= '\\' == buf
[i
] ? 2 : 1;
399 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
403 * End-of-sentence check. If the last character is an unescaped
404 * EOS character, then flag the node as being the end of a
405 * sentence. The front-end will know how to interpret this.
409 if (mandoc_eos(buf
, (size_t)i
, 0))
410 m
->last
->flags
|= MAN_EOS
;
414 * Co-ordinate what happens with having a next-line scope open:
415 * first close out the element scope (if applicable), then close
416 * out the block scope (also if applicable).
419 if (MAN_ELINE
& m
->flags
) {
420 m
->flags
&= ~MAN_ELINE
;
421 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
425 if ( ! (MAN_BLINE
& m
->flags
))
427 m
->flags
&= ~MAN_BLINE
;
429 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
431 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
436 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
443 /* Comments and empties are quickly ignored. */
447 if ('\0' == buf
[offs
])
453 * Skip whitespace between the control character and initial
454 * text. "Whitespace" is both spaces and tabs.
457 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
459 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
468 * Copy the first word into a nil-terminated buffer.
469 * Stop copying when a tab, space, or eoln is encountered.
473 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
477 tok
= (j
> 0 && j
< 4) ? man_hash_find(mac
) : MAN_MAX
;
478 if (MAN_MAX
== tok
) {
479 man_vmsg(m
, MANDOCERR_MACRO
, ln
, ppos
,
480 "unknown macro: %s%s",
481 buf
, strlen(buf
) > 3 ? "..." : "");
485 /* The macro is sane. Jump to the next word. */
487 while (buf
[i
] && ' ' == buf
[i
])
491 * Trailing whitespace. Note that tabs are allowed to be passed
492 * into the parser as "text", so we only warn about spaces here.
495 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
496 if ( ! man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
500 * Remove prior ELINE macro, as it's being clobbering by a new
501 * macro. Note that NSCOPED macros do not close out ELINE
502 * macros---they don't print text---so we let those slip by.
505 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
506 m
->flags
& MAN_ELINE
) {
507 assert(MAN_TEXT
!= m
->last
->type
);
510 * This occurs in the following construction:
516 * Flat-out disallow this madness.
518 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
) {
519 man_pmsg(m
, ln
, ppos
, MANDOCERR_SYNTLINESCOPE
);
526 assert(NULL
== n
->child
);
527 assert(0 == n
->nchild
);
529 if ( ! man_nmsg(m
, n
, MANDOCERR_LINESCOPE
))
532 man_node_delete(m
, n
);
533 m
->flags
&= ~MAN_ELINE
;
537 * Save the fact that we're in the next-line for a block. In
538 * this way, embedded roff instructions can "remember" state
542 if (MAN_BLINE
& m
->flags
)
543 m
->flags
|= MAN_BPLINE
;
545 /* Call to handler... */
547 assert(man_macros
[tok
].fp
);
548 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
553 * We weren't in a block-line scope when entering the
554 * above-parsed macro, so return.
557 if ( ! (MAN_BPLINE
& m
->flags
)) {
558 m
->flags
&= ~MAN_ILINE
;
561 m
->flags
&= ~MAN_BPLINE
;
564 * If we're in a block scope, then allow this macro to slip by
565 * without closing scope around it.
568 if (MAN_ILINE
& m
->flags
) {
569 m
->flags
&= ~MAN_ILINE
;
574 * If we've opened a new next-line element scope, then return
575 * now, as the next line will close out the block scope.
578 if (MAN_ELINE
& m
->flags
)
581 /* Close out the block scope opened in the prior line. */
583 assert(MAN_BLINE
& m
->flags
);
584 m
->flags
&= ~MAN_BLINE
;
586 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
588 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
590 err
: /* Error out. */
592 m
->flags
|= MAN_HALT
;
598 man_vmsg(struct man
*man
, enum mandocerr t
,
599 int ln
, int pos
, const char *fmt
, ...)
605 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
607 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
612 * Unlink a node from its context. If "m" is provided, the last parse
613 * point will also be adjusted accordingly.
616 man_node_unlink(struct man
*m
, struct man_node
*n
)
619 /* Adjust siblings. */
622 n
->prev
->next
= n
->next
;
624 n
->next
->prev
= n
->prev
;
630 if (n
->parent
->child
== n
)
631 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
634 /* Adjust parse point, if applicable. */
636 if (m
&& m
->last
== n
) {
637 /*XXX: this can occur when bailing from validation. */
638 /*assert(NULL == n->next);*/
641 m
->next
= MAN_NEXT_SIBLING
;
644 m
->next
= MAN_NEXT_CHILD
;
648 if (m
&& m
->first
== n
)