File: scxml.c

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (1114 lines) | stat: -rw-r--r-- 26,724 bytes parent folder | download
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
/*
 * Copyright (C) 2015-2016 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "scxml.h"

int get_names(struct scxml *scxml, char **names)
{
	struct scxml_datamodel *model;
	struct scxml_data *data;
	int n_names = 0;

	for (model = scxml->model_first; model; model = model->next) {
		for(data = model->data_first; data; data = data->next) {
			if (n_names == MAX_DATA) {
				fprintf(stderr, "datamodel has more than MAX_DATA data.\n");
				assert(0);
			}
			names[n_names++] = strdup(data->id);
		}
	}

	return n_names;
}

int get_states(struct scxml *scxml, char **states)
{
	struct scxml_state *state;
	int n_states = 0;
	for (n_states = 0, state = scxml->state_first; state; state = state->next) {
		if (n_states == MAX_STATES) {
			fprintf(stderr, "scxml has more than MAX_STATE states.\n");
			assert(0);
		}
		states[n_states++] = strdup(state->id);
	}
	return n_states;
}

void free_if(struct scxml_if *xif)
{
	struct scxml_elif *elif;
	struct scxml_elif *elif_tmp;
	struct scxml_else *xelse;
	struct scxml_else *xelse_tmp;
	struct scxml_if *subif;
	struct scxml_if *subif_tmp;
	struct scxml_assign *assign;
	struct scxml_assign *assign_tmp;
	struct scxml_extern *ex;
	struct scxml_extern *ex_tmp;

	for (subif = xif->if_first; subif; subif = subif_tmp) {
		subif_tmp = subif->next;
		free_if(subif);
	}

	for (ex = xif->ex_first; ex; ex = ex_tmp) {
		ex_tmp = ex->next;
		free(ex);
	}

	for (assign = xif->assign_first; assign; assign = assign_tmp) {
		assign_tmp = assign->next;
		free(assign);
	}

	for (elif = xif->elif_first; elif; elif = elif_tmp) {
		for (assign = elif->assign_first; assign; assign = assign_tmp) {
			assign_tmp = assign->next;
			free(assign);
		}
		for (ex = elif->ex_first; ex; ex = ex_tmp) {
			ex_tmp = ex->next;
			free(ex);
		}
		for (subif = elif->if_first; subif; subif = subif_tmp) {
			subif_tmp = subif->next;
			free_if(subif);
		}
		elif_tmp = elif->next;
		free(elif);
	}

	for (xelse = xif->else_first; xelse; xelse = xelse_tmp) {
		for (assign = xelse->assign_first; assign; assign = assign_tmp) {
			assign_tmp = assign->next;
			free(assign);
		}
		for (ex = xelse->ex_first; ex; ex = ex_tmp) {
			ex_tmp = ex->next;
			free(ex);
		}
		for (subif = xelse->if_first; subif; subif = subif_tmp) {
			subif_tmp = subif->next;
			free_if(subif);
		}
		xelse_tmp = xelse->next;
		free(xelse);
	}

	free(xif);
}

void scxml_free(struct scxml *scxml)
{
	struct scxml_state *state;
	struct scxml_state *state_tmp;
	struct scxml_trans *trans;
	struct scxml_trans *trans_tmp;
	struct scxml_assign *assign;
	struct scxml_assign *assign_tmp;
	struct scxml_extern *ex;
	struct scxml_extern *ex_tmp;
	struct scxml_if *xif;
	struct scxml_if *xif_tmp;
	struct scxml_onentry *onentry;
	struct scxml_onentry *onentry_tmp;
	struct scxml_onexit *onexit;
	struct scxml_onexit *onexit_tmp;
	struct scxml_datamodel *model;
	struct scxml_datamodel *model_tmp;
	struct scxml_data *data;
	struct scxml_data *data_tmp;
	struct scxml_function *func;
	struct scxml_function *func_tmp;

	for (func = scxml->func_first; func; func = func_tmp) {
		for (model = func->model_first; model; model = model_tmp) {
			for (data = model->data_first; data; data = data_tmp) {
				data_tmp = data->next;
				free(data);
			}
			model_tmp = model->next;
			free(model);
		}
		func_tmp = func->next;
		free(func);
	}

	for (model = scxml->model_first; model; model = model_tmp) {
		for (data = model->data_first; data; data = data_tmp) {
			data_tmp = data->next;
			free(data);
		}
		model_tmp = model->next;
		free(model);
	}

	for (state = scxml->state_first; state; state = state_tmp) {
		for (onentry = state->onentry_first; onentry; onentry= onentry_tmp) {
			for (xif = onentry->if_first; xif; xif = xif_tmp) {
				xif_tmp = xif->next;
				free_if(xif);
			}
			for (ex = onentry->ex_first; ex; ex = ex_tmp) {
				ex_tmp = ex->next;
				free(ex);
			}
			for (assign = onentry->assign_first; assign; assign = assign_tmp) {
				assign_tmp = assign->next;
				free(assign);
			}
			onentry_tmp = onentry->next;
			free(onentry);
		}
		for (xif = state->if_first; xif; xif = xif_tmp) {
			xif_tmp = xif->next;
			free_if(xif);
		}
		for (assign = state->assign_first; assign; assign = assign_tmp) {
			assign_tmp = assign->next;
			free(assign);
		}
		for (ex = state->ex_first; ex; ex = ex_tmp) {
			ex_tmp = ex->next;
			free(ex);
		}
		for (trans = state->trans_first; trans; trans = trans_tmp) {
			trans_tmp = trans->next;
			free(trans);

		}
		for (onexit = state->onexit_first; onexit; onexit= onexit_tmp) {
			for (xif = onexit->if_first; xif; xif = xif_tmp) {
				xif_tmp = xif->next;
				free_if(xif);
			}
			for (ex = onexit->ex_first; ex; ex = ex_tmp) {
				ex_tmp = ex->next;
				free(ex);
			}
			for (assign = onexit->assign_first; assign; assign = assign_tmp) {
				assign_tmp = assign->next;
				free(assign);
			}
			onexit_tmp = onexit->next;
			free(onexit);
		}
		state_tmp = state->next;
		free(state);
	}
	free(scxml);
}

int check_balance(const char *cond, char open, char close) {
	int i;
	int cnt;
	if (!cond) {
		return 0;
	}
	for (i = 0, cnt = 0; cond[i] != '\0'; i++) {
		if (cond[i] == open) {
			cnt++;
		} else if (cond[i] == close) {
			cnt--;
			if (cnt < 0) {
				fprintf(stderr, "parantheses error for () in boolean:\n%s\n", cond);
				assert(0);
			}
		}
	}
	if (cnt != 0) {
		fprintf(stderr, "parantheses error for () in boolean:\n%s\n", cond);
		assert(0);
	}
	return 0;
}

void check_cond(char **names, int n_names, char **states, int n_states, const char *cond)
{
	int i;
	char tmp[MAX_BOOL];
	char *elements[MAX_BOOL / 2];
	static const char *delim = " \n\t!()[]{}";
	if (!cond) {
		return;
	}
	strncpy(tmp, cond, MAX_BOOL);
	for (i = 0, elements[i] = strtok(tmp, delim); elements[i]; elements[++i] = strtok(NULL, delim)) {
		if (i % 2 != 0) {
			if (strcmp(elements[i], "and") != 0
			    && strcmp(elements[i], "or") != 0) {
				fprintf(stderr, "error in boolean:\n%s\nelement %i should be 'or' or 'and' but is '%s'\n", cond, i, elements[i]);
				//assert(0);
			}
		} else {
			int j;
			if (strncmp("In", elements[i], 2) == 0) {
				/* get the 'in' state without '' */
				char *state_name;
				state_name = strtok(NULL, delim);
				assert(state_name);
				if (!strrchr(state_name, '\'')) {
					fprintf(stderr, "syntax error in 'in' statement, missing '\n");
					assert(0);
				}
				*strrchr(state_name, '\'') = '\0';

				/* check if the questioned state is present */
				for (j = 0; j < n_states; j++) {
					if (strcmp(states[j], state_name + 1) == 0) {
						break;
					}
				}
				if (j == n_states) {
					fprintf(stderr, "error in boolean:\n%s\n state %s not a valid state\n", cond, state_name + 1);
					assert(0);
				}
			} else {
				int len;
				/* strip ==$value from name */
				if (!strchr(elements[i], '=')) {
					fprintf(stderr, "error in boolean:\n%s no = found in comparison %s\n", cond, elements[i]);
					assert(0);
				}
				*strchr(elements[i], '=') = '\0';

				len = strstr(elements[i], "'name'") - elements[i];
				if (len <= 0) {
					len = strlen(elements[i]);
				}
				/* check if questioned name is known */
				for (j = 0; j < n_names; j++ ) {
					if (strncmp(names[j], elements[i], len) == 0) {
						break;
					}
				}
				if (j == n_names) {
					fprintf(stderr, "error in boolean:\n%s\n%s not found in datamodel\n", cond, elements[i]);
					assert(0);
				}
			}
		}
	}
}

void check_trans(char **names, int n_names, char **states, int n_states, struct scxml_trans *trans, struct counter *c)
{
	check_balance(trans->cond, '(', ')');
	check_balance(trans->cond, '[', ']');
	check_balance(trans->cond, '{', '}');

	trans->count = c->ntrans++;

	check_cond(names, n_names, states, n_states, trans->cond);
}

void check_else(char **, int, char **, int, struct scxml_else *, struct counter *);
void check_elif (char **, int, char **, int, struct scxml_elif *, struct counter *);

void check_if (char **names, int n_names, char **states, int n_states, struct scxml_if *xif, struct counter *c)
{
	struct scxml_if *subif;
	struct scxml_elif *elseif;
	struct scxml_else *xelse;

	xif->count = c->nif++;

	check_balance(xif->cond, '(', ')');
	check_balance(xif->cond, '[', ']');
	check_balance(xif->cond, '{', '}');
	check_cond(names, n_names, states, n_states, xif->cond);

	for (subif = xif->if_first; subif; subif = subif->next) {
		check_if (names, n_names, states, n_states, subif, c);
	}
	for (elseif = xif->elif_first; elseif; elseif = elseif->next) {
		check_elif (names, n_names, states, n_states, elseif, c);
	}
	for (xelse = xif->else_first; xelse; xelse = xelse->next) {
		check_else(names, n_names, states, n_states, xelse, c);
	}
}

void check_else(char **names, int n_names, char **states, int n_states, struct scxml_else *xelse, struct counter *c)
{
	struct scxml_if *xif;

	for (xif = xelse->if_first; xif; xif = xif->next) {
		check_if (names, n_names, states, n_states, xif, c);
	}
}

void check_elif(char **names, int n_names, char **states, int n_states, struct scxml_elif *elif, struct counter *c)
{
	struct scxml_if *xif;

	check_balance(elif->cond, '(', ')');
	check_balance(elif->cond, '[', ']');
	check_balance(elif->cond, '{', '}');
	check_cond(names, n_names, states, n_states, elif->cond);

	for (xif = elif->if_first; xif; xif = xif->next) {
		check_if (names, n_names, states, n_states, xif, c);
	}
}

void check_onentry(char **names, int n_names, char **states, int n_states, struct scxml_onentry *onentry, struct counter *c)
{
	struct scxml_if *xif;

	for (xif = onentry->if_first; xif; xif = xif->next) {
		check_if (names, n_names, states, n_states, xif, c);
	}
}

void check_onexit(char **names, int n_names, char **states, int n_states, struct scxml_onexit *onexit, struct counter *c)
{
	struct scxml_if *xif;

	for (xif = onexit->if_first; xif; xif = xif->next) {
		check_if (names, n_names, states, n_states, xif, c);
	}
}

void check_booleans(struct scxml *scxml)
{
	struct scxml_state *state;
	struct scxml_if *xif;
	struct scxml_onentry *onentry;
	struct scxml_onexit *onexit;
	struct scxml_trans *trans;

	char *names[MAX_DATA];
	int n_names = 0;
	char *states[MAX_STATES];
	int n_states = 0;
	int i;

	n_names = get_names(scxml, names);

	n_states = get_states(scxml, states);
	for (state = scxml->state_first; state; state = state->next) {

		struct counter c;
		c.nif = 0;
		c.nelif = 0;
		c.ntrans = 0;

		for (onentry = state->onentry_first; onentry; onentry = onentry->next) {
			check_onentry(names, n_names, states, n_states, onentry, &c);
		}
		for (trans = state->trans_first; trans; trans = trans->next) {
			check_trans(names, n_names, states, n_states, trans, &c);
		}
		for (xif = state->if_first; xif; xif = xif->next) {
			check_if (names, n_names, states, n_states, xif, &c);
		}
		for (onexit = state->onexit_first; onexit; onexit = onexit->next) {
			check_onexit(names, n_names, states, n_states, onexit, &c);
		}
	}
	for (i = 0; i < n_states; i++) {
		free(states[i]);
	}
	for (i = 0; i < n_names; i++) {
		free(names[i]);
	}
}

struct scxml_extern* extern_get(struct xml *x1)
{
	struct scxml_extern *ex;

	ex = malloc(sizeof(*ex));
	assert(ex);
	memset(ex, 0, sizeof(*ex));

	if (strcmp(x1->start.string, "extern") != 0) {
		fprintf(stderr, "%s found as extern in datamodel\n", x1->start.string);
		assert(0);
	}

	ex->input = xml_attr_lookup(x1, "input");
	ex->output = xml_attr_lookup(x1, "output");
	if (!ex->input && !ex->output) {
		fprintf(stderr, "neither input nor output in extern\n");
		assert(0);
	}

	return ex;
}


struct scxml_data* data_get(struct xml *x1)
{
	struct scxml_data *data;
	const char *tmp;

	data = malloc(sizeof(*data));
	assert(data);
	memset(data, 0, sizeof(*data));

	if (strcmp(x1->start.string, "data") != 0) {
		fprintf(stderr, "%s found as data in datamodel\n", x1->start.string);
		assert(0);
	}

	data->id = xml_attr_lookup(x1, "id");
	assert(data->id);
	tmp = xml_attr_lookup(x1, "expr");
	assert(tmp);
	data->expr = atoi(tmp);
	data->flags = xml_attr_lookup(x1, "flags");
	/* no assert here, flags are optional */
	data->bytes = xml_attr_lookup(x1, "bytes");
	/* bytes are optional as well */

	return data;
}

struct scxml_datamodel* datamodel_get(struct xml *x0)
{
	struct xml *x1;
	struct scxml_datamodel *model;
	struct scxml_data *data;

	model = malloc(sizeof(*model));
	assert(model);
	memset(model, 0, sizeof(*model));

	assert(strcmp(x0->start.string, "datamodel") == 0);

	model->name = xml_attr_lookup(x0, "name");
	assert(model->name);

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;

		data = data_get(x1);

		data->prev = model->data_last;
		data->next = NULL;
		if (data->prev) {
			data->prev->next = data;
		} else {
			model->data_first = data;
		}
		model->data_last = data;
	}

	return model;
}

struct scxml_function* function_get(struct xml *x0)
{
	struct xml *x1;
	struct scxml_function *func;
	struct scxml_datamodel *model;

	func = malloc(sizeof(*func));
	assert(func);
	memset(func, 0, sizeof(*func));

	func->name = xml_attr_lookup(x0, "name");
	assert(func->name);

	assert(strcmp(x0->start.string, "function") == 0);

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;

		model = datamodel_get(x1);

		model->prev = func->model_last;
		model->next = NULL;
		if (model->prev) {
			model->prev->next = model;
		} else {
			func->model_first = model;
		}
		func->model_last = model;
	}

	return func;
}

struct scxml_assign* assign_get(struct xml *x1)
{
	struct scxml_assign *assign;

	assign = malloc(sizeof(*assign));
	assert(assign);
	memset(assign, 0, sizeof(*assign));

	if (strcmp(x1->start.string, "assign") != 0) {
		fprintf(stderr, "%s treated as assignment\n", x1->start.string);
		assert(0);
	}

	assign->location = xml_attr_lookup(x1, "location");
	assert(assign->location);
	assign->expr = xml_attr_lookup(x1, "expr");
	assert(assign->expr);

	return assign;
}

struct scxml_trans* trans_get(struct xml *x1)
{
	struct scxml_trans *trans;

	trans = malloc(sizeof(*trans));
	assert(trans);
	memset(trans, 0, sizeof(*trans));

	if (strcmp(x1->start.string, "transition") != 0) {
		fprintf(stderr, "%s treated as transition\n", x1->start.string);
		assert(0);
	}

	trans->event = xml_attr_lookup(x1, "event");
	assert(trans->event);
	trans->cond = xml_attr_lookup(x1, "cond");

	trans->count = -1;

	/* condition is optional assert(trans->cond); */
	trans->target = xml_attr_lookup(x1, "target");
	assert(trans->target);

	return trans;
}

/* forward declaration */
struct scxml_if* if_get(struct xml *);

struct scxml_onexit* onexit_get(struct xml *x0)
{
	struct scxml_onexit *onexit;
	struct scxml_if *subif;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct xml *x1;

	onexit = malloc(sizeof(*onexit));
	assert(onexit);
	memset(onexit, 0, sizeof(*onexit));

	if (strcmp(x0->start.string, "onexit") != 0) {
		fprintf(stderr, "%s treated as onexit\n", x0->start.string);
		assert(0);
	}

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = onexit->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				onexit->assign_first = assign;
			}
			onexit->assign_last = assign;
		} else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = onexit->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				onexit->ex_first = ex;
			}
			onexit->ex_last = ex;
		} else if (strcmp(x1->start.string, "if") == 0) {
			subif = if_get(x1);

			subif->prev = onexit->if_last;
			subif->next = NULL;
			if (subif->prev) {
				subif->prev->next = subif;
			} else {
				onexit->if_first = subif;
			}
			onexit->if_last = subif;
		} else {
			fprintf(stderr, "Unknown scxml tag found in onexit: %s\n", x1->start.string);
			assert(0);
		}
	}
	return onexit;
}

struct scxml_onentry* onentry_get(struct xml *x0)
{
	struct scxml_onentry *onentry;
	struct scxml_if *subif;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct xml *x1;

	onentry = malloc(sizeof(*onentry));
	assert(onentry);
	memset(onentry, 0, sizeof(*onentry));

	if (strcmp(x0->start.string, "onentry") != 0) {
		fprintf(stderr, "%s treated as onentry\n", x0->start.string);
		assert(0);
	}

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = onentry->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				onentry->assign_first = assign;
			}
			onentry->assign_last = assign;

		} else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = onentry->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				onentry->ex_first = ex;
			}
			onentry->ex_last = ex;
		} else if (strcmp(x1->start.string, "if") == 0) {
			subif = if_get(x1);

			subif->prev = onentry->if_last;
			subif->next = NULL;
			if (subif->prev) {
				subif->prev->next = subif;
			} else {
				onentry->if_first = subif;
			}
			onentry->if_last = subif;

		} else {
			fprintf(stderr, "Unknown scxml tag found in onentry: %s\n", x1->start.string);
			assert(0);
		}
	}
	return onentry;
}

struct scxml_else* else_get(struct xml *x0)
{
	struct scxml_else *xelse;
	struct scxml_if *subif;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct xml *x1;

	xelse = malloc(sizeof(*xelse));
	assert(xelse);
	memset(xelse, 0, sizeof(*xelse));

	if (strcmp(x0->start.string, "else") != 0) {
		fprintf(stderr, "%s treated as else\n", x0->start.string);
		assert(0);
	}

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = xelse->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				xelse->assign_first = assign;
			}
			xelse->assign_last = assign;

		} else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = xelse->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				xelse->ex_first = ex;
			}
			xelse->ex_last = ex;
		} else if (strcmp(x1->start.string, "if") == 0) {
			subif = if_get(x1);

			subif->prev = xelse->if_last;
			subif->next = NULL;
			if (subif->prev) {
				subif->prev->next = subif;
			} else {
				xelse->if_first = subif;
			}
			xelse->if_last = subif;

		} else {
			fprintf(stderr, "Unknown scxml tag found in else: %s\n", x1->start.string);
			assert(0);
		}
	}
	return xelse;
}

struct scxml_elif* elif_get(struct xml *x0)
{
	struct scxml_if *subif;
	struct scxml_elif *elif;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct xml *x1;

	elif = malloc(sizeof(*elif));
	assert(elif);
	memset(elif, 0, sizeof(*elif));

	if (strcmp(x0->start.string, "elseif") != 0) {
		fprintf(stderr, "%s treated as elseif\n", x0->start.string);
		assert(0);
	}

	elif->cond = xml_attr_lookup(x0, "cond");
	assert(elif->cond);

	elif->count = -1;

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = elif->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				elif->assign_first = assign;
			}
			elif->assign_last = assign;

		} else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = elif->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				elif->ex_first = ex;
			}
			elif->ex_last = ex;
		} else if (strcmp(x1->start.string, "if") == 0) {
			subif = if_get(x1);

			subif->prev = elif->if_last;
			subif->next = NULL;
			if (subif->prev) {
				subif->prev->next = subif;
			} else {
				elif->if_first = subif;
			}
			elif->if_last = subif;

		} else {
			fprintf(stderr, "Unknown scxml tag found in else: %s\n", x1->start.string);
			assert(0);
		}
	}
	return elif;
}


struct scxml_if* if_get(struct xml *x0)
{
	struct xml *x1;
	struct scxml_if *xif;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct scxml_elif *elif;
	struct scxml_else *xelse;
	struct scxml_if *subif;

	xif = malloc(sizeof(*xif));
	assert(xif);
	memset(xif, 0, sizeof(*xif));

	xif->cond = xml_attr_lookup(x0, "cond");
	assert(xif->cond);

	xif->count = -1;

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = xif->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				xif->ex_first = ex;
			}
			xif->ex_last = ex;
		} else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = xif->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				xif->assign_first = assign;
			}
			xif->assign_last = assign;
		} else if (strcmp(x1->start.string, "elseif") == 0) {
			elif = elif_get(x1);

			elif->prev = xif->elif_last;
			elif->next = NULL;
			if (elif->prev) {
				elif->prev->next = elif;
			} else {
				xif->elif_first = elif;
			}
			xif->elif_last = elif;
		} else if (strcmp(x1->start.string, "else") == 0) {
			xelse = else_get(x1);

			xelse->prev = xif->else_last;
			xelse->next = NULL;
			if (xelse->prev) {
				fprintf(stderr, "found multiple else for one if condition\n");
				assert(0);
			} else {
				xif->else_first = xelse;
			}
			xif->else_last = xelse;
		} else if (strcmp(x1->start.string, "if") == 0) {
			subif = if_get(x1);

			subif->prev = xif->if_last;
			subif->next = NULL;
			if (subif->prev) {
				subif->prev->next = subif;
			} else {
				xif->if_first = subif;
			}
			xif->if_last = subif;

		} else {
			fprintf(stderr, "Unknown scxml tag found in if condition: %s\n",
				x1->start.string);
			assert(0);
		}
	}

	return xif;
}

struct scxml_state* state_get(struct xml *x0)
{
	struct xml *x1;
	struct scxml_state *state;
	struct scxml_trans *trans;
	struct scxml_assign *assign;
	struct scxml_extern *ex;
	struct scxml_if *xif;
	struct scxml_onentry *onentry;
	struct scxml_onexit *onexit;

	state = malloc(sizeof(*state));
	assert(state);
	memset(state, 0, sizeof(*state));

	state->id = xml_attr_lookup(x0, "id");
	assert(state->id);

	for (x1 = x0->start.child_first; x1; x1 = x1->next) {
		if (x1->type != XML_START
		    && x1->type != XML_START_END) continue;
		else if (strcmp(x1->start.string, "extern") == 0) {
			ex = extern_get(x1);

			ex->prev = state->ex_last;
			ex->next = NULL;
			if (ex->prev) {
				ex->prev->next = ex;
			} else {
				state->ex_first = ex;
			}
			state->ex_last = ex;
		} else if (strcmp(x1->start.string, "transition") == 0) {
			trans = trans_get(x1);

			trans->prev = state->trans_last;
			trans->next = NULL;
			if (trans->prev) {
				trans->prev->next = trans;
			} else {
				state->trans_first = trans;
			}
			state->trans_last = trans;
		} else if (strcmp(x1->start.string, "assign") == 0) {
			assign = assign_get(x1);

			assign->prev = state->assign_last;
			assign->next = NULL;
			if (assign->prev) {
				assign->prev->next = assign;
			} else {
				state->assign_first = assign;
			}
			state->assign_last = assign;
		} else if (strcmp(x1->start.string, "if") == 0) {
			xif = if_get(x1);

			xif->prev = state->if_last;
			xif->next = NULL;
			if (xif->prev) {
				xif->prev->next = xif;
			} else {
				state->if_first = xif;
			}
			state->if_last = xif;
		} else if (strcmp(x1->start.string, "onentry") == 0) {
			onentry = onentry_get(x1);

			onentry->prev = state->onentry_last;
			onentry->next = NULL;
			if (onentry->prev) {
				onentry->prev->next = onentry;
			} else {
				state->onentry_first = onentry;
			}
			state->onentry_last = onentry;
		} else if (strcmp(x1->start.string, "onexit") == 0) {
			onexit = onexit_get(x1);

			onexit->prev = state->onexit_last;
			onexit->next = NULL;
			if (onexit->prev) {
				onexit->prev->next = onexit;
			} else {
				state->onexit_first = onexit;
			}
			state->onexit_last = onexit;
		} else {
			fprintf(stderr, "Unknown scxml tag found in state %s: %s\n"
				, state->id, x1->start.string);
			assert(0);
		}
	}
	return state;
}

struct scxml* scxml_read(struct xml *xml)
{
	struct scxml *scxml;
	assert(xml);

	struct xml *x1;
	//struct xml *x2;

	assert(xml->type == XML_START
	       || xml->type == XML_START_END);
	assert(strcmp(xml->start.string, "scxml") == 0);

	scxml = malloc(sizeof(*scxml));
	assert(scxml);
	memset(scxml, 0, sizeof(*scxml));

	scxml->name = xml->start.string;
	scxml->type = xml_attr_lookup(xml, "type");
	assert(scxml->type);
	scxml->initial = xml_attr_lookup(xml, "initial");
	assert(scxml->initial);

	for (x1 = xml->start.child_first; x1; x1 = x1->next) {
		if (strcmp(x1->start.string, "datamodel") == 0) {
			struct scxml_datamodel *model;

			model = datamodel_get(x1);

			model->prev = scxml->model_last;
			model->next = NULL;
			if (model->prev) {
				model->prev->next = model;
			} else {
				scxml->model_first = model;
			}
			scxml->model_last = model;
		} else if (strcmp(x1->start.string, "state") == 0) {
			struct scxml_state *state;

			state = state_get(x1);

			state->prev = scxml->state_last;
			state->next = NULL;
			if (state->prev) {
				state->prev->next = state;
			} else {
				scxml->state_first = state;
			}
			scxml->state_last = state;
		} else if (strcmp(x1->start.string, "function") == 0) {
			struct scxml_function *func;

			func = function_get(x1);

			func->prev = scxml->func_last;
			func->next = NULL;
			if (func->prev) {
				func->prev->next = func;
			} else {
				scxml->func_first = func;
			}
			scxml->func_last = func;
		} else {
			fprintf(stderr, "Unknown scxml tag found: %s\n", x1->start.string);
			assert(0);
		}
	}

	check_booleans(scxml);

	return scxml;
}