|
| 1 | +//! A simple shell demo for embedded-sdmmc |
| 2 | +//! |
| 3 | +//! Presents a basic command prompt which implements some basic MS-DOS style shell commands. |
| 4 | +
|
| 5 | +use std::io::prelude::*; |
| 6 | + |
| 7 | +use embedded_sdmmc::{Directory, Error, Volume, VolumeIdx, VolumeManager}; |
| 8 | + |
| 9 | +use crate::linux::{Clock, LinuxBlockDevice}; |
| 10 | + |
| 11 | +mod linux; |
| 12 | + |
| 13 | +struct State { |
| 14 | + directory: Directory, |
| 15 | + volume: Volume, |
| 16 | +} |
| 17 | + |
| 18 | +fn main() -> Result<(), Error<std::io::Error>> { |
| 19 | + env_logger::init(); |
| 20 | + let mut args = std::env::args().skip(1); |
| 21 | + let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into()); |
| 22 | + let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false); |
| 23 | + println!("Opening '{filename}'..."); |
| 24 | + let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?; |
| 25 | + let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> = |
| 26 | + VolumeManager::new_with_limits(lbd, Clock, 100); |
| 27 | + let stdin = std::io::stdin(); |
| 28 | + let mut volumes: [Option<State>; 4] = [None, None, None, None]; |
| 29 | + |
| 30 | + let mut current_volume = None; |
| 31 | + for volume_no in 0..4 { |
| 32 | + match volume_mgr.open_volume(VolumeIdx(volume_no)) { |
| 33 | + Ok(volume) => { |
| 34 | + println!("Volume # {}: found", volume_no,); |
| 35 | + match volume_mgr.open_root_dir(volume) { |
| 36 | + Ok(root_dir) => { |
| 37 | + volumes[volume_no] = Some(State { |
| 38 | + directory: root_dir, |
| 39 | + volume, |
| 40 | + }); |
| 41 | + if current_volume.is_none() { |
| 42 | + current_volume = Some(volume_no); |
| 43 | + } |
| 44 | + } |
| 45 | + Err(e) => { |
| 46 | + println!("Failed to open root directory: {e:?}"); |
| 47 | + volume_mgr.close_volume(volume).expect("close volume"); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + Err(e) => { |
| 52 | + println!("Failed to open volume {volume_no}: {e:?}"); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + let Some(mut current_volume) = current_volume else { |
| 58 | + println!("No volumes found in file. Sorry."); |
| 59 | + return Ok(()); |
| 60 | + }; |
| 61 | + |
| 62 | + loop { |
| 63 | + print!("{}:> ", current_volume); |
| 64 | + std::io::stdout().flush().unwrap(); |
| 65 | + let mut line = String::new(); |
| 66 | + stdin.read_line(&mut line)?; |
| 67 | + let line = line.trim(); |
| 68 | + log::info!("Got command: {line:?}"); |
| 69 | + if line == "quit" { |
| 70 | + break; |
| 71 | + } else if line == "help" { |
| 72 | + println!("Commands:"); |
| 73 | + println!("\thelp -> this help text"); |
| 74 | + println!("\t<volume>: -> change volume/partition"); |
| 75 | + println!("\tdir -> do a directory listing"); |
| 76 | + println!("\tquit -> exits the program"); |
| 77 | + } else if line == "0:" { |
| 78 | + current_volume = 0; |
| 79 | + } else if line == "1:" { |
| 80 | + current_volume = 1; |
| 81 | + } else if line == "2:" { |
| 82 | + current_volume = 2; |
| 83 | + } else if line == "3:" { |
| 84 | + current_volume = 3; |
| 85 | + } else if line == "stat" { |
| 86 | + println!("Status:\n{volume_mgr:#?}"); |
| 87 | + } else if line == "dir" { |
| 88 | + let Some(s) = &volumes[current_volume] else { |
| 89 | + println!("That volume isn't available"); |
| 90 | + continue; |
| 91 | + }; |
| 92 | + let r = volume_mgr.iterate_dir(s.directory, |entry| { |
| 93 | + println!( |
| 94 | + "{:12} {:9} {} {}", |
| 95 | + entry.name, |
| 96 | + entry.size, |
| 97 | + entry.mtime, |
| 98 | + if entry.attributes.is_directory() { |
| 99 | + "<DIR>" |
| 100 | + } else { |
| 101 | + "" |
| 102 | + } |
| 103 | + ); |
| 104 | + }); |
| 105 | + handle("iterating directory", r); |
| 106 | + } else if let Some(arg) = line.strip_prefix("cd ") { |
| 107 | + let Some(s) = &mut volumes[current_volume] else { |
| 108 | + println!("This volume isn't available"); |
| 109 | + continue; |
| 110 | + }; |
| 111 | + match volume_mgr.open_dir(s.directory, arg) { |
| 112 | + Ok(d) => { |
| 113 | + let r = volume_mgr.close_dir(s.directory); |
| 114 | + handle("closing old directory", r); |
| 115 | + s.directory = d; |
| 116 | + } |
| 117 | + Err(e) => { |
| 118 | + handle("changing directory", Err(e)); |
| 119 | + } |
| 120 | + } |
| 121 | + } else { |
| 122 | + println!("Unknown command {line:?} - try 'help' for help"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for (idx, s) in volumes.into_iter().enumerate() { |
| 127 | + if let Some(state) = s { |
| 128 | + println!("Unmounting {idx}..."); |
| 129 | + let r = volume_mgr.close_dir(state.directory); |
| 130 | + handle("closing directory", r); |
| 131 | + let r = volume_mgr.close_volume(state.volume); |
| 132 | + handle("closing volume", r); |
| 133 | + println!("Unmounted {idx}!"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + println!("Bye!"); |
| 138 | + Ok(()) |
| 139 | +} |
| 140 | + |
| 141 | +fn handle(operation: &str, r: Result<(), Error<std::io::Error>>) { |
| 142 | + if let Err(e) = r { |
| 143 | + println!("Error {operation}: {e:?}"); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// **************************************************************************** |
| 148 | +// |
| 149 | +// End Of File |
| 150 | +// |
| 151 | +// **************************************************************************** |
0 commit comments