code: 9ferno

Download patch

ref: eb81a50383aca3b30ce3687f5daf7a86c68abae2
parent: 47e76e81e288741c8e6fee4f69f963551e14985c
author: henesy <unknown>
date: Sun May 30 20:01:54 EDT 2021

sh builtin run now searches path

--- a/appl/cmd/sh/sh.b
+++ b/appl/cmd/sh/sh.b
@@ -741,6 +741,43 @@
 	return 0;
 }
 
+# Expand a program name to the full path
+pathexpand(ctxt: ref Context, progname: string): string 
+{
+	disfile := 0;
+	pathlist: list of string;
+
+	if (len progname >= 4 && progname[len progname-4:] == ".dis")
+		disfile = 1;
+
+	if (absolute(progname))
+		pathlist = list of {""};
+
+	else if ((pl := ctxt.get("path")) != nil)
+		pathlist = list2stringlist(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) {
+			fd = sys->open(npath += ".dis", sys->OREAD);
+			if(fd != nil){
+				return npath;
+			}
+		}
+	}
+
+	return progname;
+}
+
 runexternal(ctxt: ref Context, args: list of ref Listnode, last: int): string
 {
 	progname := (hd args).word;
@@ -1903,7 +1940,8 @@
 	ctxt.push();
 	{
 		ctxt.setoptions(ctxt.INTERACTIVE, 0);
-		runscript(ctxt, (hd tl args).word, tl tl args, 1);
+		path := pathexpand(ctxt, (hd tl args).word);
+		runscript(ctxt, path, tl tl args, 1);
 		ctxt.pop();
 		return nil;
 	} exception e {
--- a/appl/cmd/sh/sh.y
+++ b/appl/cmd/sh/sh.y
@@ -845,6 +845,43 @@
 	return 0;
 }
 
+# Expand a program name to the full path
+pathexpand(ctxt: ref Context, progname: string): string 
+{
+	disfile := 0;
+	pathlist: list of string;
+
+	if (len progname >= 4 && progname[len progname-4:] == ".dis")
+		disfile = 1;
+
+	if (absolute(progname))
+		pathlist = list of {""};
+
+	else if ((pl := ctxt.get("path")) != nil)
+		pathlist = list2stringlist(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) {
+			fd = sys->open(npath += ".dis", sys->OREAD);
+			if(fd != nil){
+				return npath;
+			}
+		}
+	}
+
+	return progname;
+}
+
 runexternal(ctxt: ref Context, args: list of ref Listnode, last: int): string
 {
 	progname := (hd args).word;
@@ -2053,7 +2090,8 @@
 	ctxt.push();
 	{
 		ctxt.setoptions(ctxt.INTERACTIVE, 0);
-		runscript(ctxt, (hd tl args).word, tl tl args, 1);
+		path := pathexpand(ctxt, (hd tl args).word);
+		runscript(ctxt, path, tl tl args, 1);
 		ctxt.pop();
 		return nil;
 	} exception e {