From 4b3ac76cb8e011181c9a3045038a454a6e11cebf Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 19 Sep 2003 12:35:49 +0000 Subject: [PATCH] Option handling is now specific to a feature analyzer. --- linebyline.c | 30 ++++++++++++++++++++++++++---- main.c | 32 ++++++++------------------------ shred.h | 10 ++++++---- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/linebyline.c b/linebyline.c index 7b212b4..e705371 100644 --- a/linebyline.c +++ b/linebyline.c @@ -12,9 +12,9 @@ NAME: #include "shred.h" /* control bits */ -int remove_braces = 0; -int remove_whitespace = 0; -int remove_comments = 0; +static int remove_braces = 0; +static int remove_whitespace = 0; +static int remove_comments = 0; static char *c_patterns[] = { /* Idioms that don't convey any meaning in isolation */ @@ -51,7 +51,7 @@ static regex_t shell_regexps[sizeof(shell_patterns)/sizeof(*shell_patterns)]; static linenum_t linecount; -void analyzer_init(void) +void analyzer_init(unsigned int flags) /* initialize line filtering */ { int i; @@ -70,6 +70,13 @@ void analyzer_init(void) shell_patterns[i]); exit(1); } + + if (flags & CAPC_FLAG) + remove_braces = remove_whitespace = 1; + if (flags & R_FLAG) + remove_comments = 1; + if (flags & W_FLAG) + remove_whitespace = 1; } static unsigned char active = 0; @@ -260,6 +267,20 @@ void analyzer_free(const char *text) free(text); } +void analyzer_dump(char *buf) +/* dump a Normalization line representing current options */ +{ + strcpy(buf, "line-oriented, "); + if (remove_whitespace) + strcat(buf, "remove-whitespace, "); + if (remove_comments) + strcat(buf, "remove-comments, "); + if (remove_braces) + strcat(buf, "remove-braces, "); + if (buf[0]) + buf[strlen(buf)-2] = '\0'; +} + /* our method table */ struct analyzer_t linebyline = { @@ -267,6 +288,7 @@ struct analyzer_t linebyline = mode: analyzer_mode, get: analyzer_get, free: analyzer_free, + dump: analyzer_dump, }; #ifdef TEST diff --git a/main.c b/main.c index 037b15d..b03aae5 100644 --- a/main.c +++ b/main.c @@ -97,22 +97,6 @@ void extend_current_chunk(linenum_t linenumber) chunk_buffer[chunk_count-1].end = linenumber; } -static void write_options(char *buf) -/* dump a Normalization line representing current options */ -{ - 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]) - buf[strlen(buf)-2] = '\0'; - else - strcpy(buf, "none"); -} - static void write_scf(const char *tree, FILE *ofp) /* generate shred file for given tree */ { @@ -134,7 +118,7 @@ static void write_scf(const char *tree, FILE *ofp) fputs("#SCF-A 2.0\n", ofp); fputs("Generator-Program: comparator 1.0\n", ofp); fputs("Hash-Method: " HASHMETHOD "\n", ofp); - write_options(buf); + linebyline.dump(buf); fprintf(ofp, "Normalization: %s\n", buf); fprintf(ofp, "Root: %s\n", tree); fprintf(ofp, "Shred-Size: %d\n", shredsize); @@ -346,7 +330,7 @@ static void init_scf(char *file, struct scf_t *scf, const int readfile) char buf[BUFSIZ]; scf->hash_method = HASHMETHOD; - write_options(buf); + linebyline.dump(buf); scf->normalization = strdup(buf); scf->shred_size = shredsize; scf->generator_program = "comparator " VERSION; @@ -452,11 +436,11 @@ main(int argc, char *argv[]) extern char *optarg; /* set by getopt */ extern int optind; /* set by getopt */ - int status, file_only, compile_only, argcount, mergecount; + int status, file_only, compile_only, argcount, mergecount, flags; struct scf_t *scf; char *dir, *outfile; - compile_only = file_only = nofilter = 0; + compile_only = file_only = nofilter = flags = 0; dir = outfile = NULL; while ((status = getopt(argc, argv, "cCd:hm:no:rs:vwx")) != EOF) { @@ -467,7 +451,7 @@ main(int argc, char *argv[]) break; case 'C': - remove_whitespace = remove_braces = 1; + flags |= CAPC_FLAG; break; case 'd': @@ -487,7 +471,7 @@ main(int argc, char *argv[]) break; case 'r': - remove_comments = 1; + flags |= R_FLAG; break; case 's': @@ -499,7 +483,7 @@ main(int argc, char *argv[]) break; case 'w': - remove_whitespace = 1; + flags |= W_FLAG; break; case 'x': @@ -522,7 +506,7 @@ main(int argc, char *argv[]) usage(); report_time(NULL); - linebyline.init(); + linebyline.init(flags); /* special case if user gave exactly one tree */ if (!compile_only && argcount == 1) diff --git a/shred.h b/shred.h index c40f7f0..585e6bc 100644 --- a/shred.h +++ b/shred.h @@ -61,16 +61,18 @@ feature_t; struct analyzer_t /* structure describing a feature analyzer */ { - void (*init)(void); + unsigned int flags; +#define CAPC_FLAG 0x01 +#define R_FLAG 0x02 +#define W_FLAG 0x04 + void (*init)(unsigned int); void (*mode)(int); feature_t *(*get)(const struct filehdr_t *, FILE *, linenum_t *); void (*free)(const char *); + void (*dump)(char *); }; /* control bits, meant to be set at startup */ -extern int remove_braces; -extern int remove_whitespace; -extern int remove_comments; extern int verbose, debug, nofilter; extern int shredsize, minsize; -- 2.43.0