-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathpat.cpp
More file actions
503 lines (435 loc) · 9.82 KB
/
pat.cpp
File metadata and controls
503 lines (435 loc) · 9.82 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
//____________________________________________________________
//
// PROGRAM: Language Preprocessor
// MODULE: pat.cpp
// DESCRIPTION: Code generator pattern generator
//
// The contents of this file are subject to the Interbase Public
// License Version 1.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy
// of the License at http://www.Inprise.com/IPL.html
//
// Software distributed under the License is distributed on an
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
// or implied. See the License for the specific language governing
// rights and limitations under the License.
//
// The Original Code was created by Inprise Corporation
// and its predecessors. Portions created by Inprise Corporation are
// Copyright (C) Inprise Corporation.
//
// All Rights Reserved.
// Contributor(s): ______________________________________.
//
//
//____________________________________________________________
//
//
#include "firebird.h"
#include <stdio.h>
#include <string.h>
#include "../gpre/gpre.h"
#include "../gpre/pat.h"
#include "../gpre/gpre_proto.h"
#include "../gpre/pat_proto.h"
#include "../gpre/lang_proto.h"
enum pat_t {
NL,
RH, RL, RT, RI, RS, // Request handle, level, transaction, ident, length
DH, DF, // Database handle, filename
TH, // Transaction handle
BH, BI, // Blob handle, blob_ident
FH, // Form handle
V1, V2, // Status vectors
I1, I2, // Identifier numbers
RF, RE, // OS- and language-dependent REF and REF-end character
VF, VE, // OS- and language-dependent VAL and VAL-end character
S1, S2, S3, S4, S5, S6, S7,
// Arbitrary strings
N1, N2, N3, N4, // Arbitrary number (SSHORT)
L1, L2, // Arbitrary number (SLONG)
PN, PL, PI, // Port number, port length, port ident
QN, QL, QI, // Second port number, port length, port ident
IF, EL, EN, // If, else, end
FR // Field reference
};
static constexpr struct ops
{
pat_t ops_type;
TEXT ops_string[3];
} operators[] =
{
{ RH, "RH" },
{ RL, "RL" },
{ RT, "RT" },
{ RI, "RI" },
{ RS, "RS" },
{ DH, "DH" },
{ DF, "DF" },
{ TH, "TH" },
{ BH, "BH" },
{ BI, "BI" },
{ FH, "FH" },
{ V1, "V1" },
{ V2, "V2" },
{ I1, "I1" },
{ I2, "I2" },
{ S1, "S1" },
{ S2, "S2" },
{ S3, "S3" },
{ S4, "S4" },
{ S5, "S5" },
{ S6, "S6" },
{ S7, "S7" },
{ N1, "N1" },
{ N2, "N2" },
{ N3, "N3" },
{ N4, "N4" },
{ L1, "L1" },
{ L2, "L2" },
{ PN, "PN" },
{ PL, "PL" },
{ PI, "PI" },
{ QN, "QN" },
{ QL, "QL" },
{ QI, "QI" },
{ IF, "IF" },
{ EL, "EL" },
{ EN, "EN" },
{ RF, "RF" },
{ RE, "RE" },
{ VF, "VF" },
{ VE, "VE" },
{ FR, "FR" },
{ NL, "" }
};
//____________________________________________________________
//
// Align output to a specific column for output. If the
// column is negative, don't do anything.
//
static int align(char* p, int column)
{
if (column < 0)
return 0;
char* const begin = p;
for (int i = column / 8; i; --i)
*p++ = '\t';
for (int i = column % 8; i; --i)
*p++ = ' ';
return p - begin;
}
//____________________________________________________________
//
// Expand a pattern.
//
void PATTERN_expand( USHORT column, const TEXT* pattern, PAT* args)
{
// CVC: kudos to the programmer that had chosen variables with the same
// names that structs in gpre.h and with type char* if not enough.
// Renamed ref to lang_ref and val to lang_val.
const TEXT* lang_ref = "";
const TEXT* refend = "";
const TEXT* lang_val = "";
const TEXT* valend = "";
if ((gpreGlob.sw_language == lang_c) || (isLangCpp(gpreGlob.sw_language)))
{
lang_ref = "&";
refend = "";
}
else if (gpreGlob.sw_language == lang_pascal)
{
lang_ref = "%%REF ";
refend = "";
}
else if (gpreGlob.sw_language == lang_cobol)
{
lang_ref = "BY REFERENCE ";
refend = "";
lang_val = "BY VALUE ";
valend = "";
}
else if (gpreGlob.sw_language == lang_fortran)
{
#if (defined AIX || defined AIX_PPC)
lang_ref = "%REF(";
refend = ")";
lang_val = "%VAL(";
valend = ")";
#endif
}
TEXT buffer[512];
TEXT* p = buffer;
*p++ = '\n';
bool sw_gen = true;
p += align(p, column);
SSHORT value; // value needs to be signed since some of the
// values printed out are signed.
SLONG long_value;
TEXT c;
while ((c = *pattern++))
{
if (c != '%')
{
if (sw_gen)
{
*p++ = c;
if ((c == '\n') && (*pattern))
p += align(p, column);
}
continue;
}
bool sw_ident = false;
const TEXT* string = NULL;
const ref* reference = NULL;
#ifdef GPRE_ADA
bool handle_flag = false;
#endif
bool long_flag = false;
const ops* oper_iter;
for (oper_iter = operators; oper_iter->ops_type != NL; oper_iter++)
{
if (oper_iter->ops_string[0] == pattern[0] && oper_iter->ops_string[1] == pattern[1])
{
break;
}
}
pattern += 2;
switch (oper_iter->ops_type)
{
case IF:
sw_gen = args->pat_condition;
continue;
case EL:
sw_gen = !sw_gen;
continue;
case EN:
sw_gen = true;
continue;
case RH:
#ifdef GPRE_ADA
handle_flag = true;
#endif
string = args->pat_request->req_handle;
break;
case RL:
string = args->pat_request->req_request_level;
break;
case RS:
value = args->pat_request->req_length;
break;
case RT:
#ifdef GPRE_ADA
handle_flag = true;
#endif
string = args->pat_request->req_trans;
break;
case RI:
long_value = args->pat_request->req_ident;
long_flag = true;
sw_ident = true;
break;
case DH:
#ifdef GPRE_ADA
handle_flag = true;
#endif
string = args->pat_database->dbb_name->sym_string;
break;
case DF:
string = args->pat_database->dbb_filename;
break;
case PN:
value = args->pat_port->por_msg_number;
break;
case PL:
value = args->pat_port->por_length;
break;
case PI:
long_value = args->pat_port->por_ident;
long_flag = true;
sw_ident = true;
break;
case QN:
value = args->pat_port2->por_msg_number;
break;
case QL:
value = args->pat_port2->por_length;
break;
case QI:
long_value = args->pat_port2->por_ident;
long_flag = true;
sw_ident = true;
break;
case BH:
long_value = args->pat_blob->blb_ident;
long_flag = true;
sw_ident = true;
break;
case I1:
long_value = args->pat_ident1;
long_flag = true;
sw_ident = true;
break;
case I2:
long_value = args->pat_ident2;
long_flag = true;
sw_ident = true;
break;
case S1:
string = args->pat_string1;
break;
case S2:
string = args->pat_string2;
break;
case S3:
string = args->pat_string3;
break;
case S4:
string = args->pat_string4;
break;
case S5:
string = args->pat_string5;
break;
case S6:
string = args->pat_string6;
break;
case S7:
string = args->pat_string7;
break;
case V1:
string = args->pat_vector1;
break;
case V2:
string = args->pat_vector2;
break;
case N1:
value = args->pat_value1;
break;
case N2:
value = args->pat_value2;
break;
case N3:
value = args->pat_value3;
break;
case N4:
value = args->pat_value4;
break;
case L1:
if (args && args->pat_long1)
long_value = args->pat_long1;
long_flag = true;
break;
case L2:
if (args && args->pat_long2)
long_value = args->pat_long2;
long_flag = true;
break;
case RF:
string = lang_ref;
break;
case RE:
string = refend;
break;
case VF:
string = lang_val;
break;
case VE:
string = valend;
break;
case FR:
reference = args->pat_reference;
break;
default:
snprintf(buffer, sizeof(buffer), "Unknown substitution \"%c%c\"", pattern[-2],
pattern[-1]);
CPR_error(buffer);
continue;
}
if (!sw_gen)
continue;
if (string)
{
#ifdef GPRE_ADA
if (handle_flag && (gpreGlob.sw_language == lang_ada))
{
for (const TEXT* q = gpreGlob.ada_package; *q;)
*p++ = *q++;
}
#endif
while (*string)
*p++ = *string++;
continue;
}
if (sw_ident)
{
if (long_flag)
snprintf(p, sizeof(buffer) - (p - buffer), gpreGlob.long_ident_pattern, long_value);
else
snprintf(p, sizeof(buffer) - (p - buffer), gpreGlob.ident_pattern, value);
}
else if (reference)
{
if (!reference->ref_port)
snprintf(p, sizeof(buffer) - (p - buffer), gpreGlob.ident_pattern,
reference->ref_ident);
else
{
TEXT temp1[16], temp2[16];
snprintf(temp1, sizeof(temp1), gpreGlob.ident_pattern,
reference->ref_port->por_ident);
snprintf(temp2, sizeof(temp2), gpreGlob.ident_pattern, reference->ref_ident);
switch (gpreGlob.sw_language)
{
case lang_fortran:
case lang_cobol:
strcpy(p, temp2);
break;
default:
snprintf(p, sizeof(buffer) - (p - buffer), "%s.%s", temp1, temp2);
break;
}
}
}
else if (long_flag) {
snprintf(p, sizeof(buffer) - (p - buffer), "%" SLONGFORMAT, long_value);
}
else {
snprintf(p, sizeof(buffer) - (p - buffer), "%d", value);
}
while (*p)
p++;
}
*p = 0;
#if (defined GPRE_ADA || defined GPRE_COBOL || defined GPRE_FORTRAN)
switch (gpreGlob.sw_language)
{
#ifdef GPRE_ADA
// Ada lines can be up to 120 characters long. ADA_print_buffer
// handles this problem and ensures that GPRE output is <=120 characters.
case lang_ada:
ADA_print_buffer(buffer, 0);
break;
#endif
#ifdef GPRE_COBOL
// COBOL lines can be up to 72 characters long. COB_print_buffer
// handles this problem and ensures that GPRE output is <= 72 characters.
case lang_cobol:
COB_print_buffer(buffer, true);
break;
#endif
#ifdef GPRE_FORTRAN
// In FORTRAN, characters beyond column 72 are ignored. FTN_print_buffer
// handles this problem and ensures that GPRE output does not exceed this
// limit.
case lang_fortran:
FTN_print_buffer(buffer);
break;
#endif
default:
fprintf(gpreGlob.out_file, "%s", buffer);
break;
}
#else
fprintf(gpreGlob.out_file, "%s", buffer);
#endif
}