From 2291e62e5058b8f36feff5fa2d0709ccd48a29b3 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 17:18:20 +0100 Subject: [PATCH 1/3] create all phase 1 projects --- 01_phase_rust_src/chpst/Cargo.toml | 8 ++++++++ 01_phase_rust_src/chpst/src/main.rs | 3 +++ {rust_src => 01_phase_rust_src}/sv/Cargo.toml | 0 {rust_src => 01_phase_rust_src}/sv/src/main.rs | 0 01_phase_rust_src/svlogd/Cargo.toml | 8 ++++++++ 01_phase_rust_src/svlogd/src/main.rs | 3 +++ 6 files changed, 22 insertions(+) create mode 100644 01_phase_rust_src/chpst/Cargo.toml create mode 100644 01_phase_rust_src/chpst/src/main.rs rename {rust_src => 01_phase_rust_src}/sv/Cargo.toml (100%) rename {rust_src => 01_phase_rust_src}/sv/src/main.rs (100%) create mode 100644 01_phase_rust_src/svlogd/Cargo.toml create mode 100644 01_phase_rust_src/svlogd/src/main.rs 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/rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs similarity index 100% rename from rust_src/sv/src/main.rs rename to 01_phase_rust_src/sv/src/main.rs 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!"); +} -- 2.45.2 From 3c18c49ea017f02f57aa45b3df99cf9579cac671 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 17:34:08 +0100 Subject: [PATCH 2/3] read args given to sv --- 01_phase_rust_src/sv/src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index f8f094c..2d4c1e6 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -1,3 +1,7 @@ + +use std::env; + fn main() { - println!("Hello, world! test"); + let args: Vec = env::args().collect(); + dbg!(args); } -- 2.45.2 From 41b25cd9845007a08547d18f1afd60236de564a6 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 20:12:55 +0100 Subject: [PATCH 3/3] make a parser for sv --- 01_phase_rust_src/sv/src/main.rs | 12 ++- 01_phase_rust_src/sv/src/parser.rs | 118 ++++++++++++++++++++++++ 01_phase_rust_src/sv/src/parser/misc.rs | 5 + 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 01_phase_rust_src/sv/src/parser.rs create mode 100644 01_phase_rust_src/sv/src/parser/misc.rs diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index 2d4c1e6..20097a0 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -1,7 +1,17 @@ +//mod misc; +mod parser; + use std::env; fn main() { let args: Vec = env::args().collect(); - dbg!(args); + + // 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") +} -- 2.45.2