forked from enlightenment/efl
elua: reformat C source to EFL style
This commit is contained in:
parent
5f1e08bddf
commit
6558d1ccc9
|
@ -10,136 +10,172 @@
|
|||
|
||||
/* bytecode caching */
|
||||
|
||||
static Eina_File *check_bc(Eina_File *of, const char *mode, Eina_Bool *bc) {
|
||||
const char *fname = eina_file_filename_get(of);
|
||||
const char *ext = strstr(fname, ".lua");
|
||||
if (ext && !ext[4] && (!mode || strchr(mode, 't'))) {
|
||||
static Eina_File *
|
||||
check_bc(Eina_File *of, const char *mode, Eina_Bool *bc)
|
||||
{
|
||||
const char *fname = eina_file_filename_get(of);
|
||||
const char *ext = strstr(fname, ".lua");
|
||||
if (ext && !ext[4] && (!mode || strchr(mode, 't')))
|
||||
{
|
||||
/* loading lua source file, try cached */
|
||||
char buf[PATH_MAX];
|
||||
snprintf(buf, sizeof(buf), "%sc", fname);
|
||||
Eina_File *f = eina_file_open(buf, EINA_FALSE);
|
||||
if (!f) {
|
||||
/* no cached bytecode */
|
||||
*bc = EINA_TRUE;
|
||||
} else {
|
||||
/* cached bytecode, check timestamps */
|
||||
if (eina_file_mtime_get(f) > eina_file_mtime_get(of)) {
|
||||
/* bytecode new enough, chunkname stays the same */
|
||||
eina_file_close(of);
|
||||
return f;
|
||||
} else {
|
||||
/* bytecode too old, remove old file */
|
||||
eina_file_close(f);
|
||||
*bc = EINA_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return of;
|
||||
if (!f)
|
||||
{
|
||||
/* no cached bytecode */
|
||||
*bc = EINA_TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* cached bytecode, check timestamps */
|
||||
if (eina_file_mtime_get(f) > eina_file_mtime_get(of))
|
||||
{
|
||||
/* bytecode new enough, chunkname stays the same */
|
||||
eina_file_close(of);
|
||||
return f;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* bytecode too old, remove old file */
|
||||
eina_file_close(f);
|
||||
*bc = EINA_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return of;
|
||||
}
|
||||
|
||||
static int writef(lua_State *L EINA_UNUSED, const void *p, size_t size,
|
||||
void *ud) {
|
||||
FILE *f = ud;
|
||||
return ferror(f) || (fwrite(p, 1, size, f) != size);
|
||||
static int
|
||||
writef(lua_State *L EINA_UNUSED, const void *p, size_t size, void *ud)
|
||||
{
|
||||
FILE *f = ud;
|
||||
return ferror(f) || (fwrite(p, 1, size, f) != size);
|
||||
}
|
||||
|
||||
static void write_bc(lua_State *L, const char *fname) {
|
||||
char buf[PATH_MAX];
|
||||
snprintf(buf, sizeof(buf), "%sc", fname);
|
||||
FILE *f = fopen(buf, "wb");
|
||||
if (f) {
|
||||
if (lua_dump(L, writef, f)) {
|
||||
fclose(f);
|
||||
remove(buf);
|
||||
} else fclose(f);
|
||||
}
|
||||
static void
|
||||
write_bc(lua_State *L, const char *fname)
|
||||
{
|
||||
FILE *f;
|
||||
char buf[PATH_MAX];
|
||||
snprintf(buf, sizeof(buf), "%sc", fname);
|
||||
if ((f = fopen(buf, "wb")))
|
||||
{
|
||||
if (lua_dump(L, writef, f))
|
||||
{
|
||||
fclose(f);
|
||||
remove(buf);
|
||||
}
|
||||
else fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *getf(lua_State *L EINA_UNUSED, void *ud, size_t *size) {
|
||||
char *buff = *((char**)ud);
|
||||
if (feof(stdin)) return NULL;
|
||||
*size = fread(buff, 1, LUAL_BUFFERSIZE, stdin);
|
||||
return (*size > 0) ? buff : NULL;
|
||||
static const char *
|
||||
getf(lua_State *L EINA_UNUSED, void *ud, size_t *size)
|
||||
{
|
||||
char *buff = *((char**)ud);
|
||||
if (feof(stdin)) return NULL;
|
||||
*size = fread(buff, 1, LUAL_BUFFERSIZE, stdin);
|
||||
return (*size > 0) ? buff : NULL;
|
||||
}
|
||||
|
||||
static int elua_loadstdin(lua_State *L, const char *mode) {
|
||||
char buff[LUAL_BUFFERSIZE];
|
||||
int status = lua_loadx(L, getf, &buff, "=stdin", mode);
|
||||
if (ferror(stdin)) {
|
||||
static int
|
||||
elua_loadstdin(lua_State *L, const char *mode)
|
||||
{
|
||||
char buff[LUAL_BUFFERSIZE];
|
||||
int status = lua_loadx(L, getf, &buff, "=stdin", mode);
|
||||
if (ferror(stdin))
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
lua_pushfstring(L, "cannot read stdin: %s", strerror(errno));
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
typedef struct Map_Stream {
|
||||
char *fmap;
|
||||
size_t flen;
|
||||
typedef struct Map_Stream
|
||||
{
|
||||
char *fmap;
|
||||
size_t flen;
|
||||
} Map_Stream;
|
||||
|
||||
static const char *getf_map(lua_State *L EINA_UNUSED, void *ud, size_t *size) {
|
||||
Map_Stream *s = ud;
|
||||
const char *fmap = s->fmap;
|
||||
*size = s->flen;
|
||||
/* gotta null it - tell luajit to terminate reading */
|
||||
s->fmap = NULL;
|
||||
return fmap;
|
||||
static const char *
|
||||
getf_map(lua_State *L EINA_UNUSED, void *ud, size_t *size)
|
||||
{
|
||||
Map_Stream *s = ud;
|
||||
const char *fmap = s->fmap;
|
||||
*size = s->flen;
|
||||
/* gotta null it - tell luajit to terminate reading */
|
||||
s->fmap = NULL;
|
||||
return fmap;
|
||||
}
|
||||
|
||||
int elua_loadfilex(lua_State *L, const char *fname, const char *mode) {
|
||||
Map_Stream s;
|
||||
int status;
|
||||
Eina_File *f;
|
||||
const char *chname;
|
||||
Eina_Bool bcache = EINA_FALSE;
|
||||
if (!fname) {
|
||||
int
|
||||
elua_loadfilex(lua_State *L, const char *fname, const char *mode)
|
||||
{
|
||||
Map_Stream s;
|
||||
int status;
|
||||
Eina_File *f;
|
||||
const char *chname;
|
||||
Eina_Bool bcache = EINA_FALSE;
|
||||
if (!fname)
|
||||
{
|
||||
return elua_loadstdin(L, mode);
|
||||
}
|
||||
if (!(f = eina_file_open(fname, EINA_FALSE))) {
|
||||
}
|
||||
if (!(f = eina_file_open(fname, EINA_FALSE)))
|
||||
{
|
||||
lua_pushfstring(L, "cannot open %s: %s", fname, strerror(errno));
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
chname = lua_pushfstring(L, "@%s", fname);
|
||||
f = check_bc(f, mode, &bcache);
|
||||
s.flen = eina_file_size_get(f);
|
||||
if (!(s.fmap = eina_file_map_all(f, EINA_FILE_RANDOM))) {
|
||||
}
|
||||
chname = lua_pushfstring(L, "@%s", fname);
|
||||
f = check_bc(f, mode, &bcache);
|
||||
s.flen = eina_file_size_get(f);
|
||||
if (!(s.fmap = eina_file_map_all(f, EINA_FILE_RANDOM)))
|
||||
{
|
||||
lua_pushfstring(L, "cannot read %s: %s", chname + 1, strerror(errno));
|
||||
lua_remove(L, -2);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
status = lua_loadx(L, getf_map, &s, chname, mode);
|
||||
eina_file_map_free(f, s.fmap);
|
||||
eina_file_close(f);
|
||||
if (!status && bcache) write_bc(L, fname);
|
||||
lua_remove(L, -2);
|
||||
return status;
|
||||
}
|
||||
status = lua_loadx(L, getf_map, &s, chname, mode);
|
||||
eina_file_map_free(f, s.fmap);
|
||||
eina_file_close(f);
|
||||
if (!status && bcache) write_bc(L, fname);
|
||||
lua_remove(L, -2);
|
||||
return status;
|
||||
}
|
||||
|
||||
int elua_loadfile(lua_State *L, const char *fname) {
|
||||
return elua_loadfilex(L, fname, NULL);
|
||||
int
|
||||
elua_loadfile(lua_State *L, const char *fname)
|
||||
{
|
||||
return elua_loadfilex(L, fname, NULL);
|
||||
}
|
||||
|
||||
/* lua function */
|
||||
|
||||
static int loadfile(lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
const char *mode = luaL_optstring(L, 2, NULL);
|
||||
int status = elua_loadfilex(L, fname, mode),
|
||||
hasenv = (lua_gettop(L) >= 3);
|
||||
if (!status) {
|
||||
if (hasenv) {
|
||||
lua_pushvalue(L, 3);
|
||||
lua_setfenv(L, -2);
|
||||
}
|
||||
static int
|
||||
loadfile(lua_State *L)
|
||||
{
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
const char *mode = luaL_optstring(L, 2, NULL);
|
||||
int status = elua_loadfilex(L, fname, mode),
|
||||
hasenv = (lua_gettop(L) >= 3);
|
||||
if (!status)
|
||||
{
|
||||
if (hasenv)
|
||||
{
|
||||
lua_pushvalue(L, 3);
|
||||
lua_setfenv(L, -2);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2);
|
||||
return 2;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
lua_insert(L, -2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
void elua_register_cache(lua_State *L) {
|
||||
lua_pushcfunction(L, loadfile);
|
||||
lua_setglobal(L, "loadfile");
|
||||
void
|
||||
elua_register_cache(lua_State *L)
|
||||
{
|
||||
lua_pushcfunction(L, loadfile);
|
||||
lua_setglobal(L, "loadfile");
|
||||
}
|
|
@ -17,265 +17,326 @@
|
|||
|
||||
#include "main.h"
|
||||
|
||||
static FILE *elua_popen_c(const char *path, const char *argv[], const char *mode) {
|
||||
int read = (!mode || mode[0] == 'r');
|
||||
int binary = mode && mode[0] && mode[1] == 'b';
|
||||
pid_t pid;
|
||||
static FILE *
|
||||
elua_popen_c(const char *path, const char *argv[], const char *mode)
|
||||
{
|
||||
int read = (!mode || mode[0] == 'r');
|
||||
int binary = mode && mode[0] && mode[1] == 'b';
|
||||
pid_t pid;
|
||||
|
||||
int des[2];
|
||||
if (pipe(des, mode)) return NULL;
|
||||
int des[2];
|
||||
if (pipe(des, mode)) return NULL;
|
||||
|
||||
pid = fork();
|
||||
if (!pid) {
|
||||
pid = fork();
|
||||
if (!pid)
|
||||
{
|
||||
/* if read, stdout (1) is still open here
|
||||
* (parent can read, child can write) */
|
||||
close(des[!read]);
|
||||
dup2(des[read], read ? STDOUT_FILENO : STDIN_FILENO);
|
||||
execv(path, (char * const *)argv);
|
||||
return NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* if read, stdin (0) is still open here
|
||||
* (child can read, parent can write) */
|
||||
close(des[read]);
|
||||
return fdopen(des[!read], read ? (binary ? "rb" : "r")
|
||||
: (binary ? "wb" : "w"));
|
||||
}
|
||||
: (binary ? "wb" : "w"));
|
||||
}
|
||||
}
|
||||
|
||||
static int push_ret(lua_State *L, int i, const char *fname) {
|
||||
int en = errno;
|
||||
if (i) {
|
||||
static int
|
||||
push_ret(lua_State *L, int i, const char *fname)
|
||||
{
|
||||
int en = errno;
|
||||
if (i)
|
||||
{
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushnil(L);
|
||||
if (fname) {
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
} else {
|
||||
lua_pushfstring(L, "%s", strerror(en));
|
||||
}
|
||||
if (fname)
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
else
|
||||
lua_pushfstring(L, "%s", strerror(en));
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static FILE *tofile(lua_State *L) {
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
if (!*f) {
|
||||
static FILE *
|
||||
tofile(lua_State *L)
|
||||
{
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
if (!*f)
|
||||
{
|
||||
luaL_error(L, "attempt to use a closed file");
|
||||
}
|
||||
return *f;
|
||||
}
|
||||
return *f;
|
||||
}
|
||||
|
||||
static int elua_close(lua_State *L) {
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
int ok = (fclose(*f) == 0);
|
||||
if (ok) *f = NULL;
|
||||
return push_ret(L, ok, NULL);
|
||||
static int
|
||||
elua_close(lua_State *L)
|
||||
{
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
int ok = (fclose(*f) == 0);
|
||||
if (ok) *f = NULL;
|
||||
return push_ret(L, ok, NULL);
|
||||
}
|
||||
|
||||
static int elua_flush(lua_State *L) {
|
||||
return push_ret(L, fflush(tofile(L)) == 0, NULL);
|
||||
static int
|
||||
elua_flush(lua_State *L)
|
||||
{
|
||||
return push_ret(L, fflush(tofile(L)) == 0, NULL);
|
||||
}
|
||||
|
||||
static int elua_readline(lua_State *L);
|
||||
|
||||
static int elua_lines(lua_State *L) {
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(L, elua_readline, 1);
|
||||
return 1;
|
||||
static int
|
||||
elua_lines(lua_State *L)
|
||||
{
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushcclosure(L, elua_readline, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int read_number(lua_State *L, FILE *f) {
|
||||
lua_Number d;
|
||||
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
|
||||
static int
|
||||
read_number(lua_State *L, FILE *f)
|
||||
{
|
||||
lua_Number d;
|
||||
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1)
|
||||
{
|
||||
lua_pushnumber(L, d);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_eof(lua_State *L, FILE *f) {
|
||||
int c = getc(f);
|
||||
ungetc(c, f);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
return (c != EOF);
|
||||
static int
|
||||
test_eof(lua_State *L, FILE *f)
|
||||
{
|
||||
int c = getc(f);
|
||||
ungetc(c, f);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
return (c != EOF);
|
||||
}
|
||||
|
||||
static int read_line(lua_State *L, FILE *f) {
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (;;) {
|
||||
static int
|
||||
read_line(lua_State *L, FILE *f)
|
||||
{
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
for (;;)
|
||||
{
|
||||
size_t l;
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) {
|
||||
luaL_pushresult(&b);
|
||||
return (lua_strlen(L, -1) > 0);
|
||||
}
|
||||
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL)
|
||||
{
|
||||
luaL_pushresult(&b);
|
||||
return (lua_strlen(L, -1) > 0);
|
||||
}
|
||||
l = strlen(p);
|
||||
if (!l || p[l - 1] != '\n') {
|
||||
luaL_addsize(&b, l);
|
||||
} else {
|
||||
luaL_addsize(&b, l - 1);
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!l || p[l - 1] != '\n')
|
||||
luaL_addsize(&b, l);
|
||||
else
|
||||
{
|
||||
luaL_addsize(&b, l - 1);
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int read_chars(lua_State *L, FILE *f, size_t n) {
|
||||
size_t rlen;
|
||||
size_t nr;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
rlen = LUAL_BUFFERSIZE;
|
||||
do {
|
||||
static int
|
||||
read_chars(lua_State *L, FILE *f, size_t n)
|
||||
{
|
||||
size_t rlen;
|
||||
size_t nr;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
rlen = LUAL_BUFFERSIZE;
|
||||
do
|
||||
{
|
||||
char *p = luaL_prepbuffer(&b);
|
||||
if (rlen > n) rlen = n;
|
||||
nr = fread(p, sizeof(char), rlen, f);
|
||||
luaL_addsize(&b, nr);
|
||||
n -= nr;
|
||||
} while (n > 0 && nr == rlen);
|
||||
luaL_pushresult(&b);
|
||||
return (n == 0 || lua_strlen(L, -1) > 0);
|
||||
} while (n > 0 && nr == rlen);
|
||||
luaL_pushresult(&b);
|
||||
return (n == 0 || lua_strlen(L, -1) > 0);
|
||||
}
|
||||
|
||||
static int elua_readline(lua_State *L) {
|
||||
FILE *f = *(FILE**)lua_touserdata(L, lua_upvalueindex(1));
|
||||
int success;
|
||||
if (!f) {
|
||||
static int
|
||||
elua_readline(lua_State *L)
|
||||
{
|
||||
FILE *f = *(FILE**)lua_touserdata(L, lua_upvalueindex(1));
|
||||
int success;
|
||||
if (!f)
|
||||
{
|
||||
luaL_error(L, "file is already closed");
|
||||
}
|
||||
success = read_line(L, f);
|
||||
if (ferror(f)) {
|
||||
return luaL_error(L, "%s", strerror(errno));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
success = read_line(L, f);
|
||||
if (ferror(f))
|
||||
return luaL_error(L, "%s", strerror(errno));
|
||||
return success;
|
||||
}
|
||||
|
||||
static int elua_read(lua_State *L) {
|
||||
FILE *f = tofile(L);
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int first = 2;
|
||||
int success, n;
|
||||
clearerr(f);
|
||||
if (!nargs) {
|
||||
static int
|
||||
elua_read(lua_State *L)
|
||||
{
|
||||
FILE *f = tofile(L);
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int first = 2;
|
||||
int success, n;
|
||||
clearerr(f);
|
||||
if (!nargs)
|
||||
{
|
||||
success = read_line(L, f);
|
||||
n = first + 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
luaL_checkstack(L, nargs + LUA_MINSTACK, "too many arguments");
|
||||
success = 1;
|
||||
for (n = first; nargs-- && success; ++n) {
|
||||
if (lua_type(L, n) == LUA_TNUMBER) {
|
||||
size_t l = (size_t)lua_tointeger(L, n);
|
||||
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
||||
} else {
|
||||
const char *p = lua_tostring(L, n);
|
||||
luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
|
||||
switch (p[1]) {
|
||||
case 'n':
|
||||
success = read_number(L, f);
|
||||
break;
|
||||
case 'l':
|
||||
success = read_line(L, f);
|
||||
break;
|
||||
case 'a':
|
||||
read_chars(L, f, ~((size_t)0));
|
||||
success = 1;
|
||||
break;
|
||||
default:
|
||||
return luaL_argerror(L, n, "invalid format");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ferror(f)) {
|
||||
return push_ret(L, 0, NULL);
|
||||
}
|
||||
if (!success) {
|
||||
for (n = first; nargs-- && success; ++n)
|
||||
{
|
||||
if (lua_type(L, n) == LUA_TNUMBER)
|
||||
{
|
||||
size_t l = (size_t)lua_tointeger(L, n);
|
||||
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *p = lua_tostring(L, n);
|
||||
luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
|
||||
switch (p[1])
|
||||
{
|
||||
case 'n':
|
||||
success = read_number(L, f);
|
||||
break;
|
||||
case 'l':
|
||||
success = read_line(L, f);
|
||||
break;
|
||||
case 'a':
|
||||
read_chars(L, f, ~((size_t)0));
|
||||
success = 1;
|
||||
break;
|
||||
default:
|
||||
return luaL_argerror(L, n, "invalid format");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ferror(f))
|
||||
return push_ret(L, 0, NULL);
|
||||
if (!success)
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return n - first;
|
||||
}
|
||||
return n - first;
|
||||
}
|
||||
|
||||
static int elua_write(lua_State *L) {
|
||||
FILE *f = tofile(L);
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int status = 1, arg = 2;
|
||||
for (; nargs--; ++arg) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||
status = status && (fprintf(f, LUA_NUMBER_FMT,
|
||||
lua_tonumber(L, arg)) > 0);
|
||||
} else {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||
}
|
||||
}
|
||||
return push_ret(L, status, NULL);
|
||||
static int
|
||||
elua_write(lua_State *L)
|
||||
{
|
||||
FILE *f = tofile(L);
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int status = 1, arg = 2;
|
||||
for (; nargs--; ++arg)
|
||||
{
|
||||
if (lua_type(L, arg) == LUA_TNUMBER)
|
||||
status = status && (fprintf(f, LUA_NUMBER_FMT,
|
||||
lua_tonumber(L, arg)) > 0);
|
||||
else
|
||||
{
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||
}
|
||||
}
|
||||
return push_ret(L, status, NULL);
|
||||
}
|
||||
|
||||
static int elua_fgc(lua_State *L) {
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
if (*f) {
|
||||
static int
|
||||
elua_fgc(lua_State *L)
|
||||
{
|
||||
FILE **f = (FILE**)luaL_checkudata(L, 1, "ELUA_FILE*");
|
||||
if (*f)
|
||||
{
|
||||
fclose(*f);
|
||||
*f = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int elua_ftostring(lua_State *L) {
|
||||
FILE *f = *((FILE**)luaL_checkudata(L, 1, "ELUA_FILE*"));
|
||||
if (!f) {
|
||||
lua_pushliteral(L, "file (closed)");
|
||||
} else {
|
||||
lua_pushfstring(L, "file (%p)", f);
|
||||
}
|
||||
return 1;
|
||||
static int
|
||||
elua_ftostring(lua_State *L)
|
||||
{
|
||||
FILE *f = *((FILE**)luaL_checkudata(L, 1, "ELUA_FILE*"));
|
||||
if (!f)
|
||||
lua_pushliteral(L, "file (closed)");
|
||||
else
|
||||
lua_pushfstring(L, "file (%p)", f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_reg elua_popenlib[] = {
|
||||
{ "close" , elua_close },
|
||||
{ "flush" , elua_flush },
|
||||
{ "lines" , elua_lines },
|
||||
{ "read" , elua_read },
|
||||
{ "write" , elua_write },
|
||||
{ "__gc" , elua_fgc },
|
||||
{ "__tostring", elua_ftostring },
|
||||
{ NULL , NULL }
|
||||
static const luaL_reg elua_popenlib[] =
|
||||
{
|
||||
{ "close" , elua_close },
|
||||
{ "flush" , elua_flush },
|
||||
{ "lines" , elua_lines },
|
||||
{ "read" , elua_read },
|
||||
{ "write" , elua_write },
|
||||
{ "__gc" , elua_fgc },
|
||||
{ "__tostring", elua_ftostring },
|
||||
{ NULL , NULL }
|
||||
};
|
||||
|
||||
static FILE **elua_newfile(lua_State *L) {
|
||||
FILE **f = (FILE**)lua_newuserdata(L, sizeof(FILE*));
|
||||
*f = NULL;
|
||||
if (luaL_newmetatable(L, "ELUA_FILE*")) {
|
||||
static FILE **
|
||||
elua_newfile(lua_State *L)
|
||||
{
|
||||
FILE **f = (FILE**)lua_newuserdata(L, sizeof(FILE*));
|
||||
*f = NULL;
|
||||
if (luaL_newmetatable(L, "ELUA_FILE*"))
|
||||
{
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield (L, -2, "__index");
|
||||
luaL_register(L, NULL, elua_popenlib);
|
||||
}
|
||||
lua_setmetatable(L, -2);
|
||||
return f;
|
||||
}
|
||||
lua_setmetatable(L, -2);
|
||||
return f;
|
||||
}
|
||||
|
||||
int elua_popen(lua_State *L) {
|
||||
const char *fname = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
int nargs = lua_gettop(L) - 2;
|
||||
FILE **pf = elua_newfile(L);
|
||||
if (nargs > 0) {
|
||||
int
|
||||
elua_popen(lua_State *L)
|
||||
{
|
||||
const char *fname = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
int nargs = lua_gettop(L) - 2;
|
||||
FILE **pf = elua_newfile(L);
|
||||
if (nargs > 0)
|
||||
{
|
||||
const char **argv = (const char**)alloca((nargs + 2) * sizeof(char*));
|
||||
memset(argv, 0, (nargs + 2) * sizeof(char*));
|
||||
for (; nargs; --nargs) {
|
||||
argv[nargs] = lua_tostring(L, nargs + 2);
|
||||
}
|
||||
for (; nargs; --nargs)
|
||||
{
|
||||
argv[nargs] = lua_tostring(L, nargs + 2);
|
||||
}
|
||||
argv[0] = fname;
|
||||
*pf = elua_popen_c(fname, argv, mode);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
const char **argv = (const char**)alloca(2 * sizeof(char*));
|
||||
argv[0] = fname;
|
||||
argv[1] = NULL;
|
||||
*pf = elua_popen_c(fname, argv, mode);
|
||||
}
|
||||
return (!*pf) ? push_ret(L, 0, fname) : 1;
|
||||
}
|
||||
return (!*pf) ? push_ret(L, 0, fname) : 1;
|
||||
}
|
|
@ -4,498 +4,588 @@
|
|||
|
||||
#include "main.h"
|
||||
|
||||
typedef struct Arg_Data {
|
||||
int type;
|
||||
const char *value;
|
||||
typedef struct Arg_Data
|
||||
{
|
||||
int type;
|
||||
const char *value;
|
||||
} Arg_Data;
|
||||
|
||||
enum {
|
||||
ARG_CODE = 0, ARG_LIBRARY, ARG_LIBDIR
|
||||
enum
|
||||
{
|
||||
ARG_CODE = 0,
|
||||
ARG_LIBRARY,
|
||||
ARG_LIBDIR
|
||||
};
|
||||
|
||||
static Eina_List *modlist = NULL;
|
||||
static int require_ref = LUA_REFNIL;
|
||||
static int appload_ref = LUA_REFNIL;
|
||||
static const char *progname = NULL;
|
||||
static Eina_List *modlist = NULL;
|
||||
static int require_ref = LUA_REFNIL;
|
||||
static int appload_ref = LUA_REFNIL;
|
||||
static const char *progname = NULL;
|
||||
|
||||
int el_log_domain = -1;
|
||||
|
||||
static void errmsg(const char *pname, const char *msg) {
|
||||
ERR("%s%s%s", pname ? pname : "", pname ? ": " : "", msg);
|
||||
static void
|
||||
errmsg(const char *pname, const char *msg)
|
||||
{
|
||||
ERR("%s%s%s", pname ? pname : "", pname ? ": " : "", msg);
|
||||
}
|
||||
|
||||
static int report(lua_State *L, int status) {
|
||||
if (status && !lua_isnil(L, -1)) {
|
||||
static int
|
||||
report(lua_State *L, int status)
|
||||
{
|
||||
if (status && !lua_isnil(L, -1))
|
||||
{
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
errmsg(progname, msg ? msg : "(non-string error)");
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static int traceback(lua_State *L) {
|
||||
lua_getglobal(L, "debug");
|
||||
if (!lua_istable(L, -1)) {
|
||||
static int
|
||||
traceback(lua_State *L)
|
||||
{
|
||||
lua_getglobal(L, "debug");
|
||||
if (!lua_istable(L, -1))
|
||||
{
|
||||
lua_pop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
lua_getfield(L, -1, "traceback");
|
||||
if (!lua_isfunction(L, -1)) {
|
||||
}
|
||||
lua_getfield(L, -1, "traceback");
|
||||
if (!lua_isfunction(L, -1))
|
||||
{
|
||||
lua_pop(L, 2);
|
||||
return 1;
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushinteger(L, 2);
|
||||
lua_call(L, 2, 1);
|
||||
return 1;
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushinteger(L, 2);
|
||||
lua_call(L, 2, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int docall(lua_State *L, int narg, int nret) {
|
||||
int status;
|
||||
int bs = lua_gettop(L) - narg;
|
||||
lua_pushcfunction(L, traceback);
|
||||
lua_insert(L, bs);
|
||||
status = lua_pcall(L, narg, nret, bs);
|
||||
lua_remove(L, bs);
|
||||
if (status) lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
return status;
|
||||
static int
|
||||
docall(lua_State *L, int narg, int nret)
|
||||
{
|
||||
int status;
|
||||
int bs = lua_gettop(L) - narg;
|
||||
lua_pushcfunction(L, traceback);
|
||||
lua_insert(L, bs);
|
||||
status = lua_pcall(L, narg, nret, bs);
|
||||
lua_remove(L, bs);
|
||||
if (status)
|
||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
static int getargs(lua_State *L, int argc, char **argv, int n) {
|
||||
int i;
|
||||
int narg = argc - (n + 1);
|
||||
luaL_checkstack(L, narg + 3, "too many arguments to script");
|
||||
for (i = n + 1; i < argc; ++i) lua_pushstring(L, argv[i]);
|
||||
lua_createtable(L, narg, n + 1);
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (!(i - n) && argv[i][0] == ':') {
|
||||
lua_pushstring(L, &argv[i][1]);
|
||||
} else {
|
||||
lua_pushstring(L, argv[i]);
|
||||
}
|
||||
static int
|
||||
getargs(lua_State *L, int argc, char **argv, int n)
|
||||
{
|
||||
int i;
|
||||
int narg = argc - (n + 1);
|
||||
luaL_checkstack(L, narg + 3, "too many arguments to script");
|
||||
for (i = n + 1; i < argc; ++i)
|
||||
{
|
||||
lua_pushstring(L, argv[i]);
|
||||
}
|
||||
lua_createtable(L, narg, n + 1);
|
||||
for (i = 0; i < argc; ++i)
|
||||
{
|
||||
if (!(i - n) && argv[i][0] == ':')
|
||||
lua_pushstring(L, &argv[i][1]);
|
||||
else
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawseti(L, -2, i - n);
|
||||
}
|
||||
return narg;
|
||||
}
|
||||
return narg;
|
||||
}
|
||||
|
||||
static int init_module(lua_State *L) {
|
||||
if (!lua_isnoneornil(L, 1)) {
|
||||
static int
|
||||
init_module(lua_State *L)
|
||||
{
|
||||
if (!lua_isnoneornil(L, 1))
|
||||
{
|
||||
lua_pushvalue(L, 1);
|
||||
lua_call(L, 0, 0);
|
||||
}
|
||||
if (!lua_isnoneornil(L, 2)) {
|
||||
}
|
||||
if (!lua_isnoneornil(L, 2))
|
||||
{
|
||||
lua_pushvalue(L, 2);
|
||||
modlist = eina_list_append(modlist,
|
||||
(void*)(size_t)luaL_ref(L, LUA_REGISTRYINDEX));
|
||||
}
|
||||
return 0;
|
||||
(void*)(size_t)luaL_ref(L, LUA_REGISTRYINDEX));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int register_require(lua_State *L) {
|
||||
const char *corepath = lua_touserdata(L, lua_upvalueindex(1));
|
||||
const char *modpath = lua_touserdata(L, lua_upvalueindex(2));
|
||||
const char *appspath = lua_touserdata(L, lua_upvalueindex(3));
|
||||
Eina_List *largs = lua_touserdata(L, lua_upvalueindex(4)), *l = NULL;
|
||||
Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(5));
|
||||
Arg_Data *data = NULL;
|
||||
int n = 2;
|
||||
lua_pushvalue(L, 1);
|
||||
require_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
lua_pushvalue(L, 2);
|
||||
appload_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
if (getenv("EFL_RUN_IN_TREE")) {
|
||||
static int
|
||||
register_require(lua_State *L)
|
||||
{
|
||||
const char *corepath = lua_touserdata(L, lua_upvalueindex(1));
|
||||
const char *modpath = lua_touserdata(L, lua_upvalueindex(2));
|
||||
const char *appspath = lua_touserdata(L, lua_upvalueindex(3));
|
||||
Eina_List *largs = lua_touserdata(L, lua_upvalueindex(4)), *l = NULL;
|
||||
Eina_Bool noenv = lua_toboolean (L, lua_upvalueindex(5));
|
||||
Arg_Data *data = NULL;
|
||||
int n = 2;
|
||||
lua_pushvalue(L, 1);
|
||||
require_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
lua_pushvalue(L, 2);
|
||||
appload_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
if (getenv("EFL_RUN_IN_TREE"))
|
||||
{
|
||||
corepath = PACKAGE_BUILD_DIR "/src/bin/elua/core";
|
||||
modpath = PACKAGE_BUILD_DIR "/src/bin/elua/modules";
|
||||
appspath = PACKAGE_BUILD_DIR "/src/bin/elua/apps";
|
||||
} else {
|
||||
if (!corepath) {
|
||||
if (noenv || !(corepath = getenv("ELUA_CORE_DIR")) || !corepath[0])
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!corepath)
|
||||
{
|
||||
if (noenv || !(corepath = getenv("ELUA_CORE_DIR")) || !corepath[0])
|
||||
corepath = ELUA_CORE_DIR;
|
||||
}
|
||||
if (!modpath) {
|
||||
if (noenv || !(modpath = getenv("ELUA_MODULES_DIR")) || !modpath[0])
|
||||
}
|
||||
if (!modpath)
|
||||
{
|
||||
if (noenv || !(modpath = getenv("ELUA_MODULES_DIR")) || !modpath[0])
|
||||
modpath = ELUA_MODULES_DIR;
|
||||
}
|
||||
if (!appspath) {
|
||||
if (noenv || !(appspath = getenv("ELUA_APPS_DIR")) || !appspath[0])
|
||||
}
|
||||
if (!appspath)
|
||||
{
|
||||
if (noenv || !(appspath = getenv("ELUA_APPS_DIR")) || !appspath[0])
|
||||
appspath = ELUA_APPS_DIR;
|
||||
}
|
||||
}
|
||||
lua_pushfstring(L, "%s/?.lua;", corepath);
|
||||
EINA_LIST_FOREACH(largs, l, data) {
|
||||
}
|
||||
}
|
||||
lua_pushfstring(L, "%s/?.lua;", corepath);
|
||||
EINA_LIST_FOREACH(largs, l, data)
|
||||
{
|
||||
if (data->type != ARG_LIBDIR) continue;
|
||||
lua_pushfstring(L, "%s/?.lua;", data->value);
|
||||
++n;
|
||||
}
|
||||
lua_pushfstring(L, "%s/?.lua;", modpath);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_concat(L, n + 1);
|
||||
lua_pushfstring(L, "%s/?.lua;", appspath);
|
||||
lua_pushvalue(L, 4);
|
||||
lua_concat(L, 2);
|
||||
return 2;
|
||||
}
|
||||
lua_pushfstring(L, "%s/?.lua;", modpath);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_concat(L, n + 1);
|
||||
lua_pushfstring(L, "%s/?.lua;", appspath);
|
||||
lua_pushvalue(L, 4);
|
||||
lua_concat(L, 2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int dolib(lua_State *L, const char *libname) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, require_ref);
|
||||
lua_pushstring(L, libname);
|
||||
return report(L, lua_pcall(L, 1, 0, 0));
|
||||
static int
|
||||
dolib(lua_State *L, const char *libname)
|
||||
{
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, require_ref);
|
||||
lua_pushstring(L, libname);
|
||||
return report(L, lua_pcall(L, 1, 0, 0));
|
||||
}
|
||||
|
||||
static int dofile(lua_State *L, const char *fname) {
|
||||
return report(L, elua_loadfile(L, fname) || docall(L, 0, 1));
|
||||
static int
|
||||
dofile(lua_State *L, const char *fname)
|
||||
{
|
||||
return report(L, elua_loadfile(L, fname) || docall(L, 0, 1));
|
||||
}
|
||||
|
||||
static int dostr(lua_State *L, const char *chunk, const char *chname) {
|
||||
return report(L, luaL_loadbuffer(L, chunk, strlen(chunk), chname)
|
||||
|| docall(L, 0, 0));
|
||||
static int
|
||||
dostr(lua_State *L, const char *chunk, const char *chname)
|
||||
{
|
||||
return report(L, luaL_loadbuffer(L, chunk, strlen(chunk), chname)
|
||||
|| docall(L, 0, 0));
|
||||
}
|
||||
|
||||
static Eina_Bool loadapp(lua_State *L, const char *appname) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, appload_ref);
|
||||
lua_pushstring(L, appname);
|
||||
lua_call(L, 1, 2);
|
||||
if (lua_isnil(L, -2)) {
|
||||
static Eina_Bool
|
||||
loadapp(lua_State *L, const char *appname)
|
||||
{
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, appload_ref);
|
||||
lua_pushstring(L, appname);
|
||||
lua_call(L, 1, 2);
|
||||
if (lua_isnil(L, -2))
|
||||
{
|
||||
lua_remove(L, -2);
|
||||
return EINA_FALSE;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return EINA_TRUE;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return EINA_TRUE;
|
||||
}
|
||||
|
||||
static int doscript(lua_State *L, int argc, char **argv, int n, int *quit) {
|
||||
int status;
|
||||
const char *fname = argv[n];
|
||||
int narg = getargs(L, argc, argv, n);
|
||||
lua_setglobal(L, "arg");
|
||||
if (fname[0] == '-' && !fname[1]) {
|
||||
static int
|
||||
doscript(lua_State *L, int argc, char **argv, int n, int *quit)
|
||||
{
|
||||
int status;
|
||||
const char *fname = argv[n];
|
||||
int narg = getargs(L, argc, argv, n);
|
||||
lua_setglobal(L, "arg");
|
||||
if (fname[0] == '-' && !fname[1])
|
||||
{
|
||||
fname = NULL;
|
||||
}
|
||||
if (fname && fname[0] == ':') {
|
||||
}
|
||||
if (fname && fname[0] == ':')
|
||||
{
|
||||
status = !loadapp(L, fname + 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
status = elua_loadfile(L, fname);
|
||||
}
|
||||
lua_insert(L, -(narg + 1));
|
||||
if (!status) {
|
||||
}
|
||||
lua_insert(L, -(narg + 1));
|
||||
if (!status)
|
||||
{
|
||||
status = docall(L, narg, 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pop(L, narg);
|
||||
}
|
||||
if (!status) {
|
||||
}
|
||||
if (!status)
|
||||
{
|
||||
*quit = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return report(L, status);
|
||||
}
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
void shutdown(lua_State *L, int c) {
|
||||
void *data;
|
||||
INF("elua shutdown");
|
||||
void
|
||||
shutdown(lua_State *L, int c)
|
||||
{
|
||||
void *data;
|
||||
INF("elua shutdown");
|
||||
|
||||
EINA_LIST_FREE(modlist, data) {
|
||||
EINA_LIST_FREE(modlist, data)
|
||||
{
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, (size_t)data);
|
||||
lua_call(L, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (L) lua_close(L);
|
||||
eina_shutdown();
|
||||
exit(c);
|
||||
if (L) lua_close(L);
|
||||
eina_shutdown();
|
||||
exit(c);
|
||||
}
|
||||
|
||||
static int cb_ref = LUA_REFNIL;
|
||||
static lua_State *LL = NULL;
|
||||
static int cb_ref = LUA_REFNIL;
|
||||
static lua_State *LL = NULL;
|
||||
|
||||
static void smart_cb_wrapper(void *data, void *obj EINA_UNUSED,
|
||||
void *einfo EINA_UNUSED) {
|
||||
int idx = (size_t)data;
|
||||
lua_rawgeti(LL, LUA_REGISTRYINDEX, cb_ref);
|
||||
lua_rawgeti(LL, -1, idx);
|
||||
lua_call(LL, 0, 0);
|
||||
lua_pop(LL, 1);
|
||||
static void
|
||||
smart_cb_wrapper(void *data, void *obj EINA_UNUSED, void *einfo EINA_UNUSED)
|
||||
{
|
||||
int idx = (size_t)data;
|
||||
lua_rawgeti(LL, LUA_REGISTRYINDEX, cb_ref);
|
||||
lua_rawgeti(LL, -1, idx);
|
||||
lua_call(LL, 0, 0);
|
||||
lua_pop(LL, 1);
|
||||
}
|
||||
|
||||
static int register_callbacks(lua_State *L) {
|
||||
union { void (*fptr)(void*, void*, void*); void *ptr; } u;
|
||||
lua_pushvalue(L, 1);
|
||||
cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
u.fptr = smart_cb_wrapper;
|
||||
lua_pushlightuserdata(L, u.ptr);
|
||||
return 1;
|
||||
static int
|
||||
register_callbacks(lua_State *L)
|
||||
{
|
||||
union { void (*fptr)(void*, void*, void*); void *ptr; } u;
|
||||
lua_pushvalue(L, 1);
|
||||
cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
u.fptr = smart_cb_wrapper;
|
||||
lua_pushlightuserdata(L, u.ptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int elua_build_args(lua_State *L) {
|
||||
/* todo: will probably need adjustments for Windows */
|
||||
int nargs = lua_gettop(L), n = 0, i;
|
||||
for (i = 1; i <= nargs; ++i) {
|
||||
static int
|
||||
elua_build_args(lua_State *L)
|
||||
{
|
||||
/* todo: will probably need adjustments for Windows */
|
||||
int nargs = lua_gettop(L), n = 0, i;
|
||||
for (i = 1; i <= nargs; ++i)
|
||||
{
|
||||
lua_pushliteral(L, "'"); ++n;
|
||||
lua_pushvalue(L, i); ++n;
|
||||
lua_pushliteral(L, "'"); ++n;
|
||||
if (i != nargs) {
|
||||
lua_pushliteral(L, " "); ++n;
|
||||
}
|
||||
}
|
||||
lua_concat(L, n);
|
||||
return 1;
|
||||
if (i != nargs)
|
||||
lua_pushliteral(L, " "); ++n;
|
||||
}
|
||||
lua_concat(L, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct Main_Data {
|
||||
int argc;
|
||||
char **argv;
|
||||
int status;
|
||||
struct Main_Data
|
||||
{
|
||||
int argc;
|
||||
char **argv;
|
||||
int status;
|
||||
};
|
||||
|
||||
int elua_popen(lua_State *L);
|
||||
|
||||
const luaL_reg cutillib[] = {
|
||||
{ "init_module" , init_module },
|
||||
{ "register_callbacks", register_callbacks },
|
||||
{ "build_args" , elua_build_args },
|
||||
{ "popenv" , elua_popen },
|
||||
{ NULL , NULL }
|
||||
const luaL_reg cutillib[] =
|
||||
{
|
||||
{ "init_module" , init_module },
|
||||
{ "register_callbacks", register_callbacks },
|
||||
{ "build_args" , elua_build_args },
|
||||
{ "popenv" , elua_popen },
|
||||
{ NULL , NULL }
|
||||
};
|
||||
|
||||
static int gettext_bind_textdomain(lua_State *L) {
|
||||
static int
|
||||
gettext_bind_textdomain(lua_State *L)
|
||||
{
|
||||
#if ENABLE_NLS
|
||||
const char *textdomain = luaL_checkstring(L, 1);
|
||||
const char *dirname = luaL_checkstring(L, 2);
|
||||
const char *ret;
|
||||
if (!textdomain[0] || !strcmp(textdomain, PACKAGE)) {
|
||||
const char *textdomain = luaL_checkstring(L, 1);
|
||||
const char *dirname = luaL_checkstring(L, 2);
|
||||
const char *ret;
|
||||
if (!textdomain[0] || !strcmp(textdomain, PACKAGE))
|
||||
{
|
||||
lua_pushnil(L);
|
||||
lua_pushliteral(L, "invalid textdomain");
|
||||
return 2;
|
||||
}
|
||||
if (!(ret = bindtextdomain(textdomain, dirname))) {
|
||||
}
|
||||
if (!(ret = bindtextdomain(textdomain, dirname)))
|
||||
{
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
bind_textdomain_codeset(textdomain, "UTF-8");
|
||||
lua_pushstring(L, ret);
|
||||
return 1;
|
||||
}
|
||||
bind_textdomain_codeset(textdomain, "UTF-8");
|
||||
lua_pushstring(L, ret);
|
||||
return 1;
|
||||
#else
|
||||
lua_pushliteral(L, "");
|
||||
return 1;
|
||||
lua_pushliteral(L, "");
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
const luaL_reg gettextlib[] = {
|
||||
{ "bind_textdomain", gettext_bind_textdomain },
|
||||
{ NULL, NULL }
|
||||
const luaL_reg gettextlib[] =
|
||||
{
|
||||
{ "bind_textdomain", gettext_bind_textdomain },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static void print_help(const char *progname, FILE *stream) {
|
||||
fprintf(stream, "Usage: %s [OPTIONS] [SCRIPT [ARGS]]\n\n"
|
||||
"A main entry for all EFL/LuaJIT powered applications.\n\n"
|
||||
"The following options are supported:\n\n"
|
||||
""
|
||||
" -h, --help Show this message.\n"
|
||||
" -l, --license Show a license message.\n"
|
||||
" -C[COREDIR], --core-dir=[COREDIR] Elua core directory path.\n"
|
||||
" -M[MODDIR], --modules-dir=[MODDIR] Elua modules directory path.\n"
|
||||
" -A[APPDIR], --apps-dir=[APPDIR] Elua applications directory path.\n"
|
||||
" -e[CODE], --execute=[CODE] Execute string 'code'.\n"
|
||||
" -l[LIBRARY], --library=[LIBRARY] Require library 'library'.\n"
|
||||
" -I[DIR], --lib-dir=[DIR] Append an additional require path.\n"
|
||||
" -E, --noenv Ignore environment variables.\n", progname);
|
||||
static void
|
||||
print_help(const char *progname, FILE *stream)
|
||||
{
|
||||
fprintf(stream, "Usage: %s [OPTIONS] [SCRIPT [ARGS]]\n\n"
|
||||
"A main entry for all EFL/LuaJIT powered applications.\n\n"
|
||||
"The following options are supported:\n\n"
|
||||
""
|
||||
" -h, --help Show this message.\n"
|
||||
" -l, --license Show a license message.\n"
|
||||
" -C[COREDIR], --core-dir=[COREDIR] Elua core directory path.\n"
|
||||
" -M[MODDIR], --modules-dir=[MODDIR] Elua modules directory path.\n"
|
||||
" -A[APPDIR], --apps-dir=[APPDIR] Elua applications directory path.\n"
|
||||
" -e[CODE], --execute=[CODE] Execute string 'code'.\n"
|
||||
" -l[LIBRARY], --library=[LIBRARY] Require library 'library'.\n"
|
||||
" -I[DIR], --lib-dir=[DIR] Append an additional require path.\n"
|
||||
" -E, --noenv Ignore environment variables.\n", progname);
|
||||
}
|
||||
|
||||
static struct option lopt[] = {
|
||||
{ "help" , no_argument , NULL, 'h' },
|
||||
static struct option lopt[] =
|
||||
{
|
||||
{ "help" , no_argument , NULL, 'h' },
|
||||
|
||||
{ "core-dir" , required_argument, NULL, 'C' },
|
||||
{ "modules-dir", required_argument, NULL, 'M' },
|
||||
{ "apps-dir" , required_argument, NULL, 'A' },
|
||||
{ "core-dir" , required_argument, NULL, 'C' },
|
||||
{ "modules-dir", required_argument, NULL, 'M' },
|
||||
{ "apps-dir" , required_argument, NULL, 'A' },
|
||||
|
||||
{ "execute" , required_argument, NULL, 'e' },
|
||||
{ "library" , required_argument, NULL, 'l' },
|
||||
{ "lib-dir" , required_argument, NULL, 'I' },
|
||||
{ "noenv" , no_argument , NULL, 'E' },
|
||||
{ NULL , 0 , NULL, 0 }
|
||||
{ "execute" , required_argument, NULL, 'e' },
|
||||
{ "library" , required_argument, NULL, 'l' },
|
||||
{ "lib-dir" , required_argument, NULL, 'I' },
|
||||
{ "noenv" , no_argument , NULL, 'E' },
|
||||
{ NULL , 0 , NULL, 0 }
|
||||
};
|
||||
|
||||
/* protected main */
|
||||
static int lua_main(lua_State *L) {
|
||||
Eina_Bool noenv = EINA_FALSE,
|
||||
hasexec = EINA_FALSE;
|
||||
Eina_List *largs = NULL, *l = NULL;
|
||||
Arg_Data *data = NULL;
|
||||
const char *coref = NULL;
|
||||
char *coredir = NULL, *moddir = NULL, *appsdir = NULL;
|
||||
char modfile[1024];
|
||||
static int
|
||||
lua_main(lua_State *L)
|
||||
{
|
||||
Eina_Bool noenv = EINA_FALSE,
|
||||
hasexec = EINA_FALSE;
|
||||
Eina_List *largs = NULL, *l = NULL;
|
||||
Arg_Data *data = NULL;
|
||||
const char *coref = NULL;
|
||||
char *coredir = NULL, *moddir = NULL, *appsdir = NULL;
|
||||
char modfile[1024];
|
||||
|
||||
int ch;
|
||||
int ch;
|
||||
|
||||
struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1);
|
||||
struct Main_Data *m = (struct Main_Data*)lua_touserdata(L, 1);
|
||||
|
||||
int argc = m->argc;
|
||||
char **argv = m->argv;
|
||||
int argc = m->argc;
|
||||
char **argv = m->argv;
|
||||
|
||||
#if ENABLE_NLS
|
||||
char *(*dgettextp)(const char*, const char*) = dgettext;
|
||||
char *(*dngettextp)(const char*, const char*, const char*, unsigned long)
|
||||
= dngettext;
|
||||
char *(*dgettextp)(const char*, const char*) = dgettext;
|
||||
char *(*dngettextp)(const char*, const char*, const char*, unsigned long)
|
||||
= dngettext;
|
||||
#endif
|
||||
|
||||
progname = (argv[0] && argv[0][0]) ? argv[0] : "elua";
|
||||
progname = (argv[0] && argv[0][0]) ? argv[0] : "elua";
|
||||
|
||||
while ((ch = getopt_long(argc, argv, "+LhC:M:A:e:l:I:E", lopt, NULL)) != -1) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
while ((ch = getopt_long(argc, argv, "+LhC:M:A:e:l:I:E", lopt, NULL)) != -1)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
print_help(progname, stdout);
|
||||
return 0;
|
||||
case 'C':
|
||||
case 'C':
|
||||
coredir = optarg;
|
||||
break;
|
||||
case 'M':
|
||||
case 'M':
|
||||
moddir = optarg;
|
||||
break;
|
||||
case 'A':
|
||||
case 'A':
|
||||
appsdir = optarg;
|
||||
break;
|
||||
case 'e':
|
||||
case 'l':
|
||||
case 'I': {
|
||||
Arg_Data *v = malloc(sizeof(Arg_Data));
|
||||
v->type = (ch == 'e') ? ARG_CODE : ((ch == 'l')
|
||||
? ARG_LIBRARY : ARG_LIBDIR);
|
||||
v->value = optarg;
|
||||
largs = eina_list_append(largs, v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
case 'e':
|
||||
case 'l':
|
||||
case 'I':
|
||||
{
|
||||
Arg_Data *v = malloc(sizeof(Arg_Data));
|
||||
v->type = (ch == 'e') ? ARG_CODE : ((ch == 'l')
|
||||
? ARG_LIBRARY : ARG_LIBDIR);
|
||||
v->value = optarg;
|
||||
largs = eina_list_append(largs, v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INF("arguments parsed");
|
||||
INF("arguments parsed");
|
||||
|
||||
lua_gc(L, LUA_GCSTOP, 0);
|
||||
lua_gc(L, LUA_GCSTOP, 0);
|
||||
|
||||
luaL_openlibs(L);
|
||||
luaL_openlibs(L);
|
||||
|
||||
if (getenv("EFL_RUN_IN_TREE")) {
|
||||
if (getenv("EFL_RUN_IN_TREE"))
|
||||
{
|
||||
Arg_Data *v = malloc(sizeof(Arg_Data));
|
||||
v->type = ARG_LIBDIR;
|
||||
v->value = PACKAGE_BUILD_DIR "/src/bindings/luajit";
|
||||
largs = eina_list_append(largs, v);
|
||||
coref = PACKAGE_BUILD_DIR "/src/bin/elua/core";
|
||||
} else if (!(coref = coredir)) {
|
||||
if (noenv || !(coref = getenv("ELUA_CORE_DIR")) || !coref[0]) {
|
||||
coref = ELUA_CORE_DIR;
|
||||
}
|
||||
}
|
||||
snprintf(modfile, sizeof(modfile), "%s/module.lua", coref);
|
||||
if (report(L, elua_loadfile(L, modfile))) {
|
||||
}
|
||||
else if (!(coref = coredir))
|
||||
{
|
||||
if (noenv || !(coref = getenv("ELUA_CORE_DIR")) || !coref[0])
|
||||
coref = ELUA_CORE_DIR;
|
||||
}
|
||||
snprintf(modfile, sizeof(modfile), "%s/module.lua", coref);
|
||||
if (report(L, elua_loadfile(L, modfile)))
|
||||
{
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
lua_pushlightuserdata(L, coredir);
|
||||
lua_pushlightuserdata(L, moddir);
|
||||
lua_pushlightuserdata(L, appsdir);
|
||||
lua_pushlightuserdata(L, largs);
|
||||
lua_pushboolean (L, noenv);
|
||||
lua_pushcclosure(L, register_require, 5);
|
||||
lua_createtable(L, 0, 0);
|
||||
luaL_register(L, NULL, cutillib);
|
||||
lua_call(L, 2, 0);
|
||||
}
|
||||
lua_pushlightuserdata(L, coredir);
|
||||
lua_pushlightuserdata(L, moddir);
|
||||
lua_pushlightuserdata(L, appsdir);
|
||||
lua_pushlightuserdata(L, largs);
|
||||
lua_pushboolean (L, noenv);
|
||||
lua_pushcclosure(L, register_require, 5);
|
||||
lua_createtable(L, 0, 0);
|
||||
luaL_register(L, NULL, cutillib);
|
||||
lua_call(L, 2, 0);
|
||||
|
||||
snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref);
|
||||
if (report(L, elua_loadfile(L, modfile))) {
|
||||
snprintf(modfile, sizeof(modfile), "%s/gettext.lua", coref);
|
||||
if (report(L, elua_loadfile(L, modfile)))
|
||||
{
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
lua_createtable(L, 0, 0);
|
||||
luaL_register(L, NULL, gettextlib);
|
||||
}
|
||||
lua_createtable(L, 0, 0);
|
||||
luaL_register(L, NULL, gettextlib);
|
||||
#if ENABLE_NLS
|
||||
lua_pushlightuserdata(L, *((void**)&dgettextp));
|
||||
lua_setfield(L, -2, "dgettext");
|
||||
lua_pushlightuserdata(L, *((void**)&dngettextp));
|
||||
lua_setfield(L, -2, "dngettext");
|
||||
lua_pushlightuserdata(L, *((void**)&dgettextp));
|
||||
lua_setfield(L, -2, "dgettext");
|
||||
lua_pushlightuserdata(L, *((void**)&dngettextp));
|
||||
lua_setfield(L, -2, "dngettext");
|
||||
#endif
|
||||
lua_call(L, 1, 0);
|
||||
lua_call(L, 1, 0);
|
||||
|
||||
elua_register_cache(L);
|
||||
lua_gc(L, LUA_GCRESTART, 0);
|
||||
elua_register_cache(L);
|
||||
lua_gc(L, LUA_GCRESTART, 0);
|
||||
|
||||
INF("elua lua state initialized");
|
||||
INF("elua lua state initialized");
|
||||
|
||||
/* load all the things */
|
||||
EINA_LIST_FOREACH(largs, l, data) {
|
||||
switch (data->type) {
|
||||
case ARG_CODE:
|
||||
/* load all the things */
|
||||
EINA_LIST_FOREACH(largs, l, data)
|
||||
{
|
||||
switch (data->type)
|
||||
{
|
||||
case ARG_CODE:
|
||||
if (!hasexec) hasexec = EINA_TRUE;
|
||||
if (dostr(L, data->value, "=(command line)")) {
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
if (dostr(L, data->value, "=(command line)"))
|
||||
{
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case ARG_LIBRARY:
|
||||
if (dolib(L, data->value)) {
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
case ARG_LIBRARY:
|
||||
if (dolib(L, data->value))
|
||||
{
|
||||
m->status = 1;
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
EINA_LIST_FREE(largs, data) free(data);
|
||||
/* cleanup */
|
||||
EINA_LIST_FREE(largs, data) free(data);
|
||||
|
||||
/* run script or execute sdin as file */
|
||||
if (optind < argc) {
|
||||
/* run script or execute sdin as file */
|
||||
if (optind < argc)
|
||||
{
|
||||
int quit = 0;
|
||||
if ((m->status = doscript(L, argc, argv, optind, &quit))) return 0;
|
||||
if (quit) return 0;
|
||||
} else if (!hasexec) {
|
||||
}
|
||||
else if (!hasexec)
|
||||
{
|
||||
int quit;
|
||||
if ((m->status = dofile(L, NULL))) return 0;
|
||||
quit = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
if (quit) return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ecore_main_loop_begin();
|
||||
ecore_main_loop_begin();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
struct Main_Data m;
|
||||
lua_State *L = NULL;
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct Main_Data m;
|
||||
lua_State *L = NULL;
|
||||
|
||||
#if ENABLE_NLS
|
||||
setlocale(LC_ALL, "");
|
||||
bindtextdomain(PACKAGE, LOCALE_DIR);
|
||||
bind_textdomain_codeset(PACKAGE, "UTF-8");
|
||||
textdomain(PACKAGE);
|
||||
setlocale(LC_ALL, "");
|
||||
bindtextdomain(PACKAGE, LOCALE_DIR);
|
||||
bind_textdomain_codeset(PACKAGE, "UTF-8");
|
||||
textdomain(PACKAGE);
|
||||
#endif
|
||||
|
||||
eina_init();
|
||||
eina_init();
|
||||
|
||||
if (!(el_log_domain = eina_log_domain_register("elua",
|
||||
EINA_COLOR_ORANGE))) {
|
||||
if (!(el_log_domain = eina_log_domain_register("elua", EINA_COLOR_ORANGE)))
|
||||
{
|
||||
printf("cannot set elua log domain\n");
|
||||
ERR("could not set elua log domain.");
|
||||
el_log_domain = EINA_LOG_DOMAIN_GLOBAL;
|
||||
}
|
||||
}
|
||||
|
||||
INF("elua logging initialized: %d", el_log_domain);
|
||||
INF("elua logging initialized: %d", el_log_domain);
|
||||
|
||||
if (!(L = luaL_newstate())) {
|
||||
if (!(L = luaL_newstate()))
|
||||
{
|
||||
ERR("could not initialize elua state.");
|
||||
shutdown(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
LL = L;
|
||||
LL = L;
|
||||
|
||||
INF("elua lua state created");
|
||||
INF("elua lua state created");
|
||||
|
||||
m.argc = argc;
|
||||
m.argv = argv;
|
||||
m.status = 0;
|
||||
m.argc = argc;
|
||||
m.argv = argv;
|
||||
m.status = 0;
|
||||
|
||||
shutdown(L, !!(lua_cpcall(L, lua_main, &m) || m.status));
|
||||
shutdown(L, !!(lua_cpcall(L, lua_main, &m) || m.status));
|
||||
|
||||
return 0; /* never gets here */
|
||||
return 0; /* never gets here */
|
||||
}
|
|
@ -6,22 +6,22 @@
|
|||
#endif
|
||||
|
||||
#if ENABLE_NLS
|
||||
#include <libintl.h>
|
||||
#define _(x) dgettext(PACKAGE, x)
|
||||
# include <libintl.h>
|
||||
# define _(x) dgettext(PACKAGE, x)
|
||||
#else
|
||||
#define _(x) (x)
|
||||
# define _(x) (x)
|
||||
#endif
|
||||
|
||||
#ifndef ELUA_CORE_DIR
|
||||
#define ELUA_CORE_DIR "."
|
||||
# define ELUA_CORE_DIR "."
|
||||
#endif
|
||||
|
||||
#ifndef ELUA_MODULES_DIR
|
||||
#define ELUA_MODULES_DIR "."
|
||||
# define ELUA_MODULES_DIR "."
|
||||
#endif
|
||||
|
||||
#ifndef ELUA_APPS_DIR
|
||||
#define ELUA_APPS_DIR "."
|
||||
# define ELUA_APPS_DIR "."
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
|
Loading…
Reference in New Issue