OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [insight/] [expect/] [example/] [xpstat] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#!../expectk --
2
 
3
# This script acts as a front-end for xpilot.  Run it in the background,
4
# and it will pop up a window for each server it finds running.  After
5
# you run it, press the "?" button for more info.
6
 
7
# Store the filename of your xpilot client in the following variable.
8
set xpilot /usr/local/bin/xpilot
9
 
10
# Author: Don Libes, NIST, 12/29/92
11
 
12
# I never have figured out how to get the alias out of xrdb.  For now, just
13
# read it ourselves out of .Xdefaults - ugh.
14
 
15
log_user 0
16
 
17
set timeout 60
18
 
19
proc probe {} {
20
        global max db hosts world
21
 
22
        set timeout -1
23
 
24
        expect_before eof {wait;return 0}
25
 
26
        expect -re "Server on (.*). Enter command> " {
27
                        exp_send "S\r"
28
                        set host $expect_out(1,string)
29
                        # replace dots in hostnames by underscores
30
                        regsub -all . $host _ host
31
                        # force lowercase to avoid Tk widget name problems
32
                        set host [string tolower $host]
33
                        lappend hosts $host
34
        }
35
        expect -re "WORLD\[^:]*: (\[^\r]*)\r" {
36
                set worldtmp $expect_out(1,string)
37
        }
38
        expect -re "AUTHOR\[^:]*: (\[^\r]*)\r" {
39
                set author $expect_out(1,string)
40
        }
41
        set world($host) "$worldtmp by $author"
42
 
43
        # skip over junk to get players
44
        expect {
45
                -re -+ {}
46
                -re "Enter command> " {
47
                        set max($host) 0
48
                        display $host
49
                        return 1
50
                }
51
        }
52
        set i 0
53
        expect {
54
                -re "\\.\\.\\. .  (................)   (...) *(\[^ ]*) *(\[^\r]*)\r" {
55
                        # strip trailing blanks
56
                        set alias [string trimright $expect_out(1,string)]
57
                        set db($host,$i,alias) $alias
58
 
59
                        # strip leading zeros
60
                        scan $expect_out(2,string) %d db($host,$i,life)
61
 
62
                        set db($host,$i,score) $expect_out(3,string)
63
 
64
                        set db($host,name,$alias) $expect_out(4,string)
65
 
66
                        incr i
67
                        exp_continue
68
                }
69
                -re "Enter command>"
70
 
71
        }
72
        set max($host) $i
73
        display $host
74
 
75
        return 1
76
}
77
 
78
proc resize {w a b} {
79
        # 27 is a guess at a fixed-width sufficiently comfortable for
80
        # the variable-width font.  I don't know how to do better.
81
        $w configure -width 27
82
}
83
 
84
proc play {host} {
85
        global xpilot alias
86
 
87
        exec xhost $host
88
        catch {exec $xpilot -name $alias($host) -join $host} status
89
}
90
 
91
proc show-help {x y msg} {
92
        catch {destroy .help}
93
        toplevel .help
94
        wm geometry .help +$x+$y
95
 
96
        message .help.text -text $msg
97
 
98
        button .help.ok -text "ok" -command {destroy .help}
99
        pack .help.text
100
        pack .help.ok -fill x
101
}
102
 
103
# pop up window with alias
104
proc show-alias {host seln x y} {
105
        global db
106
 
107
        catch {destroy .alias}
108
        toplevel .alias
109
        wm geometry .alias +$x+$y
110
        wm transient .alias .
111
 
112
        regexp "(.*\[^ ]) +\[-0-9]+ +\[0-9]+$" $seln discard alias
113
 
114
        button .alias.b -text "$db($host,name,$alias)" -command {
115
                destroy .alias
116
        }
117
        .alias.b config -padx 1 -pady 1 -highlightthickness 0
118
        pack .alias.b
119
}
120
 
121
proc help {x y} {
122
        show-help $x $y "xpstat - written by Don Libes, NIST, December 29, 1992
123
 
124
This script acts as a front-end for xpilot.  Run it in the background, and it will pop up a window for each server it finds running.  Press the \"?\" button for this info.
125
 
126
This program polls each xpilot server once a minute.  To make it poll immediately, press \"update\".  Press \"play as\" to enter the current game with the alias to the right.  Edit to taste.  (Your alias is initialized from the value of xpilot.name in ~/.Xdefaults.)
127
 
128
Double-click the left button on an alias to see the real user name.  To remove the user name window, click on it with the left button.
129
 
130
Pan the world/author text, player list, or your own alias by holding the middle mouse button down and moving the mouse."
131
}
132
 
133
# if user presses "update" try to update screen immediately
134
proc prod {x y} {
135
        global cat_spawn_id updateflag
136
 
137
        if $updateflag {
138
                show-help $x $y "I heard you, gimme a break.  I'm waiting for the xpilot server to respond..."
139
        }
140
        set updateflag 1
141
 
142
        exp_send -i $cat_spawn_id "\r"
143
}
144
 
145
proc display {host} {
146
        global world db alias max env
147
 
148
        set w .$host
149
        #if 0==[llength [info com $w]]
150
        if ![winfo exists $w] {
151
 
152
                # window does not exist, create it
153
 
154
                toplevel $w -class xpstat
155
                wm minsize $w 1 1
156
                wm title $w "xpilot@$host"
157
                wm iconname $w "$host xpilot stats"
158
                entry $w.world -state disabled -textvar world($host)
159
 
160
                listbox $w.players -yscroll "resize $w.players" -font 7x13bold
161
                $w.players config -highlightthickness 0 -border 0
162
                $w.world config -highlightthickness 0
163
 
164
                bind $w.players  {
165
                        scan %W ".%%\[^.]" host
166
                        show-alias $host [selection get] %X %Y
167
                }
168
 
169
                message $w.msg -text "no players" -aspect 1000 -justify center
170
 
171
                button $w.help -text ? -command {
172
                        help 10 20
173
                }
174
 
175
                button $w.update -text "update"
176
                bind $w.update <1> {
177
                        after 1 prod %X %Y
178
                }
179
 
180
                button $w.play -text "play as"
181
                bind $w.play <1> {
182
                        scan %W ".%%\[^.]" host
183
                        after 1 play $host
184
                }
185
 
186
                entry $w.alias -textvar alias($host) -width 10
187
                set alias($host) $env(USER)
188
 
189
                bind $w.alias  {
190
                        scan %W ".%%\[^.]" host
191
                        play $host
192
                }
193
 
194
                $w.play config -padx 1 -pady 1 -highlightthickness 0
195
                $w.update config -padx 1 -pady 1 -highlightthickness 0
196
                $w.help config -padx 1 -pady 1 -highlightthickness 0
197
                $w.alias config -highlightthickness 0
198
 
199
                pack $w.world -expand 1 -fill x
200
                pack $w.msg
201
                pack $w.help $w.update $w.play -side left
202
                pack $w.alias -side left -expand 1 -fill x
203
                set max($host,was) 0
204
        }
205
 
206
        if $max($host)==0 {
207
                # put up "no players" message?
208
                if $max($host,was)>0 {
209
                        pack $w.msg -after $w.world -fill x -side top
210
                        pack forget $w.world
211
                }
212
        } else {
213
                # remove "no players" message?
214
                if $max($host,was)==0 {
215
                        pack $w.players -after $w.world -side top
216
                        pack forget $w.msg
217
                }
218
        }
219
 
220
        $w.players delete 0 end
221
 
222
        for {set i 0} {$i<$max($host)} {incr i} {
223
                $w.players insert end [format "%-17s %4d %d" \
224
                        $db($host,$i,alias) \
225
                        $db($host,$i,score) \
226
                        $db($host,$i,life) \
227
                                        ]
228
        }
229
 
230
        set max($host,was) $max($host)
231
}
232
 
233
wm withdraw .
234
set oldhosts {}
235
 
236
set updateflag 0                ;# 1 if user pressed "update" button
237
 
238
# look for desired alias in the .Xdefaults file
239
set status [catch {exec egrep "xpilot.name:" [glob ~/.Xdefaults]} output]
240
if $status==0 {
241
        regexp "xpilot.name:\[ \t]*(\[^\r]*)" $output dummy env(USER)
242
}
243
 
244
spawn cat -u; set cat_spawn_id $spawn_id
245
 
246
while 1 {
247
        global xpilot hosts
248
 
249
        set hosts {}
250
 
251
        eval spawn $xpilot $argv
252
        while {[probe]} {exp_send "N\r"}
253
        catch {expect_before}   ;# disable expect_before from inside probe
254
 
255
        # clean up hosts that no longer are running xpilots
256
 
257
        foreach host $oldhosts {
258
                # if host not in hosts
259
                if -1==[lsearch $hosts $host] {
260
                        destroy .$host
261
                }
262
        }
263
        set oldhosts $hosts
264
 
265
        set updateflag 0
266
 
267
        # sleep for a little while, subject to click from "update" button
268
        expect -i $cat_spawn_id -re "...."      ;# two crlfs
269
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.