shithub: purgatorio

Download patch

ref: ec35f468e0eba87c9f09cbbe5fa8af2591e6f914
parent: 7ae7351ace73a70cbbaf5a2da016005485d0d43b
author: henesy <devnull@localhost>
date: Mon May 31 15:02:46 EDT 2021

names.m: add absolute and searchname

--- a/appl/lib/names.b
+++ b/appl/lib/names.b
@@ -1,8 +1,11 @@
 implement Names;
 
 include "sys.m";
+	sys: Sys;
 
 include "names.m";
+include "env.m";
+include "string.m";
 
 # return name rewritten to compress /+, eliminate ., and interpret ..
 
@@ -133,7 +136,8 @@
 
 elements(name: string): list of string
 {
-	sys := load Sys Sys->PATH;
+	if(sys == nil)
+		sys = load Sys Sys->PATH;
 	(nil, fld) := sys->tokenize(name, "/");
 	if(name != nil && name[0] == '/')
 		fld = "/" :: fld;
@@ -152,3 +156,63 @@
 	}
 	return name;
 }
+
+# Is a given path an absolute path?
+absolute(p: string): int
+{
+	if (len p < 2)
+		return 0;
+	if (p[0] == '/' || p[0] == '#')
+		return 1;
+	if (len p < 3 || p[0] != '.')
+		return 0;
+	if (p[1] == '/')
+		return 1;
+	if (p[1] == '.' && p[2] == '/')
+		return 1;
+	return 0;
+}
+
+# Search for name in $path or use sane default
+# Return the full path found or the same name
+searchname(progname: string): string
+{
+	if(sys == nil)
+		sys = load Sys Sys->PATH;
+	env := load Env Env->PATH;
+	str := load String String->PATH;
+	disfile := 0;
+	pathlist: list of string;
+
+	if (len progname >= 4 && progname[len progname-4:] == ".dis")
+		disfile = 1;
+
+	if (absolute(progname))
+		pathlist = nil;
+
+	else if ((pl := env->getenv("path")) != nil)
+		pathlist = str->unquoted(pl);
+	else
+		pathlist = list of {"/dis", "."};
+
+	for(; pathlist != nil; pathlist = tl pathlist)
+	{
+		npath := hd pathlist + "/" + progname;
+
+		fd := sys->open(npath, sys->OREAD);
+		if(fd != nil){
+			return npath;
+		}
+
+		if (!disfile) {
+			npath += ".dis";
+			fd = sys->open(npath, sys->OREAD);
+			if(fd != nil){
+				return npath;
+			}
+		}
+	}
+
+	return progname;
+}
+
--- a/module/names.m
+++ b/module/names.m
@@ -10,4 +10,6 @@
 	pathname:	fn(els: list of string): string;
 	rooted:	fn(root: string, name: string): string;
 	relative:	fn(name: string, root: string): string;
+	absolute:	fn(p: string): int;
+	searchname:	fn(progname: string): string;
 };