summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-10-09 12:28:38 -0400
committerOri Bernstein <ori@eigenstate.org>2014-10-09 12:28:38 -0400
commit56a192ced907bcc27405b85e120abb3005f9d981 (patch)
tree11872d2079b9444f5f21f477b66decdebd42e24a
parent29c6ded7a59b7cd8c74136957be151307c5df724 (diff)
downloadmc-56a192ced907bcc27405b85e120abb3005f9d981.tar.gz
Split out parsing of dial strings.
-rw-r--r--6/typeinfo.c1
-rw-r--r--libstd/dial.myr70
2 files changed, 41 insertions, 30 deletions
diff --git a/6/typeinfo.c b/6/typeinfo.c
index 07066ac..9cd206a 100644
--- a/6/typeinfo.c
+++ b/6/typeinfo.c
@@ -95,6 +95,7 @@ size_t tyoffset(Type *ty, Node *memb)
size_t i;
size_t off;
+ ty = tybase(ty);
if (ty->type == Typtr)
ty = tybase(ty->sub[0]);
diff --git a/libstd/dial.myr b/libstd/dial.myr
index 5a90768..b9c7771 100644
--- a/libstd/dial.myr
+++ b/libstd/dial.myr
@@ -10,6 +10,7 @@ use "option.use"
use "resolve.use"
use "result.use"
use "sleq.use"
+use "strfind.use"
use "sys.use"
use "syswrap.use"
use "utf.use"
@@ -30,10 +31,42 @@ var inited = false
/* takes a plan 9 style dial string */
const dial = {str
var proto, host, port
- var socktype, portnum
var sa : sys.sockaddr_in /* we only support inet sockets right now.. ugh. */
var sock
+ match parsedial(str)
+ | `Ok val: (proto, host, port) = val
+ | `Fail m: -> `Fail m
+ ;;
+
+ match getaddr(host)
+ | `Ipv4 bits:
+ sa.fam = sys.Afinet
+ sa.addr = bits
+ sa.port = hosttonet(port)
+ | `Ipv6 bits:
+ -> `Fail "ipv6 not yet supported"
+ ;;
+
+ sock = sys.socket(sa.fam, proto, 0)
+ if sock < 0
+ -> `Fail "failed to connect to socket"
+ ;;
+ var err
+ err = sys.connect(sock, (&sa) castto(sys.sockaddr#), sizeof(sys.sockaddr_in))
+ if err < 0
+ put("Errno %i\n", -err)
+ sys.close(sock)
+ -> `Fail "Failed to bind socket"
+ ;;
+
+ -> `Ok (sock castto(fd))
+}
+
+const parsedial = {str
+ var proto, host, port
+ var socktype, portnum
+
(proto, str) = nameseg(str)
(host, str) = nameseg(str)
(port, str) = nameseg(str)
@@ -61,28 +94,7 @@ const dial = {str
| `None: -> `Fail "bad port"
;;
- match getaddr(host)
- | `Ipv4 bits:
- sa.fam = sys.Afinet
- sa.addr = bits
- sa.port = hosttonet(portnum)
- | `Ipv6 bits:
- -> `Fail "ipv6 not yet supported"
- ;;
-
- sock = sys.socket(sa.fam, socktype, 0)
- if sock < 0
- -> `Fail "failed to connect to socket"
- ;;
- var err
- err = sys.connect(sock, (&sa) castto(sys.sockaddr#), sizeof(sys.sockaddr_in))
- if err < 0
- put("Errno %i\n", -err)
- sys.close(sock)
- -> `Fail "Failed to bind socket"
- ;;
-
- -> `Ok (sock castto(fd))
+ -> `Ok (socktype, host, portnum)
}
const parseport = {port
@@ -120,13 +132,11 @@ const getaddr = {addr
}
const nameseg = {str
- var len
-
- for len = 0; len < str.len; len++
- if str[len] == '!' castto(byte)
- -> (str[:len], str[len+1:])
- ;;
+ match strfind(str, "!")
+ | `Some idx:
+ -> (str[:idx], str[idx:])
+ | `None:
+ -> (str, "")
;;
- -> (str[:], str[len:])
}