Browse code

fix clambc's printing of source code, and add separate cmdline for it.

Also disable debug by default, --debug can be used to enable.

Török Edvin authored on 2010/02/14 02:57:33
Showing 2 changed files
... ...
@@ -51,6 +51,8 @@ static void help(void)
51 51
     printf("clambc <file> [function] [param1 ...]\n\n");
52 52
     printf("    --help                 -h         Show help\n");
53 53
     printf("    --version              -V         Show version\n");
54
+    printf("    --info                 -i         Print information about bytecode\n");
55
+    printf("    --printsrc             -p         Print bytecode source\n");
54 56
     printf("    --trace <level>                   Set bytecode trace level 0..7 (default 7)\n");
55 57
     printf("    --no-trace-showsource             Don't show source line during tracing\n");
56 58
     printf("    file                              file to test\n");
... ...
@@ -120,6 +122,7 @@ static void tracehook_ptr(struct cli_bc_ctx *ctx, const void *ptr)
120 120
     fprintf(stderr, "[trace] %p\n", ptr);
121 121
 }
122 122
 
123
+extern uint8_t cli_debug_flag;
123 124
 static void print_src(const char *file)
124 125
 {
125 126
   char buf[4096];
... ...
@@ -139,7 +142,8 @@ static void print_src(const char *file)
139 139
       }
140 140
     }
141 141
   } while (!found && (nread == sizeof(buf)));
142
-  printf("Source code:");
142
+  if (cli_debug_flag)
143
+      printf("[clambc] Source code:");
143 144
   do {
144 145
     for (;i+1<nread;i++) {
145 146
       if (buf[i] == 'S' || buf[i] == '\n') {
... ...
@@ -149,6 +153,8 @@ static void print_src(const char *file)
149 149
       putc(((buf[i]&0xf) | ((buf[i+1]&0xf)<<4)), stdout);
150 150
       i++;
151 151
     }
152
+    if (i == nread-1 && nread != 1)
153
+	fseek(f, -1, SEEK_CUR);
152 154
     i=0;
153 155
     nread = fread(buf, 1, sizeof(buf), f);
154 156
   } while (nread > 0);
... ...
@@ -199,7 +205,9 @@ int main(int argc, char *argv[])
199 199
 	exit(3);
200 200
     }
201 201
 
202
-    cl_debug();
202
+    if (optget(opts,"debug")->enabled) {
203
+	cl_debug();
204
+    }
203 205
     rc = cl_init(CL_INIT_DEFAULT);
204 206
     if (rc != CL_SUCCESS) {
205 207
 	fprintf(stderr,"Unable to init libclamav: %s\n", cl_strerror(rc));
... ...
@@ -233,20 +241,22 @@ int main(int argc, char *argv[])
233 233
 	optfree(opts);
234 234
 	exit(4);
235 235
     }
236
-
237
-    rc = cli_bytecode_prepare(&bcs);
238
-    if (rc != CL_SUCCESS) {
239
-	fprintf(stderr,"Unable to prepare bytecode: %s\n", cl_strerror(rc));
240
-	optfree(opts);
241
-	exit(4);
242
-    }
243 236
     fclose(f);
244
-
245
-    printf("Bytecode loaded\n");
246
-    if (optget(opts, "describe")->enabled) {
237
+    if (cli_debug_flag)
238
+    printf("[clambc] Bytecode loaded\n");
239
+    if (optget(opts, "info")->enabled) {
247 240
 	cli_bytecode_describe(bc);
241
+    } else if (optget(opts, "printsrc")->enabled) {
248 242
         print_src(opts->filename[0]);
249 243
     } else {
244
+	rc = cli_bytecode_prepare(&bcs);
245
+	if (rc != CL_SUCCESS) {
246
+	    fprintf(stderr,"Unable to prepare bytecode: %s\n", cl_strerror(rc));
247
+	    optfree(opts);
248
+	    exit(4);
249
+	}
250
+	if (cli_debug_flag)
251
+	    printf("[clambc] Bytecode prepared\n");
250 252
 
251 253
 	ctx = cli_bytecode_context_alloc();
252 254
 	if (!ctx) {
... ...
@@ -269,7 +279,8 @@ int main(int argc, char *argv[])
269 269
 	    funcid = atoi(opts->filename[1]);
270 270
 	}
271 271
 	cli_bytecode_context_setfuncid(ctx, bc, funcid);
272
-	printf("Running bytecode function :%u\n", funcid);
272
+	if (cli_debug_flag)
273
+	    printf("[clambc] Running bytecode function :%u\n", funcid);
273 274
 
274 275
 	if (opts->filename[1]) {
275 276
 	    i=2;
... ...
@@ -308,9 +319,11 @@ int main(int argc, char *argv[])
308 308
 	    fprintf(stderr,"Unable to run bytecode: %s\n", cl_strerror(rc));
309 309
 	} else {
310 310
 	    uint64_t v;
311
-	    printf("Bytecode run finished\n");
311
+	    if (cli_debug_flag)
312
+		printf("[clambc] Bytecode run finished\n");
312 313
 	    v = cli_bytecode_context_getresult_int(ctx);
313
-	    printf("Bytecode returned: 0x%llx\n", (long long)v);
314
+	    if (cli_debug_flag)
315
+		printf("[clambc] Bytecode returned: 0x%llx\n", (long long)v);
314 316
 	}
315 317
 	cli_bytecode_context_destroy(ctx);
316 318
     }
... ...
@@ -320,6 +333,7 @@ int main(int argc, char *argv[])
320 320
     optfree(opts);
321 321
     if (fd != -1)
322 322
 	close(fd);
323
-    printf("Exiting\n");
323
+    if (cli_debug_flag)
324
+	printf("[clambc] Exiting\n");
324 325
     return 0;
325 326
 }
... ...
@@ -66,7 +66,7 @@ const struct clam_option __clam_options[] = {
66 66
     { NULL, "config-file", 0, TYPE_STRING, NULL, 0, CONFDIR_FRESHCLAM, FLAG_REQUIRED, OPT_FRESHCLAM, "", "" },
67 67
     { NULL, "config-file", 'c', TYPE_STRING, NULL, 0, CONFDIR_MILTER, FLAG_REQUIRED, OPT_MILTER, "", "" },
68 68
     { NULL, "version", 'V', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL | OPT_MILTER | OPT_CLAMCONF | OPT_CLAMDTOP | OPT_CLAMBC, "", "" },
69
-    { NULL, "debug", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_SIGTOOL, "", "" },
69
+    { NULL, "debug", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC | OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_SIGTOOL, "", "" },
70 70
     { NULL, "verbose", 'v', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL, "", "" },
71 71
     { NULL, "quiet", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL, "", "" },
72 72
     { NULL, "leave-temps", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMSCAN, "", "" },
... ...
@@ -121,7 +121,8 @@ const struct clam_option __clam_options[] = {
121 121
     { NULL, "generate-config", 'g', TYPE_STRING, NULL, -1, NULL, 0, OPT_CLAMCONF, "", "" },
122 122
 
123 123
     { NULL, "force-interpreter", 'f', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC, "Force using the interpreter instead of the JIT", "" },
124
-    { NULL, "describe", 'd', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC, "Load and describe bytecode without executing", ""},
124
+    { NULL, "info", 'i', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC, "Load and print bytecode information without executing", ""},
125
+    { NULL, "printsrc", 'p', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC, "Print source code of bytecode", ""},
125 126
     { NULL, "input", 'i', TYPE_STRING, NULL, -1, NULL, 0, OPT_CLAMBC, "Input file to run the bytecode n", ""},
126 127
     { NULL, "trace", 't', TYPE_NUMBER, MATCH_NUMBER, 7, NULL, 0, OPT_CLAMBC, "bytecode trace level",""},
127 128
     { NULL, "no-trace-showsource", 's', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMBC, "Don't show source line during tracing",""},