-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsed.sh
executable file
·198 lines (157 loc) · 3.91 KB
/
sed.sh
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
#!/bin/sh
tmpfile=${TMPDIR:-/tmp}/sed.${USER:=$(id)}.$$
sed=/usr/bin/sed
sed_i() {
file=$1 backup_suffix=$2
shift 2
# We can't edit stdin in-place.
[ "$file" = "-" ] &&
file=./-
# Everything following is critical. Enable errexit so we don't
# have to manually handle the exit status of every program.
set -e
# In order to preserve the file permissions, we copy the target
# file.
cp -f -- "$file" "$tmpfile"
# Do the actual in-place edit.
"$sed" "$@" "$file" >"$tmpfile"
# Make a backup if desired.
if [ "$backup_suffix" ]; then
mv -f -- "$file" "$file$backup_suffix"
fi
# Overwrite the original file.
mv -f -- "$tmpfile" "$file"
set +f
}
cleanup() {
rv=$?
trap - EXIT
[ -f "$tmpfile" ] &&
rm -f -- "$tmpfile"
exit "$rv"
}
trap cleanup EXIT HUP INT TERM
unset -v argsunset isparam isoperand hasexpr iflag iparam
for arg do
# In order to be able to append the translated options, unset the
# positional parameters on the first run.
! [ "$argsunset" ] && {
argsunset=1
set --
}
# When option parsing is complete (the current argument is an
# operand) and we don't do in-place editing, we can simply append
# all remaining arguments and eventually run a single $sed at the
# end of the script. Otherwise we run the sed_i function for
# every file argument. $isoperand is set when either "--" or the
# first non-option argument is encountered.
[ "$isoperand" ] && {
if ! [ "$iflag" ]; then
set -- "$@" "$arg"
else
sed_i "$arg" "$iparam" "$@"
fi
continue
}
# The previous option requires a parameter.
[ "$isparam" ] && {
set -- "$@" "$arg"
unset isparam
continue
}
# Parse options.
case $arg in
# "--" marks the end of the options.
--)
isoperand=1
set -- "$@" "$arg"
;;
# The first non-option argument marks the end of the
# options. However, if no -e or -f option was used, this
# argument is used as the sed expression.
[!-]*|-)
isoperand=1
if [ "$hasexpr" ] && [ "$iflag" ]; then
sed_i "$arg" "$iparam" "$@"
else
set -- "$@" "$arg"
fi
;;
# Long options.
################################################
--in-place|--in-place=*)
iflag=1
case $arg in
*=*) iparam=${arg#*=} ;;
esac
;;
--expression|--expression=*|--file|--file=*)
hasexpr=1
case $arg in
--e*) set -- "$@" -e ;;
--f*) set -- "$@" -f ;;
esac
case $arg in
*=*) set -- "$@" "${arg#*=}" ;;
*) isparam=1 ;;
esac
;;
--quiet|--silent)
set -- "$@" -n
;;
--regexp-extended)
set -- "$@" -r
;;
# All other long-options will be passed as is, letting
# $sed complain about unknown options itself.
--?*)
set -- "$@" "$arg"
;;
# Short options.
################################################
# Options with parameters. The first occurence of [efi]
# marks everything after it as the parameter. If the
# parameter is missing, we get it from the next argument.
# Except for -i, where the parameter is optional and has
# to be supplied in the same argument.
-*[efi]*)
arg=${arg#-}
# Extract the last option and its parameter.
param=${arg#*[efi]}
lastopt=${arg%$param}
lastopt=${lastopt##*[!efi]}
# Extract the other options, without the last one
# and its parameter.
opts=${arg%%$lastopt*}
# Other options are simply appended to the
# positional parameters.
[ "$opts" ] &&
set -- "$@" "-$opts"
case $lastopt in
[ef])
hasexpr=1
if [ "$param" ]; then
set -- "$@" "-$lastopt" "$param"
else
set -- "$@" "-$lastopt"
isparam=1
fi
;;
i)
iflag=1
iparam=$param
;;
esac
;;
# All other options will be passed as is, letting $sed
# complain about unknown options itself.
-*)
set -- "$@" "$arg"
;;
esac
done
# If we have a -i option, we are done at this point.
[ "$iflag" ] && exit $?
# Without -i, we can simply run the original sed once, with all the
# translated options and file arguments.
"$sed" "$@"