* mandoc: Cleanup, no functional change: Move tbl(7)-specific parser
@ 2018-12-13 2:06 schwarze
0 siblings, 0 replies; only message in thread
From: schwarze @ 2018-12-13 2:06 UTC (permalink / raw)
To: source
Log Message:
-----------
Cleanup, no functional change:
Move tbl(7)-specific parser internals out of libroff.h.
Move some tbl(7)-internal processing from roff.c to tbl.c.
Modified Files:
--------------
mandoc:
Makefile
libroff.h
mandoc_headers.3
roff.c
roff.h
tbl.3
tbl.c
tbl_data.c
tbl_layout.c
tbl_opts.c
Added Files:
-----------
mandoc:
tbl_int.h
tbl_parse.h
Revision Data
-------------
--- /dev/null
+++ tbl_int.h
@@ -0,0 +1,48 @@
+/* $Id: tbl_int.h,v 1.1 2018/12/13 02:06:07 schwarze Exp $ */
+/*
+ * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011,2013,2015,2017,2018 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * 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 AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * 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
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Internal interfaces of the tbl(7) parser.
+ * For use inside the tbl(7) parser only.
+ */
+
+enum tbl_part {
+ TBL_PART_OPTS, /* In the first line, ends with semicolon. */
+ TBL_PART_LAYOUT, /* In the layout section, ends with full stop. */
+ TBL_PART_DATA, /* In the data section, ends with TE. */
+ TBL_PART_CDATA /* In a T{ block, ends with T} */
+};
+
+struct tbl_node {
+ struct tbl_opts opts; /* Options for the whole table. */
+ struct mparse *parse; /* For error reporting. */
+ struct tbl_node *next; /* Next table. */
+ struct tbl_row *first_row; /* First layout row. */
+ struct tbl_row *last_row; /* Last layout row. */
+ struct tbl_span *first_span; /* First data row. */
+ struct tbl_span *current_span; /* Data row being parsed. */
+ struct tbl_span *last_span; /* Last data row. */
+ int line; /* Line number in input file. */
+ int pos; /* Column number in input file. */
+ enum tbl_part part; /* Table section being parsed. */
+};
+
+
+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);
+void tbl_cdata(struct tbl_node *, int, const char *, int);
+void tbl_reset(struct tbl_node *);
Index: roff.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/roff.h,v
retrieving revision 1.61
retrieving revision 1.62
diff -Lroff.h -Lroff.h -u -p -r1.61 -r1.62
--- roff.h
+++ roff.h
@@ -502,7 +502,7 @@ struct roff_node {
struct mdoc_arg *args; /* BLOCK/ELEM */
union mdoc_data *norm; /* Normalized arguments. */
char *string; /* TEXT */
- const struct tbl_span *span; /* TBL */
+ struct tbl_span *span; /* TBL */
struct eqn_box *eqn; /* EQN */
int line; /* Input file line number. */
int pos; /* Input file column number. */
--- /dev/null
+++ tbl_parse.h
@@ -0,0 +1,31 @@
+/* $Id: tbl_parse.h,v 1.1 2018/12/13 02:06:07 schwarze Exp $ */
+/*
+ * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011, 2017 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * 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 AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * 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
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * External interface of the tbl(7) parser.
+ * For use in the roff(7) and tbl(7) parsers only.
+ */
+
+struct mparse;
+struct tbl_node;
+struct tbl_span;
+
+struct tbl_node *tbl_alloc(int, int, struct mparse *, struct tbl_node *);
+int tbl_end(struct tbl_node *, int);
+void tbl_free(struct tbl_node *);
+void tbl_read(struct tbl_node *, int, const char *, int);
+void tbl_restart(int, int, struct tbl_node *);
+struct tbl_span *tbl_span(struct tbl_node *);
Index: Makefile
===================================================================
RCS file: /home/cvs/mandoc/mandoc/Makefile,v
retrieving revision 1.521
retrieving revision 1.522
diff -LMakefile -LMakefile -u -p -r1.521 -r1.522
--- Makefile
+++ Makefile
@@ -203,6 +203,8 @@ DISTFILES = INSTALL \
tbl.3 \
tbl.7 \
tbl.h \
+ tbl_int.h \
+ tbl_parse.h \
term.h \
$(SRCS) \
$(TESTSRCS)
Index: mandoc_headers.3
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc_headers.3,v
retrieving revision 1.20
retrieving revision 1.21
diff -Lmandoc_headers.3 -Lmandoc_headers.3 -u -p -r1.20 -r1.21
--- mandoc_headers.3
+++ mandoc_headers.3
@@ -257,7 +257,7 @@ or
.Pa libroff.h .
.El
.Ss Parser internals
-The following headers require inclusion of a parser interface header
+Most of the following headers require inclusion of a parser interface header
before they can be included.
All parser interface headers should precede all parser internal headers.
When any parser internal headers are included, the same file should
@@ -388,20 +388,14 @@ for
and
.Qq Pa mandoc.h
for
-.Vt struct tbl_*
-and
.Vt struct eqn_box .
.Pp
Provides
-.Vt enum tbl_part ,
-.Vt struct tbl_node ,
.Vt struct eqn_def ,
.Vt struct eqn_node ,
and many functions internal to the
-.Xr tbl 7
-and
.Xr eqn 7
-parsers.
+parser.
.Pp
Uses the opaque type
.Vt struct mparse
@@ -414,6 +408,65 @@ When this header is included, the same f
.Pa libman.h ,
or
.Pa libmdoc.h .
+.It Qq Pa tbl_parse.h
+External interface of the
+.Xr tbl 7
+parser, for use in the
+.Xr roff 7
+and
+.Xr tbl 7
+parsers only.
+.Pp
+Provides the functions documented in
+.Xr tbl 3 .
+.Pp
+Uses the opaque type
+.Vt struct mparse
+from
+.Pa read.c .
+Uses the types
+.Vt struct tbl_span
+from
+.Pa tbl.h
+and
+.Vt struct tbl_node
+from
+.Pa tbl_int.h
+as opaque types for function prototypes.
+.Pp
+When this header is included, the same file should not include
+internals of a different parser.
+.It Qq Pa tbl_int.h
+Internal interfaces of the
+.Xr tbl 7
+parser, for use inside the
+.Xr tbl 7
+parser only.
+.Pp
+Requires
+.Qq Pa tbl.h
+for
+.Vt struct tbl_opts .
+.Pp
+Provides
+.Vt enum tbl_part ,
+.Vt struct tbl_node ,
+and the functions
+.Fn tbl_option ,
+.Fn tbl_layout ,
+.Fn tbl_data ,
+.Fn tbl_cdata ,
+and
+.Fn tbl_reset .
+.Pp
+Uses a pointer to the opaque type
+.Vt struct mparse
+from
+.Pa read.c
+as an opaque struct member.
+.Pp
+When this header is included, the same file should not include
+interfaces of different parsers.
.El
.Ss Formatter interface
These headers should be included after any parser interface headers.
Index: roff.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/roff.c,v
retrieving revision 1.345
retrieving revision 1.346
diff -Lroff.c -Lroff.c -u -p -r1.345 -r1.346
--- roff.c
+++ roff.c
@@ -32,10 +32,10 @@
#include "mandoc_ohash.h"
#include "mandoc.h"
#include "roff.h"
-#include "tbl.h"
#include "libmandoc.h"
#include "roff_int.h"
#include "libroff.h"
+#include "tbl_parse.h"
/* Maximum number of string expansions per line, to break infinite loops. */
#define EXPAND_LIMIT 1000
@@ -168,7 +168,7 @@ static int roffnode_cleanscope(struct
static int 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 void roff_addtbl(struct roff_man *, int, struct tbl_node *);
static int roff_als(ROFF_ARGS);
static int roff_block(ROFF_ARGS);
static int roff_block_text(ROFF_ARGS);
@@ -718,13 +718,9 @@ roffnode_push(struct roff *r, enum roff_
static void
roff_free1(struct roff *r)
{
- struct tbl_node *tbl;
int i;
- while (NULL != (tbl = r->first_tbl)) {
- r->first_tbl = tbl->next;
- tbl_free(tbl);
- }
+ tbl_free(r->first_tbl);
r->first_tbl = r->last_tbl = r->tbl = NULL;
if (r->last_eqn != NULL)
@@ -1014,15 +1010,15 @@ roff_body_alloc(struct roff_man *man, in
}
static void
-roff_addtbl(struct roff_man *man, struct tbl_node *tbl)
+roff_addtbl(struct roff_man *man, int line, struct tbl_node *tbl)
{
struct roff_node *n;
- const struct tbl_span *span;
+ struct tbl_span *span;
if (man->macroset == MACROSET_MAN)
man_breakscope(man, ROFF_TS);
while ((span = tbl_span(tbl)) != NULL) {
- n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
+ n = roff_node_alloc(man, line, 0, ROFFT_TBL, TOKEN_NONE);
n->span = span;
roff_node_append(man, n);
n->flags |= NODE_VALID | NODE_ENDED;
@@ -1660,7 +1656,7 @@ roff_parseln(struct roff *r, int ln, str
}
if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) {
tbl_read(r->tbl, ln, buf->buf, ppos);
- roff_addtbl(r->man, r->tbl);
+ roff_addtbl(r->man, ln, r->tbl);
return e;
}
if ( ! ctl)
@@ -1704,7 +1700,7 @@ roff_parseln(struct roff *r, int ln, str
while (buf->buf[pos] == ' ')
pos++;
tbl_read(r->tbl, ln, buf->buf, pos);
- roff_addtbl(r->man, r->tbl);
+ roff_addtbl(r->man, ln, r->tbl);
return ROFF_IGN;
}
@@ -1767,9 +1763,7 @@ roff_endparse(struct roff *r)
}
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, 1);
r->tbl = NULL;
}
}
@@ -3060,7 +3054,7 @@ roff_TE(ROFF_ARGS)
ln, ppos, "TE");
return ROFF_IGN;
}
- if (tbl_end(r->tbl) == 0) {
+ if (tbl_end(r->tbl, 0) == 0) {
r->tbl = NULL;
free(buf->buf);
buf->buf = mandoc_strdup(".sp");
@@ -3201,12 +3195,10 @@ roff_TS(ROFF_ARGS)
if (r->tbl != NULL) {
mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
ln, ppos, "TS breaks TS");
- tbl_end(r->tbl);
+ tbl_end(r->tbl, 0);
}
- r->tbl = tbl_alloc(ppos, ln, r->parse);
- if (r->last_tbl)
- r->last_tbl->next = r->tbl;
- else
+ r->tbl = tbl_alloc(ppos, ln, r->parse, r->last_tbl);
+ if (r->last_tbl == NULL)
r->first_tbl = r->tbl;
r->last_tbl = r->tbl;
return ROFF_IGN;
Index: tbl_opts.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tbl_opts.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -Ltbl_opts.c -Ltbl_opts.c -u -p -r1.22 -r1.23
--- tbl_opts.c
+++ tbl_opts.c
@@ -27,7 +27,7 @@
#include "mandoc.h"
#include "tbl.h"
#include "libmandoc.h"
-#include "libroff.h"
+#include "tbl_int.h"
#define KEY_DPOINT 0
#define KEY_DELIM 1
Index: tbl_data.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tbl_data.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -Ltbl_data.c -Ltbl_data.c -u -p -r1.48 -r1.49
--- tbl_data.c
+++ tbl_data.c
@@ -29,7 +29,7 @@
#include "mandoc.h"
#include "tbl.h"
#include "libmandoc.h"
-#include "libroff.h"
+#include "tbl_int.h"
static void getdata(struct tbl_node *, struct tbl_span *,
int, const char *, int *);
Index: libroff.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/libroff.h,v
retrieving revision 1.42
retrieving revision 1.43
diff -Llibroff.h -Llibroff.h -u -p -r1.42 -r1.43
--- libroff.h
+++ libroff.h
@@ -1,7 +1,7 @@
/* $Id$ */
/*
- * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2014, 2017 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -16,27 +16,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-enum tbl_part {
- TBL_PART_OPTS, /* in options (first line) */
- TBL_PART_LAYOUT, /* describing layout */
- TBL_PART_DATA, /* creating data rows */
- TBL_PART_CDATA /* continue previous row */
-};
-
-struct tbl_node {
- struct mparse *parse; /* parse point */
- int pos; /* invocation column */
- int line; /* invocation line */
- enum tbl_part part;
- struct tbl_opts opts;
- struct tbl_row *first_row;
- struct tbl_row *last_row;
- struct tbl_span *first_span;
- struct tbl_span *current_span;
- struct tbl_span *last_span;
- struct tbl_node *next;
-};
-
struct eqn_node {
struct mparse *parse; /* main parser, for error reporting */
struct roff_node *node; /* syntax tree of this equation */
@@ -61,17 +40,6 @@ struct eqn_def {
};
-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 *);
-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);
-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 *);
struct eqn_node *eqn_alloc(struct mparse *);
void eqn_box_free(struct eqn_box *);
void eqn_free(struct eqn_node *);
Index: tbl.3
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tbl.3,v
retrieving revision 1.4
retrieving revision 1.5
diff -Ltbl.3 -Ltbl.3 -u -p -r1.4 -r1.5
--- tbl.3
+++ tbl.3
@@ -1,6 +1,6 @@
.\" $Id$
.\"
-.\" Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
+.\" Copyright (c) 2013, 2015, 2018 Ingo Schwarze <schwarze@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
@@ -26,10 +26,9 @@
.Nm tbl_free
.Nd roff table parser library for mandoc
.Sh SYNOPSIS
-.In mandoc.h
+.In sys/types.h
.In tbl.h
-.In libmandoc.h
-.In libroff.h
+.In tbl_parse.h
.Ft struct tbl_node *
.Fo tbl_alloc
.Fa "int pos"
@@ -68,15 +67,15 @@ utility and not designed for stand-alone
The present manual is intended as a reference for developers working on
.Xr mandoc 1 .
.Ss Data structures
-Unless otherwise noted, all of the following data structures are defined in
-.In mandoc.h
+Unless otherwise noted, all of the following data structures are declared in
+.In tbl.h
and are deleted in
.Fn tbl_free .
.Bl -tag -width Ds
.It Vt struct tbl_node
This structure describes a complete table.
-It is defined in
-.In libroff.h ,
+It is declared in
+.In tbl_int.h ,
created in
.Fn tbl_alloc ,
and stored in the members
@@ -227,7 +226,7 @@ member is not
.Ss Interface functions
The following functions are implemented in
.Pa tbl.c ,
-and all callers in
+and all callers are in
.Pa roff.c .
.Bl -tag -width Ds
.It Fn tbl_alloc
@@ -280,6 +279,8 @@ and
.Fn roff_reset .
.El
.Ss Private functions
+The following functions are declared in
+.In tbl_int.h .
.Bl -tag -width Ds
.It Ft int Fn tbl_options "struct tbl_node *tbl" "int ln" "const char *p"
Parses the options line into
Index: tbl.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tbl.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -Ltbl.c -Ltbl.c -u -p -r1.43 -r1.44
--- tbl.c
+++ tbl.c
@@ -29,7 +29,8 @@
#include "mandoc.h"
#include "tbl.h"
#include "libmandoc.h"
-#include "libroff.h"
+#include "tbl_parse.h"
+#include "tbl_int.h"
void
@@ -87,11 +88,13 @@ tbl_read(struct tbl_node *tbl, int ln, c
}
struct tbl_node *
-tbl_alloc(int pos, int line, struct mparse *parse)
+tbl_alloc(int pos, int line, struct mparse *parse, struct tbl_node *last_tbl)
{
struct tbl_node *tbl;
tbl = mandoc_calloc(1, sizeof(*tbl));
+ if (last_tbl != NULL)
+ last_tbl->next = tbl;
tbl->line = line;
tbl->pos = pos;
tbl->parse = parse;
@@ -104,34 +107,37 @@ tbl_alloc(int pos, int line, struct mpar
void
tbl_free(struct tbl_node *tbl)
{
+ struct tbl_node *old_tbl;
struct tbl_row *rp;
struct tbl_cell *cp;
struct tbl_span *sp;
struct tbl_dat *dp;
- while ((rp = tbl->first_row) != NULL) {
- tbl->first_row = rp->next;
- while (rp->first != NULL) {
- cp = rp->first;
- rp->first = cp->next;
- free(cp->wstr);
- free(cp);
+ while (tbl != NULL) {
+ while ((rp = tbl->first_row) != NULL) {
+ tbl->first_row = rp->next;
+ while (rp->first != NULL) {
+ cp = rp->first;
+ rp->first = cp->next;
+ free(cp->wstr);
+ free(cp);
+ }
+ free(rp);
}
- free(rp);
- }
-
- while ((sp = tbl->first_span) != NULL) {
- tbl->first_span = sp->next;
- while (sp->first != NULL) {
- dp = sp->first;
- sp->first = dp->next;
- free(dp->string);
- free(dp);
+ while ((sp = tbl->first_span) != NULL) {
+ tbl->first_span = sp->next;
+ while (sp->first != NULL) {
+ dp = sp->first;
+ sp->first = dp->next;
+ free(dp->string);
+ free(dp);
+ }
+ free(sp);
}
- free(sp);
+ old_tbl = tbl;
+ tbl = tbl->next;
+ free(old_tbl);
}
-
- free(tbl);
}
void
@@ -146,25 +152,27 @@ tbl_restart(int line, int pos, struct tb
tbl->pos = pos;
}
-const struct tbl_span *
+struct tbl_span *
tbl_span(struct tbl_node *tbl)
{
struct tbl_span *span;
- assert(tbl);
span = tbl->current_span ? tbl->current_span->next
: tbl->first_span;
- if (span)
+ if (span != NULL)
tbl->current_span = span;
return span;
}
int
-tbl_end(struct tbl_node *tbl)
+tbl_end(struct tbl_node *tbl, int still_open)
{
struct tbl_span *sp;
- if (tbl->part == TBL_PART_CDATA)
+ if (still_open)
+ mandoc_msg(MANDOCERR_BLK_NOEND, tbl->parse,
+ tbl->line, tbl->pos, "TS");
+ else if (tbl->part == TBL_PART_CDATA)
mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse,
tbl->line, tbl->pos, "TE");
Index: tbl_layout.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tbl_layout.c,v
retrieving revision 1.45
retrieving revision 1.46
diff -Ltbl_layout.c -Ltbl_layout.c -u -p -r1.45 -r1.46
--- tbl_layout.c
+++ tbl_layout.c
@@ -29,7 +29,7 @@
#include "mandoc.h"
#include "tbl.h"
#include "libmandoc.h"
-#include "libroff.h"
+#include "tbl_int.h"
struct tbl_phrase {
char name;
--
To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-12-13 2:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-13 2:06 mandoc: Cleanup, no functional change: Move tbl(7)-specific parser schwarze
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).