mkfb.c
11.4 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
/*
* "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., 59 Temple
* Place, Suite 330, Boston, MA, 02111-1307, USA
*
* Este programa está nomeado como mkfb.c e possui 577 linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
* licinio@bb.com.br (Licínio Luis Branco)
* kraucer@bb.com.br (Kraucer Fernandes Mazuco)
* macmiranda@bb.com.br (Marco Aurélio Caldas Miranda)
*
*/
/*
* mkfb.c
* Utility to create RDB string definitions from a simple #ifdef'd .ad
* file
*/
#include "../include/lib3270/config.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define BUFSZ 1024 /* input line buffer size */
#define ARRSZ 8192 /* output array size */
#define SSSZ 10 /* maximum nested ifdef */
unsigned aix[ARRSZ]; /* fallback array indices */
unsigned xlno[ARRSZ]; /* fallback array line numbers */
unsigned n_fallbacks = 0; /* number of fallback entries */
/* ifdef state stack */
#define MODE_COLOR 0x00000001
#define MODE_FT 0x00000002
#define MODE_TRACE 0x00000004
#define MODE_MENUS 0x00000008
#define MODE_ANSI 0x00000010
#define MODE_KEYPAD 0x00000020
#define MODE_APL 0x00000040
#define MODE_PRINTER 0x00000080
#define MODE_STANDALONE 0x00000100
#define MODE_SCRIPT 0x00000200
#define MODE_DBCS 0x00000400
#define MODE__WIN32 0x00000800
#define MODEMASK 0x00000fff
struct {
unsigned long ifdefs;
unsigned long ifndefs;
unsigned lno;
} ss[SSSZ];
int ssp = 0;
struct {
const char *name;
unsigned long mask;
} parts[] = {
{ "COLOR", MODE_COLOR },
{ "X3270_FT", MODE_FT },
{ "X3270_TRACE", MODE_TRACE },
{ "X3270_MENUS", MODE_MENUS },
{ "X3270_ANSI", MODE_ANSI },
{ "X3270_KEYPAD", MODE_KEYPAD },
{ "X3270_APL", MODE_APL },
{ "X3270_PRINTER", MODE_PRINTER },
{ "STANDALONE", MODE_STANDALONE },
{ "X3270_SCRIPT", MODE_SCRIPT },
{ "X3270_DBCS", MODE_DBCS },
{ "_WIN32", MODE__WIN32 }
};
#define NPARTS (sizeof(parts)/sizeof(parts[0]))
unsigned long is_defined =
MODE_COLOR |
#if defined(X3270_FT)
MODE_FT
#else
0
#endif
|
#if defined(X3270_TRACE)
MODE_TRACE
#else
0
#endif
|
#if defined(X3270_MENUS)
MODE_MENUS
#else
0
#endif
|
#if defined(X3270_ANSI)
MODE_ANSI
#else
0
#endif
|
#if defined(X3270_KEYPAD)
MODE_KEYPAD
#else
0
#endif
|
#if defined(X3270_APL)
MODE_APL
#else
0
#endif
|
#if defined(X3270_PRINTER)
MODE_PRINTER
#else
0
#endif
|
#if defined(X3270_SCRIPT)
MODE_SCRIPT
#else
0
#endif
|
#if defined(X3270_DBCS)
MODE_DBCS
#else
0
#endif
|
#if defined(_WIN32)
MODE__WIN32
#else
0
#endif
;
unsigned long is_undefined;
char *me;
void emit(FILE *t, int ix, char c);
void
usage(void)
{
fprintf(stderr, "usage: %s [infile [outfile]]\n", me);
exit(1);
}
int
main(int argc, char *argv[])
{
char buf[BUFSZ];
int lno = 0;
int cc = 0;
int i;
int continued = 0;
const char *filename = "standard input";
FILE *u, *t, *tc = NULL, *tm = NULL, *o;
int cmode = 0;
unsigned long ifdefs;
unsigned long ifndefs;
int last_continue = 0;
/* Parse arguments. */
if ((me = strrchr(argv[0], '/')) != (char *)NULL)
me++;
else
me = argv[0];
if (argc > 1 && !strcmp(argv[1], "-c")) {
cmode = 1;
is_defined |= MODE_STANDALONE;
argc--;
argv++;
}
switch (argc) {
case 1:
break;
case 2:
case 3:
if (strcmp(argv[1], "-")) {
if (freopen(argv[1], "r", stdin) == (FILE *)NULL) {
perror(argv[1]);
exit(1);
}
filename = argv[1];
}
break;
default:
usage();
}
is_undefined = MODE_COLOR | (~is_defined & MODEMASK);
/* Do #ifdef, comment and whitespace processing first. */
u = tmpfile();
if (u == NULL) {
perror("tmpfile");
exit(1);
}
while (fgets(buf, BUFSZ, stdin) != (char *)NULL) {
char *s = buf;
int sl;
int i;
lno++;
/* Skip leading white space. */
while (isspace(*s))
s++;
if (cmode &&
(!strncmp(s, "x3270.", 6) || !strncmp(s, "x3270*", 6))) {
s += 6;
}
/* Remove trailing white space. */
while ((sl = strlen(s)) && isspace(s[sl-1]))
s[sl-1] = '\0';
/* Skip comments and empty lines. */
if ((!last_continue && *s == '!') || !*s)
continue;
/* Check for simple if[n]defs. */
if (*s == '#') {
int ifnd = 1;
if (!strncmp(s, "#ifdef ", 7) ||
!(ifnd = strncmp(s, "#ifndef ", 8))) {
char *tk;
if (ssp >= SSSZ) {
fprintf(stderr,
"%s, line %d: Stack overflow\n",
filename, lno);
exit(1);
}
ss[ssp].ifdefs = 0L;
ss[ssp].ifndefs = 0L;
ss[ssp].lno = lno;
tk = s + 7 + !ifnd;
for (i = 0; i < NPARTS; i++) {
if (!strcmp(tk, parts[i].name)) {
if (!ifnd)
ss[ssp++].ifndefs =
parts[i].mask;
else
ss[ssp++].ifdefs =
parts[i].mask;
break;
}
}
if (i >= NPARTS) {
fprintf(stderr,
"%s, line %d: Unknown condition\n",
filename, lno);
exit(1);
}
continue;
}
else if (!strcmp(s, "#else")) {
unsigned long tmp;
if (!ssp) {
fprintf(stderr,
"%s, line %d: Missing #if[n]def\n",
filename, lno);
exit(1);
}
tmp = ss[ssp-1].ifdefs;
ss[ssp-1].ifdefs = ss[ssp-1].ifndefs;
ss[ssp-1].ifndefs = tmp;
} else if (!strcmp(s, "#endif")) {
if (!ssp) {
fprintf(stderr,
"%s, line %d: Missing #if[n]def\n",
filename, lno);
exit(1);
}
ssp--;
} else {
fprintf(stderr,
"%s, line %d: Unrecognized # directive\n",
filename, lno);
exit(1);
}
continue;
}
/* Figure out if there's anything to emit. */
/* First, look for contradictions. */
ifdefs = 0;
ifndefs = 0;
for (i = 0; i < ssp; i++) {
ifdefs |= ss[i].ifdefs;
ifndefs |= ss[i].ifndefs;
}
if (ifdefs & ifndefs) {
#ifdef DEBUG_IFDEFS
fprintf(stderr, "contradiction, line %d\n", lno);
#endif
continue;
}
/* Then, apply the actual values. */
if (ifdefs && (ifdefs & is_defined) != ifdefs) {
#ifdef DEBUG_IFDEFS
fprintf(stderr, "ifdef failed, line %d\n", lno);
#endif
continue;
}
if (ifndefs && (ifndefs & is_undefined) != ifndefs) {
#ifdef DEBUG_IFDEFS
fprintf(stderr, "ifndef failed, line %d\n", lno);
#endif
continue;
}
/* Emit the text. */
fprintf(u, "%lx %lx %d\n%s\n", ifdefs, ifndefs, lno, s);
last_continue = strlen(s) > 0 && s[strlen(s) - 1] == '\\';
}
if (ssp) {
fprintf(stderr, "%d missing #endif(s) in %s\n", ssp, filename);
fprintf(stderr, "last #ifdef was at line %u\n", ss[ssp-1].lno);
exit(1);
}
/* Re-scan, emitting code this time. */
rewind(u);
t = tmpfile();
if (t == NULL) {
perror("tmpfile");
exit(1);
}
if (!cmode) {
tc = tmpfile();
if (tc == NULL) {
perror("tmpfile");
exit(1);
}
tm = tmpfile();
if (tm == NULL) {
perror("tmpfile");
exit(1);
}
}
/* Emit the initial boilerplate. */
fprintf(t, "/* This file was created automatically from %s by mkfb. */\n\n",
filename);
if (cmode) {
fprintf(t, "#include \"globals.h\"\n");
fprintf(t, "static unsigned char fsd[] = {\n");
} else {
fprintf(t, "unsigned char common_fallbacks[] = {\n");
fprintf(tc, "unsigned char color_fallbacks[] = {\n");
fprintf(tm, "unsigned char mono_fallbacks[] = {\n");
}
/* Scan the file, emitting the fsd array and creating the indices. */
while (fscanf(u, "%lx %lx %d\n", &ifdefs, &ifndefs, &lno) == 3) {
char *s = buf;
char c;
int white;
FILE *t_this = t;
int ix = 0;
if (fgets(buf, BUFSZ, u) == NULL)
break;
if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
#if 0
fprintf(stderr, "%lx %lx %d %s\n", ifdefs, ifndefs, lno, buf);
#endif
/* Add array offsets. */
if (cmode) {
/* Ignore color. Accumulate offsets into an array. */
if (n_fallbacks >= ARRSZ) {
fprintf(stderr, "%s, line %d: Buffer overflow\n", filename, lno);
exit(1);
}
aix[n_fallbacks] = cc;
xlno[n_fallbacks++] = lno;
} else {
/* Use color to decide which file to write into. */
if (!(ifdefs & MODE_COLOR) && !(ifndefs & MODE_COLOR)) {
/* Both. */
t_this = t;
ix = 0;
} else if (ifdefs & MODE_COLOR) {
/* Just color. */
t_this = tc;
ix = 1;
} else {
/* Just mono. */
t_this = tm;
ix = 2;
}
}
continued = 0;
white = 0;
while ((c = *s++) != '\0') {
if (c == ' ' || c == '\t')
white++;
else if (white) {
emit(t_this, ix, ' ');
cc++;
white = 0;
}
switch (c) {
case ' ':
case '\t':
break;
case '#':
if (!cmode) {
emit(t_this, ix, '\\');
emit(t_this, ix, '#');
cc += 2;
} else {
emit(t_this, ix, c);
cc++;
}
break;
case '\\':
if (*s == '\0') {
continued = 1;
break;
} else if (cmode) {
switch ((c = *s++)) {
case 't':
c = '\t';
break;
case 'n':
c = '\n';
break;
default:
break;
}
}
/* else fall through */
default:
emit(t_this, ix, c);
cc++;
break;
}
}
if (white) {
emit(t_this, ix, ' ');
cc++;
white = 0;
}
if (!continued) {
if (cmode)
emit(t_this, ix, 0);
else
emit(t_this, ix, '\n');
cc++;
}
}
fclose(u);
if (cmode)
fprintf(t, "};\n\n");
else {
emit(t, 0, 0);
fprintf(t, "};\n\n");
emit(tc, 0, 0);
fprintf(tc, "};\n\n");
emit(tm, 0, 0);
fprintf(tm, "};\n\n");
}
/* Open the output file. */
if (argc == 3) {
o = fopen(argv[2], "w");
if (o == NULL) {
perror(argv[2]);
exit(1);
}
} else
o = stdout;
/* Copy tmp to output. */
rewind(t);
if (!cmode) {
rewind(tc);
rewind(tm);
}
while (fgets(buf, sizeof(buf), t) != NULL) {
fprintf(o, "%s", buf);
}
if (!cmode) {
while (fgets(buf, sizeof(buf), tc) != NULL) {
fprintf(o, "%s", buf);
}
while (fgets(buf, sizeof(buf), tm) != NULL) {
fprintf(o, "%s", buf);
}
}
if (cmode) {
/* Emit the fallback array. */
fprintf(o, "String fallbacks[%u] = {\n", n_fallbacks + 1);
for (i = 0; i < n_fallbacks; i++) {
fprintf(o, "\t(String)&fsd[%u], /* line %u */\n",
aix[i],
xlno[i]);
}
fprintf(o, "\t(String)NULL\n};\n\n");
/* Emit some test code. */
fprintf(o, "%s", "#if defined(DUMP) /*[*/\n\
#include <stdio.h>\n\
int\n\
main(int argc, char *argv[])\n\
{\n\
int i;\n\
\n\
for (i = 0; fallbacks[i] != NULL; i++)\n\
printf(\"%d: %s\\n\", i, fallbacks[i]);\n\
return 0;\n\
}\n");
fprintf(o, "#endif /*]*/\n\n");
}
if (o != stdout)
fclose(o);
fclose(t);
if (!cmode) {
fclose(tc);
fclose(tm);
}
return 0;
}
static int n_out[3] = { 0, 0, 0 };
void
emit(FILE *t, int ix, char c)
{
if (n_out[ix] >= 19) {
fprintf(t, "\n");
n_out[ix] = 0;
}
fprintf(t, "%3d,", (unsigned char)c);
n_out[ix]++;
}