Code Coverage: vinyl-cache/lib/libvinyl/vlu.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2005-2008 Poul-Henning Kamp <phk@FreeBSD.org>
2
 * All rights reserved.
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 *
27
 * Functions for assembling a bytestream into text-lines and calling
28
 * a function on each.
29
 */
30
31
#include "config.h"
32
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <unistd.h>
37
38
#include "vdef.h"
39
40
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
41
#include "miniobj.h"
42
43
#include "vlu.h"
44
45
struct vlu {
46
        unsigned        magic;
47
#define LINEUP_MAGIC    0x08286661
48
        char            *buf;
49
        unsigned        bufl;
50
        unsigned        bufp;
51
        void            *priv;
52
        vlu_f   *func;
53
};
54
55
struct vlu *
56
VLU_New(vlu_f *func, void *priv, unsigned bufsize)
57 10404
{
58 10404
        struct vlu *l;
59
60 10404
        if (bufsize == 0)
61 10366
                bufsize = BUFSIZ;
62 10404
        ALLOC_OBJ(l, LINEUP_MAGIC);
63 10404
        if (l != NULL) {
64 10404
                l->func = func;
65 10404
                l->priv = priv;
66 10404
                l->bufl = bufsize - 1;
67 10404
                l->buf = malloc(l->bufl + 1L);
68 10404
                if (l->buf == NULL)
69 0
                        FREE_OBJ(l);
70 10404
        }
71 10404
        return (l);
72 10404
}
73
74
void
75
VLU_Reset(struct vlu *l)
76 4211
{
77 4211
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
78 4211
        l->bufp = 0;
79 4211
}
80
81
void
82
VLU_Destroy(struct vlu **lp)
83 10340
{
84 10340
        struct vlu *l;
85
86 10340
        TAKE_OBJ_NOTNULL(l, lp, LINEUP_MAGIC);
87 10340
        free(l->buf);
88 10340
        FREE_OBJ(l);
89 10340
}
90
91
static int
92
LineUpProcess(struct vlu *l)
93 13763
{
94 13763
        char *p, *q;
95 13763
        int i;
96
97 13763
        l->buf[l->bufp] = '\0';
98 124998
        for (p = l->buf; *p != '\0'; p = q) {
99
                /* Find first CR or NL */
100 5022445
                for (q = p; *q != '\0'; q++) {
101 5018821
                        if (*q == '\n' || *q == '\r')
102 111238
                                break;
103 5018821
                }
104 114862
                if (*q == '\0')
105 3624
                        break;
106 111238
                *q++ = '\0';
107 111238
                i = l->func(l->priv, p);
108 111238
                if (i != 0)
109 3
                        return (i);
110 111238
        }
111 13760
        if (*p != '\0') {
112 3624
                q = strchr(p, '\0');
113 3624
                assert(q != NULL);
114 3624
                l->bufp = (unsigned)(q - p);
115 3624
                vmemmove(l->buf, p, l->bufp);
116 3624
                l->buf[l->bufp] = '\0';
117 3624
        } else
118 10136
                l->bufp = 0;
119 13760
        return (0);
120 13760
}
121
122
int
123
VLU_Fd(struct vlu *l, int fd)
124 106806
{
125 106806
        ssize_t i;
126 106806
        size_t sz;
127
128 106806
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
129 106806
        assert(l->bufl >= l->bufp);
130 106806
        sz = l->bufl - l->bufp;
131 106806
        i = read(fd, l->buf + l->bufp, sz);
132 106806
        if (i == 0)
133 81915
                return (-2);
134 24891
        if (i < 0)
135 11204
                return (-1);
136 13687
        assert((size_t)i <= sz);
137 13687
        l->bufp += i;
138 13687
        return (LineUpProcess(l));
139 13687
}
140
141
int
142
VLU_File(int fd, vlu_f *func, void *priv, unsigned bufsize)
143 4300
{
144 4300
        struct vlu *vlu;
145 4300
        int i;
146
147 4300
        vlu = VLU_New(func, priv, bufsize);
148 4300
        AN(vlu);
149 4673
        do {
150 4673
                i = VLU_Fd(vlu, fd);
151 4673
        } while (i == 0);
152 4300
        VLU_Destroy(&vlu);
153 4300
        return (i);
154 4300
}
155
156
int
157
VLU_Feed(struct vlu *l, const char *ptr, int len)
158 76
{
159 76
        int i = 0;
160 76
        unsigned u;
161
162 76
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
163 76
        AN(ptr);
164 76
        assert(len > 0);
165 152
        while (len > 0) {
166 76
                u = len;
167 76
                if (u > l->bufl - l->bufp)
168 0
                        u = l->bufl - l->bufp;
169 76
                vmemcpy(l->buf + l->bufp, ptr, u);
170 76
                len -= u;
171 76
                ptr += u;
172 76
                l->bufp += u;
173 76
                i = LineUpProcess(l);
174 76
                if (i)
175 0
                        return (i);
176 76
        }
177 76
        return (i);
178 76
}