summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-12 11:19:06 -0400
committerOri Bernstein <ori@eigenstate.org>2015-06-12 11:19:06 -0400
commita6ba17d4905ac66f9b96843b1cfa8b6981b096f1 (patch)
tree3ca982bfae2ddb135f0863c12e2be7afa2db9927 /libstd
parent58d7f56805e5e6def788c847c49b1051f164bd6c (diff)
downloadmc-a6ba17d4905ac66f9b96843b1cfa8b6981b096f1.tar.gz
Migrate to fmt2.myr
We're trying out fmt2 as a step towards removing the current fmt code.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/dial+posixy.myr1
-rw-r--r--libstd/fmt2.myr15
-rw-r--r--libstd/optparse.myr20
3 files changed, 25 insertions, 11 deletions
diff --git a/libstd/dial+posixy.myr b/libstd/dial+posixy.myr
index 1554cdb..77920b6 100644
--- a/libstd/dial+posixy.myr
+++ b/libstd/dial+posixy.myr
@@ -56,7 +56,6 @@ const dial = {str
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"
;;
diff --git a/libstd/fmt2.myr b/libstd/fmt2.myr
index 5d3511f..bc1de45 100644
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -42,6 +42,21 @@ pkg std =
$noret const f2fatalv : (fmt : byte[:], ap : valist# -> void)
;;
+/* same as 'put', but exits the program after printing */
+const f2fatal = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ f2putv(fmt, &ap)
+ exit(1)
+}
+
+/* same as 'putv', but exits the program after printing */
+const f2fatalv = {fmt, ap
+ f2putv(fmt, ap)
+ exit(1)
+}
+
var fmtmapinited : bool = false
var fmtmap : htab(byte[:], (sb : strbuf#, ap : valist#, opts : byte[:] -> void))#
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index 4f454b5..888b72f 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -1,7 +1,7 @@
use "alloc.use"
use "die.use"
use "extremum.use"
-use "fmt.use"
+use "fmt2.use"
use "option.use"
use "sleq.use"
use "slpush.use"
@@ -62,7 +62,7 @@ const optparse = {args, def
parsed.opts = slpush(parsed.opts, optnext(&ctx))
;;
if ctx.args.len < def.minargs
- put("error: expected at least %z args, got %i\n", def.minargs, ctx.args.len)
+ f2put("error: expected at least {} args, got {}\n", def.minargs, ctx.args.len)
optusage(ctx.optargs[0], ctx.optdef)
exit(1)
;;
@@ -97,7 +97,7 @@ const optnext = {ctx
optusage(ctx.optargs[0], ctx.optdef)
exit(0)
else
- fatal("unexpected argument '%c'\n", c)
+ f2fatal("unexpected argument '{}'\n", c)
;;
| `Some (true, needed):
/* -arg => '-a' 'rg' */
@@ -111,7 +111,7 @@ const optnext = {ctx
ctx.argidx++
next(ctx)
elif needed
- put("Expected argument for %c\n", c)
+ f2put("Expected argument for {}\n", c)
exit(1)
;;
| `Some (false, _):
@@ -162,15 +162,15 @@ const next = {ctx
}
const optusage = {prog, def
- std.put("usage: %s [-h?] ", prog)
+ std.f2put("usage: {} [-h?] ", prog)
for o in def.opts
- std.put("[-%c%s%s] ", o.opt, sep(o.arg), o.arg)
+ std.f2put("[-{}{}{}] ", o.opt, sep(o.arg), o.arg)
;;
- std.put("%s\n", def.argdesc)
- std.put("\t-h\tprint this help message\n")
- std.put("\t-?\tprint this help message\n")
+ std.f2put("{}\n", def.argdesc)
+ std.f2put("\t-h\tprint this help message\n")
+ std.f2put("\t-?\tprint this help message\n")
for o in def.opts
- std.put("\t-%c%s%s\t%s\n", o.opt, sep(o.arg), o.arg, o.desc)
+ std.f2put("\t-{}{}{}\t{}\n", o.opt, sep(o.arg), o.arg, o.desc)
;;
}