aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mandocdb.c
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2011-12-08 09:19:13 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2011-12-08 09:19:13 +0000
commit031f1f42b2ca6451550a08ab288311db7b343e82 (patch)
tree46bcbe40c65735c868261c5c2d3f593c9b8a2bde /mandocdb.c
parentd26d58a731914263c5a682bb3c62c4e651d3b589 (diff)
downloadmandoc-031f1f42b2ca6451550a08ab288311db7b343e82.tar.gz
mandoc-031f1f42b2ca6451550a08ab288311db7b343e82.tar.zst
mandoc-031f1f42b2ca6451550a08ab288311db7b343e82.zip
Clean up grok of preformatted manual description.
(1) put fclose() at the end, as line isn't valid afterward (see fgetln()) (2) clean up loops to be more readable to my old eyes (3) mandate trailing newline, nul-terminate, and use strrchr
Diffstat (limited to 'mandocdb.c')
-rw-r--r--mandocdb.c65
1 files changed, 36 insertions, 29 deletions
diff --git a/mandocdb.c b/mandocdb.c
index d2963816..81e27a0e 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.27 2011/12/08 02:24:31 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.28 2011/12/08 09:19:13 kristaps Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -1288,52 +1288,59 @@ pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
buf_append(buf, of->title);
hash_put(hash, buf, TYPE_Nm);
- while (NULL != (line = fgetln(stream, &len)) && '\n' != *line)
- /* Skip to first blank line. */ ;
+ /* Skip to first blank line. */
- while (NULL != (line = fgetln(stream, &len)) &&
- ('\n' == *line || ' ' == *line))
- /* Skip to first section header. */ ;
+ while (NULL != (line = fgetln(stream, &len)))
+ if (len && '\n' == *line)
+ break;
+
+ /*
+ * Skip to first section header.
+ * This happens when text is flush-left.
+ */
+
+ while (NULL != (line = fgetln(stream, &len)))
+ if (len && '\n' != *line && ' ' != *line)
+ break;
/*
- * If no page content can be found,
- * reuse the page title as the page description.
+ * If no page content can be found or the input line is
+ * malformed (zer-length or has no trailing newline), reuse the
+ * page title as the page description.
*/
- if (NULL == (line = fgetln(stream, &len))) {
+ line = fgetln(stream, &len);
+ if (NULL == line || len == 0 || '\n' != line[(int)len - 1]) {
buf_appendb(dbuf, buf->cp, buf->size);
hash_put(hash, buf, TYPE_Nd);
fclose(stream);
return;
}
- fclose(stream);
- /*
- * If there is a dash, skip to the text following it.
+ line[(int)--len] = '\0';
+
+ /*
+ * Skip to the last dash.
+ * Use the remaining line as the description (no more than 70
+ * bytes).
*/
- for (p = line, plen = len; plen; p++, plen--)
- if ('-' == *p)
- break;
- for ( ; plen; p++, plen--)
- if ('-' != *p && ' ' != *p && 8 != *p)
- break;
- if (0 == plen) {
+ if (NULL != (p = strrchr(line, '-'))) {
+ for (++p; ' ' == *p || '\b' == *p; p++)
+ /* Skip to next word. */ ;
+ } else
p = line;
- plen = len;
- }
- /*
- * Copy the rest of the line, but no more than 70 bytes.
- */
-
- if (70 < plen)
+ if ((plen = strlen(p)) > 70) {
plen = 70;
- p[plen-1] = '\0';
- buf_appendb(dbuf, p, plen);
+ p[plen] = '\0';
+ }
+
+ buf_appendb(dbuf, p, plen + 1);
buf->len = 0;
- buf_appendb(buf, p, plen);
+ buf_appendb(buf, p, plen + 1);
hash_put(hash, buf, TYPE_Nd);
+ fclose(stream);
}
static void