From 598fbf337ea1b9754ad22a8101f9b17f842e3b69 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Tue, 18 Aug 2015 13:47:45 +0300 Subject: [PATCH 1/2] core: add extra_vars evaluation option to eval_expression --- src/core/wee-eval.c | 67 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index b16225661..4889a671d 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -52,6 +52,7 @@ char *comparisons[EVAL_NUM_COMPARISONS] = char *eval_replace_vars (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, const char *prefix, const char *suffix, + int extra_vars_eval, struct t_eval_regex *eval_regex); @@ -255,7 +256,7 @@ eval_replace_vars_cb (void *data, const char *text) const char *prefix, *suffix, *ptr_value, *ptr_arguments, *ptr_string; struct t_hdata *hdata; void *pointer; - int i, length_hide_char, length, index, rc; + int extra_vars_eval, i, length_hide_char, length, index, rc; long number; long unsigned int ptr; time_t date; @@ -265,14 +266,24 @@ eval_replace_vars_cb (void *data, const char *text) extra_vars = (struct t_hashtable *)(((void **)data)[1]); prefix = (const char *)(((void **)data)[2]); suffix = (const char *)(((void **)data)[3]); - eval_regex = (struct t_eval_regex *)(((void **)data)[4]); + extra_vars_eval = *(int *)(((void **)data)[4]); + eval_regex = (struct t_eval_regex *)(((void **)data)[5]); /* 1. variable in hashtable "extra_vars" */ if (extra_vars) { ptr_value = hashtable_get (extra_vars, text); if (ptr_value) - return strdup (ptr_value); + { + if (extra_vars_eval) + { + return eval_replace_vars (ptr_value, pointers, extra_vars, + prefix, suffix, extra_vars_eval, + eval_regex); + } + else + return strdup (ptr_value); + } } /* @@ -282,7 +293,8 @@ eval_replace_vars_cb (void *data, const char *text) if (strncmp (text, "eval:", 5) == 0) { return eval_replace_vars (text + 5, pointers, extra_vars, - prefix, suffix, eval_regex); + prefix, suffix, extra_vars_eval, + eval_regex); } /* 3. convert escaped chars */ @@ -518,15 +530,17 @@ char * eval_replace_vars (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, const char *prefix, const char *suffix, + int extra_vars_eval, struct t_eval_regex *eval_regex) { - const void *ptr[5]; + const void *ptr[6]; ptr[0] = pointers; ptr[1] = extra_vars; ptr[2] = prefix; ptr[3] = suffix; - ptr[4] = eval_regex; + ptr[4] = &extra_vars_eval; + ptr[5] = eval_regex; return string_replace_with_callback (expr, prefix, suffix, &eval_replace_vars_cb, ptr, NULL); @@ -692,7 +706,8 @@ eval_expression_condition (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, const char *prefix, - const char *suffix) + const char *suffix, + int extra_vars_eval) { int logic, comp, length, level, rc; const char *pos, *pos_end; @@ -747,7 +762,8 @@ eval_expression_condition (const char *expr, goto end; tmp_value = eval_expression_condition (sub_expr, pointers, extra_vars, - prefix, suffix); + prefix, suffix, + extra_vars_eval); free (sub_expr); rc = eval_is_true (tmp_value); if (tmp_value) @@ -768,7 +784,8 @@ eval_expression_condition (const char *expr, pos++; } tmp_value = eval_expression_condition (pos, pointers, extra_vars, - prefix, suffix); + prefix, suffix, + extra_vars_eval); rc = eval_is_true (tmp_value); if (tmp_value) free (tmp_value); @@ -809,10 +826,12 @@ eval_expression_condition (const char *expr, tmp_value = eval_replace_vars (sub_expr, pointers, extra_vars, prefix, suffix, + extra_vars_eval, NULL); tmp_value2 = eval_replace_vars (pos, pointers, extra_vars, prefix, suffix, + extra_vars_eval, NULL); } else @@ -820,10 +839,12 @@ eval_expression_condition (const char *expr, /* other comparison: fully evaluate both expressions */ tmp_value = eval_expression_condition (sub_expr, pointers, extra_vars, - prefix, suffix); + prefix, suffix, + extra_vars_eval); tmp_value2 = eval_expression_condition (pos, pointers, extra_vars, - prefix, suffix); + prefix, suffix, + extra_vars_eval); } free (sub_expr); value = eval_compare (tmp_value, comp, tmp_value2); @@ -862,7 +883,7 @@ eval_expression_condition (const char *expr, if (!sub_expr) goto end; tmp_value = eval_expression_condition (sub_expr, pointers, extra_vars, - prefix, suffix); + prefix, suffix, extra_vars_eval); free (sub_expr); if (!pos[1]) { @@ -898,7 +919,7 @@ eval_expression_condition (const char *expr, * so we just replace variables in string and return the result */ value = eval_replace_vars (expr2, pointers, extra_vars, prefix, suffix, - NULL); + extra_vars_eval, NULL); end: if (expr2) @@ -935,7 +956,8 @@ char * eval_replace_regex (const char *string, regex_t *regex, const char *replace, struct t_hashtable *pointers, struct t_hashtable *extra_vars, - const char *prefix, const char *suffix) + const char *prefix, const char *suffix, + int extra_vars_eval) { char *result, *result2, *str_replace; int length, length_replace, start_offset, i, rc, end; @@ -989,7 +1011,8 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace, eval_regex.result = result; str_replace = eval_replace_vars (replace, pointers, extra_vars, - prefix, suffix, &eval_regex); + prefix, suffix, extra_vars_eval, + &eval_regex); length_replace = (str_replace) ? strlen (str_replace) : 0; @@ -1071,7 +1094,7 @@ char * eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { - int condition, rc, pointers_allocated, regex_allocated; + int condition, extra_vars_eval, rc, pointers_allocated, regex_allocated; char *value; const char *prefix, *suffix; const char *default_prefix = EVAL_DEFAULT_PREFIX; @@ -1084,6 +1107,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers, return NULL; condition = 0; + extra_vars_eval = 0; pointers_allocated = 0; regex_allocated = 0; prefix = default_prefix; @@ -1132,6 +1156,11 @@ eval_expression (const char *expr, struct t_hashtable *pointers, if (ptr_value && (strcmp (ptr_value, "condition") == 0)) condition = 1; + /* */ + ptr_value = hashtable_get (options, "extra"); + if (ptr_value && (strcmp (ptr_value, "eval") == 0)) + extra_vars_eval = 1; + /* check for custom prefix */ ptr_value = hashtable_get (options, "prefix"); if (ptr_value && ptr_value[0]) @@ -1172,7 +1201,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers, { /* evaluate as condition (return a boolean: "0" or "1") */ value = eval_expression_condition (expr, pointers, extra_vars, - prefix, suffix); + prefix, suffix, extra_vars_eval); rc = eval_is_true (value); if (value) free (value); @@ -1185,13 +1214,13 @@ eval_expression (const char *expr, struct t_hashtable *pointers, /* replace with regex */ value = eval_replace_regex (expr, regex, regex_replace, pointers, extra_vars, - prefix, suffix); + prefix, suffix, extra_vars_eval); } else { /* only replace variables in expression */ value = eval_replace_vars (expr, pointers, extra_vars, - prefix, suffix, NULL); + prefix, suffix, extra_vars_eval, NULL); } } From ed337d929162a32a947a3c812f1d4516072a6737 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Tue, 18 Aug 2015 15:19:37 +0300 Subject: [PATCH 2/2] core: document extra option in eval_expression --- doc/en/weechat_plugin_api.en.asciidoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/en/weechat_plugin_api.en.asciidoc b/doc/en/weechat_plugin_api.en.asciidoc index 2398e45e7..d867a5d92 100644 --- a/doc/en/weechat_plugin_api.en.asciidoc +++ b/doc/en/weechat_plugin_api.en.asciidoc @@ -1943,6 +1943,10 @@ Arguments: parentheses are used, result is a boolean ("0" or "1") ** 'prefix': prefix before variables to replace (default: "${") ** 'suffix': suffix after variables to replace (default: "}") +** 'extra': default behavior is to just replace extra variables ('extra_vars'), + other behavior can be selected: +*** 'eval': extra variables ('extra_vars') are evaluated themselves before + replacing ** 'regex': a regex used to replace text in 'expr' (which is then not evaluated) ** 'regex_replace': the replacement text to use with 'regex', to replace