commit: 37a54a7d797b9291d4812e2b5a96adb7344dc5be
parent: fd718773414258cf187a4ed122dc9c4e949d1fb1
author: Chris Noxz <chris@noxz.tech>
date: Fri, 29 Dec 2023 14:09:47 +0100
enable word expansion in uri
~/ is now expanded into $HOME/. however, if the uri contains spaces
these will have to be escaped '\ ' as .we_wordv[0] only checks the
first word. a bloated solution would be to first join all words
with spaces as separators, but these is not a planned implementation.
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/browser.c b/browser.c
@@ -1283,6 +1283,8 @@ resolve_uri(const gchar *t)
*l = NULL, /* temporary string */
*e = NULL; /* uri encoded string */
const gchar *s = NULL;
+ wordexp_t x; /* wordexp struct */
+ int r = -1; /* result holder for wordexp */
/* check if valid scheme, and if so just create a copy of the text */
if ((s = g_uri_peek_scheme(t)) && g_strv_contains(CFG_L(UriSchemes), s))
@@ -1290,8 +1292,14 @@ resolve_uri(const gchar *t)
/* if no match with, test xdg schemes (schemes that are redirected) */
else if (s && CFG_L(XdgSchemes) && g_strv_contains(CFG_L(XdgSchemes), s))
xdg_open(s, t);
- /* if path is local, use the file scheme */
- else if ((l = realpath(t, NULL)) != NULL)
+ /* if path is local, use the file scheme, else try to see if the string is
+ * expandable and is a local path.
+ * note: only the first word is checked. to check path with spaces, the
+ * spaces need to be escaped:
+ * ~/dir with space -> $HOME/dir
+ * ~/dir\ with\ space -> $HOME/dir with space */
+ else if ((l = realpath(t, NULL)) != NULL || ((r = wordexp(t, &x, 0)) == 0
+ && (l = realpath(x.we_wordv[0], NULL)) != NULL))
u = g_strdup_printf("file://%s", l);
/* else, check if the text can be interpreted as a valid https uri; it's
* not enough to check if uri is valid - check for period and no spaces */
@@ -1305,6 +1313,8 @@ resolve_uri(const gchar *t)
(e = g_uri_escape_string(t, NULL, FALSE))
);
+ if (r != -1) /* r != -1 indicates that wordexp() was executed */
+ wordfree(&x);
g_free(e);
g_free(l);
return u; /* return a pointer that the caller is responsible for freeing */
diff --git a/browser.h b/browser.h
@@ -33,6 +33,7 @@
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <webkit2/webkit2.h>
+#include <wordexp.h>
#define URI_MAX 2000
#define SUFFIX_MAX 100