ref: 5518b9657c07d4ee93ac1db4c97b6d4ec164d42b
parent: 2698fe288642465ec636dca1c8f4ff88a22c5afe
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jun 7 05:54:09 EDT 2017
Add address builder function. Nice for building addresses with defaults.
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -42,6 +42,7 @@
mk.myr
mkpath.myr
mktemp.myr
+ netaddr.myr
now.myr
option.myr
optparse.myr
--- a/lib/std/dial+posixy.myr
+++ b/lib/std/dial+posixy.myr
@@ -30,6 +30,7 @@
/* takes a plan 9 style dial string */
const dial = {ds
match nameseg(ds)
+ | `Some ("net", str): -> dialsock(sys.Sockstream, str)
| `Some ("tcp", str): -> dialsock(sys.Sockstream, str)
| `Some ("udp", str): -> dialsock(sys.Sockdgram, str)
| `Some ("unix", u): -> dialunix(u)
--- /dev/null
+++ b/lib/std/netaddr.myr
@@ -1,0 +1,38 @@
+use "alloc"
+use "die"
+use "memops"
+use "endian"
+use "fmt"
+use "option"
+use "result"
+use "sldup"
+use "strfind"
+use "utf"
+
+pkg std =
+ const netaddr : (addr : byte[:], net : byte[:], port : byte[:] -> byte[:])
+;;
+
+const netaddr = {addr, net, port
+ match strfind(addr, "!")
+ | `Some idx:
+ match strfind(addr[idx+1:], "!")
+ | `Some _: -> sldup(addr)
+ | `None: /* fall through */
+ ;;
+ if port.len > 0
+ -> fmt("{}!{}", addr, port)
+ elif net.len > 0
+ -> fmt("{}!{}", net, addr)
+ else
+ -> sldup(addr)
+ ;;
+ | `None:
+ match (net.len, port.len)
+ | (0, 0): -> sldup(addr)
+ | (0, _): -> fmt("net!{}!{}", addr, port)
+ | (_, 0): -> fmt("{}!{}", net, addr)
+ | (_, _): -> fmt("{}!{}!{}", net, addr, port)
+ ;;
+ ;;
+}
--- /dev/null
+++ b/lib/std/test/netaddr.myr
@@ -1,0 +1,24 @@
+use std
+use testr
+
+const main = {
+ testr.run([
+ /* 64 bit tests */
+ [.name="nop", .fn={ctx; matches(ctx, "tcp!foo!bar", "tcp!foo!bar", "nope", "nope")}],
+ [.name="addboth", .fn={ctx; matches(ctx, "tcp!foo!bar", "foo", "tcp", "bar")}],
+ [.name="addnet", .fn={ctx; matches(ctx, "tcp!foo!bar", "foo!bar", "tcp", "")}],
+ [.name="addport", .fn={ctx; matches(ctx, "tcp!foo!bar", "tcp!foo", "tcp", "bar")}],
+ [.name="addnetport", .fn={ctx; matches(ctx, "net!foo!bar", "foo", "", "bar")}],
+ ][:])
+}
+
+const matches = {ctx, expected, addr, net, port
+ var s
+
+ s = std.netaddr(addr, net, port)
+ testr.check(ctx, std.sleq(expected, s), \
+ "mismatch: expected {}, got {} from ({}, {}, {})\n", \
+ expected, s, addr, net, port)
+ std.slfree(s)
+}
+
--- a/mk/bootstrap/bootstrap+OpenBSD-amd64.sh
+++ b/mk/bootstrap/bootstrap+OpenBSD-amd64.sh
@@ -93,6 +93,7 @@
echo as -g -o sjlj-impl.o sjlj-impl+posixy-x64.s && as -g -o sjlj-impl.o sjlj-impl+posixy-x64.s &&\
echo $pwd/6/6m -I ../sys -I . bitset.myr && $pwd/6/6m -I ../sys -I . bitset.myr &&\
echo $pwd/6/6m -I ../sys -I . fmtfuncs.myr && $pwd/6/6m -I ../sys -I . fmtfuncs.myr &&\
+echo $pwd/6/6m -I ../sys -I . netaddr.myr && $pwd/6/6m -I ../sys -I . netaddr.myr &&\
echo $pwd/6/6m -I ../sys -I . sleep.myr && $pwd/6/6m -I ../sys -I . sleep.myr &&\
echo $pwd/6/6m -I ../sys -I . try.myr && $pwd/6/6m -I ../sys -I . try.myr &&\
echo $pwd/6/6m -I ../sys -I . sort.myr && $pwd/6/6m -I ../sys -I . sort.myr &&\
@@ -101,8 +102,8 @@
echo $pwd/6/6m -I ../sys -I . swap.myr && $pwd/6/6m -I ../sys -I . swap.myr &&\
echo $pwd/6/6m -I ../sys -I . sjlj.myr && $pwd/6/6m -I ../sys -I . sjlj.myr &&\
echo $pwd/6/6m -I ../sys -I . dial+posixy.myr && $pwd/6/6m -I ../sys -I . dial+posixy.myr &&\
-echo $pwd/muse/muse -o libstd.use -p std dial.use fmtfuncs.use fmt.use try.use pathjoin.use strjoin.use sljoin.use slpush.use strstrip.use htab.use now.use getcwd.use rand.use syswrap-ss.use slurp.use varargs.use listen.use strbuf.use clear.use slput.use strsplit.use introspect.use mktemp.use alloc.use optparse.use memops.use bytealloc.use fltbits.use striter.use sldup.use fltfmt.use extremum.use option.use errno.use wait.use slcp.use writeall.use putint.use consts.use syswrap.use sleep.use readall.use sort.use blat.use diriter.use sjlj.use fltparse.use backtrace.use mk.use swap.use hassuffix.use execvp.use ipparse.use types.use slpop.use strfind.use utf.use dialparse.use cstrconv.use search.use die.use units.use result.use bitset.use dir.use env.use resolve.use intparse.use hasprefix.use mkpath.use getint.use dirname.use sleq.use endian.use iterutil.use spork.use assert.use cmp.use chartype.use bigint.use hashfuncs.use slfill.use threadhooks.use fndup.use chomp.use && $pwd/muse/muse -o libstd.use -p std dial.use fmtfuncs.use fmt.use try.use pathjoin.use strjoin.use sljoin.use slpush.use strstrip.use htab.use now.use getcwd.use rand.use syswrap-ss.use slurp.use varargs.use listen.use strbuf.use clear.use slput.use strsplit.use introspect.use mktemp.use alloc.use optparse.use memops.use bytealloc.use fltbits.use striter.use sldup.use fltfmt.use extremum.use option.use errno.use wait.use slcp.use writeall.use putint.use consts.use syswrap.use sleep.use readall.use sort.use blat.use diriter.use sjlj.use fltparse.use backtrace.use mk.use swap.use hassuffix.use execvp.use ipparse.use types.use slpop.use strfind.use utf.use dialparse.use cstrconv.use search.use die.use units.use result.use bitset.use dir.use env.use resolve.use intparse.use hasprefix.use mkpath.use getint.use dirname.use sleq.use endian.use iterutil.use spork.use assert.use cmp.use chartype.use bigint.use hashfuncs.use slfill.use threadhooks.use fndup.use chomp.use &&\
-echo ar -rcs libstd.a dial.o fmtfuncs.o fmt.o try.o pathjoin.o strjoin.o memops-impl.o sljoin.o slpush.o strstrip.o htab.o now.o getbp.o getcwd.o rand.o syswrap-ss.o slurp.o varargs.o listen.o strbuf.o clear.o slput.o strsplit.o introspect.o mktemp.o alloc.o optparse.o memops.o bytealloc.o fltbits.o striter.o sldup.o fltfmt.o extremum.o option.o errno.o wait.o slcp.o writeall.o putint.o consts.o syswrap.o sleep.o readall.o sort.o blat.o diriter.o sjlj.o fltparse.o backtrace.o mk.o swap.o hassuffix.o execvp.o ipparse.o types.o slpop.o strfind.o utf.o dialparse.o cstrconv.o search.o die.o units.o result.o bitset.o dir.o env.o resolve.o intparse.o hasprefix.o mkpath.o getint.o dirname.o sleq.o endian.o iterutil.o spork.o assert.o cmp.o sjlj-impl.o chartype.o bigint.o hashfuncs.o slfill.o threadhooks.o fndup.o chomp.o && ar -rcs libstd.a dial.o fmtfuncs.o fmt.o try.o pathjoin.o strjoin.o memops-impl.o sljoin.o slpush.o strstrip.o htab.o now.o getbp.o getcwd.o rand.o syswrap-ss.o slurp.o varargs.o listen.o strbuf.o clear.o slput.o strsplit.o introspect.o mktemp.o alloc.o optparse.o memops.o bytealloc.o fltbits.o striter.o sldup.o fltfmt.o extremum.o option.o errno.o wait.o slcp.o writeall.o putint.o consts.o syswrap.o sleep.o readall.o sort.o blat.o diriter.o sjlj.o fltparse.o backtrace.o mk.o swap.o hassuffix.o execvp.o ipparse.o types.o slpop.o strfind.o utf.o dialparse.o cstrconv.o search.o die.o units.o result.o bitset.o dir.o env.o resolve.o intparse.o hasprefix.o mkpath.o getint.o dirname.o sleq.o endian.o iterutil.o spork.o assert.o cmp.o sjlj-impl.o chartype.o bigint.o hashfuncs.o slfill.o threadhooks.o fndup.o chomp.o &&\
+echo $pwd/muse/muse -o libstd.use -p std dial.use fmtfuncs.use fmt.use try.use pathjoin.use strjoin.use sljoin.use slpush.use strstrip.use htab.use now.use getcwd.use rand.use syswrap-ss.use slurp.use varargs.use listen.use strbuf.use clear.use slput.use strsplit.use introspect.use mktemp.use alloc.use optparse.use memops.use bytealloc.use fltbits.use striter.use sldup.use fltfmt.use extremum.use option.use errno.use wait.use slcp.use writeall.use putint.use consts.use syswrap.use sleep.use readall.use sort.use blat.use diriter.use sjlj.use fltparse.use backtrace.use mk.use swap.use hassuffix.use execvp.use netaddr.use ipparse.use types.use slpop.use strfind.use utf.use dialparse.use cstrconv.use search.use die.use units.use result.use bitset.use dir.use env.use resolve.use intparse.use hasprefix.use mkpath.use getint.use dirname.use sleq.use endian.use iterutil.use spork.use assert.use cmp.use chartype.use bigint.use hashfuncs.use slfill.use threadhooks.use fndup.use chomp.use && $pwd/muse/muse -o libstd.use -p std dial.use fmtfuncs.use fmt.use try.use pathjoin.use strjoin.use sljoin.use slpush.use strstrip.use htab.use now.use getcwd.use rand.use syswrap-ss.use slurp.use varargs.use listen.use strbuf.use clear.use slput.use strsplit.use introspect.use mktemp.use alloc.use optparse.use memops.use bytealloc.use fltbits.use striter.use sldup.use fltfmt.use extremum.use option.use errno.use wait.use slcp.use writeall.use putint.use consts.use syswrap.use sleep.use readall.use sort.use blat.use diriter.use sjlj.use fltparse.use backtrace.use mk.use swap.use hassuffix.use execvp.use netaddr.use ipparse.use types.use slpop.use strfind.use utf.use dialparse.use cstrconv.use search.use die.use units.use result.use bitset.use dir.use env.use resolve.use intparse.use hasprefix.use mkpath.use getint.use dirname.use sleq.use endian.use iterutil.use spork.use assert.use cmp.use chartype.use bigint.use hashfuncs.use slfill.use threadhooks.use fndup.use chomp.use &&\
+echo ar -rcs libstd.a dial.o fmtfuncs.o fmt.o try.o pathjoin.o strjoin.o memops-impl.o sljoin.o slpush.o strstrip.o htab.o now.o getbp.o getcwd.o rand.o syswrap-ss.o slurp.o varargs.o listen.o strbuf.o clear.o slput.o strsplit.o introspect.o mktemp.o alloc.o optparse.o memops.o bytealloc.o fltbits.o striter.o sldup.o fltfmt.o extremum.o option.o errno.o wait.o slcp.o writeall.o putint.o consts.o syswrap.o sleep.o readall.o sort.o blat.o diriter.o sjlj.o fltparse.o backtrace.o mk.o swap.o hassuffix.o execvp.o netaddr.o ipparse.o types.o slpop.o strfind.o utf.o dialparse.o cstrconv.o search.o die.o units.o result.o bitset.o dir.o env.o resolve.o intparse.o hasprefix.o mkpath.o getint.o dirname.o sleq.o endian.o iterutil.o spork.o assert.o cmp.o sjlj-impl.o chartype.o bigint.o hashfuncs.o slfill.o threadhooks.o fndup.o chomp.o && ar -rcs libstd.a dial.o fmtfuncs.o fmt.o try.o pathjoin.o strjoin.o memops-impl.o sljoin.o slpush.o strstrip.o htab.o now.o getbp.o getcwd.o rand.o syswrap-ss.o slurp.o varargs.o listen.o strbuf.o clear.o slput.o strsplit.o introspect.o mktemp.o alloc.o optparse.o memops.o bytealloc.o fltbits.o striter.o sldup.o fltfmt.o extremum.o option.o errno.o wait.o slcp.o writeall.o putint.o consts.o syswrap.o sleep.o readall.o sort.o blat.o diriter.o sjlj.o fltparse.o backtrace.o mk.o swap.o hassuffix.o execvp.o netaddr.o ipparse.o types.o slpop.o strfind.o utf.o dialparse.o cstrconv.o search.o die.o units.o result.o bitset.o dir.o env.o resolve.o intparse.o hasprefix.o mkpath.o getint.o dirname.o sleq.o endian.o iterutil.o spork.o assert.o cmp.o sjlj-impl.o chartype.o bigint.o hashfuncs.o slfill.o threadhooks.o fndup.o chomp.o &&\
echo cd $pwd/lib/regex && cd $pwd/lib/regex &&\
echo $pwd/6/6m -I ../std -I ../sys types.myr && $pwd/6/6m -I ../std -I ../sys types.myr &&\
echo $pwd/6/6m -I ../std -I ../sys interp.myr && $pwd/6/6m -I ../std -I ../sys interp.myr &&\