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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "options.h"
#include "bindings.h"
#include "logging.h"
#include "lua-script/script.h"
struct settings {
const char *template_dir;
};
#define CHECK_LUA_ERROR() \
do { \
if (error != 0) { \
argent_log(FATAL, "lua_error: %s", lua_tostring(L, -1)); \
lua_close(L); \
return error; \
} \
} while(0)
int hs_call(lua_State *L, int nargs, int nret);
int main(int argc, char **argv)
{
argent_log(DEBUG, "parse command-line options");
struct argent_options opts;
int error = parse_options(&opts, argc, argv);
if (error)
// error of '2' indicates -h was passed
return error == 2 ? 0 : 1;
argent_log(INFO, "log level: %s", level_string(argent_log_level));
argent_log(INFO, "configuration file: %s", opts.conf_filename);
if (opts.script_filename != NULL) {
argent_log(INFO, "script override: %s", opts.script_filename);
}
argent_log(DEBUG, "create lua state");
lua_State *L = luaL_newstate();
luaL_openlibs(L);
argent_log(DEBUG, "create argent table");
luaL_Reg tbl[] = {
{"markdown", markdown},
{"currentWorkingDirectory", current_working_directory},
{"scanDirectory", scan_directory},
{"createDirectory", create_directory},
{"copyFile", copy_file},
{"log", argent_log_lua},
{NULL, NULL},
};
luaL_register(L, "argent", tbl);
// load argent lua script
argent_log(DEBUG, "load main lua script");
if (opts.script_filename == NULL) {
error = luaL_dostring(L, argent_script);
CHECK_LUA_ERROR();
}
else {
error = luaL_dofile(L, opts.script_filename);
CHECK_LUA_ERROR();
if (!lua_isfunction(L, -1)) {
argent_log(FATAL, "override script '%s' did not return a function!", opts.script_filename);
return 1;
}
}
// load configuration file
argent_log(DEBUG, "load configuration file");
error = luaL_loadfile(L, opts.conf_filename);
CHECK_LUA_ERROR();
// push config table to stack
argent_log(DEBUG, "run configuration file");
error = hs_call(L, 0, 1);
CHECK_LUA_ERROR();
// run argent script
argent_log(DEBUG, "run main lua script");
error = hs_call(L, 1, 0);
CHECK_LUA_ERROR();
argent_log(DEBUG, "close lua state");
lua_close(L);
return 0;
}
int hs_traceback(lua_State *L)
{
if (!lua_isstring(L, 1))
/* 'message' is not a string, keep intact */
return 1;
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_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
int hs_call(lua_State *L, int nargs, int nret)
{
int traceback_pos = lua_gettop(L) - nargs;
lua_pushcfunction(L, hs_traceback);
lua_insert(L, traceback_pos);
int result = lua_pcall(L, nargs, nret, traceback_pos);
lua_remove(L, traceback_pos);
return result;
}
|