Better argument handling for rss/curl/weather.
[monky] / src / conky.c
index d1fb1fd..136773c 100644 (file)
@@ -455,10 +455,10 @@ int check_contains(char *f, char *s)
        return ret;
 }
 
-#ifdef X11
-
 #define SECRIT_MULTILINE_CHAR '\x02'
 
+#ifdef X11
+
 static inline int calc_text_width(const char *s, int l)
 {
        if ((output_methods & TO_X) == 0) {
@@ -930,6 +930,7 @@ static void free_text_objects(struct text_object *root, int internal)
 #endif
 #ifdef HAVE_LUA
                        case OBJ_lua:
+                       case OBJ_lua_parse:
                        case OBJ_lua_bar:
 #ifdef X11
                        case OBJ_lua_graph:
@@ -2831,8 +2832,12 @@ static struct text_object *construct_text_object(const char *s,
                        char *uri = (char *) malloc(128 * sizeof(char));
 
                        argc = sscanf(arg, "%127s %f", uri, &interval);
-                       obj->data.curl.uri = uri;
-                       obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
+                       if (argc == 2) {
+                               obj->data.curl.uri = uri;
+                               obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
+                       } else {
+                               ERR("wrong number of arguments for $curl");
+                       }
                } else {
                        CRIT_ERR(obj, free_at_crash, "curl needs arguments: <uri> <interval in minutes>");
                }
@@ -2848,11 +2853,15 @@ static struct text_object *construct_text_object(const char *s,
 
                        argc = sscanf(arg, "%127s %f %63s %d %u", uri, &interval, action,
                                        &act_par, &nrspaces);
-                       obj->data.rss.uri = uri;
-                       obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
-                       obj->data.rss.action = action;
-                       obj->data.rss.act_par = act_par;
-                       obj->data.rss.nrspaces = nrspaces;
+                       if (argc == 5) {
+                               obj->data.rss.uri = uri;
+                               obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
+                               obj->data.rss.action = action;
+                               obj->data.rss.act_par = act_par;
+                               obj->data.rss.nrspaces = nrspaces;
+                       } else {
+                               ERR("wrong number of arguments for $rss");
+                       }
                } else {
                        CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <interval in minutes> <action> "
                                        "[act_par] [spaces in front]");
@@ -2861,52 +2870,57 @@ static struct text_object *construct_text_object(const char *s,
 #ifdef WEATHER
        END OBJ(weather, 0)
                if (arg) {
-                       int argc, interval;
+                       int argc;
+                       float interval;
                        char *locID = (char *) malloc(9 * sizeof(char));
                        char *uri = (char *) malloc(128 * sizeof(char));
                        char *data_type = (char *) malloc(32 * sizeof(char));
                        char *tmp_p;
 
-                       argc = sscanf(arg, "%119s %8s %31s %d", uri, locID, data_type, &interval);
+                       argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
 
-                       /* locID MUST BE upper-case */
-                       tmp_p = locID;
-                       while (*tmp_p) {
-                         *tmp_p = toupper(*tmp_p);
-                         tmp_p++;
-                       }
+                       if (argc >= 3) {
+                               /* locID MUST BE upper-case */
+                               tmp_p = locID;
+                               while (*tmp_p) {
+                                       *tmp_p = toupper(*tmp_p);
+                                       tmp_p++;
+                               }
 
-                       /* Construct complete uri */
-                       if (strstr(uri, "xoap.weather.com")) {
-                         if(xoap != NULL) {
-                           strcat(uri, locID);
-                           strcat(uri, xoap);
-                         } else {
-                           free(uri);
-                           uri = NULL;
-                         }
-                       } else if (strstr(uri, "weather.noaa.gov")) {
-                           strcat(uri, locID);
-                           strcat(uri, ".TXT");
-                       } else  if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
-                             CRIT_ERR(obj, free_at_crash, \
-                                      "could not recognize the weather uri");
-                       }
+                               /* Construct complete uri */
+                               if (strstr(uri, "xoap.weather.com")) {
+                                       if(xoap != NULL) {
+                                               strcat(uri, locID);
+                                               strcat(uri, xoap);
+                                       } else {
+                                               free(uri);
+                                               uri = NULL;
+                                       }
+                               } else if (strstr(uri, "weather.noaa.gov")) {
+                                       strcat(uri, locID);
+                                       strcat(uri, ".TXT");
+                               } else  if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
+                                       CRIT_ERR(obj, free_at_crash, \
+                                                       "could not recognize the weather uri");
+                               }
 
-                       obj->data.weather.uri = uri;
-                       obj->data.weather.data_type = data_type;
+                               obj->data.weather.uri = uri;
+                               obj->data.weather.data_type = data_type;
 
-                       /* Limit the data retrieval interval to half hour min */
-                       if (interval < 30) {
-                               interval = 30;
-                       }
+                               /* Limit the data retrieval interval to half hour min */
+                               if (interval < 30) {
+                                       interval = 30;
+                               }
 
-                       /* Convert to seconds */
-                       obj->data.weather.interval = interval * 60;
-                       free(locID);
+                               /* Convert to seconds */
+                               obj->data.weather.interval = interval * 60;
+                               free(locID);
 
-                       DBGP("weather: fetching %s from %s every %d seconds", \
-                            data_type, uri, obj->data.weather.interval);
+                               DBGP("weather: fetching %s from %s every %d seconds", \
+                                               data_type, uri, obj->data.weather.interval);
+                       } else {
+                               ERR("wrong number of arguments for $weather");
+                       }
                } else {
                        CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
                }
@@ -4691,6 +4705,7 @@ static void generate_text_internal(char *p, int p_max_size,
                                char *str = llua_getstring(obj->data.s);
                                if (str) {
                                        evaluate(str, p);
+                                       free(str);
                                }
                        }
                        OBJ(lua_bar) {
@@ -5822,8 +5837,10 @@ static void generate_text_internal(char *p, int p_max_size,
                                if(buf[obj->data.scroll.start] == 0){
                                         obj->data.scroll.start = 0;
                                }
+#ifdef X11
                                //reset color when scroll is finished
                                new_fg(p + strlen(p), obj->data.scroll.resetcolor);
+#endif
                        }
                        OBJ(combine) {
                                char buf[2][max_user_text];
@@ -6108,18 +6125,16 @@ static inline int get_string_width(const char *s)
        return strlen(s);
 }
 
+#ifdef X11
 static int get_string_width_special(char *s, int special_index)
 {
-#ifdef X11
        char *p, *final;
        int idx = 1;
        int width = 0;
        long i;
 
        if ((output_methods & TO_X) == 0) {
-#endif /* X11 */
                return (s) ? strlen(s) : 0;
-#ifdef X11
        }
 
        if (!s) {
@@ -6154,10 +6169,8 @@ static int get_string_width_special(char *s, int special_index)
        }
        free(final);
        return width;
-#endif /* X11 */
 }
 
-#ifdef X11
 static int text_size_updater(char *s, int special_index);
 
 int last_font_height;
@@ -6907,7 +6920,8 @@ static int draw_line(char *s, int special_index)
        if ((output_methods & TO_X) == 0) {
 #endif /* X11 */
                draw_string(s);
-               return 0;
+               //'special_index - special_index' instead of 0 otherwise gcc complains about not using special_index when build without X11
+               return special_index - special_index;
 #ifdef X11
        }