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 <diff/arraylist.h>
33 #include <diff/diff_main.h>
34 #include <diff/diff_output.h>
36 __dead void usage(void);
37 int diffreg(char *, char *, bool, bool, int);
38 FILE * openfile(const char *, char **, struct stat *);
40 __dead void
41 usage(void)
42 {
43 fprintf(stderr,
44 "usage: %s [-pw] [-C n] file1 file2\n"
45 "\n"
46 " -p Use Patience Diff (slower but often nicer)\n"
47 " -w Ignore Whitespace\n"
48 " -C n Number of Context Lines\n"
49 , getprogname());
50 exit(1);
51 }
53 int
54 main(int argc, char *argv[])
55 {
56 int ch, rc;
57 bool do_patience = false, ignore_whitespace = false;
58 int context_lines = 3;
60 while ((ch = getopt(argc, argv, "pwC:")) != -1) {
61 switch (ch) {
62 case 'p':
63 do_patience = true;
64 break;
65 case 'w':
66 ignore_whitespace = true;
67 break;
68 case 'C':
69 context_lines = atoi(optarg);
70 break;
71 default:
72 usage();
73 }
74 }
76 argc -= optind;
77 argv += optind;
79 if (argc != 2)
80 usage();
82 rc = diffreg(argv[0], argv[1], do_patience, ignore_whitespace,
83 context_lines);
84 if (rc != DIFF_RC_OK) {
85 fprintf(stderr, "diff: %s\n", strerror(rc));
86 return 1;
87 }
88 return 0;
89 }
91 const struct diff_algo_config myers_then_patience;
92 const struct diff_algo_config myers_then_myers_divide;
93 const struct diff_algo_config patience;
94 const struct diff_algo_config myers_divide;
96 const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
97 .impl = diff_algo_myers,
98 .permitted_state_size = 1024 * 1024 * sizeof(int),
99 .fallback_algo = &patience,
100 };
102 const struct diff_algo_config myers_then_myers_divide =
103 (struct diff_algo_config){
104 .impl = diff_algo_myers,
105 .permitted_state_size = 1024 * 1024 * sizeof(int),
106 .fallback_algo = &myers_divide,
107 };
109 const struct diff_algo_config patience = (struct diff_algo_config){
110 .impl = diff_algo_patience,
111 /* After subdivision, do Patience again: */
112 .inner_algo = &patience,
113 /* If subdivision failed, do Myers Divide et Impera: */
114 .fallback_algo = &myers_then_myers_divide,
115 };
117 const struct diff_algo_config myers_divide = (struct diff_algo_config){
118 .impl = diff_algo_myers_divide,
119 /* When division succeeded, start from the top: */
120 .inner_algo = &myers_then_myers_divide,
121 /* (fallback_algo = NULL implies diff_algo_none). */
122 };
124 const struct diff_config diff_config = {
125 .atomize_func = diff_atomize_text_by_line,
126 .algo = &myers_then_myers_divide,
127 };
129 const struct diff_config diff_config_patience = {
130 .atomize_func = diff_atomize_text_by_line,
131 .algo = &myers_then_patience,
132 };
134 int
135 diffreg(char *file1, char *file2, bool do_patience, bool ignore_whitespace,
136 int context_lines)
138 char *str1, *str2;
139 FILE *f1, *f2;
140 struct stat st1, st2;
141 struct diff_input_info info = {
142 .left_path = file1,
143 .right_path = file2,
144 };
145 struct diff_result *result;
146 int rc;
147 const struct diff_config *cfg;
148 int diff_flags = 0;
150 cfg = do_patience ? &diff_config_patience : &diff_config;
152 f1 = openfile(file1, &str1, &st1);
153 f2 = openfile(file2, &str2, &st2);
155 if (ignore_whitespace)
156 diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
158 result = diff_main(cfg, f1, str1, st1.st_size, f2, str2, st2.st_size,
159 diff_flags);
160 #if 0
161 rc = diff_output_plain(stdout, &info, result);
162 #else
163 rc = diff_output_unidiff(NULL, stdout, &info, result, context_lines);
164 #endif
165 diff_result_free(result);
167 if (str1)
168 munmap(str1, st1.st_size);
169 if (str2)
170 munmap(str2, st2.st_size);
171 fclose(f1);
172 fclose(f2);
174 return rc;
177 FILE *
178 openfile(const char *path, char **p, struct stat *st)
180 FILE *f = NULL;
182 f = fopen(path, "r");
183 if (f == NULL)
184 err(2, "%s", path);
186 if (fstat(fileno(f), st) == -1)
187 err(2, "%s", path);
189 #ifndef DIFF_NO_MMAP
190 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
191 if (*p == MAP_FAILED)
192 #endif
193 *p = NULL; /* fall back on file I/O */
195 return f;