1 /* $Vendor-Id: man.c,v 1.96 2011/01/03 11:31:26 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(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 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(int line
, int pos
, enum man_type type
, enum mant tok
)
236 p
= mandoc_calloc(1, sizeof(struct man_node
));
246 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
250 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
251 if ( ! man_node_append(m
, p
))
253 m
->next
= MAN_NEXT_CHILD
;
259 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
263 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
264 if ( ! man_node_append(m
, p
))
266 m
->next
= MAN_NEXT_CHILD
;
272 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
276 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
277 if ( ! man_node_append(m
, p
))
279 m
->next
= MAN_NEXT_CHILD
;
285 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
289 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
290 if ( ! man_node_append(m
, p
))
292 m
->next
= MAN_NEXT_CHILD
;
297 man_span_alloc(struct man
*m
, const struct tbl_span
*span
)
301 /* FIXME: grab from span */
302 n
= man_node_alloc(0, 0, MAN_TBL
, MAN_MAX
);
305 if ( ! man_node_append(m
, n
))
308 m
->next
= MAN_NEXT_SIBLING
;
313 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
320 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
321 n
->string
= mandoc_malloc(len
+ 1);
322 sv
= strlcpy(n
->string
, word
, len
+ 1);
324 /* Prohibit truncation. */
325 assert(sv
< len
+ 1);
327 if ( ! man_node_append(m
, n
))
330 m
->next
= MAN_NEXT_SIBLING
;
336 * Free all of the resources held by a node. This does NOT unlink a
337 * node from its context; for that, see man_node_unlink().
340 man_node_free(struct man_node
*p
)
350 man_node_delete(struct man
*m
, struct man_node
*p
)
354 man_node_delete(m
, p
->child
);
356 man_node_unlink(m
, p
);
362 man_addspan(struct man
*m
, const struct tbl_span
*sp
)
365 assert( ! (MAN_HALT
& m
->flags
));
366 if ( ! man_span_alloc(m
, sp
))
368 return(man_descope(m
, 0, 0));
372 man_descope(struct man
*m
, int line
, int offs
)
375 * Co-ordinate what happens with having a next-line scope open:
376 * first close out the element scope (if applicable), then close
377 * out the block scope (also if applicable).
380 if (MAN_ELINE
& m
->flags
) {
381 m
->flags
&= ~MAN_ELINE
;
382 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
386 if ( ! (MAN_BLINE
& m
->flags
))
388 m
->flags
&= ~MAN_BLINE
;
390 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
392 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
397 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
401 /* Ignore bogus comments. */
403 if ('\\' == buf
[offs
] &&
404 '.' == buf
[offs
+ 1] &&
405 '"' == buf
[offs
+ 2]) {
406 man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
410 /* Literal free-form text whitespace is preserved. */
412 if (MAN_LITERAL
& m
->flags
) {
413 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
415 return(man_descope(m
, line
, offs
));
418 /* Pump blank lines directly into the backend. */
420 for (i
= offs
; ' ' == buf
[i
]; i
++)
421 /* Skip leading whitespace. */ ;
423 if ('\0' == buf
[i
]) {
424 /* Allocate a blank entry. */
425 if ( ! man_word_alloc(m
, line
, offs
, ""))
427 return(man_descope(m
, line
, offs
));
431 * Warn if the last un-escaped character is whitespace. Then
432 * strip away the remaining spaces (tabs stay!).
435 i
= (int)strlen(buf
);
438 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
439 if (i
> 1 && '\\' != buf
[i
- 2])
440 man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
442 for (--i
; i
&& ' ' == buf
[i
]; i
--)
443 /* Spin back to non-space. */ ;
445 /* Jump ahead of escaped whitespace. */
446 i
+= '\\' == buf
[i
] ? 2 : 1;
451 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
455 * End-of-sentence check. If the last character is an unescaped
456 * EOS character, then flag the node as being the end of a
457 * sentence. The front-end will know how to interpret this.
461 if (mandoc_eos(buf
, (size_t)i
, 0))
462 m
->last
->flags
|= MAN_EOS
;
464 return(man_descope(m
, line
, offs
));
469 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
476 /* Comments and empties are quickly ignored. */
480 if ('\0' == buf
[offs
])
486 * Skip whitespace between the control character and initial
487 * text. "Whitespace" is both spaces and tabs.
490 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
492 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
501 * Copy the first word into a nil-terminated buffer.
502 * Stop copying when a tab, space, or eoln is encountered.
506 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
510 tok
= (j
> 0 && j
< 4) ? man_hash_find(mac
) : MAN_MAX
;
511 if (MAN_MAX
== tok
) {
512 man_vmsg(m
, MANDOCERR_MACRO
, ln
, ppos
, "%s", buf
+ ppos
- 1);
516 /* The macro is sane. Jump to the next word. */
518 while (buf
[i
] && ' ' == buf
[i
])
522 * Trailing whitespace. Note that tabs are allowed to be passed
523 * into the parser as "text", so we only warn about spaces here.
526 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
527 man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
530 * Remove prior ELINE macro, as it's being clobbered 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
) {
538 assert(MAN_TEXT
!= n
->type
);
540 /* Remove repeated NSCOPED macros causing ELINE. */
542 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
545 man_vmsg(m
, MANDOCERR_LINESCOPE
, n
->line
, n
->pos
,
546 "%s", man_macronames
[n
->tok
]);
548 man_node_delete(m
, n
);
549 m
->flags
&= ~MAN_ELINE
;
553 * Save the fact that we're in the next-line for a block. In
554 * this way, embedded roff instructions can "remember" state
558 if (MAN_BLINE
& m
->flags
)
559 m
->flags
|= MAN_BPLINE
;
561 /* Call to handler... */
563 assert(man_macros
[tok
].fp
);
564 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
569 * We weren't in a block-line scope when entering the
570 * above-parsed macro, so return.
573 if ( ! (MAN_BPLINE
& m
->flags
)) {
574 m
->flags
&= ~MAN_ILINE
;
577 m
->flags
&= ~MAN_BPLINE
;
580 * If we're in a block scope, then allow this macro to slip by
581 * without closing scope around it.
584 if (MAN_ILINE
& m
->flags
) {
585 m
->flags
&= ~MAN_ILINE
;
590 * If we've opened a new next-line element scope, then return
591 * now, as the next line will close out the block scope.
594 if (MAN_ELINE
& m
->flags
)
597 /* Close out the block scope opened in the prior line. */
599 assert(MAN_BLINE
& m
->flags
);
600 m
->flags
&= ~MAN_BLINE
;
602 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
604 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
606 err
: /* Error out. */
608 m
->flags
|= MAN_HALT
;
614 man_vmsg(struct man
*man
, enum mandocerr t
,
615 int ln
, int pos
, const char *fmt
, ...)
621 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
623 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
628 * Unlink a node from its context. If "m" is provided, the last parse
629 * point will also be adjusted accordingly.
632 man_node_unlink(struct man
*m
, struct man_node
*n
)
635 /* Adjust siblings. */
638 n
->prev
->next
= n
->next
;
640 n
->next
->prev
= n
->prev
;
646 if (n
->parent
->child
== n
)
647 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
650 /* Adjust parse point, if applicable. */
652 if (m
&& m
->last
== n
) {
653 /*XXX: this can occur when bailing from validation. */
654 /*assert(NULL == n->next);*/
657 m
->next
= MAN_NEXT_SIBLING
;
660 m
->next
= MAN_NEXT_CHILD
;
664 if (m
&& m
->first
== n
)