-
Notifications
You must be signed in to change notification settings - Fork 1
/
idiff.c
393 lines (343 loc) · 7.65 KB
/
idiff.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Synopsis:
* Interactive diff of two US_ASCII text files, inspiration from Pike book.
* Usage:
* idiff file1 file2
* Exit Status:
* 0 gracefull exit
* <sig> signal of interupted idiff
* 127 unknown error
* History:
* Derived by John Scott from example in 'Unix Programming Environment'
* by Brian Kernighan/Rob Pike. Substantial additions by Andrew Fullford.
* Depends:
* Output of well known unix diff program, both BSD and GNU versions.
* Blame:
* https://github.com/jmscott/work
* See:
* https://github.com/jmscott/work/blob/master/idiff.c
* Notes:
* Writes to idiff.out do not need to be buffered.
* Bufering confuses file sizes when viewing the temp file
* idiff.out for commited changes.
*
* A sporadic bug still exists when ^D exiting. Difficult to repeat.
*
* UTF-8 not tested.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#define MAXLINE 10240 /* Maximum line length handled */
static char *prog = "idiff";
static char tempfile[BUFSIZ] = {0};
static int child_pid;
/*
** ascii to unsigned int
*/
#define a2i(s, p) while (isdigit(*(s))) p = 10 * (p) + (*(s)++ - '0')
static void
leave(int sig)
{
signal(sig, SIG_IGN);
if (tempfile[0]) {
unlink(tempfile);
tempfile[0] = 0;
}
if (sig && child_pid)
kill(child_pid, SIGTERM);
exit(sig);
}
static void
usage(int fd)
{
static char blurb[] = "\n\
Usage: idiff file1 file2\n\
\n\
Interactively select file differences and write result to idiff.out\n\
\n\
For each difference group:\n\
< selects the lines from file1,\n\
> selects the lines from file2,\n\
! invokes a command, and\n\
e invokes an $EDITOR or nano on the current diff group.\n\
The result of the edit session is placed\n\
in the diff output.\n"
;
write(fd, blurb, strlen(blurb));
}
static void
die(char *msg)
{
char buf[MAXLINE];
strcpy(buf, prog);
strcat(buf, ": ERROR: ");
strncat(buf, msg, sizeof buf - (strlen(buf) + 2));
strncat(buf, "\n", sizeof buf - (strlen(buf) + 2));
buf[sizeof buf - 2] = '\n';
buf[sizeof buf - 1] = 0;
write(2, buf, strlen(buf));
usage(2);
leave(127);
}
static void
die2(char *msg1, char *msg2)
{
char buf[MAXLINE];
strncpy(buf, msg1, sizeof buf - 1);
strncat(buf, ": ", sizeof buf - (strlen(buf) + 1));
strncat(buf, msg2, sizeof buf - (strlen(buf) + 1));
die(buf);
}
static void
edie(char *msg)
{
die2(msg, strerror(errno));
}
static FILE *
_fopen(char *path, char *mode)
{
FILE *fp;
char buf[MAXLINE];
if ((fp = fopen(path, mode)))
return fp;
snprintf(buf, sizeof buf, "fopen(%s, %s) failed", path, mode);
edie(buf);
return (FILE *)0; // not reached
}
/*
** Skip n lines of file fin
*/
static void
nskip(FILE *fin, int n)
{
char buf[MAXLINE];
while (n-- > 0)
fgets(buf, sizeof buf, fin);
}
/*
** copy n lines from fin to fout. If n == -1, copies to EOF.
*/
static void
ncopy(FILE *fin, int n, FILE *fout)
{
char buf[MAXLINE];
if (n == -1) {
while (fgets(buf, sizeof buf, fin))
fputs(buf, fout);
} else while (n-- > 0) {
if (!fgets(buf, sizeof buf, fin))
return;
fputs(buf, fout);
}
}
// parse output from diff command
static void
parse(char *s, int *pfrom1, int *pto1, int *pcmd, int *pfrom2, int *pto2)
{
*pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
a2i(s, *pfrom1);
if (*s == ',') {
s++;
a2i(s, *pto1);
} else
*pto1 = *pfrom1;
*pcmd = *s++;
a2i(s, *pfrom2);
if (*s == ',') {
s++;
a2i(s, *pto2);
} else
*pto2 = *pfrom2;
}
static void
idiff(FILE *f1, FILE *f2, FILE *fin, FILE *fout)
{
char buf[MAXLINE], ed[2 * BUFSIZ];
int cmd, n, from1, to1, from2, to2, nf1, nf2, again;
nf1 = nf2 = 0;
while (fgets(buf, sizeof buf, fin)) {
parse(buf, &from1, &to1, &cmd, &from2, &to2);
n = to1 - from1 + to2 - from2 + 1; /* Lines from diff */
if (cmd == 'c')
n += 2;
else if (cmd == 'a')
from1++;
else if (cmd == 'd')
from2++;
else {
char buf[2];
buf[0] = cmd;
buf[1] = 0;
die2("unknown diff command", buf);
}
printf("%s", buf); fflush(stdout);
while (n-- > 0) {
fgets(buf, sizeof buf, fin);
printf("%s", buf);
}
again = 0;
do {
printf("? "); fflush(stdout);
fgets(buf, sizeof buf, stdin);
switch(buf[0]) {
case '>':
nskip(f1, to1 - nf1);
ncopy(f2, to2 - nf2, fout);
again = 0;
break;
case '<':
nskip(f2, to2 - nf2);
ncopy(f1, to1 - nf1, fout);
again = 0;
break;
case 'v':
case 'e': {
/*
* Note:
* Which of the goofy temp functions in
* posix simply gives me a temp path,
* not a FILE *. I can not reopen
* a FILE * for reading!
*/
char *TMPDIR;
TMPDIR = getenv("TMPDIR");
if (TMPDIR == NULL)
TMPDIR = "/tmp";
snprintf(tempfile, sizeof tempfile - 1,
"%s/idiff-%ld-%ld",
TMPDIR,
(long)getpid(),
(long)random()
);
FILE *ft = _fopen(tempfile, "w");
char *EDITOR = getenv("EDITOR");
if (EDITOR == NULL)
EDITOR = "nano";
ncopy(f1, from1 - 1 - nf1, fout);
nskip(f2, from2 - 1 - nf2);
ncopy(f1, to1 + 1 - from1, ft);
fprintf(ft, "---\n");
ncopy(f2, to2 + 1 - from2, ft);
fclose(ft);
snprintf(ed, sizeof ed - 1,
"%s %s",
EDITOR,
tempfile
);
system(ed);
ft = _fopen(tempfile, "r");
ncopy(ft, -1, fout);
fclose(ft);
unlink(tempfile);
tempfile[0] = 0;
again = 0;
} break;
case '!':
system(buf + 1);
printf("!\n");
again = 0;
break;
default:
printf("< or > or e or !\n");
again = 1;
break;
}
} while (again);
nf1 = to1;
nf2 = to2;
}
ncopy(f1, -1, fout);
}
static FILE *
efdiff(char *in1, char *in2)
{
int fds[2];
FILE *fp;
char cmd[BUFSIZ];
if (pipe(fds) != 0)
edie("pipe(diff) failed");
child_pid = fork();
switch (child_pid) {
case -1:
edie("fork(diff) failed");
/*NOTREACHED*/
return (FILE *)0;
case 0:
/*
** Adjust standard files. Attach stdout to pipe and
** stdin to /dev/null.
*/
close(1);
if (dup(fds[1]) != 1)
edie("dup(diff) failed");
close(0);
open("/dev/null", O_RDONLY, 0);
close(fds[0]);
close(fds[1]);
/*
** Exec via a shell so that "diffcmd" is interpreted.
** A command line arg might try magic.
*/
sprintf(cmd, "exec diff %s %s", in1, in2);
execlp("sh", "sh", "-c", cmd, (char *)0);
edie("exec(diff) failed");
/*NOTREACHED*/
return (FILE *)0;
default:
close(fds[1]);
fp = fdopen(fds[0], "r");
if (!fp)
edie("fdopen(fork diff)");
return fp;
}
}
int
main(int argc, char **argv)
{
FILE *fin, *fout, *f1, *f2;
char *in1, *in2;
struct stat s1, s2;
extern char *optarg;
extern int optind;
if (argc <= 1 || strcmp(argv[1], "--help") == 0) {
usage(1);
exit(0);
}
if (argc != 3)
die("expected 2 command line arguments");
if (!isatty(0))
die("input is not a terminal");
in1 = argv[1];
in2 = argv[2];
f1 = _fopen(in1, "r");
f2 = _fopen(in2, "r");
if (fstat(fileno(f1), &s1) != 0)
edie("stat(file 1) failed");
if (fstat(fileno(f2), &s2) != 0)
edie("stat(file 2) failed");
if (!S_ISREG(s1.st_mode) && !S_ISFIFO(s1.st_mode))
die2("file 1 is not a regular file", in1);
if (!S_ISREG(s2.st_mode) && !S_ISFIFO(s2.st_mode))
die2("file 2 is not a regular file", in2);
if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev)
die("input files are identical");
// could clobber existing idiff.out!
fout = _fopen("idiff.out", "w");
signal(SIGINT, leave);
signal(SIGHUP, leave);
signal(SIGTERM, leave);
fin = efdiff(in1, in2);
idiff(f1, f2, fin, fout);
printf("output in file idiff.out\n");
leave(0);
return 0; // not reached
}