aboutsummaryrefslogtreecommitdiff
path: root/tools/cfsml.pl
blob: 53d5ae1634216777bf7d398cf5f33cd998cd0ebd (plain)
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
#! /usr/bin/env perl
# The C File Storage Meta Language "reference" implementation
# This implementation is supposed to conform to version
$version = "0.8.2";
# of the spec. Please contact the maintainer if it doesn't.
#
# cfsml.pl Copyright (C) 1999, 2000, 2001 Christoph Reichenbach
#
#
# This program may be modified and copied freely according to the terms of
# the GNU general public license (GPL), as long as the above copyright
# notice and the licensing information contained herein are preserved.
#
# Please refer to www.gnu.org for licensing details.
#
# This work is provided AS IS, without warranty of any kind, expressed or
# implied, including but not limited to the warranties of merchantibility,
# noninfringement, and fitness for a specific purpose. The author will not
# be held liable for any damage caused by this work or derivatives of it.
#
# By using this source code, you agree to the licensing terms as stated
# above.
#
#
# Please contact the maintainer for bug reports or inquiries.
#
# Current Maintainer:
#
#    Christoph Reichenbach (CJR) [jameson@linuxgames.com]
#
#
# Warning: This is still a "bit" messy. Sorry for that.
#

#$debug = 1;

$write_lines = "true";
$source_file = "CFSML source file";
$type_integer = "integer";
$type_string = "string";
$type_record = "RECORD";
$type_pointer = "POINTER";
$type_abspointer = "ABSPOINTER";

%types;      # Contains all type bindings
%records;    # Contains all record bindings

$mode = undef;
while ($op = shift @ARGV) {
    if ($mode eq undef) {
	if ($op eq "-f") {
	    $mode = "fname";
	} elsif ($op eq "-l") {
	    $write_lines = undef;
	} elsif ($op eq "-v") {
	    print "cfsml.pl, the CFSML code generator, version $version\n";
	    print "This program is provided WITHOUT WARRANTY of any kind. It may be\n";
	    print "copied and modified freely according to the terms of the GNU\n";
	    print "General Public License.\n";
	    exit(0);
	} elsif ($op eq "-h") {
	    print "CFSML help:\n";
	    print "Usage: cat source | cfsml.pl [-v] [-h] [-l] [-f <filename>] > dest\n";
	    print "  -h : help\n";
	    print "  -v : print version\n";
	    print "  -l : disable line number printing in dest file\n";
	    print "  -f : specify file name for line number printing\n";
	    exit(0);
	} else {
	    die "Unknown option '$op'\n";
	}
    } elsif ($mode eq "fname") {
	$source_file = $op;
	$mode = 0;
    } else {
	die "Invalid internal state '$mode'\n";
    }
}

sub write_line_pp
# write_line_pp(int line_nr, bool input_file?)
{
    my $line_nr = shift;
    my $_file = shift;
    my $filename = "cfsml.pl";

    if (_file) {
	$filename = $source_file;
    }

    if ($write_lines) {
	print "#line $line_nr \"$filename\"\n";
    }
}

sub create_string_functions
  {
    $firstline = __LINE__;
    $firstline += 4;
    write_line_pp($firstline, 0);
    print <<'EOF';

#include <stdarg.h> // We need va_lists
#include "sci/include/sci_memory.h"

#ifdef CFSML_DEBUG_MALLOC
/*
#define free(p)        dbg_sci_free(p)
#define malloc(s)      dbg_sci_malloc(s)
#define calloc(n, s)   dbg_sci_calloc(n, s)
#define realloc(p, s)  dbg_sci_realloc(p, s)
*/
#define free        dbg_sci_free
#define malloc      dbg_sci_malloc
#define calloc      dbg_sci_calloc
#define realloc     dbg_sci_realloc
#endif

static void _cfsml_error(const char *fmt, ...) {
	va_list argp;

	fprintf(stderr, "Error: ");
	va_start(argp, fmt);
	vfprintf(stderr, fmt, argp);
	va_end(argp);
}


static struct _cfsml_pointer_refstruct {
	struct _cfsml_pointer_refstruct *next;
	void *ptr;
} *_cfsml_pointer_references = NULL;

static struct _cfsml_pointer_refstruct **_cfsml_pointer_references_current = &_cfsml_pointer_references;

static char *_cfsml_last_value_retrieved = NULL;
static char *_cfsml_last_identifier_retrieved = NULL;

static void _cfsml_free_pointer_references_recursively(struct _cfsml_pointer_refstruct *refs, int free_pointers) {
	if (!refs)
		return;

	_cfsml_free_pointer_references_recursively(refs->next, free_pointers);

	if (free_pointers)
		free(refs->ptr);

	free(refs);
}

static void _cfsml_free_pointer_references(struct _cfsml_pointer_refstruct **meta_ref, int free_pointers) {
	_cfsml_free_pointer_references_recursively(*meta_ref, free_pointers);
	*meta_ref = NULL;
	_cfsml_pointer_references_current = meta_ref;
}

static struct _cfsml_pointer_refstruct **_cfsml_get_current_refpointer() {
	return _cfsml_pointer_references_current;
}

static void _cfsml_register_pointer(void *ptr) {
	struct _cfsml_pointer_refstruct *newref = (struct _cfsml_pointer_refstruct*)sci_malloc(sizeof (struct _cfsml_pointer_refstruct));
	newref->next = *_cfsml_pointer_references_current;
	newref->ptr = ptr;
	*_cfsml_pointer_references_current = newref;
}

static char *_cfsml_mangle_string(const char *s) {
	const char *source = s;
	char c;
	char *target = (char *)sci_malloc(1 + strlen(s) * 2); // We will probably need less than that
	char *writer = target;

	while ((c = *source++)) {
		if (c < 32) { // Special character?
			*writer++ = '\\'; // Escape...
			c += ('a' - 1);
		} else if (c == '\\' || c == '"')
			*writer++ = '\\'; // Escape, but do not change
		*writer++ = c;
	}
	*writer = 0; // Terminate string

	return (char *)sci_realloc(target, strlen(target) + 1);
}

static char *_cfsml_unmangle_string(const char *s, unsigned int length) {
	char *target = (char *)sci_malloc(1 + strlen(s));
	char *writer = target;
	const char *source = s;
	const char *end = s + length;
	char c;

	while ((source != end) && (c = *source++) && (c > 31)) {
		if (c == '\\') { // Escaped character?
    			c = *source++;
			if ((c != '\\') && (c != '"')) // Un-escape 0-31 only
				c -= ('a' - 1);
		}
		*writer++ = c;
	}
	*writer = 0; // Terminate string

	return (char *)sci_realloc(target, strlen(target) + 1);
}

static char *_cfsml_get_identifier(Common::SeekableReadStream *fd, int *line, int *hiteof, int *assignment) {
	int c;
	int mem = 32;
	int pos = 0;
	int done = 0;
	char *retval = (char *)sci_malloc(mem);

	if (_cfsml_last_identifier_retrieved) {
		free(_cfsml_last_identifier_retrieved);
		_cfsml_last_identifier_retrieved = NULL;
	}

	while (isspace(c = SRSgetc(fd)) && (c != EOF));
	if (c == EOF) {
	    _cfsml_error("Unexpected end of file at line %d\n", *line);
	    free(retval);
	    *hiteof = 1;
	    return NULL;
	}

	int first = 1;
	while ((first || (c = SRSgetc(fd)) != EOF) && ((pos == 0) || (c != '\n')) && (c != '=')) {
		first = 0;
		if (pos == mem - 1) // Need more memory?
			retval = (char *)sci_realloc(retval, mem *= 2);
		if (!isspace(c)) {
			if (done) {
				_cfsml_error("Single word identifier expected at line %d\n", *line);
				free(retval);
				return NULL;
			}
			retval[pos++] = c;
		} else
			if (pos != 0)
				done = 1; // Finished the variable name
			else if (c == '\n')
				++(*line);
	}

	if (c == EOF) {
		_cfsml_error("Unexpected end of file at line %d\n", *line);
		free(retval);
		*hiteof = 1;
		return NULL;
	}

	if (c == '\n') {
		++(*line);
		if (assignment)
			*assignment = 0;
	} else
		if (assignment)
			*assignment = 1;

	if (pos == 0) {
		_cfsml_error("Missing identifier in assignment at line %d\n", *line);
		free(retval);
		return NULL;
	}

	if (pos == mem - 1) // Need more memory?
		retval = (char *)sci_realloc(retval, mem += 1);

	retval[pos] = 0; // Terminate string
EOF

if ($debug) {
    print "		printf(\"identifier is '%s'\\n\", retval);\n";
}

  $firstline = __LINE__;
  $firstline += 4;
  write_line_pp($firstline, 0);
  print <<'EOF2';

	return _cfsml_last_identifier_retrieved = retval;
}

static char *_cfsml_get_value(Common::SeekableReadStream *fd, int *line, int *hiteof) {
	int c;
	int mem = 64;
	int pos = 0;
	char *retval = (char *)sci_malloc(mem);

	if (_cfsml_last_value_retrieved) {
		free(_cfsml_last_value_retrieved);
		_cfsml_last_value_retrieved = NULL;
	}

	while (((c = SRSgetc(fd)) != EOF) && (c != '\n')) {
		if (pos == mem - 1) // Need more memory?
			retval = (char *)sci_realloc(retval, mem *= 2);

		if (pos || (!isspace(c)))
			retval[pos++] = c;
	}

	while ((pos > 0) && (isspace(retval[pos - 1])))
		--pos; // Strip trailing whitespace

	if (c == EOF)
		*hiteof = 1;

	if (pos == 0) {
	    _cfsml_error("Missing value in assignment at line %d\n", *line);
	    free(retval);
	    return NULL;
	}

	if (c == '\n')
		++(*line);

	if (pos == mem - 1) // Need more memory?
		retval = (char *)sci_realloc(retval, mem += 1);

	retval[pos] = 0; // Terminate string
EOF2

    if ($debug) {
	print "		printf(\"value is '%s'\\n\", retval);\n";
    }

    $firstline = __LINE__;
    $firstline += 4;
    write_line_pp($firstline, 0);
  print <<'EOF3';
	return (_cfsml_last_value_retrieved = (char *)sci_realloc(retval, strlen(retval) + 1));
	// Re-allocate; this value might be used for quite some while (if we are restoring a string)
}
EOF3
  }


# Call with $expression as a simple expression, like "tos + 1".
# Returns (in this case) ("tos", "-1").
sub lvaluize
  {
    my @retval;
#    print "//DEBUG: $expression [";
    my @tokens = split (/([+-\/\*])/, $expression);
#    print join(",", @tokens);
    $retval[0] = $tokens[0];

    my $rightvalue = "";
    for ($i = 1; $tokens[$i]; $i++) {

      if ($tokens[$i] eq "+") {
	$rightvalue .= "-";
      } elsif ($tokens[$i] eq "-") {
	$rightvalue .= "+";
      } elsif ($tokens[$i] eq "/") {
	$rightvalue .= "*";
      } elsif ($tokens[$i] eq "*") {
	$rightvalue .= "/";
      } else {
	$rightvalue .= $tokens[$i];
      }
    }

    $retval[1] = $rightvalue;

#   print "] => ($retval[0];$retval[1])\n";

    return @retval;
  }



sub create_declaration
  {
    $typename = $type;
    $ctype = $types{$type}->{'ctype'};

    if (not $types{$type}->{'external'}) {
      $types{$type}{'writer'} = "_cfsml_write_" . $typename;
      $types{$type}{'reader'} = "_cfsml_read_" . $typename;
      write_line_pp(__LINE__, 0);
      print "static void $types{$type}{'writer'}(Common::WriteStream *fh, $ctype* save_struc);\n";
      print "static int $types{$type}{'reader'}(Common::SeekableReadStream *fh, $ctype* save_struc, const char *lastval, int *line, int *hiteof);\n\n";
    };

  }

sub create_writer
  {
    $typename = $type;
    $ctype = $types{$type}{'ctype'};

    write_line_pp(__LINE__, 0);
    print "static void\n_cfsml_write_$typename(Common::WriteStream *fh, $ctype* save_struc)\n{\n";
    if ($types{$type}{'type'} eq $type_record) {
        print "	int min, max, i;\n\n";
    }

    if ($types{$type}{'type'} eq $type_integer) {
      print "	WSprintf(fh, \"%li\", (long)*save_struc);\n";
    }
    elsif ($types{$type}{'type'} eq $type_string) {
    write_line_pp(__LINE__, 0);
    print "	if (!(*save_struc))\n";
    print "		WSprintf(fh, \"\\\\null\\\\\");\n";
    print "	else {\n";
    print "		char *token = _cfsml_mangle_string((const char *) *save_struc);\n";
    print "		WSprintf(fh, \"\\\"%s\\\"\", token);\n";
    print "		free(token);\n";
    print "	}\n";
    }
    elsif ($types{$type}{'type'} eq $type_record) {
    write_line_pp(__LINE__, 0);
    print "	WSprintf(fh, \"{\\n\");\n";

    for $n (@{$records{$type}}) {

        print "	WSprintf(fh, \"$n->{'name'} = \");\n";

        if ($n->{'array'}) { # Check for arrays

        if ($n->{'array'} eq 'static' or $n->{'size'} * 2) { # fixed integer value?
            print "	min = max = $n->{'size'};\n";
        }
        else { # No, a variable
            print "	min = max = save_struc->$n->{'size'};\n";
        }

        if ($n->{'maxwrite'}) { # A write limit?
            print "	if (save_struc->$n->{'maxwrite'} < min)\n";
            print "		min = save_struc->$n->{'maxwrite'};\n";
        }

        if ($n->{'array'} eq 'dynamic') {
            print "	if (!save_struc->$n->{'name'})\n";
            print "		min = max = 0; /* Don't write if it points to NULL */\n";
        }

        write_line_pp(__LINE__, 0);
        print "	WSprintf(fh, \"[%d][\\n\", max);\n";
        print "	for (i = 0; i < min; i++) {\n";
        print "		$types{$n->{'type'}}{'writer'}";
        my $subscribstr = "[i]"; # To avoid perl interpolation problems
        print "(fh, &(save_struc->$n->{'name'}$subscribstr));\n";
        print "		WSprintf(fh, \"\\n\");\n";
        print "	}\n";
        print "	WSprintf(fh, \"]\");\n";

    } elsif ($n->{'type'} eq $type_pointer) { # Relative pointer

      print "	WSprintf(fh, \"%d\", save_struc->$n->{'name'} - save_struc->$n->{'anchor'}); // Relative pointer\n";

      } elsif ($n->{'type'} eq $type_abspointer) { # Absolute pointer

      print "	if (!save_struc->$n->{'name'})\n";
      print "		WSprintf(fh, \"\\\\null\\\\\");\n";
      print "	else \n";
      print "		$types{$n->{'reftype'}}{'writer'}";
      print "(fh, save_struc->$n->{'name'});\n";

    } else { # Normal record entry

      print "	$types{$n->{'type'}}{'writer'}";
      print "(fh, ($types{$n->{'type'}}{'ctype'}*) &(save_struc->$n->{'name'}));\n";

    }

    print "	WSprintf(fh, \"\\n\");\n";
      }

      print "	WSprintf(fh, \"}\");\n";
    }
    else {
      print STDERR "Warning: Attempt to create_writer for invalid type '$types{$type}{'type'}'\n";
    }
    print "}\n\n";

  }


sub create_reader
  {
    $typename = $type;
    $ctype = $types{$type}{'ctype'};

    write_line_pp(__LINE__, 0);
    print "static int\n_cfsml_read_$typename(Common::SeekableReadStream *fh, $ctype* save_struc, const char *lastval, int *line, int *hiteof)\n{\n";
    print "	char *token;\n";
    if ($types{$type}{'type'} eq $type_record) {
      print "	int min, max, i;\n";
    }
    my $reladdress_nr = 0; # Number of relative addresses needed
    my $reladdress = 0; # Current relative address number
    my $reladdress_resolver = ""; # Relative addresses are resolved after the main while block

    if ($types{$type}{'type'} eq $type_record) {

      foreach $n (@{$records{$type}}) { # Count relative addresses we need
	if ($n->{'type'} eq $type_pointer) {
	  ++$reladdress_nr;
	}
      }

      if ($reladdress_nr) { # Allocate stack space for all relative addresses needed
	print "	int reladdresses[$reladdress_nr] = {0};\n";
      }
    }

    if ($types{$type}{'type'} eq $type_integer) {
	write_line_pp(__LINE__, 0);
	print "\n	*save_struc = strtol(lastval, &token, 0);\n";
	print "	if ((*save_struc == 0) && (token == lastval)) {\n";
	print "		_cfsml_error(\"strtol failed at line %d\\n\", *line);\n";
	print "		return CFSML_FAILURE;\n";
	print "	}\n";
	print "	if (*token != 0) {\n";
	print "		_cfsml_error(\"Non-integer encountered while parsing int value at line %d\\n\", *line);\n";
	print "		return CFSML_FAILURE;\n";
	print "	}\n";
	print "	return CFSML_SUCCESS;\n";
    } elsif ($types{$type}{'type'} eq $type_string) {
	write_line_pp(__LINE__, 0);
	print "\n";
	print "	if (strcmp(lastval, \"\\\\null\\\\\")) { // null pointer?\n";
	print "		unsigned int length = strlen(lastval);\n";
	print "		if (*lastval == '\"') { // Quoted string?\n";
	print "			while (lastval[length] != '\"')\n";
	print "				--length;\n\n";
	print "			if (!length) { // No matching double-quotes?\n";
	print "				_cfsml_error(\"Unbalanced quotes at line %d\\n\", *line);\n";
	print "				return CFSML_FAILURE;\n";
	print "			}\n\n";
	print "			lastval++; // ...and skip the opening quotes locally\n";
	print "			length--;\n";
	print "		}\n";
	print "		*save_struc = _cfsml_unmangle_string(lastval, length);\n";
	print "		_cfsml_register_pointer(*save_struc);\n";
	print "		return CFSML_SUCCESS;\n";
	print "	} else {\n";
	print "		*save_struc = NULL;\n";
	print "		return CFSML_SUCCESS;\n";
	print "	}\n";
    } elsif ($types{$type}{'type'} eq $type_record) {
	write_line_pp(__LINE__, 0);
	print "	int assignment, closed, done;\n\n";
	print "	if (strcmp(lastval, \"{\")) {\n";
	print "		_cfsml_error(\"Reading record $type; expected opening braces in line %d, got \\\"%s\\\"\\n\", *line, lastval);\n";
	print "		return CFSML_FAILURE;\n";
	print "	};\n";
	print "	closed = 0;\n";
	print "	do {\n";
	print "		const char *value;\n";
	print "		token = _cfsml_get_identifier(fh, line, hiteof, &assignment);\n\n";
	print "		if (!token) {\n";
	print "			_cfsml_error(\"Expected token at line %d\\n\", *line);\n";
	print "			return CFSML_FAILURE;\n";
	print "		}\n";
	print "		if (!assignment) {\n";
	print "			if (!strcmp(token, \"}\")) \n";
	print "				closed = 1;\n";
	print "			else {\n";
	print "				_cfsml_error(\"Expected assignment or closing braces in line %d\\n\", *line);\n";
	print "				return CFSML_FAILURE;\n";
	print "			}\n";
	print "		} else {\n";
	print "			value = \"\";\n";
	print "			while (!value || !strcmp(value, \"\"))\n";
	print "				value = _cfsml_get_value(fh, line, hiteof);\n";
	print "			if (!value) {\n";
	print "				_cfsml_error(\"Expected token at line %d\\n\", *line);\n";
	print "				return CFSML_FAILURE;\n";
	print "			}\n";
#	print "		}\n";


      foreach $n (@{$records{$type}}) { # Now take care of all record elements

	my $type = $n->{'type'};
	my $reference = undef;
	if ($type eq $type_abspointer) {
	    $reference = 1;
	    $type = $n->{'reftype'};
	}
	my $name = $n->{'name'};
	my $reader = $types{$type}{'reader'};
	my $size = $n->{'size'};

	print "				if (!strcmp(token, \"$name\")) {\n";

	if ($type eq $type_pointer) { # A relative pointer

	  $reader = $types{'int'}{'reader'}; # Read relpointer as int

	  write_line_pp(__LINE__, 0);
	  print "				if ($reader(fh, &(reladdresses[$reladdress]), value, line, hiteof)) {\n";
	  print "					_cfsml_error(\"Expected token at line %d\\n\", *line);\n";
	  print "					return CFSML_FAILURE;\n";
	  print "				}\n";

	  # Make sure that the resulting variable is interpreted correctly
	  $reladdress_resolver .= "			save_struc->$n->{'name'} = save_struc->$n->{'anchor'} + reladdresses[$reladdress];\n";

	  ++$reladdress; # Prepare reladdress for next element

	} elsif ($n->{'array'}) { # Is it an array?
	    write_line_pp(__LINE__, 0);
	    print "			if ((value[0] != '[') || (value[strlen(value) - 1] != '[')) {\n";
	    # The value must end with [, since we're starting array data, and it must also
	    # begin with [, since this is either the only character in the line, or it starts
	    # the "amount of memory to allocate" block
	    print "				_cfsml_error(\"Opening brackets expected at line %d\\n\", *line);\n";
	    print "				return CFSML_FAILURE;\n";
	    print "			}\n";

	  if ($n->{'array'} eq 'dynamic') {
	      write_line_pp(__LINE__, 0);
	    # We need to allocate the array first
	    print "			// Prepare to restore dynamic array\n";
	    # Read amount of memory to allocate
	    print "			max = strtol(value + 1, NULL, 0);\n";
	    print "			if (max < 0) {\n";
	    print "				_cfsml_error(\"Invalid number of elements to allocate for dynamic array '%s' at line %d\\n\", token, *line);\n";
	    print "				return CFSML_FAILURE;\n";
	    print "			}\n\n";

	    print "			if (max) {\n";
	    print "				save_struc->$name = ($n->{'type'} *)sci_malloc(max * sizeof($type));\n";
	    print "#ifdef SATISFY_PURIFY\n";
            print "				memset(save_struc->$name, 0, max * sizeof($type));\n";
	    print "#endif\n";
	    print "				_cfsml_register_pointer(save_struc->$name);\n";
	    print "			} else\n";
	    print "				save_struc->$name = NULL;\n"

	    } else { # static array
		print "			// Prepare to restore static array\n";
		print "			max = $size;\n";
	    }

	    write_line_pp(__LINE__, 0);
	    print "			done = i = 0;\n";
	    print "			do {\n";
	    if ($type eq $type_record) {
		print "			if (!(value = _cfsml_get_value(fh, line, hiteof))) {\n";
	    } else {
		print "			if (!(value = _cfsml_get_identifier(fh, line, hiteof, NULL))) {\n";
	    }
	    write_line_pp(__LINE__, 0);
	    
	    print "				_cfsml_error(\"Token expected at line %d\\n\", *line);\n";
	    print "				return 1;\n";
	    print "			}\n";
	    print "			if (strcmp(value, \"]\")) {\n";
	    print "				if (i == max) {\n";
	    print "					_cfsml_error(\"More elements than space available (%d) in '%s' at line %d\\n\", max, token, *line);\n";
	    print "					return CFSML_FAILURE;\n";
	    print "				}\n";
	    my $helper = "[i++]";
	    print "				if ($reader(fh, &(save_struc->$name$helper), value, line, hiteof)) {\n";
	    print "					_cfsml_error(\"Token expected by $reader() for $name$helper at line %d\\n\", *line);\n";
	    print "					return CFSML_FAILURE;\n";
	    print "				}\n";
	    print "			} else \n";
	    print "				done = 1;\n";
	    print "			} while (!done);\n";

	    if ($n->{'array'} eq "dynamic") {
		my @xpr = lvaluize($expression = $n->{'size'});
		print "		 	save_struc->$xpr[0] = max $xpr[1]; // Set array size accordingly\n";
	    }

	    if ($n->{'maxwrite'}) {
		my @xpr = lvaluize($expression = $n->{'maxwrite'});
		print "			save_struc->$xpr[0] = i $xpr[1]; // Set number of elements\n";
	    }

	}
	elsif ($reference) {
	    write_line_pp(__LINE__, 0);
	    print "			if (strcmp(value, \"\\\\null\\\\\")) { // null pointer?\n";
	    print "				save_struc->$name = sci_malloc(sizeof ($type));\n";
	    print "				_cfsml_register_pointer(save_struc->$name);\n";
	    print "				if ($reader(fh, save_struc->$name, value, line, hiteof)) {\n";
	    print "					_cfsml_error(\"Token expected by $reader() for $name at line %d\\n\", *line);\n";
	    print "					return CFSML_FAILURE;\n";
	    print "				}\n";
	    print "			} else \n";
	    print "				save_struc->$name = NULL;\n";
	}
	else { # It's a simple variable or a struct
	    write_line_pp(__LINE__, 0);
	    print "				if ($reader(fh, ($types{$type}{'ctype'}*) &(save_struc->$name), value, line, hiteof)) {\n";
	    print "					_cfsml_error(\"Token expected by $reader() for $name at line %d\\n\", *line);\n";
	    print "					return CFSML_FAILURE;\n";
	    print "				}\n";
	}
	print "			} else\n";

      }
	write_line_pp(__LINE__, 0);
	print "			{\n";
	print "				_cfsml_error(\"$type: Assignment to invalid identifier '%s' in line %d\\n\", token, *line);\n";
	print "				return CFSML_FAILURE;\n";
	print "			}\n";
	print "		}\n";

      print "	} while (!closed); // Until closing braces are hit\n";

      print $reladdress_resolver; # Resolves any relative addresses

      print "	return CFSML_SUCCESS;\n";
    } else {
      print STDERR "Warning: Attempt to create_reader for invalid type '$types{$type}{'type'}'\n";
    }

    print "}\n\n";
  }

# Built-in types

%types = (
	  'int' => {
		    'type' => $type_integer,
		    'ctype' => "int",
		   },

	  'string' => {
		       'type' => $type_string,
		       'ctype' => "char *",
		      },
	 );



sub create_function_block {
  print "\n// Auto-generated CFSML declaration and function block\n\n";
  write_line_pp(__LINE__, 0);
  print "#define CFSML_SUCCESS 0\n";
  print "#define CFSML_FAILURE 1\n\n";
  create_string_functions;

  foreach $n ( keys %types ) {
    create_declaration($type = $n);
  }

  foreach $n ( keys %types ) {
    if (not $types{$n}->{'external'}) {
      create_writer($type = $n);
      create_reader($type = $n);
    }
  }
  print "\n// Auto-generated CFSML declaration and function block ends here\n";
  print "// Auto-generation performed by cfsml.pl $version \n";
}


# Gnerates code to read a data type
# Parameters: $type: Type to read
#             $datap: Pointer to the write destination
#             $fh: Existing filehandle of an open file to use
#             $eofvar: Variable to store _cfsml_eof into
sub insert_reader_code {
  print "// Auto-generated CFSML data reader code\n";
  write_line_pp(__LINE__, 0);
  print "	{\n";
  if (!$linecounter) {
      write_line_pp(__LINE__, 0);
      print "		int _cfsml_line_ctr = 0;\n";
      $linecounter = '_cfsml_line_ctr';
  }
  if ($atomic) {
      write_line_pp(__LINE__, 0);
      print "		struct _cfsml_pointer_refstruct **_cfsml_myptrrefptr = _cfsml_get_current_refpointer();\n";
  }
  write_line_pp(__LINE__, 0);
  print "		int _cfsml_eof = 0, _cfsml_error;\n";
  print "		int dummy;\n";

  if ($firsttoken) {
      write_line_pp(__LINE__, 0);
      print "		const char *_cfsml_inp = $firsttoken;\n";
  } else {
      write_line_pp(__LINE__, 0);
      print "		const char *_cfsml_inp = _cfsml_get_identifier($fh, &($linecounter), &_cfsml_eof, &dummy);\n\n";
  }

  write_line_pp(__LINE__, 0);
  print "		_cfsml_error = $types{$type}{'reader'}($fh, $datap, _cfsml_inp, &($linecounter), &_cfsml_eof);\n";

  if ($eofvar) {
      write_line_pp(__LINE__, 0);
      print "		$eofvar = _cfsml_error;\n";
  }
  if ($atomic) {
      write_line_pp(__LINE__, 0);
      print "		_cfsml_free_pointer_references(_cfsml_myptrrefptr, _cfsml_error);\n";
  }
  write_line_pp(__LINE__, 0);
  print "		if (_cfsml_last_value_retrieved) {\n";
  print "			free(_cfsml_last_value_retrieved);\n";
  print "			_cfsml_last_value_retrieved = NULL;\n";
  print "		}\n";
  print "		if (_cfsml_last_identifier_retrieved) {\n";
  print "			free(_cfsml_last_identifier_retrieved);\n";
  print "			_cfsml_last_identifier_retrieved = NULL;\n";
  print "		}\n";
  print "	}\n";
  print "// End of auto-generated CFSML data reader code\n";
}

# Generates code to write a data type
# Parameters: $type: Type to write
#             $datap: Pointer to the write destination
#             $fh: Existing filehandle of an open file to use
sub insert_writer_code {
    write_line_pp(__LINE__, 0);
    print "// Auto-generated CFSML data writer code\n";
    print "	$types{$type}{'writer'}($fh, $datap);\n";
    print "	WSprintf($fh, \"\\n\");\n";
    print "// End of auto-generated CFSML data writer code\n";
}


################
# Main program #
################

$parsing = 0;
$struct = undef; # Not working on a struct
$commentmode = undef;
$line = 0;

while (<STDIN>) {

  $line++;

  if ($parsing) {
    ($data) = split "#"; # Remove shell-style comments
    @_ = ($data);

    s/\/\*.*\*\///g; # Remove C-style one-line comments

    ($data) = split "\/\/"; # Remove C++-style comments
    @_ = ($data);

    if ($commentmode) {

      if (grep /\*\//, $_) {
	($empty, $_) = split /\*\//;
      } else {
	@_ = (); # Empty line
      }

    } else {
      if (grep /\/\*/, $_) {
	$commentmode = 1;
	($_) = split /\/\*/;
      }
    }


    # Now tokenize:
    s/;//;
    split /(\".*\"|[,\[\]\(\)\{\}])|\s+/;

    @items = @_;

    @tokens = ();

    $tokens_nr = 0;
    for ($n = 0; $n < scalar @items; $n++) { # Get rid of all undefs
      if ($_[$n]) {
	$_ = $items[$n];
	s/\"//g;
	$tokens[$tokens_nr++] = $_;
      }
    }

    # Now all tokens are in @tokens, and we have $tokens_nr of them.

#    print "//DEBUG: " . join ("|", @tokens) . "\n";

    if ($tokens_nr) {
      if ($tokens_nr == 2 && $tokens[0] eq "%END" && $tokens[1] eq "CFSML") {

	$struct && die "Record $struct needs closing braces in intput file (line $line).";

	$parsing = 0;
	create_function_block;
	my $linep = $line + 1;
	write_line_pp($linep, 1);
      } elsif ($struct) { # Parsing struct
	if ($tokens_nr == 1) {
	  if ($tokens[0] eq "}") {
	    $struct = undef;
	  } else { die "Invalid declaration of $token[0] in input file (line $line)\n";};
	} else { # Must be a member declaration

	  my @structrecs = (@{$records{$struct}});
	  my $newidx = (scalar @structrecs) or "0";
	  my %member = ();
	  $member{'name'} = $tokens[1];
	  $member{'type'} = $tokens[0];

	  if ($tokens_nr == 3 && $tokens[1] == "*") {
	      $tokens_nr = 2;
	      $member{'name'} = $tokens[2];
	      $member{'reftype'} = $tokens[0];
	      $member{'type'} = $type_abspointer;
	  }

	  if ($tokens_nr == 4 and $tokens[0] eq $type_pointer) { # Relative pointer

	    if (not $tokens[2] eq "RELATIVETO") {
	      die "Invalid relative pointer declaration in input file (line $line)\n";
	    }

	    $member{'anchor'} = $tokens[3]; # RelPointer anchor

	  } else { # Non-pointer

	    if (not $types{$tokens[0]}) {
	      die "Unknown type $tokens[0] used in input file (line $line)\n";
	    }

	    if ($tokens_nr > 2) { # Array

	      if ($tokens[2] ne "[") {
		die "Invalid token '$tokens[2]' in input file (line $line)\n";
	      }

	      $member{'array'} = "static";

	      if ($tokens[$tokens_nr - 1] ne "]") {
		die "Array declaration incorrectly terminated in input file (line $line)\n";
	      }

	      $parsepos = 3;

	      while ($parsepos < $tokens_nr) {

		if ($tokens[$parsepos] eq ",") {

		  $parsepos++;

		} elsif ($tokens[$parsepos] eq "STATIC") {

		  $member{'array'} = "static";
		  $parsepos++;

		} elsif ($tokens[$parsepos] eq "DYNAMIC") {

		  $member{'array'} = "dynamic";
		  $parsepos++;

		} elsif ($tokens[$parsepos] eq "MAXWRITE") {

		  $member{'maxwrite'} = $tokens[$parsepos + 1];
		  $parsepos += 2;

		} elsif ($tokens[$parsepos] eq "]") {

		  $parsepos++;
		  if ($parsepos != $tokens_nr) {
		    die "Error: Invalid tokens after array declaration in input file (line $line)\n";

		  }
		} else {

		  if ($member{'size'}) {
		    die "Attempt to use more than one array size in input file (line $line)\n" .
		      "(Original size was \"$member->{'size'}\", new size is \"$tokens[$parsepos]\"\n";
		  }

		  $member{'size'} = $tokens[$parsepos];
		  $parsepos++;
		}
	      }


	      unless ($member{'size'}) {
		die "Array declaration without size in input file (line $line)\n";
	      }
	    }
	  }

	  @{$records{$struct}}->[$newidx] = \%member;
	}
      } else { # not parsing struct; normal operation.

	if ($tokens[0] eq "TYPE") { # Simple type declaration

	  my $newtype = $tokens[1];

	  $types{$newtype}->{'ctype'} = $tokens[2];

	  if ($tokens_nr == 5) { # must be ...LIKE...

	    unless ($tokens[3] eq "LIKE") {
	      die "Invalid TYPE declaration in input file (line $line)\n";
	    }

	    $types{$newtype}->{'type'} = $types{$tokens[4]}->{'type'};
	    $types{$newtype}->{'reader'} = $types{$tokens[4]}->{'reader'};
	    $types{$newtype}->{'writer'} = $types{$tokens[4]}->{'writer'};

	  } elsif ($tokens_nr == 6) { # must be ...USING...

	    unless ($tokens[3] eq "USING") {
	      die "Invalid TYPE declaration in input file (line $line)\n";
	    }

	    $types{$newtype}->{'writer'} = $tokens[4];
	    $types{$newtype}->{'reader'} = $tokens[5];
	    $types{$newtype}->{'external'} = 'T';

	  } else {
	    die "Invalid TYPE declaration in input file (line $line)\n";
	  }

	} elsif ($tokens[0] eq "RECORD") {

	  $struct = $tokens[1];
	  if ($types{$struct}) {
	    die "Attempt to re-define existing type $struct as a struct in input file (line $line)";
	  }
	  $types{$struct}{'type'} = $type_record;
	  if ($tokens_nr < 3 or $tokens_nr > 6 or $tokens[$tokens_nr - 1] ne "{") {
	    die "Invalid record declaration in input file (line $line)";
	  }

	  my $extoffset = 2;

	  if ($tokens_nr > 3) {
	      if ($tokens[2] ne "EXTENDS") { # Record declaration with explicit c type
		  $types{$struct}{'ctype'} = $tokens[2];
		  $extoffset = 3;
	      } else { # Record name is the same as the c type name
		  $types{$struct}{'ctype'} = $struct;
	      }
	  } elsif ($tokens_nr == 3) {
		  $types{$struct}{'ctype'} = $struct;
	  }

	  if (($tokens_nr > $extoffset + 1) && ($extoffset + 1 <= $tokens_nr)) {
	      if ($tokens[$extoffset] ne "EXTENDS") {
		  die "Invalid or improper keyword \"$tokens[$extoffset]\" in input file (line $line)";
	      }
	      if ($extoffset + 2 >= $tokens_nr) {
		  die "RECORD \"$struct\" extends on unspecified type in input file (line $line)";
	      }
	      my $ext_type = $tokens[$extoffset + 1];

	      if (!($types{$ext_type}{type} eq $type_record)) {
		  print "$types{$ext_type}{type}";
		  die "RECORD \"$struct\" attempts to extend non-existing or non-record type \"$ext_type\" in input file (line $line)";
	      }

	      (@{$records{$struct}}) = (@{$records{$ext_type}}); # Copy type information from super type
	  }

	} else {
	  die "Invalid declaration \"$tokens[0]\" in line $line";
	}
      }
    }


  } else {

    ($subtoken) = split ";"; # Get rid of trailing ;s
    $tokens_nr = @tokens = split " ", $subtoken;

    if ($tokens_nr == 1 && $tokens[0] eq "%CFSML") {

      $parsing = 1;

    } elsif ($tokens[0] eq "%CFSMLWRITE" and $tokens[3] eq "INTO" and $tokens_nr >= 5) {

      insert_writer_code($type = $tokens[1], $datap = $tokens[2], $fh = $tokens[4]);
      my $templine = $line + 1;
      write_line_pp($templine, 1); # Yes, this sucks.

    } elsif (($tokens[0] eq "%CFSMLREAD") or ($tokens[0] eq "%CFSMLREAD-ATOMIC") and $tokens[3] eq "FROM" and $tokens_nr >= 5) {

      my $myeofvar = 0;
      my $myfirsttoken = 0;
      my $mylinecounter = 0;

      my $idcounter = 5;

      while ($idcounter < $tokens_nr) {
	if ($tokens[$idcounter] eq "ERRVAR" and $tokens_nr >= $idcounter + 2) {
	  $myeofvar = $tokens[$idcounter + 1];
	  $idcounter += 2;
	} elsif ($tokens[$idcounter] eq "FIRSTTOKEN" and $tokens_nr >= $idcounter + 2) {
	  $myfirsttoken = $tokens[$idcounter + 1];
	  $idcounter += 2;
	} elsif ($tokens[$idcounter] eq "LINECOUNTER" and $tokens_nr >= $idcounter + 2) {
	  $mylinecounter = $tokens[$idcounter + 1];
	  $idcounter += 2;
	} else {
	  die "Unknown %CFSMLREAD operational token: $tokens[$idcounter]\n";
	}
      }
      insert_reader_code($type = $tokens[1], $datap = $tokens[2],
			 $fh = $tokens[4], $eofvar = $myeofvar, $firsttoken = $myfirsttoken,
			$linecounter = $mylinecounter, $atomic = ($tokens[0] eq "%CFSMLREAD-ATOMIC"));
      my $templine = $line + 1;
      write_line_pp($templine, 1); # Yes, this sucks, too.

    } else {
      print;
    }
  }

}

if ($parsing) {
  print <STDERR>, "Warning: Missing %END CFSML\n";
}