diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-09-04 21:40:56 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-09-04 21:40:56 +0300 |
commit | 9cc2ffffedd74917bb1a26adf26afc3a4c130c8f (patch) | |
tree | 5f6fa9f29f92ca0f22ac11f06f8ea8991cd5c82d | |
parent | 013ad815ed55d01af3e9901b8a0b391aa19f42ba (diff) | |
download | rex-9cc2ffffedd74917bb1a26adf26afc3a4c130c8f.tar.gz rex-9cc2ffffedd74917bb1a26adf26afc3a4c130c8f.tar.bz2 |
Cleanup namespaces
-rwxr-xr-x | rex.exp | 252 |
1 files changed, 127 insertions, 125 deletions
@@ -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] |