From 423631c995882ca2c2da9e241987914cde8349c5 Mon Sep 17 00:00:00 2001 From: schwarze Date: Thu, 23 Apr 2015 16:17:04 +0000 Subject: [PATCH] Unify mdoc_deroff() and man_deroff() into a common function deroff(). No functional change except that for mdoc(7), it now skips leading escape sequences just like it already did for man(7). Escape sequences rarely occur in mdoc(7) code and if they do, skipping them is an improvement in this context. Minus 30 lines of code. --- usr.bin/mandoc/man.c | 48 +--------------------------------- usr.bin/mandoc/man.h | 3 +-- usr.bin/mandoc/mandocdb.c | 6 ++--- usr.bin/mandoc/mdoc.c | 41 +---------------------------- usr.bin/mandoc/mdoc.h | 10 +------ usr.bin/mandoc/mdoc_validate.c | 10 +++---- usr.bin/mandoc/roff.c | 48 +++++++++++++++++++++++++++++++++- usr.bin/mandoc/roff.h | 8 +++++- 8 files changed, 66 insertions(+), 108 deletions(-) diff --git a/usr.bin/mandoc/man.c b/usr.bin/mandoc/man.c index 3d20cd9334d..90916a44002 100644 --- a/usr.bin/mandoc/man.c +++ b/usr.bin/mandoc/man.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man.c,v 1.110 2015/04/23 15:35:39 schwarze Exp $ */ +/* $OpenBSD: man.c,v 1.111 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -316,49 +316,3 @@ man_mparse(const struct roff_man *man) assert(man && man->parse); return(man->parse); } - -void -man_deroff(char **dest, const struct roff_node *n) -{ - char *cp; - size_t sz; - - if (n->type != ROFFT_TEXT) { - for (n = n->child; n; n = n->next) - man_deroff(dest, n); - return; - } - - /* Skip leading whitespace and escape sequences. */ - - cp = n->string; - while ('\0' != *cp) { - if ('\\' == *cp) { - cp++; - mandoc_escape((const char **)&cp, NULL, NULL); - } else if (isspace((unsigned char)*cp)) - cp++; - else - break; - } - - /* Skip trailing whitespace. */ - - for (sz = strlen(cp); sz; sz--) - if (0 == isspace((unsigned char)cp[sz-1])) - break; - - /* Skip empty strings. */ - - if (0 == sz) - return; - - if (NULL == *dest) { - *dest = mandoc_strndup(cp, sz); - return; - } - - mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); - free(*dest); - *dest = cp; -} diff --git a/usr.bin/mandoc/man.h b/usr.bin/mandoc/man.h index 9ce71704565..cbd15b0c8df 100644 --- a/usr.bin/mandoc/man.h +++ b/usr.bin/mandoc/man.h @@ -1,4 +1,4 @@ -/* $OpenBSD: man.h,v 1.54 2015/04/18 17:50:02 schwarze Exp $ */ +/* $OpenBSD: man.h,v 1.55 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -64,6 +64,5 @@ __BEGIN_DECLS struct roff_man; const struct mparse *man_mparse(const struct roff_man *); -void man_deroff(char **, const struct roff_node *); __END_DECLS diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c index 01dc61f5b95..b51886acde0 100644 --- a/usr.bin/mandoc/mandocdb.c +++ b/usr.bin/mandoc/mandocdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mandocdb.c,v 1.146 2015/04/18 17:50:02 schwarze Exp $ */ +/* $OpenBSD: mandocdb.c,v 1.147 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2015 Ingo Schwarze @@ -1458,7 +1458,7 @@ parse_man(struct mpage *mpage, const struct roff_meta *meta, */ title = NULL; - man_deroff(&title, body); + deroff(&title, body); if (NULL == title) return; @@ -1702,7 +1702,7 @@ parse_mdoc_Nd(struct mpage *mpage, const struct roff_meta *meta, { if (n->type == ROFFT_BODY) - mdoc_deroff(&mpage->desc, n); + deroff(&mpage->desc, n); return(0); } diff --git a/usr.bin/mandoc/mdoc.c b/usr.bin/mandoc/mdoc.c index 6dc9d7db574..05952a28873 100644 --- a/usr.bin/mandoc/mdoc.c +++ b/usr.bin/mandoc/mdoc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc.c,v 1.140 2015/04/23 15:35:39 schwarze Exp $ */ +/* $OpenBSD: mdoc.c,v 1.141 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -505,42 +505,3 @@ mdoc_isdelim(const char *p) return(DELIM_NONE); } - -void -mdoc_deroff(char **dest, const struct roff_node *n) -{ - char *cp; - size_t sz; - - if (n->type != ROFFT_TEXT) { - for (n = n->child; n; n = n->next) - mdoc_deroff(dest, n); - return; - } - - /* Skip leading whitespace. */ - - for (cp = n->string; '\0' != *cp; cp++) - if (0 == isspace((unsigned char)*cp)) - break; - - /* Skip trailing whitespace. */ - - for (sz = strlen(cp); sz; sz--) - if (0 == isspace((unsigned char)cp[sz-1])) - break; - - /* Skip empty strings. */ - - if (0 == sz) - return; - - if (NULL == *dest) { - *dest = mandoc_strndup(cp, sz); - return; - } - - mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); - free(*dest); - *dest = cp; -} diff --git a/usr.bin/mandoc/mdoc.h b/usr.bin/mandoc/mdoc.h index 5f773453645..28669f4dcde 100644 --- a/usr.bin/mandoc/mdoc.h +++ b/usr.bin/mandoc/mdoc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc.h,v 1.66 2015/04/18 17:50:02 schwarze Exp $ */ +/* $OpenBSD: mdoc.h,v 1.67 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -279,11 +279,3 @@ extern const char *const *mdoc_macronames; /* Names of macro args. Index is enum mdocargt. */ extern const char *const *mdoc_argnames; - -__BEGIN_DECLS - -struct roff_man; - -void mdoc_deroff(char **, const struct roff_node *); - -__END_DECLS diff --git a/usr.bin/mandoc/mdoc_validate.c b/usr.bin/mandoc/mdoc_validate.c index 0063abd80cc..0c664f672ea 100644 --- a/usr.bin/mandoc/mdoc_validate.c +++ b/usr.bin/mandoc/mdoc_validate.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc_validate.c,v 1.206 2015/04/20 09:48:19 schwarze Exp $ */ +/* $OpenBSD: mdoc_validate.c,v 1.207 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -960,7 +960,7 @@ post_nm(POST_ARGS) if (NULL != mdoc->meta.name) return; - mdoc_deroff(&mdoc->meta.name, n); + deroff(&mdoc->meta.name, n); if (NULL == mdoc->meta.name) mandoc_msg(MANDOCERR_NM_NONAME, mdoc->parse, @@ -1874,7 +1874,7 @@ post_sh_head(POST_ARGS) secname = NULL; sec = SEC_CUSTOM; - mdoc_deroff(&secname, mdoc->last); + deroff(&secname, mdoc->last); sec = NULL == secname ? SEC_CUSTOM : a2sec(secname); /* The NAME should be first. */ @@ -2123,7 +2123,7 @@ post_dd(POST_ARGS) } datestr = NULL; - mdoc_deroff(&datestr, n); + deroff(&datestr, n); if (mdoc->quick) mdoc->meta.date = datestr; else { @@ -2258,7 +2258,7 @@ post_os(POST_ARGS) free(mdoc->meta.os); mdoc->meta.os = NULL; - mdoc_deroff(&mdoc->meta.os, n); + deroff(&mdoc->meta.os, n); if (mdoc->meta.os) goto out; diff --git a/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c index 239783486a7..d82eb79552b 100644 --- a/usr.bin/mandoc/roff.c +++ b/usr.bin/mandoc/roff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roff.c,v 1.140 2015/04/19 14:57:16 schwarze Exp $ */ +/* $OpenBSD: roff.c,v 1.141 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -1228,6 +1228,52 @@ roff_node_delete(struct roff_man *man, struct roff_node *n) roff_node_free(n); } +void +deroff(char **dest, const struct roff_node *n) +{ + char *cp; + size_t sz; + + if (n->type != ROFFT_TEXT) { + for (n = n->child; n != NULL; n = n->next) + deroff(dest, n); + return; + } + + /* Skip leading whitespace and escape sequences. */ + + cp = n->string; + while (*cp != '\0') { + if ('\\' == *cp) { + cp++; + mandoc_escape((const char **)&cp, NULL, NULL); + } else if (isspace((unsigned char)*cp)) + cp++; + else + break; + } + + /* Skip trailing whitespace. */ + + for (sz = strlen(cp); sz; sz--) + if ( ! isspace((unsigned char)cp[sz-1])) + break; + + /* Skip empty strings. */ + + if (sz == 0) + return; + + if (*dest == NULL) { + *dest = mandoc_strndup(cp, sz); + return; + } + + mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp); + free(*dest); + *dest = cp; +} + /* --- main functions of the roff parser ---------------------------------- */ /* diff --git a/usr.bin/mandoc/roff.h b/usr.bin/mandoc/roff.h index 50ae42abdba..478c835d5a7 100644 --- a/usr.bin/mandoc/roff.h +++ b/usr.bin/mandoc/roff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: roff.h,v 1.15 2015/04/19 13:59:37 schwarze Exp $ */ +/* $OpenBSD: roff.h,v 1.16 2015/04/23 16:17:04 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -157,3 +157,9 @@ struct roff_man { enum roff_sec lastnamed; /* Last standard section seen. */ enum roff_next next; /* Where to put the next node. */ }; + +__BEGIN_DECLS + +void deroff(char **, const struct roff_node *); + +__END_DECLS -- 2.20.1