-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (106 loc) · 2.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"github.com/mattn/anko/vm"
"github.com/mattn/go-isatty"
"github.com/zetamatta/nyagos/defined"
"github.com/zetamatta/nyagos/frame"
"github.com/zetamatta/nyagos/functions"
"github.com/zetamatta/nyagos/history"
"github.com/zetamatta/nyagos/nodos"
"github.com/zetamatta/nyagos/shell"
"github.com/zetamatta/nyagos/alias"
)
type ankoFunc struct {
f vm.Func
}
func (this *ankoFunc) Call(ctx context.Context, cmd *shell.Cmd) (next int, err error) {
args := cmd.Args()
_, err = this.f(reflect.ValueOf(args[1:]))
return 0, err
}
func (this *ankoFunc) String() string {
return "ankoFunc"
}
func ankoAlias(name string, f interface{}) {
switch code := f.(type) {
case string:
alias.Table[name] = alias.New(code)
case vm.Func:
alias.Table[name] = &ankoFunc{f: code}
default:
println(reflect.TypeOf(f).String())
}
}
func loadrc(anko *vm.Env) error {
execPath, err := os.Executable()
if err != nil {
return err
}
dir := filepath.Dir(execPath)
script := filepath.Join(dir, "nyagos.ank")
code, err := ioutil.ReadFile(script)
if err != nil {
return nil
}
_, err = anko.Execute(string(code))
if err != nil {
return fmt.Errorf("%s: %s", script, err)
}
return nil
}
func _main() error {
sh := shell.New()
defer sh.Close()
sh.Console = nodos.GetConsole()
ctx := context.Background()
if !isatty.IsTerminal(os.Stdin.Fd()) {
frame.SilentMode = true
}
anko := vm.NewEnv()
anko.Define("println", fmt.Println)
anko.Define("alias", ankoAlias)
if err := loadrc(anko); err != nil {
fmt.Fprintln(os.Stderr, err)
}
var stream1 shell.Stream
if isatty.IsTerminal(os.Stdin.Fd()) {
constream := frame.NewCmdStreamConsole(
func() (int, error) {
functions.Prompt(
&functions.Param{
Args: []interface{}{frame.Format2Prompt(os.Getenv("PROMPT"))},
Out: os.Stdout,
Err: os.Stderr,
In: os.Stdin,
Term: nodos.GetConsole(),
},
)
return 0, nil
})
stream1 = constream
frame.DefaultHistory = constream.History
ctx = context.WithValue(ctx, history.PackageId, constream.History)
} else {
stream1 = shell.NewCmdStreamFile(os.Stdin)
}
sh.ForEver(ctx, stream1)
return nil
}
var version string
func main() {
frame.Version = version
if err := frame.Start(_main); err != nil && err != io.EOF {
fmt.Fprintln(os.Stderr, err)
defer os.Exit(1)
}
if defined.DBG {
os.Stdin.Read(make([]byte, 1))
}
}