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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// generated by hxc 1.0.0-acgt-compiler from src/tripwire.hlx -- do not edit by hand
#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
// hlx:5
pub fn words(command: &str) -> Vec<String> {
// hlx:6
let mut s = String::with_capacity(command.len() + 8);
// hlx:7
let mut chars = command.chars().peekable();
// hlx:8
while let Some(c) = chars.next() {
// hlx:9
match c {
// hlx:10
'"' | '\'' | '`' => {}
// hlx:11
';' | '|' | '&' | '(' | ')' => s.push(' '),
// hlx:12
'>' => {
// hlx:13
if chars.peek() == Some(&'>') {
// hlx:14
chars.next();
// hlx:15
s.push_str(" >> ");
// hlx:16
} else {
// hlx:17
s.push_str(" > ");
// hlx:18
}
// hlx:19
}
// hlx:20
c => s.push(c),
// hlx:21
}
// hlx:22
}
// hlx:23
s.split_whitespace().map(str::to_owned).collect()
// hlx:24
}
// hlx:27
fn runner(w: &[String]) -> Option<String> {
// hlx:28
let mut i = 0;
// hlx:29
while i < w.len()
&& (matches!(
w[i].as_str(),
"sudo"
| "doas"
| "env"
| "time"
| "nohup"
| "nice"
| "command"
| "exec"
| "builtin"
| "xargs"
) || w[i].starts_with('-')
|| w[i].contains('='))
{
// hlx:30
if matches!(
w[i].as_str(),
"-u" | "-g" | "-I" | "-n" | "-P" | "-d" | "-E" | "-L"
) {
// hlx:31
i += 1;
// hlx:32
}
// hlx:33
i += 1;
// hlx:34
}
// hlx:35
let name = w.get(i)?.rsplit('/').next()?.to_lowercase();
// hlx:36
if name == "git" {
// hlx:37
let sub = w.iter().skip(i + 1).find(|x| !x.starts_with('-'))?;
// hlx:38
return Some(["git ", sub.as_str()].concat());
// hlx:39
}
// hlx:40
Some(name)
// hlx:41
}
// hlx:44
fn read_only(w: &[String]) -> bool {
// hlx:45
let Some(r) = runner(w) else { return false };
// hlx:46
matches!(
r.as_str(),
"grep"
| "egrep"
| "fgrep"
| "zgrep"
| "rg"
| "ag"
| "ack"
| "echo"
| "printf"
| "man"
| "cat"
| "bat"
| "less"
| "more"
| "head"
| "tail"
| "wc"
| "ls"
| "stat"
| "file"
| "which"
| "type"
| "whereis"
| "whatis"
| "apropos"
| "git log"
| "git grep"
| "git show"
| "git diff"
| "git blame"
| "git status"
)
// hlx:47
}
// hlx:50
fn runs_input(w: &[String]) -> bool {
// hlx:51
let Some(r) = runner(w) else { return false };
// hlx:52
matches!(
r.as_str(),
"sh" | "bash"
| "zsh"
| "dash"
| "ksh"
| "fish"
| "psql"
| "mysql"
| "mariadb"
| "sqlite3"
| "sqlcmd"
| "redis-cli"
| "mongosh"
| "pwsh"
| "powershell"
| "cmd"
| "python"
| "python3"
| "node"
| "perl"
| "ruby"
)
// hlx:53
}
// hlx:56
pub fn command_words(command: &str) -> Vec<String> {
// hlx:57
let mut segs: Vec<(String, String, bool)> = Vec::new();
// hlx:58
let mut raw = String::new();
// hlx:59
let mut bare = String::new();
// hlx:60
let mut quote = ' ';
// hlx:61
let mut prev = ' ';
// hlx:62
let mut it = command.chars();
// hlx:63
while let Some(c) = it.next() {
// hlx:64
if quote == ' ' && matches!(c, ';' | '|' | '&' | '(' | ')' | '\n') {
// hlx:65
segs.push((
raw.clone(),
bare.clone(),
c == '|' && prev != '|' && it.as_str().chars().next() != Some('|'),
));
// hlx:66
raw.clear();
// hlx:67
bare.clear();
// hlx:68
prev = c;
// hlx:69
continue;
// hlx:70
}
// hlx:71
prev = c;
// hlx:72
if c == '\\' && quote != '\'' {
// hlx:73
raw.push(c);
// hlx:74
bare.push(c);
// hlx:75
if let Some(n) = it.next() {
// hlx:76
raw.push(n);
// hlx:77
bare.push(n);
// hlx:78
}
// hlx:79
continue;
// hlx:80
}
// hlx:81
if quote != ' ' {
// hlx:82
raw.push(c);
// hlx:83
bare.push(if c == '>' { '_' } else { c });
// hlx:84
if c == quote {
// hlx:85
quote = ' ';
// hlx:86
}
// hlx:87
continue;
// hlx:88
}
// hlx:89
if c == '"' || c == '\'' || c == '`' {
// hlx:90
quote = c;
// hlx:91
}
// hlx:92
raw.push(c);
// hlx:93
bare.push(c);
// hlx:94
}
// hlx:95
segs.push((raw, bare, false));
// hlx:96
let mut out: Vec<String> = Vec::new();
// hlx:97
for i in 0..segs.len() {
// hlx:98
let w = words(&segs[i].0);
// hlx:99
if w.is_empty() {
// hlx:100
continue;
// hlx:101
}
// hlx:102
if read_only(&w)
&& !(segs[i].2 && segs.get(i + 1).is_some_and(|s| runs_input(&words(&s.0))))
{
// hlx:103
let b = words(&segs[i].1);
// hlx:104
for j in 0..b.len() {
// hlx:105
if b[j] == ">" || b[j] == ">>" {
// hlx:106
out.push(b[j].clone());
// hlx:107
if let Some(t) = b.get(j + 1) {
// hlx:108
out.push(t.clone());
// hlx:109
}
// hlx:110
}
// hlx:111
}
// hlx:112
continue;
// hlx:113
}
// hlx:114
out.extend(w);
// hlx:115
}
// hlx:116
out
// hlx:117
}
// hlx:120
fn alt_hits(pat: &str, word: &str) -> bool {
// hlx:121
let w = word.trim_end_matches(['/', '\\']).to_lowercase();
// hlx:122
let p = pat.to_lowercase();
// hlx:123
if let Some(sub) = p.strip_prefix('*') {
// hlx:124
return !sub.is_empty() && word.to_lowercase().contains(sub);
// hlx:125
}
// hlx:126
if let Some(head) = p.strip_suffix('+') {
// hlx:127
if let Some(rest) = w.strip_prefix(head) {
// hlx:128
let seg = rest.split(['/', '\\']).next().unwrap_or("");
// hlx:129
return !rest.is_empty() && !seg.contains(['*', '?', '[']) && !rest.contains('$');
// hlx:130
}
// hlx:131
return false;
// hlx:132
}
// hlx:133
if let Some((head, rest)) = p.split_once('^') {
// hlx:134
return w.starts_with(head) && [head, rest].concat().starts_with(w.as_str());
// hlx:135
}
// hlx:136
if let Some((a, b)) = p.split_once('?') {
// hlx:137
if w.len() <= a.len() + b.len() || !w.starts_with(a) || !w.ends_with(b) {
// hlx:138
return false;
// hlx:139
}
// hlx:140
let mid = &w[a.len()..w.len() - b.len()];
// hlx:141
return !mid.contains(['/', '\\', '*']);
// hlx:142
}
// hlx:143
w == p
// hlx:144
}
// hlx:147
fn token_hits(tok: &str, word: &str) -> bool {
// hlx:148
if let Some(p) = tok.strip_prefix('=') {
// hlx:149
return p.split('|').all(|a| {
a.strip_prefix('!')
.is_none_or(|n| n.is_empty() || !alt_hits(n, word))
}) && p
.split('|')
.any(|a| !a.is_empty() && !a.starts_with('!') && alt_hits(a, word));
// hlx:150
}
// hlx:151
match tok.strip_prefix('*') {
// hlx:152
Some("") => word == "*",
// hlx:153
Some(sub) => {
word.contains(sub)
|| (!sub.starts_with('-') && word.to_lowercase().contains(&sub.to_lowercase()))
}
// hlx:154
None => {
word == tok
|| (tok.chars().all(|c| c.is_ascii_alphabetic()) && word.eq_ignore_ascii_case(tok))
}
// hlx:155
}
// hlx:156
}
// hlx:159
pub fn spec_matches(spec: &str, words: &[String]) -> Option<usize> {
// hlx:160
let mut at = 0;
// hlx:161
let mut positive = 0;
// hlx:162
let mut gap: Vec<&str> = Vec::new();
// hlx:163
for tok in spec.split_whitespace() {
// hlx:164
if let Some(g) = tok.strip_prefix('<') {
// hlx:165
gap.push(g);
// hlx:166
continue;
// hlx:167
}
// hlx:168
if let Some(neg) = tok.strip_prefix('!') {
// hlx:169
if words.iter().any(|w| token_hits(neg, w)) {
// hlx:170
return None;
// hlx:171
}
// hlx:172
continue;
// hlx:173
}
// hlx:174
let found = words.iter().skip(at).position(|w| token_hits(tok, w))?;
// hlx:175
if words[at..at + found]
.iter()
.any(|w| gap.iter().any(|g| token_hits(g, w)))
{
// hlx:176
return None;
// hlx:177
}
// hlx:178
gap.clear();
// hlx:179
at += found + 1;
// hlx:180
positive += 1;
// hlx:181
}
// hlx:182
(positive > 0).then_some(positive)
// hlx:183
}
// hlx:186
fn tag_spec(tag: &str) -> Option<&str> {
// hlx:187
tag.get(..4)
.filter(|p| p.eq_ignore_ascii_case("cmd:"))
.map(|_| &tag[4..])
// hlx:188
}
// hlx:191
pub fn has_cmd(tags: &str) -> bool {
// hlx:192
tags.split(';')
.any(|t| t.trim().to_lowercase().starts_with("cmd:"))
// hlx:193
}
// hlx:196
pub fn cmd_specs(tags: &str) -> Vec<String> {
// hlx:197
let mut v: Vec<String> = tags
.split(';')
.map(str::trim)
.filter(|t| t.to_lowercase().starts_with("cmd:"))
.map(str::to_owned)
.collect();
// hlx:198
v.sort();
// hlx:199
v
// hlx:200
}
// hlx:203
pub fn best_match(tags: &str, words: &[String]) -> Option<usize> {
// hlx:204
let mut best: Option<usize> = None;
// hlx:205
for t in tags.split(';') {
// hlx:206
let Some(spec) = tag_spec(t.trim()) else {
continue;
};
// hlx:207
if let Some(n) = spec_matches(spec, words) {
// hlx:208
if best.is_none_or(|b| n > b) {
// hlx:209
best = Some(n);
// hlx:210
}
// hlx:211
}
// hlx:212
}
// hlx:213
best
// hlx:214
}