shithub: mc

Download patch

ref: a6ba17d4905ac66f9b96843b1cfa8b6981b096f1
parent: 58d7f56805e5e6def788c847c49b1051f164bd6c
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jun 12 07:19:06 EDT 2015

Migrate to fmt2.myr

    We're trying out fmt2 as a step towards removing the current
    fmt code.

--- a/libregex/compile.myr
+++ b/libregex/compile.myr
@@ -168,21 +168,21 @@
 	var l, h
 
 	indent(ind)
-	std.put("Range (end = %t) {\n", rt.end)
+	std.f2put("Range (end = {}) {\n", rt.end)
 	for i = 0; i < rt.ranges.len; i++
 		indent(ind + 1)
 		(l, h) = rt.ranges[i]
-		std.put("0x%xb-0x%xb: \n", l, h)
+		std.f2put("0x{x}-0x{x}: \n", l, h)
 		rtdump(rt.link[i], ind + 1)
 	;;
 	indent(ind)
-	std.put("}\n")
+	std.f2put("}\n")
 }
 
 const indent = {ind
 	var i
 	for i = 0; i < ind; i++
-		std.put("\t")
+		std.f2put("\t")
 	;;
 }
 
@@ -373,28 +373,28 @@
 		->
 	;;
 	for i = 0; i < re.proglen; i++
-		std.put("%i:\t", i)
+		std.f2put("{}:\t", i)
 		match re.prog[i]
 		/* Char matching. Consume exactly one byte from the string. */
-		| `Ibyte b:		std.put("`Ibyte %ub (%c)\n", b, b castto(char)) 
+		| `Ibyte b:		std.f2put("`Ibyte {} ({})\n", b, b castto(char)) 
 		| `Irange (start, end):	
-			std.put("`Irange (%ub,%ub)", start, end) 
+			std.f2put("`Irange ({},{})", start, end) 
 			if std.isalnum(start castto(char)) && std.isalnum(end castto(char))
-				std.put("\t/* %c-%c */", start castto(char), end castto(char))
+				std.f2put("\t/* {}-{} */", start castto(char), end castto(char))
 			;;
-			std.put("\n")
+			std.f2put("\n")
 		/* capture groups */
-		| `Ilbra m:		std.put("`Ilbra %z\n", m) 
-		| `Irbra m:		std.put("`Irbra %z\n", m) 
+		| `Ilbra m:		std.f2put("`Ilbra {}\n", m) 
+		| `Irbra m:		std.f2put("`Irbra {}\n", m) 
 		/* anchors */
-		| `Ibol:			std.put("`Ibol\n")
-		| `Ieol:			std.put("`Ieol\n")
-		| `Ibow:			std.put("`Ibow\n")
-		| `Ieow:			std.put("`Ieow\n")
+		| `Ibol:			std.f2put("`Ibol\n")
+		| `Ieol:			std.f2put("`Ieol\n")
+		| `Ibow:			std.f2put("`Ibow\n")
+		| `Ieow:			std.f2put("`Ieow\n")
 		/* control flow */
-		| `Ifork	(lip, rip):	std.put("`Ifork (%z,%z)\n", lip, rip) 
-		| `Ijmp ip:		std.put("`Ijmp %z\n", ip) 
-		| `Imatch id:		std.put("`Imatch %z\n", id) 
+		| `Ifork	(lip, rip):	std.f2put("`Ifork ({},{})\n", lip, rip) 
+		| `Ijmp ip:		std.f2put("`Ijmp {}\n", ip) 
+		| `Imatch id:		std.f2put("`Imatch {}\n", id) 
 		;;
 	;;
 }
@@ -407,58 +407,58 @@
 		->
 	;;
 	for i = 0; i < indent; i++
-		std.put("  ")
+		std.f2put("  ")
 	;;
 	match t#
 	| `Alt	(a, b):
-		std.put("Alt\n")
+		std.f2put("Alt\n")
 		dump(re, a, indent + 1)
 		dump(re, b, indent + 1)
 	| `Cat	(a, b):
-		std.put("Cat\n")
+		std.f2put("Cat\n")
 		dump(re, a, indent + 1)
 		dump(re, b, indent + 1)
 	/* repetition */
 	| `Star	a:
-		std.put("Star\n")
+		std.f2put("Star\n")
 		dump(re, a, indent + 1)
 	| `Rstar a:
-		std.put("Rstar\n")
+		std.f2put("Rstar\n")
 		dump(re, a, indent + 1)
 	| `Plus	a:
-		std.put("Plus\n")
+		std.f2put("Plus\n")
 		dump(re, a, indent + 1)
 	| `Rplus a:
-		std.put("Rplus\n")
+		std.f2put("Rplus\n")
 		dump(re, a, indent + 1)
 	| `Quest	a:
-		std.put("Quest\n")
+		std.f2put("Quest\n")
 		dump(re, a, indent + 1)
 	| `Bol:
-		std.put("Bol\n")
+		std.f2put("Bol\n")
 	| `Eol:
-		std.put("Eol\n")
+		std.f2put("Eol\n")
 	| `Bow:
-		std.put("Bow\n")
+		std.f2put("Bow\n")
 	| `Eow:
-		std.put("Eow\n")
+		std.f2put("Eow\n")
 	/* end matches */
 	| `Byte	b:
-		std.put("Byte %b\n", b)
+		std.f2put("Byte {}\n", b)
 	| `Chr	c:
-		std.put("Char %c\n", c)
+		std.f2put("Char {}\n", c)
 	| `Ranges rl:
-                std.put("Ranges")
+                std.f2put("Ranges")
 		for r in rl
 			for i = 0; i < indent + 1; i++
-				std.put("  ")
+				std.f2put("  ")
 			;;
-			std.put("\t(%ui-%ui)\n", r[0], r[1])
+			std.f2put("\t({}-{})\n", r[0], r[1])
 		;;
 
 	/* meta */
 	| `Cap	(m, a):
-		std.put("Cap %i\n", m)
+		std.f2put("Cap {}\n", m)
 		dump(re, a, indent + 1)
 	;;
 }
--- a/libregex/interp.myr
+++ b/libregex/interp.myr
@@ -81,7 +81,7 @@
 			thr = re.runq
 			re.runq = thr.next
 
-			trace(re, thr, "\nrunning tid=%z, ip=%z, s[%z]=%c\n", thr.tid, thr.ip, re.strp, std.decode(re.str[re.strp:]))
+			trace(re, thr, "\nrunning tid={}, ip={}, s[{}]={}\n", thr.tid, thr.ip, re.strp, std.decode(re.str[re.strp:]))
 			ip = thr.ip
 			consumed = step(re, thr, -1)
 			while !consumed
@@ -132,7 +132,7 @@
 	match re.prog[thr.ip]
 	/* Char matching. Consume exactly one byte from the string. */
 	| `Ibyte b:
-		trace(re, thr, "\t%z:\tByte %ub (%c)\n", thr.ip, b, b castto(char))
+		trace(re, thr, "\t{}:\tByte {} ({})\n", thr.ip, b, b castto(char))
 		if !within(re, str)
 			die(re, thr, "end of string")
 		elif b != str[re.strp]
@@ -139,10 +139,10 @@
 			die(re, thr, "not right char")
 		else
 			thr.ip++
-			trace(re, thr, "\t\tmatched %b with %b\n", b, str[re.strp])
+			trace(re, thr, "\t\tmatched {} with {}\n", b, str[re.strp])
 		;;
 	| `Irange (start, end):
-		trace(re, thr, "\t%z:\tRange (%ub, %ub) /* %c - %c */\n", thr.ip, start, end, start castto(char), end castto(char))
+		trace(re, thr, "\t{}:\tRange ({}, {}) /* {} - {} */\n", thr.ip, start, end, start castto(char), end castto(char))
 		if !within(re, str) || start > str[re.strp] || end < str[re.strp]
 			die(re, thr, "bad range")
 		else
@@ -153,7 +153,7 @@
 	  called again until exactly one byte is consumed from the string.
 	 */
 	| `Ibol:
-		trace(re, thr, "\t%z:\tBol\n", thr.ip)
+		trace(re, thr, "\t{}:\tBol\n", thr.ip)
 		if re.strp == 0 || str[re.strp - 1] == '\n' castto(byte)
 			thr.ip++
 			-> false
@@ -161,7 +161,7 @@
 			die(re, thr, "not beginning of line")
 		;;
 	| `Ieol:
-		trace(re, thr, "\t%z:\tEol\n", thr.ip)
+		trace(re, thr, "\t{}:\tEol\n", thr.ip)
 		if re.strp == str.len || str[re.strp] == '\n' castto(byte)
 			thr.ip++
 			-> false
@@ -170,7 +170,7 @@
 		;;
 	/* check for word characters */
 	| `Ibow:
-		trace(re, thr, "\t%z:\tBow\n", thr.ip)
+		trace(re, thr, "\t{}:\tBow\n", thr.ip)
 		if iswordchar(str[re.strp:]) && (re.strp == 0 || !iswordchar(prevchar(str, re.strp)))
 			thr.ip++
 			-> false
@@ -178,7 +178,7 @@
 			die(re, thr, "not beginning of word")
 		;;
 	| `Ieow:
-		trace(re, thr, "\t%z:\tEow\n", thr.ip)
+		trace(re, thr, "\t{}:\tEow\n", thr.ip)
 		if re.strp == str.len && iswordchar(prevchar(str, re.strp))
 			thr.ip++
 			-> false
@@ -189,18 +189,18 @@
 			die(re, thr, "not end of word")
 		;;
 	| `Ilbra m:
-		trace(re, thr, "\t%z:\tLbra %z\n", thr.ip, m)
-		trace(re, thr, "\t\tmatch start = %z\n", re.strp)
+		trace(re, thr, "\t{}:\tLbra {}\n", thr.ip, m)
+		trace(re, thr, "\t\tmatch start = {}\n", re.strp)
 		thr.mstart[m] = re.strp
 		thr.ip++
 		-> false
 	| `Irbra m:
-		trace(re, thr, "\t%z:\tRbra %z\n", thr.ip, m)
+		trace(re, thr, "\t{}:\tRbra {}\n", thr.ip, m)
 		thr.mend[m] = re.strp
 		thr.ip++
 		-> false
 	| `Ifork (lip, rip):
-		trace(re, thr, "\t%z:\tFork (%z, %z)\n", thr.ip, lip, rip)
+		trace(re, thr, "\t{}:\tFork ({}, {})\n", thr.ip, lip, rip)
 		mstart = std.sldup(thr.mstart)
 		mend = std.sldup(thr.mend)
 		fork(re, thr, rip, curip, mstart, mend)
@@ -207,11 +207,11 @@
 		thr.ip = lip
 		-> false
 	| `Ijmp ip:
-		trace(re, thr, "\t%z:\tJmp %z\n", thr.ip, ip)
+		trace(re, thr, "\t{}:\tJmp {}\n", thr.ip, ip)
 		thr.ip = ip
 		-> false
 	| `Imatch id:
-		trace(re, thr, "\t%z:\tMatch\n", thr.ip)
+		trace(re, thr, "\t{}:\tMatch\n", thr.ip)
 		finish(re, thr)
 		-> true
 	;;
@@ -239,7 +239,7 @@
 	  thread is in. We should only decrement
 	  the number of threads for that once.
 	 */
-	trace(re, thr, "\t\tdie %z: %s\n", thr.tid, msg)
+	trace(re, thr, "\t\tdie {}: {}\n", thr.tid, msg)
         if !thr.dead
 		re.nthr--
         ;;
@@ -247,7 +247,7 @@
 }
 
 const finish = {re, thr
-	trace(re, thr, "finish %z\n", thr.tid)
+	trace(re, thr, "finish {}\n", thr.tid)
 	thr.matched = true
 	re.nthr--
 }
@@ -274,7 +274,7 @@
 }
 
 const thrfree = {re, thr
-	trace(re, thr, "\t\tcleanup %z\n", thr.tid)
+	trace(re, thr, "\t\tcleanup {}\n", thr.tid)
 	std.slfree(thr.mstart)
 	std.slfree(thr.mend)
 	std.free(thr)
@@ -285,8 +285,11 @@
 }
 
 const trace : (re : regex#, thr : rethread#, msg : byte[:], args : ... -> void) = {re, thr, msg, args
+	var ap
+
 	if re.debug
-		std.putv(msg, std.vastart(&args))
+		ap = std.vastart(&args)
+		std.f2putv(msg, &ap)
 	;;
 }
 
--- a/libregex/redump.myr
+++ b/libregex/redump.myr
@@ -13,7 +13,7 @@
 	cmd = std.optparse(args, &opts)
 	match regex.dbgcompile(cmd.args[0])
 	| `std.Fail m:	
-		std.fatal("unable to compile regex: %s\n", regex.failmsg(m))
+		std.f2fatal("unable to compile regex: {}\n", regex.failmsg(m))
 	| `std.Ok re:
 		if cmd.args.len > 1
 			runall(re, cmd.args)
@@ -33,7 +33,7 @@
 			dump(re, fd)
 			bio.close(fd)
 		| `std.None:
-			std.fatal("failed to open %s\n", f)
+			std.f2fatal("failed to open {}\n", f)
 		;;
 	;;
 }
@@ -42,7 +42,7 @@
 	while true
 		match bio.readln(fd)
 		| `std.Some ln:
-			regex.exec(re, ln)
+			show(regex.exec(re, ln))
 			std.slfree(ln)
 		| `std.None:
 			break
@@ -50,3 +50,16 @@
 	;;
 }
 
+const show = {mg
+	var i
+
+	match mg
+	| `std.Some rl:
+		std.f2put("Matched: {}\n", rl[0])
+		for i = 1; i < rl.len; i++
+			std.f2put("group {}: {}\n", i, rl[i])
+		;;
+	| `std.None:
+		std.f2put("Match failed\n")
+	;;
+}
--- a/libstd/dial+posixy.myr
+++ b/libstd/dial+posixy.myr
@@ -56,7 +56,6 @@
 	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"
 	;;
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -42,6 +42,21 @@
 	$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))#
 
--- 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 @@
 		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 @@
 			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 @@
 			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 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)
 	;;
 }