From 2dbd22114b4906c6ae60de15f4c30a5ea38441c6 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 4 Dec 2021 00:07:41 +0100 Subject: Fixed standard input error handling --- pkg/pointer/exec.go | 12 ++++++++++-- pkg/pointer/intput-output.go | 23 ++++++++++++++--------- pkg/pointer/pointer.go | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/pointer/exec.go b/pkg/pointer/exec.go index 8bb9dba..55020fa 100644 --- a/pkg/pointer/exec.go +++ b/pkg/pointer/exec.go @@ -177,9 +177,17 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) { case ',': p.CharacterOutput(p.ss.head.Pop()) case '&': - p.ss.head.Push(p.DecimalInput()) + if v, err := p.DecimalInput(); err != nil { + p.Reverse() + } else { + p.ss.head.Push(v) + } case '~': - p.ss.head.Push(p.CharacterInput()) + if v, err := p.CharacterInput(); err != nil { + p.Reverse() + } else { + p.ss.head.Push(v) + } case 'y': n := p.ss.head.Pop() now := time.Now() diff --git a/pkg/pointer/intput-output.go b/pkg/pointer/intput-output.go index ac58d44..6a2423a 100644 --- a/pkg/pointer/intput-output.go +++ b/pkg/pointer/intput-output.go @@ -2,37 +2,42 @@ package pointer import ( "fmt" - "log" "os" ) var defaultInputLastChar *int = nil -func DefaultCharacterInput() int { +func DefaultCharacterInput() (int, error) { if defaultInputLastChar != nil { c := *defaultInputLastChar defaultInputLastChar = nil - return c + return c, nil } b := make([]byte, 1) i, err := os.Stdin.Read(b) if err != nil { - log.Fatalf("Error in DefaultCharacterInput { b: %c, i: %d, err: %+v }", b[0], i, err) + return 0, fmt.Errorf("Error in DefaultCharacterInput { b: %c, i: %d, err: %w }", b[0], i, err) } - return int(b[0]) + return int(b[0]), nil } -func DefaultDecimalInput() int { +func DefaultDecimalInput() (int, error) { var v int for { - c := DefaultCharacterInput() + c, err := DefaultCharacterInput() + if err != nil { + return 0, err + } if c >= '0' && c <= '9' { v = c - '0' break } } for { - c := DefaultCharacterInput() + c, err := DefaultCharacterInput() + if err != nil { + break + } if c >= '0' && c <= '9' { v = v*10 + c - '0' } else { @@ -40,7 +45,7 @@ func DefaultDecimalInput() int { break } } - return v + return v, nil } func DefaultCharacterOutput(c int) { diff --git a/pkg/pointer/pointer.go b/pkg/pointer/pointer.go index 4467235..1044cff 100644 --- a/pkg/pointer/pointer.go +++ b/pkg/pointer/pointer.go @@ -9,7 +9,7 @@ import ( var myRand = rand.New(rand.NewSource(time.Now().UnixNano())) -type InputFunction func() int +type InputFunction func() (int, error) type OutputFunction func(v int) type Pointer struct { -- cgit v1.2.3