From 4f6698343ecec5634cc12966a72350d554f9aaf4 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Tue, 2 Apr 2013 18:21:03 +0000 Subject: [PATCH] Iniciando ajustes na extensão rexx para usar carga dinâmica igual à biblioteca de hllapi --- src/plugins/rx3270/pluginmain.cc | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/plugins/rx3270/rx3270.h | 56 +++++++++++++++++++++++++++++++++++++++++--------------- src/plugins/rx3270/rxapimain.cc | 63 ++++++++++++++++++++++++++++++++++----------------------------- src/plugins/rx3270/text.cc | 50 ++++++++++++++++---------------------------------- src/plugins/rx3270/typed_routines.cc | 169 +++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------- 5 files changed, 263 insertions(+), 210 deletions(-) diff --git a/src/plugins/rx3270/pluginmain.cc b/src/plugins/rx3270/pluginmain.cc index a47f2a4..8ddb512 100644 --- a/src/plugins/rx3270/pluginmain.cc +++ b/src/plugins/rx3270/pluginmain.cc @@ -28,18 +28,149 @@ */ #include + #include #include "rx3270.h" +/*--[ Plugin session object ]--------------------------------------------------------------------------------*/ + + class plugin : public rx3270 + { + public: + plugin(H3270 *hSession); + + const char * get_version(void); + LIB3270_CSTATE get_cstate(void); + int disconnect(void); + int connect(const char *uri, bool wait = true); + int is_connected(void); + int is_ready(void); + + int iterate(void); + int wait(int seconds); + int wait_for_ready(int seconds); + + char * get_text_at(int row, int col, size_t sz); + int cmp_text_at(int row, int col, const char *text); + int set_text_at(int row, int col, const char *str); + + int set_cursor_position(int row, int col); + + int enter(void); + int pfkey(int key); + int pakey(int key); + + private: + H3270 *hSession; + + }; + + static plugin * session = NULL; + /*--[ Implement ]------------------------------------------------------------------------------------*/ LIB3270_EXPORT int pw3270_plugin_init(GtkWidget *window) { - rx3270_set_mode(RX3270_MODE_PLUGIN); + session = new plugin(lib3270_get_default_session_handle()); return 0; } LIB3270_EXPORT int pw3270_plugin_deinit(GtkWidget *window) { - rx3270_set_mode(RX3270_MODE_UNDEFINED); + if(session) + { + delete session; + session = NULL; + } + return 0; + } + + plugin::plugin(H3270 *hSession) : rx3270() + { + this->hSession = hSession; + } + + const char * plugin::get_version(void) + { + return lib3270_get_version(); + } + + LIB3270_CSTATE plugin::get_cstate(void) + { + return lib3270_get_connection_state(hSession); + } + + int plugin::disconnect(void) + { + lib3270_disconnect(hSession); + return 0; + } + + int plugin::connect(const char *uri, bool wait) + { + return lib3270_connect(hSession,uri,wait); + } + + int plugin::is_connected(void) + { + return lib3270_is_connected(hSession) ? 1 : 0; + } + + int plugin::iterate(void) + { + if(!lib3270_is_connected(hSession)) + return ENOTCONN; + + lib3270_main_iterate(hSession,1); + return 0; } + + int plugin::wait(int seconds) + { + return lib3270_wait(hSession,seconds); + } + + int plugin::enter(void) + { + return lib3270_enter(hSession); + } + + int plugin::pfkey(int key) + { + return lib3270_pfkey(hSession,key); + } + + int plugin::pakey(int key) + { + return lib3270_pakey(hSession,key); + } + + int plugin::wait_for_ready(int seconds) + { + return lib3270_wait_for_ready(hSession,seconds); + } + + char * plugin::get_text_at(int row, int col, size_t sz) + { + return lib3270_get_text_at(hSession,row,col,(int) sz); + } + + int plugin::cmp_text_at(int row, int col, const char *text) + { + return lib3270_cmp_text_at(hSession,row,col,text); + } + + int plugin::set_text_at(int row, int col, const char *str) + { + return lib3270_set_text_at(hSession,row,col,(const unsigned char *) str); + } + + int plugin::is_ready(void) + { + return lib3270_is_ready(hSession); + } + + int plugin::set_cursor_position(int row, int col) + { + return lib3270_set_cursor_position(hSession,row,col); + } diff --git a/src/plugins/rx3270/rx3270.h b/src/plugins/rx3270/rx3270.h index ba2eaa6..82ea259 100644 --- a/src/plugins/rx3270/rx3270.h +++ b/src/plugins/rx3270/rx3270.h @@ -67,32 +67,58 @@ REXX_TYPED_ROUTINE_PROTOTYPE(rx3270WaitForStringAt); REXX_TYPED_ROUTINE_PROTOTYPE(rx3270GetStringAt); REXX_TYPED_ROUTINE_PROTOTYPE(rx3270IsTerminalReady); - REXX_TYPED_ROUTINE_PROTOTYPE(rx3270ReadScreen); REXX_TYPED_ROUTINE_PROTOTYPE(rx3270queryStringAt); REXX_TYPED_ROUTINE_PROTOTYPE(rx3270SetStringAt); /*---[ Globals ]---------------------------------------------------------------------------------------------*/ - enum rx3270mode - { - RX3270_MODE_STANDALONE, /**< Running outside pw3270's main process */ - RX3270_MODE_PLUGIN, /**< Running inside pw3270's main process */ - - RX3270_MODE_UNDEFINED - }; +/*--[ 3270 Session ]-----------------------------------------------------------------------------------------*/ - #define RX3270SESSION lib3270_get_default_session_handle() +#if defined (HAVE_GNUC_VISIBILITY) + class __attribute__((visibility("default"))) rx3270 +#else + #error NOT_IMPLEMENTED +#endif + { + protected: #ifdef HAVE_ICONV - extern iconv_t outputConv; - extern iconv_t inputConv; + iconv_t conv2Local; + iconv_t conv2Host; #endif -/*--[ Prototipes ]-------------------------------------------------------------------------------------------*/ + public: + + rx3270(); + virtual ~rx3270(); + + static rx3270 * get_default(void); - char * get_contents(H3270 *hSession, int start, int sz); - char * set_contents(H3270 *hSession, const char *text); + char * get_3270_string(const char *str); + char * get_local_string(const char *str); - LIB3270_EXPORT void rx3270_set_mode(enum rx3270mode mode); + virtual const char * get_version(void) = 0; + virtual LIB3270_CSTATE get_cstate(void) = 0; + + virtual int connect(const char *uri, bool wait = true) = 0; + virtual int disconnect(void) = 0; + virtual int is_connected(void) = 0; + virtual int is_ready(void) = 0; + virtual int iterate(void) = 0; + virtual int wait(int seconds) = 0; + virtual int wait_for_ready(int seconds) = 0; + virtual int set_cursor_position(int row, int col) = 0; + + virtual int enter(void) = 0; + virtual int pfkey(int key) = 0; + virtual int pakey(int key) = 0; + + virtual char * get_text_at(int row, int col, size_t sz) = 0; + virtual int cmp_text_at(int row, int col, const char *text) = 0; + virtual int set_text_at(int row, int col, const char *str) = 0; + + + + }; #endif // RX3270_H_INCLUDED diff --git a/src/plugins/rx3270/rxapimain.cc b/src/plugins/rx3270/rxapimain.cc index 6d1a56c..2fc8649 100644 --- a/src/plugins/rx3270/rxapimain.cc +++ b/src/plugins/rx3270/rxapimain.cc @@ -38,10 +38,6 @@ #include "rx3270.h" #include -#ifdef HAVE_ICONV - #include -#endif - #include #if defined WIN32 @@ -63,7 +59,7 @@ LIB3270_EXPORT RexxPackageEntry rx3270_package_entry; - static enum rx3270mode active_mode = RX3270_MODE_UNDEFINED; + static rx3270 * hSession = NULL; /*--[ Implement ]------------------------------------------------------------------------------------*/ @@ -86,36 +82,15 @@ BOOL WINAPI DllMain(HANDLE hinst, DWORD dwcallpurpose, LPVOID lpvResvd) } #endif -void rx3270_set_mode(enum rx3270mode mode) -{ - active_mode = mode; -} - int librx3270_loaded(void) { - active_mode = RX3270_MODE_STANDALONE; - -#ifdef HAVE_ICONV - outputConv = iconv_open(REXX_DEFAULT_CHARSET, lib3270_get_default_charset()); - inputConv = iconv_open(lib3270_get_default_charset(), REXX_DEFAULT_CHARSET); -#endif - return 0; } int librx3270_unloaded(void) { - active_mode = RX3270_MODE_UNDEFINED; - -#ifdef HAVE_ICONV - - if(outputConv != (iconv_t) (-1)) - iconv_close(outputConv); - - if(inputConv != (iconv_t) (-1)) - iconv_close(inputConv); -#endif - + if(hSession) + delete hSession; return 0; } @@ -136,7 +111,6 @@ RexxRoutineEntry rx3270_functions[] = REXX_TYPED_ROUTINE(rx3270WaitForStringAt, rx3270WaitForStringAt), REXX_TYPED_ROUTINE(rx3270GetStringAt, rx3270GetStringAt), REXX_TYPED_ROUTINE(rx3270IsTerminalReady, rx3270IsTerminalReady), - REXX_TYPED_ROUTINE(rx3270ReadScreen, rx3270ReadScreen), REXX_TYPED_ROUTINE(rx3270queryStringAt, rx3270queryStringAt), REXX_TYPED_ROUTINE(rx3270SetStringAt, rx3270SetStringAt), @@ -168,4 +142,35 @@ LIB3270_EXPORT RexxPackageEntry * RexxEntry RexxGetPackage(void) END_EXTERN_C() +rx3270 * rx3270::get_default(void) +{ + return hSession; +} + +rx3270::rx3270() +{ +#ifdef HAVE_ICONV + this->conv2Local = iconv_open(REXX_DEFAULT_CHARSET, "ISO-8859-1"); + this->conv2Host = iconv_open("ISO-8859-1",REXX_DEFAULT_CHARSET); +#endif + + if(!hSession) + hSession = this; +} + +rx3270::~rx3270() +{ +#ifdef HAVE_ICONV + + if(conv2Local != (iconv_t) (-1)) + iconv_close(conv2Local); + + if(conv2Host != (iconv_t) (-1)) + iconv_close(conv2Host); +#endif + + + if(hSession == this) + hSession = NULL; +} diff --git a/src/plugins/rx3270/text.cc b/src/plugins/rx3270/text.cc index 711978d..f2ad171 100644 --- a/src/plugins/rx3270/text.cc +++ b/src/plugins/rx3270/text.cc @@ -32,27 +32,12 @@ #include - -/*--[ Globals ]--------------------------------------------------------------------------------------*/ - -#ifdef HAVE_ICONV - iconv_t outputConv = (iconv_t) (-1); - iconv_t inputConv = (iconv_t) (-1); -#endif - /*--[ Implement ]------------------------------------------------------------------------------------*/ -char * get_contents(H3270 *hSession, int start, int sz) +char * rx3270::get_3270_string(const char *str) { - unsigned char str[sz+1]; - unsigned short attr[sz+1]; - - if(!lib3270_get_contents(hSession,start,start+sz,str,attr)) - return NULL; - #ifdef HAVE_ICONV - // Convert received string to rexx encoding - if(outputConv != (iconv_t)(-1)) + if(conv2Host != (iconv_t)(-1)) { size_t in = strlen((char *) str); size_t out = (in << 1); @@ -62,9 +47,9 @@ char * get_contents(H3270 *hSession, int start, int sz) memset(ptr=buffer,0,out); - iconv(outputConv,NULL,NULL,NULL,NULL); // Reset state + iconv(conv2Host,NULL,NULL,NULL,NULL); // Reset state - if(iconv(outputConv,(char **) &str,&in,&ptr,&out) == ((size_t) -1)) + if(iconv(conv2Host,(char **) &str,&in,&ptr,&out) == ((size_t) -1)) ret = strdup((char *) str); else ret = strdup(buffer); @@ -73,30 +58,28 @@ char * get_contents(H3270 *hSession, int start, int sz) return ret; } - #endif // HAVE_ICONV - return strdup((char *) str); + return strdup(str); } -char * set_contents(H3270 *hSession, const char *str) +char * rx3270::get_local_string(const char *str) { #ifdef HAVE_ICONV - // Convert received string to 3270 encoding - if(inputConv != (iconv_t)(-1)) + if(conv2Local != (iconv_t)(-1)) { - size_t in = strlen(str); - size_t out = (in << 1); - char * ptr; - char * buffer = (char *) malloc(out); - char * ret; + size_t in = strlen((char *) str); + size_t out = (in << 1); + char *ptr; + char *buffer = (char *) malloc(out); + char *ret; memset(ptr=buffer,0,out); - iconv(inputConv,NULL,NULL,NULL,NULL); // Reset state + iconv(conv2Local,NULL,NULL,NULL,NULL); // Reset state - if(iconv(inputConv,(char **) &str,&in,&ptr,&out) == ((size_t) -1)) - ret = strdup(str); + if(iconv(conv2Local,(char **) &str,&in,&ptr,&out) == ((size_t) -1)) + ret = strdup((char *) str); else ret = strdup(buffer); @@ -106,8 +89,7 @@ char * set_contents(H3270 *hSession, const char *str) } #endif // HAVE_ICONV - return strdup((char *) str); - + return strdup(str); } diff --git a/src/plugins/rx3270/typed_routines.cc b/src/plugins/rx3270/typed_routines.cc index dc1ae2f..1e8d2d1 100644 --- a/src/plugins/rx3270/typed_routines.cc +++ b/src/plugins/rx3270/typed_routines.cc @@ -31,14 +31,13 @@ #include #include "rx3270.h" - #include /*--[ Implement ]------------------------------------------------------------------------------------*/ RexxRoutine0(CSTRING, rx3270version) { - return lib3270_get_version(); + return rx3270::get_default()->get_version(); } RexxRoutine0(CSTRING, rx3270QueryCState) @@ -64,9 +63,7 @@ RexxRoutine0(CSTRING, rx3270QueryCState) }; size_t f; - LIB3270_CSTATE state; - - state = lib3270_get_connection_state(RX3270SESSION); + LIB3270_CSTATE state = rx3270::get_default()->get_cstate(); for(f=0;f < (sizeof(xlat_state)/sizeof(struct _xlat_state)); f++) { @@ -79,112 +76,84 @@ RexxRoutine0(CSTRING, rx3270QueryCState) RexxRoutine0(int, rx3270Disconnect) { - lib3270_disconnect(RX3270SESSION); - return 0; + return rx3270::get_default()->disconnect(); } RexxRoutine2(int, rx3270Connect, CSTRING, hostname, int, wait) { - return lib3270_connect(RX3270SESSION, hostname, wait); + return rx3270::get_default()->connect(hostname,wait); } RexxRoutine0(int, rx3270isConnected) { - return lib3270_is_connected(RX3270SESSION) ? 1 : 0; + return rx3270::get_default()->is_connected(); } RexxRoutine0(int, rx3270WaitForEvents) { - H3270 * hSession = RX3270SESSION; - - if(!lib3270_is_connected(hSession)) - return ENOTCONN; - - lib3270_main_iterate(hSession,1); - - return 0; + return rx3270::get_default()->iterate(); } RexxRoutine1(int, rx3270Sleep, int, seconds) { - return lib3270_wait(RX3270SESSION,seconds); + return rx3270::get_default()->wait(seconds); } RexxRoutine0(int, rx3270SendENTERKey) { - return lib3270_enter(RX3270SESSION); + return rx3270::get_default()->enter(); } RexxRoutine1(int, rx3270SendPFKey, int, key) { - return lib3270_pfkey(RX3270SESSION,key); + return rx3270::get_default()->pfkey(key); } RexxRoutine1(int, rx3270SendPAKey, int, key) { - return lib3270_pakey(RX3270SESSION,key); + return rx3270::get_default()->pakey(key); } RexxRoutine1(int, rx3270WaitForTerminalReady, int, seconds) { - return lib3270_wait_for_ready(RX3270SESSION,seconds); + return rx3270::get_default()->wait_for_ready(seconds); } RexxRoutine4(int, rx3270WaitForStringAt, int, row, int, col, CSTRING, key, int, timeout) { - H3270 * hSession = RX3270SESSION; + rx3270 * session = rx3270::get_default(); time_t end = time(0) + timeout; - int sz = strlen(key); - int rows; - int cols; - int start; - - lib3270_get_screen_size(hSession,&rows,&cols); - - if(row < 0 || row > rows || col < 0 || col > cols) - return EINVAL; - - start = ((row) * cols) + col; + char * text = session->get_3270_string(key); while(time(0) < end) { - if(!lib3270_is_connected(hSession)) + if(!session->is_connected()) { return ENOTCONN; } - else if(lib3270_is_ready(hSession)) + else if( !(session->wait_for_ready(1) || session->cmp_text_at(row,col,text)) ) { - char *buffer = get_contents(hSession, start, start+sz); - if(buffer) - { - int rc = strncasecmp((const char *) buffer,key,sz); - free(buffer); - if(rc == 0) - return 0; - } + free(text); + return 0; } - - lib3270_main_iterate(hSession,1); - } + free(text); + return ETIMEDOUT; } RexxRoutine3(RexxStringObject, rx3270GetStringAt, int, row, int, col, int, sz) { - H3270 * hSession = RX3270SESSION; - int rows; - int cols; - char * text; - - lib3270_get_screen_size(hSession,&rows,&cols); + rx3270 * session = rx3270::get_default(); + char * str = session->get_text_at(row,col,sz); - text = get_contents(hSession, ((row) * cols) + col, sz); - if(text) + if(str) { - RexxStringObject ret = context->String((CSTRING) text); + char * text = session->get_local_string(str); + RexxStringObject ret = context->String((CSTRING) text); + free(str); free(text); return ret; } @@ -194,102 +163,42 @@ RexxRoutine3(RexxStringObject, rx3270GetStringAt, int, row, int, col, int, sz) RexxRoutine0(int, rx3270IsTerminalReady) { - return lib3270_is_ready(RX3270SESSION); -} - -RexxRoutine3(RexxStringObject, rx3270ReadScreen, OPTIONAL_int, row, OPTIONAL_int, col, OPTIONAL_int, sz) -{ - H3270 * hSession = RX3270SESSION; - int rows; - int cols; - char * text; - int start; - - lib3270_get_screen_size(hSession,&rows,&cols); - - start = (row * cols) + col; - - if(sz == 0) - sz = (rows*cols) - start; - - text = get_contents(hSession, start, sz); - if(text) - { - RexxStringObject ret = context->String((CSTRING) text); - free(text); - return ret; - } - - return context->String(""); + return rx3270::get_default()->is_ready(); } RexxRoutine3(int, rx3270queryStringAt, int, row, int, col, CSTRING, key) { - H3270 * hSession = RX3270SESSION; - int rows; - int cols; - char * text; - size_t sz = strlen(key); - - lib3270_get_screen_size(hSession,&rows,&cols); + int rc = 0; + rx3270 * session = rx3270::get_default(); + char * str = session->get_text_at(row,col,strlen(key)); - text = get_contents(hSession, (row * cols) + col, sz); - if(text) + if(str) { - int rc = strncasecmp(text,key,sz); + char * text = session->get_3270_string(key); + rc = strcasecmp(str,text); free(text); - return rc; } - return 0; + free(str); + + return rc; } RexxRoutine2(int, rx3270SetCursorPosition, int, row, int, col) { - return lib3270_set_cursor_position(RX3270SESSION,row,col); + return rx3270::get_default()->set_cursor_position(row,col); } RexxRoutine3(int, rx3270SetStringAt, int, row, int, col, CSTRING, text) { + rx3270 * session = rx3270::get_default(); + char * str = session->get_3270_string(text); int rc; - H3270 * hSession = RX3270SESSION; - char * str = set_contents(hSession,text); - if(str) - rc = lib3270_set_string_at(hSession,row,col,(const unsigned char *) str); - else - rc = -1; + rc = session->set_text_at(row,col,str); free(str); return rc; } -/* -::method RunMode -return rx3270QueryRunMode() - -::method 'encoding=' - use arg ptr -return rx3270SetCharset(ptr) - -::method sendfile - use arg from, tostrncasecmp - - status = rx3270BeginFileSend(from,to) - if status <> 0 - then return status - -return rx3270WaitForFTComplete() - -::method recvfile - use arg from, to - - status = rx3270BeginFileRecv(from,to) - if status <> 0 - then return status - -return rx3270WaitForFTComplete() -*/ - - -- libgit2 0.21.2