From d93f85619078f67c40a0661b3cc670c70ddd866e Mon Sep 17 00:00:00 2001 From: schwarze Date: Sat, 8 Jul 2017 17:52:42 +0000 Subject: [PATCH] Simplify by creating struct roff_node syntax tree nodes for tbl(7) right from roff_parseln() rather than delegating to read.c, similar to what i just did for eqn(7). The interface function roff_span() becomes obsolete and is deleted, the former interface function roff_addtbl() becomes static, the interface functions tbl_read() and tbl_cdata() become void, and minus twelve linus of code. No functional change. --- usr.bin/mandoc/libmandoc.h | 8 ++--- usr.bin/mandoc/libroff.h | 10 +++--- usr.bin/mandoc/read.c | 17 ++------- usr.bin/mandoc/roff.c | 74 +++++++++++++++++++------------------- usr.bin/mandoc/roff_int.h | 3 +- usr.bin/mandoc/tbl.c | 21 +++++------ usr.bin/mandoc/tbl_data.c | 10 +++--- 7 files changed, 59 insertions(+), 84 deletions(-) diff --git a/usr.bin/mandoc/libmandoc.h b/usr.bin/mandoc/libmandoc.h index b526498a9d7..045a8dee20f 100644 --- a/usr.bin/mandoc/libmandoc.h +++ b/usr.bin/mandoc/libmandoc.h @@ -1,7 +1,7 @@ -/* $OpenBSD: libmandoc.h,v 1.54 2017/07/08 14:51:01 schwarze Exp $ */ +/* $OpenBSD: libmandoc.h,v 1.55 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2012 Kristaps Dzonsons - * Copyright (c) 2013, 2014, 2015 Ingo Schwarze + * Copyright (c) 2013, 2014, 2015, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,7 +23,6 @@ enum rofferr { ROFF_REPARSE, /* re-run main parser on the result */ ROFF_SO, /* include another file */ ROFF_IGN, /* ignore current line */ - ROFF_TBL, /* a table row was successfully parsed */ }; struct buf { @@ -33,7 +32,6 @@ struct buf { struct mparse; -struct tbl_span; struct roff; struct roff_man; @@ -73,5 +71,3 @@ char *roff_strdup(const struct roff *, const char *); int roff_getcontrol(const struct roff *, const char *, int *); int roff_getformat(const struct roff *); - -const struct tbl_span *roff_span(const struct roff *); diff --git a/usr.bin/mandoc/libroff.h b/usr.bin/mandoc/libroff.h index 532333a438d..2f78abd960e 100644 --- a/usr.bin/mandoc/libroff.h +++ b/usr.bin/mandoc/libroff.h @@ -1,7 +1,7 @@ -/* $OpenBSD: libroff.h,v 1.19 2017/07/08 14:51:01 schwarze Exp $ */ +/* $OpenBSD: libroff.h,v 1.20 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2014, 2015 Ingo Schwarze + * Copyright (c) 2014, 2015, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -65,13 +65,13 @@ struct tbl_node *tbl_alloc(int, int, struct mparse *); void tbl_restart(int, int, struct tbl_node *); void tbl_free(struct tbl_node *); void tbl_reset(struct tbl_node *); -enum rofferr tbl_read(struct tbl_node *, int, const char *, int); +void tbl_read(struct tbl_node *, int, const char *, int); void tbl_option(struct tbl_node *, int, const char *, int *); void tbl_layout(struct tbl_node *, int, const char *, int); void tbl_data(struct tbl_node *, int, const char *, int); -int tbl_cdata(struct tbl_node *, int, const char *, int); +void tbl_cdata(struct tbl_node *, int, const char *, int); const struct tbl_span *tbl_span(struct tbl_node *); -int tbl_end(struct tbl_node **); +int tbl_end(struct tbl_node *); struct eqn_node *eqn_alloc(struct mparse *); void eqn_box_free(struct eqn_box *); void eqn_free(struct eqn_node *); diff --git a/usr.bin/mandoc/read.c b/usr.bin/mandoc/read.c index 3636e1c5658..813e8f58815 100644 --- a/usr.bin/mandoc/read.c +++ b/usr.bin/mandoc/read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read.c,v 1.162 2017/07/08 14:51:01 schwarze Exp $ */ +/* $OpenBSD: read.c,v 1.163 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -38,7 +38,6 @@ #include "mdoc.h" #include "man.h" #include "libmandoc.h" -#include "roff_int.h" #define REPARSE_LIMIT 1000 @@ -339,7 +338,6 @@ choose_parser(struct mparse *curp) static int mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start) { - const struct tbl_span *span; struct buf ln; const char *save_file; char *cp; @@ -530,18 +528,7 @@ rerun: if (curp->man->macroset == MACROSET_NONE) choose_parser(curp); - /* - * Lastly, push down into the parsers themselves. - * If libroff returns ROFF_TBL, then add it to the - * currently open parse. Since we only get here if - * there does exist data (see tbl_data.c), we're - * guaranteed that something's been allocated. - */ - - if (rr == ROFF_TBL) - while ((span = roff_span(curp->roff)) != NULL) - roff_addtbl(curp->man, span); - else if ((curp->man->macroset == MACROSET_MDOC ? + if ((curp->man->macroset == MACROSET_MDOC ? mdoc_parseln(curp->man, curp->line, ln.buf, of) : man_parseln(curp->man, curp->line, ln.buf, of)) == 2) break; diff --git a/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c index 67016b3c992..cfbbcfce88c 100644 --- a/usr.bin/mandoc/roff.c +++ b/usr.bin/mandoc/roff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roff.c,v 1.192 2017/07/08 15:28:05 schwarze Exp $ */ +/* $OpenBSD: roff.c,v 1.193 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons * Copyright (c) 2010-2015, 2017 Ingo Schwarze @@ -151,6 +151,7 @@ static void roffnode_cleanscope(struct roff *); static void roffnode_pop(struct roff *); static void roffnode_push(struct roff *, enum roff_tok, const char *, int, int); +static void roff_addtbl(struct roff_man *, struct tbl_node *); static enum rofferr roff_als(ROFF_ARGS); static enum rofferr roff_block(ROFF_ARGS); static enum rofferr roff_block_text(ROFF_ARGS); @@ -977,18 +978,21 @@ roff_body_alloc(struct roff_man *man, int line, int pos, int tok) return n; } -void -roff_addtbl(struct roff_man *man, const struct tbl_span *tbl) +static void +roff_addtbl(struct roff_man *man, struct tbl_node *tbl) { struct roff_node *n; + const struct tbl_span *span; if (man->macroset == MACROSET_MAN) man_breakscope(man, ROFF_TS); - n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE); - n->span = tbl; - roff_node_append(man, n); - n->flags |= NODE_VALID | NODE_ENDED; - man->next = ROFF_NEXT_SIBLING; + while ((span = tbl_span(tbl)) != NULL) { + n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE); + n->span = span; + roff_node_append(man, n); + n->flags |= NODE_VALID | NODE_ENDED; + man->next = ROFF_NEXT_SIBLING; + } } void @@ -1499,8 +1503,11 @@ roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs) eqn_read(r->eqn, buf->buf + ppos); return ROFF_IGN; } - if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0')) - return tbl_read(r->tbl, ln, buf->buf, ppos); + if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) { + tbl_read(r->tbl, ln, buf->buf, ppos); + roff_addtbl(r->man, r->tbl); + return ROFF_IGN; + } if ( ! ctl) return roff_parsetext(r, buf, pos, offs); @@ -1541,7 +1548,9 @@ roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs) pos++; while (buf->buf[pos] == ' ') pos++; - return tbl_read(r->tbl, ln, buf->buf, pos); + tbl_read(r->tbl, ln, buf->buf, pos); + roff_addtbl(r->man, r->tbl); + return ROFF_IGN; } /* For now, let high level macros abort .ce mode. */ @@ -1570,23 +1579,23 @@ roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs) void roff_endparse(struct roff *r) { - - if (r->last) + if (r->last != NULL) mandoc_msg(MANDOCERR_BLK_NOEND, r->parse, r->last->line, r->last->col, roff_name[r->last->tok]); - if (r->eqn) { + if (r->eqn != NULL) { mandoc_msg(MANDOCERR_BLK_NOEND, r->parse, r->eqn->node->line, r->eqn->node->pos, "EQ"); eqn_parse(r->eqn); r->eqn = NULL; } - if (r->tbl) { + if (r->tbl != NULL) { mandoc_msg(MANDOCERR_BLK_NOEND, r->parse, r->tbl->line, r->tbl->pos, "TS"); - tbl_end(&r->tbl); + tbl_end(r->tbl); + r->tbl = NULL; } } @@ -2768,16 +2777,19 @@ roff_Dd(ROFF_ARGS) static enum rofferr roff_TE(ROFF_ARGS) { - - if (NULL == r->tbl) + if (r->tbl == NULL) { mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "TE"); - else if ( ! tbl_end(&r->tbl)) { + return ROFF_IGN; + } + if (tbl_end(r->tbl) == 0) { + r->tbl = NULL; free(buf->buf); buf->buf = mandoc_strdup(".sp"); buf->sz = 4; return ROFF_REPARSE; } + r->tbl = NULL; return ROFF_IGN; } @@ -2905,22 +2917,17 @@ roff_EN(ROFF_ARGS) static enum rofferr roff_TS(ROFF_ARGS) { - struct tbl_node *tbl; - - if (r->tbl) { + if (r->tbl != NULL) { mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse, ln, ppos, "TS breaks TS"); - tbl_end(&r->tbl); + tbl_end(r->tbl); } - - tbl = tbl_alloc(ppos, ln, r->parse); - + r->tbl = tbl_alloc(ppos, ln, r->parse); if (r->last_tbl) - r->last_tbl->next = tbl; + r->last_tbl->next = r->tbl; else - r->first_tbl = r->last_tbl = tbl; - - r->tbl = r->last_tbl = tbl; + r->first_tbl = r->tbl; + r->last_tbl = r->tbl; return ROFF_IGN; } @@ -3599,13 +3606,6 @@ roff_freestr(struct roffkv *r) /* --- accessors and utility functions ------------------------------------ */ -const struct tbl_span * -roff_span(const struct roff *r) -{ - - return r->tbl ? tbl_span(r->tbl) : NULL; -} - /* * Duplicate an input string, making the appropriate character * conversations (as stipulated by `tr') along the way. diff --git a/usr.bin/mandoc/roff_int.h b/usr.bin/mandoc/roff_int.h index e8b7592fde7..14bf92a9078 100644 --- a/usr.bin/mandoc/roff_int.h +++ b/usr.bin/mandoc/roff_int.h @@ -1,4 +1,4 @@ -/* $OpenBSD: roff_int.h,v 1.8 2017/07/08 14:51:01 schwarze Exp $ */ +/* $OpenBSD: roff_int.h,v 1.9 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -25,7 +25,6 @@ void roff_elem_alloc(struct roff_man *, int, int, int); struct roff_node *roff_block_alloc(struct roff_man *, int, int, int); struct roff_node *roff_head_alloc(struct roff_man *, int, int, int); struct roff_node *roff_body_alloc(struct roff_man *, int, int, int); -void roff_addtbl(struct roff_man *, const struct tbl_span *); void roff_node_unlink(struct roff_man *, struct roff_node *); void roff_node_free(struct roff_node *); void roff_node_delete(struct roff_man *, struct roff_node *); diff --git a/usr.bin/mandoc/tbl.c b/usr.bin/mandoc/tbl.c index 63e91b77a50..d8bca8dd4af 100644 --- a/usr.bin/mandoc/tbl.c +++ b/usr.bin/mandoc/tbl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl.c,v 1.22 2017/06/08 18:11:15 schwarze Exp $ */ +/* $OpenBSD: tbl.c,v 1.23 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011, 2015 Ingo Schwarze @@ -29,7 +29,7 @@ #include "libroff.h" -enum rofferr +void tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos) { const char *cp; @@ -64,7 +64,7 @@ tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos) if (*cp == ';') { tbl_option(tbl, ln, p, &pos); if (p[pos] == '\0') - return ROFF_IGN; + return; } } @@ -73,15 +73,14 @@ tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos) switch (tbl->part) { case TBL_PART_LAYOUT: tbl_layout(tbl, ln, p, pos); - return ROFF_IGN; + break; case TBL_PART_CDATA: - return tbl_cdata(tbl, ln, p, pos) ? ROFF_TBL : ROFF_IGN; + tbl_cdata(tbl, ln, p, pos); + break; default: + tbl_data(tbl, ln, p, pos); break; } - - tbl_data(tbl, ln, p, pos); - return ROFF_TBL; } struct tbl_node * @@ -158,14 +157,10 @@ tbl_span(struct tbl_node *tbl) } int -tbl_end(struct tbl_node **tblp) +tbl_end(struct tbl_node *tbl) { - struct tbl_node *tbl; struct tbl_span *sp; - tbl = *tblp; - *tblp = NULL; - if (tbl->part == TBL_PART_CDATA) mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse, tbl->line, tbl->pos, "TE"); diff --git a/usr.bin/mandoc/tbl_data.c b/usr.bin/mandoc/tbl_data.c index 7406b0c6034..55d057a0130 100644 --- a/usr.bin/mandoc/tbl_data.c +++ b/usr.bin/mandoc/tbl_data.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl_data.c,v 1.31 2017/07/04 20:59:17 schwarze Exp $ */ +/* $OpenBSD: tbl_data.c,v 1.32 2017/07/08 17:52:42 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011, 2015, 2017 Ingo Schwarze @@ -126,7 +126,7 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, tbl->parse, ln, sv, dat->string); } -int +void tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) { struct tbl_dat *dat; @@ -141,10 +141,10 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) pos++; while (p[pos] != '\0') getdata(tbl, tbl->last_span, ln, p, &pos); - return 1; + return; } else if (p[pos] == '\0') { tbl->part = TBL_PART_DATA; - return 1; + return; } /* Fallthrough: T} is part of a word. */ @@ -164,8 +164,6 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) if (dat->layout->pos == TBL_CELL_DOWN) mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse, ln, pos, dat->string); - - return 0; } static struct tbl_span * -- 2.20.1