Add function "string_expand_home" in plugin API, fix bug with replacement of home in paths
This commit is contained in:
parent
1836b40a4a
commit
4616ca981e
@ -1,7 +1,7 @@
|
||||
WeeChat ChangeLog
|
||||
=================
|
||||
FlashCode <flashcode@flashtux.org>
|
||||
v0.3.3-dev, 2010-05-01
|
||||
v0.3.3-dev, 2010-05-02
|
||||
|
||||
|
||||
Version 0.3.3 (under dev!)
|
||||
@ -11,6 +11,8 @@ Version 0.3.3 (under dev!)
|
||||
(default: ctrl/alt + underscore) (task #9483)
|
||||
* core: fix crash with hook_process (when timer is called on a deleted hook
|
||||
process)
|
||||
* api: add function "string_expand_home", fix bug with replacement of home in
|
||||
paths
|
||||
* irc: add new options irc.network.autoreconnect_delay_growing and
|
||||
irc.network.autoreconnect_delay_max (task #10338)
|
||||
* irc: add new option weechat.color.status_name_ssl (task #10339)
|
||||
|
@ -728,6 +728,38 @@ char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_expand_home
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Replace leading `~` by string with home directory. If string does not start
|
||||
with `~`, then same string is returned.
|
||||
|
||||
Prototype:
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *weechat_string_expand_home (const char *path);
|
||||
----------------------------------------
|
||||
|
||||
Arguments:
|
||||
|
||||
* 'path': path
|
||||
|
||||
Return value:
|
||||
|
||||
* path with leading `~` replaced by home directory (must be freed by calling
|
||||
"free" after use)
|
||||
|
||||
C example:
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *str = weechat_string_expand_home ("~/file.txt");
|
||||
/* result: "/home/xxx/file.txt" */
|
||||
/* ... */
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_remove_quotes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -735,6 +735,38 @@ char *str = weechat_string_replace ("test, test", "s", "x"); /* résultat : "tex
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_expand_home
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Remplace le `~` en début de chaîne par le répertoire "home". Si la chaîne ne
|
||||
débute pas par `~`, alors une chaîne identique est retournée.
|
||||
|
||||
Prototype :
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *weechat_string_expand_home (const char *path);
|
||||
----------------------------------------
|
||||
|
||||
Paramètres :
|
||||
|
||||
* 'path': chemin
|
||||
|
||||
Valeur de retour :
|
||||
|
||||
* chemin avec le `~` en début remplacé par le répertoire "home" (doit être
|
||||
libéré par un appel à "free" après utilisation)
|
||||
|
||||
Exemple en C :
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *str = weechat_string_expand_home ("~/fichier.txt");
|
||||
/* résultat: "/home/xxx/fichier.txt" */
|
||||
/* ... */
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_remove_quotes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -747,6 +747,38 @@ char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_expand_home
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Replace leading `~` by string with home directory. If string does not start
|
||||
with `~`, then same string is returned.
|
||||
|
||||
Prototype:
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *weechat_string_expand_home (const char *path);
|
||||
----------------------------------------
|
||||
|
||||
Arguments:
|
||||
|
||||
* 'path': path
|
||||
|
||||
Return value:
|
||||
|
||||
* path with leading `~` replaced by home directory (must be freed by calling
|
||||
"free" after use)
|
||||
|
||||
C example:
|
||||
|
||||
[source,C]
|
||||
----------------------------------------
|
||||
char *str = weechat_string_expand_home ("~/file.txt");
|
||||
/* result: "/home/xxx/file.txt" */
|
||||
/* ... */
|
||||
free (str);
|
||||
----------------------------------------
|
||||
|
||||
weechat_string_remove_quotes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -3688,8 +3688,7 @@ command_upgrade (void *data, struct t_gui_buffer *buffer,
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
ptr_binary = string_replace (argv_eol[1], "~", getenv ("HOME"));
|
||||
|
||||
ptr_binary = string_expand_home (argv_eol[1]);
|
||||
if (ptr_binary)
|
||||
{
|
||||
/* check if weechat binary is here and executable by user */
|
||||
|
@ -79,8 +79,7 @@ network_init ()
|
||||
gnutls_global_init ();
|
||||
gnutls_certificate_allocate_credentials (&gnutls_xcred);
|
||||
|
||||
ca_path = string_replace (CONFIG_STRING(config_network_gnutls_ca_file),
|
||||
"~", getenv ("HOME"));
|
||||
ca_path = string_expand_home (CONFIG_STRING(config_network_gnutls_ca_file));
|
||||
if (ca_path)
|
||||
{
|
||||
ca_path2 = string_replace (ca_path, "%h", weechat_home);
|
||||
|
@ -395,6 +395,36 @@ string_replace (const char *string, const char *search, const char *replace)
|
||||
return new_string;
|
||||
}
|
||||
|
||||
/*
|
||||
* string_expand_home: expand home in a PATH
|
||||
* (for example: "~/file.txt" => "/home/xxx/file.txt")
|
||||
* note: returned value has to be free() after use
|
||||
*/
|
||||
|
||||
char *
|
||||
string_expand_home (const char *path)
|
||||
{
|
||||
char *ptr_home, *str;
|
||||
int length;
|
||||
|
||||
if (!path)
|
||||
return NULL;
|
||||
|
||||
if (!path[0] || (path[0] != '~') || (path[1] != DIR_SEPARATOR_CHAR))
|
||||
return strdup (path);
|
||||
|
||||
ptr_home = getenv ("HOME");
|
||||
|
||||
length = strlen (ptr_home) + strlen (path + 1) + 1;
|
||||
str = malloc (length);
|
||||
if (!str)
|
||||
return strdup (path);
|
||||
|
||||
snprintf (str, length, "%s%s", ptr_home, path + 1);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/*
|
||||
* string_remove_quotes: remove quotes at beginning/end of string
|
||||
* (ignore spaces if there are before first quote or
|
||||
|
@ -35,6 +35,7 @@ extern int string_match (const char *string, const char *mask,
|
||||
int case_sensitive);
|
||||
extern char *string_replace (const char *string, const char *search,
|
||||
const char *replace);
|
||||
extern char *string_expand_home (const char *path);
|
||||
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);
|
||||
|
@ -2572,8 +2572,8 @@ irc_server_gnutls_callback (void *data, gnutls_session_t tls_session,
|
||||
weechat_dir = weechat_info_get ("weechat_dir", "");
|
||||
cert_path1 = weechat_string_replace (cert_path0, "%h", weechat_dir);
|
||||
cert_path2 = (cert_path1) ?
|
||||
weechat_string_replace (cert_path1, "~", getenv ("HOME")) : NULL;
|
||||
|
||||
weechat_string_expand_home (cert_path1) : NULL;
|
||||
|
||||
if (cert_path2)
|
||||
{
|
||||
cert_str = weechat_file_get_content (cert_path2);
|
||||
|
@ -82,8 +82,7 @@ logger_get_file_path ()
|
||||
goto end;
|
||||
|
||||
/* replace "~" with user home */
|
||||
file_path = weechat_string_replace (weechat_config_string (logger_config_file_path),
|
||||
"~", getenv ("HOME"));
|
||||
file_path = weechat_string_expand_home (weechat_config_string (logger_config_file_path));
|
||||
if (!file_path)
|
||||
goto end;
|
||||
|
||||
|
@ -234,7 +234,7 @@ plugin_find_pos (struct t_weechat_plugin *plugin)
|
||||
struct t_weechat_plugin *
|
||||
plugin_load (const char *filename)
|
||||
{
|
||||
char *ptr_home, *full_name, *full_name2;
|
||||
char *full_name, *full_name2;
|
||||
void *handle;
|
||||
char *name, *api_version, *author, *description, *version;
|
||||
char *license, *charset;
|
||||
@ -260,9 +260,7 @@ plugin_load (const char *filename)
|
||||
if (plugin_autoload_array && !plugin_check_autoload (full_name))
|
||||
return NULL;
|
||||
|
||||
ptr_home = getenv ("HOME");
|
||||
full_name2 = string_replace (full_name, "~", ptr_home);
|
||||
|
||||
full_name2 = string_expand_home (full_name);
|
||||
if (full_name2)
|
||||
{
|
||||
free (full_name);
|
||||
@ -462,6 +460,7 @@ plugin_load (const char *filename)
|
||||
new_plugin->strcasestr = &string_strcasestr;
|
||||
new_plugin->string_match = &string_match;
|
||||
new_plugin->string_replace = &string_replace;
|
||||
new_plugin->string_expand_home = &string_expand_home;
|
||||
new_plugin->string_remove_quotes = &string_remove_quotes;
|
||||
new_plugin->string_strip = &string_strip;
|
||||
new_plugin->string_has_highlight = &string_has_highlight;
|
||||
@ -780,7 +779,7 @@ plugin_auto_load_file (void *plugin, const char *filename)
|
||||
void
|
||||
plugin_auto_load ()
|
||||
{
|
||||
char *ptr_home, *dir_name, *plugin_path, *plugin_path2;
|
||||
char *dir_name, *plugin_path, *plugin_path2;
|
||||
|
||||
plugin_autoload_array = NULL;
|
||||
plugin_autoload_count = 0;
|
||||
@ -797,9 +796,7 @@ plugin_auto_load ()
|
||||
if (CONFIG_STRING(config_plugin_path)
|
||||
&& CONFIG_STRING(config_plugin_path)[0])
|
||||
{
|
||||
ptr_home = getenv ("HOME");
|
||||
plugin_path = string_replace (CONFIG_STRING(config_plugin_path),
|
||||
"~", ptr_home);
|
||||
plugin_path = string_expand_home (CONFIG_STRING(config_plugin_path));
|
||||
plugin_path2 = string_replace ((plugin_path) ?
|
||||
plugin_path : CONFIG_STRING(config_plugin_path),
|
||||
"%h", weechat_home);
|
||||
|
@ -482,19 +482,7 @@ script_search_path (struct t_weechat_plugin *weechat_plugin,
|
||||
struct stat st;
|
||||
|
||||
if (filename[0] == '~')
|
||||
{
|
||||
dir_home = getenv ("HOME");
|
||||
if (!dir_home)
|
||||
return NULL;
|
||||
length = strlen (dir_home) + strlen (filename + 1) + 1;
|
||||
final_name = malloc (length);
|
||||
if (final_name)
|
||||
{
|
||||
snprintf (final_name, length, "%s%s", dir_home, filename + 1);
|
||||
return final_name;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
return weechat_string_expand_home (filename);
|
||||
|
||||
dir_home = weechat_info_get ("weechat_dir", "");
|
||||
if (dir_home)
|
||||
|
@ -39,7 +39,7 @@ struct timeval;
|
||||
*/
|
||||
|
||||
/* API version (used to check that plugin has same API and can be loaded) */
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20100302-01"
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20100502-01"
|
||||
|
||||
/* macros for defining plugin infos */
|
||||
#define WEECHAT_PLUGIN_NAME(__name) \
|
||||
@ -134,7 +134,11 @@ struct t_weechat_plugin
|
||||
struct t_weechat_plugin *prev_plugin; /* link to previous plugin */
|
||||
struct t_weechat_plugin *next_plugin; /* link to next plugin */
|
||||
|
||||
/* plugin functions (API) */
|
||||
/*
|
||||
* plugin functions (API)
|
||||
* WeeChat developers: if you add functions in API, update value of
|
||||
* constant WEECHAT_PLUGIN_API_VERSION
|
||||
*/
|
||||
|
||||
/* plugins */
|
||||
const char *(*plugin_get_name) (struct t_weechat_plugin *plugin);
|
||||
@ -157,6 +161,7 @@ struct t_weechat_plugin
|
||||
int case_sensitive);
|
||||
char *(*string_replace) (const char *string, const char *search,
|
||||
const char *replace);
|
||||
char *(*string_expand_home) (const char *path);
|
||||
char *(*string_remove_quotes) (const char *string, const char *quotes);
|
||||
char *(*string_strip) (const char *string, int left, int right,
|
||||
const char *chars);
|
||||
@ -644,8 +649,6 @@ struct t_weechat_plugin
|
||||
struct t_infolist *infolist),
|
||||
void *callback_read_data);
|
||||
void (*upgrade_close) (struct t_upgrade_file *upgrade_file);
|
||||
|
||||
/* WeeChat developers: ALWAYS add new functions at the end */
|
||||
};
|
||||
|
||||
extern int weechat_plugin_init (struct t_weechat_plugin *plugin,
|
||||
@ -697,6 +700,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
|
||||
weechat_plugin->string_match(__string, __mask, __case_sensitive)
|
||||
#define weechat_string_replace(__string, __search, __replace) \
|
||||
weechat_plugin->string_replace(__string, __search, __replace)
|
||||
#define weechat_string_expand_home(__path) \
|
||||
weechat_plugin->string_expand_home(__path)
|
||||
#define weechat_string_remove_quotes(__string, __quotes) \
|
||||
weechat_plugin->string_remove_quotes(__string, __quotes)
|
||||
#define weechat_string_strip(__string, __left, __right, __chars) \
|
||||
|
@ -84,9 +84,7 @@ xfer_file_find_filename (struct t_xfer *xfer)
|
||||
if (!XFER_IS_FILE(xfer->type))
|
||||
return;
|
||||
|
||||
dir1 = weechat_string_replace (weechat_config_string (xfer_config_file_download_path),
|
||||
"~",
|
||||
getenv ("HOME"));
|
||||
dir1 = weechat_string_expand_home (weechat_config_string (xfer_config_file_download_path));
|
||||
if (!dir1)
|
||||
return;
|
||||
|
||||
|
@ -134,8 +134,7 @@ xfer_create_directories ()
|
||||
weechat_dir = weechat_info_get ("weechat_dir", "");
|
||||
if (weechat_dir)
|
||||
{
|
||||
dir1 = weechat_string_replace (weechat_config_string (xfer_config_file_download_path),
|
||||
"~", getenv ("HOME"));
|
||||
dir1 = weechat_string_expand_home (weechat_config_string (xfer_config_file_download_path));
|
||||
dir2 = weechat_string_replace (dir1, "%h", weechat_dir);
|
||||
if (dir2)
|
||||
(void) weechat_mkdir (dir2, 0700);
|
||||
@ -817,12 +816,10 @@ xfer_add_cb (void *data, const char *signal, const char *type_data,
|
||||
if (filename[0] == '/')
|
||||
filename2 = strdup (filename);
|
||||
else if (filename[0] == '~')
|
||||
filename2 = weechat_string_replace (filename, "~", getenv ("HOME"));
|
||||
filename2 = weechat_string_expand_home (filename);
|
||||
else
|
||||
{
|
||||
dir1 = weechat_string_replace (weechat_config_string (xfer_config_file_upload_path),
|
||||
"~",
|
||||
getenv ("HOME"));
|
||||
dir1 = weechat_string_expand_home (weechat_config_string (xfer_config_file_upload_path));
|
||||
if (!dir1)
|
||||
{
|
||||
weechat_printf (NULL,
|
||||
|
Loading…
x
Reference in New Issue
Block a user