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