diff --git a/src/algebra.c b/src/algebra.c index 484e2e68..4fa5e975 100644 --- a/src/algebra.c +++ b/src/algebra.c @@ -243,3 +243,25 @@ int compare(const char *expr) /* not reached */ return -2; } + +int check_if_match(struct text_object *obj) +{ + char expression[max_user_text]; + int val; + struct information *tmp_info; + int result = 1; + + tmp_info = malloc(sizeof(struct information)); + memcpy(tmp_info, &info, sizeof(struct information)); + generate_text_internal(expression, max_user_text, *obj->sub, tmp_info); + DBGP("parsed arg into '%s'", expression); + + val = compare(expression); + if (val == -2) { + NORM_ERR("compare failed for expression '%s'", expression); + } else if (!val) { + result = 0; + } + free(tmp_info); + return result; +} diff --git a/src/algebra.h b/src/algebra.h index 46dc1e99..eb142dba 100644 --- a/src/algebra.h +++ b/src/algebra.h @@ -46,5 +46,6 @@ enum arg_type { }; int compare(const char *); +int check_if_match(struct text_object *); #endif /* _ALGEBRA_H */ diff --git a/src/common.c b/src/common.c index f8f24e4c..4567a58c 100644 --- a/src/common.c +++ b/src/common.c @@ -35,6 +35,7 @@ #include "net_stat.h" #include "specials.h" #include "timeinfo.h" +#include "top.h" #include #include #include @@ -606,3 +607,71 @@ void print_evaluate(struct text_object *obj, char *p, int p_max_size) { evaluate(obj->data.s, p, p_max_size); } + +int if_empty_iftest(struct text_object *obj) +{ + char buf[max_user_text]; + struct information *tmp_info; + int result = 1; + + tmp_info = malloc(sizeof(struct information)); + memcpy(tmp_info, &info, sizeof(struct information)); + + generate_text_internal(buf, max_user_text, *obj->sub, tmp_info); + + if (strlen(buf) != 0) { + result = 0; + } + free(tmp_info); + return result; +} + +static int check_contains(char *f, char *s) +{ + int ret = 0; + FILE *where = open_file(f, 0); + + if (where) { + char buf1[256]; + + while (fgets(buf1, 256, where)) { + if (strstr(buf1, s)) { + ret = 1; + break; + } + } + fclose(where); + } else { + NORM_ERR("Could not open the file"); + } + return ret; +} + +int if_existing_iftest(struct text_object *obj) +{ + char *spc; + int result = 1; + + spc = strchr(obj->data.s, ' '); + if (!spc && access(obj->data.s, F_OK)) { + result = 0; + } else if (spc) { + *spc = '\0'; + if (check_contains(obj->data.s, spc + 1)) + result = 0; + *spc = ' '; + } + return result; +} + +int if_running_iftest(struct text_object *obj) +{ +#ifdef __linux__ + if (!get_process_by_name(obj->data.s)) { +#else + if ((obj->data.s) && system(obj->data.s)) { +#endif + return 0; + } + return 1; +} diff --git a/src/common.h b/src/common.h index ae957be1..43f717ec 100644 --- a/src/common.h +++ b/src/common.h @@ -104,4 +104,8 @@ void print_cached(struct text_object *, char *, int); void print_evaluate(struct text_object *, char *, int); +int if_empty_iftest(struct text_object *); +int if_existing_iftest(struct text_object *); +int if_running_iftest(struct text_object *); + #endif /* _COMMON_H */ diff --git a/src/conky.c b/src/conky.c index 2b9dd8ed..b834ecba 100644 --- a/src/conky.c +++ b/src/conky.c @@ -434,27 +434,6 @@ int get_updatereset(void) return updatereset; } -int check_contains(char *f, char *s) -{ - int ret = 0; - FILE *where = open_file(f, 0); - - if (where) { - char buf1[256]; - - while (fgets(buf1, 256, where)) { - if (strstr(buf1, s)) { - ret = 1; - break; - } - } - fclose(where); - } else { - NORM_ERR("Could not open the file"); - } - return ret; -} - #define SECRIT_MULTILINE_CHAR '\x02' static inline int calc_text_width(const char *s) @@ -889,61 +868,6 @@ void generate_text_internal(char *p, int p_max_size, cimlib_add_image(obj->data.s); } #endif /* IMLIB2 */ - OBJ(if_empty) { - char buf[max_user_text]; - struct information *tmp_info = - malloc(sizeof(struct information)); - memcpy(tmp_info, cur, sizeof(struct information)); - generate_text_internal(buf, max_user_text, - *obj->sub, tmp_info); - - if (strlen(buf) != 0) { - DO_JUMP; - } - free(tmp_info); - } - OBJ(if_match) { - char expression[max_user_text]; - int val; - struct information *tmp_info; - - tmp_info = malloc(sizeof(struct information)); - memcpy(tmp_info, cur, sizeof(struct information)); - generate_text_internal(expression, max_user_text, - *obj->sub, tmp_info); - DBGP("parsed arg into '%s'", expression); - - val = compare(expression); - if (val == -2) { - NORM_ERR("compare failed for expression '%s'", - expression); - } else if (!val) { - DO_JUMP; - } - free(tmp_info); - } - OBJ(if_existing) { - char *spc; - - spc = strchr(obj->data.s, ' '); - if (!spc && access(obj->data.s, F_OK)) { - DO_JUMP; - } else if (spc) { - *spc = '\0'; - if (check_contains(obj->data.s, spc + 1)) - DO_JUMP; - *spc = ' '; - } - } - OBJ(if_running) { -#ifdef __linux__ - if (!get_process_by_name(obj->data.s)) { -#else - if ((obj->data.s) && system(obj->data.s)) { -#endif - DO_JUMP; - } - } OBJ(pid_chroot) { char buf[max_user_text]; diff --git a/src/core.c b/src/core.c index 0c2ce224..e83affb8 100644 --- a/src/core.c +++ b/src/core.c @@ -692,11 +692,14 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ_IF_ARG(if_empty, 0, "if_empty needs an argument") obj->sub = malloc(sizeof(struct text_object)); extract_variable_text_internal(obj->sub, arg); + obj->callbacks.iftest = &if_empty_iftest; END OBJ_IF_ARG(if_match, 0, "if_match needs arguments") obj->sub = malloc(sizeof(struct text_object)); extract_variable_text_internal(obj->sub, arg); + obj->callbacks.iftest = &check_if_match; END OBJ_IF_ARG(if_existing, 0, "if_existing needs an argument or two") obj->data.s = strndup(arg, text_buffer_size); + obj->callbacks.iftest = &if_existing_iftest; obj->callbacks.free = &gen_free_opaque; END OBJ_IF_ARG(if_mounted, 0, "if_mounted needs an argument") obj->data.s = strndup(arg, text_buffer_size); @@ -706,6 +709,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ_IF_ARG(if_running, &update_top, "if_running needs an argument") top_running = 1; obj->data.s = strndup(arg, text_buffer_size); + obj->callbacks.iftest = &if_running_iftest; obj->callbacks.free = &gen_free_opaque; #else END OBJ_IF_ARG(if_running, 0, "if_running needs an argument") @@ -713,6 +717,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long snprintf(buf, text_buffer_size, "pidof %s >/dev/null", arg); obj->data.s = strndup(buf, text_buffer_size); + /* XXX: maybe use a different callback here */ + obj->callbacks.iftest = &if_running_iftest; #endif END OBJ(kernel, 0) obj->callbacks.print = &print_kernel;