changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > ventivac / changeset: more fn init to top of file, more inferno-like.

changeset 105: b2d15be09468
parent 104: bd9330e95ebb
child 106: 5c7c019578cf
author: Mechiel Lukkien <mechiel@ueber.net>
date: Wed, 15 Aug 2007 20:20:48 +0200
files: appl/cmd/vacfs.b
description: more fn init to top of file, more inferno-like.
     1.1--- a/appl/cmd/vacfs.b	Wed Aug 15 18:02:32 2007 +0200
     1.2+++ b/appl/cmd/vacfs.b	Wed Aug 15 20:20:48 2007 +0200
     1.3@@ -62,160 +62,6 @@
     1.4 qidscores: list of (string, int);
     1.5 
     1.6 
     1.7-childget(qid: int, vqid: big): int
     1.8-{
     1.9-	for(l := qids[qid % len qids]; l != nil; l = tl l) {
    1.10-		if((hd l).qid != qid)
    1.11-			continue;
    1.12-		for(m := (hd l).cqids; m != nil; m = tl m) {
    1.13-			(vq, cq) := hd m;
    1.14-			if(vq == vqid)
    1.15-				return cq;
    1.16-		}
    1.17-	}
    1.18-	return -1;
    1.19-}
    1.20-
    1.21-childput(qid: int, vqid: big): int
    1.22-{
    1.23-	qd: ref Qdir;
    1.24-	for(l := qids[qid % len qids]; l != nil; l = tl l)
    1.25-		if((hd l).qid == qid) {
    1.26-			qd = hd l;
    1.27-			break;
    1.28-		}
    1.29-	if(qd == nil) {
    1.30-		qd = ref Qdir(qid, nil);
    1.31-		qids[qid % len qids] = qd::nil;
    1.32-	}
    1.33-	qd.cqids = (vqid, ++lastqid)::qd.cqids;
    1.34-	return lastqid;
    1.35-}
    1.36-
    1.37-scoreget(score: string): ref Elem
    1.38-{
    1.39-	for(l := qidscores; l != nil; l = tl l) {
    1.40-		(s, n) := hd l;
    1.41-		if(s == score)
    1.42-			return get(n);
    1.43-	}
    1.44-	return nil;
    1.45-}
    1.46-
    1.47-scoreput(score: string): int
    1.48-{
    1.49-	qidscores = (score, ++lastqid)::qidscores;
    1.50-	return lastqid;
    1.51-}
    1.52-
    1.53-Elem.mkdir(qid: int, de: ref Direntry, size: big, vd: ref Vacdir, pqid: int): ref Elem.Dir
    1.54-{
    1.55-	return ref Elem.Dir(qid, de, size, 0, vd, pqid, (-1, nil));
    1.56-}
    1.57-
    1.58-Elem.new(nqid: int, vd: ref Vacdir, de: ref Direntry, pqid: int): ref Elem
    1.59-{
    1.60-	(e, me) := vd.open(de);
    1.61-	if(e == nil)
    1.62-		return nil;
    1.63-	if(de.mode & Vac->Modedir)
    1.64-		return Elem.mkdir(nqid, de, e.size, Vacdir.new(session, e, me), pqid);
    1.65-	return ref Elem.File(nqid, de, e.size, 0, Vacfile.new(session, e));
    1.66-}
    1.67-
    1.68-Elem.stat(e: self ref Elem): ref Sys->Dir
    1.69-{
    1.70-	d := e.de.mkdir();
    1.71-	d.qid.path = big e.qid;
    1.72-	d.length = e.size;
    1.73-	return d;
    1.74-}
    1.75-
    1.76-walk(ed: ref Elem.Dir, name: string): (ref Elem, string)
    1.77-{
    1.78-	if(name == "..")
    1.79-		return (get(ed.pqid), nil);
    1.80-
    1.81-	if(ed.qid == 0) {
    1.82-		ne := scoreget(name);
    1.83-		if(ne == nil) {
    1.84-			(ok, score) := Score.parse(name);
    1.85-			if(ok != 0)
    1.86-				return (nil, "bad score");
    1.87-
    1.88-			(vd, de, err) := vac->vdroot(session, score);
    1.89-			if(err != nil)
    1.90-				return (nil, err);
    1.91-
    1.92-			nqid := scoreput(name);
    1.93-			ne = Elem.mkdir(nqid, de, big 0, vd, ed.qid);
    1.94-			set(ne);
    1.95-		}
    1.96-		return (ne, nil);
    1.97-	}
    1.98-
    1.99-	de := ed.vd.walk(name);
   1.100-	if(de == nil)
   1.101-		return (nil, sprint("%r"));
   1.102-	cqid := childget(ed.qid, de.qid);
   1.103-	ne: ref Elem;
   1.104-	if(cqid < 0)
   1.105-		cqid = childput(ed.qid, de.qid);
   1.106-	else
   1.107-		ne = get(cqid);
   1.108-	if(ne == nil) {
   1.109-		ne = Elem.new(cqid, ed.vd, de, ed.qid);
   1.110-		set(ne);
   1.111-	}
   1.112-	return (ne, nil);
   1.113-}
   1.114-
   1.115-get(qid: int): ref Elem
   1.116-{
   1.117-	for(l := elems[qid % len elems]; l != nil; l = tl l)
   1.118-		if((hd l).qid == qid)
   1.119-			return hd l;
   1.120-	return nil;
   1.121-}
   1.122-
   1.123-set(e: ref Elem)
   1.124-{
   1.125-	elems[e.qid % len elems] = e::elems[e.qid % len elems];
   1.126-}
   1.127-
   1.128-remove[T](l: list of T, e: T): list of T
   1.129-{
   1.130-	r: list of T;
   1.131-	for(; l != nil; l = tl l)
   1.132-		if(hd l != e)
   1.133-			r = hd l::r;
   1.134-	return r;
   1.135-}
   1.136-
   1.137-del(e: ref Elem)
   1.138-{
   1.139-	i := e.qid % len elems;
   1.140-	elems[i] = remove(elems[i], e);
   1.141-}
   1.142-
   1.143-getfile(qid: int): ref Elem.File
   1.144-{
   1.145-	pick file := get(qid) {
   1.146-	File =>	return file;
   1.147-	}
   1.148-	error("internal error, getfile");
   1.149-	return nil;
   1.150-}
   1.151-
   1.152-getdir(qid: int): ref Elem.Dir
   1.153-{
   1.154-	pick d := get(qid) {
   1.155-	Dir =>	return d;
   1.156-	}
   1.157-	error("internal error, getdir");
   1.158-	return nil;
   1.159-}
   1.160-
   1.161 init(nil: ref Draw->Context, args: list of string)
   1.162 {
   1.163 	sys = load Sys Sys->PATH;
   1.164@@ -430,6 +276,160 @@
   1.165 		}
   1.166 }
   1.167 
   1.168+childget(qid: int, vqid: big): int
   1.169+{
   1.170+	for(l := qids[qid % len qids]; l != nil; l = tl l) {
   1.171+		if((hd l).qid != qid)
   1.172+			continue;
   1.173+		for(m := (hd l).cqids; m != nil; m = tl m) {
   1.174+			(vq, cq) := hd m;
   1.175+			if(vq == vqid)
   1.176+				return cq;
   1.177+		}
   1.178+	}
   1.179+	return -1;
   1.180+}
   1.181+
   1.182+childput(qid: int, vqid: big): int
   1.183+{
   1.184+	qd: ref Qdir;
   1.185+	for(l := qids[qid % len qids]; l != nil; l = tl l)
   1.186+		if((hd l).qid == qid) {
   1.187+			qd = hd l;
   1.188+			break;
   1.189+		}
   1.190+	if(qd == nil) {
   1.191+		qd = ref Qdir(qid, nil);
   1.192+		qids[qid % len qids] = qd::nil;
   1.193+	}
   1.194+	qd.cqids = (vqid, ++lastqid)::qd.cqids;
   1.195+	return lastqid;
   1.196+}
   1.197+
   1.198+scoreget(score: string): ref Elem
   1.199+{
   1.200+	for(l := qidscores; l != nil; l = tl l) {
   1.201+		(s, n) := hd l;
   1.202+		if(s == score)
   1.203+			return get(n);
   1.204+	}
   1.205+	return nil;
   1.206+}
   1.207+
   1.208+scoreput(score: string): int
   1.209+{
   1.210+	qidscores = (score, ++lastqid)::qidscores;
   1.211+	return lastqid;
   1.212+}
   1.213+
   1.214+Elem.mkdir(qid: int, de: ref Direntry, size: big, vd: ref Vacdir, pqid: int): ref Elem.Dir
   1.215+{
   1.216+	return ref Elem.Dir(qid, de, size, 0, vd, pqid, (-1, nil));
   1.217+}
   1.218+
   1.219+Elem.new(nqid: int, vd: ref Vacdir, de: ref Direntry, pqid: int): ref Elem
   1.220+{
   1.221+	(e, me) := vd.open(de);
   1.222+	if(e == nil)
   1.223+		return nil;
   1.224+	if(de.mode & Vac->Modedir)
   1.225+		return Elem.mkdir(nqid, de, e.size, Vacdir.new(session, e, me), pqid);
   1.226+	return ref Elem.File(nqid, de, e.size, 0, Vacfile.new(session, e));
   1.227+}
   1.228+
   1.229+Elem.stat(e: self ref Elem): ref Sys->Dir
   1.230+{
   1.231+	d := e.de.mkdir();
   1.232+	d.qid.path = big e.qid;
   1.233+	d.length = e.size;
   1.234+	return d;
   1.235+}
   1.236+
   1.237+walk(ed: ref Elem.Dir, name: string): (ref Elem, string)
   1.238+{
   1.239+	if(name == "..")
   1.240+		return (get(ed.pqid), nil);
   1.241+
   1.242+	if(ed.qid == 0) {
   1.243+		ne := scoreget(name);
   1.244+		if(ne == nil) {
   1.245+			(ok, score) := Score.parse(name);
   1.246+			if(ok != 0)
   1.247+				return (nil, "bad score");
   1.248+
   1.249+			(vd, de, err) := vac->vdroot(session, score);
   1.250+			if(err != nil)
   1.251+				return (nil, err);
   1.252+
   1.253+			nqid := scoreput(name);
   1.254+			ne = Elem.mkdir(nqid, de, big 0, vd, ed.qid);
   1.255+			set(ne);
   1.256+		}
   1.257+		return (ne, nil);
   1.258+	}
   1.259+
   1.260+	de := ed.vd.walk(name);
   1.261+	if(de == nil)
   1.262+		return (nil, sprint("%r"));
   1.263+	cqid := childget(ed.qid, de.qid);
   1.264+	ne: ref Elem;
   1.265+	if(cqid < 0)
   1.266+		cqid = childput(ed.qid, de.qid);
   1.267+	else
   1.268+		ne = get(cqid);
   1.269+	if(ne == nil) {
   1.270+		ne = Elem.new(cqid, ed.vd, de, ed.qid);
   1.271+		set(ne);
   1.272+	}
   1.273+	return (ne, nil);
   1.274+}
   1.275+
   1.276+get(qid: int): ref Elem
   1.277+{
   1.278+	for(l := elems[qid % len elems]; l != nil; l = tl l)
   1.279+		if((hd l).qid == qid)
   1.280+			return hd l;
   1.281+	return nil;
   1.282+}
   1.283+
   1.284+set(e: ref Elem)
   1.285+{
   1.286+	elems[e.qid % len elems] = e::elems[e.qid % len elems];
   1.287+}
   1.288+
   1.289+remove[T](l: list of T, e: T): list of T
   1.290+{
   1.291+	r: list of T;
   1.292+	for(; l != nil; l = tl l)
   1.293+		if(hd l != e)
   1.294+			r = hd l::r;
   1.295+	return r;
   1.296+}
   1.297+
   1.298+del(e: ref Elem)
   1.299+{
   1.300+	i := e.qid % len elems;
   1.301+	elems[i] = remove(elems[i], e);
   1.302+}
   1.303+
   1.304+getfile(qid: int): ref Elem.File
   1.305+{
   1.306+	pick file := get(qid) {
   1.307+	File =>	return file;
   1.308+	}
   1.309+	error("internal error, getfile");
   1.310+	return nil;
   1.311+}
   1.312+
   1.313+getdir(qid: int): ref Elem.Dir
   1.314+{
   1.315+	pick d := get(qid) {
   1.316+	Dir =>	return d;
   1.317+	}
   1.318+	error("internal error, getdir");
   1.319+	return nil;
   1.320+}
   1.321+
   1.322 error(s: string)
   1.323 {
   1.324 	fprint(fildes(2), "%s\n", s);