2 * interactive diff, inspired/stolen from
3 * kernighan and pike, _unix programming environment_.
13 void copy(Biobuf*, char*, Biobuf*, char*);
14 void idiff(Biobuf*, char*, Biobuf*, char*, Biobuf*, char*, Biobuf*, char*);
15 void rundiff(char*, char*, int);
20 fprint(2, "usage: idiff [-bw] file1 file2\n");
25 main(int argc, char **argv)
28 char diffout[40], idiffout[40];
29 Biobuf *b1, *b2, bdiff, bout, bstdout;
46 if((d = dirstat(argv[0])) == nil)
47 sysfatal("stat %s: %r", argv[0]);
49 sysfatal("%s is a directory", argv[0]);
51 if((d = dirstat(argv[1])) == nil)
52 sysfatal("stat %s: %r", argv[1]);
54 sysfatal("%s is a directory", argv[1]);
57 if((b1 = Bopen(argv[0], OREAD)) == nil)
58 sysfatal("open %s: %r", argv[0]);
59 if((b2 = Bopen(argv[1], OREAD)) == nil)
60 sysfatal("open %s: %r", argv[1]);
62 strcpy(diffout, "/tmp/idiff.XXXXXX");
63 fd = opentemp(diffout, ORDWR|ORCLOSE);
64 strcpy(idiffout, "/tmp/idiff.XXXXXX");
65 ofd = opentemp(idiffout, ORDWR|ORCLOSE);
66 rundiff(argv[0], argv[1], fd);
68 Binit(&bdiff, fd, OREAD);
69 Binit(&bout, ofd, OWRITE);
70 idiff(b1, argv[0], b2, argv[1], &bdiff, diffout, &bout, idiffout);
74 Binit(&bout, ofd, OREAD);
75 Binit(&bstdout, 1, OWRITE);
76 copy(&bout, idiffout, &bstdout, "<stdout>");
81 rundiff(char *arg1, char *arg2, int outfd)
101 sysfatal("fork: %r");
107 sysfatal("exec: %r");
112 sysfatal("wait: %r");
114 sysfatal("wait got unexpected pid %d", w->pid);
115 if((p = strchr(w->msg, ':')) && strcmp(p, ": some") != 0)
116 sysfatal("%s", w->msg);
133 switch(pid = fork()){
135 sysfatal("fork: %r");
139 sysfatal("exec: %r");
144 sysfatal("wait: %r");
146 sysfatal("wait got unexpected pid %d", wpid);
151 parse(char *s, int *pfrom1, int *pto1, int *pcmd, int *pfrom2, int *pto2)
153 *pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
157 sysfatal("bad diff output0");
159 *pfrom1 = strtol(s, &s, 10);
162 *pto1 = strtol(s, &s, 10);
166 sysfatal("bad diff output1");
169 sysfatal("bad diff output2");
172 sysfatal("bad diff output3");
174 *pfrom2 = strtol(s, &s, 10);
177 *pto2 = strtol(s, &s, 10);
183 skiplines(Biobuf *b, char *name, int n)
188 while(Brdline(b, '\n')==nil){
190 sysfatal("early end of file on %s", name);
191 Bseek(b, Blinelen(b), 1);
197 copylines(Biobuf *bin, char *nin, Biobuf *bout, char *nout, int n)
203 while((p=Brdline(bin, '\n'))==nil){
204 if(Blinelen(bin) <= 0)
205 sysfatal("early end of file on %s", nin);
209 m = Bread(bin, buf, m);
210 if(Bwrite(bout, buf, m) != m)
211 sysfatal("error writing %s: %r", nout);
213 if(Bwrite(bout, p, Blinelen(bin)) != Blinelen(bin))
214 sysfatal("error writing %s: %r", nout);
219 copy(Biobuf *bin, char *nin, Biobuf *bout, char *nout)
225 while((m = Bread(bin, buf, sizeof buf)) > 0)
226 if(Bwrite(bout, buf, m) != m)
227 sysfatal("error writing %s: %r", nout);
231 idiff(Biobuf *b1, char *name1, Biobuf *b2, char *name2, Biobuf *bdiff, char *namediff, Biobuf *bout, char *nameout)
234 int interactive, defaultanswer, cmd, diffoffset;
235 int n, from1, to1, from2, to2, nf1, nf2;
242 Binit(&berr, 2, OWRITE);
243 while(diffoffset = Boffset(bdiff), p = Brdline(bdiff, '\n')){
244 p[Blinelen(bdiff)-1] = '\0';
245 parse(p, &from1, &to1, &cmd, &from2, &to2);
246 p[Blinelen(bdiff)-1] = '\n';
247 n = to1-from1 + to2-from2 + 1; /* #lines from diff */
254 to1++; /* make half-open intervals */
257 p[Blinelen(bdiff)-1] = '\0';
258 fprint(2, "%s\n", p);
259 p[Blinelen(bdiff)-1] = '\n';
260 copylines(bdiff, namediff, &berr, "<stderr>", n);
263 skiplines(bdiff, namediff, n);
267 memset(buf, 0, sizeof buf);
268 if(read(0, buf, sizeof buf - 1) < 0)
269 sysfatal("read console: %r");
271 buf[0] = defaultanswer;
275 copylines(b1, name1, bout, nameout, from1-nf1);
276 skiplines(b1, name1, to1-from1);
277 skiplines(b2, name2, from2-nf2);
278 copylines(b2, name2, bout, nameout, to2-from2);
281 copylines(b1, name1, bout, nameout, to1-nf1);
282 skiplines(b2, name2, to2-nf2);
285 copylines(b1, name1, bout, nameout, from1-nf1);
286 skiplines(b1, name1, to1-from1);
287 skiplines(b2, name2, to2-nf2);
288 if(Bseek(bdiff, diffoffset, 0) != diffoffset)
289 sysfatal("seek in diff output: %r");
290 copylines(bdiff, namediff, bout, nameout, n+1);
296 if(buf[1]=='<' || buf[1]=='>' || buf[1]=='='){
298 defaultanswer = buf[1];
300 fprint(2, "must be q<, q>, or q=\n");
303 fprint(2, "expect: <, >, =, q<, q>, q=, !cmd\n");
306 }while(buf[0] != '<' && buf[0] != '>' && buf[0] != '=');
310 copy(b1, name1, bout, nameout);