diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-02-18 16:04:48 +0100 |
---|---|---|
committer | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-02-18 16:06:42 +0100 |
commit | 6b79a74601b57500ff786e1bb9f9ec94e72416ad (patch) | |
tree | 0a67899f7150489acbcdca294c214e5c6c451453 | |
parent | 3f16e8e2b444a4da5aa99173b06b77d0b5d9e1c3 (diff) |
Expand tilde to home directory in local URLs
Allows paths like "file:~/" and "file:~/.dillo/dillorc" to be opened by
Dillo by expanding the tilde character '~' to the value of the $HOME
environment variable.
Fixes: https://github.com/dillo-browser/dillo/issues/81
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | dpi/file.c | 35 | ||||
-rw-r--r-- | src/uicmd.cc | 2 |
3 files changed, 29 insertions, 9 deletions
@@ -57,6 +57,7 @@ dillo-3.1 [not released yet] - Switch tabs using the mouse wheel by default. Use the new option scroll_switches_tabs to disable the behavior. - Fix OpenSSL handling of unexpected EOF without close notify alert. + - Expand home tilde '~' in the file plugin. Patches: Rodrigo Arias Mallo <rodarima@gmail.com> ----------------------------------------------------------------------------- @@ -2,6 +2,7 @@ * File: file.c :) * * Copyright (C) 2000-2007 Jorge Arellano Cid <jcid@dillo.org> + * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -138,7 +139,7 @@ static const char *File_get_content_type_from_data(void *Data, size_t Size) char *p = Data; size_t i, non_ascci; - _MSG("File_get_content_type_from_data:: Size = %d\n", Size); + _MSG("File_get_content_type_from_data:: Size = %zu\n", Size); /* HTML try */ for (i = 0; i < Size && dIsspace(p[i]); ++i); @@ -790,6 +791,7 @@ static int File_parse_hex_octet(const char *s) /* * Make a file URL into a human (and machine) readable path. + * The home tile '~' character is expanded from the value of $HOME. * The idea is to always have a path that starts with only one slash. * Embedded slashes are ignored. */ @@ -797,16 +799,35 @@ static char *File_normalize_path(const char *orig) { char *str = (char *) orig, *basename = NULL, *ret = NULL, *p; - dReturn_val_if (orig == NULL, ret); + if (str == NULL) + return NULL; - /* Make sure the string starts with "file:/" */ - if (dStrnAsciiCasecmp(str, "file:/", 5) != 0) + /* Make sure the string starts with "file:" */ + if (dStrnAsciiCasecmp(str, "file:", 5)) return ret; - str += 5; - /* Skip "localhost" */ - if (dStrnAsciiCasecmp(str, "//localhost/", 12) == 0) + str += 5; /* skip "file:" */ + + Dstr *tmp = dStr_sized_new(32); + + if (str[0] == '~' && (str[1] == '/' || str[1] == '\0')) { + /* Expand home tilde "~" into "/home/userxyz" */ + const char *home = getenv("HOME"); + if (home == NULL || home[0] == '\0') { + _MSG("cannot get home path from the environment variable HOME\n"); + return NULL; + } + /* Add separator if needed */ + char *sep = home[strlen(home) - 1] == '/' ? "" : "/"; + char *next = str + 1; + while (*next == '/') + next++; + dStr_sprintf(tmp, "%s%s%s", home, sep, next); + str = tmp->str; + } else if (dStrnAsciiCasecmp(str, "//localhost/", 12) == 0) { + /* Skip "//localhost" */ str += 11; + } /* Skip packed slashes, and leave just one */ while (str[0] == '/' && str[1] == '/') diff --git a/src/uicmd.cc b/src/uicmd.cc index e153ba70..5eb64b7b 100644 --- a/src/uicmd.cc +++ b/src/uicmd.cc @@ -730,8 +730,6 @@ void a_UIcmd_open_urlstr(void *vbw, const char *urlstr) ch = new_urlstr[5]; if (!ch || ch == '.') { url = a_Url_new(Paths::getOldWorkingDir(), "file:"); - } else if (ch == '~') { - url = a_Url_new(dGethomedir(), "file:"); } else { url = a_Url_new(new_urlstr, "file:"); } |