File: getfoldedline.c

package info (click to toggle)
leafnode 1.11.11-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,468 kB
  • sloc: ansic: 10,914; sh: 1,748; xml: 628; makefile: 291; perl: 84; sed: 4
file content (69 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "leafnode.h"
#include "critmem.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

#include <string.h>

/*@null@*/ /*@only@*/ char *
getfoldedline(FILE * f, char *(*reader)(FILE *))
{
    /* what characters are considered whitespace that marks the beginning of
       continuation lines.
       WARNING: NEVER EVER list \n here! */
    static const char white[] = " \t";
    char *l1, *l2;
    int c;
    size_t len, oldlen;

    l1 = reader(f);
    if (!l1)
	return NULL;
    l2 = (char *)critmalloc((len = strlen(l1)) + 1, "getfoldedline");
    strcpy(l2, l1); /* RATS: ignore */

    /* only try to read continuation if the line is not empty 
     * and not a lone dot */
    if (*l2 && strcmp(l2, ".")) {
	for (;;) {
	    c = fgetc(f);
	    if (c != EOF) {
		ungetc(c, f);
		if (strchr(white, c)) {
		    /* join */
		    l1 = reader(f);
		    if (l1) {
			oldlen = len;
			len += strlen(l1) + 1;
			l2 = (char *)critrealloc(l2, len + 2,
						   "getfoldedline");
			l2[oldlen++] = '\n';
			strcpy(l2 + oldlen, l1); /* RATS: ignore */
		    }
		} else {
		    break;
		}
	    } else {
		break;
	    }
	}
    }
    return l2;
}

#ifdef TEST
int debug = 0;

int
main()
{
    char *f;
    while ((f = getfoldedline(stdin, getaline))) {
	puts(f);
	free(f);
    }
    return 0;
}
#endif