aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 13:50:22 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-16 13:50:22 -0300
commitc9a2dfeb2c71b1c6b8164a1e5aad4b178950e197 (patch)
tree6f099e983ecb968f625a8824ac2ca78e625882e3
parent9fe5be3acf340b90fe0c24d36a601adaf6f21c79 (diff)
downloadlua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.gz
lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.bz2
lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.zip
using "zio" for parsing Lua code.
-rw-r--r--inout.c111
-rw-r--r--inout.h2
-rw-r--r--lex.c30
-rw-r--r--lex.h7
-rw-r--r--makefile34
-rw-r--r--opcode.c52
-rw-r--r--opcode.h3
-rw-r--r--undump.c105
-rw-r--r--undump.h7
9 files changed, 149 insertions, 202 deletions
diff --git a/inout.c b/inout.c
index 50782703..feecf9bc 100644
--- a/inout.c
+++ b/inout.c
@@ -5,22 +5,24 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.60 1997/06/09 17:28:14 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
12 12
13#include "auxlib.h" 13#include "auxlib.h"
14#include "lex.h" 14#include "fallback.h"
15#include "opcode.h" 15#include "hash.h"
16#include "inout.h" 16#include "inout.h"
17#include "table.h" 17#include "lex.h"
18#include "tree.h"
19#include "lua.h" 18#include "lua.h"
20#include "hash.h"
21#include "luamem.h" 19#include "luamem.h"
22#include "fallback.h"
23#include "luamem.h" 20#include "luamem.h"
21#include "opcode.h"
22#include "table.h"
23#include "tree.h"
24#include "undump.h"
25#include "zio.h"
24 26
25 27
26/* Exported variables */ 28/* Exported variables */
@@ -34,73 +36,58 @@ char *luaI_typenames[] = { /* ORDER LUA_T */
34 NULL 36 NULL
35}; 37};
36 38
37static FILE *fp;
38static char *st;
39 39
40/*
41** Function to get the next character from the input file
42*/
43static int fileinput (void)
44{
45 int c = fgetc(fp);
46 return (c == EOF) ? 0 : c;
47}
48 40
49/* 41int lua_dofile (char *filename)
50** Function to get the next character from the input string
51*/
52static int stringinput (void)
53{ 42{
54 return *st++; 43 int status;
55} 44 int c;
45 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
46 if (f == NULL)
47 return 2;
48 lua_parsedfile = luaI_createfixedstring(filename?filename:"(stdin)")->str;
49 c = fgetc(f);
50 ungetc(c, f);
51 if (c == ID_CHUNK) {
52 ZIO z;
53 f = freopen(filename, "rb", f); /* set binary mode */
54 zFopen(&z, f);
55 lua_setinput(&z);
56 status = luaI_undump(&z);
57 zclose(&z);
58 }
59 else {
60 ZIO z;
61 if (c == '#')
62 while ((c=fgetc(f)) != '\n') /* skip first line */;
63 zFopen(&z, f);
64 lua_setinput(&z);
65 status = lua_domain();
66 zclose(&z);
67 }
68 return status;
69}
56 70
57/*
58** Function to open a file to be input unit.
59** Return the file.
60*/
61FILE *lua_openfile (char *fn)
62{
63 lua_setinput (fileinput);
64 if (fn == NULL)
65 {
66 fp = stdin;
67 fn = "(stdin)";
68 }
69 else
70 fp = fopen (fn, "r");
71 lua_parsedfile = luaI_createfixedstring(fn)->str;
72 return fp;
73}
74 71
75/*
76** Function to close an opened file
77*/
78void lua_closefile (void)
79{
80 if (fp != stdin)
81 fclose(fp);
82}
83 72
84/*
85** Function to open a string to be input unit
86*/
87#define SIZE_PREF 20 /* size of string prefix to appear in error messages */ 73#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
88void lua_openstring (char *s) 74
75int lua_dostring (char *str)
89{ 76{
77 int status;
90 char buff[SIZE_PREF+25]; 78 char buff[SIZE_PREF+25];
91 lua_setinput(stringinput); 79 ZIO z;
92 st = s; 80 if (str == NULL) return 1;
93 sprintf(buff, "(dostring) >> %.20s%s", s, 81 sprintf(buff, "(dostring) >> %.20s%s", str,
94 (strlen(s) > SIZE_PREF) ? "..." : ""); 82 (strlen(str) > SIZE_PREF) ? "..." : "");
95 lua_parsedfile = luaI_createfixedstring(buff)->str; 83 lua_parsedfile = luaI_createfixedstring(buff)->str;
84 zsopen(&z, str);
85 lua_setinput(&z);
86 status = lua_domain();
87 zclose(&z);
88 return status;
96} 89}
97 90
98/*
99** Function to close an opened string
100*/
101void lua_closestring (void)
102{
103}
104 91
105 92
106 93
diff --git a/inout.h b/inout.h
index 1e544143..c1697d1a 100644
--- a/inout.h
+++ b/inout.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: inout.h,v 1.16 1996/05/28 21:07:32 roberto Exp roberto $ 2** $Id: inout.h,v 1.17 1997/02/26 17:38:41 roberto Unstable roberto $
3*/ 3*/
4 4
5 5
diff --git a/lex.c b/lex.c
index 4cfc7e13..9d592293 100644
--- a/lex.c
+++ b/lex.c
@@ -1,4 +1,4 @@
1char *rcs_lex = "$Id: lex.c,v 3.4 1997/06/11 14:20:51 roberto Exp $"; 1char *rcs_lex = "$Id: lex.c,v 3.4 1997/06/11 18:56:02 roberto Exp roberto $";
2 2
3 3
4#include <ctype.h> 4#include <ctype.h>
@@ -15,13 +15,13 @@ char *rcs_lex = "$Id: lex.c,v 3.4 1997/06/11 14:20:51 roberto Exp $";
15 15
16#define MINBUFF 250 16#define MINBUFF 250
17 17
18#define next() (current = input()) 18static int current; /* look ahead character */
19#define save(x) (yytext[tokensize++] = (x)) 19static ZIO *lex_z;
20#define save_and_next() (save(current), next())
21 20
22 21
23static int current; /* look ahead character */ 22#define next() (current = zgetc(lex_z))
24static Input input; /* input function */ 23#define save(x) (yytext[tokensize++] = (x))
24#define save_and_next() (save(current), next())
25 25
26 26
27#define MAX_IFS 5 27#define MAX_IFS 5
@@ -37,14 +37,14 @@ static struct {
37static int iflevel; /* level of nested $if's */ 37static int iflevel; /* level of nested $if's */
38 38
39 39
40void lua_setinput (Input fn) 40void lua_setinput (ZIO *z)
41{ 41{
42 current = '\n'; 42 current = '\n';
43 lua_linenumber = 0; 43 lua_linenumber = 0;
44 iflevel = 0; 44 iflevel = 0;
45 ifstate[0].skip = 0; 45 ifstate[0].skip = 0;
46 ifstate[0].elsepart = 1; /* to avoid a free $else */ 46 ifstate[0].elsepart = 1; /* to avoid a free $else */
47 input = fn; 47 lex_z = z;
48} 48}
49 49
50 50
@@ -155,7 +155,7 @@ static void ifskip (void)
155 while (ifstate[iflevel].skip) { 155 while (ifstate[iflevel].skip) {
156 if (current == '\n') 156 if (current == '\n')
157 inclinenumber(); 157 inclinenumber();
158 else if (current == 0) 158 else if (current == EOZ)
159 luaI_auxsyntaxerror("input ends inside a $if"); 159 luaI_auxsyntaxerror("input ends inside a $if");
160 else next(); 160 else next();
161 } 161 }
@@ -183,7 +183,7 @@ static void inclinenumber (void)
183 break; 183 break;
184 case 2: /* endinput */ 184 case 2: /* endinput */
185 if (!skip) { 185 if (!skip) {
186 current = 0; 186 current = EOZ;
187 iflevel = 0; /* to allow $endinput inside a $if */ 187 iflevel = 0; /* to allow $endinput inside a $if */
188 } 188 }
189 break; 189 break;
@@ -216,7 +216,7 @@ static void inclinenumber (void)
216 skipspace(); 216 skipspace();
217 if (current == '\n') /* pragma must end with a '\n' ... */ 217 if (current == '\n') /* pragma must end with a '\n' ... */
218 inclinenumber(); 218 inclinenumber();
219 else if (current != 0) /* or eof */ 219 else if (current != EOZ) /* or eof */
220 luaI_auxsyntaxerror("invalid pragma format"); 220 luaI_auxsyntaxerror("invalid pragma format");
221 ifskip(); 221 ifskip();
222 } 222 }
@@ -232,7 +232,7 @@ static int read_long_string (char *yytext, int buffsize)
232 yytext = luaI_buffer(buffsize *= 2); 232 yytext = luaI_buffer(buffsize *= 2);
233 switch (current) 233 switch (current)
234 { 234 {
235 case 0: 235 case EOZ:
236 save(0); 236 save(0);
237 return WRONGTOKEN; 237 return WRONGTOKEN;
238 case '[': 238 case '[':
@@ -295,7 +295,7 @@ int luaY_lex (void)
295 case '-': 295 case '-':
296 save_and_next(); 296 save_and_next();
297 if (current != '-') return '-'; 297 if (current != '-') return '-';
298 do { next(); } while (current != '\n' && current != 0); 298 do { next(); } while (current != '\n' && current != EOZ);
299 continue; 299 continue;
300 300
301 case '[': 301 case '[':
@@ -338,7 +338,7 @@ int luaY_lex (void)
338 yytext = luaI_buffer(buffsize *= 2); 338 yytext = luaI_buffer(buffsize *= 2);
339 switch (current) 339 switch (current)
340 { 340 {
341 case 0: 341 case EOZ:
342 case '\n': 342 case '\n':
343 save(0); 343 save(0);
344 return WRONGTOKEN; 344 return WRONGTOKEN;
@@ -455,7 +455,7 @@ int luaY_lex (void)
455 return NUMBER; 455 return NUMBER;
456 } 456 }
457 457
458 case 0: 458 case EOZ:
459 save(0); 459 save(0);
460 if (iflevel > 0) 460 if (iflevel > 0)
461 luaI_syntaxerror("missing $endif"); 461 luaI_syntaxerror("missing $endif");
diff --git a/lex.h b/lex.h
index d6789827..44172325 100644
--- a/lex.h
+++ b/lex.h
@@ -1,16 +1,15 @@
1/* 1/*
2** lex.h 2** lex.h
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** $Id: lex.h,v 1.2 1996/02/14 13:35:51 roberto Exp roberto $ 4** $Id: lex.h,v 1.3 1996/11/08 12:49:35 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef lex_h 7#ifndef lex_h
8#define lex_h 8#define lex_h
9 9
10#include "zio.h"
10 11
11typedef int (*Input) (void); 12void lua_setinput (ZIO *z);
12
13void lua_setinput (Input fn);
14void luaI_syntaxerror (char *s); 13void luaI_syntaxerror (char *s);
15int luaY_lex (void); 14int luaY_lex (void);
16void luaI_addReserved (void); 15void luaI_addReserved (void);
diff --git a/makefile b/makefile
index bc56a431..7fe53d50 100644
--- a/makefile
+++ b/makefile
@@ -1,4 +1,4 @@
1# $Id: makefile,v 1.33 1997/04/06 14:08:08 roberto Exp roberto $ 1# $Id: makefile,v 1.34 1997/06/11 18:57:00 roberto Exp roberto $
2 2
3#configuration 3#configuration
4 4
@@ -32,7 +32,8 @@ LUAOBJS = \
32 luamem.o \ 32 luamem.o \
33 func.o \ 33 func.o \
34 undump.o \ 34 undump.o \
35 auxlib.o 35 auxlib.o \
36 zio.o
36 37
37LIBOBJS = \ 38LIBOBJS = \
38 iolib.o \ 39 iolib.o \
@@ -77,29 +78,32 @@ clear :
77 co $@ 78 co $@
78 79
79 80
80auxlib.o: auxlib.c lua.h auxlib.h 81auxlib.o: auxlib.c lua.h auxlib.h luadebug.h
81fallback.o: fallback.c auxlib.h lua.h luamem.h fallback.h opcode.h \ 82fallback.o: fallback.c auxlib.h lua.h luamem.h fallback.h opcode.h \
82 types.h tree.h func.h table.h hash.h 83 types.h tree.h func.h table.h hash.h
83func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \ 84func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \
84 luamem.h 85 luamem.h
85hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \ 86hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \
86 table.h auxlib.h 87 table.h auxlib.h
87inout.o: inout.c auxlib.h lua.h lex.h opcode.h types.h tree.h func.h \ 88inout.o: inout.c auxlib.h lua.h fallback.h opcode.h types.h tree.h \
88 inout.h table.h hash.h luamem.h fallback.h 89 func.h hash.h inout.h lex.h zio.h luamem.h table.h undump.h
89iolib.o: iolib.c lua.h auxlib.h luadebug.h lualib.h 90iolib.o: iolib.c lua.h auxlib.h luadebug.h lualib.h
90lex.o: lex.c auxlib.h lua.h luamem.h tree.h types.h table.h opcode.h \ 91lex.o: lex.c auxlib.h lua.h luamem.h tree.h types.h table.h opcode.h \
91 func.h lex.h inout.h luadebug.h parser.h 92 func.h lex.h zio.h inout.h luadebug.h parser.h
92lua.o: lua.c lua.h auxlib.h lualib.h 93lua.o: lua.c lua.h auxlib.h lualib.h
93luamem.o: luamem.c luamem.h lua.h 94luamem.o: luamem.c luamem.h lua.h
94mathlib.o: mathlib.c lualib.h lua.h auxlib.h 95mathlib.o: mathlib.c lualib.h lua.h auxlib.h
95opcode.o: opcode.c luadebug.h lua.h luamem.h opcode.h types.h tree.h \ 96opcode.o: opcode.c luadebug.h lua.h luamem.h opcode.h types.h tree.h \
96 func.h hash.h inout.h table.h fallback.h undump.h auxlib.h 97 func.h hash.h inout.h table.h fallback.h auxlib.h lex.h zio.h
97parser.o: parser.c luadebug.h lua.h luamem.h lex.h opcode.h types.h \ 98parser.o: parser.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
98 tree.h func.h hash.h inout.h table.h 99 types.h tree.h func.h hash.h inout.h table.h
99strlib.o: strlib.c lua.h auxlib.h lualib.h 100strlib.o: strlib.c lua.h auxlib.h lualib.h
100table.o: table.c luamem.h auxlib.h lua.h opcode.h types.h tree.h \ 101table.o: table.c luamem.h auxlib.h lua.h func.h types.h tree.h \
101 func.h hash.h table.h inout.h fallback.h luadebug.h 102 opcode.h hash.h table.h inout.h fallback.h luadebug.h
102tree.o: tree.c luamem.h lua.h tree.h types.h lex.h hash.h opcode.h \ 103tree.o: tree.c luamem.h lua.h tree.h types.h lex.h zio.h hash.h \
103 func.h table.h fallback.h 104 opcode.h func.h table.h fallback.h
104undump.o: undump.c opcode.h lua.h types.h tree.h func.h luamem.h \ 105undump.o: undump.c auxlib.h lua.h opcode.h types.h tree.h func.h \
105 table.h undump.h 106 luamem.h table.h undump.h zio.h
107y.tab.o: y.tab.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
108 types.h tree.h func.h hash.h inout.h table.h
109zio.o: zio.c zio.h
diff --git a/opcode.c b/opcode.c
index 84b69c80..04a5b5ab 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 4.7 1997/06/09 17:28:14 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 4.8 1997/06/12 18:27:29 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -18,8 +18,8 @@ char *rcs_opcode="$Id: opcode.c,v 4.7 1997/06/09 17:28:14 roberto Exp roberto $"
18#include "table.h" 18#include "table.h"
19#include "lua.h" 19#include "lua.h"
20#include "fallback.h" 20#include "fallback.h"
21#include "undump.h"
22#include "auxlib.h" 21#include "auxlib.h"
22#include "lex.h"
23 23
24#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0)) 24#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
25#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0)) 25#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
@@ -597,7 +597,8 @@ int luaI_dorun (TFunc *tf)
597 return status; 597 return status;
598} 598}
599 599
600static int do_protectedmain (lua_CFunction closef) 600
601int lua_domain (void)
601{ 602{
602 TFunc tf; 603 TFunc tf;
603 int status; 604 int status;
@@ -614,7 +615,6 @@ static int do_protectedmain (lua_CFunction closef)
614 adjustC(0); /* erase extra slot */ 615 adjustC(0); /* erase extra slot */
615 status = 1; 616 status = 1;
616 } 617 }
617 closef();
618 if (status == 0) 618 if (status == 0)
619 status = luaI_dorun(&tf); 619 status = luaI_dorun(&tf);
620 errorJmp = oldErr; 620 errorJmp = oldErr;
@@ -622,7 +622,6 @@ static int do_protectedmain (lua_CFunction closef)
622 return status; 622 return status;
623} 623}
624 624
625
626/* 625/*
627** Execute the given lua function. Return 0 on success or 1 on error. 626** Execute the given lua function. Return 0 on success or 1 on error.
628*/ 627*/
@@ -640,47 +639,6 @@ int lua_callfunction (lua_Object function)
640 639
641 640
642/* 641/*
643** Open file, generate opcode and execute global statement. Return 0 on
644** success or non 0 on error.
645*/
646int lua_dofile (char *filename)
647{
648 int status;
649 int c;
650 FILE *f = lua_openfile(filename);
651 if (f == NULL)
652 return 2;
653 c = fgetc(f);
654 ungetc(c, f);
655 if (c == ID_CHUNK) {
656 f = freopen(filename, "rb", f); /* set binary mode */
657 status = luaI_undump(f);
658 lua_closefile();
659 }
660 else {
661 if (c == '#')
662 while ((c=fgetc(f)) != '\n') /* skip first line */;
663 status = do_protectedmain(lua_closefile);
664 }
665 return status;
666}
667
668/*
669** Generate opcode stored on string and execute global statement. Return 0 on
670** success or non 0 on error.
671*/
672int lua_dostring (char *str)
673{
674 int status;
675 if (str == NULL)
676 return 1;
677 lua_openstring(str);
678 status = do_protectedmain(lua_closestring);
679 return status;
680}
681
682
683/*
684** API: set a function as a fallback 642** API: set a function as a fallback
685*/ 643*/
686lua_Object lua_setfallback (char *name, lua_CFunction fallback) 644lua_Object lua_setfallback (char *name, lua_CFunction fallback)
@@ -1058,7 +1016,7 @@ static void call_arith (IMS event)
1058 if (ttype(im) == LUA_T_NIL) { 1016 if (ttype(im) == LUA_T_NIL) {
1059 im = luaI_getim(0, event); /* try a 'global' i.m. */ 1017 im = luaI_getim(0, event); /* try a 'global' i.m. */
1060 if (ttype(im) == LUA_T_NIL) 1018 if (ttype(im) == LUA_T_NIL)
1061 lua_error("unexpected type at conversion to number"); 1019 lua_error("unexpected type at arithmetic operation");
1062 } 1020 }
1063 } 1021 }
1064 lua_pushstring(luaI_eventname[event]); 1022 lua_pushstring(luaI_eventname[event]);
diff --git a/opcode.h b/opcode.h
index a1514a3a..d4516e86 100644
--- a/opcode.h
+++ b/opcode.h
@@ -1,6 +1,6 @@
1/* 1/*
2** TeCGraf - PUC-Rio 2** TeCGraf - PUC-Rio
3** $Id: opcode.h,v 3.32 1997/04/04 22:24:51 roberto Exp roberto $ 3** $Id: opcode.h,v 3.33 1997/04/11 21:34:53 roberto Exp roberto $
4*/ 4*/
5 5
6#ifndef opcode_h 6#ifndef opcode_h
@@ -166,5 +166,6 @@ TObject *luaI_Address (lua_Object o);
166void luaI_pushobject (TObject *o); 166void luaI_pushobject (TObject *o);
167void luaI_gcIM (TObject *o); 167void luaI_gcIM (TObject *o);
168int luaI_dorun (TFunc *tf); 168int luaI_dorun (TFunc *tf);
169int lua_domain (void);
169 170
170#endif 171#endif
diff --git a/undump.c b/undump.c
index 4f1570eb..6fb16117 100644
--- a/undump.c
+++ b/undump.c
@@ -3,7 +3,7 @@
3** load bytecodes from files 3** load bytecodes from files
4*/ 4*/
5 5
6char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $"; 6char* rcs_undump="$Id: undump.c,v 1.24 1997/06/13 11:08:47 lhf Exp $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -12,6 +12,7 @@ char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $";
12#include "luamem.h" 12#include "luamem.h"
13#include "table.h" 13#include "table.h"
14#include "undump.h" 14#include "undump.h"
15#include "zio.h"
15 16
16static int swapword=0; 17static int swapword=0;
17static int swapfloat=0; 18static int swapfloat=0;
@@ -147,10 +148,10 @@ static void Unthread(Byte* code, int i, int v)
147 } 148 }
148} 149}
149 150
150static int LoadWord(FILE* D) 151static int LoadWord(ZIO* Z)
151{ 152{
152 Word w; 153 Word w;
153 fread(&w,sizeof(w),1,D); 154 zread(Z,&w,sizeof(w));
154 if (swapword) 155 if (swapword)
155 { 156 {
156 Byte* p=(Byte*)&w; 157 Byte* p=(Byte*)&w;
@@ -160,101 +161,101 @@ static int LoadWord(FILE* D)
160 return w; 161 return w;
161} 162}
162 163
163static int LoadSize(FILE* D) 164static int LoadSize(ZIO* Z)
164{ 165{
165 Word hi=LoadWord(D); 166 Word hi=LoadWord(Z);
166 Word lo=LoadWord(D); 167 Word lo=LoadWord(Z);
167 int s=(hi<<16)|lo; 168 int s=(hi<<16)|lo;
168 if ((Word)s != s) lua_error("code too long"); 169 if ((Word)s != s) lua_error("code too long");
169 return s; 170 return s;
170} 171}
171 172
172static void* LoadBlock(int size, FILE* D) 173static void* LoadBlock(int size, ZIO* Z)
173{ 174{
174 void* b=luaI_malloc(size); 175 void* b=luaI_malloc(size);
175 fread(b,size,1,D); 176 zread(Z,b,size);
176 return b; 177 return b;
177} 178}
178 179
179static char* LoadString(FILE* D) 180static char* LoadString(ZIO* Z)
180{ 181{
181 int size=LoadWord(D); 182 int size=LoadWord(Z);
182 char *b=luaI_buffer(size); 183 char *b=luaI_buffer(size);
183 fread(b,size,1,D); 184 zread(Z,b,size);
184 return b; 185 return b;
185} 186}
186 187
187static char* LoadNewString(FILE* D) 188static char* LoadNewString(ZIO* Z)
188{ 189{
189 return LoadBlock(LoadWord(D),D); 190 return LoadBlock(LoadWord(Z),Z);
190} 191}
191 192
192static void LoadFunction(FILE* D) 193static void LoadFunction(ZIO* Z)
193{ 194{
194 TFunc* tf=new(TFunc); 195 TFunc* tf=new(TFunc);
195 tf->next=NULL; 196 tf->next=NULL;
196 tf->locvars=NULL; 197 tf->locvars=NULL;
197 tf->size=LoadSize(D); 198 tf->size=LoadSize(Z);
198 tf->lineDefined=LoadWord(D); 199 tf->lineDefined=LoadWord(Z);
199 if (IsMain(tf)) /* new main */ 200 if (IsMain(tf)) /* new main */
200 { 201 {
201 tf->fileName=LoadNewString(D); 202 tf->fileName=LoadNewString(Z);
202 Main=lastF=tf; 203 Main=lastF=tf;
203 } 204 }
204 else /* fix PUSHFUNCTION */ 205 else /* fix PUSHFUNCTION */
205 { 206 {
206 tf->marked=LoadWord(D); 207 tf->marked=LoadWord(Z);
207 tf->fileName=Main->fileName; 208 tf->fileName=Main->fileName;
208 memcpy(Main->code+tf->marked,&tf,sizeof(tf)); 209 memcpy(Main->code+tf->marked,&tf,sizeof(tf));
209 lastF=lastF->next=tf; 210 lastF=lastF->next=tf;
210 } 211 }
211 tf->code=LoadBlock(tf->size,D); 212 tf->code=LoadBlock(tf->size,Z);
212 if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size); 213 if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
213 while (1) /* unthread */ 214 while (1) /* unthread */
214 { 215 {
215 int c=getc(D); 216 int c=zgetc(Z);
216 if (c==ID_VAR) /* global var */ 217 if (c==ID_VAR) /* global var */
217 { 218 {
218 int i=LoadWord(D); 219 int i=LoadWord(Z);
219 char* s=LoadString(D); 220 char* s=LoadString(Z);
220 int v=luaI_findsymbolbyname(s); 221 int v=luaI_findsymbolbyname(s);
221 Unthread(tf->code,i,v); 222 Unthread(tf->code,i,v);
222 } 223 }
223 else if (c==ID_STR) /* constant string */ 224 else if (c==ID_STR) /* constant string */
224 { 225 {
225 int i=LoadWord(D); 226 int i=LoadWord(Z);
226 char* s=LoadString(D); 227 char* s=LoadString(Z);
227 int v=luaI_findconstantbyname(s); 228 int v=luaI_findconstantbyname(s);
228 Unthread(tf->code,i,v); 229 Unthread(tf->code,i,v);
229 } 230 }
230 else 231 else
231 { 232 {
232 ungetc(c,D); 233 zungetc(Z);
233 break; 234 break;
234 } 235 }
235 } 236 }
236} 237}
237 238
238static void LoadSignature(FILE* D) 239static void LoadSignature(ZIO* Z)
239{ 240{
240 char* s=SIGNATURE; 241 char* s=SIGNATURE;
241 while (*s!=0 && getc(D)==*s) 242 while (*s!=0 && zgetc(Z)==*s)
242 ++s; 243 ++s;
243 if (*s!=0) lua_error("cannot load binary file: bad signature"); 244 if (*s!=0) lua_error("cannot load binary file: bad signature");
244} 245}
245 246
246static void LoadHeader(FILE* D) 247static void LoadHeader(ZIO* Z)
247{ 248{
248 Word w,tw=TEST_WORD; 249 Word w,tw=TEST_WORD;
249 float f,tf=TEST_FLOAT; 250 float f,tf=TEST_FLOAT;
250 int version; 251 int version;
251 LoadSignature(D); 252 LoadSignature(Z);
252 version=getc(D); 253 version=zgetc(Z);
253 if (version>0x23) /* after 2.5 */ 254 if (version>0x23) /* after 2.5 */
254 { 255 {
255 int oldsizeofW=getc(D); 256 int oldsizeofW=zgetc(Z);
256 int oldsizeofF=getc(D); 257 int oldsizeofF=zgetc(Z);
257 int oldsizeofP=getc(D); 258 int oldsizeofP=zgetc(Z);
258 if (oldsizeofW!=2) 259 if (oldsizeofW!=2)
259 luaL_verror( 260 luaL_verror(
260 "cannot load binary file created on machine with sizeof(Word)=%d; " 261 "cannot load binary file created on machine with sizeof(Word)=%d; "
@@ -266,14 +267,14 @@ static void LoadHeader(FILE* D)
266 if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */ 267 if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
267 luaL_verror( 268 luaL_verror(
268 "cannot load binary file created on machine with sizeof(TFunc*)=%d; " 269 "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
269 "expected %d",oldsizeofP,sizeof(TFunc*)); 270 "expected %d",oldsizeofP,(int)sizeof(TFunc*));
270 } 271 }
271 fread(&w,sizeof(w),1,D); /* test word */ 272 zread(Z,&w,sizeof(w)); /* test word */
272 if (w!=tw) 273 if (w!=tw)
273 { 274 {
274 swapword=1; 275 swapword=1;
275 } 276 }
276 fread(&f,sizeof(f),1,D); /* test float */ 277 zread(Z,&f,sizeof(f)); /* test float */
277 if (f!=tf) 278 if (f!=tf)
278 { 279 {
279 Byte* p=(Byte*)&f; 280 Byte* p=(Byte*)&f;
@@ -286,13 +287,13 @@ static void LoadHeader(FILE* D)
286 } 287 }
287} 288}
288 289
289static void LoadChunk(FILE* D) 290static void LoadChunk(ZIO* Z)
290{ 291{
291 LoadHeader(D); 292 LoadHeader(Z);
292 while (1) 293 while (1)
293 { 294 {
294 int c=getc(D); 295 int c=zgetc(Z);
295 if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; } 296 if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
296 } 297 }
297} 298}
298 299
@@ -300,30 +301,26 @@ static void LoadChunk(FILE* D)
300** load one chunk from a file. 301** load one chunk from a file.
301** return list of functions found, headed by main, or NULL at EOF. 302** return list of functions found, headed by main, or NULL at EOF.
302*/ 303*/
303TFunc* luaI_undump1(FILE* D) 304static TFunc* luaI_undump1(ZIO* Z)
304{ 305{
305 while (1) 306 int c=zgetc(Z);
307 if (c==ID_CHUNK)
306 { 308 {
307 int c=getc(D); 309 LoadChunk(Z);
308 if (c==ID_CHUNK) 310 return Main;
309 {
310 LoadChunk(D);
311 return Main;
312 }
313 else if (c==EOF)
314 return NULL;
315 else
316 lua_error("not a lua binary file");
317 } 311 }
312 else if (c!=EOZ)
313 lua_error("not a lua binary file");
314 return NULL;
318} 315}
319 316
320/* 317/*
321** load and run all chunks in a file 318** load and run all chunks in a file
322*/ 319*/
323int luaI_undump(FILE* D) 320int luaI_undump(ZIO* Z)
324{ 321{
325 TFunc* m; 322 TFunc* m;
326 while ((m=luaI_undump1(D))) 323 while ((m=luaI_undump1(Z)))
327 { 324 {
328 int status=luaI_dorun(m); 325 int status=luaI_dorun(m);
329 luaI_freefunc(m); 326 luaI_freefunc(m);
diff --git a/undump.h b/undump.h
index fbe84f14..1bb6c300 100644
--- a/undump.h
+++ b/undump.h
@@ -1,10 +1,11 @@
1/* 1/*
2** undump.h 2** undump.h
3** definitions for lua decompiler 3** definitions for lua decompiler
4** $Id: undump.h,v 1.3 1996/11/14 11:44:34 lhf Exp lhf $ 4** $Id: undump.h,v 1.4 1997/04/14 12:12:40 lhf Exp roberto $
5*/ 5*/
6 6
7#include "func.h" 7#include "func.h"
8#include "zio.h"
8 9
9#define IsMain(f) (f->lineDefined==0) 10#define IsMain(f) (f->lineDefined==0)
10 11
@@ -19,5 +20,5 @@
19#define TEST_WORD 0x1234 /* a word for testing byte ordering */ 20#define TEST_WORD 0x1234 /* a word for testing byte ordering */
20#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */ 21#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
21 22
22TFunc* luaI_undump1(FILE* D); /* load one chunk */ 23
23int luaI_undump(FILE* D); /* load all chunks */ 24int luaI_undump(ZIO* Z); /* load all chunks */