interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-kernel / src / shell.rs

164 lines · 4777 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Bounded command parsing and dispatch for the early line shell.
17
18use crate::report::ReportBuf;
19use core::fmt::Write;
20
21pub const LINE_CAPACITY: usize = 64;
22pub const TOKEN_CAPACITY: usize = 16;
23
24#[derive(Clone, Copy, Debug, Eq, PartialEq)]
25pub enum Command<'a> {
26 Help,
27 Mem,
28 El,
29 Report,
30 Uptime,
31 Exit,
32 Unknown(&'a str),
33}
34
35#[derive(Clone, Copy, Debug, Eq, PartialEq)]
36pub struct ParsedLine<'a> {
37 pub command: Command<'a>,
38 pub arg: Option<&'a str>,
39}
40
41/// Parse one whitespace-delimited command and at most one bounded argument.
42pub fn parse_line(line: &str) -> Option<ParsedLine<'_>> {
43 if line.len() > LINE_CAPACITY {
44 return None;
45 }
46 let mut words = line.split_ascii_whitespace();
47 let first = words.next()?;
48 if first.len() > TOKEN_CAPACITY {
49 return None;
50 }
51 let arg = words.next();
52 if arg.is_some_and(|s| s.len() > TOKEN_CAPACITY) || words.next().is_some() {
53 return None;
54 }
55 let command = match first {
56 "help" => Command::Help,
57 "mem" => Command::Mem,
58 "el" => Command::El,
59 "report" => Command::Report,
60 "uptime" => Command::Uptime,
61 "exit" => Command::Exit,
62 _ => Command::Unknown(first),
63 };
64 Some(ParsedLine { command, arg })
65}
66
67pub struct ShellContext<'a> {
68 pub conventional_memory_kb: u64,
69 pub exception_level: u8,
70 pub report: &'a str,
71 pub uptime_ms: u64,
72}
73
74/// Format a command result in a fixed-capacity buffer. Invalid/oversized input
75/// produces a bounded diagnostic and never allocates.
76pub fn dispatch(line: &str, context: &ShellContext<'_>) -> ReportBuf<2048> {
77 let mut out = ReportBuf::new();
78 match parse_line(line) {
79 None => {
80 let _ = writeln!(out, "invalid command line");
81 }
82 Some(ParsedLine {
83 command: Command::Help,
84 ..
85 }) => {
86 let _ = writeln!(out, "commands: help mem el report uptime exit");
87 }
88 Some(ParsedLine {
89 command: Command::Mem,
90 ..
91 }) => {
92 let _ = writeln!(
93 out,
94 "conventional_memory_kb: {}",
95 context.conventional_memory_kb
96 );
97 }
98 Some(ParsedLine {
99 command: Command::El,
100 ..
101 }) => {
102 let _ = writeln!(out, "EL{}", context.exception_level);
103 }
104 Some(ParsedLine {
105 command: Command::Report,
106 ..
107 }) => {
108 let _ = write!(out, "{}", context.report);
109 }
110 Some(ParsedLine {
111 command: Command::Uptime,
112 ..
113 }) => {
114 let _ = writeln!(out, "uptime_ms: {}", context.uptime_ms);
115 }
116 Some(ParsedLine {
117 command: Command::Exit,
118 ..
119 }) => {}
120 Some(ParsedLine {
121 command: Command::Unknown(name),
122 ..
123 }) => {
124 let _ = writeln!(out, "unknown command: {name}");
125 }
126 }
127 out
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn parses_commands_and_bounded_arguments() {
136 assert_eq!(
137 parse_line("mem"),
138 Some(ParsedLine {
139 command: Command::Mem,
140 arg: None
141 })
142 );
143 assert_eq!(parse_line("unknown abc").unwrap().arg, Some("abc"));
144 assert!(parse_line(&"x".repeat(LINE_CAPACITY + 1)).is_none());
145 assert!(parse_line("x 12345678901234567").is_none());
146 assert!(parse_line("x a b").is_none());
147 }
148
149 #[test]
150 fn dispatches_report_and_diagnostics() {
151 let ctx = ShellContext {
152 conventional_memory_kb: 42,
153 exception_level: 1,
154 report: "final report\n",
155 uptime_ms: 7,
156 };
157 assert!(dispatch("el", &ctx).as_str().contains("EL1"));
158 assert!(dispatch("mem", &ctx).as_str().contains("42"));
159 assert!(dispatch("report", &ctx).as_str().contains("final report"));
160 assert!(dispatch("wat", &ctx)
161 .as_str()
162 .contains("unknown command: wat"));
163 }
164}