-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlua_dtree.c
156 lines (124 loc) · 2.57 KB
/
lua_dtree.c
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
/**
* lua_dtree.c
* Copyright (C) 2012 Jan Viktorin
*/
#include "dtree.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
/**
* Check access to the library.
* Can not call open nor close twice.
*/
enum l_dtree_state {
L_DTREE_OPEN,
L_DTREE_CLOSE
};
/**
* Current state of the dtree library.
*/
static
enum l_dtree_state state = L_DTREE_CLOSE;
//
// Open & close for Lua
//
static
int l_dtree_open(lua_State *l)
{
if(state == L_DTREE_OPEN)
luaL_error(l, "The dtree.open() has already been called without a dtree.close()");
const int argc = lua_gettop(l);
const char *path = NULL;
switch(argc) {
case 0:
path = "/proc/device-tree";
break;
case 1:
path = luaL_checkstring(l, 1);
break;
default:
luaL_error(l, "Too many arguments (%d)", argc);
break;
}
if(dtree_open(path))
luaL_error(l, dtree_errstr());
state = L_DTREE_OPEN;
return 0;
}
static
int l_dtree_close(lua_State *l)
{
if(state == L_DTREE_CLOSE)
luaL_error(l, "Invalid call to dtree.close(), call dtree.open() first");
dtree_close();
state = L_DTREE_CLOSE;
return 0;
}
static
int l_dtree_next(lua_State *l) {
if(state == L_DTREE_CLOSE)
luaL_error(l, "Invalid call to dtree.next(), call dtree.open() first");
struct dtree_dev_t *dev = dtree_next();
if(dev == NULL) {
if(dtree_iserror())
luaL_error(l, dtree_errstr());
lua_pushboolean(l, 0);
return 1;
}
lua_createtable(l, 0, 4);
lua_pushstring(l, "name");
lua_pushstring(l, dtree_dev_name(dev));
lua_rawset(l, -3);
lua_pushstring(l, "base");
lua_pushinteger(l, dtree_dev_base(dev));
lua_rawset(l, -3);
lua_pushstring(l, "high");
lua_pushinteger(l, dtree_dev_high(dev));
lua_rawset(l, -3);
lua_pushstring(l, "compat");
lua_newtable(l);
const char **compat = dtree_dev_compat(dev);
int i;
for(i = 0; compat[i] != NULL; ++i) {
lua_pushstring(l, compat[i]);
lua_rawseti(l, -2, i);
}
lua_rawset(l, -3);
dtree_dev_free(dev);
return 1;
}
//
// Register in Lua
//
static const
struct luaL_reg dtreelib[] =
{
{"open", l_dtree_open},
{"close", l_dtree_close},
{"next", l_dtree_next},
{NULL, NULL}
};
int luaopen_dtreelib(lua_State *l)
{
if(sizeof(lua_Integer) < sizeof(dtree_addr_t)) {
luaL_error(l,
"Too short lua_Integer (%d B) data type, required at least %d B",
sizeof(lua_Integer), sizeof(dtree_addr_t));
return 0;
}
luaL_openlib(l, "dtree", dtreelib, 0);
return 1;
}
#ifdef TEST
#include <stdio.h>
int main(void)
{
lua_State *l;
l = luaL_newstate();
luaL_openlibs(l);
luaopen_dtreelib(l);
printf("result: %d\n", luaL_dofile(l, "test.lua"));
lua_close(l);
return 0;
}
#endif