-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsub
More file actions
executable file
·529 lines (457 loc) · 16.2 KB
/
sub
File metadata and controls
executable file
·529 lines (457 loc) · 16.2 KB
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/awk -f
# usage: sub -- [OPTIONS] SEARCH REPLACE [FILE(s)]
# sub -- [OPTIONS] -f PATTERN_FILE [FILE(s)]
#
# The '--' is required, so AWK itself doesn't read the options
#
# In the 1st form, replace SEARCH with REPLACE in FILE(s), or stdin if no files
# are provided or a target filename is '-'.
#
# In the 2nd form, read SEARCH and REPLACE strings from PATTERN_FILE, and
# replace each SEARCH string with the appropriate REPLACE string.
# The file format is as follows:
# SEARCH
# REPLACE
# SEARCH
# REPLACE
# Each REPLACE string corresponds to the SEARCH string on the previous line. If
# '-' is used as PATTERN_FILE, the list of patterns will be read from the
# standard input and target FILEs are required. If there is an uneven number of
# lines, the last SEARCH string will not be used. Empty SEARCH strings and their
# corresponding REPLACE strings will be silently ignored.
#
# Options:
# -f, --file FILE read SEARCH and REPLACE strings from FILE, instead of the
# positional parameters. can be used multiple times for more
# than one pattern file
# -g, --global replaces all matches on each line, instead of just the first
# -i, --in-place edit FILE(s) in place, instead of printing to stdout. this
# option is ignored when reading the standard input
# -h, --help display this help and exit
#
# SEARCH strings must be non-empty, but REPLACE strings may be empty to replace
# SEARCH with nothing. (note that an argument (or line with -f) is still
# required, but it may be the empty string)
## usage: getopts(optstring [, longopt_array ])
## Parses options, and deletes them from ARGV. "optstring" is of the form
## "ab:c". Each letter is a possible option. If the letter is followed by a
## colon (:), then the option requires an argument. If an argument is not
## provided, or an invalid option is given, getopts will print the appropriate
## error message and return "?". Returns each option as it's read, and -1 when
## no options are left. "optind" will be set to the index of the next
## non-option argument when finished. "optarg" will be set to the option's
## argument, when provided. If not provided, "optarg" will be empty. "optname"
## will be set to the current option, as provided. Getopts will delete each
## option and argument that it successfully reads, so awk will be able to treat
## whatever's left as filenames/assignments, as usual. If provided,
## "longopt_array" is the name of an associative array that maps long options to
## the appropriate short option (do not include the hyphens on either).
## Sample usage can be found in the examples dir, with gawk extensions, or in
## the ogrep script for a POSIX example: https://github.com/e36freak/ogrep
function getopts(optstring, longarr, opt, trimmed, hasarg, repeat) {
hasarg = repeat = 0;
optarg = "";
# increment optind
optind++;
# return -1 if the current arg is not an option or there are no args left
if (ARGV[optind] !~ /^-/ || optind >= ARGC) {
return -1;
}
# if option is "--" (end of options), delete arg and return -1
if (ARGV[optind] == "--") {
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
return -1;
}
# if the option is a long argument...
if (ARGV[optind] ~ /^--/) {
# trim hyphens
trimmed = substr(ARGV[optind], 3);
# if of the format --foo=bar, split the two. assign "bar" to optarg and
# set hasarg to 1
if (trimmed ~ /=/) {
optarg = trimmed;
sub(/=.*/, "", trimmed); sub(/^[^=]*=/, "", optarg);
hasarg = 1;
}
# invalid long opt
if (!(trimmed in longarr)) {
printf("unrecognized option -- '%s'\n", ARGV[optind]) > "/dev/stderr";
return "?";
}
opt = longarr[trimmed];
# set optname by prepending dashes to the trimmed argument
optname = "--" trimmed;
# otherwise, it is a short option
} else {
# remove the hyphen, and get just the option letter
opt = substr(ARGV[optind], 2, 1);
# set trimmed to whatevers left
trimmed = substr(ARGV[optind], 3);
# invalid option
if (!index(optstring, opt)) {
printf("invalid option -- '%s'\n", opt) > "/dev/stderr";
return "?";
}
# if there is more to the argument than just -o
if (length(trimmed)) {
# if option requires an argument, set the rest to optarg and hasarg to 1
if (index(optstring, opt ":")) {
optarg = trimmed;
hasarg = 1;
# otherwise, prepend a hyphen to the rest and set repeat to 1, so the
# same arg is processed again without the first option
} else {
ARGV[optind] = "-" trimmed;
repeat = 1;
}
}
# set optname by prepending a hypen to opt
optname = "-" opt;
}
# if the option requires an arg and hasarg is 0
if (index(optstring, opt ":") && !hasarg) {
# increment optind, check if no arguments are left
if (++optind >= ARGC) {
printf("option requires an argument -- '%s'\n", optname) > "/dev/stderr";
return "?";
}
# set optarg
optarg = ARGV[optind];
# if repeat is set, decrement optind so we process the same arg again
# mutually exclusive to needing an argument, otherwise hasarg would be set
} else if (repeat) {
optind--;
}
# delete all arguments up to this point, just to make sure
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
# return the option letter
return opt;
}
## usage: lsub(str, repl [, in])
## substites the string "repl" in place of the first instance of "str" in the
## string "in" and returns the result. does not modify the original string.
## if "in" is not provided, uses $0.
function lsub(str, rep, val, len, i) {
# if "in" is not provided, use $0
if (!length(val)) {
val = $0;
}
# get the length of val, in order to know how much of the string to remove
if (!(len = length(str))) {
# if "str" is empty, just prepend "rep" and return
val = rep val;
return val;
}
# substitute val for rep
if (i = index(val, str)) {
val = substr(val, 1, i - 1) rep substr(val, i + len);
}
# return the result
return val;
}
## usage: glsub(str, repl [, in])
## behaves like lsub, except it replaces all occurances of "str"
function glsub(str, rep, val, out, len, i, a, l) {
# if "in" is not provided, use $0
if (!length(val)) {
val = $0;
}
# empty the output string
out = "";
# get the length of val, in order to know how much of the string to remove
if (!(len = length(str))) {
# if "str" is empty, adds "rep" between every character and returns
l = split(val, a, //);
for (i=1; i<=l; i++) {
out = out rep a[i];
}
return out rep;
}
# loop while 'val' is in 'str'
while (i = index(val, str)) {
# append everything up to the search string, and the replacement, to out
out = out substr(val, 1, i - 1) rep;
# remove everything up to and including the first instance of str from val
val = substr(val, i + len);
}
# append whatever is left in val to out and return
return out val;
}
## usage: mktemp(template [, type])
## creates a temporary file or directory, safely, and returns its name.
## if template is not a pathname, the file will be created in ENVIRON["TMPDIR"]
## if set, otherwise /tmp. the last six characters of template must be "XXXXXX",
## and these are replaced with a string that makes the filename unique. type, if
## supplied, is either "f", "d", or "u": for file, directory, or dry run (just
## returns the name, doesn't create a file), respectively. If template is not
## provided, uses "tmp.XXXXXX". Files are created u+rw, and directories u+rwx,
## minus umask restrictions. returns -1 if an error occurs.
function mktemp(template, type,
c, chars, len, dir, dir_esc, rstring, i, out, out_esc, umask,
cmd) {
# portable filename characters
c = "012345689ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
len = split(c, chars, "");
# make sure template is valid
if (length(template)) {
if (template !~ /XXXXXX$/) {
return -1;
}
# template was not supplied, use the default
} else {
template = "tmp.XXXXXX";
}
# make sure type is valid
if (length(type)) {
if (type !~ /^[fdu]$/) {
return -1;
}
# type was not supplied, use the default
} else {
type = "f";
}
# if template is a path...
if (template ~ /\//) {
dir = template;
sub(/\/[^/]*$/, "", dir);
sub(/.*\//, "", template);
# template is not a path, determine base dir
} else {
if (length(ENVIRON["TMPDIR"])) {
dir = ENVIRON["TMPDIR"];
} else {
dir = "/tmp";
}
}
# escape dir for shell commands
esc_dir = dir;
sub(/'/, "'\\''", esc_dir);
esc_dir = "'" esc_dir "'";
# if this is not a dry run, make sure the dir exists
if (type != "u" && system("test -d " esc_dir)) {
return -1;
}
# get the base of the template, sans Xs
template = substr(template, 0, length(template) - 6);
# generate the filename
do {
rstring = "";
for (i=0; i<6; i++) {
c = chars[int(rand() * len) + 1];
rstring = rstring c;
}
out_esc = out = dir "/" template rstring;
sub(/'/, "'\\''", out_esc);
out_esc = "'" out_esc "'";
} while (!system("test -e " out_esc));
# if needed, create the filename
if (type == "f") {
system("touch " out_esc);
cmd = "umask";
cmd | getline umask;
close(cmd);
umask = substr(umask, 2, 1);
system("chmod 0" 6 - umask "00 " out_esc);
} else if (type == "d") {
system("mkdir " out_esc);
cmd = "umask";
cmd | getline umask;
close(cmd);
umask = substr(umask, 2, 1);
system("chmod 0" 7 - umask "00 " out_esc);
}
# return the filename
return out;
}
## usage: shell_esc(string)
## returns the string escaped so that it can be used in a shell command
function shell_esc(str) {
gsub(/'/, "'\\''", str);
return "'" str "'";
}
# print usage info
function usage() {
printf("%s\n%s\n\n%s\n\n%s\n%s\n\n",
"usage: sub -- [OPTIONS] SEARCH REPLACE [FILE(s)]",
" sub -- [OPTIONS] -f PATTERN_FILE [FILE(s)]",
"The '--' is required, so AWK itself doesn't read the options",
"In the 1st form, replace SEARCH with REPLACE in FILE(s), or stdin if no files",
"are provided or a target filename is '-'." \
) > "/dev/stderr";
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n",
"In the 2nd form, read SEARCH and REPLACE strings from PATTERN_FILE, and",
"replace each SEARCH string with the appropriate REPLACE string.",
"The file format is as follows:",
" SEARCH", " REPLACE", " SEARCH", " REPLACE",
"Each REPLACE string corresponds to the SEARCH string on the previous line. If",
"'-' is used as PATTERN_FILE, the list of patterns will be read from the",
"standard input and target FILEs are required. If there is an uneven number of",
"lines, the last SEARCH string will not be used. Empty SEARCH strings and their",
"corresponding REPLACE strings will be silently ignored." \
) > "/dev/stderr";
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n",
" Options:",
" -f, --file FILE read SEARCH and REPLACE strings from FILE, instead of the",
" positional parameters. can be used multiple times for more",
" than one pattern file",
" -g, --global replaces all matches on each line, instead of just the first",
" -i, --in-place edit FILE(s) in place, instead of printing to stdout. this",
" option is ignored when reading the standard input",
" -h, --help display this help and exit" \
) > "/dev/stderr";
printf("%s\n%s\n%s\n",
"SEARCH strings must be non-empty, but REPLACE strings may be empty to replace",
"SEARCH with nothing. (note that an argument (or line with -f) is still",
"required, but it may be the empty string)" \
) > "/dev/stderr";
}
BEGIN {
# initialize variables to defaults
err = has_tmp = 0;
global = in_place = has_patts = patterns = patts_from_stdin = 0;
# map long options to short options
longopts["file"] = "f";
longopts["global"] = "g";
longopts["in-place"] = "i";
longopts["help"] = "h";
# parse options
while ((opt = getopts("f:gih", longopts)) != -1) {
# -f, --file
if (opt == "f") {
# if FILE is '-', patterns are coming from stdin
if (optarg = "-") {
patts_from_stdin = 1;
# otherwise, check to make sure it is a file or fifo and is readable
} else {
file = shell_esc(optarg);
if ((system("test -f " file) && system("test -p " file)) ||
system("test -r " file)) {
printf("%s: permission denied\n", optarg) > "/dev/stderr";
err = 1; exit;
}
}
# read in each pair of lines from FILE, add to array
while ((getline search < optarg) > 0 && (getline rep < optarg) > 0) {
# search string may not be empty
if (!length(search)) {
continue;
}
patterns++;
searches[patterns] = search;
replaces[patterns] = search;
}
# do not use the positional parameters for the patterns
has_patts = 1;
break;
# -g, --global
} else if (opt == "g") {
global = 1; break;
# -i, --in-place
} else if (opt == "i") {
in_place = 1; break;
# -h, --help
} else if (opt == "h") {
usage(); exit;
# error
} else {
err = 1; exit;
}
}
# if -f was used, make sure everything is valid
if (has_patts) {
# if no valid patterns were in the file(s), fail
if (!patterns) {
printf("no valid SEARCH/REPLACE pairs provided\n") > "/dev/stderr";
err = 1; exit;
}
# if stdin was used, make sure there are files given
if (patts_from_stdin && optind >= ARGC) {
printf("no input file\n") > "/dev/stderr";
err = 1; exit;
}
# get SEARCH and REPLACE from ARGV if -f was not used
} else {
# increment optind
optind++;
if (optind >= ARGC || !length(ARGV[optind-1])) {
printf("no SEARCH or REPLACE string provided\n") > "/dev/stderr";
err = 1; exit;
}
# assign strings to array
patterns++;
searches[patterns] = ARGV[optind-1];
replaces[patterns] = ARGV[optind];
# delete the args
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
}
}
# before reading each input file, if -i was passed...
FNR == 1 && in_place {
# if it's not the first file, overwrite the previous file with its temp file
if (has_tmp) {
close(tmp);
system("mv " shell_esc(tmp) " " shell_esc(FILENAME));
}
# create temp file and add temp file name to the temps array
tmp = mktemp();
temps[tmp];
has_tmp = 1;
}
# actually do the substitutions
{
# loop over each pattern pair
for (p=1; p<=patterns; p++) {
# substitute
if (global) {
$0 = glsub(searches[p], replaces[p]);
} else {
$0 = lsub(searches[p], replaces[p]);
}
}
# print line, to stdout or temp file
if (in_place) {
print > tmp;
} else {
print;
}
}
# clean up on exit, and exit according to "err"
END {
if (has_tmp) {
# if -i was used, overwrite the last file with its temp file
if (in_place) {
close(tmp);
system("mv " shell_esc(tmp) " " shell_esc(FILENAME));
}
# clean up any remaining temp files
for (tmp in temps) {
system("rm -f " shell_esc(tmp));
}
}
exit err;
}
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.