From 471161c1a366b769f145fc5e535c0c32388f9fd8 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 18 Dec 2022 00:37:13 -0500 Subject: [PATCH] initial commit: reads table-less keys from argument only --- Cargo.lock | 25 ++++++++++++++++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++ tomcat | 73 ----------------------------------------------------- 3 files changed, 70 insertions(+), 73 deletions(-) create mode 100644 Cargo.lock create mode 100644 src/main.rs delete mode 100755 tomcat diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4fd7cf7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "serde" +version = "1.0.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc" + +[[package]] +name = "tomcat" +version = "0.0.1" +dependencies = [ + "toml", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1ae6881 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,45 @@ +// Copyright (c) 2022 Emma Tebibyte +// SPDX-License-Identifier: AGPL-3.0-or-later + +/* Tomcat 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. + * + * Tomcat 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/. + */ + +use std::env; +use std::fs::File; +use std::io::Read; +use std::path::Path; + +use toml::Value; + +fn main() { + let arguments: Vec = env::args().collect(); + let argv0 = &arguments[0]; + let input = &arguments[2]; + let mut content = String::new(); + + let file = Path::new(&input); + if file.is_file() { + let mut content = String::new(); + File::open(file).expect("").read_to_string(&mut content).unwrap(); + println!("{}", content); + } else { content = input.to_string(); } + + let keyval: Vec<&str> = arguments[1].split(".").collect(); + let mut toml = content.parse::().unwrap(); + + match toml.get(keyval[1]) { + Some(value) => println!("{}", value), + None => println!("{}: {}: No such table or key.", argv0, arguments[1]), + }; +} diff --git a/tomcat b/tomcat deleted file mode 100755 index f59bac2..0000000 --- a/tomcat +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/sh - -set -e - -argv0="$0" -argv1="$1" -argv2="$2" - -# check usage -if ! test -n "$1"; then - printf "Usage: %s [OPTIONS] [TABLE.KEY[INDEX]] [FILE]\n" "$0" 1>&2 - exit 64 # sysexits(3) EX_USAGE -fi - -# test if input has a period -if test -n "$(printf "%s\n" "$argv1" | sed -n '/.*\..*/p')"; then - # cut out everything beyond the first period for TABLE - TABLE=$(printf "%s\n" "$argv1" | sed -n 's/\..*//p') - - # cut out everything before the first period for KEY - KEY=$(printf "%s\n" "$argv1" | sed -n 's/^[^.]*\.//p') - ! test -n "$KEY" &&\ - printf "%s: No key specified\n" "$argv0" 1>&2 &&\ - exit 65 # sysexits(3) EX_DATAERR -else - printf "%s: No key specified\n" "$argv0" 1>&2 - exit 65 # sysexits(3) EX_DATAERR -fi - -# remove array index from KEY -KEY=$(printf "%s\n" "$KEY" | sed 's/\[.*\]//g') -# set ARR to the array index -ARR=$(printf "%s\n" "$argv1" | sed -n 's/.\+\[//p' | tr -d ']') - -# test if argument 2 is a file or not -if test -e "$argv2"; then - # set TOML to the text from the file - TOML=$(sed 's/[^"#]#\+.*//g' <"$argv2" | sed 's/^#.*//g' ) -else - # set TOML to the text from stdin - TOML=$(printf "%s\n" "$argv2" | sed 's/[^"#]#\+.*//g') -fi - -if test -n "$TABLE"; then - KEYS=$(printf "%s\n" "$TOML" |\ - # output only lines between TABLE and the next table - awk "/^\[$TABLE\]/{flag=1; next} /^\[/{flag=0} flag" - ) -else - KEYS=$(printf "%s\n" "$TOML" |\ - # output only lines before the first table - awk '1;/^\[/{exit}' - | sed -n '/^[^[]/p') -fi - -# set VAL to the parsed KEYS list -VAL=$(printf "%s\n" "$KEYS" |\ - # remove the key from the string and delineate arrays, removing - # brackets, trailing commas, and leading spaces - sed -n "s/$KEY *= *//p" | sed 's/", "/ /g' | tr -d '[]"' |\ - sed 's/, *$//g' | sed 's/^ \+//g' | sed 's/ \+$//g') - -# test if ARR is set; if it is, then we have an array index to grab -if test -n "$ARR"; then - # change the line delineator to newlines for parsing and output the result - printf "%s\n" "$VAL" | sed 's/ /\n/g' | head -n "$ARR" |\ - tail -n 1 -elif ! test -n "$VAL"; then - printf "%s: %s: No such key or table\n" "$argv0" "$argv1" 1>&2 - exit 65 # sysexits(3) EX_DATAERR -else - printf "%s\n" "$VAL" -fi - -exit 0