From 8ce0a10670e655d9e3a4f31fedd34baf1a3189b9 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 25 Jan 2014 23:00:22 +0000 Subject: move path_to_url and url_to_path to fetch operation table --- monkey/Makefile.target | 2 +- monkey/fetch.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ monkey/fetch.h | 19 +++++++ monkey/main.c | 107 ++++++++---------------------------- monkey/utils.c | 44 --------------- 5 files changed, 188 insertions(+), 129 deletions(-) create mode 100644 monkey/fetch.c create mode 100644 monkey/fetch.h (limited to 'monkey') diff --git a/monkey/Makefile.target b/monkey/Makefile.target index 52407542d..7c0f167d3 100644 --- a/monkey/Makefile.target +++ b/monkey/Makefile.target @@ -68,7 +68,7 @@ endif # S_MONKEY are sources purely for the MONKEY build S_MONKEY := main.c utils.c filetype.c schedule.c \ bitmap.c plot.c browser.c download.c thumbnail.c \ - 401login.c cert.c font.c poll.c dispatch.c + 401login.c cert.c font.c poll.c dispatch.c fetch.c S_MONKEY := $(addprefix monkey/,$(S_MONKEY)) diff --git a/monkey/fetch.c b/monkey/fetch.c new file mode 100644 index 000000000..668ad0e64 --- /dev/null +++ b/monkey/fetch.c @@ -0,0 +1,145 @@ +/* + * Copyright 2014 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "desktop/gui.h" +#include "utils/url.h" +#include "utils/nsurl.h" +#include "utils/filepath.h" + +#include "monkey/filetype.h" +#include "monkey/fetch.h" + +extern char **respaths; + + +static char *path_to_url(const char *path) +{ + int urllen; + char *url; + + if (path == NULL) { + return NULL; + } + + urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1; + + url = malloc(urllen); + if (url == NULL) { + return NULL; + } + + if (*path == '/') { + path++; /* file: paths are already absolute */ + } + + snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path); + + return url; +} + +static char *url_to_path(const char *url) +{ + char *path; + char *respath; + url_func_result res; /* result from url routines */ + + res = url_path(url, &path); + if (res != URL_FUNC_OK) { + return NULL; + } + + res = url_unescape(path, &respath); + free(path); + if (res != URL_FUNC_OK) { + return NULL; + } + + return respath; +} + +/** + * Return the filename part of a full path + * + * \param path full path and filename + * \return filename (will be freed with free()) + */ + +static char *filename_from_path(char *path) +{ + char *leafname; + + leafname = strrchr(path, '/'); + if (!leafname) + leafname = path; + else + leafname += 1; + + return strdup(leafname); +} + +/** + * Add a path component/filename to an existing path + * + * \param path buffer containing path + free space + * \param length length of buffer "path" + * \param newpart string containing path component to add to path + * \return true on success + */ + +static bool path_add_part(char *path, int length, const char *newpart) +{ + if(path[strlen(path) - 1] != '/') + strncat(path, "/", length); + + strncat(path, newpart, length); + + return true; +} + +static nsurl *gui_get_resource_url(const char *path) +{ + char buf[PATH_MAX]; + char *raw; + nsurl *url = NULL; + + raw = path_to_url(filepath_sfind(respaths, buf, path)); + if (raw != NULL) { + nsurl_create(raw, &url); + free(raw); + } + + return url; +} + +static struct gui_fetch_table fetch_table = { + .filename_from_path = filename_from_path, + .path_add_part = path_add_part, + .filetype = monkey_fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, + + .get_resource_url = gui_get_resource_url, +}; + +struct gui_fetch_table *monkey_fetch_table = &fetch_table; diff --git a/monkey/fetch.h b/monkey/fetch.h new file mode 100644 index 000000000..59e8696d1 --- /dev/null +++ b/monkey/fetch.h @@ -0,0 +1,19 @@ +/* + * Copyright 2014 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +struct gui_fetch_table *monkey_fetch_table; diff --git a/monkey/main.c b/monkey/main.c index 1135f0e53..07ce7eeeb 100644 --- a/monkey/main.c +++ b/monkey/main.c @@ -20,13 +20,14 @@ #include #include -#include "monkey/filetype.h" #include "utils/nsoption.h" #include "monkey/poll.h" #include "monkey/dispatch.h" #include "monkey/browser.h" #include "monkey/cert.h" #include "monkey/401login.h" +#include "monkey/filetype.h" +#include "monkey/fetch.h" #include "content/urldb.h" #include "content/fetchers/resource.h" @@ -36,49 +37,34 @@ #include "utils/filepath.h" #include "utils/url.h" -static char **respaths; /** resource search path vector */ +char **respaths; /** resource search path vector */ /* Stolen from gtk/gui.c */ static char ** nsmonkey_init_resource(const char *resource_path) { - const gchar * const *langv; - char **pathv; /* resource path string vector */ - char **respath; /* resource paths vector */ + const gchar * const *langv; + char **pathv; /* resource path string vector */ + char **respath; /* resource paths vector */ - pathv = filepath_path_to_strvec(resource_path); + pathv = filepath_path_to_strvec(resource_path); - langv = g_get_language_names(); + langv = g_get_language_names(); - respath = filepath_generate(pathv, langv); + respath = filepath_generate(pathv, langv); - filepath_free_strvec(pathv); + filepath_free_strvec(pathv); - return respath; + return respath; } static void monkey_quit(void) { - urldb_save_cookies(nsoption_charp(cookie_jar)); - urldb_save(nsoption_charp(url_file)); - free(nsoption_charp(cookie_file)); - free(nsoption_charp(cookie_jar)); - monkey_fetch_filetype_fin(); -} - -static nsurl *gui_get_resource_url(const char *path) -{ - char buf[PATH_MAX]; - char *raw; - nsurl *url = NULL; - - raw = path_to_url(filepath_sfind(respaths, buf, path)); - if (raw != NULL) { - nsurl_create(raw, &url); - free(raw); - } - - return url; + urldb_save_cookies(nsoption_charp(cookie_jar)); + urldb_save(nsoption_charp(url_file)); + free(nsoption_charp(cookie_file)); + free(nsoption_charp(cookie_jar)); + monkey_fetch_filetype_fin(); } static void @@ -115,53 +101,6 @@ static bool nslog_stream_configure(FILE *fptr) return true; } -/** - * Return the filename part of a full path - * - * \param path full path and filename - * \return filename (will be freed with free()) - */ - -static char *filename_from_path(char *path) -{ - char *leafname; - - leafname = strrchr(path, '/'); - if (!leafname) - leafname = path; - else - leafname += 1; - - return strdup(leafname); -} - -/** - * Add a path component/filename to an existing path - * - * \param path buffer containing path + free space - * \param length length of buffer "path" - * \param newpart string containing path component to add to path - * \return true on success - */ - -static bool path_add_part(char *path, int length, const char *newpart) -{ - if(path[strlen(path) - 1] != '/') - strncat(path, "/", length); - - strncat(path, newpart, length); - - return true; -} - -static struct gui_fetch_table monkey_fetch_table = { - .filename_from_path = filename_from_path, - .path_add_part = path_add_part, - .filetype = monkey_fetch_filetype, - - .get_resource_url = gui_get_resource_url, -}; - static struct gui_browser_table monkey_browser_table = { .poll = monkey_poll, @@ -182,14 +121,14 @@ main(int argc, char **argv) .browser = &monkey_browser_table, .window = monkey_window_table, .download = monkey_download_table, - .fetch = &monkey_fetch_table, + .fetch = monkey_fetch_table, }; /* Unbuffer stdin/out/err */ setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL); - + /* Prep the search paths */ respaths = nsmonkey_init_resource("${HOME}/.netsurf/:${NETSURFRES}:"MONKEY_RESPATH":./monkey/res"); @@ -215,22 +154,22 @@ main(int argc, char **argv) if (ret != NSERROR_OK) { die("NetSurf failed to initialise"); } - + filepath_sfinddef(respaths, buf, "mime.types", "/etc/"); monkey_fetch_filetype_init(buf); - + urldb_load(nsoption_charp(url_file)); urldb_load_cookies(nsoption_charp(cookie_file)); - + monkey_prepare_input(); monkey_register_handler("QUIT", quit_handler); monkey_register_handler("WINDOW", monkey_window_handle_command); - + fprintf(stdout, "GENERIC STARTED\n"); netsurf_main_loop(); fprintf(stdout, "GENERIC CLOSING_DOWN\n"); monkey_kill_browser_windows(); - + netsurf_exit(); fprintf(stdout, "GENERIC FINISHED\n"); diff --git a/monkey/utils.c b/monkey/utils.c index aa7245533..88776a90f 100644 --- a/monkey/utils.c +++ b/monkey/utils.c @@ -25,50 +25,6 @@ #include "utils/url.h" #include "utils/utf8.h" -char *path_to_url(const char *path) -{ - int urllen; - char *url; - - if (path == NULL) { - return NULL; - } - - urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1; - - url = malloc(urllen); - if (url == NULL) { - return NULL; - } - - if (*path == '/') { - path++; /* file: paths are already absolute */ - } - - snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, path); - - return url; -} - -char *url_to_path(const char *url) -{ - char *path; - char *respath; - url_func_result res; /* result from url routines */ - - res = url_path(url, &path); - if (res != URL_FUNC_OK) { - return NULL; - } - - res = url_unescape(path, &respath); - free(path); - if (res != URL_FUNC_OK) { - return NULL; - } - - return respath; -} void warn_user(const char *warning, const char *detail) -- cgit v1.2.3