From a08520a33d248d07038b93f9e0a74afdd8d24022 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Tue, 14 Aug 2012 11:45:11 +0200 Subject: [PATCH] Implement cmd_exec with --- commands.c | 22 ++++++++++++++++++++++ commands.h | 9 +++++++++ config.c | 1 + utils.c | 42 ++++++++++++++++++++++++++++++++++++++++++ utils.h | 12 ++++++++++++ 5 files changed, 86 insertions(+) diff --git a/commands.c b/commands.c index c8b8d3c..5d568f7 100644 --- a/commands.c +++ b/commands.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -487,6 +488,27 @@ error_ret: return true; } +bool +cmd_exec(girara_session_t* session, girara_list_t* argument_list) +{ + g_return_val_if_fail(session != NULL, false); + g_return_val_if_fail(session->global.data != NULL, false); + zathura_t* zathura = session->global.data; + + if (zathura->document != NULL) { + const char* path = zathura_document_get_path(zathura->document); + + GIRARA_LIST_FOREACH(argument_list, char*, iter, value) + char* r = NULL; + if ((r = replace_substring(value, "$FILE", path)) != NULL) { + girara_list_iterator_set(iter, r); + } + GIRARA_LIST_FOREACH_END(argument_list, char*, iter, value); + } + + return girara_exec_with_argument_list(session, argument_list); +} + bool cmd_offset(girara_session_t* session, girara_list_t* argument_list) { diff --git a/commands.h b/commands.h index e22fa86..1327068 100644 --- a/commands.h +++ b/commands.h @@ -142,6 +142,15 @@ bool cmd_search(girara_session_t* session, const char* input, girara_argument_t* */ bool cmd_export(girara_session_t* session, girara_list_t* argument_list); +/** + * Execute command + * + * @param session The used girara session + * @param argument_list List of passed arguments + * @return true if no error occured + */ +bool cmd_exec(girara_session_t* session, girara_list_t* argument_list); + /** * Set page offset * diff --git a/config.c b/config.c index 99443b3..401472f 100644 --- a/config.c +++ b/config.c @@ -293,6 +293,7 @@ config_load_default(zathura_t* zathura) girara_inputbar_command_add(gsession, "blist", NULL, cmd_bookmark_open, cc_bookmarks, _("List all bookmarks")); girara_inputbar_command_add(gsession, "close", NULL, cmd_close, NULL, _("Close current file")); girara_inputbar_command_add(gsession, "info", NULL, cmd_info, NULL, _("Show file information")); + girara_inputbar_command_add(gsession, "exec", NULL, cmd_exec, NULL, _("Execute a command")); girara_inputbar_command_add(gsession, "help", NULL, cmd_help, NULL, _("Show help")); girara_inputbar_command_add(gsession, "open", "o", cmd_open, cc_open, _("Open document")); girara_inputbar_command_add(gsession, "quit", "q", cmd_quit, NULL, _("Close zathura")); diff --git a/utils.c b/utils.c index 724b591..dc765c3 100644 --- a/utils.c +++ b/utils.c @@ -393,3 +393,45 @@ zathura_get_version_string(zathura_t* zathura, bool markup) return version; } + +char* +replace_substring(const char* string, const char* old, const char* new) +{ + if (string == NULL || old == NULL || new == NULL) { + return NULL; + } + + size_t old_len = strlen(old); + size_t new_len = strlen(new); + + /* count occurences */ + unsigned int count = 0; + unsigned int i = 0; + + for (i = 0; string[i] != '\0'; i++) { + if (strstr(&string[i], old) == &string[i]) { + i += (old_len - 1); + count++; + } + } + + if (count == 0) { + return NULL; + } + + char* ret = g_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1)); + i = 0; + + /* replace */ + while (*string != '\0') { + if (strstr(string, old) == string) { + strcpy(&ret[i], new); + i += new_len; + string += old_len; + } else { + ret[i++] = *string++; + } + } + + return ret; +} diff --git a/utils.h b/utils.h index 2fffab8..b531731 100644 --- a/utils.h +++ b/utils.h @@ -140,4 +140,16 @@ void document_draw_search_results(zathura_t* zathura, bool value); */ char* zathura_get_version_string(zathura_t* zathura, bool markup); +/** + * Replaces all occurences of \ref old in \ref string with \ref new and returns + * a new allocated string + * + * @param string The original string + * @param old String to replace + * @param new Replacement string + * + * @return new allocated string + */ +char* replace_substring(const char* string, const char* old, const char* new); + #endif // UTILS_H