-
Notifications
You must be signed in to change notification settings - Fork 0
/
lre-find
executable file
·831 lines (740 loc) · 21.6 KB
/
lre-find
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#!/usr/bin/env lua
-- Copyright (C) 2015 Jeff Backus([email protected])
--
-- 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.
local version = "0";
local copyright = " v" .. version .. " Copyright (C) 2015 Jeff Backus";
local lfs = require "lfs";
-- Put filesystem delimiter into a global variable for later.
local fs_delim = package.config:sub(1,1)
-- Figure out how we were invoked
local run_as = arg[0]:match("([^"..fs_delim.."]+)$");
-- Attempt to determine the display size on POSIX-compatible systems. On
-- non-POSIX-compatible systems, assume a default value.
local screen_width = 80
do
-- Create a function to attempt to open a program, read the first line
-- of output and return it.
local function try( cmd )
local fin = io.popen( cmd )
if fin then
local t = fin:read()
fin:close()
return t
else
return
end
end
-- First, attempt to use tput, which is probably in the ncurses package...
local bOk, result, code = pcall( try, "/usr/bin/env tput cols" )
if( bOk ) then
screen_width = tonumber(result) or screen_width
else
-- Nope, try stty. We want the second argument.
bOk, result, code = pcall( try, "/usr/bin/env stty size" )
if( bOk ) then
screen_width = tonumber(result:match("(%d+)$")) or screen_width
end
end
end
-- Create a table to handle different options for how we are started
-- Override __index to create a default case.
local run_modes = {}
run_modes.recp = {command="copy"};
run_modes.remv = {command="move"};
run_modes.reln = {command="link"};
run_modes.rerm = {command="remove"};
run_modes.reexec = {command="exec"};
run_modes_mt = {}
run_modes_mt.__index = function(table,key)
return {command="default"}
end
setmetatable(run_modes, run_modes_mt)
-- Forward declarations of global-ish variables
local settings = {}
local options = {}
local showMsgs = {}
local paths = {}
local actions = {}
local transform
local cur_mode
-- Define helper functions
-- Implements verbose, interactive, and dry-run modes. Action function is
-- responsible for providing suitable display string. Returns true if okay
-- to continue or nil if caller should return without attempting action.
local function action_prompt( command )
local out
if( settings.verbose or settings.interactive ) then
out = io.output()
out:write(command)
if( settings.interactive ) then
out:write('? ')
end
out:close()
end
if( settings.interactive ) then
local response = io.read()
if( response:lower() ~= "y" ) then
return nil
end
else
if( settings.verbose or settings.interactive ) then
print("")
end
end
if( settings.dry_run ) then
return nil
end
return true
end
-- Attempts to match the specified pathname against settings.src. If it matches,
-- returns a table of matched elements, or nil otherwise.
local function match_pathname(path)
-- Attempt to match specified path to specified src string and put
-- result into a table.
local elms = { string.match( path, settings.src ) }
-- No match, so just return.
if #elms == 0 then
return
end
-- Build a table that can be passed to string.gsub() as a map of %N->matched
-- value. Place original string into position 0.
local map = {}
local t
if settings.lua then
t = function( i ) return i end
else
t = function( i ) return tostring(i) end
end
for i = 1, #elms do
map[ t(i) ] = elms[ i ]
end
map[ t(0) ] = path
return map
end
-- Default SRC -> DEST transform
-- Receives a table, v, of elements form the pattern match and substitutes
-- those values into settings.dest.
local function default_transform(v)
return string.gsub( cur_mode.dest, "%%(%d)", v )
end
-- Set default transform
transform = default_transform
local function link_item(src, dest)
if( cur_mode.symbolic and settings.absolute ) then
src = lfs.currentdir().."/"..src
end
local cmd = "ln"
if( cur_mode.symbolic ) then
cmd = cmd.." -s"
end
if( not action_prompt(cmd.." '"..src.."' '"..dest.."'") ) then
return
end
local ok, msg, code = lfs.link( src, dest, cur_mode.symbolic )
if( ok == nil ) then
print(" Error creating link from '"..src.."' to '"..dest.."': "..msg)
end
end
local function move_item(src, dest)
if( not action_prompt("mv '"..src.."' '"..dest.."'") ) then
return
end
local ok, msg = os.rename( src, dest )
if( ok == nil ) then
print(" Error renaming '"..src.."' to '"..dest.."': "..msg)
end
end
local function copy_file(src, dest)
if( not action_prompt("cp '"..src.."' '"..dest.."'") ) then
return
end
local BUFSIZE = 2^20 -- 1MB at a time
local fin, fout, msg
fin, msg = io.open(src, "rb")
if( fin == nil ) then
print(" Error reading from '"..src.."': "..msg)
return
end
fout, msg = io.open(dest, "wb")
if( fin == nil ) then
print(" Error writing to '"..dest.."': "..msg)
return
end
while true do
local bytes = fin:read(BUFSIZE)
if not bytes then break end
fout:write(bytes)
end
fin:close()
fout:close()
end
local function remove_item(src, dest)
if( not action_prompt("rm '"..dest.."'") ) then
return
end
local ok, msg = os.remove( dest )
if( ok == nil ) then
print(" Error removing '"..dest.."': "..msg)
end
end
local function exec_item(src, dest)
if( not action_prompt("'"..dest.."'") ) then
return
end
-- See if we are saving all matches for one run or doing one at a time.
if( cur_mode.command ) then
-- If we're queuing, add match to queue
cur_mode.command[ #cur_mode.command + 1 ] = dest
else
-- Otherwise, execute command
local bOk, result, code = os.execute( dest )
if( bOk == nil ) then
print("Error: unable to execute '"..dest.."'!")
return;
end
if( settings.verbose ) then
print(" Returned: "..result.." ("..code..")");
end
end
end
local function print_item(src, dest)
print( dest )
end
-- Create a table to mode-specific settings.
-- Override __index to create a default mode.
local debug_dir_command = function( pathname, newname )
print("Directory '"..pathname.."' -> '"..newname.."'")
end
local single_command = function ( parser ) return parser; end;
local fn_modes = {}
fn_modes.copy = {
desc = "Copy with Lua pattern matching",
parsers = { copy = single_command },
file_action=copy_file,
dir_action=debug_dir_command,
};
fn_modes.move = {
desc = "Move with Lua pattern matching",
parsers = { move = single_command },
file_action=move_item,
dir_action=move_item,
};
fn_modes.link = {
desc = "Create links with Lua pattern matching",
parsers = { link = single_command },
file_action=link_item,
dir_action=link_item,
};
fn_modes.remove = {
desc = "Remove files and directories with Lua pattern matching",
parsers = { remove = single_command },
file_action=remove_item,
dir_action=debug_dir_command,
};
fn_modes.exec = {
desc = "Find and Execute with Lua pattern matching",
parsers = { exec = single_command },
file_action=exec_item,
dir_action=exec_item,
};
fn_modes.print = {
desc = "Copy with Lua pattern matching",
parsers = { print = single_command },
file_action=print_item,
dir_action=print_item,
};
-- Default case. Create a new parser for each mode.
fn_modes_mt = {}
fn_modes_mt.__index = function(table,key)
local ret_val_mt = {}
ret_val_mt.__index = function(table, key)
return function ( parser )
return parser:command(key, fn_modes[key]["description"])
end
end
local ret_val = {}
setmetatable(ret_val, ret_val_mt)
return {
desc = "Tool to move/copy/link files with Lua pattern matching",
parsers = ret_val,
}
end
setmetatable(fn_modes, fn_modes_mt)
local function handle_dir(dir_names)
-- Verify that all entries in dir_names exists, or we'll get an error later.
local idx = 1
while( dir_names[ idx ] ) do
local attrs = lfs.attributes( dir_names[ idx ] )
if( attrs ) then
-- Yep, so increment
idx = idx + 1
else
-- Nope, warn user and remove from list.
print("Invalid Path "..dir_names[ idx ])
table.remove(dir_names, idx )
end
end
-- Track end of list instead of shifting indices. Should be faster...
local list_end = #dir_names + 1
idx = 1
while( dir_names[ idx ] ) do
local dir_name = dir_names[ idx ]
if showMsgs.paths then
print("Searching '"..dir_name.."'...")
end
for dir_obj in lfs.dir(dir_name) do
local pathname = dir_obj
if( dir_name ~= '.' ) then
pathname = dir_name.."/"..pathname
end
local attrs = lfs.attributes(pathname)
-- Share our result
if( dir_obj ~= '.' and dir_obj ~= '..' ) then
coroutine.yield( pathname )
if( attrs.mode == "directory" ) then
-- Append to end of list if recursive mode enabled.
if( settings.recursive ) then
dir_names[ list_end ] = pathname
list_end = list_end + 1
end
end
end
end
-- Efficiently remove this item from the list
dir_names[ idx ] = nil
-- Increment pointer
idx = idx + 1
end
end
local function handle_stdin()
local line
local done
while not done do
line = io.read()
if line then
coroutine.yield(line)
else
done = true
end
end
end
local function print_version()
print(run_as..copyright)
end
local function do_help()
print("Usage: "..run_as.." <pattern> [options]")
print()
print("Searches the filesystem for files/directories matching <pattern>")
print_version()
end
-- Define table of command-line arguments. Format:
-- "argument" = {
-- desc = "description",
-- args = "<arg1> <arg2>"
-- action = function( index ) return next_index end
-- }
--
-- Each action function should take 1 argument, the index into the arg
-- variable, and return the number of arguments consumed
options = {}
options["--help"] = {
desc = "Display this screen.",
action = function( opt_args )
do_help()
os.exit(0)
end
}
options["--version"] = {
desc = "Display version information and exit.",
action = function( opt_args )
print_version()
os.exit(0)
end
}
options["-D"] = {
args = "<debug modes>",
desc = "Displays debug information. Takes a comma-delimited list of debug options",
min_arguments = 1,
max_arguments = 1,
action = function( opt_args )
for i in opt_args[ 1 ]:gmatch("([^,]+)") do
showMsgs[ i ] = true
end
end
}
options["-v"] = {
desc = "Print each action as it is taken.",
action = function( opt_args )
settings.verbose = true
end
}
options["--dry-run"] = {
desc = "Only display actions, don't actually perform them. Implies -v.",
action = function( opt_args )
settings.dry_run = true
settings.verbose = true
end
}
-- Interactive mode
options["-i"] = {
desc = "Prompt before each action.",
action = function( opt_args )
settings.interactive = true
end
}
-- Search and operate recursively
options["-r"] = {
desc = "Search recursively.",
action = function( opt_args )
settings.recursive = true
settings.operate_deep = true
end
}
-- Don't search recursively, but do operate recursively (i.e. deep copy)
options["-R"] = {
desc = "Don't search recursively, but still operate recursively.",
action = function( opt_args )
settings.recursive = nil
settings.operate_deep = true
end
}
-- Only match directories
options["-d"] = {
desc = "Only match directories.",
action = function( opt_args )
settings.dirs_only = true
end
}
-- Only match files
options["-f"] = {
desc = "Only match files.",
action = function( opt_args )
settings.files_only = true
end
}
-- Copy
options["-c"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], then copy match to result. [transform] defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.copy, dest = dest,
name = "copy" }
end
}
-- Move
options["-m"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], then move match to result. [transform] defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.move, dest = dest,
name = "move" }
end
}
-- Hard link
options["-l"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], then create a hard link from match to result. [transform] "..
"defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.link, dest = dest,
symbolic = false, name = "hard link" }
end
}
-- Symbolic link
options["-s"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], then create a symbolic link from match to result. "..
"[transform] defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.link, dest = dest,
symbolic = true, name = "soft link" }
end
}
-- Use absolute path of each match
options["-a"] = {
desc = "Use absolute path when matching.",
action = function( opt_args )
settings.absolute = true
end
}
-- Remove
options["--delete"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], then deletes the result. [transform] defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.remove, dest = dest,
name = "remove"}
end
}
-- Execute once per match
options["-e"] = {
args = "<transform>",
desc = "Substitute all captures from specified match string into "..
"<transform>, then attempt to execute the result as a shell command, "..
"each match executed separately.",
min_arguments = 1,
max_arguments = 1,
action = function( opt_args )
actions[ #actions + 1 ] = { action = fn_modes.exec, dest = opt_args[ 1 ],
name = "exec per match" }
end
}
-- Execute with all matches at once
options["-E"] = {
args = "<command> [transform]",
desc = "Substitute all captures from specified match string into "..
"[transform] and append to <command>, in order elements are found. "..
"Result is executed as a shell command at end of search. [transform] "..
"defaults to %0.",
min_arguments = 1,
max_arguments = 2,
action = function( opt_args )
local dest
if( 1 < #opt_args ) then
dest = opt_args[ 2 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.exec, dest = dest,
command = { opt_args[ 1 ] },
name = "exec all at once",
post = true }
end
}
-- Print
options["-p"] = {
args = "[transform]",
desc = "Substitute all captures from specified match string into "..
"[transform], and display result to SDOUT. [transform] defaults to %0.",
max_arguments = 1,
action = function( opt_args )
local dest
if( 0 < #opt_args ) then
dest = opt_args[ 1 ]
else
dest = "%0"
end
actions[ #actions + 1 ] = { action = fn_modes.print, dest = dest,
name = "print" }
end
}
-- Specify starting search path(s)
options["-P"] = {
args = "<dir1> [dir2] ... [dirN]",
desc = "Search the specified directory/ies.",
min_arguments = 1,
max_arguments = 99,
action = function( opt_args )
for _, p in ipairs( opt_args ) do
paths[ #paths + 1 ] = p
end
end
}
options["--"] = {
desc = "Take list of items from STDIN (instead of crawling a directory tree)",
action = function( opt_args )
settings.use_stdin = true
end
}
-- Interpret DEST as Lua code
-- options["-L"] = {
-- action = function( opt_args )
-- settings.lua = true
-- end
-- }
-- TODO: Options to:
-- * change to parent directory before executing
-- * combine all matches into a single command
-- Handle Arguments
local i = 1
while i <= #arg do
--print("Checking "..tostring(i)..": '"..arg[i].."'")
if( arg[ i ]:sub( 1, 1 ) == '-' ) then
local arglist = {}
-- See if we're working with a long option or one or more short ones.
-- Put option(s) onto arglist to process
if( arg[ i ]:sub( 1, 2 ) == '--') then
-- Long option, stick remainder of string on arglist
arglist = { arg[ i ] }
else
for o in arg[ i ]:sub( 2 ):gmatch( "." ) do
arglist[ #arglist + 1 ] = '-'..o
end
end
-- move index to next item in argument list
i = i + 1
-- Now process each of the options we found earlier.
for _, elm in ipairs( arglist ) do
local opt = options[ elm ]
if opt then
-- Capture any option arguments
local opt_args = {}
local max = opt.max_arguments or 0
local done = ( max <= #opt_args ) or ( #arg < i )
while not done do
-- See if next argument is an option.
if( arg[ i ]:sub( 1, 1 ) == '-' ) then
-- If so, we're done looking for option arguments.
done = true
else
-- If not, tack onto list and see if we've found all option
-- arguments.
opt_args[ #opt_args + 1 ] = arg[ i ]
i = i + 1
done = ( max <= #opt_args ) or ( #arg < i )
end
end
-- Make sure we've met the minimum number of option arguments
local min = opt.min_arguments or 0
if ( #opt_args < min ) then
print("Not enough arguments for '"..elm.."'!")
do_help()
os.exit(1)
end
-- Handle option
options[ elm ].action( opt_args )
else
print("Error: Unknown option '"..elm.."'!")
do_help()
os.exit(1)
end
end
else
settings.src = arg[ i ]
i = i + 1
end
end
-- If no paths specified, assume we start in current one.
if #paths <= 0 then
paths = {'.'}
end
-- If -L is specified, reinterpret DEST as Lua code. Prepend defining a local
-- varsettings that contains the results of the match.
-- if settings.lua then
-- transform = loadstring("local v={...}; v=v[1]; "..settings.dest)
-- end
-- debug
if showMsgs.args then
print("[Begin Debug]")
for k,v in pairs(arg) do print("\t"..k..": '"..tostring(v).."'") end
print("----")
for k,v in pairs(settings) do print("\t"..k..": '"..tostring(v).."'") end
print("Searching:")
for k,v in pairs(paths) do print("\t"..k..": '"..tostring(v).."'") end
print("----")
print("Action Queue:")
for i,action in ipairs(actions) do
if action.dest then
print("\t"..i..": "..action.name.." -> "..action.dest)
else
print("\t"..i..": "..action.name)
end
end
print("")
print("Screen Width: "..screen_width)
print("[End Debug]")
print("")
end
-- End debug
if( (settings.src == nil) or (#settings.src == 0) ) then
print("Please specify a pattern to match!")
print()
do_help()
os.exit(1)
end
-- Wrap a coroutine around the appropriate iterator
if( settings.use_stdin ) then
searcher = coroutine.wrap(function() handle_stdin() end)
else
searcher = coroutine.wrap(function() handle_dir(paths) end)
end
-- Process the output of the iterator
for pathname in searcher do
local attrs = lfs.attributes(pathname);
-- Attempt to match against SRC
local elements = match_pathname( pathname )
for _, mode in ipairs( actions ) do
cur_mode = mode
local newname
if elements then
newname = transform( elements )
end
-- Call appropriate handler if we have a match
if( attrs ~= nil and newname ~= nil ) then
if( attrs.mode == "directory" and settings.files_only == nil ) then
mode.action.dir_action( pathname, newname )
elseif( attrs.mode == "file" and settings.dirs_only == nil ) then
mode.action.file_action( pathname, newname )
end
end
end
end
-- If we have any exec-all-matches-at-once actions, build the string to
-- execute and then execute it for each action.
for _, mode in ipairs( actions ) do
if mode.post then
cur_mode = mode
local dest = table.concat( cur_mode.command, " " )
cur_mode.command = nil
exec_item( "", dest )
end
end