-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffenc_converter
executable file
·206 lines (180 loc) · 5.45 KB
/
ffenc_converter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
readonly SCRIPT_NAME="$(basename ${0})"
readonly TAIL=".converted"
readonly TAB_NUM=4
usage()
{
cat << EOF >&2
USAGE: $SCRIPT_NAME [-h | --help]
[--name <pattern>] [--iname <pattern>]
[-f <from encoding> | --from <from encoding>]
[-t <to encoding> | --to <to encoding>]
[-d | --dos2unix] [-m | --mac2unix]
[-o | --override]
[-e | --expand]
FILE
OPTIONS
--name <pattern>
--iname <pattern>
Like --name, but the match is case insensitive.
-f <from encoding>, --from <from encoding>
-t <to encoding>, --to <to encoding>
-d, --dos2unix
-m, --mac2unix
-o, --override
-e, --expand
Convert tabs to spaces.
EOF
exit
}
join_by()
{
local d="${1}"; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}";
}
__log_field_delimiter=': '
readonly CO_NORMAL=$(echo -en "\e[0m")
readonly CO_RED=$(echo -en "\e[31m")
log_info()
{
local line=$(join_by "${__log_field_delimiter}" 'info' "${SCRIPT_NAME}" "${@}")
echo -e "${line}" >&2
}
log_error()
{
local line=$(join_by "${__log_field_delimiter}" 'error' "${SCRIPT_NAME}" "${@}")
echo -e "${CO_RED}${line}${CO_NORMAL}" >&2
}
is_number()
{
[[ "${1}" =~ ^-?[0-9]+$ ]]
}
declare -r EC_ILLEGAL_CMD=127 # Command not found
declare -r EC_FATAL_SIGNAL_BASE=128 # Base value for fatal error signal "n"
# Default trap handler
on_trapped()
{
local num name
if is_number "${1}"; then
num="${1}"
name=$(kill -l "${1}")
else
name="${1}"
num=$(kill -l "${1}")
fi
log_info "Interrupted by signal $name"
exit "$((EC_FATAL_SIGNAL_BASE + num))"
}
set_trap_handler()
{
local func="${1}" sig; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
has_command()
{
command -v "${1}" &> /dev/null
}
convert_file()
{
infile="${1}"
if [ -n "$fmt_cmd" ]; then
command="$fmt_cmd -q -k ${override:--n} $infile"
if [ -z "$override" ]; then
command="$command ${1}${TAIL}"
infile="${1}${TAIL}"
fi
rs=$(eval "$command" 2>&1); rv="${?}"
if [ "$rv" != 0 ] || [ "${#rs}" != 0 ]; then
log_error "${1}" "$rs"
#log_error "${1}" 'File format conversion failed'
return 1
fi
fi
if [ -n "$from" ] && [ -n "$to" ]; then
command="iconv -f $from -t $to $infile"
if [ -z "$override" ]; then
command="$command | sponge ${1}${TAIL}"
infile="${1}${TAIL}"
else
command="$command | sponge ${infile}"
fi
rs=$(eval "$command" 2>&1); rv="${?}"
if [ "$rv" != 0 ] || [ "${#rs}" != 0 ]; then
log_error "${1}" "$rs"
#log_error "${1}" 'File encoding conversion failed'
return 1
fi
fi
if [ -n "$toexpand" ]; then
command="expand -t $TAB_NUM $infile"
if [ -z "$override" ]; then
command="$command | sponge ${1}${TAIL}"
else
command="$command | sponge ${infile}"
fi
rs=$(eval "$command" 2>&1); rv="${?}"
if [ "$rv" != 0 ] || [ "${#rs}" != 0 ]; then
log_error "${1}" "$rs"
#log_error "${1}" 'Tabs conversion failed'
return 1
fi
fi
}
convert_dir()
{
names=$(join_by ' -o ' "${patterns[@]}")
file_list=$(eval "find ${1} -type f $names" 2> /dev/null)
for f in $file_list; do
convert_file "$f"
done
}
main()
{
if ! has_command dos2unix || ! has_command mac2unix; then
log_error 'Running this script require the dos2unix<http://waterlan.home.xs4all.nl/dos2unix.html> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command sponge; then
log_error 'Running this script require the moreutils<https://joeyh.name/code/moreutils/> to be installed'
exit "$EC_ILLEGAL_CMD"
fi
# Interrupted script by SIGHUP / SIGINT / SIGQUIT / SIGTERM
set_trap_handler on_trapped SIGHUP SIGINT SIGQUIT SIGTERM
[ "${#}" = 0 ] && usage
opts=$(getopt -o f:t:dmoeh --long name:,iname:,from:,to:,dos2unix,mac2unix,override,expand,help -n "$SCRIPT_NAME" -- "$@")
if [ "${?}" != 0 ]; then
log_error 'Failed to parsing options'
usage
fi
patterns=()
eval set -- "$opts"
while true; do
case "${1}" in
--name ) patterns+=("-name '${2}'"); shift 2 ;;
--iname ) patterns+=("-iname '${2}'"); shift 2 ;;
-f | --from ) from="${2}"; shift 2 ;;
-t | --to ) to="${2}"; shift 2 ;;
-d | --dos2unix ) fmt_cmd='dos2unix'; shift ;;
-m | --mac2unix ) fmt_cmd='mac2unix'; shift ;;
-o | --override ) override='-o'; shift ;;
-e | --expand ) toexpand=1; shift ;;
-h | --help) usage ;;
-- ) shift; break ;;
* ) log_error 'Internal error'; usage ;;
esac
done
[ -z "$fmt_cmd" ] && [ -z "$from" -o -z "$to" ] && usage
[ "${#}" -eq 0 ] && usage
while [ "${#}" -gt 0 ]; do
if [ -f "${1}" ]; then
convert_file "${1}"
elif [ -d "${1}" ]; then
convert_dir "${1}"
else
logerr "${1}" 'Target does not exist'
fi
shift
done
}
main "${@}"