summaryrefslogtreecommitdiff
path: root/support/dktools/dkct-au.ctr
blob: 45046c9c5e55e235c5dff8aaba0b29bfd06069f2 (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
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2011-xxxx
license		=	bsd



%%	module


#if DK3_USE_WX
#include "dkwxtrace.h"
#else
#include "dkct.h"
#endif


$!trace-include



/**	Sort criteria:Sort by state and input.
*/
#define DKCT_AU_RULE_SORT_STATE_INPUT	1

/**	Sort criteria: Inverted search order.
*/
#define DKCT_AU_RULE_SORT_REVERSE	2



/**	Input file section for options.
*/
#define	DKCT_AU_SECTION_OPTIONS		0

/**	Input file section for states.
*/
#define	DKCT_AU_SECTION_STATES		1

/**	Input file section for inputs.
*/
#define	DKCT_AU_SECTION_INPUTS		2

/**	Input file section for outputs.
*/
#define	DKCT_AU_SECTION_OUTPUTS		3

/**	Input file section for rules.
*/
#define	DKCT_AU_SECTION_RULES		4



/**	Section names.
*/
static char const * const dkct_au_section_names[] = {
/* 0 */ "options",
/* 1 */ "states",
/* 2 */ "inputs",
/* 3 */ "outputs",
/* 4 */ "rules",
/* 5 */ "option",
/* 6 */ "state",
/* 7 */ "input",
/* 8 */ "output",
/* 9 */ "rule",
NULL
};



/**	Keywords used by the module.
*/
static char const * const dkct_au_kw8[] = {
/*  0 */ "*",
/*  1 */ "\n",
/*  2 */ " ",
/*  3 */ "#ifndef ",
/*  4 */ "#else\n",
/*  5 */ "#endif\n\n",
/*  6 */ "#define ",
/*  7 */ "#error \"Redefinition of ",
/*  8 */ "\"\n",
/*  9 */ " %d\n",
/* 10 */ " (%d)\n",
/* 11 */ "/** ",
/* 12 */ " */\n",
/* 13 */ "State: ",
/* 14 */ "State machine input: ",
/* 15 */ "State machine output: ",
/* 16 */ "state_machine_%lu",
/* 17 */ "/** @defgroup dkct_state_machine_",
/* 18 */ " The ",
/* 19 */ " state machine. */\n/**@{*/\n\n",
/* 20 */ "/**@}*/\n\n",
/* 21 */ "#ifdef __cplusplus\nextern \"C\" { \n#endif\n\n",
/* 22 */ "#ifdef __cplusplus\n}\n#endif\n\n",
/* 23 */ "/**\tReset ",
/* 24 */ " state machine.\n",
/* 25 */ "\t@param\tst\tPointer to state variable.\n*/\n",
/* 26 */ "void ",
/* 27 */ "_reset(int *st);\n\n",
/* 28 */ "/**\tState machine ",
/* 29 */ " step.\n\t@param\tst\tPointer to state variable.\n",
/* 30 */ "\t@param\tin\tInput.\n\t@return\tTransition output.\n*/\n",
/* 31 */ "_step(int *st, int in);\n\n",
/* 32 */ "static\n",
/* 33 */ "void\n",
/* 34 */ "_reset(int *st)\n{\n  if(st) { *st = ",
/* 35 */ "; }\n}\n\n",
/* 36 */ "0",
/* 37 */ "int\n",
/* 38 */ "_step(int *st, int in)\n{\n  int back = ",
/* 39 */ ";\n",
/* 40 */ "  if(st) {\n    int os;\n    int nf = 1;\n    int ns = ",
/* 41 */ "}\n\n",
/* 42 */ "  } else {\n",
/* 43 */ "  }\n",
/* 44 */ "    if(nf) {\n",
/* 45 */ "    }\n",
/* 46 */ "    os = *st;\n",
/* 47 */ "    switch(os) {\n",
/* 48 */ "      case ",
/* 49 */ ": {\n",
/* 50 */ "      } break;\n",
/* 51 */ "        switch(in) {\n",
/* 52 */ "        }\n",
/* 53 */ "          case ",
/* 54 */ ": {\n",
/* 55 */ " nf = 0;\n          } break;\n",
/* 56 */ "ns = ",
/* 57 */ "os",
/* 58 */ "; back = ",
/* 59 */ ";",
/* 60 */ "      if(",
/* 61 */ ") {\n",
/* 62 */ "\n      } else {\n",
/* 63 */ "      }\n",
/* 64 */ "os == ",
/* 65 */ "in == ",
/* 66 */ "fprintf(",
/* 67 */ ", \"+ ",
/* 68 */ "_step state=%d input=%d\\n\", ((st) ? *st : 0), in);\n",
/* 69 */ ", \"- ",
/* 70 */ ", L\"+ ",
/* 71 */ ", L\"- ",
/* 72 */ "fwprintf(",
/* 73 */ "\". rule: ",
/* 74 */ "\\n\"", /* UNUSED */
/* 75 */ "_step state=%d output=%d\\n\", ((st) ? *st : 0), back);\n",
/* 76 */ "  return back;\n",
/* 77 */ "    *st = ns;\n",
NULL
};



/**	Option names.
*/
static char const * const dkct_au_option_names[] = {
/*  0 */ "name",
/*  1 */ "write-header",
NULL
};



/**	Compare two entries by name.
	@param	l	Left entry.
	@param	r	Right entry.
	@param	cr	Comparison critieria (0=struct/struct, 1=struct/name).
	@return	Comparison result.
*/
static
int
dkct_au_entry_compare(void const *l, void const *r, int cr)
{
  int back = 0;
  DKCT_STM_ENTRY const	*pl;	/* Left object pointer. */
  DKCT_STM_ENTRY const	*pr;	/* Right object pointer. */
  if(l) {
    if(r) {
      pl = (DKCT_STM_ENTRY const *)l;
      pr = (DKCT_STM_ENTRY const *)r;
      switch(cr) {
        case 1: {
	  if(pl->name) {
	    back = dk3str_c8_cmp(pl->name, (char const *)r);
	  } else {
	    back = -1;
	  }
	} break;
	default: {
          if(pl->name) {
            if(pr->name) {
	      back = dk3str_c8_cmp(pl->name, pr->name);
	    } else {
	      back = 1;
	    }
          } else {
            if(pr->name) {
	      back = -1;
	    }
          }
	} break;
      }
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  }
  if(back < -1) back = -1;
  if(back >  1) back =  1;
  return back;
}



/**	Delete entry.
	@param	p	Entry to delete.
*/
static
void
dkct_au_entry_delete(DKCT_STM_ENTRY *p)
{
  if(p) {	$? "= dkct_au_entry_delete \"%!8s\" \"%!8s\" %d", TR_8STR(p->name), TR_8STR(p->comment), p->value
    dk3_release(p->name);
    dk3_release(p->comment);
    p->lineno = 0UL; p->value = 0; p->flag = 0x00;
    dk3_delete(p);
  }
}



/**	Create entry.
	@param	name	Entry name.
	@param	comment	Comment.
	@param	lineno	Line number in source file.
	@param	value	Numeric value (if specified).
	@param	flag	Flag: Use value.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new entry on success, NULL on error.
*/
DKCT_STM_ENTRY *
dkct_au_entry_new(
  char const	*name,
  char const	*comment,
  unsigned long lineno,
  int		value,
  int		flag,
  dk3_app_t	*app
)
{
  DKCT_STM_ENTRY *back = NULL;
  $? "+ dkct_au_entry_new \"%!8s\" \"%!8s\"", TR_8STR(name), TR_8STR(comment)
  if(name) {
    back = dk3_new_app(DKCT_STM_ENTRY,1,app);
    if(back) {
      back->flag = ((flag)? 0x01 : 0x00);
      back->value = value;
      back->lineno = lineno;
      back->name = dk3str_c8_dup_app(name,app);
      back->comment = NULL;
      if(back->name) {
        if(comment) {
	  back->comment = dk3str_c8_dup_app(comment,app);
	  if(!(back->comment)) {
	    dkct_au_entry_delete(back);
	    back = NULL;
	  }
	}
      } else {
        dkct_au_entry_delete(back);
	back = NULL;
      }
    }
  } $? "- dkct_au_entry_new PTR=%d", TR_IPTR(back)
  return back;
}



/**	Compare two rules, either by state+input or by line number.
	@param	l	Left rule.
	@param	r	Right rule.
	@param	cr	Comparison criteria (0=line,
	DKCT_AU_RULE_SORT_STATE_INPUT=state+input).
	@return	Comparison result.
*/
static
int
dkct_au_rule_compare(void const *l, void const *r, int cr)
{
  int back = 0;
  DKCT_STM_RULE const *pl;	/* Left rule pointer. */
  DKCT_STM_RULE const *pr;	/* Right rule pointer. */
  if(l) {
    if(r) {
      pl = (DKCT_STM_RULE const *)l;
      pr = (DKCT_STM_RULE const *)r;
      switch(cr & (~(DKCT_AU_RULE_SORT_REVERSE))) {
        case DKCT_AU_RULE_SORT_STATE_INPUT: {
	  /*
	  	First sort by states.
	  */
	  if(pl->state) {
	    if(pr->state) {
	      if((pl->state)->name) {
	        if((pr->state)->name) {
		  back = dk3str_c8_cmp((pl->state)->name, (pr->state)->name);
		} else {
		  back = 1;
		}
	      } else {
	        if((pr->state)->name) {
		  back = -1;
		}
	      }
	    } else {
	      back = 1;
	    }
	  } else {
	    if(pr->state) {
	      back = -1;
	    }
	  }
	  /*
	  	For equal states sort by input.
	  */
	  if(back == 0) {
	    if(pl->input) {
	      if(pr->input) {
	        if((pl->input)->name) {
		  if((pr->input)->name) {
		    back = dk3str_c8_cmp((pl->input)->name, (pr->input)->name);
		  } else {
		    back = 1;
		  }
		} else {
		  if((pr->input)->name) {
		    back = -1;
		  }
		}
	      } else {
	        back = 1;
	      }
	    } else {
	      if(pr->input) {
	        back = -1;
	      }
	    }
	  }
	} break;
	default: {
	  /*
	  	Simply sort by line number.
	  */
	  if(pl->lineno > pr->lineno) { back = 1; }
	  else {
	    if(pl->lineno < pr->lineno) { back = -1; }
	  }
	} break;
      }
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  }
  if(back < -1) { back = -1; }
  if(back >  1) { back =  1; }
  if(cr & DKCT_AU_RULE_SORT_REVERSE) {
    switch(back) {
      case  1: { back = -1; } break;
      case -1: { back =  1; } break;
    }
  }
  return back;
}



/**	Delete rule.
	@param	rp	Rule to delete.
*/
void
dkct_au_rule_delete(DKCT_STM_RULE *rp)
{
  if(rp) { $? "= dkct_au_rule_delete"
    rp->state = NULL;
    rp->input = NULL;
    rp->newstate = NULL;
    rp->output = NULL;
    rp->lineno = 0UL;
    dk3_delete(rp);
  }
}



/**	Create rule.
	@param	os	Old state.
	@param	in	Input.
	@param	ns	New state.
	@param	out	Output.
	@param	lineno	Line number in source file.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new element on success, NULL on error.
*/
DKCT_STM_RULE *
dkct_au_rule_new(
  DKCT_STM_ENTRY *os,
  DKCT_STM_ENTRY *in,
  DKCT_STM_ENTRY *ns,
  DKCT_STM_ENTRY *out,
  unsigned long	  lineno,
  dk3_app_t	 *app
)
{
  DKCT_STM_RULE *back = NULL;
  int ok = 1;			/* Flag: No errors occured. */
  $? "+ dkct_au_rule_new"
  back = dk3_new_app(DKCT_STM_RULE,1,app);
  if(back) {
    back->state = os;
    back->input = in;
    back->newstate = ns;
    back->output = out;
    back->lineno = lineno;
    if(!(ok)) {
      dkct_au_rule_delete(back);
      back = NULL;
    }
  } $? "- dkct_au_rule_new PTR=%d", TR_IPTR(back)
  return  back;
}



/**	Delete all rules in a storage.
	@param	s	Storage containing the rules.
	@param	i	Iterator for storage.
*/
static
void
dkct_au_stm_rules_delete(dk3_sto_t *s, dk3_sto_it_t *i)
{
  DKCT_STM_RULE	*r;			/* Current rule to delete. */
  $? "+ dkct_au_stm_rules_delete"
  if(s) {
    if(i) {
      dk3sto_it_reset(i);
      while((r = (DKCT_STM_RULE *)dk3sto_it_next(i)) != NULL) {
        dkct_au_rule_delete(r);
      }
      dk3sto_it_close(i);
    }
    dk3sto_close(s);
  } $? "- dkct_au_stm_rules_delete"
}



/**	Delete all entries in a storage.
	@param	s	Storage containing the entries.
	@param	i	Iterator for storage.
*/
static
void
dkct_au_stm_entries_delete(dk3_sto_t *s, dk3_sto_it_t *i)
{
  DKCT_STM_ENTRY *e;			/* Current entry to delete. */
  $? "+ dkct_au_stm_entries_delete"
  if(s) {
    if(i) {
      dk3sto_it_reset(i);
      while((e = (DKCT_STM_ENTRY *)dk3sto_it_next(i)) != NULL) {
        dkct_au_entry_delete(e);
      }
      dk3sto_it_close(i);
    }
    dk3sto_close(s);
  } $? "- dkct_au_stm_entries_delete"
}



/**	Delete a state machine description.
	@param	p	State machine to delete.
*/
void
dkct_au_stm_delete(DKCT_STM *p)
{
  $? "+ dkct_au_stm_delete"
  if(p) {
    dkct_au_stm_rules_delete(p->s_wildcard, p->i_wildcard);
    dkct_au_stm_rules_delete(p->s_exact, p->i_exact);
    dkct_au_stm_entries_delete(p->s_states, p->i_states);
    dkct_au_stm_entries_delete(p->s_inputs, p->i_inputs);
    dkct_au_stm_entries_delete(p->s_outputs, p->i_outputs);
    p->s_wildcard = NULL; p->i_wildcard = NULL;
    p->s_exact = NULL; p->i_exact = NULL;
    p->s_states = NULL; p->i_states = NULL;
    p->s_outputs = NULL; p->i_outputs = NULL;
    p->s_inputs = NULL; p->i_inputs = NULL;
    dk3_release(p->name);
    p->lineno = 0UL;
    p->wrh = 0;
    p->rs = NULL;
    p->ds = NULL;
    p->dout = NULL;
    p->lngr = 0UL;
    dk3_delete(p);
  } $? "- dkct_au_stm_delete"
}



/**	Create new state machine description.
	@param	lineno	Line number in source file.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new structure on success, NULL on error.
*/
DKCT_STM *
dkct_au_stm_new(unsigned long lineno, dk3_app_t *app)
{
  DKCT_STM	*back	= NULL;
  int		ok 	= 0;	/* Flag: Successfully initialized. */
  $? "+ dkct_au_stm_new %lu", lineno
  back = dk3_new_app(DKCT_STM,1,app);
  if(back) {
    back->s_wildcard = NULL; back->i_wildcard = NULL;
    back->s_exact = NULL; back->i_exact = NULL;
    back->s_states = NULL; back->i_states = NULL;
    back->s_outputs = NULL; back->i_outputs = NULL;
    back->s_inputs = NULL; back->i_inputs = NULL;
    back->name = NULL;
    back->lineno = lineno;
    back->wrh = 1;
    back->section = DKCT_AU_SECTION_OPTIONS;
    back->rs = NULL;
    back->ds = NULL;
    back->dout = NULL;
    back->lngr = 0UL;
    back->stmno = 0UL;
    back->s_states = dk3sto_open_app(app);
    back->s_inputs = dk3sto_open_app(app);
    back->s_outputs = dk3sto_open_app(app);
    back->s_exact = dk3sto_open_app(app);
    back->s_wildcard = dk3sto_open_app(app);
    if(back->s_states) { back->i_states = dk3sto_it_open(back->s_states); }
    if(back->s_inputs) { back->i_inputs = dk3sto_it_open(back->s_inputs); }
    if(back->s_outputs) { back->i_outputs = dk3sto_it_open(back->s_outputs); }
    if(back->s_exact) { back->i_exact = dk3sto_it_open(back->s_exact); }
    if(back->s_wildcard) { back->i_wildcard= dk3sto_it_open(back->s_wildcard); }
    if((back->s_states) && (back->s_inputs) && (back->s_outputs)) {
      dk3sto_set_comp(back->s_states, dkct_au_entry_compare, 0);
      dk3sto_set_comp(back->s_inputs, dkct_au_entry_compare, 0);
      dk3sto_set_comp(back->s_outputs, dkct_au_entry_compare, 0);
      if((back->s_exact) && (back->s_wildcard)) {
        dk3sto_set_comp(
	  back->s_exact, dkct_au_rule_compare, DKCT_AU_RULE_SORT_STATE_INPUT
	);
	dk3sto_set_comp(back->s_wildcard, dkct_au_rule_compare, 0);
        if((back->i_states) && (back->i_inputs) && (back->i_outputs)) {
	  if((back->i_exact) && (back->i_wildcard)) {
	    ok = 1;
	  }
	}
      }
    }
    if(!(ok)) {
      dkct_au_stm_delete(back);
      back = NULL;
    }
  } $? "- dkct_au_stm_new PTR=%d", TR_IPTR(back)
  return back;
}



/**	Process one option line.
	@param	stm	State machine description.
	@param	psrc	Source structure.
	@param	line	Text line.
	@param	lineno	Line number of current line.
	@return	1 on success, 0 on error.
*/
static
int
dkct_au_add_option(
  DKCT_STM *stm, DKCT_SRC *psrc, char *line, unsigned long lineno
)
{
  int back = 0;
  char		*p1;	/* Key (option name). */
  char		*p2;	/* Value. */
  int		 ai;	/* Array index of option name. */
  $? "+ dkct_au_add_option %lu \"%!8s\"", lineno, TR_8STR(line)
  p1 = dk3str_c8_start(line, NULL);
  if(p1) {
    p2 = dk3str_c8_chr(p1, '=');
    if(p2) {
      *(p2++) = '\0';
      p2 = dk3str_c8_start(p2, NULL);
      if(p2) {
        dk3str_c8_normalize(p1, NULL, '-');
        ai = dk3str_c8_array_index(dkct_au_option_names, p1, 0);
	switch(ai) {
	  case 0: {		$? ". name"
	    if(stm->name) {
	      /* Warning: Redefinition of name */
	      dkct_to_log_1(psrc, 1, DK3_LL_WARNING, 28);
	      dk3_release(stm->name);
	    }
	    stm->name = dk3str_c8_dup_app(p2, psrc->app);
	    if(stm->name) {
	      back = 1;
	    } else {
	      /* ERROR: Memory */
	      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 29);
	    }
	  } break;
	  case 1: {		$? ". write header"
	    if(dk3str_c8_is_bool(p2)) {
	      stm->wrh = (dk3str_c8_is_on(p2) ? 1 : 0);
	      back = 1;
	    } else {
	      /* ERROR: Syntax (not boolean)! */
	      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 30);
	    }
	  } break;
	  default: {
	    dkct_to_log_3(psrc, 1, DK3_LL_ERROR, 10, 11, p1);
	  } break;
	}
      } else {
        /* ERROR: No value. */
	dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 9);
      }
    } else {
      /* Syntax */
      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 9);
    }
  }
  $? "- dkct_au_add_option %d", back
  return back;
}



/**	Add an entry to storage.
	@param	stm		State machine description.
	@param	psrc		Source structure.
	@param	line		Input line to process.
	@param	co		Comment for entry.
	@param	lineno		Line number of input line.
	@param	storage		Storage for entry.
	@param	iterator	Iterator for storage.
	@param	tp		Entry type (0=state, 1=input, 2=output).
	@return	1 on success, 0 on error.

*/
static
int
dkct_au_add_entry(
  DKCT_STM *stm,
  DKCT_SRC *psrc,
  char *line,
  char *co,
  unsigned long lineno,
  dk3_sto_t	*storage,
  dk3_sto_it_t	*iterator,
  int tp
)
{
  int 			back	= 0;
  char			*nptr;		/* Entry name. */
  char			*vptr;		/* Value (if any). */
  DKCT_STM_ENTRY	*e;		/* New entry. */
  DKCT_STM_ENTRY	*ep;		/* Search result. */
  int		 	i;		/* Value (if defined). */
  int		 	f;		/* Flag: Value defined. */
  $? "+ dkct_au_add_entry %lu \"%!8s\" \"%!8s\"", lineno, TR_8STR(line), TR_8STR(co)
  nptr = vptr = NULL;
  i = f = 0;
  nptr = dk3str_c8_start(line, NULL);
  if(nptr) {
    vptr = dk3str_c8_next(nptr, NULL);
    back = 1;
    if(vptr) {
      if(sscanf(vptr, "%d", &i) == 1) {
        f = 1;
      } else {
        back = 0;
        /* ERROR: Syntax */
	dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 31);
      }
    }
    if(back) {
      back = 0;
      e = dkct_au_entry_new(
        nptr, co, lineno, i, f, psrc->app
      );
      if(e) {
        ep = (DKCT_STM_ENTRY *)dk3sto_it_find_like(iterator, (void *)e, 0);
	if(ep) {
	  /* ERROR: Redefinition */
	  dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 32);
	  dkct_au_entry_delete(e);
	} else {
	  if(dk3sto_add(storage, (void *)e)) {
	    back = 1;
	    switch(tp) {
	      case 0: {
	        /*
			First state is reset state.
		*/
	        if(!(stm->rs)) { stm->rs = e; }
		/*
			First state is general rule next state default.
		*/
	        if(!(stm->ds)) { stm->ds = e; }
	      } break;
	      case 2: {
	        /*
			First output is general rule output default.
		*/
	        if(!(stm->dout)) { stm->dout = e; }
	      } break;
	    }
	  } else {
	    dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
	    dkct_au_entry_delete(e);
	  }
	}
      } else {
        /* ERROR Memory */
	dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
      }
    }
  } else {
    /* ERROR: Syntax */
    dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 33);
  } $? "- dkct_au_add_entry %d", back
  return back;
}



/**	Find named entry in storage.
	@param	psrc	Source structure.
	@param	sto	Storage.
	@param	ist	Storage iterator.
	@param	name	Name of entry to find.
	@param	ok	Pointer to not-found-is-ok variable.
	@param	t	Type (0=state, 1=input, 2=output).
	@param	lineno	Line number.
	@return	Pointer to found entry on success, NULL on error.
*/
static
DKCT_STM_ENTRY *
dkct_au_find_entry(
  DKCT_SRC *psrc,
  dk3_sto_t *sto,
  dk3_sto_it_t *ist,
  char *name,
  unsigned long lineno,
  int *ok,
  int t
)
{
  DKCT_STM_ENTRY	*back = NULL;
  if(dk3str_c8_cmp(dkct_au_kw8[0], name) == 0) {
    *ok = 1;
  } else {
    back = (DKCT_STM_ENTRY *)dk3sto_it_find_like(ist, name, 1);
    if(!(back)) {
      /* Warning: State/input/output undeclared! */
      dkct_to_log_3(psrc, 1, DK3_LL_WARNING, 34, 35, name);
      back = dkct_au_entry_new(name, NULL, lineno, 0, 0, psrc->app);
      if(back) {
        if(!dk3sto_add(sto, (void *)back)) {
	  dkct_au_entry_delete(back);
	  back = NULL;
	}
      } else {
        /* ERROR: Failed to create entry! */
	dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 29);
      }
    }
  }
  return back;
}



/**	Add rule to state machine.
	@param	stm	State machine description.
	@param	psrc	Source structure.
	@param	line	Input line to process.
	@param	co	Comment for rule.
	@param	lineno	Current line number.
	@return	1 on success, 0 on error.
*/
static
int
dkct_au_add_rule(
  DKCT_STM *stm, DKCT_SRC *psrc, char *line, char *co, unsigned long lineno
)
{
  int back = 0;
  DKCT_STM_RULE		tr;		/* Test rul. */
  DKCT_STM_RULE		*nr;		/* New rule. */
  char			*p1;		/* Old state. */
  char			*p2;		/* Input. */
  char			*p3;		/* New state. */
  char			*p4;		/* Output. */
  DKCT_STM_ENTRY	*e1;		/* Old state. */
  DKCT_STM_ENTRY	*e2;		/* Input. */
  DKCT_STM_ENTRY	*e3;		/* New state. */
  DKCT_STM_ENTRY	*e4;		/* Output. */
  int			ok1;		/* Flag: Old state *. */
  int			ok2;		/* Flag: Input *. */
  int			ok3;		/* Flag: New state *. */
  int			ok4;		/* Flag: Output *. */
  int			can_add;	/* Flag: Rule ok, add. */
  $? "+ dkct_au_add_rule %lu \"%!8s\" \"%!8s\"", lineno, TR_8STR(line), TR_8STR(co)
  e1 = e2 = e3 = e4 = NULL; ok1 = ok2 = ok3 = ok4 = 0;
  p1 = dk3str_c8_start(line, NULL);
  if(p1) {
    p2 = dk3str_c8_next(p1, NULL);
    if(p2) {
      p3 = dk3str_c8_next(p2, NULL);
      if(p3) {
        p4 = dk3str_c8_next(p3, NULL);
	if(p4) {
	  (void)dk3str_c8_next(p4, NULL);
	  e1 = dkct_au_find_entry(
	    psrc, stm->s_states, stm->i_states, p1, lineno, &ok1, 0
	  );
	  e2 = dkct_au_find_entry(
	    psrc, stm->s_inputs, stm->i_inputs, p2, lineno, &ok2, 1
	  );
	  e3 = dkct_au_find_entry(
	    psrc, stm->s_states, stm->i_states, p3, lineno, &ok3, 0
	  );
	  e4 = dkct_au_find_entry(
	    psrc, stm->s_outputs, stm->i_outputs, p4, lineno, &ok4, 2
	  );
	  if(
	    ((e1) || (ok1)) && ((e2) || (ok2))
	    && ((e3) || (ok3)) && ((e4) || (ok4))
	  )
	  {
	    can_add = 1;
	    tr.state = e1; tr.input = e2; tr.newstate = e3; tr.output = e4;
	    if((e1) && (e2)) {
	      DKCT_STM_RULE *xrp;
	      if((xrp = (DKCT_STM_RULE *)dk3sto_it_find_like(
	                   stm->i_exact, (void *)(&tr),
			   DKCT_AU_RULE_SORT_STATE_INPUT
	                )
	         ) != NULL
	        )
	      {
	        char	ulbuffer[32];
		sprintf(ulbuffer, "%lu", xrp->lineno);
	        can_add = 0;
		/* ERROR: Conflict! */
		dkct_to_log_3(psrc, 1, DK3_LL_ERROR, 36, 39, ulbuffer);
	      }
	    }
	    if(can_add) {
	      if((e1) && (e2)) {
	        nr = dkct_au_rule_new(e1, e2, e3, e4, lineno, psrc->app);
		if(nr) {
		  if(dk3sto_add(stm->s_exact, (void *)nr)) {
		    back = 1;
		  } else {
		    dkct_au_rule_delete(nr);
		    dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
		  }
		} else {
		  /* ERROR: Memory! */
		  dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
		}
	      } else {
	        if((e1) || (e2)) {
		  nr = dkct_au_rule_new(e1, e2, e3, e4, lineno, psrc->app);
		  if(nr) {
		    if(dk3sto_add(stm->s_wildcard, (void *)nr)) {
		      back = 1;
		    } else {
		      dkct_au_rule_delete(nr);
		      dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
		    }
		  } else {
		    /* ERROR: Memory! */
		    dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
		  }
		} else {
		  back = 1;
		  if(stm->lngr) {
		    char buffer[32];
		    sprintf(buffer, "%lu", stm->lngr);
		    /* Warning: General rule redefined! */
		    dkct_to_log_3(psrc, 1, DK3_LL_ERROR, 36, 39, buffer);
		  }
		  stm->ds = e3;
		  stm->dout = e4;
		  stm->lngr = lineno;
		}
	      }
	    }
	  } else {
	    dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 29);
	  }
	} else {
	  dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 33);
	}
      } else {
        dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 33);
      }
    } else {
      dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 33);
    }
  }
  $? "- dkct_au_add_rule %d", back
  return back;
}



/**	Process one input line.
	@param	stm	State machine description.
	@param	psrc	Source structure.
	@param	line	Line to process.
	@param	lineno	Current line number.
	@return	1 on success, 0 on error.
*/
int
dkct_au_add_line(
  DKCT_STM *stm, DKCT_SRC *psrc, char *line, unsigned long lineno
)
{
  int back = 0;
  char		*lstart;	/* Start of input line. */
  char		*pc;		/* Comment pointer. */
  int		 ai;		/* Array index. */
  $? "+ dkct_au_add_line %lu \"%!8s\"", lineno, TR_8STR(line)
  if((stm) && (line)) {		$? ". args ok"
    lstart = dk3str_c8_start(line, NULL);
    if(lstart) {		$? ". text found \"%!8s\"", lstart
      pc = NULL;
      if(*lstart != '#') {	$? ". not a comment line"
        if(*lstart == '[') {	$? ". start of section"
	  lstart++;
	  lstart = dk3str_c8_start(lstart, NULL);
	  if(lstart) {
	    stm->section = -1;
	    pc = dk3str_c8_chr(lstart, ']');
	    if(pc) {
	      *pc = '\0';
	      dk3str_c8_chomp(lstart, NULL);	$? ". lstart=\"%!8s\"", lstart
	      ai = dk3str_c8_array_index(dkct_au_section_names, lstart, 0);
	      $? ". ai=%d",ai
	      switch(ai) {
	        case 0: case 5: {
	          stm->section = DKCT_AU_SECTION_OPTIONS; back = 1;
	        } break;
	        case 1: case 6: {
	          stm->section = DKCT_AU_SECTION_STATES; back = 1;
	        } break;
	        case 2: case 7: {
	          stm->section = DKCT_AU_SECTION_INPUTS; back = 1;
	        } break;
	        case 3: case 8: {
	          stm->section = DKCT_AU_SECTION_OUTPUTS; back = 1;
	        } break;
	        case 4: case 9: {
	          stm->section = DKCT_AU_SECTION_RULES; back = 1;
	        } break;
		default: {
		  dkct_to_log_3(psrc, 1, DK3_LL_ERROR, 99, 100, lstart);
		} break;
	      }
	    } else {
	      /* ERROR: Syntax! */
	      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 37);
	    }
	  } else {
	    dkct_to_log_1(psrc, 0, DK3_LL_ERROR, 37);
	  }
	} else {		$? ". section contents %d", stm->section
	  pc = dk3str_c8_chr(lstart, '#');
	  if(pc) { *(pc++) = '\0'; pc = dk3str_c8_start(pc, NULL); }
	  switch(stm->section) {
	    case DKCT_AU_SECTION_OPTIONS: {	$? ". options"
	      back = dkct_au_add_option(stm, psrc, lstart, lineno);
	    } break;
	    case DKCT_AU_SECTION_STATES: {	$? ". states"
              back = dkct_au_add_entry(
                stm, psrc, lstart, pc, lineno, stm->s_states, stm->i_states, 0
              );
	    } break;
	    case DKCT_AU_SECTION_INPUTS: {	$? ". inputs"
              back = dkct_au_add_entry(
                stm, psrc, lstart, pc, lineno, stm->s_inputs, stm->i_inputs, 1
              );
	    } break;
	    case DKCT_AU_SECTION_OUTPUTS: {	$? ". outputs"
              back = dkct_au_add_entry(
                stm, psrc, lstart, pc, lineno, stm->s_outputs, stm->i_outputs, 2
              );
	    } break;
	    case DKCT_AU_SECTION_RULES: {	$? ". rules"
	      back = dkct_au_add_rule(stm, psrc, lstart, pc, lineno);
	    } break;
	  }
	}
      } else {
        back = 1;	/* comments ok */
      }
    } else {
      back = 1;		/* empty lines ok */
    }
  } $? "- dkct_au_add_line %d", back
  return back;
}



/**	Check whether there is at least one entry in the storage.
	@param	it	Iterator of storage to check.
	@return	1 on success, 0 on error (storage empty).
*/
static
int
dkct_au_stm_entries_check(dk3_sto_it_t *it)
{
  int back = 0;
  dk3sto_it_reset(it);
  if(dk3sto_it_next(it)) {
    back = 1;
  }
  return back;
}



/**	Assign values to state/input/output storage.
	@param	psrc	Source structure.
	@param	stm	State machine to modify.
	@param	it	Iterator for state/input/output storage.
	@return	1 on success, 0 on error.
*/
static
int
dkct_au_set_values(DKCT_SRC *psrc, DKCT_STM *stm, dk3_sto_it_t *it)
{
  int			back		= 1;	
  DKCT_STM_ENTRY	*ep		= NULL;	/* Current entry to process. */
  dk3_bf_t		*bf		= NULL;	/* Bit field, used values. */
  int			min		= 0;	/* Minimum value so far. */
  int			max		= 0;	/* Maximum value so far. */
  int			valfound	= 0;	/* Flag: Values specified. */
  int			cv		= 0;	/* Current value. */
  int			cc		= 0;	/* Flag: Continue search. */
  $? "+ dkct_au_set_values"
  dk3sto_it_reset(it);
  while((ep = (DKCT_STM_ENTRY *)dk3sto_it_next(it)) != NULL) {
    if(ep->flag) {	$? ". have value %d", ep->value
      if(valfound) {	$? ". already values found"
        if(ep->value < min) min = ep->value;
	if(ep->value > max) max = ep->value;
      } else {		$? ". first value"
        min = max = ep->value;
      }
      valfound = 1;
    } else {		$? ". no value"
    }
  }
  if(valfound) {	$? ". must take care of existing values"
    bf = dk3bf_open_app((max - min + 1), psrc->app);
    if(bf) {
      /*
      	Find used values.
      */
      dk3sto_it_reset(it);
      while((ep = (DKCT_STM_ENTRY *)dk3sto_it_next(it)) != NULL) {
        if(ep->flag) {
	  dk3bf_set(bf, (ep->value - min), 1);
	}
      }
      /*
      	Set missing values.
      */
      cv = 0;
      dk3sto_it_reset(it);
      while((ep = (DKCT_STM_ENTRY *)dk3sto_it_next(it)) != NULL) {
        if(!(ep->flag)) {
	  do {			$? ". attempt %d", cv
	    cc = 0;
	    if((cv >= min) && (cv <= max)) {
	      if(dk3bf_get(bf, (cv - min))) {
	        cc = 1;		$? "! already in use"
		cv++;
		if(cv == 0) {	$? "! Numeric overflow"
		  cc = 0;
		  back = 0;
		  /* ERROR: Overflow */
		  dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 38);
		}
	      }
	    }
	  } while(cc);
	  cc = 0;
	  if((cv >= min) && (cv <= max)) {
	    if(!dk3bf_get(bf, (cv - min))) {
	      cc = 1;
	    }
	  } else {
	    cc = 1;
	  }
	  if(cc) {		$? ". can use %d", cv
	    ep->value = cv++;
	    if(cv == 0) {	$? "! Numeric overflow"
	      back = 0;
	      /* ERROR: Overflow */
	      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 38);
	    }
	  }
	}
      }
      dk3bf_close(bf);
    } else {		$? "! failed to allocated bit field"
      back = 0;
      /* ERROR: Memory */
      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 29);
    }
  } else {		$? ". no existing values yet"
    cv = 0;
    dk3sto_it_reset(it);
    while((ep = (DKCT_STM_ENTRY *)dk3sto_it_next(it)) != NULL) {
      ep->value = cv++;
      if(cv == 0) {	$? "! numeric overflow"
        back = 0;
	/* ERROR: Overflow */
	dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 38);
      }
    }
  } $? "- dkct_au_set_values %d", back
  return back;
}



int
dkct_au_stm_check(DKCT_SRC *psrc, DKCT_STM *stm)
{
  int back = 0;
  int	sfound = 0;	/* Number of states found. */
  int	ifound = 0;	/* Number of inputs found. */
  int	ofound = 0;	/* Number of outputs found. */

  dk3app_set_source_line(psrc->app, stm->lineno);
  sfound = dkct_au_stm_entries_check(stm->i_states);
  ifound = dkct_au_stm_entries_check(stm->i_inputs);
  ofound = dkct_au_stm_entries_check(stm->i_outputs);
  if((sfound) && (ifound) && (ofound)) {
    back = 1;
    if(!dkct_au_set_values(psrc, stm, stm->i_states)) { back = 0; }
    if(!dkct_au_set_values(psrc, stm, stm->i_inputs)) { back = 0; }
    if(!dkct_au_set_values(psrc, stm, stm->i_outputs)) { back = 0; }
  } else {
    if(!(sfound)) {
      /* ERROR: No states declared! */
      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 40);
    }
    if(!(ifound)) {
      /* ERROR: No inputs declared! */
      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 41);
    }
    if(!(ofound)) {
      /* ERROR: No outputs declared! */
      dkct_to_log_1(psrc, 1, DK3_LL_ERROR, 42);
    }
  }
  return back;
}



/**	Write definition for one storage entry.
	@param	psrc	Source structure.
	@param	stm	State machine.
	@param	ep	Entry to write
	@param	commi	Keyword index for "state", "input", or "output".
*/
static
void
dkct_au_write_one_entry(
  DKCT_SRC *psrc, DKCT_STM *stm, DKCT_STM_ENTRY *ep, size_t commi
)
{
  $? "+ dkct_au_write_one_entry"
  fputs(dkct_au_kw8[3], psrc->fipo);
  fputs(ep->name, psrc->fipo);
  fputs(dkct_au_kw8[1], psrc->fipo);
  fputs(dkct_au_kw8[11], psrc->fipo);
  fputs(dkct_au_kw8[commi], psrc->fipo);
  if(ep->comment) {
    fputs(ep->comment, psrc->fipo);
  } else {
    fputs(ep->name, psrc->fipo);
  }
  fputs(dkct_au_kw8[12], psrc->fipo);
  if((psrc->dkcto).lnn) {
    psrc->lineno = ep->lineno;
    dkct_tr_show_source_line(psrc, 0);
  }
  fputs(dkct_au_kw8[6], psrc->fipo);
  fputs(ep->name, psrc->fipo);
  fprintf(psrc->fipo, dkct_au_kw8[(ep->value >= 0) ? 9 : 10], ep->value);
  fputs(dkct_au_kw8[4], psrc->fipo);		/* else */
  if((psrc->dkcto).lnn) {
    psrc->lineno = ep->lineno;
    dkct_tr_show_source_line(psrc, 0);
  }
  fputs(dkct_au_kw8[7], psrc->fipo);
  fputs(ep->name, psrc->fipo);
  fputs(dkct_au_kw8[8], psrc->fipo);
  fputs(dkct_au_kw8[5], psrc->fipo);		/* endif */
  $? "- dkct_au_write_one_entry"
}



/**	Write all entries of a storage.
	@param	psrc	Source structure.
	@param	stm	State machine.
	@param	it	Storage iterator.
	@param	commi	Keyword index for "state", "input", or "output".
*/
static
void
dkct_au_write_entry_storage(
  DKCT_SRC *psrc, DKCT_STM *stm, dk3_sto_it_t *it, size_t commi
)
{
  DKCT_STM_ENTRY	*ep;	/* Current entry. */
  $? "+ dkct_au_write_entry_storage"
  dk3sto_it_reset(it);
  while((ep = (DKCT_STM_ENTRY *)dk3sto_it_next(it)) != NULL) {
    dkct_au_write_one_entry(psrc, stm, ep, commi);
  } $? "- dkct_au_write_entry_storage"
}



/**	Write state machine name to output file.
	@param	psrc	Source structure.
	@param	stm	State machine.
*/
static
void
dkct_au_write_stm_name(DKCT_SRC *psrc, DKCT_STM *stm)
{
  if(stm->name) {
    fputs(stm->name, psrc->fipo);
  } else {
    fprintf(psrc->fipo, dkct_au_kw8[16], stm->stmno);
  }
}



/**	Write state/input/output entry definitions.
	@param	psrc	Source structure.
	@param	stm	State machine.
*/
void
dkct_au_write_entries(DKCT_SRC *psrc, DKCT_STM *stm)
{
  /*
  	Begin doxygen group
  */
  fputs(dkct_au_kw8[17], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[18], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[19], psrc->fipo);
  /*
  	Write the entries
  */
  dkct_au_write_entry_storage(psrc, stm, stm->i_states, 13);
  dkct_au_write_entry_storage(psrc, stm, stm->i_inputs, 14);
  dkct_au_write_entry_storage(psrc, stm, stm->i_outputs, 15);
  /*
  	End doxygen group
  */
  fputs(dkct_au_kw8[20], psrc->fipo);
}



void
dkct_au_write_prototypes(DKCT_SRC *psrc, DKCT_STM *stm)
{
  fputs(dkct_au_kw8[21], psrc->fipo);
  fputs(dkct_au_kw8[23], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[24], psrc->fipo);
  fputs(dkct_au_kw8[25], psrc->fipo);
  fputs(dkct_au_kw8[26], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[27], psrc->fipo);
  fputs(dkct_au_kw8[28], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[29], psrc->fipo);
  fputs(dkct_au_kw8[30], psrc->fipo);
  fputs(dkct_au_kw8[37], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[31], psrc->fipo);
  fputs(dkct_au_kw8[22], psrc->fipo);
}



/**	Write a transition result.
	@param	psrc	Source structure.
	@param	stm	State machine.
	@param	rp	Rule.
	@param	indent	Level of indent.
*/
static
void
dkct_au_write_transition_result(
  DKCT_SRC *psrc, DKCT_STM *stm, DKCT_STM_RULE *rp, int indent
)
{
  int i;	/* Number of indent spaces already written. */
  /* DEBUG CODE IF -d */
  for(i = 0; i < indent; i++) { fputc(' ', psrc->fipo); }
  fputs(dkct_au_kw8[56], psrc->fipo);
  if(rp->newstate) {
    fputs((rp->newstate)->name, psrc->fipo);
  } else {
    fputs(dkct_au_kw8[57], psrc->fipo);
  }
  fputs(dkct_au_kw8[58], psrc->fipo);
  if(rp->output) {
    fputs((rp->output)->name, psrc->fipo);
  } else {
    if(stm->dout) {
      fputs((stm->dout)->name, psrc->fipo);
    } else {
      fputs(dkct_au_kw8[36], psrc->fipo);
    }
  }
  fputs(dkct_au_kw8[59], psrc->fipo);
}



/**	Create debug code for entering and leaving the
	transition function.
	@param	psrc	Source structure.
	@param	stm	State machine.
	@param	pos	Position (0=invokation, 1=return).
*/
static
void
dkct_au_debug_transition(DKCT_SRC *psrc, DKCT_STM *stm, int pos)
{
  char const *fnptr;	/* Debug output file. */

  fnptr = dkct_tr_get_kw8(((psrc->dkcto).deb == 2) ? 28 : 29);
  fputs(dkct_tr_get_kw8(67), psrc->fipo);
  if((psrc->dkcto).win) {
    fputs(dkct_tr_get_kw8(21), psrc->fipo);
    /* char 16 start */
    psrc->lineno = stm->lineno;
    fputs(dkct_tr_get_kw8(61), psrc->fipo);
    dkct_tr_show_filename_and_lineno(psrc);
    fputs(dkct_tr_get_kw8(35), psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_tr_get_kw8(34), psrc->fipo);

    fputs(dkct_au_kw8[72], psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_au_kw8[(pos == 1) ? 71 : 70], psrc->fipo);
    dkct_au_write_stm_name(psrc, stm);
    fputs(dkct_au_kw8[(pos == 1) ? 75 : 68], psrc->fipo);

    fputs(dkct_tr_get_kw8(37), psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_tr_get_kw8(34), psrc->fipo);
    /* char 16 end */
    fputs(dkct_tr_get_kw8(22), psrc->fipo);
  }
  /* char 8 start */
  if((psrc->dkcto).deb == 1) {
    fputs(dkct_tr_get_kw8(30), psrc->fipo);
  }
  psrc->lineno = stm->lineno;
  fputs(dkct_tr_get_kw8(33), psrc->fipo);
  dkct_tr_show_filename_and_lineno(psrc);
  fputs(dkct_tr_get_kw8(35), psrc->fipo);
  fputs(fnptr, psrc->fipo);
  fputs(dkct_tr_get_kw8(34), psrc->fipo);

  fputs(dkct_au_kw8[66], psrc->fipo);
  fputs(fnptr, psrc->fipo);
  fputs(dkct_au_kw8[(pos == 1) ? 69 : 67], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[(pos == 1) ? 75 : 68], psrc->fipo);

  fputs(dkct_tr_get_kw8(37), psrc->fipo);
  fputs(fnptr, psrc->fipo);
  fputs(dkct_tr_get_kw8(34), psrc->fipo);
  if((psrc->dkcto).deb == 1) {
    fputs(dkct_tr_get_kw8(31), psrc->fipo);
  }
  /* char 8 end */
  if((psrc->dkcto).win) {
    fputs(dkct_tr_get_kw8(23), psrc->fipo);
  }
  fputs(dkct_tr_get_kw8(25), psrc->fipo);
}



/**	Write debug output to show the matching rule.
	@param	psrc	Source structure.
	@param	stm	State machine.
	@param	rp	Rule to show.
*/
static
void
dkct_au_debug_matching_rule(
  DKCT_SRC *psrc, DKCT_STM *stm, DKCT_STM_RULE *rp
)
{
  char const	*fnptr;	/* Debug output file. */
  char const	*p1;	/* Current state. */
  char const	*p2;	/* Input. */
  char const	*p3;	/* Next state. */
  char const	*p4;	/* Output. */

  if((psrc->dkcto).deb) {
    p1 = p2 = p3 = p4 = dkct_au_kw8[0];
    if(rp) {
      if(rp->state) { p1 = (rp->state)->name; }
      if(rp->input) { p2 = (rp->input)->name; }
      if(rp->newstate) { p3 = (rp->newstate)->name; }
      if(rp->output) { p4 = (rp->output)->name; }
    } else {
      if(stm->ds) { p3 = (stm->ds)->name; }
      if(stm->dout) { p4 = (stm->dout)->name; }
    }
    fnptr = dkct_tr_get_kw8(((psrc->dkcto).deb == 2) ? 28 : 29);
    psrc->lineno = stm->lineno;
    if(rp) { psrc->lineno = rp->lineno; }
    fputs(dkct_tr_get_kw8(67), psrc->fipo);
    if((psrc->dkcto).win) {
      fputs(dkct_tr_get_kw8(21), psrc->fipo);
      /* char 16 start */
      if((psrc->dkcto).deb == 1) {
        fputs(dkct_tr_get_kw8(30), psrc->fipo);
      }
      fputs(dkct_tr_get_kw8(61), psrc->fipo);
      dkct_tr_show_filename_and_lineno(psrc);
      fputs(dkct_tr_get_kw8(35), psrc->fipo);
      fputs(fnptr, psrc->fipo);
      fputs(dkct_tr_get_kw8(34), psrc->fipo);
      fputs(dkct_tr_get_kw8(61), psrc->fipo);
      fputs(dkct_au_kw8[73], psrc->fipo);
      fputs(dkct_au_kw8[2], psrc->fipo);
      fputs(p1, psrc->fipo);
      fputs(dkct_au_kw8[2], psrc->fipo);
      fputs(p2, psrc->fipo);
      fputs(dkct_au_kw8[2], psrc->fipo);
      fputs(p3, psrc->fipo);
      fputs(dkct_au_kw8[2], psrc->fipo);
      fputs(p4, psrc->fipo);
      fputs(dkct_au_kw8[74], psrc->fipo);
      fputs(dkct_tr_get_kw8(35), psrc->fipo);
      fputs(fnptr, psrc->fipo);
      fputs(dkct_tr_get_kw8(34), psrc->fipo);
      fputs(dkct_tr_get_kw8(37), psrc->fipo);
      fputs(fnptr, psrc->fipo);
      fputs(dkct_tr_get_kw8(34), psrc->fipo);
      if((psrc->dkcto).deb == 1) {
        fputs(dkct_tr_get_kw8(31), psrc->fipo);
      }
      /* char 16 end */
      fputs(dkct_tr_get_kw8(22), psrc->fipo);
    }
    /* char 8 start */
    if((psrc->dkcto).deb == 1) {
      fputs(dkct_tr_get_kw8(30), psrc->fipo);
    }
    fputs(dkct_tr_get_kw8(33), psrc->fipo);
    dkct_tr_show_filename_and_lineno(psrc);
    fputs(dkct_tr_get_kw8(35), psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_tr_get_kw8(34), psrc->fipo);
    
    fputs(dkct_tr_get_kw8(33), psrc->fipo);
    fputs(dkct_au_kw8[73], psrc->fipo);
    fputs(dkct_au_kw8[2], psrc->fipo);
    fputs(p1, psrc->fipo);
    fputs(dkct_au_kw8[2], psrc->fipo);
    fputs(p2, psrc->fipo);
    fputs(dkct_au_kw8[2], psrc->fipo);
    fputs(p3, psrc->fipo);
    fputs(dkct_au_kw8[2], psrc->fipo);
    fputs(p4, psrc->fipo);
    fputs(dkct_au_kw8[74], psrc->fipo);
    fputs(dkct_tr_get_kw8(35), psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_tr_get_kw8(34), psrc->fipo);
    
    fputs(dkct_tr_get_kw8(37), psrc->fipo);
    fputs(fnptr, psrc->fipo);
    fputs(dkct_tr_get_kw8(34), psrc->fipo);
    if((psrc->dkcto).deb == 1) {
      fputs(dkct_tr_get_kw8(31), psrc->fipo);
    }

    /* char 8 end */
    if((psrc->dkcto).win) {
      fputs(dkct_tr_get_kw8(23), psrc->fipo);
    }
    fputs(dkct_tr_get_kw8(25), psrc->fipo);
  }
}



void
dkct_au_write_functions(DKCT_SRC *psrc, DKCT_STM *stm, int f_static)
{
  DKCT_STM_ENTRY	*prevstate;	/* Current state. */
  DKCT_STM_RULE		*rp;		/* Rule to process. */
  unsigned long		 nwildcards;	/* Number of wildcards. */
  unsigned long		 i;		/* Current wildcard block to finish. */
  /*
  	Reset function.
  */
  if(f_static) {
    fputs(dkct_au_kw8[23], psrc->fipo);
    dkct_au_write_stm_name(psrc, stm);
    fputs(dkct_au_kw8[24], psrc->fipo);
    fputs(dkct_au_kw8[25], psrc->fipo);
    fputs(dkct_au_kw8[32], psrc->fipo);
  }
  fputs(dkct_au_kw8[33], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[34], psrc->fipo);
  if(stm->rs) {
    fputs((stm->rs)->name, psrc->fipo);
  } else {
    fputs(dkct_au_kw8[36], psrc->fipo);
  }
  fputs(dkct_au_kw8[35], psrc->fipo);
  /*
  	Transition function.
  */
  if(f_static) {
    fputs(dkct_au_kw8[28], psrc->fipo);
    dkct_au_write_stm_name(psrc, stm);
    fputs(dkct_au_kw8[29], psrc->fipo);
    fputs(dkct_au_kw8[30], psrc->fipo);
    fputs(dkct_au_kw8[32], psrc->fipo);
  }
  fputs(dkct_au_kw8[37], psrc->fipo);
  dkct_au_write_stm_name(psrc, stm);
  fputs(dkct_au_kw8[38], psrc->fipo);
  if(stm->dout) {
    fputs((stm->dout)->name, psrc->fipo);
  } else {
    fputs(dkct_au_kw8[36], psrc->fipo);
  }
  fputs(dkct_au_kw8[39], psrc->fipo);
  if((psrc->dkcto).deb) {
    /* Debug messages for entering function */
    dkct_au_debug_transition(psrc, stm, 0);
  }
  fputs(dkct_au_kw8[40], psrc->fipo);
  if(stm->ds) {
    fputs((stm->ds)->name, psrc->fipo);
  } else {
    fputs(dkct_au_kw8[36], psrc->fipo);
  }
  fputs(dkct_au_kw8[39], psrc->fipo);
  fputs(dkct_au_kw8[46], psrc->fipo);
  /*
  	Exact rules
  */
  prevstate = NULL;
  dk3sto_it_reset(stm->i_exact);
  while((rp = (DKCT_STM_RULE *)dk3sto_it_next(stm->i_exact)) != NULL) {
    if(prevstate) {
      if(dk3str_c8_cmp(prevstate->name, (rp->state)->name)) {
        fputs(dkct_au_kw8[52], psrc->fipo);
        fputs(dkct_au_kw8[50], psrc->fipo);
        fputs(dkct_au_kw8[48], psrc->fipo);
        fputs((rp->state)->name, psrc->fipo);
        fputs(dkct_au_kw8[49], psrc->fipo);
	fputs(dkct_au_kw8[51], psrc->fipo);
      } else {
      }
    } else {
      fputs(dkct_au_kw8[47], psrc->fipo);
      fputs(dkct_au_kw8[48], psrc->fipo);
      fputs((rp->state)->name, psrc->fipo);
      fputs(dkct_au_kw8[49], psrc->fipo);
      fputs(dkct_au_kw8[51], psrc->fipo);
    }
    prevstate = rp->state;
    fputs(dkct_au_kw8[53], psrc->fipo);
    fputs((rp->input)->name, psrc->fipo);
    fputs(dkct_au_kw8[54], psrc->fipo);
    dkct_au_debug_matching_rule(psrc, stm, rp);
    dkct_au_write_transition_result(psrc, stm, rp, 12);
    fputs(dkct_au_kw8[55], psrc->fipo);
  }
  if(prevstate) {
    fputs(dkct_au_kw8[52], psrc->fipo);
    fputs(dkct_au_kw8[50], psrc->fipo);
    fputs(dkct_au_kw8[45], psrc->fipo);
  }
  fputs(dkct_au_kw8[44], psrc->fipo);
  /*
  	Wildcard rules
  */
  nwildcards = 0UL;
  dk3sto_it_reset(stm->i_wildcard);
  while((rp = (DKCT_STM_RULE *)dk3sto_it_next(stm->i_wildcard)) != NULL) {
    fputs(dkct_au_kw8[60], psrc->fipo);
    if(rp->state) {
      fputs(dkct_au_kw8[64], psrc->fipo);
      fputs((rp->state)->name, psrc->fipo);
    } else {
      if(rp->input) {
        fputs(dkct_au_kw8[65], psrc->fipo);
	fputs((rp->input)->name, psrc->fipo);
      } else {
        fputs(dkct_au_kw8[36], psrc->fipo);
      }
    }
    fputs(dkct_au_kw8[61], psrc->fipo);
    dkct_au_debug_matching_rule(psrc, stm, rp);
    dkct_au_write_transition_result(psrc, stm, rp, 6);
    fputs(dkct_au_kw8[62], psrc->fipo);
    nwildcards++;
  }
  dkct_au_debug_matching_rule(psrc, stm, NULL);
  for(i = 0; i < nwildcards; i++) {
    fputs(dkct_au_kw8[63], psrc->fipo);
  }
  fputs(dkct_au_kw8[45], psrc->fipo);
  fputs(dkct_au_kw8[77], psrc->fipo);
  fputs(dkct_au_kw8[42], psrc->fipo);
  /* DEBUG: Invalid st pointer */
  fputs(dkct_au_kw8[43], psrc->fipo);
  if((psrc->dkcto).deb) {
    /* Debug messages for entering function */
    dkct_au_debug_transition(psrc, stm, 1);
  }
  fputs(dkct_au_kw8[76], psrc->fipo);
  fputs(dkct_au_kw8[41], psrc->fipo);
}



/* vim: set ai sw=2 : */