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>
22 #include <err.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <diff/arraylist.h>
32 #include <diff/diff_main.h>
33 #include <diff/diff_output.h>
35 #ifdef __linux__
36 /* stupid shims to compile and test on linux */
37 #define __dead
39 static const char *getprogname()
40 {
41 return "diff";
42 }
43 #endif
45 __dead void usage(void);
46 int diffreg(char *, char *, bool, bool);
47 int openfile(const char *, char **, struct stat *);
49 __dead void
50 usage(void)
51 {
52 fprintf(stderr,
53 "usage: %s [-pw] file1 file2\n"
54 "\n"
55 " -p Use Patience Diff (slower but often nicer)\n"
56 " -w Ignore Whitespace\n"
57 , getprogname());
58 exit(1);
59 }
61 int
62 main(int argc, char *argv[])
63 {
64 int ch, rc;
65 bool do_patience = false, ignore_whitespace = false;
67 while ((ch = getopt(argc, argv, "pw")) != -1) {
68 switch (ch) {
69 case 'p':
70 do_patience = true;
71 break;
72 case 'w':
73 ignore_whitespace = true;
74 break;
75 default:
76 usage();
77 }
78 }
80 argc -= optind;
81 argv += optind;
83 if (argc != 2)
84 usage();
86 rc = diffreg(argv[0], argv[1], do_patience, ignore_whitespace);
87 if (rc != DIFF_RC_OK) {
88 fprintf(stderr, "diff: %s\n", strerror(rc));
89 return 1;
90 }
91 return 0;
92 }
94 const struct diff_algo_config myers_then_patience;
95 const struct diff_algo_config myers_then_myers_divide;
96 const struct diff_algo_config patience;
97 const struct diff_algo_config myers_divide;
99 const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
100 .impl = diff_algo_myers,
101 .permitted_state_size = 1024 * 1024 * sizeof(int),
102 .fallback_algo = &patience,
103 };
105 const struct diff_algo_config myers_then_myers_divide =
106 (struct diff_algo_config){
107 .impl = diff_algo_myers,
108 .permitted_state_size = 1024 * 1024 * sizeof(int),
109 .fallback_algo = &myers_divide,
110 };
112 const struct diff_algo_config patience = (struct diff_algo_config){
113 .impl = diff_algo_patience,
114 /* After subdivision, do Patience again: */
115 .inner_algo = &patience,
116 /* If subdivision failed, do Myers Divide et Impera: */
117 .fallback_algo = &myers_then_myers_divide,
118 };
120 const struct diff_algo_config myers_divide = (struct diff_algo_config){
121 .impl = diff_algo_myers_divide,
122 /* When division succeeded, start from the top: */
123 .inner_algo = &myers_then_myers_divide,
124 /* (fallback_algo = NULL implies diff_algo_none). */
125 };
127 const struct diff_config diff_config = {
128 .atomize_func = diff_atomize_text_by_line,
129 .algo = &myers_then_myers_divide,
130 };
132 const struct diff_config diff_config_patience = {
133 .atomize_func = diff_atomize_text_by_line,
134 .algo = &myers_then_patience,
135 };
137 int
138 diffreg(char *file1, char *file2, bool do_patience, bool ignore_whitespace)
140 char *str1, *str2;
141 int fd1, fd2;
142 struct stat st1, st2;
143 struct diff_input_info info = {
144 .left_path = file1,
145 .right_path = file2,
146 };
147 struct diff_result *result;
148 int rc;
149 const struct diff_config *cfg;
151 cfg = do_patience ? &diff_config_patience : &diff_config;
153 fd1 = openfile(file1, &str1, &st1);
154 fd2 = openfile(file2, &str2, &st2);
156 result = diff_main(cfg, fd1, str1, st1.st_size, fd2, str2, st2.st_size,
157 ignore_whitespace);
158 #if 0
159 rc = diff_output_plain(stdout, &info, result);
160 #else
161 rc = diff_output_unidiff(stdout, &info, result, 3);
162 #endif
163 diff_result_free(result);
165 if (str1)
166 munmap(str1, st1.st_size);
167 if (str2)
168 munmap(str2, st2.st_size);
169 close(fd1);
170 close(fd2);
172 return rc;
175 int
176 openfile(const char *path, char **p, struct stat *st)
178 int fd;
180 fd = open(path, O_RDONLY);
181 if (fd == -1)
182 err(2, "%s", path);
184 if (fstat(fd, st) == -1)
185 err(2, "%s", path);
187 #ifndef DIFF_NO_MMAP
188 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
189 if (*p == MAP_FAILED)
190 #endif
191 *p = NULL; /* fall back on file I/O */
193 return fd;