Ignore spaces in {} when breaking up arguments to $lua
[monky] / src / llua.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Copyright (c) 2009 Toni Spets
7  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
8  *      (see AUTHORS)
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "conky.h"
26 #include "logging.h"
27 #include "build.h"
28
29 #ifdef LUA_EXTRAS
30 #include <tolua++.h>
31 #endif /* LUA_EXTRAS */
32
33 #ifdef HAVE_SYS_INOTIFY_H
34 #include <sys/inotify.h>
35
36 void llua_append_notify(const char *name);
37 void llua_rm_notifies(void);
38 static int llua_block_notify = 0;
39 #endif /* HAVE_SYS_INOTIFY_H */
40
41 #define MIN(a, b) ( (a) < (b) ? (a) : (b) )
42
43 static char *draw_pre_hook = 0;
44 static char *draw_post_hook = 0;
45 static char *startup_hook = 0;
46 static char *shutdown_hook = 0;
47
48 lua_State *lua_L = NULL;
49
50 static int llua_conky_parse(lua_State *L)
51 {
52         int n = lua_gettop(L);    /* number of arguments */
53         char *str;
54         char *buf = calloc(1, max_user_text);
55         if (n != 1) {
56                 lua_pushstring(L, "incorrect arguments, conky_parse(string) takes exactly 1 argument");
57                 lua_error(L);
58         }
59         if (!lua_isstring(L, 1)) {
60                 lua_pushstring(L, "incorrect argument (expecting a string)");
61                 lua_error(L);
62         }
63         str = strdup(lua_tostring(L, 1));
64         evaluate(str, buf, max_user_text);
65         lua_pushstring(L, buf);
66         free(str);
67         free(buf);
68         return 1;                 /* number of results */
69 }
70
71 static int llua_conky_set_update_interval(lua_State *L)
72 {
73         int n = lua_gettop(L);    /* number of arguments */
74         double value;
75         if (n != 1) {
76                 lua_pushstring(L, "incorrect arguments, conky_set_update_interval(number) takes exactly 1 argument");
77                 lua_error(L);
78         }
79         if (!lua_isnumber(L, 1)) {
80                 lua_pushstring(L, "incorrect argument (expecting a number)");
81                 lua_error(L);
82         }
83         value = lua_tonumber(L, 1);
84         set_update_interval(value);
85         return 0;                 /* number of results */
86 }
87
88 void llua_init(void)
89 {
90         const char *libs = PACKAGE_LIBDIR"/lib?.so;";
91         char *old_path, *new_path;
92         if (lua_L) return;
93         lua_L = lua_open();
94
95         /* add our library path to the lua package.cpath global var */
96         luaL_openlibs(lua_L);
97         lua_getglobal(lua_L, "package");
98         lua_getfield(lua_L, -1, "cpath");
99         old_path = strdup(lua_tostring(lua_L, -1));
100         new_path = malloc(strlen(old_path) + strlen(libs) + 1);
101         strcpy(new_path, libs);
102         strcat(new_path, old_path);
103         lua_pushstring(lua_L, new_path);
104         lua_setfield(lua_L, -3, "cpath");
105         lua_pop(lua_L, 2);
106         free(old_path);
107         free(new_path);
108
109         lua_pushstring(lua_L, PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH);
110         lua_setglobal(lua_L, "conky_build_info");
111
112         lua_pushstring(lua_L, VERSION);
113         lua_setglobal(lua_L, "conky_version");
114
115         lua_pushstring(lua_L, BUILD_DATE);
116         lua_setglobal(lua_L, "conky_build_date");
117
118         lua_pushstring(lua_L, BUILD_ARCH);
119         lua_setglobal(lua_L, "conky_build_arch");
120
121         lua_pushstring(lua_L, current_config);
122         lua_setglobal(lua_L, "conky_config");
123
124         lua_pushcfunction(lua_L, &llua_conky_parse);
125         lua_setglobal(lua_L, "conky_parse");
126
127         lua_pushcfunction(lua_L, &llua_conky_set_update_interval);
128         lua_setglobal(lua_L, "conky_set_update_interval");
129
130 #if defined(X11) && defined(LUA_EXTRAS)
131         /* register tolua++ user types */
132         tolua_open(lua_L);
133         tolua_usertype(lua_L, "Drawable");
134         tolua_usertype(lua_L, "Visual");
135         tolua_usertype(lua_L, "Display");
136 #endif /* X11 */
137 }
138
139 void llua_load(const char *script)
140 {
141         int error;
142         char path[DEFAULT_TEXT_BUFFER_SIZE];
143
144         llua_init();
145
146         to_real_path(path, script);
147         error = luaL_dofile(lua_L, path);
148         if (error) {
149                 NORM_ERR("llua_load: %s", lua_tostring(lua_L, -1));
150                 lua_pop(lua_L, 1);
151 #ifdef HAVE_SYS_INOTIFY_H
152         } else if (!llua_block_notify && inotify_fd != -1) {
153                 llua_append_notify(path);
154 #endif /* HAVE_SYS_INOTIFY_H */
155         }
156 }
157
158 /*
159  * Returns the first space-delimited token of the string starting at position *len.
160  * On return *len contains the length of the token. Spaces inside brackets are ignored, so that
161  * eg. '${foo bar}' is treated as a single token. Sets *len to zero and *str points to the end of
162  * the string when there are no more tokens.
163  */
164 static const char* tokenize(const char *str, size_t *len)
165 {
166         str += *len;
167         *len = 0;
168         while(str && isspace(*str))
169                 ++str;
170
171         size_t level = 0;
172         while(str[*len] && (level > 0 || !isspace(str[*len]))) {
173                 switch(str[*len]) {
174                         case '{': ++level; break;
175                         case '}': --level; break;
176                 }
177                 ++*len;
178         }
179
180         if(!str[*len] && level > 0)
181                 NORM_ERR("tokenize: improperly nested token: %s", str);
182
183         return str;
184 }
185
186 /*
187    llua_do_call does a flexible call to any Lua function
188 string: <function> [par1] [par2...]
189 retc: the number of return values expected
190  */
191 static char *llua_do_call(const char *string, int retc)
192 {
193         static char func[64];
194         int argc = 0;
195
196         size_t len = 0;
197
198         const char *ptr = tokenize(string, &len);
199
200         /* proceed only if the function name is present */
201         if (!len) {
202                 return NULL;
203         }
204
205         /* call only conky_ prefixed functions */
206         if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) != 0) {
207                 snprintf(func, sizeof func, "%s", LUAPREFIX);
208         } else
209                 *func = 0;
210         strncat(func, ptr, MIN(len, sizeof(func) - strlen(func) - 1));
211
212         /* push the function name to stack */
213         lua_getglobal(lua_L, func);
214
215         /* parse all function parameters from args and push them to the stack */
216         while( ptr = tokenize(ptr, &len), len) {
217                 lua_pushlstring(lua_L, ptr, len);
218                 argc++;
219         }
220
221         if(lua_pcall(lua_L, argc, retc, 0) != 0) {
222                 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
223                 lua_pop(lua_L, -1);
224                 return NULL;
225         }
226
227         return func;
228 }
229
230 #if 0
231 /*
232  * same as llua_do_call() except passes everything after func as one arg.
233  */
234 static char *llua_do_read_call(const char *function, const char *arg, int retc)
235 {
236         static char func[64];
237         snprintf(func, 64, "conky_%s", function);
238
239         /* push the function name to stack */
240         lua_getglobal(lua_L, func);
241
242         /* push function parameter to the stack */
243         lua_pushstring(lua_L, arg);
244
245         if (lua_pcall(lua_L, 1, retc, 0) != 0) {
246                 NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
247                 lua_pop(lua_L, -1);
248                 return NULL;
249         }
250
251         return func;
252 }
253 #endif
254
255 /* call a function with args, and return a string from it (must be free'd) */
256 static char *llua_getstring(const char *args)
257 {
258         char *func;
259         char *ret = NULL;
260
261         if(!lua_L) return NULL;
262
263         func = llua_do_call(args, 1);
264         if (func) {
265                 if (!lua_isstring(lua_L, -1)) {
266                         NORM_ERR("llua_getstring: function %s didn't return a string, result discarded", func);
267                 } else {
268                         ret = strdup(lua_tostring(lua_L, -1));
269                         lua_pop(lua_L, 1);
270                 }
271         }
272
273         return ret;
274 }
275
276 #if 0
277 /* call a function with args, and return a string from it (must be free'd) */
278 static char *llua_getstring_read(const char *function, const char *arg)
279 {
280         char *func;
281         char *ret = NULL;
282
283         if(!lua_L) return NULL;
284
285         func = llua_do_read_call(function, arg, 1);
286         if (func) {
287                 if(!lua_isstring(lua_L, -1)) {
288                         NORM_ERR("llua_getstring_read: function %s didn't return a string, result discarded", func);
289                 } else {
290                         ret = strdup(lua_tostring(lua_L, -1));
291                         lua_pop(lua_L, 1);
292                 }
293         }
294
295         return ret;
296 }
297 #endif
298
299 /* call a function with args, and put the result in ret */
300 static int llua_getnumber(const char *args, double *ret)
301 {
302         char *func;
303
304         if(!lua_L) return 0;
305
306         func = llua_do_call(args, 1);
307         if(func) {
308                 if(!lua_isnumber(lua_L, -1)) {
309                         NORM_ERR("llua_getnumber: function %s didn't return a number, result discarded", func);
310                 } else {
311                         *ret = lua_tonumber(lua_L, -1);
312                         lua_pop(lua_L, 1);
313                         return 1;
314                 }
315         }
316         return 0;
317 }
318
319 void llua_close(void)
320 {
321 #ifdef HAVE_SYS_INOTIFY_H
322         llua_rm_notifies();
323 #endif /* HAVE_SYS_INOTIFY_H */
324         if (draw_pre_hook) {
325                 free(draw_pre_hook);
326                 draw_pre_hook = 0;
327         }
328         if (draw_post_hook) {
329                 free(draw_post_hook);
330                 draw_post_hook = 0;
331         }
332         if (startup_hook) {
333                 free(startup_hook);
334                 startup_hook = 0;
335         }
336         if (shutdown_hook) {
337                 free(shutdown_hook);
338                 shutdown_hook = 0;
339         }
340         if(!lua_L) return;
341         lua_close(lua_L);
342         lua_L = NULL;
343 }
344
345 #ifdef HAVE_SYS_INOTIFY_H
346 struct _lua_notify_s {
347         int wd;
348         char name[DEFAULT_TEXT_BUFFER_SIZE];
349         struct _lua_notify_s *next;
350 };
351 static struct _lua_notify_s *lua_notifies = 0;
352
353 static struct _lua_notify_s *llua_notify_list_do_alloc(const char *name)
354 {
355         struct _lua_notify_s *ret = malloc(sizeof(struct _lua_notify_s));
356         memset(ret, 0, sizeof(struct _lua_notify_s));
357         strncpy(ret->name, name, DEFAULT_TEXT_BUFFER_SIZE);
358         return ret;
359 }
360
361 void llua_append_notify(const char *name)
362 {
363         /* do it */
364         struct _lua_notify_s *new_tail = 0;
365         if (!lua_notifies) {
366                 /* empty, fresh new digs */
367                 new_tail = lua_notifies = llua_notify_list_do_alloc(name);
368         } else {
369                 struct _lua_notify_s *tail = lua_notifies;
370                 while (tail->next) {
371                         tail = tail->next;
372                 }
373                 // should be @ the end now
374                 new_tail = llua_notify_list_do_alloc(name);
375                 tail->next = new_tail;
376         }
377         new_tail->wd = inotify_add_watch(inotify_fd,
378                         new_tail->name,
379                         IN_MODIFY);
380 }
381
382 void llua_rm_notifies(void)
383 {
384         /* git 'er done */
385         struct _lua_notify_s *head = lua_notifies;
386         struct _lua_notify_s *next = 0;
387         if (!lua_notifies) return;
388         inotify_rm_watch(inotify_fd, head->wd);
389         if (head->next) next = head->next;
390         free(head);
391         while (next) {
392                 head = next;
393                 next = head->next;
394                 inotify_rm_watch(inotify_fd, head->wd);
395                 free(head);
396         }
397         lua_notifies = 0;
398 }
399
400 void llua_inotify_query(int wd, int mask)
401 {
402         struct _lua_notify_s *head = lua_notifies;
403         if (mask & IN_MODIFY || mask & IN_IGNORED) {
404                 /* for whatever reason, i keep getting IN_IGNORED when the file is
405                  * modified */
406                 while (head) {
407                         if (head->wd == wd) {
408                                 llua_block_notify = 1;
409                                 llua_load(head->name);
410                                 llua_block_notify = 0;
411                                 NORM_ERR("Lua script '%s' reloaded", head->name);
412                                 if (mask & IN_IGNORED) {
413                                         /* for some reason we get IN_IGNORED here
414                                          * sometimes, so we need to re-add the watch */
415                                         head->wd = inotify_add_watch(inotify_fd,
416                                                         head->name,
417                                                         IN_MODIFY);
418                                 }
419                                 return;
420                         }
421                         head = head->next;
422                 }
423         }
424 }
425 #endif /* HAVE_SYS_INOTIFY_H */
426
427 void llua_set_number(const char *key, double value)
428 {
429         lua_pushnumber(lua_L, value);
430         lua_setfield(lua_L, -2, key);
431 }
432
433 void llua_set_startup_hook(const char *args)
434 {
435         if (startup_hook) free(startup_hook);
436         startup_hook = strdup(args);
437 }
438
439 void llua_set_shutdown_hook(const char *args)
440 {
441         if (shutdown_hook) free(shutdown_hook);
442         shutdown_hook = strdup(args);
443 }
444
445 void llua_startup_hook(void)
446 {
447         if (!lua_L || !startup_hook) return;
448         llua_do_call(startup_hook, 0);
449 }
450
451 void llua_shutdown_hook(void)
452 {
453         if (!lua_L || !shutdown_hook) return;
454         llua_do_call(shutdown_hook, 0);
455 }
456
457 #ifdef X11
458 void llua_draw_pre_hook(void)
459 {
460         if (!lua_L || !draw_pre_hook) return;
461         llua_do_call(draw_pre_hook, 0);
462 }
463
464 void llua_draw_post_hook(void)
465 {
466         if (!lua_L || !draw_post_hook) return;
467         llua_do_call(draw_post_hook, 0);
468 }
469
470 void llua_set_draw_pre_hook(const char *args)
471 {
472         draw_pre_hook = strdup(args);
473 }
474
475 void llua_set_draw_post_hook(const char *args)
476 {
477         draw_post_hook = strdup(args);
478 }
479
480 #ifdef LUA_EXTRAS
481 void llua_set_userdata(const char *key, const char *type, void *value)
482 {
483         tolua_pushusertype(lua_L, value, type);
484         lua_setfield(lua_L, -2, key);
485 }
486 #endif /* LUA_EXTRAS */
487
488 void llua_setup_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
489 {
490         if (!lua_L) return;
491         lua_newtable(lua_L);
492
493         if (output_methods & TO_X) {
494 #ifdef LUA_EXTRAS
495                 llua_set_userdata("drawable", "Drawable", (void*)&window.drawable);
496                 llua_set_userdata("visual", "Visual", window.visual);
497                 llua_set_userdata("display", "Display", display);
498 #endif /* LUA_EXTRAS */
499
500
501                 llua_set_number("width", window.width);
502                 llua_set_number("height", window.height);
503                 llua_set_number("border_inner_margin", window.border_inner_margin);
504                 llua_set_number("border_outer_margin", window.border_outer_margin);
505                 llua_set_number("border_width", window.border_width);
506
507                 llua_set_number("text_start_x", text_start_x);
508                 llua_set_number("text_start_y", text_start_y);
509                 llua_set_number("text_width", text_width);
510                 llua_set_number("text_height", text_height);
511
512                 lua_setglobal(lua_L, "conky_window");
513         }
514 }
515
516 void llua_update_window_table(int text_start_x, int text_start_y, int text_width, int text_height)
517 {
518         if (!lua_L) return;
519
520         lua_getglobal(lua_L, "conky_window");
521         if (lua_isnil(lua_L, -1)) {
522                 /* window table isn't populated yet */
523                 lua_pop(lua_L, 1);
524                 return;
525         }
526
527         llua_set_number("width", window.width);
528         llua_set_number("height", window.height);
529
530         llua_set_number("text_start_x", text_start_x);
531         llua_set_number("text_start_y", text_start_y);
532         llua_set_number("text_width", text_width);
533         llua_set_number("text_height", text_height);
534
535         lua_setglobal(lua_L, "conky_window");
536 }
537 #endif /* X11 */
538
539 void llua_setup_info(struct information *i, double u_interval)
540 {
541         if (!lua_L) return;
542         lua_newtable(lua_L);
543
544         llua_set_number("update_interval", u_interval);
545         llua_set_number("uptime", i->uptime);
546
547         lua_setglobal(lua_L, "conky_info");
548 }
549
550 void llua_update_info(struct information *i, double u_interval)
551 {
552         if (!lua_L) return;
553
554         lua_getglobal(lua_L, "conky_info");
555         if (lua_isnil(lua_L, -1)) {
556                 /* window table isn't populated yet */
557                 lua_pop(lua_L, 1);
558                 return;
559         }
560
561         llua_set_number("update_interval", u_interval);
562         llua_set_number("uptime", i->uptime);
563
564         lua_setglobal(lua_L, "conky_info");
565 }
566
567 void print_lua(struct text_object *obj, char *p, int p_max_size)
568 {
569         char *str = llua_getstring(obj->data.s);
570         if (str) {
571                 snprintf(p, p_max_size, "%s", str);
572                 free(str);
573         }
574 }
575
576 void print_lua_parse(struct text_object *obj, char *p, int p_max_size)
577 {
578         char *str = llua_getstring(obj->data.s);
579         if (str) {
580                 evaluate(str, p, p_max_size);
581                 free(str);
582         }
583 }
584
585 void print_lua_bar(struct text_object *obj, char *p, int p_max_size)
586 {
587         double per;
588         if (llua_getnumber(obj->data.s, &per)) {
589                 new_bar(obj, p, p_max_size, (per/100.0 * 255));
590         }
591 }
592
593 #ifdef X11
594 void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
595 {
596         double per;
597
598         if (!p_max_size)
599                 return;
600
601         if (llua_getnumber(obj->data.s, &per)) {
602                 new_graph(obj, p, p_max_size, per);
603         }
604 }
605 #endif /* X11 */
606
607 void print_lua_gauge(struct text_object *obj, char *p, int p_max_size)
608 {
609         double per;
610
611         if (!p_max_size)
612                 return;
613
614         if (llua_getnumber(obj->data.s, &per)) {
615                 new_gauge(obj, p, p_max_size, (per/100.0 * 255));
616         }
617 }