1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 01:03:14 +02:00

api: add function string_eval_path_home()

This commit is contained in:
Sébastien Helleu
2015-06-24 07:54:42 +02:00
parent 6668b9869b
commit 2bd2d74a07
23 changed files with 676 additions and 8 deletions
+58
View File
@@ -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"
@@ -499,6 +500,63 @@ string_expand_home (const char *path)
return str;
}
/*
* 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).
+4
View File
@@ -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);