Created
August 1, 2015 13:39
-
-
Save xaizek/79770cfe47da21a65fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for arg; do | |
if [ ! -f "$arg" ]; then | |
echo "Usage: $(basename $0) [rc...]" | |
echo "Invoked without arguments uses ~/.bashrc" | |
exit 1 | |
fi | |
done | |
if [ $# == 0 ]; then | |
in=~/.bashrc | |
else | |
in="$@" | |
fi | |
out_dir=out #~/.vifm/scripts/auto | |
function prompt() | |
{ | |
echo "$1" | |
read -n 1 answer < /dev/tty | |
echo | |
if [ "x$answer" == "xN" ] || [ "x$answer" == "xn" ]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
if [ ! -e "$out_dir" ]; then | |
echo "Output directory ($out_dir) doesn't exist." | |
if ! prompt "Do you want to create it? [Y/n]"; then | |
exit 1 | |
fi | |
if ! mkdir -p "$out_dir"; then | |
echo "Can't create output directory" | |
exit 1 | |
fi | |
fi | |
if [ ! -d "$out_dir" ]; then | |
echo "Output path isn't a directory" | |
exit 1 | |
fi | |
function create_script() | |
{ | |
local out_file="$out_dir/$1" | |
if [ -e "$out_file" ] && [ -z "$overwrite_all" ]; then | |
echo "Output file ($out_file) already exists." | |
if ! prompt "Do you want to overwrite it? [Y/n]"; then | |
return 1 | |
fi | |
if [ -z "$overwrite_all" ] && prompt "Overwrite all? [Y/n]"; then | |
overwrite_all=1 | |
else | |
overwrite_all=0 | |
fi | |
fi | |
echo -e '#!/bin/bash\n' > "$out_file" | |
chmod +x "$out_file" | |
return 0 | |
} | |
function export_alias() | |
{ | |
local alias=${1#*alias } | |
local alias_name=${alias%%=*} | |
local alias_body=${alias#*=} | |
local alias_body=${alias_body#\'} | |
local alias_body=${alias_body#\"} | |
local alias_body=${alias_body%\'} | |
local alias_body=${alias_body%\"} | |
# this is needed to avoid producing fork bombs | |
mv "$out_dir" "${out_dir}___" | |
which_output=$(which "$alias_name" 2> /dev/null) | |
if [ $? == 0 ]; then | |
alias_body=${alias_body/#${alias_name}/${which_output}} | |
fi | |
mv "${out_dir}___" "$out_dir" | |
if create_script "$alias_name"; then | |
echo "$alias_body" '"$@"' >> "$out_dir/$alias_name" | |
fi | |
} | |
function is_func_beginning() | |
{ | |
case "$1" in | |
function) | |
return 2 | |
;; | |
*\(\)) | |
return 1 | |
;; | |
*) | |
return 0 | |
;; | |
esac | |
} | |
function is_func_end() | |
{ | |
local last_token= | |
for token in $*; do | |
last_token="$token" | |
done | |
if [ "x$last_token" == "x}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function process_func() | |
{ | |
local func_name=${1%%()*} | |
local func_body=${1#*\{} | |
local func_body=${func_body%\}*} | |
if create_script "$func_name"; then | |
echo "$func_body" >> "$out_dir/$func_name" | |
fi | |
} | |
in_func=0 | |
cat $in | | |
while read line; do | |
if [ "x$line" == "x" ]; then | |
continue | |
fi | |
if [ $in_func = 1 ]; then | |
func_body="$func_body"$'\n'"$line" | |
if is_func_end "$func_body"; then | |
process_func "$func_body" | |
in_func=0 | |
fi | |
continue | |
fi | |
set - $line | |
if [ "x$1" == "xalias" ]; then | |
shift | |
export_alias "$*" | |
continue | |
fi | |
is_func_beginning "$1" | |
is_func="$?" | |
if [ "$is_func" != "0" ]; then | |
if [ "$is_func" == "2" ]; then | |
shift | |
fi | |
func_body="$*" | |
if is_func_end "$func_body"; then | |
process_func "$func_body" | |
continue | |
fi | |
in_func=1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment