1 /* $Id: man.c,v 1.145 2014/11/28 06:27:05 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
32 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 const char *const __man_macronames[MAN_MAX] = {
37 "br", "TH", "SH", "SS",
38 "TP", "LP", "PP", "P",
39 "IP", "HP", "SM", "SB",
40 "BI", "IB", "BR", "RB",
42 "RI", "na", "sp", "nf",
43 "fi", "RE", "RS", "DT",
44 "UC", "PD", "AT", "in",
45 "ft", "OP", "EX", "EE",
49 const char * const *man_macronames = __man_macronames;
51 static struct man_node *man_node_alloc(struct man *, int, int,
52 enum man_type, enum mant);
53 static void man_node_append(struct man *, struct man_node *);
54 static void man_node_free(struct man_node *);
55 static void man_node_unlink(struct man *,
57 static int man_ptext(struct man *, int, char *, int);
58 static int man_pmacro(struct man *, int, char *, int);
59 static void man_free1(struct man *);
60 static void man_alloc1(struct man *);
61 static void man_descope(struct man *, int, int);
64 const struct man_node *
65 man_node(const struct man *man)
71 const struct man_meta *
72 man_meta(const struct man *man)
79 man_reset(struct man *man)
87 man_free(struct man *man)
95 man_alloc(struct roff *roff, struct mparse *parse, int quick)
99 p = mandoc_calloc(1, sizeof(struct man));
111 man_endparse(struct man *man)
119 man_parseln(struct man *man, int ln, char *buf, int offs)
122 if (man->last->type != MAN_EQN || ln > man->last->line)
123 man->flags |= MAN_NEWLINE;
125 return (roff_getcontrol(man->roff, buf, &offs) ?
126 man_pmacro(man, ln, buf, offs) :
127 man_ptext(man, ln, buf, offs));
131 man_free1(struct man *man)
135 man_node_delete(man, man->first);
136 free(man->meta.title);
137 free(man->meta.source);
138 free(man->meta.date);
140 free(man->meta.msec);
144 man_alloc1(struct man *man)
147 memset(&man->meta, 0, sizeof(struct man_meta));
149 man->last = mandoc_calloc(1, sizeof(struct man_node));
150 man->first = man->last;
151 man->last->type = MAN_ROOT;
152 man->last->tok = MAN_MAX;
153 man->next = MAN_NEXT_CHILD;
158 man_node_append(struct man *man, struct man_node *p)
163 assert(p->type != MAN_ROOT);
166 case MAN_NEXT_SIBLING:
169 p->parent = man->last->parent;
172 man->last->child = p;
173 p->parent = man->last;
185 if (p->tok == MAN_SH || p->tok == MAN_SS)
186 man->flags &= ~MAN_LITERAL;
189 assert(p->parent->type == MAN_BLOCK);
193 assert(p->parent->type == MAN_BLOCK);
213 static struct man_node *
214 man_node_alloc(struct man *man, int line, int pos,
215 enum man_type type, enum mant tok)
219 p = mandoc_calloc(1, sizeof(struct man_node));
225 if (man->flags & MAN_NEWLINE)
226 p->flags |= MAN_LINE;
227 man->flags &= ~MAN_NEWLINE;
232 man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
236 p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
237 man_node_append(man, p);
238 man->next = MAN_NEXT_CHILD;
242 man_head_alloc(struct man *man, int line, int pos, enum mant tok)
246 p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
247 man_node_append(man, p);
248 man->next = MAN_NEXT_CHILD;
252 man_body_alloc(struct man *man, int line, int pos, enum mant tok)
256 p = man_node_alloc(man, line, pos, MAN_BODY, tok);
257 man_node_append(man, p);
258 man->next = MAN_NEXT_CHILD;
262 man_block_alloc(struct man *man, int line, int pos, enum mant tok)
266 p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
267 man_node_append(man, p);
268 man->next = MAN_NEXT_CHILD;
272 man_word_alloc(struct man *man, int line, int pos, const char *word)
276 n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
277 n->string = roff_strdup(man->roff, word);
278 man_node_append(man, n);
279 man->next = MAN_NEXT_SIBLING;
283 man_word_append(struct man *man, const char *word)
286 char *addstr, *newstr;
289 addstr = roff_strdup(man->roff, word);
290 mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
294 man->next = MAN_NEXT_SIBLING;
298 * Free all of the resources held by a node. This does NOT unlink a
299 * node from its context; for that, see man_node_unlink().
302 man_node_free(struct man_node *p)
310 man_node_delete(struct man *man, struct man_node *p)
314 man_node_delete(man, p->child);
316 man_node_unlink(man, p);
321 man_addeqn(struct man *man, const struct eqn *ep)
325 n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
327 if (ep->ln > man->last->line)
328 n->flags |= MAN_LINE;
329 man_node_append(man, n);
330 man->next = MAN_NEXT_SIBLING;
331 man_descope(man, ep->ln, ep->pos);
335 man_addspan(struct man *man, const struct tbl_span *sp)
339 n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
341 man_node_append(man, n);
342 man->next = MAN_NEXT_SIBLING;
343 man_descope(man, sp->line, 0);
347 man_descope(struct man *man, int line, int offs)
350 * Co-ordinate what happens with having a next-line scope open:
351 * first close out the element scope (if applicable), then close
352 * out the block scope (also if applicable).
355 if (man->flags & MAN_ELINE) {
356 man->flags &= ~MAN_ELINE;
357 man_unscope(man, man->last->parent);
359 if ( ! (man->flags & MAN_BLINE))
361 man->flags &= ~MAN_BLINE;
362 man_unscope(man, man->last->parent);
363 man_body_alloc(man, line, offs, man->last->tok);
367 man_ptext(struct man *man, int line, char *buf, int offs)
371 /* Literal free-form text whitespace is preserved. */
373 if (man->flags & MAN_LITERAL) {
374 man_word_alloc(man, line, offs, buf + offs);
375 man_descope(man, line, offs);
379 for (i = offs; buf[i] == ' '; i++)
380 /* Skip leading whitespace. */ ;
383 * Blank lines are ignored right after headings
384 * but add a single vertical space elsewhere.
387 if (buf[i] == '\0') {
388 /* Allocate a blank entry. */
389 if (man->last->tok != MAN_SH &&
390 man->last->tok != MAN_SS) {
391 man_elem_alloc(man, line, offs, MAN_sp);
392 man->next = MAN_NEXT_SIBLING;
398 * Warn if the last un-escaped character is whitespace. Then
399 * strip away the remaining spaces (tabs stay!).
402 i = (int)strlen(buf);
405 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
406 if (i > 1 && '\\' != buf[i - 2])
407 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
410 for (--i; i && ' ' == buf[i]; i--)
411 /* Spin back to non-space. */ ;
413 /* Jump ahead of escaped whitespace. */
414 i += '\\' == buf[i] ? 2 : 1;
418 man_word_alloc(man, line, offs, buf + offs);
421 * End-of-sentence check. If the last character is an unescaped
422 * EOS character, then flag the node as being the end of a
423 * sentence. The front-end will know how to interpret this.
427 if (mandoc_eos(buf, (size_t)i))
428 man->last->flags |= MAN_EOS;
430 man_descope(man, line, offs);
435 man_pmacro(struct man *man, int ln, char *buf, int offs)
447 * Copy the first word into a nil-terminated buffer.
448 * Stop when a space, tab, escape, or eoln is encountered.
452 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
453 mac[i++] = buf[offs++];
457 tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
459 if (tok == MAN_MAX) {
460 mandoc_msg(MANDOCERR_MACRO, man->parse,
461 ln, ppos, buf + ppos - 1);
465 /* Skip a leading escape sequence or tab. */
470 mandoc_escape(&cp, NULL, NULL);
480 /* Jump to the next non-whitespace word. */
482 while (buf[offs] && buf[offs] == ' ')
486 * Trailing whitespace. Note that tabs are allowed to be passed
487 * into the parser as "text", so we only warn about spaces here.
490 if (buf[offs] == '\0' && buf[offs - 1] == ' ')
491 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
495 * Remove prior ELINE macro, as it's being clobbered by a new
496 * macro. Note that NSCOPED macros do not close out ELINE
497 * macros---they don't print text---so we let those slip by.
500 if ( ! (man_macros[tok].flags & MAN_NSCOPED) &&
501 man->flags & MAN_ELINE) {
503 assert(MAN_TEXT != n->type);
505 /* Remove repeated NSCOPED macros causing ELINE. */
507 if (man_macros[n->tok].flags & MAN_NSCOPED)
510 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line,
511 n->pos, "%s breaks %s", man_macronames[tok],
512 man_macronames[n->tok]);
514 man_node_delete(man, n);
515 man->flags &= ~MAN_ELINE;
519 * Remove prior BLINE macro that is being clobbered.
521 if ((man->flags & MAN_BLINE) &&
522 (man_macros[tok].flags & MAN_BSCOPE)) {
525 /* Might be a text node like 8 in
529 if (n->type == MAN_TEXT)
532 /* Remove element that didn't end BLINE, if any. */
533 if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
536 assert(n->type == MAN_HEAD);
538 assert(n->type == MAN_BLOCK);
539 assert(man_macros[n->tok].flags & MAN_SCOPED);
541 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line,
542 n->pos, "%s breaks %s", man_macronames[tok],
543 man_macronames[n->tok]);
545 man_node_delete(man, n);
546 man->flags &= ~MAN_BLINE;
549 /* Remember whether we are in next-line scope for a block head. */
551 bline = man->flags & MAN_BLINE;
553 /* Call to handler... */
555 assert(man_macros[tok].fp);
556 (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
558 /* In quick mode (for mandocdb), abort after the NAME section. */
560 if (man->quick && tok == MAN_SH) {
562 if (n->type == MAN_BODY &&
563 strcmp(n->prev->child->string, "NAME"))
568 * If we are in a next-line scope for a block head,
569 * close it out now and switch to the body,
570 * unless the next-line scope is allowed to continue.
573 if ( ! bline || man->flags & MAN_ELINE ||
574 man_macros[tok].flags & MAN_NSCOPED)
577 assert(man->flags & MAN_BLINE);
578 man->flags &= ~MAN_BLINE;
580 man_unscope(man, man->last->parent);
581 man_body_alloc(man, ln, ppos, man->last->tok);
586 * Unlink a node from its context. If "man" is provided, the last parse
587 * point will also be adjusted accordingly.
590 man_node_unlink(struct man *man, struct man_node *n)
593 /* Adjust siblings. */
596 n->prev->next = n->next;
598 n->next->prev = n->prev;
604 if (n->parent->child == n)
605 n->parent->child = n->prev ? n->prev : n->next;
608 /* Adjust parse point, if applicable. */
610 if (man && man->last == n) {
611 /*XXX: this can occur when bailing from validation. */
612 /*assert(NULL == n->next);*/
615 man->next = MAN_NEXT_SIBLING;
617 man->last = n->parent;
618 man->next = MAN_NEXT_CHILD;
622 if (man && man->first == n)
626 const struct mparse *
627 man_mparse(const struct man *man)
630 assert(man && man->parse);
635 man_deroff(char **dest, const struct man_node *n)
640 if (n->type != MAN_TEXT) {
641 for (n = n->child; n; n = n->next)
646 /* Skip leading whitespace and escape sequences. */
649 while ('\0' != *cp) {
652 mandoc_escape((const char **)&cp, NULL, NULL);
653 } else if (isspace((unsigned char)*cp))
659 /* Skip trailing whitespace. */
661 for (sz = strlen(cp); sz; sz--)
662 if (0 == isspace((unsigned char)cp[sz-1]))
665 /* Skip empty strings. */
671 *dest = mandoc_strndup(cp, sz);
675 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);