diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-01-07 17:18:33 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-01-07 17:18:33 +0000 |
commit | 117049525ee6be7d59e01041ec9eb692798d62e8 (patch) | |
tree | 342da7d905a122a07121983b1af4916e5cc65e9f /usr.bin/diff | |
parent | e960df005df407b312c60da2a9c37f141207e694 (diff) |
Implement -p option.
"works here" millert@ ok miod@ deraadt@
Diffstat (limited to 'usr.bin/diff')
-rw-r--r-- | usr.bin/diff/diff.c | 20 | ||||
-rw-r--r-- | usr.bin/diff/diff.h | 6 | ||||
-rw-r--r-- | usr.bin/diff/diffreg.c | 67 |
3 files changed, 76 insertions, 17 deletions
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index 5fa299fa73f..7ad00106cae 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.43 2003/10/07 23:37:27 millert Exp $ */ +/* $OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $ */ /* * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> @@ -21,7 +21,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diff.c,v 1.43 2003/10/07 23:37:27 millert Exp $"; +static const char rcsid[] = "$OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -40,14 +40,14 @@ static const char rcsid[] = "$OpenBSD: diff.c,v 1.43 2003/10/07 23:37:27 millert #include "diff.h" -int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, rflag; +int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag; int sflag, tflag, Tflag, wflag; int format, context, status; char *start, *ifdefname, *diffargs, *label; struct stat stb1, stb2; struct excludes *excludes_list; -#define OPTIONS "0123456789abC:cdD:efhiL:lnNPqrS:sTtU:uwX:x:" +#define OPTIONS "0123456789abC:cdD:efhiL:lnNPpqrS:sTtU:uwX:x:" static struct option longopts[] = { { "text", no_argument, 0, 'a' }, { "ignore-space-change", no_argument, 0, 'b' }, @@ -62,6 +62,7 @@ static struct option longopts[] = { { "new-file", no_argument, 0, 'N' }, { "rcs", no_argument, 0, 'n' }, { "unidirectional-new-file", no_argument, 0, 'P' }, + { "show-c-function", no_argument, 0, 'p' }, { "brief", no_argument, 0, 'q' }, { "recursive", no_argument, 0, 'r' }, { "report-identical-files", no_argument, 0, 's' }, @@ -154,6 +155,9 @@ main(int argc, char **argv) case 'n': format = D_NREVERSE; break; + case 'p': + pflag = 1; + break; case 'P': Pflag = 1; break; @@ -388,11 +392,11 @@ __dead void usage(void) { (void)fprintf(stderr, - "usage: diff [-abdilqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" - " diff [-abdilqtTw] [-L label] -C number file1 file2\n" + "usage: diff [-abdilpqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" + " diff [-abdilpqtTw] [-L label] -C number file1 file2\n" " diff [-abdilqtw] -D string file1 file2\n" - " diff [-abdilqtTw] [-L label] -U number file1 file2\n" - " diff [-abdilNPqtTw] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s]\n" + " diff [-abdilpqtTw] [-L label] -U number file1 file2\n" + " diff [-abdilNPpqtTw] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s]\n" " [-S name] [-X file] [-x pattern] dir1 dir2\n"); exit(2); diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index 538d67df19d..7f658699fa3 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.h,v 1.25 2003/11/09 20:13:57 otto Exp $ */ +/* $OpenBSD: diff.h,v 1.26 2004/01/07 17:18:32 otto Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -68,8 +68,8 @@ struct excludes { struct excludes *next; }; -extern int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, rflag, sflag, - tflag, Tflag, wflag; +extern int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag, + sflag, tflag, Tflag, wflag; extern int format, context, status; extern char *start, *ifdefname, *diffargs, *label; extern struct stat stb1, stb2; diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index 74645a3c794..e2e60305f44 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.54 2003/11/22 18:02:44 millert Exp $ */ +/* $OpenBSD: diffreg.c,v 1.55 2004/01/07 17:18:32 otto Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -65,7 +65,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.54 2003/11/22 18:02:44 millert Exp $"; +static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.55 2004/01/07 17:18:32 otto Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -195,6 +195,11 @@ static struct context_vec *context_vec_start; static struct context_vec *context_vec_end; static struct context_vec *context_vec_ptr; +#define FUNCTION_CONTEXT_SIZE 41 +static char lastbuf[FUNCTION_CONTEXT_SIZE]; +static int lastline; +static int lastmatchline; + static FILE *opentemp(const char *); static void output(char *, FILE *, char *, FILE *); static void check(char *, FILE *, char *, FILE *); @@ -220,6 +225,7 @@ static int readhash(FILE *); static int files_differ(FILE *, FILE *, int); static __inline int min(int, int); static __inline int max(int, int); +static char *match_function(const long *, int, FILE *); /* @@ -292,6 +298,8 @@ diffreg(char *ofile1, char *ofile2, int flags) pid_t pid = -1; anychange = 0; + lastline = 0; + lastmatchline = 0; context_vec_ptr = context_vec_start - 1; chrtran = (iflag ? cup2low : clow2low); if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode)) @@ -1241,6 +1249,37 @@ static __inline int max(int a, int b) return (a > b ? a : b); } +static char * +match_function(const long *f, int pos, FILE *file) +{ + char buf[FUNCTION_CONTEXT_SIZE]; + size_t nc; + int last = lastline; + char *p; + + lastline = pos; + while (pos > last) { + fseek(file, f[pos - 1], SEEK_SET); + nc = f[pos] - f[pos - 1]; + if (nc >= sizeof(buf)) + nc = sizeof(buf) - 1; + nc = fread(buf, 1, nc, file); + if (nc > 0) { + buf[nc] = '\0'; + p = strchr(buf, '\n'); + if (p != NULL) + *p = '\0'; + if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') { + strlcpy(lastbuf, buf, sizeof lastbuf); + lastmatchline = pos; + return lastbuf; + } + } + pos--; + } + return lastmatchline > 0 ? lastbuf : NULL; +} + /* dump accumulated "context" diff changes */ static void dump_context_vec(FILE *f1, FILE *f2) @@ -1248,7 +1287,7 @@ dump_context_vec(FILE *f1, FILE *f2) struct context_vec *cvp = context_vec_start; int lowa, upb, lowc, upd, do_output; int a, b, c, d; - char ch; + char ch, *f; if (context_vec_start > context_vec_ptr) return; @@ -1259,7 +1298,15 @@ dump_context_vec(FILE *f1, FILE *f2) lowc = max(1, cvp->c - context); upd = min(len[1], context_vec_ptr->d + context); - printf("***************\n*** "); + printf("***************"); + if (pflag) { + f = match_function(ixold, lowa-1, f1); + if (f != NULL) { + putchar(' '); + fputs(f, stdout); + } + } + printf("\n*** "); range(lowa, upb, ","); printf(" ****\n"); @@ -1345,7 +1392,7 @@ dump_unified_vec(FILE *f1, FILE *f2) struct context_vec *cvp = context_vec_start; int lowa, upb, lowc, upd; int a, b, c, d; - char ch; + char ch, *f; if (context_vec_start > context_vec_ptr) return; @@ -1360,7 +1407,15 @@ dump_unified_vec(FILE *f1, FILE *f2) uni_range(lowa, upb); fputs(" +", stdout); uni_range(lowc, upd); - fputs(" @@\n", stdout); + fputs(" @@", stdout); + if (pflag) { + f = match_function(ixold, lowa-1, f1); + if (f != NULL) { + putchar(' '); + fputs(f, stdout); + } + } + putchar('\n'); /* * Output changes in "unified" diff format--the old and new lines |