Debian Codegraph @

tcl3270/Examples/cms_cmd.tcl3270

raw
1 #!tcl3270
2
3 # Copyright (c) 2000-2009, Paul Mattes.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of Paul Mattes nor his contributors may be used
15 # to endorse or promote products derived from this software without
16 # specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
21 # NO EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 # Pluck the username, password and command from the command line.
30 if {$argc != 4} {
31 puts stderr "Usage: $argv0 -- hostname username password command"
32 exit 1
33 }
34 set hostname [lindex $argv 0]
35 set username [lindex $argv 1]
36 set password [lindex $argv 2]
37 set command [lindex $argv 3]
38
39 # Procedure to wait for a READ prompt from CMS or CP.
40 proc waitread {} {
41 Snap Save
42 while {[Snap Ascii [expr [Snap Rows]-1] [expr [Snap Cols]-17] 4]
43 != "READ"} {
44 Snap Wait Output
45 }
46 }
47
48 # Procedure to check for the CMS "Ready" prompt.
49 # Returns its row number, or -1 for "MORE..." state, and leaves a screen with
50 # data to read in the Snap buffer.
51 proc cmd_done {} {
52 global verbose
53 Snap Save
54 while {1} {
55 if {[Snap Ascii [expr [Snap Rows]-1] [expr [Snap Cols]-20] 7]
56 == "MORE..."} {
57 if {$verbose} {puts "MORE..."}
58 return -1
59 }
60 set i [expr [Snap Rows]-2]
61 while {$i >= 0} {
62 set text [Snap Ascii $i 0 [Snap Cols]]
63 switch -regexp $text {
64 "Ready; T=.*" {return $i}
65 "Ready\(.*\); T=.*" {
66 error [Snap Ascii [expr $i-1] 0 \
67 [Snap Cols]]
68 }
69 "^ *\$" {}
70 default {
71 if {$verbose} {puts "Incomplete $i '[string trimright $text]'"}
72 set i 0
73 }
74 }
75 incr i -1
76 }
77 Snap Wait Output
78 }
79 }
80
81 # Execute a command, return the output.
82 proc cms_cmd {text} {
83 global verbose
84
85 # Clear the screen.
86 Clear
87
88 # Send the command.
89 String "$text\n"
90
91 # 'first' is the row where the first line of output will appear. For
92 # the first screenful it's 1; after that it's 0.
93 set first 1
94
95 # r is the result.
96 set r {}
97
98 while {1} {
99 # Wait for a screenful.
100 set d [cmd_done]
101
102 # Dump out what's there.
103 set i $first
104 set first 0
105 if {$d < 0} {set last [expr [Snap Rows]-2]} {set last $d}
106 while {$i < $last} {
107 set r [linsert $r end [string trimright \
108 [Snap Ascii $i 0 [Snap Cols]]]]
109 incr i
110 }
111 if {$d >= 0} {break}
112
113 # Clear the screen and go around again.
114 Clear
115 }
116 return $r
117 }
118
119 # Start of main procedure.
120
121 # Set 'verbose' to 1 to get debug output from the glue functions.
122 set verbose 0
123
124 # Connect to the host and wait for an input field.
125 Connect $hostname
126 Wait InputField
127
128 # Log in and wait for CP READ or VM READ mode.
129 String "$username\t$password\n"
130 waitread
131
132 # If we can't log on, we're hosed.
133 if {[Ascii 1 11 7] == "Already"} {
134 puts stderr "Can't run -- already logged in."
135 exit 1
136 }
137
138 # If we're in CP mode, which means we disconnected last time, boot CMS.
139 if {[Ascii [expr [Rows]-1] [expr [Cols]-20] 2] == "CP"} {
140 Clear
141 String "i cms\n"
142 waitread
143 }
144
145 # Enter an empty command to get a CMS prompt. If we don't do this, there will
146 # be a Ready prompt as the first line of output below.
147 Clear
148 Enter
149 cmd_done
150
151 # Get the output of the user's command and display it.
152 if {[catch {cms_cmd $command} result]} {
153 puts stderr $result
154 set rc 1
155 } {
156 for {set i 0} {$i < [llength $result]} {incr i} {
157 puts [lindex $result $i]
158 }
159 set rc 0
160 }
161
162 # Log off, and wait for the host to hang up on us, so we don't unintentionally
163 # create a disconnected session.
164 Clear
165 if {! [catch {String "logoff\n"}]} {Wait Disconnect}
166 exit $rc