diff options
Diffstat (limited to 'src/config/scripts.c')
-rw-r--r-- | src/config/scripts.c | 40 |
1 files changed, 39 insertions, 1 deletions
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; |