aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib/ctl-lua-utils.c
blob: da22b7ae91d1b7ccfac7bc970a22fd36b2213bde (plain)
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
/*
 * 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\
";
an>1; then if [ $OS == "cygwin" ]; then echo "OPTIONAL: Install the \"ncurses\" package in Cygwin to get colored shell output" fi else txtrst=$(tput sgr0) # reset bldred=${txtbld}$(tput setaf 1) bldgreen=${txtbld}$(tput setaf 2) fi _pushd() { pushd $1 > /dev/null } _popd() { popd > /dev/null } _wait() { if [ -z $CI ]; then echo "Press Enter when done" read fi } _install() { if [ $OS == "cygwin" ]; then _cygwin_error $1 elif [ $OS == "mac" ]; then # brew exists with 1 if it's already installed set +e brew install $1 set -e else if [ -z $DISTRO ]; then echo echo "Missing $1 - install it using your distro's package manager or build from source" _wait else if [ $DISTRO == "arch" ]; then $SUDO_CMD pacman -S $1 elif [ $DISTRO == "Ubuntu" ]; then $SUDO_CMD apt-get update -qq $SUDO_CMD apt-get install $1 -y else echo echo "Missing $1 - install it using your distro's package manager or build from source" _wait fi fi fi } CYGWIN_PACKAGES="make curl, libsasl2, ca-certificates, ncurses, python-setuptools" download() { url=$1 filename=$2 curl $url -L --O $filename } if [ `id -u` == 0 ]; then die "Error: running as root - don't use 'sudo' with this script" fi if ! command -v curl >/dev/null 2>&1; then if [ $OS == "cygwin" ]; then _cygwin_error "curl" else _install curl fi fi if [ $OS == "windows" ]; then die "Sorry, the bootstrap script for compiling from source doesn't support the Windows console - try Cygwin." fi if [ $OS == "mac" ] && ! command -v brew >/dev/null 2>&1; then echo "Installing Homebrew..." ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)" fi if ! command -v make >/dev/null 2>&1; then if [ $OS == "cygwin" ]; then _cygwin_error "make" elif [ $OS == "mac" ]; then die "Missing 'make' - install the Xcode CLI tools" else if [ $DISTRO == "arch" ]; then $SUDO_CMD pacman -S base-devel elif [ $DISTRO == "Ubuntu" ]; then $SUDO_CMD apt-get update -qq $SUDO_CMD apt-get install build-essential -y fi fi fi if ! command -v python >/dev/null 2>&1; then echo "Installing Python..." _install "python" fi if ! command -v pip >/dev/null 2>&1; then echo "Installing Pip..." if ! command -v easy_install >/dev/null 2>&1; then die "easy_install not available, can't install pip" fi $SUDO_CMD easy_install pip fi $SUDO_CMD pip install -U pip $SUDO_CMD pip install --pre -Ur pip-requirements.txt if ! command -v protoc >/dev/null 2>&1; then if [ $OS == "cygwin" ]; then _cygwin_error "protobuf" elif [ $OS == "mac" ] || [ $OS == "linux" ]; then if [ $DISTRO == "Ubuntu" ]; then _install protobuf-compiler else _install protobuf fi fi fi popd echo echo "${bldgreen}All developer dependencies installed, ready to compile.$txtrst"