niti: add -n to run the program for each line in file-name arguments
authorJean Privat <jean@pryen.org>
Wed, 1 Oct 2014 13:07:08 +0000 (09:07 -0400)
committerJean Privat <jean@pryen.org>
Wed, 1 Oct 2014 13:07:08 +0000 (09:07 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/niti_runtime.nit [new file with mode: 0644]
src/nit.nit

diff --git a/lib/niti_runtime.nit b/lib/niti_runtime.nit
new file mode 100644 (file)
index 0000000..a618fba
--- /dev/null
@@ -0,0 +1,53 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT.  This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
+# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You  are  allowed  to  redistribute it and sell it, alone or is a part of
+# another product.
+
+# Runtime library to loop around the main program for each line in file-name arguments
+#
+# Used by the interpreter when `-n` is used
+module niti_runtime
+
+redef class Sys
+       redef fun run do
+               if not args.is_empty then
+                       open_next_stream
+               end
+               loop
+                       read_next_line
+                       main
+               end
+       end
+
+       # Read the next useful line from file-name arguments
+       private fun read_next_line
+       do
+               if stdin.eof then
+                       open_next_stream
+               end
+               var line = stdin.read_line
+               loop
+                       if not stdin.eof then break
+                       open_next_stream
+                       if not line.is_empty then break
+                       line = stdin.read_line
+               end
+               self.line = line
+       end
+
+       # Open the next file until there is no more arguments
+       private fun open_next_stream
+       do
+               if args.is_empty then exit(0)
+               stdin.close
+               stdin = new IFStream.open(args.shift)
+       end
+
+       # The next line to process by the main program
+       var line: String
+end
index 7ed1528..24bb00e 100644 (file)
@@ -29,7 +29,8 @@ var opt = new OptionString("compatibility (does noting)", "-o")
 toolcontext.option_context.add_option(opt)
 var opt_mixins = new OptionArray("Additionals module to min-in", "-m")
 var opt_eval = new OptionBool("Specifies the program from command-line", "-e")
-toolcontext.option_context.add_option(opt_mixins, opt_eval)
+var opt_loop = new OptionBool("Repeatedly run the program for each line in file-name arguments", "-n")
+toolcontext.option_context.add_option(opt_mixins, opt_eval, opt_loop)
 # We do not add other options, so process them now!
 toolcontext.process_options(args)
 
@@ -49,6 +50,14 @@ if opt_eval.value then
        toolcontext.check_errors
 
        var parent = null
+       if opt_loop.value then
+               var nruntime = modelbuilder.load_module("niti_runtime")
+               if nruntime == null then
+                       toolcontext.check_errors
+                       abort
+               end
+               parent = nruntime.mmodule
+       end
 
        modelbuilder.load_rt_module(parent, amodule, "-")