hllapi.c
12.5 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
475
476
477
478
479
480
/*
* "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)
*
*/
#include <lib3270.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include <pw3270/hllapi.h>
#include <stdio.h>
#include <time.h>
#include <lib3270/log.h>
#include "client.h"
#undef trace
#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); } }
/*--[ 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 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 },
};
static const char control_char = '@';
/*--[ Implement ]------------------------------------------------------------------------------------*/
HLLAPI_API_CALL hllapi(LPWORD func, LPSTR buffer, LPWORD length, LPWORD rc)
{
unsigned int f;
trace("%s(%d)",__FUNCTION__,*func);
for(f=0;f< (sizeof (hllapi_call) / sizeof ((hllapi_call)[0]));f++)
{
if(hllapi_call[f].func == *func)
{
int status = hllapi_call[f].exec(buffer,length,rc);
trace("hllapi(%d) exits with rc=%d",*func,status);
return status;
}
}
trace("hllapi(%d) failed",*func);
*rc = HLLAPI_STATUS_UNSUPPORTED;
return *rc;
}
static int invalid_request(char *buffer, unsigned short *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;
}
if(hllapi_init(buffer) == 0)
*rc = HLLAPI_STATUS_SUCCESS;
else
*rc = HLLAPI_STATUS_UNAVAILABLE;
if(tempbuffer)
free(tempbuffer);
return 0;
}
static int disconnect_ps(char *buffer, unsigned short *length, unsigned short *rc)
{
*rc = hllapi_deinit();
return 0;
}
static int get_library_revision(char *buffer, unsigned short *length, unsigned short *rc)
{
*rc = hllapi_get_revision();
return 0;
}
static int get_cursor_position(char *buffer, unsigned short *length, unsigned short *rc)
{
int pos = hllapi_getcursor();
trace("%s(%d)",__FUNCTION__,pos);
if(pos < 0)
return -1;
*rc = pos;
return 0;
}
static int set_cursor_position(char *buffer, unsigned short *length, unsigned short *rc)
{
trace("%s(%d)",__FUNCTION__,*rc);
*rc = hllapi_setcursor(*rc);
return 0;
}
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.
return hllapi_get_screen(*rc,buffer,*length);
}
static int input_string(char *input, unsigned short *length, unsigned short *rc)
{
size_t szText;
char * text;
if(!input)
{
*rc = HLLAPI_STATUS_BAD_PARAMETER;
return HLLAPI_STATUS_BAD_PARAMETER;
}
szText = strlen(input);
if(*length > 0 && *length < szText)
szText = *length;
text = (char *) malloc(szText+2);
memcpy(text,input,szText);
text[szText] = 0;
*rc = 0;
trace("input[%s]",text);
if(strchr(text,control_char))
{
// Convert control char
char * buffer = text;
char * ptr;
for(ptr = strchr(text,control_char);ptr;ptr = strchr(buffer,control_char))
{
*(ptr++) = 0;
trace("input[%s]",buffer);
hllapi_emulate_input(buffer,-1,0);
switch(*(ptr++))
{
case 'P': // Print
*rc = hllapi_print();
break;
case 'E': // Enter
hllapi_enter();
break;
case 'F': // Erase EOF
hllapi_erase_eof();
break;
case '1': // PF1
hllapi_pfkey(1);
break;
case '2': // PF2
hllapi_pfkey(2);
break;
case '3': // PF3
hllapi_pfkey(3);
break;
case '4': // PF4
hllapi_pfkey(4);
break;
case '5': // PF5
hllapi_pfkey(5);
break;
case '6': // PF6
hllapi_pfkey(6);
break;
case '7': // PF7
hllapi_pfkey(7);
break;
case '8': // PF8
hllapi_pfkey(8);
break;
case '9': // PF9
hllapi_pfkey(9);
break;
case 'a': // PF10
hllapi_pfkey(10);
break;
case 'b': // PF11
hllapi_pfkey(11);
break;
case 'c': // PF12
hllapi_pfkey(12);
break;
}
buffer = ptr;
}
if(*buffer)
hllapi_emulate_input(buffer,-1,0);
}
else
{
hllapi_emulate_input(text,szText,0);
}
free(text);
return 0;
}
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 The Search Presentation Space function was successful.
* 1 Your program is not connected to a host session.
* 2 An error was made in specifying parameters.
* 7 The host presentation space position is not valid.
* 9 A system error was encountered.
* 24 The search string was not found.
*
*/
size_t szBuffer = strlen(buffer);
char * text;
int rc = -1;
if(*length < szBuffer)
szBuffer = *length;
text = hllapi_get_string(*ps,szBuffer);
if(!text)
return HLLAPI_STATUS_SYSTEM_ERROR;
if(strncmp(text,buffer,szBuffer))
{
// String not found
*ps = 0;
rc = 24;
}
else
{
// String found
*ps = 1;
rc = 0;
}
hllapi_free(text);
return rc;
}
static int copy_ps(char *buffer, unsigned short *length, unsigned short *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 The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.
* 1 Your program is not connected to a host session.
* 4 The host presentation space contents were copied. The connected host presentation space was waiting for host response.
* 5 The host presentation space was copied. The keyboard was locked.
* 9 A system error was encountered.
*
*/
size_t szBuffer = strlen(buffer);
char * text;
if(!hllapi_is_connected())
return HLLAPI_STATUS_DISCONNECTED;
text = hllapi_get_string(1, szBuffer);
if(!text)
return HLLAPI_STATUS_SYSTEM_ERROR;
memcpy(buffer,text,szBuffer);
hllapi_free(text);
return hllapi_get_state();
}
static int wait_system(char *buffer, unsigned short *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
*
* 0 The keyboard is unlocked and ready for input.
* 1 Your application program is not connected to a valid session.
* 4 Timeout while still in XCLOCK (X []) or XSYSTEM.
* 5 The keyboard is locked.
* 9 A system error was encountered.
*
*/
time_t end = time(0) + 3600;
while(time(0) < end)
{
int state = hllapi_get_state();
if(state != HLLAPI_STATUS_WAITING)
return state;
if(hllapi_wait(1))
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return HLLAPI_STATUS_TIMEOUT;
}
static int copy_str_to_ps(char *text, unsigned short *length, unsigned short *ps)
{
/*
* Call Parameters
*
* Data String 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 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
*
* 0 The Copy String to Presentation Space function was successful.
* 1 Your program is not connected to a host session.
* 2 Parameter error or zero length for copy.
* 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.
* 9 A system error was encountered.
*
*/
size_t szText = strlen(text);
if(*length < szText)
szText = *length;
if(!szText)
return 2;
switch(hllapi_get_message_id())
{
case LIB3270_MESSAGE_NONE:
break;
case LIB3270_MESSAGE_DISCONNECTED:
return HLLAPI_STATUS_DISCONNECTED;
case LIB3270_MESSAGE_MINUS:
case LIB3270_MESSAGE_PROTECTED:
case LIB3270_MESSAGE_NUMERIC:
case LIB3270_MESSAGE_OVERFLOW:
case LIB3270_MESSAGE_INHIBIT:
case LIB3270_MESSAGE_KYBDLOCK:
return HLLAPI_STATUS_KEYBOARD_LOCKED;
default:
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return hllapi_emulate_input(text,szText,0);
}