github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/elvish/eval/builtin_fn_cmd.go (about)

     1  package eval
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  )
     8  
     9  // Command and process control.
    10  
    11  var ErrNotInSameGroup = errors.New("not in the same process group")
    12  
    13  func init() {
    14  	addBuiltinFns(map[string]interface{}{
    15  		// Command resolution
    16  		"external":        external,
    17  		"has-external":    hasExternal,
    18  		"search-external": searchExternal,
    19  
    20  		// Process control
    21  		"fg":   fg,
    22  		"exec": execFn,
    23  		"exit": exit,
    24  	})
    25  }
    26  
    27  func external(cmd string) ExternalCmd {
    28  	return ExternalCmd{cmd}
    29  }
    30  
    31  func hasExternal(cmd string) bool {
    32  	_, err := exec.LookPath(cmd)
    33  	return err == nil
    34  }
    35  
    36  func searchExternal(cmd string) (string, error) {
    37  	return exec.LookPath(cmd)
    38  }
    39  
    40  func exit(fm *Frame, codes ...int) error {
    41  	code := 0
    42  	switch len(codes) {
    43  	case 0:
    44  	case 1:
    45  		code = codes[0]
    46  	default:
    47  		return ErrArgs
    48  	}
    49  
    50  	preExit(fm)
    51  	os.Exit(code)
    52  	// Does not return
    53  	panic("os.Exit returned")
    54  }
    55  
    56  func preExit(fm *Frame) {
    57  }
    58  
    59  var errNotSupportedOnWindows = errors.New("not supported on Windows")
    60  
    61  func notSupportedOnWindows() error {
    62  	return errNotSupportedOnWindows
    63  }