]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.80 2010/06/27 16:18:13 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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>
33 #include "libmandoc.h"
35 const char *const __man_macronames
[MAN_MAX
] = {
36 "br", "TH", "SH", "SS",
37 "TP", "LP", "PP", "P",
38 "IP", "HP", "SM", "SB",
39 "BI", "IB", "BR", "RB",
41 "RI", "na", "i", "sp",
42 "nf", "fi", "r", "RE",
43 "RS", "DT", "UC", "PD",
44 "Sp", "Vb", "Ve", "AT",
47 const char * const *man_macronames
= __man_macronames
;
49 static struct man_node
*man_node_alloc(int, int,
50 enum man_type
, enum mant
);
51 static int man_node_append(struct man
*,
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 macrowarn(struct man
*, int, const char *, int);
63 const struct man_node
*
64 man_node(const struct man
*m
)
67 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
71 const struct man_meta
*
72 man_meta(const struct man
*m
)
75 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
80 man_reset(struct man
*man
)
89 man_free(struct man
*man
)
98 man_alloc(struct regset
*regs
, void *data
,
99 int pflags
, mandocmsg msg
)
103 p
= mandoc_calloc(1, sizeof(struct man
));
117 man_endparse(struct man
*m
)
120 if (MAN_HALT
& m
->flags
)
122 else if (man_macroend(m
))
124 m
->flags
|= MAN_HALT
;
130 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
133 if (MAN_HALT
& m
->flags
)
136 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
137 man_pmacro(m
, ln
, buf
, offs
) :
138 man_ptext(m
, ln
, buf
, offs
));
143 man_free1(struct man
*man
)
147 man_node_delete(man
, man
->first
);
149 free(man
->meta
.title
);
150 if (man
->meta
.source
)
151 free(man
->meta
.source
);
152 if (man
->meta
.rawdate
)
153 free(man
->meta
.rawdate
);
157 free(man
->meta
.msec
);
162 man_alloc1(struct man
*m
)
165 memset(&m
->meta
, 0, sizeof(struct man_meta
));
167 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
169 m
->last
->type
= MAN_ROOT
;
170 m
->last
->tok
= MAN_MAX
;
171 m
->next
= MAN_NEXT_CHILD
;
176 man_node_append(struct man
*man
, struct man_node
*p
)
181 assert(MAN_ROOT
!= p
->type
);
184 case (MAN_NEXT_SIBLING
):
187 p
->parent
= man
->last
->parent
;
189 case (MAN_NEXT_CHILD
):
190 man
->last
->child
= p
;
191 p
->parent
= man
->last
;
201 if ( ! man_valid_pre(man
, p
))
206 assert(MAN_BLOCK
== p
->parent
->type
);
210 assert(MAN_BLOCK
== p
->parent
->type
);
221 if ( ! man_valid_post(man
))
223 if ( ! man_action_post(man
))
234 static struct man_node
*
235 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
239 p
= mandoc_calloc(1, sizeof(struct man_node
));
249 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
253 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
254 if ( ! man_node_append(m
, p
))
256 m
->next
= MAN_NEXT_CHILD
;
262 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
266 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
267 if ( ! man_node_append(m
, p
))
269 m
->next
= MAN_NEXT_CHILD
;
275 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
279 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
280 if ( ! man_node_append(m
, p
))
282 m
->next
= MAN_NEXT_CHILD
;
288 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
292 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
293 if ( ! man_node_append(m
, p
))
295 m
->next
= MAN_NEXT_CHILD
;
301 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
308 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
309 n
->string
= mandoc_malloc(len
+ 1);
310 sv
= strlcpy(n
->string
, word
, len
+ 1);
312 /* Prohibit truncation. */
313 assert(sv
< len
+ 1);
315 if ( ! man_node_append(m
, n
))
318 m
->next
= MAN_NEXT_SIBLING
;
324 * Free all of the resources held by a node. This does NOT unlink a
325 * node from its context; for that, see man_node_unlink().
328 man_node_free(struct man_node
*p
)
338 man_node_delete(struct man
*m
, struct man_node
*p
)
342 man_node_delete(m
, p
->child
);
344 man_node_unlink(m
, p
);
350 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
354 /* Ignore bogus comments. */
356 if ('\\' == buf
[offs
] &&
357 '.' == buf
[offs
+ 1] &&
358 '"' == buf
[offs
+ 2])
359 return(man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
));
361 /* Literal free-form text whitespace is preserved. */
363 if (MAN_LITERAL
& m
->flags
) {
364 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
369 /* Pump blank lines directly into the backend. */
371 for (i
= offs
; ' ' == buf
[i
]; i
++)
372 /* Skip leading whitespace. */ ;
374 if ('\0' == buf
[i
]) {
375 /* Allocate a blank entry. */
376 if ( ! man_word_alloc(m
, line
, offs
, ""))
382 * Warn if the last un-escaped character is whitespace. Then
383 * strip away the remaining spaces (tabs stay!).
386 i
= (int)strlen(buf
);
389 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
390 if (i
> 1 && '\\' != buf
[i
- 2])
391 if ( ! man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
))
394 for (--i
; i
&& ' ' == buf
[i
]; i
--)
395 /* Spin back to non-space. */ ;
397 /* Jump ahead of escaped whitespace. */
398 i
+= '\\' == buf
[i
] ? 2 : 1;
403 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
407 * End-of-sentence check. If the last character is an unescaped
408 * EOS character, then flag the node as being the end of a
409 * sentence. The front-end will know how to interpret this.
413 if (mandoc_eos(buf
, (size_t)i
))
414 m
->last
->flags
|= MAN_EOS
;
418 * Co-ordinate what happens with having a next-line scope open:
419 * first close out the element scope (if applicable), then close
420 * out the block scope (also if applicable).
423 if (MAN_ELINE
& m
->flags
) {
424 m
->flags
&= ~MAN_ELINE
;
425 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
429 if ( ! (MAN_BLINE
& m
->flags
))
431 m
->flags
&= ~MAN_BLINE
;
433 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
435 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
440 macrowarn(struct man
*m
, int ln
, const char *buf
, int offs
)
444 rc
= man_vmsg(m
, MANDOCERR_MACRO
, ln
, offs
,
445 "unknown macro: %s%s",
446 buf
, strlen(buf
) > 3 ? "..." : "");
448 return(MAN_IGN_MACRO
& m
->pflags
? rc
: 0);
453 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
460 /* Comments and empties are quickly ignored. */
464 if ('\0' == buf
[offs
])
470 * Skip whitespace between the control character and initial
471 * text. "Whitespace" is both spaces and tabs.
474 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
476 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
484 /* Copy the first word into a nil-terminated buffer. */
486 for (j
= 0; j
< 4; j
++, i
++) {
487 if ('\0' == (mac
[j
] = buf
[i
]))
489 else if (' ' == buf
[i
])
492 /* Check for invalid characters. */
494 if (isgraph((u_char
)buf
[i
]))
496 if ( ! man_pmsg(m
, ln
, i
, MANDOCERR_BADCHAR
))
503 if (j
== 4 || j
< 1) {
504 if ( ! macrowarn(m
, ln
, mac
, ppos
))
509 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
510 if ( ! macrowarn(m
, ln
, mac
, ppos
))
515 /* The macro is sane. Jump to the next word. */
517 while (buf
[i
] && ' ' == buf
[i
])
521 * Trailing whitespace. Note that tabs are allowed to be passed
522 * into the parser as "text", so we only warn about spaces here.
525 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
526 if ( ! man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
))
530 * Remove prior ELINE macro, as it's being clobbering by a new
531 * macro. Note that NSCOPED macros do not close out ELINE
532 * macros---they don't print text---so we let those slip by.
535 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
536 m
->flags
& MAN_ELINE
) {
537 assert(MAN_TEXT
!= m
->last
->type
);
540 * This occurs in the following construction:
546 * Flat-out disallow this madness.
548 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
) {
549 man_pmsg(m
, ln
, ppos
, MANDOCERR_SYNTLINESCOPE
);
556 assert(NULL
== n
->child
);
557 assert(0 == n
->nchild
);
559 if ( ! man_nmsg(m
, n
, MANDOCERR_LINESCOPE
))
562 man_node_delete(m
, n
);
563 m
->flags
&= ~MAN_ELINE
;
567 * Save the fact that we're in the next-line for a block. In
568 * this way, embedded roff instructions can "remember" state
572 if (MAN_BLINE
& m
->flags
)
573 m
->flags
|= MAN_BPLINE
;
575 /* Call to handler... */
577 assert(man_macros
[tok
].fp
);
578 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
583 * We weren't in a block-line scope when entering the
584 * above-parsed macro, so return.
587 if ( ! (MAN_BPLINE
& m
->flags
)) {
588 m
->flags
&= ~MAN_ILINE
;
591 m
->flags
&= ~MAN_BPLINE
;
594 * If we're in a block scope, then allow this macro to slip by
595 * without closing scope around it.
598 if (MAN_ILINE
& m
->flags
) {
599 m
->flags
&= ~MAN_ILINE
;
604 * If we've opened a new next-line element scope, then return
605 * now, as the next line will close out the block scope.
608 if (MAN_ELINE
& m
->flags
)
611 /* Close out the block scope opened in the prior line. */
613 assert(MAN_BLINE
& m
->flags
);
614 m
->flags
&= ~MAN_BLINE
;
616 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
618 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
620 err
: /* Error out. */
622 m
->flags
|= MAN_HALT
;
628 man_vmsg(struct man
*man
, enum mandocerr t
,
629 int ln
, int pos
, const char *fmt
, ...)
635 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
637 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
642 * Unlink a node from its context. If "m" is provided, the last parse
643 * point will also be adjusted accordingly.
646 man_node_unlink(struct man
*m
, struct man_node
*n
)
649 /* Adjust siblings. */
652 n
->prev
->next
= n
->next
;
654 n
->next
->prev
= n
->prev
;
660 if (n
->parent
->child
== n
)
661 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
664 /* Adjust parse point, if applicable. */
666 if (m
&& m
->last
== n
) {
667 /*XXX: this can occur when bailing from validation. */
668 /*assert(NULL == n->next);*/
671 m
->next
= MAN_NEXT_SIBLING
;
674 m
->next
= MAN_NEXT_CHILD
;
678 if (m
&& m
->first
== n
)