~ubuntu-branches/ubuntu/warty/dejagnu/warty

« back to all changes in this revision

Viewing changes to lib/rlogin.exp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2004-02-09 15:07:58 UTC
  • Revision ID: james.westby@ubuntu.com-20040209150758-oaj7r5zrop60v8sb
Tags: upstream-1.4.4
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
 
2
# 2001, 2002, 2003 Free Software Foundation, Inc.
 
3
#
 
4
# This file is part of DejaGnu.
 
5
#
 
6
# DejaGnu is free software; you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# DejaGnu is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
# General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with DejaGnu; if not, write to the Free Software Foundation,
 
18
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
#
 
21
# Connect to ARG using rlogin. This is for systems using rlogin to
 
22
# braindead targets. It returns either the spawn_id or a -1.
 
23
#
 
24
 
 
25
proc rlogin_open { arg } {
 
26
    global board_info
 
27
 
 
28
    set tries 0
 
29
    set result -1
 
30
 
 
31
    if [board_info $arg exists fileid] {
 
32
        return [board_info $arg fileid]
 
33
    }
 
34
 
 
35
    # get the hostname and port number from the config array
 
36
    if [board_info $arg exists netport] {
 
37
        set hostname [lindex [split [board_info $arg netport] ":"] 0]
 
38
    } else {
 
39
        set hostname $arg
 
40
    }
 
41
 
 
42
    if ![board_info $arg exists shell_prompt] {
 
43
        # if no prompt, then set it to something generic
 
44
        set shell_prompt ".*> "
 
45
    } else {
 
46
        set shell_prompt [board_info $arg shell_prompt]
 
47
    }
 
48
 
 
49
    if [board_info $arg exists fileid] {
 
50
        unset board_info($arg,fileid)
 
51
    }
 
52
    # get the right version of rlogin
 
53
    if ![board_info $arg exists rlogin_prog] {
 
54
        set RLOGIN rlogin
 
55
    } else {
 
56
        set RLOGIN [board_info $arg rlogin_prog]
 
57
    }
 
58
 
 
59
    # start connection and store the spawn_id
 
60
    verbose "Opening a $RLOGIN connection to $hostname" 2
 
61
    spawn $RLOGIN $hostname
 
62
    if { $spawn_id < 0 } {
 
63
        perror "invalid spawn id from rlogin"
 
64
        return
 
65
    }
 
66
    set board_info($arg,fileid) $spawn_id
 
67
 
 
68
    # Try to connect to the target. We give up after 3 attempts.
 
69
    while { $tries <= 3 } {
 
70
        expect {
 
71
            -re ".*$shell_prompt.*$" {
 
72
                verbose "Got prompt\n"
 
73
                set result 0
 
74
                break
 
75
            }
 
76
            -re "TERM = .*\\)\[ ]*$" {
 
77
                send "dumb\r\n"
 
78
                expect {
 
79
                    "Terminal type is*$" {
 
80
                        verbose "rlogin: set the terminal to dumb" 2
 
81
                    }
 
82
                    default {
 
83
                        warning "rlogin: couldn't set terminmal type"
 
84
                    }
 
85
                }
 
86
                set result 10
 
87
                break
 
88
            }
 
89
            "unknown host" {
 
90
                perror "rlogin: unknown host"
 
91
                break
 
92
            }
 
93
            "has logged on from" {
 
94
                exp_continue
 
95
            }
 
96
            "Terminal type is" {
 
97
                verbose "rlogin: connected, got terminal prompt" 2
 
98
                set result 0
 
99
                break
 
100
            }
 
101
            -re "Maximum number of users already logged in.*$" {
 
102
                warning "rlogin: maximum number of users already logged in"
 
103
            }
 
104
            -re "Sorry, shell is locked.*Connection closed.*$" {
 
105
                warning "rlogin: lready connected."
 
106
            }
 
107
            -re "Sorry, this system is engaged.*Connection closed.*$" {
 
108
                warning "rlogin: system engaged."
 
109
            }
 
110
            timeout     {
 
111
                warning "rlogin: timed out trying to connect."
 
112
            }
 
113
            eof {
 
114
                perror "rlogin: got EOF while trying to connect."
 
115
                break
 
116
            }
 
117
        }
 
118
        incr tries
 
119
    }
 
120
 
 
121
    # see if we maxed out on errors
 
122
    if { $result < 0 } {
 
123
        catch "close -i $spawn_id"
 
124
        catch "wait -i $spawn_id"
 
125
        set spawn_id -1
 
126
    } else {
 
127
        verbose "rlogin: connected to $hostname" 2
 
128
    }
 
129
 
 
130
    return $spawn_id
 
131
}
 
132
 
 
133
#
 
134
# Start CMDLINE running on DEST. Return the shell_id associated with
 
135
# the command.
 
136
#
 
137
proc rlogin_spawn { dest cmdline } {
 
138
    if ![board_info $dest exists shell_prompt] {
 
139
        set shell_prompt "(^|\[\r\n\])\[^\r\n\]*>"
 
140
    } else {
 
141
        set shell_prompt [board_info $dest shell_prompt]
 
142
    }
 
143
    set prefix ""
 
144
    set ok 0
 
145
    for { set i 0 } {$i <= 2 && ! $ok} { incr i } {
 
146
        set shell_id [remote_open $dest]
 
147
        if { $shell_id != "" && $shell_id > 0 } {
 
148
            remote_send $dest "echo k\r"
 
149
            remote_expect $dest 20 {
 
150
                -re "\\(gdb\\)" {
 
151
                    set shell_prompt "\\(gdb\\)"
 
152
                    # gdb uses 'shell command'.
 
153
                    set prefix "shell "
 
154
                    set ok 1
 
155
                }
 
156
                -re ".*$shell_prompt" {
 
157
                    set ok 1
 
158
                }
 
159
                default { }
 
160
            }
 
161
        }
 
162
        if { ! $ok } {
 
163
            remote_close $dest
 
164
            remote_reboot $dest
 
165
        }
 
166
    }
 
167
    if { ! $ok } {
 
168
        return "unable to start command"
 
169
    } else {
 
170
        remote_send $dest "${prefix}${cmdline}\n"
 
171
        return [board_info $dest fileid]
 
172
    }
 
173
}