1 /* $Id: man.c,v 1.121 2013/11/10 22:54:40 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 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>
32 #include "libmandoc.h"
34 const char *const __man_macronames[MAN_MAX] = {
35 "br", "TH", "SH", "SS",
36 "TP", "LP", "PP", "P",
37 "IP", "HP", "SM", "SB",
38 "BI", "IB", "BR", "RB",
40 "RI", "na", "sp", "nf",
41 "fi", "RE", "RS", "DT",
42 "UC", "PD", "AT", "in",
43 "ft", "OP", "EX", "EE",
47 const char * const *man_macronames = __man_macronames;
49 static struct man_node *man_node_alloc(struct man *, 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 man_descope(struct man *, int, int);
63 const struct man_node *
64 man_node(const struct man *man)
67 assert( ! (MAN_HALT & man->flags));
72 const struct man_meta *
73 man_meta(const struct man *man)
76 assert( ! (MAN_HALT & man->flags));
82 man_reset(struct man *man)
91 man_free(struct man *man)
100 man_alloc(struct roff *roff, struct mparse *parse)
104 p = mandoc_calloc(1, sizeof(struct man));
116 man_endparse(struct man *man)
119 assert( ! (MAN_HALT & man->flags));
120 if (man_macroend(man))
122 man->flags |= MAN_HALT;
128 man_parseln(struct man *man, int ln, char *buf, int offs)
131 man->flags |= MAN_NEWLINE;
133 assert( ! (MAN_HALT & man->flags));
135 return (roff_getcontrol(man->roff, buf, &offs) ?
136 man_pmacro(man, ln, buf, offs) :
137 man_ptext(man, 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);
152 free(man->meta.date);
156 free(man->meta.msec);
161 man_alloc1(struct man *man)
164 memset(&man->meta, 0, sizeof(struct man_meta));
166 man->last = mandoc_calloc(1, sizeof(struct man_node));
167 man->first = man->last;
168 man->last->type = MAN_ROOT;
169 man->last->tok = MAN_MAX;
170 man->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);
213 assert(MAN_BLOCK == p->parent->type);
226 if ( ! man_valid_post(man))
237 static struct man_node *
238 man_node_alloc(struct man *man, int line, int pos,
239 enum man_type type, enum mant tok)
243 p = mandoc_calloc(1, sizeof(struct man_node));
249 if (MAN_NEWLINE & man->flags)
250 p->flags |= MAN_LINE;
251 man->flags &= ~MAN_NEWLINE;
257 man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
261 p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
262 if ( ! man_node_append(man, p))
264 man->next = MAN_NEXT_CHILD;
270 man_tail_alloc(struct man *man, int line, int pos, enum mant tok)
274 p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
275 if ( ! man_node_append(man, p))
277 man->next = MAN_NEXT_CHILD;
283 man_head_alloc(struct man *man, int line, int pos, enum mant tok)
287 p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
288 if ( ! man_node_append(man, p))
290 man->next = MAN_NEXT_CHILD;
296 man_body_alloc(struct man *man, int line, int pos, enum mant tok)
300 p = man_node_alloc(man, line, pos, MAN_BODY, tok);
301 if ( ! man_node_append(man, p))
303 man->next = MAN_NEXT_CHILD;
309 man_block_alloc(struct man *man, int line, int pos, enum mant tok)
313 p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
314 if ( ! man_node_append(man, p))
316 man->next = MAN_NEXT_CHILD;
321 man_word_alloc(struct man *man, int line, int pos, const char *word)
325 n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
326 n->string = roff_strdup(man->roff, word);
328 if ( ! man_node_append(man, n))
331 man->next = MAN_NEXT_SIBLING;
337 * Free all of the resources held by a node. This does NOT unlink a
338 * node from its context; for that, see man_node_unlink().
341 man_node_free(struct man_node *p)
351 man_node_delete(struct man *man, struct man_node *p)
355 man_node_delete(man, p->child);
357 man_node_unlink(man, p);
362 man_addeqn(struct man *man, const struct eqn *ep)
366 assert( ! (MAN_HALT & man->flags));
368 n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
371 if ( ! man_node_append(man, n))
374 man->next = MAN_NEXT_SIBLING;
375 return(man_descope(man, ep->ln, ep->pos));
379 man_addspan(struct man *man, const struct tbl_span *sp)
383 assert( ! (MAN_HALT & man->flags));
385 n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
388 if ( ! man_node_append(man, n))
391 man->next = MAN_NEXT_SIBLING;
392 return(man_descope(man, sp->line, 0));
396 man_descope(struct man *man, int line, int offs)
399 * Co-ordinate what happens with having a next-line scope open:
400 * first close out the element scope (if applicable), then close
401 * out the block scope (also if applicable).
404 if (MAN_ELINE & man->flags) {
405 man->flags &= ~MAN_ELINE;
406 if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
410 if ( ! (MAN_BLINE & man->flags))
412 man->flags &= ~MAN_BLINE;
414 if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
416 return(man_body_alloc(man, line, offs, man->last->tok));
420 man_ptext(struct man *man, int line, char *buf, int offs)
424 /* Literal free-form text whitespace is preserved. */
426 if (MAN_LITERAL & man->flags) {
427 if ( ! man_word_alloc(man, line, offs, buf + offs))
429 return(man_descope(man, line, offs));
432 for (i = offs; ' ' == buf[i]; i++)
433 /* Skip leading whitespace. */ ;
436 * Blank lines are ignored right after headings
437 * but add a single vertical space elsewhere.
440 if ('\0' == buf[i]) {
441 /* Allocate a blank entry. */
442 if (MAN_SH != man->last->tok &&
443 MAN_SS != man->last->tok) {
444 if ( ! man_elem_alloc(man, line, offs, MAN_sp))
446 man->next = MAN_NEXT_SIBLING;
452 * Warn if the last un-escaped character is whitespace. Then
453 * strip away the remaining spaces (tabs stay!).
456 i = (int)strlen(buf);
459 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
460 if (i > 1 && '\\' != buf[i - 2])
461 man_pmsg(man, line, i - 1, MANDOCERR_EOLNSPACE);
463 for (--i; i && ' ' == buf[i]; i--)
464 /* Spin back to non-space. */ ;
466 /* Jump ahead of escaped whitespace. */
467 i += '\\' == buf[i] ? 2 : 1;
472 if ( ! man_word_alloc(man, line, offs, buf + offs))
476 * End-of-sentence check. If the last character is an unescaped
477 * EOS character, then flag the node as being the end of a
478 * sentence. The front-end will know how to interpret this.
482 if (mandoc_eos(buf, (size_t)i, 0))
483 man->last->flags |= MAN_EOS;
485 return(man_descope(man, line, offs));
489 man_pmacro(struct man *man, int ln, char *buf, int offs)
496 if ('"' == buf[offs]) {
497 man_pmsg(man, ln, offs, MANDOCERR_BADCOMMENT);
499 } else if ('\0' == buf[offs])
505 * Copy the first word into a nil-terminated buffer.
506 * Stop copying when a tab, space, or eoln is encountered.
510 while (i < 4 && '\0' != buf[offs] &&
511 ' ' != buf[offs] && '\t' != buf[offs])
512 mac[i++] = buf[offs++];
516 tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
518 if (MAN_MAX == tok) {
519 mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln,
520 ppos, "%s", buf + ppos - 1);
524 /* The macro is sane. Jump to the next word. */
526 while (buf[offs] && ' ' == buf[offs])
530 * Trailing whitespace. Note that tabs are allowed to be passed
531 * into the parser as "text", so we only warn about spaces here.
534 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
535 man_pmsg(man, ln, offs - 1, MANDOCERR_EOLNSPACE);
538 * Remove prior ELINE macro, as it's being clobbered by a new
539 * macro. Note that NSCOPED macros do not close out ELINE
540 * macros---they don't print text---so we let those slip by.
543 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
544 man->flags & MAN_ELINE) {
546 assert(MAN_TEXT != n->type);
548 /* Remove repeated NSCOPED macros causing ELINE. */
550 if (MAN_NSCOPED & man_macros[n->tok].flags)
553 mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
554 n->pos, "%s breaks %s", man_macronames[tok],
555 man_macronames[n->tok]);
557 man_node_delete(man, n);
558 man->flags &= ~MAN_ELINE;
562 * Remove prior BLINE macro that is being clobbered.
564 if ((man->flags & MAN_BLINE) &&
565 (MAN_BSCOPE & man_macros[tok].flags)) {
568 /* Might be a text node like 8 in
572 if (MAN_TEXT == n->type)
575 /* Remove element that didn't end BLINE, if any. */
576 if ( ! (MAN_BSCOPE & man_macros[n->tok].flags))
579 assert(MAN_HEAD == n->type);
581 assert(MAN_BLOCK == n->type);
582 assert(MAN_SCOPED & man_macros[n->tok].flags);
584 mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
585 n->pos, "%s breaks %s", man_macronames[tok],
586 man_macronames[n->tok]);
588 man_node_delete(man, n);
589 man->flags &= ~MAN_BLINE;
593 * Save the fact that we're in the next-line for a block. In
594 * this way, embedded roff instructions can "remember" state
598 if (MAN_BLINE & man->flags)
599 man->flags |= MAN_BPLINE;
601 /* Call to handler... */
603 assert(man_macros[tok].fp);
604 if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf))
608 * We weren't in a block-line scope when entering the
609 * above-parsed macro, so return.
612 if ( ! (MAN_BPLINE & man->flags)) {
613 man->flags &= ~MAN_ILINE;
616 man->flags &= ~MAN_BPLINE;
619 * If we're in a block scope, then allow this macro to slip by
620 * without closing scope around it.
623 if (MAN_ILINE & man->flags) {
624 man->flags &= ~MAN_ILINE;
629 * If we've opened a new next-line element scope, then return
630 * now, as the next line will close out the block scope.
633 if (MAN_ELINE & man->flags)
636 /* Close out the block scope opened in the prior line. */
638 assert(MAN_BLINE & man->flags);
639 man->flags &= ~MAN_BLINE;
641 if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
643 return(man_body_alloc(man, ln, ppos, man->last->tok));
645 err: /* Error out. */
647 man->flags |= MAN_HALT;
652 * Unlink a node from its context. If "man" is provided, the last parse
653 * point will also be adjusted accordingly.
656 man_node_unlink(struct man *man, struct man_node *n)
659 /* Adjust siblings. */
662 n->prev->next = n->next;
664 n->next->prev = n->prev;
670 if (n->parent->child == n)
671 n->parent->child = n->prev ? n->prev : n->next;
674 /* Adjust parse point, if applicable. */
676 if (man && man->last == n) {
677 /*XXX: this can occur when bailing from validation. */
678 /*assert(NULL == n->next);*/
681 man->next = MAN_NEXT_SIBLING;
683 man->last = n->parent;
684 man->next = MAN_NEXT_CHILD;
688 if (man && man->first == n)
692 const struct mparse *
693 man_mparse(const struct man *man)
696 assert(man && man->parse);