Skip to content

Commit

Permalink
shell_windows: Detect shell under ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Fanjul committed Oct 17, 2024
1 parent 55a8836 commit 3ae3da0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
Binary file added pkg/os/shell/.shell_windows.go.swp
Binary file not shown.
58 changes: 36 additions & 22 deletions pkg/os/shell/shell_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ func getNameAndItsPpid(pid uint32) (exefile string, parentid uint32, err error)
return name, pe.ParentProcessID, nil
}

func analyzeShell(shell string, shellpid uint32) (string, error) {
switch {
case strings.Contains(strings.ToLower(shell), "powershell"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "pwsh"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "cmd"):
return "cmd", nil
default:
shell, _, err := getNameAndItsPpid(shellpid)
if err != nil {
return "cmd", err // defaulting to cmd
}
switch {
case strings.Contains(strings.ToLower(shell), "powershell"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "cmd"):
return "cmd", nil
default:
return "cmd", nil // this could be either powershell or cmd, defaulting to cmd
}
}
}

func detect() (string, error) {
shell := os.Getenv("SHELL")

Expand All @@ -67,32 +91,22 @@ func detect() (string, error) {
if err != nil {
return "cmd", err // defaulting to cmd
}
switch {
case strings.Contains(strings.ToLower(shell), "powershell"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "pwsh"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "cmd"):
return "cmd", nil
default:
shell, _, err := getNameAndItsPpid(shellppid)
if err != nil {
return "cmd", err // defaulting to cmd
}
switch {
case strings.Contains(strings.ToLower(shell), "powershell"):
return "powershell", nil
case strings.Contains(strings.ToLower(shell), "cmd"):
return "cmd", nil
default:
return "cmd", nil // this could be either powershell or cmd, defaulting to cmd
}
}
return analyzeShell(shell, shellppid)
}

if os.Getenv("__fish_bin_dir") != "" {
return "fish", nil
}

return filepath.Base(shell), nil
baseShell := filepath.Base(shell)

if baseShell != "powershell" && baseShell != "cmd" {
pid := os.Getpid()
if pid < 0 || pid > math.MaxUint32 {
return "", fmt.Errorf("integer overflow for pid: %v", pid)
}
return analyzeShell(shell, uint32(pid))
}

return baseShell, nil
}

0 comments on commit 3ae3da0

Please sign in to comment.