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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include <lua.h>
#include <stdlib.h>
#include <unistd.h>
#include "log.h"
#include "lua_state.h"
#include "state.h"
#include "str.h"
lua_State *L = NULL;
bool L_is_loaded = false;
static void lua_path_push(const char *new_path) {
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
const char *current_path = lua_tostring(L, -1);
if (!current_path)
current_path = "";
lua_pop(L, 1);
lua_pushfstring(L, "%s;%s", current_path, new_path);
lua_setfield(L, -2, "path");
lua_pop(L, 1);
}
#define HANDLE_LUA_ERROR \
do { \
log_error("lua: %s", lua_tostring(L, -1)); \
lua_pop(L, 1); \
return false; \
} while (0)
bool init_lua_state(void) {
if (L_is_loaded)
return true;
L = luaL_newstate();
luaL_openlibs(L);
str lua_path = str_format("%.*s/?.lua", str_fmt(&config.dir));
lua_path_push(lua_path.data);
if (luaL_loadfile(L, config.init_path.data) || lua_pcall(L, 0, 1, 0)) {
log_error("cannot run configuration script: %s", lua_tostring(L, -1));
log_pkgit("to generate a configuration file, head into the");
log_pkgit(
"root directory of the pkgit source and run `make defconfig`");
lua_close(L);
return false;
}
if (!lua_istable(L, -1)) {
log_error("top-level configuration is not a table");
lua_pop(L, 1);
lua_close(L);
return false;
}
str_free(&lua_path);
L_is_loaded = true;
return true;
}
static void setup_base_dirs(void) {
int uid = getuid(), euid = geteuid();
config.is_root = false;
if (uid < 0 || uid != euid) {
config.is_root = true;
config.dir = mstr("/etc/pkgit");
} else {
char *tmp = getenv("XDG_CONFIG_HOME");
if (tmp) {
config.dir = str_format("%s/pkgit", tmp);
} else {
config.dir = str_format("%s/.config/pkgit", getenv("HOME"));
}
}
config.init_path = str_format(
"%.*s/init.lua",
str_fmt(&config.dir)
);
config.autogenerated_path = str_format(
"%.*s/autogenerated.lua",
str_fmt(&config.dir)
);
}
#define lua_get_field_type(name, type, file) \
do { \
lua_getfield(L, -1, name); \
if (!lua_is##type(L, -1)) { \
log_error("lua: expected type " #type " for " name " in %.*s", \
str_fmt(file)); \
lua_pop(L, 1); \
return false; \
} \
} while (0)
static bool load_init_lua(void) {
lua_setglobal(L, "__PkgitUserConfig");
lua_getglobal(L, "__PkgitUserConfig");
lua_get_field_type("dirs", table, &config.init_path);
#define X(field) \
do { \
lua_get_field_type(#field, string, &config.init_path); \
config.inst_dirs.field = str_adopt((char *)lua_tostring(L, -1)); \
lua_pop(L, 1); \
} while (0)
X(prefix);
X(bin);
X(include);
X(lib);
X(src);
#undef X
return true;
}
bool init_lua_config(void) {
setup_base_dirs();
if (!init_lua_state())
return false;
if (!load_init_lua())
return false;
return true;
}
void free_lua_state(void) {
if (L != NULL) {
lua_close(L);
L = NULL;
}
L_is_loaded = false;
}
void free_lua_config(void) {
free_lua_state();
}
|