Implement cmd_exec with

This commit is contained in:
Moritz Lipp 2012-08-14 11:45:11 +02:00
parent 65bd545222
commit a08520a33d
5 changed files with 86 additions and 0 deletions

View file

@ -21,6 +21,7 @@
#include <girara/session.h>
#include <girara/settings.h>
#include <girara/commands.h>
#include <girara/datastructures.h>
#include <girara/utils.h>
@ -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)
{

View file

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

View file

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

42
utils.c
View file

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

12
utils.h
View file

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