-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure_bom_crlf
executable file
·133 lines (112 loc) · 3.77 KB
/
ensure_bom_crlf
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
#! /usr/bin/env bash
# ensure_bom_crlf - prepare POSIX text file with UTF-8 data for use on Windows
# Copyright (C) 2022-2024 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
set -u
PROG="$(basename "$0")"
VERSION='2024-09-22-01'
print_version()
{
printf -- '%s version %s\n' "$PROG" "$VERSION"
}
print_copyright()
{
echo 'Copyright (C) 2022-2024 by Erik Auerswald <[email protected]>'
}
print_license()
{
print_copyright
cat <<EOL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
EOL
}
print_usage()
{
cat <<EOU
Usage: $PROG [-r] [[-i] FILE...]
$PROG {-h | -V | -L}
EOU
}
print_help()
{
print_usage
cat <<EOH
Prepare a UTF-8 encoded POSIX text file for use on Windows by ensuring
it starts with the UTF-8 encoded unicode code point U+FEFF "zero-width
no-break space" (the old UCS-2 byte order mark (BOM)), and uses the
<CR><LF> ("\\r\\n") end-of-line character sequence.
Options:
-h print this help and exit
-V print version information and exit
-L print license and exit
-i edit all given FILEs in-place
-r reverse operation: remove BOM and use <LF> ("\\n") as line ending
Examples:
$PROG < utf8_encoded_gnu_text > utf8_encoded_windows_text.txt
$PROG utf8_encoded_gnu_text > utf8_encoded_windows_text.txt
$PROG -i utf8_encoded_text.txt
EOH
}
INPLACE=0
REVERSE=0
while getopts ':hVLir' OPT; do
case "$OPT" in
'h') print_help; exit 0;;
'V') print_version; exit 0;;
'L') print_version; print_license; exit 0;;
'i') INPLACE=1;;
'r') REVERSE=1;;
'?') echo "$PROG: ERROR: unknown option '-$OPTARG'";
print_usage; exit 1;;
':') echo "$PROG: ERROR: argument required for option '-$OPTARG'";
print_usage; exit 1;;
*) echo "$PROG: ERROR: getopts() failure"; exit 1;;
esac
done
shift $((OPTIND - 1))
if test "$REVERSE" -eq 0; then
SEDSCRIPT='1s/^$/'$'\uFEFF''/
1s/^([^'$'\uFEFF''])/'$'\uFEFF''\1/
s/^$/\r/
s/([^\r])$/\1\r/'
else
SEDSCRIPT='1s/^'$'\uFEFF''//
s/\r$//'
fi
test "$INPLACE" -eq 1 && test $# -lt 1 && {
echo "$PROG: ERROR: -i requires a FILE"
exit 1
}
SEDOPTS='-E'
test "$INPLACE" -eq 1 && SEDOPTS="${SEDOPTS}i"
exec sed "$SEDOPTS" "$SEDSCRIPT" -- "$@"