Skip to content

Commit b6baa19

Browse files
committed
interp: move oneIf under exitStatus
1 parent 46bb4f2 commit b6baa19

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

interp/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ type exitStatus struct {
189189
err error
190190
}
191191

192+
func (e *exitStatus) oneIf(b bool) {
193+
if b {
194+
e.code = 1
195+
} else {
196+
e.code = 0
197+
}
198+
}
199+
192200
func (e *exitStatus) fatal(err error) {
193201
if !e.fatalExit && err != nil {
194202
e.exiting = true

interp/builtin.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ func IsBuiltin(name string) bool {
3838
return false
3939
}
4040

41-
func oneIf(b bool) uint8 {
42-
if b {
43-
return 1
44-
}
45-
return 0
46-
}
47-
4841
// TODO: atoi is duplicated in the expand package.
4942

5043
// atoi is like [strconv.ParseInt](s, 10, 64), but it ignores errors and trims whitespace.
@@ -427,7 +420,7 @@ func (r *Runner) builtin(ctx context.Context, pos syntax.Pos, name string, args
427420
exit.code = 2
428421
return exit
429422
}
430-
exit.code = oneIf(r.bashTest(ctx, expr, true) == "")
423+
exit.oneIf(r.bashTest(ctx, expr, true) == "")
431424
case "exec":
432425
// TODO: Consider unix.Exec, i.e. actually replacing
433426
// the process. It's in theory what a shell should do,
@@ -667,7 +660,7 @@ func (r *Runner) builtin(ctx context.Context, pos syntax.Pos, name string, args
667660
r.setVarString("OPTIND", strconv.FormatInt(int64(r.optState.argidx+1), 10))
668661
}
669662

670-
exit.code = oneIf(done)
663+
exit.oneIf(done)
671664

672665
case "shopt":
673666
mode := ""

interp/runner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (r *Runner) stmtSync(ctx context.Context, st *syntax.Stmt) {
340340
}
341341
if st.Negated {
342342
// TODO: negate the entire [exitStatus] here, wiping errors
343-
r.exit.code = oneIf(r.exit.code == 0)
343+
r.exit.oneIf(r.exit.code == 0)
344344
} else if b, ok := st.Cmd.(*syntax.BinaryCmd); ok && (b.Op == syntax.AndStmt || b.Op == syntax.OrStmt) {
345345
} else if r.exit.code != 0 && !r.noErrExit {
346346
r.trapCallback(ctx, r.callbackErr, "error")
@@ -611,7 +611,7 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
611611
case *syntax.FuncDecl:
612612
r.setFunc(cm.Name.Value, cm.Body)
613613
case *syntax.ArithmCmd:
614-
r.exit.code = oneIf(r.arithm(cm.X) == 0)
614+
r.exit.oneIf(r.arithm(cm.X) == 0)
615615
case *syntax.LetClause:
616616
var val int
617617
for _, expr := range cm.Exprs {
@@ -636,7 +636,7 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
636636
}
637637

638638
trace.newLineFlush()
639-
r.exit.code = oneIf(val == 0)
639+
r.exit.oneIf(val == 0)
640640
case *syntax.CaseClause:
641641
trace.string("case ")
642642
trace.expr(cm.Word)

0 commit comments

Comments
 (0)