From f313579fbb5a5318367521793dc24d2567da2fde Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Tue, 10 Jan 2017 12:53:07 +0000 Subject: Introduce flags NODE_NOSRC and NODE_NOPRT for AST nodes. Use them to mark generated nodes and nodes that shall not produce output. Let -Ttree output mode display these new flags. Use NODE_NOSRC for .Ar, .Mt, and .Pa default arguments. Use NODE_NOPRT for .Dd, .Dt, and .Os. These will help to make handling of text production macros more rigorous. --- mandoc.1 | 10 ++++++++-- mdoc_html.c | 5 ++++- mdoc_man.c | 7 +++++-- mdoc_term.c | 15 +++++++++++++-- mdoc_validate.c | 31 ++++++++++++++++--------------- roff.h | 6 ++++-- tree.c | 6 +++++- 7 files changed, 55 insertions(+), 25 deletions(-) diff --git a/mandoc.1 b/mandoc.1 index 98457b2f..f99bb14b 100644 --- a/mandoc.1 +++ b/mandoc.1 @@ -1,4 +1,4 @@ -.\" $Id: mandoc.1,v 1.168 2017/01/08 00:11:23 schwarze Exp $ +.\" $Id: mandoc.1,v 1.169 2017/01/10 12:53:07 schwarze Exp $ .\" .\" Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons .\" Copyright (c) 2012, 2014-2017 Ingo Schwarze @@ -15,7 +15,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 8 2017 $ +.Dd $Mdocdate: January 10 2017 $ .Dt MANDOC 1 .Os .Sh NAME @@ -529,6 +529,12 @@ The input column number (starting at one). A closing parenthesis if the node is a closing delimiter. .It A full stop if the node ends a sentence. +.It +NOSRC if the node is not in the input file, +but automatically generated from macros. +.It +NOPRT if the node is not supposed to generate output +for any output format. .El .El .Sh ENVIRONMENT diff --git a/mdoc_html.c b/mdoc_html.c index 3de14e2a..9770c7d5 100644 --- a/mdoc_html.c +++ b/mdoc_html.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_html.c,v 1.242 2017/01/09 12:48:58 schwarze Exp $ */ +/* $Id: mdoc_html.c,v 1.243 2017/01/10 12:53:07 schwarze Exp $ */ /* * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze @@ -377,6 +377,9 @@ print_mdoc_node(MDOC_ARGS) int child; struct tag *t; + if (n->flags & NODE_NOPRT) + return; + child = 1; t = h->tags.head; n->flags &= ~MDOC_ENDED; diff --git a/mdoc_man.c b/mdoc_man.c index ab245313..24241d8c 100644 --- a/mdoc_man.c +++ b/mdoc_man.c @@ -1,6 +1,6 @@ -/* $Id: mdoc_man.c,v 1.96 2016/01/08 17:48:09 schwarze Exp $ */ +/* $Id: mdoc_man.c,v 1.97 2017/01/10 12:53:07 schwarze Exp $ */ /* - * Copyright (c) 2011-2016 Ingo Schwarze + * Copyright (c) 2011-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 @@ -575,6 +575,9 @@ print_node(DECL_ARGS) struct roff_node *sub; int cond, do_sub; + if (n->flags & NODE_NOPRT) + return; + /* * Break the line if we were parsed subsequent the current node. * This makes the page structure be more consistent. diff --git a/mdoc_term.c b/mdoc_term.c index 0f27565a..8d434999 100644 --- a/mdoc_term.c +++ b/mdoc_term.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_term.c,v 1.336 2016/11/08 16:38:21 schwarze Exp $ */ +/* $Id: mdoc_term.c,v 1.337 2017/01/10 12:53:07 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2016 Ingo Schwarze @@ -285,6 +285,8 @@ terminal_mdoc(void *arg, const struct roff_man *mdoc) p->defindent = 5; term_begin(p, print_mdoc_head, print_mdoc_foot, &mdoc->meta); + while (n != NULL && n->flags & NODE_NOPRT) + n = n->next; if (n != NULL) { if (n->tok != MDOC_Sh) term_vspace(p); @@ -311,6 +313,9 @@ print_mdoc_node(DECL_ARGS) struct termpair npair; size_t offset, rmargin; + if (n->flags & NODE_NOPRT) + return; + chld = 1; offset = p->offset; rmargin = p->rmargin; @@ -564,6 +569,8 @@ print_bvspace(struct termp *p, /* Do not vspace directly after Ss/Sh. */ nn = n; + while (nn->prev != NULL && nn->prev->flags & NODE_NOPRT) + nn = nn->prev; while (nn->prev == NULL) { do { nn = nn->parent; @@ -1718,11 +1725,15 @@ termp_pf_post(DECL_ARGS) static int termp_ss_pre(DECL_ARGS) { + struct roff_node *nn; switch (n->type) { case ROFFT_BLOCK: term_newln(p); - if (n->prev) + for (nn = n->prev; nn != NULL; nn = nn->prev) + if ((nn->flags & NODE_NOPRT) == 0) + break; + if (nn != NULL) term_vspace(p); break; case ROFFT_HEAD: diff --git a/mdoc_validate.c b/mdoc_validate.c index 908f6437..be9b6f4f 100644 --- a/mdoc_validate.c +++ b/mdoc_validate.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_validate.c,v 1.312 2017/01/08 02:01:17 schwarze Exp $ */ +/* $Id: mdoc_validate.c,v 1.313 2017/01/10 12:53:07 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -945,12 +945,15 @@ post_defaults(POST_ARGS) case MDOC_Ar: mdoc->next = ROFF_NEXT_CHILD; roff_word_alloc(mdoc, nn->line, nn->pos, "file"); + mdoc->last->flags |= NODE_NOSRC; roff_word_alloc(mdoc, nn->line, nn->pos, "..."); + mdoc->last->flags |= NODE_NOSRC; break; case MDOC_Pa: case MDOC_Mt: mdoc->next = ROFF_NEXT_CHILD; roff_word_alloc(mdoc, nn->line, nn->pos, "~"); + mdoc->last->flags |= NODE_NOSRC; break; default: abort(); @@ -1941,6 +1944,8 @@ post_dd(POST_ARGS) char *datestr; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->meta.date != NULL) { mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse, n->line, n->pos, "Dd"); @@ -1958,7 +1963,7 @@ post_dd(POST_ARGS) if (n->child == NULL || n->child->string[0] == '\0') { mdoc->meta.date = mdoc->quick ? mandoc_strdup("") : mandoc_normdate(mdoc->parse, NULL, n->line, n->pos); - goto out; + return; } datestr = NULL; @@ -1970,8 +1975,6 @@ post_dd(POST_ARGS) datestr, n->line, n->pos); free(datestr); } -out: - roff_node_delete(mdoc, n); } static void @@ -1982,10 +1985,12 @@ post_dt(POST_ARGS) char *p; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->flags & MDOC_PBODY) { mandoc_msg(MANDOCERR_DT_LATE, mdoc->parse, n->line, n->pos, "Dt"); - goto out; + return; } if (mdoc->meta.title != NULL) @@ -2037,7 +2042,7 @@ post_dt(POST_ARGS) mdoc->parse, n->line, n->pos, "Dt %s", mdoc->meta.title); mdoc->meta.vol = mandoc_strdup("LOCAL"); - goto out; /* msec and arch remain NULL. */ + return; /* msec and arch remain NULL. */ } mdoc->meta.msec = mandoc_strdup(nn->string); @@ -2055,7 +2060,7 @@ post_dt(POST_ARGS) /* Optional third argument: architecture. */ if ((nn = nn->next) == NULL) - goto out; + return; for (p = nn->string; *p != '\0'; p++) *p = tolower((unsigned char)*p); @@ -2066,9 +2071,6 @@ post_dt(POST_ARGS) if ((nn = nn->next) != NULL) mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse, nn->line, nn->pos, "Dt ... %s", nn->string); - -out: - roff_node_delete(mdoc, n); } static void @@ -2096,6 +2098,8 @@ post_os(POST_ARGS) struct roff_node *n; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->meta.os != NULL) mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse, n->line, n->pos, "Os"); @@ -2116,11 +2120,11 @@ post_os(POST_ARGS) mdoc->meta.os = NULL; deroff(&mdoc->meta.os, n); if (mdoc->meta.os) - goto out; + return; if (mdoc->defos) { mdoc->meta.os = mandoc_strdup(mdoc->defos); - goto out; + return; } #ifdef OSNAME @@ -2137,9 +2141,6 @@ post_os(POST_ARGS) } mdoc->meta.os = mandoc_strdup(defbuf); #endif /*!OSNAME*/ - -out: - roff_node_delete(mdoc, n); } /* diff --git a/roff.h b/roff.h index 19ec50f4..783e5daa 100644 --- a/roff.h +++ b/roff.h @@ -1,7 +1,7 @@ -/* $OpenBSD$ */ +/* $Id: roff.h,v 1.38 2017/01/10 12:53:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 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 @@ -109,6 +109,8 @@ struct roff_node { #define MAN_VALID MDOC_VALID #define MAN_EOS MDOC_EOS #define MAN_LINE MDOC_LINE +#define NODE_NOSRC (1 << 8) /* Generated node, not in input file. */ +#define NODE_NOPRT (1 << 9) /* Shall not print anything. */ int prev_font; /* Before entering this node. */ int aux; /* Decoded node data, type-dependent. */ enum roff_type type; /* AST node type. */ diff --git a/tree.c b/tree.c index 52ca7547..2e85727b 100644 --- a/tree.c +++ b/tree.c @@ -1,4 +1,4 @@ -/* $Id: tree.c,v 1.69 2015/10/12 00:08:16 schwarze Exp $ */ +/* $Id: tree.c,v 1.70 2017/01/10 12:53:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -168,6 +168,10 @@ print_mdoc(const struct roff_node *n, int indent) putchar(')'); if (MDOC_EOS & n->flags) putchar('.'); + if (NODE_NOSRC & n->flags) + printf(" NOSRC"); + if (NODE_NOPRT & n->flags) + printf(" NOPRT"); putchar('\n'); } -- cgit v1.2.3-56-ge451