1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "options/options.h"
#include "logging/logging.h"
#include "util/util.h"
#include "common.h"
#ifdef _WIN32
# include <direct.h>
# define chdir _chdir
#else
# include <unistd.h>
#endif
#include <errno.h>
void print_load_error(lua_State *L, const char *script_file, int error_type);
void setup_version(lua_State *L, int honey_tbl);
int main(int argc, char **argv)
{
/* parse command-line options */
struct honey_options options;
int result = parse_options(&options, argc, argv);
if (result == EXIT_FAILURE) return 1;
else if (result == EXIT_SUCCESS) return 0;
honey_set_log_level(options.log_level);
/* configure working directory */
if (options.working_dir != NULL) {
chdir(options.working_dir);
if (errno == ENOENT) {
honey_log(HONEY_FATAL, "[FATAL] could not find directory \"%s\"\n", options.working_dir);
return 1;
}
}
/* set up lua state */
lua_State *L = luaL_newstate();
luaL_openlibs(L);
/* load honey bindings */
lua_createtable(L, 0, 2);
int honey_index = lua_gettop(L);
#define X(module) setup_ ## module (L, honey_index);
HONEY_MODULES
#undef X
lua_setglobal(L, "honey");
/* load main script */
int err = luaL_loadfile(L, options.script_file);
if (err != 0) {
print_load_error(L, options.script_file, err);
lua_close(L);
return 0;
}
/* run */
err = h_call(L, 0, 0);
if (err != 0) {
const char *err_str = lua_tostring(L, -1);
printf("failed to run: \n%s\n", err_str);
}
/* clean up */
lua_close(L);
return 0;
}
void print_load_error(lua_State *L, const char *script_file, int error_type)
{
switch(error_type) {
case LUA_ERRFILE:
printf("error: cannot open file '%s'\n", script_file);
break;
case LUA_ERRSYNTAX:
printf("error: failed to compile file: %s\n", lua_tostring(L, -1));
break;
case LUA_ERRMEM:
printf("error: memory error: %s\n", lua_tostring(L, -1));
break;
default:
printf("error: an unknown error occured when trying to load file '%s'.\n", script_file);
break;
};
}
void setup_version(lua_State *L, int honey_tbl)
{
struct honey_tbl_t tbl[] = {
H_INT("major", HONEY_VERSION_MAJOR),
H_INT("minor", HONEY_VERSION_MINOR),
H_INT("patch", HONEY_VERSION_PATCH),
H_STR("string", HONEY_VERSION_STR),
H_END
};
create_table(L, tbl);
lua_setfield(L, honey_tbl, "version");
}
|