| 1 |
// Copyright (c) 2017, Daniel Martí <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package interp |
| 5 |
|
| 6 |
import ( |
| 7 |
"maps" |
| 8 |
"path" |
| 9 |
"slices" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
// The `help` builtin, following bash: `help` lists every builtin's synopsis in |
| 14 |
// two columns, `help NAME` prints the synopsis and description of one, and |
| 15 |
// NAME may be a glob (`help ex*`). -d, -s and -m select short, synopsis-only |
| 16 |
// and man-page-ish output. |
| 17 |
// |
| 18 |
// A builtin the interpreter recognizes but does not implement is marked with a |
| 19 |
// star, the way bash marks disabled builtins — so `help` doubles as an honest |
| 20 |
// statement of what this shell can do. |
| 21 |
|
| 22 |
// builtinHelp is the synopsis and description of one builtin. |
| 23 |
type builtinHelp struct { |
| 24 |
synopsis string |
| 25 |
desc string |
| 26 |
} |
| 27 |
|
| 28 |
// helpTable is keyed by builtin name. Synopses follow bash 5's wording so the |
| 29 |
// output is familiar; descriptions are ours, and say where this shell differs. |
| 30 |
var helpTable = map[string]builtinHelp{ |
| 31 |
":": {": ", "Null command; expands arguments and returns success."}, |
| 32 |
".": {". filename [arguments]", "Execute commands from a file in the current shell."}, |
| 33 |
"[": {"[ arg... ]", "Evaluate a conditional expression; a synonym for `test' requiring a closing `]'."}, |
| 34 |
"alias": {"alias [-p] [name[=value] ... ]", "Define or display aliases. With no arguments, print the alias list."}, |
| 35 |
"bg": {"bg [job_spec ...]", "Resume jobs in the background."}, |
| 36 |
"bind": {"bind [-lpsvPSVX] [-m keymap] [keyseq:readline-function]", "Set readline key bindings and variables."}, |
| 37 |
"break": {"break [n]", "Exit the enclosing for, while or until loop; with n, exit n levels."}, |
| 38 |
"builtin": {"builtin [shell-builtin [arg ...]]", "Run a shell builtin, bypassing a function of the same name."}, |
| 39 |
"caller": {"caller [expr]", "Return the context of the current subroutine call."}, |
| 40 |
"cd": {"cd [-L|[-P [-e]]] [dir]", "Change the shell working directory."}, |
| 41 |
"command": {"command [-pVv] command [arg ...]", "Run a command, bypassing shell function lookup."}, |
| 42 |
"compgen": {"compgen [-abcdefgjksuv] [-o option] [word]", "Display possible completions for a word."}, |
| 43 |
"complete": {"complete [-abcdefgjksuv] [-pr] [-o option] [name ...]", |
| 44 |
"Specify how arguments are to be completed by readline."}, |
| 45 |
"compopt": {"compopt [-o|+o option] [name ...]", "Modify or display completion options."}, |
| 46 |
"continue": {"continue [n]", "Resume the next iteration of the enclosing loop; with n, of the nth enclosing loop."}, |
| 47 |
"declare": {"declare [-aAfFgiIlnrtux] [name[=value] ...]", |
| 48 |
"Set variable values and attributes, or display them."}, |
| 49 |
"dirs": {"dirs [-clpv] [+N] [-N]", "Display the list of currently remembered directories."}, |
| 50 |
"disown": {"disown [-h] [-ar] [jobspec ...]", "Remove jobs from the current shell's job table."}, |
| 51 |
"echo": {"echo [-neE] [arg ...]", "Write arguments to standard output, separated by spaces."}, |
| 52 |
"enable": {"enable [-a] [-dnps] [name ...]", "Enable and disable shell builtins."}, |
| 53 |
"eval": {"eval [arg ...]", "Concatenate arguments into a single command and execute it."}, |
| 54 |
"exec": {"exec [-cl] [-a name] [command [argument ...]]", "Replace the shell with the given command."}, |
| 55 |
"exit": {"exit [n]", "Exit the shell with status n; without n, the status of the last command."}, |
| 56 |
"export": {"export [-fn] [name[=value] ...] or export -p", "Mark names for automatic export to the environment."}, |
| 57 |
"false": {"false", "Return an unsuccessful result."}, |
| 58 |
"fc": {"fc [-e ename] [-lnr] [first] [last]", "Display or re-execute commands from the history list."}, |
| 59 |
"fg": {"fg [job_spec]", "Move a job to the foreground."}, |
| 60 |
"for": {"for NAME [in WORDS ... ] ; do COMMANDS; done", "Execute commands for each member in a list."}, |
| 61 |
"getopts": {"getopts optstring name [arg ...]", |
| 62 |
"Parse option arguments, one at a time, as a shell script would."}, |
| 63 |
"hash": {"hash [-lr] [-p pathname] [-dt] [name ...]", "Remember or display program locations."}, |
| 64 |
"help": {"help [-dms] [pattern ...]", "Display information about builtin commands."}, |
| 65 |
"history": {"history [-c] [n]", "Display the history list."}, |
| 66 |
"jobs": {"jobs [-lnprs] [jobspec ...]", "Display status of jobs."}, |
| 67 |
"kill": {"kill [-s sigspec | -n signum] pid | jobspec ...", "Send a signal to a job or process."}, |
| 68 |
"let": {"let arg [arg ...]", "Evaluate arithmetic expressions."}, |
| 69 |
"local": {"local [option] name[=value] ...", "Define local variables inside a shell function."}, |
| 70 |
"logout": {"logout [n]", "Exit a login shell."}, |
| 71 |
"mapfile": {"mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [array]", |
| 72 |
"Read lines from the standard input into an indexed array variable."}, |
| 73 |
"newgrp": {"newgrp [group]", "Change the current group ID."}, |
| 74 |
"popd": {"popd [-n] [+N | -N]", "Remove directories from the stack."}, |
| 75 |
"printf": {"printf [-v var] format [arguments]", "Format and print arguments under control of the format."}, |
| 76 |
"pushd": {"pushd [-n] [+N | -N | dir]", "Add directories to the stack and change to the top."}, |
| 77 |
"pwd": {"pwd [-LP]", "Print the name of the current working directory."}, |
| 78 |
"read": {"read [-Eers] [-a array] [-d delim] [-n nchars] [-p prompt] [-t timeout] [name ...]", "Read a line from the standard input and split it into fields."}, |
| 79 |
"readarray": {"readarray [-d delim] [-n count] [-O origin] [-s count] [-t] [array]", |
| 80 |
"Read lines from a file into an indexed array variable; a synonym for `mapfile'."}, |
| 81 |
"readonly": {"readonly [-aAf] [name[=value] ...] or readonly -p", "Mark names as unchangeable."}, |
| 82 |
"return": {"return [n]", "Return from a shell function with status n."}, |
| 83 |
"set": {"set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [arg ...]", "Set or unset shell options and positional parameters."}, |
| 84 |
"shift": {"shift [n]", "Shift positional parameters left by n (default 1)."}, |
| 85 |
"shopt": {"shopt [-pqsu] [-o] [optname ...]", "Set and unset shell options."}, |
| 86 |
"source": {"source filename [arguments]", "Execute commands from a file in the current shell; a synonym for `.'."}, |
| 87 |
"suspend": {"suspend [-f]", "Suspend shell execution."}, |
| 88 |
"test": {"test [expr]", "Evaluate a conditional expression and return 0 or 1."}, |
| 89 |
"times": {"times", "Display process times for the shell and its children."}, |
| 90 |
"trap": {"trap [-lp] [[action] signal_spec ...]", "Trap signals and other events."}, |
| 91 |
"true": {"true", "Return a successful result."}, |
| 92 |
"type": {"type [-afptP] name [name ...]", "Display information about the type of each name."}, |
| 93 |
"typeset": {"typeset [-aAfFgiIlnrtux] name[=value] ...", |
| 94 |
"Set variable values and attributes; a synonym for `declare'."}, |
| 95 |
"ulimit": {"ulimit [-SHabcdefiklmnpqrstuvxPRT] [limit]", "Modify or display shell resource limits."}, |
| 96 |
"umask": {"umask [-p] [-S] [mode]", "Display or set the file mode creation mask."}, |
| 97 |
"unalias": {"unalias [-a] name [name ...]", "Remove each name from the list of aliases."}, |
| 98 |
"unset": {"unset [-f] [-v] [-n] [name ...]", "Unset values and attributes of shell variables and functions."}, |
| 99 |
"wait": {"wait [-fn] [id ...]", "Wait for job completion and return exit status."}, |
| 100 |
} |
| 101 |
|
| 102 |
// helpUnsupported are recognized by this shell but not implemented; `help` |
| 103 |
// stars them the way bash stars disabled builtins. |
| 104 |
var helpUnsupported = map[string]bool{ |
| 105 |
"bg": true, "bind": true, "caller": true, "compgen": true, "complete": true, |
| 106 |
"compopt": true, "disown": true, "enable": true, "fc": true, "fg": true, |
| 107 |
"history": true, "jobs": true, "kill": true, "logout": true, "newgrp": true, |
| 108 |
"suspend": true, "ulimit": true, |
| 109 |
} |
| 110 |
|
| 111 |
// helpMatch returns the documented names matching a bash-style pattern. An |
| 112 |
// exact name wins; otherwise the pattern is matched as a glob, and as a |
| 113 |
// prefix, like bash's `help ex*` and `help ex`. |
| 114 |
func helpMatch(pattern string) []string { |
| 115 |
if _, ok := helpTable[pattern]; ok { |
| 116 |
return []string{pattern} |
| 117 |
} |
| 118 |
var out []string |
| 119 |
for _, name := range slices.Sorted(maps.Keys(helpTable)) { |
| 120 |
if ok, err := path.Match(pattern, name); err == nil && ok { |
| 121 |
out = append(out, name) |
| 122 |
continue |
| 123 |
} |
| 124 |
if strings.HasPrefix(name, pattern) { |
| 125 |
out = append(out, name) |
| 126 |
} |
| 127 |
} |
| 128 |
return out |
| 129 |
} |
| 130 |
|
| 131 |
// helpLine is the two-column list entry for a builtin. |
| 132 |
func helpLine(name string) string { |
| 133 |
star := " " |
| 134 |
if helpUnsupported[name] { |
| 135 |
star = "*" |
| 136 |
} |
| 137 |
return star + helpTable[name].synopsis |
| 138 |
} |
| 139 |
|
| 140 |
// runHelp implements the help builtin. It writes through the runner so it |
| 141 |
// honours redirections like every other builtin. |
| 142 |
func (r *Runner) runHelp(args []string) exitStatus { |
| 143 |
var short, synopsisOnly, manStyle bool |
| 144 |
rest := args |
| 145 |
for len(rest) > 0 && strings.HasPrefix(rest[0], "-") && rest[0] != "-" { |
| 146 |
switch rest[0] { |
| 147 |
case "-d": |
| 148 |
short = true |
| 149 |
case "-s": |
| 150 |
synopsisOnly = true |
| 151 |
case "-m": |
| 152 |
manStyle = true |
| 153 |
case "--": |
| 154 |
rest = rest[1:] |
| 155 |
goto patterns |
| 156 |
default: |
| 157 |
r.errf("help: %s: invalid option\n", rest[0]) |
| 158 |
r.errf("help: usage: %s\n", helpTable["help"].synopsis) |
| 159 |
return exitStatus{code: 2} |
| 160 |
} |
| 161 |
rest = rest[1:] |
| 162 |
} |
| 163 |
patterns: |
| 164 |
|
| 165 |
if len(rest) == 0 { |
| 166 |
r.out("A shell built on mvdan.cc/sh/v3. These shell commands are defined internally.\n") |
| 167 |
r.out("Type `help' to see this list.\n") |
| 168 |
r.out("Type `help name' to find out more about the function `name'.\n\n") |
| 169 |
r.out("A star (*) next to a name means that the command is not implemented.\n\n") |
| 170 |
|
| 171 |
names := slices.Sorted(maps.Keys(helpTable)) |
| 172 |
// two columns, like bash: first half down the left, second half right |
| 173 |
half := (len(names) + 1) / 2 |
| 174 |
for i := 0; i < half; i++ { |
| 175 |
left := helpLine(names[i]) |
| 176 |
if i+half < len(names) { |
| 177 |
r.outf("%-78s%s\n", left, helpLine(names[i+half])) |
| 178 |
} else { |
| 179 |
r.outf("%s\n", left) |
| 180 |
} |
| 181 |
} |
| 182 |
return exitStatus{} |
| 183 |
} |
| 184 |
|
| 185 |
exit := exitStatus{} |
| 186 |
for _, pattern := range rest { |
| 187 |
matches := helpMatch(pattern) |
| 188 |
if len(matches) == 0 { |
| 189 |
r.errf("help: no help topics match `%s'. Try `help help'.\n", pattern) |
| 190 |
exit = exitStatus{code: 1} |
| 191 |
continue |
| 192 |
} |
| 193 |
for _, name := range matches { |
| 194 |
h := helpTable[name] |
| 195 |
switch { |
| 196 |
case synopsisOnly: |
| 197 |
r.outf("%s: %s\n", name, h.synopsis) |
| 198 |
case short: |
| 199 |
r.outf("%s - %s\n", name, h.desc) |
| 200 |
case manStyle: |
| 201 |
r.outf("NAME\n %s - %s\n\nSYNOPSIS\n %s\n\nDESCRIPTION\n %s\n\n", |
| 202 |
name, h.desc, h.synopsis, h.desc) |
| 203 |
default: |
| 204 |
r.outf("%s: %s\n", name, h.synopsis) |
| 205 |
r.outf(" %s\n", h.desc) |
| 206 |
if helpUnsupported[name] { |
| 207 |
r.out(" (recognized by this shell but not implemented)\n") |
| 208 |
} |
| 209 |
r.out("\n") |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
return exit |
| 214 |
} |