~sircmpwn/rc-devel

interp: implement ~user v3 APPLIED

Curtis Arthaud: 1
 interp: implement ~user

 1 files changed, 34 insertions(+), 4 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/46235/mbox | git am -3
Learn more about email & git

[PATCH v3] interp: implement ~user Export this patch

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

diff --git a/interp/value.ha b/interp/value.ha
index 1ac8653..50e6921 100644
--- a/interp/value.ha
+++ b/interp/value.ha
@@ -9,6 +9,8 @@ use path;
use rt;
use strconv;
use strings;
use unix::passwd;
use unix;

// A value object.
// TODO: s/[]value/[]str/g
@@ -97,11 +99,39 @@ 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));

	let iter = path::iter(&buf);
	const prefix = match (path::nextiter(&iter)) {
		case let s: str =>
			yield s;
		case =>
			abort();
	};

	const pw = if (prefix == "~") {
		yield passwd::getuid(unix::getuid());
	} else {
		const user = strings::dup(strings::ltrim(prefix, '~'));
		yield passwd::getuser(user);
	};
	match (pw) {
	case void =>
		void;
	case =>
		const pw = pw as passwd::pwent;
		defer passwd::pwent_finish(&pw);
		const home = strings::dup(pw.homedir);
		path::popprefix(&buf, prefix)!;
		path::prepend(&buf, home)!;
	};

	const s = path::string(&buf);
	if (strings::hassuffix(pattern, '/')) {
		return strings::join("", s, "/");
	} else {
		return strings::dup(s);
	};
};

fn expand_glob(pattern: str) (value | error) = {
--
2.42.0
Thanks!

To git@git.sr.ht:~sircmpwn/rc
   0c6918c..87b794f  master -> master