From 52892e7f008b05d18571bfb055b8d957728b6d84 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Mon, 26 Aug 2019 11:24:09 -0300 Subject: [PATCH] Updating HLLAPI support library. --- src/core/calls.cc | 54 +++++++++++++++++++++++------------------------------- src/core/get.cc | 30 ------------------------------ src/core/hllapi.cc | 54 +++++++++++++++++------------------------------------- src/core/private.h | 1 + src/core/set.cc | 3 ++- src/core/tools.cc | 23 +++++++++++++++++++++++ src/include/lib3270/hllapi.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/testprogram/testprogram.cc | 9 +++++++++ 8 files changed, 182 insertions(+), 100 deletions(-) diff --git a/src/core/calls.cc b/src/core/calls.cc index 34b7e43..2a44a09 100644 --- a/src/core/calls.cc +++ b/src/core/calls.cc @@ -128,23 +128,15 @@ try { - TN3270::Host & session = getSession(); - - if(!session.isConnected()) { - hllapi_lasterror = strerror(ENOTCONN); - return HLLAPI_STATUS_DISCONNECTED; - } - - return (DWORD) session.getProgramMessage(); + return (DWORD) getSession().getProgramMessage(); } catch(std::exception &e) { hllapi_lasterror = e.what(); - return HLLAPI_STATUS_SYSTEM_ERROR; } - return HLLAPI_STATUS_SUCCESS; + return -1; } @@ -251,14 +243,7 @@ try { - TN3270::Host &host = getSession(); - - if(!host.isConnected()) - return HLLAPI_STATUS_DISCONNECTED; - - string contents = host.toString(row, col, strlen(text), 0); - - return strncmp(contents.c_str(),text,strlen(text)); + return getSession().compare((unsigned int) row, (unsigned int) col, text); } catch(std::exception &e) { @@ -270,27 +255,34 @@ } - HLLAPI_API_CALL hllapi_find_text(LPSTR text) { + HLLAPI_API_CALL hllapi_cmp_text_at_address(WORD addr, LPSTR text) { try { - TN3270::Host &host = getSession(); + return getSession().compare((int) addr, text); - if(!host.isConnected()) { - hllapi_lasterror = "Not connected"; - return -1; - } + } catch(std::exception &e) { - string contents = host.toString(0,-1,'\0'); + hllapi_lasterror = e.what(); - size_t pos = contents.find(text); + } - if(pos == string::npos) { - hllapi_lasterror = "Not found"; - return -1; - } + return HLLAPI_STATUS_SYSTEM_ERROR; + + } + + + HLLAPI_API_CALL hllapi_find_text(const LPSTR text) { + + try { + + if(text && *text) { + + size_t pos = getSession().find(text); + if(pos != string::npos) + return pos; - return pos; + } } catch(std::exception &e) { diff --git a/src/core/get.cc b/src/core/get.cc index f6ee74d..9675804 100644 --- a/src/core/get.cc +++ b/src/core/get.cc @@ -167,36 +167,6 @@ }); - /* - try { - - TN3270::Host &host = getSession(); - - if(!host.isConnected()) - return HLLAPI_STATUS_DISCONNECTED; - - if(!(buffer && *buffer)) - return HLLAPI_STATUS_BAD_PARAMETER; - - - if(len == 0) - return HLLAPI_STATUS_BAD_PARAMETER; - - string luname = host.getLUName(); - memset(buffer,' ',len); - strncpy((char *) buffer, luname.c_str(), std::min((size_t) len,luname.size())); - - } catch(std::exception &e) { - - hllapi_lasterror = e.what(); - return HLLAPI_STATUS_SYSTEM_ERROR; - - } - - return HLLAPI_STATUS_SUCCESS; - - */ - } diff --git a/src/core/hllapi.cc b/src/core/hllapi.cc index 280fee0..0510818 100644 --- a/src/core/hllapi.cc +++ b/src/core/hllapi.cc @@ -245,17 +245,17 @@ static int search_ps(char *buffer, unsigned short *length, unsigned short *ps) { try { - TN3270::Host &host = getSession(); + size_t pos = string::npos; - if(!host.isConnected()) - return HLLAPI_STATUS_DISCONNECTED; + if(length > 0) { - string contents = host.toString(0,-1, '\0'); + pos = getSession().find(string((const char *) buffer,(size_t) length).c_str()); - if( ((size_t) *ps) >= contents.size()) - return HLLAPI_STATUS_BAD_POSITION; + } else { - size_t pos = contents.find(buffer, ((size_t) *ps)); + pos = getSession().find(buffer); + + } if(pos == string::npos) { *ps = 0; @@ -266,6 +266,10 @@ static int search_ps(char *buffer, unsigned short *length, unsigned short *ps) { return HLLAPI_STATUS_SUCCESS; + } catch(const std::system_error &e) { + + return hllapi_translate_error(e); + } catch(std::exception &e) { hllapi_lasterror = e.what(); @@ -323,6 +327,7 @@ static int copy_ps(char *buffer, unsigned short *length, unsigned short *rc) { return HLLAPI_STATUS_SYSTEM_ERROR; /* + size_t szBuffer = strlen(buffer); char * text; @@ -339,9 +344,11 @@ static int copy_ps(char *buffer, unsigned short *length, unsigned short *rc) { hllapi_free(text); return hllapi_get_state(); - */ return HLLAPI_STATUS_SYSTEM_ERROR; + + */ + } static int wait_system(char *buffer, unsigned short *length, unsigned short *rc) { @@ -388,35 +395,8 @@ static int copy_str_to_ps(char *text, unsigned short *length, unsigned short *ps // HLLAPI_STATUS_SYSTEM_ERROR 9 A system error was encountered. // // - size_t szText = strlen(text); - - if(*length < szText) - szText = *length; - - if(!szText) - return HLLAPI_STATUS_BAD_PARAMETER; - - switch(hllapi_get_message_id()) - { - case LIB3270_MESSAGE_NONE: - break; - - case LIB3270_MESSAGE_DISCONNECTED: - return HLLAPI_STATUS_DISCONNECTED; - - case LIB3270_MESSAGE_MINUS: - case LIB3270_MESSAGE_PROTECTED: - case LIB3270_MESSAGE_NUMERIC: - case LIB3270_MESSAGE_OVERFLOW: - case LIB3270_MESSAGE_INHIBIT: - case LIB3270_MESSAGE_KYBDLOCK: - return HLLAPI_STATUS_KEYBOARD_LOCKED; - - default: - return HLLAPI_STATUS_SYSTEM_ERROR; - } - - return hllapi_emulate_input(text,szText,0); + *ps = 0; + return hllapi_emulate_input(text,*length,0); } static int reset_system(char *buffer, unsigned short *length, unsigned short *rc) { diff --git a/src/core/private.h b/src/core/private.h index a3bddca..c64ec21 100644 --- a/src/core/private.h +++ b/src/core/private.h @@ -54,6 +54,7 @@ TN3270_PRIVATE TN3270::Host & getSession(); TN3270_PRIVATE DWORD hllapi_translate_error(LIB3270_KEYBOARD_LOCK_STATE state); + TN3270_PRIVATE DWORD hllapi_translate_error(LIB3270_MESSAGE state); TN3270_PRIVATE DWORD hllapi_translate_error(const std::system_error &error); #endif // PRIVATE_H_INCLUDED diff --git a/src/core/set.cc b/src/core/set.cc index 67dc3f3..13578d6 100644 --- a/src/core/set.cc +++ b/src/core/set.cc @@ -56,6 +56,7 @@ } catch(const std::exception &e) { // Worker has failed! + hllapi_lasterror = e.what(); } @@ -106,7 +107,7 @@ if(!(text && *text)) return HLLAPI_STATUS_BAD_PARAMETER; - if(!length) + if(length < 1) length = strlen(text); return set([text,length](TN3270::Host &host) { diff --git a/src/core/tools.cc b/src/core/tools.cc index 2a915c0..c1244db 100644 --- a/src/core/tools.cc +++ b/src/core/tools.cc @@ -107,3 +107,26 @@ } + DWORD hllapi_translate_error(LIB3270_MESSAGE state) { + + switch(state) { + case LIB3270_MESSAGE_NONE: + return HLLAPI_STATUS_SUCCESS; + + case LIB3270_MESSAGE_DISCONNECTED: + return HLLAPI_STATUS_DISCONNECTED; + + case LIB3270_MESSAGE_MINUS: + case LIB3270_MESSAGE_PROTECTED: + case LIB3270_MESSAGE_NUMERIC: + case LIB3270_MESSAGE_OVERFLOW: + case LIB3270_MESSAGE_INHIBIT: + case LIB3270_MESSAGE_KYBDLOCK: + return HLLAPI_STATUS_KEYBOARD_LOCKED; + + default: + return HLLAPI_STATUS_SYSTEM_ERROR; + } + + return HLLAPI_STATUS_SYSTEM_ERROR; + } diff --git a/src/include/lib3270/hllapi.h b/src/include/lib3270/hllapi.h index a0ab796..3b3e6bb 100644 --- a/src/include/lib3270/hllapi.h +++ b/src/include/lib3270/hllapi.h @@ -206,16 +206,122 @@ */ HLLAPI_API_CALL hllapi_disconnect(void); + /** + * @brief Get program message. + * + * @return Current program message or -1 on error. + * + */ HLLAPI_API_CALL hllapi_get_message_id(void); + HLLAPI_API_CALL hllapi_is_connected(void); HLLAPI_API_CALL hllapi_get_state(void); HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer); HLLAPI_API_CALL hllapi_get_screen(WORD pos, LPSTR buffer, WORD len); HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text); + + /** + * @brief Compare contents at position. + * + * @param row + * @param col + * @param text + * + * @return Result of the strcmp of the string and the contents of position. + * + * @retval HLLAPI_STATUS_SYSTEM_ERROR The query has failed. + * @retval 0 The string at the position is the same. + * @retval -1 + * @retval 1 + * + */ HLLAPI_API_CALL hllapi_cmp_text_at(WORD row, WORD col, LPSTR text); - HLLAPI_API_CALL hllapi_find_text(LPSTR text); + + /** + * @brief Compare contents at position. + * + * @param row + * @param col + * @param text + * + * @return Result of the strcmp of the string and the contents of position. + * + * @retval HLLAPI_STATUS_SYSTEM_ERROR The query has failed. + * @retval 0 The string at the position is the same. + * @retval -1 + * @retval 1 + * + */ + HLLAPI_API_CALL hllapi_cmp_text_at_address(WORD addr, LPSTR text); + + /** + * @brief Find string in the screen. + * + * @return Position of the text inside the screen or -1 if failed. + * + */ + HLLAPI_API_CALL hllapi_find_text(const LPSTR text); + + /** + * @brief Interpret string with action codes prefixed by '@'. + * + * Insert string parsing the action codes prefixed with '@' character. + * + * Value | Action | Description | + * :----:|:------------|:-----------------------------------------------------------| + * @@P | - | Print the screen contents (if available) | + * @@@@ | - | Input the @@ char. | + * @@E | ENTER | - | + * @@F | ERASE_EOF | - | + * @@1 | PF1 | Send the PF1 key. | + * @@2 | PF2 | Send the PF2 key. | + * @@3 | PF3 | Send the PF3 key. | + * @@4 | PF4 | Send the PF4 key. | + * @@5 | PF5 | Send the PF5 key. | + * @@6 | PF6 | Send the PF6 key. | + * @@7 | PF7 | Send the PF7 key. | + * @@8 | PF8 | Send the PF8 key. | + * @@9 | PF9 | Send the PF9 key. | + * @@a | PF10 | Send the PF10 key. | + * @@b | PF11 | Send the PF11 key. | + * @@c | PF12 | Send the PF12 key. | + * @@d | PF13 | Send the PF13 key. | + * @@e | PF14 | Send the PF14 key. | + * @@f | PF15 | Send the PF15 key. | + * @@g | PF16 | Send the PF16 key. | + * @@h | PF17 | Send the PF17 key. | + * @@u | PF18 | Send the PF18 key. | + * @@j | PF19 | Send the PF19 key. | + * @@k | PF20 | Send the PF20 key. | + * @@l | PF21 | Send the PF21 key. | + * @@m | PF22 | Send the PF22 key. | + * @@n | PF23 | Send the PF23 key. | + * @@o | PF24 | Send the PF24 key. | + * @@x | PA1 | Send the PA1 key. | + * @@y | PA2 | Send the PA2 key. | + * @@z | PA3 | Send the PA3 key. | + * @@D | CHAR_DELETE | | + * @@N | NEWLINE | | + * @@C | CLEAR | | + * @@R | KYBD_RESET | | + * @@< | BACKSPACE | | + * + * @param text Text to input. + * @param length Length of the text (-1 to use the string length). + * @param pasting Unused (kept for compatibility). + * + */ HLLAPI_API_CALL hllapi_emulate_input(const LPSTR text, WORD length, WORD pasting); + + /** + * @brief Input string. + * + * @param buffer String to input. + * @param length Length of the string (-1 ou 0 to auto detect). + * + */ HLLAPI_API_CALL hllapi_input_string(LPSTR buffer, WORD len); + HLLAPI_API_CALL hllapi_wait_for_ready(WORD seconds); HLLAPI_API_CALL hllapi_wait_for_change(WORD seconds); HLLAPI_API_CALL hllapi_wait(WORD seconds); diff --git a/src/testprogram/testprogram.cc b/src/testprogram/testprogram.cc index edb32d2..3267ad6 100644 --- a/src/testprogram/testprogram.cc +++ b/src/testprogram/testprogram.cc @@ -192,6 +192,15 @@ rc = hllapi_input_string((LPSTR) cmdline.c_str()+6,0); + } else if(strncasecmp(cmdline.c_str(),"wait ",5) == 0) { + + rc = hllapi_wait_for_ready(5); + + } else if(strncasecmp(cmdline.c_str(),"find ",5) == 0) { + + cout << "Searching for \"" << (cmdline.c_str()+5) << "\"" << endl; + rc = hllapi_find_text(cmdline.c_str()+5); + } else { cout << "Unknown command \"" << cmdline << "\""<< endl; -- libgit2 0.21.2