diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 69e9b2e..4114cdc 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -1035,7 +1035,7 @@ * @param c Pointer to character. * @param attr Pointer to attribute. * - * @return 0 if ok or error code. + * @return 0 if ok, -1 if fails (sets errno). * */ LIB3270_EXPORT int lib3270_get_element(H3270 *h, int baddr, unsigned char *c, unsigned short *attr); diff --git a/src/lib3270/private.h b/src/lib3270/private.h index 44024cf..adc6ac1 100644 --- a/src/lib3270/private.h +++ b/src/lib3270/private.h @@ -633,6 +633,11 @@ LIB3270_INTERNAL int lib3270_default_event_dispatcher(H3270 *hSession, int block LIB3270_INTERNAL void check_session_handle(H3270 **hSession); #endif // DEBUG +LIB3270_INTERNAL int check_online_session(H3270 *hSession); + +/// @brief Returns -1 if the session is invalid or not online (sets errno). +#define FAIL_IF_NOT_ONLINE(x) if(check_online_session(x)) return -1; + LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on); #if defined(HAVE_LIBSSL) /*[*/ diff --git a/src/lib3270/screen.c b/src/lib3270/screen.c index 48da3ca..32ef38f 100644 --- a/src/lib3270/screen.c +++ b/src/lib3270/screen.c @@ -98,15 +98,18 @@ static void addch(H3270 *session, int baddr, unsigned char c, unsigned short att session->cbk.update(session,baddr,c,attr,baddr == session->cursor_addr); } -LIB3270_EXPORT int lib3270_get_element(H3270 *h, int baddr, unsigned char *c, unsigned short *attr) +LIB3270_EXPORT int lib3270_get_element(H3270 *hSession, int baddr, unsigned char *c, unsigned short *attr) { - CHECK_SESSION_HANDLE(h); + FAIL_IF_NOT_ONLINE(hSession); - if(!h->text || baddr < 0 || baddr > (h->rows*h->cols)) - return EINVAL; + if(!hSession->text || baddr < 0 || baddr > (hSession->rows*hSession->cols)) + { + errno = EINVAL; + return -1; + } - *c = h->text[baddr].chr; - *attr = h->text[baddr].attr; + *c = hSession->text[baddr].chr; + *attr = hSession->text[baddr].attr; return 0; } @@ -394,7 +397,7 @@ LIB3270_EXPORT int lib3270_get_cursor_address(H3270 *h) */ LIB3270_EXPORT int lib3270_translate_to_address(H3270 *hSession, int row, int col) { - CHECK_SESSION_HANDLE(hSession); + FAIL_IF_NOT_ONLINE(hSession); row--; col--; @@ -423,7 +426,7 @@ LIB3270_EXPORT int lib3270_translate_to_address(H3270 *hSession, int row, int co */ LIB3270_EXPORT int lib3270_set_cursor_address(H3270 *hSession, int baddr) { - CHECK_SESSION_HANDLE(hSession); + FAIL_IF_NOT_ONLINE(hSession); trace("%s(%d)",__FUNCTION__,baddr); @@ -877,10 +880,10 @@ LIB3270_ACTION( testpattern ) static const unsigned char gr[] = { 0, GR_UNDERLINE, GR_BLINK }; - int row = 0; + unsigned int row = 0; int max; int pos = 0; - int grpos = 0; + unsigned int grpos = 0; int f; int fg = COLOR_BLUE; @@ -933,10 +936,13 @@ LIB3270_EXPORT int lib3270_is_protected(H3270 *h, unsigned int baddr) { unsigned char fa; - CHECK_SESSION_HANDLE(h); + FAIL_IF_NOT_ONLINE(h); if(baddr > (h->rows * h->cols)) + { + errno = EINVAL; return -1; + } fa = get_field_attribute(h,baddr); diff --git a/src/lib3270/session.c b/src/lib3270/session.c index 50cc0b2..ef66cae 100644 --- a/src/lib3270/session.c +++ b/src/lib3270/session.c @@ -388,6 +388,25 @@ void check_session_handle(H3270 **hSession) #endif // ANDROID } +LIB3270_INTERNAL int check_online_session(H3270 *hSession) { + + // Is the session valid? + if(!hSession) + { + errno = EINVAL; + return -1; + } + + // Is it connected? + if((int) hSession->cstate < (int)LIB3270_CONNECTED_INITIAL) + { + errno = ENOTCONN; + return -1; + } + + return 0; +} + LIB3270_EXPORT H3270 * lib3270_get_default_session_handle(void) { if(default_session) -- libgit2 0.21.2