ref: 55660f5aa113613d3176eeb31514ce234cec1bfb
parent: b6239eb7e760bc00d57a82096c64be8e108fd17a
author: Ori Bernstein <[email protected]>
date: Mon Dec 29 13:44:15 EST 2014
Update dial and syswrap to work on plan9.
--- a/libstd/dial+plan9.myr
+++ b/libstd/dial+plan9.myr
@@ -1,0 +1,172 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "fmt.use"
+use "option.use"
+use "pathjoin.use"
+use "result.use"
+use "sleq.use"
+use "strfind.use"
+use "strstrip.use"
+use "syswrap.use"
+use "utf.use"
+
+
+pkg std =
+ const dial : (dialstr : byte[:] -> result(fd, byte[:]))
+;;
+
+const Maxpath = 512
+
+const dial = {str
+ var netdir, proto, rem
+
+ (netdir, proto, rem) = parsedial(str)
+ put("net=%s, proto=%s, rem=%s\n", netdir, proto, rem)
+ if netdir.len != 0
+ -> csdial(netdir, proto, rem)
+ ;;
+
+ match csdial("/net", proto, rem)
+ | `Ok fd: -> `Ok fd
+ | `Fail m:
+ put("m = %s\n", m)
+ -> csdial("/net.alt", proto, rem)
+ ;;
+}
+
+const csdial = {netdir, proto, rem
+ var dir, clone, addr, csaddr
+ var ret, csfd, n
+ var buf : byte[Maxpath]
+
+ /* Try using the connection server */
+ dir = fmt("%s/cs", netdir)
+ csfd = open(dir, Ordwr)
+ if csfd < 0
+ clone = fmt("%s/%s/clone", netdir, proto)
+ ret = call(clone, rem, netdir)
+ slfree(clone)
+ if ret == -1
+ -> `Fail "unable to dial without cs"
+ else
+ -> `Ok ret
+ ;;
+ ;;
+ slfree(dir)
+
+ csaddr = fmt("%s!%s", proto, rem)
+ put("csaddr=%s\n", csaddr)
+ if write(csfd, csaddr) < 0
+ close(csfd)
+ -> `Fail "couldn't blah cs"
+ ;;
+ slfree(csaddr)
+
+ seek(csfd, 0, 0)
+ while true
+ n = read(csfd, buf[:])
+ if n <= 0
+ break
+ ;;
+
+ match strfind(buf[:n], " ")
+ | `None: continue
+ | `Some i:
+ clone = buf[:i]
+ addr = buf[i+1:n]
+ ;;
+
+ ret = call(clone, addr, netdir)
+ if ret >= 0
+ break
+ ;;
+ ;;
+
+ close(csfd)
+ if ret < 0
+ -> `Fail "unable to dial"
+ ;;
+ -> `Ok ret
+}
+
+const call = {clone, addr, netdir
+ var namebuf : byte[Maxpath]
+ var databuf : byte[Maxpath]
+ var name, base
+ var cfd, datafd
+ var c, n
+
+ datafd = -1
+ c = nsclonestr(clone, netdir)
+ put("c = %s\n", c)
+ cfd = open(c, Ordwr)
+ if cfd < 0
+ goto cleanup
+ ;;
+
+ n = read(cfd, namebuf[:])
+ if n < 0
+ goto cleanup
+ ;;
+ fput(cfd, "connect %s", addr)
+ name = strstrip(namebuf[:n])
+ put("namebuf=%s\n", namebuf[:n])
+ match strrfind(c, "/")
+ | `None: die("there should be a '/' here\n")
+ | `Some i: base = c[:i]
+ ;;
+ n = bfmt(databuf[:], "%s/%s/data", base, name)
+ datafd = open(databuf[:n], Ordwr)
+:cleanup
+ close(cfd)
+ slfree(c)
+ -> datafd
+}
+
+const nsclonestr = {clone, netdir
+ if decode(clone) == '#' || decode(clone) == '/'
+ match std.strfind(clone[1:], "/")
+ | `Some i: clone = clone[i+1:]
+ | `None: /* nothing */
+ ;;
+ ;;
+ -> pathcat(netdir, clone)
+}
+
+const parsedial = {str
+ var netdir, proto, rem, hd, tl
+
+ netdir=""
+ proto = ""
+ rem = ""
+ match strfind(str, "!")
+ | `None:
+ proto = "net"
+ rem = str
+ | `Some sep:
+ hd = str[:sep]
+ tl = str[sep+1:]
+ put("hd = %s\n", hd)
+ if decode(hd) == '#' || decode(hd) == '/'
+ match strrfind(hd, "/")
+ | `Some idx:
+ netdir = hd[:idx]
+ proto = hd[idx+1:]
+ | `None:
+ netdir = ""
+ proto = hd
+ ;;
+ else
+ netdir = ""
+ proto = hd
+ put("proto = %s\n", proto)
+
+ ;;
+ rem = tl
+ ;;
+
+ -> (netdir, proto, rem)
+}
+
--- a/libstd/sys+plan9-x64.myr
+++ b/libstd/sys+plan9-x64.myr
@@ -149,7 +149,7 @@
const fstat : (fd : fd, edir : byte[:] -> int64)
const wstat : (name : byte[:], edir : byte[:] -> int64)
const fwstat : (fd : byte[:], edir : byte[:] -> int64)
- const seek : (fd : fd, len : off, ty : int -> off)
+ const seek : (fd : fd, len : off, ty : int64 -> off)
const mount : (fd : fd, afd : fd, old : byte[:], flag : int32, aname : byte[:] -> int64)
const await : (buf : byte[:] -> int64)
const pread : (fd : fd, buf : byte[:], off : off -> size)
--- a/libstd/syswrap+plan9.myr
+++ b/libstd/syswrap+plan9.myr
@@ -5,7 +5,12 @@
type fd = sys.fd
type pid = sys.pid
type fdopt = sys.fdopt
+ type whence = int64
+ const Seekset : whence = 0
+ const Seekcur : whence = 1
+ const Seekend : whence = 2
+
const Failmem : byte# = -1 castto(byte#)
const Ordonly : fdopt = sys.Ordonly castto(fdopt)
@@ -23,6 +28,7 @@
const creat : (path : byte[:], mode : int64 -> fd)
const read : (fd : fd, buf : byte[:] -> size)
const write : (fd : fd, buf : byte[:] -> size)
+ const seek : (fd : fd, delta : off, whence : whence -> off)
const pipe : (fds : fd[2]# -> int64)
const dup2 : (ofd : fd, nfd : fd -> fd)
@@ -46,6 +52,7 @@
pkglocal const getmem : (sz : size -> byte#)
pkglocal const freemem : (p : byte#, sz : size -> void)
pkglocal const curtime : (-> time)
+ pkglocal const p9errstr : (buf : byte[:] -> byte[:])
;;
/* fd stuff */
@@ -68,6 +75,7 @@
const close = {fd; -> sys.close(fd castto(sys.fd)) castto(int64)}
const read = {fd, buf; -> sys.pread(fd castto(sys.fd), buf, -1) castto(size)}
const write = {fd, buf; -> sys.pwrite(fd castto(sys.fd), buf, -1) castto(size)}
+const seek = {fd, off, whence; -> sys.seek(fd castto(sys.fd), off castto(sys.off), whence castto(int64)) castto(off)}
const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#)) castto(int64)}
const dup2 = {ofd, nfd; -> sys.dup(ofd castto(sys.fd), nfd castto(sys.fd)) castto(fd)}
@@ -141,3 +149,14 @@
const curtime = {
-> sys.nsec()/1000 castto(time)
}
+
+const p9errstr = {errbuf
+ var i
+
+ sys.errstr(errbuf)
+ for i = 0; errbuf[i] != 0 && i < errbuf.len; i++
+ continue
+ ;;
+ -> errbuf[:i]
+}
+
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -1,18 +1,16 @@
use sys
use "types.use"
-use "option.use"
pkg std =
type fd = sys.fd
type pid = sys.pid
type fdopt = sys.fdopt
- type whence = sys.whence
const Failmem : byte# = -1 castto(byte#)
- const Seekset : whence = sys.Seekset castto(whence)
- const Seekcur : whence = sys.Seekcur castto(whence)
- const Seekend : whence = sys.Seekend castto(whence)
+ const Seekset : seektype = 0
+ const Seekcur : seektype = 1
+ const Seekend : seektype = 2
const Ordonly : fdopt = sys.Ordonly castto(fdopt)
const Owronly : fdopt = sys.Owronly castto(fdopt)
@@ -30,18 +28,11 @@
const read : (fd : fd, buf : byte[:] -> size)
const write : (fd : fd, buf : byte[:] -> size)
const pipe : (fds : fd[2]# -> int64)
- const seek : (fd : fd, delta : off, whence : whence -> off)
const dup2 : (ofd : fd, nfd : fd -> fd)
- /* useful/portable bits of stat */
- const fmtime : (f : byte[:] -> option(time))
- const fsize : (f : byte[:] -> option(off))
-
- /* useful/portable bits of uname */
-
/* path manipulation */
const mkdir : (path : byte[:], mode : int64 -> int64)
- const remove : (path : byte[:] -> bool)
+ const unlink : (path : byte[:] -> int)
/* process stuff */
const getpid : ( -> pid)
@@ -65,12 +56,11 @@
const read = {fd, buf; -> sys.read(fd castto(sys.fd), buf) castto(size)}
const write = {fd, buf; -> sys.write(fd castto(sys.fd), buf) castto(size)}
const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#))}
-const seek = {fd, delta, whence; -> sys.lseek(fd castto(sys.fd), delta castto(sys.off), whence castto(sys.whence)) castto(off)}
const dup2 = {ofd, nfd; -> sys.dup2(ofd castto(sys.fd), nfd castto(sys.fd)) castto(fd)}
/* path manipulation */
const mkdir = {path, mode; -> sys.mkdir(path, mode)}
-const remove = {path; -> sys.unlink(path) == 0}
+const unlink = {path; -> sys.unlink(path)}
/* process stuff */
const getpid = {; -> sys.getpid() castto(pid)}
@@ -93,28 +83,5 @@
-> (sec*1_000_000 + nsec/1000) castto(time)
else
-> -1
- ;;
-}
-
-const fmtime = {path
- var sb
- var sec, nsec
-
- if sys.stat(path, &sb) == 0
- sec = sb.mtime.sec castto(time)
- nsec = sb.mtime.nsec castto(time)
- -> `Some sec*1000 + nsec/1_000_000
- else
- -> `None
- ;;
-}
-
-const fsize = {path
- var sb
-
- if sys.stat(path, &sb) == 0
- -> `Some (sb.size castto(off))
- else
- -> `None
;;
}
--- a/libstd/util+plan9-x64.s
+++ b/libstd/util+plan9-x64.s
@@ -28,7 +28,7 @@
MOVQ SP,DI /* dest */
MOVQ SP,AX /* ret val */
ANDQ $(~15),SP /* align */
- SUBQ $24,SP /* "unpop" the args and make room for return addr */
+ SUBQ $32,SP /* "unpop" the args and make room for return addr */
CLD
REP
@@ -59,7 +59,7 @@
/* get stack space */
SUBQ BX,SP /* get stack space */
MOVQ SP,AX /* top of stack (return value) */
- SUBQ $24,SP /* "unpop" the args, and make room for ret addr */
+ SUBQ $32,SP /* "unpop" the args, and make room for ret addr */
ANDQ $(~15),SP /* align */
MOVQ R15,0(SP) /* place ret addr */