fastshell 0.3.3

Lightweight Bash + Python runtime SDK for mobile AI agents
Documentation
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
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.

use crate::shell::{CommandOutput, Shell};

impl Shell {
    pub fn cmd_shuf(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut count: Option<usize> = None;
        let mut echo_mode = false;
        let mut echo_args = Vec::new();

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-n" => {
                    if i + 1 < args.len() {
                        count = args[i + 1].parse().ok();
                        i += 1;
                    }
                }
                "-e" => echo_mode = true,
                arg if !arg.starts_with('-') && echo_mode => echo_args.push(arg.to_string()),
                _ => {}
            }
            i += 1;
        }

        let mut items: Vec<String> = if echo_mode {
            echo_args.into_iter().collect()
        } else {
            let input = match stdin {
                Some(s) => s.to_string(),
                None => return CommandOutput::error("shuf: missing input\n".to_string(), 1),
            };
            input.lines().map(|l| l.to_string()).collect()
        };

        shuffle(&mut items);

        let take = count.unwrap_or(items.len()).min(items.len());
        let mut output = String::new();
        for item in items.iter().take(take) {
            output.push_str(item);
            output.push('\n');
        }

        CommandOutput::success(output)
    }

    pub fn cmd_uuidgen(&self, _args: &[&str]) -> CommandOutput {
        let id = uuid::Uuid::new_v4().to_string();
        CommandOutput::success(id + "\n")
    }

    pub fn cmd_rev(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let input = if args.iter().any(|a| !a.starts_with('-')) {
            let mut content = String::new();
            for arg in args {
                if !arg.starts_with('-') {
                    match self.vfs.read_to_string(arg, &self.cwd) {
                        Ok(c) => content.push_str(&c),
                        Err(e) => return CommandOutput::error(format!("rev: {}: {}\n", arg, e), 1),
                    }
                }
            }
            content
        } else {
            match stdin {
                Some(s) => s.to_string(),
                None => return CommandOutput::error("rev: missing input\n".to_string(), 1),
            }
        };

        let mut output = String::new();
        for line in input.lines() {
            let reversed: String = line.chars().rev().collect();
            output.push_str(&reversed);
            output.push('\n');
        }

        CommandOutput::success(output)
    }

    pub fn cmd_split(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut prefix = "x".to_string();
        let mut lines_per_file = 1000usize;
        let mut by_lines = true;
        let mut files = Vec::new();

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-l" => {
                    if i + 1 < args.len() {
                        lines_per_file = args[i + 1].parse().unwrap_or(1000);
                        by_lines = true;
                        i += 1;
                    }
                }
                "-b" => {
                    if i + 1 < args.len() {
                        lines_per_file = parse_size(args[i + 1]);
                        by_lines = false;
                        i += 1;
                    }
                }
                arg if arg.starts_with("-l") && arg.len() > 2 => {
                    lines_per_file = arg[2..].parse().unwrap_or(1000);
                    by_lines = true;
                }
                arg if arg.starts_with("-b") && arg.len() > 2 => {
                    lines_per_file = parse_size(&arg[2..]);
                    by_lines = false;
                }
                arg if !arg.starts_with('-') && files.is_empty() => files.push(arg.to_string()),
                arg if !arg.starts_with('-') && prefix == "x" => prefix = arg.to_string(),
                _ => {}
            }
            i += 1;
        }

        let input = if files.is_empty() {
            match stdin {
                Some(s) => s.to_string(),
                None => return CommandOutput::error("split: missing input\n".to_string(), 1),
            }
        } else {
            match self.vfs.read_to_string(&files[0], &self.cwd) {
                Ok(c) => c,
                Err(e) => return CommandOutput::error(format!("split: {}: {}\n", files[0], e), 1),
            }
        };

        let mut output = String::new();
        if by_lines {
            let lines: Vec<&str> = input.lines().collect();
            let mut chunk_idx = 0;
            for chunk in lines.chunks(lines_per_file) {
                let name = format!("{}{}", prefix, suffix(chunk_idx));
                let content = chunk.join("\n") + "\n";
                match self.vfs.write(&name, &self.cwd, &content) {
                    Ok(_) => {}
                    Err(e) => {
                        output.push_str(&format!("split: {}: {}\n", name, e));
                    }
                }
                chunk_idx += 1;
            }
        }

        if output.is_empty() {
            CommandOutput::success(String::new())
        } else {
            CommandOutput {
                stdout: String::new(),
                stderr: output,
                exit_code: 1,
            }
        }
    }

    pub fn cmd_comm(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let files: Vec<&str> = args
            .iter()
            .filter(|a| !a.starts_with('-'))
            .copied()
            .collect();

        if files.len() < 2 && stdin.is_none() {
            return CommandOutput::error("comm: missing file operands\n".to_string(), 1);
        }

        let lines1 = if files.is_empty() {
            match stdin {
                Some(s) => s.lines().map(|l| l.to_string()).collect(),
                None => vec![],
            }
        } else {
            match self.vfs.read_to_string(files[0], &self.cwd) {
                Ok(c) => c.lines().map(|l| l.to_string()).collect(),
                Err(e) => return CommandOutput::error(format!("comm: {}: {}\n", files[0], e), 1),
            }
        };

        let lines2 = if files.len() > 1 {
            match self.vfs.read_to_string(files[1], &self.cwd) {
                Ok(c) => c.lines().map(|l| l.to_string()).collect(),
                Err(e) => return CommandOutput::error(format!("comm: {}: {}\n", files[1], e), 1),
            }
        } else {
            vec![]
        };

        let mut output = String::new();
        let mut i = 0;
        let mut j = 0;

        while i < lines1.len() || j < lines2.len() {
            if i >= lines1.len() {
                output.push_str(&format!("\t\t{}\n", lines2[j]));
                j += 1;
            } else if j >= lines2.len() {
                output.push_str(&format!("{}\n", lines1[i]));
                i += 1;
            } else {
                match lines1[i].cmp(&lines2[j]) {
                    std::cmp::Ordering::Less => {
                        output.push_str(&format!("{}\n", lines1[i]));
                        i += 1;
                    }
                    std::cmp::Ordering::Greater => {
                        output.push_str(&format!("\t\t{}\n", lines2[j]));
                        j += 1;
                    }
                    std::cmp::Ordering::Equal => {
                        output.push_str(&format!("\t\t{}\n", lines1[i]));
                        i += 1;
                        j += 1;
                    }
                }
            }
        }

        CommandOutput::success(output)
    }

    pub fn cmd_xxd(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut reverse = false;
        let mut plain = false;
        let mut limit: Option<usize> = None;
        let mut seek: usize = 0;
        let mut files = Vec::new();

        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-r" => reverse = true,
                "-p" => plain = true,
                "-l" => {
                    if i + 1 < args.len() {
                        limit = args[i + 1].parse().ok();
                        i += 1;
                    }
                }
                "-s" => {
                    if i + 1 < args.len() {
                        seek = parse_offset(args[i + 1]);
                        i += 1;
                    }
                }
                arg if arg.starts_with("-l") && arg.len() > 2 => {
                    limit = arg[2..].parse().ok();
                }
                arg if arg.starts_with("-s") && arg.len() > 2 => {
                    seek = parse_offset(&arg[2..]);
                }
                arg if !arg.starts_with('-') => files.push(arg.to_string()),
                _ => {}
            }
            i += 1;
        }

        let data = if files.is_empty() {
            match stdin {
                Some(s) => s.as_bytes().to_vec(),
                None => return CommandOutput::error("xxd: missing input\n".to_string(), 1),
            }
        } else {
            let mut all = Vec::new();
            for file in &files {
                match self.vfs.read(file, &self.cwd) {
                    Ok(d) => all.extend_from_slice(&d),
                    Err(e) => return CommandOutput::error(format!("xxd: {}: {}\n", file, e), 1),
                }
            }
            all
        };

        let data = if seek > 0 {
            if seek >= data.len() {
                &[] as &[u8]
            } else {
                &data[seek..]
            }
        } else {
            &data[..]
        };

        let data = if let Some(lim) = limit {
            if lim < data.len() {
                &data[..lim]
            } else {
                data
            }
        } else {
            data
        };

        if reverse {
            return xxd_reverse(data);
        }

        if plain {
            return xxd_plain(data);
        }

        let mut output = String::new();
        let base_offset = seek;
        for (idx, chunk) in data.chunks(16).enumerate() {
            let offset = base_offset + idx * 16;
            output.push_str(&format!("{:08x}: ", offset));
            let mut hex_part = String::new();
            let mut ascii_part = String::new();
            for (j, &byte) in chunk.iter().enumerate() {
                hex_part.push_str(&format!("{:02x}", byte));
                if j % 2 == 1 {
                    hex_part.push(' ');
                }
                ascii_part.push(if byte >= 0x20 && byte < 0x7f {
                    byte as char
                } else {
                    '.'
                });
            }
            if chunk.len() < 16 {
                let padding = (16 - chunk.len()) * 2 + (16 - chunk.len()) / 2;
                hex_part.push_str(&" ".repeat(padding));
            }
            output.push_str(&format!("{:<50} {}\n", hex_part, ascii_part));
        }

        CommandOutput::success(output)
    }

    pub fn cmd_expr(&self, args: &[&str]) -> CommandOutput {
        if args.is_empty() {
            return CommandOutput {
                stdout: "0\n".to_string(),
                stderr: String::new(),
                exit_code: 1,
            };
        }

        let expr: Vec<&str> = args.to_vec();
        match evaluate_expr(&expr) {
            Ok(val) => CommandOutput::success(format!("{}\n", val)),
            Err(e) => CommandOutput::error(format!("expr: {}\n", e), 1),
        }
    }
}

fn parse_offset(s: &str) -> usize {
    if s.starts_with("0x") || s.starts_with("0X") {
        usize::from_str_radix(&s[2..], 16).unwrap_or(0)
    } else {
        s.parse().unwrap_or(0)
    }
}

fn xxd_reverse(data: &[u8]) -> CommandOutput {
    let input = String::from_utf8_lossy(data);
    let mut output = Vec::new();

    for line in input.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        // Skip the offset prefix if present
        let hex_part = if let Some(colon_pos) = line.find(':') {
            &line[colon_pos + 1..]
        } else {
            line
        };

        let hex_only: String = hex_part.chars().filter(|c| c.is_ascii_hexdigit()).collect();
        for chunk in hex_only.as_bytes().chunks(2) {
            if chunk.len() == 2 {
                let hex_str = std::str::from_utf8(chunk).unwrap_or("00");
                if let Ok(byte) = u8::from_str_radix(hex_str, 16) {
                    output.push(byte);
                }
            }
        }
    }

    CommandOutput::success(String::from_utf8_lossy(&output).to_string())
}

fn xxd_plain(data: &[u8]) -> CommandOutput {
    let mut output = String::new();
    for chunk in data.chunks(30) {
        for &byte in chunk {
            output.push_str(&format!("{:02x}", byte));
        }
        output.push('\n');
    }
    CommandOutput::success(output)
}

fn shuffle<T>(items: &mut Vec<T>) {
    use std::collections::hash_map::RandomState;
    use std::hash::{BuildHasher, Hasher};
    let hasher = RandomState::new().build_hasher();
    let mut seed = hasher.finish();
    let len = items.len();
    for i in (1..len).rev() {
        seed = seed
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        let j = (seed as usize) % (i + 1);
        items.swap(i, j);
    }
}

fn suffix(idx: usize) -> String {
    let letters = "abcdefghijklmnopqrstuvwxyz";
    let mut n = idx;
    let mut result = String::new();
    loop {
        result.insert(0, letters.chars().nth(n % 26).unwrap());
        n /= 26;
        if n == 0 {
            break;
        }
        n -= 1;
    }
    result
}

fn parse_size(s: &str) -> usize {
    let s = s.trim();
    if let Some(rest) = s.strip_suffix('K') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024
    } else if let Some(rest) = s.strip_suffix('M') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024 * 1024
    } else if let Some(rest) = s.strip_suffix('G') {
        rest.parse::<f64>().unwrap_or(0.0) as usize * 1024 * 1024 * 1024
    } else {
        s.parse().unwrap_or(0)
    }
}

fn evaluate_expr(args: &[&str]) -> Result<i64, String> {
    if args.len() == 1 {
        return args[0]
            .parse::<i64>()
            .map_err(|_| format!("syntax error: {}", args[0]));
    }
    if args.len() == 3 {
        let left: i64 = args[0]
            .parse()
            .map_err(|_| format!("non-integer argument: {}", args[0]))?;
        let right: i64 = args[2]
            .parse()
            .map_err(|_| format!("non-integer argument: {}", args[2]))?;
        match args[1] {
            "+" => Ok(left + right),
            "-" => Ok(left - right),
            "*" => Ok(left * right),
            "/" => {
                if right == 0 {
                    Err("division by zero".to_string())
                } else {
                    Ok(left / right)
                }
            }
            "%" => {
                if right == 0 {
                    Err("division by zero".to_string())
                } else {
                    Ok(left % right)
                }
            }
            _ => Err(format!("syntax error: {}", args[1])),
        }
    } else if args.len() > 3 {
        let first = evaluate_expr(&args[..3])?;
        let first_str = first.to_string();
        let mut rest: Vec<&str> = vec![&first_str];
        rest.extend_from_slice(&args[3..]);
        evaluate_expr(&rest)
    } else {
        Err("syntax error".to_string())
    }
}