axvisor 0.5.4

A lightweight type-1 hypervisor based on ArceOS
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
// Copyright 2025 The Axvisor Team
//
// 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.

mod base;
mod history;
mod vm;

pub use base::*;
pub use history::*;
pub use vm::*;

use std::io::prelude::*;
use std::string::String;
use std::vec::Vec;
use std::{collections::BTreeMap, string::ToString};
use std::{print, println};

lazy_static::lazy_static! {
    pub static ref COMMAND_TREE: BTreeMap<String, CommandNode> = build_command_tree();
}

#[derive(Debug, Clone)]
pub struct CommandNode {
    handler: Option<fn(&ParsedCommand)>,
    subcommands: BTreeMap<String, CommandNode>,
    description: &'static str,
    usage: Option<&'static str>,
    #[allow(dead_code)]
    log_level: log::LevelFilter,
    options: Vec<OptionDef>,
    flags: Vec<FlagDef>,
}

#[derive(Debug, Clone)]
pub struct OptionDef {
    name: &'static str,
    short: Option<char>,
    long: Option<&'static str>,
    description: &'static str,
    required: bool,
}

#[derive(Debug, Clone)]
pub struct FlagDef {
    name: &'static str,
    short: Option<char>,
    long: Option<&'static str>,
    description: &'static str,
}

#[derive(Debug, Clone)]
pub struct ParsedCommand {
    pub command_path: Vec<String>,
    pub options: BTreeMap<String, String>,
    pub flags: BTreeMap<String, bool>,
    pub positional_args: Vec<String>,
}

#[derive(Debug)]
pub enum ParseError {
    UnknownCommand(String),
    UnknownOption(String),
    MissingValue(String),
    MissingRequiredOption(String),
    NoHandler(String),
}

impl CommandNode {
    pub fn new(description: &'static str) -> Self {
        Self {
            handler: None,
            subcommands: BTreeMap::new(),
            description,
            usage: None,
            log_level: log::LevelFilter::Off,
            options: Vec::new(),
            flags: Vec::new(),
        }
    }

    pub fn with_handler(mut self, handler: fn(&ParsedCommand)) -> Self {
        self.handler = Some(handler);
        self
    }

    pub fn with_usage(mut self, usage: &'static str) -> Self {
        self.usage = Some(usage);
        self
    }

    #[allow(dead_code)]
    pub fn with_log_level(mut self, level: log::LevelFilter) -> Self {
        self.log_level = level;
        self
    }

    pub fn with_option(mut self, option: OptionDef) -> Self {
        self.options.push(option);
        self
    }

    pub fn with_flag(mut self, flag: FlagDef) -> Self {
        self.flags.push(flag);
        self
    }

    pub fn add_subcommand<S: Into<String>>(mut self, name: S, node: CommandNode) -> Self {
        self.subcommands.insert(name.into(), node);
        self
    }
}

impl OptionDef {
    pub fn new(name: &'static str, description: &'static str) -> Self {
        Self {
            name,
            short: None,
            long: None,
            description,
            required: false,
        }
    }

    #[allow(dead_code)]
    pub fn with_short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    pub fn with_long(mut self, long: &'static str) -> Self {
        self.long = Some(long);
        self
    }

    #[allow(dead_code)]
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }
}

impl FlagDef {
    pub fn new(name: &'static str, description: &'static str) -> Self {
        Self {
            name,
            short: None,
            long: None,
            description,
        }
    }

    pub fn with_short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }

    pub fn with_long(mut self, long: &'static str) -> Self {
        self.long = Some(long);
        self
    }
}

// Command Parser
pub struct CommandParser;

impl CommandParser {
    pub fn parse(input: &str) -> Result<ParsedCommand, ParseError> {
        let tokens = Self::tokenize(input);
        if tokens.is_empty() {
            return Err(ParseError::UnknownCommand("empty command".to_string()));
        }

        // Find the command path
        let (command_path, command_node, remaining_tokens) = Self::find_command(&tokens)?;

        // Parse the arguments
        let (options, flags, positional_args) = Self::parse_args(remaining_tokens, command_node)?;

        // Validate required options
        Self::validate_required_options(command_node, &options)?;

        Ok(ParsedCommand {
            command_path,
            options,
            flags,
            positional_args,
        })
    }

    fn tokenize(input: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let mut current_token = String::new();
        let mut in_quotes = false;
        let mut escape_next = false;

        for ch in input.chars() {
            if escape_next {
                current_token.push(ch);
                escape_next = false;
            } else if ch == '\\' {
                escape_next = true;
            } else if ch == '"' {
                in_quotes = !in_quotes;
            } else if ch.is_whitespace() && !in_quotes {
                if !current_token.is_empty() {
                    tokens.push(current_token.clone());
                    current_token.clear();
                }
            } else {
                current_token.push(ch);
            }
        }

        if !current_token.is_empty() {
            tokens.push(current_token);
        }

        tokens
    }

    fn find_command(
        tokens: &[String],
    ) -> Result<(Vec<String>, &CommandNode, &[String]), ParseError> {
        let mut current_node = COMMAND_TREE
            .get(&tokens[0])
            .ok_or_else(|| ParseError::UnknownCommand(tokens[0].clone()))?;

        let mut command_path = vec![tokens[0].clone()];
        let mut token_index = 1;

        // Traverse to find the deepest command node
        while token_index < tokens.len() {
            if let Some(subcommand) = current_node.subcommands.get(&tokens[token_index]) {
                current_node = subcommand;
                command_path.push(tokens[token_index].clone());
                token_index += 1;
            } else {
                break;
            }
        }

        Ok((command_path, current_node, &tokens[token_index..]))
    }

    #[allow(clippy::type_complexity)]
    fn parse_args(
        tokens: &[String],
        command_node: &CommandNode,
    ) -> Result<
        (
            BTreeMap<String, String>,
            BTreeMap<String, bool>,
            Vec<String>,
        ),
        ParseError,
    > {
        let mut options = BTreeMap::new();
        let mut flags = BTreeMap::new();
        let mut positional_args = Vec::new();
        let mut i = 0;

        while i < tokens.len() {
            let token = &tokens[i];

            if let Some(name) = token.strip_prefix("--") {
                // Long options/flags
                if let Some(eq_pos) = name.find('=') {
                    // --option=value format
                    let (opt_name, value) = name.split_at(eq_pos);
                    let value = &value[1..]; // Skip '='
                    if Self::is_option(opt_name, command_node) {
                        options.insert(opt_name.to_string(), value.to_string());
                    } else {
                        return Err(ParseError::UnknownOption(format!("--{opt_name}")));
                    }
                } else if Self::is_flag(name, command_node) {
                    flags.insert(name.to_string(), true);
                } else if Self::is_option(name, command_node) {
                    // --option value format
                    if i + 1 >= tokens.len() {
                        return Err(ParseError::MissingValue(format!("--{name}")));
                    }
                    options.insert(name.to_string(), tokens[i + 1].clone());
                    i += 1; // Skip value
                } else {
                    return Err(ParseError::UnknownOption(format!("--{name}")));
                }
            } else if token.starts_with('-') && token.len() > 1 {
                // Short options/flags
                let chars: Vec<char> = token[1..].chars().collect();
                for (j, &ch) in chars.iter().enumerate() {
                    if Self::is_short_flag(ch, command_node) {
                        flags.insert(
                            Self::get_flag_name_by_short(ch, command_node)
                                .unwrap()
                                .to_string(),
                            true,
                        );
                    } else if Self::is_short_option(ch, command_node) {
                        let opt_name = Self::get_option_name_by_short(ch, command_node).unwrap();
                        if j == chars.len() - 1 && i + 1 < tokens.len() {
                            // Last character and there is a next token as value
                            options.insert(opt_name.to_string(), tokens[i + 1].clone());
                            i += 1; // Skip value
                        } else {
                            return Err(ParseError::MissingValue(format!("-{ch}")));
                        }
                    } else {
                        return Err(ParseError::UnknownOption(format!("-{ch}")));
                    }
                }
            } else {
                // Positional arguments
                positional_args.push(token.clone());
            }
            i += 1;
        }

        Ok((options, flags, positional_args))
    }

    fn is_option(name: &str, node: &CommandNode) -> bool {
        node.options
            .iter()
            .any(|opt| (opt.long == Some(name)) || opt.name == name)
    }

    fn is_flag(name: &str, node: &CommandNode) -> bool {
        node.flags
            .iter()
            .any(|flag| (flag.long == Some(name)) || flag.name == name)
    }

    fn is_short_option(ch: char, node: &CommandNode) -> bool {
        node.options.iter().any(|opt| opt.short == Some(ch))
    }

    fn is_short_flag(ch: char, node: &CommandNode) -> bool {
        node.flags.iter().any(|flag| flag.short == Some(ch))
    }

    fn get_option_name_by_short(ch: char, node: &CommandNode) -> Option<&str> {
        node.options
            .iter()
            .find(|opt| opt.short == Some(ch))
            .map(|opt| opt.name)
    }

    fn get_flag_name_by_short(ch: char, node: &CommandNode) -> Option<&str> {
        node.flags
            .iter()
            .find(|flag| flag.short == Some(ch))
            .map(|flag| flag.name)
    }

    fn validate_required_options(
        node: &CommandNode,
        options: &BTreeMap<String, String>,
    ) -> Result<(), ParseError> {
        for option in &node.options {
            if option.required && !options.contains_key(option.name) {
                return Err(ParseError::MissingRequiredOption(option.name.to_string()));
            }
        }
        Ok(())
    }
}

// Command execution function
pub fn execute_command(input: &str) -> Result<(), ParseError> {
    let parsed = CommandParser::parse(input)?;

    // Find the corresponding command node
    let mut current_node = COMMAND_TREE.get(&parsed.command_path[0]).unwrap();
    for cmd in &parsed.command_path[1..] {
        current_node = current_node.subcommands.get(cmd).unwrap();
    }

    // Execute the command
    if let Some(handler) = current_node.handler {
        handler(&parsed);
        Ok(())
    } else {
        Err(ParseError::NoHandler(parsed.command_path.join(" ")))
    }
}

// Build command tree
fn build_command_tree() -> BTreeMap<String, CommandNode> {
    let mut tree = BTreeMap::new();

    build_base_cmd(&mut tree);
    build_vm_cmd(&mut tree);

    tree
}

// Helper function: Display command help
pub fn show_help(command_path: &[String]) -> Result<(), ParseError> {
    let mut current_node = COMMAND_TREE
        .get(&command_path[0])
        .ok_or_else(|| ParseError::UnknownCommand(command_path[0].clone()))?;

    for cmd in &command_path[1..] {
        current_node = current_node
            .subcommands
            .get(cmd)
            .ok_or_else(|| ParseError::UnknownCommand(cmd.clone()))?;
    }

    println!("Command: {}", command_path.join(" "));
    println!("Description: {}", current_node.description);

    if let Some(usage) = current_node.usage {
        println!("Usage: {}", usage);
    }

    if !current_node.options.is_empty() {
        println!("\nOptions:");
        for option in &current_node.options {
            let mut opt_str = String::new();
            if let Some(short) = option.short {
                opt_str.push_str(&format!("-{short}"));
            }
            if let Some(long) = option.long {
                if !opt_str.is_empty() {
                    opt_str.push_str(", ");
                }
                opt_str.push_str(&format!("--{long}"));
            }
            if opt_str.is_empty() {
                opt_str = option.name.to_string();
            }

            let required_str = if option.required { " (required)" } else { "" };
            println!("  {:<20} {}{}", opt_str, option.description, required_str);
        }
    }

    if !current_node.flags.is_empty() {
        println!("\nFlags:");
        for flag in &current_node.flags {
            let mut flag_str = String::new();
            if let Some(short) = flag.short {
                flag_str.push_str(&format!("-{short}"));
            }
            if let Some(long) = flag.long {
                if !flag_str.is_empty() {
                    flag_str.push_str(", ");
                }
                flag_str.push_str(&format!("--{long}"));
            }
            if flag_str.is_empty() {
                flag_str = flag.name.to_string();
            }

            println!("  {:<20} {}", flag_str, flag.description);
        }
    }

    if !current_node.subcommands.is_empty() {
        println!("\nSubcommands:");
        for (name, node) in &current_node.subcommands {
            println!("  {:<20} {}", name, node.description);
        }
    }

    Ok(())
}

pub fn print_prompt() {
    #[cfg(feature = "fs")]
    print!("axvisor:{}$ ", std::env::current_dir().unwrap());
    #[cfg(not(feature = "fs"))]
    print!("axvisor:$ ");
    std::io::stdout().flush().unwrap();
}

pub fn run_cmd_bytes(cmd_bytes: &[u8]) {
    match str::from_utf8(cmd_bytes) {
        Ok(cmd_str) => {
            let trimmed = cmd_str.trim();
            if trimmed.is_empty() {
                return;
            }

            match execute_command(trimmed) {
                Ok(_) => {
                    // Command executed successfully
                }
                Err(ParseError::UnknownCommand(cmd)) => {
                    println!("Error: Unknown command '{}'", cmd);
                    println!("Type 'help' to see available commands");
                }
                Err(ParseError::UnknownOption(opt)) => {
                    println!("Error: Unknown option '{}'", opt);
                }
                Err(ParseError::MissingValue(opt)) => {
                    println!("Error: Option '{}' is missing a value", opt);
                }
                Err(ParseError::MissingRequiredOption(opt)) => {
                    println!("Error: Missing required option '{}'", opt);
                }
                Err(ParseError::NoHandler(cmd)) => {
                    println!("Error: Command '{}' has no handler function", cmd);
                }
            }
        }
        Err(_) => {
            println!("Error: Input contains invalid UTF-8 characters");
        }
    }
}

// Built-in command handler
pub fn handle_builtin_commands(input: &str) -> bool {
    match input.trim() {
        "help" => {
            show_available_commands();
            true
        }
        "exit" | "quit" => {
            println!("Goodbye!");
            std::process::exit(0);
        }
        "clear" => {
            print!("\x1b[2J\x1b[H"); // ANSI clear screen sequence
            std::io::stdout().flush().unwrap();
            true
        }
        _ if input.starts_with("help ") => {
            let cmd_parts: Vec<String> = input[5..]
                .split_whitespace()
                .map(|s| s.to_string())
                .collect();
            if let Err(e) = show_help(&cmd_parts) {
                println!("Error: {:?}", e);
            }
            true
        }
        _ => false,
    }
}

pub fn show_available_commands() {
    println!("ArceOS Shell - Available Commands:");
    println!();

    // Display all top-level commands
    for (name, node) in COMMAND_TREE.iter() {
        println!("  {:<15} {}", name, node.description);

        // Display subcommands
        if !node.subcommands.is_empty() {
            for (sub_name, sub_node) in &node.subcommands {
                println!("    {:<13} {}", sub_name, sub_node.description);
            }
        }
    }

    println!();
    println!("Built-in Commands:");
    println!("  help            Show help information");
    println!("  help <command>  Show help for a specific command");
    println!("  clear           Clear the screen");
    println!("  exit/quit       Exit the shell");
    println!();
    println!("Tip: Use 'help <command>' to see detailed usage of a command");
}