=================================================================== RCS file: /cvs/mandoc/roff.c,v retrieving revision 1.268 retrieving revision 1.269 diff -u -p -r1.268 -r1.269 --- mandoc/roff.c 2015/04/19 14:57:38 1.268 +++ mandoc/roff.c 2015/04/23 16:17:44 1.269 @@ -1,4 +1,4 @@ -/* $Id: roff.c,v 1.268 2015/04/19 14:57:38 schwarze Exp $ */ +/* $Id: roff.c,v 1.269 2015/04/23 16:17:44 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_nod assert(n->nchild == 0); roff_node_unlink(man, 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 ---------------------------------- */