calls.cc
7.52 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
/*
* "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 calls.cc 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 <exception>
#include <pw3270/class.h>
#include <pw3270/hllapi.h>
#include "client.h"
using namespace std;
using namespace PW3270_NAMESPACE;
/*--[ Globals ]--------------------------------------------------------------------------------------*/
static session * hSession = NULL;
static time_t hllapi_timeout = 120;
/*--[ Implement ]------------------------------------------------------------------------------------*/
HLLAPI_API_CALL hllapi_init(LPSTR mode)
{
trace("%s(%s)",__FUNCTION__,mode);
try
{
if(hSession)
delete hSession;
hSession = session::create(mode);
trace("hSession=%p",hSession);
}
catch(std::exception &e)
{
trace("Error \"%s\"",e.what());
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return hSession ? HLLAPI_STATUS_SUCCESS : HLLAPI_STATUS_SYSTEM_ERROR;
}
HLLAPI_API_CALL hllapi_deinit(void)
{
trace("%s()",__FUNCTION__);
try
{
if(hSession)
{
delete hSession;
hSession = NULL;
}
}
catch(std::exception &e)
{
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_get_revision(void)
{
try
{
return atoi(session::get_default()->get_revision().c_str());
}
catch(std::exception &e)
{
return -1;
}
return (DWORD) -1;
}
HLLAPI_API_CALL hllapi_connect(LPSTR uri, WORD wait)
{
try
{
session::get_default()->connect(uri,hllapi_timeout);
}
catch(std::exception &e)
{
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return hllapi_get_state();
}
HLLAPI_API_CALL hllapi_is_connected(void)
{
return session::get_default()->is_connected();
}
HLLAPI_API_CALL hllapi_get_state(void)
{
switch(hllapi_get_message_id())
{
case LIB3270_MESSAGE_NONE: // 0 - No message
return HLLAPI_STATUS_SUCCESS; // keyboard was unlocked and ready for input.
case LIB3270_MESSAGE_DISCONNECTED: // 4 - Disconnected from host
return HLLAPI_STATUS_DISCONNECTED; // Your application program was not connected to a valid session.
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; // keyboard is locked.
case LIB3270_MESSAGE_SYSWAIT:
case LIB3270_MESSAGE_TWAIT:
case LIB3270_MESSAGE_AWAITING_FIRST:
case LIB3270_MESSAGE_X:
case LIB3270_MESSAGE_RESOLVING:
case LIB3270_MESSAGE_CONNECTING:
return HLLAPI_STATUS_WAITING; // time-out while still busy (in XCLOCK or XSYSTEM in X) for the 3270 terminal emulation.
}
return HLLAPI_STATUS_SYSTEM_ERROR;
}
HLLAPI_API_CALL hllapi_disconnect(void)
{
session::get_default()->disconnect();
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_wait_for_ready(WORD seconds)
{
session::get_default()->wait_for_ready(seconds);
return hllapi_get_state();
}
HLLAPI_API_CALL hllapi_wait(WORD seconds)
{
session::get_default()->wait(seconds);
return hllapi_get_state();
}
HLLAPI_API_CALL hllapi_get_message_id(void)
{
return session::get_default()->get_program_message();
}
HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer)
{
if(!(buffer && *buffer))
return HLLAPI_STATUS_SYSTEM_ERROR;
try
{
size_t sz = strlen(buffer);
string str = session::get_default()->get_string_at(row,col,sz);
strncpy(buffer,str.c_str(),sz);
}
catch(std::exception &e)
{
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_enter(void)
{
return session::get_default()->enter();
}
HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text)
{
try
{
session::get_default()->set_string_at(row,col,text);
}
catch(std::exception &e)
{
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_cmp_text_at(WORD row, WORD col, LPSTR text)
{
int rc = HLLAPI_STATUS_SYSTEM_ERROR;
try
{
rc = session::get_default()->cmp_string_at(row,col,text);
}
catch(std::exception &e)
{
return HLLAPI_STATUS_SYSTEM_ERROR;
}
return rc;
}
HLLAPI_API_CALL hllapi_pfkey(WORD key)
{
return session::get_default()->pfkey(key);
}
HLLAPI_API_CALL hllapi_pakey(WORD key)
{
return session::get_default()->pakey(key);
}
HLLAPI_API_CALL hllapi_get_datadir(LPSTR datadir)
{
HKEY hKey = 0;
unsigned long datalen = strlen(datadir);
*datadir = 0;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS)
{
unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
if(RegQueryValueExA(hKey,"datadir",NULL,&datatype,(LPBYTE) datadir,&datalen) != ERROR_SUCCESS)
*datadir = 0;
RegCloseKey(hKey);
}
return *datadir;
}
HLLAPI_API_CALL hllapi_setcursor(WORD pos)
{
return session::get_default()->set_cursor_addr(pos-1);
}
HLLAPI_API_CALL hllapi_getcursor()
{
return session::get_default()->get_cursor_addr()+1;
}
HLLAPI_API_CALL hllapi_get_screen(WORD offset, LPSTR buffer, WORD len)
{
int rc = HLLAPI_STATUS_SYSTEM_ERROR;
if(!(buffer && *buffer))
return rc;
try
{
size_t szBuffer = strlen(buffer);
if(len < szBuffer && len > 0)
szBuffer = len;
string str = session::get_default()->get_string(offset,szBuffer);
strncpy(buffer,str.c_str(),szBuffer);
rc = HLLAPI_STATUS_SUCCESS;
}
catch(std::exception &e)
{
rc = HLLAPI_STATUS_SYSTEM_ERROR;
}
return rc;
}
HLLAPI_API_CALL hllapi_emulate_input(LPSTR buffer, WORD len, WORD pasting)
{
session::get_default()->emulate_input(buffer);
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_erase(void)
{
session::get_default()->erase();
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_erase_eof(void)
{
session::get_default()->erase_eof();
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_erase_eol(void)
{
session::get_default()->erase_eol();
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_erase_input(void)
{
session::get_default()->erase_input();
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_action(LPSTR buffer) {
session::get_default()->action((const char *) buffer);
return HLLAPI_STATUS_SUCCESS;
}
HLLAPI_API_CALL hllapi_print(void)
{
return session::get_default()->print();
}
char * hllapi_get_string(int offset, size_t len)
{
try
{
string str = session::get_default()->get_string(offset-1,len);
char * ret = strdup(str.c_str());
return ret;
}
catch(std::exception &e)
{
}
return NULL;
}
void hllapi_free(void *p)
{
free(p);
}