Imported from ../lua-3.0.tar.gz.
[lua.git] / src / luac / luac.c
blob713da1fb0f8bf968d4e61b179da2bc1be1fa4999
1 /*
2 ** luac.c
3 ** lua compiler (saves bytecodes to files; also list binary files)
4 */
6 char* rcs_luac="$Id: luac.c,v 1.23 1997/06/20 20:34:04 lhf Exp $";
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "luac.h"
12 #include "lex.h"
13 #include "zio.h"
15 static void compile(char* filename);
16 static void undump(char* filename);
18 static int listing=0; /* list bytecodes? */
19 static int dumping=1; /* dump bytecodes? */
20 static int undumping=0; /* undump bytecodes? */
21 static FILE* D; /* output file */
23 static void usage(void)
25 fprintf(stderr,
26 "usage: luac [-c | -u] [-d] [-l] [-p] [-q] [-v] [-o output] file ...\n"
27 " -c\tcompile (default)\n"
28 " -u\tundump\n"
29 " -d\tgenerate debugging information\n"
30 " -l\tlist (default for -u)\n"
31 " -o\toutput file for -c (default \"luac.out\")\n"
32 " -p\tparse only\n"
33 " -q\tquiet (default for -c)\n"
34 " -v\tshow version information\n"
36 exit(1);
39 #define IS(s) (strcmp(argv[i],s)==0)
41 int main(int argc, char* argv[])
43 char* d="luac.out"; /* default output file */
44 int i;
45 for (i=1; i<argc; i++)
47 if (argv[i][0]!='-') /* end of options */
48 break;
49 else if (IS("-")) /* use stdin */
50 break;
51 else if (IS("-c")) /* compile (and dump) */
53 dumping=1;
54 undumping=0;
56 else if (IS("-d")) /* debug */
57 lua_debug=1;
58 else if (IS("-l")) /* list */
59 listing=1;
60 else if (IS("-o")) /* output file */
61 d=argv[++i];
62 else if (IS("-p")) /* parse only (for timing purposes) */
63 dumping=0;
64 else if (IS("-q")) /* quiet */
65 listing=0;
66 else if (IS("-u")) /* undump */
68 dumping=0;
69 undumping=1;
70 listing=1;
72 else if (IS("-v")) /* show version */
73 printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);
74 else /* unknown option */
75 usage();
77 --i; /* fake new argv[0] */
78 argc-=i;
79 argv+=i;
80 if (dumping)
82 if (argc<2) usage();
83 for (i=1; i<argc; i++) /* play safe with output file */
84 if (IS(d))
86 fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);
87 exit(1);
89 D=fopen(d,"wb"); /* must open in binary mode */
90 if (D==NULL)
92 fprintf(stderr,"luac: cannot open ");
93 perror(d);
94 exit(1);
96 for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
97 fclose(D);
99 if (undumping)
101 if (argc<2)
102 undump("luac.out");
103 else
104 for (i=1; i<argc; i++) undump(IS("-")? NULL : argv[i]);
106 return 0;
109 static void do_dump(TFunc* Main)
111 TFunc* tf;
112 LinkFunctions(Main);
113 if (listing)
115 for (tf=Main; tf!=NULL; tf=tf->next) PrintFunction(tf,Main);
117 if (dumping)
119 DumpHeader(D);
120 for (tf=Main; tf!=NULL; tf=tf->next) DumpFunction(tf,D);
122 for (tf=Main; tf!=NULL; )
124 TFunc* nf=tf->next;
125 luaI_freefunc(tf);
126 tf=nf;
130 static void do_compile(ZIO* z)
132 TFunc* tf=new(TFunc);
133 lua_setinput(z);
134 luaI_initTFunc(tf);
135 tf->fileName=lua_parsedfile;
136 lua_parse(tf);
137 do_dump(tf);
140 static void compile(char* filename)
142 FILE* f= (filename==NULL) ? stdin : fopen(filename, "r");
143 if (f==NULL)
145 fprintf(stderr,"luac: cannot open ");
146 perror(filename);
147 exit(1);
149 else
151 ZIO z;
152 zFopen(&z,f);
153 luaI_setparsedfile(filename?filename:"(stdin)");
154 do_compile(&z);
155 fclose(f);
159 static void do_undump(ZIO* z)
161 TFunc* Main;
162 while ((Main=luaI_undump1(z)))
164 if (listing)
166 TFunc* tf;
167 for (tf=Main; tf!=NULL; tf=tf->next)
168 PrintFunction(tf,Main);
170 luaI_freefunc(Main); /* TODO: free others */
174 static void undump(char* filename)
176 FILE* f= (filename==NULL) ? stdin : fopen(filename, "rb");
177 if (f==NULL)
179 fprintf(stderr,"luac: cannot open ");
180 perror(filename);
181 exit(1);
183 else
185 ZIO z;
186 zFopen(&z,f);
187 do_undump(&z);
188 fclose(f);