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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/inplace.rb', line 41
def main(argv)
$uninterruptible = $interrupt = false
[:SIGINT, :SIGQUIT, :SIGTERM].each { |sig|
trap(sig) {
if $uninterruptible
$interrupt = true
else
interrupt
end
}
}
usage = "usage: \#{MYNAME} [-Lfinstvz] [-b SUFFIX]\n [-e \"COMMANDLINE\"] [-E COMMAND ... --] [file ...]\n EOF\n\n banner = <<-\"EOF\"\n\#{MYNAME} version \#{Inplace::VERSION}\n\nEdits files in-place through given filter commands.\n\n\#{usage}\n EOF\n\n filters = []\n\n $config = Inplace::Config.new\n [\n File.join(\n ENV.fetch(\"XDG_CONFIG_HOME\") { File.expand_path(\"~/.config\") },\n \"inplace/config\"\n ),\n File.expand_path(\"~/.inplace\"),\n ].each do |file|\n if File.exist?(file)\n $config.load(file)\n break\n end\n end\n\n opts = OptionParser.new(banner, 24) { |opts|\n nextline = \"\\n\" << opts.summary_indent << \" \" * opts.summary_width << \" \"\n\n opts.on(\"-h\", \"--help\",\n \"Show this message.\") {\n print opts\n exit 0\n }\n\n opts.on(\"-L\", \"--dereference\",\n \"Edit the original file for each symlink.\") {\n |b| $dereference = b\n }\n\n opts.on(\"-b\", \"--backup-suffix=SUFFIX\",\n \"Create a backup file with the SUFFIX for each file.\" << nextline <<\n \"Backup files will be written over existing files,\" << nextline <<\n \"if any.\") {\n |s| $backup_suffix = s\n }\n\n opts.on(\"-D\", \"--debug\",\n \"Turn on debug mode.\") {\n |b| $debug = b and $verbose = true\n }\n\n opts.on(\"-e\", \"--execute=COMMANDLINE\",\n \"Run COMMANDLINE for each file in which the following\" << nextline <<\n \"placeholders can be used:\" << nextline <<\n \" %0: replaced by the original file path\" << nextline <<\n \" %1: replaced by the source file path\" << nextline <<\n \" %2: replaced by the destination file path\" << nextline <<\n \" %%: replaced by a %\" << nextline <<\n \"Missing %2 indicates %1 is modified destructively,\" << nextline <<\n \"and missing both %1 and %2 implies \\\"(...) < %1 > %2\\\"\" << nextline <<\n \"around the command line.\") {\n |s| filters << FileFilter.new($config.expand_alias(s))\n }\n\n opts.on(\"-E\", \"--execute-args[=TERM]\",\n \"Run COMMAND with all following arguments until TERM\" << nextline <<\n \"(default: '--') is encountered.\" << nextline <<\n \"This is similar to -e except it takes a list of\" << nextline <<\n \"arguments.\") { |s|\n term = s || '--'\n args = []\n until (arg = argv.shift) == term\n raise \"-E must end with \#{term}\" if arg.nil?\n\n args << arg\n end\n commandline = args.map { |arg|\n case arg\n when /%/\n arg.gsub(/[^A-Za-z0-9_\\-.,:+\\/@\\n%]/, \"\\\\\\\\\\\\&\").gsub(/\\n/, \"'\\n'\")\n else\n arg.shellescape\n end\n }.join(' ')\n filters << FileFilter.new($config.expand_alias(commandline))\n }\n\n opts.on(\"-f\", \"--force\",\n \"Force editing even if a file is read-only.\") {\n |b| $force = b\n }\n\n opts.on(\"-i\", \"--preserve-inode\",\n \"Make sure to preserve the inode number of each file.\") {\n |b| $preserve_inode = b\n }\n\n opts.on(\"-n\", \"--dry-run\",\n \"Just show what would have been done.\") {\n |b| $dry_run = b and $verbose = true\n }\n\n opts.on(\"-s\", \"--same-directory\",\n \"Create a temporary file in the same directory as\" << nextline <<\n \"each replaced file.\") {\n |b| $same_directory = b\n }\n\n opts.on(\"-t\", \"--preserve-timestamp\",\n \"Preserve the modification time of each file.\") {\n |b| $preserve_time = b\n }\n\n opts.on(\"-v\", \"--verbose\",\n \"Turn on verbose mode.\") {\n |b| $verbose = b\n }\n\n opts.on(\"-z\", \"--accept-empty\",\n \"Accept empty (zero-sized) output.\") {\n |b| $accept_empty = b\n }\n }\n\n setup()\n\n argv = argv.dup\n opts.order!(argv)\n files = argv\n\n if filters.empty? && !files.empty?\n filters << FileFilter.new($config.expand_alias(files.shift))\n end\n\n if files.empty?\n STDERR.puts \"No files to process given.\", \"\"\n print opts\n exit 2\n end\n\n case filters.size\n when 0\n STDERR.puts \"No filter command line to execute given.\", \"\"\n print opts\n exit 1\n when 1\n filter = filters.first\n\n files.each { |file|\n begin\n filter.filter!(file, file)\n rescue => e\n STDERR.puts \"\#{file}: skipped: \#{e}\"\n end\n }\n else\n files.each { |file|\n tmpfile = FileFilter.make_tmpfile_for(file)\n\n first, last = 0, filters.size - 1\n\n begin\n filters.each_with_index { |filter, i|\n if i == first\n filter.filter(file, file, tmpfile)\n elsif i == last\n filter.filter(file, tmpfile, file)\n else\n filter.filter!(file, tmpfile)\n end\n }\n rescue => e\n STDERR.puts \"\#{file}: skipped: \#{e}\"\n end\n }\n end\nrescue OptionParser::ParseError => e\n STDERR.puts \"\#{MYNAME}: \#{e}\", usage\n exit 64\nrescue => e\n STDERR.puts \"\#{MYNAME}: \#{e}\"\n exit 1\nend\n"
|