mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38: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
155 lines
4.1 KiB
Go
155 lines
4.1 KiB
Go
// Copyright 2023, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/base"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/server"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/shexec"
|
|
)
|
|
|
|
var BuildTime = "0"
|
|
|
|
func readFullRunPacket(packetParser *packet.PacketParser) (*packet.RunPacketType, error) {
|
|
rpb := packet.MakeRunPacketBuilder()
|
|
for pk := range packetParser.MainCh {
|
|
ok, runPacket := rpb.ProcessPacket(pk)
|
|
if runPacket != nil {
|
|
return runPacket, nil
|
|
}
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid packet '%s' sent to mshell", pk.GetType())
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no run packet received")
|
|
}
|
|
|
|
func handleSingle() {
|
|
packetParser := packet.MakePacketParser(os.Stdin, nil)
|
|
sender := packet.MakePacketSender(os.Stdout, nil)
|
|
defer func() {
|
|
sender.Close()
|
|
sender.WaitForDone()
|
|
}()
|
|
initPacket := shexec.MakeInitPacket()
|
|
sender.SendPacket(initPacket)
|
|
if len(os.Args) >= 3 && os.Args[2] == "--version" {
|
|
return
|
|
}
|
|
runPacket, err := readFullRunPacket(packetParser)
|
|
if err != nil {
|
|
sender.SendErrorResponse(runPacket.ReqId, err)
|
|
return
|
|
}
|
|
err = shexec.ValidateRunPacket(runPacket)
|
|
if err != nil {
|
|
sender.SendErrorResponse(runPacket.ReqId, err)
|
|
return
|
|
}
|
|
err = runPacket.CK.Validate("run packet")
|
|
if err != nil {
|
|
sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("run packets from server must have a CK: %v", err))
|
|
}
|
|
if runPacket.Detached {
|
|
cmd, startPk, err := shexec.RunCommandDetached(runPacket, sender)
|
|
if err != nil {
|
|
sender.SendErrorResponse(runPacket.ReqId, err)
|
|
return
|
|
}
|
|
sender.SendPacket(startPk)
|
|
sender.Close()
|
|
sender.WaitForDone()
|
|
cmd.DetachedWait(startPk)
|
|
return
|
|
} else {
|
|
shexec.IgnoreSigPipe()
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
go func() {
|
|
for range ticker.C {
|
|
// this will let the command detect when the server has gone away
|
|
// that will then trigger cmd.SendHup() to send SIGHUP to the exec'ed process
|
|
sender.SendPacket(packet.MakePingPacket())
|
|
}
|
|
}()
|
|
defer ticker.Stop()
|
|
cmd, err := shexec.RunCommandSimple(runPacket, sender, true)
|
|
if err != nil {
|
|
sender.SendErrorResponse(runPacket.ReqId, fmt.Errorf("error running command: %w", err))
|
|
return
|
|
}
|
|
defer cmd.Close()
|
|
startPacket := cmd.MakeCmdStartPacket(runPacket.ReqId)
|
|
sender.SendPacket(startPacket)
|
|
go func() {
|
|
exitErr := sender.WaitForDone()
|
|
if exitErr != nil {
|
|
base.Logf("I/O error talking to server, sending SIGHUP to children\n")
|
|
cmd.SendSignal(syscall.SIGHUP)
|
|
}
|
|
}()
|
|
cmd.RunRemoteIOAndWait(packetParser, sender)
|
|
return
|
|
}
|
|
}
|
|
|
|
func handleUsage() {
|
|
usage := `
|
|
mshell is a helper program for wave terminal. it is used to execute commands
|
|
|
|
Options:
|
|
--help - prints this message
|
|
--version - print version
|
|
--server - multiplexer to run multiple commands
|
|
--single - run a single command (connected to multiplexer)
|
|
--single --version - return an init packet with version info
|
|
|
|
mshell does not open any external ports and does not require any additional permissions.
|
|
it communicates exclusively through stdin/stdout with an attached process
|
|
via a JSON packet format.
|
|
`
|
|
fmt.Printf("%s\n\n", strings.TrimSpace(usage))
|
|
}
|
|
|
|
func main() {
|
|
base.SetBuildTime(BuildTime)
|
|
if len(os.Args) == 1 {
|
|
handleUsage()
|
|
return
|
|
}
|
|
firstArg := os.Args[1]
|
|
if firstArg == "--help" {
|
|
handleUsage()
|
|
return
|
|
} else if firstArg == "--version" {
|
|
fmt.Printf("mshell %s+%s\n", base.MShellVersion, base.BuildTime)
|
|
return
|
|
} else if firstArg == "--single" || firstArg == "--single-from-server" {
|
|
base.ProcessType = base.ProcessType_WaveShellSingle
|
|
base.InitDebugLog("single")
|
|
handleSingle()
|
|
return
|
|
} else if firstArg == "--server" {
|
|
base.ProcessType = base.ProcessType_WaveShellServer
|
|
base.InitDebugLog("server")
|
|
rtnCode, err := server.RunServer()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[error] %v\n", err)
|
|
}
|
|
if rtnCode != 0 {
|
|
os.Exit(rtnCode)
|
|
}
|
|
return
|
|
} else {
|
|
handleUsage()
|
|
return
|
|
}
|
|
}
|