From bfcf4caaee030acb1aac0a8e7822f2d33b34c069 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Mon, 6 Aug 2012 18:49:39 +0000 Subject: [PATCH] Ajustando transferência de arquivos para funcionar em multi-sessão --- ft.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------- ft_cut.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------ ft_dft.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------- ftc.h | 20 +++++++++++--------- 4 files changed, 306 insertions(+), 264 deletions(-) diff --git a/ft.c b/ft.c index adefec6..1e6ad23 100644 --- a/ft.c +++ b/ft.c @@ -18,7 +18,7 @@ * 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 2143 linhas de código. + * Este programa está nomeado como ft.c e possui - linhas de código. * * Contatos: * @@ -26,7 +26,6 @@ * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) * licinio@bb.com.br (Licínio Luis Branco) * kraucer@bb.com.br (Kraucer Fernandes Mazuco) - * macmiranda@bb.com.br (Marco Aurélio Caldas Miranda) * */ @@ -79,16 +78,16 @@ static void ft_in3270(H3270 *session, int ignored unused, void *unused); #define BN (Boolean *)NULL // Globals. -H3270FT *ftsession = NULL; +// H3270FT *ftsession = NULL; // enum ft_state ft_state = FT_NONE; // File transfer state // char *ft_local_filename; // Local file to transfer to/from -Boolean ft_last_cr = False; // CR was last char in local file -Boolean ascii_flag = True; // Convert to ascii -Boolean cr_flag = True; // Add crlf to each line -Boolean remap_flag = True; // Remap ASCII<->EBCDIC -unsigned long ft_length = 0; // Length of transfer -static Boolean ft_is_cut; // File transfer is CUT-style +// Boolean ft_last_cr = 0; // CR was last char in local file +// Boolean ascii_flag = True; // Convert to ascii +// Boolean cr_flag = True; // Add crlf to each line +// Boolean remap_flag = True; // Remap ASCII<->EBCDIC +// unsigned long ft_length = 0; // Length of transfer +// static Boolean ft_is_cut; // File transfer is CUT-style // static struct timeval starting_time; // Starting time @@ -101,13 +100,19 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); /*---[ Implement ]-------------------------------------------------------------------------------------------------------*/ - static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state) + H3270FT * get_ft_handle(H3270 *hSession) { - CHECK_FT_HANDLE(session); + if(!hSession->ft) + { + popup_an_error(hSession,"Unexpected call to %s: No active filetransfer",__FUNCTION__); + } + return hSession->ft; + } + static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state) + { if(session->state == state) return; - session->state = state; session->state_changed(session,state); @@ -126,10 +131,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); CHECK_SESSION_HANDLE(hSession); - ft = (H3270FT *) hSession->ft; - if(!ft) - return EINVAL; - + ft = get_ft_handle(hSession); if (ft->state == LIB3270_FT_STATE_RUNNING) { @@ -180,7 +182,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); 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 **msg) { - H3270FT * ftHandle = NULL; + H3270FT * ftHandle = (H3270FT *) session->ft; FILE * ft_local_file = NULL; // unsigned long length = 0L; @@ -192,19 +194,21 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); return NULL; } - if(session->ft) + if(ftHandle) { *msg = N_( "File transfer is already active in this session" ); errno = EBUSY; return NULL; } +/* if(ftsession) { *msg = N_( "File transfer is already active" ); errno = EBUSY; return NULL; } +*/ // Check remote file if(!*remote) @@ -227,14 +231,17 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); session->dft_buffersize = dft; set_dft_buffersize(session); - // Initialize ft control structure. - ft_last_cr = False; - ft_is_cut = False; - + // Create & Initialize ft control structure. ftHandle = lib3270_malloc(sizeof(H3270FT)+strlen(local)+strlen(remote)+3); ftHandle->sz = sizeof(H3270FT); ftHandle->host = session; + + ftHandle->ft_last_cr = 0; + ftHandle->remap_flag = 1; // Remap ASCII<->EBCDIC + ftHandle->cr_flag = 1; + ftHandle->ascii_flag = 1; + ftHandle->ft_is_cut = 0; ftHandle->flags = flags; ftHandle->local_file = ft_local_file; ftHandle->state = LIB3270_FT_STATE_AWAIT_ACK; @@ -249,6 +256,8 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); ftHandle->primspace = primspace; ftHandle->secspace = secspace; ftHandle->dft = dft; + ftHandle->quadrant = -1; + ftHandle->local = (char *) (ftHandle+1); strcpy((char *) ftHandle->local,local); @@ -258,7 +267,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); session->ft = ftHandle; - return ftsession = ftHandle; + return ftHandle; } LIB3270_EXPORT int lib3270_ft_start(H3270 *hSession) @@ -275,15 +284,15 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); CHECK_SESSION_HANDLE(hSession); - ft = (H3270FT *) hSession->ft; + ft = get_ft_handle(hSession); if(!ft) return EINVAL; recfm = (ft->flags & FT_RECORD_FORMAT_MASK) >> 8; units = (ft->flags & FT_ALLOCATION_UNITS_MASK) >> 12; - ascii_flag = ((ft->flags & LIB3270_FT_OPTION_ASCII) != 0); - cr_flag = ((ft->flags & LIB3270_FT_OPTION_CRLF) != 0); - remap_flag = ((ft->flags & LIB3270_FT_OPTION_ASCII) != 0); + ft->ascii_flag = ((ft->flags & LIB3270_FT_OPTION_ASCII) == 0) ? 1 : 0; + ft->cr_flag = ((ft->flags & LIB3270_FT_OPTION_CRLF) == 0) ? 1 : 0; + ft->remap_flag = ((ft->flags & LIB3270_FT_OPTION_ASCII) == 0) ? 1 : 0; if(ft->flags & LIB3270_FT_OPTION_RECEIVE) { @@ -399,32 +408,30 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); * Pop up a message, end the transfer, release resources. * */ -void ft_complete(H3270FT *session, const char *errmsg) +void ft_complete(H3270FT *ft, const char *errmsg) { - double kbytes_sec = 0; - struct timeval t1; - - CHECK_FT_HANDLE(session); + double kbytes_sec = 0; + struct timeval t1; (void) gettimeofday(&t1, (struct timezone *)NULL); - kbytes_sec = (double)ft_length / 1024.0 / - ((double)(t1.tv_sec - session->starting_time.tv_sec) + - (double)(t1.tv_usec - session->starting_time.tv_usec) / 1.0e6); + 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(session->local_file) + if(ft->local_file) { - fclose(session->local_file); - session->local_file = NULL; + fclose(ft->local_file); + ft->local_file = NULL; } // Clean up the state. - set_ft_state(session,FT_NONE); + set_ft_state(ft,FT_NONE); - ft_update_length(session); + ft_update_length(ft); - session->message(session,errmsg); - session->complete(session,ft_length,kbytes_sec,ft_is_cut ? "CUT" : "DFT"); + ft->message(ft,errmsg); + ft->complete(ft,ft->ft_length,kbytes_sec,ft->ft_is_cut ? "CUT" : "DFT"); } @@ -447,12 +454,12 @@ LIB3270_EXPORT int lib3270_ft_destroy(H3270 *hSession) session->local_file = NULL; } - if(session == ftsession) - ftsession = NULL; +// if(session == ftsession) +// ftsession = NULL; hSession->ft = NULL; - free(session); + lib3270_free(session); return 0; } @@ -462,46 +469,43 @@ void ft_update_length(H3270FT *session) { double kbytes_sec = 0; - CHECK_FT_HANDLE(session); - - if(ft_length > 1024.0) + if(session->ft_length > 1024.0) { struct timeval t1; (void) gettimeofday(&t1, (struct timezone *)NULL); - kbytes_sec = (double)ft_length / 1024.0 / + 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->update(session,ft_length,session->length,kbytes_sec); + session->update(session,session->ft_length,session->length,kbytes_sec); } -// Process a transfer acknowledgement. -void ft_running(H3270FT *h, Boolean is_cut) +/** + * Process a transfer acknowledgement. + * + */ +void ft_running(H3270FT *ft, Boolean is_cut) { - CHECK_FT_HANDLE(h); - - ft_is_cut = is_cut; - ft_length = 0; + ft->ft_is_cut = is_cut ? 1 : 0; + ft->ft_length = 0; - gettimeofday(&h->starting_time, (struct timezone *)NULL); + gettimeofday(&ft->starting_time, (struct timezone *)NULL); - if (h->state == FT_AWAIT_ACK) - set_ft_state(h,FT_RUNNING); + if (ft->state == FT_AWAIT_ACK) + set_ft_state(ft,FT_RUNNING); - h->running(h,is_cut); + ft->running(ft,is_cut); - ft_update_length(h); + ft_update_length(ft); } // Process a protocol-generated abort. void ft_aborting(H3270FT *h) { - CHECK_FT_HANDLE(h); - if (h->state == FT_RUNNING || h->state == FT_ABORT_WAIT) set_ft_state(h,FT_ABORT_SENT); @@ -513,14 +517,14 @@ void ft_aborting(H3270FT *h) static void ft_connected(H3270 *session, int ignored, void *dunno) { if (!CONNECTED && lib3270_get_ft_state(session) != LIB3270_FT_STATE_NONE) - ft_complete(session->ft,_("Host disconnected, transfer cancelled")); + ft_complete(get_ft_handle(session),_("Host disconnected, transfer cancelled")); } /* Process an abort from no longer being in 3270 mode. */ static void ft_in3270(H3270 *session, int ignored, void *dunno) { if (!IN_3270 && lib3270_get_ft_state(session) != LIB3270_FT_STATE_NONE) - ft_complete(session->ft,_("Not in 3270 mode, transfer cancelled")); + ft_complete(get_ft_handle(session),_("Not in 3270 mode, transfer cancelled")); } LIB3270_EXPORT LIB3270_FT_STATE lib3270_get_ft_state(H3270 *session) diff --git a/ft_cut.c b/ft_cut.c index 1df6d51..37ef118 100644 --- a/ft_cut.c +++ b/ft_cut.c @@ -58,9 +58,9 @@ /* Data stream conversion tables. */ -#define NQ 4 /* number of quadrants */ -#define NE 77 /* number of elements per quadrant */ -#define OTHER_2 2 /* "OTHER 2" quadrant (includes NULL) */ +#define NQ 4 /* number of quadrants */ +#define NE 77 /* number of elements per quadrant */ +#define OTHER_2 2 /* "OTHER 2" quadrant (includes NULL) */ #define XLATE_NULL 0xc1 /* translation of NULL */ static const char alphas[NE + 1] = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%&_()<+,-./:>?"; @@ -109,14 +109,14 @@ static const struct }; static const char table6[] = "abcdefghijklmnopqrstuvwxyz&-.,:+ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"; -static int quadrant = -1; -static unsigned long expanded_length; -static char *saved_errmsg = CN; +// static int quadrant = -1; +// static unsigned long expanded_length; +// static char *saved_errmsg = CN; -#define XLATE_NBUF 4 -static int xlate_buffered = 0; /* buffer count */ -static int xlate_buf_ix = 0; /* buffer index */ -static unsigned char xlate_buf[XLATE_NBUF]; /* buffer */ +#define XLATE_NBUF LIB3270_XLATE_NBUF +// static int xlate_buffered = 0; /* buffer count */ +// static int xlate_buf_ix = 0; /* buffer index */ +// static unsigned char xlate_buf[XLATE_NBUF]; /* buffer */ static void cut_control_code(H3270 *hSession); static void cut_data_request(H3270 *hSession); @@ -127,7 +127,7 @@ static void cut_ack(H3270 *hSession); static void cut_abort(H3270 *hSession, unsigned short code, const char *fmt, ...) printflike(3,4); static unsigned from6(unsigned char c); -static int xlate_getc(void); +static int xlate_getc(H3270FT *ft); /** * Convert a buffer for uploading (host->local). Overwrites the buffer. @@ -138,8 +138,9 @@ static int xlate_getc(void); */ static int upload_convert(H3270 *hSession, unsigned char *buf, int len) { - unsigned char *ob0 = buf; - unsigned char *ob = ob0; + unsigned char * ob0 = buf; + unsigned char * ob = ob0; + H3270FT * ft = get_ft_handle(hSession); while (len--) { @@ -149,15 +150,15 @@ static int upload_convert(H3270 *hSession, unsigned char *buf, int len) // int oq = -1; retry: - if (quadrant < 0) + if (ft->quadrant < 0) { /* Find the quadrant. */ - for (quadrant = 0; quadrant < NQ; quadrant++) + for (ft->quadrant = 0; ft->quadrant < NQ; ft->quadrant++) { - if (c == conv[quadrant].selector) + if (c == conv[ft->quadrant].selector) break; } - if (quadrant >= NQ) + if (ft->quadrant >= NQ) { cut_abort(hSession,SC_ABORT_XMIT, "%s", _("Data conversion error")); return -1; @@ -178,7 +179,7 @@ static int upload_convert(H3270 *hSession, unsigned char *buf, int len) { /* Try a different quadrant. */ // oq = quadrant; - quadrant = -1; + ft->quadrant = -1; goto retry; } ix = ixp - alphas; @@ -187,19 +188,19 @@ static int upload_convert(H3270 *hSession, unsigned char *buf, int len) * See if it's mapped by that quadrant, handling NULLs * specially. */ - if (quadrant != OTHER_2 && c != XLATE_NULL && !conv[quadrant].xlate[ix]) + if (ft->quadrant != OTHER_2 && c != XLATE_NULL && !conv[ft->quadrant].xlate[ix]) { /* Try a different quadrant. */ // oq = quadrant; - quadrant = -1; + ft->quadrant = -1; goto retry; } /* Map it. */ - c = conv[quadrant].xlate[ix]; - if (ascii_flag && cr_flag && (c == '\r' || c == 0x1a)) + c = conv[ft->quadrant].xlate[ix]; + if (ft->ascii_flag && ft->cr_flag && (c == '\r' || c == 0x1a)) continue; - if (ascii_flag && remap_flag) + if (ft->ascii_flag && ft->remap_flag) c = ft2asc[c]; *ob++ = c; } @@ -207,60 +208,68 @@ static int upload_convert(H3270 *hSession, unsigned char *buf, int len) return ob - ob0; } -/* Convert a buffer for downloading (local->host). */ -static int -download_convert(unsigned const char *buf, unsigned len, unsigned char *xobuf) +/** + * Convert a buffer for downloading (local->host). + */ +static int download_convert(unsigned const char *buf, unsigned len, unsigned char *xobuf) { - unsigned char *ob0 = xobuf; - unsigned char *ob = ob0; + H3270FT * ft = get_ft_handle(&h3270); + unsigned char * ob0 = xobuf; + unsigned char * ob = ob0; - while (len--) { + while (len--) + { unsigned char c = *buf++; unsigned char *ixp; unsigned ix; int oq; /* Handle nulls separately. */ - if (!c) { - if (quadrant != OTHER_2) { - quadrant = OTHER_2; - *ob++ = conv[quadrant].selector; + if (!c) + { + if (ft->quadrant != OTHER_2) + { + ft->quadrant = OTHER_2; + *ob++ = conv[ft->quadrant].selector; } *ob++ = XLATE_NULL; continue; } /* Translate. */ - if (ascii_flag && remap_flag) + if (ft->ascii_flag && ft->remap_flag) c = asc2ft[c]; /* Quadrant already defined. */ - if (quadrant >= 0) { - ixp = (unsigned char *)memchr(conv[quadrant].xlate, c, - NE); - if (ixp != (unsigned char *)NULL) { - ix = ixp - conv[quadrant].xlate; + if (ft->quadrant >= 0) { + ixp = (unsigned char *)memchr(conv[ft->quadrant].xlate, c, NE); + if (ixp != (unsigned char *)NULL) + { + ix = ixp - conv[ft->quadrant].xlate; *ob++ = asc2ebc[(int)alphas[ix]]; continue; } } /* Locate a quadrant. */ - oq = quadrant; - for (quadrant = 0; quadrant < NQ; quadrant++) { - if (quadrant == oq) + oq = ft->quadrant; + for (ft->quadrant = 0; ft->quadrant < NQ; ft->quadrant++) + { + if (ft->quadrant == oq) continue; - ixp = (unsigned char *)memchr(conv[quadrant].xlate, c, - NE); + + ixp = (unsigned char *)memchr(conv[ft->quadrant].xlate, c, NE); + if (ixp == (unsigned char *)NULL) continue; - ix = ixp - conv[quadrant].xlate; - *ob++ = conv[quadrant].selector; + ix = ixp - conv[ft->quadrant].xlate; + *ob++ = conv[ft->quadrant].selector; *ob++ = asc2ebc[(int)alphas[ix]]; break; } - if (quadrant >= NQ) { - quadrant = -1; + if (ft->quadrant >= NQ) + { + ft->quadrant = -1; fprintf(stderr, "Oops\n"); continue; } @@ -304,10 +313,11 @@ void ft_cut_data(H3270 *hSession) */ static void cut_control_code(H3270 *hSession) { - unsigned short code; - char *buf; - char *bp; - int i; + H3270FT * ft = get_ft_handle(hSession); + unsigned short code; + char * buf; + char * bp; + int i; trace_ds(hSession,"< FT CONTROL_CODE "); code = (hSession->ea_buf[O_CC_STATUS_CODE].cc << 8) | hSession->ea_buf[O_CC_STATUS_CODE + 1].cc; @@ -317,18 +327,18 @@ static void cut_control_code(H3270 *hSession) case SC_HOST_ACK: trace_ds(hSession,"HOST_ACK\n"); hSession->cut_xfer_in_progress = 1; - expanded_length = 0; - quadrant = -1; - xlate_buffered = 0; + ft->expanded_length = 0; + ft->quadrant = -1; + ft->xlate_buffered = 0; cut_ack(hSession); - ft_running(NULL,True); + ft_running(hSession->ft,True); break; case SC_XFER_COMPLETE: trace_ds(hSession,"XFER_COMPLETE\n"); cut_ack(hSession); hSession->cut_xfer_in_progress = 0; - ft_complete(NULL,N_( "Complete" ) ); + ft_complete(ft,N_( "Complete" ) ); break; case SC_ABORT_FILE: @@ -337,10 +347,10 @@ static void cut_control_code(H3270 *hSession) hSession->cut_xfer_in_progress = 0; cut_ack(hSession); - if (lib3270_get_ft_state(hSession) == FT_ABORT_SENT && saved_errmsg != CN) + if (lib3270_get_ft_state(hSession) == FT_ABORT_SENT && ft->saved_errmsg != CN) { - buf = saved_errmsg; - saved_errmsg = CN; + buf = ft->saved_errmsg; + ft->saved_errmsg = CN; } else { @@ -363,7 +373,7 @@ static void cut_control_code(H3270 *hSession) if (!*buf) strcpy(buf, N_( "Transfer cancelled by host" ) ); } - ft_complete(NULL,buf); + ft_complete(hSession->ft,buf); lib3270_free(buf); break; @@ -379,12 +389,13 @@ static void cut_control_code(H3270 *hSession) */ static void cut_data_request(H3270 *hSession) { - unsigned char seq = hSession->ea_buf[O_DR_FRAME_SEQ].cc; - int count; - unsigned char cs; - int c; - int i; - unsigned char attr; + H3270FT * ft = get_ft_handle(hSession); + unsigned char seq = hSession->ea_buf[O_DR_FRAME_SEQ].cc; + int count; + unsigned char cs; + int c; + int i; + unsigned char attr; trace_ds(hSession,"< FT DATA_REQUEST %u\n", from6(seq)); if (lib3270_get_ft_state(hSession) == FT_ABORT_WAIT) @@ -393,15 +404,18 @@ static void cut_data_request(H3270 *hSession) return; } + /* Copy data into the screen buffer. */ count = 0; - while (count < O_UP_MAX && (c = xlate_getc()) != EOF) { + while (count < O_UP_MAX && (c = xlate_getc(hSession->ft)) != EOF) + { ctlr_add(hSession,O_UP_DATA + count, c, 0); count++; } /* Check for errors. */ - if (ferror(((H3270FT *) hSession->ft)->local_file)) { + if (ferror(((H3270FT *) hSession->ft)->local_file)) + { int j; /* Clean out any data we may have written. */ @@ -438,8 +452,8 @@ static void cut_data_request(H3270 *hSession) /* Send it up to the host. */ trace_ds(hSession,"> FT DATA %u\n", from6(seq)); - ft_update_length(NULL); - expanded_length += count; + ft_update_length(ft); + ft->expanded_length += count; lib3270_enter(hSession); } @@ -473,13 +487,14 @@ from6(unsigned char c) */ static void cut_data(H3270 *hSession) { + H3270FT *ft = get_ft_handle(hSession); static unsigned char cvbuf[O_RESPONSE - O_DT_DATA]; unsigned short raw_length; int conv_length; register int i; trace_ds(hSession,"< FT DATA\n"); - if (((H3270FT *) hSession->ft)->state == LIB3270_FT_STATE_ABORT_WAIT) + if (ft->state == LIB3270_FT_STATE_ABORT_WAIT) { cut_abort(hSession,SC_ABORT_FILE,"%s",_("Transfer cancelled by user")); return; @@ -510,14 +525,14 @@ static void cut_data(H3270 *hSession) return; /* Write it to the file. */ - if (fwrite((char *)cvbuf, conv_length, 1, ((H3270FT *) hSession->ft)->local_file) == 0) + if (fwrite((char *)cvbuf, conv_length, 1, ft->local_file) == 0) { cut_abort(hSession,SC_ABORT_FILE,_( "Error \"%s\" writing to local file (rc=%d)" ),strerror(errno),errno); } else { - ft_length += conv_length; - ft_update_length(NULL); + ft->ft_length += conv_length; + ft_update_length(ft); cut_ack(hSession); } } @@ -536,14 +551,15 @@ static void cut_ack(H3270 *hSession) */ static void cut_abort(H3270 *hSession, unsigned short reason, const char *fmt, ...) { - va_list args; + H3270FT * ft = get_ft_handle(hSession); + va_list args; - if(saved_errmsg) - lib3270_free(saved_errmsg); + if(ft->saved_errmsg) + lib3270_free(ft->saved_errmsg); /* Save the error message. */ va_start(args, fmt); - saved_errmsg = lib3270_vsprintf(fmt, args); + ft->saved_errmsg = lib3270_vsprintf(fmt, args); va_end(args); /* Send the abort sequence. */ @@ -556,15 +572,15 @@ static void cut_abort(H3270 *hSession, unsigned short reason, const char *fmt, . lib3270_pfkey(hSession,2); /* Update the in-progress pop-up. */ - ft_aborting(NULL); + ft_aborting(ft); } -/* +/** * Get the next translated character from the local file. - * Returns the character (in EBCDIC), or EOF. + * + * @return the character (in EBCDIC), or EOF. */ -static int -xlate_getc(void) +static int xlate_getc(H3270FT *ft) { int r; int c; @@ -573,25 +589,29 @@ xlate_getc(void) int nc; /* If there is a data buffered, return it. */ - if (xlate_buffered) { - r = xlate_buf[xlate_buf_ix]; - xlate_buf_ix++; - xlate_buffered--; + if (ft->xlate_buffered) + { + r = ft->xlate_buf[ft->xlate_buf_ix]; + ft->xlate_buf_ix++; + ft->xlate_buffered--; return r; } /* Get the next byte from the file. */ - c = fgetc(((H3270FT *) h3270.ft)->local_file); + c = fgetc(ft->local_file); if (c == EOF) return c; - ft_length++; + ft->ft_length++; /* Expand it. */ - if (ascii_flag && cr_flag && !ft_last_cr && c == '\n') { + if (ft->ascii_flag && ft->cr_flag && !ft->ft_last_cr && c == '\n') + { nc = download_convert((unsigned const char *)"\r", 1, cbuf); - } else { + } + else + { nc = 0; - ft_last_cr = (c == '\r'); + ft->ft_last_cr = (c == '\r') ? 1 : 0; } /* Convert it. */ @@ -600,12 +620,13 @@ xlate_getc(void) /* Return it and buffer what's left. */ r = cbuf[0]; - if (nc > 1) { + if (nc > 1) + { int i; for (i = 1; i < nc; i++) - xlate_buf[xlate_buffered++] = cbuf[i]; - xlate_buf_ix = 0; + ft->xlate_buf[ft->xlate_buffered++] = cbuf[i]; + ft->xlate_buf_ix = 0; } return r; } diff --git a/ft_dft.c b/ft_dft.c index d58fee9..a370dfa 100644 --- a/ft_dft.c +++ b/ft_dft.c @@ -59,34 +59,35 @@ // extern unsigned char aid; /* Macros. */ -#define OPEN_MSG "FT:MSG" /* Open request for message */ +#define OPEN_MSG "FT:MSG" /* Open request for message */ #define END_TRANSFER "TRANS03" /* Message for xfer complete */ #define DFT_MIN_BUF 256 #define DFT_MAX_BUF 32768 /* Typedefs. */ -struct data_buffer { - char sf_length[2]; /* SF length = 0x0023 */ - char sf_d0; /* 0xD0 */ - char sf_request_type[2]; /* request type */ - char compress_indic[2]; /* 0xc080 */ - char begin_data; /* 0x61 */ - char data_length[2]; /* Data Length in 3270 byte order+5 */ - char data[256]; /* The actual data */ +struct data_buffer +{ + char sf_length[2]; /**< SF length = 0x0023 */ + char sf_d0; /**< 0xD0 */ + char sf_request_type[2]; /**< request type */ + char compress_indic[2]; /**< 0xc080 */ + char begin_data; /**< 0x61 */ + char data_length[2]; /**< Data Length in 3270 byte order+5 */ + char data[256]; /**< The actual data */ }; /* Globals. */ // int dft_buffersize = 0; /* Buffer size (LIMIN, LIMOUT) */ /* Statics. */ -static Boolean message_flag = False; /* Open Request for msg received */ -static int dft_eof; -static unsigned long recnum; +// static Boolean message_flag = False; /* Open Request for msg received */ +// static int dft_eof; +// static unsigned long recnum; // static char *abort_string = CN; -static unsigned char *dft_savebuf = NULL; -static int dft_savebuf_len = 0; -static int dft_savebuf_max = 0; +// static unsigned char *dft_savebuf = NULL; +// static int dft_savebuf_len = 0; +// static int dft_savebuf_max = 0; static void dft_abort(H3270 *hSession, unsigned short code, const char *fmt, ...); @@ -158,10 +159,11 @@ void ft_dft_data(H3270 *hSession, unsigned char *data, int length unused) /* Process an Open request. */ static void dft_open_request(H3270 *hSession, unsigned short len, unsigned char *cp) { - char *name = "?"; - char namebuf[8]; - char *s; - unsigned short recsz = 0; + H3270FT * ft = get_ft_handle(hSession); + char * name = "?"; + char namebuf[8]; + char * s; + unsigned short recsz = 0; if (len == 0x23) { @@ -199,15 +201,15 @@ static void dft_open_request(H3270 *hSession, unsigned short len, unsigned char } if (!strcmp(namebuf, OPEN_MSG)) - message_flag = True; + ft->message_flag = 1; else { - message_flag = False; - ft_running(hSession->ft,False); + ft->message_flag = 0; + ft_running(ft,False); } - dft_eof = False; - recnum = 1; + ft->dft_eof = 0; + ft->recnum = 1; /* Acknowledge the Open. */ trace_ds(hSession,"> WriteStructuredField FileTransferData OpenAck\n"); @@ -231,10 +233,11 @@ static void dft_insert_request(H3270 *hSession) static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) { /* Received a data buffer, get the length and process it */ - int my_length; - unsigned char *cp; + H3270FT * ft = get_ft_handle(hSession); + int my_length; + unsigned char * cp; - if(!message_flag && lib3270_get_ft_state(hSession) == FT_ABORT_WAIT) + if(!ft->message_flag && lib3270_get_ft_state(hSession) == FT_ABORT_WAIT) { dft_abort(hSession,TR_DATA_INSERT, "%s", _("Transfer cancelled by user") ); return; @@ -248,13 +251,13 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) /* Adjust for 5 extra count */ my_length -= 5; - trace_ds(hSession," Data(rec=%lu) %d bytes\n", recnum, my_length); + trace_ds(hSession," Data(rec=%lu) %d bytes\n", ft->recnum, my_length); /* * First, check to see if we have message data or file data. * Message data will result in a popup. */ - if (message_flag) + if (ft->message_flag) { /* Data is from a message */ unsigned char *msgp; @@ -281,8 +284,8 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) else if (lib3270_get_ft_state(hSession) == FT_ABORT_SENT && ((H3270FT *) hSession->ft)->abort_string != CN) { lib3270_free(msgp); - ft_complete(hSession->ft,((H3270FT *) hSession->ft)->abort_string); - lib3270_free(((H3270FT *) hSession->ft)->abort_string); + ft_complete(ft,ft->abort_string); + lib3270_free(ft->abort_string); } else { @@ -293,17 +296,20 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) /* Write the data out to the file. */ int rv = 1; - if (ascii_flag && remap_flag) { + if (ft->ascii_flag && ft->remap_flag) + { /* Filter. */ unsigned char *s = (unsigned char *)data_bufr->data; unsigned len = my_length; - while (len--) { + while (len--) + { *s = ft2asc[*s]; s++; } } - if (ascii_flag && cr_flag) { + if (ft->ascii_flag && ft->cr_flag) + { char *s = (char *)data_bufr->data; unsigned len = my_length; @@ -316,7 +322,7 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) rv = fwrite(s, l, (size_t)1,((H3270FT *) h3270.ft)->local_file); if (rv == 0) break; - ft_length += l; + ft->ft_length += l; } if (l < len) l++; @@ -325,7 +331,7 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) } } else { rv = fwrite((char *)data_bufr->data, my_length,(size_t)1, ((H3270FT *) h3270.ft)->local_file); - ft_length += my_length; + ft->ft_length += my_length; } if (!rv) { @@ -334,11 +340,11 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) } /* Add up amount transferred. */ - ft_update_length((H3270FT *) hSession->ft); + ft_update_length(ft); } /* Send an acknowledgement frame back. */ - trace_ds(hSession,"> WriteStructuredField FileTransferData DataAck(rec=%lu)\n", recnum); + trace_ds(hSession,"> WriteStructuredField FileTransferData DataAck(rec=%lu)\n", ft->recnum); hSession->obptr = hSession->obuf; space3270out(hSession,12); *hSession->obptr++ = AID_SF; @@ -346,8 +352,8 @@ static void dft_data_insert(H3270 *hSession, struct data_buffer *data_bufr) *hSession->obptr++ = SF_TRANSFER_DATA; SET16(hSession->obptr, TR_NORMAL_REPLY); SET16(hSession->obptr, TR_RECNUM_HDR); - SET32(hSession->obptr, recnum); - recnum++; + SET32(hSession->obptr, ft->recnum); + ft->recnum++; net_output(hSession); } @@ -361,14 +367,15 @@ static void dft_set_cur_req(H3270 *hSession) /* Process a Get request. */ static void dft_get_request(H3270 *hSession) { - int numbytes; - size_t numread; - size_t total_read = 0; - unsigned char *bufptr; + int numbytes; + size_t numread; + size_t total_read = 0; + unsigned char * bufptr; + H3270FT * ft = get_ft_handle(hSession); trace_ds(hSession," Get\n"); - if (!message_flag && lib3270_get_ft_state(hSession) == FT_ABORT_WAIT) + if (!ft->message_flag && lib3270_get_ft_state(hSession) == FT_ABORT_WAIT) { dft_abort(hSession,TR_GET_REQ, _( "Transfer cancelled by user" ) ); return; @@ -376,43 +383,46 @@ static void dft_get_request(H3270 *hSession) /* Read a buffer's worth. */ set_dft_buffersize(hSession); - space3270out(hSession,h3270.dft_buffersize); + space3270out(hSession,hSession->dft_buffersize); numbytes = h3270.dft_buffersize - 27; /* always read 5 bytes less than we're allowed */ bufptr = h3270.obuf + 17; - while (!dft_eof && numbytes) { - if (ascii_flag && cr_flag) { + while (!ft->dft_eof && numbytes) + { + if (ft->ascii_flag && ft->cr_flag) + { int c; /* Read one byte and do CR/LF translation. */ - c = fgetc(((H3270FT *) h3270.ft)->local_file); + c = fgetc(ft->local_file); if (c == EOF) { break; } - if (!ft_last_cr && c == '\n') { + if (!ft->ft_last_cr && c == '\n') + { if (numbytes < 2) { /* * Not enough room to expand NL to * CR/LF. */ - ungetc(c, ((H3270FT *) h3270.ft)->local_file); + ungetc(c, ft->local_file); break; } *bufptr++ = '\r'; numbytes--; total_read++; } - ft_last_cr = (c == '\r'); - *bufptr++ = remap_flag? asc2ft[c]: c; + ft->ft_last_cr = (c == '\r') ? 1 : 0; + *bufptr++ = ft->remap_flag? asc2ft[c]: c; numbytes--; total_read++; } else { /* Binary read. */ - numread = fread(bufptr, 1, numbytes, ((H3270FT *) h3270.ft)->local_file); + numread = fread(bufptr, 1, numbytes, ft->local_file); if (numread <= 0) { break; } - if (ascii_flag && remap_flag) { + if (ft->ascii_flag && ft->remap_flag) { unsigned char *s = bufptr; int i = numread; @@ -426,7 +436,8 @@ static void dft_get_request(H3270 *hSession) numbytes -= numread; total_read += numread; } - if (feof(((H3270FT *) h3270.ft)->local_file) || ferror(((H3270FT *) h3270.ft)->local_file)) { + if (feof(ft->local_file) || ferror(ft->local_file)) + { break; } } @@ -439,55 +450,57 @@ static void dft_get_request(H3270 *hSession) } /* Set up SF header for Data or EOF. */ - h3270.obptr = h3270.obuf; - *h3270.obptr++ = AID_SF; - h3270.obptr += 2; /* skip SF length for now */ - *h3270.obptr++ = SF_TRANSFER_DATA; - - if (total_read) { - trace_ds(hSession,"> WriteStructuredField FileTransferData Data(rec=%lu) %d bytes\n",(unsigned long) recnum, (int) total_read); - SET16(h3270.obptr, TR_GET_REPLY); - SET16(h3270.obptr, TR_RECNUM_HDR); - SET32(h3270.obptr, recnum); - recnum++; - SET16(h3270.obptr, TR_NOT_COMPRESSED); - *h3270.obptr++ = TR_BEGIN_DATA; - SET16(h3270.obptr, total_read + 5); - h3270.obptr += total_read; - - ft_length += total_read; - - if (feof(((H3270FT *) h3270.ft)->local_file)) + hSession->obptr = hSession->obuf; + *hSession->obptr++ = AID_SF; + hSession->obptr += 2; /* skip SF length for now */ + *hSession->obptr++ = SF_TRANSFER_DATA; + + if (total_read) + { + trace_ds(hSession,"> WriteStructuredField FileTransferData Data(rec=%lu) %d bytes\n",(unsigned long) ft->recnum, (int) total_read); + SET16(hSession->obptr, TR_GET_REPLY); + SET16(hSession->obptr, TR_RECNUM_HDR); + SET32(hSession->obptr, ft->recnum); + ft->recnum++; + SET16(hSession->obptr, TR_NOT_COMPRESSED); + *hSession->obptr++ = TR_BEGIN_DATA; + SET16(hSession->obptr, total_read + 5); + hSession->obptr += total_read; + + ft->ft_length += total_read; + + if (feof(ft->local_file)) { - dft_eof = True; + ft->dft_eof = 1; } } else { trace_ds(hSession,"> WriteStructuredField FileTransferData EOF\n"); - *h3270.obptr++ = HIGH8(TR_GET_REQ); - *h3270.obptr++ = TR_ERROR_REPLY; - SET16(h3270.obptr, TR_ERROR_HDR); - SET16(h3270.obptr, TR_ERR_EOF); + *hSession->obptr++ = HIGH8(TR_GET_REQ); + *hSession->obptr++ = TR_ERROR_REPLY; + SET16(hSession->obptr, TR_ERROR_HDR); + SET16(hSession->obptr, TR_ERR_EOF); - dft_eof = True; + ft->dft_eof = 1; } /* Set the SF length. */ - bufptr = h3270.obuf + 1; - SET16(bufptr, h3270.obptr - (h3270.obuf + 1)); + bufptr = hSession->obuf + 1; + SET16(bufptr, hSession->obptr - (hSession->obuf + 1)); /* Save the data. */ - dft_savebuf_len = h3270.obptr - h3270.obuf; - if (dft_savebuf_len > dft_savebuf_max) { - dft_savebuf_max = dft_savebuf_len; - Replace(dft_savebuf, (unsigned char *)lib3270_malloc(dft_savebuf_max)); + ft->dft_savebuf_len = hSession->obptr - hSession->obuf; + if (ft->dft_savebuf_len > ft->dft_savebuf_max) + { + ft->dft_savebuf_max = ft->dft_savebuf_len; + Replace(ft->dft_savebuf, (unsigned char *)lib3270_malloc(ft->dft_savebuf_max)); } - (void) memcpy(dft_savebuf, h3270.obuf, dft_savebuf_len); - h3270.aid = AID_SF; + (void) memcpy(ft->dft_savebuf, hSession->obuf, ft->dft_savebuf_len); + hSession->aid = AID_SF; /* Write the data. */ net_output(hSession); - ft_update_length((H3270FT *) h3270.ft); + ft_update_length(get_ft_handle(hSession)); } /* Process a Close request. */ @@ -555,13 +568,15 @@ filter_len(char *s, register int len) */ void dft_read_modified(H3270 *hSession) { - if (dft_savebuf_len) + H3270FT *ft = get_ft_handle(hSession); + + if(ft->dft_savebuf_len) { trace_ds(hSession,"> WriteStructuredField FileTransferData\n"); hSession->obptr = hSession->obuf; - space3270out(hSession,dft_savebuf_len); - memcpy(hSession->obptr, dft_savebuf, dft_savebuf_len); - hSession->obptr += dft_savebuf_len; + space3270out(hSession,ft->dft_savebuf_len); + memcpy(hSession->obptr, ft->dft_savebuf, ft->dft_savebuf_len); + hSession->obptr += ft->dft_savebuf_len; net_output(hSession); } } diff --git a/ftc.h b/ftc.h index 9ae6cb9..b7442bc 100644 --- a/ftc.h +++ b/ftc.h @@ -22,22 +22,24 @@ #include - #define CHECK_FT_HANDLE(x) if(!x) x = ftsession; +// #define CHECK_FT_HANDLE(x) if(!x) x = ftsession; LIB3270_INTERNAL Boolean ascii_flag; LIB3270_INTERNAL Boolean cr_flag; LIB3270_INTERNAL unsigned long ft_length; - LIB3270_INTERNAL H3270FT * ftsession; +// LIB3270_INTERNAL H3270FT * ftsession; - LIB3270_INTERNAL Boolean ft_last_cr; - LIB3270_INTERNAL Boolean remap_flag; +// LIB3270_INTERNAL Boolean ft_last_cr; +// LIB3270_INTERNAL Boolean remap_flag; - LIB3270_INTERNAL void ft_init(H3270 *session); + LIB3270_INTERNAL H3270FT * get_ft_handle(H3270 *hSession); - LIB3270_INTERNAL void ft_aborting(H3270FT *h); - LIB3270_INTERNAL void ft_complete(H3270FT *h, const char *errmsg); - LIB3270_INTERNAL void ft_running(H3270FT *h, Boolean is_cut); - LIB3270_INTERNAL void ft_update_length(H3270FT *h); + LIB3270_INTERNAL void ft_init(H3270 *hSession); + + LIB3270_INTERNAL void ft_aborting(H3270FT *h); + LIB3270_INTERNAL void ft_complete(H3270FT *h, const char *errmsg); + LIB3270_INTERNAL void ft_running(H3270FT *h, Boolean is_cut); + LIB3270_INTERNAL void ft_update_length(H3270FT *h); #endif /*]*/ -- libgit2 0.21.2