Browse code

print bytecode source code.

Török Edvin authored on 2010/02/07 00:53:17
Showing 2 changed files
... ...
@@ -120,6 +120,41 @@ 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
+static void print_src(const char *file)
124
+{
125
+  char buf[4096];
126
+  int nread, i, found = 0;
127
+  FILE *f = fopen(file, "r");
128
+  if (!f) {
129
+    fprintf(stderr,"Unable to reopen %s\n", file);
130
+    return;
131
+  }
132
+  do {
133
+    nread = fread(buf, 1, sizeof(buf), f);
134
+    for (i=0;i<nread-1;i++) {
135
+      if (buf[i] == '\n' && buf[i+1] == 'S') {
136
+        found = 1;
137
+        i++;
138
+        break;
139
+      }
140
+    }
141
+  } while (!found && (nread == sizeof(buf)));
142
+  printf("Source code:");
143
+  do {
144
+    for (;i+1<nread;i++) {
145
+      if (buf[i] == 'S' || buf[i] == '\n') {
146
+        putc('\n', stdout);
147
+        continue;
148
+      }
149
+      putc((buf[i]&0xf | ((buf[i+1]&0xf)<<4)), stdout);
150
+      i++;
151
+    }
152
+    i=0;
153
+    nread = fread(buf, 1, sizeof(buf), f);
154
+  } while (nread > 0);
155
+  fclose(f);
156
+}
157
+
123 158
 int main(int argc, char *argv[])
124 159
 {
125 160
     FILE *f;
... ...
@@ -210,6 +245,7 @@ int main(int argc, char *argv[])
210 210
     printf("Bytecode loaded\n");
211 211
     if (optget(opts, "describe")->enabled) {
212 212
 	cli_bytecode_describe(bc);
213
+        print_src(opts->filename[0]);
213 214
     } else {
214 215
 
215 216
 	ctx = cli_bytecode_context_alloc();
... ...
@@ -1229,7 +1229,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio, int tru
1229 1229
     unsigned linelength=0;
1230 1230
     char firstbuf[FILEBUFF];
1231 1231
     enum parse_state state;
1232
-    int rc;
1232
+    int rc, end=0;
1233 1233
 
1234 1234
     memset(bc, 0, sizeof(*bc));
1235 1235
     bc->trusted = trust;
... ...
@@ -1257,7 +1257,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio, int tru
1257 1257
 	return CL_EMEM;
1258 1258
     }
1259 1259
     state = PARSE_BC_LSIG;
1260
-    while (cli_dbgets(buffer, linelength, f, dbio)) {
1260
+    while (cli_dbgets(buffer, linelength, f, dbio) && !end) {
1261 1261
 	cli_chomp(buffer);
1262 1262
 	row++;
1263 1263
 	switch (state) {
... ...
@@ -1324,6 +1324,10 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio, int tru
1324 1324
 		}
1325 1325
 		/* fall-through */
1326 1326
 	    case PARSE_FUNC_HEADER:
1327
+                if (*buffer == 'S') {
1328
+		    end = 1;
1329
+		    break;
1330
+		}
1327 1331
 		rc = parseFunctionHeader(bc, current_func, (unsigned char*)buffer);
1328 1332
 		if (rc != CL_SUCCESS) {
1329 1333
 		    cli_errmsg("Error at bytecode line %u\n", row);