2013-08-27 15:14:44 +04:00
|
|
|
#!/bin/bash
|
|
|
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
2014-10-12 21:51:38 +04:00
|
|
|
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
|
|
|
urequire DEFAULTS
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
function display_help() {
|
2014-10-12 21:53:08 +04:00
|
|
|
uecho "$scriptname: convertir un fichier ou les fichiers d'un répertoire
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
USAGE
|
2014-10-12 21:53:08 +04:00
|
|
|
$scriptname [options] <file|dir> [cmds...]
|
2014-10-12 21:51:38 +04:00
|
|
|
|
|
|
|
Une ou plusieurs commandes peuvent être spécifiées, séparées //
|
|
|
|
La command par défaut est 'lf'
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
OPTIONS
|
2014-10-12 21:51:38 +04:00
|
|
|
-N, --detect-always
|
|
|
|
Pour la commande conv, ne pas optimiser le calcul de l'encoding. Cette
|
|
|
|
option n'est valide que si src_enc n'est pas spécifié. On assume que
|
|
|
|
tous les fichiers n'ont pas le même encoding: l'encoding src_enc est
|
|
|
|
donc recalculé à chaque fois.
|
|
|
|
-r, --reverse
|
|
|
|
Inverser src_enc et dest_enc, qui doivent être tous les deux spécifiés.
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
COMMANDES
|
|
|
|
conv dest_enc [src_enc]
|
|
|
|
Convertir le fichier dans un autre encoding.
|
|
|
|
dest_enc est l'encoding destination. Il doit être spécifié.
|
|
|
|
src_enc est l'encoding source. S'il n'est pas spécifié ou vaut 'detect',
|
|
|
|
il est autodétecté.
|
|
|
|
utf8 [src_enc]
|
|
|
|
Equivalent à conv utf8 src_enc
|
|
|
|
latin1 [src_enc]
|
|
|
|
Equivalent à conv latin1 src_enc
|
|
|
|
lf
|
|
|
|
crlf
|
|
|
|
cr
|
|
|
|
Convertir respectivement les caractères de fin de ligne en LF, CR/LF ou CR
|
|
|
|
noaccents
|
|
|
|
Transformer caractères accentués en caractères non accentués"
|
|
|
|
}
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
function detect_enc() {
|
|
|
|
if [ -f "$1" ]; then
|
|
|
|
"$scriptdir/lib/pywrapper" uencdetect.py -f "$1"
|
|
|
|
return $?
|
|
|
|
elif [ -d "$1" ]; then
|
|
|
|
local -a files
|
|
|
|
setx -a files=evalp find "$1" -type f // head -n1
|
|
|
|
if [ ${#files[*]} -gt 0 ]; then
|
|
|
|
"$scriptdir/lib/pywrapper" uencdetect.py -f "${files[0]}"
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
echo Unknown
|
|
|
|
}
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
function before_parse_cmd() {
|
|
|
|
case "$1" in
|
|
|
|
conv)
|
|
|
|
if [ -n "$OPTIMIZE_CONV" ]; then
|
|
|
|
local to="$2"
|
|
|
|
[ -n "$to" ] && setx CONV_TO=__norm_encoding "$to"
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
local from="${3:-detect}"
|
|
|
|
if [ "$from" == "detect" ]; then
|
|
|
|
setx from=detect_enc "$FILE"
|
|
|
|
if [ "$from" == "Unknown" ]; then
|
|
|
|
echo "$FILE: Impossible de détecter l'encoding"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
[ -n "$from" ] && setx CONV_FROM=__norm_encoding "$from"
|
|
|
|
OPTIMIZE_CONV=
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
noaccents|noa|fixchars)
|
|
|
|
export LANG=fr_FR.UTF-8
|
|
|
|
;;
|
|
|
|
esac
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
function iconv_detect() {
|
|
|
|
local to="$1"; shift
|
|
|
|
local from
|
|
|
|
setx from=detect_enc "$FILE"
|
|
|
|
if [ "$from" == "Unknown" ]; then
|
|
|
|
cat
|
|
|
|
return 1
|
2013-08-27 15:14:44 +04:00
|
|
|
else
|
2014-10-12 21:51:38 +04:00
|
|
|
if [ -n "$REVERSE_CONV" ]; then
|
|
|
|
local tmp="$to"
|
|
|
|
to="$from"
|
|
|
|
from="$tmp"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
2014-10-12 21:51:38 +04:00
|
|
|
iconv -f "$from" -t "$to" "$@"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse_cmd() {
|
|
|
|
local cmd="$1"; shift
|
|
|
|
local -a args
|
|
|
|
case "$cmd" in
|
|
|
|
utf8) parse_cmd conv utf8 "$@";;
|
|
|
|
latin1) parse_cmd conv latin1 "$@";;
|
|
|
|
conv)
|
|
|
|
local to="$CONV_TO" from="$CONV_FROM"
|
|
|
|
[ -n "$to" ] || to="$1"; shift
|
|
|
|
[ -f "$from" ] || from="${1:-detect}"; shift
|
|
|
|
|
|
|
|
if [ -z "$to" ]; then
|
|
|
|
echo "conv: vous devez spécifier l'encoding destination"
|
|
|
|
return 1
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
2014-10-12 21:51:38 +04:00
|
|
|
setx to=__norm_encoding "$to"
|
|
|
|
if [ "$from" == "detect" ]; then
|
|
|
|
qvals iconv_detect "$to" "$@"
|
|
|
|
else
|
|
|
|
setx from=__norm_encoding "$from"
|
|
|
|
if [ "$from" == "$to" ]; then
|
|
|
|
echo "Une conversion de $from à $to n'a pas de sens. Veuillez re-essayer avec -N si nécessaire."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -n "$REVERSE_CONV" ]; then
|
|
|
|
local tmp="$to"
|
|
|
|
to="$from"
|
|
|
|
from="$tmp"
|
|
|
|
fi
|
|
|
|
qvals iconv -f "$from" -t "$to" "$@"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
2014-10-12 21:51:38 +04:00
|
|
|
;;
|
|
|
|
lf) echo _nl2lf;;
|
|
|
|
crlf) echo _nl2crlf;;
|
|
|
|
cr) echo _nl2cr;;
|
|
|
|
noaccents|noa|fixchars)
|
|
|
|
echo _noaccents;;
|
|
|
|
*)
|
|
|
|
echo "$cmd: commande invalide"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
return 0
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
function ffconv() {
|
|
|
|
local tmp="$FILE.tmp.$$"
|
|
|
|
autoclean "$tmp"
|
|
|
|
|
|
|
|
ebegin "$FILE"
|
|
|
|
if eval "$CMD" <"$FILE" >"$tmp"; then
|
|
|
|
(cat "$tmp" >"$FILE"; edot $? "replace $FILE")
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
2014-10-12 21:51:38 +04:00
|
|
|
(rm "$tmp"; edot $? "remove temp")
|
|
|
|
eend
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
OPTIMIZE_CONV=1
|
|
|
|
REVERSE_CONV=
|
|
|
|
CONV_FROM=
|
|
|
|
CONV_TO=
|
|
|
|
parse_opts + "${PRETTYOPTS[@]}" \
|
|
|
|
--help '$exit_with display_help' \
|
|
|
|
-N,--detect-always OPTIMIZE_CONV= \
|
|
|
|
-r,--reverse REVERSE_CONV=1 \
|
|
|
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
|
|
|
|
|
|
|
FILE="$1"; shift
|
|
|
|
[ "$FILE" == - ] && FILE=/dev/stdin
|
|
|
|
if [ "$FILE" != /dev/stdin ]; then
|
|
|
|
[ -e "$FILE" ] || die "$FILE: fichier introuvable"
|
|
|
|
fi
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-10-12 21:51:38 +04:00
|
|
|
[ $# -gt 0 ] || set -- lf # commande par défaut
|
|
|
|
|
|
|
|
CMD= # la commande finale à lancer pour convertir le flux
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
args=()
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
arg="$1"; shift
|
|
|
|
if [ "$arg" == // ]; then
|
|
|
|
break
|
|
|
|
elif [ "${arg%//}" != "$arg" ]; then
|
|
|
|
local tmp="${arg%//}"
|
|
|
|
if [ -z "${tmp//\\/}" ]; then
|
|
|
|
arg="${arg#\\}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
args=("${args[@]}" "$arg")
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ ${#args[*]} -gt 0 ]; then
|
|
|
|
before_parse_cmd "${args[@]}"
|
|
|
|
setx cmd=parse_cmd "${args[@]}" || die "$cmd"
|
|
|
|
if [ "$FILE" == /dev/stdin ]; then
|
|
|
|
# pas de suivi pour /dev/stdin
|
|
|
|
CMD="${CMD:+$CMD | }$cmd"
|
|
|
|
else
|
|
|
|
CMD="${CMD:+$CMD | }($cmd; edot \$? $cmd)"
|
|
|
|
fi
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
2014-10-12 21:51:38 +04:00
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$FILE" == /dev/stdin ]; then
|
|
|
|
eval "$CMD"
|
|
|
|
elif [ -f "$FILE" ]; then
|
|
|
|
ffconv
|
|
|
|
elif [ -d "$FILE" ]; then
|
|
|
|
setx -a files=find "$FILE" -type f
|
|
|
|
for FILE in "${files[@]}"; do
|
|
|
|
ffconv
|
2013-08-27 15:14:44 +04:00
|
|
|
done
|
2014-10-12 21:51:38 +04:00
|
|
|
else
|
|
|
|
die "$FILE: fichier invalide"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|