Skip to content

Commit

Permalink
Implement a wrapper for sed to try to have a consistent syntax
Browse files Browse the repository at this point in the history
for regular expressions across platforms. This is an attempt to try
to fix #33 .
  • Loading branch information
Dan Liew committed Jan 5, 2017
1 parent 404b51b commit 17ed258
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions unrarall
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# Set some defaults
declare -x FIND="find" #set to gfind to use homebrew's GNU findutils on OSX Yosemite
declare -x SED="sed"
declare -x DIR=""
declare -x RM="rm"
declare -rx UNRARALL_VERSION="0.4.3"
Expand Down Expand Up @@ -234,7 +235,7 @@ function detect_clean_up_hooks()
continue
fi

UNRARALL_DETECTED_CLEAN_UP_HOOKS[$index]=$( echo "$hook" | sed 's/unrarall_clean_//')
UNRARALL_DETECTED_CLEAN_UP_HOOKS[$index]=$( echo "$hook" | sed_wrapper 's/unrarall_clean_//')
((index++))
done

Expand All @@ -247,8 +248,11 @@ function detect_clean_up_hooks()
#This is posix egrep style regex
function find_regex_escape()
{
#echo "$1" | sed 's#\\#\\\\#g ;s/\?/\\?/g ; s/\./\\./g ; s/\+/\\+/g ; s/\*/\\*/g; s/\^/\\^/g ; s/\$/\\$/g; s/\[/\\[/g; s/\]/\\]/g;'
echo "$1" | sed 's#\\#\\\\#g ;s/\?/\\?/g ; s/\./\\./g ; s/\+/\\+/g ; s/\*/\\*/g; s/\^/\\^/g ; s/\$/\\$/g; s/\[/\\[/g; s/\]/\\]/g; s/(/\\(/g; s/)/\\)/g; s/|/\\|/g;'
echo "$1" | sed_wrapper 's#\\#\\\\#g ;s/\?/\\?/g ; s/\./\\./g ; s/\+/\\+/g ; s/\*/\\*/g; s/\^/\\^/g ; s/\$/\\$/g; s/\[/\\[/g; s/\]/\\]/g; s/\(/\\(/g; s/\)/\\)/g; s/\|/\\|/g;'
if [ $? -ne 0 ]; then
message error "Failed to escape"
exit 1
fi
}

#Helper function for hooks to remove a single file/folder
Expand Down Expand Up @@ -430,6 +434,23 @@ function unrarall_clean_empty_folders()

#end of clean-up hooks

# Detect the version of sed available and create a wrapper function
# that will behave like sed using "extended regular expressions"
# This is to force consistency across multiple platforms.
if [ "X$(${SED} --version >/dev/null 2>&1 ; echo $?)" == "X0" ]; then
# GNU find supports `--version`
[ $VERBOSE -eq 1 ] && message info "Detected GNU sed"
function sed_wrapper() {
${SED} --regexp-extended "$@"
}
else
# Assume *BSD/macOS sed
[ $VERBOSE -eq 1 ] && message info "Detected BSD sed"
function sed_wrapper() {
${SED} -E "$@"
}
fi


# Parse command line arguments
if [ "$#" -lt 1 ]; then
Expand Down Expand Up @@ -458,7 +479,7 @@ while [ -n "$1" ]; do
;;
--clean=*)
index=0;
for hook in $( echo "$1" | sed 's/--clean=//' | tr , " ") ; do
for hook in $( echo "$1" | sed_wrapper 's/--clean=//' | tr , " ") ; do
UNRARALL_CLEAN_UP_HOOKS_TO_RUN[$index]="$hook"
((index++))
done
Expand Down Expand Up @@ -515,7 +536,7 @@ while [ -n "$1" ]; do
;;
--backend=*)
# If using `--dry` don't modify UNRARALL_BIN
requested_backend="$( echo "$1" | sed 's/--backend=//')"
requested_backend="$( echo "$1" | sed_wrapper 's/--backend=//')"
if [ "$UNRARALL_BIN" != 'echo' ]; then
case "${requested_backend}" in
unrar)
Expand Down Expand Up @@ -572,6 +593,7 @@ else
}
fi


detect_clean_up_hooks

#Verify selected clean-up hooks are ok
Expand Down Expand Up @@ -669,7 +691,7 @@ for file in $(find_wrapper "$DIR" -depth -iregex '.*\.(rar|001)$'); do
# if rar is of style partxx.rar then only extract $filename.part01.rar
if [[ "$file" =~ .part[0-9]+.rar$ ]]; then
if [[ "$file" =~ .part0*1.rar$ ]]; then
sfilename=`echo "$sfilename" | sed 's/\.part[0-9]\+$//'`
sfilename=`echo "$sfilename" | sed_wrapper 's/\.part[0-9]+$//'`
[ $VERBOSE -eq 1 ] && message info "Using rar 3.x split archive format"
else
continue
Expand Down

0 comments on commit 17ed258

Please sign in to comment.