Fix old copyright year
[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         error = luaL_loadfile(lua_L, script);
44         if(error) {
45                 ERR("llua_load: %s\n", lua_tostring(lua_L, -1));
46                 lua_pop(lua_L, 1);
47         } else {
48                 lua_pcall(lua_L, 0, 0, 0);
49         }
50 }
51
52 char *llua_getstring(const char *args)
53 {
54         char *ret = NULL;
55         char *tmp = strdup(args);
56         char func[64];
57         int parcount = 0;
58
59         if(!lua_L) return NULL;
60
61         char *ptr = strtok(tmp, " ");
62         if(!ptr) return NULL; /* function name missing */
63         snprintf(func, 64, "conky_%s", ptr);
64
65         lua_getglobal(lua_L, func);
66
67         ptr = strtok(NULL, " ");
68         while(ptr) {
69                 lua_pushstring(lua_L, ptr);
70                 ptr = strtok(NULL, " ");
71                 parcount++;
72         }
73
74         if(lua_pcall(lua_L, parcount, 1, 0) != 0) {
75                 ERR("llua_getstring: function %s execution failed: %s\n", func, lua_tostring(lua_L, -1));
76                 lua_pop(lua_L, -1);
77         } else {
78                 if(!lua_isstring(lua_L, -1)) {
79                         ERR("llua_getstring: function %s didn't return a string, result discarded\n", func);
80                 } else {
81                         ret = strdup((char *)lua_tostring(lua_L, -1));
82                         lua_pop(lua_L, 1);
83                 }
84         }
85
86         free(tmp);
87
88         return ret;
89 }
90
91 int llua_getpercent(const char *args, int *per)
92 {
93         char func[64];
94         char *tmp = strdup(args);
95         int parcount = 0;
96
97         if(!lua_L) return 0;
98
99         char *ptr = strtok(tmp, " ");
100         if(!ptr) return 0; /* function name missing */
101         snprintf(func, 64, "conky_%s", ptr);
102
103         lua_getglobal(lua_L, func);
104
105         ptr = strtok(NULL, " ");
106         while(ptr) {
107                 lua_pushstring(lua_L, ptr);
108                 ptr = strtok(NULL, " ");
109                 parcount++;
110         }
111         free(tmp);
112
113         if(lua_pcall(lua_L, parcount, 1, 0) != 0) {
114                 ERR("llua_getpercent: function %s execution failed: %s\n", func, lua_tostring(lua_L, -1));
115                 lua_pop(lua_L, -1);
116         } else {
117                 if(!lua_isnumber(lua_L, -1)) {
118                         ERR("llua_getpercent: function %s didn't return a number (percent), result discarded\n", func);
119                 } else {
120                         *per = lua_tonumber(lua_L, -1);
121                         lua_pop(lua_L, 1);
122                         return 1;
123                 }
124         }
125         return 0;
126 }
127
128 void llua_close()
129 {
130         if(!lua_L) return;
131         lua_close(lua_L);
132         lua_L = NULL;
133 }