convert remaining functions in diff3.c to struct got_error
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
diff --git a/lib/diff3.c b/lib/diff3.c
index 5ebdc20..962e41a 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -152,18 +152,22 @@ struct diff3_state {
static const struct got_error *duplicate(int *, struct range *, struct range *,
struct diff3_state *);
-static int edit(struct diff *, int, int, struct diff3_state *);
-static char *getchange(FILE *, struct diff3_state *);
-static char *get_line(FILE *, size_t *, struct diff3_state *);
+static const struct got_error *edit(struct diff *, int, int *,
+ struct diff3_state *);
+static const struct got_error *getchange(char **, FILE *, struct diff3_state *);
+static const struct got_error *get_line(char **, FILE *, size_t *,
+ struct diff3_state *);
static int number(char **);
static const struct got_error *readin(size_t *, char *, struct diff **,
struct diff3_state *);
static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
-static int skip(int, int, char *, struct diff3_state *);
+static const struct got_error *skip(int *, int, int, char *,
+ struct diff3_state *);
static const struct got_error *edscript(int, struct diff3_state *);
static const struct got_error *merge(size_t, size_t, struct diff3_state *);
-static void change(int, struct range *, int, struct diff3_state *);
-static void keep(int, struct range *, struct diff3_state *);
+static const struct got_error *change(int, struct range *, int,
+ struct diff3_state *);
+static const struct got_error *keep(int, struct range *, struct diff3_state *);
static void prange(struct range *, struct diff3_state *);
static const struct got_error *repos(int, struct diff3_state *);
static void separate(const char *, struct diff3_state *);
@@ -584,7 +588,10 @@ readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
d3s->fp[0] = fopen(name, "r");
if (d3s->fp[0] == NULL)
return got_error_from_errno2("fopen", name);
- for (i = 0; (p = getchange(d3s->fp[0], d3s)); i++) {
+ err = getchange(&p, d3s->fp[0], d3s);
+ if (err)
+ return err;
+ for (i = 0; p; i++) {
if (i >= d3s->szchanges - 1) {
err = increase(d3s);
if (err)
@@ -611,6 +618,10 @@ readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
(*dd)[i].old.to = b;
(*dd)[i].new.from = c;
(*dd)[i].new.to = d;
+
+ err = getchange(&p, d3s->fp[0], d3s);
+ if (err)
+ return err;
}
if (i) {
@@ -637,38 +648,49 @@ number(char **lc)
return (nn);
}
-static char *
-getchange(FILE *b, struct diff3_state *d3s)
+static const struct got_error *
+getchange(char **line, FILE *b, struct diff3_state *d3s)
{
- char *line;
+ const struct got_error *err = NULL;
- while ((line = get_line(b, NULL, d3s))) {
- if (isdigit((unsigned char)line[0]))
- return (line);
- }
+ *line = NULL;
+ do {
+ if (*line && isdigit((unsigned char)(*line)[0]))
+ return NULL;
+ err = get_line(line, b, NULL, d3s);
+ if (err)
+ return err;
+ } while (*line);
- return (NULL);
+ return NULL;
}
-static char *
-get_line(FILE *b, size_t *n, struct diff3_state *d3s)
+static const struct got_error *
+get_line(char **ret, FILE *b, size_t *n, struct diff3_state *d3s)
{
+ const struct got_error *err = NULL;
char *cp = NULL;
size_t size;
ssize_t len;
char *new;
- char *ret = NULL;
+
+ *ret = NULL;
len = getline(&cp, &size, b);
- if (len == -1)
+ if (len == -1) {
+ if (ferror(b))
+ err = got_error_from_errno("getline");
goto done;
+ }
if (cp[len - 1] != '\n') {
len++;
if (len + 1 > size) {
new = realloc(cp, len + 1);
- if (new == NULL)
+ if (new == NULL) {
+ err = got_error_from_errno("realloc");
goto done;
+ }
cp = new;
}
cp[len - 1] = '\n';
@@ -676,13 +698,13 @@ get_line(FILE *b, size_t *n, struct diff3_state *d3s)
}
free(d3s->buf);
- ret = d3s->buf = cp;
+ *ret = d3s->buf = cp;
cp = NULL;
if (n != NULL)
*n = len;
done:
free(cp);
- return (ret);
+ return err;
}
static const struct got_error *
@@ -714,9 +736,15 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
/* stuff peculiar to 1st file */
if (d3s->eflag == 0) {
separate("1", d3s);
- change(1, &d1->old, 0, d3s);
- keep(2, &d1->new, d3s);
- change(3, &d1->new, 0, d3s);
+ err = change(1, &d1->old, 0, d3s);
+ if (err)
+ return err;
+ err = keep(2, &d1->new, d3s);
+ if (err)
+ return err;
+ err = change(3, &d1->new, 0, d3s);
+ if (err)
+ return err;
}
d1++;
continue;
@@ -726,9 +754,15 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
if (!t1 || (t2 && d2->new.to < d1->new.from)) {
if (d3s->eflag == 0) {
separate("2", d3s);
- keep(1, &d2->new, d3s);
- change(2, &d2->old, 0, d3s);
- change(3, &d2->new, 0, d3s);
+ err = keep(1, &d2->new, d3s);
+ if (err)
+ return err;
+ err = change(2, &d2->old, 0, d3s);
+ if (err)
+ return err;
+ err = change(3, &d2->new, 0, d3s);
+ if (err)
+ return err;
}
d2++;
continue;
@@ -764,12 +798,21 @@ merge(size_t m1, size_t m2, struct diff3_state *d3s)
*/
if (d3s->eflag == 0) {
separate(dpl ? "3" : "", d3s);
- change(1, &d1->old, dpl, d3s);
- change(2, &d2->old, 0, d3s);
+ err = change(1, &d1->old, dpl, d3s);
+ if (err)
+ return err;
+ err = change(2, &d2->old, 0, d3s);
+ if (err)
+ return err;
d3 = d1->old.to > d1->old.from ? d1 : d2;
- change(3, &d3->new, 0, d3s);
- } else
- j = edit(d1, dpl, j, d3s);
+ err = change(3, &d3->new, 0, d3s);
+ if (err)
+ return err;
+ } else {
+ err = edit(d1, dpl, &j, d3s);
+ if (err)
+ return err;
+ }
d1++;
d2++;
continue;
@@ -809,17 +852,22 @@ separate(const char *s, struct diff3_state *d3s)
* It is to be printed only if it does not duplicate something to be
* printed later.
*/
-static void
+static const struct got_error *
change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
{
+ const struct got_error *err = NULL;
+ int nskipped;
+
diff_output(d3s->diffbuf, "%d:", i);
d3s->last[i] = rold->to;
prange(rold, d3s);
if (fdup || d3s->debug)
- return;
+ return NULL;
i--;
- (void)skip(i, rold->from, NULL, d3s);
- (void)skip(i, rold->to, " ", d3s);
+ err = skip(&nskipped, i, rold->from, NULL, d3s);
+ if (err)
+ return err;
+ return skip(&nskipped, i, rold->to, " ", d3s);
}
/*
@@ -843,7 +891,7 @@ prange(struct range *rold, struct diff3_state *d3s)
* and an artificial dummy difference (trange) must be ginned up to
* correspond to the change reported in the other file.
*/
-static void
+static const struct got_error *
keep(int i, struct range *rnew, struct diff3_state *d3s)
{
int delta;
@@ -852,27 +900,31 @@ keep(int i, struct range *rnew, struct diff3_state *d3s)
delta = d3s->last[3] - d3s->last[i];
trange.from = rnew->from - delta;
trange.to = rnew->to - delta;
- change(i, &trange, 1, d3s);
+ return change(i, &trange, 1, d3s);
}
/*
* skip to just before line number from in file "i". If "pr" is non-NULL,
* print all skipped stuff with string pr as a prefix.
*/
-static int
-skip(int i, int from, char *pr, struct diff3_state *d3s)
+static const struct got_error *
+skip(int *nskipped, int i, int from, char *pr, struct diff3_state *d3s)
{
+ const struct got_error *err = NULL;
size_t j, n;
char *line;
+ *nskipped = 0;
for (n = 0; d3s->cline[i] < from - 1; n += j) {
- if ((line = get_line(d3s->fp[i], &j, d3s)) == NULL)
- return (-1);
+ err = get_line(&line, d3s->fp[i], &j, d3s);
+ if (err)
+ return err;
if (pr != NULL)
diff_output(d3s->diffbuf, "%s%s", pr, line);
d3s->cline[i]++;
}
- return ((int) n);
+ *nskipped = n;
+ return NULL;
}
/*
@@ -885,15 +937,19 @@ duplicate(int *dpl, struct range *r1, struct range *r2, struct diff3_state *d3s)
const struct got_error *err = NULL;
int c,d;
int nchar;
- int nline;
+ int nline, nskipped;
*dpl = 0;
if (r1->to-r1->from != r2->to-r2->from)
return NULL;
- (void)skip(0, r1->from, NULL, d3s);
- (void)skip(1, r2->from, NULL, d3s);
+ err = skip(&nskipped, 0, r1->from, NULL, d3s);
+ if (err)
+ return err;
+ err = skip(&nskipped, 1, r2->from, NULL, d3s);
+ if (err)
+ return err;
nchar = 0;
for (nline=0; nline < r1->to - r1->from; nline++) {
do {
@@ -931,22 +987,27 @@ repos(int nchar, struct diff3_state *d3s)
/*
* collect an editing script for later regurgitation
*/
-static int
-edit(struct diff *diff, int fdup, int j, struct diff3_state *d3s)
+static const struct got_error *
+edit(struct diff *diff, int fdup, int *j, struct diff3_state *d3s)
{
+ const struct got_error *err = NULL;
+ int nskipped;
+
if (((fdup + 1) & d3s->eflag) == 0)
- return (j);
- j++;
- d3s->overlap[j] = !fdup;
+ return NULL;
+ (*j)++;
+ d3s->overlap[*j] = !fdup;
if (!fdup)
d3s->overlapcnt++;
- d3s->de[j].old.from = diff->old.from;
- d3s->de[j].old.to = diff->old.to;
- d3s->de[j].new.from =
- d3s->de[j-1].new.to + skip(2, diff->new.from, NULL, d3s);
- d3s->de[j].new.to =
- d3s->de[j].new.from + skip(2, diff->new.to, NULL, d3s);
- return (j);
+ d3s->de[*j].old.from = diff->old.from;
+ d3s->de[*j].old.to = diff->old.to;
+ err = skip(&nskipped, 2, diff->new.from, NULL, d3s);
+ if (err)
+ return err;
+ d3s->de[*j].new.from = d3s->de[*j - 1].new.to + nskipped;
+ err = skip(&nskipped, 2, diff->new.to, NULL, d3s);
+ d3s->de[*j].new.to = d3s->de[*j].new.from + nskipped;
+ return NULL;
}
/* regurgitate */