| 1 | // Copyright (c) 2017, Daniel Martà <[email protected]> |
| 2 | // See LICENSE for licensing information |
| 3 | |
| 4 | package interpscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/ |
| 5 | |
| 6 | import ( |
| 7 | "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/" |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 10 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 12 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 13 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 14 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 15 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 16 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 17 | "syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/" |
| 18 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 19 | |
| 20 | "golang.org/x/termscip-go gomod golang.org/x/term v0.41.0 `golang.org/x/term`/" |
| 21 | |
| 22 | "mvdan.cc/sh/v3/expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/" |
| 23 | "mvdan.cc/sh/v3/syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/" |
| 24 | ) |
| 25 | |
| 26 | // TODO: given the categories below, perhaps this should be more like: |
| 27 | // |
| 28 | // func IsBuiltin(lang syntax.LangVariant, name string) bool |
| 29 | // |
| 30 | // or perhaps some API that also lets the user iterate through the builtins? |
| 31 | // |
| 32 | // Also, should we move this to the syntax package too? |
| 33 | // It's not a syntactical property strictly speaking, |
| 34 | // but it's also odd to require importing the interp package for it. |
| 35 | |
| 36 | // IsBuiltin returns true if the given word is a POSIX Shell |
| 37 | // or Bash builtin. |
| 38 | func IsBuiltinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/IsBuiltin().(name string) bool { |
| 39 | switch name { |
| 40 | case |
| 41 | // POSIX Shell builtins, from section 1.d obtained in September 2025 from: |
| 42 | // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01 |
| 43 | "alias", |
| 44 | "bg", |
| 45 | "cd", |
| 46 | "command", |
| 47 | "false", |
| 48 | "fc", |
| 49 | "fg", |
| 50 | "getopts", |
| 51 | "hash", |
| 52 | "jobs", |
| 53 | "kill", |
| 54 | "newgrp", |
| 55 | "pwd", |
| 56 | "read", |
| 57 | "true", |
| 58 | "umask", |
| 59 | "unalias", |
| 60 | "wait", |
| 61 | |
| 62 | // POSIX Shell special built-ins, obtained in September 2025 from: |
| 63 | // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14 |
| 64 | "break", |
| 65 | ":", |
| 66 | "continue", |
| 67 | ".", |
| 68 | "eval", |
| 69 | "exec", |
| 70 | "exit", |
| 71 | "export", // NOTE: our parser treats this as a keyword |
| 72 | "readonly", // NOTE: our parser treats this as a keyword |
| 73 | "return", |
| 74 | "set", |
| 75 | "shift", |
| 76 | "times", |
| 77 | "trap", |
| 78 | "unset", |
| 79 | |
| 80 | // Bash built-ins which are not present in POSIX, obtained in September 2025 from: |
| 81 | // https://man.archlinux.org/man/bash.1.en#SHELL_BUILTIN_COMMANDS |
| 82 | "source", |
| 83 | "bind", |
| 84 | "builtin", |
| 85 | "caller", |
| 86 | "compgen", |
| 87 | "complete", |
| 88 | "compopt", |
| 89 | "declare", // NOTE: our parser treats this as a keyword |
| 90 | "typeset", // NOTE: our parser treats this as a keyword |
| 91 | "dirs", |
| 92 | "disown", |
| 93 | "echo", // TODO: surely this is POSIX? but why is it not in the main POSIX spec page? |
| 94 | "enable", |
| 95 | "history", |
| 96 | "help", |
| 97 | "let", // NOTE: our parser treats this as a keyword |
| 98 | "local", |
| 99 | "logout", |
| 100 | "mapfile", |
| 101 | "readarray", |
| 102 | "popd", |
| 103 | "printf", // TODO: surely this is POSIX? but why is it not in the main POSIX spec page? |
| 104 | "pushd", |
| 105 | "shopt", |
| 106 | "suspend", |
| 107 | "test", |
| 108 | "[", // NOTE: an alias for "test", not explicitly listed |
| 109 | "type", |
| 110 | "ulimit": |
| 111 | return true |
| 112 | } |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | // TODO: atoi is duplicated in the expand package. |
| 117 | |
| 118 | // atoi is like [strconv.ParseInt](s, 10, 64), but it ignores errors and trims whitespace. |
| 119 | func atoiscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/atoi().(s string) int64 { |
| 120 | s = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(s) |
| 121 | n, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(s, 10, 64) |
| 122 | return n |
| 123 | } |
| 124 | |
| 125 | type errBuiltinExitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/errBuiltinExitStatus# exitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus# |
| 126 | |
| 127 | func (e errBuiltinExitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/errBuiltinExitStatus#) Errorscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/errBuiltinExitStatus#Error().() string { |
| 128 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("builtin exit status %d", e.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code.) |
| 129 | } |
| 130 | |
| 131 | // Builtin allows [ExecHandlerFunc] implementations to execute any builtin, |
| 132 | // which can be useful for an exec handler to wrap or combine builtin calls. |
| 133 | // |
| 134 | // Note that a non-nil error may be returned in cases where the builtin |
| 135 | // alters the control flow of the runner, even if the builtin did not fail. |
| 136 | // For example, this is the case with `exit 0` or `return`. |
| 137 | func (hc HandlerContextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/HandlerContext#) Builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/HandlerContext#Builtin().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, args []string) error { |
| 138 | if hc.kindscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/HandlerContext#kind. != handlerKindExecscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/handlerKindExec. { |
| 139 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("HandlerContext.Builtin can only be called via an ExecHandlerFunc") |
| 140 | } |
| 141 | exit := hc.runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/HandlerContext#runner..builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, hc.Posscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/HandlerContext#Pos., args[0], args[1:]) |
| 142 | if exit != (exitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#{}) { |
| 143 | return errBuiltinExitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/errBuiltinExitStatus#(exit) |
| 144 | } |
| 145 | return nil |
| 146 | } |
| 147 | |
| 148 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, pos syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.Posscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Pos#, name string, args []string) (exit exitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#) { |
| 149 | failf := func(code uint8, format string, args ...any) exitStatusscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus# { |
| 150 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().(format, args...) |
| 151 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = code |
| 152 | return exit |
| 153 | } |
| 154 | switch name { |
| 155 | case ":", "true": |
| 156 | case "false": |
| 157 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 1 |
| 158 | case "exit": |
| 159 | switch len(args) { |
| 160 | case 0: |
| 161 | exit = r.lastExitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#lastExit. |
| 162 | case 1: |
| 163 | n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(args[0]) |
| 164 | if err != nil { |
| 165 | return failf(2, "invalid exit status code: %q\n", args[0]) |
| 166 | } |
| 167 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = uint8(n) |
| 168 | default: |
| 169 | return failf(1, "exit cannot take multiple arguments\n") |
| 170 | } |
| 171 | exit.exitingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#exiting. = true |
| 172 | case "set": |
| 173 | if err := Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Params().(args...)(r); err != nil { |
| 174 | return failf(2, "set: %v\n", err) |
| 175 | } |
| 176 | r.updateExpandOptsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#updateExpandOpts().() |
| 177 | case "shift": |
| 178 | n := 1 |
| 179 | switch len(args) { |
| 180 | case 0: |
| 181 | case 1: |
| 182 | if n2, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(args[0]); err == nil { |
| 183 | n = n2 |
| 184 | break |
| 185 | } |
| 186 | fallthrough |
| 187 | default: |
| 188 | return failf(2, "usage: shift [n]\n") |
| 189 | } |
| 190 | if n >= len(r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params.) { |
| 191 | r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. = nil |
| 192 | } else { |
| 193 | r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. = r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params.[n:] |
| 194 | } |
| 195 | case "unset": |
| 196 | vars := true |
| 197 | funcs := true |
| 198 | unsetOpts: |
| 199 | for i, arg := range args { |
| 200 | switch arg { |
| 201 | case "-v": |
| 202 | funcs = false |
| 203 | case "-f": |
| 204 | vars = false |
| 205 | default: |
| 206 | args = args[i:] |
| 207 | break unsetOpts |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | for _, arg := range args { |
| 212 | if vars && r.lookupVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#lookupVar().(arg).IsSetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#IsSet().() { |
| 213 | r.delVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#delVar().(arg) |
| 214 | } else if _, ok := r.Funcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Funcs.[arg]; ok && funcs { |
| 215 | delete(r.Funcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Funcs., arg) |
| 216 | } |
| 217 | } |
| 218 | case "echo": |
| 219 | newline, doExpand := true, false |
| 220 | echoOpts: |
| 221 | for len(args) > 0 { |
| 222 | switch args[0] { |
| 223 | case "-n": |
| 224 | newline = false |
| 225 | case "-e": |
| 226 | doExpand = true |
| 227 | case "-E": // default |
| 228 | default: |
| 229 | break echoOpts |
| 230 | } |
| 231 | args = args[1:] |
| 232 | } |
| 233 | for i, arg := range args { |
| 234 | if i > 0 { |
| 235 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().(" ") |
| 236 | } |
| 237 | if doExpand { |
| 238 | arg, _, _ = expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Formatscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Format().(r.ecfgscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#ecfg., arg, nil) |
| 239 | } |
| 240 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().(arg) |
| 241 | } |
| 242 | if newline { |
| 243 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("\n") |
| 244 | } |
| 245 | case "printf": |
| 246 | if len(args) == 0 { |
| 247 | return failf(2, "usage: printf format [arguments]\n") |
| 248 | } |
| 249 | format, args := args[0], args[1:] |
| 250 | for { |
| 251 | s, n, err := expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Formatscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Format().(r.ecfgscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#ecfg., format, args) |
| 252 | if err != nil { |
| 253 | return failf(1, "%v\n", err) |
| 254 | } |
| 255 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().(s) |
| 256 | args = args[n:] |
| 257 | if n == 0 || len(args) == 0 { |
| 258 | break |
| 259 | } |
| 260 | } |
| 261 | case "break", "continue": |
| 262 | if !r.inLoopscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inLoop. { |
| 263 | return failf(0, "%s is only useful in a loop\n", name) |
| 264 | } |
| 265 | enclosing := &r.breakEnclosingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#breakEnclosing. |
| 266 | if name == "continue" { |
| 267 | enclosing = &r.contnEnclosingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#contnEnclosing. |
| 268 | } |
| 269 | switch len(args) { |
| 270 | case 0: |
| 271 | *enclosing = 1 |
| 272 | case 1: |
| 273 | if n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(args[0]); err == nil { |
| 274 | *enclosing = n |
| 275 | break |
| 276 | } |
| 277 | fallthrough |
| 278 | default: |
| 279 | return failf(2, "usage: %s [n]\n", name) |
| 280 | } |
| 281 | case "pwd": |
| 282 | evalSymlinks := false |
| 283 | for len(args) > 0 { |
| 284 | switch args[0] { |
| 285 | case "-L": |
| 286 | evalSymlinks = false |
| 287 | case "-P": |
| 288 | evalSymlinks = true |
| 289 | default: |
| 290 | return failf(2, "invalid option: %q\n", args[0]) |
| 291 | } |
| 292 | args = args[1:] |
| 293 | } |
| 294 | pwd := r.envGetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#envGet().("PWD") |
| 295 | if evalSymlinks { |
| 296 | var err error |
| 297 | pwd, err = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.EvalSymlinksscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/EvalSymlinks().(pwd) |
| 298 | if err != nil { |
| 299 | exit.fatalscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#fatal().(err) // perhaps overly dramatic? |
| 300 | return exit |
| 301 | } |
| 302 | } |
| 303 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\n", pwd) |
| 304 | case "cd": |
| 305 | var path string |
| 306 | switch len(args) { |
| 307 | case 0: |
| 308 | path = r.envGetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#envGet().("HOME") |
| 309 | case 1: |
| 310 | path = args[0] |
| 311 | |
| 312 | // replicate the commonly implemented behavior of `cd -` |
| 313 | // ref: https://www.man7.org/linux/man-pages/man1/cd.1p.html#OPERANDS |
| 314 | if path == "-" { |
| 315 | path = r.envGetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#envGet().("OLDPWD") |
| 316 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\n", path) |
| 317 | } |
| 318 | default: |
| 319 | return failf(2, "usage: cd [dir]\n") |
| 320 | } |
| 321 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = r.changeDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#changeDir().(ctx, "cd", path) |
| 322 | case "wait": |
| 323 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 324 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 325 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 326 | case "-n", "-p": |
| 327 | return failf(2, "wait: unsupported option %q\n", flag) |
| 328 | default: |
| 329 | return failf(2, "wait: invalid option %q\n", flag) |
| 330 | } |
| 331 | } |
| 332 | if len(args) == 0 { |
| 333 | // Note that "wait" without arguments always returns exit status zero. |
| 334 | for _, bg := range r.bgProcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#bgProcs. { |
| 335 | <-bg.donescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bgProc#done. |
| 336 | } |
| 337 | break |
| 338 | } |
| 339 | for _, arg := range args { |
| 340 | arg, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(arg, "g") |
| 341 | pid := atoiscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/atoi().(arg) |
| 342 | if !ok || pid <= 0 || pid > int64(len(r.bgProcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#bgProcs.)) { |
| 343 | return failf(1, "wait: pid %s is not a child of this shell\n", arg) |
| 344 | } |
| 345 | bg := r.bgProcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#bgProcs.[pid-1] |
| 346 | <-bg.donescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bgProc#done. |
| 347 | exit = *bg.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bgProc#exit. |
| 348 | } |
| 349 | case "builtin": |
| 350 | if len(args) < 1 { |
| 351 | break |
| 352 | } |
| 353 | if !IsBuiltinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/IsBuiltin().(args[0]) { |
| 354 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 1 |
| 355 | return exit |
| 356 | } |
| 357 | exit = r.builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, pos, args[0], args[1:]) |
| 358 | case "type": |
| 359 | anyNotFound := false |
| 360 | mode := "" |
| 361 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 362 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 363 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 364 | case "-a", "-f", "--help": |
| 365 | return failf(3, "command: NOT IMPLEMENTED\n") |
| 366 | case "-p", "-P", "-t": |
| 367 | mode = flag |
| 368 | default: |
| 369 | return failf(2, "command: invalid option %q\n", flag) |
| 370 | } |
| 371 | } |
| 372 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 373 | for _, arg := range args { |
| 374 | if mode == "-p" || mode == "-P" { |
| 375 | if path, err := LookPathDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/LookPathDir().(r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir., r.writeEnvscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#writeEnv., arg); err == nil { |
| 376 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\n", path) |
| 377 | } else { |
| 378 | anyNotFound = true |
| 379 | } |
| 380 | continue |
| 381 | } |
| 382 | if syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.IsKeywordscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/IsKeyword().(arg) { |
| 383 | if mode == "-t" { |
| 384 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("keyword\n") |
| 385 | } else { |
| 386 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s is a shell keyword\n", arg) |
| 387 | } |
| 388 | continue |
| 389 | } |
| 390 | if als, ok := r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias.[arg]; ok && r.optsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#opts.[optExpandAliasesscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/optExpandAliases.] { |
| 391 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 392 | if len(als.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#args.) > 0 { |
| 393 | printer := syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.NewPrinterscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/NewPrinter().() |
| 394 | printer.Printscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Printer#Print().(&buf, &syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.CallExprscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/CallExpr#{ |
| 395 | Argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/CallExpr#Args.: als.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#args., |
| 396 | }) |
| 397 | } |
| 398 | if als.blankscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#blank. { |
| 399 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteByte().(' ') |
| 400 | } |
| 401 | if mode == "-t" { |
| 402 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("alias\n") |
| 403 | } else { |
| 404 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s is aliased to `%s'\n", arg, &buf) |
| 405 | } |
| 406 | continue |
| 407 | } |
| 408 | if _, ok := r.Funcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Funcs.[arg]; ok { |
| 409 | if mode == "-t" { |
| 410 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("function\n") |
| 411 | } else { |
| 412 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s is a function\n", arg) |
| 413 | } |
| 414 | continue |
| 415 | } |
| 416 | if IsBuiltinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/IsBuiltin().(arg) { |
| 417 | if mode == "-t" { |
| 418 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("builtin\n") |
| 419 | } else { |
| 420 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s is a shell builtin\n", arg) |
| 421 | } |
| 422 | continue |
| 423 | } |
| 424 | if path, err := LookPathDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/LookPathDir().(r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir., r.writeEnvscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#writeEnv., arg); err == nil { |
| 425 | if mode == "-t" { |
| 426 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("file\n") |
| 427 | } else { |
| 428 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s is %s\n", arg, path) |
| 429 | } |
| 430 | continue |
| 431 | } |
| 432 | if mode != "-t" { |
| 433 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("type: %s: not found\n", arg) |
| 434 | } |
| 435 | anyNotFound = true |
| 436 | } |
| 437 | if anyNotFound { |
| 438 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 1 |
| 439 | } |
| 440 | case "hash": |
| 441 | // TODO: implement. for now, having this as a no-op is better than nothing. |
| 442 | case "eval": |
| 443 | src := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(args, " ") |
| 444 | p := syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.NewParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/NewParser().() |
| 445 | file, err := p.Parsescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Parser#Parse().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReader().(src), "") |
| 446 | if err != nil { |
| 447 | return failf(1, "eval: %v\n", err) |
| 448 | } |
| 449 | r.stmtsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stmts().(ctx, file.Stmtsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/File#Stmts.) |
| 450 | exit = r.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exit. |
| 451 | case "source", ".": |
| 452 | if len(args) < 1 { |
| 453 | return failf(2, "%v: source: need filename\n", pos) |
| 454 | } |
| 455 | path, err := scriptFromPathDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/scriptFromPathDir().(r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir., r.writeEnvscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#writeEnv., args[0]) |
| 456 | if err != nil { |
| 457 | // If the script was not found in PATH or there was any error, pass |
| 458 | // the source path to the open handler so it has a chance to look |
| 459 | // at files it manages (eg: virtual filesystem), and also allow |
| 460 | // it to look for the sourced script in the current directory. |
| 461 | path = args[0] |
| 462 | } |
| 463 | f, err := r.openscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#open().(ctx, path, osscip-go gomod github.com/golang/go/src go1.25.0 os/.O_RDONLYscip-go gomod github.com/golang/go/src go1.25.0 os/O_RDONLY., 0, false) |
| 464 | if err != nil { |
| 465 | return failf(1, "source: %v\n", err) |
| 466 | } |
| 467 | defer f.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() |
| 468 | p := syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.NewParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/NewParser().() |
| 469 | file, err := p.Parsescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Parser#Parse().(f, path) |
| 470 | if err != nil { |
| 471 | return failf(1, "source: %v\n", err) |
| 472 | } |
| 473 | |
| 474 | // Keep the current versions of some fields we might modify. |
| 475 | oldParams := r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. |
| 476 | oldSourceSetParams := r.sourceSetParamsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#sourceSetParams. |
| 477 | oldInSource := r.inSourcescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inSource. |
| 478 | |
| 479 | // If we run "source file args...", set said args as parameters. |
| 480 | // Otherwise, keep the current parameters. |
| 481 | sourceArgs := len(args[1:]) > 0 |
| 482 | if sourceArgs { |
| 483 | r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. = args[1:] |
| 484 | r.sourceSetParamsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#sourceSetParams. = false |
| 485 | } |
| 486 | // We want to track if the sourced file explicitly sets the |
| 487 | // parameters. |
| 488 | r.sourceSetParamsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#sourceSetParams. = false |
| 489 | r.inSourcescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inSource. = true // know that we're inside a sourced script. |
| 490 | r.stmtsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stmts().(ctx, file.Stmtsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/File#Stmts.) |
| 491 | |
| 492 | // If we modified the parameters and the sourced file didn't |
| 493 | // explicitly set them, we restore the old ones. |
| 494 | if sourceArgs && !r.sourceSetParamsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#sourceSetParams. { |
| 495 | r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. = oldParams |
| 496 | } |
| 497 | r.sourceSetParamsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#sourceSetParams. = oldSourceSetParams |
| 498 | r.inSourcescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inSource. = oldInSource |
| 499 | |
| 500 | exit = r.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exit. |
| 501 | exit.returningscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#returning. = false |
| 502 | case "[": |
| 503 | if len(args) == 0 || args[len(args)-1] != "]" { |
| 504 | return failf(2, "%v: [: missing matching ]\n", pos) |
| 505 | } |
| 506 | args = args[:len(args)-1] |
| 507 | fallthrough |
| 508 | case "test": |
| 509 | parseErr := false |
| 510 | p := testParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/testParser#{ |
| 511 | remscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/testParser#rem.: args, |
| 512 | errscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/testParser#err.: func(err error) { |
| 513 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("%v: %v\n", pos, err) |
| 514 | parseErr = true |
| 515 | }, |
| 516 | } |
| 517 | p.nextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/testParser#next().() |
| 518 | expr := p.classicTestscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/testParser#classicTest().("[", false) |
| 519 | if parseErr { |
| 520 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 2 |
| 521 | return exit |
| 522 | } |
| 523 | exit.oneIfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#oneIf().(r.bashTestscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#bashTest().(ctx, expr, true) == "") |
| 524 | case "exec": |
| 525 | // TODO: Consider unix.Exec, i.e. actually replacing |
| 526 | // the process. It's in theory what a shell should do, |
| 527 | // but in practice it would kill the entire Go process |
| 528 | // and it's not available on Windows. |
| 529 | if len(args) == 0 { |
| 530 | r.keepRedirsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#keepRedirs. = true |
| 531 | break |
| 532 | } |
| 533 | r.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exit..exitingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#exiting. = true |
| 534 | r.execscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exec().(ctx, pos, args) |
| 535 | exit = r.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exit. |
| 536 | case "command": |
| 537 | show := false |
| 538 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 539 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 540 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 541 | case "-v": |
| 542 | show = true |
| 543 | default: |
| 544 | return failf(2, "command: invalid option %q\n", flag) |
| 545 | } |
| 546 | } |
| 547 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 548 | if len(args) == 0 { |
| 549 | break |
| 550 | } |
| 551 | if !show { |
| 552 | if IsBuiltinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/IsBuiltin().(args[0]) { |
| 553 | return r.builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, pos, args[0], args[1:]) |
| 554 | } |
| 555 | r.execscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exec().(ctx, pos, args) |
| 556 | exit = r.exitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#exit. |
| 557 | return exit |
| 558 | } |
| 559 | last := uint8(0) |
| 560 | for _, arg := range args { |
| 561 | last = 0 |
| 562 | if r.Funcsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Funcs.[arg] != nil || IsBuiltinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/IsBuiltin().(arg) { |
| 563 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\n", arg) |
| 564 | } else if path, err := LookPathDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/LookPathDir().(r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir., r.writeEnvscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#writeEnv., arg); err == nil { |
| 565 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\n", path) |
| 566 | } else { |
| 567 | last = 1 |
| 568 | } |
| 569 | } |
| 570 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = last |
| 571 | case "dirs": |
| 572 | for i, dir := range slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Backwardscip-go gomod github.com/golang/go/src go1.25.0 slices/Backward().(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.) { |
| 573 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s", dir) |
| 574 | if i > 0 { |
| 575 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().(" ") |
| 576 | } |
| 577 | } |
| 578 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().("\n") |
| 579 | case "pushd": |
| 580 | change := true |
| 581 | if len(args) > 0 && args[0] == "-n" { |
| 582 | change = false |
| 583 | args = args[1:] |
| 584 | } |
| 585 | swap := func() string { |
| 586 | oldtop := r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] |
| 587 | top := r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-2] |
| 588 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] = top |
| 589 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-2] = oldtop |
| 590 | return top |
| 591 | } |
| 592 | switch len(args) { |
| 593 | case 0: |
| 594 | if !change { |
| 595 | break |
| 596 | } |
| 597 | if len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.) < 2 { |
| 598 | return failf(1, "pushd: no other directory\n") |
| 599 | } |
| 600 | newtop := swap() |
| 601 | if code := r.changeDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#changeDir().(ctx, "pushd", newtop); code != 0 { |
| 602 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = code |
| 603 | return exit |
| 604 | } |
| 605 | r.builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.Posscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Pos#{}, "dirs", nil) |
| 606 | case 1: |
| 607 | if change { |
| 608 | if code := r.changeDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#changeDir().(ctx, "pushd", args[0]); code != 0 { |
| 609 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = code |
| 610 | return exit |
| 611 | } |
| 612 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack. = append(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack., r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir.) |
| 613 | } else { |
| 614 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack. = append(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack., args[0]) |
| 615 | swap() |
| 616 | } |
| 617 | r.builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.Posscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Pos#{}, "dirs", nil) |
| 618 | default: |
| 619 | return failf(2, "pushd: too many arguments\n") |
| 620 | } |
| 621 | case "popd": |
| 622 | change := true |
| 623 | if len(args) > 0 && args[0] == "-n" { |
| 624 | change = false |
| 625 | args = args[1:] |
| 626 | } |
| 627 | switch len(args) { |
| 628 | case 0: |
| 629 | if len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.) < 2 { |
| 630 | return failf(1, "popd: directory stack empty\n") |
| 631 | } |
| 632 | oldtop := r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] |
| 633 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack. = r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[:len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] |
| 634 | if change { |
| 635 | newtop := r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] |
| 636 | if code := r.changeDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#changeDir().(ctx, "popd", newtop); code != 0 { |
| 637 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = code |
| 638 | return exit |
| 639 | } |
| 640 | } else { |
| 641 | r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.[len(r.dirStackscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#dirStack.)-1] = oldtop |
| 642 | } |
| 643 | r.builtinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#builtin().(ctx, syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.Posscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Pos#{}, "dirs", nil) |
| 644 | default: |
| 645 | return failf(2, "popd: invalid argument\n") |
| 646 | } |
| 647 | case "return": |
| 648 | if !r.inFuncscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inFunc. && !r.inSourcescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#inSource. { |
| 649 | return failf(1, "return: can only be done from a func or sourced script\n") |
| 650 | } |
| 651 | switch len(args) { |
| 652 | case 0: |
| 653 | case 1: |
| 654 | n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(args[0]) |
| 655 | if err != nil { |
| 656 | return failf(2, "invalid return status code: %q\n", args[0]) |
| 657 | } |
| 658 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = uint8(n) |
| 659 | default: |
| 660 | return failf(2, "return: too many arguments\n") |
| 661 | } |
| 662 | exit.returningscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#returning. = true |
| 663 | case "read": |
| 664 | var prompt string |
| 665 | raw := false |
| 666 | silent := false |
| 667 | readArray := false |
| 668 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 669 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 670 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 671 | case "-s": |
| 672 | silent = true |
| 673 | case "-r": |
| 674 | raw = true |
| 675 | case "-a": |
| 676 | readArray = true |
| 677 | case "-p": |
| 678 | prompt = fp.valuescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#value().() |
| 679 | if prompt == "" { |
| 680 | return failf(2, "read: -p: option requires an argument\n") |
| 681 | } |
| 682 | default: |
| 683 | return failf(2, "read: invalid option %q\n", flag) |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 688 | for _, name := range args { |
| 689 | if !syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.ValidNamescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/ValidName().(name) { |
| 690 | return failf(2, "read: invalid identifier %q\n", name) |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if prompt != "" { |
| 695 | r.outscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#out().(prompt) |
| 696 | } |
| 697 | |
| 698 | var line []byte |
| 699 | var err error |
| 700 | if silent { |
| 701 | // Note that on Windows, syscall.Stdin is of type uintptr. |
| 702 | line, err = termscip-go gomod golang.org/x/term v0.41.0 `golang.org/x/term`/.ReadPasswordscip-go gomod golang.org/x/term v0.41.0 `golang.org/x/term`/ReadPassword().(int(syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.Stdinscip-go gomod github.com/golang/go/src go1.25.0 syscall/Stdin.)) |
| 703 | } else { |
| 704 | line, err = r.readLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#readLine().(ctx, raw) |
| 705 | } |
| 706 | if readArray { |
| 707 | // read -a arrayname: split line into fields and assign to indexed array. |
| 708 | arrayName := shellReplyVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/shellReplyVar. |
| 709 | if len(args) > 0 { |
| 710 | arrayName = args[0] |
| 711 | } |
| 712 | // Use -1 as max to get all fields without joining the last ones. |
| 713 | values := expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.ReadFieldsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/ReadFields().(r.ecfgscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#ecfg., string(line), -1, raw) |
| 714 | r.setVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVar().(arrayName, expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Variablescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#{ |
| 715 | Setscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#Set.: true, |
| 716 | Kindscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#Kind.: expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Indexedscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Indexed., |
| 717 | Listscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#List.: values, |
| 718 | }) |
| 719 | } else { |
| 720 | if len(args) == 0 { |
| 721 | args = append(args, shellReplyVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/shellReplyVar.) |
| 722 | } |
| 723 | |
| 724 | values := expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.ReadFieldsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/ReadFields().(r.ecfgscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#ecfg., string(line), len(args), raw) |
| 725 | for i, name := range args { |
| 726 | val := "" |
| 727 | if i < len(values) { |
| 728 | val = values[i] |
| 729 | } |
| 730 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().(name, val) |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // We can get data back from readLine and an error at the same time, so |
| 735 | // check err after we process the data. |
| 736 | if err != nil { |
| 737 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 1 |
| 738 | return exit |
| 739 | } |
| 740 | |
| 741 | case "getopts": |
| 742 | if len(args) < 2 { |
| 743 | return failf(2, "getopts: usage: getopts optstring name [arg ...]\n") |
| 744 | } |
| 745 | optind, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(r.envGetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#envGet().("OPTIND")) |
| 746 | if optind-1 != r.optStatescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optState..argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx. { |
| 747 | if optind < 1 { |
| 748 | optind = 1 |
| 749 | } |
| 750 | r.optStatescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optState. = getoptsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#{argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.: optind - 1} |
| 751 | } |
| 752 | optstr := args[0] |
| 753 | name := args[1] |
| 754 | if !syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.ValidNamescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/ValidName().(name) { |
| 755 | return failf(2, "getopts: invalid identifier: %q\n", name) |
| 756 | } |
| 757 | args = args[2:] |
| 758 | if len(args) == 0 { |
| 759 | args = r.Paramsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Params. |
| 760 | } |
| 761 | diagnostics := !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(optstr, ":") |
| 762 | |
| 763 | opt, optarg, done := r.optStatescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optState..nextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#next().(optstr, args) |
| 764 | |
| 765 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().(name, string(opt)) |
| 766 | r.delVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#delVar().("OPTARG") |
| 767 | switch { |
| 768 | case opt == '?' && diagnostics && !done: |
| 769 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("getopts: illegal option -- %q\n", optarg) |
| 770 | case opt == ':' && diagnostics: |
| 771 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("getopts: option requires an argument -- %q\n", optarg) |
| 772 | default: |
| 773 | if optarg != "" { |
| 774 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().("OPTARG", optarg) |
| 775 | } |
| 776 | } |
| 777 | if optind-1 != r.optStatescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optState..argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx. { |
| 778 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().("OPTIND", strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/FormatInt().(int64(r.optStatescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optState..argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.+1), 10)) |
| 779 | } |
| 780 | |
| 781 | exit.oneIfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#oneIf().(done) |
| 782 | |
| 783 | case "shopt": |
| 784 | mode := "" |
| 785 | posixOpts := false |
| 786 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 787 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 788 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 789 | case "-s", "-u": |
| 790 | mode = flag |
| 791 | case "-o": |
| 792 | posixOpts = true |
| 793 | case "-p", "-q": |
| 794 | return failf(2, "shopt: unsupported option %q\n", flag) |
| 795 | default: |
| 796 | return failf(2, "shopt: invalid option %q\n", flag) |
| 797 | } |
| 798 | } |
| 799 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 800 | if len(args) == 0 { |
| 801 | if posixOpts { |
| 802 | for i, opt := range &posixOptsTablescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/posixOptsTable. { |
| 803 | r.printOptLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#printOptLine().(opt.namescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/posixOpt#name., r.optsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#opts.[i], true) |
| 804 | } |
| 805 | } else { |
| 806 | for i, opt := range bashOptsTablescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bashOptsTable. { |
| 807 | r.printOptLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#printOptLine().(opt.namescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bashOpt#name., r.optsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#opts.[len(posixOptsTablescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/posixOptsTable.)+i], opt.supportedscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/bashOpt#supported.) |
| 808 | } |
| 809 | } |
| 810 | break |
| 811 | } |
| 812 | for _, arg := range args { |
| 813 | opt, supported := (*bool)(nil), true |
| 814 | if posixOpts { |
| 815 | opt = r.posixOptByNamescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#posixOptByName().(arg) |
| 816 | } else { |
| 817 | opt, supported = r.bashOptByNamescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#bashOptByName().(arg) |
| 818 | } |
| 819 | if opt == nil { |
| 820 | return failf(1, "shopt: invalid option name %q\n", arg) |
| 821 | } |
| 822 | |
| 823 | switch mode { |
| 824 | case "-s", "-u": |
| 825 | if !supported { |
| 826 | return failf(1, "shopt: unsupported option %q\n", arg) |
| 827 | } |
| 828 | *opt = mode == "-s" |
| 829 | default: // "" |
| 830 | r.printOptLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#printOptLine().(arg, *opt, supported) |
| 831 | } |
| 832 | } |
| 833 | r.updateExpandOptsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#updateExpandOpts().() |
| 834 | |
| 835 | case "alias": |
| 836 | show := func(name string, als aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#) { |
| 837 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 838 | if len(als.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#args.) > 0 { |
| 839 | printer := syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.NewPrinterscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/NewPrinter().() |
| 840 | printer.Printscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Printer#Print().(&buf, &syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.CallExprscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/CallExpr#{ |
| 841 | Argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/CallExpr#Args.: als.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#args., |
| 842 | }) |
| 843 | } |
| 844 | if als.blankscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#blank. { |
| 845 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteByte().(' ') |
| 846 | } |
| 847 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("alias %s='%s'\n", name, &buf) |
| 848 | } |
| 849 | |
| 850 | if len(args) == 0 { |
| 851 | for name, als := range r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias. { |
| 852 | show(name, als) |
| 853 | } |
| 854 | } |
| 855 | argsLoop: |
| 856 | for _, arg := range args { |
| 857 | name, src, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(arg, "=") |
| 858 | if !ok { |
| 859 | als, ok := r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias.[name] |
| 860 | if !ok { |
| 861 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("alias: %q not found\n", name) |
| 862 | continue |
| 863 | } |
| 864 | show(name, als) |
| 865 | continue |
| 866 | } |
| 867 | |
| 868 | // TODO: parse any CallExpr perhaps, or even any Stmt |
| 869 | parser := syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.NewParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/NewParser().() |
| 870 | var words []*syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.Wordscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Word# |
| 871 | for w, err := range parser.WordsSeqscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/Parser#WordsSeq().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReader().(src)) { |
| 872 | if err != nil { |
| 873 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("alias: could not parse %q: %v\n", src, err) |
| 874 | continue argsLoop |
| 875 | } |
| 876 | words = append(words, w) |
| 877 | } |
| 878 | |
| 879 | if r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias. == nil { |
| 880 | r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias. = make(map[string]aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#) |
| 881 | } |
| 882 | r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias.[name] = aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#{ |
| 883 | argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#args.: words, |
| 884 | blankscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/alias#blank.: stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimRightscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimRight().(src, " \t") != src, |
| 885 | } |
| 886 | } |
| 887 | case "unalias": |
| 888 | for _, name := range args { |
| 889 | delete(r.aliasscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#alias., name) |
| 890 | } |
| 891 | |
| 892 | case "trap": |
| 893 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 894 | callback := "-" |
| 895 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 896 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 897 | case "-l", "-p": |
| 898 | return failf(2, "trap: %q: NOT IMPLEMENTED flag\n", flag) |
| 899 | case "-": |
| 900 | // default signal |
| 901 | default: |
| 902 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("trap: %q: invalid option\n", flag) |
| 903 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("trap: usage: trap [-lp] [[arg] signal_spec ...]\n") |
| 904 | exit.codescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/exitStatus#code. = 2 |
| 905 | return exit |
| 906 | } |
| 907 | } |
| 908 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 909 | switch len(args) { |
| 910 | case 0: |
| 911 | // Print non-default signals |
| 912 | if r.callbackExitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackExit. != "" { |
| 913 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("trap -- %q EXIT\n", r.callbackExitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackExit.) |
| 914 | } |
| 915 | if r.callbackErrscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackErr. != "" { |
| 916 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("trap -- %q ERR\n", r.callbackErrscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackErr.) |
| 917 | } |
| 918 | case 1: |
| 919 | // assume it's a signal, the default will be restored |
| 920 | default: |
| 921 | callback = args[0] |
| 922 | args = args[1:] |
| 923 | } |
| 924 | // For now, treat both empty and - the same since ERR and EXIT have no |
| 925 | // default callback. |
| 926 | if callback == "-" { |
| 927 | callback = "" |
| 928 | } |
| 929 | for _, arg := range args { |
| 930 | switch arg { |
| 931 | case "ERR": |
| 932 | r.callbackErrscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackErr. = callback |
| 933 | case "EXIT": |
| 934 | r.callbackExitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#callbackExit. = callback |
| 935 | default: |
| 936 | return failf(2, "trap: %s: invalid signal specification\n", arg) |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | case "readarray", "mapfile": |
| 941 | dropDelim := false |
| 942 | delim := "\n" |
| 943 | fp := flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#{remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.: args} |
| 944 | for fp.morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() { |
| 945 | switch flag := fp.flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().(); flag { |
| 946 | case "-t": |
| 947 | // Remove the delim from each line read |
| 948 | dropDelim = true |
| 949 | case "-d": |
| 950 | if len(fp.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.) == 0 { |
| 951 | return failf(2, "%s: -d: option requires an argument\n", name) |
| 952 | } |
| 953 | delim = fp.valuescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#value().() |
| 954 | if delim == "" { |
| 955 | // Bash sets the delim to an ASCII NUL if provided with an empty |
| 956 | // string. |
| 957 | delim = "\x00" |
| 958 | } |
| 959 | default: |
| 960 | return failf(2, "%s: invalid option %q\n", name, flag) |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | args := fp.argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() |
| 965 | var arrayName string |
| 966 | switch len(args) { |
| 967 | case 0: |
| 968 | arrayName = "MAPFILE" |
| 969 | case 1: |
| 970 | if !syntaxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/.ValidNamescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/syntax`/ValidName().(args[0]) { |
| 971 | return failf(2, "%s: invalid identifier %q\n", name, args[0]) |
| 972 | } |
| 973 | arrayName = args[0] |
| 974 | default: |
| 975 | return failf(2, "%s: Only one array name may be specified, %v\n", name, args) |
| 976 | } |
| 977 | |
| 978 | var vr expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Variablescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable# |
| 979 | vr.Kindscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#Kind. = expandscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/.Indexedscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Indexed. |
| 980 | scanner := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(r.stdinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stdin.) |
| 981 | scanner.Splitscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Split().(mapfileSplitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/mapfileSplit().(delim[0], dropDelim)) |
| 982 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() { |
| 983 | vr.Listscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#List. = append(vr.Listscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/expand`/Variable#List., scanner.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().()) |
| 984 | } |
| 985 | if err := scanner.Errscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Err().(); err != nil { |
| 986 | return failf(2, "%s: unable to read, %v\n", name, err) |
| 987 | } |
| 988 | r.setVarscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVar().(arrayName, vr) |
| 989 | |
| 990 | default: |
| 991 | return failf(2, "%s: unsupported builtin\n", name) |
| 992 | } |
| 993 | return exit |
| 994 | } |
| 995 | |
| 996 | // mapfileSplit returns a suitable Split function for a [bufio.Scanner]; |
| 997 | // the code is mostly stolen from [bufio.ScanLines]. |
| 998 | func mapfileSplitscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/mapfileSplit().(delim byte, dropDelim bool) bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.SplitFuncscip-go gomod github.com/golang/go/src go1.25.0 bufio/SplitFunc# { |
| 999 | return func(data []byte, atEOF bool) (advance int, token []byte, err error) { |
| 1000 | if atEOF && len(data) == 0 { |
| 1001 | return 0, nil, nil |
| 1002 | } |
| 1003 | if i := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/IndexByte().(data, delim); i >= 0 { |
| 1004 | // We have a full newline-terminated line. |
| 1005 | if dropDelim { |
| 1006 | return i + 1, data[0:i], nil |
| 1007 | } else { |
| 1008 | return i + 1, data[0 : i+1], nil |
| 1009 | } |
| 1010 | } |
| 1011 | // If we're at EOF, we have a final, non-terminated line. Return it. |
| 1012 | if atEOF { |
| 1013 | return len(data), data, nil |
| 1014 | } |
| 1015 | // Request more data. |
| 1016 | return 0, nil, nil |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) printOptLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#printOptLine().(name string, enabled, supported bool) { |
| 1021 | state := r.optStatusTextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optStatusText().(enabled) |
| 1022 | if supported { |
| 1023 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\t%s\n", name, state) |
| 1024 | return |
| 1025 | } |
| 1026 | r.outfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#outf().("%s\t%s\t(%q not supported)\n", name, state, r.optStatusTextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optStatusText().(!enabled)) |
| 1027 | } |
| 1028 | |
| 1029 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) readLinescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#readLine().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, raw bool) ([]byte, error) { |
| 1030 | if r.stdinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stdin. == nil { |
| 1031 | return nil, errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("interp: can't read, there's no stdin") |
| 1032 | } |
| 1033 | |
| 1034 | var line []byte |
| 1035 | esc := false |
| 1036 | |
| 1037 | stopc := make(chan struct{}) |
| 1038 | stop := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 context/AfterFunc().(ctx, func() { |
| 1039 | r.stdinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stdin..SetReadDeadlinescip-go gomod github.com/golang/go/src go1.25.0 os/File#SetReadDeadline().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().()) |
| 1040 | close(stopc) |
| 1041 | }) |
| 1042 | defer func() { |
| 1043 | if !stop() { |
| 1044 | // The AfterFunc was started. |
| 1045 | // Wait for it to complete, and reset the file's deadline. |
| 1046 | <-stopc |
| 1047 | r.stdinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stdin..SetReadDeadlinescip-go gomod github.com/golang/go/src go1.25.0 os/File#SetReadDeadline().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#{}) |
| 1048 | } |
| 1049 | }() |
| 1050 | for { |
| 1051 | var buf [1]byte |
| 1052 | n, err := r.stdinscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stdin..Readscip-go gomod github.com/golang/go/src go1.25.0 os/File#Read().(buf[:]) |
| 1053 | if n > 0 { |
| 1054 | b := buf[0] |
| 1055 | switch { |
| 1056 | case !raw && b == '\\': |
| 1057 | line = append(line, b) |
| 1058 | esc = !esc |
| 1059 | case !raw && b == '\n' && esc: |
| 1060 | // line continuation |
| 1061 | line = line[len(line)-1:] |
| 1062 | esc = false |
| 1063 | case b == '\n': |
| 1064 | return line, nil |
| 1065 | default: |
| 1066 | line = append(line, b) |
| 1067 | esc = false |
| 1068 | } |
| 1069 | } |
| 1070 | if err != nil { |
| 1071 | return line, err |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) changeDirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#changeDir().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, cmd, path string) uint8 { |
| 1077 | if path == "" { |
| 1078 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("%s: empty directory path\n", cmd) |
| 1079 | return 1 |
| 1080 | } |
| 1081 | apath := r.absPathscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#absPath().(path) |
| 1082 | info, err := r.statscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#stat().(ctx, apath) |
| 1083 | if err != nil || !info.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 1084 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("%s: no such file or directory: %q\n", cmd, path) |
| 1085 | return 1 |
| 1086 | } |
| 1087 | if r.accessscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#access().(ctx, apath, access_X_OKscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/access_X_OK.) != nil { |
| 1088 | r.errfscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#errf().("%s: permission denied: %q\n", cmd, path) |
| 1089 | return 1 |
| 1090 | } |
| 1091 | r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir. = apath |
| 1092 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().("OLDPWD", r.envGetscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#envGet().("PWD")) |
| 1093 | r.setVarStringscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#setVarString().("PWD", apath) |
| 1094 | return 0 |
| 1095 | } |
| 1096 | |
| 1097 | func absPathscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/absPath().(dir, path string) string { |
| 1098 | if path == "" { |
| 1099 | return "" |
| 1100 | } |
| 1101 | if !filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/IsAbs().(path) { |
| 1102 | path = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, path) |
| 1103 | } |
| 1104 | return filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(path) // TODO: this clean is likely unnecessary |
| 1105 | } |
| 1106 | |
| 1107 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) absPathscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#absPath().(path string) string { |
| 1108 | return absPathscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/absPath().(r.Dirscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#Dir., path) |
| 1109 | } |
| 1110 | |
| 1111 | // flagParser is used to parse builtin flags. |
| 1112 | // |
| 1113 | // It's similar to the getopts implementation, but with some key differences. |
| 1114 | // First, the API is designed for Go loops, making it easier to use directly. |
| 1115 | // Second, it doesn't require the awkward ":ab" syntax that getopts uses. |
| 1116 | // Third, it supports "-a" flags as well as "+a". |
| 1117 | type flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser# struct { |
| 1118 | currentscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#current. string |
| 1119 | remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. []string |
| 1120 | } |
| 1121 | |
| 1122 | func (p *flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#) morescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#more().() bool { |
| 1123 | if p.currentscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#current. != "" { |
| 1124 | // We're still parsing part of "-ab". |
| 1125 | return true |
| 1126 | } |
| 1127 | if len(p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.) == 0 { |
| 1128 | // Nothing left. |
| 1129 | p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. = nil |
| 1130 | return false |
| 1131 | } |
| 1132 | arg := p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[0] |
| 1133 | if arg == "--" { |
| 1134 | // We explicitly stop parsing flags. |
| 1135 | p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. = p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[1:] |
| 1136 | return false |
| 1137 | } |
| 1138 | if len(arg) == 0 || (arg[0] != '-' && arg[0] != '+') { |
| 1139 | // The next argument is not a flag. |
| 1140 | return false |
| 1141 | } |
| 1142 | // More flags to come. |
| 1143 | return true |
| 1144 | } |
| 1145 | |
| 1146 | func (p *flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#) flagscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#flag().() string { |
| 1147 | arg := p.currentscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#current. |
| 1148 | if arg == "" { |
| 1149 | arg = p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[0] |
| 1150 | p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. = p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[1:] |
| 1151 | } else { |
| 1152 | p.currentscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#current. = "" |
| 1153 | } |
| 1154 | if len(arg) > 2 { |
| 1155 | // We have "-ab", so return "-a" and keep "-b". |
| 1156 | p.currentscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#current. = arg[:1] + arg[2:] |
| 1157 | arg = arg[:2] |
| 1158 | } |
| 1159 | return arg |
| 1160 | } |
| 1161 | |
| 1162 | func (p *flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#) valuescip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#value().() string { |
| 1163 | if len(p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.) == 0 { |
| 1164 | return "" |
| 1165 | } |
| 1166 | arg := p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[0] |
| 1167 | p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. = p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining.[1:] |
| 1168 | return arg |
| 1169 | } |
| 1170 | |
| 1171 | func (p *flagParserscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#) argsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#args().() []string { return p.remainingscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/flagParser#remaining. } |
| 1172 | |
| 1173 | type getoptsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts# struct { |
| 1174 | argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx. int |
| 1175 | runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx. int |
| 1176 | } |
| 1177 | |
| 1178 | func (g *getoptsscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#) nextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#next().(optstr string, args []string) (opt rune, optarg string, done bool) { |
| 1179 | if len(args) == 0 || g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx. >= len(args) { |
| 1180 | return '?', "", true |
| 1181 | } |
| 1182 | arg := []rune(args[g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.]) |
| 1183 | if len(arg) < 2 || arg[0] != '-' || arg[1] == '-' { |
| 1184 | return '?', "", true |
| 1185 | } |
| 1186 | |
| 1187 | opts := arg[1:] |
| 1188 | opt = opts[g.runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx.] |
| 1189 | if g.runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx.+1 < len(opts) { |
| 1190 | g.runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx.++ |
| 1191 | } else { |
| 1192 | g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.++ |
| 1193 | g.runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx. = 0 |
| 1194 | } |
| 1195 | |
| 1196 | i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.25.0 strings/IndexRune().(optstr, opt) |
| 1197 | if i < 0 { |
| 1198 | // invalid option |
| 1199 | return '?', string(opt), false |
| 1200 | } |
| 1201 | |
| 1202 | if i+1 < len(optstr) && optstr[i+1] == ':' { |
| 1203 | if g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx. >= len(args) { |
| 1204 | // missing argument |
| 1205 | return ':', string(opt), false |
| 1206 | } |
| 1207 | optarg = args[g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.] |
| 1208 | g.argidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#argidx.++ |
| 1209 | g.runeidxscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/getopts#runeidx. = 0 |
| 1210 | } |
| 1211 | |
| 1212 | return opt, optarg, false |
| 1213 | } |
| 1214 | |
| 1215 | // optStatusText returns a shell option's status text display |
| 1216 | func (r *Runnerscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#) optStatusTextscip-go gomod mvdan.cc/sh/v3 . `mvdan.cc/sh/v3/interp`/Runner#optStatusText().(status bool) string { |
| 1217 | if status { |
| 1218 | return "on" |
| 1219 | } |
| 1220 | return "off" |
| 1221 | } |