OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [dejagnu/] [lib/] [ftp.exp] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Copyright (C) 92, 93, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
2
 
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 
17
# Please email any bugs, comments, and/or additions to this file to:
18
# bug-dejagnu@prep.ai.mit.edu
19
 
20
#
21
# Support downloading files using ftp.
22
#
23
 
24
#
25
# Open a connection to HOST.
26
#
27
proc ftp_open { host } {
28
    set prompt "ftp>"
29
    global board_info
30
 
31
    if [board_info $host exists name] {
32
        set host [board_info $host name];
33
    }
34
 
35
    if [board_info $host exists ftp_fileid] {
36
        return [board_info $host ftp_fileid];
37
    }
38
 
39
    if [board_info $host exists hostname] {
40
        set remotehost [board_info $host hostname];
41
    } else {
42
        set remotehost $host;
43
    }
44
 
45
    # LoseQVT tends to get stuck sometimes; we'll loop around a few million
46
    # times when it gets a "connection refused".
47
    set spawn_id -1;
48
    set count 3;
49
    while { $spawn_id < 0 && $count >= 0 } {
50
        spawn ftp -n $remotehost;
51
        expect {
52
            -i $spawn_id -re ".*220.*$prompt" { }
53
            -i $spawn_id -re ".*Connection refused.*$prompt" {
54
                sleep 2;
55
                send "open $remotehost\n";
56
                exp_continue
57
            }
58
            -i $spawn_id default {
59
                close -i $spawn_id;
60
                wait -i $spawn_id;
61
                set spawn_id -1;
62
            }
63
        }
64
        incr count -1;
65
    }
66
    if { $spawn_id < 0 } {
67
        return -1;
68
    }
69
    set board_info($host,ftp_fileid) $spawn_id;
70
    if [board_info $host exists ftp_username] {
71
        if [board_info $host exists ftp_password] {
72
            set command "user [board_info $host ftp_username] [board_info $host ftp_password]\n";
73
        } else {
74
            set command "user [board_info $host ftp_username]\n";
75
        }
76
        send "$command"
77
        expect {
78
            -i $spawn_id -re ".*230.*$prompt" { }
79
            -i $spawn_id default {
80
                close -i $spawn_id;
81
                wait -i $spawn_id;
82
                return -1;
83
            }
84
        }
85
    }
86
    set timeout 15
87
    send -i $spawn_id "binary\n"
88
    expect {
89
        -i $spawn_id -re "200.*$prompt" { }
90
        -i $spawn_id timeout {
91
            close -i $spawn_id;
92
            wait -i $spawn_id;
93
            return -1
94
        }
95
    }
96
    if [board_info $host exists ftp_directory] {
97
        send "cd [board_info $host ftp_directory]\n";
98
        expect {
99
            -i $spawn_id -re "250.*$prompt" { }
100
            -i $spawn_id default {
101
                close -i $spawn_id;
102
                wait -i $spawn_id;
103
                return -1;
104
            }
105
        }
106
    }
107
 
108
    if [board_info $host exists ftp_no_passive] {
109
        send "passive\n";
110
        expect {
111
            -i $spawn_id -re "Passive mode off.*$prompt" { }
112
            -i $spawn_id -re "Passive mode on.*$prompt" {
113
                send "passive\n";
114
                exp_continue;
115
            }
116
            -i $spawn_id -re ".*$prompt" { }
117
        }
118
    }
119
 
120
    set board_info($host,ftp_fileid) $spawn_id;
121
    return $spawn_id;
122
}
123
 
124
#
125
# Grab REMOTEFILE from HOST and store it as LOCALFILE.
126
#
127
proc ftp_upload { host remotefile localfile } {
128
    set prompt "ftp>"
129
 
130
    verbose "ftping $remotefile from $host to $localfile"
131
    set timeout 15
132
    set spawn_id [ftp_open $host];
133
    if { $spawn_id < 0 } {
134
        return "";
135
    }
136
    set loop 1;
137
 
138
    while { $loop } {
139
        send -i $spawn_id "get $remotefile $localfile\n";
140
        expect {
141
            -i $spawn_id -re ".*Too many open files.*$prompt" {
142
                ftp_close $host;
143
            }
144
            -i $spawn_id -re ".*No such file or directory.*$prompt" {
145
                set loop 0;
146
                set remotefile "";
147
            }
148
            -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" { set loop 0; }
149
            -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
150
                set loop 0;
151
                set remotefile "";
152
            }
153
            -i $spawn_id default {
154
                ftp_close $host;
155
            }
156
        }
157
        if { $loop } {
158
            set spawn_id [ftp_open $host];
159
            if { $spawn_id < 0 } {
160
                return "";
161
            }
162
        }
163
    }
164
    return $localfile;
165
}
166
 
167
#
168
# Download LOCALFILE to HOST as REMOTEFILE.
169
#
170
proc ftp_download { host localfile remotefile } {
171
    set prompt "ftp>"
172
 
173
    verbose "putting $localfile $remotefile"
174
 
175
    if [board_info $host exists hostname] {
176
        set remotehost [board_info $host hostname];
177
    } else {
178
        set remotehost $host;
179
    }
180
 
181
    set spawn_id [ftp_open $host];
182
    if { $spawn_id < 0 } {
183
        return "";
184
    }
185
    set loop 1;
186
 
187
    while { $loop } {
188
        send -i $spawn_id "put $localfile $remotefile\n"
189
        expect {
190
            -i $spawn_id -re ".*Too many open files.*$prompt" {
191
                ftp_close $host;
192
            }
193
            -i $spawn_id -re ".*No such file or directory.*$prompt" {
194
                set loop 0;
195
                set remotefile "";
196
            }
197
            -re "(^|\[\r\n\])150.*connection for (.*) \[(\]\[0-9.,\]+\\)\[\r\n\]" {
198
                set remotefile $expect_out(2,string);
199
                exp_continue;
200
            }
201
            -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" {
202
                set loop 0;
203
            }
204
            -i $spawn_id -re "Timeout.*$prompt" {
205
                ftp_close $host;
206
            }
207
            -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
208
                set loop 0;
209
                set remotefile "";
210
            }
211
            -i $spawn_id default {
212
                ftp_close $host;
213
            }
214
        }
215
        if { $loop } {
216
            set spawn_id [ftp_open $host];
217
            if { $spawn_id < 0 } {
218
                return "";
219
            }
220
        }
221
    }
222
    return $remotefile;
223
}
224
 
225
#
226
# Close the connection.
227
#
228
proc ftp_close { host } {
229
    global board_info
230
 
231
    if [board_info $host exists name] {
232
        set host [board_info $host name];
233
    }
234
 
235
    if ![board_info $host exists ftp_fileid] {
236
        return "";
237
    }
238
 
239
    set spawn_id [board_info $host ftp_fileid];
240
    unset board_info($host,ftp_fileid);
241
 
242
    send -i $spawn_id "quit\n"
243
    close -i $spawn_id
244
    wait -i $spawn_id;
245
    return "";
246
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.