This file is indexed.

/usr/share/tcltk/tcllib1.16/fileutil/multiop.tcl is in tcllib 1.16-dfsg-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# ### ### ### ######### ######### #########
##
# (c) 2007-2008 Andreas Kupries.

# DSL allowing the easy specification of multi-file copy and/or move
# and/or deletion operations. Alternate names would be scatter/gather
# processor, or maybe even assembler.

# Examples:
# (1) copy
#     into [installdir_of tls]
#     from c:/TDK/PrivateOpenSSL/bin
#     the  *.dll
#
# (2) move
#     from /sources
#     into /scratch
#     the  *
#     but not *.html
#  (Alternatively: except for *.html)
#
# (3) into /scratch
#     from /sources
#     move
#     as   pkgIndex.tcl
#     the  index
#
# (4) in /scratch
#     remove
#     the *.txt

# The language is derived from the parts of TclApp's option language
# dealing with files and their locations, yet not identical. In parts
# simplified, in parts more capable, keyword names were changed
# throughout.

# Language commands

# From the examples
#
# into        DIR           : Specify destination directory.
# in          DIR           : See 'into'.
# from        DIR           : Specify source directory.
# the         PATTERN (...) : Specify files to operate on.
# but not     PATTERN       : Specify exceptions to 'the'.
# but exclude PATTERN       : Specify exceptions to 'the'.
# except for  PATTERN       : See 'but not'.
# as          NAME          : New name for file.
# move                      : Move files.
# copy                      : Copy files.
# remove                    : Delete files.
#
# Furthermore
#
# reset     : Force to defaults.
# cd    DIR : Change destination to subdirectory.
# up        : Change destination to parent directory.
# (         : Save a copy of the current state.
# )         : Restore last saved state and make it current.

# The main active element is the command 'the'. In other words, this
# command not only specifies the files to operate on, but also
# executes the operation as defined in the current state. All other
# commands modify the state to set the operation up, and nothing
# else. To allow for a more natural syntax the active command also
# looks ahead for the commands 'as', 'but', and 'except', and executes
# them, like qualifiers, so that they take effect as if they had been
# written before. The command 'but' and 'except use identical
# constructions to handle their qualifiers, i.e. 'not' and 'for'.

# Note that the fact that most commands just modify the state allows
# us to use more off forms as specifications instead of just natural
# language sentences For example the example 2 can re-arranged into:
#
# (5) from /sources
#     into /scratch
#     but not *.html
#     move
#     the  *
#
# and the result is still a valid specification.

# Further note that the information collected by 'but', 'except', and
# 'as' is automatically reset after the associated 'the' was
# executed. However no other state is reset in that manner, allowing
# the user to avoid repetitions of unchanging information. Lets us for
# example merge the examples 2 and 3. The trivial merge is:

# (6) move
#     into /scratch
#     from /sources
#     the  *
#     but not *.html not index
#     move
#     into /scratch
#     from /sources
#     the  index
#     as   pkgIndex.tcl
#
# With less repetitions
#
# (7) move
#     into /scratch
#     from /sources
#     the  *
#     but not *.html not index
#     the  index
#     as   pkgIndex.tcl

# I have not yet managed to find a suitable syntax to specify when to
# add a new extension to the moved/copied files, or have to strip all
# extensions, a specific extension, or even replace extensions.

# Other possibilities to muse about: Load the patterns for 'not'/'for'
# from a file ... Actually, load the whole exceptions from a file,
# with its contents a proper interpretable word list. Which makes it
# general processing of include files.

# ### ### ### ######### ######### #########
## Requisites

# This processor uses the 'wip' word list interpreter as its
# foundation.

package require fileutil      ; # File testing
package require snit          ; # OO support
package require struct::stack ; # Context stack
package require wip           ; # DSL execution core

# ### ### ### ######### ######### #########
## API & Implementation

snit::type ::fileutil::multi::op {
    # ### ### ### ######### ######### #########
    ## API

    constructor {args} {} ; # create processor

    # ### ### ### ######### ######### #########
    ## API - Implementation.

    constructor {args} {
	install stack using struct::stack ${selfns}::stack
	$self wip_setup

	# Mapping dsl commands to methods.
	defdva \
	    reset  Reset	(    Push	)       Pop	\
	    into   Into		in   Into	from    From	\
	    cd     ChDir	up   ChUp	as      As	\
	    move   Move		copy Copy	remove  Remove	\
	    but    But		not  Exclude	the     The	\
	    except Except	for  Exclude    exclude Exclude \
	    to     Into         ->   Save       the-set TheSet  \
	    recursive Recursive recursively Recursive           \
	    for-win     ForWindows   for-unix   ForUnix         \
	    for-windows ForWindows   expand     Expand          \
	    invoke Invoke strict Strict !strict NotStrict \
	    files  Files  links  Links  all Everything    \
	    dirs   Directories directories Directories    \
	    state? QueryState from? QueryFrom into? QueryInto \
	    excluded? QueryExcluded as? QueryAs type? QueryType \
	    recursive? QueryRecursive operation? QueryOperation \
	    strict? QueryStrict !recursive NotRecursive

	$self Reset
	runl $args
	return
    }

    destructor {
	$mywip destroy
	return
    }

    method do {args} {
	return [runl $args]
    }

    # ### ### ### ######### ######### #########
    ## DSL Implementation
    wip::dsl

    # General reset of processor state
    method Reset {} {
	$stack clear
	set base     ""
	set alias    ""
	set op       ""
	set recursive 0 
	set src      ""
	set excl     ""
	set types    {}
	set strict   0
	return
    }

    # Stack manipulation
    method Push {} {
	$stack push [list $base $alias $op $opcmd $recursive $src $excl $types $strict]
	return
    }

    method Pop {} {
	if {![$stack size]} {
	    return -code error {Stack underflow}
	}
	foreach {base alias op opcmd recursive src excl types strict} [$stack pop] break
	return
    }

    # Destination directory
    method Into {dir} {
	if {$dir eq ""} {set dir [pwd]}
	if {$strict && ![fileutil::test $dir edr msg {Destination directory}]} {
	    return -code error $msg
	}
	set base $dir
	return
    }

    method ChDir {dir} { $self Into [file join    $base $dir] ; return }
    method ChUp  {}    { $self Into [file dirname $base]      ; return }

    # Detail
    method As {fname} {
	set alias [ForceRelative $fname]
	return
    }

    # Operations
    method Move   {} { set op move   ; return }
    method Copy   {} { set op copy   ; return }
    method Remove {} { set op remove ; return }
    method Expand {} { set op expand ; return }

    method Invoke {cmdprefix} {
	set op    invoke
	set opcmd $cmdprefix
	return
    }

    # Operation qualifier
    method Recursive    {} { set recursive 1 ; return }
    method NotRecursive {} { set recursive 0 ; return }

    # Source directory
    method From {dir} {
	if {$dir eq ""} {set dir [pwd]}
	if {![fileutil::test $dir edr msg {Source directory}]} {
	    return -code error $msg
	}
	set src $dir
	return
    }

    # Exceptions
    method But    {} { run_next_while {not exclude} ; return }
    method Except {} { run_next_while {for}         ; return }

    method Exclude {pattern} {
	lappend excl $pattern
	return
    }

    # Define the files to operate on, and perform the operation.
    method The {pattern} {
	run_next_while {as but except exclude from into in to files dirs directories links all}

	switch -exact -- $op {
	    invoke {Invoke [Resolve [Remember [Exclude [Expand $src  $pattern]]]]}
	    move   {Move   [Resolve [Remember [Exclude [Expand $src  $pattern]]]]}
	    copy   {Copy   [Resolve [Remember [Exclude [Expand $src  $pattern]]]]}
	    remove {Remove          [Remember [Exclude [Expand $base $pattern]]] }
	    expand {                 Remember [Exclude [Expand $base $pattern]]  }
	}

	# Reset the per-pattern flags of the resolution context back
	# to their defaults, for the next pattern.

	set alias    {}
	set excl     {}
	set recursive 0
	return
    }

    # Like 'The' above, except that the fileset is taken from the
    # specified variable. Semi-complementary to 'Save' below.
    # Exclusion data and recursion info do not apply for this, this is
    # already implicitly covered by the set, when it was generated.

    method TheSet {varname} {
	# See 'Save' for the levels we jump here.
	upvar 5 $varname var

	run_next_while {as from into in to}

	switch -exact -- $op {
	    invoke {Invoke [Resolve $var]}
	    move   {Move   [Resolve $var]}
	    copy   {Copy   [Resolve $var]}
	    remove {Remove          $var }
	    expand {
		return -code error "Expansion does not make sense\
                                    when we already have a set of files."
	    }
	}

	# Reset the per-pattern flags of the resolution context back
	# to their defaults, for the next pattern.

	set alias    {}
	return
    }

    # Save the last expansion result to a variable for use by future commands.

    method Save {varname} {
	# Levels to jump. Brittle.
	# 5: Caller
	# 4:   object do ...
	# 3:     runl
	# 2:       wip::runl
	# 1:         run_next
	# 0: Here
	upvar 5 $varname v
	set v $lastexpansion
	return
    }

    # Platform conditionals ...

    method ForUnix {} {
	global tcl_platform
	if {$tcl_platform(platform) eq "unix"} return
	# Kill the remaining code. This effectively aborts processing.
	replacel {}
	return
    }

    method ForWindows {} {
	global tcl_platform
	if {$tcl_platform(platform) eq "windows"} return
	# Kill the remaining code. This effectively aborts processing.
	replacel {}
	return
    }

    # Strictness

    method Strict {} {
	set strict 1
	return
    }

    method NotStrict {} {
	set strict 0
	return
    }

    # Type qualifiers

    method Files {} {
	set types files
	return
    }

    method Links {} {
	set types links
	return
    }

    method Directories {} {
	set types dirs
	return
    }

    method Everything {} {
	set types {}
	return
    }

    # State interogation

    method QueryState {} {
	return [list \
		    from      $src \
		    into      $base \
		    as        $alias \
		    op        $op \
		    excluded  $excl \
		    recursive $recursive \
		    type      $types \
		    strict    $strict \
		   ]
    }
    method QueryExcluded {} {
	return $excl
    }
    method QueryFrom {} {
	return $src
    }
    method QueryInto {} {
	return $base
    }
    method QueryAs {} {
	return $alias
    }
    method QueryOperation {} {
	return $op
    }
    method QueryRecursive {} {
	return $recursive
    }
    method QueryType {} {
	return $types
    }
    method QueryStrict {} {
	return $strict
    }

    # ### ### ### ######### ######### #########
    ## DSL State

    component stack       ; # State stack     - ( )
    variable  base     "" ; # Destination dir - into, in, cd, up
    variable  alias    "" ; # Detail          - as
    variable  op       "" ; # Operation       - move, copy, remove, expand, invoke
    variable  opcmd    "" ; # Command prefix for invoke.
    variable  recursive 0 ; # Op. qualifier: recursive expansion?
    variable  src      "" ; # Source dir      - from
    variable  excl     "" ; # Excluded files  - but not|exclude, except for
    # incl                ; # Included files  - the (immediate use)
    variable types     {} ; # Limit glob/find to specific types (f, l, d).
    variable strict    0  ; # Strictness of into/Expand

    variable lastexpansion "" ; # Area for last expansion result, for 'Save' to take from.

    # ### ### ### ######### ######### #########
    ## Internal -- Path manipulation helpers.

    proc ForceRelative {path} {
	set pathtype [file pathtype $path]
	switch -exact -- $pathtype {
	    relative {
		return $path
	    }
	    absolute {
		# Chop off the first element in the path, which is the
		# root, either '/' or 'x:/'. If this was the only
		# element assume an empty path.

		set path [lrange [file split $path] 1 end]
		if {![llength $path]} {return {}}
		return [eval [linsert $path 0 file join]]
	    }
	    volumerelative {
		return -code error {Unable to handle volumerelative path, yet}
	    }
	}

	return -code error \
	    "file pathtype returned unknown type \"$pathtype\""
    }

    proc ForceAbsolute {path} {
	return [file join [pwd] $path]
    }

    # ### ### ### ######### ######### #########
    ## Internal - Operation execution helpers

    proc Invoke {files} {
	upvar 1 base base src src opcmd opcmd
	uplevel #0 [linsert $opcmd end $src $base $files]
	return
    }

    proc Move {files} {
	upvar 1 base base src src

	foreach {s d} $files {
	    set s [file join $src  $s]
	    set d [file join $base $d]

	    file mkdir [file dirname $d]
	    file rename -force $s $d
	}
	return
    }

    proc Copy {files} {
	upvar 1 base base src src

	foreach {s d} $files {
	    set s [file join $src  $s]
	    set d [file join $base $d]

	    file mkdir [file dirname $d]
	    if {
		[file isdirectory $s] &&
		[file exists      $d] &&
		[file isdirectory $d]
	    } {
		# Special case: source and destination are
		# directories, and the latter exists. This puts the
		# source under the destination, and may even prevent
		# copying at all. The semantics of the operation is
		# that the source is the destination. We avoid the
		# trouble by copying the contents of the source,
		# instead of the directory itself.
		foreach path [glob -directory $s *] {
		    file copy -force $path $d
		}
	    } else {
		file copy -force $s $d
	    }
	}
	return
    }

    proc Remove {files} {
	upvar 1 base base

	foreach f $files {
	    file delete -force [file join $base $f]
	}
	return
    }

    # ### ### ### ######### ######### #########
    ## Internal -- Resolution helper commands

    typevariable tmap -array {
	files {f TFile}
	links {l TLink}
	dirs  {d TDir}
	{}    {{} {}}
    }

    proc Expand {dir pattern} {
	upvar 1 recursive recursive strict strict types types tmap tmap
	# FUTURE: struct::list filter ...

	set files {}
	if {$recursive} {
	    # Recursion through the entire directory hierarchy, save
	    # all matching paths.

	    set filter [lindex $tmap($types) 1]
	    if {$filter ne ""} {
		set filter [myproc $filter]
	    }

	    foreach f [fileutil::find $dir $filter] {
		if {![string match $pattern [file tail $f]]} continue
		lappend files [fileutil::stripPath $dir $f]
	    }
	} else {
	    # No recursion, just scan the whole directory for matching paths.
	    # check for specific types integrated.

	    set filter [lindex $tmap($types) 0]
	    if {$filter ne ""} {
		foreach f [glob -nocomplain -directory $dir -types $filter -- $pattern] {
		    lappend files [fileutil::stripPath $dir $f]
		}
	    } else {
		foreach f [glob -nocomplain -directory $dir -- $pattern] {
		    lappend files [fileutil::stripPath $dir $f]
		}
	    }
	}

	if {[llength $files]} {return $files}
	if {!$strict}         {return {}}

	return -code error \
	    "No files matching pattern \"$pattern\" in directory \"$dir\""
    }

    proc TFile {f} {file isfile $f}
    proc TDir  {f} {file isdirectory $f}
    proc TLink {f} {expr {[file type $f] eq "link"}}

    proc Exclude {files} {
	upvar 1 excl excl

	# FUTURE: struct::list filter ...
	set res {}
	foreach f $files {
	    if {[IsExcluded $f $excl]} continue
	    lappend res $f
	}
	return $res
    }

    proc IsExcluded {f patterns} {
	foreach p $patterns {
	    if {[string match $p $f]} {return 1}
	}
	return 0
    }

    proc Resolve {files} {
	upvar 1 alias alias
	set res {}
	foreach f $files {

	    # Remember alias for processing and auto-invalidate to
	    # prevent contamination of the next file.

	    set thealias $alias
	    set alias    ""

	    if {$thealias eq ""} {
		set d $f
	    } else {
		set d [file dirname $f]
		if {$d eq "."} {
		    set d $thealias
		} else {
		    set d [file join $d $thealias]
		}
	    }

	    lappend res $f $d
	}
	return $res
    }

    proc Remember {files} {
	upvar 1 lastexpansion lastexpansion
	set lastexpansion $files
	return $files
    }

    ##
    # ### ### ### ######### ######### #########
}

# ### ### ### ######### ######### #########
## Ready

package provide fileutil::multi::op 0.5.3