From 911d84c9085086146dfdf486ae218f1d32b1cec2 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 23 Oct 2011 17:01:15 +0200 Subject: [PATCH 1/7] update includes due to girara restructuring --- bookmarks.c | 3 +++ callbacks.c | 4 +++- callbacks.h | 2 +- commands.c | 3 +++ commands.h | 2 +- completion.c | 5 +++++ completion.h | 2 +- config.c | 6 ++++++ database-plain.c | 3 ++- database-sqlite.c | 3 ++- database.h | 2 +- document.c | 4 ++++ document.h | 2 +- print.c | 3 +++ render.c | 3 +++ render.h | 2 +- shortcuts.c | 5 ++++- shortcuts.h | 2 +- utils.c | 2 ++ utils.h | 2 +- zathura.c | 6 +++++- zathura.h | 3 ++- 22 files changed, 55 insertions(+), 14 deletions(-) diff --git a/bookmarks.c b/bookmarks.c index f84ee10..6841824 100644 --- a/bookmarks.c +++ b/bookmarks.c @@ -5,6 +5,9 @@ #include "database.h" #include "document.h" +#include +#include + zathura_bookmark_t* zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page) { diff --git a/callbacks.c b/callbacks.c index a9d0f64..662223b 100644 --- a/callbacks.c +++ b/callbacks.c @@ -1,6 +1,8 @@ /* See LICENSE file for license and copyright information */ -#include +#include +#include +#include #include #include diff --git a/callbacks.h b/callbacks.h index a4edc1a..cfa7e20 100644 --- a/callbacks.h +++ b/callbacks.h @@ -4,7 +4,7 @@ #define CALLBACKS_H #include -#include +#include #include "zathura.h" diff --git a/commands.c b/commands.c index c98c10b..35c3ac8 100644 --- a/commands.c +++ b/commands.c @@ -11,6 +11,9 @@ #include "document.h" #include "utils.h" +#include +#include + bool cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list) { diff --git a/commands.h b/commands.h index 1f09318..33c727a 100644 --- a/commands.h +++ b/commands.h @@ -4,7 +4,7 @@ #define COMMANDS_H #include -#include +#include /** * Create a bookmark diff --git a/completion.c b/completion.c index 872dfba..f113168 100644 --- a/completion.c +++ b/completion.c @@ -10,6 +10,11 @@ #include "completion.h" #include "utils.h" +#include +#include +#include +#include + girara_completion_t* cc_open(girara_session_t* session, const char* input) { diff --git a/completion.h b/completion.h index 947e9d8..6bead72 100644 --- a/completion.h +++ b/completion.h @@ -3,7 +3,7 @@ #ifndef COMPLETION_H #define COMPLETION_H -#include +#include /** * Completion for the open command - Creates a list of accesible directories or diff --git a/config.c b/config.c index 5cf1475..d9343b7 100644 --- a/config.c +++ b/config.c @@ -7,6 +7,12 @@ #include "shortcuts.h" #include "zathura.h" +#include +#include +#include +#include +#include + void config_load_default(zathura_t* zathura) { diff --git a/database-plain.c b/database-plain.c index 783e3b2..411646a 100644 --- a/database-plain.c +++ b/database-plain.c @@ -1,12 +1,13 @@ /* See LICENSE file for license and copyright information */ -#include #include #include #include #include #include #include +#include +#include #include "database.h" diff --git a/database-sqlite.c b/database-sqlite.c index a6bfa2e..6ec5d44 100644 --- a/database-sqlite.c +++ b/database-sqlite.c @@ -2,7 +2,8 @@ #include #include -#include +#include +#include #include "database.h" diff --git a/database.h b/database.h index 6958b39..71a2b83 100644 --- a/database.h +++ b/database.h @@ -4,7 +4,7 @@ #define DATABASE_H #include -#include +#include #include "zathura.h" #include "bookmarks.h" diff --git a/document.c b/document.c index 0285b4d..37a1db8 100644 --- a/document.c +++ b/document.c @@ -22,6 +22,10 @@ #include "render.h" #include "database.h" +#include +#include +#include + /** * Register document plugin */ diff --git a/document.h b/document.h index 0df6bbd..84d6a5c 100644 --- a/document.h +++ b/document.h @@ -7,7 +7,7 @@ #include #include -#include +#include #include "zathura.h" #define PLUGIN_REGISTER_FUNCTION "plugin_register" diff --git a/print.c b/print.c index 2c6d14d..e213c65 100644 --- a/print.c +++ b/print.c @@ -4,6 +4,9 @@ #include "document.h" #include "render.h" +#include +#include + void print(zathura_t* zathura) { diff --git a/render.c b/render.c index fcb2ca4..7a2f470 100644 --- a/render.c +++ b/render.c @@ -1,6 +1,9 @@ /* See LICENSE file for license and copyright information */ #include +#include +#include +#include #include "render.h" #include "zathura.h" diff --git a/render.h b/render.h index a8628cf..87ef366 100644 --- a/render.h +++ b/render.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include "zathura.h" #include "callbacks.h" diff --git a/shortcuts.c b/shortcuts.c index b88d600..e41883c 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -1,6 +1,9 @@ /* See LICENSE file for license and copyright information */ -#include +#include +#include +#include +#include #include #include "callbacks.h" diff --git a/shortcuts.h b/shortcuts.h index 7123b08..8bf9284 100644 --- a/shortcuts.h +++ b/shortcuts.h @@ -3,7 +3,7 @@ #ifndef SHORTCUTS_H #define SHORTCUTS_H -#include +#include /** * Abort the current action and return to normal mode diff --git a/utils.c b/utils.c index 4ce9462..e10dab6 100644 --- a/utils.c +++ b/utils.c @@ -12,6 +12,8 @@ #include "zathura.h" #include "document.h" +#include + #define BLOCK_SIZE 64 bool diff --git a/utils.h b/utils.h index f073476..d04e75f 100644 --- a/utils.h +++ b/utils.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include "document.h" diff --git a/zathura.c b/zathura.c index d5ee0da..ebaf12d 100644 --- a/zathura.c +++ b/zathura.c @@ -5,7 +5,11 @@ #include -#include +#include +#include +#include +#include +#include #include #include "bookmarks.h" diff --git a/zathura.h b/zathura.h index df3d963..cd8b7be 100644 --- a/zathura.h +++ b/zathura.h @@ -4,7 +4,8 @@ #define ZATHURA_H #include -#include +#include +#include #ifdef UNUSED #elif defined(__GNUC__) From 49b682e0a89978aa09f3779392f4093eadca7f88 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 23 Oct 2011 17:34:10 +0200 Subject: [PATCH 2/7] duplicate the string for stdin support --- zathura.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zathura.c b/zathura.c index ebaf12d..c94d1f0 100644 --- a/zathura.c +++ b/zathura.c @@ -352,8 +352,9 @@ document_info_open(gpointer data) if (file == NULL) { girara_notify(document_info->zathura->ui.session, GIRARA_ERROR, "Could not read file from stdin and write it to a temporary file."); + } else { + document_info->zathura->stdin_support.file = g_strdup(file); } - document_info->zathura->stdin_support.file = file; } else { file = g_strdup(document_info->path); } From 7ca688a9649f2e8ecaeb3ebf5024fb0714117360 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 23 Oct 2011 19:26:06 +0200 Subject: [PATCH 3/7] read file blockwise to determine filetype --- document.c | 68 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/document.c b/document.c index 37a1db8..11c1f6d 100644 --- a/document.c +++ b/document.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "document.h" #include "utils.h" @@ -148,6 +149,48 @@ zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* return atleastone; } +static const gchar* +guess_type(const char* path) +{ + gboolean uncertain; + const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain); + if (content_type == NULL) { + return NULL; + } + + FILE* f = fopen(path, "r"); + if (f == NULL) { + return NULL; + } + + const int fd = fileno(f); + static const size_t BLKSIZE = 4096; + guchar* content = NULL; + size_t length = 0u; + while (uncertain == TRUE) { + g_free((void*)content_type); + content_type = NULL; + + content = g_realloc(content, length + BLKSIZE); + const ssize_t r = read(fd, content + length, BLKSIZE); + if (r == -1) { + break; + } + + length += r; + content_type = g_content_type_guess(NULL, content, length, &uncertain); + } + + fclose(f); + if (uncertain == TRUE) { + g_free((void*)content_type); + content_type = NULL; + } + + g_free(content); + return content_type; +} + zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password) { @@ -160,33 +203,12 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password return NULL; } - gboolean uncertain; - const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain); + const gchar* content_type = guess_type(path); if (content_type == NULL) { - girara_error("Could not determine file type"); + girara_error("Could not determine file type."); return NULL; } - if (uncertain == TRUE) { - g_free((void*)content_type); - content_type = NULL; - - gchar* contents = NULL; - gsize length = 0; - if (g_file_get_contents(path, &contents, &length, NULL) == FALSE) { - girara_error("Could not determine file type"); - return NULL; - } - - content_type = g_content_type_guess(NULL, (guchar*) contents, length, &uncertain); - g_free(contents); - if (content_type == NULL || uncertain == TRUE) { - g_free((void*)content_type); - girara_error("Could not determine file type"); - return NULL; - } - } - /* determine real path */ long path_max; #ifdef PATH_MAX From c06a1a4df70d3feebb5fef98bd5fde3bc7d55789 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 23 Oct 2011 19:27:57 +0200 Subject: [PATCH 4/7] use BUFSIZ --- document.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/document.c b/document.c index 11c1f6d..8bfb1b1 100644 --- a/document.c +++ b/document.c @@ -164,15 +164,14 @@ guess_type(const char* path) } const int fd = fileno(f); - static const size_t BLKSIZE = 4096; guchar* content = NULL; size_t length = 0u; while (uncertain == TRUE) { g_free((void*)content_type); content_type = NULL; - content = g_realloc(content, length + BLKSIZE); - const ssize_t r = read(fd, content + length, BLKSIZE); + content = g_realloc(content, length + BUFSIZ); + const ssize_t r = read(fd, content + length, BUFSIZ); if (r == -1) { break; } From d18f255bc036f03d271ff969988ae308b8e58ab9 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 23 Oct 2011 20:18:44 +0200 Subject: [PATCH 5/7] use girara_list_find --- bookmarks.c | 28 ++++++++++++++-------------- zathura.c | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/bookmarks.c b/bookmarks.c index 6841824..e0a90ed 100644 --- a/bookmarks.c +++ b/bookmarks.c @@ -8,18 +8,25 @@ #include #include +static int +bookmark_compare_find(const void* item, const void* data) +{ + const zathura_bookmark_t* bookmark = item; + const char* id = data; + + return g_strcmp0(bookmark->id, id); +} + zathura_bookmark_t* zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page) { g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, NULL); g_return_val_if_fail(id, NULL); - GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark) - if (strcmp(bookmark->id, id) == 0) { - girara_list_iterator_free(iter); - return NULL; - } - GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark); + zathura_bookmark_t* old = girara_list_find(zathura->bookmarks.bookmarks, bookmark_compare_find, id); + if (old != NULL) { + return NULL; + } zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t)); bookmark->id = g_strdup(id); @@ -61,14 +68,7 @@ zathura_bookmark_get(zathura_t* zathura, const gchar* id) g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL); g_return_val_if_fail(id, NULL); - GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark) - if (strcmp(bookmark->id, id) == 0) { - girara_list_iterator_free(iter); - return bookmark; - } - GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark); - - return NULL; + return girara_list_find(zathura->bookmarks.bookmarks, bookmark_compare_find, id); } void diff --git a/zathura.c b/zathura.c index c94d1f0..7aa268b 100644 --- a/zathura.c +++ b/zathura.c @@ -1,7 +1,7 @@ /* See LICENSE file for license and copyright information */ #define _BSD_SOURCE -#define _XOPEN_SOURCE 500 +#define _XOPEN_SOURCE 700 #include From 0476dbe93088f404f083c407de0d00de72062adf Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Mon, 24 Oct 2011 00:46:53 -0400 Subject: [PATCH 6/7] Fix build warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The patch fixes the following build warnings: zathura.c: In function ‘prepare_document_open_from_stdin’: zathura.c:306: warning: implicit declaration of function ‘close’ zathura.c:314: warning: implicit declaration of function ‘read’ zathura.c:316: warning: implicit declaration of function ‘write’ Signed-off-by: Pavel Borzenkov Signed-off-by: Sebastian Ramacher --- zathura.c | 1 + 1 file changed, 1 insertion(+) diff --git a/zathura.c b/zathura.c index 7aa268b..696b570 100644 --- a/zathura.c +++ b/zathura.c @@ -4,6 +4,7 @@ #define _XOPEN_SOURCE 700 #include +#include #include #include From 744320b8faf8bb166ae032f5ac0c7956e6e73817 Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Mon, 24 Oct 2011 02:38:32 -0400 Subject: [PATCH 7/7] Fix build warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The patch fixes the following warning: document.c: In function ‘guess_type’: document.c:174: warning: implicit declaration of function ‘read’ Signed-off-by: Pavel Borzenkov Signed-off-by: Sebastian Ramacher --- document.c | 1 + 1 file changed, 1 insertion(+) diff --git a/document.c b/document.c index 8bfb1b1..897da11 100644 --- a/document.c +++ b/document.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include