From 37e67777884acea177d793619440f803d2b9979d Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 10 Sep 2003 09:31:46 +0000 Subject: [PATCH] Added comment stripping. --- Makefile | 6 +++--- comparator.xml | 6 +++++- main.c | 19 ++++++++++++++----- shred.h | 1 + shredtree.c | 8 ++++++++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 36d9ce3..a69cd9c 100644 --- a/Makefile +++ b/Makefile @@ -34,11 +34,11 @@ scf-standard.html: scf-standard.xml xmlto html-nochunks scf-standard.xml test1: comparator - @comparator -w -C -d test test1-a test1-b + @comparator -C -d test test1-a test1-b test2: comparator - @comparator -w -C -d test test2-a test2-b + @comparator -C -d test test2-a test2-b test3: comparator - @comparator -w -C -d test test3-a test3-b + @comparator -C -d test test3-a test3-b install: comparator.1 uninstall install -m 755 -o 0 -g 0 -d $(ROOT)/usr/bin/ diff --git a/comparator.xml b/comparator.xml index f83f789..82fe0f9 100644 --- a/comparator.xml +++ b/comparator.xml @@ -17,11 +17,12 @@ comparator -c + -C -d dir -o file + -r -s shredsize -w - -C -x path @@ -99,6 +100,9 @@ applies C-language-specific normalization; { and } characters are removed. This is recommended for comparing C code; it prevents the comparison from being fooled by differences in indent style. +The option removes comments. This is +only effective in conjunction with . + The enables some debugging output. It is for developers; if you need to understand it, read the code. diff --git a/main.c b/main.c index f5c0f3b..e0dd202 100644 --- a/main.c +++ b/main.c @@ -86,6 +86,8 @@ static void write_scf(const char *tree, FILE *ofp) buf[0] = '\0'; if (remove_whitespace) strcat(buf, "remove-whitespace,"); + if (remove_comments) + strcat(buf, "remove-comments,"); if (remove_braces) strcat(buf, "remove-braces,"); if (buf[0]) @@ -331,10 +333,12 @@ void report_time(char *legend, ...) static void usage(void) { - fprintf(stderr,"usage: shredtree [-c] [-d dir ] [-o file] [-s shredsize] [-w] [-x] path...\n"); + fprintf(stderr,"usage: comparator [-c] [-C] [-d dir ] [-r] [-o file] [-s shredsize] [-w] [-x] path...\n"); fprintf(stderr," -c = generate SCF files\n"); + fprintf(stderr," -C = apply C normalizations\n"); fprintf(stderr," -d dir = change directory before digesting.\n"); fprintf(stderr," -o file = write to the specified file.\n"); + fprintf(stderr," -r = renove comments\n"); fprintf(stderr," -s size = set shred size (default %d)\n", shredsize); fprintf(stderr," -w = remove whitespace.\n"); fprintf(stderr," -x = debug, display chunks in output.\n"); @@ -352,7 +356,7 @@ main(int argc, char *argv[]) compile_only = file_only = 0; dir = outfile = NULL; - while ((status = getopt(argc, argv, "cCd:ho:s:wx")) != EOF) + while ((status = getopt(argc, argv, "cCd:ho:rs:wx")) != EOF) { switch (status) { @@ -361,7 +365,7 @@ main(int argc, char *argv[]) break; case 'C': - remove_braces = 1; + remove_whitespace = remove_braces = 1; break; case 'd': @@ -372,6 +376,9 @@ main(int argc, char *argv[]) outfile = optarg; break; + case 'r': + remove_comments = 1; + case 's': shredsize = atoi(optarg); break; @@ -480,9 +487,11 @@ main(int argc, char *argv[]) dummy_scf.hash_method = "MD5"; buf[0] == '\0'; if (remove_whitespace) - strcat(buf, "remove-whitespace,"); + strcat(buf, "remove-whitespace, "); + if (remove_comments) + strcat(buf, "remove-comments, "); if (remove_braces) - strcat(buf, "remove-braces,"); + strcat(buf, "remove-braces, "); if (buf[0]) buf[strlen(buf)-1] = '\0'; else diff --git a/shred.h b/shred.h index 548d5ca..dd52ba7 100644 --- a/shred.h +++ b/shred.h @@ -33,6 +33,7 @@ struct sorthash_t /* control bits, meant to be set at startup */ extern int remove_braces; extern int remove_whitespace; +extern int remove_comments; extern int debug; extern int shredsize; diff --git a/shredtree.c b/shredtree.c index a2fd18b..f8ca2ee 100644 --- a/shredtree.c +++ b/shredtree.c @@ -13,6 +13,7 @@ /* control bits, meant to be set at startup */ int remove_braces = 0; int remove_whitespace = 0; +int remove_comments = 0; int shredsize = 3; struct item @@ -68,6 +69,13 @@ static int eligible(const char *file) static int normalize(char *buf) /* normalize a buffer in place, return 0 if it should be skipped */ { + if (remove_comments && remove_braces) /* remove_c_comments */ + { + char *start = strstr(buf, "/*"), *end = strstr(buf, "*/"); + + if (start && end && start < end) + strcpy(start, end+2); + } if (remove_whitespace) /* strip whitespace, ignore blank lines */ { char *tp, *sp; -- 2.43.0