hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 75231b4510e10cb3def577c213d1142e0e593d5c
parent 92d2807ea87ffb2ecf663b88e5e6799382f6562a
Author: Mykyta Holubakha <hilobakho@gmail.com>
Date:   Fri,  2 Apr 2021 00:33:09 +0300

net::unix: unix socket address type

Diffstat:
Anet/unix/+linux.ha | 29+++++++++++++++++++++++++++++
Anet/unix/addr.ha | 5+++++
Mscripts/gen-stdlib | 9+++++++++
Mstdlib.mk | 30++++++++++++++++++++++++++++++
4 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/net/unix/+linux.ha b/net/unix/+linux.ha @@ -0,0 +1,29 @@ +use rt; +use strings; + +// Converts a UNIX socket address to a native sockaddr. +export fn to_native(addr: addr) (rt::sockaddr | invalid) = { + // sun_path should be null-terminated and fit into rt::UNIX_PATH_MAX + if (len(addr) > rt::UNIX_PATH_MAX - 1) { + return invalid; + }; + let ret = rt::sockaddr { + un = rt::sockaddr_un { + sun_family = rt::AF_UNIX, + ... + } + }; + rt::memcpy(&ret.un.sun_path, + (&addr: *types::string).data, len(addr)); + ret.un.sun_path[len(addr)] = 0; + return ret; +}; + +// Converts a native sockaddr to a UNIX socket address. The return value is +// borrowed from the input. +export fn from_native(a: rt::sockaddr) addr = { + return switch (a.in.sin_family) { + rt::AF_UNIX => strings::fromc(&a.un.sun_path[0]), + * => abort("Wrong address family!"), + }; +}; diff --git a/net/unix/addr.ha b/net/unix/addr.ha @@ -0,0 +1,5 @@ +// A UNIX socket address. +export type addr = str; + +// Invalid UNIX socket path. +export type invalid = void!; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -400,6 +400,14 @@ net_ip() { gen_ssa net::ip bytes io strconv strings strio fmt } +net_unix() { + printf '# net::unix\n' + gen_srcs net::unix \ + addr.ha \ + '$(PLATFORM).ha' + gen_ssa net::unix strings +} + math_random() { gen_srcs math::random \ random.ha @@ -584,6 +592,7 @@ linux linux_vdso net net_ip +net_unix math_random os os_exec diff --git a/stdlib.mk b/stdlib.mk @@ -156,6 +156,9 @@ hare_stdlib_deps+=$(stdlib_net) stdlib_net_ip=$(HARECACHE)/net/ip/net_ip.o hare_stdlib_deps+=$(stdlib_net_ip) +stdlib_net_unix=$(HARECACHE)/net/unix/net_unix.o +hare_stdlib_deps+=$(stdlib_net_unix) + stdlib_math_random=$(HARECACHE)/math/random/math_random.o hare_stdlib_deps+=$(stdlib_math_random) @@ -542,6 +545,18 @@ $(HARECACHE)/net/ip/net_ip.ssa: $(stdlib_net_ip_srcs) $(stdlib_rt) $(stdlib_byte @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nnet::ip \ -t$(HARECACHE)/net/ip/net_ip.td $(stdlib_net_ip_srcs) +# net::unix +# net::unix +stdlib_net_unix_srcs= \ + $(STDLIB)/net/unix/addr.ha \ + $(STDLIB)/net/unix/$(PLATFORM).ha + +$(HARECACHE)/net/unix/net_unix.ssa: $(stdlib_net_unix_srcs) $(stdlib_rt) $(stdlib_strings) + @printf 'HAREC \t$@\n' + @mkdir -p $(HARECACHE)/net/unix + @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nnet::unix \ + -t$(HARECACHE)/net/unix/net_unix.td $(stdlib_net_unix_srcs) + # math::random stdlib_math_random_srcs= \ $(STDLIB)/math/random/random.ha @@ -891,6 +906,9 @@ hare_testlib_deps+=$(testlib_net) testlib_net_ip=$(TESTCACHE)/net/ip/net_ip.o hare_testlib_deps+=$(testlib_net_ip) +testlib_net_unix=$(TESTCACHE)/net/unix/net_unix.o +hare_testlib_deps+=$(testlib_net_unix) + testlib_math_random=$(TESTCACHE)/math/random/math_random.o hare_testlib_deps+=$(testlib_math_random) @@ -1286,6 +1304,18 @@ $(TESTCACHE)/net/ip/net_ip.ssa: $(testlib_net_ip_srcs) $(testlib_rt) $(testlib_b @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nnet::ip \ -t$(TESTCACHE)/net/ip/net_ip.td $(testlib_net_ip_srcs) +# net::unix +# net::unix +testlib_net_unix_srcs= \ + $(STDLIB)/net/unix/addr.ha \ + $(STDLIB)/net/unix/$(PLATFORM).ha + +$(TESTCACHE)/net/unix/net_unix.ssa: $(testlib_net_unix_srcs) $(testlib_rt) $(testlib_strings) + @printf 'HAREC \t$@\n' + @mkdir -p $(TESTCACHE)/net/unix + @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nnet::unix \ + -t$(TESTCACHE)/net/unix/net_unix.td $(testlib_net_unix_srcs) + # math::random testlib_math_random_srcs= \ $(STDLIB)/math/random/random.ha