-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplumb
More file actions
executable file
·125 lines (91 loc) · 2.22 KB
/
Copy pathplumb
File metadata and controls
executable file
·125 lines (91 loc) · 2.22 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
#!/usr/bin/env tclsh
#
# minimal "plumber"
fconfigure stdout -translation lf
fconfigure stderr -translation lf
set plumbfile "~/.plumb"
set plumbing_rules {}
set ws [pwd]
set attrs [dict create]
set info 0
proc Usage {{code 1}} {
puts stderr {usage: plumb [-p FILENAME] [-h] [-a NAME=VAL] [-w DIR] [-i] [--] STRING ...}
exit $code
}
proc Plumb {pat code} {
global plumbing_rules
lappend plumbing_rules [list $pat $code]
}
proc GetArg {{i 1}} {
global command_arguments
return [lindex $command_arguments $i]
}
proc TempFile {} {
global env
set tmpdir "/tmp"
if {[info exists env(TMPDIR)]} {
set tmpdir $env(TMPDIR)
}
return "$tmpdir/0.[pid].[expr rand()]"
}
proc GetAttr {name {default ""}} {
global attrs
if {[dict exists $attrs $name]} {
return [dict get $attrs $name]
}
return $default
}
proc Run args {
exec {*}$args 2>@ stderr < /dev/null &
}
proc RunOutput args {
exec {*}$args 2>@ stderr < /dev/null | ma-eval &
}
if {[info exists env(HERE)] && [file exists $env(HERE)/lib/plumb]} {
source $env(HERE)/lib/plumb
}
set str ""
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
switch -regex -- $arg {
{^--?h(elp)?$} {Usage 0}
{^-p$} {
incr i
set plumbfile [lindex $argv $i]
}
{^-w$} {
incr i
cd [lindex $argv $i]
}
{^-a$} {
incr i
set arg [lindex $argv $i]
if {[regexp {^([^=]+)=(\S+)$} $arg _ n v]} {
dict set attrs $n $v
} else Usage
}
{^-i$} {set info 1}
{^--$} {
set str [concat $str [lrange $argv [expr $i + 1] end]]
set i $argc
}
{^-} Usage
default {lappend str $arg}
}
}
if {[file exists $plumbfile]} {
source $plumbfile
}
close stdout
set str [string trim [join $str]]
set mode [GetAttr mode plumb]
set rules $plumbing_rules
foreach r $rules {
set command_arguments [regexp -inline -- [lindex $r 0] $str]
if {$command_arguments != ""} {
if {$info} {puts stderr $command_arguments}
set x [apply [list {} [lindex $r 1]]]
if {$x != 0} exit
}
}
exit 1