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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// 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
if r == "tail" && w.iter().any(|x| x.starts_with("--pid")) {
// hlx:47
return false;
// hlx:48
}
// hlx:49
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:50
}
// hlx:53
fn runs_input(w: &[String]) -> bool {
// hlx:54
let Some(r) = runner(w) else { return false };
// hlx:55
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:56
}
// hlx:59
pub fn command_words(command: &str) -> Vec<String> {
// hlx:60
let mut segs: Vec<(String, String, bool)> = Vec::new();
// hlx:61
let mut raw = String::new();
// hlx:62
let mut bare = String::new();
// hlx:63
let mut quote = ' ';
// hlx:64
let mut prev = ' ';
// hlx:65
let mut it = command.chars();
// hlx:66
while let Some(c) = it.next() {
// hlx:67
if quote == ' ' && matches!(c, ';' | '|' | '&' | '(' | ')' | '\n') {
// hlx:69
if c == '&'
&& prev != '&'
&& prev != '>'
&& prev != '|'
&& !matches!(it.as_str().chars().next(), Some('&' | '>'))
{
// hlx:70
raw.push_str(" __bg__");
// hlx:71
bare.push_str(" __bg__");
// hlx:72
if it.as_str().trim_start().starts_with(')') {
// hlx:73
raw.push_str(" __subbg__");
// hlx:74
bare.push_str(" __subbg__");
// hlx:75
}
// hlx:76
}
// hlx:77
segs.push((
raw.clone(),
bare.clone(),
c == '|' && prev != '|' && it.as_str().chars().next() != Some('|'),
));
// hlx:78
raw.clear();
// hlx:79
bare.clear();
// hlx:80
prev = c;
// hlx:81
continue;
// hlx:82
}
// hlx:83
prev = c;
// hlx:84
if c == '\\' && quote != '\'' {
// hlx:85
raw.push(c);
// hlx:86
bare.push(c);
// hlx:87
if let Some(n) = it.next() {
// hlx:88
raw.push(n);
// hlx:89
bare.push(n);
// hlx:90
}
// hlx:91
continue;
// hlx:92
}
// hlx:93
if quote != ' ' {
// hlx:94
raw.push(c);
// hlx:95
bare.push(if c == '>' { '_' } else { c });
// hlx:96
if c == quote {
// hlx:97
quote = ' ';
// hlx:98
}
// hlx:99
continue;
// hlx:100
}
// hlx:101
if c == '"' || c == '\'' || c == '`' {
// hlx:102
quote = c;
// hlx:103
}
// hlx:104
raw.push(c);
// hlx:105
bare.push(c);
// hlx:106
}
// hlx:107
segs.push((raw, bare, false));
// hlx:108
let mut out: Vec<String> = Vec::new();
// hlx:109
for i in 0..segs.len() {
// hlx:110
let w = words(&segs[i].0);
// hlx:111
if w.is_empty() {
// hlx:112
continue;
// hlx:113
}
// hlx:114
if read_only(&w)
&& !w.iter().any(|x| x == "__bg__")
&& !(segs[i].2 && segs.get(i + 1).is_some_and(|s| runs_input(&words(&s.0))))
{
// hlx:115
let b = words(&segs[i].1);
// hlx:116
for j in 0..b.len() {
// hlx:117
if b[j] == ">" || b[j] == ">>" {
// hlx:118
out.push(b[j].clone());
// hlx:119
if let Some(t) = b.get(j + 1) {
// hlx:120
out.push(t.clone());
// hlx:121
}
// hlx:122
}
// hlx:123
}
// hlx:124
continue;
// hlx:125
}
// hlx:126
out.extend(w);
// hlx:127
}
// hlx:128
out
// hlx:129
}
// hlx:132
fn alt_hits(pat: &str, word: &str) -> bool {
// hlx:133
let w = word.trim_end_matches(['/', '\\']).to_lowercase();
// hlx:134
let p = pat.to_lowercase();
// hlx:135
if let Some(sub) = p.strip_prefix('*') {
// hlx:136
return !sub.is_empty() && word.to_lowercase().contains(sub);
// hlx:137
}
// hlx:138
if let Some(head) = p.strip_suffix('+') {
// hlx:139
if let Some(rest) = w.strip_prefix(head) {
// hlx:140
let seg = rest.split(['/', '\\']).next().unwrap_or("");
// hlx:141
return !rest.is_empty() && !seg.contains(['*', '?', '[']) && !rest.contains('$');
// hlx:142
}
// hlx:143
return false;
// hlx:144
}
// hlx:145
if let Some((head, rest)) = p.split_once('^') {
// hlx:146
return w.starts_with(head) && [head, rest].concat().starts_with(w.as_str());
// hlx:147
}
// hlx:148
if let Some((a, b)) = p.split_once('?') {
// hlx:149
if w.len() <= a.len() + b.len() || !w.starts_with(a) || !w.ends_with(b) {
// hlx:150
return false;
// hlx:151
}
// hlx:152
let mid = &w[a.len()..w.len() - b.len()];
// hlx:153
return !mid.contains(['/', '\\', '*']);
// hlx:154
}
// hlx:155
w == p
// hlx:156
}
// hlx:159
fn token_hits(tok: &str, word: &str) -> bool {
// hlx:160
if let Some(p) = tok.strip_prefix('=') {
// hlx:161
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:162
}
// hlx:163
match tok.strip_prefix('*') {
// hlx:164
Some("") => word == "*",
// hlx:165
Some(sub) => {
word.contains(sub)
|| (!sub.starts_with('-') && word.to_lowercase().contains(&sub.to_lowercase()))
}
// hlx:166
None => {
word == tok
|| (tok.chars().all(|c| c.is_ascii_alphabetic()) && word.eq_ignore_ascii_case(tok))
}
// hlx:167
}
// hlx:168
}
// hlx:171
pub fn spec_matches(spec: &str, words: &[String]) -> Option<usize> {
// hlx:172
let mut at = 0;
// hlx:173
let mut positive = 0;
// hlx:174
let mut gap: Vec<&str> = Vec::new();
// hlx:175
for tok in spec.split_whitespace() {
// hlx:176
if let Some(g) = tok.strip_prefix('<') {
// hlx:177
gap.push(g);
// hlx:178
continue;
// hlx:179
}
// hlx:180
if let Some(neg) = tok.strip_prefix('!') {
// hlx:181
if words.iter().any(|w| token_hits(neg, w)) {
// hlx:182
return None;
// hlx:183
}
// hlx:184
continue;
// hlx:185
}
// hlx:186
let found = words.iter().skip(at).position(|w| token_hits(tok, w))?;
// hlx:187
if words[at..at + found]
.iter()
.any(|w| gap.iter().any(|g| token_hits(g, w)))
{
// hlx:188
return None;
// hlx:189
}
// hlx:190
gap.clear();
// hlx:191
at += found + 1;
// hlx:192
positive += 1;
// hlx:193
}
// hlx:194
(positive > 0).then_some(positive)
// hlx:195
}
// hlx:198
fn tag_spec(tag: &str) -> Option<&str> {
// hlx:199
tag.get(..4)
.filter(|p| p.eq_ignore_ascii_case("cmd:"))
.map(|_| &tag[4..])
// hlx:200
}
// hlx:203
pub fn has_cmd(tags: &str) -> bool {
// hlx:204
tags.split(';')
.any(|t| t.trim().to_lowercase().starts_with("cmd:"))
// hlx:205
}
// hlx:208
pub fn cmd_specs(tags: &str) -> Vec<String> {
// hlx:209
let mut v: Vec<String> = tags
.split(';')
.map(str::trim)
.filter(|t| t.to_lowercase().starts_with("cmd:"))
.map(str::to_owned)
.collect();
// hlx:210
v.sort();
// hlx:211
v
// hlx:212
}
// hlx:215
pub fn best_match(tags: &str, words: &[String]) -> Option<usize> {
// hlx:216
let mut best: Option<usize> = None;
// hlx:217
for t in tags.split(';') {
// hlx:218
let Some(spec) = tag_spec(t.trim()) else {
continue;
};
// hlx:219
if let Some(n) = spec_matches(spec, words) {
// hlx:220
if best.is_none_or(|b| n > b) {
// hlx:221
best = Some(n);
// hlx:222
}
// hlx:223
}
// hlx:224
}
// hlx:225
best
// hlx:226
}