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
//! noshell, a `no_std` argument parser and a shell for constrained systems.
pub use ;
pub use events;
pub use Console;
/// Defines the possible errors that may occur during usage of the crate.
// /// Command trait.
// pub trait Callback {
// /// Execute the callback.
// fn call(&mut self, input: &str);
// }
// /// Command.
// pub struct Command<'a, OutputTy: Write>(pub(crate) TypedCommand<'a, dyn Callback + 'a, OutputTy>);
// pub(crate) struct TypedCommand<'a, CalleeTy: Callback + ?Sized, OutputTy: Write> {
// callee: &'a CalleeTy,
// }
// /// Command.
// pub struct Command(pub(crate) Call)
// /// Callback inner function type.
// pub struct CallbackImpl<'a, CalleeTy, OutputTy>
// where
// CalleeTy: FnMut(&str, &mut OutputTy),
// OutputTy: Write,
// {
// inner: CalleeTy,
// output: &'a mut OutputTy,
// }
// impl<'a, CalleeTy, OutputTy> CallbackImpl<'a, CalleeTy, OutputTy>
// where
// CalleeTy: FnMut(&str, &mut OutputTy),
// OutputTy: Write,
// {
// /// Create a new callback.
// pub fn new(inner: CalleeTy, output: &'a mut OutputTy) -> Self {
// CallbackImpl { inner, output }
// }
// }
// impl<CalleeTy, OutputTy> Callback for CallbackImpl<'_, CalleeTy, OutputTy>
// where
// CalleeTy: FnMut(&str, &mut OutputTy),
// OutputTy: Write,
// {
// fn execute(&mut self, input: &str) {
// (self.inner)(input, self.output)
// }
// }
// /// Parse top-level commands.
// pub fn lookup_in_static_entries<'a>(name: &str) -> Result<&'a mut Command<'static>, Error> {
// let entries: &'static mut [Command<'static>] = unsafe {
// let start = (&NOSHELL_COMMANDS_START as *const u32)
// .cast::<Command<'static>>()
// .cast_mut();
// let end = (&NOSHELL_COMMANDS_END as *const u32)
// .cast::<Command<'static>>()
// .cast_mut();
// let len = (end as usize) - (start as usize);
// core::slice::from_raw_parts_mut(start, len)
// };
// entries
// .iter_mut()
// .find(|entry| name == entry.name)
// .ok_or(Error::CommandNotFound)
// }
// unsafe extern "C" {
// static NOSHELL_COMMANDS_START: u32;
// static NOSHELL_COMMANDS_END: u32;
// }
// /// Character write trait.
// pub trait Write {
// /// Error type.
// type Error;
// /// Write the given data to the underlying byte stream.
// async fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error>;
// }
// /// Character read trait.
// pub trait Read {
// /// Error type;
// type Error;
// /// Read some data from the underlying byte stream.
// async fn read(&self, data: &mut [u8]) -> Result<usize, Self::Error>;
// }
// /// Run the shell.
// pub async fn run<IO: Read + Write>(mut io: IO) -> Result<(), Error> {
// let mut input = [0u8; 1024];
// let mut output = [0u8; 1024];
// let mut cursor = 0;
// loop {
// 'restart: {
// let cmdline = loop {
// match io.read(&mut input[cursor..]).await {
// Ok(len) => {
// if let Some(eol) = input[cursor..cursor + len]
// .iter()
// .position(|&x| x as char == '\n')
// {
// let end = cursor + eol;
// cursor = 0;
// let cmdline = str::from_utf8(&input[..end]).map_err(|_| Error::Utf8)?;
// break cmdline;
// } else {
// cursor += len;
// if cursor >= input.len() {
// cursor = 0;
// break 'restart;
// }
// }
// }
// Err(_) => {
// cursor = 0;
// break 'restart;
// }
// }
// };
// let Some(name) = cmdline.split(" ").next() else {
// break 'restart;
// };
// let Ok(cmd) = lookup_in_static_entries(name) else {
// break 'restart;
// };
// let len = cmd.run(cmdline, &mut output);
// io.write(&output[..len]).await.ok();
// }
// }
// }