Compare commits

...

2 Commits

Author SHA1 Message Date
Emma Tebibyte 19e5f4e2f4
added c-main dependency 2023-03-25 00:46:37 -04:00
Emma Tebibyte 03e02b9882
code cleanup 2023-03-25 00:40:00 -04:00
4 changed files with 53 additions and 53 deletions

10
Cargo.lock generated
View File

@ -30,6 +30,15 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "c-main"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "797bbff8bd2bcddb7f0ee638b55398686adac15174689a86da5ffc0f51219f75"
dependencies = [
"libc",
]
[[package]]
name = "cexpr"
version = "0.6.0"
@ -205,6 +214,7 @@ dependencies = [
name = "tomcat"
version = "0.0.1"
dependencies = [
"c-main",
"toml",
"yacexits",
]

View File

@ -6,5 +6,6 @@ license = "AGPL-3.0-or-later"
authors = [ "Emma Tebibyte <emma@tebibyte.media>" ]
[dependencies]
c-main = "1.0.1"
toml = "0.5.9"
yacexits = "0.1.2"

0
cargo Normal file
View File

View File

@ -16,18 +16,18 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#![no_main]
use std::{
env,
fs::File,
io::{
Read,
stdin,
},
io::Read,
iter::Peekable,
os::fd::{ FromRawFd },
path::Path,
str::FromStr,
};
use c_main::Args;
use toml::Value;
use yacexits::*;
@ -48,21 +48,15 @@ fn parse_toml(
match value {
Value::Table(table) => {
match tabkey.peek() {
Some(_) => {
if tabkey.peek().is_some() {
root = toml::Value::Table(table.to_owned());
continue;
},
None => {}, // out.push_str(table.as_str()),
};
},
_ => {
match tabkey.peek() {
Some(_) => {
return Err((format!("{}: Not a table.", item), EX_DATAERR));
},
None => {},
};
if tabkey.peek().is_some() {
return Err((format!("{}: Not a table.", item), EX_DATAERR));
}
},
};
@ -105,54 +99,46 @@ fn parse_toml(
Ok(out)
}
fn main() {
let argv: Vec<String> = env::args().collect();
#[no_mangle]
fn rust_main(args: Args) {
let argv: Vec<&str> = args.into_iter().collect();
let usage_info = format!("Usage: {} [table.]key[[index]] [file...]", argv[0]);
if argv.len() <= 1 {
eprintln!("{}", usage_info);
exit(64); // sysexits(3) EX_USAGE
exit(EX_USAGE);
}
let input = match argv.get(2) {
Some(val) => val,
None => {
eprintln!("{}", usage_info);
exit(EX_USAGE);
},
};
let input = argv.get(2).unwrap_or(&"");
let mut content = Vec::new();
let file = Path::new(&input);
if input == &"-" {
match stdin().lock().read_to_end(&mut content) {
Ok(_) => {},
Err(_) => {
eprintln!("{}: Could not read from standard input.", argv[0]);
exit(EX_OSERR);
},
};
} else {
match File::open(file).unwrap().read_to_end(&mut content) {
Ok(_) => {},
Err(_) => {
eprintln!("{}: {:?}: No such file or directory.", argv[0], file);
match input.to_owned() {
"-" | "" => unsafe { File::from_raw_fd(0) },
_ => {
File::open(Path::new(&input)).unwrap_or_else(|_| {
eprintln!(
"{}: {}: No such file or directory.",
argv[0],
&input
);
exit(EX_UNAVAILABLE);
},
};
}
})
},
}.read_to_end(&mut content)
.unwrap_or_else(|_| {
eprintln!("{}: Could not read input.", argv[0]);
exit(EX_OSERR);
});
let mut tabkey: Vec<&str> = argv[1].split(".").collect();
let mut indexvec = Vec::new();
let mut index: Option<usize> = None;
match tabkey.iter().skip(1).peekable().peek() {
Some(_) => {
if tabkey.iter().skip(1).peekable().peek().is_some() {
indexvec = tabkey[1].split(&['[', ']'][..]).collect();
tabkey[1] = indexvec.remove(0);
},
None => {},
};
if ! indexvec.is_empty() {
@ -166,13 +152,16 @@ fn main() {
};
}
let root = match String::from_utf8(content).unwrap().parse::<Value>() {
Ok(toml) => toml,
Err(_) => {
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(|_| {
eprintln!("{}: Unable to parse TOML.", argv[0]);
exit(EX_DATAERR);
},
};
});
let valiter = tabkey.iter().peekable();
println!(