Commit f4f81190e3584cc812453dacd3e89bd18b8408fb

Authored by perry.werneck@gmail.com
1 parent 85193f92

Work in progress

@@ -49,7 +49,7 @@ SSL_CFLAGS=@LIBSSL_CFLAGS@ @@ -49,7 +49,7 @@ SSL_CFLAGS=@LIBSSL_CFLAGS@
49 DLL_FLAGS=@DLL_FLAGS@ 49 DLL_FLAGS=@DLL_FLAGS@
50 LDFLAGS=@LDFLAGS@ 50 LDFLAGS=@LDFLAGS@
51 51
52 -LIBS=@LIBS@ @LIBSSL_LIBS@ @INTL_LIBS@ @SOCKET_LIBS@ 52 +LIBS=@LIBS@ @LIBSSL_LIBS@ @INTL_LIBS@ @SOCKET_LIBS@ @LIBICONV@
53 53
54 #---[ Tools ]------------------------------------------------------------------ 54 #---[ Tools ]------------------------------------------------------------------
55 55
@@ -144,7 +144,7 @@ $(BINRLS)/lib3270@DLLEXT@.@VERSION@: $(foreach SRC, $(basename $(SOURCES)), $(OB @@ -144,7 +144,7 @@ $(BINRLS)/lib3270@DLLEXT@.@VERSION@: $(foreach SRC, $(basename $(SOURCES)), $(OB
144 $(BINDBG)/testprogram$(EXEEXT): $(OBJDBG)/testprogram.o $(foreach SRC, $(basename $(SOURCES)), $(OBJDBG)/$(SRC)@OBJEXT@) 144 $(BINDBG)/testprogram$(EXEEXT): $(OBJDBG)/testprogram.o $(foreach SRC, $(basename $(SOURCES)), $(OBJDBG)/$(SRC)@OBJEXT@)
145 @echo " CCLD `basename $@`" 145 @echo " CCLD `basename $@`"
146 @$(MKDIR) `dirname $@` 146 @$(MKDIR) `dirname $@`
147 - @$(LD) -pthread -o $@ $^ $(LIBS) 147 + @$(LD) -o $@ $^ $(LIBS)
148 148
149 $(BINDIR)/pot/lib3270.pot: $(foreach SRC, $(basename $(SOURCES)), $(TMPDIR)/pot/$(SRC).pot) 149 $(BINDIR)/pot/lib3270.pot: $(foreach SRC, $(basename $(SOURCES)), $(TMPDIR)/pot/$(SRC).pot)
150 @rm -f $@ 150 @rm -f $@
@@ -323,30 +323,11 @@ @@ -323,30 +323,11 @@
323 323
324 LOCAL_EXTERN SCRIPT_STATE status_script(SCRIPT_STATE state); 324 LOCAL_EXTERN SCRIPT_STATE status_script(SCRIPT_STATE state);
325 325
326 -// #define Toggled(ix) lib3270_get_toggle(NULL,ix)  
327 -// #define CallAndWait(c,h,p) lib3270_call_thread(c,h,p)  
328 -  
329 -// #define RunPendingEvents(x) lib3270_main_iterate(x)  
330 -// #define Wait(s) lib3270_wait(x)  
331 -  
332 -// LOCAL_EXTERN int ctlr_get_cols(void) __attribute__ ((deprecated));  
333 -// LOCAL_EXTERN int ctlr_get_rows(void) __attribute__ ((deprecated));  
334 -  
335 - /* Screen calls */  
336 -// LOCAL_EXTERN void screen_resume(H3270 *session);  
337 -// LOCAL_EXTERN void screen_suspend(H3270 *session);  
338 -// LOCAL_EXTERN void screen_disp(H3270 *session);  
339 -  
340 #include <lib3270/actions.h> 326 #include <lib3270/actions.h>
341 327
342 #define host_connect(n,wait) lib3270_connect(NULL,n,wait) 328 #define host_connect(n,wait) lib3270_connect(NULL,n,wait)
343 #define host_reconnect(w) lib3270_reconnect(NULL,w) 329 #define host_reconnect(w) lib3270_reconnect(NULL,w)
344 330
345 - /* Console/Trace window */  
346 -// LOCAL_EXTERN HCONSOLE console_window_new(const char *title, const char *label);  
347 -// LOCAL_EXTERN void console_window_delete(HCONSOLE hwnd);  
348 -// LOCAL_EXTERN int console_window_append(HCONSOLE hwnd, const char *fmt, ...);  
349 -// LOCAL_EXTERN char * console_window_wait_for_user_entry(HCONSOLE hwnd);  
350 331
351 #ifdef __cplusplus 332 #ifdef __cplusplus
352 } 333 }
@@ -47,6 +47,9 @@ @@ -47,6 +47,9 @@
47 #include <fcntl.h> 47 #include <fcntl.h>
48 #endif 48 #endif
49 49
  50 +#ifdef HAVE_ICONV
  51 + #include <iconv.h>
  52 +#endif // HAVE_ICONV
50 53
51 #if defined(_WIN32) /*[*/ 54 #if defined(_WIN32) /*[*/
52 #define SOCK_CLOSE(s) closesocket(s->sock); s->sock = -1; 55 #define SOCK_CLOSE(s) closesocket(s->sock); s->sock = -1;
@@ -83,7 +86,7 @@ static void net_connected(H3270 *hSession) @@ -83,7 +86,7 @@ static void net_connected(H3270 *hSession)
83 _( "Network error" ), 86 _( "Network error" ),
84 _( "Unable to get connection state." ), 87 _( "Unable to get connection state." ),
85 #ifdef _WIN32 88 #ifdef _WIN32
86 - "%s", lib3270_win32_strerror(WSAGetLastError())); 89 + "%s", lib3270_win32_strerror(WSAGetLastError())
87 #else 90 #else
88 _( "%s" ), strerror(errno) 91 _( "%s" ), strerror(errno)
89 #endif // _WIN32 92 #endif // _WIN32
@@ -103,6 +106,7 @@ static void net_connected(H3270 *hSession) @@ -103,6 +106,7 @@ static void net_connected(H3270 *hSession)
103 _( "%s" ), strerror(err) 106 _( "%s" ), strerror(err)
104 #endif // _WIN32 107 #endif // _WIN32
105 ); 108 );
  109 + trace("%s",__FUNCTION__);
106 return; 110 return;
107 } 111 }
108 112
@@ -236,12 +240,40 @@ static void net_connected(H3270 *hSession) @@ -236,12 +240,40 @@ static void net_connected(H3270 *hSession)
236 240
237 snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc); 241 snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
238 242
  243 +#if defined(WIN32) && defined(HAVE_ICONV)
  244 + {
  245 + char tmpbuffer[4096];
  246 + const char * msg = gai_strerror(s);
  247 + size_t in = strlen(msg);
  248 + size_t out = 4096;
  249 + char * ptr = tmpbuffer;
  250 +
  251 + iconv_t hConv = iconv_open(lib3270_win32_local_charset(),"UTF-8");
  252 +
  253 + trace("Antes: [%s]",msg);
  254 + if(iconv(hConv,&msg,&in,&ptr,&out) != ((size_t) -1))
  255 + msg = tmpbuffer;
  256 + trace("Depois: [%s]",msg);
  257 +
  258 + iconv_close(hConv);
  259 +
  260 + lib3270_popup_dialog( hSession,
  261 + LIB3270_NOTIFY_ERROR,
  262 + _( "Connection error" ),
  263 + buffer,
  264 + "%s",
  265 + msg);
  266 + }
  267 +
  268 +#else
239 lib3270_popup_dialog( hSession, 269 lib3270_popup_dialog( hSession,
240 LIB3270_NOTIFY_ERROR, 270 LIB3270_NOTIFY_ERROR,
241 _( "Connection error" ), 271 _( "Connection error" ),
242 buffer, 272 buffer,
243 "%s", 273 "%s",
244 gai_strerror(s)); 274 gai_strerror(s));
  275 +#endif // WIN32
  276 +
245 277
246 lib3270_set_disconnected(hSession); 278 lib3270_set_disconnected(hSession);
247 return ENOENT; 279 return ENOENT;
@@ -383,10 +415,11 @@ static void net_connected(H3270 *hSession) @@ -383,10 +415,11 @@ static void net_connected(H3270 *hSession)
383 if(hSession->sock < 0) 415 if(hSession->sock < 0)
384 { 416 {
385 lib3270_set_disconnected(hSession); 417 lib3270_set_disconnected(hSession);
386 - return -1; 418 + return ENOTCONN;
387 } 419 }
388 420
389 // Connecting, set callbacks, wait for connection 421 // Connecting, set callbacks, wait for connection
  422 + hSession->cstate = LIB3270_PENDING;
390 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True); 423 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
391 424
392 #ifdef _WIN32 425 #ifdef _WIN32
@@ -397,6 +430,33 @@ static void net_connected(H3270 *hSession) @@ -397,6 +430,33 @@ static void net_connected(H3270 *hSession)
397 #endif // WIN32 430 #endif // WIN32
398 431
399 trace("%s: Connection in progress",__FUNCTION__); 432 trace("%s: Connection in progress",__FUNCTION__);
  433 +
  434 + if(opt&LIB3270_CONNECT_OPTION_WAIT)
  435 + {
  436 + time_t end = time(0)+120;
  437 +
  438 + while(time(0) < end)
  439 + {
  440 + lib3270_main_iterate(hSession,1);
  441 +
  442 + switch(hSession->cstate)
  443 + {
  444 + case LIB3270_PENDING:
  445 + break;
  446 +
  447 + case CONNECTED_INITIAL:
  448 + trace("%s: Connected, exiting wait",__FUNCTION__);
  449 + return 0;
  450 +
  451 + default:
  452 + trace("%s: State changed to %d",__FUNCTION__,hSession->cstate);
  453 + return -1;
  454 + }
  455 +
  456 + }
  457 + return ETIMEDOUT;
  458 + }
  459 +
400 return 0; 460 return 0;
401 461
402 } 462 }
@@ -35,8 +35,8 @@ int main(int numpar, char *param[]) @@ -35,8 +35,8 @@ int main(int numpar, char *param[])
35 // pthread_create(&thread, NULL, mainloop, NULL); 35 // pthread_create(&thread, NULL, mainloop, NULL);
36 // pthread_detach(thread); 36 // pthread_detach(thread);
37 37
38 - lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_DEFAULTS);  
39 -// lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_DEFAULTS); 38 +// lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_WAIT);
  39 + lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_WAIT);
40 40
41 mainloop(0); 41 mainloop(0);
42 42
@@ -65,6 +65,10 @@ @@ -65,6 +65,10 @@
65 #include <stdlib.h> 65 #include <stdlib.h>
66 #endif // !ANDROID 66 #endif // !ANDROID
67 67
  68 +#ifdef HAVE_ICONV
  69 + #include <iconv.h>
  70 +#endif // HAVE_ICONV
  71 +
68 #include <stdarg.h> 72 #include <stdarg.h>
69 #include "resources.h" 73 #include "resources.h"
70 74
@@ -151,11 +155,50 @@ LIB3270_EXPORT const char * lib3270_win32_strerror(int e) @@ -151,11 +155,50 @@ LIB3270_EXPORT const char * lib3270_win32_strerror(int e)
151 static char buffer[4096]; 155 static char buffer[4096];
152 156
153 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0) 157 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0)
154 - snprintf(buffer, 4095, "Windows error %d", e); 158 + {
  159 + snprintf(buffer, 4095, _( "Windows error %d" ), e);
  160 + return buffer;
  161 + }
  162 +
  163 + trace("%s",buffer);
  164 +
  165 +#ifdef HAVE_ICONV
  166 + {
  167 + // Convert from windows codepage to UTF-8 pw3270´s default charset
  168 + char tmpbuffer[4096];
  169 + size_t in = strlen(buffer);
  170 + size_t out = 4095;
  171 + char * ptr = tmpbuffer;
  172 +
  173 + iconv_t hConv = iconv_open("UTF-8",lib3270_win32_local_charset());
  174 +
  175 + trace("[%s]",buffer);
  176 +
  177 + if(hConv == (iconv_t) -1)
  178 + {
  179 + lib3270_write_log(NULL,"iconv","%s: Error creating charset conversion",__FUNCTION__);
  180 + }
  181 + else if(iconv(hConv,(const char **) &buffer,&in,&ptr,&out) != ((size_t) -1))
  182 + {
  183 + // strncpy(buffer,tmpbuffer,4096);
  184 + }
  185 +
  186 + trace("[%s]",buffer);
155 187
  188 + iconv_close(hConv);
  189 + }
  190 +#endif // HAVE_ICONV
  191 +
  192 + trace("[%s]",buffer);
156 return buffer; 193 return buffer;
157 } 194 }
158 195
  196 +LIB3270_EXPORT const char * lib3270_win32_local_charset(void)
  197 +{
  198 + return "CP1252";
  199 +}
  200 +
  201 +
159 #endif // _WIN32 202 #endif // _WIN32
160 203
161 /* 204 /*
@@ -943,49 +986,6 @@ LIB3270_EXPORT void * lib3270_strdup(const char *str) @@ -943,49 +986,6 @@ LIB3270_EXPORT void * lib3270_strdup(const char *str)
943 return r; 986 return r;
944 } 987 }
945 988
946 -/*  
947 -LIB3270_EXPORT char * lib3270_get_resource_string(H3270 *hSession, const char *first_element, ...)  
948 -{  
949 -#ifdef ANDROID  
950 -  
951 - #warning No resource on Android  
952 -  
953 -#else  
954 -  
955 - char * str = lib3270_malloc(4097);  
956 - char * ptr = str;  
957 - const char * element;  
958 - va_list args;  
959 - const char * res;  
960 -  
961 - va_start(args, first_element);  
962 -  
963 - for(element = first_element;element;element = va_arg(args, const char *))  
964 - {  
965 - if(ptr != str)  
966 - *(ptr++) = '.';  
967 -  
968 - strncpy(ptr,element,4096-strlen(str));  
969 - ptr += strlen(ptr);  
970 - }  
971 -  
972 - va_end(args);  
973 -  
974 - *ptr = 0;  
975 -  
976 - res = get_resource(hSession,str);  
977 -  
978 - trace("%s(%s)=%s",__FUNCTION__,str,res ? res : "NULL");  
979 -  
980 - lib3270_free(str);  
981 -  
982 - if(res)  
983 - return strdup(res);  
984 -#endif  
985 - return NULL;  
986 -}  
987 -*/  
988 -  
989 LIB3270_EXPORT const char * lib3270_get_version(void) 989 LIB3270_EXPORT const char * lib3270_get_version(void)
990 { 990 {
991 return build_rpq_version; 991 return build_rpq_version;