~vcs-imports-ii/dejagnu/master

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
# Copyright (C) 1992-2019, 2020 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Connect to HOSTNAME using tip(1).  Sets the board's fileid field
# with the spawn_id on success and returns the spawn id, otherwise
# returns -1.
#
proc tip_open { hostname } {
    global verbose
    global spawn_id

    set tries 0
    set result -1

    if {[board_info $hostname exists name]} {
	set hostname [board_info $hostname name]
    }
    set port [board_info $hostname tipname]
    if {[board_info $hostname exists shell_prompt]} {
	set shell_prompt [board_info $hostname shell_prompt]
    } else {
	# Pick something reasonably generic.
	set shell_prompt ".*> "
    }

    if {[board_info $hostname exists fileid]} {
	unset board_info($hostname,fileid)
    }
    spawn tip -v $port
    if { $spawn_id < 0 } {
	perror "invalid spawn id from tip"
	return -1
    }
    expect {
	-re ".*connected.*$" {
	    send "\r\n"
	    expect {
		-re ".*$shell_prompt.*$" {
		    verbose "Got prompt\n"
		    set result 0
		    incr tries
		}
		timeout {
		    warning "Never got prompt."
		    set result -1
		    incr tries
		    if { $tries <= 2 } {
			exp_continue
		    }
		}
	    }
	}
	-re "all ports busy.*$" {
	    set result -1
	    perror "All ports busy."
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	-re "Connection Closed.*$" {
	    perror "Never connected."
	    set result -1
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	-re ".*: Permission denied.*link down.*$" {
	    perror "Link down."
	    set result -1
	    incr tries
	}
	timeout	{
	    perror "Timed out trying to connect."
	    set result -1
	    incr tries
	    if { $tries <= 2 } {
		exp_continue
	    }
	}
	eof {
	    perror "Got unexpected EOF from tip."
	    set result -1
	    incr tries
	}
    }

    send "\n~s"
    expect {
	"~\[set\]*" {
	    verbose "Setting verbose mode" 1
	    send "verbose\n\n\n"
	}
    }

    if { $result < 0 } {
	perror "Couldn't connect after $tries tries."
	return -1
    } else {
	set board_info($hostname,fileid) $spawn_id
	return $spawn_id
    }
}

# Download FILE to DEST using the ~put command in tip(1).
# Returns -1 if an error occurred, otherwise returns 0.
#
proc tip_download { dest file args } {
    global verbose
    global decimal
    global expect_out

    if {[board_info $dest exists shell_prompt]} {
	set shell_prompt [board_info $dest shell_prompt]
    } else {
	set shell_prompt ".*>"
    }

    set result ""
    if {![board_info $dest exists fileid]} {
	perror "tip_download: no connection to $dest."
	return $result
    }
    set shell_id [board_info $dest fileid]

    if {![file exists $file]} {
	perror "$file doesn't exist."
	return $result
    }

    send -i $shell_id "\n~p"
    expect {
	-i $shell_id "~\[put\]*" {
	    verbose "Downloading $file, please wait" 1
	    send -i $shell_id "$file\n"
	    set timeout 50
	    expect {
		-i $shell_id -re ".*$file.*$" {
		    exp_continue
		}
		-i $shell_id -re ".*lines transferred in.*minute.*seconds.*$shell_prompt.*$" {
		    verbose "Download $file successfully" 1
		    set result $file
		}
		-i $shell_id -re ".*Invalid command.*$shell_prompt$" {
		    warning "Got an invalid command to the remote shell."
		}
		-i $shell_id -re ".*$decimal\r" {
		    if {[info exists expect_out(buffer)]} {
			verbose $expect_out(buffer)
			exp_continue
		    }
		}
		-i $shell_id timeout {
		    perror "Timed out trying to download."
		}
	    }
	}
	timeout {
	    perror "Timed out waiting for response to put command."
	}
    }
    set timeout 10
    return $result
}