tomcat/src/main.rs

178 lines
4.8 KiB
Rust
Raw Normal View History

2023-02-26 02:00:36 +00:00
/*
2023-03-20 20:40:43 +00:00
* Copyright (c) 20222023 Emma Tebibyte
2023-02-26 02:00:36 +00:00
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
2023-02-26 02:00:36 +00:00
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
2023-03-25 04:46:37 +00:00
#![no_main]
2023-02-26 02:00:36 +00:00
use std::{
fs::File,
2023-03-25 04:40:00 +00:00
io::Read,
2023-02-26 02:00:36 +00:00
iter::Peekable,
2023-03-25 04:40:00 +00:00
os::fd::{ FromRawFd },
2023-02-26 02:00:36 +00:00
path::Path,
str::FromStr,
};
2023-03-25 04:46:37 +00:00
use c_main::Args;
use toml::Value;
2023-02-26 02:00:36 +00:00
use yacexits::*;
fn parse_toml(
mut root: Value,
mut tabkey: Peekable<std::slice::Iter<'_, &str>>,
index: Option<usize>,
) -> Result<String, (String, u32)> {
let mut out = String::new();
while let Some(item) = tabkey.next() {
let value = match root.get(item) {
Some(val) => val,
None => {
return Err((format!("{}: No such table or key.", item), EX_DATAERR));
},
};
match value {
Value::Table(table) => {
2023-03-25 04:40:00 +00:00
if tabkey.peek().is_some() {
2023-02-26 02:00:36 +00:00
root = toml::Value::Table(table.to_owned());
continue;
};
},
_ => {
2023-03-25 04:40:00 +00:00
if tabkey.peek().is_some() {
return Err((format!("{}: Not a table.", item), EX_DATAERR));
}
2023-02-26 02:00:36 +00:00
},
};
match value {
2023-03-20 20:40:43 +00:00
Value::Array(array) => { // TODO: Split Array logic into separate function
2023-02-26 02:00:36 +00:00
let element: String;
match index {
Some(i) => {
element = match array.get(i) {
Some(element) => {
match element.as_str() {
Some(val) => val.to_owned(),
None => {
return Err((
2023-03-20 20:40:43 +00:00
format!("{:?}: No value at given key.", i),
2023-02-26 02:00:36 +00:00
EX_DATAERR
));
},
}
},
None => {
return Err(
(format!("{:?}: No value at given index.", i), EX_DATAERR)
);
},
};
},
None => element = format!("{:?}", array),
};
out.push_str(&element);
},
2023-03-20 20:40:43 +00:00
Value::Boolean(boolean) => out.push_str(&format!("{:?}", boolean)),
Value::Datetime(datetime) => out.push_str(&format!("{:?}", datetime)),
Value::Float(float) => out.push_str(&format!("{:?}", float)),
Value::Integer(int) => out.push_str(&format!("{:?}", int)),
2023-02-26 02:00:36 +00:00
Value::String(string) => out.push_str(string.as_str()),
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
};
}
Ok(out)
}
2023-03-25 04:46:37 +00:00
#[no_mangle]
fn rust_main(args: Args) {
let argv: Vec<&str> = args.into_iter().collect();
2023-03-24 23:42:34 +00:00
let usage_info = format!("Usage: {} [table.]key[[index]] [file...]", argv[0]);
2023-02-26 02:00:36 +00:00
if argv.len() <= 1 {
2023-03-24 23:44:17 +00:00
eprintln!("{}", usage_info);
2023-03-25 04:40:00 +00:00
exit(EX_USAGE);
2022-12-19 05:41:14 +00:00
}
2023-03-25 04:40:00 +00:00
2023-03-25 04:46:37 +00:00
let input = argv.get(2).unwrap_or(&"");
2023-03-25 04:40:00 +00:00
2023-02-26 02:00:36 +00:00
let mut content = Vec::new();
2023-03-25 04:46:37 +00:00
match input.to_owned() {
2023-03-25 04:40:00 +00:00
"-" | "" => unsafe { File::from_raw_fd(0) },
_ => {
File::open(Path::new(&input)).unwrap_or_else(|_| {
eprintln!(
"{}: {}: No such file or directory.",
argv[0],
&input
);
2023-02-26 02:00:36 +00:00
exit(EX_UNAVAILABLE);
2023-03-25 04:40:00 +00:00
})
},
}.read_to_end(&mut content)
.unwrap_or_else(|_| {
eprintln!("{}: Could not read input.", argv[0]);
exit(EX_OSERR);
});
2023-02-26 02:00:36 +00:00
let mut tabkey: Vec<&str> = argv[1].split(".").collect();
2022-12-19 19:19:08 +00:00
let mut indexvec = Vec::new();
2023-02-26 02:00:36 +00:00
let mut index: Option<usize> = None;
2022-12-19 19:19:08 +00:00
2023-03-25 04:40:00 +00:00
if tabkey.iter().skip(1).peekable().peek().is_some() {
2022-12-19 19:19:08 +00:00
indexvec = tabkey[1].split(&['[', ']'][..]).collect();
tabkey[1] = indexvec.remove(0);
};
2022-12-19 18:51:28 +00:00
2023-02-26 02:00:36 +00:00
if ! indexvec.is_empty() {
let istr = indexvec.remove(0);
match usize::from_str(istr) {
Ok(i) => index = Some(i),
Err(_) => {
eprintln!("{}: {}: Cannot index by given value.", argv[0], istr);
exit(EX_USAGE);
},
};
}
2023-02-26 02:00:36 +00:00
2023-03-25 04:40:00 +00:00
let root = String::from_utf8(content)
.unwrap_or_else(|_| {
eprintln!("{}: Input is not valid UTF-8.", argv[0]);
exit(EX_DATAERR);
})
.parse::<Value>()
.unwrap_or_else(|_| {
2023-02-26 02:00:36 +00:00
eprintln!("{}: Unable to parse TOML.", argv[0]);
exit(EX_DATAERR);
2023-03-25 04:40:00 +00:00
});
2023-02-26 02:00:36 +00:00
let valiter = tabkey.iter().peekable();
println!(
"{}",
match parse_toml(root, valiter, index) {
Ok(val) => val,
Err((err, code)) => {
eprintln!("{}: {}", argv[0], err);
exit(code);
},
},
);
}