#!/bin/sh # # repair-with - run a file filter on a set of files, # replacing them if successful # # $Id$ # # this script is a generalization of ./tidy-us # which I used to tidy a directory of HTML files with #--- auxiliaries ---# # me=`basename "$0"` Erro() { #echo $me: "$@" 1>&2 echo "$@" 1>&2 } ErroV() { case "$opt_v" in yes) Erro "$@" ;; esac } Usage() { cat 1>&2 </dev/null 2>&1 } Execute() # always called with 2 arguments { f="$2" case "yes" in "$opt_c") ErroV "$@" eval $1 "$2" 2>&3 ;; "$opt_f") ErroV "$1" '<' "$2" eval $1 \< "$2" 2>&3 ;; *) ErroV "$@" "$@" 2>&3 esac >"$f".repaired 3>"$f".err } Repair() { f="$1" if [ ! -f "$f" ] then Erro "$f doesn't exist or isn't a plain file, skipped" return elif Execute "$cmd" "$f" then : else Erro "$f cannot be repaired" return fi if IdenticalContent "$f" "$f".repaired then # keep the original, with its file attributes and possibly hardlinks rm -f "$f".repaired Erro "$f doesn't need any repairs" elif [ "$opt_0" = no ] && IdenticalContent "$f".repaired /dev/null then Erro "$f becomes empty, not replacing" else mv "$f" "$f".unrepaired && mv "$f".repaired "$f" && ErroV "$f has been repaired" || Erro "$f couldn't be replaced with the repaired version" fi # hdr2col -e 's/Parsing "(.*)"/$1/' "$f".err | # perl -ne 's/line (\d+) column (\d+) - Warning:/:$1.$2 / and print' # # while read line # do # Erro $line # done < "$f".err rm -f "$f".repaired "$f".err } #--- cmdline parse ---# # opt_e=no opt_f=no opt_v=no opt_0=no while [ $# -gt 0 ] do case "$1" in -c) opt_c=yes shift ;; -f) opt_f=yes shift ;; -v) opt_v=yes shift ;; -0) opt_0=yes shift ;; -h|-?) Usage exit 0 ;; -*) Erro "invalid option $1" Usage exit 1 ;; *) break ;; esac done if [ $# -eq 0 ] then Erro "no command supplied" exit 1 fi cmd="$1" shift if [ $# -eq 0 ] then Erro "no files or directories supplied" exit 1 fi case yes in "$opt_c"|"$opt_f") ;; *) if type "$cmd" >/dev/null 2>&1 then ErroV "OK, the command '$cmd' is executable" else Erro "the command '$cmd' is not executable, did you forget -c?" exit 2 fi ;; esac if [ $# = 0 ] then Erro "no file or directory arguments provided" exit 3 fi #--- go go go! ---# # for arg in "$@" do case "$arg" in *.err|*.repaired|*.unrepaired) ;; *) Repair "$arg" ;; esac done