跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

parse.go

212 行 · 5.4 KB历史下载
1// Package env reads and writes agent-vm's env files, ~/.agent-vm/env and a
2// project's .agent-vm.env, which the VM's shell sources: without ever running
3// a shell on the host, since the VM can write the project's.
4package env
5
6import (
7 "regexp"
8 "strings"
9)
10
11// SQEscape makes s safe inside a single-quoted shell literal: ' becomes
12// '"'"'.
13func SQEscape(s string) string {
14 return strings.ReplaceAll(s, "'", `'"'"'`)
15}
16
17// Status of a read.
18const (
19 Found = 0
20 NotFound = 1
21 // Refused: the file uses syntax this reader does not interpret, and no
22 // key can be answered.
23 Refused = 3
24)
25
26// records splits content as awk reads lines: a last line without a newline
27// is one too.
28func records(content string) []string {
29 if content == "" {
30 return nil
31 }
32 return strings.Split(strings.TrimSuffix(content, "\n"), "\n")
33}
34
35var (
36 leadingBlank = regexp.MustCompile(`^[ \t]+`)
37 exportPrefix = regexp.MustCompile(`^export[ \t]+`)
38 assignment = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*=`)
39 blankOrNote = regexp.MustCompile(`^(#.*)?$`) // CRs already dropped (Lookup)
40 restOK = regexp.MustCompile(`^[ \t]+(#.*)?$`)
41)
42
43// Lookup is the value content assigns to key, as a shell sourcing it would,
44// without a shell. Accepted: an optional `export `, then single-quoted parts
45// (which may span lines, as Set writes a value with a newline), double-quoted
46// parts with nothing to expand, and bare characters other than
47// $ ` \ ; & | < > ( ) ~, then an optional `# comment`. The last assignment
48// wins. One line refused, and no key is answered: a quote left open or a
49// trailing backslash makes the shell read the next lines into that value,
50// and other syntax can run anything, an assignment to any key included. A
51// line that is not an assignment, a comment or blank counts as refused.
52func Lookup(content, key string) (string, int) {
53 // As the VM sources it: the CR ending each line dropped (StripCR),
54 // inside a quoted value too.
55 lines := records(StripCR(content))
56 found := false
57 val := ""
58 for i := 0; i < len(lines); i++ {
59 line := leadingBlank.ReplaceAllString(lines[i], "")
60 if blankOrNote.MatchString(line) {
61 continue
62 }
63 line = exportPrefix.ReplaceAllString(line, "")
64 if !assignment.MatchString(line) {
65 return "", Refused
66 }
67 eq := strings.IndexByte(line, '=')
68 v, st, used := parseValue(line[eq+1:], lines[i+1:])
69 i += used
70 if st != Found {
71 return "", Refused
72 }
73 if line[:eq] == key {
74 found, val = true, v
75 }
76 }
77 if !found {
78 return "", NotFound
79 }
80 return val, Found
81}
82
83// parseValue reads the value s starts, taking from more the lines a
84// single-quoted part goes on to; used is how many.
85func parseValue(s string, more []string) (v string, st, used int) {
86 for s != "" {
87 switch c := s[0]; {
88 case c == '\'':
89 s = s[1:]
90 for {
91 e := strings.IndexByte(s, '\'')
92 if e >= 0 {
93 v += s[:e]
94 s = s[e+1:]
95 break
96 }
97 if used == len(more) {
98 return v, Refused, used
99 }
100 v += s + "\n"
101 s = more[used]
102 used++
103 }
104 case c == '"':
105 s = s[1:]
106 e := strings.IndexByte(s, '"')
107 if e < 0 || strings.ContainsAny(s[:e], "$`\\") {
108 return v, Refused, used
109 }
110 v += s[:e]
111 s = s[e+1:]
112 case c == ' ' || c == '\t':
113 if restOK.MatchString(s) {
114 return v, Found, used
115 }
116 return v, Refused, used
117 case strings.IndexByte("$`\\;&|<>()~\r", c) >= 0:
118 return v, Refused, used
119 default:
120 v += s[:1] // the byte, not string(c), which would encode it as a rune
121 s = s[1:]
122 }
123 }
124 return v, Found, used
125}
126
127var nameAssign = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*=`)
128
129// Names is every name content assigns, once per assignment, in order.
130func Names(content string) []string {
131 var names []string
132 walk(content, func(_ string, name string) { names = append(names, name) }, nil)
133 return names
134}
135
136// Drop is content without key's assignments, whole: a value Set wrote over
137// several lines goes with its first line. Each line keeps its newline.
138func Drop(content, key string) string {
139 var b strings.Builder
140 walk(content, nil, func(line, name string) {
141 if name != key {
142 b.WriteString(line + "\n")
143 }
144 })
145 return b.String()
146}
147
148// walk calls name for each assignment, and line for each line with the name
149// of the assignment it belongs to ("" for none). A line inside a quoted value
150// is never read as an assignment: it is data.
151func walk(content string, name func(line, name string), line func(line, name string)) {
152 q := byte(0) // the open quote: ', ", or $ for $'...'
153 cur := ""
154 scan := func(s string) {
155 for i := 0; i < len(s); i++ {
156 c := s[i]
157 switch q {
158 case '\'':
159 if c == '\'' {
160 q = 0
161 }
162 continue
163 case '$':
164 if c == '\\' {
165 i++
166 } else if c == '\'' {
167 q = 0
168 }
169 continue
170 case '"':
171 if c == '\\' {
172 i++
173 } else if c == '"' {
174 q = 0
175 }
176 continue
177 }
178 switch {
179 case c == '\\':
180 i++
181 case c == '$' && i+1 < len(s) && s[i+1] == '\'':
182 q = '$'
183 i++
184 case c == '\'' || c == '"':
185 q = c
186 case c == '#' && i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'):
187 return
188 }
189 }
190 }
191 for _, l := range records(content) {
192 if q != 0 {
193 scan(l)
194 if line != nil {
195 line(l, cur)
196 }
197 continue
198 }
199 cur = ""
200 t := exportPrefix.ReplaceAllString(leadingBlank.ReplaceAllString(l, ""), "")
201 if m := nameAssign.FindString(t); m != "" {
202 cur = m[:len(m)-1]
203 if name != nil {
204 name(l, cur)
205 }
206 scan(t[len(m):])
207 }
208 if line != nil {
209 line(l, cur)
210 }
211 }
212}