]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.97 2011/01/12 10:43:22 kristaps 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", "sp", "nf",
40 "fi", "RE", "RS", "DT",
41 "UC", "PD", "AT", "in",
45 const char * const *man_macronames
= __man_macronames
;
47 static struct man_node
*man_node_alloc(struct man
*, int, int,
48 enum man_type
, enum mant
);
49 static int man_node_append(struct man
*,
51 static int man_span_alloc(struct man
*,
52 const struct tbl_span
*);
53 static void man_node_free(struct man_node
*);
54 static void man_node_unlink(struct man
*,
56 static int man_ptext(struct man
*, int, char *, int);
57 static int man_pmacro(struct man
*, int, char *, int);
58 static void man_free1(struct man
*);
59 static void man_alloc1(struct man
*);
60 static int man_descope(struct man
*, int, int);
63 const struct man_node
*
64 man_node(const struct man
*m
)
67 assert( ! (MAN_HALT
& m
->flags
));
72 const struct man_meta
*
73 man_meta(const struct man
*m
)
76 assert( ! (MAN_HALT
& m
->flags
));
82 man_reset(struct man
*man
)
91 man_free(struct man
*man
)
100 man_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
104 p
= mandoc_calloc(1, sizeof(struct man
));
117 man_endparse(struct man
*m
)
120 assert( ! (MAN_HALT
& m
->flags
));
123 m
->flags
|= MAN_HALT
;
129 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
132 m
->flags
|= MAN_NEWLINE
;
134 assert( ! (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
);
222 if ( ! man_valid_post(man
))
233 static struct man_node
*
234 man_node_alloc(struct man
*m
, int line
, int pos
,
235 enum man_type type
, enum mant tok
)
239 p
= mandoc_calloc(1, sizeof(struct man_node
));
245 if (MAN_NEWLINE
& m
->flags
)
246 p
->flags
|= MAN_LINE
;
247 m
->flags
&= ~MAN_NEWLINE
;
253 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
257 p
= man_node_alloc(m
, line
, pos
, MAN_ELEM
, tok
);
258 if ( ! man_node_append(m
, p
))
260 m
->next
= MAN_NEXT_CHILD
;
266 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
270 p
= man_node_alloc(m
, line
, pos
, MAN_HEAD
, tok
);
271 if ( ! man_node_append(m
, p
))
273 m
->next
= MAN_NEXT_CHILD
;
279 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
283 p
= man_node_alloc(m
, line
, pos
, MAN_BODY
, tok
);
284 if ( ! man_node_append(m
, p
))
286 m
->next
= MAN_NEXT_CHILD
;
292 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
296 p
= man_node_alloc(m
, line
, pos
, MAN_BLOCK
, tok
);
297 if ( ! man_node_append(m
, p
))
299 m
->next
= MAN_NEXT_CHILD
;
304 man_span_alloc(struct man
*m
, const struct tbl_span
*span
)
308 /* FIXME: grab from span */
309 n
= man_node_alloc(m
, 0, 0, MAN_TBL
, MAN_MAX
);
312 if ( ! man_node_append(m
, n
))
315 m
->next
= MAN_NEXT_SIBLING
;
320 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
327 n
= man_node_alloc(m
, line
, pos
, MAN_TEXT
, MAN_MAX
);
328 n
->string
= mandoc_malloc(len
+ 1);
329 sv
= strlcpy(n
->string
, word
, len
+ 1);
331 /* Prohibit truncation. */
332 assert(sv
< len
+ 1);
334 if ( ! man_node_append(m
, n
))
337 m
->next
= MAN_NEXT_SIBLING
;
343 * Free all of the resources held by a node. This does NOT unlink a
344 * node from its context; for that, see man_node_unlink().
347 man_node_free(struct man_node
*p
)
357 man_node_delete(struct man
*m
, struct man_node
*p
)
361 man_node_delete(m
, p
->child
);
363 man_node_unlink(m
, p
);
369 man_addspan(struct man
*m
, const struct tbl_span
*sp
)
372 assert( ! (MAN_HALT
& m
->flags
));
373 if ( ! man_span_alloc(m
, sp
))
375 return(man_descope(m
, 0, 0));
379 man_descope(struct man
*m
, int line
, int offs
)
382 * Co-ordinate what happens with having a next-line scope open:
383 * first close out the element scope (if applicable), then close
384 * out the block scope (also if applicable).
387 if (MAN_ELINE
& m
->flags
) {
388 m
->flags
&= ~MAN_ELINE
;
389 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
393 if ( ! (MAN_BLINE
& m
->flags
))
395 m
->flags
&= ~MAN_BLINE
;
397 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
399 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
404 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
408 /* Ignore bogus comments. */
410 if ('\\' == buf
[offs
] &&
411 '.' == buf
[offs
+ 1] &&
412 '"' == buf
[offs
+ 2]) {
413 man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
417 /* Literal free-form text whitespace is preserved. */
419 if (MAN_LITERAL
& m
->flags
) {
420 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
422 return(man_descope(m
, line
, offs
));
425 /* Pump blank lines directly into the backend. */
427 for (i
= offs
; ' ' == buf
[i
]; i
++)
428 /* Skip leading whitespace. */ ;
430 if ('\0' == buf
[i
]) {
431 /* Allocate a blank entry. */
432 if ( ! man_word_alloc(m
, line
, offs
, ""))
434 return(man_descope(m
, line
, offs
));
438 * Warn if the last un-escaped character is whitespace. Then
439 * strip away the remaining spaces (tabs stay!).
442 i
= (int)strlen(buf
);
445 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
446 if (i
> 1 && '\\' != buf
[i
- 2])
447 man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
449 for (--i
; i
&& ' ' == buf
[i
]; i
--)
450 /* Spin back to non-space. */ ;
452 /* Jump ahead of escaped whitespace. */
453 i
+= '\\' == buf
[i
] ? 2 : 1;
458 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
462 * End-of-sentence check. If the last character is an unescaped
463 * EOS character, then flag the node as being the end of a
464 * sentence. The front-end will know how to interpret this.
468 if (mandoc_eos(buf
, (size_t)i
, 0))
469 m
->last
->flags
|= MAN_EOS
;
471 return(man_descope(m
, line
, offs
));
476 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
483 /* Comments and empties are quickly ignored. */
487 if ('\0' == buf
[offs
])
493 * Skip whitespace between the control character and initial
494 * text. "Whitespace" is both spaces and tabs.
497 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
499 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
508 * Copy the first word into a nil-terminated buffer.
509 * Stop copying when a tab, space, or eoln is encountered.
513 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
517 tok
= (j
> 0 && j
< 4) ? man_hash_find(mac
) : MAN_MAX
;
518 if (MAN_MAX
== tok
) {
519 man_vmsg(m
, MANDOCERR_MACRO
, ln
, ppos
, "%s", buf
+ ppos
- 1);
523 /* The macro is sane. Jump to the next word. */
525 while (buf
[i
] && ' ' == buf
[i
])
529 * Trailing whitespace. Note that tabs are allowed to be passed
530 * into the parser as "text", so we only warn about spaces here.
533 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
534 man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
537 * Remove prior ELINE macro, as it's being clobbered by a new
538 * macro. Note that NSCOPED macros do not close out ELINE
539 * macros---they don't print text---so we let those slip by.
542 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
543 m
->flags
& MAN_ELINE
) {
545 assert(MAN_TEXT
!= n
->type
);
547 /* Remove repeated NSCOPED macros causing ELINE. */
549 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
552 man_vmsg(m
, MANDOCERR_LINESCOPE
, n
->line
, n
->pos
,
553 "%s", man_macronames
[n
->tok
]);
555 man_node_delete(m
, n
);
556 m
->flags
&= ~MAN_ELINE
;
560 * Save the fact that we're in the next-line for a block. In
561 * this way, embedded roff instructions can "remember" state
565 if (MAN_BLINE
& m
->flags
)
566 m
->flags
|= MAN_BPLINE
;
568 /* Call to handler... */
570 assert(man_macros
[tok
].fp
);
571 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
576 * We weren't in a block-line scope when entering the
577 * above-parsed macro, so return.
580 if ( ! (MAN_BPLINE
& m
->flags
)) {
581 m
->flags
&= ~MAN_ILINE
;
584 m
->flags
&= ~MAN_BPLINE
;
587 * If we're in a block scope, then allow this macro to slip by
588 * without closing scope around it.
591 if (MAN_ILINE
& m
->flags
) {
592 m
->flags
&= ~MAN_ILINE
;
597 * If we've opened a new next-line element scope, then return
598 * now, as the next line will close out the block scope.
601 if (MAN_ELINE
& m
->flags
)
604 /* Close out the block scope opened in the prior line. */
606 assert(MAN_BLINE
& m
->flags
);
607 m
->flags
&= ~MAN_BLINE
;
609 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
611 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
613 err
: /* Error out. */
615 m
->flags
|= MAN_HALT
;
621 man_vmsg(struct man
*man
, enum mandocerr t
,
622 int ln
, int pos
, const char *fmt
, ...)
628 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
630 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
635 * Unlink a node from its context. If "m" is provided, the last parse
636 * point will also be adjusted accordingly.
639 man_node_unlink(struct man
*m
, struct man_node
*n
)
642 /* Adjust siblings. */
645 n
->prev
->next
= n
->next
;
647 n
->next
->prev
= n
->prev
;
653 if (n
->parent
->child
== n
)
654 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
657 /* Adjust parse point, if applicable. */
659 if (m
&& m
->last
== n
) {
660 /*XXX: this can occur when bailing from validation. */
661 /*assert(NULL == n->next);*/
664 m
->next
= MAN_NEXT_SIBLING
;
667 m
->next
= MAN_NEXT_CHILD
;
671 if (m
&& m
->first
== n
)