Skip to content

Commit

Permalink
iterating
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jul 9, 2022
1 parent 2f6e010 commit 009290d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
12 changes: 10 additions & 2 deletions internal/pkg/auxents/repl/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,17 @@ func ReplMain(args []string) int {
repl.openFiles(filenames)
}

repl.handleSession(os.Stdin)
err = repl.handleSession(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %v", repl.exeName, repl.replName, err)
os.Exit(1)
}

repl.bufferedRecordOutputStream.Flush()
repl.closeBufferedOutputStream()
err = repl.closeBufferedOutputStream()
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %v", repl.exeName, repl.replName, err)
os.Exit(1)
}
return 0
}
32 changes: 20 additions & 12 deletions internal/pkg/auxents/repl/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func controlCHandler(sysToSignalHandlerChannel chan os.Signal, appSignalNotifica
}

// ----------------------------------------------------------------
func (repl *Repl) handleSession(istream *os.File) {
func (repl *Repl) handleSession(istream *os.File) error {
if repl.showStartupBanner {
repl.printStartupBanner()
}
Expand All @@ -167,8 +167,7 @@ func (repl *Repl) handleSession(istream *os.File) {
}

if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %v", repl.exeName, repl.replName, err)
os.Exit(1)
return err
}

// Acknowledge any control-C's, even if typed at a ready prompt. We
Expand All @@ -191,9 +190,15 @@ func (repl *Repl) handleSession(istream *os.File) {
trimmedLine := strings.TrimSpace(line)

if trimmedLine == "<" {
repl.handleMultiLine(lineReader, ">", true) // multi-line immediate
err = repl.handleMultiLine(lineReader, ">", true) // multi-line immediate
if err != nil {
return err
}
} else if trimmedLine == "<<" {
repl.handleMultiLine(lineReader, ">>", false) // multi-line non-immediate
err = repl.handleMultiLine(lineReader, ">>", false) // multi-line non-immediate
if err != nil {
return err
}
} else if trimmedLine == ":quit" || trimmedLine == ":q" {
break
} else if repl.handleNonDSLLine(trimmedLine) {
Expand All @@ -206,6 +211,7 @@ func (repl *Repl) handleSession(istream *os.File) {
}
}
}
return nil
}

// ----------------------------------------------------------------
Expand All @@ -215,7 +221,7 @@ func (repl *Repl) handleMultiLine(
lineReader *bufio.Reader,
terminator string,
doImmediate bool,
) {
) error {
var buffer bytes.Buffer
for {
repl.printPrompt2()
Expand All @@ -226,8 +232,7 @@ func (repl *Repl) handleMultiLine(
}

if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %v\n", repl.exeName, repl.replName, err)
os.Exit(1)
return err
}

if strings.TrimSpace(line) == terminator {
Expand All @@ -246,6 +251,8 @@ func (repl *Repl) handleMultiLine(
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

return nil
}

func (repl *Repl) setBufferedOutputStream(
Expand All @@ -257,13 +264,14 @@ func (repl *Repl) setBufferedOutputStream(
repl.bufferedRecordOutputStream = bufio.NewWriter(recordOutputStream)
}

func (repl *Repl) closeBufferedOutputStream() {
func (repl *Repl) closeBufferedOutputStream() error {
if repl.recordOutputStream != os.Stdout {
err := repl.recordOutputStream.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "mlr repl: error on redirect close of %s: %v\n",
repl.recordOutputFileName, err)
os.Exit(1)
return fmt.Errorf("mlr repl: error on redirect close of %s: %v\n",
repl.recordOutputFileName, err,
)
}
}
return nil
}

0 comments on commit 009290d

Please sign in to comment.