Compare commits

..

No commits in common. "19e5f4e2f4a0681b38da29d4d975b1b1d9c82b96" and "152348a17aab5616c17770133e17a79c54f037d6" have entirely different histories.

4 changed files with 53 additions and 53 deletions

10
Cargo.lock generated
View File

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

View File

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

0
cargo
View File

View File

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