diff options
-rwxr-xr-x | cal.lua | 60 | ||||
-rw-r--r-- | inout.lua | 10 |
2 files changed, 60 insertions, 10 deletions
@@ -7,6 +7,8 @@ local io = require "io" local ioopen = io.open +local iostdin = io.stdin +local iostdout = io.stdout local math = require "math" local mathmin = math.min @@ -301,21 +303,59 @@ local loaddata = function (fname) error ("could not open file " .. fname .. " for reading") end - return fh:read ("*a") + local ret = fh:read ("*a") + fh:close () + + return ret end -local main = function (argv) - if #argv ~= 1 then - error "pass me a file name, I insist!" - end +------------------------------------------------------------------------------- +--- API +------------------------------------------------------------------------------- + +local set_verbosity = function (n) verboselvl = n end + +local parse = function (raw) + local ok, obj = pcall (parse_calendar, raw) + return ok and obj +end + +local parse_handle = function (fh) + local raw = fh:read ("*a") + return raw ~= nil and parse (raw) +end - local raw = loaddata (argv [1]) - local cal = parse_calendar (raw) +local parse_file = function (fname) + local fh = ioopen (fname, "r") + local cal = parse_handle (fh) + fh:close () + return cal +end + +local parse_stdin = function () + return parse_handle (iostdin) +end - if cal ~= nil then println (fmt_calendar (cal, nil, "\n")) end +local format = function (cal, newline) + return fmt_calendar (cal, nil, newline or "\n") +end + +local output_handle = function (cal, fh) + local str = fmt_calendar (cal, nil, newline or "\n") + fh:write (str) +end - return 0 +local output = function (cal) + return output_handle (cal, iostdout) end -return main (arg) +return { set_verbosity = set_verbosity + , parse = parse + , parse_handle = parse_handle + , parse_file = parse_file + , parse_stdin = parse_stdin + , format = format + , output = output + , output_handle = output_handle + } diff --git a/inout.lua b/inout.lua new file mode 100644 index 0000000..ad1c23d --- /dev/null +++ b/inout.lua @@ -0,0 +1,10 @@ +local cal = require "cal" + +local main = function (argv) + local ok = cal.output (cal.parse_stdin ()) + + return ok and 0 or -1 +end + +os.exit (main ()) + |