---
utilities.c | 80 ++++++++++++++++++++++++++++------------------------------
utilities.h | 2 +
2 files changed, 41 insertions(+), 41 deletions(-)
diff --git a/utilities.c b/utilities.c
index 9bb4f49..387a466 100644
--- a/utilities.c
+++ b/utilities.c
@@ -795,50 +795,48 @@ void make_uri_handlers_list(URIHandler *uri_handlers, int length)
}
}
+/* spawn_command_on("xpdf %s", "file.pdf") -> spawn "xpdf \"file.pdf\""
+ * todo: no way to escape whitespace in p_cmd. (ws in path is ok)
+ * Maybe just escape, printf, execute via bash?
+ * also todo: Will leak mem if %s is used more than once.
+ */
+void
+spawn_command_on(const char* p_cmd, char* arg) {
+ char *argv[64], cmd[MAX_SETTING_SIZE];
+ strncpy(cmd, p_cmd, MAX_SETTING_SIZE);
+ char *dynarg = NULL;
+ int j;
+ char *word = strtok(cmd, " ");
+ for(j = 0; j < 62 && word; ++j, word = strtok(NULL, " "))
+ argv[j] = (strstr(word, "%s")
+ ? dynarg = g_strdup_printf(word, arg)
+ : word);
+ argv[j] = NULL;
+ g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
+ if(dynarg)
+ g_free(dynarg);
+}
+
+gboolean
+is_prefix_of(char* s, char* t) {
+ return strlen(s) <= strlen(t) && !strncmp(s, t, strlen(s));
+}
+
gboolean
open_handler(char *uri) {
- char *argv[64];
- char *p = NULL, *arg, arg_temp[MAX_SETTING_SIZE], *temp, temp2[MAX_SETTING_SIZE] = "", *temp3;
- int j;
- GList *l;
+ if(!strchr(uri, ':'))
+ return FALSE;
- p = strchr(uri, ':');
- if (p) {
- if (dynamic_uri_handlers != NULL) {
- for (l = dynamic_uri_handlers; l; l = g_list_next(l)) {
- URIHandler *s = (URIHandler *)l->data;
- if (strlen(uri) >= strlen(s->handle) && strncmp(s->handle, uri, strlen(s->handle)) == 0) {
- if (strlen(s->handler) > 0) {
- arg = (uri + strlen(s->handle));
- strncpy(temp2, s->handler, MAX_SETTING_SIZE);
- temp = strtok(temp2, " ");
- j = 0;
- while (temp != NULL) {
- if (strstr(temp, "%s")) {
- temp3 = temp;
- memset(arg_temp, 0, MAX_SETTING_SIZE);
- while (strncmp(temp3, "%s", 2) != 0) {
- strncat(arg_temp, temp3, 1);
- temp3++;
- }
- strcat(arg_temp, arg);
- temp3++;
- temp3++;
- strcat(arg_temp, temp3);
- argv[j] = arg_temp;
- } else {
- argv[j] = temp;
- }
- temp = strtok(NULL, " ");
- j++;
- }
- argv[j] = NULL;
- g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
- }
- return TRUE;
- }
- }
- }
+ if(dynamic_uri_handlers) {
+ GList *l;
+ for (l = dynamic_uri_handlers; l; l = g_list_next(l)) {
+ URIHandler *s = (URIHandler *)l->data;
+ if(is_prefix_of(s->handle, uri)) {
+ if (strlen(s->handler) > 0)
+ spawn_command_on(s->handler, uri + strlen(s->handle));
+ return TRUE;
+ }
+ }
}
return FALSE;
}
diff --git a/utilities.h b/utilities.h
index f9ac1ba..8cfd7b9 100644
--- a/utilities.h
+++ b/utilities.h
@@ -34,5 +34,7 @@ void free_list(Listelement *elementlist);
char *find_uri_for_searchengine(const char *handle);
void make_searchengines_list(Searchengine *searchengines, int length);
void make_uri_handlers_list(URIHandler *uri_handlers, int length);
+void spawn_command_on(const char* p_cmd, char* arg);
gboolean open_handler(char *uri);
+gboolean is_prefix_of(char* s, char* t);
--
1.7.9.1
|