From 26ae1b95cc30d99f53a60f32c3d81bbf77fedb91 Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:11:13 +0800 Subject: [PATCH 1/3] Move sigterm channel to internal/util --- cmd/micro/micro.go | 9 ++++----- internal/util/util.go | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index ac36ec19..f3c6acf1 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -40,8 +40,7 @@ var ( flagClean = flag.Bool("clean", false, "Clean configuration directory") optionFlags map[string]*string - sigterm chan os.Signal - sighup chan os.Signal + sighup chan os.Signal timerChan chan func() ) @@ -360,9 +359,9 @@ func main() { screen.Events = make(chan tcell.Event) - sigterm = make(chan os.Signal, 1) + util.Sigterm = make(chan os.Signal, 1) sighup = make(chan os.Signal, 1) - signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT) + signal.Notify(util.Sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT) signal.Notify(sighup, syscall.SIGHUP) timerChan = make(chan func()) @@ -437,7 +436,7 @@ func DoEvent() { } } os.Exit(0) - case <-sigterm: + case <-util.Sigterm: for _, b := range buffer.OpenBuffers { if !b.Modified() { b.Fini() diff --git a/internal/util/util.go b/internal/util/util.go index 1cd5d46c..83dc4458 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -41,6 +41,8 @@ var ( // Stdout is a buffer that is written to stdout when micro closes Stdout *bytes.Buffer + // Sigterm is a channel where micro exits when written + Sigterm chan os.Signal ) func init() { From f05d3582b3f22b24596408375aa8639d6d9e0776 Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:07:10 +0800 Subject: [PATCH 2/3] Receive SIGINT only in RunInteractiveShell Temporarily reset SIGINT signal handlers and receive SIGINT in RunInteractiveShell. Do not try to kill the process in micro when signal is received. --- internal/shell/shell.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/shell/shell.go b/internal/shell/shell.go index c38e6e88..5bf5973f 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -11,6 +11,7 @@ import ( shellquote "github.com/kballard/go-shellquote" "github.com/zyedidia/micro/v2/internal/screen" + "github.com/zyedidia/micro/v2/internal/util" ) // ExecCommand executes a command using exec @@ -95,15 +96,11 @@ func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error cmd.Stderr = os.Stderr // This is a trap for Ctrl-C so that it doesn't kill micro - // Instead we trap Ctrl-C to kill the program we're running + // micro is killed if the signal is ignored only on Windows, so it is + // received c := make(chan os.Signal, 1) + signal.Reset(os.Interrupt) signal.Notify(c, os.Interrupt) - go func() { - for range c { - cmd.Process.Kill() - } - }() - cmd.Start() err = cmd.Wait() @@ -117,6 +114,9 @@ func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error // Start the screen back up screen.TempStart(screenb) + signal.Notify(util.Sigterm, os.Interrupt) + signal.Stop(c) + return output, err } From a84aa225ab3bbea69d2427a69c9875398f5f2cfc Mon Sep 17 00:00:00 2001 From: niten94 <127052329+niten94@users.noreply.github.com> Date: Sat, 22 Jun 2024 21:21:13 +0800 Subject: [PATCH 3/3] Return error with start in RunInteractiveShell Print and return error with process start in RunInteractiveShell if process was not able to be started. Wait until enter is pressed even if `wait` is false. Co-authored-by: Dmitry Maluka --- internal/shell/shell.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 5bf5973f..9149da9f 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -101,16 +101,19 @@ func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error c := make(chan os.Signal, 1) signal.Reset(os.Interrupt) signal.Notify(c, os.Interrupt) - cmd.Start() - err = cmd.Wait() + err = cmd.Start() + if err == nil { + err = cmd.Wait() + if wait { + // This is just so we don't return right away and let the user press enter to return + screen.TermMessage("") + } + } else { + screen.TermMessage(err) + } output := outputBytes.String() - if wait { - // This is just so we don't return right away and let the user press enter to return - screen.TermMessage("") - } - // Start the screen back up screen.TempStart(screenb)