ed

[hare] The standard editor
Log | Files | Refs | README | LICENSE

commit 8ceb9e41bed207252e7f1d2e6f717e43f0e11e47
parent b43c325c5cfca9d86dc2674c32de6ff2f12ea091
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue, 22 Nov 2022 00:21:57 +0000

add main.ha, Makefile, .gitignore

Diffstat:
A.gitignore | 2++
AMakefile | 15+++++++++++++++
Amain.ha | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +ed +tmp diff --git a/Makefile b/Makefile @@ -0,0 +1,15 @@ +HARE=hare +HAREFLAGS= + +source=\ + main.ha + +all: ed + +ed: ${source} + ${HARE} build ${HAREFLAGS} -o $@ + +clean: + rm -f ed + +.PHONY: all clean diff --git a/main.ha b/main.ha @@ -0,0 +1,72 @@ +use bufio; +use fmt; +use getopt; +use io; +use os; + +let prompt: str = ""; + +let surpress: bool = false; + +type filename = (str | void); + +export fn main() void = { + const help: [_]getopt::help = [ + "standard line editor", + ('p', "prompt", "set the command prompt"), + ('s', "suppress byte counts and '!' prompt"), + "[file]", + ]; + const cmd = getopt::parse(os::args, help...); + defer getopt::finish(&cmd); + + for (let i = 0z; i < len(cmd.opts); i += 1) { + const opt = cmd.opts[i]; + switch (opt.0) { + case 'p' => + prompt = opt.1; + case 's' => + surpress = true; + }; + }; + + if (len(cmd.args) > 1) { + exit_usage(help); + }; + + let fname: filename = switch (len(cmd.args) == 1) { + case false => + void; + case true => + switch (cmd.args[0]) { + case "-" => + fmt::fatal("Invalid filename '-'"); + case "" => + fmt::fatal("Invalid filename ''"); + case => + cmd.args[0]; + }; + }; + + for (true) :repl { + fmt::error(prompt)!; + + const rawline = match (bufio::scanline(os::stdin)) { + case let rawline: []u8 => + yield rawline; + case io::EOF => + break; + case => + abort(); + }; + defer free(rawline); + + const input = fmt::bsprint(rawline); + }; +}; + +@noreturn fn exit_usage(help: []getopt::help) void = { + getopt::printusage(os::stderr, os::args[0], help); + os::exit(1); +}; +