Merge pull request 'phase_1' (#2) from phase_1 into main
Reviewed-on: https://gitea.ahur.ac/statzitz/rustit/pulls/2
This commit is contained in:
commit
fad19ac060
9 changed files with 162 additions and 3 deletions
8
01_phase_rust_src/chpst/Cargo.toml
Normal file
8
01_phase_rust_src/chpst/Cargo.toml
Normal file
|
@ -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]
|
3
01_phase_rust_src/chpst/src/main.rs
Normal file
3
01_phase_rust_src/chpst/src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
17
01_phase_rust_src/sv/src/main.rs
Normal file
17
01_phase_rust_src/sv/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
|
||||
//mod misc;
|
||||
mod parser;
|
||||
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
// args
|
||||
let arg_parser: Vec<String> = parser::parse_args(args);
|
||||
|
||||
dbg!(arg_parser);
|
||||
|
||||
|
||||
//dbg!(args);
|
||||
}
|
118
01_phase_rust_src/sv/src/parser.rs
Normal file
118
01_phase_rust_src/sv/src/parser.rs
Normal file
|
@ -0,0 +1,118 @@
|
|||
|
||||
|
||||
mod misc;
|
||||
|
||||
use std::process::exit;
|
||||
|
||||
pub fn parse_args(args: Vec<String>) -> Vec<String>{
|
||||
let mut i: i8 = 0;
|
||||
|
||||
let mut parser: Vec<String> = 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<char> = 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()
|
||||
|
||||
|
||||
}
|
5
01_phase_rust_src/sv/src/parser/misc.rs
Normal file
5
01_phase_rust_src/sv/src/parser/misc.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
pub fn usage() {
|
||||
println!("usage: sv [-v] [-w sec] command service ...\n")
|
||||
}
|
8
01_phase_rust_src/svlogd/Cargo.toml
Normal file
8
01_phase_rust_src/svlogd/Cargo.toml
Normal file
|
@ -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]
|
3
01_phase_rust_src/svlogd/src/main.rs
Normal file
3
01_phase_rust_src/svlogd/src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world! test");
|
||||
}
|
Loading…
Reference in a new issue