-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_cmd.go
More file actions
126 lines (101 loc) · 2.76 KB
/
lua_cmd.go
File metadata and controls
126 lines (101 loc) · 2.76 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
package main
import (
"fmt"
"io"
"os"
"regexp"
"strings"
)
type LuaRunFileCmd struct{}
func (cmd LuaRunFileCmd) Name() string {
return ".lua-eval-file"
}
func (cmd LuaRunFileCmd) Description() string {
return "Execute a Lua file"
}
func (cmd LuaRunFileCmd) Usage() string {
return ".lua-eval-file <filename> <arg> <arg> <arg>..."
}
func (cmd LuaRunFileCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
filename := args[0]
scriptContent, err := FetchLuaScriptContent(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read Lua script: %v\n", err)
os.Exit(1)
}
return ExecuteLuaScript(string(scriptContent), args[1:], os.Stdout)
}
type LuaCmd struct{}
func (cmd LuaCmd) Name() string {
return ".lua-eval"
}
func (cmd LuaCmd) Description() string {
return "Execute a Lua script"
}
func (cmd LuaCmd) Usage() string {
return ".lua-eval \"<script>\""
}
// ParseLuaScriptAndArgs parses the raw input to extract the Lua script and arguments
func (cmd LuaCmd) parseLuaScriptAndArgs(rawInput string) (string, []string, error) {
// Find the script part (everything between the first pair of quotes)
re := regexp.MustCompile(`\.lua-eval\s+"((?:[^"\\]|\\.)*)"`)
matches := re.FindStringSubmatch(rawInput)
if len(matches) < 2 {
return "", nil, fmt.Errorf("invalid script format: script must be enclosed in quotes")
}
// Get the script content
script := matches[1]
script = strings.Replace(script, `\"`, `"`, -1)
// Get the position after the script
scriptEndPos := strings.Index(rawInput, matches[0]) + len(matches[0])
argsPart := strings.TrimSpace(rawInput[scriptEndPos:])
// Parse arguments properly handling quotes
var parsedArgs []string
var currentArg strings.Builder
var inQuotes bool
var escapeNext bool
for i := 0; i < len(argsPart); i++ {
char := argsPart[i]
if escapeNext {
currentArg.WriteByte(char)
escapeNext = false
continue
}
if char == '\\' {
escapeNext = true
continue
}
if char == '"' {
if inQuotes {
inQuotes = false
} else {
inQuotes = true
}
continue
}
if char == ' ' && !inQuotes {
if currentArg.Len() > 0 {
parsedArgs = append(parsedArgs, currentArg.String())
currentArg.Reset()
}
continue
}
currentArg.WriteByte(char)
}
if currentArg.Len() > 0 {
parsedArgs = append(parsedArgs, currentArg.String())
}
return script, parsedArgs, nil
}
func (cmd LuaCmd) Handle(args []string, rawInput string, resultWriter io.Writer) error {
if len(args) < 1 {
return fmt.Errorf("usage: .lua-eval \"<script>\" <args> <args> <args> ...")
}
// Parse the script and arguments
script, parsedArgs, err := cmd.parseLuaScriptAndArgs(rawInput)
if err != nil {
return err
}
// Execute the Lua script
return ExecuteLuaScript(script, parsedArgs, resultWriter)
}