xref: /netbsd/external/mit/lua/dist/src/luac.c (revision 6550d01e)
1 /*	$NetBSD: luac.c,v 1.1.1.1 2010/10/31 11:17:00 mbalmer Exp $	*/
2 
3 /*
4 ** Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp
5 ** Lua compiler (saves bytecodes to files; also list bytecodes)
6 ** See Copyright Notice in lua.h
7 */
8 
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #define luac_c
15 #define LUA_CORE
16 
17 #include "lua.h"
18 #include "lauxlib.h"
19 
20 #include "ldo.h"
21 #include "lfunc.h"
22 #include "lmem.h"
23 #include "lobject.h"
24 #include "lopcodes.h"
25 #include "lstring.h"
26 #include "lundump.h"
27 
28 #define PROGNAME	"luac"		/* default program name */
29 #define	OUTPUT		PROGNAME ".out"	/* default output file */
30 
31 static int listing=0;			/* list bytecodes? */
32 static int dumping=1;			/* dump bytecodes? */
33 static int stripping=0;			/* strip debug information? */
34 static char Output[]={ OUTPUT };	/* default output file name */
35 static const char* output=Output;	/* actual output file name */
36 static const char* progname=PROGNAME;	/* actual program name */
37 
38 static void fatal(const char* message)
39 {
40  fprintf(stderr,"%s: %s\n",progname,message);
41  exit(EXIT_FAILURE);
42 }
43 
44 static void cannot(const char* what)
45 {
46  fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
47  exit(EXIT_FAILURE);
48 }
49 
50 static void usage(const char* message)
51 {
52  if (*message=='-')
53   fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
54  else
55   fprintf(stderr,"%s: %s\n",progname,message);
56  fprintf(stderr,
57  "usage: %s [options] [filenames].\n"
58  "Available options are:\n"
59  "  -        process stdin\n"
60  "  -l       list\n"
61  "  -o name  output to file " LUA_QL("name") " (default is \"%s\")\n"
62  "  -p       parse only\n"
63  "  -s       strip debug information\n"
64  "  -v       show version information\n"
65  "  --       stop handling options\n",
66  progname,Output);
67  exit(EXIT_FAILURE);
68 }
69 
70 #define	IS(s)	(strcmp(argv[i],s)==0)
71 
72 static int doargs(int argc, char* argv[])
73 {
74  int i;
75  int version=0;
76  if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
77  for (i=1; i<argc; i++)
78  {
79   if (*argv[i]!='-')			/* end of options; keep it */
80    break;
81   else if (IS("--"))			/* end of options; skip it */
82   {
83    ++i;
84    if (version) ++version;
85    break;
86   }
87   else if (IS("-"))			/* end of options; use stdin */
88    break;
89   else if (IS("-l"))			/* list */
90    ++listing;
91   else if (IS("-o"))			/* output file */
92   {
93    output=argv[++i];
94    if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
95    if (IS("-")) output=NULL;
96   }
97   else if (IS("-p"))			/* parse only */
98    dumping=0;
99   else if (IS("-s"))			/* strip debug information */
100    stripping=1;
101   else if (IS("-v"))			/* show version */
102    ++version;
103   else					/* unknown option */
104    usage(argv[i]);
105  }
106  if (i==argc && (listing || !dumping))
107  {
108   dumping=0;
109   argv[--i]=Output;
110  }
111  if (version)
112  {
113   printf("%s  %s\n",LUA_RELEASE,LUA_COPYRIGHT);
114   if (version==argc-1) exit(EXIT_SUCCESS);
115  }
116  return i;
117 }
118 
119 #define toproto(L,i) (clvalue(L->top+(i))->l.p)
120 
121 static const Proto* combine(lua_State* L, int n)
122 {
123  if (n==1)
124   return toproto(L,-1);
125  else
126  {
127   int i,pc;
128   Proto* f=luaF_newproto(L);
129   setptvalue2s(L,L->top,f); incr_top(L);
130   f->source=luaS_newliteral(L,"=(" PROGNAME ")");
131   f->maxstacksize=1;
132   pc=2*n+1;
133   f->code=luaM_newvector(L,pc,Instruction);
134   f->sizecode=pc;
135   f->p=luaM_newvector(L,n,Proto*);
136   f->sizep=n;
137   pc=0;
138   for (i=0; i<n; i++)
139   {
140    f->p[i]=toproto(L,i-n-1);
141    f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
142    f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
143   }
144   f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
145   return f;
146  }
147 }
148 
149 static int writer(lua_State* L, const void* p, size_t size, void* u)
150 {
151  UNUSED(L);
152  return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
153 }
154 
155 struct Smain {
156  int argc;
157  char** argv;
158 };
159 
160 static int pmain(lua_State* L)
161 {
162  struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
163  int argc=s->argc;
164  char** argv=s->argv;
165  const Proto* f;
166  int i;
167  if (!lua_checkstack(L,argc)) fatal("too many input files");
168  for (i=0; i<argc; i++)
169  {
170   const char* filename=IS("-") ? NULL : argv[i];
171   if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
172  }
173  f=combine(L,argc);
174  if (listing) luaU_print(f,listing>1);
175  if (dumping)
176  {
177   FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
178   if (D==NULL) cannot("open");
179   lua_lock(L);
180   luaU_dump(L,f,writer,D,stripping);
181   lua_unlock(L);
182   if (ferror(D)) cannot("write");
183   if (fclose(D)) cannot("close");
184  }
185  return 0;
186 }
187 
188 int main(int argc, char* argv[])
189 {
190  lua_State* L;
191  struct Smain s;
192  int i=doargs(argc,argv);
193  argc-=i; argv+=i;
194  if (argc<=0) usage("no input files given");
195  L=lua_open();
196  if (L==NULL) fatal("not enough memory for state");
197  s.argc=argc;
198  s.argv=argv;
199  if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
200  lua_close(L);
201  return EXIT_SUCCESS;
202 }
203