From f146743061ba8170569bf18518202df9a43c09f3 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Fri, 26 Jan 2024 12:28:39 +0100 Subject: Clean up part of the code --- shell/src/main.rs | 158 +++++++++++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 79 deletions(-) (limited to 'shell/src') diff --git a/shell/src/main.rs b/shell/src/main.rs index 4901e7f..4a98828 100644 --- a/shell/src/main.rs +++ b/shell/src/main.rs @@ -1,84 +1,5 @@ use std::{os::unix::process::CommandExt, process::ExitCode}; -fn parse_sq(s: &str) -> Option<(String, &str)> { - #[derive(PartialEq, Eq)] - enum SqState { - Quoted, - Unquoted { may_escape: bool }, - UnquotedEscaped, - } - - let mut result = String::new(); - let mut state = SqState::Unquoted { may_escape: false }; - let mut remaining = ""; - for (i, c) in s.char_indices() { - match state { - SqState::Unquoted { may_escape: false } => { - if c != '\'' { - return None; - } - state = SqState::Quoted - } - SqState::Quoted => { - if c == '\'' { - state = SqState::Unquoted { may_escape: true }; - continue; - } - result.push(c); - } - SqState::Unquoted { may_escape: true } => { - if is_posix_space(c) { - remaining = &s[i..]; - break; - } - if c != '\\' { - return None; - } - state = SqState::UnquotedEscaped; - } - SqState::UnquotedEscaped => { - if c != '\\' && c != '!' { - return None; - } - result.push(c); - state = SqState::Unquoted { may_escape: false }; - } - } - } - - if state != (SqState::Unquoted { may_escape: true }) { - return None; - } - Some((result, remaining)) -} - -fn parse_cmd(mut cmd: &str) -> Option> { - let mut args = Vec::::new(); - - cmd = cmd.trim_matches(is_posix_space); - while !cmd.is_empty() { - if cmd.starts_with('\'') { - let (arg, remaining) = parse_sq(cmd)?; - args.push(arg); - cmd = remaining.trim_start_matches(is_posix_space); - } else if let Some((arg, remaining)) = cmd.split_once(is_posix_space) { - args.push(arg.to_owned()); - cmd = remaining.trim_start_matches(is_posix_space); - } else { - args.push(cmd.to_owned()); - cmd = ""; - } - } - - Some(args) -} - -fn is_posix_space(c: char) -> bool { - // Form feed: 0x0c - // Vertical tab: 0x0b - c == ' ' || c == '\x0c' || c == '\n' || c == '\r' || c == '\t' || c == '\x0b' -} - fn main() -> ExitCode { let bad_usage = ExitCode::from(2); @@ -141,3 +62,82 @@ fn main() -> ExitCode { eprintln!("Error: {e}"); ExitCode::FAILURE } + +fn parse_cmd(mut cmd: &str) -> Option> { + let mut args = Vec::::new(); + + cmd = cmd.trim_matches(is_posix_space); + while !cmd.is_empty() { + if cmd.starts_with('\'') { + let (arg, remaining) = parse_sq(cmd)?; + args.push(arg); + cmd = remaining.trim_start_matches(is_posix_space); + } else if let Some((arg, remaining)) = cmd.split_once(is_posix_space) { + args.push(arg.to_owned()); + cmd = remaining.trim_start_matches(is_posix_space); + } else { + args.push(cmd.to_owned()); + cmd = ""; + } + } + + Some(args) +} + +fn is_posix_space(c: char) -> bool { + // Form feed: 0x0c + // Vertical tab: 0x0b + c == ' ' || c == '\x0c' || c == '\n' || c == '\r' || c == '\t' || c == '\x0b' +} + +fn parse_sq(s: &str) -> Option<(String, &str)> { + #[derive(PartialEq, Eq)] + enum SqState { + Quoted, + Unquoted { may_escape: bool }, + UnquotedEscaped, + } + + let mut result = String::new(); + let mut state = SqState::Unquoted { may_escape: false }; + let mut remaining = ""; + for (i, c) in s.char_indices() { + match state { + SqState::Unquoted { may_escape: false } => { + if c != '\'' { + return None; + } + state = SqState::Quoted + } + SqState::Quoted => { + if c == '\'' { + state = SqState::Unquoted { may_escape: true }; + continue; + } + result.push(c); + } + SqState::Unquoted { may_escape: true } => { + if is_posix_space(c) { + remaining = &s[i..]; + break; + } + if c != '\\' { + return None; + } + state = SqState::UnquotedEscaped; + } + SqState::UnquotedEscaped => { + if c != '\\' && c != '!' { + return None; + } + result.push(c); + state = SqState::Unquoted { may_escape: false }; + } + } + } + + if state != (SqState::Unquoted { may_escape: true }) { + return None; + } + Some((result, remaining)) +} -- cgit v1.2.3