1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2009 Toni Spets
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 lua_State *lua_L = NULL;
40 void llua_load(const char *script)
44 error = luaL_dofile(lua_L, script);
46 ERR("llua_load: %s", lua_tostring(lua_L, -1));
52 llua_do_call does a flexible call to any Lua function
53 string: <function> [par1] [par2...]
54 retc: the number of return values expected
56 char *llua_do_call(const char *string, int retc)
61 char *tmp = strdup(string);
62 char *ptr = strtok(tmp, " ");
64 /* proceed only if the function name is present */
70 /* call only conky_ prefixed functions */
71 snprintf(func, 64, "conky_%s", ptr);
73 /* push the function name to stack */
74 lua_getglobal(lua_L, func);
76 /* parse all function parameters from args and push them to the stack */
77 ptr = strtok(NULL, " ");
79 lua_pushstring(lua_L, ptr);
80 ptr = strtok(NULL, " ");
86 if(lua_pcall(lua_L, argc, retc, 0) != 0) {
87 ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
95 char *llua_getstring(const char *args)
100 if(!lua_L) return NULL;
102 func = llua_do_call(args, 1);
104 if(!lua_isstring(lua_L, -1)) {
105 ERR("llua_getstring: function %s didn't return a string, result discarded", func);
107 ret = strdup((char *)lua_tostring(lua_L, -1));
115 int llua_getinteger(const char *args, int *per)
121 func = llua_do_call(args, 1);
123 if(!lua_isnumber(lua_L, -1)) {
124 ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
126 *per = lua_tointeger(lua_L, -1);