fix: add newline at the end of input if in an unfinished heredoc

and define (or fix) the behavior of input ending with a slash
it will now add a newline at the end of input always
pull/322/head
sammyette 2024-08-16 13:50:34 -04:00
parent fc6a9a33e1
commit 7074a39799
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 44 additions and 25 deletions

52
exec.go
View File

@ -98,6 +98,7 @@ func runInput(input string, priv bool) {
var exitCode uint8 var exitCode uint8
var err error var err error
var cont bool var cont bool
var newline bool
// save incase it changes while prompting (For some reason) // save incase it changes while prompting (For some reason)
currentRunner := runnerMode currentRunner := runnerMode
if currentRunner.Type() == rt.StringType { if currentRunner.Type() == rt.StringType {
@ -108,9 +109,9 @@ func runInput(input string, priv bool) {
cmdFinish(0, input, priv) cmdFinish(0, input, priv)
return return
} }
input, exitCode, cont, err = handleSh(input) input, exitCode, cont, newline, err = handleSh(input)
case "hybridRev": case "hybridRev":
_, _, _, err = handleSh(input) _, _, _, _, err = handleSh(input)
if err == nil { if err == nil {
cmdFinish(0, input, priv) cmdFinish(0, input, priv)
return return
@ -119,12 +120,12 @@ func runInput(input string, priv bool) {
case "lua": case "lua":
input, exitCode, err = handleLua(input) input, exitCode, err = handleLua(input)
case "sh": case "sh":
input, exitCode, cont, err = handleSh(input) input, exitCode, cont, newline, err = handleSh(input)
} }
} else { } else {
// can only be a string or function so // can only be a string or function so
var runnerErr error var runnerErr error
input, exitCode, cont, runnerErr, err = runLuaRunner(currentRunner, input) input, exitCode, cont, newline, runnerErr, err = runLuaRunner(currentRunner, input)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
cmdFinish(124, input, priv) cmdFinish(124, input, priv)
@ -137,7 +138,7 @@ func runInput(input string, priv bool) {
} }
if cont { if cont {
input, err = reprompt(input) input, err = reprompt(input, newline)
if err == nil { if err == nil {
goto rerun goto rerun
} else if err == io.EOF { } else if err == io.EOF {
@ -155,26 +156,28 @@ func runInput(input string, priv bool) {
cmdFinish(exitCode, input, priv) cmdFinish(exitCode, input, priv)
} }
func reprompt(input string) (string, error) { func reprompt(input string, newline bool) (string, error) {
for { for {
in, err := continuePrompt(strings.TrimSuffix(input, "\\")) /*
if strings.HasSuffix(input, "\\") {
input = strings.TrimSuffix(input, "\\") + "\n"
}
*/
in, err := continuePrompt(input, newline)
if err != nil { if err != nil {
lr.SetPrompt(fmtPrompt(prompt)) lr.SetPrompt(fmtPrompt(prompt))
return input, err return input, err
} }
if strings.HasSuffix(in, "\\") {
continue
}
return in, nil return in, nil
} }
} }
func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, runnerErr, err error) { func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, newline bool, runnerErr, err error) {
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false) term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false)
err = rt.Call(l.MainThread(), runr, []rt.Value{rt.StringValue(userInput)}, term) err = rt.Call(l.MainThread(), runr, []rt.Value{rt.StringValue(userInput)}, term)
if err != nil { if err != nil {
return "", 124, false, nil, err return "", 124, false, false, nil, err
} }
var runner *rt.Table var runner *rt.Table
@ -202,6 +205,10 @@ func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8
if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok { if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok {
continued = c continued = c
} }
if nl, ok := runner.Get(rt.StringValue("newline")).TryBool(); ok {
newline = nl
}
return return
} }
@ -232,35 +239,40 @@ func handleLua(input string) (string, uint8, error) {
return cmdString, 125, err return cmdString, 125, err
} }
func handleSh(cmdString string) (input string, exitCode uint8, cont bool, runErr error) { func handleSh(cmdString string) (input string, exitCode uint8, cont bool, newline bool, runErr error) {
shRunner := hshMod.Get(rt.StringValue("runner")).AsTable().Get(rt.StringValue("sh")) shRunner := hshMod.Get(rt.StringValue("runner")).AsTable().Get(rt.StringValue("sh"))
var err error var err error
input, exitCode, cont, runErr, err = runLuaRunner(shRunner, cmdString) input, exitCode, cont, newline, runErr, err = runLuaRunner(shRunner, cmdString)
if err != nil { if err != nil {
runErr = err runErr = err
} }
return return
} }
func execSh(cmdString string) (string, uint8, bool, error) { func execSh(cmdString string) (input string, exitcode uint8, cont bool, newline bool, e error) {
_, _, err := execCommand(cmdString, nil) _, _, err := execCommand(cmdString, nil)
if err != nil { if err != nil {
// If input is incomplete, start multiline prompting // If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) { if syntax.IsIncomplete(err) {
if !interactive { if !interactive {
return cmdString, 126, false, err return cmdString, 126, false, false, err
} }
return cmdString, 126, true, err
newline := false
if strings.Contains(err.Error(), "unclosed here-document") {
newline = true
}
return cmdString, 126, true, newline, err
} else { } else {
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
return cmdString, code, false, nil return cmdString, code, false, false, nil
} else { } else {
return cmdString, 126, false, err return cmdString, 126, false, false, err
} }
} }
} }
return cmdString, 0, false, nil return cmdString, 0, false, false, nil
} }
// Run command in sh interpreter // Run command in sh interpreter

14
main.go
View File

@ -223,8 +223,9 @@ input:
} }
if strings.HasSuffix(input, "\\") { if strings.HasSuffix(input, "\\") {
print("\n")
for { for {
input, err = continuePrompt(input) input, err = continuePrompt(strings.TrimSuffix(input, "\\") + "\n", false)
if err != nil { if err != nil {
running = true running = true
lr.SetPrompt(fmtPrompt(prompt)) lr.SetPrompt(fmtPrompt(prompt))
@ -248,16 +249,21 @@ input:
exit(0) exit(0)
} }
func continuePrompt(prev string) (string, error) { func continuePrompt(prev string, newline bool) (string, error) {
hooks.Emit("multiline", nil) hooks.Emit("multiline", nil)
lr.SetPrompt(multilinePrompt) lr.SetPrompt(multilinePrompt)
cont, err := lr.Read() cont, err := lr.Read()
if err != nil { if err != nil {
return "", err return "", err
} }
cont = strings.TrimSpace(cont)
return prev + strings.TrimSuffix(cont, "\n"), nil if newline || strings.HasSuffix(cont, "\\") {
// a newline will get trimmed when this input is passed on, so we add 2
cont = cont + "\n\n"
}
return prev + cont, nil
} }
// This semi cursed function formats our prompt (obviously) // This semi cursed function formats our prompt (obviously)

View File

@ -85,7 +85,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return nil, err return nil, err
} }
_, exitCode, cont, err := execSh(aliases.Resolve(cmd)) _, exitCode, cont, newline, err := execSh(aliases.Resolve(cmd))
var luaErr rt.Value = rt.NilValue var luaErr rt.Value = rt.NilValue
if err != nil { if err != nil {
luaErr = rt.StringValue(err.Error()) luaErr = rt.StringValue(err.Error())
@ -94,6 +94,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
runnerRet.Set(rt.StringValue("input"), rt.StringValue(cmd)) runnerRet.Set(rt.StringValue("input"), rt.StringValue(cmd))
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode))) runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode)))
runnerRet.Set(rt.StringValue("continue"), rt.BoolValue(cont)) runnerRet.Set(rt.StringValue("continue"), rt.BoolValue(cont))
runnerRet.Set(rt.StringValue("newline"), rt.BoolValue(newline))
runnerRet.Set(rt.StringValue("err"), luaErr) runnerRet.Set(rt.StringValue("err"), luaErr)
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil