diff --git a/extr b/extr index f55d2e4..54b254c 100755 --- a/extr +++ b/extr @@ -15,6 +15,9 @@ #Copyright 2019-2021 rexy712 +export EXTR_MODULE_PATH=("$HOME/.bashfiles/extr-modules" "/etc/extr-modules") +export EXTR_LOADED_MODULES=() + function extr_usage() { if [ -n "$1" ];then echo "$1" >&2 @@ -32,6 +35,28 @@ function extr_usage() { echo "There is NO WARRANTY, to the extent permitted by law." echo "Copyright (C) 2019-2021 rexy712" } + +function extr_is_module_registered(){ + eval [ -n '"$'"EXTR_MODULE_$1"'"' ] +} +function extr_register_module(){ + if ! extr_is_module_registered "$1";then + eval export "EXTR_MODULE_$1"=1 + EXTR_LOADED_MODULES+=("$1") + fi +} +function extr_register_extensions(){ + extr_is_module_registered "$1" || return 1 + + eval export "EXTR_EXTENSIONS_$1"='$'"2" +} +function regex_match(){ + local haystack="$1" + local needle="$2" + + grep -Eqs "$needle" <<< "$haystack" +} + function extr_unsupported_file(){ echo "Unsupported format. Ignoring." >&2 return 0 @@ -120,206 +145,27 @@ function fatal_error(){ exit 1 } -function do_tar(){ - local flags=() - if [ "$verbosity" == 1 ];then - #flags+=("--checkpoint") - :; - elif [ "$verbosity" == 2 ];then - flags+=("--verbose") - fi - if [ -n "$overwrite" ];then - flags+=("--overwrite") - else - flags+=("--keep-old-files") - fi - if [ -n "$outloc" ];then - flags+=("-C" "$outloc") - fi - tar "${flags[@]}" -xf "$file" "${passthrough_args[@]}" -} -function do_bz2(){ - local flags=() - if [ "$verbosity" == 0 ];then - flags+=("--quiet") - elif [ "$verbosity" == 2 ];then - flags+=("--verbose") - fi - if [ -n "$overwrite" ];then - flags+=("--force") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-4}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" - else - bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} -function do_gz(){ - local flags=() - if [ "$verbosity" == 0 ];then - flags+=("--quiet") - elif [ "$verbosity" == 2 ];then - flags+=("--verbose") - fi - if [ -n "$overwrite" ];then - flags+=("--force") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-3}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" - else - gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} -function do_zip(){ - local flags=() - if [ "$verbosity" -lt "2" ];then - flags+=("-q") - fi - if [ -n "$overwrite" ];then - flags+=("-o") - fi - if [ -n "$outloc" ];then - flags+=("-d" "$outloc") - fi - unzip "${flags[@]}" "${passthrough_args[@]}" "$file" -} -function do_uncompress(){ - local flags=() - if [ "$verbosity" == 2 ];then - flags+=("-v") - fi - if [ -n "$overwrite" ];then - flags+=("-f") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-2}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - uncompress "${flags[@]}" "${passthrough_args[@]}" "$file" -c > "${fileout}" - else - uncompress "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} -function do_7zip(){ - local flags=() - if [ -n "$outloc" ];then - flags+=("-o${outloc}") - fi - if [ -n "$overwrite" ];then - flags+=("-y") - fi - if [ "$verbosity" == 0 ];then - 7za x "${flags[@]}" "${passthrough_args[@]}" "$file" >/dev/null 2>&1 - else - 7za x "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} -function do_rar(){ - local flags=() - if [ "$verbosity" == 0 ];then - flags+=("-inul") - elif [ "$verbosity" == 2 ];then - flags+=("-ierr") - fi - if [ -n "$overwrite" ];then - flags+=("-o+") - else - flags+=("-o-") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-4}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - unrar "${flags[@]}" p "${passthrough_args[@]}" "$file" > "${fileout}" - else - unrar "${flags[@]}" x "${passthrough_args[@]}" "$file" - fi -} -function do_zstd(){ - local flags=() - if [ "$verbosity" == 0 ];then - flags+=("--quiet") - elif [ "$verbosity" == 2 ];then - flags+=("-v") - fi - if [ -n "$overwrite" ];then - flags+=("-f") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-5}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" - else - unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} -function do_xz(){ - local flags=() - if [ "$verbosity" == 0 ];then - flags+=("--quiet") - elif [ "$verbosity" == 2 ];then - flags+=("--verbose") - fi - if [ -n "$overwrite" ];then - flags+=("--force") - fi - if [ -n "$outloc" ];then - local fileout="${outloc}/$(basename "${file:0:-3}")" - if [ -e "${fileout}" ] && [ -z "$overwrite" ];then - echo "Could not decompress $file; output file exists" >&2 - return 0 - fi - xz -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" - else - xz -dk "${flags[@]}" "${passthrough_args[@]}" "$file" - fi -} - function do_file_extension(){ local file="$1" - case "$file" in - *.tar.*|*.tbz2|*.tgz|*.txz|*.tar|*.tarZ|*.targz|*.tarxz|*.tzst) - do_tar;; - *.bz2) - do_bz2;; - *.gz) - do_gz;; - *.zip) - do_zip;; - *.Z) - do_uncompress;; - *.7z) - do_7zip;; - *.rar) - do_rar;; - *.zst) - do_zstd;; - *.xz) - do_xz;; - *) - extr_unsupported_file || return -1;; - esac - retval=$? + for module in "${EXTR_LOADED_MODULES[@]}";do + eval regex_match "$file" '$'"EXTR_EXTENSIONS_$module" + if [ $? -eq 0 ];then + eval "do_$module" + retval=$? + break + fi + done return 0 } +function load_modules(){ + for dir in "${EXTR_MODULE_PATH[@]}";do + [ ! -d "$dir" ] && continue + for file in $(ls "$dir");do + source "${dir}/${file}" + done + done +} function _extr(){ local verbosity= local overwrite= @@ -335,6 +181,7 @@ function _extr(){ if [ "${#filenames[@]}" == 0 ];then extr_usage "Missing filenames!" fi + load_modules for file in "${filenames[@]}";do if [ -f "$file" ];then do_file_extension "$file" diff --git a/extr-modules/00-tar b/extr-modules/00-tar new file mode 100644 index 0000000..6aee6b5 --- /dev/null +++ b/extr-modules/00-tar @@ -0,0 +1,38 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'tar' +extr_register_extensions 'tar' '\.tar\..*$|\.tbz2$|\.tgz$|\.txz$|\.tar$|\.tarZ$|\.targz$|\.tarxz$|\.tzst$' + +do_tar(){ + local flags=() + if [ "$verbosity" == 1 ];then + #flags+=("--checkpoint") + :; + elif [ "$verbosity" == 2 ];then + flags+=("--verbose") + fi + if [ -n "$overwrite" ];then + flags+=("--overwrite") + else + flags+=("--keep-old-files") + fi + if [ -n "$outloc" ];then + flags+=("-C" "$outloc") + fi + tar "${flags[@]}" -xf "$file" "${passthrough_args[@]}" +} diff --git a/extr-modules/10-7zip b/extr-modules/10-7zip new file mode 100644 index 0000000..6917e17 --- /dev/null +++ b/extr-modules/10-7zip @@ -0,0 +1,34 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module '7zip' +extr_register_extensions '7zip' '\.7z$' + +function do_7zip(){ + local flags=() + if [ -n "$outloc" ];then + flags+=("-o${outloc}") + fi + if [ -n "$overwrite" ];then + flags+=("-y") + fi + if [ "$verbosity" == 0 ];then + 7za x "${flags[@]}" "${passthrough_args[@]}" "$file" >/dev/null 2>&1 + else + 7za x "${flags[@]}" "${passthrough_args[@]}" "$file" + fi +} diff --git a/extr-modules/10-bz2 b/extr-modules/10-bz2 new file mode 100644 index 0000000..1772d50 --- /dev/null +++ b/extr-modules/10-bz2 @@ -0,0 +1,46 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'bz2' +extr_register_extensions 'bz2' '\.bz2$' + +function do_bz2(){ + local flags=() + if [ "$verbosity" == 0 ];then + flags+=("--quiet") + elif [ "$verbosity" == 2 ];then + flags+=("--verbose") + fi + if [ -n "$overwrite" ];then + flags+=("--force") + fi + local fileout= + if [ -n "$outloc" ];then + fileout="${outloc}/$(basename "${file:0:-4}")" + else + fileout="${PWD}/$(basename "${file:0:-4}")" + fi + if [ -e "${fileout}" ] && [ -z "$overwrite" ];then + echo "Could not decompress $file; output file exists" >&2 + return 1 + fi + if [ ! -r "${file}" ] || [ ! -w "$(dirname "${fileout}")" ];then + echo "${file} decompress failed; permission denied" + return 1 + fi + bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" +} diff --git a/extr-modules/10-deb b/extr-modules/10-deb new file mode 100644 index 0000000..f8eea24 --- /dev/null +++ b/extr-modules/10-deb @@ -0,0 +1,33 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'deb' +extr_register_extensions 'deb' '\.deb$' + +function do_deb(){ + local flags=() + if [ "$verbosity" == 2 ];then + flags+=("-v") + fi + local outdir= + if [ -n "$outloc" ];then + outdir="$outloc" + else + outdir="$PWD" + fi + ar x "${flags[@]}" "${passthrough_args[@]}" "$file" --output "${outdir}" +} diff --git a/extr-modules/10-gz b/extr-modules/10-gz new file mode 100644 index 0000000..034587d --- /dev/null +++ b/extr-modules/10-gz @@ -0,0 +1,46 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'gz' +extr_register_extensions 'gz' '\.gz$' + +function do_gz(){ + local flags=() + if [ "$verbosity" == 0 ];then + flags+=("--quiet") + elif [ "$verbosity" == 2 ];then + flags+=("--verbose") + fi + if [ -n "$overwrite" ];then + flags+=("--force") + fi + local fileout= + if [ -n "$outloc" ];then + fileout="${outloc}/$(basename "${file:0:-3}")" + else + fileout="${PWD}/$(basename "${file:0:-3}")" + fi + if [ -e "${fileout}" ] && [ -z "$overwrite" ];then + echo "Could not decompress $file; output file exists" >&2 + return 1 + fi + if [ ! -r "${file}" ] || [ ! -w "$(dirname "${fileout}")" ];then + echo "${file} decompress failed; permission denied" + return 1 + fi + gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" +} diff --git a/extr-modules/10-rar b/extr-modules/10-rar new file mode 100644 index 0000000..ad164d7 --- /dev/null +++ b/extr-modules/10-rar @@ -0,0 +1,40 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'rar' +extr_register_extensions 'rar' '\.rar$' + +function do_rar(){ + local flags=() + if [ "$verbosity" == 0 ];then + flags+=("-inul") + elif [ "$verbosity" == 2 ];then + flags+=("-ierr") + fi + if [ -n "$overwrite" ];then + flags+=("-o+") + else + flags+=("-o-") + fi + local outdir= + if [ -n "$outloc" ];then + outdir="${outloc}/" + else + outdir="${PWD}/" + fi + unrar "${flags[@]}" x "${passthrough_args[@]}" "$file" "${outdir}" +} diff --git a/extr-modules/10-uncompress b/extr-modules/10-uncompress new file mode 100644 index 0000000..ebc4918 --- /dev/null +++ b/extr-modules/10-uncompress @@ -0,0 +1,44 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'uncompress' +extr_register_extensions 'uncompress' '\.Z$' + +function do_uncompress(){ + local flags=() + if [ "$verbosity" == 2 ];then + flags+=("-v") + fi + if [ -n "$overwrite" ];then + flags+=("-f") + fi + local fileout= + if [ -n "$outloc" ];then + fileout="${outloc}/$(basename "${file:0:-2}")" + else + fileout="${PWD}/$(basename "${file:0:-2}")" + fi + if [ -e "${fileout}" ] && [ -z "$overwrite" ];then + echo "Could not decompress $file; output file exists" >&2 + return 0 + fi + if [ ! -r "${file}" ] || [ ! -w "$(dirname "${fileout}")" ];then + echo "${file} decompress failed; permission denied" + return 1 + fi + uncompress "${flags[@]}" "${passthrough_args[@]}" "$file" -c > "${fileout}" +} diff --git a/extr-modules/10-xz b/extr-modules/10-xz new file mode 100644 index 0000000..6f08847 --- /dev/null +++ b/extr-modules/10-xz @@ -0,0 +1,45 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'xz' +extr_register_extensions 'xz' '\.xz$' + +function do_xz(){ + local flags=() + if [ "$verbosity" == 0 ];then + flags+=("--quiet") + elif [ "$verbosity" == 2 ];then + flags+=("--verbose") + fi + if [ -n "$overwrite" ];then + flags+=("--force") + fi + if [ -n "$outloc" ];then + fileout="${outloc}/$(basename "${file:0:-3}")" + else + fileout="${PWD}/$(basename "${file:0:-3}")" + fi + if [ -e "${fileout}" ] && [ -z "$overwrite" ];then + echo "Could not decompress $file; output file exists" >&2 + return 1 + fi + if [ ! -r "${file}" ] || [ ! -w "$(dirname "${fileout}")" ];then + echo "${file} decompress failed; permission denied" + return 1 + fi + xz -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" +} diff --git a/extr-modules/10-zip b/extr-modules/10-zip new file mode 100644 index 0000000..c56ec73 --- /dev/null +++ b/extr-modules/10-zip @@ -0,0 +1,33 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'zip' +extr_register_extensions 'zip' '\.zip$' + +function do_zip(){ + local flags=() + if [ "$verbosity" -lt "2" ];then + flags+=("-q") + fi + if [ -n "$overwrite" ];then + flags+=("-o") + fi + if [ -n "$outloc" ];then + flags+=("-d" "$outloc") + fi + unzip "${flags[@]}" "${passthrough_args[@]}" "$file" +} diff --git a/extr-modules/10-zstd b/extr-modules/10-zstd new file mode 100644 index 0000000..2facecc --- /dev/null +++ b/extr-modules/10-zstd @@ -0,0 +1,46 @@ +#!/bin/bash + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. + +#This program 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 General Public License for more details. + +#You should have received a copy of the GNU General Public License +#along with this program. If not, see . + +#Copyright 2021 rexy712 + +extr_register_module 'zstd' +extr_register_extensions 'zstd' '\.zstd$' + +function do_zstd(){ + local flags=() + if [ "$verbosity" == 0 ];then + flags+=("--quiet") + elif [ "$verbosity" == 2 ];then + flags+=("-v") + fi + if [ -n "$overwrite" ];then + flags+=("-f") + fi + local fileout= + if [ -n "$outloc" ];then + fileout="${outloc}/$(basename "${file:0:-5}")" + else + fileout="${PWD}/$(basename "${file:0:-5}")" + fi + if [ -e "${fileout}" ] && [ -z "$overwrite" ];then + echo "Could not decompress $file; output file exists" >&2 + return 1 + fi + if [ ! -r "${file}" ] || [ ! -w "$(dirname "${fileout}")" ];then + echo "${file} decompress failed; permission denied" + return 1 + fi + unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}" +}