rustit/01_phase_rust_src/sv/src/main.rs

90 lines
2.2 KiB
Rust
Raw Normal View History

2024-04-05 18:34:08 +02:00
2024-04-05 21:12:55 +02:00
mod parser;
mod executor;
2024-04-05 21:12:55 +02:00
2024-04-05 18:34:08 +02:00
use std::env;
2024-04-06 00:33:10 +02:00
use std::process::exit;
2024-04-05 18:34:08 +02:00
2024-03-25 00:57:14 +01:00
fn main() {
2024-04-05 21:12:55 +02:00
2024-04-05 23:00:23 +02:00
// def env variables.
let svdir_env = env::var("SVDIR");
2024-04-05 23:10:11 +02:00
let svwait_env = env::var("SVWAIT");
2024-04-05 23:00:23 +02:00
let svdir_var = match svdir_env {
Ok(var) => var,
Err(..) => "/run/runit/service".to_string(),
};
2024-04-06 00:33:10 +02:00
let mut svwait_var = match svwait_env {
2024-04-05 23:10:11 +02:00
Ok(env) => match env.parse::<i32>() {
Ok(var) => var,
Err(..) => 7,
},
Err(..) => 7,
};
let mut verbose: i8 = 0;
2024-04-06 00:33:10 +02:00
// run the parser
let args: Vec<String> = env::args().collect();
2024-04-05 21:12:55 +02:00
let arg_parser: Vec<String> = parser::parse_args(args);
2024-04-06 00:33:10 +02:00
let mut i = 0;
let mut command = "INVALID";
let mut services: Vec<String> = Vec::new();
// make data from parser.
2024-04-06 00:33:10 +02:00
for val in arg_parser.iter() {
if i == 0 {
if val == "-v" {
verbose = 1;
}
else if val == "-w" {
i = 3;
// take next parametter before returning to i = 0.
}
else if val == "--" {
i += 1;
}
else {
2024-04-06 12:48:48 +02:00
if val.len() >= 2 {
let mut j = 0;
for ch in val.chars() {
if j == 1 {
println!("sv: illegal option -- {}", ch );
}
j += 1;
}
}
2024-04-06 00:33:10 +02:00
parser::misc::usage();
exit(100);
}
}
2024-04-06 00:41:24 +02:00
else if i == 1 {
// Set the command as first arguments.
2024-04-06 00:41:24 +02:00
command = val;
i += 1;
}
else if i == 2 {
// add services to control.
services.push(val.to_string());
}
2024-04-06 00:33:10 +02:00
else if i == 3 {
// change svwait after flag -w
2024-04-06 00:33:10 +02:00
svwait_var = match val.parse::<i32>() {
Ok(val) => val,
Err(..) => svwait_var,
};
i = 0;
}
}
// Exit after all services are correctly executed.
exit(executor::execute(svdir_var, svwait_var, verbose, command, services));
2024-03-25 00:57:14 +01:00
}