1 /* $Id: man.c,v 1.107 2011/03/29 08:30:49 kristaps 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",
46 const char * const *man_macronames = __man_macronames;
48 static struct man_node *man_node_alloc(struct man *, int, int,
49 enum man_type, enum mant);
50 static int man_node_append(struct man *,
52 static void man_node_free(struct man_node *);
53 static void man_node_unlink(struct man *,
55 static int man_ptext(struct man *, int, char *, int);
56 static int man_pmacro(struct man *, int, char *, int);
57 static void man_free1(struct man *);
58 static void man_alloc1(struct man *);
59 static int man_descope(struct man *, int, int);
62 const struct man_node *
63 man_node(const struct man *m)
66 assert( ! (MAN_HALT & m->flags));
71 const struct man_meta *
72 man_meta(const struct man *m)
75 assert( ! (MAN_HALT & m->flags));
81 man_reset(struct man *man)
90 man_free(struct man *man)
99 man_alloc(struct regset *regs, struct mparse *parse)
103 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));
134 return (mandoc_getcontrol(buf, &offs) ?
135 man_pmacro(m, ln, buf, offs) :
136 man_ptext(m, ln, buf, offs));
141 man_free1(struct man *man)
145 man_node_delete(man, man->first);
147 free(man->meta.title);
148 if (man->meta.source)
149 free(man->meta.source);
151 free(man->meta.date);
155 free(man->meta.msec);
160 man_alloc1(struct man *m)
163 memset(&m->meta, 0, sizeof(struct man_meta));
165 m->last = mandoc_calloc(1, sizeof(struct man_node));
167 m->last->type = MAN_ROOT;
168 m->last->tok = MAN_MAX;
169 m->next = MAN_NEXT_CHILD;
174 man_node_append(struct man *man, struct man_node *p)
179 assert(MAN_ROOT != p->type);
182 case (MAN_NEXT_SIBLING):
185 p->parent = man->last->parent;
187 case (MAN_NEXT_CHILD):
188 man->last->child = p;
189 p->parent = man->last;
199 if ( ! man_valid_pre(man, p))
204 assert(MAN_BLOCK == p->parent->type);
208 assert(MAN_BLOCK == p->parent->type);
212 assert(MAN_BLOCK == p->parent->type);
225 if ( ! man_valid_post(man))
236 static struct man_node *
237 man_node_alloc(struct man *m, int line, int pos,
238 enum man_type type, enum mant tok)
242 p = mandoc_calloc(1, sizeof(struct man_node));
248 if (MAN_NEWLINE & m->flags)
249 p->flags |= MAN_LINE;
250 m->flags &= ~MAN_NEWLINE;
256 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
260 p = man_node_alloc(m, line, pos, MAN_ELEM, tok);
261 if ( ! man_node_append(m, p))
263 m->next = MAN_NEXT_CHILD;
269 man_tail_alloc(struct man *m, int line, int pos, enum mant tok)
273 p = man_node_alloc(m, line, pos, MAN_TAIL, tok);
274 if ( ! man_node_append(m, p))
276 m->next = MAN_NEXT_CHILD;
282 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
286 p = man_node_alloc(m, line, pos, MAN_HEAD, tok);
287 if ( ! man_node_append(m, p))
289 m->next = MAN_NEXT_CHILD;
295 man_body_alloc(struct man *m, int line, int pos, enum mant tok)
299 p = man_node_alloc(m, line, pos, MAN_BODY, tok);
300 if ( ! man_node_append(m, p))
302 m->next = MAN_NEXT_CHILD;
308 man_block_alloc(struct man *m, int line, int pos, enum mant tok)
312 p = man_node_alloc(m, line, pos, MAN_BLOCK, tok);
313 if ( ! man_node_append(m, p))
315 m->next = MAN_NEXT_CHILD;
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);
368 man_addeqn(struct man *m, const struct eqn *ep)
372 assert( ! (MAN_HALT & m->flags));
374 n = man_node_alloc(m, ep->line, ep->pos, MAN_EQN, MAN_MAX);
377 if ( ! man_node_append(m, n))
380 m->next = MAN_NEXT_SIBLING;
381 return(man_descope(m, ep->line, ep->pos));
385 man_addspan(struct man *m, const struct tbl_span *sp)
389 assert( ! (MAN_HALT & m->flags));
391 n = man_node_alloc(m, sp->line, 0, MAN_TBL, MAN_MAX);
394 if ( ! man_node_append(m, n))
397 m->next = MAN_NEXT_SIBLING;
398 return(man_descope(m, sp->line, 0));
402 man_descope(struct man *m, int line, int offs)
405 * Co-ordinate what happens with having a next-line scope open:
406 * first close out the element scope (if applicable), then close
407 * out the block scope (also if applicable).
410 if (MAN_ELINE & m->flags) {
411 m->flags &= ~MAN_ELINE;
412 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
416 if ( ! (MAN_BLINE & m->flags))
418 m->flags &= ~MAN_BLINE;
420 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
422 return(man_body_alloc(m, line, offs, m->last->tok));
426 man_ptext(struct man *m, int line, char *buf, int offs)
430 /* Literal free-form text whitespace is preserved. */
432 if (MAN_LITERAL & m->flags) {
433 if ( ! man_word_alloc(m, line, offs, buf + offs))
435 return(man_descope(m, line, offs));
438 /* Pump blank lines directly into the backend. */
440 for (i = offs; ' ' == buf[i]; i++)
441 /* Skip leading whitespace. */ ;
443 if ('\0' == buf[i]) {
444 /* Allocate a blank entry. */
445 if ( ! man_word_alloc(m, line, offs, ""))
447 return(man_descope(m, line, offs));
451 * Warn if the last un-escaped character is whitespace. Then
452 * strip away the remaining spaces (tabs stay!).
455 i = (int)strlen(buf);
458 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
459 if (i > 1 && '\\' != buf[i - 2])
460 man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE);
462 for (--i; i && ' ' == buf[i]; i--)
463 /* Spin back to non-space. */ ;
465 /* Jump ahead of escaped whitespace. */
466 i += '\\' == buf[i] ? 2 : 1;
471 if ( ! man_word_alloc(m, line, offs, buf + offs))
475 * End-of-sentence check. If the last character is an unescaped
476 * EOS character, then flag the node as being the end of a
477 * sentence. The front-end will know how to interpret this.
481 if (mandoc_eos(buf, (size_t)i, 0))
482 m->last->flags |= MAN_EOS;
484 return(man_descope(m, line, offs));
488 man_pmacro(struct man *m, int ln, char *buf, int offs)
495 if ('"' == buf[offs]) {
496 man_pmsg(m, ln, offs, MANDOCERR_BADCOMMENT);
498 } else if ('\0' == buf[offs])
504 * Copy the first word into a nil-terminated buffer.
505 * Stop copying when a tab, space, or eoln is encountered.
509 while (i < 4 && '\0' != buf[offs] &&
510 ' ' != buf[offs] && '\t' != buf[offs])
511 mac[i++] = buf[offs++];
515 tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
517 if (MAN_MAX == tok) {
518 mandoc_vmsg(MANDOCERR_MACRO, m->parse, ln,
519 ppos, "%s", buf + ppos - 1);
523 /* The macro is sane. Jump to the next word. */
525 while (buf[offs] && ' ' == buf[offs])
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[offs] && ' ' == buf[offs - 1])
534 man_pmsg(m, ln, offs - 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 mandoc_vmsg(MANDOCERR_LINESCOPE, m->parse, n->line,
553 n->pos, "%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, &offs, buf))
575 * We weren't in a block-line scope when entering the
576 * above-parsed macro, so return.
579 if ( ! (MAN_BPLINE & m->flags)) {
580 m->flags &= ~MAN_ILINE;
583 m->flags &= ~MAN_BPLINE;
586 * If we're in a block scope, then allow this macro to slip by
587 * without closing scope around it.
590 if (MAN_ILINE & m->flags) {
591 m->flags &= ~MAN_ILINE;
596 * If we've opened a new next-line element scope, then return
597 * now, as the next line will close out the block scope.
600 if (MAN_ELINE & m->flags)
603 /* Close out the block scope opened in the prior line. */
605 assert(MAN_BLINE & m->flags);
606 m->flags &= ~MAN_BLINE;
608 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
610 return(man_body_alloc(m, ln, ppos, m->last->tok));
612 err: /* Error out. */
614 m->flags |= MAN_HALT;
619 * Unlink a node from its context. If "m" is provided, the last parse
620 * point will also be adjusted accordingly.
623 man_node_unlink(struct man *m, struct man_node *n)
626 /* Adjust siblings. */
629 n->prev->next = n->next;
631 n->next->prev = n->prev;
637 if (n->parent->child == n)
638 n->parent->child = n->prev ? n->prev : n->next;
641 /* Adjust parse point, if applicable. */
643 if (m && m->last == n) {
644 /*XXX: this can occur when bailing from validation. */
645 /*assert(NULL == n->next);*/
648 m->next = MAN_NEXT_SIBLING;
651 m->next = MAN_NEXT_CHILD;
655 if (m && m->first == n)