]> thyrsus.com Git - comparator.git/commitdiff
Option handling is now specific to a feature analyzer.
authorEric S. Raymond <esr@thyrsus.com>
Fri, 19 Sep 2003 12:35:49 +0000 (12:35 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Fri, 19 Sep 2003 12:35:49 +0000 (12:35 +0000)
linebyline.c
main.c
shred.h

index 7b212b4633d255bd1c477aaf46d58b8aaa1ecf60..e705371846b68f2648428360030577b9e015ead8 100644 (file)
@@ -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 037b15ddbdc0ba06abf9915769112eb3ea813fae..b03aae50bf26ab968c9c861572c7ad42b383a3b5 100644 (file)
--- 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 c40f7f066ec4109fd0b09d7a582b3fcc371cbed7..585e6bcbfa199c82485f990d172ff1040c3deddf 100644 (file)
--- 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;