2023-02-26 02:00:36 +00:00
|
|
|
|
/*
|
2023-03-20 20:40:43 +00:00
|
|
|
|
* Copyright (c) 2022–2023 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.
|
2022-12-18 05:37:13 +00:00
|
|
|
|
*
|
2023-02-26 02:00:36 +00:00
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
2022-12-18 05:37:13 +00:00
|
|
|
|
* 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-02-26 02:00:36 +00:00
|
|
|
|
use std::{
|
2023-04-10 02:05:31 +00:00
|
|
|
|
env::args,
|
2023-02-26 02:00:36 +00:00
|
|
|
|
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,
|
|
|
|
|
};
|
2022-12-18 05:37:13 +00:00
|
|
|
|
|
|
|
|
|
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() {
|
2023-03-25 06:36:38 +00:00
|
|
|
|
let mut value = match root.get(item) {
|
2023-02-26 02:00:36 +00:00
|
|
|
|
Some(val) => val,
|
|
|
|
|
None => {
|
|
|
|
|
return Err((format!("{}: No such table or key.", item), EX_DATAERR));
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match value {
|
2023-03-25 06:36:38 +00:00
|
|
|
|
Value::Array(array) => {
|
|
|
|
|
let i = match index {
|
|
|
|
|
Some(i) => i,
|
|
|
|
|
None => {
|
|
|
|
|
for v in array.iter() {
|
|
|
|
|
out.push_str(&format!("{}\n", v.to_string()));
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match array.get(i) {
|
|
|
|
|
Some(val) => value = val,
|
|
|
|
|
None => {
|
|
|
|
|
return Err((format!("No value at index {}.", i), EX_DATAERR));
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
},
|
2023-02-26 02:00:36 +00:00
|
|
|
|
Value::Table(table) => {
|
2023-03-25 04:40:00 +00:00
|
|
|
|
if tabkey.peek().is_some() {
|
2023-04-10 02:05:31 +00:00
|
|
|
|
root = toml::Value::Table(table.to_owned());
|
|
|
|
|
continue;
|
2023-02-26 02:00:36 +00:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
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-07-13 18:29:40 +00:00
|
|
|
|
Value::Array(_) => {},
|
2023-03-25 06:36:38 +00:00
|
|
|
|
Value::Boolean(boolean) => out.push_str(&format!("{:?}\n", boolean)),
|
|
|
|
|
Value::Datetime(datetime) => out.push_str(&format!("{:?}\n", datetime)),
|
|
|
|
|
Value::Float(float) => out.push_str(&format!("{:?}\n", float)),
|
|
|
|
|
Value::Integer(int) => out.push_str(&format!("{:?}\n", int)),
|
|
|
|
|
Value::String(string) => {
|
|
|
|
|
let contents = string.to_owned();
|
|
|
|
|
let mut lines: Vec<&str> = contents.lines().collect();
|
|
|
|
|
if lines.last().unwrap().is_empty() { _ = lines.pop(); }
|
|
|
|
|
|
|
|
|
|
for line in lines.iter() { out.push_str(&format!("{}\n", line)); }
|
|
|
|
|
},
|
2023-02-26 02:00:36 +00:00
|
|
|
|
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Ok(out)
|
|
|
|
|
}
|
2022-12-18 05:37:13 +00:00
|
|
|
|
|
2023-04-10 02:05:31 +00:00
|
|
|
|
fn main() {
|
|
|
|
|
let argv: Vec<String> = args().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-04-10 02:05:31 +00:00
|
|
|
|
let input = argv.get(2).unwrap_or(&"".to_owned()).to_owned();
|
2023-03-25 04:40:00 +00:00
|
|
|
|
|
2023-02-26 02:00:36 +00:00
|
|
|
|
let mut content = Vec::new();
|
2022-12-18 05:37:13 +00:00
|
|
|
|
|
2023-04-10 02:05:31 +00:00
|
|
|
|
match input.as_str() {
|
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
|
|
|
|
})
|
|
|
|
|
},
|
2023-03-25 06:36:38 +00:00
|
|
|
|
}.read_to_end(&mut content).unwrap_or_else(|_| {
|
2023-03-25 04:40:00 +00:00
|
|
|
|
eprintln!("{}: Could not read input.", argv[0]);
|
|
|
|
|
exit(EX_OSERR);
|
2023-03-25 06:36:38 +00:00
|
|
|
|
});
|
2023-03-25 04:40:00 +00:00
|
|
|
|
|
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);
|
2022-12-18 19:39:56 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
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();
|
2023-03-25 06:36:38 +00:00
|
|
|
|
print!(
|
2023-02-26 02:00:36 +00:00
|
|
|
|
"{}",
|
|
|
|
|
match parse_toml(root, valiter, index) {
|
|
|
|
|
Ok(val) => val,
|
|
|
|
|
Err((err, code)) => {
|
|
|
|
|
eprintln!("{}: {}", argv[0], err);
|
|
|
|
|
exit(code);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-12-18 05:37:13 +00:00
|
|
|
|
}
|