ft.c
20.3 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como ft.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
* licinio@bb.com.br (Licínio Luis Branco)
* kraucer@bb.com.br (Kraucer Fernandes Mazuco)
*
*/
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#endif // WIN32
#include <config.h>
#include <lib3270.h>
#include <lib3270/filetransfer.h>
#include <lib3270/log.h>
#include <internals.h>
#include <lib3270/trace.h>
#include <errno.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif // HAVE_MALLOC_H
#include "ft_cutc.h"
#include "ft_dftc.h"
#include "ftc.h"
#include "hostc.h"
#include "kybdc.h"
#include "popupsc.h"
#include "screenc.h"
// #include "tablesc.h"
#include "telnetc.h"
#include "utilc.h"
#include "trace_dsc.h"
static void ft_connected(H3270 *session, int ignored, void *unused);
static void ft_in3270(H3270 *session, int ignored, void *unused);
/* Macros. */
#define eos(s) strchr((s), '\0')
#if defined(X3270_DISPLAY)
#define FILE_WIDTH 300 // width of file name widgets
#define MARGIN 3 // distance from margins to widgets
#define CLOSE_VGAP 0 // distance between paired toggles
#define FAR_VGAP 10 // distance between single toggles and groups
#define BUTTON_GAP 5 // horizontal distance between buttons
#define COLUMN_GAP 40 // distance between columns
#endif
#define BN (Boolean *)NULL
#define snconcat(x,s,fmt,...) snprintf(x+strlen(x),s-strlen(x),fmt,__VA_ARGS__)
static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
/*---[ Implement ]-------------------------------------------------------------------------------------------------------*/
H3270FT * get_ft_handle(H3270 *hSession) {
return hSession->ft;
}
static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state) {
static const struct _msg {
LIB3270_FT_STATE state;
const char * text;
} msg[] = {
{ LIB3270_FT_STATE_NONE, N_( "No transfer in progress" ) },
{ LIB3270_FT_STATE_RUNNING, N_( "Ack received, data flowing" ) },
{ LIB3270_FT_STATE_ABORT_WAIT, N_( "Awaiting chance to send an abort" ) },
{ LIB3270_FT_STATE_ABORT_SENT, N_( "Abort sent; awaiting response" ) },
};
int f;
const char *message = NULL;
if(session->state == state)
return;
// State changed, notify
for(f = 0; f < sizeof(msg)/sizeof(msg[0]); f++) {
if(msg[f].state == state) {
message = msg[f].text;
break;
}
}
session->state = state;
ft_message(session,message);
session->cbk.state_changed(session->host,state,dgettext(GETTEXT_PACKAGE,message),session->user_data);
}
void ft_init(H3270 *hSession) {
/* Register for state changes. */
lib3270_register_schange(hSession, LIB3270_STATE_CONNECT, ( void (*)(H3270 *, int, void *)) ft_connected, NULL);
lib3270_register_schange(hSession, LIB3270_STATE_3270_MODE, ( void (*)(H3270 *, int, void *)) ft_in3270, NULL);
}
LIB3270_EXPORT int lib3270_ft_cancel(H3270 *hSession, int force, const char *reason) {
H3270FT *ft;
CHECK_SESSION_HANDLE(hSession);
ft = get_ft_handle(hSession);
if(!ft)
return errno = EINVAL;
if (ft->state == LIB3270_FT_STATE_RUNNING) {
set_ft_state(ft,LIB3270_FT_STATE_ABORT_WAIT);
ft->cbk.aborting(hSession,reason,ft->user_data);
return 0;
}
if(!force)
return errno = EBUSY;
// Impatient user or hung host -- just clean up.
if(!reason)
reason = _("Cancelled by user");
ft_failed(ft, reason);
return 0;
}
static void def_complete(H3270 *hSession, unsigned long GNUC_UNUSED(length), double GNUC_UNUSED(kbytes_sec), const char *msg, void GNUC_UNUSED(*userdata)) {
hSession->ft->cbk.message(hSession,msg,hSession->ft->user_data);
}
static void def_failed(H3270 *hSession, unsigned long length, double kbytes_sec, const char *msg, void *userdata) {
hSession->ft->cbk.complete(hSession,length,kbytes_sec,msg,userdata);
}
static void def_message(H3270 *hSession, const char *msg, void GNUC_UNUSED(*userdata)) {
lib3270_write_log(hSession,"ft","%s",msg);
}
static void def_update(H3270 GNUC_UNUSED(*hSession), unsigned long GNUC_UNUSED(current), unsigned long GNUC_UNUSED(length), double GNUC_UNUSED(kbytes_sec), void GNUC_UNUSED(*userdata)) {
}
static void def_running(H3270 GNUC_UNUSED(*hSession), int GNUC_UNUSED(is_cut), void GNUC_UNUSED(*userdata)) {
}
static void def_aborting(H3270 GNUC_UNUSED(*hSession), const char GNUC_UNUSED(*reason), void GNUC_UNUSED(*userdata)) {
}
static void def_state_changed(H3270 GNUC_UNUSED(*hSession), LIB3270_FT_STATE GNUC_UNUSED(state), const char GNUC_UNUSED(*text), void GNUC_UNUSED(*userdata)) {
}
static H3270FT * ft_creation_failed(H3270 *session, int rc, const char **dest, const char *message) {
errno = rc;
if(!dest) {
// Nao tem destino para a mensagem, apresenta
lib3270_popup_dialog(session, LIB3270_NOTIFY_ERROR, _( "Request failed" ), _( "Can't start file transfer." ), "%s", message);
} else {
*dest = message;
}
return NULL;
}
LIB3270_EXPORT H3270FT * lib3270_ft_new(H3270 *session, LIB3270_FT_OPTION flags, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft, const char **message) {
static const unsigned short asc2ft[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xd5, 0x5c, 0xe5, 0xd8, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x5d, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 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b2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0xff
};
H3270FT * ftHandle = (H3270FT *) session->ft;
FILE * ft_local_file = NULL;
int f;
if(!lib3270_is_connected(session))
return ft_creation_failed(session,ENOTCONN,message,_( "Disconnected from host." ));
if(ftHandle)
return ft_creation_failed(session,EBUSY,message,_( "File transfer is already active in this session." ));
// Check remote file
if(!*remote)
return ft_creation_failed(session,EINVAL,message,_( "The remote file name is invalid." ));
// Open local file
#ifdef _WIN32
ft_local_file = fopen(local,(flags & LIB3270_FT_OPTION_RECEIVE) ? ((flags & LIB3270_FT_OPTION_APPEND) ? "ab" : "wb") : "rb");
#else
ft_local_file = fopen(local,(flags & LIB3270_FT_OPTION_RECEIVE) ? ((flags & LIB3270_FT_OPTION_APPEND) ? "a" : "w") : "r");
#endif // _WIN32
if(!ft_local_file)
return ft_creation_failed(session,errno,message,strerror(errno));
// Set options
lib3270_set_dft_buffersize(session, dft);
// Create & Initialize ft control structure.
ftHandle = lib3270_malloc(sizeof(H3270FT)+strlen(local)+strlen(remote)+3);
ftHandle->host = session;
session->ft = ftHandle;
ftHandle->ft_last_cr = 0;
ftHandle->ft_is_cut = 0;
ftHandle->ascii_flag = (flags & LIB3270_FT_OPTION_ASCII) ? 1 : 0;
ftHandle->cr_flag = (flags & LIB3270_FT_OPTION_CRLF) ? 1 : 0;
ftHandle->remap_flag = (flags & LIB3270_FT_OPTION_REMAP) ? 1 : 0;
ftHandle->unix_text = (flags & LIB3270_FT_OPTION_UNIX) ? 1 : 0;
ftHandle->flags = flags;
ftHandle->local_file = ft_local_file;
ftHandle->state = LIB3270_FT_STATE_AWAIT_ACK;
ftHandle->lrecl = lrecl;
ftHandle->blksize = blksize;
ftHandle->primspace = primspace;
ftHandle->secspace = secspace;
ftHandle->dft = dft;
ftHandle->quadrant = -1;
// Setup file transfer charset.
memcpy(&ftHandle->charset,&session->charset,sizeof(struct lib3270_charset));
// Copy "IND$FILE" charset.
for(f=0; f<0x0100; f++) {
ftHandle->charset.ebc2asc[f] = ft2asc[f];
ftHandle->charset.asc2ebc[f] = asc2ft[f];
}
// Setup files.
ftHandle->local = (char *) (ftHandle+1);
strcpy((char *) ftHandle->local,local);
ftHandle->remote = ftHandle->local + strlen(ftHandle->local)+1;
strcpy((char *) ftHandle->remote,remote);
lib3270_reset_ft_callbacks(session);
return ftHandle;
}
LIB3270_EXPORT int lib3270_reset_ft_callbacks(H3270 *hSession) {
CHECK_SESSION_HANDLE(hSession);
if(!hSession->ft) {
return errno = EINVAL;
}
hSession->ft->cbk.complete = def_complete;
hSession->ft->cbk.failed = def_failed;
hSession->ft->cbk.message = def_message;
hSession->ft->cbk.update = def_update;
hSession->ft->cbk.running = def_running;
hSession->ft->cbk.aborting = def_aborting;
hSession->ft->cbk.state_changed = def_state_changed;
return 0;
}
LIB3270_EXPORT void lib3270_ft_set_user_data(H3270 *hSession, void *ptr) {
H3270FT * ft;
CHECK_SESSION_HANDLE(hSession);
ft = get_ft_handle(hSession);
if(!ft)
return;
ft->user_data = ptr;
}
LIB3270_EXPORT void * lib3270_ft_get_user_data(H3270 *hSession) {
H3270FT * ft;
CHECK_SESSION_HANDLE(hSession);
ft = get_ft_handle(hSession);
if(!ft)
return NULL;
return ft->user_data;
}
LIB3270_EXPORT int lib3270_ft_start(H3270 *hSession) {
static const char * rec = "FVU";
static const char * un[] = { "tracks", "cylinders", "avblock" };
char op[4096];
char buffer[4096];
unsigned int flen;
unsigned short recfm;
unsigned short units;
H3270FT * ft;
CHECK_SESSION_HANDLE(hSession);
ft = get_ft_handle(hSession);
if(!ft)
return EINVAL;
recfm = (ft->flags & LIB3270_FT_RECORD_FORMAT_MASK) >> 8;
units = (ft->flags & LIB3270_FT_ALLOCATION_UNITS_MASK) >> 12;
ft->ascii_flag = (ft->flags & LIB3270_FT_OPTION_ASCII) ? 1 : 0;
ft->cr_flag = (ft->flags & LIB3270_FT_OPTION_CRLF) ? 1 : 0;
ft->remap_flag = (ft->flags & LIB3270_FT_OPTION_REMAP) ? 1 : 0;
if(ft->flags & LIB3270_FT_OPTION_RECEIVE) {
// Receiving file
lib3270_write_dstrace(
ft->host,
"\nReceiving file %s (%s %s %s %s)\n",
ft->local,
ft->ascii_flag ? "ASCII" : "BINARY",
ft->cr_flag ? "CRLF" : "NOCRLF",
ft->remap_flag ? "REMAP" : "NOREMAP",
ft->unix_text ? "LF Only" : "CR/LF"
);
} else {
// Sending file
if(fseek(ft->local_file,0L,SEEK_END) < 0) {
ft_failed(ft,_( "Can't get file size" ));
return errno ? errno : -1;
}
ft->length = ftell(ft->local_file);
lib3270_write_dstrace(
ft->host,
"\nSending file %s (%ld bytes %s %s %s %s)\n",
ft->local,
ft->length,
ft->ascii_flag ? "ASCII" : "BINARY",
ft->cr_flag ? "CRLF" : "NOCRLF",
ft->remap_flag ? "REMAP" : "NOREMAP",
ft->unix_text ? "LF only" : "CR/LF"
);
rewind(ft->local_file);
}
/* Build the ind$file command */
snprintf(op,4095,"%s%s%s",
(ft->flags & LIB3270_FT_OPTION_ASCII) ? " ASCII" : "",
(ft->flags & LIB3270_FT_OPTION_CRLF) ? " CRLF" : "",
(ft->flags & LIB3270_FT_OPTION_APPEND) ? " APPEND" : ""
);
trace("tso=%s",lib3270_is_tso(hSession) ? "yes" : "No");
if(!(ft->flags & LIB3270_FT_OPTION_RECEIVE)) {
// Sending file
if(lib3270_is_tso(hSession)) {
// TSO Host
if(recfm > 0) {
snconcat(op,4096," RECFM(%c)",rec[recfm-1]);
if(ft->lrecl > 0)
snconcat(op,4096," LRECL(%d)",ft->lrecl);
if(ft->blksize > 0)
snconcat(op,4096," BLKSIZE(%d)", ft->blksize);
}
if(units > 0) {
snconcat(op,4096," %s",un[units-1]);
if(ft->primspace > 0) {
snconcat(op,4096," SPACE(%d",ft->primspace);
if(ft->secspace)
snconcat(op,4096,",%d",ft->secspace);
snconcat(op,4096,"%s",")");
}
}
} else {
// VM Host
if(recfm > 0) {
snconcat(op,4096," RECFM %c",rec[recfm-1]);
if(ft->lrecl > 0)
snconcat(op,4096," LRECL %d",ft->lrecl);
}
}
}
snprintf(buffer,4095,"%s %s %s", "IND$FILE",
(ft->flags & LIB3270_FT_OPTION_RECEIVE) ? "GET" : "PUT",
ft->remote );
if(*op) {
if(lib3270_is_tso(hSession))
snconcat(buffer,4095," %s",op+1);
else
snconcat(buffer,4095," (%s",op+1);
}
snconcat(buffer,4095,"%s","\n");
trace("%s file using %s",(ft->flags & LIB3270_FT_OPTION_RECEIVE) ? "Receiving" : "Sending",buffer);
trace_ds(hSession,"\n%s file using %s",(ft->flags & LIB3270_FT_OPTION_RECEIVE) ? "Receiving" : "Sending",buffer);
// Erase the line and enter the command.
flen = kybd_prime(ft->host);
if (!flen || flen < strlen(buffer) - 1) {
lib3270_write_log(ft->host, "ft", "Unable to send command \"%s\" (flen=%d szBuffer=%u)",buffer,flen,(unsigned int) strlen(buffer));
ft_failed(ft,_( "Unable to send file-transfer request" ));
return errno = EINVAL;
}
lib3270_write_event_trace(hSession,"Sending FT request:\n%s\n",buffer);
lib3270_emulate_input(ft->host, buffer, strlen(buffer), False);
if(ft->flags & LIB3270_FT_OPTION_RECEIVE)
ft_message(ft,_( "Waiting for GET response" ));
else
ft_message(ft,_( "Waiting for PUT response" ));
return 0;
}
/* External entry points called by ft_dft and ft_cut. */
void ft_message(H3270FT *ft, const char *msg) {
lib3270_write_event_trace(ft->host,"%s\n",msg);
ft->cbk.message(ft->host,msg,ft->user_data);
}
static double finish(H3270FT *ft) {
double kbytes_sec = 0;
struct timeval t1;
(void) gettimeofday(&t1, (struct timezone *)NULL);
kbytes_sec = (double) ft->ft_length / 1024.0 /
((double)(t1.tv_sec - ft->starting_time.tv_sec) +
(double)(t1.tv_usec - ft->starting_time.tv_usec) / 1.0e6);
// Close the local file.
if(ft->local_file) {
fclose(ft->local_file);
ft->local_file = NULL;
}
// Clean up the state.
set_ft_state(ft,LIB3270_FT_STATE_NONE);
ft_update_length(ft);
return kbytes_sec;
}
void ft_complete(H3270FT *ft, const char *errmsg) {
ft->cbk.complete(ft->host,ft->ft_length,finish(ft),errmsg ? errmsg : _("Transfer complete"),ft->user_data);
}
void ft_failed(H3270FT *ft, const char *errmsg) {
ft->cbk.failed(ft->host,ft->ft_length,finish(ft),errmsg ? errmsg : _("Transfer failed"),ft->user_data);
}
LIB3270_EXPORT int lib3270_ft_destroy(H3270 *hSession, const char *reason) {
H3270FT *session;
CHECK_SESSION_HANDLE(hSession);
session = (H3270FT *) hSession->ft;
if(!session)
return EINVAL;
if (session->state != LIB3270_FT_STATE_NONE) {
lib3270_ft_cancel(hSession,1,reason);
}
if(session->local_file) {
fclose(session->local_file);
session->local_file = NULL;
}
hSession->ft = NULL;
lib3270_free(session);
return 0;
}
// Update the bytes-transferred count on the progress pop-up.
void ft_update_length(H3270FT *session) {
double kbytes_sec = 0;
if(session->ft_length > 1024.0) {
struct timeval t1;
(void) gettimeofday(&t1, (struct timezone *)NULL);
kbytes_sec = (double)session->ft_length / 1024.0 /
((double)(t1.tv_sec - session->starting_time.tv_sec) +
(double)(t1.tv_usec - session->starting_time.tv_usec) / 1.0e6);
}
session->cbk.update(session->host,session->ft_length,session->length,kbytes_sec,session->user_data);
}
/**
* Process a transfer acknowledgement.
*
*/
void ft_running(H3270FT *ft, Boolean is_cut) {
ft->ft_is_cut = is_cut ? 1 : 0;
ft->ft_length = 0;
gettimeofday(&ft->starting_time, (struct timezone *)NULL);
if (ft->state == LIB3270_FT_STATE_AWAIT_ACK)
set_ft_state(ft,LIB3270_FT_STATE_RUNNING);
ft->cbk.running(ft->host,is_cut,ft->user_data);
ft_update_length(ft);
}
LIB3270_EXPORT struct lib3270_ft_callbacks * lib3270_get_ft_callbacks(H3270 *session, unsigned short sz) {
CHECK_SESSION_HANDLE(session);
if(sz != sizeof(struct lib3270_ft_callbacks))
return NULL;
if(session->ft)
return &(session->ft->cbk);
return NULL;
}
// Process a protocol-generated abort.
void ft_aborting(H3270FT *h, const char *reason) {
if (h->state == LIB3270_FT_STATE_RUNNING || h->state == LIB3270_FT_STATE_ABORT_WAIT) {
set_ft_state(h,LIB3270_FT_STATE_ABORT_SENT);
h->cbk.message(h->host,_("Aborting..."),h->user_data);
h->cbk.aborting(h->host,reason,h->user_data);
}
}
/* Process a disconnect abort. */
static void ft_connected(H3270 *hSession, int GNUC_UNUSED(ignored), void GNUC_UNUSED(*dunno)) {
if (!CONNECTED && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE)
ft_failed(get_ft_handle(hSession),_("Host disconnected, transfer cancelled"));
}
/// @brief Process an abort from no longer being in 3270 mode.
static void ft_in3270(H3270 *hSession, int GNUC_UNUSED(ignored), void GNUC_UNUSED(*dunno)) {
if (!IN_3270 && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE)
ft_failed(get_ft_handle(hSession),_("Not in 3270 mode, transfer cancelled"));
}
LIB3270_EXPORT LIB3270_FT_STATE lib3270_get_ft_state(H3270 *session) {
CHECK_SESSION_HANDLE(session);
if(!session->ft)
return LIB3270_FT_STATE_NONE;
return ((H3270FT *) session->ft)->state;
}
LIB3270_EXPORT int lib3270_send(H3270 *hSession, const char GNUC_UNUSED(*from), const char GNUC_UNUSED(*to), const char GNUC_UNUSED(**args)) {
FAIL_IF_NOT_ONLINE(hSession);
if(hSession->ft)
return EBUSY;
return ENOTSUP;
}
LIB3270_EXPORT int lib3270_receive(H3270 *hSession, const char GNUC_UNUSED(*from), const char GNUC_UNUSED(*to), const char GNUC_UNUSED(**args)) {
FAIL_IF_NOT_ONLINE(hSession);
if(hSession->ft)
return EBUSY;
return ENOTSUP;
}