diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs
index e6734c7f2..cde43e78c 100644
--- a/services/shellchat/src/cmds.rs
+++ b/services/shellchat/src/cmds.rs
@@ -1,18 +1,19 @@
-mod echo;
-use echo::*;
-
use xous::String;
use core::fmt::Write;
-pub trait ShellCmdApi {
+pub trait ShellCmdApi<'a> {
// checks if the command matches the current verb in question
fn matches(&self, verb: &str) -> bool;
// called to process the command with the remainder of the string attached
- fn process(&self, rest: String::<1024>) -> Result>, xous::Error>;
+ fn process(&mut self, rest: String::<1024>) -> Result >, xous::Error>;
// returns my verb
- fn verb(&self) -> &str;
+ fn verb(&self) -> &'static str;
}
+/// extract the first token, as delimited by spaces
+/// modifies the incoming line by removing the token and returning the remainder
+/// returns the found token
+/// note: we don't have split() because of nostd
pub fn tokenize(line: &mut String::<1024>) -> Option> {
let mut token = String::<1024>::new();
let mut retline = String::<1024>::new();
@@ -43,33 +44,68 @@ pub fn tokenize(line: &mut String::<1024>) -> Option> {
}
}
+mod echo;
+use echo::*;
+mod test;
+use test::*;
+
#[derive(Debug)]
pub struct CmdEnv {
+ // placeholder for any environment variables relevant to interpreting commands
}
impl CmdEnv {
pub fn new() -> CmdEnv {
- CmdEnv {
-
- }
+ CmdEnv { }
}
pub fn dispatch(&self, cmdline: &mut String::<1024>) -> Result>, xous::Error> {
let mut ret = String::<1024>::new();
- let echo = Echo {};
+ /*
+ To add a new command:
+ - ensure that the command implements the ShellCmdApi
+ - mod/use the new command (above)
+ - create a mutable version of the command's data structure
+ - add the structure to the array all_commands below
+ */
+ let mut echo_cmd = Echo {};
+ let mut test_cmd = Test::new();
+ let all_commands: &mut [&mut dyn ShellCmdApi] = &mut [
+ &mut echo_cmd,
+ &mut test_cmd,
+ ];
let maybe_verb = tokenize(cmdline);
+ let mut cmd_ret: Result >, xous::Error> = Ok(None);
if let Some(verb_string) = maybe_verb {
let verb = verb_string.to_str();
- if echo.matches(verb) {
- echo.process(*cmdline)
- } else {
- write!(ret, "Commands: ").unwrap();
- ret.append(echo.verb())?;
+ // search through the list of commands linearly until one matches,
+ // then run it.
+ let mut match_found = false;
+ for cmd in all_commands.iter_mut() {
+ if cmd.matches(verb) {
+ match_found = true;
+ cmd_ret = cmd.process(*cmdline);
+ };
+ }
+
+ // if none match, create a list of available commands
+ if !match_found {
+ let mut first = true;
+ write!(ret, "Commands: ").unwrap();
+ for cmd in all_commands.iter() {
+ if !first {
+ ret.append(", ")?;
+ first = false;
+ }
+ ret.append(cmd.verb())?;
+ }
Ok(Some(ret))
+ } else {
+ cmd_ret
}
} else {
Ok(None)
diff --git a/services/shellchat/src/cmds/echo.rs b/services/shellchat/src/cmds/echo.rs
index 8365692c8..51ca03314 100644
--- a/services/shellchat/src/cmds/echo.rs
+++ b/services/shellchat/src/cmds/echo.rs
@@ -6,7 +6,7 @@ pub struct Echo {
const VERB: &str = "echo";
-impl ShellCmdApi for Echo {
+impl<'a> ShellCmdApi<'a> for Echo {
fn matches(&self, verb: &str) -> bool {
if verb == VERB {
true
@@ -15,11 +15,11 @@ impl ShellCmdApi for Echo {
}
}
- fn process(&self, rest: String::<1024>) -> Result >, xous::Error> {
+ fn process(&mut self, rest: String::<1024>) -> Result >, xous::Error> {
Ok(Some(rest))
}
- fn verb(&self) -> &str {
+ fn verb(&self) -> &'static str {
VERB
}
}
\ No newline at end of file
diff --git a/services/shellchat/src/cmds/test.rs b/services/shellchat/src/cmds/test.rs
new file mode 100644
index 000000000..3ffd6dd5e
--- /dev/null
+++ b/services/shellchat/src/cmds/test.rs
@@ -0,0 +1,38 @@
+use crate::ShellCmdApi;
+use xous::String;
+
+pub struct Test {
+ state: u32
+}
+impl Test {
+ pub fn new() -> Self {
+ Test {
+ state: 0
+ }
+ }
+}
+
+const VERB: &str = "test";
+
+impl<'a> ShellCmdApi<'a> for Test {
+ fn matches(&self, verb: &str) -> bool {
+ if verb == VERB {
+ true
+ } else {
+ false
+ }
+ }
+
+ fn process(&mut self, _rest: String::<1024>) -> Result >, xous::Error> {
+ use core::fmt::Write;
+
+ self.state += 1;
+ let mut ret = String::<1024>::new();
+ write!(ret, "Test has run {} times.", self.state).unwrap();
+ Ok(Some(ret))
+ }
+
+ fn verb(&self) -> &'static str {
+ VERB
+ }
+}
\ No newline at end of file