1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-20 03:51:18 +00:00
conky/src/llua.c

140 lines
3.0 KiB
C
Raw Normal View History

2009-05-15 18:08:51 +00:00
/* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2009 Toni Spets
2009-05-15 18:08:51 +00:00
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "conky.h"
#include "logging.h"
2009-05-15 18:08:51 +00:00
lua_State *lua_L = NULL;
void llua_init()
{
if(lua_L) return;
lua_L = lua_open();
luaL_openlibs(lua_L);
}
void llua_load(const char *script)
{
int error;
if(!lua_L) return;
2009-05-16 20:55:05 +00:00
error = luaL_dofile(lua_L, script);
if (error) {
2009-05-15 20:01:08 +00:00
ERR("llua_load: %s", lua_tostring(lua_L, -1));
2009-05-15 18:08:51 +00:00
lua_pop(lua_L, 1);
}
}
/*
llua_do_call does a flexible call to any Lua function
string: <function> [par1] [par2...]
retc: the number of return values expected
*/
char *llua_do_call(const char *string, int retc)
2009-05-15 18:08:51 +00:00
{
static char func[64];
int argc = 0;
2009-05-15 18:08:51 +00:00
char *tmp = strdup(string);
2009-05-15 18:08:51 +00:00
char *ptr = strtok(tmp, " ");
/* proceed only if the function name is present */
if(!ptr) {
free(tmp);
return NULL;
}
/* call only conky_ prefixed functions */
2009-05-15 18:08:51 +00:00
snprintf(func, 64, "conky_%s", ptr);
/* push the function name to stack */
2009-05-15 18:08:51 +00:00
lua_getglobal(lua_L, func);
/* parse all function parameters from args and push them to the stack */
2009-05-15 18:08:51 +00:00
ptr = strtok(NULL, " ");
while(ptr) {
lua_pushstring(lua_L, ptr);
ptr = strtok(NULL, " ");
argc++;
2009-05-15 18:08:51 +00:00
}
free(tmp);
if(lua_pcall(lua_L, argc, retc, 0) != 0) {
ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
2009-05-15 18:08:51 +00:00
lua_pop(lua_L, -1);
return NULL;
}
return func;
}
char *llua_getstring(const char *args)
{
char *func;
char *ret = NULL;
if(!lua_L) return NULL;
func = llua_do_call(args, 1);
if(func) {
2009-05-15 18:08:51 +00:00
if(!lua_isstring(lua_L, -1)) {
2009-05-15 20:01:08 +00:00
ERR("llua_getstring: function %s didn't return a string, result discarded", func);
2009-05-15 18:08:51 +00:00
} else {
ret = strdup((char *)lua_tostring(lua_L, -1));
lua_pop(lua_L, 1);
}
}
return ret;
}
2009-05-16 20:55:05 +00:00
int llua_getinteger(const char *args, int *per)
2009-05-15 18:08:51 +00:00
{
char *func;
2009-05-15 18:08:51 +00:00
if(!lua_L) return 0;
func = llua_do_call(args, 1);
if(func) {
2009-05-15 18:08:51 +00:00
if(!lua_isnumber(lua_L, -1)) {
2009-05-16 20:55:05 +00:00
ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
2009-05-15 18:08:51 +00:00
} else {
*per = lua_tointeger(lua_L, -1);
2009-05-15 18:08:51 +00:00
lua_pop(lua_L, 1);
return 1;
}
}
return 0;
}
void llua_close()
{
if(!lua_L) return;
lua_close(lua_L);
lua_L = NULL;
}