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 *, int);
47 int openfile(const char *, char **, struct stat *);
49 __dead void
50 usage(void)
51 {
52 fprintf(stderr,
53 "usage: %s [-p] file1 file2\n"
54 "\n"
55 " -p Use Patience Diff (slower but often nicer)\n"
56 , getprogname());
57 exit(1);
58 }
60 static bool do_patience = false;
62 int
63 main(int argc, char *argv[])
64 {
65 int ch, rc;
67 while ((ch = getopt(argc, argv, "p")) != -1) {
68 switch (ch) {
69 case 'p':
70 do_patience = true;
71 break;
72 default:
73 usage();
74 }
75 }
77 argc -= optind;
78 argv += optind;
80 if (argc != 2)
81 usage();
83 rc = diffreg(argv[0], argv[1], 0);
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, int flags)
137 char *str1, *str2;
138 int fd1, fd2;
139 struct stat st1, st2;
140 struct diff_input_info info = {
141 .left_path = file1,
142 .right_path = file2,
143 };
144 struct diff_result *result;
145 int rc;
146 const struct diff_config *cfg;
148 cfg = do_patience ? &diff_config_patience : &diff_config;
150 fd1 = openfile(file1, &str1, &st1);
151 fd2 = openfile(file2, &str2, &st2);
153 result = diff_main(cfg, fd1, str1, st1.st_size, fd2, str2, st2.st_size);
154 #if 0
155 rc = diff_output_plain(stdout, &info, result);
156 #else
157 rc = diff_output_unidiff(stdout, &info, result, 3);
158 #endif
159 diff_result_free(result);
161 if (str1)
162 munmap(str1, st1.st_size);
163 if (str2)
164 munmap(str2, st2.st_size);
165 close(fd1);
166 close(fd2);
168 return rc;
171 int
172 openfile(const char *path, char **p, struct stat *st)
174 int fd;
176 fd = open(path, O_RDONLY);
177 if (fd == -1)
178 err(2, "%s", path);
180 if (fstat(fd, st) == -1)
181 err(2, "%s", path);
183 #ifndef DIFF_NO_MMAP
184 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
185 if (*p == MAP_FAILED)
186 #endif
187 *p = NULL; /* fall back on file I/O */
189 return fd;