diff options
author | Ingo Schwarze <schwarze@openbsd.org> | 2015-04-23 16:17:44 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@openbsd.org> | 2015-04-23 16:17:44 +0000 |
commit | 16ee2910b2d5d0fad6b303e92efff26c0543db03 (patch) | |
tree | 83bd6cef6324bc9f9a440072d5a4d594703e057a /roff.c | |
parent | 48b896d8e54fb00900d8be7283d92985cdcdb0f5 (diff) | |
download | mandoc-16ee2910b2d5d0fad6b303e92efff26c0543db03.tar.gz |
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.
Diffstat (limited to 'roff.c')
-rw-r--r-- | roff.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -1230,6 +1230,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 ---------------------------------- */ /* |