Initial working shell: bash-for-windows
- Cross-compiled Go-based shell for Windows (PE32+ executable) - Builtins: cd, pwd, echo, exit, export, source, alias, type - Coreutils: ls, cat, grep, sort, wc, head, find, cp, mv, rm, mkdir, touch, clear - Command chaining: &&, ||, ; - Pipes between builtins and external commands - Variable expansion (, ) and assignment (NAME=VALUE) - Tokenizer with single/double quote handling - Linux and Windows (x86_64) builds via build.sh
This commit is contained in:
66
cmd/bash/main.go
Normal file
66
cmd/bash/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package bash
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"github.com/cametendo/bash-for-windows/internal/shell"
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
args := os.Args[1:]
|
||||
|
||||
if len(args) > 0 {
|
||||
if args[0] == "-c" && len(args) > 1 {
|
||||
// Execute a command string
|
||||
return runCommand(strings.Join(args[1:], " "))
|
||||
}
|
||||
// Run a script file
|
||||
return runFile(args[0])
|
||||
}
|
||||
|
||||
return interactive()
|
||||
}
|
||||
|
||||
func runCommand(cmd string) error {
|
||||
sh := shell.New()
|
||||
return sh.Execute(cmd)
|
||||
}
|
||||
|
||||
func runFile(path string) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", path, err)
|
||||
}
|
||||
sh := shell.New()
|
||||
return sh.Execute(string(data))
|
||||
}
|
||||
|
||||
func interactive() error {
|
||||
sh := shell.New()
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("bash-for-windows v1.0.0")
|
||||
|
||||
for {
|
||||
fmt.Print("bash$ ")
|
||||
input, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
input = strings.TrimSpace(input)
|
||||
if input == "" {
|
||||
continue
|
||||
}
|
||||
if input == "exit" {
|
||||
break
|
||||
}
|
||||
|
||||
if err := sh.Execute(input); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "bash: %v\n", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user