Couple minor lua changes.
[monky] / src / llua.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2009 Toni Spets
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
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.
18  *
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/>.
25  *
26  */
27
28 #include "conky.h"
29 #include "logging.h"
30
31 lua_State *lua_L = NULL;
32
33 void llua_init()
34 {
35         if(lua_L) return;
36         lua_L = lua_open();
37         luaL_openlibs(lua_L);
38 }
39
40 void llua_load(const char *script)
41 {
42         int error;
43         if(!lua_L) return;
44         error = luaL_dofile(lua_L, script);
45         if (error) {
46                 ERR("llua_load: %s", lua_tostring(lua_L, -1));
47                 lua_pop(lua_L, 1);
48         }
49 }
50
51 /*
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
55 */
56 char *llua_do_call(const char *string, int retc)
57 {
58         static char func[64];
59         int argc = 0;
60
61         char *tmp = strdup(string);
62         char *ptr = strtok(tmp, " ");
63
64         /* proceed only if the function name is present */
65         if(!ptr) {
66                 free(tmp);
67                 return NULL;
68         }
69
70         /* call only conky_ prefixed functions */
71         snprintf(func, 64, "conky_%s", ptr);
72
73         /* push the function name to stack */
74         lua_getglobal(lua_L, func);
75
76         /* parse all function parameters from args and push them to the stack */
77         ptr = strtok(NULL, " ");
78         while(ptr) {
79                 lua_pushstring(lua_L, ptr);
80                 ptr = strtok(NULL, " ");
81                 argc++;
82         }
83
84         free(tmp);
85
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));
88                 lua_pop(lua_L, -1);
89                 return NULL;
90         }
91
92         return func;
93 }
94
95 char *llua_getstring(const char *args)
96 {
97         char *func;
98         char *ret = NULL;
99
100         if(!lua_L) return NULL;
101
102         func = llua_do_call(args, 1);
103         if(func) {
104                 if(!lua_isstring(lua_L, -1)) {
105                         ERR("llua_getstring: function %s didn't return a string, result discarded", func);
106                 } else {
107                         ret = strdup((char *)lua_tostring(lua_L, -1));
108                         lua_pop(lua_L, 1);
109                 }
110         }
111
112         return ret;
113 }
114
115 int llua_getinteger(const char *args, int *per)
116 {
117         char *func;
118
119         if(!lua_L) return 0;
120
121         func = llua_do_call(args, 1);
122         if(func) {
123                 if(!lua_isnumber(lua_L, -1)) {
124                         ERR("llua_getinteger: function %s didn't return an integer, result discarded", func);
125                 } else {
126                         *per = lua_tointeger(lua_L, -1);
127                         lua_pop(lua_L, 1);
128                         return 1;
129                 }
130         }
131         return 0;
132 }
133
134 void llua_close()
135 {
136         if(!lua_L) return;
137         lua_close(lua_L);
138         lua_L = NULL;
139 }