mu-wizard

shell script to easily configure mu4e accounts on Emacs
git clone git://git.ckyln.com/mu-wizard
Log | Files | Refs | LICENSE

commit 1b531faffb39699d98c5955156b37c20a0186d7e
parent 1e397280b9c94a9014955015cdc0c7a49deb4ba2
Author: Cem Keylan <cem@ckyln.com>
Date:   Thu, 15 Sep 2022 16:21:40 +0200

sed_i(): revamp function

This makes the usage close to actual sed and adds some security details.
The changes were applied to the places the function was used.

Diffstat:
Mbin/muw | 41+++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/bin/muw b/bin/muw @@ -397,8 +397,8 @@ yesno() { delete() { rm -f "$accountdir/$1.el" - sed_i "$HOME/.mbsyncrc" "/^IMAPStore $1-remote\$/,/^# End profile\$/d" - sed_i "$config_home/msmtp/config" "/^account $1\$/,/^# End profile\$/d" + sed_i "/^IMAPStore $1-remote\$/,/^# End profile\$/d" "$HOME/.mbsyncrc" + sed_i "/^account $1\$/,/^# End profile\$/d" "$config_home/msmtp/config" rm -f /tmp/mbsync-boxes } @@ -415,10 +415,39 @@ EOF sed_i() { # POSIX compliant 'sed -i' like function. This can be only used for a single # file, and the filename should always come first. - file=$1; shift - [ -f "$file" ] || die "$file: No such file or directory" - sed "$@" "$file" > _; cat _ > "$file" - rm -f _ + script='' sedf='--' optesc='' suffix=tmp.$$ + + while getopts nre:Ef: flag; do + case $flag in + E|r|n) sedf="-${sedf##*-}$flag" optesc=-- ;; + e) script=$(printf '%s\n%s\n' "$script" "$OPTARG") ;; + f) script=$(printf '%s\n%s\n' "$script" "$(cat "$OPTARG")") ;; + *) return 1 + esac + done + + shift "$((OPTIND - 1))" + + [ "$script" ] || { script=$1; shift ;} + + for file; do + # Create traps for removing temporary files on failure + trap 'rm -f "$file.$suffix"' EXIT + trap 'rm -f "$file.$suffix"; exit 1' INT + + # Save the edited stream in the temporary file + sed "$sedf" $optesc "$script" "$file" > "$file.$suffix" + + # Pipe back the contents of the temporary file so that we don't have any + # permission related issues. + cat "$file.$suffix" > "$file" + + # Remove the temporary file. + rm -f "$file.$suffix" + + # Restore the trap + trap - INT EXIT + done } get_profiles() {