mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
422338c04b
adds zsh support to waveterm. big change, lots going on here. lots of other improvements and bug fixes added while debugging and building out the feature. Commits: * refactor shexec parser.go into new package shellenv. separate out bash specific parsing from generic functions * checkpoint * work on refactoring shexec. created two new packages shellapi (for bash/zsh specific stuff), and shellutil (shared between shellapi and shexec) * more refactoring * create shellapi interface to abstract bash specific functionality * more refactoring, move bash shell state parsing to shellapi * move makeRcFile to shellapi. remove all of the 'client' options CLI options from waveshell * get shellType passed through to server/single paths for waveshell * add a local shelltype detector * mock out a zshapi * move shelltype through more of the code * get a command to run via zsh * zsh can now switch directories. poc, needs cleanup * working on ShellState encoding differences between zsh/bash. Working on parsing zsh decls. move utilfn package into waveshell (shouldn't have been in wavesrv) * switch to use []byte for vardecl serialization + diffs * progress on zsh environment. still have issues reconciling init environment with trap environment * fix typeset argument parsing * parse promptvars, more zsh specific ignores * fix bug with promptvar not getting set (wrong check in FeState func) * add sdk (issue #188) to list of rtnstate commands * more zsh compatibility -- working with a larger ohmyzsh environment. ignore more variables, handle exit trap better. unique path/fpath. add a processtype variable to base. * must return a value * zsh alias parsing/restoring. diff changes (and rtnstate changes). introduces linediff v1. * force zmodload of zsh/parameter * starting work on zsh functions * need a v1 of mapdiff as well (to handle null chars) * pack/unpack of ints was wrong (one used int and one use uint). turned out we only ever encoded '0' so it worked. that also means it is safe to change unpack to unpackUInt * reworking for binary encoding of aliases and functions (because of zsh allows any character, including nulls, in names and values) * fixes, working on functions, issue with line endings * zsh functions. lots of ugliness here around dealing with line dicipline and cooked stty. new runcommand function to grab output from a non-tty fd. note that we still to run the actual command in a stty to get the proper output. * write uuid tempdir, cleanup with tmprcfilename code * hack in some simple zsh function declaration finding code for rtnstate. create function diff for rtnstate that supports zsh * make sure key order is constant so shell hashes are consistent * fix problems with state diffs to support new zsh formats. add diff/apply code to shellapi (moved from shellenv), that is now specific to zsh or bash * add log packet and new shellstate packets * switch to shellstate map that's also keyed by shelltype * add shelltype to remoteinstance * remove shell argument from waveshell * added new shelltype statemap to remote.go (msh), deal with fallout * move shellstate out of init packet, and move to an explicit reinit call. try to initialize all of the active shell states * change dont always store init state (only store on demand). initialize shell states on demand (if not already initialized). allow reset to change shells * add shellpref field to remote table. use to drive the default shell choice for new tabs * show shelltag on cmdinput, pass through ri and remote (defaultshellstate) * bump mshell version to v0.4 * better version validation for shellstate. also relax compatibility requirements for diffing states (shelltype + major version need to match) * better error handling, check shellstate compatibility during run (on waveshell server) * add extra separator for bash shellstate processing to deal with spurious output from rc files * special migration for v30 -- flag invalid bash shell states and show special button in UI to fix * format * remove zsh-decls (unused) * remove test code * remove debug print * fix typo
261 lines
7.8 KiB
Go
261 lines
7.8 KiB
Go
// Copyright 2023, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package shellapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/alessio/shellescape"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/shellenv"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/statediff"
|
|
)
|
|
|
|
const BaseBashOpts = `set +m; set +H; shopt -s extglob`
|
|
|
|
const BashShellVersionCmdStr = `echo bash v${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}`
|
|
const RemoteBashPath = "bash"
|
|
|
|
// TODO fix bash path in these constants
|
|
const RunBashSudoCommandFmt = `sudo -n -C %d bash /dev/fd/%d`
|
|
const RunBashSudoPasswordCommandFmt = `cat /dev/fd/%d | sudo -k -S -C %d bash -c "echo '[from-mshell]'; exec %d>&-; bash /dev/fd/%d < /dev/fd/%d"`
|
|
|
|
// do not use these directly, call GetLocalMajorVersion()
|
|
var localBashMajorVersionOnce = &sync.Once{}
|
|
var localBashMajorVersion = ""
|
|
|
|
// the "exec 2>" line also adds an extra printf at the *beginning* to strip out spurious rc file output
|
|
var GetBashShellStateCmds = []string{
|
|
"exec 2> /dev/null;",
|
|
BashShellVersionCmdStr + ";",
|
|
`pwd;`,
|
|
`declare -p $(compgen -A variable);`,
|
|
`alias -p;`,
|
|
`declare -f;`,
|
|
GetGitBranchCmdStr + ";",
|
|
}
|
|
|
|
type bashShellApi struct{}
|
|
|
|
func (b bashShellApi) GetShellType() string {
|
|
return packet.ShellType_bash
|
|
}
|
|
|
|
func (b bashShellApi) MakeExitTrap(fdNum int) string {
|
|
return MakeBashExitTrap(fdNum)
|
|
}
|
|
|
|
func (b bashShellApi) GetLocalMajorVersion() string {
|
|
return GetLocalBashMajorVersion()
|
|
}
|
|
|
|
func (b bashShellApi) GetLocalShellPath() string {
|
|
return GetLocalBashPath()
|
|
}
|
|
|
|
func (b bashShellApi) GetRemoteShellPath() string {
|
|
return RemoteBashPath
|
|
}
|
|
|
|
func (b bashShellApi) MakeRunCommand(cmdStr string, opts RunCommandOpts) string {
|
|
if !opts.Sudo {
|
|
return fmt.Sprintf(RunCommandFmt, cmdStr)
|
|
}
|
|
if opts.SudoWithPass {
|
|
return fmt.Sprintf(RunBashSudoPasswordCommandFmt, opts.PwFdNum, opts.MaxFdNum+1, opts.PwFdNum, opts.CommandFdNum, opts.CommandStdinFdNum)
|
|
} else {
|
|
return fmt.Sprintf(RunBashSudoCommandFmt, opts.MaxFdNum+1, opts.CommandFdNum)
|
|
}
|
|
}
|
|
|
|
func (b bashShellApi) MakeShExecCommand(cmdStr string, rcFileName string, usePty bool) *exec.Cmd {
|
|
return MakeBashShExecCommand(cmdStr, rcFileName, usePty)
|
|
}
|
|
|
|
func (b bashShellApi) GetShellState() (*packet.ShellState, error) {
|
|
return GetBashShellState()
|
|
}
|
|
|
|
func (b bashShellApi) GetBaseShellOpts() string {
|
|
return BaseBashOpts
|
|
}
|
|
|
|
func (b bashShellApi) ParseShellStateOutput(output []byte) (*packet.ShellState, error) {
|
|
return parseBashShellStateOutput(output)
|
|
}
|
|
|
|
func (b bashShellApi) MakeRcFileStr(pk *packet.RunPacketType) string {
|
|
var rcBuf bytes.Buffer
|
|
rcBuf.WriteString(b.GetBaseShellOpts() + "\n")
|
|
varDecls := shellenv.VarDeclsFromState(pk.State)
|
|
for _, varDecl := range varDecls {
|
|
if varDecl.IsExport() || varDecl.IsReadOnly() {
|
|
continue
|
|
}
|
|
rcBuf.WriteString(BashDeclareStmt(varDecl))
|
|
rcBuf.WriteString("\n")
|
|
}
|
|
if pk.State != nil && pk.State.Funcs != "" {
|
|
rcBuf.WriteString(pk.State.Funcs)
|
|
rcBuf.WriteString("\n")
|
|
}
|
|
if pk.State != nil && pk.State.Aliases != "" {
|
|
rcBuf.WriteString(pk.State.Aliases)
|
|
rcBuf.WriteString("\n")
|
|
}
|
|
return rcBuf.String()
|
|
}
|
|
|
|
func GetBashShellStateCmd() string {
|
|
return strings.Join(GetBashShellStateCmds, ` printf "\x00\x00";`)
|
|
}
|
|
|
|
func execGetLocalBashShellVersion() string {
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), GetStateTimeout)
|
|
defer cancelFn()
|
|
ecmd := exec.CommandContext(ctx, "bash", "-c", BashShellVersionCmdStr)
|
|
out, err := ecmd.Output()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
versionStr := strings.TrimSpace(string(out))
|
|
if strings.Index(versionStr, "bash ") == -1 {
|
|
// invalid shell version (only bash is supported)
|
|
return ""
|
|
}
|
|
return versionStr
|
|
}
|
|
|
|
func GetLocalBashMajorVersion() string {
|
|
localBashMajorVersionOnce.Do(func() {
|
|
fullVersion := execGetLocalBashShellVersion()
|
|
localBashMajorVersion = packet.GetMajorVersion(fullVersion)
|
|
})
|
|
return localBashMajorVersion
|
|
}
|
|
|
|
func GetBashShellState() (*packet.ShellState, error) {
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), GetStateTimeout)
|
|
defer cancelFn()
|
|
cmdStr := BaseBashOpts + "; " + GetBashShellStateCmd()
|
|
ecmd := exec.CommandContext(ctx, GetLocalBashPath(), "-l", "-i", "-c", cmdStr)
|
|
outputBytes, err := RunSimpleCmdInPty(ecmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return parseBashShellStateOutput(outputBytes)
|
|
}
|
|
|
|
func GetLocalBashPath() string {
|
|
if runtime.GOOS == "darwin" {
|
|
macShell := GetMacUserShell()
|
|
if strings.Index(macShell, "bash") != -1 {
|
|
return shellescape.Quote(macShell)
|
|
}
|
|
}
|
|
return "bash"
|
|
}
|
|
|
|
func GetLocalZshPath() string {
|
|
if runtime.GOOS == "darwin" {
|
|
macShell := GetMacUserShell()
|
|
if strings.Index(macShell, "zsh") != -1 {
|
|
return shellescape.Quote(macShell)
|
|
}
|
|
}
|
|
return "zsh"
|
|
}
|
|
|
|
func GetBashShellStateRedirectCommandStr(outputFdNum int) string {
|
|
return fmt.Sprintf("cat <(%s) > /dev/fd/%d", GetBashShellStateCmd(), outputFdNum)
|
|
}
|
|
|
|
func MakeBashExitTrap(fdNum int) string {
|
|
stateCmd := GetBashShellStateRedirectCommandStr(fdNum)
|
|
fmtStr := `
|
|
_waveshell_exittrap () {
|
|
%s
|
|
}
|
|
trap _waveshell_exittrap EXIT
|
|
`
|
|
return fmt.Sprintf(fmtStr, stateCmd)
|
|
}
|
|
|
|
func MakeBashShExecCommand(cmdStr string, rcFileName string, usePty bool) *exec.Cmd {
|
|
if usePty {
|
|
return exec.Command(GetLocalBashPath(), "--rcfile", rcFileName, "-i", "-c", cmdStr)
|
|
} else {
|
|
return exec.Command(GetLocalBashPath(), "--rcfile", rcFileName, "-c", cmdStr)
|
|
}
|
|
}
|
|
|
|
func (bashShellApi) MakeShellStateDiff(oldState *packet.ShellState, oldStateHash string, newState *packet.ShellState) (*packet.ShellStateDiff, error) {
|
|
if oldState == nil {
|
|
return nil, fmt.Errorf("cannot diff, oldState is nil")
|
|
}
|
|
if newState == nil {
|
|
return nil, fmt.Errorf("cannot diff, newState is nil")
|
|
}
|
|
if !packet.StateVersionsCompatible(oldState.Version, newState.Version) {
|
|
return nil, fmt.Errorf("cannot diff, incompatible shell versions: %q %q", oldState.Version, newState.Version)
|
|
}
|
|
rtn := &packet.ShellStateDiff{}
|
|
rtn.BaseHash = oldStateHash
|
|
rtn.Version = newState.Version // always set version in the diff
|
|
if oldState.Cwd != newState.Cwd {
|
|
rtn.Cwd = newState.Cwd
|
|
}
|
|
rtn.Error = newState.Error
|
|
oldVars := shellenv.ShellStateVarsToMap(oldState.ShellVars)
|
|
newVars := shellenv.ShellStateVarsToMap(newState.ShellVars)
|
|
rtn.VarsDiff = statediff.MakeMapDiff(oldVars, newVars)
|
|
rtn.AliasesDiff = statediff.MakeLineDiff(oldState.Aliases, newState.Aliases, oldState.GetLineDiffSplitString())
|
|
rtn.FuncsDiff = statediff.MakeLineDiff(oldState.Funcs, newState.Funcs, oldState.GetLineDiffSplitString())
|
|
return rtn, nil
|
|
}
|
|
|
|
func (bashShellApi) ApplyShellStateDiff(oldState *packet.ShellState, diff *packet.ShellStateDiff) (*packet.ShellState, error) {
|
|
if oldState == nil {
|
|
return nil, fmt.Errorf("cannot apply diff, oldState is nil")
|
|
}
|
|
if diff == nil {
|
|
return oldState, nil
|
|
}
|
|
rtnState := &packet.ShellState{}
|
|
var err error
|
|
rtnState.Version = oldState.Version
|
|
// work around a bug (before v0.6.0) where version could be invalid.
|
|
// so only overwrite the oldversion if diff version is valid
|
|
_, _, diffVersionErr := packet.ParseShellStateVersion(diff.Version)
|
|
if diffVersionErr == nil {
|
|
rtnState.Version = diff.Version
|
|
}
|
|
rtnState.Cwd = oldState.Cwd
|
|
if diff.Cwd != "" {
|
|
rtnState.Cwd = diff.Cwd
|
|
}
|
|
rtnState.Error = diff.Error
|
|
oldVars := shellenv.ShellStateVarsToMap(oldState.ShellVars)
|
|
newVars, err := statediff.ApplyMapDiff(oldVars, diff.VarsDiff)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("applying mapdiff 'vars': %v", err)
|
|
}
|
|
rtnState.ShellVars = shellenv.StrMapToShellStateVars(newVars)
|
|
rtnState.Aliases, err = statediff.ApplyLineDiff(oldState.Aliases, diff.AliasesDiff)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("applying diff 'aliases': %v", err)
|
|
}
|
|
rtnState.Funcs, err = statediff.ApplyLineDiff(oldState.Funcs, diff.FuncsDiff)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("applying diff 'funcs': %v", err)
|
|
}
|
|
return rtnState, nil
|
|
}
|