Adjust Automake options to build maximally portable tarballs
[dejagnu.git] / lib / ftp.exp
blob89e017d2f9023357d9416575b220eaff6b147c43
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 # Open an FTP connection to HOST.
21 proc ftp_open {host} {
22 set prompt "ftp>"
23 global board_info
25 if {[board_info $host exists name]} {
26 set host [board_info $host name]
29 if {[board_info $host exists ftp_fileid]} {
30 return [board_info $host ftp_fileid]
33 if {[board_info $host exists hostname]} {
34 set remotehost [board_info $host hostname]
35 } else {
36 set remotehost $host
39 # LoseQVT tends to get stuck sometimes; we'll loop around a few million
40 # times when it gets a "connection refused".
41 set spawn_id -1
42 set count 3
43 while {$spawn_id < 0 && $count >= 0} {
44 spawn ftp -n $remotehost
45 expect {
46 -i $spawn_id -re ".*220.*$prompt" { }
47 -i $spawn_id -re ".*Connection refused.*$prompt" {
48 sleep 2
49 send "open $remotehost\n"
50 exp_continue
52 -i $spawn_id default {
53 close -i $spawn_id
54 wait -i $spawn_id
55 set spawn_id -1
58 incr count -1
60 if {$spawn_id < 0} {
61 return -1
63 set board_info($host,ftp_fileid) $spawn_id
64 if {[board_info $host exists ftp_username]} {
65 if {[board_info $host exists ftp_password]} {
66 set command "user [board_info $host ftp_username] [board_info $host ftp_password]\n"
67 } else {
68 set command "user [board_info $host ftp_username]\n"
70 send $command
71 expect {
72 -i $spawn_id -re ".*230.*$prompt" { }
73 -i $spawn_id default {
74 close -i $spawn_id
75 wait -i $spawn_id
76 return -1
80 set timeout 15
81 send -i $spawn_id "binary\n"
82 expect {
83 -i $spawn_id -re "200.*$prompt" { }
84 -i $spawn_id timeout {
85 close -i $spawn_id
86 wait -i $spawn_id
87 return -1
90 if {[board_info $host exists ftp_directory]} {
91 send "cd [board_info $host ftp_directory]\n"
92 expect {
93 -i $spawn_id -re "250.*$prompt" { }
94 -i $spawn_id default {
95 close -i $spawn_id
96 wait -i $spawn_id
97 return -1
102 if {[board_info $host exists ftp_no_passive]} {
103 send "passive\n"
104 expect {
105 -i $spawn_id -re "Passive mode off.*$prompt" { }
106 -i $spawn_id -re "Passive mode on.*$prompt" {
107 send "passive\n"
108 exp_continue
110 -i $spawn_id -re ".*$prompt" { }
114 set board_info($host,ftp_fileid) $spawn_id
115 return $spawn_id
118 # Fetch REMOTEFILE from HOST and store it as LOCALFILE.
120 proc ftp_upload {host remotefile localfile} {
121 set prompt "ftp>"
123 verbose "ftping $remotefile from $host to $localfile"
124 set timeout 15
125 set spawn_id [ftp_open $host]
126 if {$spawn_id < 0} {
127 return ""
129 set loop 1
131 while {$loop} {
132 send -i $spawn_id "get $remotefile $localfile\n"
133 expect {
134 -i $spawn_id -re ".*Too many open files.*$prompt" {
135 ftp_close $host
137 -i $spawn_id -re ".*No such file or directory.*$prompt" {
138 set loop 0
139 set remotefile ""
141 -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" {set loop 0}
142 -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
143 set loop 0
144 set remotefile ""
146 -i $spawn_id default {
147 ftp_close $host
150 if {$loop} {
151 set spawn_id [ftp_open $host]
152 if {$spawn_id < 0} {
153 return ""
157 return $localfile
160 # Download LOCALFILE to HOST as REMOTEFILE.
162 proc ftp_download {host localfile remotefile} {
163 set prompt "ftp>"
165 verbose "putting $localfile $remotefile"
167 if {[board_info $host exists hostname]} {
168 set remotehost [board_info $host hostname]
169 } else {
170 set remotehost $host
173 set spawn_id [ftp_open $host]
174 if {$spawn_id < 0} {
175 return ""
177 set loop 1
179 while {$loop} {
180 send -i $spawn_id "put $localfile $remotefile\n"
181 expect {
182 -i $spawn_id -re ".*Too many open files.*$prompt" {
183 ftp_close $host
185 -i $spawn_id -re ".*No such file or directory.*$prompt" {
186 set loop 0
187 set remotefile ""
189 -re {(^|[\r\n])150.*connection for (.*) [(][0-9.,]+\)[\r\n]} {
190 set remotefile $expect_out(2,string)
191 exp_continue
193 -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" {
194 set loop 0
196 -i $spawn_id -re "Timeout.*$prompt" {
197 ftp_close $host
199 -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
200 set loop 0
201 set remotefile ""
203 -i $spawn_id default {
204 ftp_close $host
207 if {$loop} {
208 set spawn_id [ftp_open $host]
209 if {$spawn_id < 0} {
210 return ""
214 return $remotefile
217 # Close the FTP connection to HOST.
219 proc ftp_close {host} {
220 global board_info
222 if {[board_info $host exists name]} {
223 set host [board_info $host name]
226 if {![board_info $host exists ftp_fileid]} {
227 return ""
230 set spawn_id [board_info $host ftp_fileid]
231 unset board_info($host,ftp_fileid)
233 send -i $spawn_id "quit\n"
234 close -i $spawn_id
235 wait -i $spawn_id
236 return ""