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
|
#include <stdio.h>
#include <math.h>
#include <string.h>
#define TRUE (~0)
#define FALSE 0
#define TABLESIZE 1100
struct entry {
struct entry *t_link;
char *t_name;
char *t_lex;
unsigned short t_id;
} *table[TABLESIZE];
FILE *infile, *outfile;
unsigned short nsym = 0;
unsigned short hashstore = TRUE;
unsigned short f386 = TRUE;
extern char *_strdup();
extern char *malloc();
extern unsigned short hash();
extern unsigned short atoi();
static tfree(); /* defined below */
static struct entry *talloc(); /* defined below */
/* Local functions */
void s_entries ( struct entry *, char * );
void enter ( char *, char *, unsigned short );
_CRTAPI1 main( ac, av )
int ac;
char **av;
{
char ent[30], name[30], lex[30], ts[30], tc[30];
unsigned short size, count;
register unsigned short i;
register struct entry *p;
struct entry *q;
char *a;
double n, j, ssq, sum;
ac--;
av++;
while (**av == '-') {
ac--;
a = *av++;
while (*++a){
if (_stricmp(a, "dnoV386") == 0){
f386 = FALSE;
break;
}
else
if (*a == 'h')
hashstore = ~hashstore;
else {
fprintf( stderr, "usage: genkey [-dnoV386] [-h] [infile] [outfile]\n" );
exit( 1 );
}
}
}
if (ac < 1)
infile = stdin;
else {
ac--;
if ((infile = fopen( *av++, "r" )) == NULL) {
fprintf( stderr, "Cannot open input file %s\n", *--av );
exit( 1 );
}
}
if (ac < 1)
outfile = stdout;
else if ((outfile = fopen( *av, "w" )) == NULL) {
fprintf( stderr, "Cannot open output file %s\n", *av );
exit( 1 );
}
#ifdef DEBUG
setbuf( outfile, NULL );
#endif
/* copy first line of file */
do {
i = getc( infile );
putc( i, outfile );
} while (i != '\n');
while (fscanf( infile, " %s %s %s\n", tc, ts, name) == 3) {
count = atoi( tc );
size = atoi( ts );
#ifdef DEBUG
printf( "DEBUG: name=%s, size=%u, count=%u\n", name, size, count );
#endif
for (i = 0; i < TABLESIZE; table[i++] = NULL)
;
for (i = 0; i < count; i++)
if (fscanf( infile, " %s %s\n", ent, lex ) == 2) {
#ifdef DEBUG
printf( "DEBUG: ent=%s, lex=%s\n", ent, lex );
#endif
enter( ent, lex, size );
}
else {
fprintf( stderr, "Error in input file\n" );
exit( 1 );
}
#ifdef DEBUG
printf( "DEBUG: finished input loop\n" );
#endif
print_table( size );
#ifdef DEBUG
printf( "DEBUG: finished print_table()\n" );
#endif
print_struct( name, size );
#ifdef DEBUG
printf("DEBUG: finished print_struct()\n" );
#endif
n = sum = ssq = 0.0;
for (i = 0; i < TABLESIZE; i++) {
j = 0.0;
if (p = table[i]) {
n += 1.0;
do {
q = p->t_link;
tfree( p );
j += 1.0;
} while (p = q);
sum += j;
ssq += j * j;
}
}
#ifdef DEBUG
printf( "DEBUG: finished statistics loop\n" );
#endif
printf( "%6s: Size = %3u, Buckets = %3u, Avg = %.2f, Std = %.2f\n",
name, (unsigned short)sum, (unsigned short)n, sum / n,
sqrt( (ssq - sum * sum / n) / (n - 1.0) )
);
#ifdef DEBUG
printf( "DEBUG: finished this table; looking for more\n" );
#endif
}
exit( 0 );
}
/****************************************************************/
/* */
/* enter : make an ent into the symbol table. */
/* */
/****************************************************************/
void enter ( ent, lex, size )
char *ent;
char *lex;
unsigned short size;
{
register unsigned short hashval;
register struct entry *p;
int cb;
int fIs386Only;
cb = strlen(ent);
fIs386Only = !strcmp(ent + strlen(ent) - 4, ".386");
if (!f386 && fIs386Only)
return;
if (fIs386Only)
*(ent + cb - 4) = '\0';
p = talloc();
p->t_id = nsym++;
hashval = hash( ent ) % size;
p->t_link = table[hashval];
table[hashval] = p;
if ((p->t_name = _strdup( ent )) == NULL
|| (p->t_lex = _strdup( lex )) == NULL)
memerror();
}
/****************************************************************/
/* */
/* print_table : output the table we have built. */
/* */
/****************************************************************/
print_table ( size )
unsigned short size;
{
register unsigned short i;
register struct entry *p;
fprintf( outfile, "/***\n" );
for (i = 0; i < size; i++) {
fprintf( outfile, " *\t[%u]\n", i );
for (p = table[i]; p; p = p->t_link)
fprintf( outfile, " *\t\t%s,\t%s\n", p->t_name,
p->t_lex );
}
fprintf( outfile, " */\n" );
}
/****************************************************************/
/* */
/* print_struct : print the initialization structures. */
/* */
/****************************************************************/
print_struct ( name, size )
char *name;
unsigned short size;
{
register unsigned short i;
for (i = 0; i < size; i++)
s_entries( table[i], name );
s_symbols( name, size );
s_header( name, size );
}
/****************************************************************/
/* */
/* s_entries : print the symbol names and defs. */
/* */
/****************************************************************/
void s_entries ( p, name )
register struct entry *p;
char *name;
{
struct reverse {
struct entry *actual;
struct reverse *next;
} *head = NULL;
register struct reverse *q;
if (!p)
return;
while (p) {
/*
** all definitions must be reversed so that output will be that a
** unit will be defined before it is used.
*/
if ((q = (struct reverse *)malloc( sizeof(struct reverse) ))
== NULL)
memerror();
q->actual = p;
q->next = head;
head = q;
p = p->t_link;
}
for (q = head; q; q = q->next) {
fprintf( outfile, "static KEYSYM\t%s%u\t= {", name,
q->actual->t_id );
if (hashstore)
if (q->actual->t_link)
fprintf( outfile, "&%s%u,\"%s\",%u,%s", name,
q->actual->t_link->t_id,
q->actual->t_name,
hash( q->actual->t_name ),
q->actual->t_lex
);
else
fprintf( outfile, "0,\"%s\",%u,%s",
q->actual->t_name,
hash( q->actual->t_name ),
q->actual->t_lex
);
else if (q->actual->t_link)
fprintf( outfile, "&%s%u,\"%s\",%s", name,
q->actual->t_link->t_id, q->actual->t_name,
q->actual->t_lex
);
else
fprintf( outfile, "0,\"%s\",%s", q->actual->t_name,
q->actual->t_lex
);
fprintf( outfile, "};\n" );
}
for (q = head; q; head = q) {
q = q->next;
free( head );
}
}
/****************************************************************/
/* */
/* s_symbols : output the structure defining the */
/* symbol table. */
/* */
/****************************************************************/
s_symbols ( name, size )
char *name;
unsigned short size;
{
register unsigned short i;
fprintf( outfile, "\nstatic KEYSYM FARSYM *%s_words[%u] = {\n", name,
size );
for (i = 0; i < size; i++)
if (table[i])
fprintf( outfile, "\t&%s%u%c\n", name, table[i]->t_id,
((i < (size - 1)) ? ',' : ' ') );
else
fprintf( outfile, "\t0%c\n",
((i < (size - 1)) ? ',' : ' ') );
fprintf( outfile, "\t};\n" );
}
/****************************************************************/
/* */
/* s_header : output the header for the symbol table. */
/* */
/****************************************************************/
s_header ( name, size )
char *name;
unsigned short size;
{
fprintf( outfile, "\nKEYWORDS %s_table = {%s_words,%u};\n\n\n",
name, name, size );
}
static struct entry *head = NULL;
/****************************************************************/
/* */
/* talloc -- allocate space for a table entry */
/* */
/****************************************************************/
static struct entry *
talloc ()
{
register struct entry *p;
if (p = head) {
head = head->t_link;
return( p );
}
if ((p = (struct entry *)malloc( sizeof(struct entry) )))
return( p );
memerror();
}
/****************************************************************/
/* */
/* tfree -- free space for a table entry */
/* */
/****************************************************************/
static
tfree ( p )
struct entry *p;
{
free( p->t_name );
free( p->t_lex );
p->t_link = head;
head = p;
}
/****************************************************************/
/* */
/* memerr -- ran out of heap space; die */
/* */
/****************************************************************/
memerror ()
{
fprintf( stderr, "Out of heap space\n" );
exit( 1 );
}
#ifdef XENIX
int _stricmp ( first, last )
register char *first;
register char *last;
{
register f;
register l;
do {
if ((f = *first++) >= 'A' && f <= 'Z')
f += 'a' - 'A';
if ((l = *last++) >= 'A' && l <= 'Z')
l += 'a' - 'A';
} while (f && f == l);
return( f - l );
}
#endif /* XENIX */
|