Skip to content

Commit 7074a39

Browse files
committed
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
1 parent fc6a9a3 commit 7074a39

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

exec.go

+32-20
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func runInput(input string, priv bool) {
9898
var exitCode uint8
9999
var err error
100100
var cont bool
101+
var newline bool
101102
// save incase it changes while prompting (For some reason)
102103
currentRunner := runnerMode
103104
if currentRunner.Type() == rt.StringType {
@@ -108,9 +109,9 @@ func runInput(input string, priv bool) {
108109
cmdFinish(0, input, priv)
109110
return
110111
}
111-
input, exitCode, cont, err = handleSh(input)
112+
input, exitCode, cont, newline, err = handleSh(input)
112113
case "hybridRev":
113-
_, _, _, err = handleSh(input)
114+
_, _, _, _, err = handleSh(input)
114115
if err == nil {
115116
cmdFinish(0, input, priv)
116117
return
@@ -119,12 +120,12 @@ func runInput(input string, priv bool) {
119120
case "lua":
120121
input, exitCode, err = handleLua(input)
121122
case "sh":
122-
input, exitCode, cont, err = handleSh(input)
123+
input, exitCode, cont, newline, err = handleSh(input)
123124
}
124125
} else {
125126
// can only be a string or function so
126127
var runnerErr error
127-
input, exitCode, cont, runnerErr, err = runLuaRunner(currentRunner, input)
128+
input, exitCode, cont, newline, runnerErr, err = runLuaRunner(currentRunner, input)
128129
if err != nil {
129130
fmt.Fprintln(os.Stderr, err)
130131
cmdFinish(124, input, priv)
@@ -137,7 +138,7 @@ func runInput(input string, priv bool) {
137138
}
138139

139140
if cont {
140-
input, err = reprompt(input)
141+
input, err = reprompt(input, newline)
141142
if err == nil {
142143
goto rerun
143144
} else if err == io.EOF {
@@ -155,26 +156,28 @@ func runInput(input string, priv bool) {
155156
cmdFinish(exitCode, input, priv)
156157
}
157158

158-
func reprompt(input string) (string, error) {
159+
func reprompt(input string, newline bool) (string, error) {
159160
for {
160-
in, err := continuePrompt(strings.TrimSuffix(input, "\\"))
161+
/*
162+
if strings.HasSuffix(input, "\\") {
163+
input = strings.TrimSuffix(input, "\\") + "\n"
164+
}
165+
*/
166+
in, err := continuePrompt(input, newline)
161167
if err != nil {
162168
lr.SetPrompt(fmtPrompt(prompt))
163169
return input, err
164170
}
165171

166-
if strings.HasSuffix(in, "\\") {
167-
continue
168-
}
169172
return in, nil
170173
}
171174
}
172175

173-
func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, runnerErr, err error) {
176+
func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8, continued bool, newline bool, runnerErr, err error) {
174177
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 3, false)
175178
err = rt.Call(l.MainThread(), runr, []rt.Value{rt.StringValue(userInput)}, term)
176179
if err != nil {
177-
return "", 124, false, nil, err
180+
return "", 124, false, false, nil, err
178181
}
179182

180183
var runner *rt.Table
@@ -202,6 +205,10 @@ func runLuaRunner(runr rt.Value, userInput string) (input string, exitCode uint8
202205
if c, ok := runner.Get(rt.StringValue("continue")).TryBool(); ok {
203206
continued = c
204207
}
208+
209+
if nl, ok := runner.Get(rt.StringValue("newline")).TryBool(); ok {
210+
newline = nl
211+
}
205212
return
206213
}
207214

@@ -232,35 +239,40 @@ func handleLua(input string) (string, uint8, error) {
232239
return cmdString, 125, err
233240
}
234241

235-
func handleSh(cmdString string) (input string, exitCode uint8, cont bool, runErr error) {
242+
func handleSh(cmdString string) (input string, exitCode uint8, cont bool, newline bool, runErr error) {
236243
shRunner := hshMod.Get(rt.StringValue("runner")).AsTable().Get(rt.StringValue("sh"))
237244
var err error
238-
input, exitCode, cont, runErr, err = runLuaRunner(shRunner, cmdString)
245+
input, exitCode, cont, newline, runErr, err = runLuaRunner(shRunner, cmdString)
239246
if err != nil {
240247
runErr = err
241248
}
242249
return
243250
}
244251

245-
func execSh(cmdString string) (string, uint8, bool, error) {
252+
func execSh(cmdString string) (input string, exitcode uint8, cont bool, newline bool, e error) {
246253
_, _, err := execCommand(cmdString, nil)
247254
if err != nil {
248255
// If input is incomplete, start multiline prompting
249256
if syntax.IsIncomplete(err) {
250257
if !interactive {
251-
return cmdString, 126, false, err
258+
return cmdString, 126, false, false, err
259+
}
260+
261+
newline := false
262+
if strings.Contains(err.Error(), "unclosed here-document") {
263+
newline = true
252264
}
253-
return cmdString, 126, true, err
265+
return cmdString, 126, true, newline, err
254266
} else {
255267
if code, ok := interp.IsExitStatus(err); ok {
256-
return cmdString, code, false, nil
268+
return cmdString, code, false, false, nil
257269
} else {
258-
return cmdString, 126, false, err
270+
return cmdString, 126, false, false, err
259271
}
260272
}
261273
}
262274

263-
return cmdString, 0, false, nil
275+
return cmdString, 0, false, false, nil
264276
}
265277

266278
// Run command in sh interpreter

main.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ input:
223223
}
224224

225225
if strings.HasSuffix(input, "\\") {
226+
print("\n")
226227
for {
227-
input, err = continuePrompt(input)
228+
input, err = continuePrompt(strings.TrimSuffix(input, "\\") + "\n", false)
228229
if err != nil {
229230
running = true
230231
lr.SetPrompt(fmtPrompt(prompt))
@@ -248,16 +249,21 @@ input:
248249
exit(0)
249250
}
250251

251-
func continuePrompt(prev string) (string, error) {
252+
func continuePrompt(prev string, newline bool) (string, error) {
252253
hooks.Emit("multiline", nil)
253254
lr.SetPrompt(multilinePrompt)
255+
254256
cont, err := lr.Read()
255257
if err != nil {
256258
return "", err
257259
}
258-
cont = strings.TrimSpace(cont)
259260

260-
return prev + strings.TrimSuffix(cont, "\n"), nil
261+
if newline || strings.HasSuffix(cont, "\\") {
262+
// a newline will get trimmed when this input is passed on, so we add 2
263+
cont = cont + "\n\n"
264+
}
265+
266+
return prev + cont, nil
261267
}
262268

263269
// This semi cursed function formats our prompt (obviously)

runnermode.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
8585
return nil, err
8686
}
8787

88-
_, exitCode, cont, err := execSh(aliases.Resolve(cmd))
88+
_, exitCode, cont, newline, err := execSh(aliases.Resolve(cmd))
8989
var luaErr rt.Value = rt.NilValue
9090
if err != nil {
9191
luaErr = rt.StringValue(err.Error())
@@ -94,6 +94,7 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
9494
runnerRet.Set(rt.StringValue("input"), rt.StringValue(cmd))
9595
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode)))
9696
runnerRet.Set(rt.StringValue("continue"), rt.BoolValue(cont))
97+
runnerRet.Set(rt.StringValue("newline"), rt.BoolValue(newline))
9798
runnerRet.Set(rt.StringValue("err"), luaErr)
9899

99100
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil

0 commit comments

Comments
 (0)