From a9fab9ed2dc75671e61a6d44ba0ea5c861eeb5dc Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 5 Feb 2017 18:54:33 +0000 Subject: [PATCH] Allow unloading all plugins --- src/command/cmd_defs.c | 11 +- src/command/cmd_funcs.c | 242 ++++++++++++++++++++++------------------ src/command/cmd_funcs.h | 7 ++ src/plugins/plugins.c | 21 ++++ src/plugins/plugins.h | 1 + 5 files changed, 168 insertions(+), 114 deletions(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index fb33dac9..eb5eca01 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2047,13 +2047,18 @@ static struct cmd_t command_defs[] = { "/plugins", parse_args, 0, 2, NULL, - CMD_NOSUBFUNCS + CMD_SUBFUNCS( + { "install", cmd_plugins_install }, + { "load", cmd_plugins_load }, + { "unload", cmd_plugins_unload }, + { "reload", cmd_plugins_reload }, + { "python_version", cmd_plugins_python_version }) CMD_MAINFUNC(cmd_plugins) CMD_NOTAGS CMD_SYN( "/plugins", "/plugins install ", - "/plugins unload ", + "/plugins unload []", "/plugins load ", "/plugins reload []", "/plugins python_version") @@ -2062,7 +2067,7 @@ static struct cmd_t command_defs[] = CMD_ARGS( { "install ", "Install file to plugins directory, and load or reload the plugin." }, { "load ", "Load a plugin that already exists in the plugin directory." }, - { "unload ", "Unload a loaded plugin." }, + { "unload []", "Unload a loaded plugin, passing no argument will unload all plugins." }, { "reload []", "Reload a plugin, passing no argument will reload all plugins." }, { "python_version", "Show the Python interpreter version." }) CMD_EXAMPLES( diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index b8657e59..6f238612 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -6212,120 +6212,140 @@ cmd_xa(ProfWin *window, const char *const command, gchar **args) } gboolean -cmd_plugins(ProfWin *window, const char *const command, gchar **args) +cmd_plugins_install(ProfWin *window, const char *const command, gchar **args) { - if (g_strcmp0(args[0], "install") == 0) { - char *filename = args[1]; - if (filename == NULL) { - cons_bad_cmd_usage(command); - return TRUE; - } - - // expand ~ to $HOME - if (filename[0] == '~' && filename[1] == '/') { - if (asprintf(&filename, "%s/%s", getenv("HOME"), filename+2) == -1) { - return TRUE; - } - } else { - filename = strdup(filename); - } - - if (access(filename, R_OK) != 0) { - cons_show("File not found: %s", filename); - free(filename); - return TRUE; - } - - if (!is_regular_file(filename)) { - cons_show("Not a file: %s", filename); - free(filename); - return TRUE; - } - - if (!g_str_has_suffix(filename, ".py") && !g_str_has_suffix(filename, ".so")) { - cons_show("Plugins must have one of the following extensions: '.py' '.so'"); - free(filename); - return TRUE; - } - - gchar *plugin_name = g_path_get_basename(filename); - gboolean result = plugins_install(plugin_name, filename); - if (result) { - cons_show("Plugin installed: %s", plugin_name); - } else { - cons_show("Failed to install plugin: %s", plugin_name); - } - g_free(plugin_name); - - free(filename); - return TRUE; - } else if (g_strcmp0(args[0], "load") == 0) { - if (args[1] == NULL) { - cons_bad_cmd_usage(command); - return TRUE; - } - gboolean res = plugins_load(args[1]); - if (res) { - cons_show("Loaded plugin: %s", args[1]); - } else { - cons_show("Failed to load plugin: %s", args[1]); - } - - return TRUE; - } else if (g_strcmp0(args[0], "unload") == 0) { - if (args[1] == NULL) { - cons_bad_cmd_usage(command); - return TRUE; - } - gboolean res = plugins_unload(args[1]); - if (res) { - cons_show("Unloaded plugin: %s", args[1]); - } else { - cons_show("Failed to unload plugin: %s", args[1]); - } - - return TRUE; - } else if (g_strcmp0(args[0], "reload") == 0) { - if (args[1] == NULL) { - plugins_reload_all(); - cons_show("Reloaded all plugins"); - } else { - gboolean res = plugins_reload(args[1]); - if (res) { - cons_show("Reloaded plugin: %s", args[1]); - } else { - cons_show("Failed to reload plugin: %s", args[1]); - } - } - - return TRUE; - } else if (g_strcmp0(args[0], "python_version") == 0) { -#ifdef HAVE_PYTHON - const char *version = python_get_version(); - cons_show("Python version:"); - cons_show("%s", version); -#else - cons_show("This build does not support pytyon plugins."); -#endif - return TRUE; - - } else { - GList *plugins = plugins_loaded_list(); - if (plugins == NULL) { - cons_show("No plugins installed."); - return TRUE; - } - - GList *curr = plugins; - cons_show("Installed plugins:"); - while (curr) { - cons_show(" %s", curr->data); - curr = g_list_next(curr); - } - g_list_free(plugins); - + char *filename = args[1]; + if (filename == NULL) { + cons_bad_cmd_usage(command); return TRUE; } + + // expand ~ to $HOME + if (filename[0] == '~' && filename[1] == '/') { + if (asprintf(&filename, "%s/%s", getenv("HOME"), filename+2) == -1) { + return TRUE; + } + } else { + filename = strdup(filename); + } + + if (access(filename, R_OK) != 0) { + cons_show("File not found: %s", filename); + free(filename); + return TRUE; + } + + if (!is_regular_file(filename)) { + cons_show("Not a file: %s", filename); + free(filename); + return TRUE; + } + + if (!g_str_has_suffix(filename, ".py") && !g_str_has_suffix(filename, ".so")) { + cons_show("Plugins must have one of the following extensions: '.py' '.so'"); + free(filename); + return TRUE; + } + + gchar *plugin_name = g_path_get_basename(filename); + gboolean result = plugins_install(plugin_name, filename); + if (result) { + cons_show("Plugin installed: %s", plugin_name); + } else { + cons_show("Failed to install plugin: %s", plugin_name); + } + g_free(plugin_name); + + free(filename); + return TRUE; +} + +gboolean +cmd_plugins_load(ProfWin *window, const char *const command, gchar **args) +{ + if (args[1] == NULL) { + cons_bad_cmd_usage(command); + return TRUE; + } + gboolean res = plugins_load(args[1]); + if (res) { + cons_show("Loaded plugin: %s", args[1]); + } else { + cons_show("Failed to load plugin: %s", args[1]); + } + + return TRUE; +} + +gboolean +cmd_plugins_unload(ProfWin *window, const char *const command, gchar **args) +{ + if (args[1] == NULL) { + plugins_unload_all(); + cons_show("Unloaded all plugins"); + return TRUE; + } + + gboolean res = plugins_unload(args[1]); + if (res) { + cons_show("Unloaded plugin: %s", args[1]); + } else { + cons_show("Failed to unload plugin: %s", args[1]); + } + + return TRUE; +} + +gboolean +cmd_plugins_reload(ProfWin *window, const char *const command, gchar **args) +{ + if (args[1] == NULL) { + plugins_reload_all(); + cons_show("Reloaded all plugins"); + return TRUE; + } + + gboolean res = plugins_reload(args[1]); + if (res) { + cons_show("Reloaded plugin: %s", args[1]); + } else { + cons_show("Failed to reload plugin: %s", args[1]); + } + + return TRUE; +} + +gboolean +cmd_plugins_python_version(ProfWin *window, const char *const command, gchar **args) +{ +#ifdef HAVE_PYTHON + const char *version = python_get_version(); + cons_show("Python version:"); + cons_show("%s", version); +#else + cons_show("This build does not support pytyon plugins."); +#endif + return TRUE; +} + +gboolean +cmd_plugins(ProfWin *window, const char *const command, gchar **args) +{ + GList *plugins = plugins_loaded_list(); + if (plugins == NULL) { + cons_show("No plugins installed."); + return TRUE; + } + + GList *curr = plugins; + cons_show("Installed plugins:"); + while (curr) { + cons_show(" %s", curr->data); + curr = g_list_next(curr); + } + g_list_free(plugins); + + return TRUE; } gboolean diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h index b16c187a..c7a89e43 100644 --- a/src/command/cmd_funcs.h +++ b/src/command/cmd_funcs.h @@ -158,7 +158,14 @@ gboolean cmd_script(ProfWin *window, const char *const command, gchar **args); gboolean cmd_export(ProfWin *window, const char *const command, gchar **args); gboolean cmd_charset(ProfWin *window, const char *const command, gchar **args); gboolean cmd_console(ProfWin *window, const char *const command, gchar **args); + gboolean cmd_plugins(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_plugins_install(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_plugins_load(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_plugins_unload(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_plugins_reload(ProfWin *window, const char *const command, gchar **args); +gboolean cmd_plugins_python_version(ProfWin *window, const char *const command, gchar **args); + gboolean cmd_blocked(ProfWin *window, const char *const command, gchar **args); gboolean cmd_account(ProfWin *window, const char *const command, gchar **args); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 5b4de8a0..c9e37c15 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -190,6 +190,27 @@ plugins_load(const char *const name) } } +void +plugins_unload_all(void) +{ + GList *plugin_names = g_hash_table_get_keys(plugins); + GList *plugin_names_dup = NULL; + GList *curr = plugin_names; + while (curr) { + plugin_names_dup = g_list_append(plugin_names_dup, strdup(curr->data)); + curr = g_list_next(curr); + } + g_list_free(plugin_names); + + curr = plugin_names_dup; + while (curr) { + plugins_unload(curr->data); + curr = g_list_next(curr); + } + + g_list_free_full(plugin_names_dup, free); +} + gboolean plugins_unload(const char *const name) { diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index a899bde3..8762556e 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -110,6 +110,7 @@ void plugins_shutdown(void); gboolean plugins_install(const char *const plugin_name, const char *const filename); gboolean plugins_load(const char *const name); gboolean plugins_unload(const char *const name); +void plugins_unload_all(void); gboolean plugins_reload(const char *const name); void plugins_reload_all(void);