Commit 981631c39e8892578ca3ae874795c11dab4fbeff
1 parent
30919320
Exists in
master
and in
5 other branches
Corrigindo segfaults em chamadas HLLAPI.
Showing
8 changed files
with
86 additions
and
23 deletions
Show diff stats
src/classlib/remote.cc
| ... | ... | @@ -417,6 +417,8 @@ |
| 417 | 417 | HKEY hKey = 0; |
| 418 | 418 | unsigned long datalen = sizeof(buffer); |
| 419 | 419 | |
| 420 | + debug("%s(%s)",__FUNCTION__,name); | |
| 421 | + | |
| 420 | 422 | *buffer = 0; |
| 421 | 423 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS) |
| 422 | 424 | { |
| ... | ... | @@ -448,6 +450,8 @@ |
| 448 | 450 | |
| 449 | 451 | hPipe = INVALID_HANDLE_VALUE; |
| 450 | 452 | |
| 453 | + trace("%s(%s)",__FUNCTION__,session); | |
| 454 | + | |
| 451 | 455 | if(strcasecmp(session,"start") == 0 || strcasecmp(session,"new") == 0) |
| 452 | 456 | { |
| 453 | 457 | // Start a new session |
| ... | ... | @@ -509,7 +513,16 @@ |
| 509 | 513 | } |
| 510 | 514 | |
| 511 | 515 | // Wait? |
| 512 | - int delay = atoi(getRegistryKey("hllapiWait").c_str()); | |
| 516 | + int delay; | |
| 517 | + | |
| 518 | + try | |
| 519 | + { | |
| 520 | + delay = atoi(getRegistryKey("hllapiWait").c_str()); | |
| 521 | + } | |
| 522 | + catch(std::exception &e) | |
| 523 | + { | |
| 524 | + delay = 0; | |
| 525 | + } | |
| 513 | 526 | |
| 514 | 527 | if(delay) { |
| 515 | 528 | timer = time(0) + delay; | ... | ... |
src/classlib/session.cc
| ... | ... | @@ -38,20 +38,25 @@ |
| 38 | 38 | #include <unistd.h> |
| 39 | 39 | #endif // !WIN32 |
| 40 | 40 | |
| 41 | - #ifdef HAVE_SYSLOG | |
| 41 | +#ifdef HAVE_SYSLOG | |
| 42 | 42 | #include <syslog.h> |
| 43 | - #endif // HAVE_SYSLOG | |
| 43 | +#endif // HAVE_SYSLOG | |
| 44 | + | |
| 45 | +#if __cplusplus < 201103L | |
| 46 | + #define nullptr NULL | |
| 47 | +#endif // !c11 | |
| 44 | 48 | |
| 45 | 49 | /*--[ Implement ]--------------------------------------------------------------------------------------------------*/ |
| 46 | 50 | |
| 47 | 51 | namespace PW3270_NAMESPACE |
| 48 | 52 | { |
| 49 | - session * session::first = 0; | |
| 50 | - session * session::last = 0; | |
| 51 | - session * (*session::factory)(const char *name) = 0; | |
| 53 | + session * session::first = nullptr; | |
| 54 | + session * session::last = nullptr; | |
| 55 | + session * (*session::factory)(const char *name) = nullptr; | |
| 52 | 56 | |
| 53 | 57 | session::session() |
| 54 | 58 | { |
| 59 | + | |
| 55 | 60 | #ifdef HAVE_ICONV |
| 56 | 61 | this->conv2Local = (iconv_t) (-1); |
| 57 | 62 | this->conv2Host = (iconv_t) (-1); |
| ... | ... | @@ -98,6 +103,8 @@ |
| 98 | 103 | // Factory methods and settings |
| 99 | 104 | session * session::create(const char *name) throw (std::exception) |
| 100 | 105 | { |
| 106 | + trace("%s(%s)",__FUNCTION__,name); | |
| 107 | + | |
| 101 | 108 | if(factory) |
| 102 | 109 | return factory(name); |
| 103 | 110 | |
| ... | ... | @@ -112,6 +119,11 @@ |
| 112 | 119 | return create(name); |
| 113 | 120 | } |
| 114 | 121 | |
| 122 | + bool session::has_default(void) | |
| 123 | + { | |
| 124 | + return first != nullptr; | |
| 125 | + } | |
| 126 | + | |
| 115 | 127 | session * session::get_default(void) |
| 116 | 128 | { |
| 117 | 129 | if(first) | ... | ... |
src/include/pw3270/class.h
| ... | ... | @@ -132,6 +132,7 @@ |
| 132 | 132 | static session * start(const char *name = 0); |
| 133 | 133 | static session * create(const char *name = 0) throw (std::exception); |
| 134 | 134 | static session * get_default(void); |
| 135 | + static bool has_default(void); | |
| 135 | 136 | static void set_plugin(session * (*factory)(const char *name)); |
| 136 | 137 | |
| 137 | 138 | // Log management | ... | ... |
src/plugins/hllapi/Makefile.in
| ... | ... | @@ -44,11 +44,12 @@ include $(CLASSLIBDIR)/class.mak |
| 44 | 44 | #---[ Release Targets ]-------------------------------------------------------- |
| 45 | 45 | |
| 46 | 46 | $(BINRLS)/$(PLUGIN_NAME): \ |
| 47 | - $(foreach SRC, $(basename $(PLUGIN_SRC)), $(OBJRLS)/$(SRC).o) $(BINRLS)$(DLL_NAME) | |
| 47 | + $(foreach SRC, $(basename $(PLUGIN_SRC)), $(OBJRLS)/$(SRC).o) \ | |
| 48 | + $(BINRLS)$(DLL_NAME) | |
| 48 | 49 | |
| 49 | 50 | @echo " CCLD `basename $@`" |
| 50 | 51 | @$(MKDIR) `dirname $@` |
| 51 | - @$(CXX) $(DLL_FLAGS) @LDSOFLAGS@ $(LDFLAGS) @RLS_LDFLAGS@ -o $@ $^ $(LIBS) $(LIB3270_LIBS) $(CLASS_LIBS) | |
| 52 | + @$(CXX) $(DLL_FLAGS) @LDSOFLAGS@ $(LDFLAGS) @RLS_LDFLAGS@ -o $@ $^ $(LIBS) $(LIB3270_LIBS) $(GTK_LIBS) $(GLIB_LIBS) $(PW3270_LIBS) | |
| 52 | 53 | |
| 53 | 54 | $(BINRLS)$(DLL_NAME).$(VERSION): \ |
| 54 | 55 | $(foreach SRC, $(basename $(EXTAPI_SRC)), $(OBJRLS)/$(SRC).o) $(CLASS_RELEASE_OBJECTS) |
| ... | ... | @@ -62,11 +63,12 @@ install: |
| 62 | 63 | #---[ Debug Targets ]---------------------------------------------------------- |
| 63 | 64 | |
| 64 | 65 | $(BINDBG)/$(PLUGIN_NAME): \ |
| 65 | - $(foreach SRC, $(basename $(PLUGIN_SRC)), $(OBJDBG)/$(SRC).o) $(BINDBG)$(DLL_NAME) | |
| 66 | + $(foreach SRC, $(basename $(PLUGIN_SRC)), $(OBJDBG)/$(SRC).o) \ | |
| 67 | + $(BINDBG)$(DLL_NAME) | |
| 66 | 68 | |
| 67 | 69 | @echo " CCLD `basename $@`" |
| 68 | 70 | @$(MKDIR) `dirname $@` |
| 69 | - @$(CXX) $(DLL_FLAGS) @LDSOFLAGS@ @DBGRPATH@ $(LDFLAGS) -o $@ $^ $(LIBS) $(LIB3270_LIBS) $(CLASS_LIBS) | |
| 71 | + @$(CXX) $(DLL_FLAGS) @LDSOFLAGS@ @DBGRPATH@ $(LDFLAGS) -o $@ $^ $(LIBS) $(LIB3270_LIBS) $(GTK_LIBS) $(GLIB_LIBS) $(PW3270_LIBS) | |
| 70 | 72 | |
| 71 | 73 | $(BINDBG)$(DLL_NAME).$(VERSION): \ |
| 72 | 74 | $(foreach SRC, $(basename $(EXTAPI_SRC)), $(OBJDBG)/$(SRC)@OBJEXT@) $(CLASS_DEBUG_OBJECTS) | ... | ... |
src/plugins/hllapi/calls.cc
| ... | ... | @@ -114,6 +114,11 @@ |
| 114 | 114 | |
| 115 | 115 | HLLAPI_API_CALL hllapi_is_connected(void) |
| 116 | 116 | { |
| 117 | + if(!session::has_default()) | |
| 118 | + { | |
| 119 | + return 0; | |
| 120 | + } | |
| 121 | + | |
| 117 | 122 | return session::get_default()->is_connected(); |
| 118 | 123 | } |
| 119 | 124 | |
| ... | ... | @@ -255,7 +260,13 @@ |
| 255 | 260 | |
| 256 | 261 | HLLAPI_API_CALL hllapi_setcursor(WORD pos) |
| 257 | 262 | { |
| 258 | - return session::get_default()->set_cursor_addr(pos-1); | |
| 263 | + if(!hllapi_is_connected()) | |
| 264 | + return HLLAPI_STATUS_DISCONNECTED; | |
| 265 | + | |
| 266 | + session::get_default()->set_cursor_addr(pos-1); | |
| 267 | + | |
| 268 | + return HLLAPI_STATUS_SUCCESS; | |
| 269 | + | |
| 259 | 270 | } |
| 260 | 271 | |
| 261 | 272 | HLLAPI_API_CALL hllapi_getcursor() |
| ... | ... | @@ -267,15 +278,28 @@ |
| 267 | 278 | { |
| 268 | 279 | int rc = HLLAPI_STATUS_SYSTEM_ERROR; |
| 269 | 280 | |
| 281 | + if(!session::has_default()) | |
| 282 | + { | |
| 283 | + return HLLAPI_STATUS_DISCONNECTED; | |
| 284 | + } | |
| 285 | + | |
| 270 | 286 | if(!(buffer && *buffer)) |
| 271 | 287 | return rc; |
| 272 | 288 | |
| 273 | 289 | try |
| 274 | 290 | { |
| 275 | - size_t szBuffer = strlen(buffer); | |
| 291 | + size_t szBuffer; | |
| 292 | + | |
| 293 | + if(len > 0) | |
| 294 | + { | |
| 295 | + szBuffer = (size_t) len; | |
| 296 | + } | |
| 297 | + else | |
| 298 | + { | |
| 299 | + return HLLAPI_STATUS_BAD_PARAMETER; | |
| 300 | + } | |
| 276 | 301 | |
| 277 | - if(len < szBuffer && len > 0) | |
| 278 | - szBuffer = len; | |
| 302 | + memset(buffer,' ',szBuffer); | |
| 279 | 303 | |
| 280 | 304 | string str = session::get_default()->get_string(offset,szBuffer); |
| 281 | 305 | strncpy(buffer,str.c_str(),szBuffer); | ... | ... |
src/plugins/hllapi/hllapi.c
| ... | ... | @@ -37,10 +37,7 @@ |
| 37 | 37 | #include <lib3270/log.h> |
| 38 | 38 | #include "client.h" |
| 39 | 39 | |
| 40 | - #undef trace | |
| 41 | - #define trace( fmt, ... ) { FILE *out = fopen("c:\\Users\\Perry\\hllapi.log","a"); if(out) { fprintf(out, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fclose(out); } } | |
| 42 | - | |
| 43 | -/*--[ Prototipes ]-----------------------------------------------------------------------------------*/ | |
| 40 | + /*--[ Prototipes ]-----------------------------------------------------------------------------------*/ | |
| 44 | 41 | |
| 45 | 42 | static int connect_ps(char *buffer, unsigned short *length, unsigned short *rc); |
| 46 | 43 | static int disconnect_ps(char *buffer, unsigned short *length, unsigned short *rc); |
| ... | ... | @@ -177,9 +174,12 @@ static int set_cursor_position(char *buffer, unsigned short *length, unsigned sh |
| 177 | 174 | |
| 178 | 175 | static int copy_ps_to_str(char *buffer, unsigned short *length, unsigned short *rc) |
| 179 | 176 | { |
| 177 | + | |
| 180 | 178 | // Length Length of the target data string. |
| 181 | 179 | // PS Position Position within the host presentation space of the first byte in your target data string. |
| 182 | - return hllapi_get_screen(*rc,buffer,*length); | |
| 180 | + *rc = hllapi_get_screen(*rc,buffer,*length); | |
| 181 | + | |
| 182 | + return 0; | |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | static int input_string(char *input, unsigned short *length, unsigned short *rc) |
| ... | ... | @@ -187,6 +187,11 @@ static int input_string(char *input, unsigned short *length, unsigned short *rc) |
| 187 | 187 | size_t szText; |
| 188 | 188 | char * text; |
| 189 | 189 | |
| 190 | + if(!hllapi_is_connected()) { | |
| 191 | + *rc = HLLAPI_STATUS_DISCONNECTED; | |
| 192 | + return HLLAPI_STATUS_DISCONNECTED; | |
| 193 | + } | |
| 194 | + | |
| 190 | 195 | if(!input) |
| 191 | 196 | { |
| 192 | 197 | *rc = HLLAPI_STATUS_BAD_PARAMETER; | ... | ... |
src/plugins/hllapi/hllapi.cbp
| ... | ... | @@ -68,6 +68,7 @@ |
| 68 | 68 | <Unit filename="../../classlib/remote.cc" /> |
| 69 | 69 | <Unit filename="../../classlib/session.cc" /> |
| 70 | 70 | <Unit filename="../../include/lib3270.h" /> |
| 71 | + <Unit filename="../../include/pw3270/class.h" /> | |
| 71 | 72 | <Unit filename="../../include/pw3270/hllapi.h" /> |
| 72 | 73 | <Unit filename="Makefile.in" /> |
| 73 | 74 | <Unit filename="calls.cc" /> | ... | ... |
src/plugins/hllapi/testprogram.c
| ... | ... | @@ -32,13 +32,18 @@ |
| 32 | 32 | #include <time.h> |
| 33 | 33 | #include <pw3270/hllapi.h> |
| 34 | 34 | |
| 35 | + #if defined(DEBUG) && defined(_WIN32) | |
| 36 | + #undef trace | |
| 37 | + #define trace( fmt, ... ) { FILE *out = fopen("c:\\Users\\Perry\\hllapi.log","a"); if(out) { fprintf(out, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fclose(out); } } | |
| 38 | + #endif // DEBUG | |
| 39 | + | |
| 35 | 40 | #define MAX_DATA_SIZE 8000 //< Maximum data size for this application. |
| 36 | 41 | |
| 37 | 42 | static CHAR hllapi_data[MAX_DATA_SIZE]; |
| 38 | 43 | |
| 39 | 44 | /*---[ Implement ]--------------------------------------------------------------------------------*/ |
| 40 | 45 | |
| 41 | - static void connect(const char *session) | |
| 46 | + static void connect_ps(const char *session) | |
| 42 | 47 | { |
| 43 | 48 | WORD len; |
| 44 | 49 | WORD fn = HLLAPI_CMD_CONNECTPS; |
| ... | ... | @@ -51,16 +56,16 @@ |
| 51 | 56 | |
| 52 | 57 | if(rc) |
| 53 | 58 | { |
| 54 | - fprintf(stderr,"HLLAPI_CMD_CONNECTPS exits with rc=%d",(int) rc); | |
| 59 | + trace("HLLAPI_CMD_CONNECTPS(%s) exits with rc=%d", session, (int) rc); | |
| 55 | 60 | } |
| 56 | 61 | |
| 57 | 62 | } |
| 58 | 63 | |
| 59 | 64 | int main(int numpar, char *param[]) |
| 60 | 65 | { |
| 61 | - const char *session = "pw3270:a"; | |
| 66 | + const char *session = "pw3270:A"; | |
| 62 | 67 | |
| 63 | - connect(session); | |
| 68 | + connect_ps(session); | |
| 64 | 69 | |
| 65 | 70 | /* |
| 66 | 71 | ... | ... |