1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-04 00:33:13 +02:00

core: add evaluation of conditions in evaluation of expressions with "eval_cond:" (closes #1582)

This commit is contained in:
Sébastien Helleu
2020-11-14 09:28:46 +01:00
parent eb90a73fe8
commit b626df72fb
27 changed files with 574 additions and 468 deletions
+19 -17
View File
@@ -7431,33 +7431,34 @@ command_init ()
"Some variables are replaced in expression, using the format "
"${variable}, variable can be, by order of priority:\n"
" 1. an evaluated sub-string (format: \"eval:xxx\")\n"
" 2. a string with escaped chars (format: \"esc:xxx\" or \"\\xxx\")\n"
" 3. a string with chars to hide (format: \"hide:char,string\")\n"
" 4. a string with max chars (format: \"cut:max,suffix,string\" "
" 2. an evaluated condition (format: \"eval_cond:xxx\")\n"
" 3. a string with escaped chars (format: \"esc:xxx\" or \"\\xxx\")\n"
" 4. a string with chars to hide (format: \"hide:char,string\")\n"
" 5. a string with max chars (format: \"cut:max,suffix,string\" "
"or \"cut:+max,suffix,string\")\n"
" or max chars displayed on screen "
"(format: \"cutscr:max,suffix,string\" or "
"\"cutscr:+max,suffix,string\")\n"
" 5. a reversed string (format: \"rev:xxx\" or \"revscr:xxx\")\n"
" 6. a repeated string (format: \"repeat:count,string\")\n"
" 7. length of a string (format: \"length:xxx\" or "
" 6. a reversed string (format: \"rev:xxx\" or \"revscr:xxx\")\n"
" 7. a repeated string (format: \"repeat:count,string\")\n"
" 8. length of a string (format: \"length:xxx\" or "
"\"lengthscr:xxx\")\n"
" 8. a color (format: \"color:xxx\", see \"Plugin API "
" 9. a color (format: \"color:xxx\", see \"Plugin API "
"reference\", function \"color\")\n"
" 9. a modifier (format: \"modifier:name,data,string\")\n"
" 10. an info (format: \"info:name,arguments\", arguments are "
" 10. a modifier (format: \"modifier:name,data,string\")\n"
" 11. an info (format: \"info:name,arguments\", arguments are "
"optional)\n"
" 11. a base 16/32/64 encoded/decoded string (format: "
" 12. a base 16/32/64 encoded/decoded string (format: "
"\"base_encode:base,xxx\" or \"base_decode:base,xxx\")\n"
" 12. current date/time (format: \"date\" or \"date:format\")\n"
" 13. an environment variable (format: \"env:XXX\")\n"
" 14. a ternary operator (format: "
" 13. current date/time (format: \"date\" or \"date:format\")\n"
" 14. an environment variable (format: \"env:XXX\")\n"
" 15. a ternary operator (format: "
"\"if:condition?value_if_true:value_if_false\")\n"
" 15. result of an expression with parentheses and operators "
" 16. result of an expression with parentheses and operators "
"+ - * / // % ** (format: \"calc:xxx\")\n"
" 16. an option (format: \"file.section.option\")\n"
" 17. a local variable in buffer\n"
" 18. a hdata name/variable (the value is automatically converted "
" 17. an option (format: \"file.section.option\")\n"
" 18. a local variable in buffer\n"
" 19. a hdata name/variable (the value is automatically converted "
"to string), by default \"window\" and \"buffer\" point to current "
"window/buffer.\n"
"Format for hdata can be one of following:\n"
@@ -7477,6 +7478,7 @@ command_init ()
"reference\", function \"weechat_hdata_get\".\n"
"\n"
"Examples (simple strings):\n"
" /eval -n ${eval_cond:${window.win_width}>100} ==> 1\n"
" /eval -n ${info:version} ==> 0.4.3\n"
" /eval -n ${env:HOME} ==> /home/user\n"
" /eval -n ${weechat.look.scroll_amount} ==> 3\n"
+66 -37
View File
@@ -188,6 +188,27 @@ eval_strstr_level (const char *string, const char *search,
return NULL;
}
/*
* Evaluates a condition and returns boolean result:
* "0" if false
* "1" if true
*
* Note: result must be freed after use.
*/
char *
eval_string_eval_cond (const char *text, struct t_eval_context *eval_context)
{
char *tmp;
int rc;
tmp = eval_expression_condition (text, eval_context);
rc = eval_is_true (tmp);
if (tmp)
free (tmp);
return strdup ((rc) ? EVAL_STR_TRUE : EVAL_STR_FALSE);
}
/*
* Hides chars in a string.
*
@@ -569,7 +590,7 @@ eval_string_date (const char *text)
}
/*
* Evaluates a condition.
* Evaluates a condition and returns evaluated if/else clause.
*
* Note: result must be freed after use.
*/
@@ -893,29 +914,30 @@ end:
* Replaces variables, which can be, by order of priority:
* 1. an extra variable from hashtable "extra_vars"
* 2. a string to evaluate (format: eval:xxx)
* 3. a string with escaped chars (format: esc:xxx or \xxx)
* 4. a string with chars to hide (format: hide:char,string)
* 5. a string with max chars (format: cut:max,suffix,string or
* 3. a condition to evaluate (format: eval_cond:xxx)
* 4. a string with escaped chars (format: esc:xxx or \xxx)
* 5. a string with chars to hide (format: hide:char,string)
* 6. a string with max chars (format: cut:max,suffix,string or
* cut:+max,suffix,string) or max chars on screen
* (format: cutscr:max,suffix,string or cutscr:+max,suffix,string)
* 6. a reversed string (format: rev:xxx) or reversed string for screen,
* 7. a reversed string (format: rev:xxx) or reversed string for screen,
* color codes are not reversed (format: revscr:xxx)
* 7. a repeated string (format: repeat:count,string)
* 8. length of a string (format: length:xxx) or length of a string on screen
* 8. a repeated string (format: repeat:count,string)
* 9. length of a string (format: length:xxx) or length of a string on screen
* (format: lengthscr:xxx); color codes are ignored
* 9. a regex group captured (format: re:N (0.99) or re:+)
* 10. a color (format: color:xxx)
* 11. a modifier (format: modifier:name,data,xxx)
* 12. an info (format: info:name,arguments)
* 13. a base 16/32/64 encoded/decoded string (format: base_encode:base,xxx
* 10. a regex group captured (format: re:N (0.99) or re:+)
* 11. a color (format: color:xxx)
* 12. a modifier (format: modifier:name,data,xxx)
* 13. an info (format: info:name,arguments)
* 14. a base 16/32/64 encoded/decoded string (format: base_encode:base,xxx
* or base_decode:base,xxx)
* 14. current date/time (format: date or date:xxx)
* 15. an environment variable (format: env:XXX)
* 16. a ternary operator (format: if:condition?value_if_true:value_if_false)
* 17. calculate result of an expression (format: calc:xxx)
* 18. an option (format: file.section.option)
* 19. a buffer local variable
* 20. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2
* 15. current date/time (format: date or date:xxx)
* 16. an environment variable (format: env:XXX)
* 17. a ternary operator (format: if:condition?value_if_true:value_if_false)
* 18. calculate result of an expression (format: calc:xxx)
* 19. an option (format: file.section.option)
* 20. a buffer local variable
* 21. a hdata variable (format: hdata.var1.var2 or hdata[list].var1.var2
* or hdata[ptr].var1.var2)
*
* See /help in WeeChat for examples.
@@ -969,18 +991,25 @@ eval_replace_vars_cb (void *data, const char *text)
if (strncmp (text, "eval:", 5) == 0)
return eval_replace_vars (text + 5, eval_context);
/* 3. convert escaped chars */
/*
* 3. force evaluation of condition (recursive call)
* --> use with caution: the text must be safe!
*/
if (strncmp (text, "eval_cond:", 10) == 0)
return eval_string_eval_cond (text + 10, eval_context);
/* 4. convert escaped chars */
if (strncmp (text, "esc:", 4) == 0)
return string_convert_escaped_chars (text + 4);
if ((text[0] == '\\') && text[1] && (text[1] != '\\'))
return string_convert_escaped_chars (text);
/* 4. hide chars: replace all chars by a given char/string */
/* 5. hide chars: replace all chars by a given char/string */
if (strncmp (text, "hide:", 5) == 0)
return eval_string_hide (text + 5);
/*
* 5. cut chars:
* 6. cut chars:
* cut: max number of chars, and add an optional suffix when the
* string is cut
* cutscr: max number of chars displayed on screen, and add an optional
@@ -991,18 +1020,18 @@ eval_replace_vars_cb (void *data, const char *text)
if (strncmp (text, "cutscr:", 7) == 0)
return eval_string_cut (text + 7, 1);
/* 6. reverse string */
/* 7. reverse string */
if (strncmp (text, "rev:", 4) == 0)
return string_reverse (text + 4);
if (strncmp (text, "revscr:", 7) == 0)
return string_reverse_screen (text + 7);
/* 7. repeated string */
/* 8. repeated string */
if (strncmp (text, "repeat:", 7) == 0)
return eval_string_repeat (text + 7);
/*
* 8. length of string:
* 9. length of string:
* length: number of chars
* lengthscr: number of chars displayed on screen
*/
@@ -1019,33 +1048,33 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (str_value);
}
/* 9. regex group captured */
/* 10. regex group captured */
if (strncmp (text, "re:", 3) == 0)
return eval_string_regex_group (text + 3, eval_context);
/* 10. color code */
/* 11. color code */
if (strncmp (text, "color:", 6) == 0)
return eval_string_color (text + 6);
/* 11. modifier */
/* 12. modifier */
if (strncmp (text, "modifier:", 9) == 0)
return eval_string_modifier (text + 9);
/* 12. info */
/* 13. info */
if (strncmp (text, "info:", 5) == 0)
return eval_string_info (text + 5);
/* 13. base_encode/base_decode */
/* 14. base_encode/base_decode */
if (strncmp (text, "base_encode:", 12) == 0)
return eval_string_base_encode (text + 12);
if (strncmp (text, "base_decode:", 12) == 0)
return eval_string_base_decode (text + 12);
/* 14. current date/time */
/* 15. current date/time */
if ((strncmp (text, "date", 4) == 0) && (!text[4] || (text[4] == ':')))
return eval_string_date (text + 4);
/* 15. environment variable */
/* 16. environment variable */
if (strncmp (text, "env:", 4) == 0)
{
ptr_value = getenv (text + 4);
@@ -1053,18 +1082,18 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
/* 16: ternary operator: if:condition?value_if_true:value_if_false */
/* 17: ternary operator: if:condition?value_if_true:value_if_false */
if (strncmp (text, "if:", 3) == 0)
return eval_string_if (text + 3, eval_context);
/*
* 17. calculate the result of an expression
* 18. calculate the result of an expression
* (with number, operators and parentheses)
*/
if (strncmp (text, "calc:", 5) == 0)
return calc_expression (text + 5);
/* 18. option: if found, return this value */
/* 19. option: if found, return this value */
if (strncmp (text, "sec.data.", 9) == 0)
{
ptr_value = hashtable_get (secure_hashtable_data, text + 9);
@@ -1094,7 +1123,7 @@ eval_replace_vars_cb (void *data, const char *text)
}
}
/* 19. local variable in buffer */
/* 20. local variable in buffer */
ptr_buffer = hashtable_get (eval_context->pointers, "buffer");
if (ptr_buffer)
{
@@ -1103,7 +1132,7 @@ eval_replace_vars_cb (void *data, const char *text)
return strdup (ptr_value);
}
/* 20. hdata */
/* 21. hdata */
return eval_string_hdata (text, eval_context);
}