From f7f231244c91ac980b0c06abdc66d25e4ca904b4 Mon Sep 17 00:00:00 2001 From: Ethan Marshall Date: Wed, 24 Jan 2024 16:43:13 +0000 Subject: [PATCH] Fix bad script input buffering strategy --- pajen.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pajen.go b/pajen.go index 8776de7..6867549 100644 --- a/pajen.go +++ b/pajen.go @@ -1,11 +1,11 @@ package main import ( + "bufio" "fmt" "io" "log" "os/exec" - "strings" ) // Pajen represents an instance of the pajen.pl script running in infinite @@ -51,22 +51,17 @@ func (r *Pajen) Read(b []byte) (int, error) { } func (r *Pajen) run() { - buf := make([]byte, 255) + sc := bufio.NewScanner(r.out) + sc.Split(bufio.ScanLines) - for { - _, err := r.out.Read(buf) - if err != nil { - log.Println("pajen read error:", err) - return - } - - wk := string(buf) - lines := strings.Split(wk, "\n") - - for _, l := range lines { - if l != "" && l != "\n" { - r.Chan <- l - } + for sc.Scan() { + l := sc.Text() + if l != "" && l != "\n" { + r.Chan <- l } } + + if sc.Err() != nil { + log.Println("pajen: output read error: %s", sc.Err()) + } }