1 | /* |
2 | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
5 | * |
6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
7 | */ |
8 | |
9 | #include "monetdb_config.h" |
10 | |
11 | #include "difflib.h" |
12 | #ifdef HAVE_IO_H |
13 | # include <io.h> |
14 | #endif |
15 | #include <string.h> |
16 | |
17 | #ifdef HAVE_GETOPT |
18 | #ifdef HAVE_GETOPT_H |
19 | #include <getopt.h> |
20 | #endif |
21 | #else |
22 | #include "testing_getopt.c" |
23 | #endif |
24 | |
25 | static void |
26 | showUsage(char *name) |
27 | { |
28 | printf("Usage: %s [-I<exp>] [-F<exp>] [-C<num>] [-A<num>] [-t<text>] [-r<rev>] <oldfile> <newfile> [<outfile>]\n" , name); |
29 | printf("\n" ); |
30 | printf(" -I<exp> : ignore lines matching <exp> during diff (optional, default: -I'^#')\n" ); |
31 | printf(" -F<exp> : show the most recent line matching <exp>\n" ); |
32 | printf(" -C<num> : use <num> lines of context during diff (optional, default: -C1)\n" ); |
33 | printf(" -A<num> : accuracy for diff: 0=lines, 1=words, 2=chars (optional, default: -A1)\n" ); |
34 | printf(" -d : change the algorithm to perhaps find a smaller set of changes;\n" ); |
35 | printf(" this makes diff slower (sometimes much slower)\n" ); |
36 | printf(" -t<text> : text for caption (optional, default: empty)\n" ); |
37 | printf(" -r<rev> : revision of old file (optional, default: empty)\n" ); |
38 | printf(" -q : be less verbose\n" ); |
39 | printf(" <oldfile> : first file for diff\n" ); |
40 | printf(" <newfile> : second file for diff\n" ); |
41 | printf(" <outfile> : file for HTML output (optional, default: stdout)\n" ); |
42 | } |
43 | |
44 | int |
45 | main(int argc, char **argv) |
46 | { |
47 | char EMPTY[] = "" ; |
48 | #ifdef NATIVE_WIN32 |
49 | char DEFAULT[] = "\"-I^#\"" ; |
50 | #else |
51 | char DEFAULT[] = "-I'^#'" ; |
52 | #endif |
53 | char ignoreWHITE[] = " -b -B" ; |
54 | char *old_fn, *new_fn, *html_fn, *caption = EMPTY, *revision = EMPTY, *ignoreEXP = DEFAULT, *ignore = NULL, *function = EMPTY; |
55 | int LWC = 1, context = 1, option, mindiff = 0, quiet = 0; |
56 | |
57 | while ((option = getopt(argc, argv, "hdqA:C:I:F:t:r:" )) != EOF) |
58 | switch (option) { |
59 | case 'd': |
60 | mindiff = 1; |
61 | break; |
62 | case 'A': |
63 | LWC = atoi(optarg); |
64 | break; |
65 | case 'C': |
66 | context = atoi(optarg); |
67 | |
68 | break; |
69 | case 'I': |
70 | ignoreEXP = (char *) malloc(strlen(optarg) + 6); |
71 | #ifdef NATIVE_WIN32 |
72 | strcpy(ignoreEXP, "\"-I" ); |
73 | #else |
74 | strcpy(ignoreEXP, "'-I" ); |
75 | #endif |
76 | strcat(ignoreEXP, optarg); |
77 | #ifdef NATIVE_WIN32 |
78 | strcat(ignoreEXP, "\"" ); |
79 | #else |
80 | strcat(ignoreEXP, "'" ); |
81 | #endif |
82 | break; |
83 | case 'F': |
84 | function = malloc(strlen(optarg) + 6); |
85 | #ifdef NATIVE_WIN32 |
86 | strcpy(function, "\"-F" ); |
87 | #else |
88 | strcpy(function, "'-F" ); |
89 | #endif |
90 | strcat(function, optarg); |
91 | #ifdef NATIVE_WIN32 |
92 | strcat(function, "\"" ); |
93 | #else |
94 | strcat(function, "'" ); |
95 | #endif |
96 | break; |
97 | case 't': |
98 | caption = optarg; |
99 | break; |
100 | case 'r': |
101 | revision = optarg; |
102 | break; |
103 | case 'q': |
104 | quiet = 1; |
105 | break; |
106 | case 'h': |
107 | default: |
108 | showUsage(argv[0]); |
109 | if (ignoreEXP != DEFAULT) |
110 | free(ignoreEXP); |
111 | if (function != EMPTY) |
112 | free(function); |
113 | exit(1); |
114 | } |
115 | |
116 | ignore = (char *) malloc(strlen(ignoreEXP) + strlen(ignoreWHITE) + 2); |
117 | strcpy(ignore, ignoreEXP); |
118 | strcat(ignore, ignoreWHITE); |
119 | |
120 | optind--; |
121 | old_fn = ((argc > (++optind)) ? argv[optind] : "-" ); |
122 | new_fn = ((argc > (++optind)) ? argv[optind] : "-" ); |
123 | html_fn = ((argc > (++optind)) ? argv[optind] : "-" ); |
124 | |
125 | TRACE(fprintf(STDERR, "%s %s -A %i -C %i %s %s -t %s -r %s %s %s %s\n" , argv[0], mindiff ? "-d" : "" , LWC, context, ignore, function, caption, revision, old_fn, new_fn, html_fn)); |
126 | |
127 | switch (oldnew2html(mindiff, LWC, context, ignore, function, old_fn, new_fn, html_fn, caption, revision)) { |
128 | case 0: |
129 | if (quiet == 0) |
130 | fprintf(STDERR, "%s and %s are equal.\n" , old_fn, new_fn); |
131 | break; |
132 | case 1: |
133 | if (quiet != 0) { |
134 | fprintf(STDERR, "\n+ (%s) slightly\n" , new_fn); |
135 | } else { |
136 | fprintf(STDERR, "%s and %s differ slightly.\n" , old_fn, new_fn); |
137 | } |
138 | break; |
139 | case 2: |
140 | if (quiet != 0) { |
141 | fprintf(STDERR, "\n* (%s) significantly\n" , new_fn); |
142 | } else { |
143 | fprintf(STDERR, "%s and %s differ SIGNIFICANTLY!\n" , old_fn, new_fn); |
144 | } |
145 | break; |
146 | } |
147 | free(ignore); |
148 | if (ignoreEXP != DEFAULT) |
149 | free(ignoreEXP); |
150 | if (function != EMPTY) |
151 | free(function); |
152 | |
153 | TRACE(fprintf(STDERR, "done.\n" )); |
154 | return 0; |
155 | } |
156 | |