File: diff.cc

package info (click to toggle)
aegis 4.24.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 34,056 kB
  • ctags: 12,500
  • sloc: cpp: 178,528; sh: 79,948; makefile: 34,813; yacc: 4,610; perl: 1,499; ansic: 492; awk: 325
file content (399 lines) | stat: -rw-r--r-- 7,955 bytes parent folder | download | duplicates (3)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
//	aegis - project change supervisor
//	Copyright (C) 2002-2006, 2008 Peter Miller
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program. If not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/ac/stdio.h>
#include <common/ac/stdlib.h>
#include <common/ac/string.h>

#include <common/error.h>
#include <libaegis/patch.h>
#include <libaegis/patch/context.h>
#include <libaegis/patch/format/diff.h>
#include <common/trace.h>


static int
starts_with(string_ty *line, const char *prefix)
{
    size_t	    pfxlen;

    pfxlen = strlen(prefix);
    return
	(
	    line->str_length > pfxlen + 1
	&&
	    0 == memcmp(line->str_text, prefix, pfxlen)
	&&
	    (line->str_text[pfxlen] == ' ' || line->str_text[pfxlen] == '\t')
	);
}


static int
parse_command(string_ty *line, long *b1, long *b2, char *c, long *a1, long *a2)
{
    char	    *s;
    char	    *end;

    //
    // Get the first range.
    //
    s = line->str_text;
    *b1 = strtol(s, &end, 10);
    if (s == end)
	return 0;
    s = end;

    if (*s == ',')
    {
	++s;
	*b2 = strtol(s, &end, 10);
	if (s == end)
	    return 0;
	s = end;
    }
    else
	*b2 = *b1;

    //
    // Get the command.
    //
    if (!strchr("acd", *s))
	return 0;
    *c = *s++;

    //
    // Get the second range.
    //
    *a1 = strtol(s, &end, 10);
    if (s == end)
	return 0;
    s = end;

    if (*s == ',')
    {
	++s;
	*a2 = strtol(s, &end, 10);
	if (s == end)
	    return 0;
	s = end;
    }
    else
	*a2 = *a1;

    if (*c == 'a' && *b1 != *b2)
	return 0;
    if (*c == 'd' && *a1 != *a2)
	return 0;

    //
    // Make sure there is no junk on the end of the line.
    //
    return !*s;
}


static string_ty *
second_word(string_ty *line)
{
	const char	*cp;
	const char	*ep;

	cp = line->str_text;
	while (*cp && *cp != ' ' && *cp != '\t')
		++cp;
	while (*cp && (*cp == ' ' || *cp == '\t'))
		++cp;
	ep = cp;
	while (*ep && *ep != ' ' && *ep != '\t')
		++ep;
	return str_n_from_c(cp, ep - cp);
}


static patch_ty *
diff_header(patch_context_ty *context)
{
    string_ty	    *line;
    int		    idx;
    patch_ty	    *result;
    string_ty	    *s;
    long	    b1, b2, a1, a2;
    char	    cmd;

    trace(("diff_header()\n{\n"));
    result = patch_new();

    //
    // Look for the optional index line.
    //
    line = patch_context_getline(context, 0);
    if (!line)
    {
	oops:
	patch_delete(result);
	trace(("return 0\n"));
	trace(("}\n"));
	return 0;
    }
    idx = 0;
    if (starts_with(line, "Index:"))
    {
	s = second_word(line);
	result->name.push_back(s);
	str_free(s);
	idx++;
    }

    //
    // Look for a diff -r line, with two file names on it.
    // This may be the only clue we get as to the file names.
    //
    line = patch_context_getline(context, idx);
    if (!line)
	goto oops;
    if (starts_with(line, "diff"))
    {
	string_list_ty	wl;
	size_t		j;

	wl.split(line, 0, true);
	for (j = 1; j < wl.nstrings; ++j)
	    if (wl.string[j]->str_text[0] != '-')
		break;
	if (j + 2 == wl.nstrings)
	{
	    static string_ty *dev_null;
	    if (!dev_null)
		dev_null = str_from_c("/dev/null");
	    s = wl.string[j];
	    if (!str_equal(s, dev_null))
		result->name.push_back(s);
	    s = wl.string[j + 1];
	    if (!str_equal(s, dev_null))
		result->name.push_back(s);

	    //
	    // Get next line, we've used this one.
	    //
	    ++idx;
	    line = patch_context_getline(context, idx);
	    if (!line)
		goto oops;
	}
    }

    //
    // Unlike "diff -u" or "diff -c", it is possible with this format to
    // have zero filenames.  This happens to aeannotate in particular.
    //
    // So you can't say
    //     if (result->name.nstrings == 0) goto oops;
    // at this point, HOWEVER other code needs at least one filename.
    //
    if (result->name.empty())
    {
	string_ty *nsf = str_from_c("(no file name present)");
	result->name.push_back(nsf);
	str_free(nsf);
    }

    //
    // Look for a line which contains one of our commands
    //
    if (!parse_command(line, &b1, &b2, &cmd, &a1, &a2))
	goto oops;

    //
    // Discard all of the header lines, except the command line
    // (it's actually part of the first hunk).
    //
    patch_context_discard(context, idx);
    trace(("return %08lX\n", (long)result));
    trace(("}\n"));
    return result;
}


static patch_hunk_ty *
diff_hunk(patch_context_ty *context)
{
    long	    before1;
    long	    before2;
    long	    after1;
    long	    after2;
    char	    cmd;
    string_ty	    *line;
    patch_hunk_ty   *php;
    int		    idx;
    long	    j;
    string_ty	    *value;

    trace(("diff_hunk()\n{\n"));
    line = patch_context_getline(context, 0);
    if (!line)
    {
	oops:
	trace(("return 0;\n}\n"));
	return 0;
    }
    if (!parse_command(line, &before1, &before2, &cmd, &after1, &after2))
	goto oops;
    idx = 1;

    php = patch_hunk_new();
    php->before.start_line_number = before1;
    php->after.start_line_number = after1;

    switch (cmd)
    {
    case 'a':
	php->before.start_line_number++;
	for (j = after1; j <= after2; ++j)
	{
	    line = patch_context_getline(context, idx++);
	    if (!line)
		goto oops;
	    if
	    (
		line->str_length < 2
	    ||
		line->str_text[0] != '>'
	    ||
		line->str_text[1] != ' '
	    )
		goto oops;
	    value = str_n_from_c(line->str_text + 2, line->str_length - 2);
	    patch_line_list_append
	    (
		&php->after,
		patch_line_type_inserted,
		value
	    );
	    str_free(value);
	}
	break;

    case 'c':
	for (j = before1; j <= before2; ++j)
	{
	    line = patch_context_getline(context, idx++);
	    if (!line)
		goto oops;
	    if
	    (
		line->str_length < 2
	    ||
		line->str_text[0] != '<'
	    ||
		line->str_text[1] != ' '
	    )
		goto oops;
	    value = str_n_from_c(line->str_text + 2, line->str_length - 2);
	    patch_line_list_append
	    (
		&php->before,
		patch_line_type_deleted,
		value
	    );
	    str_free(value);
	}
	line = patch_context_getline(context, idx++);
	if (!line)
	    goto oops;
        static string_ty *dash3;
	if (!dash3)
	    dash3 = str_from_c("---");
	if (!str_equal(line, dash3))
	    goto oops;
	for (j = after1; j <= after2; ++j)
	{
	    line = patch_context_getline(context, idx++);
	    if (!line)
		goto oops;
	    if
	    (
		line->str_length < 2
	    ||
		line->str_text[0] != '>'
	    ||
		line->str_text[1] != ' '
	    )
		goto oops;
	    value = str_n_from_c(line->str_text + 2, line->str_length - 2);
	    patch_line_list_append
	    (
		&php->after,
		patch_line_type_inserted,
		value
	    );
	    str_free(value);
	}
	break;

    case 'd':
	php->after.start_line_number++;
	for (j = before1; j <= before2; ++j)
	{
	    line = patch_context_getline(context, idx++);
	    if (!line)
		goto oops;
	    if
	    (
		line->str_length < 2
	    ||
		line->str_text[0] != '<'
	    ||
		line->str_text[1] != ' '
	    )
		goto oops;
	    value = str_n_from_c(line->str_text + 2, line->str_length - 2);
	    patch_line_list_append
	    (
		&php->before,
		patch_line_type_deleted,
		value
	    );
	    str_free(value);
	}
	break;

    default:
	assert(0);
	break;
    }

    //
    // We have a viable hunk, take them out of the context because
    // we won't need to backtrack them any more.
    //
    patch_context_discard(context, idx);

    trace(("return %08lX\n", (long)php));
    trace(("}\n"));
    return php;
}


patch_format_ty patch_format_diff =
{
    "diff",
    diff_header,
    diff_hunk,
};