ed

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

commit be423731e3f6b7fc4cca4b0ea1e9f00356c41de2
parent 76241273947303e26572921eb447a1a857f47d1c
Author: Byron Torres <b@torresjrjr.com>
Date:   Mon,  8 Jan 2024 18:35:18 +0000

cmd_read(): impl ! shell commands

Diffstat:
Mcommand.ha | 32++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/command.ha b/command.ha @@ -1,3 +1,4 @@ +use time; use errors; use fmt; use fs; @@ -445,10 +446,24 @@ fn cmd_quit_forced(s: *Session, cmd: *Command) (void | Error) = { fn cmd_read(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; - const n = get_linenum(cmd.linenums, s.buf.cursor); + const n = get_linenum(cmd.linenums, addr_lastline(s.buf)); - const fname = filename(s, cmd, false)?; - const h = os::open(fname)?: io::handle; + const h: io::handle = + if (!strings::hasprefix(cmd.arg1, "!")) { + const fname = filename(s, cmd, false)?; + yield os::open(fname)?; + } else { + // TODO: handle '!'s + let shcmd = exec::cmd( + "sh", "-c", strings::cut(cmd.arg1, "!").1 + )!; + let pipe = exec::pipe(); + exec::addfile(&shcmd, os::stdout_file, pipe.1); + let proc = exec::start(&shcmd)!; + io::close(pipe.1)!; + exec::wait(&proc)!; + yield pipe.0; + }; defer io::close(h)!; const (sz, _) = buf_read(s.buf, h, n)?; @@ -637,7 +652,7 @@ fn cmd_shellescape(s: *Session, cmd: *Command) (void | Error) = { }; }; - let shcmdline = strings::dup(memio::string(&new)!); + let shcmdline = memio::string(&new)!; let shcmd = exec::cmd("sh", "-c", shcmdline)!; let pipe = exec::pipe(); @@ -645,17 +660,18 @@ fn cmd_shellescape(s: *Session, cmd: *Command) (void | Error) = { let proc = exec::start(&shcmd)!; io::close(pipe.1)!; - let data = io::drain(pipe.0)!; - io::close(pipe.0)!; exec::wait(&proc)!; if (preview) fmt::println(shcmdline)!; - io::write(os::stdout, data)!; + + io::copy(os::stdout, pipe.0)!; + io::close(pipe.0)!; + if (!s.suppressmode) fmt::println("!")!; - s.prev_shcmd = shcmdline; + s.prev_shcmd = strings::dup(shcmdline); }; fn cmd_null(s: *Session, cmd: *Command) (void | Error) = {