=================================================================== RCS file: /cvs/mandoc/man.c,v retrieving revision 1.141 retrieving revision 1.150 diff -u -p -r1.141 -r1.150 --- mandoc/man.c 2014/10/20 15:50:24 1.141 +++ mandoc/man.c 2015/04/02 21:36:49 1.150 @@ -1,16 +1,16 @@ -/* $Id: man.c,v 1.141 2014/10/20 15:50:24 schwarze Exp $ */ +/* $Id: man.c,v 1.150 2015/04/02 21:36:49 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2013, 2014 Ingo Schwarze + * Copyright (c) 2013, 2014, 2015 Ingo Schwarze * Copyright (c) 2011 Joerg Sonnenberger * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -27,11 +27,12 @@ #include #include -#include "man.h" -#include "mandoc.h" #include "mandoc_aux.h" -#include "libman.h" +#include "mandoc.h" +#include "roff.h" +#include "man.h" #include "libmandoc.h" +#include "libman.h" const char *const __man_macronames[MAN_MAX] = { "br", "TH", "SH", "SS", @@ -39,7 +40,7 @@ const char *const __man_macronames[MAN_MAX] = { "IP", "HP", "SM", "SB", "BI", "IB", "BR", "RB", "R", "B", "I", "IR", - "RI", "na", "sp", "nf", + "RI", "sp", "nf", "fi", "RE", "RS", "DT", "UC", "PD", "AT", "in", "ft", "OP", "EX", "EE", @@ -48,18 +49,18 @@ const char *const __man_macronames[MAN_MAX] = { const char * const *man_macronames = __man_macronames; +static void man_alloc1(struct man *); +static void man_breakscope(struct man *, enum mant); +static void man_descope(struct man *, int, int); +static void man_free1(struct man *); static struct man_node *man_node_alloc(struct man *, int, int, - enum man_type, enum mant); -static int man_node_append(struct man *, - struct man_node *); + enum roff_type, enum mant); +static void man_node_append(struct man *, struct man_node *); static void man_node_free(struct man_node *); static void man_node_unlink(struct man *, struct man_node *); static int man_ptext(struct man *, int, char *, int); static int man_pmacro(struct man *, int, char *, int); -static void man_free1(struct man *); -static void man_alloc1(struct man *); -static int man_descope(struct man *, int, int); const struct man_node * @@ -93,7 +94,8 @@ man_free(struct man *man) } struct man * -man_alloc(struct roff *roff, struct mparse *parse, int quick) +man_alloc(struct roff *roff, struct mparse *parse, + const char *defos, int quick) { struct man *p; @@ -101,6 +103,7 @@ man_alloc(struct roff *roff, struct mparse *parse, int man_hash_init(); p->parse = parse; + p->defos = defos; p->quick = quick; p->roff = roff; @@ -108,18 +111,18 @@ man_alloc(struct roff *roff, struct mparse *parse, int return(p); } -int +void man_endparse(struct man *man) { - return(man_macroend(man)); + man_macroend(man); } int man_parseln(struct man *man, int ln, char *buf, int offs) { - if (man->last->type != MAN_EQN || ln > man->last->line) + if (man->last->type != ROFFT_EQN || ln > man->last->line) man->flags |= MAN_NEWLINE; return (roff_getcontrol(man->roff, buf, &offs) ? @@ -133,16 +136,11 @@ man_free1(struct man *man) if (man->first) man_node_delete(man, man->first); - if (man->meta.title) - free(man->meta.title); - if (man->meta.source) - free(man->meta.source); - if (man->meta.date) - free(man->meta.date); - if (man->meta.vol) - free(man->meta.vol); - if (man->meta.msec) - free(man->meta.msec); + free(man->meta.title); + free(man->meta.source); + free(man->meta.date); + free(man->meta.vol); + free(man->meta.msec); } static void @@ -153,19 +151,19 @@ man_alloc1(struct man *man) man->flags = 0; man->last = mandoc_calloc(1, sizeof(struct man_node)); man->first = man->last; - man->last->type = MAN_ROOT; + man->last->type = ROFFT_ROOT; man->last->tok = MAN_MAX; man->next = MAN_NEXT_CHILD; } -static int +static void man_node_append(struct man *man, struct man_node *p) { assert(man->last); assert(man->first); - assert(MAN_ROOT != p->type); + assert(p->type != ROFFT_ROOT); switch (man->next) { case MAN_NEXT_SIBLING: @@ -186,20 +184,16 @@ man_node_append(struct man *man, struct man_node *p) p->parent->nchild++; switch (p->type) { - case MAN_BLOCK: + case ROFFT_BLOCK: if (p->tok == MAN_SH || p->tok == MAN_SS) man->flags &= ~MAN_LITERAL; break; - case MAN_HEAD: - assert(MAN_BLOCK == p->parent->type); + case ROFFT_HEAD: + assert(p->parent->type == ROFFT_BLOCK); p->parent->head = p; break; - case MAN_TAIL: - assert(MAN_BLOCK == p->parent->type); - p->parent->tail = p; - break; - case MAN_BODY: - assert(MAN_BLOCK == p->parent->type); + case ROFFT_BODY: + assert(p->parent->type == ROFFT_BLOCK); p->parent->body = p; break; default: @@ -209,22 +203,19 @@ man_node_append(struct man *man, struct man_node *p) man->last = p; switch (p->type) { - case MAN_TBL: + case ROFFT_TBL: /* FALLTHROUGH */ - case MAN_TEXT: - if ( ! man_valid_post(man)) - return(0); + case ROFFT_TEXT: + man_valid_post(man); break; default: break; } - - return(1); } static struct man_node * man_node_alloc(struct man *man, int line, int pos, - enum man_type type, enum mant tok) + enum roff_type type, enum mant tok) { struct man_node *p; @@ -234,85 +225,76 @@ man_node_alloc(struct man *man, int line, int pos, p->type = type; p->tok = tok; - if (MAN_NEWLINE & man->flags) + if (man->flags & MAN_NEWLINE) p->flags |= MAN_LINE; man->flags &= ~MAN_NEWLINE; return(p); } -int +void man_elem_alloc(struct man *man, int line, int pos, enum mant tok) { struct man_node *p; - p = man_node_alloc(man, line, pos, MAN_ELEM, tok); - if ( ! man_node_append(man, p)) - return(0); + p = man_node_alloc(man, line, pos, ROFFT_ELEM, tok); + man_node_append(man, p); man->next = MAN_NEXT_CHILD; - return(1); } -int -man_tail_alloc(struct man *man, int line, int pos, enum mant tok) -{ - struct man_node *p; - - p = man_node_alloc(man, line, pos, MAN_TAIL, tok); - if ( ! man_node_append(man, p)) - return(0); - man->next = MAN_NEXT_CHILD; - return(1); -} - -int +void man_head_alloc(struct man *man, int line, int pos, enum mant tok) { struct man_node *p; - p = man_node_alloc(man, line, pos, MAN_HEAD, tok); - if ( ! man_node_append(man, p)) - return(0); + p = man_node_alloc(man, line, pos, ROFFT_HEAD, tok); + man_node_append(man, p); man->next = MAN_NEXT_CHILD; - return(1); } -int +void man_body_alloc(struct man *man, int line, int pos, enum mant tok) { struct man_node *p; - p = man_node_alloc(man, line, pos, MAN_BODY, tok); - if ( ! man_node_append(man, p)) - return(0); + p = man_node_alloc(man, line, pos, ROFFT_BODY, tok); + man_node_append(man, p); man->next = MAN_NEXT_CHILD; - return(1); } -int +void man_block_alloc(struct man *man, int line, int pos, enum mant tok) { struct man_node *p; - p = man_node_alloc(man, line, pos, MAN_BLOCK, tok); - if ( ! man_node_append(man, p)) - return(0); + p = man_node_alloc(man, line, pos, ROFFT_BLOCK, tok); + man_node_append(man, p); man->next = MAN_NEXT_CHILD; - return(1); } -int +void man_word_alloc(struct man *man, int line, int pos, const char *word) { struct man_node *n; - n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX); + n = man_node_alloc(man, line, pos, ROFFT_TEXT, MAN_MAX); n->string = roff_strdup(man->roff, word); + man_node_append(man, n); + man->next = MAN_NEXT_SIBLING; +} - if ( ! man_node_append(man, n)) - return(0); +void +man_word_append(struct man *man, const char *word) +{ + struct man_node *n; + char *addstr, *newstr; + n = man->last; + addstr = roff_strdup(man->roff, word); + mandoc_asprintf(&newstr, "%s %s", n->string, addstr); + free(addstr); + free(n->string); + n->string = newstr; man->next = MAN_NEXT_SIBLING; - return(1); } /* @@ -323,8 +305,7 @@ static void man_node_free(struct man_node *p) { - if (p->string) - free(p->string); + free(p->string); free(p); } @@ -339,39 +320,34 @@ man_node_delete(struct man *man, struct man_node *p) man_node_free(p); } -int +void man_addeqn(struct man *man, const struct eqn *ep) { struct man_node *n; - n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX); + n = man_node_alloc(man, ep->ln, ep->pos, ROFFT_EQN, MAN_MAX); n->eqn = ep; if (ep->ln > man->last->line) n->flags |= MAN_LINE; - - if ( ! man_node_append(man, n)) - return(0); - + man_node_append(man, n); man->next = MAN_NEXT_SIBLING; - return(man_descope(man, ep->ln, ep->pos)); + man_descope(man, ep->ln, ep->pos); } -int +void man_addspan(struct man *man, const struct tbl_span *sp) { struct man_node *n; - n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX); + man_breakscope(man, MAN_MAX); + n = man_node_alloc(man, sp->line, 0, ROFFT_TBL, MAN_MAX); n->span = sp; - - if ( ! man_node_append(man, n)) - return(0); - + man_node_append(man, n); man->next = MAN_NEXT_SIBLING; - return(man_descope(man, sp->line, 0)); + man_descope(man, sp->line, 0); } -static int +static void man_descope(struct man *man, int line, int offs) { /* @@ -380,19 +356,15 @@ man_descope(struct man *man, int line, int offs) * out the block scope (also if applicable). */ - if (MAN_ELINE & man->flags) { + if (man->flags & MAN_ELINE) { man->flags &= ~MAN_ELINE; - if ( ! man_unscope(man, man->last->parent)) - return(0); + man_unscope(man, man->last->parent); } - - if ( ! (MAN_BLINE & man->flags)) - return(1); + if ( ! (man->flags & MAN_BLINE)) + return; man->flags &= ~MAN_BLINE; - - if ( ! man_unscope(man, man->last->parent)) - return(0); - return(man_body_alloc(man, line, offs, man->last->tok)); + man_unscope(man, man->last->parent); + man_body_alloc(man, line, offs, man->last->tok); } static int @@ -402,13 +374,13 @@ man_ptext(struct man *man, int line, char *buf, int of /* Literal free-form text whitespace is preserved. */ - if (MAN_LITERAL & man->flags) { - if ( ! man_word_alloc(man, line, offs, buf + offs)) - return(0); - return(man_descope(man, line, offs)); + if (man->flags & MAN_LITERAL) { + man_word_alloc(man, line, offs, buf + offs); + man_descope(man, line, offs); + return(1); } - for (i = offs; ' ' == buf[i]; i++) + for (i = offs; buf[i] == ' '; i++) /* Skip leading whitespace. */ ; /* @@ -416,12 +388,11 @@ man_ptext(struct man *man, int line, char *buf, int of * but add a single vertical space elsewhere. */ - if ('\0' == buf[i]) { + if (buf[i] == '\0') { /* Allocate a blank entry. */ - if (MAN_SH != man->last->tok && - MAN_SS != man->last->tok) { - if ( ! man_elem_alloc(man, line, offs, MAN_sp)) - return(0); + if (man->last->tok != MAN_SH && + man->last->tok != MAN_SS) { + man_elem_alloc(man, line, offs, MAN_sp); man->next = MAN_NEXT_SIBLING; } return(1); @@ -448,10 +419,8 @@ man_ptext(struct man *man, int line, char *buf, int of buf[i] = '\0'; } + man_word_alloc(man, line, offs, buf + offs); - if ( ! man_word_alloc(man, line, offs, buf + offs)) - return(0); - /* * End-of-sentence check. If the last character is an unescaped * EOS character, then flag the node as being the end of a @@ -462,124 +431,88 @@ man_ptext(struct man *man, int line, char *buf, int of if (mandoc_eos(buf, (size_t)i)) man->last->flags |= MAN_EOS; - return(man_descope(man, line, offs)); + man_descope(man, line, offs); + return(1); } static int man_pmacro(struct man *man, int ln, char *buf, int offs) { - char mac[5]; struct man_node *n; + const char *cp; enum mant tok; int i, ppos; int bline; + char mac[5]; ppos = offs; /* * Copy the first word into a nil-terminated buffer. - * Stop copying when a tab, space, or eoln is encountered. + * Stop when a space, tab, escape, or eoln is encountered. */ i = 0; - while (i < 4 && '\0' != buf[offs] && ' ' != buf[offs] && - '\t' != buf[offs]) + while (i < 4 && strchr(" \t\\", buf[offs]) == NULL) mac[i++] = buf[offs++]; mac[i] = '\0'; tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX; - if (MAN_MAX == tok) { + if (tok == MAN_MAX) { mandoc_msg(MANDOCERR_MACRO, man->parse, ln, ppos, buf + ppos - 1); return(1); } - /* The macro is sane. Jump to the next word. */ + /* Skip a leading escape sequence or tab. */ - while (buf[offs] && ' ' == buf[offs]) + switch (buf[offs]) { + case '\\': + cp = buf + offs + 1; + mandoc_escape(&cp, NULL, NULL); + offs = cp - buf; + break; + case '\t': offs++; + break; + default: + break; + } + /* Jump to the next non-whitespace word. */ + + while (buf[offs] && buf[offs] == ' ') + offs++; + /* * Trailing whitespace. Note that tabs are allowed to be passed * into the parser as "text", so we only warn about spaces here. */ - if ('\0' == buf[offs] && ' ' == buf[offs - 1]) + if (buf[offs] == '\0' && buf[offs - 1] == ' ') mandoc_msg(MANDOCERR_SPACE_EOL, man->parse, ln, offs - 1, NULL); /* - * Remove prior ELINE macro, as it's being clobbered by a new - * macro. Note that NSCOPED macros do not close out ELINE - * macros---they don't print text---so we let those slip by. + * Some macros break next-line scopes; otherwise, remember + * whether we are in next-line scope for a block head. */ - if ( ! (MAN_NSCOPED & man_macros[tok].flags) && - man->flags & MAN_ELINE) { - n = man->last; - assert(MAN_TEXT != n->type); - - /* Remove repeated NSCOPED macros causing ELINE. */ - - if (MAN_NSCOPED & man_macros[n->tok].flags) - n = n->parent; - - mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, - n->pos, "%s breaks %s", man_macronames[tok], - man_macronames[n->tok]); - - man_node_delete(man, n); - man->flags &= ~MAN_ELINE; - } - - /* - * Remove prior BLINE macro that is being clobbered. - */ - if ((man->flags & MAN_BLINE) && - (MAN_BSCOPE & man_macros[tok].flags)) { - n = man->last; - - /* Might be a text node like 8 in - * .TP 8 - * .SH foo - */ - if (MAN_TEXT == n->type) - n = n->parent; - - /* Remove element that didn't end BLINE, if any. */ - if ( ! (MAN_BSCOPE & man_macros[n->tok].flags)) - n = n->parent; - - assert(MAN_HEAD == n->type); - n = n->parent; - assert(MAN_BLOCK == n->type); - assert(MAN_SCOPED & man_macros[n->tok].flags); - - mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, - n->pos, "%s breaks %s", man_macronames[tok], - man_macronames[n->tok]); - - man_node_delete(man, n); - man->flags &= ~MAN_BLINE; - } - - /* Remember whether we are in next-line scope for a block head. */ - + man_breakscope(man, tok); bline = man->flags & MAN_BLINE; /* Call to handler... */ assert(man_macros[tok].fp); - if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf)) - return(0); + (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf); /* In quick mode (for mandocdb), abort after the NAME section. */ - if (man->quick && MAN_SH == tok) { + if (man->quick && tok == MAN_SH) { n = man->last; - if (MAN_BODY == n->type && + if (n->type == ROFFT_BODY && strcmp(n->prev->child->string, "NAME")) return(2); } @@ -594,14 +527,70 @@ man_pmacro(struct man *man, int ln, char *buf, int off man_macros[tok].flags & MAN_NSCOPED) return(1); - assert(MAN_BLINE & man->flags); + assert(man->flags & MAN_BLINE); man->flags &= ~MAN_BLINE; - if ( ! man_unscope(man, man->last->parent)) - return(0); - return(man_body_alloc(man, ln, ppos, man->last->tok)); + man_unscope(man, man->last->parent); + man_body_alloc(man, ln, ppos, man->last->tok); + return(1); } +void +man_breakscope(struct man *man, enum mant tok) +{ + struct man_node *n; + + /* + * An element next line scope is open, + * and the new macro is not allowed inside elements. + * Delete the element that is being broken. + */ + + if (man->flags & MAN_ELINE && (tok == MAN_MAX || + ! (man_macros[tok].flags & MAN_NSCOPED))) { + n = man->last; + assert(n->type != ROFFT_TEXT); + if (man_macros[n->tok].flags & MAN_NSCOPED) + n = n->parent; + + mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, + n->line, n->pos, "%s breaks %s", + tok == MAN_MAX ? "TS" : man_macronames[tok], + man_macronames[n->tok]); + + man_node_delete(man, n); + man->flags &= ~MAN_ELINE; + } + + /* + * A block header next line scope is open, + * and the new macro is not allowed inside block headers. + * Delete the block that is being broken. + */ + + if (man->flags & MAN_BLINE && (tok == MAN_MAX || + man_macros[tok].flags & MAN_BSCOPE)) { + n = man->last; + if (n->type == ROFFT_TEXT) + n = n->parent; + if ( ! (man_macros[n->tok].flags & MAN_BSCOPE)) + n = n->parent; + + assert(n->type == ROFFT_HEAD); + n = n->parent; + assert(n->type == ROFFT_BLOCK); + assert(man_macros[n->tok].flags & MAN_SCOPED); + + mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, + n->line, n->pos, "%s breaks %s", + tok == MAN_MAX ? "TS" : man_macronames[tok], + man_macronames[n->tok]); + + man_node_delete(man, n); + man->flags &= ~MAN_BLINE; + } +} + /* * Unlink a node from its context. If "man" is provided, the last parse * point will also be adjusted accordingly. @@ -657,7 +646,7 @@ man_deroff(char **dest, const struct man_node *n) char *cp; size_t sz; - if (MAN_TEXT != n->type) { + if (n->type != ROFFT_TEXT) { for (n = n->child; n; n = n->next) man_deroff(dest, n); return;