//
//
// gsh - Go lang based Shell
// (c) 2020 ITS more Co., Ltd.
// 2020-0807 created by SatoxITS (ITS more)
//
package main
import (
"bufio" // https://golang.org/pkg/bufio/
"strings"
"fmt"
//"strconv"
"os"
"syscall"
"go/types"
"go/token"
)
var LINESIZE = 1024
var PROMPT = "> "
func env(argv []string) {
env := os.Environ()
for _, v := range env {
fmt.Printf("%v\n",v)
}
}
func which(path string) (fullpath string, itis bool){
pathenv, found := os.LookupEnv("PATH")
if found {
dirv := strings.Split(pathenv,":")
for _, dir := range dirv {
fi, err := os.Stat(dir+"/"+path)
if err == nil {
fm := fi.Mode()
if fm.IsRegular() {
//fmt.Printf("%s\n",dir+"/"+path)
return dir+"/"+path, true
}
}
}
}
return "", false
}
func eval(argv []string, nlend bool){
var ai = 1
pfmt := "%s"
if argv[ai][0:1] == "%" {
pfmt = argv[ai]
ai = 2
}
if len(argv) <= ai {
return
}
gocode := strings.Join(argv[ai:]," ");
fset := token.NewFileSet()
rval, _ := types.Eval(fset,nil,token.NoPos,gocode)
fmt.Printf(pfmt,rval.Value)
if nlend { fmt.Printf("\n") }
}
func main() {
for hi := 0; ; hi++ {
fmt.Printf("%d",hi)
fmt.Print(PROMPT)
reader := bufio.NewReaderSize(os.Stdin,LINESIZE);
line, _, _ := reader.ReadLine()
argv := strings.Split(string(line)," ");
cmd := argv[0]
if cmd == "exit" || cmd == "quit" {
break
}
if cmd == "env" {
env(argv)
continue;
}
if cmd == "eval" {
eval(argv,true)
continue;
}
if cmd == "which" {
which(argv[1]);
continue;
}
fullpath, itis := which(cmd)
if itis {
syscall.Exec(fullpath,argv,os.Environ())
}
}
}
//---END---
//