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
|
/* Commandline diff utility to test diff implementations. */
/*
* Copyright (c) 2018 Martin Pieuchot
* Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <arraylist.h>
#include <diff_main.h>
#include <diff_output.h>
enum diffreg_algo {
DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE = 0,
DIFFREG_ALGO_MYERS_THEN_PATIENCE = 1,
DIFFREG_ALGO_PATIENCE = 2,
DIFFREG_ALGO_NONE = 3,
};
__dead void usage(void);
int diffreg(char *, char *, enum diffreg_algo, bool, bool, bool,
int, bool);
FILE * openfile(const char *, char **, struct stat *);
__dead void
usage(void)
{
fprintf(stderr,
"usage: %s [-apPQTwe] [-U n] file1 file2\n"
"\n"
" -a Treat input as ASCII even if binary data is detected\n"
" -p Show function prototypes in hunk headers\n"
" -P Use Patience Diff (slower but often nicer)\n"
" -Q Use forward-Myers for small files, otherwise Patience\n"
" -T Trivial algo: detect similar start and end only\n"
" -w Ignore Whitespace\n"
" -U n Number of Context Lines\n"
" -e Produce ed script output\n"
, getprogname());
exit(1);
}
int
main(int argc, char *argv[])
{
int ch, rc;
bool force_text = false;
bool ignore_whitespace = false;
bool show_function_prototypes = false;
bool edscript = false;
int context_lines = 3;
enum diffreg_algo algo = DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE;
while ((ch = getopt(argc, argv, "apPQTwU:e")) != -1) {
switch (ch) {
case 'a':
force_text = true;
break;
case 'p':
show_function_prototypes = true;
break;
case 'P':
algo = DIFFREG_ALGO_PATIENCE;
break;
case 'Q':
algo = DIFFREG_ALGO_MYERS_THEN_PATIENCE;
break;
case 'T':
algo = DIFFREG_ALGO_NONE;
break;
case 'w':
ignore_whitespace = true;
break;
case 'U':
context_lines = atoi(optarg);
break;
case 'e':
edscript = true;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 2)
usage();
rc = diffreg(argv[0], argv[1], algo, force_text, ignore_whitespace,
show_function_prototypes, context_lines, edscript);
if (rc != DIFF_RC_OK) {
fprintf(stderr, "diff: %s\n", strerror(rc));
return 1;
}
return 0;
}
const struct diff_algo_config myers_then_patience;
const struct diff_algo_config myers_then_myers_divide;
const struct diff_algo_config patience;
const struct diff_algo_config myers_divide;
const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
.impl = diff_algo_myers,
.permitted_state_size = 1024 * 1024 * sizeof(int),
.fallback_algo = &patience,
};
const struct diff_algo_config myers_then_myers_divide =
(struct diff_algo_config){
.impl = diff_algo_myers,
.permitted_state_size = 1024 * 1024 * sizeof(int),
.fallback_algo = &myers_divide,
};
const struct diff_algo_config patience = (struct diff_algo_config){
.impl = diff_algo_patience,
/* After subdivision, do Patience again: */
.inner_algo = &patience,
/* If subdivision failed, do Myers Divide et Impera: */
.fallback_algo = &myers_then_myers_divide,
};
const struct diff_algo_config myers_divide = (struct diff_algo_config){
.impl = diff_algo_myers_divide,
/* When division succeeded, start from the top: */
.inner_algo = &myers_then_myers_divide,
/* (fallback_algo = NULL implies diff_algo_none). */
};
const struct diff_algo_config no_algo = (struct diff_algo_config){
.impl = diff_algo_none,
};
/* If the state for a forward-Myers is small enough, use Myers, otherwise first
* do a Myers-divide. */
const struct diff_config diff_config_myers_then_myers_divide = {
.atomize_func = diff_atomize_text_by_line,
.algo = &myers_then_myers_divide,
};
/* If the state for a forward-Myers is small enough, use Myers, otherwise first
* do a Patience. */
const struct diff_config diff_config_myers_then_patience = {
.atomize_func = diff_atomize_text_by_line,
.algo = &myers_then_patience,
};
/* Directly force Patience as a first divider of the source file. */
const struct diff_config diff_config_patience = {
.atomize_func = diff_atomize_text_by_line,
.algo = &patience,
};
/* Directly force Patience as a first divider of the source file. */
const struct diff_config diff_config_no_algo = {
.atomize_func = diff_atomize_text_by_line,
};
int
diffreg(char *file1, char *file2, enum diffreg_algo algo, bool force_text,
bool ignore_whitespace, bool show_function_prototypes, int context_lines,
bool edscript)
{
char *str1, *str2;
FILE *f1, *f2;
struct stat st1, st2;
struct diff_input_info info = {
.left_path = file1,
.right_path = file2,
};
struct diff_data left = {}, right = {};
struct diff_result *result = NULL;
int rc;
const struct diff_config *cfg;
int diff_flags = 0;
switch (algo) {
default:
case DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE:
cfg = &diff_config_myers_then_myers_divide;
break;
case DIFFREG_ALGO_MYERS_THEN_PATIENCE:
cfg = &diff_config_myers_then_patience;
break;
case DIFFREG_ALGO_PATIENCE:
cfg = &diff_config_patience;
break;
case DIFFREG_ALGO_NONE:
cfg = &diff_config_no_algo;
break;
}
f1 = openfile(file1, &str1, &st1);
f2 = openfile(file2, &str2, &st2);
if (force_text)
diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
if (ignore_whitespace)
diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
if (show_function_prototypes)
diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
rc = diff_atomize_file(&left, cfg, f1, str1, st1.st_size, diff_flags);
if (rc)
goto done;
rc = diff_atomize_file(&right, cfg, f2, str2, st2.st_size, diff_flags);
if (rc)
goto done;
result = diff_main(cfg, &left, &right);
#if 0
rc = diff_output_plain(stdout, &info, result);
#else
if (edscript)
rc = diff_output_edscript(NULL, stdout, &info, result);
else {
rc = diff_output_unidiff(NULL, stdout, &info, result,
context_lines);
}
#endif
done:
diff_result_free(result);
diff_data_free(&left);
diff_data_free(&right);
if (str1)
munmap(str1, st1.st_size);
if (str2)
munmap(str2, st2.st_size);
fclose(f1);
fclose(f2);
return rc;
}
FILE *
openfile(const char *path, char **p, struct stat *st)
{
FILE *f = NULL;
f = fopen(path, "r");
if (f == NULL)
err(2, "%s", path);
if (fstat(fileno(f), st) == -1)
err(2, "%s", path);
#ifndef DIFF_NO_MMAP
*p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
if (*p == MAP_FAILED)
#endif
*p = NULL; /* fall back on file I/O */
return f;
}
|