This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathPluginAPI.c
More file actions
77 lines (60 loc) · 2.18 KB
/
PluginAPI.c
File metadata and controls
77 lines (60 loc) · 2.18 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
#include "config.h"
#include "PluginAPI.h"
#include "Process.h"
#ifdef HAVE_LUA
/*{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define check_field(_name, _type, _err) \
do { \
int typ = lua_getfield(L, 1, _name); \
if (typ != _type) { \
lua_settop(L, 0); \
lua_pushnil(L); \
lua_pushliteral(L, _err); \
return 2; \
} \
lua_pop(L, 1); \
} while(0)
#define assign_string(_var, _idx, _field) \
do { \
lua_getfield(L, _idx, _field); \
_var = xStrdup(lua_tostring(L, -1)); \
lua_pop(L, 1); \
} while(0)
}*/
int PluginAPI_newColumn(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
check_field("name", LUA_TSTRING, "expected 'name' field of type string");
check_field("description", LUA_TSTRING, "expected 'description' field of type string");
check_field("header", LUA_TSTRING, "expected 'header' field of type string");
check_field("format", LUA_TSTRING, "expected 'format' field of type string");
check_field("run", LUA_TFUNCTION, "expected 'run' field of type function");
lua_getfield(L, LUA_REGISTRYINDEX, "htop_columns");
lua_len(L, -1);
int len = lua_tonumber(L, -1);
lua_pop(L, 1);
ProcessFieldData* pfd = lua_newuserdata(L, sizeof(ProcessFieldData));
assign_string(pfd->name, 1, "name");
assign_string(pfd->description, 1, "description");
assign_string(pfd->title, 1, "header");
assign_string(pfd->format, 1, "format");
lua_seti(L, -2, len + 1);
lua_settop(L, 0);
lua_pushboolean(L, 1);
return 1;
}
static luaL_Reg PluginAPI_functions[] = {
{ .name = "new_column", .func = PluginAPI_newColumn },
{ .name = NULL, .func = NULL },
};
int PluginAPI_new(lua_State* L) {
lua_settop(L, 0);
lua_newtable(L);
lua_setfield(L, LUA_REGISTRYINDEX, "htop_columns");
lua_newtable(L);
luaL_setfuncs(L, PluginAPI_functions, 0);
return 1;
}
#endif