varnish-cache/lib/libvarnish/vlu.c
1
/*-
2
 * Copyright (c) 2005-2008 Poul-Henning Kamp <phk@freebsd.org>
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * Functions for assembling a bytestream into text-lines and calling
27
 * a function on each.
28
 */
29
30
#include "config.h"
31
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <unistd.h>
36
37
#include "vdef.h"
38
39
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
40
#include "miniobj.h"
41
42
#include "vlu.h"
43
44
struct vlu {
45
        unsigned        magic;
46
#define LINEUP_MAGIC    0x8286661
47
        char            *buf;
48
        unsigned        bufl;
49
        unsigned        bufp;
50
        void            *priv;
51
        vlu_f   *func;
52
};
53
54
struct vlu *
55 3032
VLU_New(vlu_f *func, void *priv, unsigned bufsize)
56
{
57
        struct vlu *l;
58
59 3032
        if (bufsize == 0)
60 3031
                bufsize = BUFSIZ;
61 3032
        ALLOC_OBJ(l, LINEUP_MAGIC);
62 3032
        if (l != NULL) {
63 3032
                l->func = func;
64 3032
                l->priv = priv;
65 3032
                l->bufl = bufsize - 1;
66 3032
                l->buf = malloc(l->bufl + 1L);
67 3032
                if (l->buf == NULL) {
68 0
                        FREE_OBJ(l);
69 0
                        l = NULL;
70
                }
71
        }
72 3032
        return (l);
73
}
74
75
void
76 3032
VLU_Destroy(struct vlu **lp)
77
{
78
        struct vlu *l;
79
80 3032
        AN(lp);
81 3032
        l = *lp;
82 3032
        *lp = NULL;
83 3032
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
84 3032
        free(l->buf);
85 3032
        FREE_OBJ(l);
86 3032
}
87
88
static int
89 1734
LineUpProcess(struct vlu *l)
90
{
91
        char *p, *q;
92
        int i;
93
94 1734
        l->buf[l->bufp] = '\0';
95 4105
        for (p = l->buf; *p != '\0'; p = q) {
96
                /* Find first CR or NL */
97 79343
                for (q = p; *q != '\0'; q++) {
98 79031
                        if (*q == '\n' || *q == '\r')
99
                                break;
100
                }
101 2683
                if (*q == '\0')
102 312
                        break;
103 2371
                *q++ = '\0';
104 2371
                i = l->func(l->priv, p);
105 2371
                if (i != 0)
106 0
                        return (i);
107
        }
108 1734
        if (*p != '\0') {
109 312
                q = strchr(p, '\0');
110 312
                assert(q != NULL);
111 312
                l->bufp = (unsigned)(q - p);
112 312
                memmove(l->buf, p, l->bufp);
113 312
                l->buf[l->bufp] = '\0';
114
        } else
115 1422
                l->bufp = 0;
116 1734
        return (0);
117
}
118
119
int
120 4157
VLU_Fd(struct vlu *l, int fd)
121
{
122
        int i;
123
124 4157
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
125 4157
        i = read(fd, l->buf + l->bufp, l->bufl - l->bufp);
126 4157
        if (i <= 0)
127 2423
                return (-1);
128 1734
        l->bufp += i;
129 1734
        return (LineUpProcess(l));
130
}
131
132
int
133 2417
VLU_File(int fd, vlu_f *func, void *priv, unsigned bufsize)
134
{
135
        struct vlu *vlu;
136
        int i;
137
138 2417
        vlu = VLU_New(func, priv, bufsize);
139 2417
        AN(vlu);
140
        do {
141 2595
                i = VLU_Fd(vlu, fd);
142 2595
        } while (i == 0);
143 2417
        VLU_Destroy(&vlu);
144 2417
        return (i);
145
}