Don't use alarm() in texeci to terminate long processes.
[monky] / src / exec.c
index 4e1f9ba..5c9662c 100644 (file)
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * 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.
  *
 #include "logging.h"
 #include "specials.h"
 #include "text_object.h"
+#include "timed_thread.h"
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 struct execi_data {
        double last_update;
@@ -44,6 +46,7 @@ struct execi_data {
        char *buffer;
        double data;
        timed_thread *p_timed_thread;
+       float barnum;
 };
 
 /* FIXME: this will probably not work, since the variable is being reused
@@ -137,7 +140,8 @@ static inline double get_barnum(char *buf)
        return barnum;
 }
 
-static inline void read_exec(const char *data, char *buf, const int size)
+static inline void read_exec(const char *data, char *buf, const int size, const
+               char use_alarm)
 {
        FILE *fp;
 
@@ -146,7 +150,7 @@ static inline void read_exec(const char *data, char *buf, const int size)
        if (!data)
                return;
 
-       alarm(update_interval);
+       if (use_alarm) alarm(update_interval);
        fp = pid_popen(data, "r", &childpid);
        if(fp) {
                int length;
@@ -160,7 +164,7 @@ static inline void read_exec(const char *data, char *buf, const int size)
        } else {
                buf[0] = '\0';
        }
-       alarm(0);
+       if (use_alarm) alarm(0);
 }
 
 static void *threaded_exec(void *) __attribute__((noreturn));
@@ -173,7 +177,7 @@ static void *threaded_exec(void *arg)
 
        while (1) {
                buff = malloc(text_buffer_size);
-               read_exec(ed->cmd, buff, text_buffer_size);
+               read_exec(ed->cmd, buff, text_buffer_size, 0);
                p2 = buff;
                while (*p2) {
                        if (*p2 == '\001') {
@@ -213,7 +217,7 @@ void scan_pre_exec_arg(struct text_object *obj, const char *arg)
        char buf[2048];
 
        obj->type = OBJ_text;
-       read_exec(arg, buf, sizeof(buf));
+       read_exec(arg, buf, sizeof(buf), 1);
        obj->data.s = strndup(buf, text_buffer_size);
 }
 
@@ -234,9 +238,28 @@ void scan_execi_arg(struct text_object *obj, const char *arg)
        obj->data.opaque = ed;
 }
 
+#ifdef X11
+void scan_execgraph_arg(struct text_object *obj, const char *arg)
+{
+       struct execi_data *ed;
+       char *buf;
+
+       ed = malloc(sizeof(struct execi_data));
+       memset(ed, 0, sizeof(struct execi_data));
+
+       buf = scan_graph(obj, arg, 100);
+       if (!buf) {
+               NORM_ERR("missing command argument to execgraph object");
+               return;
+       }
+       ed->cmd = buf;
+       obj->data.opaque = ed;
+}
+#endif /* X11 */
+
 void print_exec(struct text_object *obj, char *p, int p_max_size)
 {
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        remove_deleted_chars(p);
 }
 
@@ -244,15 +267,20 @@ void print_execp(struct text_object *obj, char *p, int p_max_size)
 {
        struct information *tmp_info;
        struct text_object subroot;
+       char *buf;
+
+       buf = malloc(text_buffer_size);
+       memset(buf, 0, text_buffer_size);
 
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, buf, text_buffer_size, 1);
 
        tmp_info = malloc(sizeof(struct information));
        memcpy(tmp_info, &info, sizeof(struct information));
-       parse_conky_vars(&subroot, p, p, tmp_info);
+       parse_conky_vars(&subroot, buf, p, p_max_size, tmp_info);
 
        free_text_objects(&subroot, 1);
        free(tmp_info);
+       free(buf);
 }
 
 void print_execi(struct text_object *obj, char *p, int p_max_size)
@@ -265,13 +293,13 @@ void print_execi(struct text_object *obj, char *p, int p_max_size)
        if (time_to_update(ed)) {
                if (!ed->buffer)
                        ed->buffer = malloc(text_buffer_size);
-               read_exec(ed->cmd, ed->buffer, text_buffer_size);
+               read_exec(ed->cmd, ed->buffer, text_buffer_size, 1);
                ed->last_update = current_update_time;
        }
        snprintf(p, p_max_size, "%s", ed->buffer);
 }
 
-void print_execpi(struct text_object *obj, char *p)
+void print_execpi(struct text_object *obj, char *p, int p_max_size)
 {
        struct execi_data *ed = obj->data.opaque;
        struct text_object subroot;
@@ -283,9 +311,7 @@ void print_execpi(struct text_object *obj, char *p)
        tmp_info = malloc(sizeof(struct information));
        memcpy(tmp_info, &info, sizeof(struct information));
 
-       if (!time_to_update(ed)) {
-               parse_conky_vars(&subroot, ed->buffer, p, tmp_info);
-       } else {
+       if (time_to_update(ed)) {
                char *output;
                int length;
                FILE *fp = pid_popen(ed->cmd, "r", &childpid);
@@ -302,9 +328,9 @@ void print_execpi(struct text_object *obj, char *p)
                        output[length - 1] = '\0';
                }
 
-               parse_conky_vars(&subroot, ed->buffer, p, tmp_info);
                ed->last_update = current_update_time;
        }
+       parse_conky_vars(&subroot, ed->buffer, p, p_max_size, tmp_info);
        free_text_objects(&subroot, 1);
        free(tmp_info);
 }
@@ -336,47 +362,33 @@ void print_texeci(struct text_object *obj, char *p, int p_max_size)
        }
 }
 
-#ifdef X11
 void print_execgauge(struct text_object *obj, char *p, int p_max_size)
 {
        double barnum;
 
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        barnum = get_barnum(p); /*using the same function*/
 
        if (barnum >= 0.0) {
                barnum /= 100;
-               new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
+               new_gauge(obj, p, p_max_size, round_to_int(barnum * 255.0));
        }
 }
 
+#ifdef X11
 void print_execgraph(struct text_object *obj, char *p, int p_max_size)
 {
-       char showaslog = FALSE;
-       char tempgrad = FALSE;
        double barnum;
        struct execi_data *ed = obj->data.opaque;
-       char *cmd;
 
        if (!ed)
                return;
 
-       cmd = ed->cmd;
-
-       if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
-               tempgrad = TRUE;
-               cmd += strlen(" "TEMPGRAD);
-       }
-       if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
-               showaslog = TRUE;
-               cmd += strlen(" "LOGGRAPH);
-       }
-       read_exec(cmd, p, p_max_size);
+       read_exec(ed->cmd, p, p_max_size, 1);
        barnum = get_barnum(p);
 
        if (barnum > 0) {
-               new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
-                               100, 1, showaslog, tempgrad);
+               new_graph(obj, p, p_max_size, round_to_int(barnum));
        }
 }
 
@@ -389,30 +401,18 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size)
 
        if (time_to_update(ed)) {
                double barnum;
-               char showaslog = FALSE;
-               char tempgrad = FALSE;
-               char *cmd = ed->cmd;
 
-               if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
-                       tempgrad = TRUE;
-                       cmd += strlen(" "TEMPGRAD);
-               }
-               if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
-                       showaslog = TRUE;
-                       cmd += strlen(" "LOGGRAPH);
-               }
-               obj->char_a = showaslog;
-               obj->char_b = tempgrad;
-               read_exec(cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {
-                       obj->f = barnum;
+                       ed->barnum = barnum;
                }
                ed->last_update = current_update_time;
        }
-       new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
+       new_graph(obj, p, p_max_size, (int) (ed->barnum));
 }
+#endif /* X11 */
 
 void print_execigauge(struct text_object *obj, char *p, int p_max_size)
 {
@@ -424,32 +424,26 @@ void print_execigauge(struct text_object *obj, char *p, int p_max_size)
        if (time_to_update(ed)) {
                double barnum;
 
-               read_exec(ed->cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {
-                       obj->f = 255 * barnum / 100.0;
+                       ed->barnum = 255 * barnum / 100.0;
                }
                ed->last_update = current_update_time;
        }
-       new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
+       new_gauge(obj, p, p_max_size, round_to_int(ed->barnum));
 }
-#endif /* X11 */
 
 void print_execbar(struct text_object *obj, char *p, int p_max_size)
 {
        double barnum;
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        barnum = get_barnum(p);
 
        if (barnum >= 0.0) {
-#ifdef X11
-               if(output_methods & TO_X) {
-                       barnum /= 100;
-                       new_bar(obj, p, round_to_int(barnum * 255.0));
-               }else
-#endif /* X11 */
-                       new_bar_in_shell(obj, p, p_max_size, barnum);
+               barnum /= 100;
+               new_bar(obj, p, p_max_size, round_to_int(barnum * 255.0));
        }
 }
 
@@ -462,20 +456,15 @@ void print_execibar(struct text_object *obj, char *p, int p_max_size)
                return;
 
        if (time_to_update(ed)) {
-               read_exec(ed->cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {
-                       obj->f = barnum;
+                       ed->barnum = barnum;
                }
                ed->last_update = current_update_time;
        }
-#ifdef X11
-       if(output_methods & TO_X) {
-               new_bar(obj, p, round_to_int(obj->f * 2.55));
-       } else
-#endif /* X11 */
-               new_bar_in_shell(obj, p, p_max_size, round_to_int(obj->f));
+       new_bar(obj, p, p_max_size, round_to_int(ed->barnum * 2.55));
 }
 
 void free_exec(struct text_object *obj)