1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 04:46:37 +02:00

scripts: add "eval" option in script commands and info "xxx_eval" (issue #128)

For now this works only in python, perl, ruby and guile.
This commit is contained in:
Sébastien Helleu
2017-12-28 17:21:51 +01:00
parent 4884ee6634
commit 414959a474
12 changed files with 1956 additions and 526 deletions
+128 -14
View File
@@ -39,7 +39,15 @@ WEECHAT_PLUGIN_PRIORITY(4000);
struct t_weechat_plugin *weechat_php_plugin;
int php_quiet;
int php_quiet = 0;
struct t_plugin_script *php_script_eval = NULL;
int php_eval_mode = 0;
int php_eval_send_input = 0;
int php_eval_exec_commands = 0;
struct t_gui_buffer *php_eval_buffer = NULL;
char *php_eval_output = NULL;
struct t_plugin_script *php_scripts = NULL;
struct t_plugin_script *last_php_script = NULL;
struct t_plugin_script *php_current_script = NULL;
@@ -577,7 +585,15 @@ weechat_php_exec (struct t_plugin_script *script, int ret_type,
}
else
{
WEECHAT_SCRIPT_MSG_WRONG_ARGS(PHP_CURRENT_SCRIPT_NAME, function);
if (ret_type != WEECHAT_SCRIPT_EXEC_IGNORE)
{
weechat_printf (NULL,
weechat_gettext ("%s%s: function \"%s\" "
"must return a valid "
"value"),
weechat_prefix ("error"), PHP_PLUGIN_NAME,
function);
}
}
}
else
@@ -589,6 +605,13 @@ weechat_php_exec (struct t_plugin_script *script, int ret_type,
}
zend_end_try ();
if ((ret_type != WEECHAT_SCRIPT_EXEC_IGNORE) && !ret_value)
{
weechat_printf (NULL,
weechat_gettext ("%s%s: error in function \"%s\""),
weechat_prefix ("error"), PHP_PLUGIN_NAME, function);
}
/* Cleanup */
if (params)
{
@@ -608,13 +631,14 @@ weechat_php_exec (struct t_plugin_script *script, int ret_type,
/*
* Loads a PHP script.
*
* Returns:
* 1: OK
* 0: error
* If code is NULL, the content of filename is read and executed.
* If code is not NULL, it is executed (the file is not read).
*
* Returns pointer to new registered script, NULL if error.
*/
int
weechat_php_load (const char *filename)
struct t_plugin_script *
weechat_php_load (const char *filename, const char *code)
{
zend_file_handle file_handle;
@@ -647,7 +671,7 @@ weechat_php_load (const char *filename)
weechat_gettext ("%s%s: function \"register\" not "
"found (or failed) in file \"%s\""),
weechat_prefix ("error"), PHP_PLUGIN_NAME, filename);
return 0;
return NULL;
}
php_current_script = php_registered_script;
@@ -661,7 +685,7 @@ weechat_php_load (const char *filename)
WEECHAT_HOOK_SIGNAL_STRING,
php_current_script->filename);
return 1;
return php_current_script;
}
/*
@@ -674,7 +698,7 @@ weechat_php_load_cb (void *data, const char *filename)
/* make C compiler happy */
(void) data;
weechat_php_load (filename);
weechat_php_load (filename, NULL);
}
/*
@@ -782,7 +806,7 @@ weechat_php_reload_name (const char *name)
weechat_gettext ("%s: script \"%s\" unloaded"),
PHP_PLUGIN_NAME, name);
}
weechat_php_load (filename);
weechat_php_load (filename, NULL);
free (filename);
}
}
@@ -794,6 +818,27 @@ weechat_php_reload_name (const char *name)
}
}
/*
* Evaluates PHP code.
*
* Returns:
* 1: OK
* 0: error
*/
int
weechat_php_eval (struct t_gui_buffer *buffer, int send_to_buffer_as_input,
int exec_commands, const char *code)
{
/* TODO: implement PHP eval */
(void) buffer;
(void) send_to_buffer_as_input;
(void) exec_commands;
(void) code;
return 1;
}
/*
* Callback for command "/php".
*/
@@ -803,12 +848,12 @@ weechat_php_command_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
char *ptr_name, *path_script;
char *ptr_name, *ptr_code, *path_script;
int i, send_to_buffer_as_input, exec_commands;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) buffer;
if (argc == 1)
{
@@ -878,7 +923,8 @@ weechat_php_command_cb (const void *pointer, void *data,
/* load PHP script */
path_script = plugin_script_search_path (weechat_php_plugin,
ptr_name);
weechat_php_load ((path_script) ? path_script : ptr_name);
weechat_php_load ((path_script) ? path_script : ptr_name,
NULL);
if (path_script)
free (path_script);
}
@@ -894,6 +940,43 @@ weechat_php_command_cb (const void *pointer, void *data,
}
php_quiet = 0;
}
else if (weechat_strcasecmp (argv[1], "eval") == 0)
{
send_to_buffer_as_input = 0;
exec_commands = 0;
ptr_code = argv_eol[2];
for (i = 2; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (strcmp (argv[i], "-o") == 0)
{
if (i + 1 >= argc)
WEECHAT_COMMAND_ERROR;
send_to_buffer_as_input = 1;
exec_commands = 0;
ptr_code = argv_eol[i + 1];
}
else if (strcmp (argv[i], "-oc") == 0)
{
if (i + 1 >= argc)
WEECHAT_COMMAND_ERROR;
send_to_buffer_as_input = 1;
exec_commands = 1;
ptr_code = argv_eol[i + 1];
}
}
else
break;
}
if (!weechat_php_eval (buffer, send_to_buffer_as_input,
exec_commands, ptr_code))
WEECHAT_COMMAND_ERROR;
/* TODO: implement /php eval */
weechat_printf (NULL,
"%sCommand \"/php eval\" is not yet implemented",
weechat_prefix ("error"));
}
else
WEECHAT_COMMAND_ERROR;
}
@@ -939,6 +1022,28 @@ weechat_php_hdata_cb (const void *pointer, void *data,
hdata_name);
}
/*
* Returns PHP info "php_eval".
*/
const char *
weechat_php_info_eval_cb (const void *pointer, void *data,
const char *info_name,
const char *arguments)
{
static const char *not_implemented = "not yet implemented";
/* make C compiler happy */
(void) pointer;
(void) data;
(void) info_name;
(void) arguments;
return not_implemented;
return NULL;
}
/*
* Returns infolist with PHP scripts.
*/
@@ -1142,6 +1247,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
init.callback_command = &weechat_php_command_cb;
init.callback_completion = &weechat_php_completion_cb;
init.callback_hdata = &weechat_php_hdata_cb;
init.callback_info_eval = &weechat_php_info_eval_cb;
init.callback_infolist = &weechat_php_infolist_cb;
init.callback_signal_debug_dump = &weechat_php_signal_debug_dump_cb;
init.callback_signal_script_action = &weechat_php_signal_script_action_cb;
@@ -1178,6 +1284,11 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
/* unload all scripts */
php_quiet = 1;
plugin_script_end (plugin, &php_scripts, &weechat_php_unload_all);
if (php_script_eval)
{
weechat_php_unload (php_script_eval);
php_script_eval = NULL;
}
php_quiet = 0;
if (weechat_php_func_map)
@@ -1195,6 +1306,9 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
free (php_action_remove_list);
if (php_action_autoload_list)
free (php_action_autoload_list);
/* weechat_string_dyn_free (php_buffer_output, 1); */
if (php_eval_output)
free (php_eval_output);
return WEECHAT_RC_OK;
}