diff --git a/Makefile.in b/Makefile.in index 96d22da..89963df 100644 --- a/Makefile.in +++ b/Makefile.in @@ -49,7 +49,7 @@ SSL_CFLAGS=@LIBSSL_CFLAGS@ DLL_FLAGS=@DLL_FLAGS@ LDFLAGS=@LDFLAGS@ -LIBS=@LIBS@ @LIBSSL_LIBS@ @INTL_LIBS@ @SOCKET_LIBS@ +LIBS=@LIBS@ @LIBSSL_LIBS@ @INTL_LIBS@ @SOCKET_LIBS@ @LIBICONV@ #---[ Tools ]------------------------------------------------------------------ @@ -144,7 +144,7 @@ $(BINRLS)/lib3270@DLLEXT@.@VERSION@: $(foreach SRC, $(basename $(SOURCES)), $(OB $(BINDBG)/testprogram$(EXEEXT): $(OBJDBG)/testprogram.o $(foreach SRC, $(basename $(SOURCES)), $(OBJDBG)/$(SRC)@OBJEXT@) @echo " CCLD `basename $@`" @$(MKDIR) `dirname $@` - @$(LD) -pthread -o $@ $^ $(LIBS) + @$(LD) -o $@ $^ $(LIBS) $(BINDIR)/pot/lib3270.pot: $(foreach SRC, $(basename $(SOURCES)), $(TMPDIR)/pot/$(SRC).pot) @rm -f $@ diff --git a/api.h b/api.h index 40e6ae6..0578389 100644 --- a/api.h +++ b/api.h @@ -323,30 +323,11 @@ LOCAL_EXTERN SCRIPT_STATE status_script(SCRIPT_STATE state); -// #define Toggled(ix) lib3270_get_toggle(NULL,ix) -// #define CallAndWait(c,h,p) lib3270_call_thread(c,h,p) - -// #define RunPendingEvents(x) lib3270_main_iterate(x) -// #define Wait(s) lib3270_wait(x) - -// LOCAL_EXTERN int ctlr_get_cols(void) __attribute__ ((deprecated)); -// LOCAL_EXTERN int ctlr_get_rows(void) __attribute__ ((deprecated)); - - /* Screen calls */ -// LOCAL_EXTERN void screen_resume(H3270 *session); -// LOCAL_EXTERN void screen_suspend(H3270 *session); -// LOCAL_EXTERN void screen_disp(H3270 *session); - #include #define host_connect(n,wait) lib3270_connect(NULL,n,wait) #define host_reconnect(w) lib3270_reconnect(NULL,w) - /* Console/Trace window */ -// LOCAL_EXTERN HCONSOLE console_window_new(const char *title, const char *label); -// LOCAL_EXTERN void console_window_delete(HCONSOLE hwnd); -// LOCAL_EXTERN int console_window_append(HCONSOLE hwnd, const char *fmt, ...); -// LOCAL_EXTERN char * console_window_wait_for_user_entry(HCONSOLE hwnd); #ifdef __cplusplus } diff --git a/connect.c b/connect.c index 21d5867..f39c45f 100644 --- a/connect.c +++ b/connect.c @@ -47,6 +47,9 @@ #include #endif +#ifdef HAVE_ICONV + #include +#endif // HAVE_ICONV #if defined(_WIN32) /*[*/ #define SOCK_CLOSE(s) closesocket(s->sock); s->sock = -1; @@ -83,7 +86,7 @@ static void net_connected(H3270 *hSession) _( "Network error" ), _( "Unable to get connection state." ), #ifdef _WIN32 - "%s", lib3270_win32_strerror(WSAGetLastError())); + "%s", lib3270_win32_strerror(WSAGetLastError()) #else _( "%s" ), strerror(errno) #endif // _WIN32 @@ -103,6 +106,7 @@ static void net_connected(H3270 *hSession) _( "%s" ), strerror(err) #endif // _WIN32 ); + trace("%s",__FUNCTION__); return; } @@ -236,12 +240,40 @@ static void net_connected(H3270 *hSession) snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc); +#if defined(WIN32) && defined(HAVE_ICONV) + { + char tmpbuffer[4096]; + const char * msg = gai_strerror(s); + size_t in = strlen(msg); + size_t out = 4096; + char * ptr = tmpbuffer; + + iconv_t hConv = iconv_open(lib3270_win32_local_charset(),"UTF-8"); + + trace("Antes: [%s]",msg); + if(iconv(hConv,&msg,&in,&ptr,&out) != ((size_t) -1)) + msg = tmpbuffer; + trace("Depois: [%s]",msg); + + iconv_close(hConv); + + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Connection error" ), + buffer, + "%s", + msg); + } + +#else lib3270_popup_dialog( hSession, LIB3270_NOTIFY_ERROR, _( "Connection error" ), buffer, "%s", gai_strerror(s)); +#endif // WIN32 + lib3270_set_disconnected(hSession); return ENOENT; @@ -383,10 +415,11 @@ static void net_connected(H3270 *hSession) if(hSession->sock < 0) { lib3270_set_disconnected(hSession); - return -1; + return ENOTCONN; } // Connecting, set callbacks, wait for connection + hSession->cstate = LIB3270_PENDING; lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True); #ifdef _WIN32 @@ -397,6 +430,33 @@ static void net_connected(H3270 *hSession) #endif // WIN32 trace("%s: Connection in progress",__FUNCTION__); + + if(opt&LIB3270_CONNECT_OPTION_WAIT) + { + time_t end = time(0)+120; + + while(time(0) < end) + { + lib3270_main_iterate(hSession,1); + + switch(hSession->cstate) + { + case LIB3270_PENDING: + break; + + case CONNECTED_INITIAL: + trace("%s: Connected, exiting wait",__FUNCTION__); + return 0; + + default: + trace("%s: State changed to %d",__FUNCTION__,hSession->cstate); + return -1; + } + + } + return ETIMEDOUT; + } + return 0; } diff --git a/testprogram.c b/testprogram.c index fe7f2ce..b22788e 100644 --- a/testprogram.c +++ b/testprogram.c @@ -35,8 +35,8 @@ int main(int numpar, char *param[]) // pthread_create(&thread, NULL, mainloop, NULL); // pthread_detach(thread); - lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_DEFAULTS); -// lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_DEFAULTS); +// lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_WAIT); + lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_WAIT); mainloop(0); diff --git a/util.c b/util.c index 0459329..fe7392a 100644 --- a/util.c +++ b/util.c @@ -65,6 +65,10 @@ #include #endif // !ANDROID +#ifdef HAVE_ICONV + #include +#endif // HAVE_ICONV + #include #include "resources.h" @@ -151,11 +155,50 @@ LIB3270_EXPORT const char * lib3270_win32_strerror(int e) static char buffer[4096]; if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0) - snprintf(buffer, 4095, "Windows error %d", e); + { + snprintf(buffer, 4095, _( "Windows error %d" ), e); + return buffer; + } + + trace("%s",buffer); + +#ifdef HAVE_ICONV + { + // Convert from windows codepage to UTF-8 pw3270´s default charset + char tmpbuffer[4096]; + size_t in = strlen(buffer); + size_t out = 4095; + char * ptr = tmpbuffer; + + iconv_t hConv = iconv_open("UTF-8",lib3270_win32_local_charset()); + + trace("[%s]",buffer); + + if(hConv == (iconv_t) -1) + { + lib3270_write_log(NULL,"iconv","%s: Error creating charset conversion",__FUNCTION__); + } + else if(iconv(hConv,(const char **) &buffer,&in,&ptr,&out) != ((size_t) -1)) + { + // strncpy(buffer,tmpbuffer,4096); + } + + trace("[%s]",buffer); + iconv_close(hConv); + } +#endif // HAVE_ICONV + + trace("[%s]",buffer); return buffer; } +LIB3270_EXPORT const char * lib3270_win32_local_charset(void) +{ + return "CP1252"; +} + + #endif // _WIN32 /* @@ -943,49 +986,6 @@ LIB3270_EXPORT void * lib3270_strdup(const char *str) return r; } -/* -LIB3270_EXPORT char * lib3270_get_resource_string(H3270 *hSession, const char *first_element, ...) -{ -#ifdef ANDROID - - #warning No resource on Android - -#else - - char * str = lib3270_malloc(4097); - char * ptr = str; - const char * element; - va_list args; - const char * res; - - va_start(args, first_element); - - for(element = first_element;element;element = va_arg(args, const char *)) - { - if(ptr != str) - *(ptr++) = '.'; - - strncpy(ptr,element,4096-strlen(str)); - ptr += strlen(ptr); - } - - va_end(args); - - *ptr = 0; - - res = get_resource(hSession,str); - - trace("%s(%s)=%s",__FUNCTION__,str,res ? res : "NULL"); - - lib3270_free(str); - - if(res) - return strdup(res); -#endif - return NULL; -} -*/ - LIB3270_EXPORT const char * lib3270_get_version(void) { return build_rpq_version; -- libgit2 0.21.2