void-packages

Void Source Packages
git clone git://ezup.dev/void-packages.git
Log | Files | Refs | README | LICENSE

vsed.sh (1424B)


      1# Helper function for calling sed on files and checking if the
      2# file is actually changed
      3#
      4# NOTE: this will not check if the input is valid, you can problably
      5# make it execute arbirtrary commands via passing '; cmd' to a vsed
      6# call.
      7
      8vsed() {
      9	local files=() regexes=() OPTIND OPTSTRING="ie:" has_inline=
     10
     11	eval set -- "$(getopt -s bash "$OPTSTRING" "$@")";
     12
     13	while getopts "$OPTSTRING" opt; do
     14		case $opt in
     15			i) has_inline=1 ;;
     16			e) regexes+=("$OPTARG") ;;
     17			*) ;;
     18		esac
     19	done
     20
     21	if ! [ "$has_inline" ]; then
     22		msg_red "$pkgver: vsed: you must specify -i.\n"
     23		return 1
     24	fi
     25
     26	shift $(($OPTIND - 1))
     27
     28	if [ ${#regexes[@]} -eq 0 ] && [ $# -ge 2 ]; then
     29		regexes+=("$1")
     30		shift
     31	fi
     32
     33	if [ ${#regexes[@]} -eq 0 ]; then
     34		msg_red "$pkgver: vsed: no regexes specified.\n"
     35		return 1
     36	fi
     37
     38	for i; do
     39		files+=("$i")
     40	done
     41
     42	if [ ${#files[@]} -eq 0 ]; then
     43		msg_red "$pkgver: vsed: no files specified.\n"
     44		return 1
     45	fi
     46
     47	for f in "${files[@]}"; do
     48		olddigest="$($XBPS_DIGEST_CMD "$f")"
     49		olddigest="${olddigest%% *}"
     50
     51		for rx in "${regexes[@]}"; do
     52			sed -i "$f" -e "$rx" || {
     53				msg_red "$pkgver: vsed: sed call failed with regex \"$rx\" on file \"$f\"\n"
     54				return 1
     55			}
     56
     57			newdigest="$($XBPS_DIGEST_CMD "$f")"
     58			newdigest="${newdigest%% *}"
     59
     60			if [ "$olddigest" = "$newdigest" ]; then
     61				msg_warn "$pkgver: vsed: regex \"$rx\" didn't change file \"$f\"\n"
     62			fi
     63			olddigest="${newdigest}"
     64		done
     65	done
     66}