diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2021-09-18 14:56:25 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2021-09-20 21:15:28 +0200 |
commit | a6826af796c2809681dec21e7d9dcbd9896ffe42 (patch) | |
tree | 2e803d9d349e36a213964f348e9d358671f8d851 /src | |
parent | bd21b25badd72dbec7723ca002663ba38e3e07c9 (diff) | |
download | weechat-a6826af796c2809681dec21e7d9dcbd9896ffe42.zip |
core: add creation of user variables in evaluated expressions with ${define:name,value}
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-command.c | 113 | ||||
-rw-r--r-- | src/core/wee-eval.c | 51 | ||||
-rw-r--r-- | src/core/wee-eval.h | 1 |
3 files changed, 108 insertions, 57 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index a4ae9d15a..71ff892d7 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -7658,40 +7658,42 @@ command_init () "Some variables are replaced in expression, using the format " "${variable}, variable can be, by order of priority:\n" " 1. the string itself without evaluation (format: \"raw:xxx\")\n" - " 2. an evaluated sub-string (format: \"eval:xxx\")\n" - " 3. an evaluated condition (format: \"eval_cond:xxx\")\n" - " 4. a string with escaped chars (format: \"esc:xxx\" or \"\\xxx\")\n" - " 5. a string with chars to hide (format: \"hide:char,string\")\n" - " 6. a string with max chars (format: \"cut:max,suffix,string\" " + " 2. a user-defined variable (format: \"name\")\n" + " 3. an evaluated sub-string (format: \"eval:xxx\")\n" + " 4. an evaluated condition (format: \"eval_cond:xxx\")\n" + " 5. a string with escaped chars (format: \"esc:xxx\" or \"\\xxx\")\n" + " 6. a string with chars to hide (format: \"hide:char,string\")\n" + " 7. 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" - " 7. a reversed string (format: \"rev:xxx\" or \"revscr:xxx\")\n" - " 8. a repeated string (format: \"repeat:count,string\")\n" - " 9. length of a string (format: \"length:xxx\" or " + " 8. a reversed string (format: \"rev:xxx\" or \"revscr:xxx\")\n" + " 9. a repeated string (format: \"repeat:count,string\")\n" + " 10. length of a string (format: \"length:xxx\" or " "\"lengthscr:xxx\")\n" - " 10. split of a string (format: " + " 11. split of a string (format: " "\"split:number,separators,flags,xxx\")\n" - " 11. split of shell argmuents (format: \"split_shell:number,xxx\")\n" - " 12. a color (format: \"color:xxx\", see \"Plugin API " + " 12. split of shell argmuents (format: \"split_shell:number,xxx\")\n" + " 13. a color (format: \"color:xxx\", see \"Plugin API " "reference\", function \"color\")\n" - " 13. a modifier (format: \"modifier:name,data,string\")\n" - " 14. an info (format: \"info:name,arguments\", arguments are " + " 14. a modifier (format: \"modifier:name,data,string\")\n" + " 15. an info (format: \"info:name,arguments\", arguments are " "optional)\n" - " 15. a base 16/32/64 encoded/decoded string (format: " + " 16. a base 16/32/64 encoded/decoded string (format: " "\"base_encode:base,xxx\" or \"base_decode:base,xxx\")\n" - " 16. current date/time (format: \"date\" or \"date:format\")\n" - " 17. an environment variable (format: \"env:XXX\")\n" - " 18. a ternary operator (format: " + " 17. current date/time (format: \"date\" or \"date:format\")\n" + " 18. an environment variable (format: \"env:XXX\")\n" + " 19. a ternary operator (format: " "\"if:condition?value_if_true:value_if_false\")\n" - " 19. result of an expression with parentheses and operators " + " 20. result of an expression with parentheses and operators " "+ - * / // % ** (format: \"calc:xxx\")\n" - " 20. a random integer number (format: \"random:min,max\")\n" - " 21. a translated string (format: \"translate:xxx\")\n" - " 22. an option (format: \"file.section.option\")\n" - " 23. a local variable in buffer\n" - " 24. a hdata name/variable (the value is automatically converted " + " 21. a random integer number (format: \"random:min,max\")\n" + " 22. a translated string (format: \"translate:xxx\")\n" + " 23. define a user variable (format: \"define:name,value\")\n" + " 24. an option (format: \"file.section.option\")\n" + " 25. a local variable in buffer\n" + " 26. 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" @@ -7713,38 +7715,39 @@ command_init () "reference\", function \"weechat_hdata_get\".\n" "\n" "Examples (simple strings):\n" - " /eval -n ${raw:${info:version}} ==> ${info:version}\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" - " /eval -n ${sec.data.password} ==> secret\n" - " /eval -n ${window} ==> 0x2549aa0\n" - " /eval -n ${window.buffer} ==> 0x2549320\n" - " /eval -n ${window.buffer.full_name} ==> core.weechat\n" - " /eval -n ${window.buffer.number} ==> 1\n" - " /eval -n ${\\t} ==> <tab>\n" - " /eval -n ${hide:-,${relay.network.password}} ==> --------\n" - " /eval -n ${cut:3,+,test} ==> tes+\n" - " /eval -n ${cut:+3,+,test} ==> te+\n" - " /eval -n ${date:%H:%M:%S} ==> 07:46:40\n" - " /eval -n ${if:${info:term_width}>80?big:small} ==> big\n" - " /eval -n ${rev:Hello} ==> olleH\n" - " /eval -n ${repeat:5,-} ==> -----\n" - " /eval -n ${length:test} ==> 4\n" - " /eval -n ${split:1,,,abc,def,ghi} ==> abc\n" - " /eval -n ${split:-1,,,abc,def,ghi} ==> ghi\n" - " /eval -n ${split:count,,,abc,def,ghi} ==> 3\n" - " /eval -n ${split:random,,,abc,def,ghi} ==> def\n" - " /eval -n ${split_shell:1,\"arg 1\" arg2} ==> arg 1\n" - " /eval -n ${split_shell:-1,\"arg 1\" arg2} ==> arg2\n" - " /eval -n ${split_shell:count,\"arg 1\" arg2} ==> 2\n" - " /eval -n ${split_shell:random,\"arg 1\" arg2} ==> arg2\n" - " /eval -n ${calc:(5+2)*3} ==> 21\n" - " /eval -n ${random:0,10} ==> 3\n" - " /eval -n ${base_encode:64,test} ==> dGVzdA==\n" - " /eval -n ${base_decode:64,dGVzdA==} ==> test\n" - " /eval -n ${translate:Plugin} ==> Extension\n" + " /eval -n ${raw:${info:version}} ==> ${info:version}\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" + " /eval -n ${sec.data.password} ==> secret\n" + " /eval -n ${window} ==> 0x2549aa0\n" + " /eval -n ${window.buffer} ==> 0x2549320\n" + " /eval -n ${window.buffer.full_name} ==> core.weechat\n" + " /eval -n ${window.buffer.number} ==> 1\n" + " /eval -n ${\\t} ==> <tab>\n" + " /eval -n ${hide:-,${relay.network.password}} ==> --------\n" + " /eval -n ${cut:3,+,test} ==> tes+\n" + " /eval -n ${cut:+3,+,test} ==> te+\n" + " /eval -n ${date:%H:%M:%S} ==> 07:46:40\n" + " /eval -n ${if:${info:term_width}>80?big:small} ==> big\n" + " /eval -n ${rev:Hello} ==> olleH\n" + " /eval -n ${repeat:5,-} ==> -----\n" + " /eval -n ${length:test} ==> 4\n" + " /eval -n ${split:1,,,abc,def,ghi} ==> abc\n" + " /eval -n ${split:-1,,,abc,def,ghi} ==> ghi\n" + " /eval -n ${split:count,,,abc,def,ghi} ==> 3\n" + " /eval -n ${split:random,,,abc,def,ghi} ==> def\n" + " /eval -n ${split_shell:1,\"arg 1\" arg2} ==> arg 1\n" + " /eval -n ${split_shell:-1,\"arg 1\" arg2} ==> arg2\n" + " /eval -n ${split_shell:count,\"arg 1\" arg2} ==> 2\n" + " /eval -n ${split_shell:random,\"arg 1\" arg2} ==> arg2\n" + " /eval -n ${calc:(5+2)*3} ==> 21\n" + " /eval -n ${random:0,10} ==> 3\n" + " /eval -n ${base_encode:64,test} ==> dGVzdA==\n" + " /eval -n ${base_decode:64,dGVzdA==} ==> test\n" + " /eval -n ${translate:Plugin} ==> Extension\n" + " /eval -n ${define:len,${calc:5+3}}${len}x${len} ==> 8x8\n" "\n" "Examples (conditions):\n" " /eval -n -c ${window.buffer.number} > 2 ==> 0\n" diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 3135380d5..6c1fd22ec 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -1069,6 +1069,28 @@ eval_string_translate (const char *text) } /* + * Defines a variable. + */ + +void +eval_string_define (const char *text, struct t_eval_context *eval_context) +{ + char *pos, *name; + + pos = strchr (text, ','); + if (!pos) + return; + + name = strndup (text, pos - text); + if (!name) + return; + + hashtable_set (eval_context->user_vars, name, pos + 1); + + free (name); +} + +/* * Gets value of hdata using "path" to a variable. * * Note: result must be freed after use. @@ -1352,7 +1374,7 @@ end: /* * Replaces variables, which can be, by order of priority: * 1. the string itself without evaluation (format: raw:xxx) - * 2. an extra variable from hashtable "extra_vars" + * 2. a variable from hashtable "user_vars" or "extra_vars" * 3. a WeeChat home directory, one of: "weechat_config_dir", * "weechat_data_dir", "weechat_cache_dir", "weechat_runtime_dir" * 4. a string to evaluate (format: eval:xxx) @@ -1421,7 +1443,13 @@ eval_replace_vars_cb (void *data, const char *text) goto end; } - /* 2. variable in hashtable "extra_vars" */ + /* 2. variable in hashtable "user_vars" or "extra_vars" */ + ptr_value = hashtable_get (eval_context->user_vars, text); + if (ptr_value) + { + value = strdup (ptr_value); + goto end; + } if (eval_context->extra_vars) { ptr_value = hashtable_get (eval_context->extra_vars, text); @@ -1668,6 +1696,14 @@ eval_replace_vars_cb (void *data, const char *text) goto end; } + /* 25. define a variable */ + if (strncmp (text, "define:", 7) == 0) + { + eval_string_define (text + 7, eval_context); + value = strdup (""); + goto end; + } + /* 25. option: if found, return this value */ if (strncmp (text, "sec.data.", 9) == 0) { @@ -2333,6 +2369,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { struct t_eval_context context, *eval_context; + struct t_hashtable *user_vars; int condition, rc, pointers_allocated, regex_allocated, debug_id; int ptr_window_added, ptr_buffer_added; long number; @@ -2347,6 +2384,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers, return NULL; condition = 0; + user_vars = NULL; pointers_allocated = 0; regex_allocated = 0; regex = NULL; @@ -2371,10 +2409,17 @@ eval_expression (const char *expr, struct t_hashtable *pointers, pointers_allocated = 1; } + user_vars = hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + eval_context = &context; eval_context->pointers = pointers; eval_context->extra_vars = extra_vars; + eval_context->user_vars = user_vars; eval_context->extra_vars_eval = 0; eval_context->prefix = default_prefix; eval_context->suffix = default_suffix; @@ -2506,6 +2551,8 @@ eval_expression (const char *expr, struct t_hashtable *pointers, if (ptr_buffer_added) hashtable_remove (pointers, "buffer"); } + if (user_vars) + hashtable_free (user_vars); if (regex && regex_allocated) { regfree (regex); diff --git a/src/core/wee-eval.h b/src/core/wee-eval.h index 5c97b7a31..03b33a421 100644 --- a/src/core/wee-eval.h +++ b/src/core/wee-eval.h @@ -73,6 +73,7 @@ struct t_eval_context { struct t_hashtable *pointers; /* pointers used in eval */ struct t_hashtable *extra_vars; /* extra variables used in eval */ + struct t_hashtable *user_vars; /* user-defined variables */ int extra_vars_eval; /* 1 if extra vars must be evaluated */ const char *prefix; /* prefix (default is "${") */ const char *suffix; /* suffix (default is "}") */ |