From 1fdad013c2f68c3336295b2f1df2ee3751745cca Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 20 Sep 2012 12:52:59 +0000 Subject: [PATCH] HLLAPI passa a converter o charset do terminal, funcao lib3270_get_text estava aceitando gets maiores que o tamanho do terminal, implementacao da funcao HLLAPI para obter o buffer do terminal, HLLAPI estava fechando a pipe de comunicacao logo apos a conexao --- src/lib3270/selection.c | 6 ++---- src/plugins/remotectl/hllapi.c | 5 ++++- src/plugins/remotectl/remotectl.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/plugins/remotectl/remotectl.h | 2 +- src/plugins/remotectl/testprogram.c | 27 ++++++++++++++++++++++----- 5 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/lib3270/selection.c b/src/lib3270/selection.c index 0bd46d2..b58aefd 100644 --- a/src/lib3270/selection.c +++ b/src/lib3270/selection.c @@ -464,11 +464,9 @@ LIB3270_EXPORT char * lib3270_get_text(H3270 *h, int offset, int len) if(!lib3270_connected(h)) return NULL; - maxlen = h->rows * (h->cols+1); + maxlen = (h->rows * (h->cols+1)) - offset; - if(len < 0) - len = (maxlen - offset); - else if(len > maxlen) + if(len < 0 || len > maxlen) len = maxlen; buffer = lib3270_malloc(len+1); diff --git a/src/plugins/remotectl/hllapi.c b/src/plugins/remotectl/hllapi.c index c1c973a..911656a 100644 --- a/src/plugins/remotectl/hllapi.c +++ b/src/plugins/remotectl/hllapi.c @@ -68,6 +68,7 @@ if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL)) return GetLastError(); + trace("Pipe %ld open",(unsigned long) hPipe); #else #error Not implemented @@ -89,6 +90,7 @@ if(hPipe == INVALID_HANDLE_VALUE) { + trace("Invalid pipe handle %ld",(unsigned long) hPipe); result = EPERM; } else @@ -180,8 +182,9 @@ if(!result) { result = run_query(*func, arg, str, *length, rc); - if(result || rc) + if(result || *rc) { + trace("Closing pipe rc=%d result=%d ",*rc,result); CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; } diff --git a/src/plugins/remotectl/remotectl.c b/src/plugins/remotectl/remotectl.c index f1301ca..2835724 100644 --- a/src/plugins/remotectl/remotectl.c +++ b/src/plugins/remotectl/remotectl.c @@ -162,15 +162,36 @@ return 0; } - static int cmd_sendstring(H3270 *hSession, unsigned short rc, char *text, unsigned short length) + static int cmd_sendstring(H3270 *hSession, unsigned short dunno, char *buffer, unsigned short length) { + gchar * text; + GError * error = NULL; + gsize bytes_read; + gsize bytes_written; + const gchar * charset; + int rc = -1; + if(!lib3270_connected(hSession)) return ENOTCONN; - #warning Converter formato da string - lib3270_emulate_input(hSession,text,strlen(text),0); + g_get_charset(&charset); - return 0; + text = g_convert(buffer,-1,lib3270_get_charset(hSession),charset,&bytes_read,&bytes_written,&error); + if(text) + { + #warning Converter "@" em "\\" + lib3270_emulate_input(hSession,text,strlen(text),0); + g_free(text); + rc = 0; + } + else + { + strncpy(buffer,error->message,length); + rc = error->code; + g_error_free(error); + } + + return rc; } static int cmd_wait(H3270 *hSession, unsigned short rc, char *text, unsigned short length) @@ -178,6 +199,39 @@ return lib3270_wait_for_ready(hSession,60); } + static int cmd_copypstostr(H3270 *hSession, unsigned short pos, char *buffer, unsigned short length) + { + char * text = lib3270_get_text(hSession, (int) pos, (int) length); + gchar * local; + GError * error = NULL; + gsize bytes_read; + gsize bytes_written; + int rc = 0; + const gchar * charset; + + if(!text) + return -1; + + g_get_charset(&charset); + + local = g_convert((const gchar *) text,-1,charset,lib3270_get_charset(hSession),&bytes_read,&bytes_written,&error); + + if(local) + { + strncpy(buffer,local,length); + g_free(local); + } + else + { + strncpy(buffer,error->message,length); + rc = error->code; + g_error_free(error); + } + + lib3270_free(text); + return rc; + } + int run_hllapi(unsigned long function, char *string, unsigned short length, unsigned short rc) { static const struct _cmd @@ -191,7 +245,8 @@ { HLLAPI_CMD_INPUTSTRING, cmd_sendstring }, { HLLAPI_CMD_WAIT, cmd_wait }, { HLLAPI_CMD_SETCURSOR, cmd_setcursor }, - { HLLAPI_CMD_GETREVISION, cmd_getrevision } + { HLLAPI_CMD_GETREVISION, cmd_getrevision }, + { HLLAPI_CMD_COPYPSTOSTR, cmd_copypstostr } }; int f; diff --git a/src/plugins/remotectl/remotectl.h b/src/plugins/remotectl/remotectl.h index 68302b8..70976df 100644 --- a/src/plugins/remotectl/remotectl.h +++ b/src/plugins/remotectl/remotectl.h @@ -46,7 +46,7 @@ #ifdef WIN32 - #define PIPE_BUFFER_LENGTH 4096 + #define PIPE_BUFFER_LENGTH 8192 void init_source_pipe(HANDLE hPipe); void popup_lasterror(const gchar *fmt, ...); diff --git a/src/plugins/remotectl/testprogram.c b/src/plugins/remotectl/testprogram.c index 8f0dec9..ad6cd77 100644 --- a/src/plugins/remotectl/testprogram.c +++ b/src/plugins/remotectl/testprogram.c @@ -31,12 +31,17 @@ #include #include + #define BUFFER_LENGTH 4096 + /*---[ Implement ]--------------------------------------------------------------------------------*/ int main(int numpar, char *param[]) { - char buffer[1024]; + char buffer[BUFFER_LENGTH]; unsigned short rc; + unsigned short len; + int result; + unsigned long fn; static const struct _cmd { @@ -49,7 +54,6 @@ { "GetRevision", HLLAPI_CMD_GETREVISION, "" }, { "InputString", HLLAPI_CMD_INPUTSTRING, "test" }, - { "DisconnectPS", HLLAPI_CMD_DISCONNECTPS, "" }, }; int f; @@ -57,14 +61,27 @@ for(f=0;f< (sizeof(cmd)/sizeof(struct _cmd)); f++) { - unsigned short len = 1024; - int result; - + len = BUFFER_LENGTH; strcpy(buffer,cmd[f].arg); result = hllapi(&cmd[f].fn,buffer,&len,&rc); printf("%s exits with %d\n[%s]\n",cmd[f].name,result,buffer); } + len = BUFFER_LENGTH; + rc = 1; + fn = HLLAPI_CMD_COPYPSTOSTR; + result = hllapi(&fn,buffer,&len,&rc); + printf("%s exits with %d\n%s\n","HLLAPI_CMD_COPYPSTOSTR",result,buffer); + + + // Disconnect + len = BUFFER_LENGTH; + rc = 1; + fn = HLLAPI_CMD_DISCONNECTPS; + *buffer = 0; + result = hllapi(&fn,buffer,&len,&rc); + printf("%s exits with %d [%s]\n","HLLAPI_CMD_DISCONNECTPS",result,buffer); + return 0; } -- libgit2 0.21.2