diff options
author | rsc <devnull@localhost> | 2005-01-26 07:10:02 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2005-01-26 07:10:02 +0000 |
commit | 4ac5f249ad40f3452fc87dc9a3f4b9d9546c83b0 (patch) | |
tree | 6c07ca323274f6bda70ee10dea4299c765ba4e62 /src/cmd/diff | |
parent | 57d2613621107f7ee9d9e717c3c2903b1caf4bbe (diff) | |
download | plan9port-4ac5f249ad40f3452fc87dc9a3f4b9d9546c83b0.tar.gz plan9port-4ac5f249ad40f3452fc87dc9a3f4b9d9546c83b0.tar.bz2 plan9port-4ac5f249ad40f3452fc87dc9a3f4b9d9546c83b0.zip |
add -c option
Diffstat (limited to 'src/cmd/diff')
-rw-r--r-- | src/cmd/diff/diff.h | 2 | ||||
-rw-r--r-- | src/cmd/diff/diffio.c | 80 | ||||
-rw-r--r-- | src/cmd/diff/diffreg.c | 1 |
3 files changed, 82 insertions, 1 deletions
diff --git a/src/cmd/diff/diff.h b/src/cmd/diff/diff.h index c809ae81..25e39c6c 100644 --- a/src/cmd/diff/diff.h +++ b/src/cmd/diff/diff.h @@ -24,4 +24,4 @@ Biobuf *prepare(int, char *); void panic(int, char *, ...); void check(Biobuf *, Biobuf *); void change(int, int, int, int); - +void flushchanges(void); diff --git a/src/cmd/diff/diffio.c b/src/cmd/diff/diffio.c index a7f3f0b7..5317539b 100644 --- a/src/cmd/diff/diffio.c +++ b/src/cmd/diff/diffio.c @@ -226,7 +226,18 @@ static void fetch(long *f, int a, int b, Biobuf *bp, char *s) { char buf[MAXLINELEN]; + int maxb; + if(a <= 1) + a = 1; + if(bp == input[0]) + maxb = len[0]; + else + maxb = len[1]; + if(b > maxb) + b = maxb; + if(a > maxb) + return; Bseek(bp, f[a-1], 0); while (a++ <= b) { readline(bp, buf); @@ -234,11 +245,24 @@ fetch(long *f, int a, int b, Biobuf *bp, char *s) } } +typedef struct Change Change; +struct Change +{ + int a; + int b; + int c; + int d; +}; + +Change *changes; +int nchanges; + void change(int a, int b, int c, int d) { char verb; char buf[4]; + Change *ch; if (a > b && c > d) return; @@ -277,6 +301,15 @@ change(int a, int b, int c, int d) Bputc(&stdout, verb); range(a, b, " "); break; + case 'c': + if(nchanges%1024 == 0) + changes = erealloc(changes, (nchanges+1024)*sizeof(changes[0])); + ch = &changes[nchanges++]; + ch->a = a; + ch->b = b; + ch->c = c; + ch->d = d; + return; } Bputc(&stdout, '\n'); if (mode == 0 || mode == 'n') { @@ -289,3 +322,50 @@ change(int a, int b, int c, int d) Bprint(&stdout, ".\n"); } +enum +{ + Lines = 3, /* number of lines of context shown */ +}; + +int +changeset(int i) +{ + while(i<nchanges && changes[i].b+1+2*Lines > changes[i+1].a) + i++; + if(i<nchanges) + return i+1; + return nchanges; +} + +void +flushchanges(void) +{ + int a, b, c, d, at; + int i, j; + + if(nchanges == 0) + return; + + for(i=0; i<nchanges; ){ + j = changeset(i); + a = changes[i].a; + b = changes[j-1].b; + c = changes[i].c; + d = changes[j-1].d; + Bprint(&stdout, "%s:", file1); + range(a, b, ","); + Bprint(&stdout, " - "); + Bprint(&stdout, "%s:", file2); + range(c, d, ","); + Bputc(&stdout, '\n'); + at = a-Lines; + for(; i<j; i++){ + fetch(ixold, at, changes[i].a-1, input[0], " "); + fetch(ixold, changes[i].a, changes[i].b, input[0], "< "); + fetch(ixnew, changes[i].c, changes[i].d, input[1], "> "); + at = changes[i].b+1; + } + fetch(ixold, at, b+Lines, input[0], " "); + } + nchanges = 0; +} diff --git a/src/cmd/diff/diffreg.c b/src/cmd/diff/diffreg.c index 87061898..9a29cf61 100644 --- a/src/cmd/diff/diffreg.c +++ b/src/cmd/diff/diffreg.c @@ -313,6 +313,7 @@ output(void) } if (m == 0) change(1, 0, 1, len[1]); + flushchanges(); } #define BUF 4096 |