session.cc
13.8 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
* "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., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como session.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 <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "private.h"
#ifndef WIN32
#include <unistd.h>
#endif // !WIN32
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif // HAVE_SYSLOG
/*--[ Implement ]--------------------------------------------------------------------------------------------------*/
using namespace PW3270_NAMESPACE;
#if defined(linux)
static void onLoad() __attribute__((constructor));
static void onUnLoad() __attribute__((destructor));
static void onLoad()
{
session::init();
}
static void onUnLoad()
{
session::deinit();
}
#endif // linux
#ifdef _WIN32
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
session::init();
break;
case DLL_PROCESS_DETACH:
session::deinit();
break;
}
return TRUE;
}
#endif // _WIN32
namespace PW3270_NAMESPACE {
session * session::first = nullptr;
session * session::last = nullptr;
session * (*session::factory)(const char *name) = nullptr;
static recursive_mutex mtx;
inline void lock()
{
mtx.lock();
}
inline void unlock()
{
mtx.unlock();
}
void session::init()
{
trace("Loading %s objects",PACKAGE_NAME);
}
void session::deinit()
{
trace("Unloading %s objects",PACKAGE_NAME);
while(first)
{
delete first;
}
}
session::session()
{
#ifdef HAVE_ICONV
this->conv2Local = (iconv_t) (-1);
this->conv2Host = (iconv_t) (-1);
#endif
if(first)
{
prev = last;
next = 0;
last->next = this;
last = this;
}
else
{
prev = next = 0;
first = last = this;
}
}
session::~session()
{
#ifdef HAVE_ICONV
if(this->conv2Local != (iconv_t) (-1))
iconv_close(this->conv2Local);
if(this->conv2Host != (iconv_t) (-1))
iconv_close(this->conv2Host);
#endif
if(prev)
prev->next = next;
else
first = next;
if(next)
next->prev = prev;
else
last = prev;
}
// Factory methods and settings
session * session::create(const char *name)
{
session *rc = nullptr;
trace("%s(%s)",__FUNCTION__,name);
lock();
try
{
if(factory)
rc = factory(name);
else if(name && *name)
rc = create_remote(name);
else
rc = create_local();
}
catch(std::exception &e)
{
unlock();
throw exception("%s",e.what());
}
unlock();
return rc;
}
session * session::start(const char *name)
{
return create(name);
}
bool session::has_default(void)
{
return first != nullptr;
}
session * session::get_default(void)
{
if(first)
return first;
return create(NULL);
}
void session::set_plugin(session * (*factory)(const char *name))
{
trace("%s(%p)",__FUNCTION__,factory);
session::factory = factory;
}
// Object settings
void session::set_display_charset(const char *remote, const char *local)
{
trace("%s(%s,%s)",__FUNCTION__,remote,local);
#ifdef HAVE_ICONV
string display_charset = this->get_display_charset();
if(this->conv2Local != (iconv_t) (-1))
iconv_close(this->conv2Local);
if(this->conv2Host != (iconv_t) (-1))
iconv_close(this->conv2Host);
if(!remote)
remote = display_charset.c_str();
trace("%s remote=%s local=%s",__FUNCTION__,remote,local);
if(strcmp(local,remote))
{
// Local and remote charsets aren't the same, setup conversion
conv2Local = iconv_open(local, remote);
conv2Host = iconv_open(remote,local);
}
else
{
// Same charset, doesn't convert
conv2Local = conv2Host = (iconv_t)(-1);
}
#else
#error aqui
throw exception("%s",strerror(ENOSUP));
#endif
}
string session::get_display_charset(void)
{
return string(get_encoding());
}
const char * session::get_encoding(void)
{
return "ISO-8859-1";
}
// 3270 methods
const string session::get_version(void)
{
return string(PACKAGE_VERSION);
}
const string session::get_revision(void)
{
#ifdef PACKAGE_REVISION
return string(PACKAGE_REVISION);
#else
return string(STRINGIZE_VALUE_OF(BUILD_DATE));
#endif // PACKAGE_REVISION
}
void session::log(const char *fmt, ...)
{
va_list arg_ptr;
va_start(arg_ptr, fmt);
this->logva(fmt,arg_ptr);
va_end(arg_ptr);
}
void session::logva(const char *fmt, va_list args)
{
#ifdef HAVE_SYSLOG
openlog(PACKAGE_NAME, LOG_NDELAY, LOG_USER);
vsyslog(LOG_INFO,fmt,args);
closelog();
#else
vfprintf(stderr,fmt,args);
#endif
}
int session::wait_for_text_at(int row, int col, const char *key, int timeout)
{
time_t end = time(0)+timeout;
trace("%s(%d,%d,%s,%d)",__FUNCTION__,row,col,key,timeout);
iterate(false);
while(time(0) < end)
{
trace("Aguardar %d segundos por \"%s\" @%d,%d (%s)",(int) (end - time(0)),key,row,col,get_text_at(row,col,strlen(key)).c_str());
int rc = wait_for_ready(end - time(0));
if(rc) {
return rc;
}
if(!cmp_text_at(row,col,key)) {
return 0;
}
iterate(true);
}
trace("Tela:\n%s\n", ((string) *this).c_str());
return ETIMEDOUT;
}
int session::set_copy(const char *text)
{
return EINVAL;
}
string session::get_copy(void)
{
errno = EINVAL;
return string();
}
string session::get_clipboard(void)
{
#if defined(_WIN32)
if (! OpenClipboard(0))
{
throw exception(GetLastError(),"%s","Can´t open system clipboard");
return NULL;
}
HANDLE hData = GetClipboardData(CF_TEXT);
if(!hData)
{
throw exception(GetLastError(),"%s","Can´t get clipboard data");
return NULL;
}
char * pszText = static_cast<char*>( GlobalLock(hData) );
if(!pszText)
{
throw exception(GetLastError(),"%s","Can´t lock clipboard");
return NULL;
}
string text = string ( pszText );
GlobalUnlock( hData );
CloseClipboard();
return text;
#else
errno = EINVAL;
return NULL;
#endif // _WIN32
}
int session::set_clipboard(const char *text)
{
#if defined(_WIN32)
if (! OpenClipboard(0))
{
throw exception(GetLastError(),"%s","Can´t open system clipboard");
return -1;
}
EmptyClipboard();
size_t size = strlen(text)+1;
HGLOBAL hClipboardData = GlobalAlloc(GMEM_MOVEABLE , size);
strcpy((char *) GlobalLock(hClipboardData), text);
if(!SetClipboardData(CF_TEXT, hClipboardData))
{
GlobalUnlock(hClipboardData);
CloseClipboard();
throw exception(GetLastError(),"%s","Can´t set system clipboard");
}
GlobalUnlock(hClipboardData);
CloseClipboard();
return 0;
#else
return EINVAL;
#endif // _WIN32
}
int session::popup_dialog(LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, ...)
{
return -1;
}
string session::file_chooser_dialog(int action, const char *title, const char *extension, const char *filename)
{
return string("");
}
string session::get_3270_text(const char *str)
{
string rc;
#ifdef HAVE_ICONV
size_t in = strlen(str);
if(in && conv2Host != (iconv_t)(-1))
{
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) str;
memset(ptr=outBuffer,0,out);
iconv(conv2Host,NULL,NULL,NULL,NULL); // Reset state
if(iconv(conv2Host,&inBuffer,&in,&ptr,&out) == ((size_t) -1)) {
rc.assign(str);
} else {
rc.assign(outBuffer);
}
free(outBuffer);
} else {
rc.assign(str);
}
#else
rc.assign(str);
#endif // HAVE_ICONV
trace("%s(\"%s\")=\"%s\"",__FUNCTION__,str,rc.c_str());
return rc;
}
string session::get_local_text(const char *str)
{
string rc;
#ifdef HAVE_ICONV
size_t in = strlen(str);
if(in && conv2Local != (iconv_t)(-1))
{
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) str;
memset(ptr=outBuffer,0,out);
iconv(conv2Local,NULL,NULL,NULL,NULL); // Reset state
if(iconv(conv2Local,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
rc.assign(outBuffer);
free(outBuffer);
}
else
{
char * text = strdup(str);
for(char *ptr = text;*ptr;ptr++)
{
if(*ptr < ' ' || *ptr > 128)
{
*ptr = '?';
}
}
rc = text;
free(text);
}
#else
char * text = strdup(str);
for(char *ptr = text;*ptr;ptr++)
{
if(*ptr < ' ' || *ptr > 128)
{
*ptr = '?';
}
}
rc = text;
free(text);
#endif // HAVE_ICONV
return rc;
}
string session::get_string_at(int row, int col, size_t sz, bool lf)
{
return this->get_local_text(this->get_text_at(row,col,sz,lf).c_str());
}
int session::set_string_at(int row, int col, const char *str)
{
if(!str)
return -1;
#ifdef HAVE_ICONV
if(conv2Host != (iconv_t)(-1))
{
size_t in = strlen(str);
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) str;
memset(ptr=outBuffer,0,out);
iconv(conv2Host,NULL,NULL,NULL,NULL); // Reset state
if(iconv(conv2Host,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
{
int rc = this->set_text_at(row,col,outBuffer);
free(outBuffer);
return rc;
}
free(outBuffer);
}
#endif // HAVE_ICONV
return this->set_text_at(row,col,str);
}
int session::input_string(const char *str)
{
if(!str)
return -1;
#ifdef HAVE_ICONV
if(conv2Host != (iconv_t)(-1))
{
size_t in = strlen(str);
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) str;
memset(ptr=outBuffer,0,out);
iconv(conv2Host,NULL,NULL,NULL,NULL); // Reset state
if(iconv(conv2Host,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
{
int rc = this->emulate_input(outBuffer);
free(outBuffer);
return rc;
}
free(outBuffer);
}
#endif // HAVE_ICONV
return this->emulate_input(str);
}
int session::cmp_string_at(int row, int col, const char *text, bool lf)
{
return cmp_text_at(row,col,get_3270_text(text).c_str(),lf);
}
int session::wait_for_string_at(int row, int col, const char *key, int timeout)
{
return wait_for_text_at(row,col,get_3270_text(key).c_str(),timeout);
}
string session::get_string(int baddr, size_t len, bool lf)
{
return get_local_text(get_text(baddr,len,lf).c_str());
}
string session::asc2ebc(string &str)
{
size_t sz = str.size();
unsigned char buffer[sz+1];
memcpy(buffer,str.c_str(),sz);
return string(asc2ebc(buffer,sz));
}
string session::ebc2asc(string &str)
{
size_t sz = str.size();
unsigned char buffer[sz+1];
memcpy(buffer,str.c_str(),sz);
return string(ebc2asc(buffer,sz));
}
int session::file_transfer(LIB3270_FT_OPTION options, const char *local, const char *remote, int lrecl, int blksize, int primspace, int secspace, int dft)
{
log("Can't transfer %s: File transfer is unavailable", local ? local : "file");
return EINVAL;
}
int session::set_host(const char *host)
{
return set_url(host);
}
int session::connect(const char *url, time_t wait)
{
int rc = 0;
if(url && *url)
{
set_url(url);
}
rc = connect();
trace("%s: connect=%d wait=%u",__FUNCTION__,rc,(unsigned int) wait);
if(!rc && wait)
{
rc = wait_for_ready(wait);
}
return rc;
}
#ifdef _WIN32
string session::win32_strerror(int e)
{
static char buffer[4096];
memset(buffer,0,sizeof(buffer));
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0)
{
snprintf(buffer,4095,"Windows error %d", e);
}
for(size_t f=0;f<sizeof(buffer);f++)
{
if(buffer[f] < ' ')
{
buffer[f] = 0;
break;
}
}
return string(buffer);
}
#endif // _WIN32
int session::erase(int mode) {
switch(mode) {
case 0:
return erase();
case 1:
return erase_eof();
case 2:
return erase_eol();
case 3:
return erase_input();
}
return -1;
}
string session::get_contents(bool lf)
{
string rc = "";
int rows = get_height();
int cols = get_width();
for(int r = 0; r < rows; r++)
{
rc += get_string_at(r+1,1,cols).c_str();
if(lf) {
rc += "\n";
}
}
return rc;
}
size_t session::find_string(const char *str, bool lf) {
int rc = 0;
try
{
size_t pos = get_contents(lf).find(str);
if(pos != string::npos) {
rc = ((int) pos) + 1;
}
}
catch(std::exception &e)
{
rc = 0;
}
return rc;
}
string session::get_session_name(void) const
{
return string();
}
int session::close(void) {
return 0;
}
/**
* @brief Define após quantos segundos uma sessão IDLE será cancelada.
*
* @param timeout Nº de segundos a esperar em sessão inativa.
*
*/
void session::set_timeout(time_t timeout) {
}
/**
* @brief Define após quantos segundos uma sessão offline será cancelada.
*
* @param timeout Nº de segundos a esperar em sessão offline.
*
*/
void session::set_autoclose(time_t timeout) {
}
}