hllapi.cc
14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA, 02111-1307, USA
*
* Este programa está nomeado como hllapi.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
#if defined(_MSC_VER)
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
#include "private.h"
#include <lib3270/hllapi.h>
/*--[ Prototipes ]-----------------------------------------------------------------------------------*/
static int connect_ps(char *buffer, unsigned short *length, unsigned short *rc);
static int disconnect_ps(char *buffer, unsigned short *length, unsigned short *rc);
static int get_library_revision(char *buffer, unsigned short *length, unsigned short *rc);
static int copy_ps_to_str(char *buffer, unsigned short *length, unsigned short *rc);
static int copy_str_to_ps(char *buffer, unsigned short *length, unsigned short *rc);
static int search_ps(char *buffer, unsigned short *length, unsigned short *rc);
static int copy_ps(char *buffer, unsigned short *length, unsigned short *rc);
static int wait_system(char *buffer, unsigned short *length, unsigned short *rc);
static int reset_system(char *buffer, unsigned short *length, unsigned short *rc);
static int pause_system(char *buffer, unsigned short *length, unsigned short *rc);
static int set_session_parameters(char *buffer, unsigned short *length, unsigned short *rc);
static int get_cursor_position(char *buffer, unsigned short *length, unsigned short *rc);
static int set_cursor_position(char *buffer, unsigned short *length, unsigned short *rc);
static int input_string(char *buffer, unsigned short *length, unsigned short *rc);
static int invalid_request(char *buffer, unsigned short *length, unsigned short *rc);
/*--[ Globals ]--------------------------------------------------------------------------------------*/
static const struct _hllapi_call
{
unsigned long func;
int (*exec)(char *buffer, unsigned short *length, unsigned short *rc);
} hllapi_call[] =
{
{ HLLAPI_CMD_CONNECTPS, connect_ps },
{ HLLAPI_CMD_DISCONNECTPS, disconnect_ps },
{ HLLAPI_CMD_GETREVISION, get_library_revision },
{ HLLAPI_CMD_QUERYCURSOR, get_cursor_position },
{ HLLAPI_CMD_SETCURSOR, set_cursor_position },
{ HLLAPI_CMD_COPYPSTOSTR, copy_ps_to_str },
{ HLLAPI_CMD_INPUTSTRING, input_string },
{ HLLAPI_CMD_WAIT, wait_system },
{ HLLAPI_CMD_COPYPS, copy_ps },
{ HLLAPI_CMD_SEARCHPS, search_ps },
{ HLLAPI_CMD_COPYSTRTOPS, copy_str_to_ps },
{ HLLAPI_CMD_SENDFILE, invalid_request },
{ HLLAPI_CMD_RECEIVEFILE, invalid_request },
{ HLLAPI_RESET_SYSTEM, reset_system },
{ HLLAPI_CMD_PAUSE, pause_system },
{ HLLAPI_SET_SESSION_PARAMETERS, set_session_parameters }
};
static enum _pause_mode
{
PAUSE_MODE_IPAUSE, ///< @brief Interruptible pause. After the Start Host Notification (23) function is executed, a host event satisfies a pause.
PAUSE_MODE_FPAUSE ///< @brief A full-duration pause lasts for however long you specified in the Pause (18) function.
} pause_mode = PAUSE_MODE_IPAUSE;
/*--[ Implement ]------------------------------------------------------------------------------------*/
HLLAPI_API_CALL hllapi(const LPWORD func, LPSTR buffer, LPWORD length, LPWORD rc) {
for(unsigned int f=0;f < (sizeof (hllapi_call) / sizeof ((hllapi_call)[0]));f++) {
if(hllapi_call[f].func == *func) {
try {
return hllapi_call[f].exec(buffer,length,rc);
} catch(std::exception &e) {
hllapi_lasterror = e.what();
*rc = HLLAPI_STATUS_SYSTEM_ERROR;
return *rc;
}
}
}
trace("hllapi(%d) failed",*func);
*rc = HLLAPI_STATUS_UNSUPPORTED;
return *rc;
}
static int invalid_request(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
*rc = HLLAPI_STATUS_BAD_PARAMETER;
return *rc;
}
static int connect_ps(char *buffer, unsigned short *length, unsigned short *rc) {
char *tempbuffer = NULL;
trace("%s: len=%d buflen=%d",__FUNCTION__,*length,(int) strlen(buffer));
if(strlen(buffer) > *length)
buffer[*length] = 0;
if(!strrchr(buffer,':'))
{
int sz = strlen(buffer);
tempbuffer = (char *) malloc(sz+2);
strcpy(tempbuffer,buffer);
tempbuffer[sz-1] = ':';
tempbuffer[sz] = buffer[sz-1];
tempbuffer[sz+1] = 0;
buffer = tempbuffer;
}
*rc = hllapi_init(buffer);
if(tempbuffer)
free(tempbuffer);
return HLLAPI_STATUS_SUCCESS;
}
static int disconnect_ps(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
*rc = hllapi_deinit();
return HLLAPI_STATUS_SUCCESS;
}
static int get_library_revision(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
*rc = hllapi_get_revision();
return HLLAPI_STATUS_SUCCESS;
}
static int get_cursor_position(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
try {
TN3270::Host &host = getSession();
if(!host.isConnected())
return HLLAPI_STATUS_DISCONNECTED;
*rc = (unsigned short) (host.getCursorAddress()+1);
} catch(std::exception &e) {
hllapi_lasterror = e.what();
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return HLLAPI_STATUS_SUCCESS;
}
static int set_cursor_position(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
try {
TN3270::Host &host = getSession();
if(!host.isConnected()) {
hllapi_lasterror = _( "Disconnected from host" );
return HLLAPI_STATUS_DISCONNECTED;
}
host.setCursor((unsigned short) *rc -1);
return HLLAPI_STATUS_SUCCESS;
} catch(std::exception &e) {
hllapi_lasterror = e.what();
}
return HLLAPI_STATUS_SYSTEM_ERROR;
}
static int copy_ps_to_str(char *buffer, unsigned short *length, unsigned short *rc) {
// Length Length of the target data string.
// PS Position Position within the host presentation space of the first byte in your target data string.
*rc = hllapi_get_screen(*rc,buffer,*length);
return HLLAPI_STATUS_SUCCESS;
}
static int input_string(char *buffer, unsigned short *length, unsigned short *rc) {
*rc = hllapi_input_string(buffer,*length);
return HLLAPI_STATUS_SUCCESS;
}
static int search_ps(char *buffer, unsigned short *length, unsigned short *ps) {
//
// Data String Target string for search.
// Length Length of the target data string. Overridden in EOT mode.
//
// PS Position Position within the host presentation space where the search is to begin (SRCHFRWD option) or to end
// (SRCHBKWD option). Overridden in SRCHALL (default) mode.
//
// Return in *ps:
//
// = 0 The string was not found.
// > 0 The string was found at the indicated host presentation space position.
//
// Return code:
//
// 0 HLLAPI_STATUS_SUCCESS The Search Presentation Space function was successful.
// 1 HLLAPI_STATUS_DISCONNECTED Your program is not connected to a host session.
// 2 HLLAPI_STATUS_BAD_PARAMETER An error was made in specifying parameters.
// 7 HLLAPI_STATUS_BAD_POSITION The host presentation space position is not valid.
// 9 HLLAPI_STATUS_SYSTEM_ERROR A system error was encountered.
// 24 HLLAPI_STATUS_NOT_FOUND The search string was not found.
//
//
try {
size_t pos = string::npos;
if(length) {
pos = getSession().find(string((const char *) buffer,(size_t) length).c_str());
} else {
pos = getSession().find(buffer);
}
if(pos == string::npos) {
*ps = 0;
return HLLAPI_STATUS_NOT_FOUND;
}
*ps = pos;
return HLLAPI_STATUS_SUCCESS;
} catch(const std::system_error &e) {
return hllapi_translate_error(e);
} catch(std::exception &e) {
hllapi_lasterror = e.what();
}
return HLLAPI_STATUS_SYSTEM_ERROR;
}
static int copy_ps(char *buffer, unsigned short *length, unsigned short GNUC_UNUSED(*rc)) {
//
// Data String Preallocated target string the size of your host presentation space. This can vary depending on how your host presentation space
// is configured. When the Set Session Parameters (9) function with the EAB option is issued, the length of the data string must be
// at least twice the length of the presentation space.
// DBCS Only: When the EAD option is specified, the length of the data string must be at least three times the length of the
// presentation space. When both the EAB and EAD options are specified, the length of the data string must be at least four times
// the length of the presentation space.
//
// Length NA (the length of the host presentation space is implied).
// PS Position NA.
//
// Return values:
//
// 0 HLLAPI_STATUS_SUCCESS The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.
// 1 HLLAPI_STATUS_DISCONNECTED Your program is not connected to a host session.
// 4 HLLAPI_STATUS_TIMEOUT The host presentation space contents were copied. The connected host presentation space was waiting for host response.
// 5 HLLAPI_STATUS_KEYBOARD_LOCKED The host presentation space was copied. The keyboard was locked.
// 9 HLLAPI_STATUS_SYSTEM_ERROR A system error was encountered.
//
//
try {
TN3270::Host &host = getSession();
if(!host.isConnected()) {
hllapi_lasterror = _( "Disconnected from host" );
return HLLAPI_STATUS_DISCONNECTED;
}
string contents = host.toString(0,-1,'\0');
size_t szBuffer = min(contents.size(), ((size_t) *length));
*length = (unsigned short) szBuffer;
strncpy(buffer,contents.c_str(),szBuffer);
return hllapi_get_state();
} catch(std::exception &e) {
hllapi_lasterror = e.what();
}
return HLLAPI_STATUS_SYSTEM_ERROR;
}
static int wait_system(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
//
// Checks the status of the host-connected presentation space. If the session is
// waiting for a host response (indicated by XCLOCK (X []) or XSYSTEM), the Wait
// function causes HLLAPI to wait up to 1 minute to see if the condition clears.
//
//
//
// Return Code Definition
//
// HLLAPI_STATUS_SUCCESS 0 The keyboard is unlocked and ready for input.
// HLLAPI_STATUS_DISCONNECTED 1 Your application program is not connected to a valid session.
// HLLAPI_STATUS_TIMEOUT 4 Timeout while still in XCLOCK (X []) or XSYSTEM.
// HLLAPI_STATUS_KEYBOARD_LOCKED 5 The keyboard is locked.
// HLLAPI_STATUS_SYSTEM_ERROR 9 A system error was encountered.
//
//
int state = hllapi_wait_for_ready(60);
*rc = (unsigned short) state;
return (state == HLLAPI_STATUS_WAITING ? HLLAPI_STATUS_TIMEOUT : state);
}
static int copy_str_to_ps(char *text, unsigned short *length, unsigned short *ps) {
//
// Call Parameters
//
// Data String of ASCII data to be copied into the host presentation space.
// Length Length, in number of bytes, of the source data string. Overridden if in EOT mode.
// PS Position in the host presentation space to begin the copy, a value between 1 and the configured size of your host presentation space.
//
// Return Parameters
//
// HLLAPI_STATUS_SUCCESS 0 The Copy String to Presentation Space function was successful.
// HLLAPI_STATUS_DISCONNECTED 1 Your program is not connected to a host session.
// HLLAPI_STATUS_BAD_PARAMETER 2 Parameter error or zero length for copy.
// HLLAPI_STATUS_KEYBOARD_LOCKED 5 The target presentation space is protected or inhibited, or incorrect data was sent to the target presentation space (such as a field attribute byte).
// 6 The copy was completed, but the data was truncated.
// 7 The host presentation space position is not valid.
// HLLAPI_STATUS_SYSTEM_ERROR 9 A system error was encountered.
//
//
*ps = 0;
return hllapi_emulate_input(text,*length,0);
}
static int reset_system(char GNUC_UNUSED(*buffer), unsigned short GNUC_UNUSED(*length), unsigned short *rc) {
int state = hllapi_kybdreset();
*rc = (unsigned short) state;
return state;
}
static int pause_system(char GNUC_UNUSED(*buffer), unsigned short *length, unsigned short *rc) {
if(!*length)
{
// If you use the IPAUSE option and the pause value is zero, then the function
// waits up to 2400 half-second intervals, unless interrupted sooner. If you use the
// FPAUSE option and the pause value is zero, then the function returns
// immediately.
if(pause_mode == PAUSE_MODE_FPAUSE)
{
return HLLAPI_STATUS_SUCCESS;
}
return hllapi_wait_for_ready(1200);
}
if(pause_mode == PAUSE_MODE_FPAUSE)
{
// Pause fixo - Aguarda pelo tempo informado, independente de eventos.
return hllapi_wait( (*length) / 2);
}
// Pause "flexivel", aguarda mudança no conteúdo da tela!!!
int state = hllapi_wait_for_change((*length) / 2);
*rc = (unsigned short) state;
return state;
}
static int set_session_parameters(char *buffer, unsigned short *length, unsigned short *rc)
{
if(!(*length && buffer && *buffer))
{
return HLLAPI_STATUS_BAD_PARAMETER;
}
*rc = hllapi_set_session_parameter(buffer, *length, *rc);
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_set_session_parameter(LPSTR param, WORD len, WORD value)
{
if(!param)
return HLLAPI_STATUS_BAD_PARAMETER;
if(!len)
len = strlen(param);
if(!strncasecmp(param,"IPAUSE",len)) {
// IPAUSE
pause_mode = PAUSE_MODE_IPAUSE;
} else if(!strncasecmp(param,"FPAUSE",len)) {
// FPAUSE
pause_mode = PAUSE_MODE_FPAUSE;
} else if(!strncasecmp(param,"UNLOCKDELAY",len)) {
// UNLOCKDELAY
hllapi_set_unlock_delay(value);
} else if(!strncasecmp(param,"TIMEOUT",len)) {
// UNLOCKDELAY
hllapi_set_timeout(value);
} else {
return HLLAPI_STATUS_BAD_PARAMETER;
}
return HLLAPI_STATUS_SUCCESS;
}