Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
luac.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstring.h"
#include "lundump.h"

Go to the source code of this file.

Data Structures

struct  Smain
 

Macros

#define luac_c
 
#define LUA_CORE
 
#define PROGNAME   "luac" /* default program name */
 
#define OUTPUT   PROGNAME ".out" /* default output file */
 
#define IS(s)   (strcmp(argv[i],s)==0)
 
#define toproto(L, i)   (clvalue(L->top+(i))->l.p)
 

Functions

static void fatal (const char *message)
 
static void cannot (const char *what)
 
static void usage (const char *message)
 
static int doargs (int argc, char *argv[])
 
static const Protocombine (lua_State *L, int n)
 
static int writer (lua_State *L, const void *p, size_t size, void *u)
 
static int pmain (lua_State *L)
 
int main (int argc, char *argv[])
 

Variables

static int listing =0
 
static int dumping =1
 
static int stripping =0
 
static char Output [] ={ OUTPUT }
 
static const char * output =Output
 
static const char * progname =PROGNAME
 

Macro Definition Documentation

◆ IS

#define IS ( s)    (strcmp(argv[i],s)==0)

Definition at line 68 of file lua-5.1.5/src/luac.c.

Referenced by doargs(), and pmain().

◆ LUA_CORE

#define LUA_CORE

Definition at line 13 of file lua-5.1.5/src/luac.c.

◆ luac_c

#define luac_c

Definition at line 12 of file lua-5.1.5/src/luac.c.

◆ OUTPUT

#define OUTPUT   PROGNAME ".out" /* default output file */

Definition at line 27 of file lua-5.1.5/src/luac.c.

◆ PROGNAME

#define PROGNAME   "luac" /* default program name */

Definition at line 26 of file lua-5.1.5/src/luac.c.

Referenced by combine().

◆ toproto

#define toproto ( L,
i )   (clvalue(L->top+(i))->l.p)

Definition at line 117 of file lua-5.1.5/src/luac.c.

Referenced by combine().

Function Documentation

◆ cannot()

static void cannot ( const char * what)
static

Definition at line 42 of file lua-5.1.5/src/luac.c.

43{
44 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
45 exit(EXIT_FAILURE);
46}
static const char * output
static const char * progname

References output, and progname.

Referenced by pmain().

◆ combine()

static const Proto * combine ( lua_State * L,
int n )
static

Definition at line 119 of file lua-5.1.5/src/luac.c.

120{
121 if (n==1)
122 return toproto(L,-1);
123 else
124 {
125 int i,pc;
126 Proto* f=luaF_newproto(L);
127 setptvalue2s(L,L->top,f); incr_top(L);
128 f->source=luaS_newliteral(L,"=(" PROGNAME ")");
129 f->maxstacksize=1;
130 pc=2*n+1;
132 f->sizecode=pc;
133 f->p=luaM_newvector(L,n,Proto*);
134 f->sizep=n;
135 pc=0;
136 for (i=0; i<n; i++)
137 {
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);
141 }
142 f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
143 return f;
144 }
145}
#define incr_top(L)
Proto * luaF_newproto(lua_State *L)
lu_int32 Instruction
#define luaM_newvector(L, n, t)
#define setptvalue2s
#define CREATE_ABx(o, a, bc)
#define CREATE_ABC(o, a, b, c)
#define luaS_newliteral(L, s)
#define toproto(L, i)
#define PROGNAME
Instruction * code
struct Proto ** p
lu_byte maxstacksize
TString * source

References Proto::code, CREATE_ABC, CREATE_ABx, incr_top, luaF_newproto(), luaM_newvector, luaS_newliteral, Proto::maxstacksize, OP_CALL, OP_CLOSURE, OP_RETURN, Proto::p, PROGNAME, setptvalue2s, Proto::sizecode, Proto::sizep, Proto::source, lua_State::top, and toproto.

Referenced by pmain().

◆ doargs()

static int doargs ( int argc,
char * argv[] )
static

Definition at line 70 of file lua-5.1.5/src/luac.c.

71{
72 int i;
73 int version=0;
74 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
75 for (i=1; i<argc; i++)
76 {
77 if (*argv[i]!='-') /* end of options; keep it */
78 break;
79 else if (IS("--")) /* end of options; skip it */
80 {
81 ++i;
82 if (version) ++version;
83 break;
84 }
85 else if (IS("-")) /* end of options; use stdin */
86 break;
87 else if (IS("-l")) /* list */
88 ++listing;
89 else if (IS("-o")) /* output file */
90 {
91 output=argv[++i];
92 if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
93 if (IS("-")) output=NULL;
94 }
95 else if (IS("-p")) /* parse only */
96 dumping=0;
97 else if (IS("-s")) /* strip debug information */
98 stripping=1;
99 else if (IS("-v")) /* show version */
100 ++version;
101 else /* unknown option */
102 usage(argv[i]);
103 }
104 if (i==argc && (listing || !dumping))
105 {
106 dumping=0;
107 argv[--i]=Output;
108 }
109 if (version)
110 {
112 if (version==argc-1) exit(EXIT_SUCCESS);
113 }
114 return i;
115}
#define NULL
Definition gmacros.h:924
#define LUA_COPYRIGHT
#define LUA_RELEASE
static int stripping
static int dumping
static void usage(const char *message)
#define IS(s)
static char Output[]
static int listing
#define LUA_QL(x)
#define printf

References dumping, IS, listing, LUA_COPYRIGHT, LUA_QL, LUA_RELEASE, NULL, Output, output, printf, progname, stripping, and usage().

Referenced by main().

◆ fatal()

static void fatal ( const char * message)
static

Definition at line 36 of file lua-5.1.5/src/luac.c.

37{
38 fprintf(stderr,"%s: %s\n",progname,message);
39 exit(EXIT_FAILURE);
40}

References progname.

Referenced by duk_err_setup_heap_ljstate(), main(), and pmain().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 186 of file lua-5.1.5/src/luac.c.

187{
188 lua_State* L;
189 struct Smain s;
190 int i=doargs(argc,argv);
191 argc-=i; argv+=i;
192 if (argc<=0) usage("no input files given");
193 L=lua_open();
194 if (L==NULL) fatal("not enough memory for state");
195 s.argc=argc;
196 s.argv=argv;
197 if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
198 lua_close(L);
199 return EXIT_SUCCESS;
200}
LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
LUA_API void lua_close(lua_State *L)
#define lua_open()
#define lua_tostring(L, i)
static int doargs(int argc, char *argv[])
static void fatal(const char *message)
static int pmain(lua_State *L)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318
char ** argv

References Smain::argc, Smain::argv, doargs(), fatal(), lua_close(), lua_cpcall(), lua_open, lua_tostring, NULL, pmain(), s, and usage().

◆ pmain()

static int pmain ( lua_State * L)
static

Definition at line 158 of file lua-5.1.5/src/luac.c.

159{
160 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
161 int argc=s->argc;
162 char** argv=s->argv;
163 const Proto* f;
164 int i;
165 if (!lua_checkstack(L,argc)) fatal("too many input files");
166 for (i=0; i<argc; i++)
167 {
168 const char* filename=IS("-") ? NULL : argv[i];
169 if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
170 }
171 f=combine(L,argc);
172 if (listing) luaU_print(f,listing>1);
173 if (dumping)
174 {
175 FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
176 if (D==NULL) cannot("open");
177 lua_lock(L);
179 lua_unlock(L);
180 if (ferror(D)) cannot("write");
181 if (fclose(D)) cannot("close");
182 }
183 return 0;
184}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUA_API int lua_checkstack(lua_State *L, int size)
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip)
#define lua_unlock(L)
#define lua_lock(L)
static const Proto * combine(lua_State *L, int n)
static int writer(lua_State *L, const void *p, size_t size, void *u)
static void cannot(const char *what)
#define luaL_loadfile(L, f)
#define luaU_print

References Smain::argc, Smain::argv, cannot(), combine(), dumping, fatal(), IS, listing, lua_checkstack(), lua_lock, lua_tostring, lua_touserdata(), lua_unlock, luaL_loadfile, luaU_dump(), luaU_print, NULL, output, s, stripping, and writer().

Referenced by main().

◆ usage()

static void usage ( const char * message)
static

Definition at line 48 of file lua-5.1.5/src/luac.c.

49{
50 if (*message=='-')
51 fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
52 else
53 fprintf(stderr,"%s: %s\n",progname,message);
54 fprintf(stderr,
55 "usage: %s [options] [filenames].\n"
56 "Available options are:\n"
57 " - process stdin\n"
58 " -l list\n"
59 " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
60 " -p parse only\n"
61 " -s strip debug information\n"
62 " -v show version information\n"
63 " -- stop handling options\n",
65 exit(EXIT_FAILURE);
66}
#define LUA_QS

References LUA_QL, LUA_QS, Output, and progname.

Referenced by doargs(), and main().

◆ writer()

static int writer ( lua_State * L,
const void * p,
size_t size,
void * u )
static

Definition at line 147 of file lua-5.1.5/src/luac.c.

148{
149 UNUSED(L);
150 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
151}
#define UNUSED(x)
size_t fwrite(const void *, size_t, size_t, FILE *)

References fwrite(), and UNUSED.

Referenced by pmain().

Variable Documentation

◆ dumping

int dumping =1
static

Definition at line 30 of file lua-5.1.5/src/luac.c.

Referenced by doargs(), and pmain().

◆ listing

int listing =0
static

Definition at line 29 of file lua-5.1.5/src/luac.c.

Referenced by doargs(), and pmain().

◆ Output

char Output[] ={ OUTPUT }
static

Definition at line 32 of file lua-5.1.5/src/luac.c.

32{ OUTPUT }; /* default output file name */
#define OUTPUT

Referenced by doargs(), and usage().

◆ output

const char* output =Output
static

◆ progname

const char* progname =PROGNAME
static

Definition at line 34 of file lua-5.1.5/src/luac.c.

Referenced by cannot(), doargs(), fatal(), and usage().

◆ stripping

int stripping =0
static

Definition at line 31 of file lua-5.1.5/src/luac.c.

Referenced by doargs(), and pmain().