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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/* $OpenBSD: util.c,v 1.1 2006/04/26 02:55:13 joris Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
* Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "includes.h"
#include "buf.h"
#include "util.h"
#include "xmalloc.h"
/*
* Split the contents of a file into a list of lines.
*/
struct rcs_lines *
rcs_splitlines(const char *fcont)
{
char *dcp;
struct rcs_lines *lines;
struct rcs_line *lp;
lines = xmalloc(sizeof(*lines));
TAILQ_INIT(&(lines->l_lines));
lines->l_nblines = 0;
lines->l_data = xstrdup(fcont);
lp = xmalloc(sizeof(*lp));
lp->l_line = NULL;
lp->l_lineno = 0;
TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
for (dcp = lines->l_data; *dcp != '\0';) {
lp = xmalloc(sizeof(*lp));
lp->l_line = dcp;
lp->l_lineno = ++(lines->l_nblines);
TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
dcp = strchr(dcp, '\n');
if (dcp == NULL)
break;
*(dcp++) = '\0';
}
return (lines);
}
void
rcs_freelines(struct rcs_lines *lines)
{
struct rcs_line *lp;
while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
xfree(lp);
}
xfree(lines->l_data);
xfree(lines);
}
BUF *
rcs_patchfile(const char *data, const char *patch,
int (*p)(struct rcs_lines *, struct rcs_lines *))
{
struct rcs_lines *dlines, *plines;
struct rcs_line *lp;
size_t len;
int lineno;
BUF *res;
len = strlen(data);
if ((dlines = rcs_splitlines(data)) == NULL)
return (NULL);
if ((plines = rcs_splitlines(patch)) == NULL)
return (NULL);
if (p(dlines, plines) < 0) {
rcs_freelines(dlines);
rcs_freelines(plines);
return (NULL);
}
lineno = 0;
res = rcs_buf_alloc(len, BUF_AUTOEXT);
TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
if (lineno != 0)
rcs_buf_fappend(res, "%s\n", lp->l_line);
lineno++;
}
rcs_freelines(dlines);
rcs_freelines(plines);
return (res);
}
/*
* rcs_yesno()
*
* Read from standart input for `y' or `Y' character.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_yesno(void)
{
int c, ret;
ret = 0;
fflush(stderr);
fflush(stdout);
if ((c = getchar()) != 'y' && c != 'Y')
ret = -1;
else
while (c != EOF && c != '\n')
c = getchar();
return (ret);
}
/*
* rcs_strsplit()
*
* Split a string <str> of <sep>-separated values and allocate
* an argument vector for the values found.
*/
struct rcs_argvector *
rcs_strsplit(char *str, const char *sep)
{
struct rcs_argvector *av;
size_t i = 0;
char **nargv;
char *cp, *p;
cp = xstrdup(str);
av = xmalloc(sizeof(*av));
av->str = cp;
av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
while ((p = strsep(&cp, sep)) != NULL) {
av->argv[i++] = p;
nargv = xrealloc(av->argv,
i + 1, sizeof(*(av->argv)));
av->argv = nargv;
}
av->argv[i] = NULL;
return (av);
}
/*
* rcs_argv_destroy()
*
* Free an argument vector previously allocated by rcs_strsplit().
*/
void
rcs_argv_destroy(struct rcs_argvector *av)
{
xfree(av->str);
xfree(av->argv);
xfree(av);
}
|