shithub: purgatorio

Download patch

ref: 2459e34d77e2e21ef829c0dfaafd99433899494f
parent: c3101e4da486b2bf2e8b911bcb62dd8ec31c41bf
author: henesy <devnull@localhost>
date: Tue Jul 9 22:06:23 EDT 2019

add -r,-c flags to os(1) to allow for more intuitive control for running external programs inside the cwd of Inferno

--- a/appl/cmd/os.b	Tue Jul  9 22:05:34 2019
+++ b/appl/cmd/os.b	Tue Jul  9 22:06:23 2019
@@ -8,6 +8,12 @@
 include "string.m";
 	str: String;
 
+include "env.m";
+	env: Env;
+
+include "workdir.m";
+	wd: Workdir;
+
 include "arg.m";
 
 Os: module
@@ -21,21 +27,35 @@
 	str = load String String->PATH;
 	if(str == nil)
 		fail(sys->sprint("cannot load %s: %r", String->PATH));
+	env = load Env Env->PATH;
+	if(env == nil)
+		fail(sys->sprint("cannot load %s: %r", Env->PATH));
+	wd= load Workdir Workdir->PATH;
+	if(wd== nil)
+		fail(sys->sprint("cannot load %s: %r", Workdir->PATH));
 	arg := load Arg Arg->PATH;
 	if(arg == nil)
 		fail(sys->sprint("cannot load %s: %r", Arg->PATH));
 
 	arg->init(args);
-	arg->setusage("os [-d dir] [-m mount] [-n] [-N nice] [-b] command [arg...]");
+	arg->setusage("os [-rc] [-d dir] [-m mount] [-n] [-N nice] [-b] command [arg...]");
 
 	nice := 0;
 	nicearg: string;
-	workdir := "";
+	workdir:= "";
 	mntpoint := "";
 	foreground := 1;
+	rooted := 0;
+	usecwd := 0;
 
 	while((opt := arg->opt()) != 0) {
 		case opt {
+		'r' =>
+			rooted = 1;
+		'c' =>
+			# Since the cwd is rooted, we set rooted flag
+			rooted = 1;
+			usecwd = 1;
 		'd' =>
 			workdir = arg->earg();
 		'm' =>
@@ -78,6 +98,15 @@
 	wfd := sys->open(dir+"/wait", Sys->OREAD);
 	if(nice && sys->fprint(cfd, "nice%s", nicearg) < 0)
 		sys->fprint(sys->fildes(2), "os: warning: can't set nice priority: %r\n");
+
+	if(usecwd)
+		workdir = wd->init();
+
+	if(rooted){
+		# If $emuroot is not set, don't care, directory is checked below
+		emuroot := env->getenv("emuroot");
+		workdir = emuroot + workdir;
+	}
 
 	if(workdir != nil && sys->fprint(cfd, "dir %s", workdir) < 0)
 		fail(sys->sprint("cannot set cwd %q: %r", workdir));
--- a/man/1/os	Tue Jul  9 22:05:34 2019
+++ b/man/1/os	Tue Jul  9 22:06:23 2019
@@ -6,6 +6,8 @@
 .br
 .B os
 [
+.B -rc
+] [
 .B -b
 ] [
 .B -m
@@ -44,7 +46,23 @@
 .IR dir ;
 an error results and the command will not run if
 .I dir
-does not exist or is inaccessible.
+does not exist or is inaccessible. If the
+.B -r
+option is specified, 
+.B $emuroot
+will be prepended to the operating directory, which may include
+.I dir
+if specified. If the
+.B -c
+option is specified, the current working directory will be used for execution, setting
+the 
+.B -r
+option in the process. 
+Note that the
+.B -c
+option overrides the 
+.B -d
+option. 
 The standard output and standard error of the command appear on the standard output
 and standard error streams of the
 .I os