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
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
// EndBASIC
// Copyright 2020 Julio Merino
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations
// under the License.

//! Stored program manipulation and interactive editor.

use crate::ast::{ArgSep, Expr, Value, VarType};
use crate::console::Console;
use crate::editor::Editor;
use crate::eval::{CallableMetadata, CallableMetadataBuilder};
use crate::exec::{self, BuiltinCommand, Machine};
use async_trait::async_trait;
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::io;
use std::path::PathBuf;
use std::rc::Rc;
use std::str;

/// Metadata of an entry in the store.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Metadata {
    /// Last modification time of the entry.
    pub date: time::OffsetDateTime,

    /// Total size of the entry.
    pub length: u64,
}

/// Abstract operations to load and store programs on persistent storage.
pub trait Store {
    /// Deletes the program given by `name`.
    fn delete(&mut self, name: &str) -> io::Result<()>;

    /// Returns a sorted list of the entries in the store and their metadata.
    fn enumerate(&self) -> io::Result<BTreeMap<String, Metadata>>;

    /// Loads the contents of the program given by `name`.
    fn get(&self, name: &str) -> io::Result<String>;

    /// Saves the in-memory program given by `content` into `name`.
    fn put(&mut self, name: &str, content: &str) -> io::Result<()>;
}

/// Wraps a `Store` and exposes a bunch of read-only demo files.
///
/// All demo file names are case insensitive.  However, this preserves the case sensitiveness
/// behavior of the underlying store for any files that are passed through.
///
/// This takes ownership of any file names that start with `DEMO:`, which means any such files in
/// the underlying store become invisible.  This should not be a problem in practice because most
/// file systems deny the `:` character in file names.
pub struct DemoStoreOverlay<S: Store> {
    /// The demos to expose, expressed as a mapping of names to (metadata, content) pairs.
    demos: HashMap<&'static str, (Metadata, String)>,

    /// The wrapped store.
    delegate: S,
}

/// Converts the raw bytes of a demo file into the program string to expose.
///
/// The input `bytes` must be valid UTF-8, which we can guarantee because these bytes come from
/// files that we own in the source tree.
///
/// On Windows, the output string has all CRLF pairs converted to LF to ensure that the reported
/// demo file sizes are consistent across platforms.
fn process_demo(bytes: &[u8]) -> String {
    let raw_content = str::from_utf8(bytes).expect("Malformed demo file");
    if cfg!(target_os = "windows") {
        raw_content.replace("\r\n", "\n")
    } else {
        raw_content.to_owned()
    }
}

impl<S: Store> DemoStoreOverlay<S> {
    /// Creates a new demo store that wraps the `delegate` store.
    pub fn new(delegate: S) -> Self {
        let mut demos = HashMap::default();
        {
            let content = process_demo(include_bytes!("../examples/guess.bas"));
            let metadata = Metadata {
                date: time::OffsetDateTime::from_unix_timestamp(1608693152),
                length: content.len() as u64,
            };
            demos.insert("DEMO:GUESS.BAS", (metadata, content));
        }
        {
            let content = process_demo(include_bytes!("../examples/hello.bas"));
            let metadata = Metadata {
                date: time::OffsetDateTime::from_unix_timestamp(1608646800),
                length: content.len() as u64,
            };
            demos.insert("DEMO:HELLO.BAS", (metadata, content));
        }
        {
            let content = process_demo(include_bytes!("../examples/tour.bas"));
            let metadata = Metadata {
                date: time::OffsetDateTime::from_unix_timestamp(1608774770),
                length: content.len() as u64,
            };
            demos.insert("DEMO:TOUR.BAS", (metadata, content));
        }
        Self { demos, delegate }
    }

    /// Disowns and returns the underlying delegate store.
    pub fn unmount(self) -> S {
        self.delegate
    }
}

impl<S: Store> Store for DemoStoreOverlay<S> {
    fn delete(&mut self, name: &str) -> io::Result<()> {
        let uc_name = name.to_ascii_uppercase();
        match self.demos.get(&uc_name.as_ref()) {
            Some(_) => {
                Err(io::Error::new(io::ErrorKind::PermissionDenied, "Demo files are read-only"))
            }
            _ if uc_name.starts_with("DEMO:") => {
                Err(io::Error::new(io::ErrorKind::PermissionDenied, "Demo files are read-only"))
            }
            _ => self.delegate.delete(name),
        }
    }

    fn enumerate(&self) -> io::Result<BTreeMap<String, Metadata>> {
        let mut entries = self.delegate.enumerate()?;

        // TODO(https://github.com/rust-lang/rust/issues/70530): Use drain_filter when available.
        let mut hidden_names = vec![];
        for (name, _) in entries.iter() {
            if name.to_ascii_uppercase().starts_with("DEMO:") {
                hidden_names.push(name.to_owned());
            }
        }
        for name in hidden_names {
            entries.remove(&name);
        }

        for (name, (metadata, _content)) in self.demos.iter() {
            entries.insert(name.to_string(), metadata.clone());
        }
        Ok(entries)
    }

    fn get(&self, name: &str) -> io::Result<String> {
        let uc_name = name.to_ascii_uppercase();
        match self.demos.get(&uc_name.as_ref()) {
            Some(value) => {
                let (_metadata, content) = value;
                Ok(content.to_string())
            }
            _ if uc_name.starts_with("DEMO:") => {
                Err(io::Error::new(io::ErrorKind::NotFound, "Non-existing demo file"))
            }
            _ => self.delegate.get(name),
        }
    }

    fn put(&mut self, name: &str, content: &str) -> io::Result<()> {
        let uc_name = name.to_ascii_uppercase();
        match self.demos.get(&uc_name.as_ref()) {
            Some(_) => {
                Err(io::Error::new(io::ErrorKind::PermissionDenied, "Demo files are read-only"))
            }
            _ if uc_name.starts_with("DEMO:") => {
                Err(io::Error::new(io::ErrorKind::PermissionDenied, "Demo files are read-only"))
            }
            _ => self.delegate.put(name, content),
        }
    }
}

/// Representation of the single program that we can keep in memory.
#[async_trait(?Send)]
pub trait Program {
    /// Edits the program interactively via the given `console`.
    async fn edit(&mut self, console: &mut dyn Console) -> io::Result<()>;

    /// Reloads the contents of the stored program with the given `text`.
    fn load(&mut self, text: &str);

    /// Gets the contents of the stored program as a single string.
    fn text(&self) -> String;
}

/// Computes the path to a source file given the `dir` where it lives and a `basename`.
fn to_filename<S: Into<PathBuf>>(basename: S) -> io::Result<String> {
    let mut basename = basename.into();

    if basename.components().fold(0, |count, _| count + 1) != 1 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "Filename must be a single path component",
        ));
    }

    if let Some(ext) = basename.extension() {
        if ext != "bas" && ext != "BAS" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid filename extension"));
        }
    } else {
        // Attempt to determine a sensible extension based on the case of the basename, assuming
        // that an all-uppercase basename wants an all-uppercase extension.  This is fragile on
        // case-sensitive file systems, but there is not a lot we can do.
        let mut ext = "BAS";
        for ch in basename.to_string_lossy().chars() {
            if ch.is_ascii_lowercase() {
                ext = "bas";
                break;
            }
        }
        basename.set_extension(ext);
    }
    Ok(basename.to_str().expect("Path came from a String").to_owned())
}

/// Shows the contents of directory `path`.
fn show_dir(store: &dyn Store, console: &mut dyn Console) -> io::Result<()> {
    let entries = store.enumerate()?;

    console.print("")?;
    console.print("    Modified              Size    Name")?;
    let mut total_files = 0;
    let mut total_bytes = 0;
    for (name, details) in entries {
        console.print(&format!(
            "    {}    {:6}    {}",
            details.date.format("%F %H:%M"),
            details.length,
            name,
        ))?;
        total_files += 1;
        total_bytes += details.length;
    }
    if total_files > 0 {
        console.print("")?;
    }
    console.print(&format!("    {} file(s), {} bytes", total_files, total_bytes))?;
    console.print("")?;
    Ok(())
}

/// The `DEL` command.
struct DelCommand {
    metadata: CallableMetadata,
    store: Rc<RefCell<dyn Store>>,
}

impl DelCommand {
    /// Creates a new `DEL` command that deletes a file from the `store`.
    pub fn new(store: Rc<RefCell<dyn Store>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("DEL", VarType::Void)
                .with_syntax("filename")
                .with_category("Stored program manipulation")
                .with_description(
                    "Deletes the given program.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS.",
                )
                .build(),
            store,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for DelCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("DEL requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars(), machine.get_functions())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                self.store.borrow_mut().delete(&name)?;
            }
            _ => return exec::new_usage_error("DEL requires a string as the filename"),
        }
        Ok(())
    }
}

/// The `DIR` command.
struct DirCommand {
    metadata: CallableMetadata,
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
}

impl DirCommand {
    /// Creates a new `DIR` command that lists the contents of the `store` on the `console`.
    pub fn new(console: Rc<RefCell<dyn Console>>, store: Rc<RefCell<dyn Store>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("DIR", VarType::Void)
                .with_syntax("")
                .with_category("Stored program manipulation")
                .with_description("Displays the list of files on disk.")
                .build(),
            console,
            store,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for DirCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        _machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("DIR takes no arguments");
        }
        show_dir(&*self.store.borrow(), &mut *self.console.borrow_mut())?;
        Ok(())
    }
}

/// The `EDIT` command.
struct EditCommand {
    metadata: CallableMetadata,
    console: Rc<RefCell<dyn Console>>,
    program: Rc<RefCell<dyn Program>>,
}

impl EditCommand {
    /// Creates a new `EDIT` command that edits the stored `program` in the `console`.
    pub fn new(console: Rc<RefCell<dyn Console>>, program: Rc<RefCell<dyn Program>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("EDIT", VarType::Void)
                .with_syntax("")
                .with_category("Stored program manipulation")
                .with_description("Interactively edits the stored program.")
                .build(),
            console,
            program,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for EditCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        _machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("EDIT takes no arguments");
        }

        let mut console = self.console.borrow_mut();
        let mut program = self.program.borrow_mut();
        program.edit(&mut *console).await?;
        Ok(())
    }
}

/// The `LOAD` command.
struct LoadCommand {
    metadata: CallableMetadata,
    store: Rc<RefCell<dyn Store>>,
    program: Rc<RefCell<dyn Program>>,
}

impl LoadCommand {
    /// Creates a new `LOAD` command that loads a program from the `store` into `program`.
    pub fn new(store: Rc<RefCell<dyn Store>>, program: Rc<RefCell<dyn Program>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("LOAD", VarType::Void)
                .with_syntax("filename")
                .with_category("Stored program manipulation")
                .with_description(
                    "Loads the given program.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS.",
                )
                .build(),
            store,
            program,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for LoadCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("LOAD requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars(), machine.get_functions())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                let content = self.store.borrow().get(&name)?;
                self.program.borrow_mut().load(&content);
                machine.clear();
            }
            _ => return exec::new_usage_error("LOAD requires a string as the filename"),
        }
        Ok(())
    }
}

/// The `NEW` command.
struct NewCommand {
    metadata: CallableMetadata,
    program: Rc<RefCell<dyn Program>>,
}

impl NewCommand {
    /// Creates a new `NEW` command that clears the contents of `program`.
    pub fn new(program: Rc<RefCell<dyn Program>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("NEW", VarType::Void)
                .with_syntax("")
                .with_category("Stored program manipulation")
                .with_description("Clears the stored program from memory.")
                .build(),
            program,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for NewCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("NEW takes no arguments");
        }
        self.program.borrow_mut().load("");
        machine.clear();
        Ok(())
    }
}

/// The `RUN` command.
struct RunCommand {
    metadata: CallableMetadata,
    program: Rc<RefCell<dyn Program>>,
}

impl RunCommand {
    /// Creates a new `RUN` command that executes the `program`.
    pub fn new(program: Rc<RefCell<dyn Program>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("RUN", VarType::Void)
                .with_syntax("")
                .with_category("Stored program manipulation")
                .with_description(
                    "Runs the stored program.
Note that the program runs in the context of the interpreter so it will pick up any variables \
and other state that may already be set.",
                )
                .build(),
            program,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for RunCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("RUN takes no arguments");
        }
        let program = self.program.borrow().text();
        machine.exec(&mut program.as_bytes()).await
    }
}

/// The `SAVE` command.
struct SaveCommand {
    metadata: CallableMetadata,
    store: Rc<RefCell<dyn Store>>,
    program: Rc<RefCell<dyn Program>>,
}

impl SaveCommand {
    /// Creates a new `SAVE` command that saves the contents of the `program` in the `store`.
    pub fn new(store: Rc<RefCell<dyn Store>>, program: Rc<RefCell<dyn Program>>) -> Rc<Self> {
        Rc::from(Self {
            metadata: CallableMetadataBuilder::new("SAVE", VarType::Void)
                .with_syntax("filename")
                .with_category("Stored program manipulation")
                .with_description(
                    "Saves the current program in memory to the given filename.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS.",
                )
                .build(),
            store,
            program,
        })
    }
}

#[async_trait(?Send)]
impl BuiltinCommand for SaveCommand {
    fn metadata(&self) -> &CallableMetadata {
        &self.metadata
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("SAVE requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars(), machine.get_functions())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                let content = self.program.borrow().text();
                self.store.borrow_mut().put(&name, &content)?;
            }
            _ => return exec::new_usage_error("SAVE requires a string as the filename"),
        }
        Ok(())
    }
}

/// Instantiates all program editing commands against the stored `program`, using `console` for
/// interactive editing, and using `dir` as the on-disk storage for the programs.
fn all_commands_for(
    program: Rc<RefCell<dyn Program>>,
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
) -> Vec<Rc<dyn BuiltinCommand>> {
    vec![
        DelCommand::new(store.clone()),
        DirCommand::new(console.clone(), store.clone()),
        EditCommand::new(console.clone(), program.clone()),
        LoadCommand::new(store.clone(), program.clone()),
        NewCommand::new(program.clone()),
        RunCommand::new(program.clone()),
        SaveCommand::new(store, program),
    ]
}

/// Instantiates all program editing commands against a new (empty) program, using `console` for
/// interactive editing, and using `dir` as the on-disk storage for the programs.
pub fn all_commands(
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
) -> Vec<Rc<dyn BuiltinCommand>> {
    all_commands_for(Rc::from(RefCell::from(Editor::new())), console, store)
}

#[cfg(test)]
mod testutils {
    use super::*;
    use crate::console;
    use std::collections::HashMap;

    #[derive(Default)]
    pub(crate) struct InMemoryStore {
        programs: HashMap<String, String>,
    }

    impl Store for InMemoryStore {
        fn delete(&mut self, name: &str) -> io::Result<()> {
            match self.programs.remove(name) {
                Some(_) => Ok(()),
                None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
            }
        }

        fn enumerate(&self) -> io::Result<BTreeMap<String, Metadata>> {
            let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875);

            let mut entries = BTreeMap::new();
            for (name, contents) in &self.programs {
                entries.insert(name.clone(), Metadata { date, length: contents.len() as u64 });
            }
            Ok(entries)
        }

        fn get(&self, name: &str) -> io::Result<String> {
            match self.programs.get(name) {
                Some(content) => Ok(content.to_owned()),
                None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
            }
        }

        fn put(&mut self, name: &str, content: &str) -> io::Result<()> {
            self.programs.insert(name.to_owned(), content.to_owned());
            Ok(())
        }
    }

    pub(crate) struct RecordedProgram {
        content: String,
    }

    impl RecordedProgram {
        pub fn new(content: &'static str) -> Self {
            Self { content: content.to_owned() }
        }
    }

    #[async_trait(?Send)]
    impl Program for RecordedProgram {
        async fn edit(&mut self, console: &mut dyn Console) -> io::Result<()> {
            let append = console::read_line(console, "", "").await?;
            self.content.push_str(&append);
            self.content.push('\n');
            Ok(())
        }

        fn load(&mut self, text: &str) {
            self.content = text.to_owned();
        }

        fn text(&self) -> String {
            self.content.clone()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::testutils::*;
    use super::*;
    use crate::console::testutils::*;
    use crate::exec::testutils::*;
    use crate::exec::MachineBuilder;
    use futures_lite::future::block_on;

    #[test]
    fn test_demo_store_overlay_delete() {
        let mut store = InMemoryStore::default();
        store.put("delete.bas", "underlying file").unwrap();
        store.put("keep.bas", "underlying file").unwrap();
        store.put("demo:unknown.bas", "should not be touched").unwrap();
        let store = {
            let mut store = DemoStoreOverlay::new(store);

            store.delete("delete.bas").unwrap();
            assert_eq!(io::ErrorKind::NotFound, store.delete("KEEP.Bas").unwrap_err().kind());

            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.delete("demo:hello.bas").unwrap_err().kind()
            );
            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.delete("DEMO:Hello.BAS").unwrap_err().kind()
            );

            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.delete("demo:unknown.bas").unwrap_err().kind()
            );

            store.unmount()
        };
        assert_eq!(io::ErrorKind::NotFound, store.get("delete.bas").unwrap_err().kind());
        assert_eq!("underlying file", store.get("keep.bas").unwrap());
        assert_eq!(io::ErrorKind::NotFound, store.get("demo:hello.bas").unwrap_err().kind());
        assert_eq!("should not be touched", store.get("demo:unknown.bas").unwrap());
    }

    #[test]
    fn test_demo_store_overlay_enumerate() {
        let mut store = InMemoryStore::default();
        store.put("under.bas", "underlying file").unwrap();
        store.put("demo:hidden.bas", "will be clobbered").unwrap();
        let store = DemoStoreOverlay::new(store);

        let entries = store.enumerate().unwrap();
        assert!(entries.contains_key("under.bas"));
        assert!(entries.contains_key("DEMO:GUESS.BAS"));
        assert!(entries.contains_key("DEMO:HELLO.BAS"));
        assert!(entries.contains_key("DEMO:TOUR.BAS"));
        assert!(!entries.contains_key("DEMO:HIDDEN.BAS"));
        assert!(!entries.contains_key("demo:hidden.bas"));
    }

    #[test]
    fn test_demo_store_overlay_get() {
        let mut store = InMemoryStore::default();
        store.put("under.bas", "underlying file").unwrap();
        store.put("demo:hidden.bas", "will be clobbered").unwrap();
        let store = DemoStoreOverlay::new(store);

        assert_eq!("underlying file", store.get("under.bas").unwrap());
        assert_eq!(io::ErrorKind::NotFound, store.get("Under.bas").unwrap_err().kind());

        assert_eq!(
            process_demo(include_bytes!("../examples/hello.bas")),
            store.get("demo:hello.bas").unwrap()
        );
        assert_eq!(
            process_demo(include_bytes!("../examples/hello.bas")),
            store.get("Demo:Hello.Bas").unwrap()
        );

        assert_eq!(io::ErrorKind::NotFound, store.get("demo:hidden.bas").unwrap_err().kind());
        assert_eq!(io::ErrorKind::NotFound, store.get("demo:unknown.bas").unwrap_err().kind());
        assert_eq!(io::ErrorKind::NotFound, store.get("unknown.bas").unwrap_err().kind());
    }

    #[test]
    fn test_demo_store_overlay_put() {
        let mut store = InMemoryStore::default();
        store.put("modify.bas", "previous contents").unwrap();
        store.put("avoid.bas", "previous contents").unwrap();
        let store = {
            let mut store = DemoStoreOverlay::new(store);

            store.put("modify.bas", "new contents").unwrap();

            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.put("demo:hello.bas", "").unwrap_err().kind()
            );
            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.put("DEMO:Hello.BAS", "").unwrap_err().kind()
            );

            assert_eq!(
                io::ErrorKind::PermissionDenied,
                store.put("demo:unknown.bas", "").unwrap_err().kind()
            );

            store.unmount()
        };
        assert_eq!(io::ErrorKind::NotFound, store.get("demo:unknown.bas").unwrap_err().kind());
        assert_eq!("new contents", store.get("modify.bas").unwrap());
        assert_eq!("previous contents", store.get("avoid.bas").unwrap());
    }

    /// Runs the `input` code on a new machine that stores programs in `store` and verifies its
    /// output.
    ///
    /// `golden_in` is a sequence of keys to feed to the commands that request console input.
    ///
    /// `expected_out` is a sequence of expected calls to `PRINT`.
    ///
    /// `exp_program` is the expected state of `program` after execution.
    fn do_ok_test_with_store(
        program: Rc<RefCell<dyn Program>>,
        store: Rc<RefCell<dyn Store>>,
        input: &str,
        golden_in: &'static str,
        expected_out: &'static [&'static str],
        exp_program: &'static str,
    ) {
        let console =
            Rc::from(RefCell::from(MockConsoleBuilder::new().add_input_chars(golden_in).build()));
        let mut machine = MachineBuilder::default()
            .add_commands(all_commands_for(program.clone(), console.clone(), store))
            .build();
        block_on(machine.exec(&mut input.as_bytes())).expect("Execution failed");
        let expected_out: Vec<CapturedOut> =
            expected_out.iter().map(|x| CapturedOut::Print((*x).to_owned())).collect();
        assert_eq!(expected_out, console.borrow().captured_out());
        assert_eq!(exp_program, program.borrow().text());
    }

    /// Same as `do_ok_test_with_store` but with an automatic `store`.
    fn do_ok_test(
        program: Rc<RefCell<dyn Program>>,
        input: &str,
        golden_in: &'static str,
        expected_out: &'static [&'static str],
        exp_program: &'static str,
    ) {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_ok_test_with_store(program, store, input, golden_in, expected_out, exp_program)
    }

    /// Runs the `input` code on a new machine and verifies that it fails with `expected_err`.
    ///
    /// Ensures that this does not touch the console.
    fn do_error_test_with_store(store: Rc<RefCell<dyn Store>>, input: &str, expected_err: &str) {
        let console = Rc::from(RefCell::from(MockConsoleBuilder::new().build()));
        let program = Rc::from(RefCell::from(RecordedProgram::new("")));
        let mut machine = MachineBuilder::default()
            .add_commands(all_commands_for(program, console.clone(), store))
            .build();
        assert_eq!(
            expected_err,
            format!(
                "{}",
                block_on(machine.exec(&mut input.as_bytes())).expect_err("Execution did not fail")
            )
        );
        assert!(console.borrow().captured_out().is_empty());
    }

    /// Same as `do_error_test_with_store` but with an automatic (and inaccessible) `dir`.
    fn do_error_test(input: &str, expected_err: &str) {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_error_test_with_store(store, input, expected_err)
    }

    #[test]
    fn test_del_ok() {
        let mut store = InMemoryStore::default();
        store.put("bar.bas", "").unwrap();
        let store = Rc::from(RefCell::from(store));

        let program = Rc::from(RefCell::from(RecordedProgram::new("Leave me alone")));

        for p in &["foo", "foo.bas"] {
            store.borrow_mut().put("foo.bas", "line 1\n  line 2\n").unwrap();
            do_ok_test_with_store(
                program.clone(),
                store.clone(),
                &("DEL \"".to_owned() + p + "\""),
                "",
                &[],
                "Leave me alone",
            );
        }

        store.borrow().get("bar.bas").unwrap(); // Check that unrelated entries were not touched.
    }

    #[test]
    fn test_del_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("DEL", store.clone());

        do_error_test_with_store(store.clone(), "DEL \"missing-file\"", "Entry not found");

        store.borrow_mut().put("mismatched-extension.bat", "").unwrap();
        do_error_test_with_store(store, "DEL \"mismatched-extension\"", "Entry not found");
    }

    #[test]
    fn test_dir_empty() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_ok_test_with_store(
            Rc::from(RefCell::from(RecordedProgram::new(""))),
            store,
            "DIR",
            "",
            &["", "    Modified              Size    Name", "    0 file(s), 0 bytes", ""],
            "",
        );
    }

    #[test]
    fn test_dir_entries_are_sorted() {
        let mut store = InMemoryStore::default();
        store.put("empty.bas", "").unwrap();
        store.put("some other file.bas", "not empty\n").unwrap();
        store.put("00AAA.BAS", "first\nfile\n").unwrap();
        store.put("not a bas.txt", "").unwrap();
        let store = Rc::from(RefCell::from(store));

        do_ok_test_with_store(
            Rc::from(RefCell::from(RecordedProgram::new(""))),
            store,
            "DIR",
            "",
            &[
                "",
                "    Modified              Size    Name",
                "    2020-05-06 09:37        11    00AAA.BAS",
                "    2020-05-06 09:37         0    empty.bas",
                "    2020-05-06 09:37         0    not a bas.txt",
                "    2020-05-06 09:37        10    some other file.bas",
                "",
                "    4 file(s), 21 bytes",
                "",
            ],
            "",
        );
    }

    #[test]
    fn test_dir_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_error_test_with_store(store, "DIR 2", "DIR takes no arguments");
    }

    #[test]
    fn test_edit_ok() {
        do_ok_test(
            Rc::from(RefCell::from(RecordedProgram::new("previous\n"))),
            "EDIT",
            "new line\n",
            &[],
            "previous\nnew line\n",
        );
    }

    #[test]
    fn test_edit_errors() {
        do_error_test("EDIT 1", "EDIT takes no arguments");
    }

    #[test]
    fn test_load_ok() {
        let mut store = InMemoryStore::default();
        store.put("foo.bas", "line 1\n\n  line 2\n").unwrap();
        store.put("foo.bak", "").unwrap();
        store.put("BAR.BAS", "line 1\n\n  line 2\n").unwrap();
        store.put("Baz.bas", "line 1\n\n  line 2\n").unwrap();
        let store = Rc::from(RefCell::from(store));

        for p in &["foo", "foo.bas", "BAR", "BAR.BAS", "Baz"] {
            do_ok_test_with_store(
                Rc::from(RefCell::from(RecordedProgram::new(""))),
                store.clone(),
                &("LOAD \"".to_owned() + p + "\""),
                "",
                &[],
                "line 1\n\n  line 2\n",
            );
        }
    }

    /// Checks errors that should be handled the same way by `LOAD` and `SAVE`.
    fn check_load_save_common_errors(cmd: &str, store: Rc<RefCell<dyn Store>>) {
        do_error_test_with_store(store.clone(), &cmd, &format!("{} requires a filename", cmd));
        do_error_test_with_store(
            store.clone(),
            &format!("{} 3", cmd),
            &format!("{} requires a string as the filename", cmd),
        );

        let mut non_basenames = vec!["./foo.bas", "a/b.bas", "a/b"];
        if cfg!(target_os = "windows") {
            non_basenames.push("c:foo.bas");
        }
        for p in non_basenames.as_slice() {
            do_error_test_with_store(
                store.clone(),
                &format!("{} \"{}\"", cmd, p),
                "Filename must be a single path component",
            );
        }

        for p in &["foo.bak", "foo.ba", "foo.basic"] {
            do_error_test_with_store(
                store.clone(),
                &format!("{} \"{}\"", cmd, p),
                "Invalid filename extension",
            );
        }
    }

    #[test]
    fn test_load_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("LOAD", store.clone());

        do_error_test_with_store(store.clone(), "LOAD \"missing-file\"", "Entry not found");

        store.borrow_mut().put("mismatched-extension.bat", "").unwrap();
        do_error_test_with_store(store, "LOAD \"mismatched-extension\"", "Entry not found");
    }

    #[test]
    fn test_new_nothing() {
        do_ok_test(Rc::from(RefCell::from(RecordedProgram::new(""))), "NEW", "", &[], "");
    }

    #[test]
    fn test_new_clears_program_and_variables() {
        let program = Rc::from(RefCell::from(RecordedProgram::new("some stuff")));

        let mut machine =
            MachineBuilder::default().add_command(NewCommand::new(program.clone())).build();

        block_on(machine.exec(&mut b"NEW".as_ref())).unwrap();
        assert!(program.borrow().text().is_empty());
        assert!(machine.get_vars().is_empty());
        // TODO(jmmv): If we get user-supplied functions, we need to check here that they were
        // cleared too.
    }

    #[test]
    fn test_new_errors() {
        do_error_test("NEW 10", "NEW takes no arguments");
    }

    #[test]
    fn test_run_nothing() {
        do_ok_test(Rc::from(RefCell::from(RecordedProgram::new(""))), "RUN", "", &[], "");
    }

    #[test]
    fn test_run_something_that_shares_state() {
        let program = Rc::from(RefCell::from(RecordedProgram::new("OUT var\nvar = var + 1")));

        let captured_out = Rc::from(RefCell::from(vec![]));
        let mut machine = MachineBuilder::default()
            .add_command(OutCommand::new(captured_out.clone()))
            .add_command(RunCommand::new(program))
            .build();

        block_on(machine.exec(&mut b"var = 7: RUN".as_ref())).unwrap();
        assert_eq!(&["7"], captured_out.borrow().as_slice());

        captured_out.borrow_mut().clear();
        block_on(machine.exec(&mut b"RUN".as_ref())).unwrap();
        assert_eq!(&["8"], captured_out.borrow().as_slice());
    }

    #[test]
    fn test_run_errors() {
        do_error_test("RUN 10", "RUN takes no arguments");
    }

    #[test]
    fn test_save_ok() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));

        let program = Rc::from(RefCell::from(RecordedProgram::new("line 1\n  line 2\n")));

        for p in &["first", "second.bas", "THIRD", "FOURTH.BAS", "Fifth"] {
            do_ok_test_with_store(
                program.clone(),
                store.clone(),
                &("SAVE \"".to_owned() + p + "\""),
                "",
                &[],
                "line 1\n  line 2\n",
            );
        }

        for p in &["first.bas", "second.bas", "THIRD.BAS", "FOURTH.BAS", "Fifth.bas"] {
            let content = store.borrow().get(p).unwrap();
            assert_eq!(content, "line 1\n  line 2\n");
        }
    }

    #[test]
    fn test_save_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("SAVE", store);
    }
}