github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/pogosh/state.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package pogosh 6 7 import "os" 8 9 // TODO: rename file to types.go ? 10 11 // Cmd is a builtin command to run. 12 type Cmd struct { 13 os.ProcAttr 14 name string 15 argv []string 16 } 17 18 // Builtin is the function used to run a builtin. 19 type Builtin = func(*State, *Cmd) 20 21 // State holds data on the current interpreter execution. 22 type State struct { 23 IsInteractive bool 24 25 Builtins map[string]func(*State, *Cmd) 26 Aliases map[string]string 27 variables map[string]Var 28 29 // Special variables 30 varExitStatus int // $? 31 32 Overrides Overrides 33 34 parent *State 35 } 36 37 // Var holds information on shell variable. 38 type Var struct { 39 Value string 40 } 41 42 // Overrides change the behaviour of builtins. 43 type Overrides struct { 44 Chdir func(dir string) error 45 Environ func() []string 46 Exit func(code int) 47 } 48 49 // DefaultState creates a new default state. 50 func DefaultState() State { 51 return State{ 52 Builtins: DefaultBuiltins(), 53 Aliases: map[string]string{}, 54 55 Overrides: DefaultOverrides(), 56 } 57 } 58 59 // DefaultOverrides creates a new default overrides. 60 func DefaultOverrides() Overrides { 61 return Overrides{ 62 Chdir: os.Chdir, 63 Environ: os.Environ, 64 Exit: func(code int) { panic(exitError{code}) }, 65 } 66 } 67 68 // Errors propagated using panic 69 type exitError struct{ code int }