diff --git a/src/core/calls.cc b/src/core/calls.cc index 8a34465..84562cc 100644 --- a/src/core/calls.cc +++ b/src/core/calls.cc @@ -37,7 +37,7 @@ return atoi(getSession().getRevision().c_str()); - } catch(std::exception &e) { + } catch(const std::exception &e) { hllapi_lasterror = e.what(); @@ -50,15 +50,21 @@ { try { - getSession().connect((const char *) uri, (wait != 0)); + getSession().connect((const char *) uri); if(wait) return hllapi_wait_for_ready(wait); - } catch(std::exception &e) { + } catch(const std::exception &e) { hllapi_lasterror = e.what(); return HLLAPI_STATUS_SYSTEM_ERROR; + + } catch(...) { + + hllapi_lasterror = "Unexpected error"; + return HLLAPI_STATUS_SYSTEM_ERROR; + } return hllapi_get_state(); @@ -170,7 +176,7 @@ return hllapi_get_state(); - } catch(std::exception &e) { + } catch(const std::exception &e) { hllapi_lasterror = e.what(); diff --git a/src/core/get.cc b/src/core/get.cc index a8e7e68..0cf2efe 100644 --- a/src/core/get.cc +++ b/src/core/get.cc @@ -31,8 +31,48 @@ /*--[ Implement ]------------------------------------------------------------------------------------*/ + static DWORD get(std::function worker) noexcept { + + try { + + TN3270::Host &host = getSession(); + + if(!host.isConnected()) + return HLLAPI_STATUS_DISCONNECTED; + + worker(host); + + } catch(std::exception &e) { + + hllapi_lasterror = e.what(); + return HLLAPI_STATUS_SYSTEM_ERROR; + + } catch(...) { + + hllapi_lasterror = "Unexpected error"; + return HLLAPI_STATUS_SYSTEM_ERROR; + + } + + return HLLAPI_STATUS_SUCCESS; + + } + HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer) { + if(!(buffer && *buffer)) + return HLLAPI_STATUS_BAD_PARAMETER; + + return get([row,col,buffer](TN3270::Host &host) { + + size_t length = strlen(buffer); + string contents = host.toString( (int) row, (int) col, length); + + strncpy((char *) buffer, contents.c_str(), std::min(length,contents.size())); + + }); + + /* try { TN3270::Host &host = getSession(); @@ -56,11 +96,29 @@ } return HLLAPI_STATUS_SUCCESS; + */ } HLLAPI_API_CALL hllapi_get_screen(WORD offset, LPSTR buffer, WORD len) { + if(!(buffer && *buffer)) + return HLLAPI_STATUS_BAD_PARAMETER; + + if(len == 0) + return HLLAPI_STATUS_BAD_PARAMETER; + + return get([offset,buffer,len](TN3270::Host &host) { + + string contents = host.toString((int) offset, (size_t) len); + + memset(buffer,' ',len); + strncpy((char *) buffer, contents.c_str(), std::min((size_t) len,contents.size())); + + }); + + + /* try { TN3270::Host &host = getSession(); @@ -87,11 +145,27 @@ } return HLLAPI_STATUS_SUCCESS; + */ } HLLAPI_API_CALL hllapi_get_lu_name(LPSTR buffer, WORD len) { + if(!(buffer && *buffer)) + return HLLAPI_STATUS_BAD_PARAMETER; + + if(len == 0) + return HLLAPI_STATUS_BAD_PARAMETER; + + return get([buffer,len](TN3270::Host &host) { + + string luname = host.getLUName(); + memset(buffer,' ',len); + strncpy((char *) buffer, luname.c_str(), std::min((size_t) len,luname.size())); + + }); + + /* try { TN3270::Host &host = getSession(); @@ -107,7 +181,6 @@ 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())); @@ -120,6 +193,8 @@ return HLLAPI_STATUS_SUCCESS; + */ + } diff --git a/src/core/set.cc b/src/core/set.cc index 2e1cd7b..357fc77 100644 --- a/src/core/set.cc +++ b/src/core/set.cc @@ -28,70 +28,105 @@ */ #include "private.h" + #include #include + #include /*--[ Implement ]------------------------------------------------------------------------------------*/ - HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text) { + DWORD hllapi_translate_keyboard_state(LIB3270_KEYBOARD_LOCK_STATE state, HLLAPI_STATUS def = HLLAPI_STATUS_SYSTEM_ERROR) { + + // Is unlocked. + if(state == LIB3270_KL_UNLOCKED) + return def; + + // Is connected? + if((state & LIB3270_KL_NOT_CONNECTED) != 0) + return HLLAPI_STATUS_DISCONNECTED; + + if( (state & (LIB3270_KL_AWAITING_FIRST|LIB3270_KL_OIA_TWAIT)) != 0) + return HLLAPI_STATUS_WAITING; + + return HLLAPI_STATUS_KEYBOARD_LOCKED; + + } - try { + static DWORD set(std::function worker) noexcept { + + LIB3270_KEYBOARD_LOCK_STATE kLock = LIB3270_KL_UNLOCKED; + + try { TN3270::Host &host = getSession(); - if(!host.isConnected()) - return HLLAPI_STATUS_DISCONNECTED; + kLock = host.waitForKeyboardUnlock(); + if(kLock == LIB3270_KL_UNLOCKED) { - if(!(text && *text)) - return HLLAPI_STATUS_BAD_PARAMETER; + try { - host.push(row,col,(const char *) text); + worker(host); + return HLLAPI_STATUS_SUCCESS; + + } catch(const std::exception &e) { + // Worker has failed! + hllapi_lasterror = e.what(); + } - } catch(std::exception &e) { + // Failed! Get lock state. + kLock = host.getKeyboardLockState(); + } + + + } catch(const std::exception &e) { + + // Error getting session or lock state hllapi_lasterror = e.what(); return HLLAPI_STATUS_SYSTEM_ERROR; + } catch(...) { + + // Unexpected error getting session or lock state + hllapi_lasterror = "Unexpected error"; + return HLLAPI_STATUS_SYSTEM_ERROR; + } - return HLLAPI_STATUS_SUCCESS; + return hllapi_translate_keyboard_state(kLock); } - HLLAPI_API_CALL hllapi_input_string(LPSTR text, WORD length) - { + HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text) { - try { + if(!(text && *text)) + return HLLAPI_STATUS_BAD_PARAMETER; - TN3270::Host &host = getSession(); + return set([row,col,text](TN3270::Host &host) { - if(!host.isConnected()) - return HLLAPI_STATUS_DISCONNECTED; + host.push(row,col,(const char *) text); - if(!(text && *text)) - return HLLAPI_STATUS_BAD_PARAMETER; + }); - if(!length) - length = strlen(text); - host.input((const char *) text, (size_t) length); + } - } catch(std::exception &e) { + HLLAPI_API_CALL hllapi_input_string(LPSTR text, WORD length) + { - hllapi_lasterror = e.what(); - return HLLAPI_STATUS_SYSTEM_ERROR; + if(!(text && *text)) + return HLLAPI_STATUS_BAD_PARAMETER; - } + return set([text,length](TN3270::Host &host) { - return HLLAPI_STATUS_SUCCESS; + host.input((const char *) text, (int) length, '@'); + }); } - - /* HLLAPI_API_CALL hllapi_emulate_input(const LPSTR buffer, WORD len, WORD pasting) { -- libgit2 0.21.2