summaryrefslogtreecommitdiffstats
path: root/ctl-lib/ctl-lua-utils.c
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-05-06 14:24:42 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-05-09 15:03:13 +0200
commit2fa16f981f6e5d86ac5938821e0c533786b60fc7 (patch)
treeab073906b708cfb2fd7811de5e440e27515087da /ctl-lib/ctl-lua-utils.c
parent59bbc828469169b518eea036f63f60b5ac8f6264 (diff)
Upgrade config schema
Change the way to load LUA scripts. They are now considerate as Plugin and loads with them. This imply rework of how to search and find plugins as well as the way to load LUA. Also load an harcoded LUA scripts providing LUA helpers and managing global variables lock unlock mechanism Change-Id: I64e38aa27278d0cfdca787155db2d0c89953f905 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib/ctl-lua-utils.c')
-rw-r--r--ctl-lib/ctl-lua-utils.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/ctl-lib/ctl-lua-utils.c b/ctl-lib/ctl-lua-utils.c
new file mode 100644
index 0000000..da22b7a
--- /dev/null
+++ b/ctl-lib/ctl-lua-utils.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2016 \"IoT.bzh\"
+ * Author Fulup Ar Foll <fulup@iot.bzh>
+ *
+ Licensed under the Apache License, Version 2.0 (the \"License\");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an \"AS IS\" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ref:
+ * (manual) https://www.lua.org/manual/5.3/manual.html
+ * (lua->C) http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm#_Anatomy_of_a_Lua_Call
+ * (lua/C Var) http://acamara.es/blog/2012/08/passing-variables-from-lua-5-2-to-c-and-vice-versa/
+ * (Lua/C Lib)https://john.nachtimwald.com/2014/07/12/wrapping-a-c-library-in-lua/
+ * (Lua/C Table) https://gist.github.com/SONIC3D/10388137
+ * (Lua/C Nested table) https://stackoverflow.com/questions/45699144/lua-nested-table-from-lua-to-c
+ * (Lua/C Wrapper) https://stackoverflow.com/questions/45699950/lua-passing-handle-to-function-created-with-newlib
+ *
+ */
+
+const char *lua_utils = "--===================================================\n\
+--= Niklas Frykholm\n\
+-- basically if user tries to create global variable\n\
+-- the system will not let them!!\n\
+-- call GLOBAL_lock(_G)\n\
+-- \n\
+--===================================================\n\
+function GLOBAL_lock(t)\n\
+ local mt = getmetatable(t) or {}\n\
+ mt.__newindex = lock_new_index\n\
+ setmetatable(t, mt)\n\
+end\n\
+\n\
+--===================================================\n\
+-- call GLOBAL_unlock(_G)\n\
+-- to change things back to normal.\n\
+--===================================================\n\
+function GLOBAL_unlock(t)\n\
+ local mt = getmetatable(t) or {}\n\
+ mt.__newindex = unlock_new_index\n\
+ setmetatable(t, mt)\n\
+end\n\
+\n\
+function lock_new_index(t, k, v)\n\
+ if (string.sub(k,1,1) ~= \"_\") then\n\
+ GLOBAL_unlock(_G)\n\
+ error(\"GLOBALS are locked -- \" .. k ..\n\
+ \" must be declared local or prefix with '_' for globals.\", 2)\n\
+ else\n\
+ rawset(t, k, v)\n\
+ end\n\
+end\n\
+\n\
+function unlock_new_index(t, k, v)\n\
+ rawset(t, k, v)\n\
+end\n\
+\n\
+-- return serialised version of printable table\n\
+function Dump_Table(o)\n\
+ if type(o) == 'table' then\n\
+ local s = '{ '\n\
+ for k,v in pairs(o) do\n\
+ if type(k) ~= 'number' then k = '\"'..k..'\"' end\n\
+ s = s .. '['..k..'] = ' .. Dump_Table(v) .. ','\
+ end\n\
+ return s .. '} '\n\
+ else\n\
+ return tostring(o)\n\
+ end\n\
+end\n\
+\n\
+-- simulate C prinf function\n\
+printf = function(s,...)\n\
+ io.write(s:format(...))\n\
+ io.write(\"\")\n\
+ return\n\
+end\n\
+\n\
+-- lock global variable\n\
+GLOBAL_lock(_G)\n\
+";