Skip to content

Commit

Permalink
Update sophie.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
LyubomirT authored Oct 10, 2023
1 parent 6efc37e commit 897406a
Showing 1 changed file with 78 additions and 75 deletions.
153 changes: 78 additions & 75 deletions sophie.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

Expand Down

0 comments on commit 897406a

Please sign in to comment.