From b0435ca10002ceabc126a3f9685d27e5c881aa8a Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 7 Nov 2022 15:15:32 -0500 Subject: [PATCH] fixed the changes i made while reformatting --- tomcat | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/tomcat b/tomcat index 781f9d7..7298ef0 100755 --- a/tomcat +++ b/tomcat @@ -2,35 +2,54 @@ set -e -argv1=$1 -argv2=$2 - # check usage -if ! test -n "$argv1"; then +if ! test -n "$1"; then printf "Usage: %s [TABLE.KEY[INDEX]] [FILE]\n" "$0" 1>&2 exit 64 # sysexits(3) EX_USAGE fi -set -- $(printf "%s\n" "$argv1" | cut -d "." --output-delimiter=" " -f 1-) +# test if input has a period +if test -n $(printf "%s\n" "$1" | sed -n '/.*\..*/p'); then + # cut out everything beyond the first period for TABLE + TABLE=$(printf "%s\n" "$1" | sed -n 's/\..*//p') -ARR=$(printf "%s\n" "$2" | sed -n 's/.\+\[//p' | tr -d ']') + # cut out everything before the first period for KEY + KEY=$(printf "%s\n" "$1" | sed -n 's/^[^.]\+\.//p') +else + # set key to input if there is no table specified + KEY=$1 +fi -ARG=$(printf "%s\n" "$2" | sed 's/\[.*\]//g') +# remove array index from KEY +KEY=$(printf "%s\n" "$KEY" | sed 's/\[.*\]//g') +# set ARR to the array index +ARR=$(printf "%s\n" "$1" | sed -n 's/.\+\[//p' | tr -d ']') -VAL=$(if test -e "$argv2"; then - argv2=$(sed 's/[^"#]#\+.*//g' <"$argv2") - else - argv2=$(printf "%s\n" "$argv2" | sed 's/[^"#]#\+.*//g') - fi - printf "%s\n" "$argv2" |\ - awk "/^\[$1\]/{flag=1; next} /^\[/{flag=0} flag" - |\ - sed -n "s/$ARG *= *//p" | sed 's/", "/","/g' | tr -d '[]' |\ - sed 's/^ //g') +# test if argument 2 is a file or not +if test -e "$2"; then + # set TOML to the text from the file + TOML=$(sed 's/[^"#]#\+.*//g' <"$2" | sed 's/^#.*//g' ) +else + # set TOML to the text from stdin + TOML=$(printf "%s\n" "$2" | sed 's/[^"#]#\+.*//g') +fi +# set VAL to the parsed TOML +VAL=$(printf "%s\n" "$TOML" |\ + # output only lines between TABLE and the next table + awk "/^\[$TABLE\]/{flag=1; next} /^\[/{flag=0} flag" - |\ + # remove the key from the string and concatenate arrays, removing + # brackets, trailing commas, and leading spaces + sed -n "s/$KEY *= *//p" | sed 's/", "/","/g' | tr -d '[]' |\ + 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' | tr -d '"' | head -n "$ARR" |\ tail -n 1 else + # remove quotes and output VAL printf "%s\n" "$VAL" | tr -d '"' fi