shithub: purgatorio

Download patch

ref: e11c7aa718df592bd69de53ce1d6498cc870f256
parent: 606901dc5da9cb09acb5593c5cf74ce1b52ca6e2
author: henesy <devnull@localhost>
date: Wed Mar 13 19:20:12 EDT 2019

add usage to acme ;; remove util arg/env redundant functionality ;; fix acme failing to start starting a trashed draw context

--- a/appl/acme/acme.b
+++ b/appl/acme/acme.b
@@ -4,6 +4,8 @@
 
 sys : Sys;
 bufio : Bufio;
+env : Env;
+arg : Arg;
 workdir : Workdir;
 drawm : Draw;
 styx : Styx;
@@ -42,8 +44,8 @@
 Xfid : import xfidm;
 cmouse, ckeyboard, cwait, ccommand, ckill, cxfidalloc, cxfidfree, cerr, cplumb, cedit : import dat;
 font, bflush, balloc, draw : import graph;
-Arg, PNPROC, PNGROUP : import utils;
-arginit, argopt, argf, error, warning, postnote : import utils;
+PNPROC, PNGROUP : import utils;
+error, warning, postnote : import utils;
 yellow, green, red, blue, black, white, mainwin, display : import gui;
 Disk : import diskm;
 Row : import rowm;
@@ -57,120 +59,6 @@
 tfd : ref Sys->FD;
 lasttime : int;
 
-init(ctxt : ref Draw->Context, argl : list of string)
-{
-	acmectxt = ctxt;
-
-	sys = load Sys Sys->PATH;
-	sys->pctl(Sys->NEWPGRP, nil);
-
-	{
-		# tfd = sys->create("./time", Sys->OWRITE, 8r600);
-		# lasttime = sys->millisec();
-		bufio = load Bufio Bufio->PATH;
-		workdir = load Workdir Workdir->PATH;
-		drawm = load Draw Draw->PATH;
-	
-		styx = load Styx Styx->PATH;
-	
-		acme = load Acme SELF;
-	
-		gui = load Gui path(Gui->PATH);
-		graph = load Graph path(Graph->PATH);
-		dat = load Dat path(Dat->PATH);
-		framem = load Framem path(Framem->PATH);
-		utils = load Utils path(Utils->PATH);
-		regx = load Regx path(Regx->PATH);
-		scrl = load Scroll path(Scroll->PATH);
-		textm = load Textm path(Textm->PATH);
-		filem = load Filem path(Filem->PATH);
-		windowm = load Windowm path(Windowm->PATH);
-		rowm = load Rowm path(Rowm->PATH);
-		columnm = load Columnm path(Columnm->PATH);
-		bufferm = load Bufferm path(Bufferm->PATH);
-		diskm = load Diskm path(Diskm->PATH);
-		exec = load Exec path(Exec->PATH);
-		look = load Look path(Look->PATH);
-		timerm = load Timerm path(Timerm->PATH);
-		fsys = load Fsys path(Fsys->PATH);
-		xfidm = load Xfidm path(Xfidm->PATH);
-		plumbmsg = load Plumbmsg Plumbmsg->PATH;
-		editm = load Edit path(Edit->PATH);
-		editlog = load Editlog path(Editlog->PATH);
-		editcmd = load Editcmd path(Editcmd->PATH);
-		styxaux = load Styxaux path(Styxaux->PATH);
-		
-		mods := ref Dat->Mods(sys, bufio, drawm, styx, styxaux,
-						acme, gui, graph, dat, framem,
-						utils, regx, scrl,
-						textm, filem, windowm, rowm, columnm,
-						bufferm, diskm, exec, look, timerm,
-						fsys, xfidm, plumbmsg, editm, editlog, editcmd);
-	
-		styx->init();
-		styxaux->init();
-	
-		utils->init(mods);
-		gui->init(mods);
-		graph->init(mods);
-		dat->init(mods);
-		framem->init(mods);
-		regx->init(mods);
-		scrl->init(mods);
-		textm->init(mods);
-		filem->init(mods);
-		windowm->init(mods);
-		rowm->init(mods);
-		columnm->init(mods);
-		bufferm->init(mods);
-		diskm->init(mods);
-		exec->init(mods);
-		look->init(mods);
-		timerm->init(mods);
-		fsys->init(mods);
-		xfidm->init(mods);
-		editm->init(mods);
-		editlog->init(mods);
-		editcmd->init(mods);
-	
-		utils->debuginit();
-	
-		if (plumbmsg->init(1, "edit", Dat->PLUMBSIZE) >= 0)
-			plumbed = 1;
-	
-		main(argl);
-	
-	}
-#	exception{
-#		* =>
-#			sys->fprint(sys->fildes(2), "acme: fatal: %s\n", utils->getexc());
-#			sys->print("acme: fatal: %s\n", utils->getexc());
-#			shutdown("error");
-#	}
-}
-
-timing(s : string)
-{
-	thistime := sys->millisec();
-	sys->fprint(tfd, "%s	%d\n", s, thistime-lasttime);
-	lasttime = thistime;
-}
-
-path(p : string) : string
-{
-	if (RELEASECOPY)
-		return p;
-	else {
-		# inlined strrchr since not loaded yet
-		 for (n := len p - 1; n >= 0; n--)
-			if (p[n] == '/')
-				break;
-		 if (n >= 0)
-			p = p[n+1:];
-		 return "/usr/jrf/acme/" + p;
-	}
-}
-
 waitpid0, waitpid1 : int;
 mainpid : int;
 
@@ -189,15 +77,68 @@
 NSnarf : con 32;
 snarfrune : ref Dat->Astring;
 
-main(argl : list of string)
+init(ctxt : ref Draw->Context, argl : list of string)
 {
 	i, ac : int;
 	loadfile : string;
 	p : int;
 	c : ref Column;
-	arg : ref Arg;
 	ncol : int;
+	acmectxt = ctxt;
 
+	sys = load Sys Sys->PATH;
+	sys->pctl(Sys->NEWPGRP, nil);
+
+	# tfd = sys->create("./time", Sys->OWRITE, 8r600);
+	# lasttime = sys->millisec();
+	arg = load Arg Arg->PATH;
+	env = load Env Env->PATH;
+	bufio = load Bufio Bufio->PATH;
+	workdir = load Workdir Workdir->PATH;
+	drawm = load Draw Draw->PATH;
+
+	styx = load Styx Styx->PATH;
+
+	acme = load Acme SELF;
+
+	gui = load Gui path(Gui->PATH);
+	graph = load Graph path(Graph->PATH);
+	dat = load Dat path(Dat->PATH);
+	framem = load Framem path(Framem->PATH);
+	utils = load Utils path(Utils->PATH);
+	regx = load Regx path(Regx->PATH);
+	scrl = load Scroll path(Scroll->PATH);
+	textm = load Textm path(Textm->PATH);
+	filem = load Filem path(Filem->PATH);
+	windowm = load Windowm path(Windowm->PATH);
+	rowm = load Rowm path(Rowm->PATH);
+	columnm = load Columnm path(Columnm->PATH);
+	bufferm = load Bufferm path(Bufferm->PATH);
+	diskm = load Diskm path(Diskm->PATH);
+	exec = load Exec path(Exec->PATH);
+	look = load Look path(Look->PATH);
+	timerm = load Timerm path(Timerm->PATH);
+	fsys = load Fsys path(Fsys->PATH);
+	xfidm = load Xfidm path(Xfidm->PATH);
+	plumbmsg = load Plumbmsg Plumbmsg->PATH;
+	editm = load Edit path(Edit->PATH);
+	editlog = load Editlog path(Editlog->PATH);
+	editcmd = load Editcmd path(Editcmd->PATH);
+	styxaux = load Styxaux path(Styxaux->PATH);
+	
+	mods := ref Dat->Mods(sys, bufio, env, arg, 
+					drawm, styx, styxaux,
+					acme, gui, graph, dat, framem,
+					utils, regx, scrl,
+					textm, filem, windowm, rowm, columnm,
+					bufferm, diskm, exec, look, timerm,
+					fsys, xfidm, plumbmsg, editm, editlog, editcmd);
+
+	arg->init(argl);
+	styx->init();
+	styxaux->init();
+
+	# Commandline flag handling
 	ncol = -1;
 
 	mainpid = sys->pctl(0, nil);
@@ -204,40 +145,75 @@
 	loadfile = nil;
 	fontnames = array[2] of string;
 	fontnames[0:] = deffontnames[0:2];
-	f := utils->getenv("acme-font");
+	f := env->getenv("acme-font");
 	if (f != nil)
 		fontnames[0] = f;
 	else {
-		f = utils->getenv("font");
+		f = env->getenv("font");
 		if (f != nil)
 			fontnames[0] = f;
 	}
-	f = utils->getenv("acme-Font");
+	f = env->getenv("acme-Font");
 	if (f != nil)
 		fontnames[1] = f;
 	else {
-		f = utils->getenv("Font");
+		f = env->getenv("Font");
 		if (f != nil)
 			fontnames[0] = f;
 	}
-	arg = arginit(argl);
-	while(ac = argopt(arg)) case(ac){
-	'b' =>
-		dat->bartflag = TRUE;
-	'c' =>
-		ncol = int argf(arg);
-	'f' =>
-		fontnames[0] = argf(arg);
-	'F' =>
-		fontnames[1] = argf(arg);
-	'l' =>
-		loadfile = argf(arg);
-	}
 
-	dat->home = utils->getenv("home");
+	arg->setusage("acme [-f varfont] [-F fixfont] [-c ncol] [-b] [-l file | file ...]");
+
+	while((ac = arg->opt()) != 0)
+		case(ac){
+		'b' =>
+			dat->bartflag = TRUE;
+		'c' =>
+			ncol = int arg->earg();
+		'f' =>
+			fontnames[0] = arg->earg();
+		'F' =>
+			fontnames[1] = arg->earg();
+		'l' =>
+			loadfile = arg->earg();
+		* =>
+			arg->usage();
+		}
+	argl = arg->argv();
+
+	utils->init(mods);
+	gui->init(mods);
+	graph->init(mods);
+	dat->init(mods);
+	framem->init(mods);
+	regx->init(mods);
+	scrl->init(mods);
+	textm->init(mods);
+	filem->init(mods);
+	windowm->init(mods);
+	rowm->init(mods);
+	columnm->init(mods);
+	bufferm->init(mods);
+	diskm->init(mods);
+	exec->init(mods);
+	look->init(mods);
+	timerm->init(mods);
+	fsys->init(mods);
+	xfidm->init(mods);
+	editm->init(mods);
+	editlog->init(mods);
+	editcmd->init(mods);
+
+	utils->debuginit();
+
+	if (plumbmsg->init(1, "edit", Dat->PLUMBSIZE) >= 0)
+		plumbed = 1;
+
+	# Begin main logic
+	dat->home = env->getenv("home");
 	if (dat->home == nil)
 		dat->home = utils->gethome(utils->getuser());
-	ts := utils->getenv("tabstop");
+	ts := env->getenv("tabstop");
 	if (ts != nil)
 		maxtab = int ts;
 	if (maxtab <= 0)
@@ -244,7 +220,7 @@
 		maxtab = 4;
 	snarfrune = utils->stralloc(NSnarf);
 	sys->pctl(Sys->FORKNS|Sys->FORKENV, nil);
-	utils->setenv("font", fontnames[0]);
+	env->setenv("font", fontnames[0]);
 	sys->bind("/acme/dis", "/dis", Sys->MBEFORE);
 	wdir = workdir->init();
 	if (wdir == nil)
@@ -305,10 +281,10 @@
 	else{
 		row.init(mainwin.clipr);
 		if(ncol < 0){
-			if(arg.av == nil)
+			if(arg->argv() == nil)
 				ncol = 2;
 			else{
-				ncol = (len arg.av+(WPERCOL-1))/WPERCOL;
+				ncol = (len arg->argv()+(WPERCOL-1))/WPERCOL;
 				if(ncol < 2)
 					ncol = 2;
 			}
@@ -321,12 +297,12 @@
 				error("initializing columns");
 		}
 		c = row.col[row.ncol-1];
-		if(arg.av == nil)
+		if(arg->argv() == nil)
 			readfile(c, wdir);
 		else
 			i = 0;
-			for( ; arg.av != nil; arg.av = tl arg.av){
-				filen := hd arg.av;
+			for( ; argl != nil; argl = tl argl){
+				filen := hd argl;
 				p = utils->strrchr(filen, '/');
 				if((p>=0 && filen[p:] == "/guide") || i/WPERCOL>=row.ncol)
 					readfile(c, filen);
@@ -349,6 +325,28 @@
 	exit;
 }
 
+timing(s : string)
+{
+	thistime := sys->millisec();
+	sys->fprint(tfd, "%s	%d\n", s, thistime-lasttime);
+	lasttime = thistime;
+}
+
+path(p : string) : string
+{
+	if (RELEASECOPY)
+		return p;
+	else {
+		# inlined strrchr since not loaded yet
+		 for (n := len p - 1; n >= 0; n--)
+			if (p[n] == '/')
+				break;
+		 if (n >= 0)
+			p = p[n+1:];
+		 return "/usr/jrf/acme/" + p;
+	}
+}
+
 readfile(c : ref Column, s : string)
 {
 	w : ref Window;
@@ -1042,11 +1040,11 @@
 
 cenv(s : string, t : string, but : int, i : ref Image) : ref Image
 {
-	c := utils->getenv("acme-" + s + "-" + t + "-" + string but);
+	c := env->getenv("acme-" + s + "-" + t + "-" + string but);
 	if (c == nil)
-		c = utils->getenv("acme-" + s + "-" + string but);
+		c = env->getenv("acme-" + s + "-" + string but);
 	if (c == nil && but != 0)
-		c = utils->getenv("acme-" + s);
+		c = env->getenv("acme-" + s);
 	if (c != nil) {
 		if (c[0] == '#' && len c >= 7) {
 			(r1, g1, b1) := rgb(c, 1);
--- a/appl/acme/common.m
+++ b/appl/acme/common.m
@@ -1,5 +1,7 @@
 include "sys.m";
 include "bufio.m";
+include "env.m";
+include "arg.m";
 include "plumbmsg.m";
 include "workdir.m";
 include "draw.m";
--- a/appl/acme/dat.m
+++ b/appl/acme/dat.m
@@ -6,6 +6,8 @@
 	Mods : adt {
 		sys : Sys;
 		bufio : Bufio;
+		env : Env;
+		arg : Arg;
 		draw : Draw;
 		styx : Styx;
 		styxaux : Styxaux;
--- a/appl/acme/exec.b
+++ b/appl/acme/exec.b
@@ -3,6 +3,7 @@
 include "common.m";
 
 sys : Sys;
+env : Env;
 dat : Dat;
 acme : Acme;
 utils : Utils;
@@ -39,6 +40,7 @@
 init(mods : ref Dat->Mods)
 {
 	sys = mods.sys;
+	env = mods.env;
 	dat = mods.dat;
 	acme = mods.acme;
 	utils = mods.utils;
@@ -1203,7 +1205,7 @@
 			dir = nil;
 		}
 		if(filename != nil)
-			utils->setenv("%", filename);
+			env->setenv("%", filename);
 		c.md = fsys->fsysmount(rdir, ndir, incl, nincl);
 		if(c.md == nil){
 			# error("child: can't mount /mnt/acme");
@@ -1242,7 +1244,7 @@
 		else
 			sys->dup(1, 2);
 		tfd = nil;
-		utils->setenv("acmewin", wids);
+		env->setenv("acmewin", wids);
 	}else{
 		if(win != nil)
 			win.close();
@@ -1271,7 +1273,7 @@
 	}
 
 	if(argaddr != nil)
-		utils->setenv("acmeaddr", argaddr);
+		env->setenv("acmeaddr", argaddr);
 	hard := 0;
 	if(len s > 512-10)	# may need to print into stack 
 		hard = 1;
--- a/appl/acme/util.b
+++ b/appl/acme/util.b
@@ -2,7 +2,6 @@
 
 include "common.m";
 include "sh.m";
-include "env.m";
 
 sys : Sys;
 draw : Draw;
@@ -115,25 +114,6 @@
 	return 0;
 }
 
-env : Env;
-
-getenv(s : string) : string
-{
-	if (env == nil)
-		env = load Env Env->PATH;
-	e := env->getenv(s);
-	if(e != nil && e[len e - 1] == '\n')	# shell bug
-		return e[0: len e -1];
-	return e;
-}
-
-setenv(s, t : string)
-{
-	if (env == nil)
-		env = load Env Env->PATH;
-	env->setenv(s, t);
-}
-
 stob(s : string, n : int) : array of byte
 {
 	b := array[2*n] of byte;
@@ -161,59 +141,6 @@
 		ol = tl ol;
 	}
 	return nl;
-}
-
-nextarg(p : ref Arg) : int
-{
-	bp : string;
-
-	if(p.av != nil){
-		bp = hd p.av;
-		if(bp != nil && bp[0] == '-'){
-			p.p = bp[1:];
-			p.av = tl p.av;
-			return 1;
-		}
-	}
-	p.p = nil;
-	return 0;
-}
-
-arginit(av : list of string) : ref Arg
-{
-	p : ref Arg;
-
-	p = ref Arg;
-	p.arg0 = hd av;
-	p.av = tl av;
-	nextarg(p);
-	return p;
-}
-
-argopt(p : ref Arg) : int
-{
-	r : int;
-
-	if(p.p == nil && nextarg(p) == 0)
-		return 0;
-	r = p.p[0];
-	p.p = p.p[1:];
-	return r;
-}
-
-argf(p : ref Arg) : string
-{
-	bp : string;
-
-	if(p.p != nil){
-		bp = p.p;
-		p.p = nil;
-	} else if(p.av != nil){
-		bp = hd p.av;
-		p.av = tl p.av;
-	} else
-		bp = nil;
-	return bp;
 }
 
 exec(cmd : string, argl : list of string)
--- a/appl/acme/util.m
+++ b/appl/acme/util.m
@@ -3,18 +3,9 @@
 
 	stderr : ref Sys->FD;
 
-	Arg : adt {
-		arg0 : string;
-		av : list of string;
-		p : string;
-	};
-
 	PNPROC, PNGROUP : con iota;
 
 	init : fn(mods : ref Dat->Mods);
-	arginit : fn(av : list of string) : ref Arg;
-	argopt : fn(p : ref Arg) : int;
-	argf : fn(p : ref Arg) : string;
 	min : fn(a : int, b : int) : int;
 	max : fn(a : int, b : int) : int;
 	abs : fn(x : int) : int;
@@ -40,8 +31,6 @@
 	strchr : fn(s : string, c : int) : int;
 	strrchr: fn(s : string, c : int) : int;
 	strncmp : fn(s, t : string, n : int) : int;
-	getenv : fn(s : string) : string;
-	setenv : fn(s, t : string);
 	stob : fn(s : string, n : int) : array of byte;
 	btos : fn(b : array of byte, s : ref Dat->Astring);
 	findbl : fn(s : string, n : int) : (string, int);