From eca73909515a067725092f4464a12ad418e4b69a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 17 Oct 2015 22:58:45 +0100 Subject: [PATCH] Implemented /script show --- src/command/commands.c | 4 ++++ src/config/scripts.c | 40 +++++++++++++++++++++++++++++++++++- src/config/scripts.h | 1 + src/ui/console.c | 18 ++++++++++++++++ src/ui/ui.h | 1 + tests/unittests/ui/stub_ui.c | 1 + 6 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/command/commands.c b/src/command/commands.c index 58f2a9b2..808f573e 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -702,6 +702,10 @@ cmd_script(ProfWin *window, const char * const command, gchar **args) GSList *scripts = scripts_list(); cons_show_scripts(scripts); g_slist_free_full(scripts, g_free); + } else if ((g_strcmp0(args[0], "show") == 0) && args[1]) { + GSList *commands = scripts_read(args[1]); + cons_show_script(args[1], commands); + g_slist_free_full(commands, g_free); } else { cons_bad_cmd_usage(command); } diff --git a/src/config/scripts.c b/src/config/scripts.c index 916e292c..7d7caa4d 100644 --- a/src/config/scripts.c +++ b/src/config/scripts.c @@ -94,6 +94,44 @@ scripts_list(void) return result; } +GSList* +scripts_read(const char *const script) +{ + gchar *data_home = xdg_get_data_home(); + GString *scriptpath = g_string_new(data_home); + free(data_home); + + g_string_append(scriptpath, "/profanity/scripts/"); + g_string_append(scriptpath, script); + + FILE *scriptfile = g_fopen(scriptpath->str, "r"); + if (!scriptfile) { + log_info("Script not found: %s", scriptpath->str); + g_string_free(scriptpath, TRUE); + return NULL; + } + + g_string_free(scriptpath, TRUE); + + char *line = NULL; + size_t len = 0; + ssize_t read; + GSList *result = NULL; + + while ((read = getline(&line, &len, scriptfile)) != -1) { + if (g_str_has_suffix(line, "\n")) { + result = g_slist_append(result, g_strndup(line, strlen(line) -1)); + } else { + result = g_slist_append(result, strdup(line)); + } + } + + fclose(scriptfile); + if (line) free(line); + + return result; +} + gboolean scripts_exec(const char *const script) { @@ -113,7 +151,7 @@ scripts_exec(const char *const script) g_string_free(scriptpath, TRUE); - char * line = NULL; + char *line = NULL; size_t len = 0; ssize_t read; diff --git a/src/config/scripts.h b/src/config/scripts.h index 9b56fac4..7ad19974 100644 --- a/src/config/scripts.h +++ b/src/config/scripts.h @@ -36,4 +36,5 @@ void scripts_init(void); GSList* scripts_list(void); +GSList* scripts_read(const char *const script); gboolean scripts_exec(const char *const script); diff --git a/src/ui/console.c b/src/ui/console.c index 2388e79b..37316d83 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -1599,6 +1599,24 @@ cons_show_scripts(GSList *scripts) cons_alert(); } +void +cons_show_script(const char *const script, GSList *commands) +{ + cons_show(""); + + if (commands == NULL) { + cons_show("Script not found: %s", script); + } else { + cons_show("%s:", script); + while (commands) { + cons_show(" %s", commands->data); + commands = g_slist_next(commands); + } + } + + cons_alert(); +} + void cons_prefs(void) { diff --git a/src/ui/ui.h b/src/ui/ui.h index 1a534012..b7c7a8ae 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -279,6 +279,7 @@ void cons_show_info(PContact pcontact); void cons_show_caps(const char * const fulljid, resource_presence_t presence); void cons_show_themes(GSList *themes); void cons_show_scripts(GSList *scripts); +void cons_show_script(const char *const script, GSList *commands); void cons_show_aliases(GList *aliases); void cons_show_login_success(ProfAccount *account, int secured); void cons_show_software_version(const char * const jid, diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c index ae959740..99fe3007 100644 --- a/tests/unittests/ui/stub_ui.c +++ b/tests/unittests/ui/stub_ui.c @@ -416,6 +416,7 @@ void cons_show_info(PContact pcontact) {} void cons_show_caps(const char * const fulljid, resource_presence_t presence) {} void cons_show_themes(GSList *themes) {} void cons_show_scripts(GSList *scripts) {} +void cons_show_script(const char *const script, GSList *commands) {} void cons_show_aliases(GList *aliases) {