shell

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: GPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DquoteSpecialChars are characters that could interrupt
	// a double-quoted shell string unless escaped.
	DquoteSpecialChars = "$\r\n\t\"`"

	// DquoteNonliteralChars are characters whose escaped form
	// does not not end with their literal value.
	// For example, "\$" has a literal value of "$", so is not included.
	// But "\n" does not have a literal value of "n", so it is included.
	DquoteNonliteralChars = "\r\n\t"

	// SpecialChars are characters that could interrupt a raw shell string
	// unless quoted or escaped.
	SpecialChars = DquoteSpecialChars + `!()[]{}<>|!&*? ;#'"\`
)

Variables

View Source
var DefaultCommands = map[string]CommandFunc{
	"cat":   Cat,
	"echo":  Echo,
	"false": False,
	"true":  True,
}
View Source
var ErrNoMatch = errors.New("no match")

Functions

func BufferReturn

func BufferReturn(
	line parsing.LineInfo,
	rest *[]byte,
) (
	buf *bytes.Buffer,
	errOut func(span int, msg string, args ...any) (string, []byte, error),
	noMatchOut func() (string, []byte, error),
	okOut func() (string, []byte, error),
)

func DquoteString

func DquoteString(value string) string

func ErrOut

func ErrOut[T any](
	line parsing.LineInfo,
	rest *[]byte,
) func(span int, msg string, args ...any) (T, []byte, error)

func FormatAssignment

func FormatAssignment(key, value string) string

func FormatString

func FormatString(value string) string

FormatString returns value if it is not empty and it equals its raw shell string rendering. Otherwise, we escape as needed and wrap it in double-quotes.

func IsAlphaOrUnderscore

func IsAlphaOrUnderscore(c rune) bool

func IsDigit

func IsDigit(c rune) bool

func ParseAssignment

func ParseAssignment(
	line parsing.LineInfo,
	rest []byte,
) (key, value string, newRest []byte, err error)

func ParseIdentifier

func ParseIdentifier(
	line parsing.LineInfo,
	rest []byte,
) (identifier string, newRest []byte, err error)

func ParseShellString

func ParseShellString(
	li parsing.LineInfo,
	rest []byte,
) (value string, newRest []byte, err error)

func ParseShellStringDquote

func ParseShellStringDquote(
	li parsing.LineInfo,
	rest []byte,
) (value string, newRest []byte, err error)

func ParseShellStringRaw

func ParseShellStringRaw(
	li parsing.LineInfo,
	rest []byte,
) (value string, newRest []byte, err error)

func ParseShellStringSquote

func ParseShellStringSquote(
	li parsing.LineInfo,
	rest []byte,
) (value string, newRest []byte, err error)

func TrimSpace

func TrimSpace(s []byte) []byte

Types

type ArgsAndFiles

type ArgsAndFiles struct {
	Args  []string
	Files []InlineFile
}

func ParseWhitespaceThenArgs

func ParseWhitespaceThenArgs(
	li parsing.LineInfo,
	rest []byte,
) (argsAndFiles ArgsAndFiles, newRest []byte, err error)

type Closure

type Closure struct {
	Env     Env
	Command Command
}

func ParseInlineFile

func ParseInlineFile(
	li parsing.LineInfo,
	rest []byte,
) (subProg []Closure, newRest []byte, err error)

type Code

type Code int
const (
	CodeOK              Code = 0
	CodeError           Code = 1
	CodeCannotExecute   Code = 126
	CodeCommandNotFound Code = 127
)

func Cat

func Cat(env Env, args ...string) (code Code)

func Echo

func Echo(env Env, args ...string) Code

func False

func False(env Env, args ...string) Code

func True

func True(env Env, args ...string) Code

type Command

type Command struct {
	Name string

	// Col points to the first char of the command name, not the assignments.
	Col         int
	Vars        Vars
	Args        []string
	InlineFiles []InlineFile
}

func ParseCommand

func ParseCommand(
	li parsing.LineInfo,
	rest []byte,
) (cmd Command, newRest []byte, err error)

func (Command) Run

func (c Command) Run(env Env) (Code, error)

func (Command) String

func (c Command) String() string

type CommandFunc

type CommandFunc func(
	env Env,
	args ...string,
) Code

type Env

type Env struct {
	LineInfo parsing.LineInfo
	Col      int // points to cmd name, not assignments
	Vars     Vars
	Files    fs.FS
	Library  map[string]CommandFunc
	Stdout   io.Writer
	Stderr   io.Writer
}

func (Env) Child

func (e Env) Child(colOffset int) Env

func (Env) LookupCommand

func (e Env) LookupCommand(
	cmd string,
) (CommandFunc, error)

type InlineFile

type InlineFile struct {
	Name    string
	SubProg []Closure
}

func (InlineFile) Run

func (inlineFile InlineFile) Run(
	stdout, stderr io.Writer,
) (Code, error)

Run the subprogram.

func (InlineFile) String

func (f InlineFile) String() string

type OverlayFS

type OverlayFS []fs.FS

func (OverlayFS) Open

func (o OverlayFS) Open(fpath string) (fs.File, error)

type UnknownCommandError

type UnknownCommandError string

func (UnknownCommandError) Error

func (e UnknownCommandError) Error() string

type Vars

type Vars map[string]string

func (Vars) Lines

func (v Vars) Lines() string

func (Vars) PairStrings

func (v Vars) PairStrings() []string

func (Vars) String

func (v Vars) String() string

Jump to

Keyboard shortcuts

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