util.c
10.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
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
/*
* "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. 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 - e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
/**
* @brief Utility functions.
*/
#define _GNU_SOURCE
#include <internals.h>
#include "utilc.h"
#include "popupsc.h"
#include <lib3270/selection.h>
#include <lib3270/log.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(HAVE_LIBSSL)
#include <openssl/opensslv.h>
#endif // HAVE_LIBSSL
#if defined(HAVE_MALLOC_H)
#include <malloc.h>
#endif // defined
#define my_isspace(c) isspace((unsigned char)c)
/// @brief Cheesy internal version of sprintf that allocates its own memory.
char * lib3270_vsprintf(const char *fmt, va_list args)
{
char *r = NULL;
#if defined(HAVE_VASPRINTF)
if(vasprintf(&r, fmt, args) < 0 || !r)
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
#else
char buf[16384];
int nc;
nc = vsnprintf(buf, sizeof(buf), fmt, args);
if(nc < 0)
{
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
}
else if (nc < sizeof(buf))
{
r = lib3270_malloc(nc + 1);
strcpy(r, buf);
}
else
{
r = lib3270_malloc(nc + 1);
if(vsnprintf(r, nc, fmt, args) < 0)
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
}
#endif
return r;
}
LIB3270_EXPORT char * lib3270_strdup_printf(const char *fmt, ...)
{
va_list args;
char *r;
va_start(args, fmt);
r = lib3270_vsprintf(fmt, args);
va_end(args);
return r;
}
/**
* @brief Common helper functions to insert strings, through a template, into a new buffer.
*
* @param format A printf format string with '%s's in it.
*
*/
char * xs_buffer(const char *fmt, ...)
{
va_list args;
char *r;
va_start(args, fmt);
r = lib3270_vsprintf(fmt, args);
va_end(args);
return r;
}
/**
* @brief Expands a character in the manner of "cat -v".
*/
char *
ctl_see(int c)
{
static char buf[64];
char *p = buf;
c &= 0xff;
if ((c & 0x80) && (c <= 0xa0)) {
*p++ = 'M';
*p++ = '-';
c &= 0x7f;
}
if (c >= ' ' && c != 0x7f) {
*p++ = c;
} else {
*p++ = '^';
if (c == 0x7f) {
*p++ = '?';
} else {
*p++ = c + '@';
}
}
*p = '\0';
return buf;
}
LIB3270_EXPORT void * lib3270_free(void *p)
{
if(p)
free(p);
return NULL;
}
LIB3270_EXPORT void lib3270_autoptr_cleanup_char(char **ptr)
{
if(ptr && *ptr)
{
free((void *) *ptr);
*ptr = NULL;
}
}
LIB3270_EXPORT void lib3270_autoptr_cleanup_LIB3270_POPUP(LIB3270_POPUP **ptr)
{
if(ptr && *ptr)
{
free((void *) *ptr);
*ptr = NULL;
}
}
LIB3270_EXPORT void * lib3270_realloc(void *p, int len)
{
p = realloc(p, len);
if(!p)
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
return p;
}
LIB3270_EXPORT void * lib3270_calloc(int elsize, int nelem, void *ptr)
{
size_t sz = nelem * elsize;
if(ptr)
ptr = realloc(ptr,sz);
else
ptr = malloc(sz);
if(ptr)
memset(ptr,0,sz);
else
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
return ptr;
}
LIB3270_EXPORT void * lib3270_malloc(int len)
{
char *r;
r = malloc(len);
if (r == (char *)NULL)
{
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
return 0;
}
memset(r,0,len);
return r;
}
LIB3270_EXPORT void * lib3270_strdup(const char *str)
{
char *r;
r = strdup(str);
if (r == (char *)NULL)
{
lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL);
return 0;
}
return r;
}
LIB3270_EXPORT const char * lib3270_get_version(void)
{
return PACKAGE_VERSION;
}
LIB3270_EXPORT const char * lib3270_get_revision(void)
{
return RPQ_REVISION;
}
LIB3270_EXPORT char * lib3270_get_version_info(void)
{
#if defined(HAVE_LIBSSL)
return lib3270_strdup_printf(
"%s version %s-%s build %s (%s)",
PACKAGE_NAME,
PACKAGE_VERSION,
PACKAGE_RELEASE,
RPQ_TIMESTAMP_VALUE,
OPENSSL_VERSION_TEXT
);
#else
return lib3270_strdup_printf("%s version %s-%s build %s",PACKAGE_NAME,PACKAGE_VERSION,PACKAGE_RELEASE,RPQ_TIMESTAMP_VALUE);
#endif // HAVE_LIBSSL
}
void lib3270_popup_an_errno(H3270 *hSession, int errn, const char *fmt, ...)
{
va_list args;
char * text;
va_start(args, fmt);
text = lib3270_vsprintf(fmt, args);
va_end(args);
lib3270_write_log(hSession, "3270", "Error Popup:\n%s\nrc=%d (%s)",text,errn,strerror(errn));
lib3270_popup_dialog(hSession, LIB3270_NOTIFY_ERROR, _( "Error" ), text, "%s (rc=%d)", errn, strerror(errn));
lib3270_free(text);
}
LIB3270_EXPORT int lib3270_print(H3270 *hSession)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
return hSession->cbk.print(hSession, (hSession->selected ? LIB3270_CONTENT_SELECTED : LIB3270_CONTENT_ALL));
}
LIB3270_EXPORT int lib3270_print_all(H3270 *hSession)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
return hSession->cbk.print(hSession,LIB3270_CONTENT_ALL);
}
LIB3270_EXPORT int lib3270_print_selected(H3270 *hSession)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
if(hSession->selected)
return hSession->cbk.print(hSession,LIB3270_CONTENT_SELECTED);
return errno = ENODATA;
}
LIB3270_EXPORT int lib3270_print_copy(H3270 *hSession)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
return hSession->cbk.print(hSession,LIB3270_CONTENT_COPY);
}
LIB3270_EXPORT int lib3270_load(H3270 *hSession, const char *filename)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
return hSession->cbk.load(hSession, filename);
}
LIB3270_EXPORT int lib3270_save(H3270 *hSession, LIB3270_CONTENT_OPTION mode, const char *filename)
{
return hSession->cbk.save(hSession, mode, filename);
}
LIB3270_EXPORT int lib3270_save_all(H3270 *hSession, const char *filename)
{
if(check_online_session(hSession))
return errno = ENOTCONN;
return lib3270_save(hSession,LIB3270_CONTENT_ALL,filename);
}
LIB3270_EXPORT int lib3270_save_selected(H3270 *hSession, const char *filename)
{
if(hSession->selected)
return lib3270_save(hSession,LIB3270_CONTENT_SELECTED,filename);
return errno = ENODATA;
}
LIB3270_EXPORT int lib3270_save_copy(H3270 *hSession, const char *filename)
{
return lib3270_save(hSession,LIB3270_CONTENT_COPY,filename);
}
LIB3270_EXPORT LIB3270_POINTER lib3270_get_pointer(H3270 *hSession, int baddr)
{
static const struct _ptr {
unsigned short id;
LIB3270_POINTER value;
} ptr[] = {
{ 0x80, LIB3270_POINTER_MOVE_SELECTION },
{ 0x82, LIB3270_POINTER_SELECTION_TOP },
{ 0x86, LIB3270_POINTER_SELECTION_TOP_RIGHT },
{ 0x84, LIB3270_POINTER_SELECTION_RIGHT },
{ 0x81, LIB3270_POINTER_SELECTION_LEFT },
{ 0x89, LIB3270_POINTER_SELECTION_BOTTOM_LEFT },
{ 0x88, LIB3270_POINTER_SELECTION_BOTTOM },
{ 0x8c, LIB3270_POINTER_SELECTION_BOTTOM_RIGHT },
{ 0x83, LIB3270_POINTER_SELECTION_TOP_LEFT }
};
size_t f;
unsigned short id = lib3270_get_selection_flags(hSession,baddr) & 0x8f;
if(!lib3270_is_connected(hSession) || baddr < 0)
return LIB3270_POINTER_LOCKED;
for(f = 0; f < (sizeof(ptr)/sizeof(ptr[0]));f++)
{
if(ptr[f].id == id)
{
return ptr[f].value;
}
}
return hSession->pointer;
}
static int xdigit_value(const char scanner)
{
if(scanner >= '0' && scanner <= '9') {
return scanner - '0';
}
if(scanner >= 'A' && scanner <= 'F') {
return 10 + (scanner - 'A');
}
if(scanner >= 'a' && scanner <= 'f') {
return 10 + (scanner - 'a');
}
return -1;
}
static int unescape_character(const char *scanner)
{
int first_digit = xdigit_value(*scanner++);
int second_digit = xdigit_value(*scanner++);
if (first_digit < 0)
return -1;
if (second_digit < 0)
return -1;
return (first_digit << 4) | second_digit;
}
char * lib3270_unescape(const char *text)
{
if(!text)
return NULL;
size_t sz = strlen(text);
char * outString = lib3270_malloc(sz+1);
char * dst = outString;
const char * src = text;
char * ptr = strchr(src,'%');
memset(outString,0,sz+1);
while(ptr)
{
if(ptr[1] == '%')
{
src = ptr+2;
}
else
{
size_t sz = (ptr - src);
memcpy(dst,src,sz);
dst += sz;
int chr = unescape_character(ptr+1);
if(chr < 0)
{
*(dst++) = '?';
}
else
{
*(dst++) = (char) chr;
}
src += (sz+3);
}
ptr = strchr(src,'%');
}
strcpy(dst,src);
return outString;
}
int lib3270_compare_alnum(const char *s1, const char *s2)
{
while(*s1 && *s2) {
char c1 = toupper(*s1);
if(!isalnum(c1)) {
s1++;
continue;
}
char c2 = toupper(*s2);
if(!isalnum(c2)) {
s2++;
continue;
}
if(c1 != c2)
return 1;
s1++;
s2++;
}
return 0;
}
LIB3270_EXPORT const char * lib3270_get_translation_domain() {
return GETTEXT_PACKAGE;
}
LIB3270_INTERNAL char * lib3270_file_get_contents(H3270 GNUC_UNUSED(*hSession), const char *filename) {
int fd = open(filename,O_RDONLY);
if(fd < 0)
return NULL;
// Get file size.
struct stat st;
if(fstat(fd,&st) < 0) {
int err = errno;
close(fd);
errno = err;
return NULL;
}
char * text = lib3270_malloc(st.st_size+1);
memset(text,0,st.st_size+1);
char *ptr = text;
while(st.st_size) {
ssize_t bytes = read(fd,ptr,st.st_size);
if(bytes < 0) {
int err = errno;
close(fd);
lib3270_free(text);
errno = err;
return NULL;
}
if(!bytes)
break;
ptr += bytes;
st.st_size -= bytes;
}
close(fd);
return text;
}