diff options
23 files changed, 676 insertions, 8 deletions
diff --git a/ChangeLog.asciidoc b/ChangeLog.asciidoc index f470426cd..a399b0853 100644 --- a/ChangeLog.asciidoc +++ b/ChangeLog.asciidoc @@ -21,6 +21,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] * core: allow pointer as list name in evaluation of hdata (closes #450) * core: add signal "signal_sighup" +* api: add function string_eval_path_home() * logger: evaluate content of option logger.file.path (issue #388) * relay: display value of HTTP header "X-Real-IP" for websocket connections (closes #440) diff --git a/doc/de/weechat_scripting.de.asciidoc b/doc/de/weechat_scripting.de.asciidoc index dc3af2ddb..7b191e6f7 100644 --- a/doc/de/weechat_scripting.de.asciidoc +++ b/doc/de/weechat_scripting.de.asciidoc @@ -404,7 +404,7 @@ Liste der Skript API Funktionen: charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | Verzeichnisse | mkdir_home, mkdir, mkdir_parents | sortierte Listen | diff --git a/doc/en/weechat_plugin_api.en.asciidoc b/doc/en/weechat_plugin_api.en.asciidoc index 711a9ba11..d9bfa0ecb 100644 --- a/doc/en/weechat_plugin_api.en.asciidoc +++ b/doc/en/weechat_plugin_api.en.asciidoc @@ -917,6 +917,64 @@ free (str); [NOTE] This function is not available in scripting API. +==== string_eval_path_home + +_WeeChat ≥ 1.3._ + +Evaluate a path in 3 steps: + +. replace leading `%h` by WeeChat home directory, +. replace leading `~` by user home directory (call to + <<_string_expand_home,weechat_string_expand_home>>), +. evaluate variables + (see <<_string_eval_expression,weechat_string_eval_expression>>). + +Prototype: + +[source,C] +---- +char *weechat_string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); +---- + +Arguments: + +* 'path': path +* 'pointers': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'extra_vars': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'options': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> + +Return value: + +* evaluated path (must be freed by calling "free" after use) + +C example: + +[source,C] +---- +char *str = weechat_string_expand_home ("%h/test/file.txt"); +/* result: "/home/xxx/.weechat/test/file.txt" */ +/* ... */ +free (str); +---- + +Script (Python): + +[source,python] +---- +# prototype +path = weechat.string_eval_path_home(path, pointers, extra_vars, options) + +# example +path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "") +# path == "/home/xxx/.weechat/test/file.txt" +---- + ==== string_remove_quotes Remove quotes at beginning and end of string (ignore spaces if there are before diff --git a/doc/en/weechat_scripting.en.asciidoc b/doc/en/weechat_scripting.en.asciidoc index 6a03be0de..b8864a26d 100644 --- a/doc/en/weechat_scripting.en.asciidoc +++ b/doc/en/weechat_scripting.en.asciidoc @@ -392,7 +392,7 @@ List of functions in script API: charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | directories | mkdir_home, mkdir, mkdir_parents | sorted lists | diff --git a/doc/fr/weechat_plugin_api.fr.asciidoc b/doc/fr/weechat_plugin_api.fr.asciidoc index 31c5a3086..041a7d2f4 100644 --- a/doc/fr/weechat_plugin_api.fr.asciidoc +++ b/doc/fr/weechat_plugin_api.fr.asciidoc @@ -927,6 +927,64 @@ free (str); [NOTE] Cette fonction n'est pas disponible dans l'API script. +==== string_eval_path_home + +_WeeChat ≥ 1.3._ + +Évaluer un chemin en 3 étapes : + +. remplacer le `%h` du début par le répertoire maison de WeeChat, +. remplacer le `~` du début par le répertoire maison de l'utilisateur (appel à + <<_string_expand_home,weechat_string_expand_home>>), +. évaluer les variables + (voir <<_string_eval_expression,weechat_string_eval_expression>>). + +Prototype : + +[source,C] +---- +char *weechat_string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); +---- + +Paramètres : + +* 'path' : chemin +* 'pointers' : table de hachage pour l'appel à la fonction + <<_string_eval_expression,weechat_string_eval_expression>> +* 'extra_vars' : table de hachage pour l'appel à la fonction + <<_string_eval_expression,weechat_string_eval_expression>> +* 'options' : table de hachage pour l'appel à la fonction + <<_string_eval_expression,weechat_string_eval_expression>> + +Valeur de retour : + +* chemin évalué (doit être supprimé par un appel à "free" après utilisation) + +Exemple en C : + +[source,C] +---- +char *str = weechat_string_expand_home ("%h/test/file.txt"); +/* result: "/home/xxx/.weechat/test/file.txt" */ +/* ... */ +free (str); +---- + +Script (Python): + +[source,python] +---- +# prototype +path = weechat.string_eval_path_home(path, pointers, extra_vars, options) + +# exemple +path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "") +# path == "/home/xxx/.weechat/test/file.txt" +---- + ==== string_remove_quotes Supprimer les apostrophes/guillemets au début et à la fin d'une chaîne (les diff --git a/doc/fr/weechat_scripting.fr.asciidoc b/doc/fr/weechat_scripting.fr.asciidoc index d5e3a08e7..b4a992ba9 100644 --- a/doc/fr/weechat_scripting.fr.asciidoc +++ b/doc/fr/weechat_scripting.fr.asciidoc @@ -402,7 +402,7 @@ Liste des fonctions de l'API script : charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | répertoires | mkdir_home, mkdir, mkdir_parents | listes triées | diff --git a/doc/it/weechat_plugin_api.it.asciidoc b/doc/it/weechat_plugin_api.it.asciidoc index a7e792034..0acfa2424 100644 --- a/doc/it/weechat_plugin_api.it.asciidoc +++ b/doc/it/weechat_plugin_api.it.asciidoc @@ -955,6 +955,67 @@ free (str); [NOTE] Questa funzione non è disponibile nelle API per lo scripting. +==== string_eval_path_home + +_WeeChat ≥ 1.3._ + +// TRANSLATION MISSING +Evaluate a path in 3 steps: + +. replace leading `%h` by WeeChat home directory, +. replace leading `~` by user home directory (call to + <<_string_expand_home,weechat_string_expand_home>>), +. evaluate variables + (see <<_string_eval_expression,weechat_string_eval_expression>>). + +Prototipo: + +[source,C] +---- +char *weechat_string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); +---- + +// TRANSLATION MISSING +Argomenti: + +* 'path': path +* 'pointers': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'extra_vars': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'options': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> + +// TRANSLATION MISSING +Valore restituito: + +* evaluated path (must be freed by calling "free" after use) + +Esempio in C: + +[source,C] +---- +char *str = weechat_string_expand_home ("%h/test/file.txt"); +/* result: "/home/xxx/.weechat/test/file.txt" */ +/* ... */ +free (str); +---- + +Script (Python): + +[source,python] +---- +# prototipo +path = weechat.string_eval_path_home(path, pointers, extra_vars, options) + +# esempio +path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "") +# path == "/home/xxx/.weechat/test/file.txt" +---- + ==== string_remove_quotes Rimuove le virgolette all'inizio e alla fine della stringa (ignora gli diff --git a/doc/it/weechat_scripting.it.asciidoc b/doc/it/weechat_scripting.it.asciidoc index 1fbd026cf..2084fddf5 100644 --- a/doc/it/weechat_scripting.it.asciidoc +++ b/doc/it/weechat_scripting.it.asciidoc @@ -406,7 +406,7 @@ Elenco di funzioni nelle API per gli script: charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | directory | mkdir_home, mkdir, mkdir_parents | liste ordinate | diff --git a/doc/ja/weechat_plugin_api.ja.asciidoc b/doc/ja/weechat_plugin_api.ja.asciidoc index bbcf50784..0cbb3c139 100644 --- a/doc/ja/weechat_plugin_api.ja.asciidoc +++ b/doc/ja/weechat_plugin_api.ja.asciidoc @@ -921,6 +921,67 @@ free (str); [NOTE] スクリプト API ではこの関数を利用できません。 +==== string_eval_path_home + +_WeeChat バージョン 0.3.3 以上で利用可。_ + +// TRANSLATION MISSING +Evaluate a path in 3 steps: + +. replace leading `%h` by WeeChat home directory, +. replace leading `~` by user home directory (call to + <<_string_expand_home,weechat_string_expand_home>>), +. evaluate variables + (see <<_string_eval_expression,weechat_string_eval_expression>>). + +プロトタイプ: + +[source,C] +---- +char *weechat_string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); +---- + +// TRANSLATION MISSING +引数: + +* 'path': path +* 'pointers': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'extra_vars': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> +* 'options': hashtable for call to function + <<_string_eval_expression,weechat_string_eval_expression>> + +// TRANSLATION MISSING +戻り値: + +* evaluated path (must be freed by calling "free" after use) + +C 言語での使用例: + +[source,C] +---- +char *str = weechat_string_expand_home ("%h/test/file.txt"); +/* result: "/home/xxx/.weechat/test/file.txt" */ +/* ... */ +free (str); +---- + +スクリプト (Python) での使用例: + +[source,python] +---- +# プロトタイプ +path = weechat.string_eval_path_home(path, pointers, extra_vars, options) + +# 例 +path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "") +# path == "/home/xxx/.weechat/test/file.txt" +---- + ==== string_remove_quotes 文字列の最初と最後から引用符号を削除 diff --git a/doc/ja/weechat_scripting.ja.asciidoc b/doc/ja/weechat_scripting.ja.asciidoc index eec45a64e..9db3410ac 100644 --- a/doc/ja/weechat_scripting.ja.asciidoc +++ b/doc/ja/weechat_scripting.ja.asciidoc @@ -396,7 +396,7 @@ API に含まれる関数の詳しい情報は charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | ディレクトリ操作 | mkdir_home, mkdir, mkdir_parents | ソート済みリスト | diff --git a/doc/pl/weechat_scripting.pl.asciidoc b/doc/pl/weechat_scripting.pl.asciidoc index 3b80f9cdd..2414af300 100644 --- a/doc/pl/weechat_scripting.pl.asciidoc +++ b/doc/pl/weechat_scripting.pl.asciidoc @@ -395,7 +395,7 @@ Lista funkcji w API skryptów: charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, + strlen_screen, string_match, string_has_highlight, string_has_highlight_regex, string_mask_to_regex, string_remove_color, string_is_command_char, - string_input_for_buffer, string_eval_expression + string_input_for_buffer, string_eval_expression, string_eval_path_home | katalogi | mkdir_home, mkdir, mkdir_parents | przechowywane listy | diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 3f7b62fa2..28e960c18 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -48,6 +48,7 @@ #include "weechat.h" #include "wee-string.h" #include "wee-config.h" +#include "wee-eval.h" #include "wee-hashtable.h" #include "wee-utf8.h" #include "../gui/gui-color.h" @@ -500,6 +501,63 @@ string_expand_home (const char *path) } /* + * Evaluate a path by replacing (in this order): + * 1. "%h" (at beginning of string) by WeeChat home directory. + * 2. "~" by user home directory (call to string_expand_home) + * 3. evaluated variables (see /help eval) + * + * Returns the evaluated path, NULL if error. + * + * Note: result must be freed after use. + */ + +char * +string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options) +{ + char *path1, *path2, *path3; + int length; + + if (!path) + return NULL; + + path1 = NULL; + path2 = NULL; + path3 = NULL; + + /* replace "%h" by Weechat home */ + if (strncmp (path, "%h", 2) == 0) + { + length = strlen (weechat_home) + strlen (path + 2) + 1; + path1 = malloc (length); + if (path1) + snprintf (path1, length, "%s%s", weechat_home, path + 2); + } + else + path1 = strdup (path); + if (!path1) + goto end; + + /* replace "~" by user home */ + path2 = string_expand_home (path1); + if (!path2) + goto end; + + /* evaluate content of path */ + path3 = eval_expression (path2, pointers, extra_vars, options); + +end: + if (path1) + free (path1); + if (path2) + free (path2); + + return path3; +} + +/* * Removes quotes at beginning/end of string (ignores spaces if there are before * first quote or after last quote). * diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 683660eed..0c0b1f807 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -44,6 +44,10 @@ extern int string_match (const char *string, const char *mask, extern char *string_replace (const char *string, const char *search, const char *replace); extern char *string_expand_home (const char *path); +extern char *string_eval_path_home (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); extern char *string_remove_quotes (const char *string, const char *quotes); extern char *string_strip (const char *string, int left, int right, const char *chars); diff --git a/src/plugins/guile/weechat-guile-api.c b/src/plugins/guile/weechat-guile-api.c index 1c9762913..14bc1cfa4 100644 --- a/src/plugins/guile/weechat-guile-api.c +++ b/src/plugins/guile/weechat-guile-api.c @@ -480,6 +480,49 @@ weechat_guile_api_string_eval_expression (SCM expr, SCM pointers, } SCM +weechat_guile_api_string_eval_path_home (SCM path, SCM pointers, + SCM extra_vars, SCM options) +{ + char *result; + SCM return_value; + struct t_hashtable *c_pointers, *c_extra_vars, *c_options; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + if (!scm_is_string (path) || !scm_list_p (pointers) + || !scm_list_p (extra_vars) || !scm_list_p (options)) + API_WRONG_ARGS(API_RETURN_EMPTY); + + c_pointers = weechat_guile_alist_to_hashtable ( + pointers, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + c_extra_vars = weechat_guile_alist_to_hashtable ( + extra_vars, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + c_options = weechat_guile_alist_to_hashtable ( + options, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (API_SCM_TO_STRING(path), + c_pointers, c_extra_vars, + c_options); + + if (c_pointers) + weechat_hashtable_free (c_pointers); + if (c_extra_vars) + weechat_hashtable_free (c_extra_vars); + if (c_options) + weechat_hashtable_free (c_options); + + API_RETURN_STRING_FREE(result); +} + +SCM weechat_guile_api_mkdir_home (SCM directory, SCM mode) { API_INIT_FUNC(1, "mkdir_home", API_RETURN_ERROR); @@ -4660,6 +4703,7 @@ weechat_guile_api_module_init (void *data) API_DEF_FUNC(string_is_command_char, 1); API_DEF_FUNC(string_input_for_buffer, 1); API_DEF_FUNC(string_eval_expression, 4); + API_DEF_FUNC(string_eval_path_home, 4); API_DEF_FUNC(mkdir_home, 2); API_DEF_FUNC(mkdir, 2); API_DEF_FUNC(mkdir_parents, 2); diff --git a/src/plugins/javascript/weechat-js-api.cpp b/src/plugins/javascript/weechat-js-api.cpp index 11b4b6c8a..0be1fc1ea 100644 --- a/src/plugins/javascript/weechat-js-api.cpp +++ b/src/plugins/javascript/weechat-js-api.cpp @@ -421,6 +421,43 @@ API_FUNC(string_eval_expression) API_RETURN_STRING_FREE(result); } +API_FUNC(string_eval_path_home) +{ + struct t_hashtable *pointers, *extra_vars, *options; + char *result; + + API_INIT_FUNC(1, "string_eval_path_home", "shhh", API_RETURN_EMPTY); + + v8::String::Utf8Value path(args[0]); + pointers = weechat_js_object_to_hashtable ( + args[1]->ToObject(), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + extra_vars = weechat_js_object_to_hashtable ( + args[2]->ToObject(), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + options = weechat_js_object_to_hashtable ( + args[3]->ToObject(), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (*path, pointers, extra_vars, + options); + + if (pointers) + weechat_hashtable_free (pointers); + if (extra_vars) + weechat_hashtable_free (extra_vars); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING_FREE(result); +} + API_FUNC(mkdir_home) { int mode; @@ -4681,6 +4718,7 @@ WeechatJsV8::loadLibs() API_DEF_FUNC(string_is_command_char); API_DEF_FUNC(string_input_for_buffer); API_DEF_FUNC(string_eval_expression); + API_DEF_FUNC(string_eval_path_home); API_DEF_FUNC(mkdir_home); API_DEF_FUNC(mkdir); API_DEF_FUNC(mkdir_parents); diff --git a/src/plugins/lua/weechat-lua-api.c b/src/plugins/lua/weechat-lua-api.c index ce704a73d..2b3270a92 100644 --- a/src/plugins/lua/weechat-lua-api.c +++ b/src/plugins/lua/weechat-lua-api.c @@ -441,6 +441,43 @@ API_FUNC(string_eval_expression) API_RETURN_STRING_FREE(result); } +API_FUNC(string_eval_path_home) +{ + const char *path; + struct t_hashtable *pointers, *extra_vars, *options; + char *result; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + if (lua_gettop (L) < 4) + API_WRONG_ARGS(API_RETURN_EMPTY); + + path = lua_tostring (L, -4); + pointers = weechat_lua_tohashtable (L, -3, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + extra_vars = weechat_lua_tohashtable (L, -2, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + options = weechat_lua_tohashtable (L, -1, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (path, pointers, extra_vars, + options); + + if (pointers) + weechat_hashtable_free (pointers); + if (extra_vars) + weechat_hashtable_free (extra_vars); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING_FREE(result); +} + API_FUNC(mkdir_home) { const char *directory; @@ -4957,6 +4994,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = { API_DEF_FUNC(string_is_command_char), API_DEF_FUNC(string_input_for_buffer), API_DEF_FUNC(string_eval_expression), + API_DEF_FUNC(string_eval_path_home), API_DEF_FUNC(mkdir_home), API_DEF_FUNC(mkdir), API_DEF_FUNC(mkdir_parents), diff --git a/src/plugins/perl/weechat-perl-api.c b/src/plugins/perl/weechat-perl-api.c index afcf09d42..dddcb9500 100644 --- a/src/plugins/perl/weechat-perl-api.c +++ b/src/plugins/perl/weechat-perl-api.c @@ -437,6 +437,43 @@ API_FUNC(string_eval_expression) API_RETURN_STRING_FREE(result); } +API_FUNC(string_eval_path_home) +{ + char *path, *result; + struct t_hashtable *pointers, *extra_vars, *options; + dXSARGS; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + if (items < 4) + API_WRONG_ARGS(API_RETURN_EMPTY); + + path = SvPV_nolen (ST (0)); + pointers = weechat_perl_hash_to_hashtable (ST (1), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + extra_vars = weechat_perl_hash_to_hashtable (ST (2), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + options = weechat_perl_hash_to_hashtable (ST (3), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (path, pointers, extra_vars, + options); + + if (pointers) + weechat_hashtable_free (pointers); + if (extra_vars) + weechat_hashtable_free (extra_vars); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING_FREE(result); +} + API_FUNC(mkdir_home) { dXSARGS; @@ -4899,6 +4936,7 @@ weechat_perl_api_init (pTHX) API_DEF_FUNC(string_is_command_char); API_DEF_FUNC(string_input_for_buffer); API_DEF_FUNC(string_eval_expression); + API_DEF_FUNC(string_eval_path_home); API_DEF_FUNC(mkdir_home); API_DEF_FUNC(mkdir); API_DEF_FUNC(mkdir_parents); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index bf5484b7d..cee134d90 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -614,6 +614,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->string_match = &string_match; new_plugin->string_replace = &string_replace; new_plugin->string_expand_home = &string_expand_home; + new_plugin->string_eval_path_home = &string_eval_path_home; new_plugin->string_remove_quotes = &string_remove_quotes; new_plugin->string_strip = &string_strip; new_plugin->string_convert_escaped_chars = &string_convert_escaped_chars; diff --git a/src/plugins/python/weechat-python-api.c b/src/plugins/python/weechat-python-api.c index 02d3658cf..77f2195df 100644 --- a/src/plugins/python/weechat-python-api.c +++ b/src/plugins/python/weechat-python-api.c @@ -419,6 +419,45 @@ API_FUNC(string_eval_expression) API_RETURN_STRING_FREE(result); } +API_FUNC(string_eval_path_home) +{ + char *path, *result; + struct t_hashtable *pointers, *extra_vars, *options; + PyObject *dict, *dict2, *dict3, *return_value; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + path = NULL; + pointers = NULL; + extra_vars = NULL; + options = NULL; + if (!PyArg_ParseTuple (args, "sOOO", &path, &dict, &dict2, &dict3)) + API_WRONG_ARGS(API_RETURN_EMPTY); + pointers = weechat_python_dict_to_hashtable (dict, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + extra_vars = weechat_python_dict_to_hashtable (dict2, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + options = weechat_python_dict_to_hashtable (dict3, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (path, pointers, extra_vars, + options); + + if (pointers) + weechat_hashtable_free (pointers); + if (extra_vars) + weechat_hashtable_free (extra_vars); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING_FREE(result); +} + API_FUNC(mkdir_home) { char *directory; @@ -4880,6 +4919,7 @@ PyMethodDef weechat_python_funcs[] = API_DEF_FUNC(string_is_command_char), API_DEF_FUNC(string_input_for_buffer), API_DEF_FUNC(string_eval_expression), + API_DEF_FUNC(string_eval_path_home), API_DEF_FUNC(mkdir_home), API_DEF_FUNC(mkdir), API_DEF_FUNC(mkdir_parents), diff --git a/src/plugins/ruby/weechat-ruby-api.c b/src/plugins/ruby/weechat-ruby-api.c index 68eba9f5f..1cd26074f 100644 --- a/src/plugins/ruby/weechat-ruby-api.c +++ b/src/plugins/ruby/weechat-ruby-api.c @@ -508,6 +508,55 @@ weechat_ruby_api_string_eval_expression (VALUE class, VALUE expr, } static VALUE +weechat_ruby_api_string_eval_path_home (VALUE class, VALUE path, + VALUE pointers, VALUE extra_vars, + VALUE options) +{ + char *c_path, *result; + struct t_hashtable *c_pointers, *c_extra_vars, *c_options; + VALUE return_value; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + if (NIL_P (path) || NIL_P (pointers) || NIL_P (extra_vars) + || NIL_P (options)) + API_WRONG_ARGS(API_RETURN_EMPTY); + + Check_Type (path, T_STRING); + Check_Type (pointers, T_HASH); + Check_Type (extra_vars, T_HASH); + Check_Type (options, T_HASH); + + c_path = StringValuePtr (path); + c_pointers = weechat_ruby_hash_to_hashtable ( + pointers, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + c_extra_vars = weechat_ruby_hash_to_hashtable ( + extra_vars, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + c_options = weechat_ruby_hash_to_hashtable ( + options, + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (c_path, c_pointers, c_extra_vars, + c_options); + + if (c_pointers) + weechat_hashtable_free (c_pointers); + if (c_extra_vars) + weechat_hashtable_free (c_extra_vars); + if (c_options) + weechat_hashtable_free (c_options); + + API_RETURN_STRING_FREE(result); +} + +static VALUE weechat_ruby_api_mkdir_home (VALUE class, VALUE directory, VALUE mode) { char *c_directory; @@ -6005,6 +6054,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat) API_DEF_FUNC(string_is_command_char, 1); API_DEF_FUNC(string_input_for_buffer, 1); API_DEF_FUNC(string_eval_expression, 4); + API_DEF_FUNC(string_eval_path_home, 4); API_DEF_FUNC(mkdir_home, 2); API_DEF_FUNC(mkdir, 2); API_DEF_FUNC(mkdir_parents, 2); diff --git a/src/plugins/tcl/weechat-tcl-api.c b/src/plugins/tcl/weechat-tcl-api.c index 03b7ecfa5..851d423d1 100644 --- a/src/plugins/tcl/weechat-tcl-api.c +++ b/src/plugins/tcl/weechat-tcl-api.c @@ -574,6 +574,47 @@ API_FUNC(string_eval_expression) API_RETURN_STRING_FREE(result); } +API_FUNC(string_eval_path_home) +{ + Tcl_Obj *objp; + char *path, *result; + struct t_hashtable *pointers, *extra_vars, *options; + int i; + + API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY); + if (objc < 5) + API_WRONG_ARGS(API_RETURN_EMPTY); + + path = Tcl_GetStringFromObj (objv[1], &i); + pointers = weechat_tcl_dict_to_hashtable ( + interp, objv[2], + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_POINTER); + extra_vars = weechat_tcl_dict_to_hashtable ( + interp, objv[3], + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + options = weechat_tcl_dict_to_hashtable ( + interp, objv[4], + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + + result = weechat_string_eval_path_home (path, pointers, extra_vars, + options); + + if (pointers) + weechat_hashtable_free (pointers); + if (extra_vars) + weechat_hashtable_free (extra_vars); + if (options) + weechat_hashtable_free (options); + + API_RETURN_STRING_FREE(result); +} + API_FUNC(mkdir_home) { Tcl_Obj *objp; @@ -5356,6 +5397,7 @@ void weechat_tcl_api_init (Tcl_Interp *interp) API_DEF_FUNC(string_is_command_char); API_DEF_FUNC(string_input_for_buffer); API_DEF_FUNC(string_eval_expression); + API_DEF_FUNC(string_eval_path_home); API_DEF_FUNC(mkdir_home); API_DEF_FUNC(mkdir); API_DEF_FUNC(mkdir_parents); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 4f402f044..3987e3b15 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -57,7 +57,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20150114-01" +#define WEECHAT_PLUGIN_API_VERSION "20150624-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -276,6 +276,10 @@ struct t_weechat_plugin char *(*string_replace) (const char *string, const char *search, const char *replace); char *(*string_expand_home) (const char *path); + char *(*string_eval_path_home) (const char *path, + struct t_hashtable *pointers, + struct t_hashtable *extra_vars, + struct t_hashtable *options); char *(*string_remove_quotes) (const char *string, const char *quotes); char *(*string_strip) (const char *string, int left, int right, const char *chars); @@ -1040,6 +1044,10 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->string_replace)(__string, __search, __replace) #define weechat_string_expand_home(__path) \ (weechat_plugin->string_expand_home)(__path) +#define weechat_string_eval_path_home(__path, __pointers, \ + __extra_vars, __options) \ + (weechat_plugin->string_eval_path_home)(__path, __pointers, \ + __extra_vars, __options) #define weechat_string_remove_quotes(__string, __quotes) \ (weechat_plugin->string_remove_quotes)(__string, __quotes) #define weechat_string_strip(__string, __left, __right, __chars) \ diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp index 23533bea4..4171aa5cc 100644 --- a/tests/unit/core/test-string.cpp +++ b/tests/unit/core/test-string.cpp @@ -23,12 +23,18 @@ extern "C" { +#ifndef HAVE_CONFIG_H +#define HAVE_CONFIG_H +#endif +#include <stdlib.h> #include <stdio.h> #include <string.h> #include <regex.h> #include "tests/tests.h" +#include "src/core/weechat.h" #include "src/core/wee-string.h" #include "src/core/wee-hashtable.h" +#include "src/plugins/plugin.h" } #define ONE_KB 1000ULL @@ -331,7 +337,9 @@ TEST(String, ExpandHome) home = getenv ("HOME"); length_home = strlen (home); - result = string_expand_home("~/abc.txt"); + POINTERS_EQUAL(NULL, string_expand_home (NULL)); + + result = string_expand_home ("~/abc.txt"); CHECK(strncmp (result, home, length_home) == 0); LONGS_EQUAL(length_home + 8, strlen (result)); STRCMP_EQUAL(result + length_home, "/abc.txt"); @@ -340,6 +348,66 @@ TEST(String, ExpandHome) /* * Tests functions: + * string_eval_path_home + */ + +TEST(String, EvalPathHome) +{ + char *home, *result; + int length_home, length_weechat_home; + struct t_hashtable *extra_vars; + + home = getenv ("HOME"); + length_home = strlen (home); + + length_weechat_home = strlen (weechat_home); + + POINTERS_EQUAL(NULL, string_eval_path_home (NULL, NULL, NULL, NULL)); + + result = string_eval_path_home ("/tmp/test", NULL, NULL, NULL); + STRCMP_EQUAL(result, "/tmp/test"); + free (result); + + result = string_eval_path_home ("~/test", NULL, NULL, NULL); + CHECK(strncmp (result, home, length_home) == 0); + LONGS_EQUAL(length_home + 5, strlen (result)); + STRCMP_EQUAL(result + length_home, "/test"); + free (result); + + result = string_eval_path_home ("%h/test", NULL, NULL, NULL); + CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); + LONGS_EQUAL(length_weechat_home + 5, strlen (result)); + STRCMP_EQUAL(result + length_weechat_home, "/test"); + free (result); + + setenv ("WEECHAT_TEST_PATH", "path1", 1); + + result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/path2", + NULL, NULL, NULL); + CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); + LONGS_EQUAL(length_weechat_home + 12, strlen (result)); + STRCMP_EQUAL(result + length_weechat_home, "/path1/path2"); + free (result); + + extra_vars = hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, NULL); + CHECK(extra_vars); + hashtable_set (extra_vars, "path2", "value"); + + result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/${path2}", + NULL, extra_vars, NULL); + CHECK(strncmp (result, weechat_home, length_weechat_home) == 0); + LONGS_EQUAL(length_weechat_home + 12, strlen (result)); + STRCMP_EQUAL(result + length_weechat_home, "/path1/value"); + free (result); + + hashtable_free (extra_vars); +} + +/* + * Tests functions: * string_remove_quotes */ |