From 897406a4b35671e01cafc3a25754eb7526db699e Mon Sep 17 00:00:00 2001 From: Lyubomir <127299159+LyubomirT@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:51:24 -0600 Subject: [PATCH] Update sophie.sh --- sophie.sh | 153 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 75 deletions(-) diff --git a/sophie.sh b/sophie.sh index 90637ce..9df2a7d 100755 --- a/sophie.sh +++ b/sophie.sh @@ -114,47 +114,49 @@ while read -r line; do # Remove any whitespace at the beginning line=$(printf "%s" "$line" | sed "s/^[ ]*//") - # Filter - cut_count=$(expr "$line" : "[^=]*=\"") - filter=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//") - cut_count=$((cut_count + 1)) - line=$(printf "%s" "$line" | cut -c"$cut_count"-) - - # Filter condition - cut_count=$(expr "$line" : "[^\<\";\>]*\";") - condition=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//") - cut_count=$((cut_count + 1)) - line=$(printf "%s" "$line" | cut -c"$cut_count"-) - - # Fetching current command that represents matching files - if printf "%s" "$file_cabinet" | grep -q -F "__SOPHIE_IDENTIFIER$curr_identifier="; then - old_files=$(printf "%s" "$file_cabinet" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier=") - old_files=$(printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//") - - # Need to remove leftover \n at the beginning from previous commands - old_files=$(printf "%s" "$old_files" | tr -d "\n") - else - old_files="find . -type f" - fi - - # Applying filters - if [ "$filter" = "pattern" ]; then - new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-regex $condition") - fi + # Handle chaining of multiple filters on a single line + while [ "$line" != "]" ]; do + # Filter + cut_count=$(expr "$line" : "[^=]*=\"") + filter=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//") + cut_count=$((cut_count + 1)) + line=$(printf "%s" "$line" | cut -c"$cut_count"-) + + # Filter condition + cut_count=$(expr "$line" : "[^\<\";\>]*\";") + condition=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//") + cut_count=$((cut_count + 1)) + line=$(printf "%s" "$line" | cut -c"$cut_count"-) + + # Fetching current command that represents matching files + if printf "%s" "$file_cabinet" | grep -q -F "__SOPHIE_IDENTIFIER$curr_identifier="; then + old_files=$(printf "%s" "$file_cabinet" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier=") + old_files=$(printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//") + + # Need to remove leftover \n at the beginning from previous commands + old_files=$(printf "%s" "$old_files" | tr -d "\n") + else + old_files="find . -type f" + fi - if [ "$filter" = "size" ]; then - new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-size $condition") - fi + # Applying filters + if [ "$filter" = "pattern" ]; then + new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-regex $condition") + fi - if [ "$filter" = "mtime" ]; then - new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-mtime $condition") - fi + if [ "$filter" = "size" ]; then + new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-size $condition") + fi - # Remove old - file_cabinet=$(printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//") - # Put new - file_cabinet=$(printf "%s\n%s" "$file_cabinet" "__SOPHIE_IDENTIFIER$curr_identifier=$new_files") + if [ "$filter" = "mtime" ]; then + new_files=$(printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-mtime $condition") + fi + # Remove old + file_cabinet=$(printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//") + # Put new + file_cabinet=$(printf "%s\n%s" "$file_cabinet" "__SOPHIE_IDENTIFIER$curr_identifier=$new_files") + done done # This identifier is now defined. We put a refinement marker in case a new command to refine the result of the previous one will be defined. @@ -177,49 +179,50 @@ while read -r line; do # Remove any whitespace at the beginning line=$(printf "%s" "$line" | sed "s/^[ ]*//") - # Filter - cut_count=$(expr "$line" : "[^=]*=\"") - filter=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//") - cut_count=$((cut_count + 1)) - line=$(printf "%s" "$line" | cut -c"$cut_count"-) - - # Filter condition - cut_count=$(expr "$line" : "[^\<\";\>]*\";") - condition=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//") - cut_count=$((cut_count + 1)) - line=$(printf "%s" "$line" | cut -c"$cut_count"-) - - # Apply filter after fetching old filter condition - if printf "%s" "$notices" | grep -q "$curr_identifier"; then - old_notice=$(printf "%s" "$notices" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier=") - old_notice=$(printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//") - - # Need to remove leftover \n at the beginning from previous commands - old_notice=$(printf "%s" "$old_notice" | tr -d "\n") - else - old_notice="grep -q" - fi - - if [ "$filter" = "literal" ]; then - new_notice=$(printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "-F \"$condition\"") - fi + # Handle chaining of multiple filters on a single line + while [ "$line" != "]" ]; do + # Filter + cut_count=$(expr "$line" : "[^=]*=\"") + filter=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//") + cut_count=$((cut_count + 1)) + line=$(printf "%s" "$line" | cut -c"$cut_count"-) + + # Filter condition + cut_count=$(expr "$line" : "[^\<\";\>]*\";") + condition=$(printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//") + cut_count=$((cut_count + 1)) + line=$(printf "%s" "$line" | cut -c"$cut_count"-) + + # Apply filter after fetching old filter condition + if printf "%s" "$notices" | grep -q "$curr_identifier"; then + old_notice=$(printf "%s" "$notices" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier=") + old_notice=$(printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//") + + # Need to remove leftover \n at the beginning from previous commands + old_notice=$(printf "%s" "$old_notice" | tr-d "\n") + else + old_notice="grep -q" + fi - # The `literal` filter is mutually excl. with `pattern`. Must not use both in a definition. - if [ "$filter" = "pattern" ]; then - new_notice=$(printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "\"$condition\"") - fi + if [ "$filter" = "literal" ]; then + new_notice=$(printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "-F \"$condition\"") + fi - if [ "$filter" = "location" ]; then - new_notice=$(printf "%s%s" "sed -n \"$condition\"p | " "$old_notice") - fi + # The `literal` filter is mutually excl. with `pattern`. Must not use both in a definition. + if [ "$filter" = "pattern" ]; then + new_notice=$(printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "\"$condition\"") + fi - # Remove old - notices=$(printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//") - # Put new - notices=$(printf "%s\n%s" "$notices" "__SOPHIE_IDENTIFIER$curr_identifier=$new_notice") + if [ "$filter" = "location" ]; then + new_notice=$(printf "%s%s" "sed -n \"$condition\"p | " "$old_notice") + fi + # Remove old + notices=$(printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//") + # Put new + notices=$(printf "%s\n%s" "$notices" "__SOPHIE_IDENTIFIER$curr_identifier=$new_notice") + done done - elif [ "$curr_command" = "compliance" ]; then # Hardcoded to only support the `in` preposition. Prepositions can be extended easily without breaking configs.