decs.c 10.9 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
#include <dcw.h>
#include "struct.h"
#include "decs.h"


dcw's avatar
dcw committed
6
7
8
9
10
11
12
/*
/^#ifdef HASPROTOS
!/endif$
stat %
*/


dcw's avatar
dcw committed
13
#ifdef HASPROTOS
dcw's avatar
dcw committed
14
static void line( char * , long , long , long , long );
15
16
static void h_declns( char * , char * , declnlist );
static void c_declns( char * , char * , declnlist );
17
static void data_decls( declnlist );
18
static void declfields( paramlist );
dcw's avatar
dcw committed
19
static void protos( declnlist , BOOL );
20
21
static void one_proto( char * , shapelist , BOOL );
static void cons_fns( declnlist );
dcw's avatar
dcw committed
22
23
24
static void cons_fn_hdr( char * , shapelist );
static void cons_fn_body( declnlist , char * , shapelist );
static void print_fns( declnlist );
25
26
27
static void print_fn_shape( declnlist , shapelist );
static void print_all_params( declnlist , shapelist );
static void print_param( char * , paramlist , BOOL );
dcw's avatar
dcw committed
28
#else
dcw's avatar
dcw committed
29
static void line();
30
31
static void h_declns();
static void c_declns();
dcw's avatar
dcw committed
32
static void data_decls();
33
static void declfields();
dcw's avatar
dcw committed
34
static void protos();
35
36
static void one_proto();
static void cons_fns();
dcw's avatar
dcw committed
37
38
static void cons_fn_hdr();
static void cons_fn_body();
dcw's avatar
dcw committed
39
static void print_fns();
40
41
42
static void print_fn_shape();
static void print_all_params();
static void print_param();
dcw's avatar
dcw committed
43
44
45
#endif


46
47
48
49
50
51
52
static int numtabs = 0;
static FILE *outfile;


#define indent() numtabs++
#define outdent() numtabs--

dcw's avatar
dcw committed
53
54
55
56
#define nl()		fputc( '\n', outfile )
#define outchar(c)	fputc( c, outfile )
#define usefile(f)	outfile = f, numtabs = 0

57
58
59
60
61
62
63
64
65


/*VARARGS*/
static void line( fmt, a, b, c, d ) char *fmt; long a, b, c, d;
{
	int i;

	for( i=numtabs; i; i-- ) fputc( '\t', outfile );
	fprintf( outfile, fmt, a, b, c, d );
dcw's avatar
dcw committed
66
	nl();
67
68
69
}


70
void make_declns( exports, globals, d, basename ) declnlist d; char *exports, *globals, *basename;
dcw's avatar
dcw committed
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
	printf( "datadec: Making data declarations in %s.[ch]\n", basename );
	h_declns( basename, exports, d );
	c_declns( basename, globals, d );

}


static void h_declns( basename, exports, d ) char *basename, *exports; declnlist d;
{
	char		tempname[256];
	char		*exportptr;
	FILE		*hfile;
	declnlist	dl;

	sprintf( tempname, "%s.h", basename );
	hfile = fopen( tempname, "w" );
	ASSERT( hfile != NULL, ("datadec: can't create '%s'\n",tempname) );

	usefile( hfile );

	line( "/*" );
	line( " * Automatically Generated by DataDec" );
	line( " */\n" );
	line( "typedef char *string;\n" );
	line( "typedef char BOOL;" );
	line( "#define TRUE 1" );
	line( "#define FALSE 0\n" );
	line( "#define NEW(t) ((t)malloc(sizeof(struct t)))\n\n" );

	if( *exports != '\0' )
dcw's avatar
dcw committed
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
		int i;

		line( "/* Contents of EXPORT section */" );
		for( exportptr=exports; *exportptr; exportptr++ )
		{
			if( *exportptr == '@' && exportptr[1] == '@'
			&&   exportptr[2] == '\n' )
			{
				exportptr += 2;
				break;
			}
			outchar( *exportptr );
		}

		nl();
		nl();
	}

	data_decls( d );

	for( dl = d; dl != NULL; dl=dl->next )
	{
		if( dl->UseNull )
		{
			line( "#define %s_%s() ((%s)NULL)",
				dl->name, dl->shapes->name, dl->name );
		}
	}
	line( "#ifdef HASPROTOS" );
	protos( d, TRUE );
	line( "#else" );
	protos( d, FALSE );
	line( "#endif" );

	if( *exportptr != '\0' )
	{
		nl();
		line( "/* Remaining contents of EXPORT section */" );
		line( exportptr );
		nl();
dcw's avatar
dcw committed
143
	}
144
145

	fclose( hfile );
dcw's avatar
dcw committed
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
static void c_declns( basename, globals, d ) char *basename, *globals; declnlist d;
{
	char	tempname[256];
	FILE	*cfile;
	char	*globalptr;

	sprintf( tempname, "%s.c", basename );
	cfile = fopen( tempname, "w" );
	ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );

	usefile( cfile );

	line( "/*" );
	line( " * Automatically Generated by DataDec" );
	line( " */\n" );
	line( "#include <stdio.h>" );
	line( "#include <malloc.h>" );
	line( "#include \"%s.h\"\n\n", basename );

	if( *globals != '\0' )
	{
		line( "/* Contents of GLOBAL section */" );
		for( globalptr=globals; *globalptr; globalptr++ )
		{
			if( *globalptr == '@' && globalptr[1] == '@'
			&&   globalptr[2] == '\n' )
			{
				globalptr += 2;
				break;
			}
			outchar( *globalptr );
		}

		nl();
		nl();
	}

	cons_fns( d );

	print_fns( d );

	if( *globalptr != '\0' )
	{
		nl();
		nl();
		line( "/* Remaining contents of GLOBAL section */" );
		line( globalptr );
		nl();
	}

	fclose( cfile );
}


/* --------------------------- The types ---------------------------------- */


206
static void data_decls( decs ) declnlist decs;
dcw's avatar
dcw committed
207
{
208
209
210
	declnlist	d;
	shapelist	s;
	int		n;
dcw's avatar
dcw committed
211

dcw's avatar
dcw committed
212
	for( d=decs; d != NULL; d=d->next )
dcw's avatar
dcw committed
213
	{
dcw's avatar
dcw committed
214
215
		if( d->Struct )
		{
216
217
			line("struct %s;", d->name);
			line("typedef struct %s *%s;", d->name, d->name );
dcw's avatar
dcw committed
218
219
220
221
		} else
		{
			line( "typedef int %s;", d->name );
		}
dcw's avatar
dcw committed
222
	}
223
	line( "\n" );
dcw's avatar
dcw committed
224
	for( d=decs; d != NULL; d=d->next )
dcw's avatar
dcw committed
225
	{
226
		line( "/* ---- Type %s ---- */\n", d->name );
dcw's avatar
dcw committed
227
		n = 0;
dcw's avatar
dcw committed
228
		if( d->Defines )
dcw's avatar
dcw committed
229
		{
dcw's avatar
dcw committed
230
			for( s = d->shapes; s != NULL; s=s->next )
dcw's avatar
dcw committed
231
232
233
234
235
236
237
238
239
240
241
242
			{
				if( d->Struct )
				{
					line( "#define %s_%s_tag %d",
						d->name, s->name, n++ );
				} else
				{
					line( "#define %s_%s() %d",
						d->name, s->name, n++ );
				}
			}
			line( "" );
dcw's avatar
dcw committed
243
		}
244

dcw's avatar
dcw committed
245
		if( d->Struct )
dcw's avatar
dcw committed
246
		{
dcw's avatar
dcw committed
247
			line( "struct %s {", d->name );
dcw's avatar
dcw committed
248
249
			indent();
			if( d->TagField )
dcw's avatar
dcw committed
250
			{
dcw's avatar
dcw committed
251
252
253
254
255
				line( "int\ttag;" );
			}
			if( d->Union )
			{
				line( "union {" );
256
				indent();
dcw's avatar
dcw committed
257
			}
dcw's avatar
dcw committed
258
			for( s = d->shapes; s != NULL; s=s->next )
dcw's avatar
dcw committed
259
260
261
262
263
264
265
266
267
268
			{
				if( d->Union && s->params )
				/* need internal structures */
				{
					line( "struct {" );
					indent();
					declfields( s->params );
					outdent();
					line( "} %s;", s->name );
				} else
dcw's avatar
dcw committed
269
				{
dcw's avatar
dcw committed
270
					declfields( s->params );
dcw's avatar
dcw committed
271
				}
dcw's avatar
dcw committed
272
273
274
			}
			if( d->Union )
			{
275
				outdent();
dcw's avatar
dcw committed
276
				line( "} u;" );
dcw's avatar
dcw committed
277
			}
dcw's avatar
dcw committed
278
279
			outdent();
			line( "};\n" );
dcw's avatar
dcw committed
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
static void declfields( p ) paramlist p;
{
	for( ; p != NULL; p=p->next )
	{
		line( "%s\t%s;", p->type, p->name );
	}
}


/* ------------------------ Constructor functions ------------------------- */


static void protos( d, prot ) declnlist d; BOOL prot;
{
	shapelist	s;
	shapelist	shapes;

	for( ; d != NULL; d = d->next )
	{
		if( d->Struct )
		{
			shapes = d->shapes;
			if( d->UseNull )
			{
				shapes = shapes->next;
			}
			for( s = shapes; s != NULL; s = s->next )
			{
				one_proto( d->name, s, prot );
			}
		}
		fprintf( outfile, "extern void print_%s(", d->name );
		if( prot )
		{
			fprintf( outfile, " %s ", d->name );
		}
		fputs( ");\n", outfile );
	}
}


dcw's avatar
dcw committed
326
static void one_proto( name, s, prot ) char *name; shapelist s; BOOL prot;
dcw's avatar
dcw committed
327
328
329
330
{
	paramlist p;
	BOOL first;

dcw's avatar
dcw committed
331
	fprintf( outfile, "extern %s %s_%s( ", name, name, s->name );
332
	if( prot )
dcw's avatar
dcw committed
333
334
335
	{
		if( s->params == NULL )
		{
dcw's avatar
dcw committed
336
			fputs( "void ", outfile );
dcw's avatar
dcw committed
337
338
		} else
		{
dcw's avatar
dcw committed
339
			first = TRUE;
dcw's avatar
dcw committed
340
			for( p = s->params; p != NULL ; p=p->next, first=FALSE )
dcw's avatar
dcw committed
341
			{
dcw's avatar
dcw committed
342
343
344
				if( !first ) fputs( ", ", outfile );
				fputs( p->type, outfile );
				fputc( ' ', outfile );
dcw's avatar
dcw committed
345
346
347
			}
		}
	}
dcw's avatar
dcw committed
348
	fputs( ");\n", outfile );
dcw's avatar
dcw committed
349
350
351
}


352
static void cons_fns( d ) declnlist d;
dcw's avatar
dcw committed
353
{
354
	shapelist	s;
dcw's avatar
dcw committed
355
	shapelist	shapes;
dcw's avatar
dcw committed
356

357
	for( ; d != NULL; d=d->next )
dcw's avatar
dcw committed
358
	{
dcw's avatar
dcw committed
359
		if( d->Struct )
dcw's avatar
dcw committed
360
		{
361
			/* We need constructor functions */
dcw's avatar
dcw committed
362
363
364
			shapes = d->shapes;
			if( d->UseNull )
			{
365
				/* Null cons func already made */
dcw's avatar
dcw committed
366
367
				shapes = shapes->next;
			}
368
			for( s = shapes; s != NULL; s=s->next )
dcw's avatar
dcw committed
369
			{
370
371
372
				cons_fn_hdr( d->name, s );
				cons_fn_body( d, d->name, s );
				fprintf( outfile, "\n\n" );
dcw's avatar
dcw committed
373
			}
374
		}
dcw's avatar
dcw committed
375
	}
dcw's avatar
dcw committed
376
377
378
}


dcw's avatar
dcw committed
379
static void cons_fn_hdr( name, s ) char *name; shapelist s;
dcw's avatar
dcw committed
380
381
382
383
{
	paramlist p;
	BOOL first;

dcw's avatar
dcw committed
384
	fprintf( outfile, "%s %s_%s( ", name, name, s->name );
dcw's avatar
dcw committed
385
	first = TRUE;
dcw's avatar
dcw committed
386
	for( p=s->params; p != NULL; p=p->next, first=FALSE )
dcw's avatar
dcw committed
387
	{
dcw's avatar
dcw committed
388
389
		if( !first ) fputs( ", ", outfile );
		fputs( p->name, outfile );
dcw's avatar
dcw committed
390
	}
dcw's avatar
dcw committed
391
392
	fputs( " ) ", outfile );
	for( p=s->params; p != NULL; p=p->next )
dcw's avatar
dcw committed
393
	{
dcw's avatar
dcw committed
394
		fprintf( outfile, "%s %s; ", p->type, p->name );
dcw's avatar
dcw committed
395
	}
dcw's avatar
dcw committed
396
	nl();
dcw's avatar
dcw committed
397
398
399
}


dcw's avatar
dcw committed
400
static void cons_fn_body( d, name, s ) declnlist d; char *name; shapelist s;
dcw's avatar
dcw committed
401
402
403
{
	paramlist p;

dcw's avatar
dcw committed
404
	ASSERT( d->Struct,
405
		("error: cons_fn_body called but d->Struct is FALSE!\n") );
406
407
	line( "{" );
	indent();
dcw's avatar
dcw committed
408
	line( "%s\tnew;\n", name );
dcw's avatar
dcw committed
409
	line( "new = NEW(%s);", name );
dcw's avatar
dcw committed
410
411
412
413
	if( d->TagField )
	{
		line( "new->tag = %s_%s_tag;", name, s->name );
	}
dcw's avatar
dcw committed
414
	for( p=s->params; p != NULL; p=p->next )
dcw's avatar
dcw committed
415
	{
dcw's avatar
dcw committed
416
417
418
419
420
421
422
423
		if( d->Union )
		{
			line( "new->u.%s.%s = %s;",
				s->name, p->name, p->name );
		} else
		{
			line( "new->%s = %s;", p->name, p->name );
		}
dcw's avatar
dcw committed
424
	}
425
426
427
	line( "return new;" );
	outdent();
	line( "}" );
dcw's avatar
dcw committed
428
429
430
}


431
/* -------------------------- Print functions ----------------------------- */
dcw's avatar
dcw committed
432
433


dcw's avatar
dcw committed
434
static void print_fns( d ) declnlist d;
dcw's avatar
dcw committed
435
436
{
	shapelist s;
dcw's avatar
dcw committed
437
	shapelist shapes;
dcw's avatar
dcw committed
438

dcw's avatar
dcw committed
439
	for( ; d != NULL; d = d->next )
dcw's avatar
dcw committed
440
	{
dcw's avatar
dcw committed
441
		shapes = d->shapes;
442
443
444
		line( "void print_%s( f, p ) FILE *f; %s p;",d->name, d->name );
		line( "{" );
		indent();
dcw's avatar
dcw committed
445
		if( d->UseNull )
dcw's avatar
dcw committed
446
		{
dcw's avatar
dcw committed
447
448
449
450
451
452
453
454
455
456
457
458
459
			line( "if( p == NULL )" );
			line( "{" );
			indent();
			print_all_params( d, shapes );
			line( "return;" );
			outdent();
			line( "}" );
			shapes = shapes->next;
		}
		if( d->TagField )
		{
			line( "switch( p->tag )" );
			line( "{" );
dcw's avatar
dcw committed
460
			for( s = shapes; s != NULL; s = s->next )
dcw's avatar
dcw committed
461
462
463
464
465
466
467
468
469
470
471
			{
				print_fn_shape( d, s );
			}
			line( "default:" );
			indent();
			line( "fprintf( stderr," );
			line( " \"print_%s: impossible tag %%d\\n\", p->tag );",
				d->name );
			line( "exit(1);" );
			outdent();
			line( "}" );
dcw's avatar
dcw committed
472
		} else if( ! d->TagField && d->Struct )	/* one shape left */
dcw's avatar
dcw committed
473
474
		{
			print_all_params( d, shapes );
dcw's avatar
dcw committed
475
		} else if( ! d->Struct && d->Defines )	/* enumerated type */
dcw's avatar
dcw committed
476
477
478
		{
			line( "switch( p )" );
			line( "{" );
dcw's avatar
dcw committed
479
			for( s = shapes; s != NULL; s = s->next )
dcw's avatar
dcw committed
480
			{
dcw's avatar
dcw committed
481
				line( "case %s_%s():", d->name, s->name );
dcw's avatar
dcw committed
482
483
484
485
486
487
				indent();
				print_all_params( d, s );
				line( "break;" );
				outdent();
			}
			line( "}" );
dcw's avatar
dcw committed
488
489
490
491
492
		} else
		{
			fprintf( stderr,
				 "datadec: internal error in print_fns\n" );
			exit(1);
dcw's avatar
dcw committed
493
		}
494
495
		outdent();
		line( "}\n" );
dcw's avatar
dcw committed
496
497
498
	}
}

dcw's avatar
dcw committed
499

500
static void print_fn_shape( d, s ) declnlist d; shapelist s;
dcw's avatar
dcw committed
501
{
502
503
504
505
506
	line( "case %s_%s_tag:", d->name, s->name );
	indent();
	print_all_params( d, s );
	line( "break;" );
	outdent();
dcw's avatar
dcw committed
507
508
509
}


510
static void print_all_params( d, s ) declnlist d; shapelist s;
dcw's avatar
dcw committed
511
{
512
513
514
	printlist	pl;
	paramlist	p;
	int		n;
dcw's avatar
dcw committed
515

516
	if( s->pl == NULL )
dcw's avatar
dcw committed
517
	{
518
519
		line( "fputs( \"%s\", f );", s->name );
		if( s->params )
dcw's avatar
dcw committed
520
		{
521
			line( "fputc( '(', f );" );
dcw's avatar
dcw committed
522
		}
523
		for( p = s->params; p != NULL; p = p->next )
dcw's avatar
dcw committed
524
		{
525
			print_param( s->name, p, d->Union );
dcw's avatar
dcw committed
526
		}
527
528
529
530
531
		if( s->params )
		{
			line( "fputc( ')', f );" );
		}
	} else
dcw's avatar
dcw committed
532
	{
533
		for( pl = s->pl; pl != NULL; pl = pl->next )
dcw's avatar
dcw committed
534
		{
535
			if( pl->item->tag == printitem_is_str )
dcw's avatar
dcw committed
536
			{
537
538
539
540
541
542
543
544
545
546
547
				line( "fputs( \"%s\", f );",
					pl->item->str );
			} else
			{
				n = pl->item->num;
				for( p = s->params; --n; p = p->next )
				{
					ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->name,d->name));
				}
				print_param( s->name, p, d->Union );
dcw's avatar
dcw committed
548
549
550
			}
		}
	}
551
}
dcw's avatar
dcw committed
552
553


554
555
556
static void print_param( shape, p, Union ) char *shape; paramlist p; BOOL Union;
{
	char pname[200];
dcw's avatar
dcw committed
557

558
	if( Union )
dcw's avatar
dcw committed
559
	{
560
561
562
563
		sprintf( pname, "p->u.%s.%s", shape, p->name );
	} else
	{
		sprintf( pname, "p->%s", p->name );
dcw's avatar
dcw committed
564
565
	}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
	if( streq( p->type, "int" ) )
	{
		line( "fprintf( f, \"%%d\", %s );", pname );
	} else if( streq( p->type, "char" ) )
	{
		line( "fputc( %s, f );", pname );
	} else if( streq( p->type, "BOOL" ) )
	{
		line( "fputs( %s?\"true\":\"false\", f );", pname );
	} else if( streq( p->type, "string" ) )
	{
		line( "fputs( %s, f );", pname );
	} else
	{
		line( "print_%s( f, %s );", p->type, pname );
	}
dcw's avatar
dcw committed
582
}