interp

package
v0.0.0-...-389821e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2018 License: BSD-3-Clause, BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Overview

Package interp implements an interpreter that executes shell programs. It aims to support POSIX, but its support is not complete yet. It also supports some Bash features.

This package is a work in progress and EXPERIMENTAL; its API is not subject to the 1.x backwards compatibility guarantee.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultExec

func DefaultExec(ctx Ctxt, path string, args []string) error

func DefaultOpen

func DefaultOpen(ctx Ctxt, path string, flag int, perm os.FileMode) (io.ReadWriteCloser, error)

Types

type AssocArray

type AssocArray map[string]string

type Ctxt

type Ctxt struct {
	Context     context.Context
	Env         Environ
	Dir         string
	Stdin       io.Reader
	Stdout      io.Writer
	Stderr      io.Writer
	KillTimeout time.Duration
}

Ctxt is the type passed to all the module functions. It contains some of the current state of the Runner, as well as some fields necessary to implement some of the modules.

func (*Ctxt) UnixPath

func (c *Ctxt) UnixPath(path string) string

UnixPath fixes absolute unix paths on Windows, for example converting "C:\\CurDir\\dev\\null" to "/dev/null".

type Environ

type Environ interface {
	Get(name string) (value string, exists bool)
	Set(name, value string)
	Delete(name string)
	Names() []string
	Copy() Environ
}

func EnvFromList

func EnvFromList(list []string) (Environ, error)

type ExitCode

type ExitCode uint8

func (ExitCode) Error

func (e ExitCode) Error() string

type FuncEnviron

type FuncEnviron func(string) string

func (FuncEnviron) Copy

func (f FuncEnviron) Copy() Environ

func (FuncEnviron) Delete

func (f FuncEnviron) Delete(name string)

func (FuncEnviron) Get

func (f FuncEnviron) Get(name string) (string, bool)

func (FuncEnviron) Names

func (f FuncEnviron) Names() []string

func (FuncEnviron) Set

func (f FuncEnviron) Set(name, value string)

type IndexArray

type IndexArray []string

type ModuleExec

type ModuleExec func(ctx Ctxt, path string, args []string) error

ModuleExec is the module responsible for executing a program. It is executed for all CallExpr nodes where the first argument is neither a declared function nor a builtin.

Note that the name is included as the first argument. If path is an empty string, it means that the executable did not exist or was not found in $PATH.

Use a return error of type ExitCode to set the exit code. A nil error has the same effect as ExitCode(0). If the error is of any other type, the interpreter will come to a stop.

type ModuleOpen

type ModuleOpen func(ctx Ctxt, path string, flag int, perm os.FileMode) (io.ReadWriteCloser, error)

ModuleOpen is the module responsible for opening a file. It is executed for all files that are opened directly by the shell, such as in redirects. Files opened by executed programs are not included.

The path parameter is absolute and has been cleaned.

Use a return error of type *os.PathError to have the error printed to stderr and the exit code set to 1. If the error is of any other type, the interpreter will come to a stop.

TODO: What about stat calls? They are used heavily in the builtin test expressions, and also when doing a cd. Should they have a separate module?

func OpenDevImpls

func OpenDevImpls(next ModuleOpen) ModuleOpen

type Runner

type Runner struct {
	// Env specifies the environment of the interpreter.
	// If Env is nil, Run uses the current process's environment.
	Env Environ

	// Dir specifies the working directory of the command. If Dir is
	// the empty string, Run runs the command in the calling
	// process's current directory.
	Dir string

	// Params are the current parameters, e.g. from running a shell
	// file or calling a function. Accessible via the $@/$* family
	// of vars.
	Params []string

	Exec ModuleExec
	Open ModuleOpen

	// Separate maps, note that bash allows a name to be both a var
	// and a func simultaneously
	Vars  map[string]Variable
	Funcs map[string]*syntax.Stmt

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	// Context can be used to cancel the interpreter before it finishes
	Context context.Context

	// KillTimeout holds how much time the interpreter will wait for a
	// program to stop after being sent an interrupt signal, after
	// which a kill signal will be sent. This process will happen when the
	// interpreter's context is cancelled.
	//
	// The zero value will default to 2 seconds.
	//
	// A negative value means that a kill signal will be sent immediately.
	//
	// On Windows, the kill signal is always sent immediately,
	// because Go doesn't currently support sending Interrupt on Windows.
	KillTimeout time.Duration
	// contains filtered or unexported fields
}

A Runner interprets shell programs. It cannot be reused once a program has been interpreted.

Note that writes to Stdout and Stderr may not be sequential. If you plan on using an io.Writer implementation that isn't safe for concurrent use, consider a workaround like hiding writes behind a mutex.

func (*Runner) Fields

func (r *Runner) Fields(words ...*syntax.Word) []string

func (*Runner) FromArgs

func (r *Runner) FromArgs(args ...string) ([]string, error)

FromArgs populates the shell options and returns the remaining arguments. For example, running FromArgs("-e", "--", "foo") will set the "-e" option and return []string{"foo"}.

This is similar to what the interpreter's "set" builtin does.

func (*Runner) Reset

func (r *Runner) Reset() error

Reset will set the unexported fields back to zero, fill any exported fields with their default values if not set, and prepare the runner to interpret a program.

This function should be called once before running any node. It can be skipped before any following runs to keep internal state, such as declared variables.

func (*Runner) Run

func (r *Runner) Run(node syntax.Node) error

Run starts the interpreter and returns any error.

func (*Runner) Stmt

func (r *Runner) Stmt(stmt *syntax.Stmt) error

type StringVal

type StringVal string

type VarValue

type VarValue interface{}

VarValue is one of:

StringVal
IndexArray
AssocArray

type Variable

type Variable struct {
	Local    bool
	Exported bool
	ReadOnly bool
	NameRef  bool
	Value    VarValue
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL