~sircmpwn/rc-devel

interp: expand tilde v1 APPLIED

Curtis Arthaud: 1
 interp: expand tilde

 1 files changed, 12 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/rc-devel/patches/44835/mbox | git am -3
Learn more about email & git

[PATCH] interp: expand tilde Export this patch

Signed-off-by: Curtis Arthaud <uku82@gmx.fr>
---
 interp/value.ha | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/interp/value.ha b/interp/value.ha
index 2d762b0..4993987 100644
--- a/interp/value.ha
+++ b/interp/value.ha
@@ -5,6 +5,7 @@ use glob;
use io;
use os::exec;
use os;
use path;
use rt;
use strconv;
use strings;
@@ -20,6 +21,9 @@ export fn expand(state: *state, val: *ast::value, glob: bool) (value | error) =
	case let val: ast::string =>
		return strings::dup(val);
	case let val: ast::argument =>
		if (strings::hasprefix(val, '~')) {
			val = expand_tilde(val);
		};
		if (glob && strings::contains(val, '*')) {
			return expand_glob(val)?;
		};
@@ -92,6 +96,14 @@ export fn expand(state: *state, val: *ast::value, glob: bool) (value | error) =
	};
};

fn expand_tilde(pattern: str) str = {
	const home = os::getenv("HOME") as str;
	let buf = path::init(pattern)!;
	path::popprefix(&buf, "~")!;
	path::prepend(&buf, home)!;
	return strings::dup(path::string(&buf));
};

fn expand_glob(pattern: str) (value | error) = {
	let result: []value = [];

--
2.42.0
FWIW this omits the ~username form (which expands to /home/username, or
whatever via /etc/passwd maybe?), but I'm cool with this as-is. Thanks!

To git@git.sr.ht:~sircmpwn/rc
   b01c720..95387be  master -> master