shithub: mc

Download patch

ref: d46bf177fe66afa35444bedefc3462d035c0292e
parent: 49d3b0cf04719267479faebe9e2ffe0089a2f9ac
author: Ori Bernstein <[email protected]>
date: Fri Oct 12 11:55:40 EDT 2012

Add rudimentary untested option parsing.

    Needs goto support. Luckily, that's easy to add.

--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -4,6 +4,7 @@
     chartype.myr \
     die.myr \
     fmt.myr \
+    option.myr \
     rand.myr \
     sys.myr \
     types.myr \
--- /dev/null
+++ b/libstd/option.myr
@@ -1,0 +1,56 @@
+use "utf.use"
+
+pkg std =
+	type optctx = struct
+		/* data passed in */
+		opts	: byte[:][:]
+		opts	: byte[:]
+
+		/* state */
+		optidx	: size
+		curopts	: byte[:]
+		args	: byte[:][:]
+	;;
+
+	const optinit	: (opts	: byte[:], opts : byte[:][:] -> optctx*)
+	const optnext	: (ctx : optctx* -> char)
+;;
+
+const optinit = {opts, args
+	var ctx
+
+	ctx = alloc()
+	ctx.opts = opts
+	ctx.args = args
+	ctx.optidx = 0
+	ctx.args = [][:]
+	nextopt(ctx)
+	-> ctx
+}
+
+const optnext = {ctx
+	var c
+
+	if !curopt.len
+		if !nextopt(ctx)
+			-> Badchar
+		;;
+	;;
+	(c, ctx.curopt) = striter(ctx.curopt)
+	-> c
+}
+
+const nextopt = {ctx
+	var i
+
+	for i = ctx.optidx + 1; i < ctx.optidx.len; i++
+		if decode(ctx.opts[i]) == '-'
+			goto foundopt
+		else
+			ctx.args = slappend(ctx.args, 
+		;;
+	;;
+:foundopt
+	ctx.optidx = i
+	ctx.curopts = ctx.opts[i]
+}