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
|
/*
* interactive diff, inspired/stolen from
* kernighan and pike, _unix programming environment_.
*/
#include <u.h>
#include <libc.h>
#include <bio.h>
int diffbflag;
int diffwflag;
void copy(Biobuf*, char*, Biobuf*, char*);
void idiff(Biobuf*, char*, Biobuf*, char*, Biobuf*, char*, Biobuf*, char*);
int opentemp(char*, int, long);
void rundiff(char*, char*, int);
void
usage(void)
{
fprint(2, "usage: idiff [-bw] file1 file2\n");
exits("usage");
}
void
main(int argc, char **argv)
{
int fd, ofd;
char diffout[40], idiffout[40];
Biobuf *b1, *b2, bdiff, bout, bstdout;
Dir *d;
ARGBEGIN{
default:
usage();
case 'b':
diffbflag++;
break;
case 'w':
diffwflag++;
break;
}ARGEND
if(argc != 2)
usage();
if((d = dirstat(argv[0])) == nil)
sysfatal("stat %s: %r", argv[0]);
if(d->mode&DMDIR)
sysfatal("%s is a directory", argv[0]);
free(d);
if((d = dirstat(argv[1])) == nil)
sysfatal("stat %s: %r", argv[1]);
if(d->mode&DMDIR)
sysfatal("%s is a directory", argv[1]);
free(d);
if((b1 = Bopen(argv[0], OREAD)) == nil)
sysfatal("open %s: %r", argv[0]);
if((b2 = Bopen(argv[1], OREAD)) == nil)
sysfatal("open %s: %r", argv[1]);
strcpy(diffout, "/tmp/idiff.XXXXXX");
fd = opentemp(diffout, ORDWR|ORCLOSE, 0);
strcpy(idiffout, "/tmp/idiff.XXXXXX");
ofd = opentemp(idiffout, ORDWR|ORCLOSE, 0);
rundiff(argv[0], argv[1], fd);
seek(fd, 0, 0);
Binit(&bdiff, fd, OREAD);
Binit(&bout, ofd, OWRITE);
idiff(b1, argv[0], b2, argv[1], &bdiff, diffout, &bout, idiffout);
Bterm(&bdiff);
Bflush(&bout);
seek(ofd, 0, 0);
Binit(&bout, ofd, OREAD);
Binit(&bstdout, 1, OWRITE);
copy(&bout, idiffout, &bstdout, "<stdout>");
exits(nil);
}
int
opentemp(char *template, int mode, long perm)
{
int fd;
Dir d;
fd = mkstemp(template);
if(fd < 0)
sysfatal("could not create temporary file");
nulldir(&d);
d.mode = perm;
dirfwstat(fd, &d);
return fd;
}
void
rundiff(char *arg1, char *arg2, int outfd)
{
char *arg[10], *p;
int narg, pid;
Waitmsg *w;
narg = 0;
arg[narg++] = "/bin/diff";
arg[narg++] = "-n";
if(diffbflag)
arg[narg++] = "-b";
if(diffwflag)
arg[narg++] = "-w";
arg[narg++] = arg1;
arg[narg++] = arg2;
arg[narg] = nil;
switch(pid = fork()){
case -1:
sysfatal("fork: %r");
case 0:
dup(outfd, 1);
close(0);
exec("/bin/diff", arg);
sysfatal("exec: %r");
default:
w = wait();
if(w==nil)
sysfatal("wait: %r");
if(w->pid != pid)
sysfatal("wait got unexpected pid %d", w->pid);
if((p = strchr(w->msg, ':')) && strcmp(p, ": some") != 0)
sysfatal("%s", w->msg);
free(w);
}
}
void
runcmd(char *cmd)
{
char *arg[10];
int narg, pid, wpid;
narg = 0;
arg[narg++] = "/bin/rc";
arg[narg++] = "-c";
arg[narg++] = cmd;
arg[narg] = nil;
switch(pid = fork()){
case -1:
sysfatal("fork: %r");
case 0:
exec("/bin/rc", arg);
sysfatal("exec: %r");
default:
wpid = waitpid();
if(wpid < 0)
sysfatal("wait: %r");
if(wpid != pid)
sysfatal("wait got unexpected pid %d", wpid);
}
}
void
parse(char *s, int *pfrom1, int *pto1, int *pcmd, int *pfrom2, int *pto2)
{
*pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
s = strchr(s, ':');
if(s == nil)
sysfatal("bad diff output0");
s++;
*pfrom1 = strtol(s, &s, 10);
if(*s == ','){
s++;
*pto1 = strtol(s, &s, 10);
}else
*pto1 = *pfrom1;
if(*s++ != ' ')
sysfatal("bad diff output1");
*pcmd = *s++;
if(*s++ != ' ')
sysfatal("bad diff output2");
s = strchr(s, ':');
if(s == nil)
sysfatal("bad diff output3");
s++;
*pfrom2 = strtol(s, &s, 10);
if(*s == ','){
s++;
*pto2 = strtol(s, &s, 10);
}else
*pto2 = *pfrom2;
}
void
skiplines(Biobuf *b, char *name, int n)
{
int i;
for(i=0; i<n; i++){
while(Brdline(b, '\n')==nil){
if(Blinelen(b) <= 0)
sysfatal("early end of file on %s", name);
Bseek(b, Blinelen(b), 1);
}
}
}
void
copylines(Biobuf *bin, char *nin, Biobuf *bout, char *nout, int n)
{
char buf[4096], *p;
int i, m;
for(i=0; i<n; i++){
while((p=Brdline(bin, '\n'))==nil){
if(Blinelen(bin) <= 0)
sysfatal("early end of file on %s", nin);
m = Blinelen(bin);
if(m > sizeof buf)
m = sizeof buf;
m = Bread(bin, buf, m);
if(Bwrite(bout, buf, m) != m)
sysfatal("error writing %s: %r", nout);
}
if(Bwrite(bout, p, Blinelen(bin)) != Blinelen(bin))
sysfatal("error writing %s: %r", nout);
}
}
void
copy(Biobuf *bin, char *nin, Biobuf *bout, char *nout)
{
char buf[4096];
int m;
USED(nin);
while((m = Bread(bin, buf, sizeof buf)) > 0)
if(Bwrite(bout, buf, m) != m)
sysfatal("error writing %s: %r", nout);
}
void
idiff(Biobuf *b1, char *name1, Biobuf *b2, char *name2, Biobuf *bdiff, char *namediff, Biobuf *bout, char *nameout)
{
char buf[256], *p;
int interactive, defaultanswer, cmd, diffoffset;
int n, from1, to1, from2, to2, nf1, nf2;
Biobuf berr;
nf1 = 1;
nf2 = 1;
interactive = 1;
defaultanswer = 0;
Binit(&berr, 2, OWRITE);
while(diffoffset = Boffset(bdiff), p = Brdline(bdiff, '\n')){
p[Blinelen(bdiff)-1] = '\0';
parse(p, &from1, &to1, &cmd, &from2, &to2);
p[Blinelen(bdiff)-1] = '\n';
n = to1-from1 + to2-from2 + 1; /* #lines from diff */
if(cmd == 'c')
n += 2;
else if(cmd == 'a')
from1++;
else if(cmd == 'd')
from2++;
to1++; /* make half-open intervals */
to2++;
if(interactive){
p[Blinelen(bdiff)-1] = '\0';
fprint(2, "%s\n", p);
p[Blinelen(bdiff)-1] = '\n';
copylines(bdiff, namediff, &berr, "<stderr>", n);
Bflush(&berr);
}else
skiplines(bdiff, namediff, n);
do{
if(interactive){
fprint(2, "? ");
memset(buf, 0, sizeof buf);
if(read(0, buf, sizeof buf - 1) < 0)
sysfatal("read console: %r");
}else
buf[0] = defaultanswer;
switch(buf[0]){
case '>':
copylines(b1, name1, bout, nameout, from1-nf1);
skiplines(b1, name1, to1-from1);
skiplines(b2, name2, from2-nf2);
copylines(b2, name2, bout, nameout, to2-from2);
break;
case '<':
copylines(b1, name1, bout, nameout, to1-nf1);
skiplines(b2, name2, to2-nf2);
break;
case '=':
copylines(b1, name1, bout, nameout, from1-nf1);
skiplines(b1, name1, to1-from1);
skiplines(b2, name2, to2-nf2);
if(Bseek(bdiff, diffoffset, 0) != diffoffset)
sysfatal("seek in diff output: %r");
copylines(bdiff, namediff, bout, nameout, n+1);
break;
case '!':
runcmd(buf+1);
break;
case 'q':
if(buf[1]=='<' || buf[1]=='>' || buf[1]=='='){
interactive = 0;
defaultanswer = buf[1];
}else
fprint(2, "must be q<, q>, or q=\n");
break;
default:
fprint(2, "expect: <, >, =, q<, q>, q=, !cmd\n");
break;
}
}while(buf[0] != '<' && buf[0] != '>' && buf[0] != '=');
nf1 = to1;
nf2 = to2;
}
copy(b1, name1, bout, nameout);
}
|