diff --git a/01_phase_rust_src/chpst/Cargo.toml b/01_phase_rust_src/chpst/Cargo.toml new file mode 100644 index 0000000..5e25448 --- /dev/null +++ b/01_phase_rust_src/chpst/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "chpst" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/01_phase_rust_src/chpst/src/main.rs b/01_phase_rust_src/chpst/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/01_phase_rust_src/chpst/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/rust_src/sv/Cargo.toml b/01_phase_rust_src/sv/Cargo.toml similarity index 100% rename from rust_src/sv/Cargo.toml rename to 01_phase_rust_src/sv/Cargo.toml diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs new file mode 100644 index 0000000..20097a0 --- /dev/null +++ b/01_phase_rust_src/sv/src/main.rs @@ -0,0 +1,17 @@ + +//mod misc; +mod parser; + +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + + // args + let arg_parser: Vec = parser::parse_args(args); + + dbg!(arg_parser); + + + //dbg!(args); +} diff --git a/01_phase_rust_src/sv/src/parser.rs b/01_phase_rust_src/sv/src/parser.rs new file mode 100644 index 0000000..aa2a608 --- /dev/null +++ b/01_phase_rust_src/sv/src/parser.rs @@ -0,0 +1,118 @@ + + +mod misc; + +use std::process::exit; + +pub fn parse_args(args: Vec) -> Vec{ + let mut i: i8 = 0; + + let mut parser: Vec = Vec::new(); + let mut tmpstr: String; + + for arg in args.iter() { + if i == 0 { + i += 1; + } + else if i == 1 { + tmpstr = parse_args_1(arg); + + if tmpstr == "--" { + i += 1; + } + if tmpstr == "-w" { + // i = 4 signifie, take next word, and return to i = 1. + i = 4; + } + + if tmpstr != "--" && tmpstr != "" { + parser.push(tmpstr); + } + else if tmpstr == "" { + // We are actually in phase II. Do exactly the same if it was phase II. + parser.push(phase2(arg)); + i = 3; + } + } + else if i == 2 { + parser.push(phase2(arg)); + i += 1; + } + else if i == 3 { + // phase 3, everything is a service name. + parser.push(arg.to_string()); + } + else if i == 4 { + // 4. push arg and return to stage 1. + parser.push(arg.to_string()); + i = 1; + + } + } + + return parser; +} + +fn phase2(arg: &String) -> String { + let tmpstr: String; + tmpstr = parse_args_2(arg); + + if tmpstr == "INVALID" { + misc::usage(); + exit(100); + + } + + return tmpstr + +} + +pub fn parse_args_1(arg: &String) -> String{ + // Get a string + // Return a vect ('first_char', 'length', 'entry_necessary') + + + let ch1 = arg.chars().next().unwrap(); + + //println!("{}", ch1); + + if ch1 == '-' && arg.len() >= 2{ + return (&arg[..2]).to_string() + } else if ch1 == '-' { + return ch1.to_string() + } + + println!("{}",arg); + return "".to_string() + + +} + + +pub fn parse_args_2(arg: &String) -> String{ + let possible: Vec<&str> = vec!["start", "stop", "reload", "restart", "shutdown", "force-stop", "force-reload", "force-restart", "force-shutdown", "try-restart"]; + let cpossible: Vec = vec!['s', 'u', 'd', 'o', 'p', 'c', 'h', 'a', 'i', 'q', '1', '2', 't', 'k']; + + + + for var in possible.iter() { + if arg == var { + return var.to_string() + } + } + + + let ch1 = arg.chars().next().unwrap(); + + + for var in cpossible.iter() { + if ch1 == *var { + return ch1.to_string() + } + } + + + return "INVALID".to_string() + + +} diff --git a/01_phase_rust_src/sv/src/parser/misc.rs b/01_phase_rust_src/sv/src/parser/misc.rs new file mode 100644 index 0000000..9fcd81d --- /dev/null +++ b/01_phase_rust_src/sv/src/parser/misc.rs @@ -0,0 +1,5 @@ + + +pub fn usage() { + println!("usage: sv [-v] [-w sec] command service ...\n") +} diff --git a/01_phase_rust_src/svlogd/Cargo.toml b/01_phase_rust_src/svlogd/Cargo.toml new file mode 100644 index 0000000..97e19c5 --- /dev/null +++ b/01_phase_rust_src/svlogd/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "svlogd" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/01_phase_rust_src/svlogd/src/main.rs b/01_phase_rust_src/svlogd/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/01_phase_rust_src/svlogd/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/rust_src/sv/src/main.rs b/rust_src/sv/src/main.rs deleted file mode 100644 index f8f094c..0000000 --- a/rust_src/sv/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world! test"); -}