2014-01-30 22:09:04 +08:00
|
|
|
// elvish is an experimental Unix shell. It tries to incorporate a powerful
|
2014-02-08 19:26:06 +08:00
|
|
|
// programming language with an extensible, friendly user interface.
|
2013-06-14 17:22:55 +08:00
|
|
|
package main
|
|
|
|
|
2013-06-16 16:08:08 +08:00
|
|
|
import (
|
2014-01-27 18:55:45 +08:00
|
|
|
"fmt"
|
2014-02-10 11:33:53 +08:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
|
2014-01-29 18:37:07 +08:00
|
|
|
"github.com/xiaq/elvish/edit"
|
|
|
|
"github.com/xiaq/elvish/eval"
|
|
|
|
"github.com/xiaq/elvish/parse"
|
|
|
|
"github.com/xiaq/elvish/util"
|
2013-06-16 16:08:08 +08:00
|
|
|
)
|
|
|
|
|
2014-03-03 17:53:23 +08:00
|
|
|
const (
|
|
|
|
sigchSize = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(xiaq): Currently only the editor deals with signals.
|
2013-06-14 17:22:55 +08:00
|
|
|
func main() {
|
2014-02-10 21:35:15 +08:00
|
|
|
ev := eval.NewEvaluator()
|
2014-01-31 19:18:10 +08:00
|
|
|
cmdNum := 0
|
2013-10-17 16:45:26 +08:00
|
|
|
|
2014-01-04 22:45:35 +08:00
|
|
|
username := "???"
|
|
|
|
user, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "???"
|
|
|
|
}
|
2014-03-10 14:50:53 +08:00
|
|
|
rpromptStr := username + "@" + hostname
|
2014-01-04 22:45:35 +08:00
|
|
|
|
2014-03-03 17:53:23 +08:00
|
|
|
sigch := make(chan os.Signal, sigchSize)
|
2014-03-10 21:22:29 +08:00
|
|
|
// signal.Notify(sigch)
|
2014-03-03 17:53:23 +08:00
|
|
|
|
2014-03-10 21:22:29 +08:00
|
|
|
ed := edit.New(os.Stdin, ev, sigch)
|
2014-02-08 21:21:01 +08:00
|
|
|
|
2013-07-29 14:39:21 +08:00
|
|
|
for {
|
2014-01-31 19:18:10 +08:00
|
|
|
cmdNum++
|
|
|
|
name := fmt.Sprintf("<tty %d>", cmdNum)
|
2013-08-15 16:36:29 +08:00
|
|
|
|
2013-09-03 22:22:28 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-09-18 22:58:50 +08:00
|
|
|
|
2014-03-10 14:50:53 +08:00
|
|
|
prompt := func() string {
|
|
|
|
return util.Getwd() + "> "
|
|
|
|
}
|
|
|
|
rprompt := func() string {
|
|
|
|
return rpromptStr
|
|
|
|
}
|
|
|
|
|
2014-01-04 22:45:35 +08:00
|
|
|
lr := ed.ReadLine(prompt, rprompt)
|
2013-09-03 22:22:28 +08:00
|
|
|
|
2014-01-31 19:18:10 +08:00
|
|
|
if lr.EOF {
|
2013-07-29 14:39:21 +08:00
|
|
|
break
|
2013-09-03 22:22:28 +08:00
|
|
|
} else if lr.Err != nil {
|
|
|
|
panic(lr.Err)
|
2013-07-29 14:39:21 +08:00
|
|
|
}
|
2013-09-18 22:58:50 +08:00
|
|
|
|
2014-01-28 12:10:21 +08:00
|
|
|
n, pe := parse.Parse(name, lr.Line)
|
2013-10-17 10:21:35 +08:00
|
|
|
if pe != nil {
|
2013-11-16 19:34:13 +08:00
|
|
|
fmt.Print(pe.(*util.ContextualError).Pprint())
|
2013-08-15 16:36:29 +08:00
|
|
|
continue
|
|
|
|
}
|
2013-09-18 19:46:34 +08:00
|
|
|
|
2014-01-28 12:10:21 +08:00
|
|
|
ee := ev.Eval(name, lr.Line, n)
|
2013-10-17 21:24:44 +08:00
|
|
|
if ee != nil {
|
2014-03-06 16:42:03 +08:00
|
|
|
fmt.Print(ee.(*util.ContextualError).Pprint())
|
2013-10-02 19:25:25 +08:00
|
|
|
continue
|
2013-08-17 20:06:06 +08:00
|
|
|
}
|
2013-07-29 14:39:21 +08:00
|
|
|
}
|
2013-06-14 17:22:55 +08:00
|
|
|
}
|