idiff.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * interactive diff, inspired/stolen from
  11. * kernighan and pike, _unix programming environment_.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. int diffbflag;
  17. int diffwflag;
  18. void copy(Biobuf*, char*, Biobuf*, char*);
  19. void idiff(Biobuf*, char*, Biobuf*, char*, Biobuf*, char*, Biobuf*,
  20. char*);
  21. int opentemp(char*, int, int32_t);
  22. void rundiff(char*, char*, int);
  23. void
  24. usage(void)
  25. {
  26. fprint(2, "usage: idiff [-bw] file1 file2\n");
  27. exits("usage");
  28. }
  29. void
  30. main(int argc, char **argv)
  31. {
  32. int fd, ofd;
  33. char diffout[40], idiffout[40];
  34. Biobuf *b1, *b2, bdiff, bout, bstdout;
  35. Dir *d;
  36. ARGBEGIN{
  37. default:
  38. usage();
  39. case 'b':
  40. diffbflag++;
  41. break;
  42. case 'w':
  43. diffwflag++;
  44. break;
  45. }ARGEND
  46. if(argc != 2)
  47. usage();
  48. if((d = dirstat(argv[0])) == nil)
  49. sysfatal("stat %s: %r", argv[0]);
  50. if(d->mode&DMDIR)
  51. sysfatal("%s is a directory", argv[0]);
  52. free(d);
  53. if((d = dirstat(argv[1])) == nil)
  54. sysfatal("stat %s: %r", argv[1]);
  55. if(d->mode&DMDIR)
  56. sysfatal("%s is a directory", argv[1]);
  57. free(d);
  58. if((b1 = Bopen(argv[0], OREAD)) == nil)
  59. sysfatal("open %s: %r", argv[0]);
  60. if((b2 = Bopen(argv[1], OREAD)) == nil)
  61. sysfatal("open %s: %r", argv[1]);
  62. strcpy(diffout, "/tmp/idiff.XXXXXX");
  63. fd = opentemp(diffout, ORDWR|ORCLOSE, 0);
  64. strcpy(idiffout, "/tmp/idiff.XXXXXX");
  65. ofd = opentemp(idiffout, ORDWR|ORCLOSE, 0);
  66. rundiff(argv[0], argv[1], fd);
  67. seek(fd, 0, 0);
  68. Binit(&bdiff, fd, OREAD);
  69. Binit(&bout, ofd, OWRITE);
  70. idiff(b1, argv[0], b2, argv[1], &bdiff, diffout, &bout, idiffout);
  71. Bterm(&bdiff);
  72. Bflush(&bout);
  73. seek(ofd, 0, 0);
  74. Binit(&bout, ofd, OREAD);
  75. Binit(&bstdout, 1, OWRITE);
  76. copy(&bout, idiffout, &bstdout, "<stdout>");
  77. exits(nil);
  78. }
  79. int
  80. opentemp(char *template, int mode, int32_t perm)
  81. {
  82. int fd, i;
  83. char *p;
  84. p = strdup(template);
  85. if(p == nil)
  86. sysfatal("strdup out of memory");
  87. fd = -1;
  88. for(i=0; i<10; i++){
  89. mktemp(p);
  90. if(access(p, 0) < 0 && (fd=create(p, mode, perm)) >= 0)
  91. break;
  92. strcpy(p, template);
  93. }
  94. if(fd < 0)
  95. sysfatal("could not create temporary file");
  96. strcpy(template, p);
  97. free(p);
  98. return fd;
  99. }
  100. void
  101. rundiff(char *arg1, char *arg2, int outfd)
  102. {
  103. char *arg[10], *p;
  104. int narg, pid;
  105. Waitmsg *w;
  106. narg = 0;
  107. arg[narg++] = "/bin/diff";
  108. arg[narg++] = "-n";
  109. if(diffbflag)
  110. arg[narg++] = "-b";
  111. if(diffwflag)
  112. arg[narg++] = "-w";
  113. arg[narg++] = arg1;
  114. arg[narg++] = arg2;
  115. arg[narg] = nil;
  116. switch(pid = fork()){
  117. case -1:
  118. sysfatal("fork: %r");
  119. case 0:
  120. dup(outfd, 1);
  121. close(0);
  122. exec("/bin/diff", arg);
  123. sysfatal("exec: %r");
  124. default:
  125. w = wait();
  126. if(w==nil)
  127. sysfatal("wait: %r");
  128. if(w->pid != pid)
  129. sysfatal("wait got unexpected pid %d", w->pid);
  130. if((p = strchr(w->msg, ':')) && strcmp(p, ": some") != 0)
  131. sysfatal("%s", w->msg);
  132. free(w);
  133. }
  134. }
  135. void
  136. runcmd(char *cmd)
  137. {
  138. char *arg[10];
  139. int narg, pid, wpid;
  140. narg = 0;
  141. arg[narg++] = "/bin/rc";
  142. arg[narg++] = "-c";
  143. arg[narg++] = cmd;
  144. arg[narg] = nil;
  145. switch(pid = fork()){
  146. case -1:
  147. sysfatal("fork: %r");
  148. case 0:
  149. exec("/bin/rc", arg);
  150. sysfatal("exec: %r");
  151. default:
  152. wpid = waitpid();
  153. if(wpid < 0)
  154. sysfatal("wait: %r");
  155. if(wpid != pid)
  156. sysfatal("wait got unexpected pid %d", wpid);
  157. }
  158. }
  159. void
  160. parse(char *s, int *pfrom1, int *pto1, int *pcmd, int *pfrom2,
  161. int *pto2)
  162. {
  163. *pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
  164. s = strchr(s, ':');
  165. if(s == nil)
  166. sysfatal("bad diff output0");
  167. s++;
  168. *pfrom1 = strtol(s, &s, 10);
  169. if(*s == ','){
  170. s++;
  171. *pto1 = strtol(s, &s, 10);
  172. }else
  173. *pto1 = *pfrom1;
  174. if(*s++ != ' ')
  175. sysfatal("bad diff output1");
  176. *pcmd = *s++;
  177. if(*s++ != ' ')
  178. sysfatal("bad diff output2");
  179. s = strchr(s, ':');
  180. if(s == nil)
  181. sysfatal("bad diff output3");
  182. s++;
  183. *pfrom2 = strtol(s, &s, 10);
  184. if(*s == ','){
  185. s++;
  186. *pto2 = strtol(s, &s, 10);
  187. }else
  188. *pto2 = *pfrom2;
  189. }
  190. void
  191. skiplines(Biobuf *b, char *name, int n)
  192. {
  193. int i;
  194. for(i=0; i<n; i++){
  195. while(Brdline(b, '\n')==nil){
  196. if(Blinelen(b) <= 0)
  197. sysfatal("early end of file on %s", name);
  198. Bseek(b, Blinelen(b), 1);
  199. }
  200. }
  201. }
  202. void
  203. copylines(Biobuf *bin, char *nin, Biobuf *bout, char *nout, int n)
  204. {
  205. char buf[4096], *p;
  206. int i, m;
  207. for(i=0; i<n; i++){
  208. while((p=Brdline(bin, '\n'))==nil){
  209. if(Blinelen(bin) <= 0)
  210. sysfatal("early end of file on %s", nin);
  211. m = Blinelen(bin);
  212. if(m > sizeof buf)
  213. m = sizeof buf;
  214. m = Bread(bin, buf, m);
  215. if(Bwrite(bout, buf, m) != m)
  216. sysfatal("error writing %s: %r", nout);
  217. }
  218. if(Bwrite(bout, p, Blinelen(bin)) != Blinelen(bin))
  219. sysfatal("error writing %s: %r", nout);
  220. }
  221. }
  222. void
  223. copy(Biobuf *bin, char *nin, Biobuf *bout, char *nout)
  224. {
  225. char buf[4096];
  226. int m;
  227. USED(nin);
  228. while((m = Bread(bin, buf, sizeof buf)) > 0)
  229. if(Bwrite(bout, buf, m) != m)
  230. sysfatal("error writing %s: %r", nout);
  231. }
  232. void
  233. idiff(Biobuf *b1, char *name1, Biobuf *b2, char *name2, Biobuf *bdiff,
  234. char *namediff, Biobuf *bout, char *nameout)
  235. {
  236. char buf[256], *p;
  237. int interactive, defaultanswer, cmd, diffoffset;
  238. int n, from1, to1, from2, to2, nf1, nf2;
  239. Biobuf berr;
  240. nf1 = 1;
  241. nf2 = 1;
  242. interactive = 1;
  243. defaultanswer = 0;
  244. Binit(&berr, 2, OWRITE);
  245. while(diffoffset = Boffset(bdiff), p = Brdline(bdiff, '\n')){
  246. p[Blinelen(bdiff)-1] = '\0';
  247. parse(p, &from1, &to1, &cmd, &from2, &to2);
  248. p[Blinelen(bdiff)-1] = '\n';
  249. n = to1-from1 + to2-from2 + 1; /* #lines from diff */
  250. if(cmd == 'c')
  251. n += 2;
  252. else if(cmd == 'a')
  253. from1++;
  254. else if(cmd == 'd')
  255. from2++;
  256. to1++; /* make half-open intervals */
  257. to2++;
  258. if(interactive){
  259. p[Blinelen(bdiff)-1] = '\0';
  260. fprint(2, "%s\n", p);
  261. p[Blinelen(bdiff)-1] = '\n';
  262. copylines(bdiff, namediff, &berr, "<stderr>", n);
  263. Bflush(&berr);
  264. }else
  265. skiplines(bdiff, namediff, n);
  266. do{
  267. if(interactive){
  268. fprint(2, "? ");
  269. memset(buf, 0, sizeof buf);
  270. if(read(0, buf, sizeof buf - 1) < 0)
  271. sysfatal("read console: %r");
  272. }else
  273. buf[0] = defaultanswer;
  274. switch(buf[0]){
  275. case '>':
  276. copylines(b1, name1, bout, nameout, from1-nf1);
  277. skiplines(b1, name1, to1-from1);
  278. skiplines(b2, name2, from2-nf2);
  279. copylines(b2, name2, bout, nameout, to2-from2);
  280. break;
  281. case '<':
  282. copylines(b1, name1, bout, nameout, to1-nf1);
  283. skiplines(b2, name2, to2-nf2);
  284. break;
  285. case '=':
  286. copylines(b1, name1, bout, nameout, from1-nf1);
  287. skiplines(b1, name1, to1-from1);
  288. skiplines(b2, name2, to2-nf2);
  289. if(Bseek(bdiff, diffoffset, 0) != diffoffset)
  290. sysfatal("seek in diff output: %r");
  291. copylines(bdiff, namediff, bout, nameout, n+1);
  292. break;
  293. case '!':
  294. runcmd(buf+1);
  295. break;
  296. case 'q':
  297. if(buf[1]=='<' || buf[1]=='>' || buf[1]=='='){
  298. interactive = 0;
  299. defaultanswer = buf[1];
  300. }else
  301. fprint(2, "must be q<, q>, or q=\n");
  302. break;
  303. default:
  304. fprint(2, "expect: <, >, =, q<, q>, q=, !cmd\n");
  305. break;
  306. }
  307. }while(buf[0] != '<' && buf[0] != '>' && buf[0] != '=');
  308. nf1 = to1;
  309. nf2 = to2;
  310. }
  311. copy(b1, name1, bout, nameout);
  312. }