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
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
use std::fmt::Write;
use std::ops::ControlFlow;
use std::str::FromStr;

use clap::{Args, Parser};
use owo_colors::AnsiColors;

use crate::command::{self, BoxCommand, UserFunc};
use crate::utils::validate_variable_name;
use crate::{Error, ExecContext, ExecResult, LocateExternalCommand, Status, VarScope, Variable};

macro_rules! bail {
    ($($msg:tt)+) => {
        return Err(crate::Error::Custom(format!($($msg)+)).into())
    };
}

macro_rules! ensure {
    ($cond:expr, $($msg:tt)+) => {
        if !$cond {
            bail!($($msg)+);
        }
    };
}

pub mod test;

pub fn all_builtins() -> impl ExactSizeIterator<Item = (&'static str, BoxCommand)> {
    [
        (
            "command",
            Box::new(command::parsed_builtin(command)) as BoxCommand,
        ),
        ("set", Box::new(command::parsed_builtin(set))),
        ("builtin", Box::new(command::parsed_builtin(builtin))),
        ("source", Box::new(command::raw_builtin(source))),
        ("test", Box::new(command::raw_builtin(test::test))),
        ("functions", Box::new(command::parsed_builtin(functions))),
        ("set_color", Box::new(command::parsed_builtin(set_color))),
        ("echo", Box::new(command::raw_builtin(echo))),
        ("type", Box::new(command::parsed_builtin(type_))),
    ]
    .into_iter()
}

#[derive(Debug, Parser)]
pub(crate) struct FunctionOpts {
    #[arg(long, short)]
    pub description: Option<String>,
}

// TODO
#[derive(Debug, Parser)]
pub struct SetOpts {
    #[command(flatten)]
    op: SetOp,

    #[command(flatten)]
    scope: SetScope,

    #[command(flatten)]
    attr: SetVarAttr,

    #[arg(trailing_var_arg = true)]
    args: Vec<String>,
}

#[derive(Debug, Args)]
#[group(required = false, multiple = false)]
pub struct SetScope {
    #[arg(long, short)]
    local: bool,
    #[arg(long, short)]
    function: bool,
    #[arg(long, short)]
    global: bool,
    #[arg(long, short = 'U')]
    universal: bool,
}

#[derive(Debug, Args)]
#[group(required = false, multiple = false)]
pub struct SetOp {
    #[arg(long, short)]
    erase: bool,
    #[arg(long, short)]
    query: bool,
}

#[derive(Debug, Args)]
#[group(required = false, multiple = false)]
pub struct SetVarAttr {
    #[arg(long, short = 'x')]
    export: bool,
    #[arg(long, short)]
    unexport: bool,
}

pub async fn set(ctx: &mut ExecContext<'_>, args: SetOpts) -> ExecResult<Option<Status>> {
    let scope = if args.scope.local {
        VarScope::Local
    } else if args.scope.function {
        VarScope::Function
    } else if args.scope.global {
        VarScope::Global
    } else if args.scope.universal {
        VarScope::Universal
    } else {
        VarScope::Auto
    };

    if args.op.erase || args.op.query {
        ensure!(
            !args.attr.export && !args.attr.unexport,
            "--export or --unexport can only be used for setting or listing variables",
        );
    }

    if args.op.query {
        let mut fail_cnt = 0usize;
        for name in &args.args {
            ensure!(scope == VarScope::Auto, "TODO");
            if ctx.get_var(name).is_none() {
                fail_cnt += 1;
            }
        }
        Ok(Some(fail_cnt.into()))
    } else if args.op.erase {
        for name in &args.args {
            validate_variable_name(name)?;
        }
        let mut fail_cnt = 0usize;
        for name in &args.args {
            if !ctx.remove_var(scope, name) {
                fail_cnt += 1;
            }
        }
        Ok(Some(fail_cnt.into()))
    } else if let Some((name, vals)) = args.args.split_first() {
        validate_variable_name(name)?;
        ensure!(
            !ctx.has_special_var(name),
            "cannot modify special variable: {name:?}",
        );
        let mut var = Variable::new_list(vals.to_vec());
        var.export = args.attr.export;
        ctx.set_var(name, scope, var);
        // Keep previous status.
        Ok(None)
    } else {
        let mut buf = String::new();
        ctx.list_vars::<()>(scope, |name, var| {
            if args.attr.export && !var.export || args.attr.unexport && var.export {
                return ControlFlow::Continue(());
            }

            buf.push_str(name);
            if !var.value.is_empty() {
                buf.push(' ');
                for (idx, val) in var.value.iter().enumerate() {
                    if idx != 0 {
                        buf.push_str("  ");
                    }
                    write!(buf, "\"{}\"", val.escape_debug()).unwrap();
                }
            }
            buf.push('\n');
            ControlFlow::Continue(())
        });
        let _: ExecResult<_> = ctx.io().stdout.write_all(buf).await;
        Ok(Some(Status::SUCCESS))
    }
}

#[derive(Debug, Parser)]
pub struct BuiltinArgs {
    #[arg(long, short)]
    names: bool,
    #[arg(long, short)]
    query: bool,

    #[arg(trailing_var_arg = true)]
    args: Vec<String>,
}

pub async fn builtin(ctx: &mut ExecContext<'_>, args: BuiltinArgs) -> ExecResult<Option<Status>> {
    ensure!(
        !(args.names && args.query),
        "--names and --query are mutually exclusive"
    );
    if args.names {
        let mut names = ctx.builtins().map(|(name, _)| name).collect::<Vec<_>>();
        names.sort_unstable();
        let out = names.iter().flat_map(|&s| [s, "\n"]).collect::<String>();
        ctx.io().stdout.write_all(out).await?;
        Ok(Some(Status::SUCCESS))
    } else if args.query {
        let ok = args.args.iter().any(|name| ctx.get_builtin(name).is_some());
        Ok(Some(ok.into()))
    } else {
        ensure!(!args.args.is_empty(), "missing builtin name");
        let name = &args.args[0];
        let cmd = ctx
            .get_builtin(name)
            .ok_or_else(|| Error::CommandNotFound(name.into()))?;
        cmd.exec(ctx, &args.args).await;
        Ok(None)
    }
}

#[derive(Debug, Parser)]
pub struct CommandArgs {
    #[arg(long, short)]
    pub query: bool,
    #[arg(long, short)]
    pub search: bool,

    #[arg(trailing_var_arg = true)]
    pub args: Vec<String>,
}

pub async fn command(ctx: &mut ExecContext<'_>, args: CommandArgs) -> ExecResult<Status> {
    if args.query {
        let mut found = false;
        for name in &args.args {
            if let LocateExternalCommand::ExecFile(_) = ctx.locate_external_command(name) {
                found = true;
                break;
            }
        }
        Ok(found.into())
    } else if args.search {
        let mut found = true;
        let mut buf = String::new();
        for name in &args.args {
            if let LocateExternalCommand::ExecFile(path) = ctx.locate_external_command(name) {
                found = true;
                writeln!(buf, "{}", path.display()).unwrap();
            }
        }
        let _: ExecResult<_> = ctx.io().stdout.write_all(buf).await;
        Ok(found.into())
    } else {
        let cmd = args.args.first().ok_or(Error::EmptyCommand)?;
        let exe_path = match ctx.locate_external_command(cmd) {
            LocateExternalCommand::ExecFile(path) => path,
            LocateExternalCommand::NotExecFile(path) | LocateExternalCommand::Dir(path) => {
                return Err(Error::CommandNotFound(format!(
                    "{} (candidate {} is not an executable file)",
                    cmd,
                    path.display(),
                )));
            }
            LocateExternalCommand::NotFound => {
                return Err(Error::CommandNotFound(cmd.into()));
            }
        };
        ctx.exec_external_command(&exe_path, &args.args).await
    }
}

pub async fn source(ctx: &mut ExecContext<'_>, args: &[String]) -> ExecResult<()> {
    let (path, text, args) = match args[1..].split_first() {
        Some((path, args)) => {
            let text = tokio::task::spawn_blocking({
                let path = path.clone();
                move || std::fs::read_to_string(path)
            })
            .await
            .expect("no panic")
            .map_err(Error::ReadWrite)?;
            (path.clone(), text, args)
        }
        None => {
            let text = ctx.io().stdin.read_to_string().await?;
            ("<stdin>".into(), text, &[][..])
        }
    };

    let scope = &mut **ctx.enter_local_scope();
    scope.set_var("argv", VarScope::Local, args.to_vec());
    scope.exec_source(Some(path), text).await;
    Ok(())
}

#[derive(Debug, Parser)]
pub struct FunctionsOpts {
    #[arg(long, short)]
    pub erase: bool,
    #[arg(long, short)]
    pub query: bool,
    #[arg(long, short)]
    pub all: bool,

    pub funcs: Vec<String>,
}

pub async fn functions(ctx: &mut ExecContext<'_>, args: FunctionsOpts) -> ExecResult {
    ensure!(
        args.erase as u8 + args.query as u8 <= 1,
        "--erase and --query are mutually exclusive",
    );
    ensure!(
        !args.all || args.funcs.is_empty(),
        "--all can only be used without positional arguments"
    );

    if args.erase {
        let fail_cnt = args
            .funcs
            .iter()
            .map(|name| !ctx.remove_global_func(name) as usize)
            .sum::<usize>();
        Ok(fail_cnt.into())
    } else if args.query {
        let mut fail_cnt = 0usize;
        for name in &args.funcs {
            if ctx.get_or_autoload_func(name).await.is_none() {
                fail_cnt += 1;
            }
        }
        Ok(fail_cnt.into())
    } else if args.funcs.is_empty() {
        let mut buf = String::new();
        ctx.list_funcs::<()>(|name, _cmd| {
            if args.all || !name.starts_with("_") {
                writeln!(buf, "{name}").unwrap();
            }
            ControlFlow::Continue(())
        })
        .await;
        let _: ExecResult<_> = ctx.io().stdout.write_all(buf).await;
        Ok(Status::SUCCESS)
    } else {
        let mut buf = String::new();
        let mut fail_cnt = 0usize;
        for name in &args.funcs {
            if let Some(cmd) = ctx.get_or_autoload_func(name).await {
                let func = cmd.as_any().downcast_ref::<UserFunc>().unwrap();
                writeln!(buf, "{:#?}", func.stmt()).unwrap();
            } else {
                fail_cnt += 1;
            }
        }
        let _: ExecResult<_> = ctx.io().stdout.write_all(buf).await;
        Ok(fail_cnt.into())
    }
}

#[derive(Debug, Parser)]
pub struct SetColorOpts {
    #[arg(long, short)]
    background: Option<SetColor>,

    #[arg(long, short = 'c')]
    print_colors: Option<SetColor>,

    #[arg(long, short = 'o')]
    bold: bool,

    #[arg(long, short)]
    dim: bool,

    #[arg(long, short)]
    italics: bool,

    #[arg(long, short)]
    reverse: bool,

    #[arg(long, short)]
    underline: bool,

    color: Option<SetColor>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetColor {
    Normal,
    Ansi(AnsiColors),
    Rgb(u8, u8, u8),
}

impl FromStr for SetColor {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || format!("invalid color: {s:?}");
        Ok(Self::Ansi(match s {
            "normal" => return Ok(Self::Normal),

            "black" => AnsiColors::Black,
            "red" => AnsiColors::Red,
            "green" => AnsiColors::Green,
            "yellow" => AnsiColors::Yellow,
            "blue" => AnsiColors::Blue,
            "magenta" => AnsiColors::Magenta,
            "cyan" => AnsiColors::Cyan,
            "white" => AnsiColors::White,
            "brblack" => AnsiColors::BrightBlack,
            "brred" => AnsiColors::BrightRed,
            "brgreen" => AnsiColors::BrightGreen,
            "bryellow" => AnsiColors::BrightYellow,
            "brblue" => AnsiColors::BrightBlue,
            "brmagenta" => AnsiColors::BrightMagenta,
            "brcyan" => AnsiColors::BrightCyan,
            "brwhite" => AnsiColors::BrightWhite,

            s if s.len() == 3 => {
                let v = u32::from_str_radix(s, 16).map_err(|_| err())?;
                let f = |x: u32| x as u8 * 11;
                return Ok(Self::Rgb(f(v >> 8), f((v >> 4) & 0xF), f(v & 0xF)));
            }
            s if s.len() == 6 => {
                let v = u32::from_str_radix(s, 16).map_err(|_| err())?;
                let f = |x: u32| x as u8;
                return Ok(Self::Rgb(f(v >> 16), f((v >> 8) & 0xFF), f(v & 0xFF)));
            }
            _ => return Err(err()),
        }))
    }
}

pub async fn set_color(ctx: &mut ExecContext<'_>, args: SetColorOpts) -> ExecResult {
    let mut s = owo_colors::Style::new();
    match args.background {
        Some(SetColor::Normal) => s = s.on_default_color(),
        Some(SetColor::Ansi(c)) => s = s.on_color(c),
        Some(SetColor::Rgb(r, g, b)) => s = s.on_truecolor(r, g, b),
        None => {}
    }
    if args.bold {
        s = s.bold();
    }
    if args.dim {
        s = s.dimmed();
    }
    if args.italics {
        s = s.italic();
    }
    if args.reverse {
        s = s.reversed();
    }
    if args.underline {
        s = s.underline();
    }

    match args.color {
        Some(SetColor::Normal) => s = s.default_color(),
        Some(SetColor::Ansi(c)) => s = s.color(c),
        Some(SetColor::Rgb(r, g, b)) => s = s.truecolor(r, g, b),
        None => {}
    }

    let reset = if args.color == Some(SetColor::Normal) || args.background == Some(SetColor::Normal)
    {
        "\x1B[m"
    } else {
        ""
    };

    let s = format!("{reset}{}", s.prefix_formatter());
    if !s.is_empty() {
        let _: ExecResult<_> = ctx.io().stdout.write_all(s).await;
        Ok(Status::SUCCESS)
    } else {
        Ok(Status::FAILURE)
    }
}

pub async fn echo(ctx: &mut ExecContext<'_>, args: &[String]) -> ExecResult {
    let mut newline = true;
    let mut unescape = false;
    let mut space = true;

    let mut iter = args[1..].iter();
    let mut buf = String::new();
    let mut first = true;
    for el in iter.by_ref() {
        match &**el {
            "-n" => newline = false,
            "-s" => space = false,
            "-E" => unescape = false,
            "-e" => unescape = true,
            "--" => break,
            _ => {
                buf.push_str(el);
                first = false;
                break;
            }
        }
    }

    for el in iter {
        if first {
            first = false;
        } else if space {
            buf.push(' ');
        }
        buf.push_str(el);
    }
    if newline {
        buf.push('\n');
    }

    ensure!(!unescape, "TODO");

    ctx.io().stdout.write_all(buf).await
}

#[derive(Debug, Parser)]
pub struct TypeArgs {
    pub names: Vec<String>,
}

pub async fn type_(ctx: &mut ExecContext<'_>, args: TypeArgs) -> ExecResult {
    let mut buf = String::new();
    let mut failed = 0usize;
    for name in &args.names {
        if ctx.get_global_func(name).is_some() {
            // TODO: function source
            writeln!(buf, "{name} is a function").unwrap();
        } else if ctx.get_builtin(name).is_some() {
            writeln!(buf, "{name} is a builtin").unwrap();
        } else {
            match ctx.locate_external_command(name) {
                LocateExternalCommand::ExecFile(path) => {
                    writeln!(buf, "{name} is {}", path.display()).unwrap();
                }
                LocateExternalCommand::NotExecFile(_)
                | LocateExternalCommand::Dir(_)
                | LocateExternalCommand::NotFound => {
                    writeln!(buf, "type: cannot find {name:?}").unwrap();
                    failed += 1;
                }
            }
        }
    }

    let _: ExecResult<_> = ctx.io().stdout.write_all(buf).await;
    Ok(failed.into())
}