23
45
#include <../lua51/lstring.h>
24
46
#include <../lua51/lundump.h>
26
#define PROGNAME "texluac" /* default program name */
27
#define OUTPUT PROGNAME ".out" /* default output file */
29
static int dumping=1; /* dump bytecodes? */
30
static int stripping=0; /* strip debug information? */
31
static char Output[]={ OUTPUT }; /* default output file name */
32
static const char* output=Output; /* actual output file name */
33
static const char* progname=PROGNAME; /* actual program name */
35
static void fatal(const char* message)
37
fprintf(stderr,"%s: %s\n",progname,message);
41
static void cannot(const char* what)
43
fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
47
static void usage(const char* message)
50
fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
52
fprintf(stderr,"%s: %s\n",progname,message);
54
"usage: %s [options] [filenames].\n"
55
"Available options are:\n"
57
" -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
59
" -s strip debug information\n"
60
" -v show version information\n"
61
" -- stop handling options\n",
66
#define IS(s) (strcmp(argv[i],s)==0)
68
static int doargs(int argc, char* argv[])
72
if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
73
for (i=1; i<argc; i++)
75
if (*argv[i]!='-') /* end of options; keep it */
77
else if (IS("--")) /* end of options; skip it */
80
if (version) ++version;
83
else if (IS("-")) /* end of options; use stdin */
85
else if (IS("-o")) /* output file */
88
if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
89
if (IS("-")) output=NULL;
91
else if (IS("-p")) /* parse only */
93
else if (IS("-s")) /* strip debug information */
95
else if (IS("-v")) /* show version */
97
else if (IS("--luaconly")) /* ignore */
99
else if (IS("--luac")) /* ignore */
101
else /* unknown option */
104
if (i==argc && (!dumping))
111
printf("%s %s\n",LUA_RELEASE,LUA_COPYRIGHT);
112
if (version==argc-1) exit(EXIT_SUCCESS);
48
static const char _svn_version[] =
49
"$Id: texluac.c 1226 2008-05-02 16:11:02Z oneiros $ $URL: http://scm.foundry.supelec.fr/svn/luatex/tags/beta-0.28.0/src/texk/web2c/luatexdir/lua/texluac.c $";
51
#define PROGNAME "texluac" /* default program name */
52
#define OUTPUT PROGNAME ".out" /* default output file */
54
static int dumping = 1; /* dump bytecodes? */
55
static int stripping = 0; /* strip debug information? */
56
static char Output[] = { OUTPUT }; /* default output file name */
57
static const char *output = Output; /* actual output file name */
58
static const char *progname = PROGNAME; /* actual program name */
60
static void fatal(const char *message)
62
fprintf(stderr, "%s: %s\n", progname, message);
66
static void cannot(const char *what)
68
fprintf(stderr, "%s: cannot %s %s: %s\n", progname, what, output,
73
static void usage(const char *message)
76
fprintf(stderr, "%s: unrecognized option " LUA_QS "\n", progname,
79
fprintf(stderr, "%s: %s\n", progname, message);
81
"usage: %s [options] [filenames].\n"
82
"Available options are:\n"
84
" -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
86
" -s strip debug information\n"
87
" -v show version information\n"
88
" -- stop handling options\n", progname, Output);
92
#define IS(s) (strcmp(argv[i],s)==0)
94
static int doargs(int argc, char *argv[])
98
if (argv[0] != NULL && *argv[0] != 0)
100
for (i = 1; i < argc; i++) {
101
if (*argv[i] != '-') /* end of options; keep it */
103
else if (IS("--")) { /* end of options; skip it */
108
} else if (IS("-")) /* end of options; use stdin */
110
else if (IS("-o")) { /* output file */
112
if (output == NULL || *output == 0)
113
usage(LUA_QL("-o") " needs argument");
116
} else if (IS("-p")) /* parse only */
118
else if (IS("-s")) /* strip debug information */
120
else if (IS("-v")) /* show version */
122
else if (IS("--luaconly")) /* ignore */
124
else if (IS("--luac")) /* ignore */
126
else /* unknown option */
129
if (i == argc && (!dumping)) {
134
printf("%s %s\n", LUA_RELEASE, LUA_COPYRIGHT);
135
if (version == argc - 1)
117
141
#define toproto(L,i) (clvalue(L->top+(i))->l.p)
119
static const Proto* combine(lua_State* L, int n)
143
static const Proto *combine(lua_State * L, int n)
122
return toproto(L,-1);
126
Proto* f=luaF_newproto(L);
127
setptvalue2s(L,L->top,f); incr_top(L);
128
f->source=luaS_newliteral(L,"=(" PROGNAME ")");
131
f->code=luaM_newvector(L,pc,Instruction);
133
f->p=luaM_newvector(L,n,Proto*);
138
f->p[i]=toproto(L,i-n-1);
139
f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
140
f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
142
f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
146
return toproto(L, -1);
149
Proto *f = luaF_newproto(L);
150
setptvalue2s(L, L->top, f);
152
f->source = luaS_newliteral(L, "=(" PROGNAME ")");
155
f->code = luaM_newvector(L, pc, Instruction);
157
f->p = luaM_newvector(L, n, Proto *);
160
for (i = 0; i < n; i++) {
161
f->p[i] = toproto(L, i - n - 1);
162
f->code[pc++] = CREATE_ABx(OP_CLOSURE, 0, i);
163
f->code[pc++] = CREATE_ABC(OP_CALL, 0, 1, 1);
165
f->code[pc++] = CREATE_ABC(OP_RETURN, 0, 1, 0);
147
static int writer(lua_State* L, const void* p, size_t size, void* u)
170
static int writer(lua_State * L, const void *p, size_t size, void *u)
150
return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
173
return (fwrite(p, size, 1, (FILE *) u) != 1) && (size != 0);
158
static int pmain(lua_State* L)
181
static int pmain(lua_State * L)
160
struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
165
if (!lua_checkstack(L,argc)) fatal("too many input files");
166
for (i=0; i<argc; i++)
168
const char* filename=IS("-") ? NULL : argv[i];
169
if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
174
FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
175
if (D==NULL) cannot("open");
177
luaU_dump(L,f,writer,D,stripping);
179
if (ferror(D)) cannot("write");
180
if (fclose(D)) cannot("close");
183
struct Smain *s = (struct Smain *) lua_touserdata(L, 1);
185
char **argv = s->argv;
188
if (!lua_checkstack(L, argc))
189
fatal("too many input files");
190
for (i = 0; i < argc; i++) {
191
const char *filename = IS("-") ? NULL : argv[i];
192
if (luaL_loadfile(L, filename) != 0)
193
fatal(lua_tostring(L, -1));
195
f = combine(L, argc);
197
FILE *D = (output == NULL) ? stdout : fopen(output, "wb");
201
luaU_dump(L, f, writer, D, stripping);
185
int luac_main(int argc, char* argv[])
211
int luac_main(int argc, char *argv[])
189
int i=doargs(argc,argv);
191
if (argc<=0) usage("no input files given");
193
if (L==NULL) fatal("not enough memory for state");
196
if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
215
int i = doargs(argc, argv);
219
usage("no input files given");
222
fatal("not enough memory for state");
225
if (lua_cpcall(L, pmain, &s) != 0)
226
fatal(lua_tostring(L, -1));