Commit 530bea6248cd5e6a283b1bd832b852cf631056cd

Authored by Perry Werneck
1 parent 4e94d4aa
Exists in master and in 1 other branch develop

Debugging HLLAPI module.

@@ -41,6 +41,7 @@ @@ -41,6 +41,7 @@
41 <Unit filename="src/core/keyboard.cc" /> 41 <Unit filename="src/core/keyboard.cc" />
42 <Unit filename="src/core/private.h" /> 42 <Unit filename="src/core/private.h" />
43 <Unit filename="src/core/set.cc" /> 43 <Unit filename="src/core/set.cc" />
  44 + <Unit filename="src/core/tools.cc" />
44 <Unit filename="src/core/windows/resources.rc" /> 45 <Unit filename="src/core/windows/resources.rc" />
45 <Unit filename="src/include/config.h" /> 46 <Unit filename="src/include/config.h" />
46 <Unit filename="src/include/lib3270/hllapi.h" /> 47 <Unit filename="src/include/lib3270/hllapi.h" />
src/core/actions.cc
@@ -45,6 +45,10 @@ @@ -45,6 +45,10 @@
45 45
46 return HLLAPI_STATUS_SUCCESS; 46 return HLLAPI_STATUS_SUCCESS;
47 47
  48 + } catch(const std::system_error &e) {
  49 +
  50 + return hllapi_translate_error(e);
  51 +
48 } catch(const std::exception &e) { 52 } catch(const std::exception &e) {
49 53
50 hllapi_lasterror = e.what(); 54 hllapi_lasterror = e.what();
src/core/calls.cc
@@ -223,25 +223,6 @@ @@ -223,25 +223,6 @@
223 return HLLAPI_STATUS_SYSTEM_ERROR; 223 return HLLAPI_STATUS_SYSTEM_ERROR;
224 } 224 }
225 225
226 - HLLAPI_API_CALL hllapi_get_datadir(LPSTR datadir) {  
227 -  
228 - #ifdef _WIN32  
229 - HKEY hKey = 0;  
230 - unsigned long datalen = strlen(datadir);  
231 -  
232 - *datadir = 0;  
233 -  
234 - if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS)  
235 - {  
236 - unsigned long datatype; // #defined in winnt.h (predefined types 0-11)  
237 - if(RegQueryValueExA(hKey,"datadir",NULL,&datatype,(LPBYTE) datadir,&datalen) != ERROR_SUCCESS)  
238 - *datadir = 0;  
239 - RegCloseKey(hKey);  
240 - }  
241 -#endif // _WIN32  
242 -  
243 - return *datadir;  
244 - }  
245 226
246 HLLAPI_API_CALL hllapi_wait(WORD seconds) { 227 HLLAPI_API_CALL hllapi_wait(WORD seconds) {
247 228
@@ -321,20 +302,3 @@ @@ -321,20 +302,3 @@
321 302
322 } 303 }
323 304
324 - DWORD hllapi_translate_keyboard_state(LIB3270_KEYBOARD_LOCK_STATE state, HLLAPI_STATUS def) {  
325 -  
326 - // Is unlocked.  
327 - if(state == LIB3270_KL_UNLOCKED)  
328 - return def;  
329 -  
330 - // Is connected?  
331 - if((state & LIB3270_KL_NOT_CONNECTED) != 0)  
332 - return HLLAPI_STATUS_DISCONNECTED;  
333 -  
334 - if( (state & (LIB3270_KL_AWAITING_FIRST|LIB3270_KL_OIA_TWAIT)) != 0)  
335 - return HLLAPI_STATUS_WAITING;  
336 -  
337 - return HLLAPI_STATUS_KEYBOARD_LOCKED;  
338 -  
339 - }  
340 -  
src/core/get.cc
@@ -37,11 +37,13 @@ @@ -37,11 +37,13 @@
37 37
38 TN3270::Host &host = getSession(); 38 TN3270::Host &host = getSession();
39 39
40 - if(!host.isConnected())  
41 - return HLLAPI_STATUS_DISCONNECTED;  
42 - 40 + host.waitForReady();
43 worker(host); 41 worker(host);
44 42
  43 + } catch(const std::system_error &e) {
  44 +
  45 + return hllapi_translate_error(e);
  46 +
45 } catch(std::exception &e) { 47 } catch(std::exception &e) {
46 48
47 hllapi_lasterror = e.what(); 49 hllapi_lasterror = e.what();
@@ -66,7 +68,7 @@ @@ -66,7 +68,7 @@
66 return get([row,col,buffer](TN3270::Host &host) { 68 return get([row,col,buffer](TN3270::Host &host) {
67 69
68 size_t length = strlen(buffer); 70 size_t length = strlen(buffer);
69 - string contents = host.toString( (int) row, (int) col, length); 71 + string contents = host.toString( (unsigned int) row, (unsigned int) col, (int) length);
70 72
71 strncpy((char *) buffer, contents.c_str(), std::min(length,contents.size())); 73 strncpy((char *) buffer, contents.c_str(), std::min(length,contents.size()));
72 74
src/core/keyboard.cc
@@ -64,6 +64,10 @@ static DWORD set(std::function&lt;void(TN3270::Host &amp;)&gt; worker) noexcept { @@ -64,6 +64,10 @@ static DWORD set(std::function&lt;void(TN3270::Host &amp;)&gt; worker) noexcept {
64 64
65 HLLAPI_API_CALL hllapi_pfkey(WORD key) { 65 HLLAPI_API_CALL hllapi_pfkey(WORD key) {
66 66
  67 +#ifdef DEBUG
  68 + clog << __FUNCTION__ << "(" << key << ")" << endl;
  69 +#endif // DEBUG
  70 +
67 return set([key](TN3270::Host &host) { 71 return set([key](TN3270::Host &host) {
68 72
69 host.pfkey((unsigned short) key); 73 host.pfkey((unsigned short) key);
src/core/private.h
@@ -36,6 +36,8 @@ @@ -36,6 +36,8 @@
36 #include <string> 36 #include <string>
37 #include <cerrno> 37 #include <cerrno>
38 #include <cstring> 38 #include <cstring>
  39 + #include <iostream>
  40 + #include <system_error>
39 #include <lib3270/ipc.h> 41 #include <lib3270/ipc.h>
40 #include <lib3270/log.h> 42 #include <lib3270/log.h>
41 #include <lib3270/hllapi.h> 43 #include <lib3270/hllapi.h>
@@ -44,10 +46,14 @@ @@ -44,10 +46,14 @@
44 using std::string; 46 using std::string;
45 using TN3270::Host; 47 using TN3270::Host;
46 using std::exception; 48 using std::exception;
  49 + using std::clog;
  50 + using std::endl;
47 51
48 extern TN3270_PRIVATE std::string hllapi_lasterror; 52 extern TN3270_PRIVATE std::string hllapi_lasterror;
49 53
50 TN3270_PRIVATE TN3270::Host & getSession(); 54 TN3270_PRIVATE TN3270::Host & getSession();
51 - TN3270_PRIVATE DWORD hllapi_translate_keyboard_state(LIB3270_KEYBOARD_LOCK_STATE state, HLLAPI_STATUS def = HLLAPI_STATUS_SYSTEM_ERROR); 55 +
  56 + TN3270_PRIVATE DWORD hllapi_translate_error(LIB3270_KEYBOARD_LOCK_STATE state);
  57 + TN3270_PRIVATE DWORD hllapi_translate_error(const std::system_error &error);
52 58
53 #endif // PRIVATE_H_INCLUDED 59 #endif // PRIVATE_H_INCLUDED
src/core/set.cc
@@ -45,6 +45,7 @@ @@ -45,6 +45,7 @@
45 TN3270::Host &host = getSession(); 45 TN3270::Host &host = getSession();
46 46
47 kLock = host.waitForKeyboardUnlock(); 47 kLock = host.waitForKeyboardUnlock();
  48 +
48 if(kLock == LIB3270_KL_UNLOCKED) { 49 if(kLock == LIB3270_KL_UNLOCKED) {
49 50
50 try { 51 try {
@@ -63,6 +64,9 @@ @@ -63,6 +64,9 @@
63 64
64 } 65 }
65 66
  67 + } catch(const std::system_error &e) {
  68 +
  69 + return hllapi_translate_error(e);
66 70
67 } catch(const std::exception &e) { 71 } catch(const std::exception &e) {
68 72
@@ -78,7 +82,7 @@ @@ -78,7 +82,7 @@
78 82
79 } 83 }
80 84
81 - return hllapi_translate_keyboard_state(kLock); 85 + return hllapi_translate_error(kLock);
82 86
83 } 87 }
84 88
@@ -102,6 +106,9 @@ @@ -102,6 +106,9 @@
102 if(!(text && *text)) 106 if(!(text && *text))
103 return HLLAPI_STATUS_BAD_PARAMETER; 107 return HLLAPI_STATUS_BAD_PARAMETER;
104 108
  109 + if(!length)
  110 + length = strlen(text);
  111 +
105 return set([text,length](TN3270::Host &host) { 112 return set([text,length](TN3270::Host &host) {
106 113
107 host.input((const char *) text, (int) length, '@'); 114 host.input((const char *) text, (int) length, '@');
src/core/tools.cc 0 → 100644
@@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple
  19 + * Place, Suite 330, Boston, MA, 02111-1307, USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + */
  29 +
  30 + #include "private.h"
  31 + #include <errno.h>
  32 +
  33 +/*--[ Implement ]------------------------------------------------------------------------------------*/
  34 +
  35 + HLLAPI_API_CALL hllapi_get_datadir(LPSTR datadir) {
  36 +
  37 + #ifdef _WIN32
  38 + HKEY hKey = 0;
  39 + unsigned long datalen = strlen(datadir);
  40 +
  41 + *datadir = 0;
  42 +
  43 + if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS)
  44 + {
  45 + unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
  46 + if(RegQueryValueExA(hKey,"datadir",NULL,&datatype,(LPBYTE) datadir,&datalen) != ERROR_SUCCESS)
  47 + *datadir = 0;
  48 + RegCloseKey(hKey);
  49 + }
  50 +#endif // _WIN32
  51 +
  52 + return *datadir;
  53 + }
  54 +
  55 + DWORD hllapi_translate_error(LIB3270_KEYBOARD_LOCK_STATE state) {
  56 +
  57 + // Is unlocked.
  58 + if(state == LIB3270_KL_UNLOCKED)
  59 + return HLLAPI_STATUS_SYSTEM_ERROR;
  60 +
  61 + // Is connected?
  62 + if((state & LIB3270_KL_NOT_CONNECTED) != 0)
  63 + return HLLAPI_STATUS_DISCONNECTED;
  64 +
  65 + if( (state & (LIB3270_KL_AWAITING_FIRST|LIB3270_KL_OIA_TWAIT)) != 0)
  66 + return HLLAPI_STATUS_WAITING;
  67 +
  68 + return HLLAPI_STATUS_KEYBOARD_LOCKED;
  69 +
  70 + }
  71 +
  72 + DWORD hllapi_translate_error(const std::system_error &error) {
  73 +
  74 + static const struct Translate {
  75 + int from;
  76 + HLLAPI_STATUS to;
  77 + } translate[] = {
  78 + { ENOTCONN, HLLAPI_STATUS_DISCONNECTED },
  79 + { ECONNRESET, HLLAPI_STATUS_DISCONNECTED },
  80 + { EHOSTDOWN, HLLAPI_STATUS_DISCONNECTED },
  81 + { EINVAL, HLLAPI_STATUS_BAD_PARAMETER },
  82 + { ETIMEDOUT, HLLAPI_STATUS_TIMEOUT },
  83 + { EBUSY, HLLAPI_STATUS_TIMEOUT },
  84 + { EPERM, HLLAPI_STATUS_KEYBOARD_LOCKED },
  85 + { EROFS, HLLAPI_STATUS_KEYBOARD_LOCKED },
  86 + { EOVERFLOW, HLLAPI_STATUS_BAD_POSITION },
  87 + };
  88 +
  89 + int value = error.code().value();
  90 +
  91 + hllapi_lasterror = error.what();
  92 +
  93 + hllapi_lasterror += " (rc=";
  94 + hllapi_lasterror += std::to_string(value);
  95 + hllapi_lasterror += ")";
  96 +
  97 + for(size_t ix = 0; ix < (sizeof(translate)/sizeof(struct Translate)); ix++) {
  98 +
  99 + if(translate[ix].from == value)
  100 + return translate[ix].to;
  101 +
  102 + }
  103 +
  104 + return HLLAPI_STATUS_SYSTEM_ERROR;
  105 +
  106 + }
  107 +
src/include/lib3270/hllapi.h
@@ -199,7 +199,6 @@ @@ -199,7 +199,6 @@
199 HLLAPI_API_CALL hllapi_get_state(void); 199 HLLAPI_API_CALL hllapi_get_state(void);
200 HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer); 200 HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer);
201 HLLAPI_API_CALL hllapi_get_screen(WORD pos, LPSTR buffer, WORD len); 201 HLLAPI_API_CALL hllapi_get_screen(WORD pos, LPSTR buffer, WORD len);
202 - HLLAPI_API_CALL hllapi_enter(void);  
203 HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text); 202 HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text);
204 HLLAPI_API_CALL hllapi_cmp_text_at(WORD row, WORD col, LPSTR text); 203 HLLAPI_API_CALL hllapi_cmp_text_at(WORD row, WORD col, LPSTR text);
205 HLLAPI_API_CALL hllapi_find_text(LPSTR text); 204 HLLAPI_API_CALL hllapi_find_text(LPSTR text);
@@ -213,6 +212,7 @@ @@ -213,6 +212,7 @@
213 212
214 HLLAPI_API_CALL hllapi_set_session_parameter(LPSTR param, WORD len, WORD value); 213 HLLAPI_API_CALL hllapi_set_session_parameter(LPSTR param, WORD len, WORD value);
215 214
  215 + HLLAPI_API_CALL hllapi_enter(void);
216 HLLAPI_API_CALL hllapi_erase(void); 216 HLLAPI_API_CALL hllapi_erase(void);
217 HLLAPI_API_CALL hllapi_erase_eof(void); 217 HLLAPI_API_CALL hllapi_erase_eof(void);
218 HLLAPI_API_CALL hllapi_erase_eol(void); 218 HLLAPI_API_CALL hllapi_erase_eol(void);
src/testprogram/testprogram.cc
@@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
30 #include <iostream> 30 #include <iostream>
31 #include <getopt.h> 31 #include <getopt.h>
32 #include <cstring> 32 #include <cstring>
  33 + #include <cstdio>
33 #include <lib3270/hllapi.h> 34 #include <lib3270/hllapi.h>
34 35
35 #define SCREEN_LENGTH 2000 36 #define SCREEN_LENGTH 2000
@@ -43,7 +44,7 @@ @@ -43,7 +44,7 @@
43 char buffer[SCREEN_LENGTH+1]; 44 char buffer[SCREEN_LENGTH+1];
44 45
45 const char *host = ""; 46 const char *host = "";
46 - const char *session = ""; // "pw3270:A"; 47 + const char *session = "pw3270:a";
47 48
48 #pragma GCC diagnostic push 49 #pragma GCC diagnostic push
49 static struct option options[] = 50 static struct option options[] =
@@ -92,11 +93,12 @@ @@ -92,11 +93,12 @@
92 memset(buffer,' ',SCREEN_LENGTH); 93 memset(buffer,' ',SCREEN_LENGTH);
93 buffer[SCREEN_LENGTH] = 0; 94 buffer[SCREEN_LENGTH] = 0;
94 95
  96 + rc = HLLAPI_STATUS_SUCCESS;
  97 +
95 if(strcasecmp(cmdline.c_str(),"connect") == 0) { 98 if(strcasecmp(cmdline.c_str(),"connect") == 0) {
96 99
97 cout << "Connecting..." << endl; 100 cout << "Connecting..." << endl;
98 rc = hllapi_connect((LPSTR) host,1); 101 rc = hllapi_connect((LPSTR) host,1);
99 - cout << "hllapi_connect returns with rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl;  
100 102
101 } else if(strcasecmp(cmdline.c_str(),"wait") == 0) { 103 } else if(strcasecmp(cmdline.c_str(),"wait") == 0) {
102 104
@@ -107,15 +109,12 @@ @@ -107,15 +109,12 @@
107 109
108 cout << "Disconnecting..." << endl; 110 cout << "Disconnecting..." << endl;
109 rc = hllapi_disconnect(); 111 rc = hllapi_disconnect();
110 - cout << "hllapi_disconnect returns with rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl;  
111 112
112 } else if(strcasecmp(cmdline.c_str(),"luname") == 0) { 113 } else if(strcasecmp(cmdline.c_str(),"luname") == 0) {
113 114
114 rc = hllapi_get_lu_name(buffer,SCREEN_LENGTH); 115 rc = hllapi_get_lu_name(buffer,SCREEN_LENGTH);
115 if(rc == HLLAPI_STATUS_SUCCESS) { 116 if(rc == HLLAPI_STATUS_SUCCESS) {
116 cout << "LU Name is " << buffer << endl; 117 cout << "LU Name is " << buffer << endl;
117 - } else {  
118 - cout << "hllapi_get_lu_name returns with rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl;  
119 } 118 }
120 119
121 } else if(strcasecmp(cmdline.c_str(),"contents") == 0) { 120 } else if(strcasecmp(cmdline.c_str(),"contents") == 0) {
@@ -124,11 +123,84 @@ @@ -124,11 +123,84 @@
124 cout << "hllapi_get_screen returns with rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl 123 cout << "hllapi_get_screen returns with rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl
125 << buffer << endl << endl; 124 << buffer << endl << endl;
126 125
  126 + } else if(strncasecmp(cmdline.c_str(),"cursor",6) == 0) {
  127 +
  128 + unsigned int row, col;
  129 +
  130 + switch(std::sscanf(cmdline.c_str()+6,"%u,%u",&row,&col)) {
  131 + case 1:
  132 + cout << "Moving cursor to " << row << endl;
  133 + rc = hllapi_set_cursor_address((WORD) row);
  134 + break;
  135 +
  136 + case 2:
  137 + cout << "Moving cursor to " << row << "," << col << endl;
  138 + rc = hllapi_set_cursor_position((WORD) row, (WORD) col);
  139 + break;
  140 + }
  141 +
  142 + } else if(strncasecmp(cmdline.c_str(),"at",2) == 0) {
  143 +
  144 + unsigned int row, col, len;
  145 +
  146 + switch(std::sscanf(cmdline.c_str()+3,"%u,%u,%u",&row,&col,&len)) {
  147 + case 3:
  148 + {
  149 + cout << "Getting " << len << " bytes from " << row << "," << col << endl;
  150 +
  151 + char * buffer = new char[len+1];
  152 + memset(buffer,' ',len+1);
  153 + buffer[len] = 0;
  154 +
  155 + rc = hllapi_get_screen_at(row,col,buffer);
  156 + if(rc == HLLAPI_STATUS_SUCCESS) {
  157 + cout << "Contents:" << endl << buffer << endl;
  158 + }
  159 +
  160 + delete[] buffer;
  161 + }
  162 + break;
  163 +
  164 + case 2:
  165 + {
  166 + cout << "Getting " << col << " bytes from " << row << endl;
  167 +
  168 + char * buffer = new char[col+1];
  169 + memset(buffer,' ',col+1);
  170 + buffer[col] = 0;
  171 +
  172 + rc = hllapi_get_screen(row,buffer,col);
  173 + if(rc == HLLAPI_STATUS_SUCCESS) {
  174 + cout << "Contents:" << endl << buffer << endl;
  175 + }
  176 +
  177 + delete[] buffer;
  178 +
  179 + }
  180 + break;
  181 + }
  182 +
  183 + } else if(strncasecmp(cmdline.c_str(),"enter",5) == 0) {
  184 +
  185 + rc = hllapi_enter();
  186 +
  187 + } else if(strncasecmp(cmdline.c_str(),"pf ",3) == 0) {
  188 +
  189 + rc = hllapi_pfkey(atoi(cmdline.c_str()+3));
  190 +
  191 + } else if(strncasecmp(cmdline.c_str(),"input ",6) == 0) {
  192 +
  193 + rc = hllapi_input_string((LPSTR) cmdline.c_str()+6,0);
  194 +
127 } else { 195 } else {
128 196
129 - cout << "Unknown command" << endl; 197 + cout << "Unknown command \"" << cmdline << "\""<< endl;
130 } 198 }
131 199
  200 + if(rc != HLLAPI_STATUS_SUCCESS) {
  201 + cout << "rc=" << rc << " (" << hllapi_get_last_error() << ")" << endl;
  202 + rc = HLLAPI_STATUS_SUCCESS;
  203 + }
132 204
133 cout << endl << ">"; 205 cout << endl << ">";
134 cout.flush(); 206 cout.flush();