changes related to temperature and layout
[monky] / src / llua.c
index 750c8f3..8f1df3c 100644 (file)
@@ -4,7 +4,7 @@
  * Conky, a system monitor, based on torsmo
  *
  * Copyright (c) 2009 Toni Spets
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -26,6 +26,8 @@
 #include "logging.h"
 #include "build.h"
 
+#include <ctype.h>
+
 #ifdef LUA_EXTRAS
 #include <tolua++.h>
 #endif /* LUA_EXTRAS */
@@ -38,6 +40,8 @@ void llua_rm_notifies(void);
 static int llua_block_notify = 0;
 #endif /* HAVE_SYS_INOTIFY_H */
 
+#define MIN(a, b) ( (a) < (b) ? (a) : (b) )
+
 static char *draw_pre_hook = 0;
 static char *draw_post_hook = 0;
 static char *startup_hook = 0;
@@ -154,6 +158,34 @@ void llua_load(const char *script)
 }
 
 /*
+ * Returns the first space-delimited token of the string starting at position *len.
+ * On return *len contains the length of the token. Spaces inside brackets are ignored, so that
+ * eg. '${foo bar}' is treated as a single token. Sets *len to zero and *str points to the end of
+ * the string when there are no more tokens.
+ */
+static const char* tokenize(const char *str, size_t *len)
+{
+       str += *len;
+       *len = 0;
+       while(str && isspace(*str))
+               ++str;
+
+       size_t level = 0;
+       while(str[*len] && (level > 0 || !isspace(str[*len]))) {
+               switch(str[*len]) {
+                       case '{': ++level; break;
+                       case '}': --level; break;
+               }
+               ++*len;
+       }
+
+       if(!str[*len] && level > 0)
+               NORM_ERR("tokenize: improperly nested token: %s", str);
+
+       return str;
+}
+
+/*
    llua_do_call does a flexible call to any Lua function
 string: <function> [par1] [par2...]
 retc: the number of return values expected
@@ -163,35 +195,31 @@ static char *llua_do_call(const char *string, int retc)
        static char func[64];
        int argc = 0;
 
-       char *tmp = strdup(string);
-       char *ptr = strtok(tmp, " ");
+       size_t len = 0;
+
+       const char *ptr = tokenize(string, &len);
 
        /* proceed only if the function name is present */
-       if (!ptr) {
-               free(tmp);
+       if (!len) {
                return NULL;
        }
 
        /* call only conky_ prefixed functions */
-       if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) == 0) {
-               snprintf(func, 64, "%s", ptr);
-       }else{
-               snprintf(func, 64, "%s%s", LUAPREFIX, ptr);
-       }
+       if(strncmp(ptr, LUAPREFIX, strlen(LUAPREFIX)) != 0) {
+               snprintf(func, sizeof func, "%s", LUAPREFIX);
+       } else
+               *func = 0;
+       strncat(func, ptr, MIN(len, sizeof(func) - strlen(func) - 1));
 
        /* push the function name to stack */
        lua_getglobal(lua_L, func);
 
        /* parse all function parameters from args and push them to the stack */
-       ptr = strtok(NULL, " ");
-       while (ptr) {
-               lua_pushstring(lua_L, ptr);
-               ptr = strtok(NULL, " ");
+       while( ptr = tokenize(ptr, &len), len) {
+               lua_pushlstring(lua_L, ptr, len);
                argc++;
        }
 
-       free(tmp);
-
        if(lua_pcall(lua_L, argc, retc, 0) != 0) {
                NORM_ERR("llua_do_call: function %s execution failed: %s", func, lua_tostring(lua_L, -1));
                lua_pop(lua_L, -1);
@@ -303,6 +331,14 @@ void llua_close(void)
                free(draw_post_hook);
                draw_post_hook = 0;
        }
+       if (startup_hook) {
+               free(startup_hook);
+               startup_hook = 0;
+       }
+       if (shutdown_hook) {
+               free(shutdown_hook);
+               shutdown_hook = 0;
+       }
        if(!lua_L) return;
        lua_close(lua_L);
        lua_L = NULL;
@@ -398,11 +434,13 @@ void llua_set_number(const char *key, double value)
 
 void llua_set_startup_hook(const char *args)
 {
+       if (startup_hook) free(startup_hook);
        startup_hook = strdup(args);
 }
 
 void llua_set_shutdown_hook(const char *args)
 {
+       if (shutdown_hook) free(shutdown_hook);
        shutdown_hook = strdup(args);
 }
 
@@ -563,7 +601,7 @@ void print_lua_graph(struct text_object *obj, char *p, int p_max_size)
                return;
 
        if (llua_getnumber(obj->data.s, &per)) {
-               new_graph(obj, p, per);
+               new_graph(obj, p, p_max_size, per);
        }
 }
 #endif /* X11 */