-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathng.go
221 lines (196 loc) · 4.68 KB
/
ng.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2016 The Neugram Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"time"
"neugram.io/ng/eval/shell"
"neugram.io/ng/gengo"
"neugram.io/ng/jupyter"
"neugram.io/ng/ngcore"
"neugram.io/ng/parser"
)
var (
sigint = make(chan os.Signal)
ng = ngcore.New()
)
func exit(code int) {
ng.Close()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(code)
}
func exitf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "ng: "+format+"\n", args...)
exit(1)
}
const usageLine = "ng [programfile | -e cmd | -jupyter file] [arguments]"
func usage() {
fmt.Fprintf(os.Stderr, `ng - neugram scripting language and shell
Usage:
%s
Options:
`, usageLine)
flag.PrintDefaults()
}
func main() {
shell.Init()
flagJupyter := flag.String("jupyter", "", "path to jupyter kernel connection file")
flagShell := flag.Bool("shell", false, "start in shell mode")
flagHelp := flag.Bool("h", false, "display help message and exit")
flagE := flag.String("e", "", "program passed as a string")
flagO := flag.String("o", "", "compile the program to the named file")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageLine)
os.Exit(1)
}
flag.Parse()
if *flagHelp {
usage()
os.Exit(0)
}
if *flagJupyter != "" {
err := jupyter.Run(context.Background(), *flagJupyter)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
os.Exit(0)
}
if *flagE != "" {
ng, err := ng.NewSession(context.Background(), filepath.Join(cwd, "ng-arg"), os.Environ())
if err != nil {
exitf("%v", err)
}
defer ng.Close()
initSession(ng)
vals, err := ng.Exec([]byte(*flagE))
if err != nil {
exitf("%v", err)
}
ng.Display(ng.Stdout, vals)
return
}
if args := flag.Args(); len(args) > 0 {
// TODO: plumb through the rest of the args
path := args[0]
if *flagO != "" {
res, err := gengo.GenGo(path, "main")
if err != nil {
exitf("%v", err)
}
fmt.Printf("%s\n", res)
exitf("TODO gengo")
return
}
ng, err := ng.NewSession(context.Background(), path, os.Environ())
if err != nil {
exitf("%v", err)
}
defer ng.Close()
initSession(ng)
f, err := os.Open(path)
if err != nil {
exitf("%v", err)
}
defer f.Close()
state, err := ng.RunScript(f)
if err != nil {
exitf("%v", err)
}
if state == parser.StateCmd {
exitf("%s: ends in an unclosed shell statement", args[0])
}
return
}
if *flagO != "" {
exitf("-o specified but no program file provided")
}
loop(context.Background(), os.Args[0] == "ngsh" || os.Args[0] == "-ngsh" || *flagShell)
}
func setWindowSize(env map[interface{}]interface{}) {
// TODO windowsize
// TODO
// TODO
// TODO
/*
rows, cols, err := job.WindowSize(os.Stderr.Fd())
if err != nil {
fmt.Printf("ng: could not get window size: %v\n", err)
} else {
// TODO: these are meant to be shell variables, not
// environment variables. But then, how do programs
// like `ls` read them?
env["LINES"] = strconv.Itoa(rows)
env["COLUMNS"] = strconv.Itoa(cols)
}
*/
}
var cwd string
func init() {
var err error
cwd, err = os.Getwd()
if err != nil {
panic(err)
}
}
func initSession(ng *ngcore.Session) {
ng.Stdin = os.Stdin
ng.Stdout = os.Stdout
ng.Stderr = os.Stderr
// TODO this env setup could be done in neugram code
env := ng.Program.Environ()
wd, err := os.Getwd()
if err == nil {
env.Set("PWD", wd)
}
//setWindowSize(env)
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
for {
s := <-sig
select {
case sigint <- s:
case <-time.After(500 * time.Millisecond):
// The evaluator has not handled the signal
// promptly. There are several possible
// reasons for this. The most likely right now
// is the evaluator is in arbitrary Go code,
// which does not have a way to be preempted.
// It is also possible we have run into a
// bug in the evaluator.
//
// Either way, instead of being one of those
// obnoxious programs that refuses to respond
// to Ctrl-C, be overly agressive and let the
// entire ng process exit.
//
// This is bad if you use ng as your primary
// shell, but good if you invoke ng to handle
// scripts.
fmt.Fprintf(os.Stderr, "ng: exiting on interrupt\n")
exit(1)
}
}
}()
}
func loop(ctx context.Context, startInShell bool) {
path := filepath.Join(cwd, "ng-interactive")
ng, err := ng.NewSession(ctx, path, os.Environ())
if err != nil {
exitf("%v", err)
}
defer ng.Close()
initSession(ng)
err = ng.Run(ctx, startInShell, sigint)
if err != nil {
exitf("%v", err)
}
exit(0)
}