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.
21 #include <sys/types.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 int openfile(const char *, char **, struct stat *);
44 "usage: %s [-pw] [-C n] file1 file2\n"
46 " -p Use Patience Diff (slower but often nicer)\n"
47 " -w Ignore Whitespace\n"
48 " -C n Number of Context Lines\n"
54 main(int argc, char *argv[])
57 bool do_patience = false, ignore_whitespace = false;
58 int context_lines = 3;
60 while ((ch = getopt(argc, argv, "pwC:")) != -1) {
66 ignore_whitespace = true;
69 context_lines = atoi(optarg);
82 rc = diffreg(argv[0], argv[1], do_patience, ignore_whitespace,
84 if (rc != DIFF_RC_OK) {
85 fprintf(stderr, "diff: %s\n", strerror(rc));
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,
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,
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,
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). */
124 const struct diff_config diff_config = {
125 .atomize_func = diff_atomize_text_by_line,
126 .algo = &myers_then_myers_divide,
129 const struct diff_config diff_config_patience = {
130 .atomize_func = diff_atomize_text_by_line,
131 .algo = &myers_then_patience,
135 diffreg(char *file1, char *file2, bool do_patience, bool ignore_whitespace,
140 struct stat st1, st2;
141 struct diff_input_info info = {
145 struct diff_result *result;
147 const struct diff_config *cfg;
149 cfg = do_patience ? &diff_config_patience : &diff_config;
151 fd1 = openfile(file1, &str1, &st1);
152 fd2 = openfile(file2, &str2, &st2);
154 result = diff_main(cfg, fd1, str1, st1.st_size, fd2, str2, st2.st_size,
157 rc = diff_output_plain(stdout, &info, result);
159 rc = diff_output_unidiff(stdout, &info, result, context_lines);
161 diff_result_free(result);
164 munmap(str1, st1.st_size);
166 munmap(str2, st2.st_size);
174 openfile(const char *path, char **p, struct stat *st)
178 fd = open(path, O_RDONLY);
182 if (fstat(fd, st) == -1)
186 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
187 if (*p == MAP_FAILED)
189 *p = NULL; /* fall back on file I/O */