/usr/bin/nns is in tcllib 1.16-dfsg-2.
This file is owned by root:root, with mode 0o755.
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 | #! /usr/bin/env tclsh
# -*- tcl -*-
# @@ Meta Begin
# Application nns 1.2
# Meta platform tcl
# Meta summary Nano Name Service Client
# Meta description This application connects to a name service demon
# Meta description and either registers a name with associated data
# Meta description (until exit) or searches for entries matching a
# Meta description glob pattern. Operations to identify client and
# Meta description server are made available as well. It will survive
# Meta description the loss of the nameserver and automatically reconnect
# Meta description and continue when it comes back (bind and search).
# Meta description
# Meta subject {name service} client
# Meta require {Tcl 8.4}
# Meta require logger
# Meta require nameserv::auto
# Meta require struct::matrix
# Meta author Andreas Kupries
# Meta license BSD
# @@ Meta End
package provide nns 1.2
# nns - Nano Name Service Client
# === = ========================
#
# Use cases
# ---------
#
# (1) Register something at a nano name service
# (2) Query protocol and feature information.
# (3) Provide application version, and protocol information
# (4) Search service for entries matching a glob-pattern
#
# Command syntax
# --------------
#
# (Ad 1) nns bind ?-host NAME|IP? ?-port PORT? name data
# (Ad 2) nns ident ?-host NAME|IP? ?-port PORT?
# (Ad 3) nns who
# (Ad 4) nns search ?-host NAME|IP? ?-port PORT? ?-continuous? ?pattern?
#
# Register a name with data. If no port is specified the default
# port 38573 is used to connect to it. If no host is specified
# the default (localhost) is used to connect to it.
# ### ### ### ######### ######### #########
## Requirements
lappend auto_path [file join [file dirname [file dirname \
[file normalize [info script]]]] modules]
package require nameserv::auto 0.3 ;# Need auto-restoring search.
package require struct::matrix
logger::initNamespace ::nns
namespace eval ::nns { log::setlevel info }
# ### ### ### ######### ######### #########
## Process application command line
proc ::nns::ProcessCommandLine {} {
global argv
variable xcmd
variable xname
variable xdata
variable xpat *
variable xwatch 0
# Process the options, perform basic validation.
if {[llength $argv] < 1} Usage
set cmd [lindex $argv 0]
set argv [lrange $argv 1 end]
switch -exact -- $cmd {
bind - ident - who - search {set xcmd $cmd}
default Usage
}
while {[llength $argv]} {
set opt [lindex $argv 0]
if {![string match "-*" $opt]} break
switch -exact -- $opt {
-host {
if {$xcmd == "who"} Usage
if {[llength $argv] < 2} Usage
set host [lindex $argv 1]
set argv [lrange $argv 2 end]
nameserv::auto::configure -host $host
}
-port {
if {$xcmd == "who"} Usage
if {[llength $argv] < 2} Usage
# Todo: Check non-zero unsigned short integer
set port [lindex $argv 1]
set argv [lrange $argv 2 end]
nameserv::auto::configure -port $port
}
-continuous {
set xwatch 1
set argv [lrange $argv 1 end]
}
-debug {
# Undocumented. Activate the logger services provided
# by various packages.
logger::setlevel debug
set argv [lrange $argv 1 end]
}
default Usage
}
}
# Additional validation, and extraction of the non-option
# arguments. Of which this application has none.
switch -exact -- $xcmd {
bind {
if {[llength $argv] != 2} Usage
foreach {xname xdata} $argv break
}
search {
if {[llength $argv] > 1} Usage
if {[llength $argv] == 1} {
set xpat [lindex $argv 0]
}
}
who - ident {
if {[llength $argv] != 0} Usage
}
}
return
}
proc ::nns::Usage {{sfx {}}} {
global argv0 ; append argv0 $sfx
set blank [blank $argv0]
puts stderr "$argv0 wrong#args, expected: bind ?-host NAME|IP? ?-port PORT? NAME DATA"
puts stderr "$blank ident ?-host NAME|IP? ?-port PORT?"
puts stderr "$blank search ?-host NAME|IP? ?-port PORT? ?-continuous? ?PATTERN?"
puts stderr "$blank who"
exit 1
}
proc ::nns::ArgError {text} {
global argv0
puts stderr "$argv0: $text"
#puts $::errorInfo
exit 1
}
proc ::nns::blank {s} {
regsub -all -- {[^ ]} $s { } s
return $s
}
# ### ### ### ######### ######### #########
proc ::nns::My {} {
# Quick access to format the identity of the name service the
# client talks to.
return "[nameserv::auto::cget -host] @[nameserv::auto::cget -port]"
}
proc ::nns::Connection {message args} {
# args = tag event details, ignored
log::info $message
return
}
proc ::nns::MonitorConnection {} {
uevent::bind nameserv lost-connection [list ::nns::Connection "Disconnected name service at [My]"]
uevent::bind nameserv re-connection [list ::nns::Connection "Reconnected2 name service at [My]"]
return
}
# ### ### ### ######### ######### #########
## Main
proc ::nns::Do.bind {} {
global argv0
variable xname
variable xdata
MonitorConnection
log::info "Binding with name service at [My]: $xname = $xdata"
nameserv::auto::bind $xname $xdata
vwait ::forever
# Not reached.
return
}
proc ::nns::Do.ident {} {
set sp [nameserv::auto::server_protocol]
set sf [join [nameserv::auto::server_features] {, }]
if {[llength $sf] > 1} {
set sf [linsert $sf end-1 and]
}
puts "Server [My]"
puts " Protocol: $sp"
puts " Features: $sf"
return
}
proc ::nns::Do.search {} {
variable xpat
variable xwatch
struct::matrix M
M add columns 2
if {$xwatch} {
MonitorConnection
set contents [nameserv::auto::search -continuous $xpat]
$contents configure -command [list ::nns::Do.search.change $contents]
vwait ::forever
# Not reached.
} else {
Do.search.print [nameserv::auto::search $xpat]
}
return
}
proc ::nns::Do.search.change {res type response} {
# Ignoring the arguments, we simply print the full results every
# time.
if {$type eq "stop"} {
# Cannot happen for nameserv::auto client, we are free to panic.
$res destroy
log::critical {Bad event 'stop' <=> Lost connection, search closed}
return
}
# Clear screen ...
puts -nonewline stdout "\033\[H\033\[J"; # Home + Erase Down
flush stdout
::nns::Do.search.print [$res getall]
return
}
proc ::nns::Do.search.print {contents} {
log::info "Searching at name service at [My]"
if {![llength $contents]} {
log info "Nothing found..."
return
}
catch {M delete rows [M rows]}
foreach {name data} $contents {
M add row [list $name $data]
}
foreach line [split [M format 2string] \n] { log::info $line }
return
}
proc ::nns::Do.who {} {
# FUTURE: access and print the metadata contained in ourselves.
global argv0
puts "$argv0 [package require nns] (Client Protocol [nameserv::auto::protocol])"
return
}
# ### ### ### ######### ######### #########
## Invoking the functionality.
::nns::ProcessCommandLine
if {[catch {
::nns::Do.$::nns::xcmd
} msg]} {
::nns::ArgError $msg
}
# ### ### ### ######### ######### #########
exit
|