]> diplodocus.org Git - nmh/blob - sbr/vfgets.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / vfgets.c
1 /* vfgets.c -- virtual fgets
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 #define QUOTE '\\'
12
13
14 int
15 vfgets (FILE *in, char **bp)
16 {
17 int toggle;
18 char *cp, *dp, *ep, *fp;
19 static int len = 0;
20 static char *pp = NULL;
21
22 if (pp == NULL)
23 pp = mh_xmalloc ((size_t) (len = BUFSIZ));
24
25 for (ep = (cp = pp) + len - 1;;) {
26 if (fgets (cp, ep - cp + 1, in) == NULL) {
27 if (cp != pp) {
28 *bp = pp;
29 return 0;
30 }
31 return (ferror (in) && !feof (in) ? -1 : 1);
32 }
33
34 if ((dp = cp + strlen (cp) - 2) < cp || *dp != QUOTE) {
35 wrong_guess:
36 if (cp > ++dp)
37 adios (NULL, "vfgets() botch -- you lose big");
38 if (*dp == '\n') {
39 *bp = pp;
40 return 0;
41 }
42 cp = ++dp;
43 } else {
44 for (fp = dp - 1, toggle = 0; fp >= cp; fp--) {
45 if (*fp != QUOTE)
46 break;
47 toggle = !toggle;
48 }
49 if (toggle)
50 goto wrong_guess;
51
52 if (*++dp == '\n') {
53 *--dp = 0;
54 cp = dp;
55 } else {
56 cp = ++dp;
57 }
58 }
59
60 if (cp >= ep) {
61 int curlen = cp - pp;
62
63 dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ));
64 cp = dp + curlen;
65 ep = (pp = dp) + len - 1;
66 }
67 }
68 }