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 --- amiga/gui.c | 2 + amiga/misc.h | 4 + atari/findfile.h | 5 +- atari/gui.c | 2 + beos/gui.cpp | 26 ++-- cocoa/BrowserViewController.m | 1 + cocoa/Makefile.target | 1 - cocoa/NetsurfApp.m | 1 - cocoa/fetch.h | 4 +- cocoa/fetch.m | 61 +++++++- cocoa/gui.h | 1 - cocoa/gui.m | 32 ---- cocoa/url.m | 34 ----- content/fetchers/file.c | 4 +- desktop/gui.h | 16 ++ desktop/gui_factory.c | 6 + desktop/searchweb.c | 2 +- framebuffer/fetch.c | 19 ++- gtk/Makefile.target | 2 +- gtk/dialogs/source.c | 17 ++- gtk/fetch.c | 347 ++++++++++++++++++++++++++++++++++++++++++ gtk/fetch.h | 30 ++++ gtk/filetype.c | 234 ---------------------------- gtk/filetype.h | 22 --- gtk/gui.c | 120 +-------------- gtk/gui.h | 2 + monkey/Makefile.target | 2 +- monkey/fetch.c | 145 ++++++++++++++++++ monkey/fetch.h | 19 +++ monkey/main.c | 107 +++---------- monkey/utils.c | 44 ------ riscos/gui.c | 261 +++++++++++++++---------------- utils/url.h | 3 - windows/findfile.h | 3 + windows/gui.c | 2 + 35 files changed, 841 insertions(+), 740 deletions(-) delete mode 100644 cocoa/url.m create mode 100644 gtk/fetch.c create mode 100644 gtk/fetch.h delete mode 100644 gtk/filetype.c delete mode 100644 gtk/filetype.h create mode 100644 monkey/fetch.c create mode 100644 monkey/fetch.h diff --git a/amiga/gui.c b/amiga/gui.c index a9c72c9e9..074dfafa6 100644 --- a/amiga/gui.c +++ b/amiga/gui.c @@ -5189,6 +5189,8 @@ static struct gui_fetch_table amiga_fetch_table = { .filename_from_path = filename_from_path, .path_add_part = path_add_part, .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, .get_resource_url = gui_get_resource_url, }; diff --git a/amiga/misc.h b/amiga/misc.h index 34a85fa90..60f249c2c 100644 --- a/amiga/misc.h +++ b/amiga/misc.h @@ -18,6 +18,10 @@ #ifndef AMIGA_MISC_H #define AMIGA_MISC_H + char *translate_escape_chars(const char *s); int32 ami_warn_user_multi(const char *body, const char *opt1, const char *opt2, struct Window *win); +char *url_to_path(const char *url); +char *path_to_url(const char *path); + #endif diff --git a/atari/findfile.h b/atari/findfile.h index e87f7cff7..cced0092c 100755 --- a/atari/findfile.h +++ b/atari/findfile.h @@ -21,6 +21,9 @@ #define NS_ATARI_FINDFILE_H extern char *atari_find_resource(char *buf, const char *filename, const char *def); -char * local_file_to_url( const char * filename ); +char *local_file_to_url(const char *filename); + +char *path_to_url(const char *path_in); +char *url_to_path(const char *url); #endif /* NETSURF_ATARI_FINDFILE_H */ diff --git a/atari/gui.c b/atari/gui.c index 0267148d2..f1f557e2e 100644 --- a/atari/gui.c +++ b/atari/gui.c @@ -1077,6 +1077,8 @@ static struct gui_fetch_table atari_fetch_table = { .filename_from_path = filename_from_path, .path_add_part = path_add_part, .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, .get_resource_url = gui_get_resource_url, .mimetype = fetch_mimetype, diff --git a/beos/gui.cpp b/beos/gui.cpp index ca176c83f..05e06373c 100644 --- a/beos/gui.cpp +++ b/beos/gui.cpp @@ -786,6 +786,17 @@ static void gui_quit(void) fetch_rsrc_unregister(); } +static char *url_to_path(const char *url) +{ + char *url_path = curl_unescape(url, 0); + char *path; + + /* return the absolute path including leading / */ + path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1)); + curl_free(url_path); + + return path; +} /** * Send the source of a content to a text editor. @@ -983,7 +994,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, return UTF8_CONVERT_OK; } -char *path_to_url(const char *path) +static char *path_to_url(const char *path) { int urllen = strlen(path) + FILE_SCHEME_PREFIX_LEN + 1; char *url = (char *)malloc(urllen); @@ -1001,17 +1012,6 @@ char *path_to_url(const char *path) return url; } -char *url_to_path(const char *url) -{ - char *url_path = curl_unescape(url, 0); - char *path; - - /* return the absolute path including leading / */ - path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1)); - curl_free(url_path); - - return path; -} static void *myrealloc(void *ptr, size_t len, void *pw) { @@ -1071,6 +1071,8 @@ static struct gui_fetch_table beos_fetch_table = { filename_from_path, path_add_part, fetch_filetype, + path_to_url, + url_to_path, gui_get_resource_url, NULL //fetch_mimetype }; diff --git a/cocoa/BrowserViewController.m b/cocoa/BrowserViewController.m index 77ee57bd1..956654aba 100644 --- a/cocoa/BrowserViewController.m +++ b/cocoa/BrowserViewController.m @@ -19,6 +19,7 @@ #import "cocoa/BrowserViewController.h" #import "cocoa/BrowserView.h" #import "cocoa/BrowserWindowController.h" +#import "cocoa/fetch.h" #import "desktop/browser_private.h" #import "desktop/local_history.h" diff --git a/cocoa/Makefile.target b/cocoa/Makefile.target index 8bd13c29b..49f67f085 100644 --- a/cocoa/Makefile.target +++ b/cocoa/Makefile.target @@ -95,7 +95,6 @@ S_COCOA := \ schedule.m \ selection.m \ thumbnail.m \ - url.m \ utf8.m \ utils.m \ ArrowBox.m \ diff --git a/cocoa/NetsurfApp.m b/cocoa/NetsurfApp.m index 5643efd6c..32ad8f1fb 100644 --- a/cocoa/NetsurfApp.m +++ b/cocoa/NetsurfApp.m @@ -25,7 +25,6 @@ #import "desktop/gui.h" #import "content/urldb.h" -#import "content/fetch.h" #import "css/utils.h" #import "desktop/gui.h" #import "desktop/local_history.h" diff --git a/cocoa/fetch.h b/cocoa/fetch.h index 4a50b6e8d..5d2e9288d 100644 --- a/cocoa/fetch.h +++ b/cocoa/fetch.h @@ -16,4 +16,6 @@ * along with this program. If not, see . */ -const char *fetch_filetype(const char *unix_path); +extern struct gui_fetch_table *cocoa_fetch_table; + +char *url_to_path(const char *url); diff --git a/cocoa/fetch.m b/cocoa/fetch.m index b1c7aea9e..7bf00e0ba 100644 --- a/cocoa/fetch.m +++ b/cocoa/fetch.m @@ -19,7 +19,7 @@ #import #import "utils/log.h" -#import "content/fetch.h" +#import "desktop/gui.h" #import "cocoa/fetch.h" @@ -42,9 +42,9 @@ static const struct mimemap_s { }; -const char *fetch_filetype(const char *unix_path) +static const char *fetch_filetype(const char *unix_path) { - NSString *uti; + NSString *uti; NSString *mimeType = nil; NSError *utiError = nil; @@ -58,7 +58,7 @@ const char *fetch_filetype(const char *unix_path) LOG(("uti call failed")); - strncpy(cocoafiletype, "text/html", sizeof(cocoafiletype)); + strncpy(cocoafiletype, "text/html", sizeof(cocoafiletype)); return cocoafiletype; } @@ -83,13 +83,60 @@ const char *fetch_filetype(const char *unix_path) eidx++; } - strncpy(cocoafiletype, - cocoamimemap[eidx].mimetype, + strncpy(cocoafiletype, + cocoamimemap[eidx].mimetype, sizeof(cocoafiletype)); } } LOG(( "\tMIME type for '%s' is '%s'", unix_path, cocoafiletype )); - + return cocoafiletype; } + +char *url_to_path(const char *url) +{ + NSURL *nsurl = [NSURL URLWithString: [NSString stringWithUTF8String: url]]; + return strdup([[nsurl path] UTF8String]); +} + +static char *path_to_url(const char *path) +{ + return strdup( [[[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] + absoluteString] UTF8String] ); +} + +static char *filename_from_path(char *path) +{ + return strdup( [[[NSString stringWithUTF8String: path] lastPathComponent] UTF8String] ); +} + +static bool path_add_part(char *path, int length, const char *newpart) +{ + NSString *newPath = [[NSString stringWithUTF8String: path] stringByAppendingPathComponent: [NSString stringWithUTF8String: newpart]]; + + strncpy( path, [newPath UTF8String], length ); + + return true; +} + +static nsurl *gui_get_resource_url(const char *path) +{ + nsurl *url = NULL; + NSString *nspath = [[NSBundle mainBundle] pathForResource: [NSString stringWithUTF8String: path] ofType: @""]; + if (nspath == nil) return NULL; + nsurl_create([[[NSURL fileURLWithPath: nspath] absoluteString] UTF8String], &url); + return url; +} + +static struct gui_fetch_table fetch_table = { + .filename_from_path = filename_from_path, + .path_add_part = path_add_part, + .filetype = 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 *cocoa_fetch_table = &fetch_table; diff --git a/cocoa/gui.h b/cocoa/gui.h index 55b69072e..757140030 100644 --- a/cocoa/gui.h +++ b/cocoa/gui.h @@ -20,7 +20,6 @@ extern struct gui_window_table *cocoa_window_table; extern struct gui_clipboard_table *cocoa_clipboard_table; -extern struct gui_fetch_table *cocoa_fetch_table; extern struct gui_browser_table *cocoa_browser_table; extern NSString * const kCookiesFileOption; diff --git a/cocoa/gui.m b/cocoa/gui.m index 2a97df39d..750d9fca4 100644 --- a/cocoa/gui.m +++ b/cocoa/gui.m @@ -46,15 +46,6 @@ NSString * const kAlwaysCloseMultipleTabs = @"AlwaysCloseMultipleTabs"; #define UNIMPL() NSLog( @"Function '%s' unimplemented", __func__ ) -static nsurl *gui_get_resource_url(const char *path) -{ - nsurl *url = NULL; - NSString *nspath = [[NSBundle mainBundle] pathForResource: [NSString stringWithUTF8String: path] ofType: @""]; - if (nspath == nil) return NULL; - nsurl_create([[[NSURL fileURLWithPath: nspath] absoluteString] UTF8String], &url); - return url; -} - static void gui_poll(bool active) { cocoa_autorelease(); @@ -273,20 +264,6 @@ static void gui_cert_verify(nsurl *url, const struct ssl_cert_info *certs, cb( false, cbpw ); } -static char *filename_from_path(char *path) -{ - return strdup( [[[NSString stringWithUTF8String: path] lastPathComponent] UTF8String] ); -} - -static bool path_add_part(char *path, int length, const char *newpart) -{ - NSString *newPath = [[NSString stringWithUTF8String: path] stringByAppendingPathComponent: [NSString stringWithUTF8String: newpart]]; - - strncpy( path, [newPath UTF8String], length ); - - return true; -} - static struct gui_window_table window_table = { .create = gui_window_create, @@ -312,15 +289,6 @@ static struct gui_window_table window_table = { struct gui_window_table *cocoa_window_table = &window_table; -static struct gui_fetch_table fetch_table = { - .filename_from_path = filename_from_path, - .path_add_part = path_add_part, - .filetype = fetch_filetype, - - .get_resource_url = gui_get_resource_url, -}; - -struct gui_fetch_table *cocoa_fetch_table = &fetch_table; static struct gui_browser_table browser_table = { .poll = gui_poll, diff --git a/cocoa/url.m b/cocoa/url.m deleted file mode 100644 index 931f459a3..000000000 --- a/cocoa/url.m +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2011 Sven Weidauer - * - * 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 . - */ - -#import - -#import "utils/url.h" - - -char *url_to_path(const char *url) -{ - NSURL *nsurl = [NSURL URLWithString: [NSString stringWithUTF8String: url]]; - return strdup([[nsurl path] UTF8String]); -} - -char *path_to_url(const char *path) -{ - return strdup( [[[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] - absoluteString] UTF8String] ); -} diff --git a/content/fetchers/file.c b/content/fetchers/file.c index 7b93ab172..00d5cc5f1 100644 --- a/content/fetchers/file.c +++ b/content/fetchers/file.c @@ -142,7 +142,7 @@ fetch_file_setup(struct fetch *fetchh, if (ctx == NULL) return NULL; - ctx->path = url_to_path(nsurl_access(url)); + ctx->path = guit->fetch->url_to_path(nsurl_access(url)); if (ctx->path == NULL) { free(ctx); return NULL; @@ -600,7 +600,7 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, } } - if((path = path_to_url(urlpath)) == NULL) + if((path = guit->fetch->path_to_url(urlpath)) == NULL) continue; if (S_ISREG(ent_stat.st_mode)) { diff --git a/desktop/gui.h b/desktop/gui.h index 724355d10..da609b1e3 100644 --- a/desktop/gui.h +++ b/desktop/gui.h @@ -319,6 +319,22 @@ struct gui_fetch_table { */ const char *(*filetype)(const char *unix_path); + /** + * Convert a pathname to a file: URL. + * + * \param path pathname + * \return URL, allocated on heap, or NULL on failure + */ + char *(*path_to_url)(const char *path); + + /** + * Convert a file: URL to a pathname. + * + * \param url a file: URL + * \return pathname, allocated on heap, or NULL on failure + */ + char *(*url_to_path)(const char *url); + /* Optional entries */ diff --git a/desktop/gui_factory.c b/desktop/gui_factory.c index 06b5383ba..dfe775571 100644 --- a/desktop/gui_factory.c +++ b/desktop/gui_factory.c @@ -317,6 +317,12 @@ static nserror verify_fetch_register(struct gui_fetch_table *gft) if (gft->filetype == NULL) { return NSERROR_BAD_PARAMETER; } + if (gft->path_to_url == NULL) { + return NSERROR_BAD_PARAMETER; + } + if (gft->url_to_path == NULL) { + return NSERROR_BAD_PARAMETER; + } /* fill in the optional entries with defaults */ diff --git a/desktop/searchweb.c b/desktop/searchweb.c index d42919d2d..b3f6162a6 100644 --- a/desktop/searchweb.c +++ b/desktop/searchweb.c @@ -236,7 +236,7 @@ void search_web_retrieve_ico(bool localdefault) if (localdefault) { if (search_default_ico_location == NULL) return; - url = path_to_url(search_default_ico_location); + url = guit->fetch->path_to_url(search_default_ico_location); } else { url = search_web_ico_name(); } diff --git a/framebuffer/fetch.c b/framebuffer/fetch.c index 2e304c222..0c9b90f7c 100644 --- a/framebuffer/fetch.c +++ b/framebuffer/fetch.c @@ -68,7 +68,13 @@ static bool path_add_part(char *path, int length, const char *newpart) return true; } -char *path_to_url(const char *path) +/** + * Convert a pathname to a file: URL. + * + * \param path pathname + * \return URL, allocated on heap, or NULL on failure + */ +static char *path_to_url(const char *path) { int urllen; char *url; @@ -88,8 +94,13 @@ char *path_to_url(const char *path) return url; } - -char *url_to_path(const char *url) +/** + * Convert a file: URL to a pathname. + * + * \param url a file: URL + * \return pathname, allocated on heap, or NULL on failure + */ +static char *url_to_path(const char *url) { char *path; char *respath; @@ -178,6 +189,8 @@ static struct gui_fetch_table fetch_table = { .filename_from_path = filename_from_path, .path_add_part = path_add_part, .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, .get_resource_url = get_resource_url, .mimetype = fetch_mimetype, diff --git a/gtk/Makefile.target b/gtk/Makefile.target index 426c4affd..ec19d1b36 100644 --- a/gtk/Makefile.target +++ b/gtk/Makefile.target @@ -108,7 +108,7 @@ $(eval $(foreach V,$(filter GTK_IMAGE_%,$(.VARIABLES)),$(call convert_image,$($( # S_GTK are sources purely for the GTK build S_GTK := font_pango.c bitmap.c gui.c schedule.c thumbnail.c plotters.c \ treeview.c scaffolding.c gdk.c completion.c login.c throbber.c \ - selection.c history.c window.c filetype.c download.c menu.c \ + selection.c history.c window.c fetch.c download.c menu.c \ print.c search.c tabs.c theme.c toolbar.c gettext.c \ compat.c cookies.c hotlist.c \ $(addprefix dialogs/,preferences.c about.c source.c) diff --git a/gtk/dialogs/source.c b/gtk/dialogs/source.c index 5306bdc16..a7c98152e 100644 --- a/gtk/dialogs/source.c +++ b/gtk/dialogs/source.c @@ -23,13 +23,6 @@ #include #include -#include "gtk/compat.h" -#include "gtk/dialogs/source.h" -#include "gtk/dialogs/about.h" -#include "gtk/window.h" -#include "gtk/gui.h" -#include "gtk/print.h" -#include "gtk/selection.h" #include "desktop/browser_private.h" #include "desktop/netsurf.h" #include "desktop/print.h" @@ -42,9 +35,17 @@ #include "render/font.h" #include "content/content.h" #include "content/content_type.h" - #include "utils/log.h" +#include "gtk/compat.h" +#include "gtk/dialogs/source.h" +#include "gtk/dialogs/about.h" +#include "gtk/window.h" +#include "gtk/gui.h" +#include "gtk/print.h" +#include "gtk/selection.h" +#include "gtk/fetch.h" + struct nsgtk_source_window { gchar *url; char *data; diff --git a/gtk/fetch.c b/gtk/fetch.c new file mode 100644 index 000000000..f4a42b5f8 --- /dev/null +++ b/gtk/fetch.c @@ -0,0 +1,347 @@ +/* + * 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 "utils/hashtable.h" +#include "utils/url.h" +#include "utils/log.h" +#include "utils/filepath.h" +#include "desktop/gui.h" + +#include "gtk/gui.h" +#include "gtk/fetch.h" + +static struct hash_table *mime_hash = NULL; + +void gtk_fetch_filetype_init(const char *mimefile) +{ + struct stat statbuf; + FILE *fh = NULL; + + mime_hash = hash_create(117); + + /* first, check to see if /etc/mime.types in preference */ + + if ((stat("/etc/mime.types", &statbuf) == 0) && + S_ISREG(statbuf.st_mode)) { + mimefile = "/etc/mime.types"; + } + + fh = fopen(mimefile, "r"); + + /* Some OSes (mentioning no Solarises) have a worthlessly tiny + * /etc/mime.types that don't include essential things, so we + * pre-seed our hash with the essentials. These will get + * over-ridden if they are mentioned in the mime.types file. + */ + + hash_add(mime_hash, "css", "text/css"); + hash_add(mime_hash, "htm", "text/html"); + hash_add(mime_hash, "html", "text/html"); + hash_add(mime_hash, "jpg", "image/jpeg"); + hash_add(mime_hash, "jpeg", "image/jpeg"); + hash_add(mime_hash, "gif", "image/gif"); + hash_add(mime_hash, "png", "image/png"); + hash_add(mime_hash, "jng", "image/jng"); + hash_add(mime_hash, "mng", "image/mng"); + hash_add(mime_hash, "webp", "image/webp"); + hash_add(mime_hash, "spr", "image/x-riscos-sprite"); + + if (fh == NULL) { + LOG(("Unable to open a mime.types file, so using a minimal one for you.")); + return; + } + + while (!feof(fh)) { + char line[256], *ptr, *type, *ext; + + if (fgets(line, 256, fh) == NULL) + break; + + if (!feof(fh) && line[0] != '#') { + ptr = line; + + /* search for the first non-whitespace character */ + while (isspace(*ptr)) { + ptr++; + } + + /* is this line empty other than leading whitespace? */ + if (*ptr == '\n' || *ptr == '\0') { + continue; + } + + type = ptr; + + /* search for the first non-whitespace char or NUL or + * NL */ + while (*ptr && (!isspace(*ptr)) && *ptr != '\n') { + ptr++; + } + + if (*ptr == '\0' || *ptr == '\n') { + /* this mimetype has no extensions - read next + * line. + */ + continue; + } + + *ptr++ = '\0'; + + /* search for the first non-whitespace character which + * will be the first filename extenion */ + while (isspace(*ptr)) { + ptr++; + } + + while(true) { + ext = ptr; + + /* search for the first whitespace char or + * NUL or NL which is the end of the ext. + */ + while (*ptr && + (!isspace(*ptr)) && + *ptr != '\n') { + ptr++; + } + + if (*ptr == '\0' || *ptr == '\n') { + /* special case for last extension on + * the line + */ + *ptr = '\0'; + hash_add(mime_hash, ext, type); + break; + } + + *ptr++ = '\0'; + hash_add(mime_hash, ext, type); + + /* search for the first non-whitespace char or + * NUL or NL, to find start of next ext. + */ + while (*ptr && + (isspace(*ptr)) && + *ptr != '\n') { + ptr++; + } + } + } + } + + fclose(fh); +} + +void gtk_fetch_filetype_fin(void) +{ + hash_destroy(mime_hash); +} + +const char *fetch_filetype(const char *unix_path) +{ + struct stat statbuf; + char *ext; + const char *ptr; + char *lowerchar; + const char *type; + int l; + + if (stat(unix_path, &statbuf) != 0) { + /* stat failed */ + return "text/plain"; + } + + if (S_ISDIR(statbuf.st_mode)) { + return "application/x-netsurf-directory"; + } + + l = strlen(unix_path); + + /* Hacky RISC OS compatibility */ + if ((3 < l) && (strcasecmp(unix_path + l - 4, ",f79") == 0)) { + return "text/css"; + } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",faf") == 0)) { + return "text/html"; + } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",b60") == 0)) { + return "image/png"; + } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",ff9") == 0)) { + return "image/x-riscos-sprite"; + } + + if (strchr(unix_path, '.') == NULL) { + /* no extension anywhere! */ + return "text/plain"; + } + + ptr = unix_path + strlen(unix_path); + while (*ptr != '.' && *ptr != '/') { + ptr--; + } + + if (*ptr != '.') { + return "text/plain"; + } + + ext = strdup(ptr + 1); /* skip the . */ + + /* the hash table only contains lower-case versions - make sure this + * copy is lower case too. + */ + lowerchar = ext; + while (*lowerchar) { + *lowerchar = tolower(*lowerchar); + lowerchar++; + } + + type = hash_get(mime_hash, ext); + free(ext); + + if (type == NULL) { + type = "text/plain"; + } + + return type; +} + +/** + * 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; +} + +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; +} + +static nsurl *gui_get_resource_url(const char *path) +{ + char buf[PATH_MAX]; + char *raw; + nsurl *url = NULL; + + /* default.css -> gtkdefault.css */ + if (strcmp(path, "default.css") == 0) { + path = "gtkdefault.css"; + } + + /* favicon.ico -> favicon.png */ + if (strcmp(path, "favicon.ico") == 0) { + path = "favicon.png"; + } + + 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 = 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 *nsgtk_fetch_table = &fetch_table; diff --git a/gtk/fetch.h b/gtk/fetch.h new file mode 100644 index 000000000..400a06a35 --- /dev/null +++ b/gtk/fetch.h @@ -0,0 +1,30 @@ +/* + * 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 . + */ + +#ifndef NETSURF_GTK_FETCH_H +#define NETSURF_GTK_FETCH_H + +struct gui_fetch_table *nsgtk_fetch_table; + +void gtk_fetch_filetype_init(const char *mimefile); +void gtk_fetch_filetype_fin(void); +const char *fetch_filetype(const char *unix_path); + +char *path_to_url(const char *path); + +#endif diff --git a/gtk/filetype.c b/gtk/filetype.c deleted file mode 100644 index a949b14d5..000000000 --- a/gtk/filetype.c +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Copyright 2007 Rob Kendrick - * Copyright 2007 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 -#include -#include - -#include "gtk/filetype.h" -#include "content/fetch.h" -#include "utils/log.h" -#include "utils/hashtable.h" - -static struct hash_table *mime_hash = NULL; - -void gtk_fetch_filetype_init(const char *mimefile) -{ - struct stat statbuf; - FILE *fh = NULL; - - mime_hash = hash_create(117); - - /* first, check to see if /etc/mime.types in preference */ - - if ((stat("/etc/mime.types", &statbuf) == 0) && - S_ISREG(statbuf.st_mode)) { - mimefile = "/etc/mime.types"; - - } - - fh = fopen(mimefile, "r"); - - /* Some OSes (mentioning no Solarises) have a worthlessly tiny - * /etc/mime.types that don't include essential things, so we - * pre-seed our hash with the essentials. These will get - * over-ridden if they are mentioned in the mime.types file. - */ - - hash_add(mime_hash, "css", "text/css"); - hash_add(mime_hash, "htm", "text/html"); - hash_add(mime_hash, "html", "text/html"); - hash_add(mime_hash, "jpg", "image/jpeg"); - hash_add(mime_hash, "jpeg", "image/jpeg"); - hash_add(mime_hash, "gif", "image/gif"); - hash_add(mime_hash, "png", "image/png"); - hash_add(mime_hash, "jng", "image/jng"); - hash_add(mime_hash, "mng", "image/mng"); - hash_add(mime_hash, "webp", "image/webp"); - hash_add(mime_hash, "spr", "image/x-riscos-sprite"); - - if (fh == NULL) { - LOG(("Unable to open a mime.types file, so using a minimal one for you.")); - return; - } - - while (!feof(fh)) { - char line[256], *ptr, *type, *ext; - if (fgets(line, 256, fh) == NULL) - break; - if (!feof(fh) && line[0] != '#') { - ptr = line; - - /* search for the first non-whitespace character */ - while (isspace(*ptr)) - ptr++; - - /* is this line empty other than leading whitespace? */ - if (*ptr == '\n' || *ptr == '\0') - continue; - - type = ptr; - - /* search for the first non-whitespace char or NUL or - * NL */ - while (*ptr && (!isspace(*ptr)) && *ptr != '\n') - ptr++; - - if (*ptr == '\0' || *ptr == '\n') { - /* this mimetype has no extensions - read next - * line. - */ - continue; - } - - *ptr++ = '\0'; - - /* search for the first non-whitespace character which - * will be the first filename extenion */ - while (isspace(*ptr)) - ptr++; - - while(true) { - ext = ptr; - - /* search for the first whitespace char or - * NUL or NL which is the end of the ext. - */ - while (*ptr && (!isspace(*ptr)) && - *ptr != '\n') - ptr++; - - if (*ptr == '\0' || *ptr == '\n') { - /* special case for last extension on - * the line - */ - *ptr = '\0'; - hash_add(mime_hash, ext, type); - break; - } - - *ptr++ = '\0'; - hash_add(mime_hash, ext, type); - - /* search for the first non-whitespace char or - * NUL or NL, to find start of next ext. - */ - while (*ptr && (isspace(*ptr)) && *ptr != '\n') - ptr++; - } - } - } - - fclose(fh); -} - -void gtk_fetch_filetype_fin(void) -{ - hash_destroy(mime_hash); -} - -const char *fetch_filetype(const char *unix_path) -{ - struct stat statbuf; - char *ext; - const char *ptr; - char *lowerchar; - const char *type; - int l; - - if (stat(unix_path, &statbuf) != 0) { - /* stat failed */ - return "text/plain"; - } - - if (S_ISDIR(statbuf.st_mode)) - return "application/x-netsurf-directory"; - - l = strlen(unix_path); - - /* Hacky RISC OS compatibility */ - if ((3 < l) && (strcasecmp(unix_path + l - 4, ",f79") == 0)) { - return "text/css"; - } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",faf") == 0)) { - return "text/html"; - } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",b60") == 0)) { - return "image/png"; - } else if ((3 < l) && (strcasecmp(unix_path + l - 4, ",ff9") == 0)) { - return "image/x-riscos-sprite"; - } - - if (strchr(unix_path, '.') == NULL) { - /* no extension anywhere! */ - return "text/plain"; - } - - ptr = unix_path + strlen(unix_path); - while (*ptr != '.' && *ptr != '/') - ptr--; - - if (*ptr != '.') - return "text/plain"; - - ext = strdup(ptr + 1); /* skip the . */ - - /* the hash table only contains lower-case versions - make sure this - * copy is lower case too. - */ - lowerchar = ext; - while(*lowerchar) { - *lowerchar = tolower(*lowerchar); - lowerchar++; - } - - type = hash_get(mime_hash, ext); - free(ext); - - return type != NULL ? type : "text/plain"; -} - -#ifdef TEST_RIG - -int main(int argc, char *argv[]) -{ - unsigned int c1, *c2; - const char *key; - - gtk_fetch_filetype_init("./mime.types"); - - c1 = 0; c2 = 0; - - while ( (key = hash_iterate(mime_hash, &c1, &c2)) != NULL) { - printf("%s ", key); - } - - printf("\n"); - - if (argc > 1) { - printf("%s maps to %s\n", argv[1], fetch_filetype(argv[1])); - } - - gtk_fetch_filetype_fin(); -} - -#endif diff --git a/gtk/filetype.h b/gtk/filetype.h deleted file mode 100644 index 68bb9c0ff..000000000 --- a/gtk/filetype.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2007 Rob Kendrick - * Copyright 2007 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 . - */ - -void gtk_fetch_filetype_init(const char *mimefile); -void gtk_fetch_filetype_fin(void); -const char *fetch_filetype(const char *unix_path); diff --git a/gtk/gui.c b/gtk/gui.c index d6d35029d..ad80e8941 100644 --- a/gtk/gui.c +++ b/gtk/gui.c @@ -60,7 +60,7 @@ #include "gtk/completion.h" #include "gtk/cookies.h" #include "gtk/download.h" -#include "gtk/filetype.h" +#include "gtk/fetch.h" #include "gtk/gui.h" #include "gtk/history.h" #include "gtk/hotlist.h" @@ -109,7 +109,7 @@ static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data); #define THROBBER_FRAMES 9 -static char **respaths; /** resource search path vector */ +char **respaths; /** resource search path vector */ /** Create an array of valid paths to search for resources. * @@ -324,28 +324,6 @@ static void check_options(char **respath) } -static nsurl *gui_get_resource_url(const char *path) -{ - char buf[PATH_MAX]; - char *raw; - nsurl *url = NULL; - - /* default.css -> gtkdefault.css */ - if (strcmp(path, "default.css") == 0) - path = "gtkdefault.css"; - - /* favicon.ico -> favicon.png */ - if (strcmp(path, "favicon.ico") == 0) - path = "favicon.png"; - - raw = path_to_url(filepath_sfind(respaths, buf, path)); - if (raw != NULL) { - nsurl_create(raw, &url); - free(raw); - } - - return url; -} /** @@ -842,51 +820,6 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, } -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; -} #ifdef WITH_PDF_EXPORT @@ -1089,59 +1022,12 @@ uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *key) } } -/** - * 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_clipboard_table nsgtk_clipboard_table = { .get = gui_get_clipboard, .set = gui_set_clipboard, }; -static struct gui_fetch_table nsgtk_fetch_table = { - .filename_from_path = filename_from_path, - .path_add_part = path_add_part, - .filetype = fetch_filetype, - - .get_resource_url = gui_get_resource_url, - -}; - static struct gui_browser_table nsgtk_browser_table = { .poll = gui_poll, @@ -1166,7 +1052,7 @@ int main(int argc, char** argv) .window = nsgtk_window_table, .clipboard = &nsgtk_clipboard_table, .download = nsgtk_download_table, - .fetch = &nsgtk_fetch_table, + .fetch = nsgtk_fetch_table, }; /* check home directory is available */ diff --git a/gtk/gui.h b/gtk/gui.h index 65a6e0742..dc1f2b340 100644 --- a/gtk/gui.h +++ b/gtk/gui.h @@ -59,6 +59,8 @@ extern char *themelist_file_location; extern GdkPixbuf *favicon_pixbuf; /* favicon default pixbuf */ +extern char **respaths; /** resource search path vector */ + uint32_t gtk_gui_gdkkey_to_nskey(GdkEventKey *); extern void gui_401login_open(nsurl *url, const char *realm, 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) diff --git a/riscos/gui.c b/riscos/gui.c index 8495dd6aa..43e2f3a4e 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -719,6 +719,135 @@ void ro_gui_check_resolvers(void) } } +/** + * Convert a RISC OS pathname to a file: URL. + * + * \param path RISC OS pathname + * \return URL, allocated on heap, or 0 on failure + */ + +static char *path_to_url(const char *path) +{ + int spare; + char *canonical_path; /* canonicalised RISC OS path */ + char *unix_path; /* unix path */ + char *escurl; + os_error *error; + url_func_result url_err; + int urllen; + char *url; /* resulting url */ + + /* calculate the canonical risc os path */ + error = xosfscontrol_canonicalise_path(path, 0, 0, 0, 0, &spare); + if (error) { + LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s", + error->errnum, error->errmess)); + warn_user("PathToURL", error->errmess); + return NULL; + } + + canonical_path = malloc(1 - spare); + if (canonical_path == NULL) { + LOG(("malloc failed")); + warn_user("NoMemory", 0); + free(canonical_path); + return NULL; + } + + error = xosfscontrol_canonicalise_path(path, canonical_path, 0, 0, 1 - spare, 0); + if (error) { + LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s", + error->errnum, error->errmess)); + warn_user("PathToURL", error->errmess); + free(canonical_path); + return NULL; + } + + /* create a unix path from teh cananocal risc os one */ + unix_path = __unixify(canonical_path, __RISCOSIFY_NO_REVERSE_SUFFIX, NULL, 0, 0); + + if (unix_path == NULL) { + LOG(("__unixify failed: %s", canonical_path)); + free(canonical_path); + return NULL; + } + free(canonical_path); + + /* convert the unix path into a url */ + urllen = strlen(unix_path) + FILE_SCHEME_PREFIX_LEN + 1; + url = malloc(urllen); + if (url == NULL) { + LOG(("Unable to allocate url")); + free(unix_path); + return NULL; + } + + if (*unix_path == '/') { + snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path + 1); + } else { + snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path); + } + free(unix_path); + + /* We don't want '/' to be escaped. */ + url_err = url_escape(url, FILE_SCHEME_PREFIX_LEN, false, "/", &escurl); + free(url); url = NULL; + if (url_err != URL_FUNC_OK) { + LOG(("url_escape failed: %s", url)); + return NULL; + } + + return escurl; +} + + +/** + * Convert a file: URL to a RISC OS pathname. + * + * \param url a file: URL + * \return RISC OS pathname, allocated on heap, or 0 on failure + */ + +static char *url_to_path(const char *url) +{ + char *path; + char *filename; + char *respath; + url_func_result res; /* result from url routines */ + char *r; + + res = url_path(url, &path); + if (res != URL_FUNC_OK) { + warn_user("NoMemory", 0); + return NULL; + } + + res = url_unescape(path, &respath); + free(path); + if (res != URL_FUNC_OK) { + return NULL; + } + + /* RISC OS path should not be more than 100 characters longer */ + filename = malloc(strlen(respath) + 100); + if (!filename) { + free(respath); + warn_user("NoMemory", 0); + return NULL; + } + + r = __riscosify(respath, 0, __RISCOSIFY_NO_SUFFIX, + filename, strlen(respath) + 100, 0); + + free(respath); + if (r == 0) { + free(filename); + LOG(("__riscosify failed")); + return NULL; + } + + return filename; +} /** * Last-minute gui init, after all other modules have initialised. @@ -838,7 +967,6 @@ static bool nslog_stream_configure(FILE *fptr) } - /** * Close down the gui (RISC OS). */ @@ -1873,135 +2001,6 @@ void ro_msg_window_info(wimp_message *message) } -/** - * Convert a RISC OS pathname to a file: URL. - * - * \param path RISC OS pathname - * \return URL, allocated on heap, or 0 on failure - */ - -char *path_to_url(const char *path) -{ - int spare; - char *canonical_path; /* canonicalised RISC OS path */ - char *unix_path; /* unix path */ - char *escurl; - os_error *error; - url_func_result url_err; - int urllen; - char *url; /* resulting url */ - - /* calculate the canonical risc os path */ - error = xosfscontrol_canonicalise_path(path, 0, 0, 0, 0, &spare); - if (error) { - LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s", - error->errnum, error->errmess)); - warn_user("PathToURL", error->errmess); - return NULL; - } - - canonical_path = malloc(1 - spare); - if (canonical_path == NULL) { - LOG(("malloc failed")); - warn_user("NoMemory", 0); - free(canonical_path); - return NULL; - } - - error = xosfscontrol_canonicalise_path(path, canonical_path, 0, 0, 1 - spare, 0); - if (error) { - LOG(("xosfscontrol_canonicalise_path failed: 0x%x: %s", - error->errnum, error->errmess)); - warn_user("PathToURL", error->errmess); - free(canonical_path); - return NULL; - } - - /* create a unix path from teh cananocal risc os one */ - unix_path = __unixify(canonical_path, __RISCOSIFY_NO_REVERSE_SUFFIX, NULL, 0, 0); - - if (unix_path == NULL) { - LOG(("__unixify failed: %s", canonical_path)); - free(canonical_path); - return NULL; - } - free(canonical_path); - - /* convert the unix path into a url */ - urllen = strlen(unix_path) + FILE_SCHEME_PREFIX_LEN + 1; - url = malloc(urllen); - if (url == NULL) { - LOG(("Unable to allocate url")); - free(unix_path); - return NULL; - } - - if (*unix_path == '/') { - snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path + 1); - } else { - snprintf(url, urllen, "%s%s", FILE_SCHEME_PREFIX, unix_path); - } - free(unix_path); - - /* We don't want '/' to be escaped. */ - url_err = url_escape(url, FILE_SCHEME_PREFIX_LEN, false, "/", &escurl); - free(url); url = NULL; - if (url_err != URL_FUNC_OK) { - LOG(("url_escape failed: %s", url)); - return NULL; - } - - return escurl; -} - - -/** - * Convert a file: URL to a RISC OS pathname. - * - * \param url a file: URL - * \return RISC OS pathname, allocated on heap, or 0 on failure - */ - -char *url_to_path(const char *url) -{ - char *path; - char *filename; - char *respath; - url_func_result res; /* result from url routines */ - char *r; - - res = url_path(url, &path); - if (res != URL_FUNC_OK) { - warn_user("NoMemory", 0); - return NULL; - } - - res = url_unescape(path, &respath); - free(path); - if (res != URL_FUNC_OK) { - return NULL; - } - - /* RISC OS path should not be more than 100 characters longer */ - filename = malloc(strlen(respath) + 100); - if (!filename) { - free(respath); - warn_user("NoMemory", 0); - return NULL; - } - - r = __riscosify(respath, 0, __RISCOSIFY_NO_SUFFIX, - filename, strlen(respath) + 100, 0); - - free(respath); - if (r == 0) { - free(filename); - LOG(("__riscosify failed")); - return NULL; - } - - return filename; -} /** @@ -2357,6 +2356,8 @@ static struct gui_fetch_table riscos_fetch_table = { .filename_from_path = filename_from_path, .path_add_part = path_add_part, .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, .get_resource_url = gui_get_resource_url, .mimetype = fetch_mimetype, diff --git a/utils/url.h b/utils/url.h index 7c716d723..621e62f0d 100644 --- a/utils/url.h +++ b/utils/url.h @@ -58,7 +58,4 @@ url_func_result url_escape(const char *unescaped, size_t toskip, url_func_result url_unescape(const char *str, char **result); url_func_result url_path(const char *url, char **result); -char *path_to_url(const char *path); -char *url_to_path(const char *url); - #endif diff --git a/windows/findfile.h b/windows/findfile.h index 5f8c7290c..8a3e719ec 100644 --- a/windows/findfile.h +++ b/windows/findfile.h @@ -23,5 +23,8 @@ extern char *nsws_find_resource(char *buf, const char *filename, const char *def char **nsws_init_resource(const char *resource_path); +char *path_to_url(const char *path); +char *url_to_path(const char *url); + #endif /* _NETSURF_WINDOWS_FINDFILE_H_ */ diff --git a/windows/gui.c b/windows/gui.c index a6b2facaf..6ac4f6155 100644 --- a/windows/gui.c +++ b/windows/gui.c @@ -1882,6 +1882,8 @@ static struct gui_fetch_table fetch_table = { .filename_from_path = filename_from_path, .path_add_part = path_add_part, .filetype = fetch_filetype, + .path_to_url = path_to_url, + .url_to_path = url_to_path, .mimetype = fetch_mimetype, }; -- cgit v1.2.3