diffio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include "diff.h"
  6. struct line {
  7. int serial;
  8. int value;
  9. };
  10. extern struct line *file[2];
  11. extern int len[2];
  12. extern long *ixold, *ixnew;
  13. extern int *J;
  14. static Biobuf *input[2];
  15. static char *file1, *file2;
  16. static int firstchange;
  17. #define MAXLINELEN 4096
  18. #define MIN(x, y) ((x) < (y) ? (x): (y))
  19. static int
  20. readline(Biobuf *bp, char *buf)
  21. {
  22. int c;
  23. char *p, *e;
  24. p = buf;
  25. e = p + MAXLINELEN-1;
  26. do {
  27. c = Bgetc(bp);
  28. if (c < 0) {
  29. if (p == buf)
  30. return -1;
  31. break;
  32. }
  33. if (c == '\n')
  34. break;
  35. *p++ = c;
  36. } while (p < e);
  37. *p = 0;
  38. if (c != '\n' && c >= 0) {
  39. do c = Bgetc(bp);
  40. while (c >= 0 && c != '\n');
  41. }
  42. return p - buf;
  43. }
  44. #define HALFLONG 16
  45. #define low(x) (x&((1L<<HALFLONG)-1))
  46. #define high(x) (x>>HALFLONG)
  47. /*
  48. * hashing has the effect of
  49. * arranging line in 7-bit bytes and then
  50. * summing 1-s complement in 16-bit hunks
  51. */
  52. static int
  53. readhash(Biobuf *bp, char *buf)
  54. {
  55. long sum;
  56. unsigned shift;
  57. char *p;
  58. int len, space;
  59. sum = 1;
  60. shift = 0;
  61. if ((len = readline(bp, buf)) == -1)
  62. return 0;
  63. p = buf;
  64. switch(bflag) /* various types of white space handling */
  65. {
  66. case 0:
  67. while (len--) {
  68. sum += (long)*p++ << (shift &= (HALFLONG-1));
  69. shift += 7;
  70. }
  71. break;
  72. case 1:
  73. /*
  74. * coalesce multiple white-space
  75. */
  76. for (space = 0; len--; p++) {
  77. if (isspace(*p)) {
  78. space++;
  79. continue;
  80. }
  81. if (space) {
  82. shift += 7;
  83. space = 0;
  84. }
  85. sum += (long)*p << (shift &= (HALFLONG-1));
  86. shift += 7;
  87. }
  88. break;
  89. default:
  90. /*
  91. * strip all white-space
  92. */
  93. while (len--) {
  94. if (isspace(*p)) {
  95. p++;
  96. continue;
  97. }
  98. sum += (long)*p++ << (shift &= (HALFLONG-1));
  99. shift += 7;
  100. }
  101. break;
  102. }
  103. sum = low(sum) + high(sum);
  104. return ((short)low(sum) + (short)high(sum));
  105. }
  106. Biobuf *
  107. prepare(int i, char *arg)
  108. {
  109. struct line *p;
  110. int j, h;
  111. Biobuf *bp;
  112. char *cp, buf[MAXLINELEN];
  113. int nbytes;
  114. Rune r;
  115. bp = Bopen(arg, OREAD);
  116. if (!bp) {
  117. panic(mflag ? 0: 2, "cannot open %s: %r\n", arg);
  118. return 0;
  119. }
  120. if (binary)
  121. return bp;
  122. nbytes = Bread(bp, buf, MIN(1024, MAXLINELEN));
  123. if (nbytes > 0) {
  124. cp = buf;
  125. while (cp < buf+nbytes-UTFmax) {
  126. /*
  127. * heuristic for a binary file in the
  128. * brave new UNICODE world
  129. */
  130. cp += chartorune(&r, cp);
  131. if (r == 0 || (r > 0x7f && r <= 0xa0)) {
  132. binary++;
  133. return bp;
  134. }
  135. }
  136. Bseek(bp, 0, 0);
  137. }
  138. p = MALLOC(struct line, 3);
  139. for (j = 0; h = readhash(bp, buf); p[j].value = h)
  140. p = REALLOC(p, struct line, (++j+3));
  141. len[i] = j;
  142. file[i] = p;
  143. input[i] = bp; /*fix*/
  144. if (i == 0) { /*fix*/
  145. file1 = arg;
  146. firstchange = 0;
  147. }
  148. else
  149. file2 = arg;
  150. return bp;
  151. }
  152. static int
  153. squishspace(char *buf)
  154. {
  155. char *p, *q;
  156. int space;
  157. for (space = 0, q = p = buf; *q; q++) {
  158. if (isspace(*q)) {
  159. space++;
  160. continue;
  161. }
  162. if (space && bflag == 1) {
  163. *p++ = ' ';
  164. space = 0;
  165. }
  166. *p++ = *q;
  167. }
  168. *p = 0;
  169. return p - buf;
  170. }
  171. /*
  172. * need to fix up for unexpected EOF's
  173. */
  174. void
  175. check(Biobuf *bf, Biobuf *bt)
  176. {
  177. int f, t, flen, tlen;
  178. char fbuf[MAXLINELEN], tbuf[MAXLINELEN];
  179. ixold[0] = ixnew[0] = 0;
  180. for (f = t = 1; f < len[0]; f++) {
  181. flen = readline(bf, fbuf);
  182. ixold[f] = ixold[f-1] + flen + 1; /* ftell(bf) */
  183. if (J[f] == 0)
  184. continue;
  185. do {
  186. tlen = readline(bt, tbuf);
  187. ixnew[t] = ixnew[t-1] + tlen + 1; /* ftell(bt) */
  188. } while (t++ < J[f]);
  189. if (bflag) {
  190. flen = squishspace(fbuf);
  191. tlen = squishspace(tbuf);
  192. }
  193. if (flen != tlen || strcmp(fbuf, tbuf))
  194. J[f] = 0;
  195. }
  196. while (t < len[1]) {
  197. tlen = readline(bt, tbuf);
  198. ixnew[t] = ixnew[t-1] + tlen + 1; /* fseek(bt) */
  199. t++;
  200. }
  201. }
  202. static void
  203. range(int a, int b, char *separator)
  204. {
  205. Bprint(&stdout, "%d", a > b ? b: a);
  206. if (a < b)
  207. Bprint(&stdout, "%s%d", separator, b);
  208. }
  209. static void
  210. fetch(long *f, int a, int b, Biobuf *bp, char *s)
  211. {
  212. char buf[MAXLINELEN];
  213. int maxb;
  214. if(a <= 1)
  215. a = 1;
  216. if(bp == input[0])
  217. maxb = len[0];
  218. else
  219. maxb = len[1];
  220. if(b > maxb)
  221. b = maxb;
  222. if(a > maxb)
  223. return;
  224. Bseek(bp, f[a-1], 0);
  225. while (a++ <= b) {
  226. readline(bp, buf);
  227. Bprint(&stdout, "%s%s\n", s, buf);
  228. }
  229. }
  230. typedef struct Change Change;
  231. struct Change
  232. {
  233. int a;
  234. int b;
  235. int c;
  236. int d;
  237. };
  238. Change *changes;
  239. int nchanges;
  240. void
  241. change(int a, int b, int c, int d)
  242. {
  243. char verb;
  244. char buf[4];
  245. Change *ch;
  246. if (a > b && c > d)
  247. return;
  248. anychange = 1;
  249. if (mflag && firstchange == 0) {
  250. if(mode) {
  251. buf[0] = '-';
  252. buf[1] = mode;
  253. buf[2] = ' ';
  254. buf[3] = '\0';
  255. } else {
  256. buf[0] = '\0';
  257. }
  258. Bprint(&stdout, "diff %s%s %s\n", buf, file1, file2);
  259. firstchange = 1;
  260. }
  261. verb = a > b ? 'a': c > d ? 'd': 'c';
  262. switch(mode) {
  263. case 'e':
  264. range(a, b, ",");
  265. Bputc(&stdout, verb);
  266. break;
  267. case 0:
  268. range(a, b, ",");
  269. Bputc(&stdout, verb);
  270. range(c, d, ",");
  271. break;
  272. case 'n':
  273. Bprint(&stdout, "%s:", file1);
  274. range(a, b, ",");
  275. Bprint(&stdout, " %c ", verb);
  276. Bprint(&stdout, "%s:", file2);
  277. range(c, d, ",");
  278. break;
  279. case 'f':
  280. Bputc(&stdout, verb);
  281. range(a, b, " ");
  282. break;
  283. case 'c':
  284. case 'a':
  285. if(nchanges%1024 == 0)
  286. changes = erealloc(changes, (nchanges+1024)*sizeof(changes[0]));
  287. ch = &changes[nchanges++];
  288. ch->a = a;
  289. ch->b = b;
  290. ch->c = c;
  291. ch->d = d;
  292. return;
  293. }
  294. Bputc(&stdout, '\n');
  295. if (mode == 0 || mode == 'n') {
  296. fetch(ixold, a, b, input[0], "< ");
  297. if (a <= b && c <= d)
  298. Bprint(&stdout, "---\n");
  299. }
  300. fetch(ixnew, c, d, input[1], mode == 0 || mode == 'n' ? "> ": "");
  301. if (mode != 0 && mode != 'n' && c <= d)
  302. Bprint(&stdout, ".\n");
  303. }
  304. enum
  305. {
  306. Lines = 3, /* number of lines of context shown */
  307. };
  308. int
  309. changeset(int i)
  310. {
  311. while(i<nchanges && changes[i].b+1+2*Lines > changes[i+1].a)
  312. i++;
  313. if(i<nchanges)
  314. return i+1;
  315. return nchanges;
  316. }
  317. void
  318. flushchanges(void)
  319. {
  320. int a, b, c, d, at;
  321. int i, j;
  322. if(nchanges == 0)
  323. return;
  324. for(i=0; i<nchanges; ){
  325. j = changeset(i);
  326. a = changes[i].a-Lines;
  327. b = changes[j-1].b+Lines;
  328. c = changes[i].c-Lines;
  329. d = changes[j-1].d+Lines;
  330. if(a < 1)
  331. a = 1;
  332. if(c < 1)
  333. c = 1;
  334. if(b > len[0])
  335. b = len[0];
  336. if(d > len[1])
  337. d = len[1];
  338. if(mode == 'a'){
  339. a = 1;
  340. b = len[0];
  341. c = 1;
  342. d = len[1];
  343. j = nchanges;
  344. }
  345. Bprint(&stdout, "%s:", file1);
  346. range(a, b, ",");
  347. Bprint(&stdout, " - ");
  348. Bprint(&stdout, "%s:", file2);
  349. range(c, d, ",");
  350. Bputc(&stdout, '\n');
  351. at = a;
  352. for(; i<j; i++){
  353. fetch(ixold, at, changes[i].a-1, input[0], " ");
  354. fetch(ixold, changes[i].a, changes[i].b, input[0], "- ");
  355. fetch(ixnew, changes[i].c, changes[i].d, input[1], "+ ");
  356. at = changes[i].b+1;
  357. }
  358. fetch(ixold, at, b, input[0], " ");
  359. }
  360. nchanges = 0;
  361. }