diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2014-11-28 05:51:30 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2014-11-28 05:51:30 +0000 |
commit | 0c7d90a5a4779732a78b2e26b6e14740da4238b2 (patch) | |
tree | 5a60f360b24f6b0e0a3ae8ffbd118306e303d855 /usr.bin/mandoc/man.c | |
parent | 2c0d86f1edee3da18b5a7c30c605033bf46df75c (diff) |
Simplify by making many functions in the man(7) parser void,
and some cleanup; no functional change, minus 70 lines.
Diffstat (limited to 'usr.bin/mandoc/man.c')
-rw-r--r-- | usr.bin/mandoc/man.c | 188 |
1 files changed, 70 insertions, 118 deletions
diff --git a/usr.bin/mandoc/man.c b/usr.bin/mandoc/man.c index 0b0acdc4380..c5b0f0e22de 100644 --- a/usr.bin/mandoc/man.c +++ b/usr.bin/mandoc/man.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man.c,v 1.91 2014/11/19 03:07:43 schwarze Exp $ */ +/* $OpenBSD: man.c,v 1.92 2014/11/28 05:51:29 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org> @@ -48,8 +48,7 @@ const char * const *man_macronames = __man_macronames; 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 *); +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 *); @@ -57,7 +56,7 @@ 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); +static void man_descope(struct man *, int, int); const struct man_node * @@ -110,7 +109,8 @@ int man_endparse(struct man *man) { - return(man_macroend(man)); + man_macroend(man); + return(1); } int @@ -131,16 +131,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 @@ -157,13 +152,13 @@ man_alloc1(struct man *man) } -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 != MAN_ROOT); switch (man->next) { case MAN_NEXT_SIBLING: @@ -189,15 +184,11 @@ man_node_append(struct man *man, struct man_node *p) man->flags &= ~MAN_LITERAL; break; case MAN_HEAD: - assert(MAN_BLOCK == p->parent->type); + assert(p->parent->type == MAN_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); + assert(p->parent->type == MAN_BLOCK); p->parent->body = p; break; default: @@ -210,14 +201,11 @@ man_node_append(struct man *man, struct man_node *p) case MAN_TBL: /* FALLTHROUGH */ case MAN_TEXT: - if ( ! man_valid_post(man)) - return(0); + man_valid_post(man); break; default: break; } - - return(1); } static struct man_node * @@ -232,85 +220,61 @@ 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); - 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_node_append(man, p); 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); + 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); + 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); + 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->string = roff_strdup(man->roff, word); - - if ( ! man_node_append(man, n)) - return(0); - + man_node_append(man, n); man->next = MAN_NEXT_SIBLING; - return(1); } void @@ -336,8 +300,7 @@ static void man_node_free(struct man_node *p) { - if (p->string) - free(p->string); + free(p->string); free(p); } @@ -361,12 +324,10 @@ man_addeqn(struct man *man, const struct eqn *ep) 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); + return(1); } int @@ -376,15 +337,13 @@ man_addspan(struct man *man, const struct tbl_span *sp) n = man_node_alloc(man, sp->line, 0, MAN_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); + return(1); } -static int +static void man_descope(struct man *man, int line, int offs) { /* @@ -393,19 +352,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 @@ -415,13 +370,13 @@ man_ptext(struct man *man, int line, char *buf, int offs) /* 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. */ ; /* @@ -429,12 +384,11 @@ man_ptext(struct man *man, int line, char *buf, int offs) * 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); @@ -461,9 +415,7 @@ man_ptext(struct man *man, int line, char *buf, int offs) buf[i] = '\0'; } - - if ( ! man_word_alloc(man, line, offs, buf + offs)) - return(0); + man_word_alloc(man, line, offs, buf + offs); /* * End-of-sentence check. If the last character is an unescaped @@ -475,7 +427,8 @@ man_ptext(struct man *man, int line, char *buf, int offs) 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 @@ -526,7 +479,7 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) /* Jump to the next non-whitespace word. */ - while (buf[offs] && ' ' == buf[offs]) + while (buf[offs] && buf[offs] == ' ') offs++; /* @@ -534,7 +487,7 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) * 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); @@ -544,14 +497,14 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) * macros---they don't print text---so we let those slip by. */ - if ( ! (MAN_NSCOPED & man_macros[tok].flags) && + if ( ! (man_macros[tok].flags & MAN_NSCOPED) && 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) + if (man_macros[n->tok].flags & MAN_NSCOPED) n = n->parent; mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, @@ -566,24 +519,24 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) * Remove prior BLINE macro that is being clobbered. */ if ((man->flags & MAN_BLINE) && - (MAN_BSCOPE & man_macros[tok].flags)) { + (man_macros[tok].flags & MAN_BSCOPE)) { n = man->last; /* Might be a text node like 8 in * .TP 8 * .SH foo */ - if (MAN_TEXT == n->type) + if (n->type == MAN_TEXT) n = n->parent; /* Remove element that didn't end BLINE, if any. */ - if ( ! (MAN_BSCOPE & man_macros[n->tok].flags)) + if ( ! (man_macros[n->tok].flags & MAN_BSCOPE)) n = n->parent; - assert(MAN_HEAD == n->type); + assert(n->type == MAN_HEAD); n = n->parent; - assert(MAN_BLOCK == n->type); - assert(MAN_SCOPED & man_macros[n->tok].flags); + assert(n->type == MAN_BLOCK); + assert(man_macros[n->tok].flags & MAN_SCOPED); mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line, n->pos, "%s breaks %s", man_macronames[tok], @@ -600,14 +553,13 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) /* 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 == MAN_BODY && strcmp(n->prev->child->string, "NAME")) return(2); } @@ -622,12 +574,12 @@ man_pmacro(struct man *man, int ln, char *buf, int offs) 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); } /* @@ -685,7 +637,7 @@ man_deroff(char **dest, const struct man_node *n) char *cp; size_t sz; - if (MAN_TEXT != n->type) { + if (n->type != MAN_TEXT) { for (n = n->child; n; n = n->next) man_deroff(dest, n); return; |