aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-09-04 21:40:56 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2016-09-04 21:40:56 +0300
commit9cc2ffffedd74917bb1a26adf26afc3a4c130c8f (patch)
tree5f6fa9f29f92ca0f22ac11f06f8ea8991cd5c82d
parent013ad815ed55d01af3e9901b8a0b391aa19f42ba (diff)
downloadrex-9cc2ffffedd74917bb1a26adf26afc3a4c130c8f.tar.gz
rex-9cc2ffffedd74917bb1a26adf26afc3a4c130c8f.tar.bz2
Cleanup namespaces
-rwxr-xr-xrex.exp252
1 files changed, 127 insertions, 125 deletions
diff --git a/rex.exp b/rex.exp
index e673648..4229942 100755
--- a/rex.exp
+++ b/rex.exp
@@ -53,105 +53,107 @@ proc config_option {key} {
# and parsing its return.
# #######################################################################
-namespace eval pmres { }
-
-proc ::pmres::hostorder {} {
- global config
-
- if {![info exists config(hostorder)]} {
- set config(hostorder) {files dns}
- if {[catch {open "/etc/nsswitch.conf" "r"} fd] == 0} {
- while {[gets $fd line] >= 0} {
- if [regexp {[^[:space:]]*hosts:} "$line"] {
- set config(hostorder) [lrange [regexp -all -inline {[^[:space:]]+} $line] 1 end]
- break
+namespace eval pmres {
+ proc hostorder {} {
+ global config
+
+ if {![info exists config(hostorder)]} {
+ set config(hostorder) {files dns}
+ if {[catch {open "/etc/nsswitch.conf" "r"} fd] == 0} {
+ while {[gets $fd line] >= 0} {
+ if [regexp {[^[:space:]]*hosts:} "$line"] {
+ set config(hostorder) [lrange [regexp -all -inline {[^[:space:]]+} $line] 1 end]
+ break
+ }
}
+ close $fd
}
- close $fd
}
+ return $config(hostorder)
}
- return $config(hostorder)
-}
-proc ::pmres::files_match {mode arg var} {
- upvar $var res
+ proc files_match {mode arg var} {
+ upvar $var res
- if {[catch {open "/etc/hosts" "r"} fd] == 0} {
- while {[gets $fd line] >= 0} {
- regsub "#.*" $line "" rec
- if {$rec == ""} {
- continue
- }
- if {$mode == "-ip"} {
- if {[lindex $rec 0] == $arg} {
- eval lappend res [lrange $rec 1 end]
+ if {[catch {open "/etc/hosts" "r"} fd] == 0} {
+ while {[gets $fd line] >= 0} {
+ regsub "#.*" $line "" rec
+ if {$rec == ""} {
+ continue
}
- } else {
- if {[lsearch -exact [lrange $rec 1 end] $arg] != -1} {
- lappend res [lindex $rec 0]
+ if {$mode == "-ip"} {
+ if {[lindex $rec 0] == $arg} {
+ eval lappend res [lrange $rec 1 end]
+ }
+ } else {
+ if {[lsearch -exact [lrange $rec 1 end] $arg] != -1} {
+ lappend res [lindex $rec 0]
+ }
}
}
+ close $fd
}
- close $fd
}
-}
-
-proc ::pmres::dns_match {mode arg var} {
- upvar $var res
-
- if {[catch [list exec host $arg] ans] == 0} {
- foreach line [split $ans "\n"] {
- if {$mode == "-ip"} {
- if [regexp "domain name pointer" $line] {
- regsub {\.$} [lindex $line 4] "" t
- lappend res $t
- }
- } else {
- if [regexp "has address" $line] {
- lappend res [lindex $line 3]
- } elseif [regexp "has IPv6 address" $line] {
- lappend res [lindex $line 4]
+
+ proc dns_match {mode arg var} {
+ upvar $var res
+
+ if {[catch [list exec host $arg] ans] == 0} {
+ foreach line [split $ans "\n"] {
+ if {$mode == "-ip"} {
+ if [regexp "domain name pointer" $line] {
+ regsub {\.$} [lindex $line 4] "" t
+ lappend res $t
+ }
+ } else {
+ if [regexp "has address" $line] {
+ lappend res [lindex $line 3]
+ } elseif [regexp "has IPv6 address" $line] {
+ lappend res [lindex $line 4]
+ }
}
}
}
}
-}
-array set dnscache {}
+ variable dnscache
+ array set dnscache {}
-proc ::pmres::resolve {args} {
- global dnscache
- set mode [lindex $args 0]
- if {$mode == "-host" || $mode == "-ip"} {
- set arg [lindex $args 1]
- } else {
- set arg [lindex $args 0]
- if {[regexp {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} $arg]} {
- set mode "-ip"
- } elseif [regexp -nocase {(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.in-addr.arpa} $arg o4 o3 o2 o1] {
- set mode "-ip"
- set arg "$o1.$o2.$o3.$o4"
+ proc resolve {args} {
+ variable dnscache
+
+ set mode [lindex $args 0]
+ if {$mode == "-host" || $mode == "-ip"} {
+ set arg [lindex $args 1]
} else {
- set mode "-host"
+ set arg [lindex $args 0]
+ if {[regexp {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} $arg]} {
+ set mode "-ip"
+ } elseif [regexp -nocase {(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.in-addr.arpa} $arg o4 o3 o2 o1] {
+ set mode "-ip"
+ set arg "$o1.$o2.$o3.$o4"
+ } else {
+ set mode "-host"
+ }
+ }
+ if [info exists dnscache($mode,$arg)] {
+ return $dnscache($mode,$arg)
}
- }
- if [info exists dnscache($mode,$arg)] {
- return $dnscache($mode,$arg)
- }
- set res {}
+ set res {}
- foreach x [hostorder] {
- set name "${x}_match"
- if {[info procs "$name"] == "$name"} {
- eval $name $mode $arg res
+ foreach x [hostorder] {
+ set name "${x}_match"
+ if {[info procs "$name"] == "$name"} {
+ eval $name $mode $arg res
+ }
}
+ if {$res != ""} {
+ set dnscache($mode,$arg) $res
+ }
+ return $res
}
- if {$res != ""} {
- set dnscache($mode,$arg) $res
- }
- return $res
-}
+}
proc hostname {arg} {
if {[config_option resolve] && [regexp {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} $arg]} {
@@ -1340,6 +1342,55 @@ proc runcmd {hosts command} {
debug 2 "finished $command on $hosts"
}
+# The hostproc namespace provides functions for processing output
+# from hosts.
+namespace eval hostproc {
+ # A prologue function. Called before processing the first batch of hosts.
+ proc prologue {} {}
+
+ # Epilogue function is called when all hosts have been processed.
+ proc epilogue {} {}
+
+ proc transition {host state} {
+ if {$state == "COMMAND" &&\
+ ![config_option no-host-header] &&\
+ ![config_option buffer-output]} {
+ puts "[hostname $host]:"
+ }
+ }
+
+ # Getline is invoked when next line of output has been received from the
+ # host. The default implementation provides basic output capability.
+ proc getline {host line} {
+ if [config_option buffer-output] {
+ return
+ }
+
+ if [config_option prefix] {
+ puts -nonewline "[hostname $host]> "
+ }
+ puts -nonewline $line
+ }
+
+ # The finish function is called when EOF has been received from a host.
+ # The REF argument is the name of the variable which, if exists, contains
+ # full text received from the host.
+ proc finish {host ref} {
+ upvar $ref text
+ if [info exists text] {
+ if ![config_option no-host-header] {
+ puts "[hostname $host]:"
+ }
+ foreach line $text {
+ if [config_option prefix] {
+ puts -nonewline "[hostname $host]> "
+ }
+ puts -nonewline $line
+ }
+ }
+ }
+}
+
# #######################################################################
# The game begins...
# #######################################################################
@@ -1552,55 +1603,6 @@ if [info exists config(option,hostgroup)] {
}
}
-# The hostproc namespace provides functions for processing output
-# from hosts.
-namespace eval hostproc {
- # A prologue function. Called before processing the first batch of hosts.
- proc prologue {} {}
-
- # Epilogue function is called when all hosts have been processed.
- proc epilogue {} {}
-
- proc transition {host state} {
- if {$state == "COMMAND" &&\
- ![config_option no-host-header] &&\
- ![config_option buffer-output]} {
- puts "[hostname $host]:"
- }
- }
-
- # Getline is invoked when next line of output has been received from the
- # host. The default implementation provides basic output capability.
- proc getline {host line} {
- if [config_option buffer-output] {
- return
- }
-
- if [config_option prefix] {
- puts -nonewline "[hostname $host]> "
- }
- puts -nonewline $line
- }
-
- # The finish function is called when EOF has been received from a host.
- # The REF argument is the name of the variable which, if exists, contains
- # full text received from the host.
- proc finish {host ref} {
- upvar $ref text
- if [info exists text] {
- if ![config_option no-host-header] {
- puts "[hostname $host]:"
- }
- foreach line $text {
- if [config_option prefix] {
- puts -nonewline "[hostname $host]> "
- }
- puts -nonewline $line
- }
- }
- }
-}
-
if [info exists config(script)] {
set config(script) [lindex $argv 0]
set argv [lrange $argv 1 end]

Return to:

Send suggestions and report system problems to the System administrator.