Implemented /script show

This commit is contained in:
James Booth
2015-10-17 22:58:45 +01:00
parent a35cbea732
commit eca7390951
6 changed files with 64 additions and 1 deletions

View File

@@ -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;