-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdiff.c
245 lines (199 loc) · 5.05 KB
/
cdiff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Colorize (adds ANSI codes) output of diff.
* Copyright (c) 2005,2006 by Michal Nazareicz <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* This is part of Tiny Applications Collection
* -> http://tinyapps.sourceforge.net/
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/******************** Main ***************************************************/
static void not_tty(char **argv);
static int run_diff(char **argv);
static void loop(void);
int main(int argc, char **argv) {
/* If not TTY either run diff or pass in->out through (cat) */
if (!isatty(1)) {
not_tty(argv);
return 1;
}
/* If arguments given run diff and prepare pipe */
if (argc > 1) {
int ret = run_diff(argv);
if (ret) {
return ret;
}
}
/* Colourise output */
loop();
return 0;
}
/******************** Handle no TTY case *************************************/
static void not_tty(char **argv) {
argv[0] = (char *)(argv[1] ? "diff" : "cat");
execvp(argv[0], argv);
perror(argv[1] ? "exec: diff" : "exec: cat");
}
/******************** Run diff and set up pipe *******************************/
static int run_diff(char **argv) {
int fds[2];
if (pipe(fds) < 0) {
perror("pipe");
return 1;
}
switch (fork()) {
case -1:
perror("fork");
return 1;
case 0:
close(fds[0]);
if (dup2(fds[1], 1) < 0) {
perror("dup2");
return 1;
}
argv[0] = (char *)"diff";
execvp("diff", argv);
perror("exec: diff");
return 1;
default:
close(fds[1]);
if (dup2(fds[0], 0) < 0) {
perror("dup2");
return 1;
}
}
return 0;
}
/******************** Main loop; main part of the app ************************/
struct pattern {
const char pattern[7];
unsigned char idx;
};
static unsigned find(const struct pattern *patterns, const char *str);
static void loop(void) {
enum color_number {
COLOR_DEFAULT, /* must be zero */
COLOR_INS,
COLOR_DEL,
COLOR_CHANGE,
COLOR_EQUAL,
COLOR_FILE1,
COLOR_FILE2,
COLOR_POS1,
COLOR_POS2,
COLOR_MISC
};
static const char colors[][8] = {
[COLOR_DEFAULT] = "0;37",
[COLOR_INS] = "1;32",
[COLOR_DEL] = "1;31",
[COLOR_CHANGE] = "1;33",
[COLOR_EQUAL] = "0;37",
[COLOR_FILE1] = "0;32",
[COLOR_FILE2] = "0;31",
[COLOR_POS1] = "0;32",
[COLOR_POS2] = "0;31",
[COLOR_MISC] = "0;35",
};
static const struct pattern unified_ruleset[] = {
{ "+++ " , COLOR_FILE1 },
{ "--- " , COLOR_FILE2 },
{ "@@ " , COLOR_MISC },
{ "+" , COLOR_INS },
{ "-" , COLOR_DEL },
{ " " , COLOR_EQUAL },
{ "" , COLOR_DEFAULT },
};
static const struct pattern contex_ruleset[] = {
{ "*** 0", COLOR_POS1 },
{ "--- 0", COLOR_POS2 },
{ "*** " , COLOR_FILE1 },
{ "--- " , COLOR_FILE2 },
{ "*" , COLOR_MISC },
{ "+ " , COLOR_INS },
{ "- " , COLOR_DEL },
{ "! " , COLOR_CHANGE },
{ " " , COLOR_EQUAL },
{ "? " , COLOR_DEFAULT },
{ "??" , COLOR_MISC },
{ "" , COLOR_DEFAULT },
};
static const struct pattern normal_ruleset[] = {
{ "0" , COLOR_MISC },
{ "---" , COLOR_MISC },
{ "> " , COLOR_INS },
{ "< " , COLOR_DEL },
{ "" , COLOR_DEFAULT },
};
static const struct pattern *rulesets[] = {
0, /* must be at index zero */
unified_ruleset,
contex_ruleset,
normal_ruleset
};
static const struct pattern modes[] = {
{ "-", 1 },
{ "*", 2 },
{ "0", 3 },
{ "", 0 } /* side note: 0 == COLOR_DEFAULT */
};
const struct pattern *ruleset = 0;
char buf[4096];
while (fgets(buf, sizeof buf, stdin)) {
unsigned idx;
char *ch;
do {
idx = find(ruleset ? ruleset : modes, buf);
} while (!ruleset && (ruleset = rulesets[idx]));
printf("\x1b[%sm", colors[idx]);
do {
ch = strchr(buf, '\n');
if (ch) {
*ch = '\0';
}
fputs(buf, stdout);
} while (!ch && fgets(buf, sizeof buf, stdin));
puts("\x1b[0m");
}
}
/******************** String matching ****************************************/
static bool match(const char *pattern, const char *str);
static unsigned find(const struct pattern *patterns, const char *str) {
while (!match(patterns->pattern, str)) {
++patterns;
}
return patterns->idx;
}
static bool match(const char *pat, const char *str) {
for (;;) {
switch (*pat) {
case 0:
return true;
case '?':
if (!*str) return false;
break;
case '0':
if (*str < '0' || '9' < *str) return false;
break;
default:
if (*pat != *str) return false;
}
++pat;
++str;
}
}