-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_lua.c
More file actions
277 lines (233 loc) · 7.67 KB
/
splinter_cli_cmd_lua.c
File metadata and controls
277 lines (233 loc) · 7.67 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* @file splinter_cli_cmd_label.c
* @brief Implements the CLI 'label' command to tag keys via Bloom filter.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <lua5.4/lua.h>
#include <lua5.4/lualib.h>
#include <lua5.4/lauxlib.h>
#include "splinter_cli.h"
/**
* @brief Register a Lua script's interest in a Signal Group.
*/
static int lua_splinter_watch(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint8_t group_id = (uint8_t)luaL_checkinteger(L, 2);
if (splinter_watch_register(key, group_id) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Unregister from a Signal Group.
*/
static int lua_splinter_unwatch(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint8_t group_id = (uint8_t)luaL_checkinteger(L, 2);
if (splinter_watch_unregister(key, group_id) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Batch-retrieves a tandem set (key, key.1, key.2, etc) into a Lua table.
*/
static int lua_splinter_get_tandem(lua_State *L) {
const char *base_key = luaL_checkstring(L, 1);
int max_orders = (int)luaL_optinteger(L, 2, 64); // Safety cap
char tandem_name[SPLINTER_KEY_MAX];
char buf[4096]; // Use a larger buffer or H->max_val_sz if accessible
size_t received = 0;
lua_newtable(L);
for (int i = 0; i < max_orders; i++) {
if (i == 0) {
strncpy(tandem_name, base_key, SPLINTER_KEY_MAX - 1);
} else {
snprintf(tandem_name, sizeof(tandem_name), "%s.%d", base_key, i);
}
if (splinter_get(tandem_name, buf, sizeof(buf), &received) == 0) {
// Push the index (1-based for Lua) and the value
lua_pushinteger(L, i + 1);
lua_pushlstring(L, buf, received);
lua_settable(L, -3);
} else {
// Stop at the first missing order
break;
}
}
return 1;
}
/**
* @brief Batch-pushes a Lua table of values as a tandem set.
* Expects a table where index 1 is the base key, index 2 is order 1, etc.
*/
static int lua_splinter_set_tandem(lua_State *L) {
const char *base_key = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
char tandem_name[SPLINTER_KEY_MAX];
int n = (int)lua_rawlen(L, 2);
for (int i = 1; i <= n; i++) {
lua_rawgeti(L, 2, i);
size_t len;
const char *val = lua_tolstring(L, -1, &len);
if (i == 1) {
strncpy(tandem_name, base_key, SPLINTER_KEY_MAX - 1);
} else {
snprintf(tandem_name, sizeof(tandem_name), "%s.%d", base_key, i - 1);
}
if (val && splinter_set(tandem_name, val, len) != 0) {
lua_pushboolean(L, 0);
return 1;
}
lua_pop(L, 1);
}
lua_pushboolean(L, 1);
return 1;
}
static int lua_splinter_math(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
const char *op_str = luaL_checkstring(L, 2);
uint64_t val = (uint64_t)luaL_optinteger(L, 3, 0);
splinter_integer_op_t op;
if (strcasecmp(op_str, "inc") == 0) op = SPL_OP_INC;
else if (strcasecmp(op_str, "dec") == 0) op = SPL_OP_DEC;
else if (strcasecmp(op_str, "and") == 0) op = SPL_OP_AND;
else if (strcasecmp(op_str, "or") == 0) op = SPL_OP_OR;
else if (strcasecmp(op_str, "xor") == 0) op = SPL_OP_XOR;
else if (strcasecmp(op_str, "not") == 0) op = SPL_OP_NOT;
else return luaL_error(L, "invalid math operation: %s", op_str);
if (splinter_integer_op(key, op, &val) == 0) {
lua_pushboolean(L, 1);
return 1;
}
if (errno == EPROTOTYPE) {
return luaL_error(L, "key '%s' is not a BIGUINT slot", key);
}
lua_pushboolean(L, 0);
return 1;
}
static int lua_splinter_get(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
char buf[SPLINTER_KEY_MAX + 8]; // Support key + alignment pad
size_t received = 0;
splinter_slot_snapshot_t snap = {0};
// 1. Fetch data from SHM
if (splinter_get(key, buf, sizeof(buf), &received) != 0) {
lua_pushnil(L);
return 1;
}
// 2. Determine semantic type via snapshot
if (splinter_get_slot_snapshot(key, &snap) == 0) {
if (snap.type_flag & SPL_SLOT_TYPE_BIGUINT) {
// Return as a Lua integer for math
uint64_t val = *(uint64_t *)buf;
lua_pushinteger(L, (lua_Integer)val);
return 1;
}
}
// 3. Fallback to string (VARTEXT/JSON/VOID)
lua_pushlstring(L, buf, received);
return 1;
}
static int lua_splinter_set(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
int type = lua_type(L, 2);
if (type == LUA_TNUMBER) {
// 1. Auto-promote to BIGUINT and expand if needed
uint64_t val = (uint64_t)lua_tointeger(L, 2);
// Prepare the slot for 8-byte integer operations
if (splinter_set_named_type(key, SPL_SLOT_TYPE_BIGUINT) != 0) {
// If key doesn't exist, we create it first as VOID then upgrade
// This maintains the "silent" experience
splinter_set(key, &val, 8);
splinter_set_named_type(key, SPL_SLOT_TYPE_BIGUINT);
} else {
// Key existed, was expanded, now store the integer
splinter_set(key, &val, 8);
}
} else {
// 2. Standard string/binary set
size_t len;
const char *val = luaL_checklstring(L, 2, &len);
if (splinter_set(key, val, len) != 0) {
lua_pushboolean(L, 0);
return 1;
}
}
lua_pushboolean(L, 1);
return 1;
}
static int lua_splinter_unset(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
int deleted_len = splinter_unset(key);
if (deleted_len >= 0) {
lua_pushinteger(L, deleted_len);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
static int lua_splinter_label(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint64_t mask = 0;
if (lua_isnumber(L, 2)) {
mask = (uint64_t)lua_tointeger(L, 2);
} else {
// Here you could link into your label resolution logic if desired
return luaL_error(L, "Label must be a numeric mask");
}
if (splinter_set_label(key, mask) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
static const char *modname = "lua";
void help_cmd_lua(unsigned int level) {
(void) level;
printf("Usage: %s <script.lua>\n", modname);
puts("");
}
// This may end up being shared across modules
// Hence, not static. But not prototyped publicly either, yet.
int luaopen_splinter(lua_State *L) {
static const struct luaL_Reg bus_funcs[] = {
{"get", lua_splinter_get},
{"get_tandem", lua_splinter_get_tandem},
{"set", lua_splinter_set},
{"set_tandem", lua_splinter_set_tandem},
{"math", lua_splinter_math},
{"watch", lua_splinter_watch},
{"unwatch", lua_splinter_unwatch},
{"label", lua_splinter_label},
{"unset", lua_splinter_unset},
{NULL, NULL}
};
luaL_newlib(L, bus_funcs);
return 1;
}
int cmd_lua(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: lua <script.lua>\n");
return 1;
}
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_requiref(L, "splinter", luaopen_splinter, 1);
lua_pop(L, 1);
if (luaL_dofile(L, argv[1]) != LUA_OK) {
fprintf(stderr, "Lua Error: %s\n", lua_tostring(L, -1));
lua_close(L);
return 1;
}
lua_close(L);
return 0;
}