ed

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit b459d51057d6791a1cac2eaca0f471dc4b9e3ef1
parent 078ab7e5f7f943c82e37467a0892805f7747caef
Author: Byron Torres <b@torresjrjr.com>
Date:   Thu,  9 Sep 2021 03:00:28 +0100

Parse file argument

Diffstat:
Med.ha | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/ed.ha b/ed.ha @@ -1,6 +1,12 @@ +use fmt; +use fs; use getopt; +use io; use os; +type filename = (str | void); +type file = (io::file | void); + export fn main() void = { const help: [_]getopt::help = [ "standard line editor", @@ -25,6 +31,23 @@ export fn main() void = { if (len(cmd.args) > 1) { exit_usage(help); }; + + let filename: filename = switch (len(cmd.args) == 1) { + false => void, + true => switch (cmd.args[0]) { + "-" => fmt::fatal("Invalid filename '-'"), + "" => fmt::fatal("Invalid filename ''"), + * => cmd.args[0], + }, + }; + + let file: file = match (filename) { + void => void, + filename: str => match (os::open(filename)) { + err: fs::error => fmt::fatal("Error {}", fs::strerror(err)), + file: io::file => file, + }, + }; }; @noreturn fn exit_usage(help: []getopt::help) void = {