]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.101 2011/02/09 09:18:15 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 void man_node_free(struct man_node
*);
52 static void man_node_unlink(struct man
*,
54 static int man_ptext(struct man
*, int, char *, int);
55 static int man_pmacro(struct man
*, int, char *, int);
56 static void man_free1(struct man
*);
57 static void man_alloc1(struct man
*);
58 static int man_descope(struct man
*, int, int);
61 const struct man_node
*
62 man_node(const struct man
*m
)
65 assert( ! (MAN_HALT
& m
->flags
));
70 const struct man_meta
*
71 man_meta(const struct man
*m
)
74 assert( ! (MAN_HALT
& m
->flags
));
80 man_reset(struct man
*man
)
89 man_free(struct man
*man
)
98 man_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
102 p
= mandoc_calloc(1, sizeof(struct man
));
115 man_endparse(struct man
*m
)
118 assert( ! (MAN_HALT
& m
->flags
));
121 m
->flags
|= MAN_HALT
;
127 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
130 m
->flags
|= MAN_NEWLINE
;
132 assert( ! (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
);
149 if (man
->meta
.rawdate
)
150 free(man
->meta
.rawdate
);
154 free(man
->meta
.msec
);
159 man_alloc1(struct man
*m
)
162 memset(&m
->meta
, 0, sizeof(struct man_meta
));
164 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
166 m
->last
->type
= MAN_ROOT
;
167 m
->last
->tok
= MAN_MAX
;
168 m
->next
= MAN_NEXT_CHILD
;
173 man_node_append(struct man
*man
, struct man_node
*p
)
178 assert(MAN_ROOT
!= p
->type
);
181 case (MAN_NEXT_SIBLING
):
184 p
->parent
= man
->last
->parent
;
186 case (MAN_NEXT_CHILD
):
187 man
->last
->child
= p
;
188 p
->parent
= man
->last
;
198 if ( ! man_valid_pre(man
, p
))
203 assert(MAN_BLOCK
== p
->parent
->type
);
207 assert(MAN_BLOCK
== p
->parent
->type
);
220 if ( ! man_valid_post(man
))
231 static struct man_node
*
232 man_node_alloc(struct man
*m
, int line
, int pos
,
233 enum man_type type
, enum mant tok
)
237 p
= mandoc_calloc(1, sizeof(struct man_node
));
243 if (MAN_NEWLINE
& m
->flags
)
244 p
->flags
|= MAN_LINE
;
245 m
->flags
&= ~MAN_NEWLINE
;
251 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
255 p
= man_node_alloc(m
, line
, pos
, MAN_ELEM
, tok
);
256 if ( ! man_node_append(m
, p
))
258 m
->next
= MAN_NEXT_CHILD
;
264 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
268 p
= man_node_alloc(m
, line
, pos
, MAN_HEAD
, tok
);
269 if ( ! man_node_append(m
, p
))
271 m
->next
= MAN_NEXT_CHILD
;
277 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
281 p
= man_node_alloc(m
, line
, pos
, MAN_BODY
, tok
);
282 if ( ! man_node_append(m
, p
))
284 m
->next
= MAN_NEXT_CHILD
;
290 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
294 p
= man_node_alloc(m
, line
, pos
, MAN_BLOCK
, tok
);
295 if ( ! man_node_append(m
, p
))
297 m
->next
= MAN_NEXT_CHILD
;
302 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
309 n
= man_node_alloc(m
, line
, pos
, MAN_TEXT
, MAN_MAX
);
310 n
->string
= mandoc_malloc(len
+ 1);
311 sv
= strlcpy(n
->string
, word
, len
+ 1);
313 /* Prohibit truncation. */
314 assert(sv
< len
+ 1);
316 if ( ! man_node_append(m
, n
))
319 m
->next
= MAN_NEXT_SIBLING
;
325 * Free all of the resources held by a node. This does NOT unlink a
326 * node from its context; for that, see man_node_unlink().
329 man_node_free(struct man_node
*p
)
339 man_node_delete(struct man
*m
, struct man_node
*p
)
343 man_node_delete(m
, p
->child
);
345 man_node_unlink(m
, p
);
350 man_addeqn(struct man
*m
, const struct eqn
*ep
)
354 assert( ! (MAN_HALT
& m
->flags
));
356 n
= man_node_alloc(m
, ep
->line
, ep
->pos
, MAN_EQN
, MAN_MAX
);
359 if ( ! man_node_append(m
, n
))
362 m
->next
= MAN_NEXT_SIBLING
;
363 return(man_descope(m
, ep
->line
, ep
->pos
));
367 man_addspan(struct man
*m
, const struct tbl_span
*sp
)
371 assert( ! (MAN_HALT
& m
->flags
));
373 n
= man_node_alloc(m
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
376 if ( ! man_node_append(m
, n
))
379 m
->next
= MAN_NEXT_SIBLING
;
380 return(man_descope(m
, sp
->line
, 0));
384 man_descope(struct man
*m
, int line
, int offs
)
387 * Co-ordinate what happens with having a next-line scope open:
388 * first close out the element scope (if applicable), then close
389 * out the block scope (also if applicable).
392 if (MAN_ELINE
& m
->flags
) {
393 m
->flags
&= ~MAN_ELINE
;
394 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
398 if ( ! (MAN_BLINE
& m
->flags
))
400 m
->flags
&= ~MAN_BLINE
;
402 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
404 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
409 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
413 /* Ignore bogus comments. */
415 if ('\\' == buf
[offs
] &&
416 '.' == buf
[offs
+ 1] &&
417 '"' == buf
[offs
+ 2]) {
418 man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
422 /* Literal free-form text whitespace is preserved. */
424 if (MAN_LITERAL
& m
->flags
) {
425 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
427 return(man_descope(m
, line
, offs
));
430 /* Pump blank lines directly into the backend. */
432 for (i
= offs
; ' ' == buf
[i
]; i
++)
433 /* Skip leading whitespace. */ ;
435 if ('\0' == buf
[i
]) {
436 /* Allocate a blank entry. */
437 if ( ! man_word_alloc(m
, line
, offs
, ""))
439 return(man_descope(m
, line
, offs
));
443 * Warn if the last un-escaped character is whitespace. Then
444 * strip away the remaining spaces (tabs stay!).
447 i
= (int)strlen(buf
);
450 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
451 if (i
> 1 && '\\' != buf
[i
- 2])
452 man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
454 for (--i
; i
&& ' ' == buf
[i
]; i
--)
455 /* Spin back to non-space. */ ;
457 /* Jump ahead of escaped whitespace. */
458 i
+= '\\' == buf
[i
] ? 2 : 1;
463 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
467 * End-of-sentence check. If the last character is an unescaped
468 * EOS character, then flag the node as being the end of a
469 * sentence. The front-end will know how to interpret this.
473 if (mandoc_eos(buf
, (size_t)i
, 0))
474 m
->last
->flags
|= MAN_EOS
;
476 return(man_descope(m
, line
, offs
));
481 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
488 /* Comments and empties are quickly ignored. */
492 if ('\0' == buf
[offs
])
498 * Skip whitespace between the control character and initial
499 * text. "Whitespace" is both spaces and tabs.
502 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
504 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
513 * Copy the first word into a nil-terminated buffer.
514 * Stop copying when a tab, space, or eoln is encountered.
518 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
522 tok
= (j
> 0 && j
< 4) ? man_hash_find(mac
) : MAN_MAX
;
523 if (MAN_MAX
== tok
) {
524 man_vmsg(m
, MANDOCERR_MACRO
, ln
, ppos
, "%s", buf
+ ppos
- 1);
528 /* The macro is sane. Jump to the next word. */
530 while (buf
[i
] && ' ' == buf
[i
])
534 * Trailing whitespace. Note that tabs are allowed to be passed
535 * into the parser as "text", so we only warn about spaces here.
538 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
539 man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
542 * Remove prior ELINE macro, as it's being clobbered by a new
543 * macro. Note that NSCOPED macros do not close out ELINE
544 * macros---they don't print text---so we let those slip by.
547 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
548 m
->flags
& MAN_ELINE
) {
550 assert(MAN_TEXT
!= n
->type
);
552 /* Remove repeated NSCOPED macros causing ELINE. */
554 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
557 man_vmsg(m
, MANDOCERR_LINESCOPE
, n
->line
, n
->pos
,
558 "%s", man_macronames
[n
->tok
]);
560 man_node_delete(m
, n
);
561 m
->flags
&= ~MAN_ELINE
;
565 * Save the fact that we're in the next-line for a block. In
566 * this way, embedded roff instructions can "remember" state
570 if (MAN_BLINE
& m
->flags
)
571 m
->flags
|= MAN_BPLINE
;
573 /* Call to handler... */
575 assert(man_macros
[tok
].fp
);
576 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
581 * We weren't in a block-line scope when entering the
582 * above-parsed macro, so return.
585 if ( ! (MAN_BPLINE
& m
->flags
)) {
586 m
->flags
&= ~MAN_ILINE
;
589 m
->flags
&= ~MAN_BPLINE
;
592 * If we're in a block scope, then allow this macro to slip by
593 * without closing scope around it.
596 if (MAN_ILINE
& m
->flags
) {
597 m
->flags
&= ~MAN_ILINE
;
602 * If we've opened a new next-line element scope, then return
603 * now, as the next line will close out the block scope.
606 if (MAN_ELINE
& m
->flags
)
609 /* Close out the block scope opened in the prior line. */
611 assert(MAN_BLINE
& m
->flags
);
612 m
->flags
&= ~MAN_BLINE
;
614 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
616 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
618 err
: /* Error out. */
620 m
->flags
|= MAN_HALT
;
626 man_vmsg(struct man
*man
, enum mandocerr t
,
627 int ln
, int pos
, const char *fmt
, ...)
633 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
635 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
640 * Unlink a node from its context. If "m" is provided, the last parse
641 * point will also be adjusted accordingly.
644 man_node_unlink(struct man
*m
, struct man_node
*n
)
647 /* Adjust siblings. */
650 n
->prev
->next
= n
->next
;
652 n
->next
->prev
= n
->prev
;
658 if (n
->parent
->child
== n
)
659 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
662 /* Adjust parse point, if applicable. */
664 if (m
&& m
->last
== n
) {
665 /*XXX: this can occur when bailing from validation. */
666 /*assert(NULL == n->next);*/
669 m
->next
= MAN_NEXT_SIBLING
;
672 m
->next
= MAN_NEXT_CHILD
;
676 if (m
&& m
->first
== n
)