1 /* Commandline diff utility to test diff implementations. */
3 * Copyright (c) 2018 Martin Pieuchot
4 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
31 #include <diff/arraylist.h>
32 #include <diff/diff_main.h>
33 #include <diff/diff_output.h>
36 /* stupid shims to compile and test on linux */
39 static const char *getprogname()
45 __dead void usage(void);
46 int diffreg(char *, char *, bool, bool, int);
47 int openfile(const char *, char **, struct stat *);
53 "usage: %s [-pw] [-C n] file1 file2\n"
55 " -p Use Patience Diff (slower but often nicer)\n"
56 " -w Ignore Whitespace\n"
57 " -C n Number of Context Lines\n"
63 main(int argc, char *argv[])
66 bool do_patience = false, ignore_whitespace = false;
67 int context_lines = 3;
69 while ((ch = getopt(argc, argv, "pwC:")) != -1) {
75 ignore_whitespace = true;
78 context_lines = atoi(optarg);
91 rc = diffreg(argv[0], argv[1], do_patience, ignore_whitespace,
93 if (rc != DIFF_RC_OK) {
94 fprintf(stderr, "diff: %s\n", strerror(rc));
100 const struct diff_algo_config myers_then_patience;
101 const struct diff_algo_config myers_then_myers_divide;
102 const struct diff_algo_config patience;
103 const struct diff_algo_config myers_divide;
105 const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
106 .impl = diff_algo_myers,
107 .permitted_state_size = 1024 * 1024 * sizeof(int),
108 .fallback_algo = &patience,
111 const struct diff_algo_config myers_then_myers_divide =
112 (struct diff_algo_config){
113 .impl = diff_algo_myers,
114 .permitted_state_size = 1024 * 1024 * sizeof(int),
115 .fallback_algo = &myers_divide,
118 const struct diff_algo_config patience = (struct diff_algo_config){
119 .impl = diff_algo_patience,
120 /* After subdivision, do Patience again: */
121 .inner_algo = &patience,
122 /* If subdivision failed, do Myers Divide et Impera: */
123 .fallback_algo = &myers_then_myers_divide,
126 const struct diff_algo_config myers_divide = (struct diff_algo_config){
127 .impl = diff_algo_myers_divide,
128 /* When division succeeded, start from the top: */
129 .inner_algo = &myers_then_myers_divide,
130 /* (fallback_algo = NULL implies diff_algo_none). */
133 const struct diff_config diff_config = {
134 .atomize_func = diff_atomize_text_by_line,
135 .algo = &myers_then_myers_divide,
138 const struct diff_config diff_config_patience = {
139 .atomize_func = diff_atomize_text_by_line,
140 .algo = &myers_then_patience,
144 diffreg(char *file1, char *file2, bool do_patience, bool ignore_whitespace,
149 struct stat st1, st2;
150 struct diff_input_info info = {
154 struct diff_result *result;
156 const struct diff_config *cfg;
158 cfg = do_patience ? &diff_config_patience : &diff_config;
160 fd1 = openfile(file1, &str1, &st1);
161 fd2 = openfile(file2, &str2, &st2);
163 result = diff_main(cfg, fd1, str1, st1.st_size, fd2, str2, st2.st_size,
166 rc = diff_output_plain(stdout, &info, result);
168 rc = diff_output_unidiff(stdout, &info, result, context_lines);
170 diff_result_free(result);
173 munmap(str1, st1.st_size);
175 munmap(str2, st2.st_size);
183 openfile(const char *path, char **p, struct stat *st)
187 fd = open(path, O_RDONLY);
191 if (fstat(fd, st) == -1)
195 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
196 if (*p == MAP_FAILED)
198 *p = NULL; /* fall back on file I/O */