-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (144 loc) · 3.09 KB
/
main.go
File metadata and controls
166 lines (144 loc) · 3.09 KB
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
package main
import (
"bufio"
_ "embed"
"flag"
"fmt"
"io"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/feather-lang/feather"
)
//go:embed feather-httpd.tcl
var DefaultConfig string
func main() {
scriptFile := flag.String("f", "feather-httpd.tcl", "TCL script file to load")
noRepl := flag.Bool("no-repl", false, "Disable interactive REPL")
flag.Parse()
interp := feather.New()
defer interp.Close()
state := NewServerState()
registerCommands(interp, state)
// Handle SIGINT for graceful shutdown
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigCh
fmt.Println("\nShutting down...")
close(state.shutdown)
if state.server != nil {
state.server.Close()
}
}()
script, err := os.ReadFile(*scriptFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading %s: %v\n", *scriptFile, err)
os.Exit(1)
}
// Eval startup script directly (before interpreter loop starts)
_, err = interp.Eval(string(script))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if *noRepl {
// No REPL - just run the interpreter loop for HTTP requests
state.RunInterpreter(interp)
} else {
// Start interpreter loop in background
go state.RunInterpreter(interp)
// Start telnet REPL server on port 8081
go runTelnetRepl(state)
// Wait for shutdown
<-state.shutdown
}
}
func runTelnetRepl(state *ServerState) {
listener, err := net.Listen("tcp", "127.0.0.1:8081")
if err != nil {
fmt.Fprintf(os.Stderr, "REPL listen error: %v\n", err)
return
}
fmt.Println("REPL listening on 127.0.0.1:8081")
// Close listener on shutdown
go func() {
<-state.shutdown
listener.Close()
}()
for {
conn, err := listener.Accept()
if err != nil {
return // listener closed
}
go func(c net.Conn) {
defer c.Close()
runRepl(state, c, c)
}(conn)
}
}
func runRepl(state *ServerState, r io.Reader, w io.Writer) {
scanner := bufio.NewScanner(r)
fmt.Fprint(w, "feather> ")
var multiline strings.Builder
for scanner.Scan() {
line := scanner.Text()
// Accumulate multiline input
multiline.WriteString(line)
multiline.WriteString("\n")
input := strings.TrimSpace(multiline.String())
if input == "" {
fmt.Fprint(w, "feather> ")
continue
}
// Check for balanced braces (simple heuristic for multiline)
if !isComplete(input) {
fmt.Fprint(w, " > ")
continue
}
result, err := state.EvalWithOutput(input, w)
if err != nil {
fmt.Fprintf(w, "error: %v\n", err)
} else if result.String() != "" {
fmt.Fprintln(w, result.String())
}
multiline.Reset()
fmt.Fprint(w, "feather> ")
}
}
func isComplete(input string) bool {
braces := 0
brackets := 0
inQuote := false
escaped := false
for _, c := range input {
if escaped {
escaped = false
continue
}
if c == '\\' {
escaped = true
continue
}
if c == '"' {
inQuote = !inQuote
continue
}
if inQuote {
continue
}
switch c {
case '{':
braces++
case '}':
braces--
case '[':
brackets++
case ']':
brackets--
}
}
return braces == 0 && brackets == 0 && !inQuote
}