Blob


1 /* Commandline diff utility to test diff implementations. */
2 /*
3 * Copyright (c) 2018 Martin Pieuchot
4 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <err.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include <arraylist.h>
33 #include <diff_main.h>
34 #include <diff_output.h>
36 enum diffreg_algo {
37 DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE = 0,
38 DIFFREG_ALGO_MYERS_THEN_PATIENCE = 1,
39 DIFFREG_ALGO_PATIENCE = 2,
40 DIFFREG_ALGO_NONE = 3,
41 };
43 __dead void usage(void);
44 int diffreg(char *, char *, enum diffreg_algo, bool, bool,
45 int, bool);
46 FILE * openfile(const char *, char **, struct stat *);
48 __dead void
49 usage(void)
50 {
51 fprintf(stderr,
52 "usage: %s [-pPQTwe] [-U n] file1 file2\n"
53 "\n"
54 " -p Show function prototypes in hunk headers\n"
55 " -P Use Patience Diff (slower but often nicer)\n"
56 " -Q Use forward-Myers for small files, otherwise Patience\n"
57 " -T Trivial algo: detect similar start and end only\n"
58 " -w Ignore Whitespace\n"
59 " -U n Number of Context Lines\n"
60 " -e Produce ed script output\n"
61 , getprogname());
62 exit(1);
63 }
65 int
66 main(int argc, char *argv[])
67 {
68 int ch, rc;
69 bool ignore_whitespace = false;
70 bool show_function_prototypes = false;
71 bool edscript = false;
72 int context_lines = 3;
73 enum diffreg_algo algo = DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE;
75 while ((ch = getopt(argc, argv, "pPQTwU:e")) != -1) {
76 switch (ch) {
77 case 'p':
78 show_function_prototypes = true;
79 break;
80 case 'P':
81 algo = DIFFREG_ALGO_PATIENCE;
82 break;
83 case 'Q':
84 algo = DIFFREG_ALGO_MYERS_THEN_PATIENCE;
85 break;
86 case 'T':
87 algo = DIFFREG_ALGO_NONE;
88 break;
89 case 'w':
90 ignore_whitespace = true;
91 break;
92 case 'U':
93 context_lines = atoi(optarg);
94 break;
95 case 'e':
96 edscript = true;
97 break;
98 default:
99 usage();
103 argc -= optind;
104 argv += optind;
106 if (argc != 2)
107 usage();
109 rc = diffreg(argv[0], argv[1], algo, ignore_whitespace,
110 show_function_prototypes, context_lines, edscript);
111 if (rc != DIFF_RC_OK) {
112 fprintf(stderr, "diff: %s\n", strerror(rc));
113 return 1;
115 return 0;
118 const struct diff_algo_config myers_then_patience;
119 const struct diff_algo_config myers_then_myers_divide;
120 const struct diff_algo_config patience;
121 const struct diff_algo_config myers_divide;
123 const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
124 .impl = diff_algo_myers,
125 .permitted_state_size = 1024 * 1024 * sizeof(int),
126 .fallback_algo = &patience,
127 };
129 const struct diff_algo_config myers_then_myers_divide =
130 (struct diff_algo_config){
131 .impl = diff_algo_myers,
132 .permitted_state_size = 1024 * 1024 * sizeof(int),
133 .fallback_algo = &myers_divide,
134 };
136 const struct diff_algo_config patience = (struct diff_algo_config){
137 .impl = diff_algo_patience,
138 /* After subdivision, do Patience again: */
139 .inner_algo = &patience,
140 /* If subdivision failed, do Myers Divide et Impera: */
141 .fallback_algo = &myers_then_myers_divide,
142 };
144 const struct diff_algo_config myers_divide = (struct diff_algo_config){
145 .impl = diff_algo_myers_divide,
146 /* When division succeeded, start from the top: */
147 .inner_algo = &myers_then_myers_divide,
148 /* (fallback_algo = NULL implies diff_algo_none). */
149 };
151 const struct diff_algo_config no_algo = (struct diff_algo_config){
152 .impl = diff_algo_none,
153 };
155 /* If the state for a forward-Myers is small enough, use Myers, otherwise first
156 * do a Myers-divide. */
157 const struct diff_config diff_config_myers_then_myers_divide = {
158 .atomize_func = diff_atomize_text_by_line,
159 .algo = &myers_then_myers_divide,
160 };
162 /* If the state for a forward-Myers is small enough, use Myers, otherwise first
163 * do a Patience. */
164 const struct diff_config diff_config_myers_then_patience = {
165 .atomize_func = diff_atomize_text_by_line,
166 .algo = &myers_then_patience,
167 };
169 /* Directly force Patience as a first divider of the source file. */
170 const struct diff_config diff_config_patience = {
171 .atomize_func = diff_atomize_text_by_line,
172 .algo = &patience,
173 };
175 /* Directly force Patience as a first divider of the source file. */
176 const struct diff_config diff_config_no_algo = {
177 .atomize_func = diff_atomize_text_by_line,
178 };
180 int
181 diffreg(char *file1, char *file2, enum diffreg_algo algo, bool ignore_whitespace,
182 bool show_function_prototypes, int context_lines, bool edscript)
184 char *str1, *str2;
185 FILE *f1, *f2;
186 struct stat st1, st2;
187 struct diff_input_info info = {
188 .left_path = file1,
189 .right_path = file2,
190 };
191 struct diff_result *result;
192 int rc;
193 const struct diff_config *cfg;
194 int diff_flags = 0;
196 switch (algo) {
197 default:
198 case DIFFREG_ALGO_MYERS_THEN_MYERS_DIVIDE:
199 cfg = &diff_config_myers_then_myers_divide;
200 break;
201 case DIFFREG_ALGO_MYERS_THEN_PATIENCE:
202 cfg = &diff_config_myers_then_patience;
203 break;
204 case DIFFREG_ALGO_PATIENCE:
205 cfg = &diff_config_patience;
206 break;
207 case DIFFREG_ALGO_NONE:
208 cfg = &diff_config_no_algo;
209 break;
212 f1 = openfile(file1, &str1, &st1);
213 f2 = openfile(file2, &str2, &st2);
215 if (ignore_whitespace)
216 diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
217 if (show_function_prototypes)
218 diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
220 result = diff_main(cfg, f1, str1, st1.st_size, f2, str2, st2.st_size,
221 diff_flags);
222 #if 0
223 rc = diff_output_plain(stdout, &info, result);
224 #else
225 if (edscript)
226 rc = diff_output_edscript(NULL, stdout, &info, result);
227 else {
228 rc = diff_output_unidiff(NULL, stdout, &info, result,
229 context_lines);
231 #endif
232 diff_result_free(result);
234 if (str1)
235 munmap(str1, st1.st_size);
236 if (str2)
237 munmap(str2, st2.st_size);
238 fclose(f1);
239 fclose(f2);
241 return rc;
244 FILE *
245 openfile(const char *path, char **p, struct stat *st)
247 FILE *f = NULL;
249 f = fopen(path, "r");
250 if (f == NULL)
251 err(2, "%s", path);
253 if (fstat(fileno(f), st) == -1)
254 err(2, "%s", path);
256 #ifndef DIFF_NO_MMAP
257 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
258 if (*p == MAP_FAILED)
259 #endif
260 *p = NULL; /* fall back on file I/O */
262 return f;