epics-base-rs 0.30.0

Pure Rust EPICS IOC core — record system, database, iocsh, calc engine
Documentation
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
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
use std::collections::{HashMap, HashSet};
use std::sync::{Mutex, OnceLock};

use crate::error::{CaError, CaResult};
use crate::runtime::log::ERL_ERROR;
use crate::server::record::{FieldDesc, Record, check_link_text};
use crate::types::{DbfCode, EpicsValue, PvString};

mod include;
mod source;
mod substitution;
#[cfg(test)]
pub(crate) use include::parse_include_directive;
pub use include::{
    DbLoadConfig, DbOpenedFile, PATH_LIST_SEPARATOR, db_add_path, db_open_file,
    db_open_file_located, db_path, expand_includes, loaded_path, parse_db_file,
    parse_db_file_with_breaktables, parse_db_opened_with_breaktables, set_loaded_path,
};
pub use source::{DbIncludeFrame, DbSource};
pub use substitution::{
    RowMacro, SubstitutionEvent, SubstitutionFault, Substitutions, TemplateLoad,
    db_template_max_vars, parse_substitutions, set_db_template_max_vars,
};

/// Factory function that creates a record instance.
pub type RecordFactory = Box<dyn Fn() -> Box<dyn Record> + Send + Sync>;

/// Global registry of external record type factories.
/// External crates (e.g., asyn-rs) can register their own record types
/// to override built-in stubs.
static RECORD_FACTORY_REGISTRY: OnceLock<Mutex<HashMap<String, RecordFactory>>> = OnceLock::new();

fn get_registry() -> &'static Mutex<HashMap<String, RecordFactory>> {
    RECORD_FACTORY_REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}

/// Register an external record type factory.
///
/// Two jobs. It lets an external crate override a built-in — the factory is
/// checked FIRST in [`create_record`], so `asyn-rs`'s full `AsynRecord` wins
/// over the CNCT-only stub here. And it is how an application obtains a record
/// type this crate implements but does not register: `dbd/stdRecords.dbd` is
/// what an EPICS Base IOC links, and `aCalcout`, `sCalcout`, `sseq`, `swait`,
/// `transform`, `asyn` and `busy` are not in it. A `.db` naming one of those
/// fails to load until the application asks for it, exactly as a C IOC needs
/// the module's `.dbd` before `dbLoadRecords` will take the record.
pub fn register_record_type(name: &str, factory: RecordFactory) {
    snapshot_declared_fields(name, &factory);
    let mut reg = get_registry()
        .lock()
        .expect("record factory registry mutex poisoned");
    reg.insert(name.to_string(), factory);
}

/// Publish `name`'s `.dbd` field declarations to the by-name registry
/// [`crate::types::dbf_link_class`] reads.
///
/// A downstream record type's declarations live in that crate's own
/// `dbd_generated` and reach `epics-base-rs` only through the record it
/// builds, so the factory is asked for one throwaway instance here — every
/// factory in the workspace is a bare `Default::default()` constructor. A type
/// `dbd_generated` already covers reports the same table it would be looked up
/// in, so registering it changes nothing.
pub(crate) fn snapshot_declared_fields(name: &str, factory: &RecordFactory) {
    use crate::server::record::FieldDeclaration;
    let fields = factory().field_list();
    if !fields.is_empty() {
        declared_types()
            .lock()
            .expect("declared record type set mutex poisoned")
            .insert(name.to_string());
    }
    crate::server::record::register_declared_fields(name, fields);
}

/// Every record type whose field declarations this process can reach BY NAME
/// through the by-name registry, as opposed to through `dbd_generated`.
///
/// [`record_type_field_exists`] needs to tell "this type declares no such
/// field" apart from "this process cannot see this type's declarations at
/// all", and the by-name registry cannot: `declared_field` answers a `dbCommon`
/// match for a type nobody ever registered. This set is that missing bit, and
/// [`snapshot_declared_fields`] is the one funnel every registration path —
/// [`register_record_type`], `IocBuilder` and `IocApplication` — already goes
/// through, so recording it there keeps a single owner.
static DECLARED_TYPES: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();

fn declared_types() -> &'static Mutex<HashSet<String>> {
    DECLARED_TYPES.get_or_init(|| Mutex::new(HashSet::new()))
}

/// C `dbFindField` (`dbStaticLib.c:1847-1859` @`R7.0.10`), asked with a record
/// type NAME because the parser holds a type string and no instance.
///
/// `Some(true)`/`Some(false)` is C's answer. `None` means this process cannot
/// see the type's declarations, and then it must not answer at all: C reaches
/// `dbRecordField` only for a type `dbFindRecordType` already resolved —
/// `dbRecordHead` calls `yyerrorAbort` for anything else
/// (`dbLexRoutines.c:1159-1165`) — so a type the port cannot resolve has no C
/// behaviour to match here, and [`create_record`] is what rejects it later.
///
/// Three populations answer `true`, and they are C's three:
///
/// - the field table, which for a vendored type already folds in `dbCommon`;
/// - the `DBF_NOACCESS` names the generator drops from that table but keeps in
///   `record_noaccess_fields` / `DB_COMMON_NOACCESS` — C keeps them in
///   `papFldDes`, so `dbFindField` FINDS them and it is `dbPutString` that
///   then refuses the write ([`is_declared_no_access`] is that half);
/// - `RTYP` and `VERS`, which are not fields at all. `dbFindField` falls back
///   to `dbGetRecordAttribute` (`dbStaticLib.c:1852-1853`) and `dbReadCOM`
///   installs exactly those two on every record type
///   (`dbLexRoutines.c:311-331`), so `field(RTYP,"zz")` loads in C. Measured.
///
/// The comparison is case-SENSITIVE: `dbFindFieldPart` binary-searches
/// `papsortFldName` with `strncmp` (`dbStaticLib.c:1822-1824`), so
/// `field(calc,"1")` on a calc record is NOT the `CALC` field. Asking
/// `declared_field` (which matches case-insensitively) and then comparing the
/// declaration's own spelling is what makes that exact.
fn record_type_field_exists(record_type: &str, field: &str) -> Option<bool> {
    use crate::server::record::dbd_generated;

    let visible = dbd_generated::record_fields(record_type).is_some()
        || declared_types()
            .lock()
            .expect("declared record type set mutex poisoned")
            .contains(record_type);
    if !visible {
        return None;
    }
    if crate::server::record::declared_field(record_type, field).is_some_and(|d| d.name == field) {
        return Some(true);
    }
    let listed = |names: &[&str]| names.contains(&field);
    if dbd_generated::record_noaccess_fields(record_type).is_some_and(listed)
        || listed(dbd_generated::DB_COMMON_NOACCESS)
    {
        return Some(true);
    }
    Some(matches!(field, "RTYP" | "VERS"))
}

/// C's `dbFieldConfusionMap` (`dbLexRoutines.c:1220-1228` @`R7.0.10`): pairs of
/// field names that name the same idea on different record types, so a `.db`
/// written against one type is answered with the other type's spelling instead
/// of a lexical near-miss. The partner of entry `i` is entry `i ^ 1`, which is
/// why one flat table reads in both directions.
///
/// A trailing `A-J` is a RANGE rather than a literal name: the character in the
/// last position carries its offset across to the partner's range, which is
/// what turns `DOLA` into `INPK` and `INP0` into `INPA`.
const FIELD_CONFUSION_MAP: [&str; 14] = [
    "INP", "OUT", "DOL", "INP", "ZNAM", "ZRST", "ONAM", "ONST", "INPA-J", "DOL0-9", "INPK-P",
    "DOLA-F", "INP0-9", "INPA-J",
];

/// The field name entry `i` of [`FIELD_CONFUSION_MAP`] proposes for `name`, or
/// `None` when the entry does not apply. Whether the record type DECLARES the
/// proposal is the caller's test, exactly as it is C's.
fn confusion_guess(name: &str, i: usize) -> Option<String> {
    let fieldname = FIELD_CONFUSION_MAP[i].as_bytes();
    let replacement = FIELD_CONFUSION_MAP[i ^ 1].as_bytes();
    let n = name.as_bytes();
    let l = fieldname.len();

    if l >= 3 && fieldname[l - 2] == b'-' {
        // Range entry. C compares `l-3` bytes with `strncmp`, which a shorter
        // name loses on its terminator, and then reads `name[l-3]` — the
        // terminator again for a name that stops at the prefix, and no range
        // starts at NUL. Note what C does NOT test: the tail beyond `l-3` is
        // ignored, so `INPAX` proposes `DOL0` and is rejected only because no
        // record declares it.
        let k = l - 3;
        if n.len() < k || n[..k] != fieldname[..k] {
            return None;
        }
        let c = n.get(k).copied().unwrap_or(0);
        if c < fieldname[k] || c > fieldname[l - 1] {
            return None;
        }
        let l2 = replacement.len();
        let mut buf = replacement[..l2 - 2].to_vec();
        buf[l2 - 3] += c - fieldname[k];
        return String::from_utf8(buf).ok();
    }

    (n == fieldname).then(|| String::from_utf8_lossy(replacement).into_owned())
}

/// C `epicsStrSimilarity` (`epicsString.c:398-456` @`R7.0.10`) — a Levenshtein
/// distance whose substitution is weighted, normalised into `0.0..=1.0`.
///
/// The weights are the whole point and a plain edit distance is not a
/// substitute: a delete or an insert costs 2, a substitution between different
/// letters costs 2, and one between the same letter in different case costs 1.
/// So `"YES"` scores 0.5 against `"yes"` where a plain distance would score it
/// the same as `"NO"`. The normaliser is `2 * max(len)`, the cost of rewriting
/// every character, which makes a pair sharing no usable character score
/// exactly 0 — the value the caller reads as "not a candidate at all".
fn str_similarity(a: &str, b: &str) -> f64 {
    let (a, b) = (a.as_bytes(), b.as_bytes());
    let norm = 2 * a.len().max(b.len());
    let mut dist0: Vec<usize> = (0..=b.len()).map(|i| 2 * i).collect();
    let mut dist1 = vec![0usize; b.len() + 1];

    for (ai, &ca) in a.iter().enumerate() {
        dist1[0] = 2 * (ai + 1);
        for (bi, &cb) in b.iter().enumerate() {
            let delcost = dist0[bi + 1] + 2;
            let inscost = dist1[bi] + 2;
            let mut subcost = dist0[bi];
            if ca != cb {
                subcost += 1;
            }
            if !ca.eq_ignore_ascii_case(&cb) {
                subcost += 1;
            }
            dist1[bi + 1] = delcost.min(inscost).min(subcost);
        }
        std::mem::swap(&mut dist0, &mut dist1);
    }

    if norm == 0 {
        1.0
    } else {
        (norm - dist0[b.len()]) as f64 / norm as f64
    }
}

/// The factor C applies to a candidate's similarity once it has parsed the
/// assigned VALUE against the candidate's declared type
/// (`dbLexRoutines.c:1295-1368`): 1.0 when the value fits, 0.1 when it does
/// not, and for a menu or device field 0.5 or 1.5 for the two ways it can fit.
fn value_type_weight(record_type: &str, desc: &FieldDesc, value: &str) -> f64 {
    use crate::types::c_parse::{self, NumericField};

    let numeric = match desc.declared_dbf {
        DbfCode::Char => NumericField::Char,
        DbfCode::UChar => NumericField::UChar,
        DbfCode::Short => NumericField::Short,
        // C runs `DBF_ENUM` through `epicsParseUInt16` alongside `DBF_USHORT`
        // (`dbLexRoutines.c:1311-1314`) — an enum's `.db` spelling is its index.
        DbfCode::UShort | DbfCode::Enum => NumericField::UShort,
        DbfCode::Long => NumericField::Long,
        DbfCode::ULong => NumericField::ULong,
        DbfCode::Int64 => NumericField::Int64,
        DbfCode::UInt64 => NumericField::UInt64,
        DbfCode::Float => NumericField::Float,
        DbfCode::Double => NumericField::Double,
        DbfCode::Menu | DbfCode::Device => return menu_type_weight(record_type, desc, value),
        // C's `default:` arm leaves the status zero and `end` pointing AT the
        // quote it compares against, so a string or link candidate never takes
        // the mismatch penalty however the value is spelled.
        DbfCode::String
        | DbfCode::Inlink
        | DbfCode::Outlink
        | DbfCode::Fwdlink
        | DbfCode::NoAccess => return 1.0,
    };
    if c_parse::parse_auto_base_units_null(numeric, value).is_some() {
        1.0
    } else {
        0.1
    }
}

/// [`value_type_weight`]'s `DBF_MENU`/`DBF_DEVICE` arm: the value may name the
/// choice or index it, and C scores the two differently — a numeric device type
/// index halves the score because almost nobody writes one, a spelled-out
/// choice multiplies it by 1.5.
fn menu_type_weight(record_type: &str, desc: &FieldDesc, value: &str) -> f64 {
    use crate::types::c_parse::{self, NumericField};

    let device = matches!(desc.declared_dbf, DbfCode::Device);
    // C dereferences `pflddes->ftPvt` here without a NULL test and SEGFAULTS
    // when the loaded `.dbd` set declares no device support for the record
    // type: `record(calc,"C") { field(DOL, "1") }` kills softIoc @`R7.0.10`
    // outright, after the ERROR line and before any suggestion. An absent menu
    // is read here as an EMPTY one, which is the path C's own code takes for
    // `nChoice == 0` — neither the index nor the string can match, so the
    // candidate takes the mismatch penalty and the loader carries on.
    let contributed;
    let choices: &[&str] = if device {
        contributed = crate::server::record::merged_device_menu(record_type);
        &contributed
    } else {
        // `menu(menuScan)` is the one menu the generated table deliberately
        // leaves empty — its choices are the site's `menuScan.dbd`, resolved at
        // run time — so `SCAN` and `SSCN` come from the shared registry that
        // every other reader of a menu field uses. C has no such split: it
        // reads `pflddes->ftPvt`, which IS the loaded menu whatever the site
        // declared, so taking base's compile-time table here would answer for
        // the wrong menu on a site that ships its own rates.
        desc.menu
            .or_else(|| crate::server::record::shared_menu_choices(desc.name))
            .unwrap_or(&[])
    };

    if let Some(EpicsValue::UShort(index)) =
        c_parse::parse_auto_base_units_null(NumericField::UShort, value)
    {
        if usize::from(index) < choices.len() {
            return if device { 0.5 } else { 1.0 };
        }
    }
    if choices.contains(&value) { 1.5 } else { 0.1 }
}

/// C's suggestion line for `desc` (`dbLexRoutines.c:1379-1384` @`R7.0.10`),
/// without its newline.
///
/// Byte-exact against C: four leading spaces, the proposal in double quotes,
/// the question mark, then — only for a field whose `.dbd` declares a
/// `prompt` — TWO spaces and the prompt in parentheses.
fn suggestion_line(desc: &FieldDesc) -> String {
    let mut line = did_you_mean(desc.name);
    if let Some(prompt) = desc.prompt {
        line.push_str(&format!("  ({prompt})"));
    }
    line
}

/// The proposal both of C's `.db` suggestions are built from — four leading
/// spaces, the name in double quotes, a question mark
/// (`dbLexRoutines.c:1381`, `dbStaticLib.c:2702`), without its newline.
fn did_you_mean(name: &str) -> String {
    format!("    Did you mean \"{name}\"?")
}

/// The field C would propose after refusing `name` on `record_type`, or `None`
/// when C proposes nothing (`dbLexRoutines.c:1241-1378` @`R7.0.10`).
///
/// `None` is a real answer, not a fallback: every candidate whose similarity
/// weighs out to exactly zero is skipped, so a name sharing no character with
/// anything the record type declares — `field(ZZZZ, "1")` on an `ai` — leaves
/// the ERROR line standing alone rather than reaching for the least-bad name.
///
/// The confusion map is consulted first and wins outright when it hits, which
/// is why `bo.INP` proposes `OUT` rather than the `INP`-shaped near-misses that
/// score higher lexically.
fn suggest_field(record_type: &str, name: &str, value: &str) -> Option<&'static FieldDesc> {
    use crate::server::record::{Special, declared_field, declared_fields};

    for i in 0..FIELD_CONFUSION_MAP.len() {
        let Some(guess) = confusion_guess(name, i) else {
            continue;
        };
        // C resolves the proposal through `dbFindFieldPart`, an exact search of
        // the record type's sorted field names, so a proposal the type does not
        // declare is dropped and the next entry tried.
        if let Some(desc) = declared_field(record_type, &guess).filter(|d| d.name == guess) {
            return Some(desc);
        }
    }

    let mut best: Option<&'static FieldDesc> = None;
    let mut best_sim = -1.0_f64;
    for desc in declared_fields(record_type) {
        // A field the record type will not let a `.db` set is not a candidate
        // for having been misspelled INTO. The test is on the DECLARATION: C
        // reads `pdbFldDes->special`, which an SPC_DBADDR field's `cvt_dbaddr`
        // never rewrites, so `lsi.VAL` stays skipped even though the address it
        // resolves to carries `SPC_MOD`.
        if matches!(desc.declared_special, Special::NoMod | Special::DbAddr) {
            continue;
        }
        let mut sim = str_similarity(name, desc.name);
        if desc.promptgroup.is_none() {
            sim *= 0.5;
        }
        if desc.interest != 0 {
            sim *= 1.0 - 0.1 * f64::from(desc.interest);
        }
        if sim == 0.0 {
            continue;
        }
        if !value.is_empty() {
            sim *= value_type_weight(record_type, desc, value);
        }
        // `>`, not `>=`: a tie goes to the field declared first, and `dbCommon`
        // is included ahead of the record's own fields.
        if sim > best_sim {
            best_sim = sim;
            best = Some(desc);
        }
    }
    best
}

/// The `.dbd` name and choice table of the menu `field` resolves its `.db`
/// value against, or `None` when the field is not a menu.
///
/// A menu field is one declared `DBF_MENU` — or one that carries its own
/// choice table, however its `DbfCode` reads. An externally-registered
/// record type builds its `FieldDesc`s in its own crate, so its tables are
/// not the generated `MENUS` statics and its `declared_dbf` is whatever
/// [`FieldDesc::new`] derived from the served type (`Enum`, mostly); C could
/// only have written such a field as `DBF_MENU` with a named `menu()`, so a
/// carried table is treated as that declaration. The name half of C's pair
/// is then unavailable — the `MENUS` lookup is by table identity and the
/// external static is not in it — which is why the name is an `Option`:
/// validation does not need it, only the refusal wording does.
///
/// `DBF_DEVICE` is deliberately not answered here. C's device menu is the
/// record type's `device()` lines, so every DTYP a `.db` may name is in it; the
/// port has a second channel — `IocBuilder::register_device_support` and
/// `register_dynamic_device_support` — whose factories never reach
/// `merged_device_menu`, so a valid `field(DTYP,"asynInt32")` is absent from
/// the menu that would judge it.
///
/// `menuScan` is why the menu's NAME is looked up rather than carried on the
/// declaration: its choices are the site's, resolved at run time, so the
/// generated `FieldDesc` holds `None` and the table is in no generated row.
fn load_menu_of(desc: &FieldDesc) -> Option<(Option<&'static str>, &'static [&'static str])> {
    use crate::server::record::shared_menu_choices;

    // `DBF_DEVICE` stays out even when it carries a table: its channel is
    // the device menu, and its C refusal is worded differently (see above).
    if desc.declared_dbf != DbfCode::Menu
        && (desc.menu.is_none() || desc.declared_dbf == DbfCode::Device)
    {
        return None;
    }
    let choices = desc
        .menu
        .or_else(|| shared_menu_choices(desc.name))
        .unwrap_or(&[]);
    if choices.is_empty() {
        return None;
    }
    Some((menu_name_of(choices), choices))
}

/// The `.dbd` name of the menu whose choice table is `choices`.
///
/// C reads it off the menu itself (`pdbMenu->name`, `dbStaticRun.c:497`). The
/// generated `MENUS` table is name -> choices and a menu field's `menu` member
/// is that same static, so identity — not equality — is the relation: two menus
/// may list the same strings, and only identity tells them apart.
fn menu_name_of(choices: &'static [&'static str]) -> Option<&'static str> {
    use crate::server::record::dbd_generated::MENUS;

    if std::ptr::eq(choices, crate::server::record::menu_scan().choices()) {
        return Some("menuScan");
    }
    MENUS
        .iter()
        .find(|(_, _, table)| std::ptr::eq(*table, choices))
        .map(|(name, _, _)| *name)
}

/// C's refusal for a `.db` field value `dbPutString` would not take
/// (`dbLexRoutines.c:1406-1413` @`R7.0.10`), without its newline.
///
/// `detail` is `pdbentry->message`, which C prints unconditionally between the
/// value and the status text — an arm that set no message still leaves the two
/// spaces that produces.
fn cant_set_line(record: &str, field: &str, value: &str, detail: &str, status: &str) -> String {
    format!("{ERL_ERROR}: Can't set '{record}.{field}' to '{value}' {detail} : {status}")
}

/// The refusal C's `.db` loader would print for `value` on `field`, or `None`
/// when C takes the value.
///
/// This is `dbPutString` (`dbStaticLib.c:2570`) read as a predicate: the field
/// type it switches on is the DECLARED `DBF_*` token, and each arm either
/// stores or returns a status `dbRecordField` then prints. The name says menu
/// because the menu arm was ported first; the numeric arms are
/// [`numeric_value_refusal`] and reach the same two load paths through the same
/// three call sites, which is the point of deciding it here rather than at
/// whichever site happens to apply the field.
///
/// The menu arm of `dbPutStringNum` (`dbStaticRun.c:484-512`) has TWO refusals,
/// and they read differently. A value that is neither an exact choice nor a
/// number is `S_db_badChoice`, and the arm names the menu first
/// (`dbMsgPrint(pdbentry, "using menu %s", pdbMenu->name)`). A value that IS a
/// number but fails C's out-of-menu bound is `S_dbLib_badField` with no message
/// at all, which is why that line carries two spaces where the other carries
/// the menu.
///
/// The bound itself is not restated here: it already has an owner — the loader
/// arm of the single menu converter, which lets one past the last choice and
/// `65535` through — so this asks it, then asks only whether the value parsed,
/// to pick which of C's two wordings applies.
///
/// `None` also covers a menu that resolves no choice table at all; such a
/// field falls to the numeric arms. A menu whose NAME did not resolve — an
/// externally-registered record type's own table — still validates against
/// its choices; only the refusal wording loses the name C would print.
/// DTYP is deliberately NOT judged here, and the omission is a decision
/// rather than an oversight. C refuses a bad `field(DTYP,…)` during the
/// `.db` parse, through `dbPutStringNum`'s own `DBF_DEVICE` branch
/// (`dbStaticRun.c:499-501`), and words it its own way — `no such device
/// support for '<record type>' record type`, NOT the menu wording this
/// function builds. The port instead warns at `iocInit` from
/// `wire_device_support` and lets the load succeed. Closing that gap needs a
/// decision about when the device menu is considered CLOSED: C loads every
/// `.dbd` before any `.db`, so `dbDeviceMenu` is complete at put time,
/// whereas the port's `contributed_device_menu` is filled by downstream
/// crates (asyn) AFTER the load — refusing here would fail a
/// `field(DTYP,"asynInt32")` that works today.
pub(crate) fn menu_value_refusal(
    record_type: &str,
    record_name: &str,
    field: &str,
    value: &str,
) -> Option<MenuRefusal> {
    use crate::server::record::declared_field;

    let desc = declared_field(record_type, field)?;
    // C `dbPutString` refuses ANY field value that still carries a macro,
    // before the field-type switch it would otherwise reach
    // (`dbStaticLib.c:2579-2585` @`R7.0.10`). The `macroIsOk` half of that test
    // is `dbIsMacroOk(pdbentry)`, whose only definition in base is
    // `return(FALSE)` (`dbStaticRun.c:228`), so it exempts nothing in an IOC
    // and `stringHasMacro` alone decides; `dbLoadTemplate` defers by expanding
    // its substitutions before the text reaches the loader, not through this
    // hook.
    if value.contains("$(") || value.contains("${") {
        return Some(MenuRefusal {
            notice: Some(format!("{record_name}.{field} Has unexpanded macro")),
            // `S_dbLib_badField` with no `pdbentry->message`, which is what
            // leaves two spaces before the colon.
            line: cant_set_line(record_name, field, value, "", "Bad Field value"),
            // `dbRecordField` runs `dbPutStringSuggest` after every refusal
            // (`dbLexRoutines.c:1414`) and only its menu and device arms
            // propose anything, so a menu field still gets a proposal off the
            // unexpanded text -- measured on `compressTest.db`,
            // `field(ALG,"$(ALG,undefined)")` gets `Did you mean "Average"?`
            // while `field(NSAM,"$(NSAM,undefined)")` gets nothing.
            suggestion: load_menu_of(desc).and_then(|(_, choices)| suggest_choice(choices, value)),
        });
    }
    let probe = db_put_string_probe(value);
    let (detail, status, suggestion) = match load_menu_of(desc) {
        Some((menu_name, choices)) => menu_arm(desc, field, menu_name, choices, probe, value)?,
        // Not a menu — or one that resolves no choice table. Either way the
        // numeric arms of the same C function decide it.
        None => (
            String::new(),
            numeric_value_refusal(desc.declared_dbf, field, probe)?,
            None,
        ),
    };
    Some(MenuRefusal {
        notice: None,
        line: cant_set_line(record_name, field, value, &detail, status),
        suggestion,
    })
}

/// The `DBF_MENU` arm of `dbPutStringNum` (`dbStaticRun.c:484-512`), as the
/// three pieces its refusal is printed from.
fn menu_arm(
    desc: &FieldDesc,
    field: &str,
    menu_name: Option<&str>,
    choices: &[&'static str],
    probe: &str,
    value: &str,
) -> Option<(String, &'static str, Option<String>)> {
    use crate::server::record::resolve_menu_field_string_db_load;
    use crate::types::c_parse::{self, NumericField};

    if resolve_menu_field_string_db_load(field, choices, desc.dbf_type, probe).is_ok() {
        return None;
    }
    let (detail, status) = match c_parse::parse_auto_base_units_null(NumericField::UShort, probe) {
        // `epicsParseUInt16` succeeded, so the bound is what refused it.
        Some(_) => (String::new(), "Bad Field value"),
        // C words this with `pdbMenu->name`; an externally-registered menu
        // has no `.dbd` name, and the field's own name is the nearest thing
        // the wording can carry.
        None => (
            format!("using menu {}", menu_name.unwrap_or(field)),
            "Illegal choice",
        ),
    };
    // C suggests off the value it was GIVEN, not the "0" the numeric arm
    // substituted for an empty one — `dbRecordField` hands
    // `dbPutStringSuggest` its own `value` (`dbLexRoutines.c:1414`). Only the
    // menu and device arms of that function propose anything at all
    // (`dbStaticLib.c:2669-2708`), which is why the numeric refusals below
    // carry no suggestion.
    Some((detail, status, suggest_choice(choices, value)))
}

/// The `errSymLookup` text of the `S_stdlib_*` statuses (`epicsStdlib.h:43-51`)
/// `dbRecordField` prints after the value it refused.
const S_STDLIB_NO_CONVERSION: &str = "No digits to convert";
const S_STDLIB_EXTRANEOUS: &str = "Extraneous characters";
const S_STDLIB_UNDERFLOW: &str = "Too small to represent";
const S_STDLIB_OVERFLOW: &str = "Too large to represent";

/// C `dbPutStringNum`'s "empty string is the same as writing numeric zero"
/// (`dbStaticRun.c:405-407`).
///
/// C substitutes once, at the top of the function, so the choice test and the
/// numeric arms both see the `"0"` — `field(PINI,"")` is index 0 and
/// `field(PREC,"")` is the number 0, from one rule rather than two.
fn db_put_string_probe(value: &str) -> &str {
    if value.is_empty() { "0" } else { value }
}

/// The numeric arms of C `dbPutStringNum` (`dbStaticRun.c:409-481` @`R7.0.10`)
/// as the status text of the refusal they return, or `None` when C stores the
/// value.
///
/// `dbConvertStrict` is zero unless an IOC sets it (`dbStaticRun.c:35`), so the
/// integer widths do NOT range-check against the field: every signed one parses
/// through `epicsParseInt64` and every unsigned one through `epicsParseUInt64`,
/// and the 64-bit result is then CAST to the field. `field(PHAS,"70000")`
/// therefore loads and stores 4464 and `field(TPRO,"-1")` stores 255; only what
/// `strtol`/`strtoul` itself refuses — no digits, a 64-bit overflow, or a
/// trailing non-space tail — fails the load, which is why `field(VAL,"1.0")`
/// on a `longout` is "Extraneous characters". `DBF_ENUM` shares the unsigned
/// arm rather than the menu one, so `field(VAL,"ONE")` on an `mbbo` is "No
/// digits to convert" and never a choice lookup.
fn numeric_value_refusal(declared: DbfCode, field: &str, s: &str) -> Option<&'static str> {
    db_load_numeric_value(declared, field, s).err()
}

/// The numeric arms of C `dbPutStringNum` in full: `Ok(Some(v))` is the value C
/// STORES, `Err(status)` the refusal it returns, and `Ok(None)` a declaration
/// these arms do not decide.
///
/// The stored value is what makes this the whole arm rather than half of it.
/// `dbConvertStrict` is zero unless an IOC sets it (`dbStaticRun.c:35`), so the
/// integer widths do not range-check against the field: every signed one parses
/// through `epicsParseInt64` and every unsigned one through `epicsParseUInt64`,
/// and the 64-bit result is then CAST. Reading only the refusal out of that
/// leaves the truncation behind, and the port then refused `field(PHAS,"70000")`
/// where C stores 4464 — the mirror of the values it used to accept.
fn db_load_numeric_value(
    declared: DbfCode,
    field: &str,
    s: &str,
) -> Result<Option<EpicsValue>, &'static str> {
    use crate::runtime::stdlib::{ParseDoubleError, epics_parse_double};
    use crate::types::c_parse::{self, NumericField};

    let s = db_put_string_probe(s);
    // The 64-bit parse C runs and the cast it then makes, as one row per
    // declared width (`dbStaticRun.c:433-438`, `:466-471`). Both casts go
    // through `u64` because two's-complement truncation is the same operation
    // either way: `-40000` reaches a `DBF_SHORT` as 25536 and `-1` reaches a
    // `DBF_UCHAR` as 255, which is what C's `(epicsInt16)` / `(epicsUInt8)`
    // does to `strtol`'s and `strtoul`'s answers.
    let (width, cast): (NumericField, fn(u64) -> EpicsValue) = match declared {
        DbfCode::Char => (NumericField::Int64, |v| EpicsValue::Char(v as u8)),
        DbfCode::Short => (NumericField::Int64, |v| EpicsValue::Short(v as i16)),
        DbfCode::Long => (NumericField::Int64, |v| EpicsValue::Long(v as i32)),
        DbfCode::Int64 => (NumericField::Int64, |v| EpicsValue::Int64(v as i64)),
        DbfCode::UChar => (NumericField::UInt64, |v| EpicsValue::UChar(v as u8)),
        // C gives `DBF_ENUM` and `DBF_USHORT` one case, so a label never
        // resolves on an `mbbo` VAL — `field(VAL,"ONE")` is "No digits to
        // convert" and not a choice lookup.
        DbfCode::Enum | DbfCode::UShort => (NumericField::UInt64, |v| EpicsValue::UShort(v as u16)),
        DbfCode::ULong => (NumericField::UInt64, |v| EpicsValue::ULong(v as u32)),
        DbfCode::UInt64 => (NumericField::UInt64, |v| EpicsValue::UInt64(v)),
        DbfCode::Float | DbfCode::Double => {
            let v = epics_parse_double(s).map_err(|e| match e {
                ParseDoubleError::NoConversion => S_STDLIB_NO_CONVERSION,
                ParseDoubleError::Extraneous => S_STDLIB_EXTRANEOUS,
                ParseDoubleError::Underflow => S_STDLIB_UNDERFLOW,
                ParseDoubleError::Overflow => S_STDLIB_OVERFLOW,
            })?;
            if declared == DbfCode::Double {
                return Ok(Some(EpicsValue::Double(v)));
            }
            // `epicsParseFloat32` is `epicsParseDouble` plus two more gates
            // (`epicsStdlib.c:325-329`), whose owner is `narrow_to_f32`. They
            // are disjoint by magnitude, so the value itself names the side it
            // fell off.
            return match c_parse::narrow_to_f32(v) {
                Some(f) => Ok(Some(EpicsValue::Float(f))),
                None if v.abs() <= f64::from(f32::MIN_POSITIVE) => Err(S_STDLIB_UNDERFLOW),
                None => Err(S_STDLIB_OVERFLOW),
            };
        }
        // C's `dbPutString` sends the rest somewhere that is not a numeric
        // parse: a link to `dbParseLink`, a `DBF_STRING` to a length check
        // against the field size, `DBF_DEVICE` to the device menu, and
        // `DBF_NOACCESS` to "Can't set array field before iocInit()"
        // (`dbStaticLib.c:2586-2650`). None of them is decided here.
        DbfCode::String
        | DbfCode::Menu
        | DbfCode::Device
        | DbfCode::Inlink
        | DbfCode::Outlink
        | DbfCode::Fwdlink
        | DbfCode::NoAccess => return Ok(None),
    };
    let bits = match c_parse::parse_auto_base_units_null(width, s) {
        // `parse_auto_base_units_null` answers in the variant its width names,
        // and `width` is only ever one of these two.
        Some(EpicsValue::Int64(v)) => v as u64,
        Some(EpicsValue::UInt64(v)) => v,
        _ => return Err(integer_refusal(width, field, s)),
    };
    Ok(Some(cast(bits)))
}

/// Which of `epicsParseLong`'s three statuses (`epicsStdlib.c:38-47`) refused
/// `s`, once the strict parse has said that one of them did.
fn integer_refusal(
    width: crate::types::c_parse::NumericField,
    field: &str,
    s: &str,
) -> &'static str {
    // `strtol` with base 0 either converts a digit or converts nothing, since
    // both prefixes it detects begin with the digit `0`, so the first character
    // after the leading whitespace and sign decides `endp == str`.
    let scanned = s.trim_start_matches(crate::runtime::stdlib::c_isspace);
    let scanned = scanned.strip_prefix(['-', '+']).unwrap_or(scanned);
    if !scanned.starts_with(|c: char| c.is_ascii_digit()) {
        return S_STDLIB_NO_CONVERSION;
    }
    // The same scan with the `units == NULL` tail test dropped, which is the
    // only other thing the strict form refuses for: digits that still do not
    // convert are `strtol`'s own ERANGE.
    if crate::types::c_parse::put_string_element(field, width, s).is_err() {
        S_STDLIB_OVERFLOW
    } else {
        S_STDLIB_EXTRANEOUS
    }
}

/// A refused `.db` menu value, with every line C prints for it.
pub(crate) struct MenuRefusal {
    /// What `dbPutString` itself printed before returning the status
    /// (`dbStaticLib.c:2582-2584`), which therefore precedes the `ERROR:` line
    /// its caller prints. `None` for the arms that return a status silently.
    pub notice: Option<String>,
    /// The `ERROR:` line.
    pub line: String,
    /// C `dbPutStringSuggest`'s proposal, or `None` when it proposes nothing.
    pub suggestion: Option<String>,
}

/// The choice C would propose after refusing `value` (`dbPutStringSuggest`,
/// `dbStaticLib.c:2665-2708` @`R7.0.10`), or `None` when it proposes none.
///
/// `maxdist` starts at 0.0 and the test is strict `>`, so a choice that scores
/// exactly zero is never offered and the earliest of equal scorers wins — the
/// same "no similarity, no suggestion" rule the field-name half follows, and
/// the reason `field(PINI,"7")` gets a bare refusal.
///
/// The metric is C's own `epicsStrSimilarity`, unweighted here: unlike the
/// field-name suggestion this one has no promptgroup, interest or type to
/// weigh a candidate by — a menu choice is just a string.
fn suggest_choice(choices: &[&'static str], value: &str) -> Option<String> {
    let mut best: Option<&str> = None;
    let mut max_dist = 0.0_f64;
    for choice in choices {
        let dist = str_similarity(value, choice);
        if dist > max_dist {
            max_dist = dist;
            best = Some(choice);
        }
    }
    best.map(did_you_mean)
}

/// Every externally registered record type, as `(name, entry address)`.
///
/// The address is the registered factory's own, which is what C's
/// `registryRecordTypeFind` prints: `registryAdd` stores the
/// `recordTypeLocation` pointer and the `%p` the command prints
/// (`registryIocRegister.c:31`) is that stored value. Read by the
/// `registry*` iocsh family; the built-in half of the type universe is
/// [`crate::server::record::dbd_generated::RECORD_TYPES`], which this map
/// overrides (see [`create_record`]).
pub(crate) fn registered_record_type_entries() -> Vec<(String, usize)> {
    let reg = get_registry()
        .lock()
        .expect("record factory registry mutex poisoned");
    reg.iter()
        .map(|(name, factory)| (name.clone(), (&**factory) as *const _ as *const () as usize))
        .collect()
}

/// A record definition parsed from a .db file.
#[derive(Debug, Clone)]
pub struct DbRecordDef {
    pub record_type: String,
    pub name: String,
    /// The record's fields, in file order. A `.db` value is a BYTE STRING, not
    /// text: C's escape translation writes one byte per `\xHH`
    /// (`epicsString.c:106`), and a DBF_STRING's budget is 40 BYTES. Modelling
    /// the value as a Rust `String` made `\xff` two bytes (R19-68).
    pub fields: Vec<DbFieldDef>,
    /// Aliases declared inside the record body (`alias("name")`).
    /// Mirrors epics-base PR #336 — alias names are validated with
    /// the same rules as record names.
    pub aliases: Vec<String>,
    /// `info(tag, value)` pairs declared inside the record body.
    /// Mirrors `info` directives in EPICS db files; common tags include
    /// `asyn:READBACK` (asyn upstream PRs #60 / #208), `Q:form`, etc.
    pub info_tags: Vec<(String, String)>,
    /// 1-based line of the flattened, macro-expanded text the record HEAD
    /// ended on — the index [`DbSource::at`] takes, and C's
    /// `pinputFileNow->line_num` when `dbRecordHead` reduces.
    ///
    /// The head and not the body, because that is where C decides the record
    /// exists: `dbCreateRecord` runs from the `record_head` action
    /// (`dbYacc.y:227-235`), so an unknown record type is refused with the
    /// closing `)` still the last token the lexer matched — ` at or before
    /// ')' … line 1` on `softIoc` @`R7.0.10`, not the line of the `}` far
    /// below. It travels ON the record for the reason [`DbFieldDef::line`]
    /// gives: the install loop reorders and drops entries, and a parallel
    /// vector would hand the survivors somebody else's line.
    ///
    /// `0` is "no source behind this record" — one built programmatically,
    /// where C has no line either.
    pub line: u32,
}

/// One `field(NAME,"value")` element, with where it was read from.
///
/// The line is not decoration: C refuses a field value inside the parse,
/// so `yyerror` can name the line from `pinputFileNow` (`dbYacc.y:380`).
/// This port screens the same values in the install loop, after the text
/// is gone, so the position has to travel with the element — and it has
/// to travel ON it rather than in a parallel vector, because the install
/// loop `retain`s the list and a parallel vector would silently shift
/// every remaining field's line by one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DbFieldDef {
    pub name: String,
    pub value: PvString,
    /// 1-based line of the flattened, macro-expanded text — the index
    /// [`DbSource::at`] takes. C's `pinputFileNow->line_num` when
    /// `dbRecordField` reduces, which is the line the element's closing
    /// `)` sits on.
    pub line: u32,
}

impl DbFieldDef {
    /// A field with no source behind it — a record built programmatically
    /// or by a test, where C has no line either.
    pub fn new(name: impl Into<String>, value: impl Into<PvString>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            line: 0,
        }
    }
}

/// Validate a record (or alias) name against epics-base PR #78 rules.
///
/// Returns `Err(CaError::DbParseError)` for the hard-error cases (empty
/// name, embedded space/tab/quote/dot/dollar). Logs `tracing::warn!`
/// for the soft-warning cases (leading `-`/`+`/`[`/`{`, embedded
/// non-printable characters); the parse continues so legacy databases
/// still load.
///
/// The check runs **after** macro substitution, mirroring base where
/// `dbRecordHead` is invoked from the lexer with the substituted name.
///
/// The third argument is no longer read. C locates both refusals at the `)`
/// the `record_head`/`alias` production had just reduced — measured on
/// `softIoc` @`R7.0.10`, `record(ai, "")` and `record(ai, "a b")` both print
/// ` at or before ')'` — so the locator is a constant, not something a caller
/// can know better. Dropping the parameter needs the iocsh caller that still
/// passes one, which is another panel's file.
pub(crate) fn validate_record_name(name: &str, line: usize, _col: usize) -> CaResult<()> {
    if name.is_empty() {
        return Err(CaError::DbParseError {
            line,
            token: String::from(")"),
            message: "record/alias name can't be empty".into(),
        });
    }
    for (i, c) in name.chars().enumerate() {
        if i == 0 && matches!(c, '-' | '+' | '[' | '{') {
            tracing::warn!(name, "record/alias name should not begin with '{}'", c);
        }
        // Non-printable ASCII (< space) is a warning, not an error —
        // matches base PR #78's `errlogPrintf("Warning: ...")` branch.
        if (c as u32) < 0x20 {
            tracing::warn!(
                name,
                "record/alias name should not contain non-printable 0x{:02X}",
                c as u32
            );
            continue;
        }
        if matches!(c, ' ' | '\t' | '"' | '\'' | '.' | '$') {
            return Err(CaError::DbParseError {
                line,
                token: String::from(")"),
                message: format!("bad character '{c}' in record/alias name \"{name}\""),
            });
        }
    }
    Ok(())
}

/// The single owner of C's `yyerror` / `yyerrorAbort` distinction.
///
/// C's `.db` reader has two error routines and uses both inside the same
/// file: `yyerror(NULL)` (`dbLexRoutines.c:453`, `:1178`) records the
/// failure, leaves everything already parsed in `pdbbase`, and lets
/// `pvt_yy_parse` return -1 once the whole text has been read
/// (`dbYacc.y:384-398`); `yyerrorAbort` (`dbLexRoutines.c:1133`, `:1166`)
/// stops the parse outright.
///
/// Invariant: a per-item failure records a diagnostic and a non-zero final
/// status but MUST NOT discard items already parsed; only an abort-class
/// failure — an `Err` return out of the parse — stops it. Every
/// recoverable site reports through [`DbFaults::recoverable`], which is
/// the one place that decides what a recoverable failure does, so no
/// caller has to re-derive the classification.
///
/// Emission invariant: [`DbFaults::recoverable`] is the SOLE owner of a
/// loader diagnostic's trip to the operator. Every other holder of a
/// `DbFaults` MUST treat it as a record that a diagnostic was emitted, and
/// MUST NOT emit one itself; a command that wants to say the load failed
/// says so in C's own words — `ERROR: Failed to load '<file>'` — and never
/// by repeating a line `recoverable` has already written. The `.db` loader
/// had exactly that double voice: `recoverable` printed AND stored, a
/// `status()` accessor handed the stored copy back, and `dbLoadRecords`
/// returned it as the shell's error text, so the port said each diagnostic
/// twice where C says it once. The rule is enforced by construction rather
/// than by convention: `messages` is private, no error value the loader
/// returns carries text, and the only accessor that yields the string is
/// `#[cfg(test)]`, so outside this crate's own unit tests a second print
/// cannot be written at all.
/// One recoverable `.db` diagnostic with every line C prints for it, in
/// C's order.
///
/// The order is the whole reason this is one value rather than three
/// calls. C writes a refusal as `dbPutString`'s own line, then the
/// `ERROR:` line `dbRecordField` builds from the status it returned,
/// then `dbPutStringSuggest`'s proposal, and only then `yyerror(NULL)`
/// (`dbLexRoutines.c:1379-1387`, `:1414`) — so the suggestion lands
/// ABOVE the position context, not below it. A `DbFaults` that also
/// offered a bare "print this line" method let a caller choose the side,
/// and both callers chose the wrong one: `Did you mean "Average"?`
/// printed after the ` 8 | …` echo instead of before the `ERROR: ` that
/// introduces it. There is no such method now; the order is a property
/// of the value and cannot be restated at a call site.
#[derive(Debug)]
pub struct DbDiagnostic {
    /// C `dbPutString`'s own `fprintf` before it returns the status
    /// (`dbStaticLib.c:2582-2584`) — printed ABOVE the `ERROR:` line.
    pub notice: Option<String>,
    /// The `ERROR:` line itself, and the only part recorded as a fault.
    pub message: String,
    /// C `dbPutStringSuggest`'s proposal — printed BELOW the `ERROR:`
    /// line and ABOVE `yyerror`'s position context.
    pub suggestion: Option<String>,
}

impl DbDiagnostic {
    /// The bare shape: an `ERROR:` line with nothing around it.
    pub fn new(message: String) -> Self {
        Self {
            notice: None,
            message,
            suggestion: None,
        }
    }

    /// The same line with C's `dbPutStringSuggest` proposal under it.
    pub fn suggesting(message: String, suggestion: Option<String>) -> Self {
        Self {
            notice: None,
            message,
            suggestion,
        }
    }
}

#[derive(Debug, Default)]
pub struct DbFaults {
    messages: Vec<String>,
    /// C's lexer state that `yyerror` reads — `pinputFileNow` and
    /// `my_buffer` (`dbYacc.y:374-381`) — for the whole flattened text.
    source: Option<source::DbSource>,
    /// Where in [`Self::source`] a diagnostic is currently about, i.e.
    /// where C's `pinputFileNow` would be parked, together with C's
    /// `yytext` — the token that names the spot inside the line.
    ///
    /// A token and not a column: C's ` at or before '%s'` quotes the text
    /// the lexer had matched, and every abort this port raises knows that
    /// text, because a recursive-descent parser rejects AT a token it has
    /// just read or is looking at. Carrying a column instead was this
    /// port's own invention and read ` at or before column 1` where C
    /// reads ` at or before '}'`.
    ///
    /// ONE field, not a line beside a token, because the two must agree:
    /// a line from one diagnostic wearing another's token is a position
    /// that points nowhere, and a caller that could set them apart would
    /// eventually set exactly that. `None` means the position is unknown
    /// and the context is omitted — this port emits some diagnostics
    /// after the parse has finished, and a stale line number is worse
    /// for the operator than no line number, so the position is CLEARED
    /// at the end of the parse rather than left to decay. See
    /// [`Self::seek`].
    park: Option<(u32, String)>,
    /// C's `yyFailed` (`dbYacc.y:377-380`): ` at or before` and the
    /// include stack are printed for the FIRST diagnostic of a text
    /// only, and every one after it gets the line echo alone.
    yy_failed: bool,
}

impl DbFaults {
    /// Bind the text a diagnostic will be located in — the port's
    /// standing-in for C's lexer holding `inputFileList` and `my_buffer`
    /// as it reads.
    pub fn bind_source(&mut self, source: source::DbSource) {
        self.source = Some(source);
    }

    /// C's lexer seat: say where the parser now is, so a diagnostic
    /// raised from here names that line. `yytext` is the token just
    /// consumed, which is what C quotes in ` at or before '%s'`.
    pub fn seek(&mut self, line: u32, yytext: &str) {
        self.park = Some((line, yytext.to_owned()));
    }

    /// Leave the lexer's seat. Every later diagnostic prints without
    /// position rather than with the last one the parser happened to
    /// hold — the port raises some of them long after the text was read.
    pub fn seek_end(&mut self) {
        self.park = None;
    }

    /// C `yyerror(str)` — the arm with a message of its own
    /// (`dbYacc.y:373-374`), which is how EVERY abort-class rejection of
    /// a `.db` reaches the operator: `yyparse` calls it with `"syntax
    /// error"`, `dbLex.l` calls it with `"Invalid character '%c'"`, and
    /// `yyerrorAbort` calls it before setting `yyAbort`. The position
    /// context that follows is the same `yyerror` body the recoverable
    /// arm prints, so an abort names its file, its line and its source
    /// line exactly as a recovered fault does.
    ///
    /// This port raises an abort as a `CaError` returned from the
    /// parser rather than as a call into the lexer, so the position
    /// travels on the error and is unpacked here. `line == 0` is the
    /// include layer's "this failure has no line in any file" — a
    /// circular `include`, a file that could not be read — and prints
    /// the message alone, which is also what C's `yyerror` degrades to
    /// when `dbIncludePrint` has no frame to walk.
    ///
    /// Reporting HERE is what closes the family: every one of the
    /// parser's abort sites returns through the single exit that calls
    /// this, so none of them can be given the context and none
    /// forgotten, and no caller outside this module is left printing a
    /// `.db` rejection as a bare error string.
    pub fn abort(&mut self, err: &CaError) {
        let message = match err {
            CaError::DbParseError {
                line,
                token,
                message,
            } => {
                // `line: 0` is the include layer saying this failure has
                // no line in any file — a circular `include`, a file
                // that could not be read. The message stands alone then,
                // which is also what C's `yyerror` degrades to when
                // `dbIncludePrint` has no frame to walk.
                self.park = (*line > 0).then(|| (*line as u32, token.clone()));
                // The `message` field, NOT the error's `Display`: that
                // spells the line a second time, in this port's own
                // words (`DB parse error at line 3: …`), and the position
                // is what the two lines below say properly.
                message.clone()
            }
            _ => {
                self.park = None;
                err.to_string()
            }
        };
        self.yyerror(Some(&message));
        self.messages.push(message);
    }

    /// C `yyerror(NULL)`: print the diagnostic, remember that the load
    /// failed, and let the caller carry on with the next item.
    ///
    /// The context that follows is `yyerror`'s own body and not an
    /// addition to it: `ERROR: `, then — once per text — ` at or before
    /// '<yytext>'` and `dbIncludePrint`, then always the blank line and
    /// the ` <line> | <source>` echo `"\n %d | %s\n"` produces
    /// (`dbYacc.y:374-381`). Owning it here is what closes the family:
    /// every `.db` diagnostic in this port already reports through this
    /// one function, so none of them can be given the context and none
    /// forgotten.
    pub fn recoverable(&mut self, message: String) {
        self.report(DbDiagnostic::new(message));
    }

    /// [`Self::recoverable`] with the lines C prints AROUND the `ERROR:`
    /// one, in C's own order — see [`DbDiagnostic`].
    pub fn report(&mut self, diagnostic: DbDiagnostic) {
        let DbDiagnostic {
            notice,
            message,
            suggestion,
        } = diagnostic;
        if let Some(notice) = notice {
            eprintln!("{notice}");
        }
        eprintln!("{message}");
        if let Some(suggestion) = suggestion {
            eprintln!("{suggestion}");
        }
        self.yyerror(None);
        self.messages.push(message);
    }

    /// C `yyerror` itself (`dbYacc.y:371-383`), both of its arms.
    ///
    /// `str` is the message it was called WITH, and the arm is the whole
    /// difference in shape: `Some` writes `ERROR: <str>` on a line of
    /// its own and the position below it starts bare, while `None`
    /// writes the bare `ERROR: ` that the position's own first clause
    /// continues — which is where the two spaces in `ERROR:  at or
    /// before` come from. Modelled as C's argument rather than as a flag
    /// of this port's invention, so the two arms cannot drift apart.
    fn yyerror(&mut self, str: Option<&str>) {
        if let Some(str) = str {
            eprintln!("{ERL_ERROR}: {str}");
        }
        // C always has a position here — its lexer is still parked on
        // the line that failed. This port sometimes does not, and then
        // the bare `ERROR: ` is withheld WITH the position it exists to
        // introduce, rather than left dangling at the end of the stream.
        if let Some(context) = self.position_context() {
            if str.is_none() {
                eprint!("{ERL_ERROR}: ");
            }
            eprint!("{context}");
        }
    }

    /// The position half of `yyerror` (`dbYacc.y:377-381`) — everything
    /// after the message slot — or `None` when this port cannot say
    /// where it was.
    fn position_context(&mut self) -> Option<String> {
        let (line, token) = self.park.as_ref()?;
        let line = *line;
        let clause = format!(" at or before '{token}'");
        let (frames, text) = self.source.as_ref()?.at(line)?;
        let mut out = String::new();
        if !self.yy_failed {
            out.push_str(&clause);
            out.push_str(&source::include_print(frames));
            self.yy_failed = true;
        }
        // C's `my_buffer` still carries the newline `fgets` left on it, so
        // this format's own trailing `\n` is what puts a blank line under
        // the echo. A last line with no newline of its own loses it, in C
        // too.
        out.push_str(&format!("\n {line} | {text}\n"));
        Some(out)
    }

    /// Take over the faults a nested parse layer collected — an
    /// `include`, a `.substitutions` row — so the outermost load still
    /// reports them exactly once.
    ///
    /// The nested layer's source binding and `yyFailed` come with them:
    /// C has ONE lexer for the whole `dbReadCOM`, so a text that already
    /// printed ` at or before` must not print it again from an outer
    /// frame, and an outer frame with no text of its own must be able to
    /// locate a diagnostic in the inner one.
    pub fn absorb(&mut self, other: DbFaults) {
        self.messages.extend(other.messages);
        self.yy_failed |= other.yy_failed;
        if self.source.is_none() {
            self.source = other.source;
        }
    }

    /// Whether anything recoverable was reported.
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }

    /// The first diagnostic this load reported, for a test asserting the
    /// wording.
    ///
    /// `cfg(test)` is the whole point, not a convenience. Every one of these
    /// lines is ALREADY on the operator's stream — [`Self::recoverable`] put
    /// it there — so a production caller that could read the text back would
    /// be one `eprintln!` away from saying it twice, which is exactly what
    /// this type used to let `dbLoadRecords` do. Outside the crate's own
    /// tests there is now no way to obtain the string at all, so the second
    /// print is unwritable rather than merely discouraged. What production
    /// is allowed to learn is [`Self::is_empty`]: whether the load failed.
    #[cfg(test)]
    pub fn first_diagnostic(&self) -> Option<String> {
        match self.messages.split_first() {
            None => None,
            Some((first, [])) => Some(first.clone()),
            Some((first, rest)) => Some(format!("{first} (+{} more)", rest.len())),
        }
    }
}

/// A `menu(name) { choice(id, "string") ... }` declaration
/// (`dbYacc.y:82-101` @`R7.0.10`).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdMenu {
    pub name: String,
    /// `(identifier, display string)` in declaration order — C keeps the
    /// order because a menu field's wire value is the INDEX into it.
    pub choices: Vec<(String, String)>,
}

/// One `field(NAME, DBF_TYPE) { item(value) ... }` inside a `recordtype`
/// (`dbYacc.y:120-153`).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdField {
    pub name: String,
    pub dbf_type: String,
    /// Every `item(value)` pair in order. `menu(...)` arrives here under
    /// the literal key `"menu"`, which is what C's own action does
    /// (`dbRecordtypeFieldItem("menu", $3)`, `dbYacc.y:150`).
    pub items: Vec<(String, String)>,
}

/// A `recordtype(name) { ... }` declaration (`dbYacc.y:103-118`).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdRecordType {
    pub name: String,
    pub fields: Vec<DbdField>,
    /// `%`-prefixed C definition lines, each without its `%`
    /// (`dbLex.l:94-97`: the pattern is `%.*`, so a cdef is a whole line).
    pub cdefs: Vec<String>,
}

/// `device(recordType, linkType, dsetName, "choiceString")`
/// (`dbYacc.y:155-163`).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdDevice {
    pub record_type: String,
    pub link_type: String,
    pub dset: String,
    pub choice: String,
}

/// `link(key, lsetName)` (`dbYacc.y:172-178`) — the jlink registration.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdLinkType {
    pub key: String,
    pub lset: String,
}

/// `variable(name)` or `variable(name, type)` (`dbYacc.y:192-202`). C
/// defaults the type to `int` in the one-argument form.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdVariable {
    pub name: String,
    pub dtype: String,
}

/// The `.dbd` declarations a text carries.
///
/// C has ONE grammar: `database_item` (`dbYacc.y:48-63` @`R7.0.10`) lists
/// `record`/`grecord` alongside `menu`, `recordtype`, `device`, `driver`,
/// `link`, `registrar`, `function` and `variable`, and `dbLoadRecords` and
/// `dbLoadDatabase` differ only in which file they hand to the same
/// `yyparse`. A loader that accepts `record` and nothing else therefore
/// rejects text a C IOC loads. Each construct is parsed into its own shape
/// here rather than skipped, so a caller that needs the declaration — the
/// menu choice list behind a `.SCAN` enum, say — has it.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DbdDefs {
    pub menus: Vec<DbdMenu>,
    pub record_types: Vec<DbdRecordType>,
    pub devices: Vec<DbdDevice>,
    pub drivers: Vec<String>,
    pub link_types: Vec<DbdLinkType>,
    pub registrars: Vec<String>,
    pub functions: Vec<String>,
    pub variables: Vec<DbdVariable>,
}

impl DbdDefs {
    /// True when the text declared nothing at all — the ordinary `.db`
    /// case.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.menus.is_empty()
            && self.record_types.is_empty()
            && self.devices.is_empty()
            && self.drivers.is_empty()
            && self.link_types.is_empty()
            && self.registrars.is_empty()
            && self.functions.is_empty()
            && self.variables.is_empty()
    }

    /// The menu declared under `name`, if this text declared it.
    #[must_use]
    pub fn menu(&self, name: &str) -> Option<&DbdMenu> {
        self.menus.iter().find(|m| m.name == name)
    }
}

/// Everything one `.db` text yields.
pub struct ParsedDb {
    pub records: Vec<DbRecordDef>,
    /// `breaktable(...)` definitions found in the text (C `dbBreakBody`,
    /// `dbLexRoutines.c`). The IOC builder feeds these into the
    /// breakpoint-table registry so `ai`/`ao` records with `LINR >= 3`
    /// can resolve their linearisation table.
    pub breaktables: Vec<crate::server::cvt_bpt::BrkTable>,
    /// File-scope `alias("record","new")` directives whose target is not
    /// declared in this text. C `dbAlias` (`dbLexRoutines.c:1508`) looks
    /// the target up in `savedPdbbase` — the whole accumulated database
    /// — so only a caller that owns that database can resolve them, and
    /// the parser must not decide their fate on its own.
    pub unresolved_aliases: Vec<(String, String)>,
    /// Recoverable failures this text produced (C `yyerror(NULL)`). The
    /// records above are everything that parsed in spite of them; the
    /// caller turns a non-empty set into the load's non-zero status.
    pub faults: DbFaults,
    /// The `.dbd` declarations the text carried. Empty for an ordinary
    /// `.db`; C accepts both through the same grammar.
    pub dbd: DbdDefs,
}

impl ParsedDb {
    /// Settle the load's status the way C `dbLoadRecords` does
    /// (`dbAccess.c:795-813`): anything `yyerror(NULL)` recovered from
    /// makes the whole load fail, even though the records that parsed
    /// are still carried here. Only a caller that can report a partial
    /// success — iocsh `dbLoadRecords`, which prints the status and
    /// keeps the shell alive — may read [`ParsedDb::faults`] directly;
    /// every caller that returns a plain `CaResult` goes through here so
    /// a diagnostic can never leave stderr with an `Ok` beside it.
    ///
    /// `source` is what C names in `ERROR: Failed to load '%s'`.
    pub fn load_status(self, source: &str) -> CaResult<Self> {
        if self.faults.is_empty() {
            Ok(self)
        } else {
            Err(CaError::DbLoadFailed(source.to_string()))
        }
    }
}

/// What [`ParsedDb::load_status`] names as the source when the text came
/// from memory rather than a file: C only ever loads a named file, so
/// there is no filename to quote.
pub const DB_STRING_SOURCE: &str = "<db string>";

/// C `dbAlias`'s diagnostic for a target no database knows
/// (`dbLexRoutines.c:1509-1513`). C follows it with `yyerror(NULL)`,
/// which sets `yyFailed` and returns 0: the load's status goes
/// non-zero, the parse continues, and everything already read stays.
pub fn unknown_alias_message(alias: &str, target: &str) -> String {
    format!("{ERL_ERROR}: Alias '{alias}' names an unknown record '{target}'")
}

/// Parse an EPICS .db file with macro substitution.
///
/// The records-only entry point: it has no database to resolve a
/// file-scope alias against, so an unmatched one is reported to stderr
/// and dropped. C keeps the records it already read too — callers that
/// own a database ([`crate::server::ioc_builder`], `dbLoadRecords`)
/// take [`ParsedDb::unresolved_aliases`] instead and resolve them.
pub fn parse_db(input: &str, macros: impl Into<MacroDefs>) -> CaResult<Vec<DbRecordDef>> {
    let parsed = parse_db_with_breaktables(input, macros)?.load_status(DB_STRING_SOURCE)?;
    for (target, alias) in &parsed.unresolved_aliases {
        eprintln!("{}", unknown_alias_message(alias, target));
    }
    Ok(parsed.records)
}

/// C `dbQuietMacroWarnings` (`dbLexRoutines.c:58`), the `int` the `.db`
/// loader hands `macSuppressWarning` (`:273`).
///
/// A process global because C's is one, and because the only thing that
/// writes it is iocsh `var` — a per-load argument would let two loads in
/// one `st.cmd` disagree with the single knob the operator set.
static DB_QUIET_MACRO_WARNINGS: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

/// Read the knob. C default is `0`, i.e. warnings ON.
pub fn db_quiet_macro_warnings() -> bool {
    DB_QUIET_MACRO_WARNINGS.load(std::sync::atomic::Ordering::Relaxed)
}

/// Write the knob — iocsh `var dbQuietMacroWarnings <n>`.
pub fn set_db_quiet_macro_warnings(quiet: bool) {
    DB_QUIET_MACRO_WARNINGS.store(quiet, std::sync::atomic::Ordering::Relaxed);
}

/// C `db_yyinput` (`dbLexRoutines.c:368-410`), as much of it as a port
/// that flattens before it parses can have.
///
/// C reads one `fgets` line, hands it to `macExpandString`, and warns
/// when the expansion reported an error — so both macro diagnostics an
/// operator sees are per LINE and neither is raised by the parser. This
/// is that seat: it expands line by line (quote state cannot leak across
/// a newline), raises the loader's own warning for a line macLib could
/// not fully resolve, and returns the flattened text together with the
/// [`DbSource`] that maps a line of it back to the file and text C would
/// have named.
///
/// The warning fires on C's `entry->error`, not on an undefined name:
/// `macExpandString` returns the same negative length for a recursive
/// reference (`macCore.c:895-904`), and that arm IS reachable from a
/// `.db` load — `dbLoadTemplate` hands a row's `A="$(B)", B="$(A)"`
/// straight to `dbLoadRecords`, where the two raw values walk into each
/// other on the first expansion. Measured against `softIoc R7.0.10`:
/// three lines C wrote that this used to swallow, now
/// [`MacroFault::Recursive`] and the notice in `mac_lib`'s `refer`.
///
/// The earlier reading that no such input existed came from testing
/// through the iocsh command line, where the shell's own expansion
/// consumes `$(…)` before `dbLoadRecords` is called — `A=$(B),B=$(A)`
/// there is two UNDEFINED references, not a cycle.
///
/// The unterminated arm (`macCore.c:862-875`) is a separate `macLib:`
/// line C prints for a `$(`/`${` whose closing delimiter never matched,
/// and it raises this same warning under it — measured on `softIoc
/// R7.0.10`, two lines per bad line, and `var dbQuietMacroWarnings 1`
/// drops the `macLib:` one and keeps this one, exactly as for the other
/// two arms. [`MacroFault::Unterminated`] and the notice in `mac_lib`'s `refer`.
///
/// `filename` is `None` for text that never came from a file — C has no
/// such path (`dbReadDatabase` always names one), so the per-file warning
/// is simply not raised rather than invented with a placeholder name.
pub(crate) fn db_read_lines(
    text: &str,
    macros: impl Into<MacroDefs>,
    filename: Option<&str>,
) -> (String, DbSource) {
    // One table for the file, because C holds one `MAC_HANDLE` for the
    // whole `.db` (`dbReadCOM`, `dbLexRoutines.c:256-300`) and reads
    // `dbQuietMacroWarnings` into it once, at open. So a macro whose own
    // value is faulty is expanded — and complained about — once here,
    // not once per line that mentions it.
    let mut table = MacroTable::new(
        macros,
        MacroExpandOptions {
            suppress_warnings: db_quiet_macro_warnings(),
            ..MacroExpandOptions::default()
        },
    );
    let mut out = String::with_capacity(text.len());
    let mut lines = Vec::new();
    let mut frames = Vec::new();
    for (i, raw) in text.split_inclusive('\n').enumerate() {
        let line_num = i as u32 + 1;
        let expanded = db_expand_line_in(&mut table, raw, filename, line_num);
        out.push_str(&expanded);
        lines.push(expanded);
        frames.push(std::sync::Arc::from(vec![DbIncludeFrame {
            path: None,
            filename: filename.map(str::to_string),
            line: line_num,
        }]));
    }
    (out, DbSource::new(lines, frames))
}

/// One line through C's `macExpandString` call and the warning that
/// follows it (`dbLexRoutines.c:378-387`) — the single seat for both
/// macro diagnostics a `.db` load produces.
///
/// `raw` must carry its own trailing newline exactly as `fgets` leaves
/// it, because macLib quotes the string it was given: C's console shows
/// the closing `)` of `(expanding string …)` on the NEXT line for that
/// reason, and a caller that trimmed first would print a different shape.
///
/// The table is always the caller's, and there is no variant that builds
/// one per line: C has exactly one `MAC_HANDLE` for a whole `dbReadCOM`
/// (`dbLexRoutines.c:256-300`) and msi one for a whole run
/// (`msi.cpp:111`), so a per-line table can only ever announce a faulty
/// definition once per line where C announces it once per file.
pub(crate) fn db_expand_line_in(
    table: &mut MacroTable,
    raw: &str,
    filename: Option<&str>,
    line_num: u32,
) -> String {
    let expansion = table.expand(raw);
    // C `entry->error`, not "an undefined name": `macExpandString`
    // returns the same negative length for a recursive reference, and
    // this warning is what tells the operator the line went out wrong.
    if expansion.errored()
        && let Some(name) = filename
    {
        // C `db_yyinput` (`dbLexRoutines.c:384-387`), a direct `fprintf`
        // — so its magenta reaches a pipe as well as a terminal, unlike
        // the `macLib:` line above it, which errlog strips. C passes
        // `line_num+1` because its own counter is bumped after the read;
        // the count here is the reader's own and already 1-based.
        //
        // NOT gated on `dbQuietMacroWarnings`: `trans` sets
        // `entry->error` before it consults `FLAG_SUPPRESS_WARNINGS`
        // (`macCore.c:912-919`), so `macExpandString` still returns a
        // negative length and this line still prints. Measured on
        // `compressTest.db` with `var dbQuietMacroWarnings 1`: C drops
        // the four `macLib:` notices and keeps all four of these.
        eprintln!(
            "{}: '{name}' line {line_num} has undefined macros",
            crate::runtime::log::ERL_WARNING
        );
    }
    expansion.text
}

/// Like [`parse_db`] but returns the whole [`ParsedDb`].
pub fn parse_db_with_breaktables(input: &str, macros: impl Into<MacroDefs>) -> CaResult<ParsedDb> {
    // Per LINE, not per file: C's loader expands each `fgets` line
    // separately (`dbLexRoutines.c:375-391`), so quote state cannot leak
    // across lines — see [`db_read_lines`].
    let (expanded, source) = db_read_lines(input, macros, None);
    parse_db_expanded(&expanded, source)
}

/// The parser proper: text that has ALREADY been through
/// [`db_read_lines`], plus the map that locates a line in it.
///
/// Split out so a `.db` text is macro-expanded exactly ONCE per load.
/// The file path expanded twice — once in `expand_includes`, which must,
/// because it has to resolve an `include` filename before it can open it,
/// and again here — which was invisible while the expander said nothing
/// and would have printed every `macLib:` line twice the moment it did.
pub(crate) fn parse_db_expanded(expanded: &str, source: DbSource) -> CaResult<ParsedDb> {
    // The ONE place a `.db` abort meets the text it came from. C reaches
    // the same point through `yyerror(str)` from inside the lexer; this
    // parser returns instead, so the return is where the report has to
    // happen — and it is a single return, so every abort site the
    // grammar has gets the position at once. See [`DbFaults::abort`].
    let mut faults = DbFaults::default();
    faults.bind_source(source);
    match parse_db_items(expanded, &mut faults) {
        Ok(items) => Ok(ParsedDb {
            records: items.records,
            breaktables: items.breaktables,
            unresolved_aliases: items.unresolved_aliases,
            faults,
            dbd: items.dbd,
        }),
        Err(e) => {
            faults.abort(&e);
            // C follows the abort with `WARNING: dbReadCOM: Parser stack
            // dirty w/o error. <n>` when its `tempList` still holds the
            // half-built objects `yyparse` abandoned
            // (`dbLexRoutines.c:303-305`). Deliberately absent: it counts
            // C's own allocations, this parser builds its records on the
            // stack and has no such list, and printing it would state a
            // fact about a structure that does not exist here.
            Err(e)
        }
    }
}

/// What one text yields before its faults are attached.
///
/// Separate from [`ParsedDb`] because the reporter cannot be a field of
/// the result while the parse is still running: it is the parse's own
/// output channel, and an abort has to report through it AFTER the body
/// has given up on producing a result at all.
struct DbItems {
    records: Vec<DbRecordDef>,
    breaktables: Vec<crate::server::cvt_bpt::BrkTable>,
    unresolved_aliases: Vec<(String, String)>,
    dbd: DbdDefs,
}

/// The grammar itself.
///
/// `dbYacc.y` declares no `error` productions, so a genuine syntax error
/// ends `yyparse` and the whole text is lost in C too: every `?` below
/// is abort-class by C's own choice, and all of them leave through
/// [`parse_db_expanded`], which reports them. Only the semantic actions
/// C guards with `yyerror(NULL)` are recoverable, and those live in the
/// layers that own a database — see [`DbFaults`].
fn parse_db_items(expanded: &str, faults: &mut DbFaults) -> CaResult<DbItems> {
    let mut records = Vec::new();
    let mut breaktables: Vec<crate::server::cvt_bpt::BrkTable> = Vec::new();
    // `.dbd` declarations: C parses these through the same `yyparse` as
    // `record`, so they are accepted here rather than rejected.
    let mut dbd = DbdDefs::default();
    // Standalone `alias("record","newname")` directives (dbYacc.y:275).
    // Resolved against the record list after the full file is parsed
    // so the alias target may appear before or after the directive.
    let mut global_aliases: Vec<(String, String)> = Vec::new();
    let chars: Vec<char> = expanded.chars().collect();
    let mut pos = 0;
    let mut line = 1;
    let mut col = 1;

    while pos < chars.len() {
        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        if pos >= chars.len() {
            break;
        }

        // Top-level keyword. C dbStatic (`dbYacc.y:48-62`) accepts
        // `record`/`grecord` plus the directives below at file scope.
        let word = read_word(&chars, &mut pos, &mut col);
        if word.is_empty() {
            pos += 1;
            col += 1;
            continue;
        }

        // `path "dir"` / `addpath "dir"` — search-path directives
        // (dbYacc.y:71-81). Include resolution is handled by the
        // file-expansion layer (`expand_includes`); by the time raw
        // text reaches `parse_db` the path is already fixed, so these
        // are accepted and skipped rather than erroring out.
        if word == "path" || word == "addpath" {
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            let _dir = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
            continue;
        }

        // `include "file"` (dbYacc.y:65-69). Includes are normally
        // inlined by `expand_includes` before `parse_db` runs; a bare
        // `include` reaching here means the caller parsed un-expanded
        // text. Accept the directive so the grammar matches C, but the
        // file is NOT loaded at this layer — that is the expansion
        // layer's job.
        if word == "include" {
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            let _file = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
            continue;
        }

        // Standalone 2-arg `alias("record","newname")` (dbYacc.y:275).
        // Distinct from the in-record-body `alias("name")` form. The
        // new name is attached to the named record's alias list once
        // all records are parsed (the target may appear later in the
        // file).
        if word == "alias" {
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, '(', line)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            let target = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, ',', line)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            let alias_name = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, ')', line)?;
            validate_record_name(&alias_name, line, col)?;
            global_aliases.push((target, alias_name));
            continue;
        }

        // `breaktable(name) { raw eng  raw eng  ... }` (dbYacc.y / C
        // `dbBreakBody`, dbLexRoutines.c:1003-1080). The body is a flat list
        // of numbers in `(raw, eng)` pairs, whitespace- or comma-separated;
        // `BrkTable::build` computes the interval slopes and validates the
        // table (>= 2 points, monotonic, no zero slope).
        if word == "breaktable" {
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, '(', line)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            let bt_name = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, ')', line)?;
            skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
            expect_char(&chars, &mut pos, &mut col, '{', line)?;

            // Read numeric tokens until the closing brace. Numbers are
            // separated by whitespace and/or commas (C `tokenVALUE` lexing).
            let is_number_char =
                |c: char| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '+' | ':' | '.');
            let mut nums: Vec<f64> = Vec::new();
            loop {
                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                if pos >= chars.len() {
                    return Err(CaError::DbParseError {
                        line,
                        token: String::new(),
                        message: "unexpected end of file in breaktable body".into(),
                    });
                }
                if chars[pos] == '}' {
                    pos += 1;
                    col += 1;
                    break;
                }
                if chars[pos] == ',' {
                    pos += 1;
                    col += 1;
                    continue;
                }
                let mut tok = String::new();
                while pos < chars.len() && is_number_char(chars[pos]) {
                    tok.push(chars[pos]);
                    pos += 1;
                    col += 1;
                }
                if tok.is_empty() {
                    return Err(CaError::DbParseError {
                        line,
                        token: yytext_at(&chars, pos),
                        message: format!(
                            "breaktable {bt_name}: expected a number, got '{}'",
                            chars[pos]
                        ),
                    });
                }
                let num: f64 = tok.parse().map_err(|_| CaError::DbParseError {
                    line,
                    token: tok.clone(),
                    message: format!("breaktable {bt_name}: non-numeric value '{tok}'"),
                })?;
                nums.push(num);
            }

            // C `dbBreakBody`: an odd count is a missing raw/eng partner.
            if nums.len() % 2 != 0 {
                return Err(CaError::DbParseError {
                    line,
                    token: String::from("}"),
                    message: format!("breaktable {bt_name}: Raw value missing"),
                });
            }
            let pairs: Vec<(f64, f64)> = nums.chunks_exact(2).map(|c| (c[0], c[1])).collect();
            let table = crate::server::cvt_bpt::BrkTable::build(bt_name, &pairs).map_err(|e| {
                CaError::DbParseError {
                    line,
                    token: String::from("}"),
                    message: e,
                }
            })?;
            breaktables.push(table);
            continue;
        }

        // The `.dbd` half of C's one grammar (`dbYacc.y:51-59, 155-202`).
        // `menu` and `recordtype` carry a brace body; the rest are a
        // parenthesised argument list.
        if word == "menu" {
            dbd.menus
                .push(parse_menu(&chars, &mut pos, &mut line, &mut col)?);
            continue;
        }
        if word == "recordtype" {
            dbd.record_types
                .push(parse_recordtype(&chars, &mut pos, &mut line, &mut col)?);
            continue;
        }
        if word == "device" {
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 4, 4)?;
            dbd.devices.push(DbdDevice {
                record_type: a[0].clone(),
                link_type: a[1].clone(),
                dset: a[2].clone(),
                choice: a[3].clone(),
            });
            continue;
        }
        if word == "driver" {
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 1, 1)?;
            dbd.drivers.push(a[0].clone());
            continue;
        }
        if word == "link" {
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 2, 2)?;
            dbd.link_types.push(DbdLinkType {
                key: a[0].clone(),
                lset: a[1].clone(),
            });
            continue;
        }
        if word == "registrar" {
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 1, 1)?;
            dbd.registrars.push(a[0].clone());
            continue;
        }
        if word == "function" {
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 1, 1)?;
            dbd.functions.push(a[0].clone());
            continue;
        }
        if word == "variable" {
            // C `dbYacc.y:192-202`: the one-argument form is
            // `dbVariable($3, "int")`.
            let a = parse_arg_list(&chars, &mut pos, &mut line, &mut col, 1, 2)?;
            dbd.variables.push(DbdVariable {
                name: a[0].clone(),
                dtype: a.get(1).cloned().unwrap_or_else(|| "int".to_string()),
            });
            continue;
        }

        if word != "record" && word != "grecord" {
            return Err(CaError::DbParseError {
                line,
                token: word.clone(),
                message: SYNTAX_ERROR.into(),
            });
        }

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        expect_char(&chars, &mut pos, &mut col, '(', line)?;

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        let rec_type = read_token_string(&chars, &mut pos, &mut line, &mut col)?;

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        expect_char(&chars, &mut pos, &mut col, ',', line)?;

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        let name = read_token_string(&chars, &mut pos, &mut line, &mut col)?;

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        expect_char(&chars, &mut pos, &mut col, ')', line)?;
        // Where C's lexer is parked when `dbRecordHead` reduces, which is the
        // line every refusal of the head itself is reported on.
        let head_line = line as u32;
        // C runs `dbRecordNameValidate` from the `record_head` ACTION, which
        // bison reduces only after the `)` (`dbYacc.y:227-235`), so a third
        // argument is a grammar rejection at the comma and never reaches the
        // name check: `record(ai, "", extra)` prints ` at or before ','` on
        // `softIoc` @`R7.0.10`, while `record(ai, "")` prints the empty-name
        // refusal located at `)`. Validating before the paren gave both of
        // them the name refusal.
        validate_record_name(&name, line, col)?;

        skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
        let mut fields = Vec::new();
        let mut aliases: Vec<String> = Vec::new();
        let mut info_tags: Vec<(String, String)> = Vec::new();
        // C `dbYacc.y:237-241`: `record_body: /* empty */` is the FIRST
        // alternative, ahead of `'{' '}'` and `'{' record_field_list '}'`,
        // and `tokenRECORD` and `tokenGRECORD` share it — so
        // `record(ai,"TEMP")` with no braces is an all-defaults record,
        // not a syntax error. `record`/`grecord` reach this through the
        // same path above, so both forms are covered here.
        if pos < chars.len() && chars[pos] == '{' {
            pos += 1;
            col += 1;
            loop {
                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                if pos >= chars.len() {
                    return Err(CaError::DbParseError {
                        line,
                        token: String::new(),
                        message: "unexpected end of file in record body".into(),
                    });
                }
                if chars[pos] == '}' {
                    pos += 1;
                    col += 1;
                    break;
                }

                let kw = read_word(&chars, &mut pos, &mut col);
                // `record_field: ... | include` (`dbYacc.y:271`). As at
                // file scope, the expansion layer owns the file; the
                // directive is accepted so the grammar matches C.
                if kw == "include" {
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    let _file = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
                    continue;
                }
                if kw != "field" && kw != "info" && kw != "alias" {
                    return Err(CaError::DbParseError {
                        line,
                        token: kw.clone(),
                        message: SYNTAX_ERROR.into(),
                    });
                }

                if kw == "alias" {
                    // alias("name") — capture and validate per PR #336.
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    expect_char(&chars, &mut pos, &mut col, '(', line)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    let alias_name = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    expect_char(&chars, &mut pos, &mut col, ')', line)?;
                    validate_record_name(&alias_name, line, col)?;
                    aliases.push(alias_name);
                    continue;
                }
                if kw == "info" {
                    // info(tag, value) — capture for downstream consumers
                    // (asyn:READBACK, Q:form, etc.). PR #60 / #208 needs
                    // the asyn:READBACK tag in particular.
                    //
                    // Both `tag` and `value` accept quoted *or* unquoted
                    // tokens. Base's dbStaticLib parser tolerates either
                    // form and ad-core templates rely on the unquoted
                    // shape (`info(asyn:READBACK, "1")`).
                    //
                    // C `info(tokenSTRING, json_value)` (dbYacc.y:262-267): the tag
                    // is a `tokenSTRING` — the same raw, untranslated token a record
                    // NAME is — and only the value is a `jsonSTRING` that
                    // `dbRecordInfo` runs `dbTranslateEscape` over
                    // (dbLexRoutines.c:1435-1440). Hence the two different readers.
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    expect_char(&chars, &mut pos, &mut col, '(', line)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    let tag = read_token_string(&chars, &mut pos, &mut line, &mut col)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    expect_char(&chars, &mut pos, &mut col, ',', line)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    let value = read_field_value(&chars, &mut pos, &mut line, &mut col)?;
                    skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                    expect_char(&chars, &mut pos, &mut col, ')', line)?;
                    info_tags.push((tag, value.as_str_lossy().into_owned()));
                    continue;
                }

                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                expect_char(&chars, &mut pos, &mut col, '(', line)?;

                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                let field_name = read_token_string(&chars, &mut pos, &mut line, &mut col)?;

                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                expect_char(&chars, &mut pos, &mut col, ',', line)?;

                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                let field_value = read_field_value(&chars, &mut pos, &mut line, &mut col)?;

                skip_whitespace_and_comments(&chars, &mut pos, &mut line, &mut col);
                expect_char(&chars, &mut pos, &mut col, ')', line)?;
                // C reduces `dbRecordField` with the closing paren as the
                // last token the lexer matched, which is why every field
                // diagnostic in C's console quotes ` at or before ')'`.
                let field_line = line as u32;
                faults.seek(field_line, ")");

                // C `dbRecordField` (`dbLexRoutines.c:1231-1416` @`R7.0.10`)
                // asks the record type what it declares BEFORE it stores
                // anything, and both of its name-side refusals end in
                // `yyerror(NULL)`: the field is dropped, the record and its
                // other fields survive, and the load's status goes non-zero.
                // The port had neither refusal, so `field(OUT,...)` on a calc
                // record loaded and `dbgf C:TWO.OUT` answered the raw text —
                // an invalid database that reads back plausibly, which is
                // worse than the parse error it replaced. This is the one
                // place the check can live: `apply_fields` resolves every
                // record type but never learns the record's NAME, and the
                // message is built from it.
                match record_type_field_exists(&rec_type, &field_name) {
                    Some(false) => {
                        // C follows the refusal with a spelling suggestion when
                        // it finds one, and prints NOTHING when it does not
                        // (`dbLexRoutines.c:1241-1385`). The silence is the half
                        // that has to be reproduced exactly: a suggestion the
                        // operator did not mean is worse than none.
                        //
                        // C reads the value BEFORE `dbTranslateEscape`, so its
                        // type test sees `\t` as two characters where this sees
                        // the tab. Nothing in the map or in the numeric and
                        // choice tests can be spelled with an escape, so the
                        // two agree on every value that changes a suggestion.
                        faults.report(DbDiagnostic::suggesting(
                            format!(
                                "{ERL_ERROR}: {rec_type} record '{name}' doesn't have a field \
                                 '{field_name}'"
                            ),
                            suggest_field(&rec_type, &field_name, &field_value.as_str_lossy())
                                .map(suggestion_line),
                        ));
                        continue;
                    }
                    // `indfield == 0` is `NAME`, dbCommon's first field
                    // (`dbLexRoutines.c:1390-1396`). `dbFindField` finds it,
                    // so this is a second, separately-worded refusal rather
                    // than a missing declaration.
                    Some(true) if field_name == "NAME" => {
                        faults.recoverable(format!(
                            "{ERL_ERROR}: Can't set 'NAME' field of record '{name}'"
                        ));
                        continue;
                    }
                    _ => {}
                }
                fields.push(DbFieldDef {
                    name: field_name,
                    value: field_value,
                    line: field_line,
                });
            }
        }

        records.push(DbRecordDef {
            record_type: rec_type,
            name,
            fields,
            aliases,
            info_tags,
            line: head_line,
        });
    }

    // Attach standalone `alias("record","newname")` directives to the
    // matching record (C `dbAlias`). A target this text does not
    // declare is NOT an error here: C resolves it against
    // `savedPdbbase`, so an earlier `dbLoadRecords` may own it. Hand it
    // to the caller, which is the only party that can see the database.
    let mut unresolved_aliases = Vec::new();
    for (target, alias_name) in global_aliases {
        match records.iter_mut().find(|r| r.name == target) {
            Some(rec) => rec.aliases.push(alias_name),
            None => unresolved_aliases.push((target, alias_name)),
        }
    }

    // C's lexer is done with this text; a diagnostic raised after this
    // point (the install loop, an alias resolved against the database)
    // must seek for itself rather than inherit wherever the parse
    // stopped. See [`DbFaults::seek_end`].
    faults.seek_end();

    Ok(DbItems {
        records,
        breaktables,
        unresolved_aliases,
        dbd,
    })
}

// libCom macLib lives in `epics-libcom-rs`; these are its names at the paths
// this crate has always offered them under.
pub use crate::runtime::mac_lib::{
    MacroDefs, MacroExpandOptions, MacroExpansion, MacroFault, MacroTable, expand_macros,
    substitute_macros, substitute_macros_per_line,
};

/// Read `( a , b , ... )` and return the strings. `min`/`max` bound the
/// count the way C's grammar does by having one production per arity.
///
/// The bound is checked WHERE C's grammar checks it, not after the list is
/// built: an argument too many is rejected at the comma that would open it —
/// `record(ai, "", extra)` prints ` at or before ','` on `softIoc` @`R7.0.10`,
/// with the parser never reaching the `)` — and an argument too few at the `)`
/// that ended the list, which is where `alias("C8")` reports.
fn parse_arg_list(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
    min: usize,
    max: usize,
) -> CaResult<Vec<String>> {
    skip_whitespace_and_comments(chars, pos, line, col);
    let open_line = *line;
    expect_char(chars, pos, col, '(', open_line)?;
    let mut args = Vec::new();
    loop {
        skip_whitespace_and_comments(chars, pos, line, col);
        args.push(read_token_string(chars, pos, line, col)?);
        skip_whitespace_and_comments(chars, pos, line, col);
        match chars.get(*pos) {
            Some(',') => {
                if args.len() >= max {
                    return Err(CaError::DbParseError {
                        line: *line,
                        token: String::from(","),
                        message: SYNTAX_ERROR.into(),
                    });
                }
                *pos += 1;
                *col += 1;
            }
            _ => break,
        }
    }
    skip_whitespace_and_comments(chars, pos, line, col);
    expect_char(chars, pos, col, ')', *line)?;
    if args.len() < min {
        return Err(CaError::DbParseError {
            line: open_line,
            token: String::from(")"),
            message: SYNTAX_ERROR.into(),
        });
    }
    Ok(args)
}

/// `menu(name) { choice(id, "string") ... }` (`dbYacc.y:82-101`). The body
/// also admits `include` (`:101`).
fn parse_menu(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<DbdMenu> {
    let name = parse_arg_list(chars, pos, line, col, 1, 1)?.remove(0);
    skip_whitespace_and_comments(chars, pos, line, col);
    expect_char(chars, pos, col, '{', *line)?;
    let mut choices = Vec::new();
    loop {
        skip_whitespace_and_comments(chars, pos, line, col);
        match chars.get(*pos) {
            None => {
                return Err(CaError::DbParseError {
                    line: *line,
                    token: String::new(),
                    message: format!("unexpected end of file in menu({name})"),
                });
            }
            Some('}') => {
                *pos += 1;
                *col += 1;
                break;
            }
            _ => {}
        }
        let kw = read_word(chars, pos, col);
        match kw.as_str() {
            "choice" => {
                let mut a = parse_arg_list(chars, pos, line, col, 2, 2)?;
                let value = a.remove(1);
                choices.push((a.remove(0), value));
            }
            "include" => {
                skip_whitespace_and_comments(chars, pos, line, col);
                let _file = read_token_string(chars, pos, line, col)?;
            }
            _ => {
                return Err(CaError::DbParseError {
                    line: *line,
                    token: kw.clone(),
                    message: SYNTAX_ERROR.into(),
                });
            }
        }
    }
    // `menuScan` is not an ordinary menu: it is the SCAN field's domain AND
    // the periodic scan rates, and C reads it out of `pdbbase` at `iocInit`
    // (`initPeriodic`, `dbScan.c:858`). Installing it here is C's own timing —
    // the yacc action fills `pdbbase` during the parse — and it is the single
    // place a site menu can enter the record system from, so both load paths
    // (`IocBuilder` and iocsh `dbLoadRecords`) get it without either knowing.
    if name == "menuScan" {
        let values: Vec<String> = choices.iter().map(|(_, value)| value.clone()).collect();
        crate::server::record::menu_scan::install(&values).map_err(|e| CaError::DbParseError {
            line: *line,
            token: String::from("}"),
            message: format!("menu({name}): {e}"),
        })?;
    }
    Ok(DbdMenu { name, choices })
}

/// `recordtype(name) { field(NAME, TYPE) { item(v) ... } | %cdef |
/// include ... }` (`dbYacc.y:103-153`). The empty body `{}` is its own C
/// production (`dbRecordtypeEmpty`) and is accepted here as an empty
/// field list.
fn parse_recordtype(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<DbdRecordType> {
    let name = parse_arg_list(chars, pos, line, col, 1, 1)?.remove(0);
    skip_whitespace_and_comments(chars, pos, line, col);
    expect_char(chars, pos, col, '{', *line)?;
    let mut fields = Vec::new();
    let mut cdefs = Vec::new();
    loop {
        skip_whitespace_and_comments(chars, pos, line, col);
        match chars.get(*pos) {
            None => {
                return Err(CaError::DbParseError {
                    line: *line,
                    token: String::new(),
                    message: format!("unexpected end of file in recordtype({name})"),
                });
            }
            Some('}') => {
                *pos += 1;
                *col += 1;
                break;
            }
            // `%.*` (`dbLex.l:94-97`): a C definition is the rest of the
            // line, minus the `%`. It is NOT a comment, so it cannot go
            // through `skip_whitespace_and_comments`.
            Some('%') => {
                *pos += 1;
                *col += 1;
                let mut text = String::new();
                while let Some(&c) = chars.get(*pos) {
                    if c == '\n' {
                        break;
                    }
                    text.push(c);
                    *pos += 1;
                    *col += 1;
                }
                cdefs.push(text);
                continue;
            }
            _ => {}
        }
        let kw = read_word(chars, pos, col);
        match kw.as_str() {
            "field" => fields.push(parse_recordtype_field(chars, pos, line, col, &name)?),
            "include" => {
                skip_whitespace_and_comments(chars, pos, line, col);
                let _file = read_token_string(chars, pos, line, col)?;
            }
            _ => {
                return Err(CaError::DbParseError {
                    line: *line,
                    token: kw.clone(),
                    message: SYNTAX_ERROR.into(),
                });
            }
        }
    }
    Ok(DbdRecordType {
        name,
        fields,
        cdefs,
    })
}

/// `field(NAME, DBF_TYPE) { item(value) ... }` (`dbYacc.y:132-153`).
fn parse_recordtype_field(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
    rtype: &str,
) -> CaResult<DbdField> {
    let mut head = parse_arg_list(chars, pos, line, col, 2, 2)?;
    let dbf_type = head.remove(1);
    let name = head.remove(0);
    skip_whitespace_and_comments(chars, pos, line, col);
    expect_char(chars, pos, col, '{', *line)?;
    let mut items = Vec::new();
    loop {
        skip_whitespace_and_comments(chars, pos, line, col);
        match chars.get(*pos) {
            None => {
                return Err(CaError::DbParseError {
                    line: *line,
                    token: String::new(),
                    message: format!("unexpected end of file in {rtype}.{name}"),
                });
            }
            Some('}') => {
                *pos += 1;
                *col += 1;
                break;
            }
            _ => {}
        }
        // C has two productions here and the second exists only because
        // `menu` lexes as its own token: it stores the item under the
        // literal key "menu" (`dbYacc.y:144-152`), so both arms land in
        // the same list.
        let key = read_word(chars, pos, col);
        if key.is_empty() {
            return Err(CaError::DbParseError {
                line: *line,
                token: yytext_at(chars, *pos),
                message: SYNTAX_ERROR.into(),
            });
        }
        let value = parse_arg_list(chars, pos, line, col, 1, 1)?.remove(0);
        items.push((key, value));
    }
    Ok(DbdField {
        name,
        dbf_type,
        items,
    })
}

fn skip_whitespace_and_comments(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) {
    while *pos < chars.len() {
        match chars[*pos] {
            ' ' | '\t' | '\r' => {
                *pos += 1;
                *col += 1;
            }
            '\n' => {
                *pos += 1;
                *line += 1;
                *col = 1;
            }
            '#' => {
                // Line comment
                while *pos < chars.len() && chars[*pos] != '\n' {
                    *pos += 1;
                }
            }
            _ => break,
        }
    }
}

fn read_word(chars: &[char], pos: &mut usize, col: &mut usize) -> String {
    let mut word = String::new();
    while *pos < chars.len() && (chars[*pos].is_ascii_alphanumeric() || chars[*pos] == '_') {
        word.push(chars[*pos]);
        *pos += 1;
        *col += 1;
    }
    word
}

fn read_quoted_string(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<String> {
    if *pos >= chars.len() || chars[*pos] != '"' {
        return Err(CaError::DbParseError {
            line: *line,
            token: yytext_at(chars, *pos),
            message: SYNTAX_ERROR.into(),
        });
    }
    *pos += 1;
    *col += 1;

    // C dbStatic lexer parity (dbLex.l:88-92): a quoted `tokenSTRING` matches
    // `{doublequote}({dqschar}|{escape})*{doublequote}` where `escape =
    // {backslash}.`. The lexer keeps the **raw bytes** of the string body —
    // `dbmfStrdup(yytext+1)` then NUL-terminates one byte early, so only the
    // surrounding quotes are stripped; escape sequences are NOT translated.
    //
    // This rule reads NAMES ONLY (record/alias/breaktable names, `path` and
    // `include` arguments, the `info` tag) — every `tokenSTRING` in the C
    // grammar. Field and info VALUES are a different token from a different
    // start condition (`jsonSTRING`, dbLex.l:111-114) and DO get translated;
    // they go through [`read_json_string`]. Do not merge the two: unescaping
    // here would corrupt every record name (R18-91).
    //
    // The escape sequence still consumes its following char for delimiter
    // purposes — `\"` does not terminate the string — but both bytes are
    // emitted verbatim.
    let mut s = String::new();
    while *pos < chars.len() && chars[*pos] != '"' {
        if chars[*pos] == '\\' && *pos + 1 < chars.len() && chars[*pos + 1] != '\n' {
            // Emit both the backslash and the escaped char raw.
            //
            // The `!= '\n'` guard is required: C `{escape}` is
            // `{backslash}.` and flex `.` never matches a newline
            // (`{dqschar}` is `[^"\n\\]`), so a backslash immediately
            // before a newline is NOT an escape. Skipping this branch
            // lets the newline fall through to the `Newline in string`
            // error below (dbLex.l:131-133).
            s.push('\\');
            s.push(chars[*pos + 1]);
            *pos += 2;
            *col += 2;
        } else if chars[*pos] == '\n' {
            // dbLex.l:131-133: a newline inside an unterminated quoted
            // string is a hard parse error (`yyerrorAbort("Newline in
            // string, closing quote missing")`), not a literal char.
            return Err(CaError::DbParseError {
                line: *line,
                token: String::from("\""),
                message: "Newline in string, closing quote missing".into(),
            });
        } else {
            s.push(chars[*pos]);
            *pos += 1;
            *col += 1;
        }
    }

    if *pos >= chars.len() {
        return Err(CaError::DbParseError {
            line: *line,
            token: String::new(),
            message: "unterminated string".into(),
        });
    }
    *pos += 1; // skip closing "
    *col += 1;
    Ok(s)
}

/// A quoted field/info VALUE — C's `jsonSTRING` (dbLex.l:111-114, matched in
/// the `JSON` start condition the `field(`/`info(` rules switch into,
/// dbYacc.y:256-267), followed by the translation `dbLexRoutines.c:1398-1403`
/// and `:1435-1440` run on it.
///
/// C keeps the quotes on a `jsonSTRING` — they are precisely the marker that
/// the translation is still owed — and `dbGetFieldValue`/`dbRecordInfo` then
/// strip them and call `dbTranslateEscape(value, value)` before `dbPutString`.
/// So `field(DESC,"hex\x41end")` stores `hexAend`, and a link field's
/// `"@drvUser(\"chan1\")"` reaches device support with real quotes in it, not
/// backslashes. This is the half [`read_quoted_string`] (names, raw) must NOT
/// do, and the whole of R18-91.
///
/// The accepted grammar is C's (dbLex.l:23-33), which is STRICTER than a raw
/// read: a literal control character and the escapes `\1`..`\9` are syntax
/// errors, `\x` demands exactly two hex digits and `\u` exactly four. Single
/// quotes delimit a value string as well as double (`jsonsqstr`), and the other
/// quote character is then an ordinary character. C accepts `\uHHHH` in the
/// lexer but implements no `\u` in the translation, so it comes out as the
/// literal text `uHHHH` — reproduced, because a `.db` in the field may rely on
/// what C actually stores.
fn read_json_string(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<PvString> {
    let quote = match chars.get(*pos) {
        Some(&c @ ('"' | '\'')) => c,
        _ => {
            return Err(CaError::DbParseError {
                line: *line,
                token: yytext_at(chars, *pos),
                message: SYNTAX_ERROR.into(),
            });
        }
    };
    // Where C's `yytext` for this token begins. Every abort below is raised
    // from inside the one `jsonstr` rule (`dbLex.l:32`), so they all quote the
    // same run of text and the locator is decided once.
    let quote_start = *pos;
    *pos += 1;
    *col += 1;

    let err = |line: usize, message: &str| CaError::DbParseError {
        line,
        token: yytext_at(chars, quote_start),
        message: message.into(),
    };

    // The escaped source text, quotes stripped — C's `yytext`, minus the two
    // quote characters `dbGetFieldValue` steps over.
    let mut escaped = String::new();
    loop {
        let Some(&c) = chars.get(*pos) else {
            return Err(err(*line, "unterminated string"));
        };
        if c == quote {
            *pos += 1;
            *col += 1;
            break;
        }
        if c == '\n' {
            // dbLex.l:131-133 — the JSON string rule cannot match a newline
            // (`normalchar` excludes every control character), and the
            // catch-all reports the missing quote.
            return Err(err(*line, "Newline in string, closing quote missing"));
        }
        if c == '\\' {
            let Some(&esc) = chars.get(*pos + 1) else {
                return Err(err(*line, "unterminated string"));
            };
            // `escapedchar` is `{backslash}[^ux1-9]`; `x` and `u` introduce the
            // fixed-width `latinchar`/`unicodechar` forms, and `\1`..`\9` are
            // not escapes at all (C has no octal here).
            let hex = |n: usize| {
                chars
                    .get(*pos + 2..*pos + 2 + n)
                    .is_some_and(|ds| ds.iter().all(|d| d.is_ascii_hexdigit()))
            };
            let width = match esc {
                'x' if hex(2) => 4,
                'u' if hex(4) => 6,
                'x' | 'u' | '1'..='9' => {
                    return Err(err(
                        *line,
                        "invalid escape sequence (\\x needs 2 hex digits, \
                         \\u needs 4, and \\1..\\9 are not escapes)",
                    ));
                }
                _ => 2,
            };
            // `\<newline>` is a legal escape here (the class `[^ux1-9]` matches
            // a newline where the name rule's `.` does not), so the line
            // counter has to follow it.
            let consumed = &chars[*pos..*pos + width];
            escaped.extend(consumed);
            *pos += width;
            if consumed.contains(&'\n') {
                *line += 1;
                *col = 0;
            } else {
                *col += width;
            }
            continue;
        }
        if (c as u32) < 0x20 {
            return Err(err(
                *line,
                "a control character must be escaped inside a quoted value",
            ));
        }
        escaped.push(c);
        *pos += 1;
        *col += 1;
    }

    Ok(PvString::from_bytes(
        crate::runtime::epics_string::raw_from_escaped(&escaped),
    ))
}

/// A brace-delimited JSON field value, returned verbatim (braces included).
/// Braces inside a JSON string do not nest the value, and a `\` escapes the
/// next character inside a string.
fn read_json_value(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<String> {
    let start_line = *line;
    let close = if chars.get(*pos) == Some(&'[') {
        ']'
    } else {
        '}'
    };
    let mut s = String::new();
    let mut depth = 0usize;
    let mut in_string = false;
    let mut escaped = false;

    while *pos < chars.len() {
        let c = chars[*pos];
        s.push(c);
        *pos += 1;
        if c == '\n' {
            *line += 1;
            *col = 0;
        } else {
            *col += 1;
        }

        if in_string {
            if escaped {
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if c == '"' {
                in_string = false;
            }
            continue;
        }
        match c {
            '"' => in_string = true,
            '{' | '[' => depth += 1,
            '}' | ']' => {
                depth -= 1;
                if depth == 0 {
                    return Ok(s);
                }
            }
            _ => {}
        }
    }

    Err(CaError::DbParseError {
        line: start_line,
        token: String::new(),
        message: format!("unterminated JSON value (missing '{close}')"),
    })
}

/// A C `tokenSTRING` — the ONE reader for every grammar position `dbYacc.y`
/// spells that way (record type, record name, field name, info tag, alias
/// names, breaktable name, path/include).
///
/// `dbLex.l:88-97` gives `tokenSTRING` two lexical forms and the grammar never
/// distinguishes them:
///
/// * a bareword — `[a-zA-Z0-9_\-+:.\[\]<>;]+` (`dbLex.l:21`);
/// * a double-quoted string — taken RAW, the lexer only strips the quotes; no
///   escape translation happens in the INITIAL start condition (that is the
///   `JSON` condition's job, and it applies to VALUES only — see
///   [`read_field_value`]).
///
/// So `record("ai", "QT1") { field("VAL", "5") }` and `record(ai, QT2)` are the
/// same grammar to C, and softIoc loads both. The port used to read the type
/// and the field name with [`read_word`] (bareword only, and a narrower charset
/// at that) and the record/alias names with [`read_quoted_string`] (quoted
/// only) — so each position accepted one of C's two forms and refused the
/// other, turning a legal `.db` into a hard startup failure. Reading them all
/// through here is what makes the two forms indistinguishable to the caller,
/// as they are to C.
fn read_token_string(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<String> {
    if matches!(chars.get(*pos), Some('"')) {
        return read_quoted_string(chars, pos, line, col);
    }

    let mut s = String::new();
    while let Some(&c) = chars.get(*pos) {
        if c.is_ascii_alphanumeric()
            || matches!(c, '_' | '-' | '+' | ':' | '.' | '[' | ']' | '<' | '>' | ';')
        {
            s.push(c);
            *pos += 1;
            *col += 1;
        } else {
            break;
        }
    }
    if s.is_empty() {
        return Err(CaError::DbParseError {
            line: *line,
            token: yytext_at(chars, *pos),
            message: SYNTAX_ERROR.into(),
        });
    }
    Ok(s)
}

/// A field or info VALUE — C's `json_value` (dbYacc.y:256-267): the parser
/// switches into the `JSON` start condition for it, so a quoted value is a
/// `jsonSTRING` and its escapes are translated ([`read_json_string`]). The
/// unquoted forms (`jsonBARE`, `jsonNUMBER`) carry no escapes and are taken
/// verbatim, as C does.
fn read_field_value(
    chars: &[char],
    pos: &mut usize,
    line: &mut usize,
    col: &mut usize,
) -> CaResult<PvString> {
    if matches!(chars.get(*pos), Some('"' | '\'')) {
        return read_json_string(chars, pos, line, col);
    }

    // JSON value: C's dbLex tokenizes a brace- or bracket-delimited field
    // value as JSON (`field(DOL, {const:"hello"})`, `field(INP, {pva:"PV"})`,
    // `field(INP, [1, 2, 3])`) and hands the raw text to `dbParseLink`, which
    // calls `dbJLinkParse` for an object (dbStaticLib.c:2280-2285) and takes
    // a bracketed value as an array CONSTANT (`:2352-2356`). `json_array` is
    // a `json_value` alternative just like `json_object` (dbYacc.y:328-337,
    // `:356-363`), so both open the same way; the port had no `[` branch, so
    // `[1, 2, 3]` fell through to the bareword reader, stopped at the first
    // comma and failed the whole file. Keep the text verbatim — the link
    // parser is the one that interprets it.
    if matches!(chars.get(*pos), Some('{' | '[')) {
        // The brace text is JSON source — ASCII/UTF-8 by construction, and the
        // escapes inside it belong to yajl, not to the `.db` lexer (R19-67).
        return read_json_value(chars, pos, line, col).map(PvString::from);
    }

    // Unquoted value: a C `bareword` (dbLex.l:21) —
    // `[a-zA-Z0-9_\-+:.\[\]<>;]+`. Leading/trailing whitespace is
    // skipped; an embedded space or any character outside the
    // bareword set is a lexer error in C (the text would tokenize as
    // two tokens). L-5: the Rust parser previously accepted arbitrary
    // bytes up to the next `,`/`)`, which is strictly more permissive
    // than C.
    let is_bareword = |c: char| {
        c.is_ascii_alphanumeric()
            || matches!(c, '_' | '-' | '+' | ':' | '.' | '[' | ']' | '<' | '>' | ';')
    };

    let mut s = String::new();
    while *pos < chars.len() && is_bareword(chars[*pos]) {
        s.push(chars[*pos]);
        *pos += 1;
        *col += 1;
    }
    // Skip trailing whitespace before the delimiter.
    while *pos < chars.len() && matches!(chars[*pos], ' ' | '\t' | '\r' | '\n') {
        if chars[*pos] == '\n' {
            *line += 1;
            *col = 0;
        }
        *pos += 1;
        *col += 1;
    }
    // The only thing allowed after an unquoted value is the field
    // delimiter (`,` or `)`); anything else means the value contained
    // an illegal bareword character.
    if *pos < chars.len() && chars[*pos] != ')' && chars[*pos] != ',' {
        return Err(CaError::DbParseError {
            line: *line,
            token: yytext_at(chars, *pos),
            message: SYNTAX_ERROR.into(),
        });
    }
    Ok(PvString::from(s))
}

/// The sentence bison itself writes for EVERY grammar rejection
/// (`yyerror("syntax error")`): `dbYacc.y` declares no `%error-verbose` and no
/// per-production message, so `record(ai)`, `field(DESC "x")` and a stray `}`
/// all print the same three words on `softIoc` @`R7.0.10`. The token is what
/// tells them apart, and it is the ` at or before` clause's job.
///
/// This port used to name the expectation instead — `expected ')', got '}'` —
/// which reads better and is not what an operator comparing against C sees.
const SYNTAX_ERROR: &str = "syntax error";

/// C's `yytext` at `pos`: the token the `.db` lexer would have matched there,
/// which is what `yyerror` quotes (`dbYacc.y:378`).
///
/// The lexer's own token shapes, so the quoting matches C's. A quoted string
/// is returned WITH its quotes — `field(DESC "x")` prints ` at or before '"x"'`
/// on `softIoc` @`R7.0.10`, not the one character the port used to name — a
/// bareword is the whole `{bareword}` run (`dbLex.l:20`), and anything else is
/// the single character. End of input matched no token at all and answers with
/// the empty string.
fn yytext_at(chars: &[char], pos: usize) -> String {
    let Some(&c) = chars.get(pos) else {
        return String::new();
    };
    if c == '"' || c == '\'' {
        // `{doublequote}({dqschar}|{escape})*{doublequote}` (`dbLex.l:88-92`):
        // the escape is two characters and neither of them closes the string.
        let mut out = String::from(c);
        let mut i = pos + 1;
        while let Some(&d) = chars.get(i) {
            out.push(d);
            i += 1;
            if d == '\\' {
                if let Some(&e) = chars.get(i) {
                    out.push(e);
                    i += 1;
                }
                continue;
            }
            if d == c || d == '\n' {
                break;
            }
        }
        return out;
    }
    if is_bareword_char(c) {
        return chars[pos..]
            .iter()
            .take_while(|&&d| is_bareword_char(d))
            .collect();
    }
    c.to_string()
}

/// `bareword [a-zA-Z0-9_\-+:.\[\]<>;]` (`dbLex.l:20`).
fn is_bareword_char(c: char) -> bool {
    c.is_ascii_alphanumeric()
        || matches!(c, '_' | '-' | '+' | ':' | '.' | '[' | ']' | '<' | '>' | ';')
}

fn expect_char(
    chars: &[char],
    pos: &mut usize,
    col: &mut usize,
    expected: char,
    line: usize,
) -> CaResult<()> {
    if *pos >= chars.len() || chars[*pos] != expected {
        return Err(CaError::DbParseError {
            line,
            token: yytext_at(chars, *pos),
            message: SYNTAX_ERROR.into(),
        });
    }
    *pos += 1;
    *col += 1;
    Ok(())
}

/// C `dbStaticLib`'s record PROTOTYPE — the `initial("…")` directive.
///
/// `dbReadDatabase` builds one zeroed prototype per record type and writes each
/// field's `.dbd` `initial(...)` into it (`dbLexRoutines.c:1150-1163`,
/// `dbPutStringNum`); `dbCreateRecord` then copies that prototype, and the
/// `.db`'s own `field(...)` lines overwrite it. So a `.dbd` initial is the
/// record's default, and a `record(calc,"X") {}` with no CALC line still has
/// `CALC="0"` — not the empty string.
///
/// The port had no such step: every record hand-wrote a Rust `Default`, and
/// nothing tied it to the `.dbd`. calc, calcout and swait all forgot
/// `initial("0")` on their CALC/OCAL (`calcRecord.dbd:26`,
/// `calcoutRecord.dbd:62,382`, `swaitRecord.dbd:424`), so a default record
/// carried the EMPTY expression — which base's `postfix()` refuses
/// (`CALC_ERR_NULL_ARG`) and every evaluation then fails, putting a plain
/// `record(calc,"X") {}` into CALC/INVALID on its first process where C sits at
/// NO_ALARM.
///
/// This is the single owner of that step, so the `.dbd` is the only place a
/// default can be stated. A record's `Default` supplies the zeroed struct; the
/// `.dbd` supplies the initial value. The store is [`Record::put_field_internal`]
/// — dbStatic writes the prototype directly, with no `special()` and no
/// `SPC_NOMOD` gate, so a read-only field's initial lands too (its record's
/// `init_record` overwrites it afterwards, exactly as in C).
///
/// A field the record does not own carries its default in `CommonFields`
/// instead; there is no record storage here to write it into, and
/// `tests/dbd_initial_parity.rs` is what holds those to the `.dbd`.
fn apply_dbd_initials(record: &mut Box<dyn Record>) -> CaResult<()> {
    // The `.dbd` is the source, so the initials come from the GENERATED table —
    // `crate::server::record::dbd_generated`, which `tools/dbd-codegen` emits
    // from the vendored `.dbd` files. Asking the `.dbd` directly is what makes
    // the default impossible to get wrong by hand.
    //
    // This is BASE's generated table, so it seeds base's record types only. The
    // downstream record types (`motor`, `table`, `scaler`, `epid`, `throttle`,
    // `timestamp`) now carry a generated table too — in their own crate, which
    // base cannot name — and their `.dbd` `initial()` values are therefore
    // parsed but never applied: those records keep the defaults their `Default`
    // impl sets. Seeding them is not a table lookup but a semantic change (C
    // applies the `.dbd` initial BEFORE `init_record`, and each record's
    // `init_record` then overwrites some of them — `motor`'s VERS is stamped
    // 7.4 over an `initial("1")`), so it is left to a change that can validate
    // each record against its C `init_record` rather than folded in here.
    let Some(fields) = crate::server::record::dbd_generated::record_fields(record.record_type())
    else {
        return Ok(()); // a record type base's vendored `.dbd` set does not declare
    };
    let initials: Vec<(&'static str, &'static str)> = fields
        .iter()
        .filter(|f| record.implements_field(f.name))
        .filter_map(|f| f.initial.map(|v| (f.name, v)))
        .collect();

    for (name, initial) in initials {
        let spec = fields.iter().find(|f| f.name == name);
        // The `.db` load path's coercion, reused verbatim: a `menu()` field's
        // initial is a LABEL (`initial("Passive")`), everything else parses to
        // the field's stored type — which is what the record already holds, the
        // `.dbd` type being the fallback for a field it cannot yet produce.
        let dbf_type = match record
            .get_field(name)
            .map(|v| v.db_field_type())
            .or_else(|| spec.map(|f| f.dbf_type))
        {
            Some(t) => t,
            None => continue,
        };
        let parsed = if let Some(choices) = spec
            .and_then(|f| f.menu)
            .or_else(|| record.menu_field_choices(name))
            .or_else(|| crate::server::record::shared_menu_choices(name))
        {
            crate::server::record::resolve_menu_field_string_db_load(
                name, choices, dbf_type, initial,
            )?
        } else {
            EpicsValue::parse_bytes(dbf_type, initial.as_bytes()).map_err(|e| {
                CaError::InvalidValue(format!(
                    "{}.{name}: cannot parse .dbd initial(\"{initial}\") as {dbf_type:?}: {e}",
                    record.record_type()
                ))
            })?
        };
        // A field whose zeroed Rust default already IS the `.dbd` initial needs
        // no store — and must not get one: a `SPC_NOMOD` field the record serves
        // but has no store arm for (`sseq.IX1`, C's read-only step index, whose
        // initial is the 0 it already holds) would fail the write. The write is
        // therefore driven by the DIFFERENCE, which is the thing this step exists
        // to remove.
        if record.get_field(name).as_ref() == Some(&parsed) {
            continue;
        }
        record.put_field_internal(name, parsed)?;
    }
    Ok(())
}

/// Create a record from a type name.
/// Checks the external factory registry first, then falls back to built-in types.
///
/// The record comes back holding its `.dbd` initial values — see
/// `apply_dbd_initials`. Every load path (`IocBuilder`, `dbLoadRecords`,
/// `dbCreateRecord`) goes through here, so no record can reach the database
/// disagreeing with its `.dbd` about a default.
pub fn create_record(record_type: &str) -> CaResult<Box<dyn Record>> {
    let mut record = create_record_raw(record_type)?;
    apply_dbd_initials(&mut record)?;
    Ok(record)
}

fn create_record_raw(record_type: &str) -> CaResult<Box<dyn Record>> {
    // Check external registry first (allows overrides from e.g. asyn-rs)
    if let Ok(reg) = get_registry().lock() {
        if let Some(factory) = reg.get(record_type) {
            return Ok(factory());
        }
    }

    use crate::server::records::*;

    match record_type {
        "ai" => Ok(Box::new(ai::AiRecord::default())),
        "ao" => Ok(Box::new(ao::AoRecord::default())),
        "bi" => Ok(Box::new(bi::BiRecord::default())),
        "bo" => Ok(Box::new(bo::BoRecord::default())),
        "stringin" => Ok(Box::new(stringin::StringinRecord::default())),
        "stringout" => Ok(Box::new(stringout::StringoutRecord::default())),
        "longin" => Ok(Box::new(longin::LonginRecord::default())),
        "longout" => Ok(Box::new(longout::LongoutRecord::default())),
        "int64in" => Ok(Box::new(int64in::Int64inRecord::default())),
        "int64out" => Ok(Box::new(int64out::Int64outRecord::default())),
        "lsi" => Ok(Box::new(lsi::LsiRecord::default())),
        "lso" => Ok(Box::new(lso::LsoRecord::default())),
        "mbbi" => Ok(Box::new(mbbi::MbbiRecord::default())),
        "mbbo" => Ok(Box::new(mbbo::MbboRecord::default())),
        "mbbiDirect" => Ok(Box::new(mbbi_direct::MbbiDirectRecord::default())),
        "mbboDirect" => Ok(Box::new(mbbo_direct::MbboDirectRecord::default())),
        "event" => Ok(Box::new(event::EventRecord::default())),
        "printf" => Ok(Box::new(printf::PrintfRecord::default())),
        "waveform" => Ok(Box::new(waveform::WaveformRecord::with_kind(
            waveform::ArrayKind::Waveform,
        ))),
        "aai" => Ok(Box::new(waveform::WaveformRecord::with_kind(
            waveform::ArrayKind::Aai,
        ))),
        "aao" => Ok(Box::new(waveform::WaveformRecord::with_kind(
            waveform::ArrayKind::Aao,
        ))),
        "subArray" => Ok(Box::new(waveform::WaveformRecord::with_kind(
            waveform::ArrayKind::SubArray,
        ))),
        "calc" => Ok(Box::new(calc::CalcRecord::default())),
        "fanout" => Ok(Box::new(fanout::FanoutRecord::default())),
        "seq" => Ok(Box::new(seq::SeqRecord::default())),
        "calcout" => Ok(Box::new(calcout::CalcoutRecord::default())),
        "dfanout" => Ok(Box::new(dfanout::DfanoutRecord::default())),
        "compress" => Ok(Box::new(compress::CompressRecord::default())),
        "histogram" => Ok(Box::new(histogram::HistogramRecord::default())),
        "sel" => Ok(Box::new(sel::SelRecord::default())),
        "sub" => Ok(Box::new(sub_record::SubRecord::default())),
        "aSub" => Ok(Box::new(asub_record::ASubRecord::default())),
        "permissive" => Ok(Box::new(permissive::PermissiveRecord::default())),
        "state" => Ok(Box::new(state::StateRecord::default())),
        _ => Err(unregistered_record_type(record_type)),
    }
}

/// The error a record type reaches when no arm above claims it.
///
/// Two populations land here and they are not the same mistake. A type
/// `epics-base-rs` vendors a `.dbd` for — so `record_fields` knows its shape —
/// but does not construct is **module-owned**: `dbd/stdRecords.dbd`, vendored
/// byte-identical from C, is the manifest of what an EPICS Base IOC links, and
/// `aCalcout`/`asyn`/`busy`/`sCalcout`/`sseq`/`swait`/`transform` are not in
/// it. They belong to calc, asyn and busy, and base's default registry no
/// longer claims them; the application opts in with
/// [`register_record_type`] (or `IocBuilder`/`IocApplication`'s method of the
/// same name). Anything else is simply not a record type the port knows.
fn unregistered_record_type(record_type: &str) -> CaError {
    let message = if crate::server::record::dbd_generated::record_fields(record_type).is_some() {
        format!(
            "record type '{record_type}' is not an EPICS Base record type \
             (it is not in stdRecords.dbd) and is not registered; register it \
             with register_record_type(\"{record_type}\", ...) before loading \
             a .db that uses it"
        )
    } else {
        format!("unknown record type: '{record_type}'")
    };
    CaError::DbParseError {
        line: 0,
        token: String::new(),
        message,
    }
}

/// Create a record, checking extra factories first, then built-in types.
/// Preferred over `create_record()` — avoids the global registry.
pub fn create_record_with_factories(
    record_type: &str,
    extra_factories: &std::collections::HashMap<String, super::RecordFactory>,
) -> CaResult<Box<dyn Record>> {
    if let Some(factory) = extra_factories.get(record_type) {
        let mut record = factory();
        apply_dbd_initials(&mut record)?;
        return Ok(record);
    }
    create_record(record_type)
}

/// Apply fields from a DbRecordDef to a record.
/// Returns the record along with any common field values.
/// Rewrite a `LINR` field that names a loaded breakpoint table to the numeric
/// `menuConvert` index that selects it. C extends the `menuConvert` menu with
/// one name-sorted choice per loaded table, so the first table is index 3
/// ([`crate::server::cvt_bpt::BreakTableRegistry`]). Only `ai`/`ao` carry a
/// `LINR` field; fixed labels (`NO_CONVERSION`/`SLOPE`/`LINEAR`) and numeric
/// values match no table name and are left for [`apply_fields`]'s menu
/// resolution. A no-op when the registry is empty. Shared by the IocBuilder
/// and `dbLoadRecords` load paths so both resolve table names identically.
pub fn resolve_linr_breaktable_names(
    record_type: &str,
    fields: &mut [DbFieldDef],
    registry: &crate::server::cvt_bpt::BreakTableRegistry,
) {
    if registry.is_empty() || !matches!(record_type, "ai" | "ao") {
        return;
    }
    for DbFieldDef {
        name: fname,
        value: fvalue,
        ..
    } in fields.iter_mut()
    {
        if fname.eq_ignore_ascii_case("LINR") {
            // A breakpoint-table NAME is an identifier — ASCII.
            if let Some(idx) = registry.linr_index_of(&fvalue.as_str_lossy()) {
                *fvalue = PvString::from(idx.to_string());
            }
        }
    }
}

/// The link fields whose value must reach [`RecordCommon`] — and therefore
/// device-support init via `DeviceSupportContext` — for every record type,
/// even one that stores the field itself.
///
/// [`RecordCommon`]: crate::server::record::CommonFields
fn is_common_link_field(upper_name: &str) -> bool {
    matches!(upper_name, "INP" | "OUT")
}

/// C's db-load half of the link gate. `dbPutString` on a link field runs
/// `dbParseLink` and stores the text (`dbStaticLib.c:2626-2634`) — nothing
/// else is asked at load, so a `.db` fails here only on text that will not
/// PARSE: a brace-delimited link naming no registered jlink type, or a `#`
/// form whose identifier letters name no bus.
///
/// Whether the link FITS the record's bound device support is `dbCanSetLink`,
/// and C asks that exactly once, at `iocInit`, from `dbInitRecordLinks`
/// (`dbStaticLib.c:2222`) — which prints the refusal and keeps the record.
/// [`PvDatabase::db_init_record_links`] is that pass. Asking it here instead
/// cost a `.db` every record after the first bad link: measured against
/// `softIoc` R7.0.10 on a ten-record file, C kept ten and the port kept two.
///
/// [`PvDatabase::db_init_record_links`]: crate::server::database::PvDatabase
fn check_link_syntax(record_type: &str, fields: &[DbFieldDef]) -> CaResult<()> {
    for DbFieldDef { name, value, .. } in fields {
        check_link_text(record_type, &name.to_uppercase(), &value.as_str_lossy())?;
    }
    Ok(())
}

/// C's `pflddes->field_type == DBF_NOACCESS`, read from the two places the
/// generated tables keep the declaration.
///
/// A `DBF_NOACCESS` row survives into the field table two ways, and neither
/// leaves `dbf_type` saying `NOACCESS` — which is why [`FieldDesc::no_access`]
/// has to read the DECLARED type. `special(SPC_DBADDR)` rows are re-typed from
/// `cvt_dbaddr.types` so they can be SERVED; rows whose `extra(...)` names a
/// plain scalar (`BKPT`, `TIME`) are carried for their width and stay
/// unservable. Everything else is dropped from the table and survives only as a
/// name, in `record_noaccess_fields` / `DB_COMMON_NOACCESS`. The union of table
/// and names is the declaration, so both are asked.
fn is_declared_no_access(
    record_type: &str,
    field: &str,
    spec: Option<&'static crate::server::record::FieldDesc>,
) -> bool {
    if spec.is_some_and(|f| f.no_access()) {
        return true;
    }
    let named = |list: &[&str]| list.iter().any(|f| f.eq_ignore_ascii_case(field));
    crate::server::record::dbd_generated::record_noaccess_fields(record_type).is_some_and(named)
        || named(crate::server::record::dbd_generated::DB_COMMON_NOACCESS)
}

/// The value C's `.db` loader stores for `value` on a numeric `field`, or
/// `None` where `dbPutStringNum` has no numeric arm for the declaration.
///
/// A refusal is `None` too, and deliberately: the screen
/// ([`menu_value_refusal`]) has already removed every field C refuses before
/// the load reaches here, so a refusal at this point can only come from a
/// caller that skipped the screen — and those keep the conversion they had.
fn lax_numeric(declared: DbfCode, field: &str, value: &str) -> Option<EpicsValue> {
    db_load_numeric_value(declared, field, value).ok().flatten()
}

pub fn apply_fields(
    record: &mut Box<dyn Record>,
    fields: &[DbFieldDef],
    common_fields: &mut Vec<(String, EpicsValue)>,
) -> CaResult<()> {
    check_link_syntax(record.record_type(), fields)?;

    for DbFieldDef { name, value, .. } in fields {
        let upper_name = name.to_uppercase();
        // Menu labels, numbers and link text are ASCII by construction; only a
        // DBF_STRING can carry a byte outside it, and that one goes to
        // `EpicsValue::parse_bytes` below, which keeps the bytes.
        let value_str = &value.as_str_lossy();

        // Two separate questions, and they must stay separate: *who owns* this
        // field (the record, or the framework's dbCommon fallback), and *what
        // type* the `.db` string parses as. `field_list()` is the spec and now
        // carries every field the `.dbd` declares — including INP/OUT, which
        // most record types leave to the framework — so ownership is asked of
        // `implements_field()`, not of table membership.
        let owned = record.implements_field(&upper_name);
        // The DECLARATION, from the same owner every other consumer asks: the
        // `.dbd`-generated table first, the record's hand table only where the
        // `.dbd` does not reach. Reading it off `field_list()` alone left an
        // `aSub`'s `field(FTA,"LONG")` with no `menu()` to resolve against.
        let spec = crate::server::record::record_instance::field_desc_of(
            record.as_ref(),
            upper_name.as_str(),
        );

        // C `dbPutString` switches on the DECLARED field type, and a
        // `DBF_NOACCESS` field is refused outright — `dbStaticLib.c:2646-2650`:
        //
        // ```c
        //     case DBF_NOACCESS:
        //         dbMsgPrint(pdbentry, "Can't set array field before iocInit()");
        //         /* fall through */
        //     default:
        //         return S_dbLib_badField;
        // ```
        //
        // The port used to name the five fields it had been told about
        // (lsi/lso/printf VAL, lsi/lso OVAL) through `long_string_fields`, so the
        // other 172 `DBF_NOACCESS` rows the vendored `.dbd`s declare — every
        // `waveform.VAL`, `aSub.VALx`, `SIMPVT` — were accepted and quietly
        // stored where C fails the load. A by-name list cannot be complete;
        // the declaration can, so ask it.
        if is_declared_no_access(record.record_type(), &upper_name, spec) {
            return Err(CaError::InvalidValue(format!(
                "field {upper_name}: can't set array field before iocInit() \
                 (a DBF_NOACCESS field is not settable from a .db file, as in C)"
            )));
        }

        if owned {
            // Parse to the type the record STORES, exactly as
            // `put_field_internal_default` coerces to it: `put_field`'s arms
            // match on what is stored, and a `menu()` field is declared
            // `DBF_MENU` but stored as a `Short` choice index. The `.dbd` type
            // is the fallback for a field the record cannot yet produce a value
            // for.
            let dbf_type = record
                .get_field(&upper_name)
                .map(|v| v.db_field_type())
                .or_else(|| spec.map(|f| f.dbf_type))
                .ok_or_else(|| {
                    CaError::InvalidValue(format!("field {upper_name}: no type to parse it as"))
                })?;
            // A `DBF_MENU` field's `.db` value resolves against THAT field's
            // own menu (label-first, then a numeric index) — C dbStaticLib
            // `dbPutStringNum`. Using the field's choices, not a cross-menu
            // global table, is what keeps a menu-specific label from being
            // dropped or mis-mapped (e.g. `field(SELM,"Specified")`).
            let parsed = if let Some(choices) = spec
                .and_then(|f| f.menu)
                .or_else(|| record.menu_field_choices(&upper_name))
                .or_else(|| crate::server::record::shared_menu_choices(&upper_name))
            {
                crate::server::record::resolve_menu_field_string_db_load(
                    &upper_name,
                    choices,
                    dbf_type,
                    value_str,
                )?
            } else if let Some(v) =
                spec.and_then(|f| lax_numeric(f.declared_dbf, &upper_name, value_str))
            {
                // C's `dbPutStringNum` conversion, not `dbConvert`'s: the `.db`
                // load parses to 64 bits and casts, so it stores what a
                // `caput` of the same text would be refused for. Projected onto
                // the carrier the record actually holds, which for a numeric
                // field is the declared width itself.
                v.convert_to(dbf_type)
            } else {
                // BYTES, not text: a DBF_STRING is a byte string with a 40-byte
                // budget, and `field(VAL,"h\xffz")` is 3 bytes in C (R19-68).
                EpicsValue::parse_bytes(dbf_type, value.as_bytes()).map_err(|e| {
                    CaError::InvalidValue(format!(
                        "field {upper_name} (type {dbf_type:?}): cannot parse '{value_str}': {e}"
                    ))
                })?
            };
            record.put_field(&upper_name, parsed)?;
            // A record type may declare INP/OUT in its own `field_list`,
            // mirroring its C `.dbd` (`scalerRecord`, `motorRecord`,
            // `acalcout`, …). The value is then stored by the record — but in
            // C the link is *also* what device support reads at init:
            // `devXxx.c::init_record` dereferences `prec->out` / `prec->inp`
            // directly (e.g. `devScalerAsyn.c::scaler_init_record` parses OUT
            // to pick its board), and a record owning the field changes
            // nothing about that. Routing the value to the record ALONE left
            // `RecordCommon.inp`/`.out` — the single source of truth
            // `DeviceSupportContext` is built from — empty, so a dynamic
            // device-support factory saw `ctx.out == ""` and could not
            // disambiguate. Mirror link fields into the common fields as well
            // so the link text reaches device-support init for every record
            // type. This is text only: `RecordInstance::put_common_field`
            // arms the framework's own link dispatch (`parsed_inp` /
            // `parsed_out`) only for a record that does NOT declare the field,
            // so a record driving its own link is not driven twice.
            if is_common_link_field(&upper_name) {
                common_fields.push((upper_name.clone(), EpicsValue::String(value.clone())));
            }
        } else {
            // Store as common field for RecordInstance to handle. The BYTES: a
            // common DBF_STRING (DESC, EGU, …) has the same 40-byte budget.
            // A numeric one is converted HERE, by the same `dbPutStringNum` the
            // screen refuses with, so the sink is handed a value rather than
            // text it would convert by the runtime `dbPut` rules instead.
            let converted = spec.and_then(|f| lax_numeric(f.declared_dbf, &upper_name, value_str));
            common_fields.push((
                upper_name.clone(),
                converted.unwrap_or_else(|| EpicsValue::String(value.clone())),
            ));
        }

        // C `dbPutString` (`dbStaticLib.c:2653-2660`): writing VAL through the
        // STATIC library also writes UDF = 0 —
        //
        // ```c
        // if (!status && strcmp(pflddes->name, "VAL") == 0) {
        //     ...
        //     if (!dbFindField(&dbentry, "UDF")) dbPutString(&dbentry, "0");
        // }
        // ```
        //
        // — so `field(VAL,"1")` in a `.db` DEFINES the record at load, on every
        // record type, whatever its `process()` later does with UDF. The load
        // owner is the only place this can live: it is the one gate both
        // `dbLoadRecords` paths (IocBuilder and iocsh) cross. Pushed in file
        // order, so an explicit `field(UDF,...)` after `field(VAL,...)` still
        // wins, exactly as it does in C.
        if upper_name == "VAL" {
            common_fields.push(("UDF".to_string(), EpicsValue::Char(0)));
        }
    }
    Ok(())
}

/// Render a fixture path so it survives being used as a MACRO or
/// ENVIRONMENT value.
///
/// `trans` discards escapes in a substituted value (C
/// `macCore.c:701-703`), so every `\` in a Windows path is eaten the
/// moment the value is re-scanned and `$(TOP)/x.db` resolves against a
/// separator-less string. Win32 accepts `/` as a separator, which is
/// why EPICS build systems put `TOP` in forward-slash form there; a
/// test that feeds `tempdir()` into a macro has to do the same.
#[cfg(test)]
pub(crate) fn macro_safe_path(path: &std::path::Path) -> String {
    path.to_string_lossy().replace('\\', "/")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_db() {
        let input = r#"
    record(ai, "TEMP") {
    field(DESC, "Temperature")
    field(SCAN, "1 second")
    field(HOPR, "100")
    field(LOPR, "0")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].record_type, "ai");
        assert_eq!(records[0].name, "TEMP");
        assert_eq!(records[0].fields.len(), 4);
        // Line 3, not 2: the raw string opens with a newline, so
        // `record(...)` is line 2 and this field is the line under it.
        // The number is the whole point of carrying it on the element —
        // it is what `yyerror` prints when the field is refused.
        assert_eq!(
            records[0].fields[0],
            DbFieldDef {
                name: "DESC".into(),
                value: "Temperature".into(),
                line: 3
            }
        );
    }

    #[test]
    fn test_macro_substitution() {
        let input = r#"
    record(ai, "$(P)TEMP") {
    field(DESC, "$(D=Default Desc)")
    }
    "#;
        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "IOC:".to_string());

        let records = parse_db(input, &macros).unwrap();
        assert_eq!(records[0].name, "IOC:TEMP");
        assert_eq!(records[0].fields[0].value, "Default Desc");
    }

    #[test]
    fn test_multiple_records() {
        let input = r#"
    record(ai, "TEMP1") {
    field(VAL, "25.0")
    }
    record(bo, "SWITCH") {
    field(VAL, "1")
    field(ZNAM, "Off")
    field(ONAM, "On")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records.len(), 2);
        assert_eq!(records[0].record_type, "ai");
        assert_eq!(records[1].record_type, "bo");
    }

    #[test]
    fn test_comments() {
        let input = r#"
    # This is a comment
    record(ai, "TEMP") {
    # Another comment
    field(VAL, "25.0")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records.len(), 1);
    }

    #[test]
    fn test_unknown_record_type() {
        let result = create_record("nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_quoted_string_escape() {
        // R18-91: a quoted field VALUE is a `jsonSTRING`, and
        // `dbGetFieldValue` runs `dbTranslateEscape` over it — so
        // `"hello \"world\""` stores the 13 chars `hello "world"`, with real
        // quotes. (This test previously pinned the defect, asserting the raw
        // 15-char form.) The `\"` still does not terminate the string.
        //
        // softIoc: field(VAL,"a \"b\" c") -> dbgf prints "a \"b\" c", i.e. it
        // re-escapes on print (dbTest.c:1007), so the stored bytes are 9.
        let input = r#"
    record(stringin, "TEST") {
    field(VAL, "hello \"world\"")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].fields[0].value, r#"hello "world""#);
    }

    /// C `dbRecordField`'s `dbFindField` arm (`dbLexRoutines.c:1240-1388`
    /// @`R7.0.10`) ends in `yyerror(NULL)`, not `yyerrorAbort`: the FIELD is
    /// dropped and nothing else is. Measured on softIoc @`R7.0.10` with this
    /// exact text — `dbl` lists C:ONE, C:TWO and C:THREE, `dbgf C:TWO.CALC`
    /// is "2", and `dbgf C:TWO.OUT` is `PV 'C:TWO.OUT' not found`.
    #[test]
    fn an_undeclared_field_is_dropped_and_the_rest_of_the_load_survives() {
        let parsed = parse_db_with_breaktables(
            r#"
record(calc, "C:ONE") { field(CALC, "1") }
record(calc, "C:TWO") {
    field(OUT, "C:ONE.VAL PP")
    field(CALC, "2")
}
record(calc, "C:THREE") { field(CALC, "3") }
"#,
            &HashMap::new(),
        )
        .expect("an undeclared field must not abort the parse");

        let names: Vec<&str> = parsed.records.iter().map(|r| r.name.as_str()).collect();
        assert_eq!(
            names,
            ["C:ONE", "C:TWO", "C:THREE"],
            "every record survives"
        );
        let two = &parsed.records[1];
        assert_eq!(
            two.fields
                .iter()
                .map(|f| f.name.as_str())
                .collect::<Vec<_>>(),
            ["CALC"],
            "OUT is dropped, CALC is kept"
        );
        assert_eq!(
            parsed.faults.first_diagnostic().unwrap(),
            format!("{ERL_ERROR}: calc record 'C:TWO' doesn't have a field 'OUT'"),
            "C's message, verbatim"
        );
    }

    /// The second name-side refusal in the same C function
    /// (`dbLexRoutines.c:1390-1396`): `dbFindField` FINDS `NAME` — it is
    /// dbCommon's field 0 — so `indfield == 0` refuses it separately, with its
    /// own wording. softIoc @`R7.0.10` on `field(NAME,"C:OTHER")` prints
    /// `ERROR: Can't set 'NAME' field of record 'C:N'` and still loads C:N
    /// with CALC applied.
    #[test]
    fn the_name_field_is_refused_with_its_own_message() {
        let parsed = parse_db_with_breaktables(
            r#"record(calc, "C:N") { field(NAME, "C:OTHER") field(CALC, "5") }"#,
            &HashMap::new(),
        )
        .expect("NAME must not abort the parse");
        assert_eq!(parsed.records[0].name, "C:N");
        assert_eq!(
            parsed.records[0]
                .fields
                .iter()
                .map(|f| f.name.as_str())
                .collect::<Vec<_>>(),
            ["CALC"]
        );
        assert_eq!(
            parsed.faults.first_diagnostic().unwrap(),
            format!("{ERL_ERROR}: Can't set 'NAME' field of record 'C:N'")
        );
    }

    /// `dbFindFieldPart` compares with `strncmp` (`dbStaticLib.c:1822-1824`),
    /// so the lookup is case-SENSITIVE and `calc` is not `CALC`. softIoc
    /// @`R7.0.10`: `ERROR: calc record 'C:CASE' doesn't have a field 'calc'`.
    #[test]
    fn a_field_name_in_the_wrong_case_is_not_the_field() {
        let parsed = parse_db_with_breaktables(
            r#"record(calc, "C:CASE") { field(calc, "9") }"#,
            &HashMap::new(),
        )
        .unwrap();
        assert!(parsed.records[0].fields.is_empty());
        assert_eq!(
            parsed.faults.first_diagnostic().unwrap(),
            format!("{ERL_ERROR}: calc record 'C:CASE' doesn't have a field 'calc'")
        );
    }

    /// `dbFindField` falls back to `dbGetRecordAttribute`
    /// (`dbStaticLib.c:1852-1853`), and `dbReadCOM` installs `RTYP` and `VERS`
    /// on every record type (`dbLexRoutines.c:311-331`) — so these two load in
    /// C where any other unknown name does not. Measured: softIoc @`R7.0.10`
    /// takes `field(RTYP,"zz")` and `field(VERS,"9")` on a calc record with no
    /// diagnostic at all.
    #[test]
    fn the_rtyp_and_vers_record_attributes_load() {
        let parsed = parse_db_with_breaktables(
            r#"record(calc, "C:A") { field(RTYP, "zz") field(VERS, "9") field(CALC, "1") }"#,
            &HashMap::new(),
        )
        .unwrap();
        assert_eq!(parsed.records[0].fields.len(), 3);
        assert!(parsed.faults.is_empty(), "C reports nothing here");
    }

    /// A `DBF_NOACCESS` field is IN `papFldDes`, so `dbFindField` finds it and
    /// it is `dbPutString` that refuses the write (`dbStaticLib.c:2646-2650`).
    /// The name-side gate must therefore let it through to
    /// [`is_declared_no_access`] rather than claim the field does not exist.
    #[test]
    fn a_noaccess_field_passes_the_name_gate() {
        let parsed = parse_db_with_breaktables(
            r#"record(waveform, "W:ONE") { field(VAL, "1") }"#,
            &HashMap::new(),
        )
        .unwrap();
        assert_eq!(parsed.records[0].fields.len(), 1);
        assert!(parsed.faults.is_empty());
        assert_eq!(
            record_type_field_exists("waveform", "VAL"),
            Some(true),
            "waveform.VAL is declared DBF_NOACCESS, not absent"
        );
    }

    /// C `dbPutString`'s `DBF_NOACCESS` arm refuses the write
    /// (`dbStaticLib.c:2646-2650`), and `is_declared_no_access` asks two
    /// sources because the declaration lives in two places. This pins that the
    /// answer does not depend on WHICH.
    ///
    /// `BKPT` and `TIME` are the reason: they used to be answered by the name
    /// list, and carrying their descriptors took them out of it, so the refusal
    /// now comes from the `spec` arm alone. That arm had no live caller before
    /// — every row reaching it was `special(SPC_DBADDR)`, which the name gate
    /// had already let through — so a silent acceptance here would have looked
    /// exactly like the absence it replaced.
    #[test]
    fn a_no_access_field_is_refused_from_a_db_file_by_either_source() {
        use crate::server::record::Record;

        let load = |rt: &str, field: &str| -> CaResult<Box<dyn Record>> {
            let mut rec = create_record(rt).unwrap();
            let mut common = Vec::new();
            apply_fields(&mut rec, &[DbFieldDef::new(field, "1")], &mut common)?;
            Ok(rec)
        };

        // The `spec` arm: a carried descriptor, no longer in any name list.
        for field in ["BKPT", "TIME"] {
            assert!(
                !crate::server::record::dbd_generated::DB_COMMON_NOACCESS.contains(&field),
                "{field} is carried as a descriptor, so the name list must not \
                 be what refuses it"
            );
            let Err(err) = load("ai", field) else {
                panic!("ai.{field} is DBF_NOACCESS and must not be settable from a .db");
            };
            assert!(err.to_string().contains("DBF_NOACCESS"), "{field}: {err}");
        }

        // The name-list arm, which those two left: still refusing.
        let Err(err) = load("ai", "MLOK") else {
            panic!("ai.MLOK is DBF_NOACCESS and must not be settable from a .db");
        };
        assert!(err.to_string().contains("DBF_NOACCESS"), "MLOK: {err}");

        // And a settable field of the same record still loads, so the gate did
        // not simply start refusing everything.
        assert!(load("ai", "PREC").is_ok());
    }

    /// C reaches `dbRecordField` only for a type `dbFindRecordType` resolved;
    /// anything else is `yyerrorAbort` in `dbRecordHead`
    /// (`dbLexRoutines.c:1159-1165`). The port cannot abort there — the type
    /// is resolved later, by `create_record` — so the name gate must decline
    /// to judge a type it cannot see instead of calling every field absent.
    #[test]
    fn a_record_type_with_no_visible_declarations_is_not_judged() {
        assert_eq!(record_type_field_exists("noSuchRecordType", "ANY"), None);
        let parsed = parse_db_with_breaktables(
            r#"record(noSuchRecordType, "X:ONE") { field(WHATEVER, "1") }"#,
            &HashMap::new(),
        )
        .unwrap();
        assert_eq!(parsed.records[0].fields.len(), 1);
        assert!(parsed.faults.is_empty());
    }

    #[test]
    fn test_quoted_value_translates_escapes() {
        // R18-91: `\n`, `\t`, `\\` and `\xHH` are all TRANSLATED in a field
        // value — softIoc `field(DESC,"hex\x41end")` -> `dbgf E2.DESC` =
        // "hexAend". (This test previously pinned the defect under the name
        // `test_quoted_string_keeps_escapes_raw`.)
        // Every field named here is one `stringin` actually declares:
        // `field(OUT,...)` used to stand in the middle of this list, which
        // `dbRecordField` refuses outright (`ERROR: stringin record 'TEST'
        // doesn't have a field 'OUT'`, measured on softIoc @`R7.0.10`).
        let input = r#"
    record(stringin, "TEST") {
    field(DESC, "line1\nline2")
    field(VAL, "a\\b\tc")
    field(SIOL, "hex\x41end")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].fields[0].value, "line1\nline2");
        assert_eq!(records[0].fields[1].value, "a\\b\tc");
        assert_eq!(records[0].fields[2].value, "hexAend");
    }

    /// The other half of the split: a record/alias NAME is a `tokenSTRING`
    /// (dbLex.l:88-92) and keeps its escape bytes RAW. Unescaping the shared
    /// helper would have broken every name.
    #[test]
    fn test_record_name_keeps_escapes_raw() {
        let input = r#"
    record(stringin, "T\tEST") {
    field(VAL, "x")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].name, r"T\tEST");
    }

    /// An `info` VALUE is translated (`dbRecordInfo`, dbLexRoutines.c:1435-1440);
    /// the info TAG is a `tokenSTRING` and is not.
    #[test]
    fn test_info_value_translates_escapes() {
        let input = r#"
    record(stringin, "TEST") {
    info("Q:group", "tab\there")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].info_tags[0].0, "Q:group");
        assert_eq!(records[0].info_tags[0].1, "tab\there");
    }

    /// A single-quoted value is a `jsonsqstr` (dbLex.l:31-33) and is translated
    /// the same way — softIoc: `field(VAL,'sq:\tx')` stores `sq:<TAB>x`.
    #[test]
    fn test_single_quoted_value_translates_escapes() {
        let input = "record(stringin, \"TEST\") {\n field(VAL, 'sq:\\tx')\n}\n";
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].fields[0].value, "sq:\tx");
    }

    /// C's value grammar is STRICTER than a raw read: `\1`..`\9` is not an
    /// escape (`escapedchar` = `{backslash}[^ux1-9]`, dbLex.l:25), so a `.db`
    /// carrying an octal escape is a syntax error — softIoc rejects
    /// `field(VAL,"octal\101x")` with `ERROR: syntax error`, and the port must
    /// not silently start up with different data.
    #[test]
    fn test_octal_escape_in_value_is_rejected() {
        let input = r#"
    record(stringin, "TEST") {
    field(VAL, "octal\101x")
    }
    "#;
        assert!(matches!(
            parse_db(input, &HashMap::new()),
            Err(CaError::DbParseError { .. })
        ));
    }

    /// A link field rides the same path: the escaped quotes reach device
    /// support as real quotes, not backslashes (R18-91's headline impact).
    #[test]
    fn test_link_field_value_is_translated() {
        let input = r#"
    record(stringin, "TEST") {
    field(INP, "@drvUser(\"chan1\")")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].fields[0].value, r#"@drvUser("chan1")"#);
    }

    #[test]
    fn test_quoted_string_newline_aborts() {
        // a literal newline inside a quoted string (missing
        // closing quote) is a hard parse error in C (dbLex.l:131-133).
        let input = "record(stringin, \"TEST\") {\n    field(DESC, \"line1\nline2\")\n}\n";
        let res = parse_db(input, &HashMap::new());
        assert!(
            matches!(res, Err(CaError::DbParseError { ref message, .. })
                if message.contains("Newline in string")),
            "expected newline-in-string abort, got {res:?}"
        );
    }

    #[test]
    fn test_quoted_string_backslash_before_newline_aborts() {
        // The NAME rule (dbLex.l:88-92): `{escape}` is `{backslash}.` and flex
        // `.` never matches a newline (`{dqschar}` is `[^"\n\\]`), so a
        // backslash immediately before a newline is NOT an escape — the string
        // stays unterminated and aborts (dbLex.l:131-133).
        //
        // softIoc, `record(stringin,"BN2\<newline>X")`:
        //   ERROR: Invalid character '"' / Invalid character '\' / syntax error
        let input = "record(stringin, \"TE\\\nST\") {\n    field(DESC, \"x\")\n}\n";
        let res = parse_db(input, &HashMap::new());
        assert!(
            matches!(res, Err(CaError::DbParseError { ref message, .. })
                if message.contains("Newline in string")),
            "expected newline-in-string abort, got {res:?}"
        );
    }

    /// The VALUE rule is the other start condition and disagrees on exactly
    /// this character: `escapedchar` is `{backslash}[^ux1-9]` (dbLex.l:25), a
    /// character CLASS, which does match a newline. So a backslash before a
    /// newline is a valid escape in a value, and the translation's default arm
    /// yields the newline itself.
    ///
    /// softIoc, `field(DESC, "line1\<newline>line2")` → `dbgf BN.DESC` prints
    /// `"line1\nline2"` — i.e. a real newline, re-escaped for display.
    #[test]
    fn test_value_backslash_before_newline_is_a_newline() {
        let input = "record(stringin, \"TEST\") {\n    field(DESC, \"line1\\\nline2\")\n}\n";
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records[0].fields[0].value, "line1\nline2");
    }

    #[test]
    fn test_macro_with_quoted_default_in_string() {
        // C EPICS macLib treats quotes inside $(...) as literal characters.
        // e.g. $(XPOS="") means "default to empty-string pair".
        let input = r#"
    record(longout, "$(P)$(R)PositionXLink") {
    field(DOL, "$(XPOS="") CP MS")
    }
    "#;
        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "SIM1:".to_string());
        macros.insert("R".to_string(), "Over1:1:".to_string());
        macros.insert("XPOS".to_string(), "SIM1:ROI1:MinX_RBV".to_string());
        let records = parse_db(input, &macros).unwrap();
        assert_eq!(records[0].fields[0].value, "SIM1:ROI1:MinX_RBV CP MS");
    }

    #[test]
    fn test_macro_with_quoted_default_unset() {
        // When XPOS is not set, $(XPOS="") should expand to "" (literal quotes)
        let input = r#"
    record(longout, "TEST:Link") {
    field(DOL, "$(XPOS="") CP MS")
    }
    "#;
        let macros = HashMap::new();
        let records = parse_db(input, &macros).unwrap();
        // With undefined macro and default="", the field gets the raw default
        assert!(records[0].fields[0].value.as_str_lossy().contains("CP MS"));
    }

    #[test]
    fn test_recursive_macro_default() {
        // $(TS_PORT=$(PORT)_TS) with PORT=ATTR1 → ATTR1_TS
        let input = r#"
    record(stringin, "TEST") {
    field(VAL, "$(TS_PORT=$(PORT)_TS)")
    }
    "#;
        let mut macros = HashMap::new();
        macros.insert("PORT".to_string(), "ATTR1".to_string());
        let records = parse_db(input, &macros).unwrap();
        assert_eq!(records[0].fields[0].value, "ATTR1_TS");
    }

    #[test]
    fn test_substitute_directive_in_expand() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        // Create a simple child template
        let child = dir.path().join("child.db");
        let mut f = std::fs::File::create(&child).unwrap();
        writeln!(f, r#"record(ai, "$(P)$(R)Val") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "$(ADDR)")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        // Create parent with substitute + include
        let parent = dir.path().join("parent.db");
        let mut f = std::fs::File::create(&parent).unwrap();
        writeln!(f, r#"substitute "R=A:,ADDR=0""#).unwrap();
        writeln!(f, r#"include "child.db""#).unwrap();
        writeln!(f, r#"substitute "R=B:,ADDR=1""#).unwrap();
        writeln!(f, r#"include "child.db""#).unwrap();

        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "IOC:".to_string());
        // C `dbOpenFile` searches the path list, never the including
        // file's directory, so the tempdir has to be ON the list.
        let config = DbLoadConfig {
            include_paths: vec![dir.path().to_path_buf()],
            max_include_depth: 10,
        };
        let records = parse_db_file(&parent, &macros, &config).unwrap();
        assert_eq!(records.len(), 2);
        assert_eq!(records[0].name, "IOC:A:Val");
        assert_eq!(records[0].fields[0].value, "0");
        assert_eq!(records[1].name, "IOC:B:Val");
        assert_eq!(records[1].fields[0].value, "1");
    }

    #[test]
    fn test_empty_string_numeric_parse() {
        // C EPICS treats empty VAL as 0 for numeric record types
        let input = r#"
    record(longin, "TEST:Int") {
    field(VAL, "")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        // Should parse without error — empty string → 0
        assert_eq!(records.len(), 1);
    }

    #[test]
    fn test_calcout_process() {
        use crate::server::record::Record;
        use crate::server::records::calcout::CalcoutRecord;

        let mut rec = CalcoutRecord::default();
        rec.put_field("CALC", EpicsValue::String("A+B".into()))
            .unwrap();
        // C compiles CALC in special(SPC_CALC), never in the field write —
        // dbPut always runs both, so drive the same pair here.
        rec.special("CALC", true).unwrap();
        rec.put_field("A", EpicsValue::Double(3.0)).unwrap();
        rec.put_field("B", EpicsValue::Double(4.0)).unwrap();
        rec.process().unwrap();
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 7.0).abs() < 1e-10),
            other => panic!("expected Double(7.0), got {:?}", other),
        }
    }

    #[test]
    fn test_calcout_oopt() {
        use crate::server::record::Record;
        use crate::server::records::calcout::CalcoutRecord;

        let mut rec = CalcoutRecord::default();
        rec.put_field("CALC", EpicsValue::String("A".into()))
            .unwrap();
        rec.special("CALC", true).unwrap();
        rec.put_field("OOPT", EpicsValue::Short(1)).unwrap(); // On Change
        rec.put_field("A", EpicsValue::Double(5.0)).unwrap();

        // First process — value changes from 0 to 5
        rec.process().unwrap();
        assert!((rec.oval - 5.0).abs() < 1e-10);

        // Second process — same value, OVAL should not update (but val still computes)
        rec.process().unwrap();
        // OVAL is still 5.0 since val didn't change
    }

    #[test]
    fn test_calcout_dopt() {
        use crate::server::record::Record;
        use crate::server::records::calcout::CalcoutRecord;

        let mut rec = CalcoutRecord::default();
        rec.put_field("CALC", EpicsValue::String("A+B".into()))
            .unwrap();
        // C compiles CALC in special(SPC_CALC), never in the field write —
        // dbPut always runs both, so drive the same pair here.
        rec.special("CALC", true).unwrap();
        rec.put_field("OCAL", EpicsValue::String("A*B".into()))
            .unwrap();
        rec.special("OCAL", true).unwrap();
        rec.put_field("DOPT", EpicsValue::Short(1)).unwrap(); // Use OCAL
        rec.put_field("A", EpicsValue::Double(3.0)).unwrap();
        rec.put_field("B", EpicsValue::Double(4.0)).unwrap();
        rec.process().unwrap();

        // VAL = A+B = 7, OVAL = A*B = 12
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 7.0).abs() < 1e-10),
            other => panic!("expected Double(7.0), got {:?}", other),
        }
        match rec.get_field("OVAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 12.0).abs() < 1e-10),
            other => panic!("expected Double(12.0), got {:?}", other),
        }
    }

    #[test]
    fn test_dfanout_basic() {
        use crate::server::record::Record;
        use crate::server::records::dfanout::DfanoutRecord;

        let mut rec = DfanoutRecord::default();
        rec.put_field("VAL", EpicsValue::Double(42.0)).unwrap();
        assert_eq!(rec.record_type(), "dfanout");
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 42.0).abs() < 1e-10),
            other => panic!("expected Double(42.0), got {:?}", other),
        }
    }

    #[test]
    fn test_dfanout_output_links() {
        use crate::server::record::Record;
        use crate::server::records::dfanout::DfanoutRecord;

        let mut rec = DfanoutRecord::default();
        rec.put_field("OUTA", EpicsValue::String("REC_A".into()))
            .unwrap();
        rec.put_field("OUTB", EpicsValue::String("REC_B".into()))
            .unwrap();
        let links = rec.output_links();
        assert_eq!(links.len(), 2);
    }

    #[test]
    fn test_compress_circular_buffer() {
        use crate::server::record::Record;
        use crate::server::records::compress::CompressRecord;

        let mut rec = CompressRecord::new(5, 4); // nsam=5, alg=Circular Buffer
        for i in 0..7 {
            rec.push_value(i as f64);
        }
        // C `get_array_info` linearises FIFO oldest→newest. After
        // 7 pushes to nsam=5: nuse saturates at 5, the last 5 values
        // are [2, 3, 4, 5, 6].
        match rec.get_field("VAL") {
            Some(EpicsValue::DoubleArray(arr)) => {
                assert_eq!(arr, vec![2.0, 3.0, 4.0, 5.0, 6.0]);
            }
            other => panic!("expected DoubleArray, got {:?}", other),
        }
    }

    #[test]
    fn test_compress_n_to_1_mean() {
        use crate::server::record::Record;
        use crate::server::records::compress::CompressRecord;

        let mut rec = CompressRecord::new(10, 2); // alg=Mean
        rec.put_field("N", EpicsValue::Long(3)).unwrap();
        rec.push_value(3.0);
        rec.push_value(6.0);
        rec.push_value(9.0); // mean = 6.0
        match rec.get_field("VAL") {
            Some(EpicsValue::DoubleArray(arr)) => {
                assert!((arr[0] - 6.0).abs() < 1e-10);
            }
            other => panic!("expected DoubleArray, got {:?}", other),
        }
    }

    #[test]
    fn test_histogram_bucket_count() {
        use crate::server::records::histogram::HistogramRecord;

        let mut rec = HistogramRecord::new(10, 0.0, 10.0);
        rec.add_sample(2.5); // bucket 2
        rec.add_sample(2.7); // bucket 2
        // C `histogramRecord.c:340-345` selects the bucket with a
        // closed upper edge (`temp <= i*wdth`): a value exactly on a
        // boundary lands in the LOWER bucket. sgnl=7.0, wdth=1.0 ->
        // i=7 is the first `7.0 <= i*1.0`, dest = i-1 = bucket 6.
        rec.add_sample(7.0); // boundary value -> bucket 6 (C parity)
        assert_eq!(rec.val[2], 2);
        assert_eq!(rec.val[6], 1);
    }

    #[test]
    fn test_histogram_out_of_range() {
        use crate::server::records::histogram::HistogramRecord;

        let mut rec = HistogramRecord::new(10, 0.0, 10.0);
        rec.add_sample(-1.0); // below range
        rec.add_sample(10.0); // at upper limit (excluded)
        rec.add_sample(15.0); // above range
        let total: u32 = rec.val.iter().sum();
        assert_eq!(total, 0);
    }

    #[test]
    fn test_sel_specified() {
        use crate::server::record::Record;
        use crate::server::records::sel::SelRecord;

        let mut rec = SelRecord::default();
        rec.put_field("SELM", EpicsValue::Short(0)).unwrap(); // Specified
        rec.put_field("SELN", EpicsValue::Short(2)).unwrap(); // Select C
        rec.put_field("C", EpicsValue::Double(99.0)).unwrap();
        rec.process().unwrap();
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 99.0).abs() < 1e-10),
            other => panic!("expected Double(99.0), got {:?}", other),
        }
    }

    #[test]
    fn test_sel_high_low_median() {
        use crate::server::record::Record;
        use crate::server::records::sel::SelRecord;

        let mut rec = SelRecord::default();
        rec.put_field("A", EpicsValue::Double(10.0)).unwrap();
        rec.put_field("B", EpicsValue::Double(30.0)).unwrap();
        rec.put_field("C", EpicsValue::Double(20.0)).unwrap();

        // High
        rec.put_field("SELM", EpicsValue::Short(1)).unwrap();
        rec.process().unwrap();
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 30.0).abs() < 1e-10),
            other => panic!("expected Double(30.0), got {:?}", other),
        }

        // Low
        rec.put_field("SELM", EpicsValue::Short(2)).unwrap();
        rec.process().unwrap();
        match rec.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 10.0).abs() < 1e-10), // min of finite values (A=10,B=30,C=20)
            other => panic!("expected near 0.0, got {:?}", other),
        }
    }

    #[test]
    fn db_load_menu_labels_resolve_against_field_menu() {
        // A DBF_MENU field's `.db` value resolves against THAT field's own
        // menu (C dbStaticLib dbPutStringNum: label-first, then a numeric
        // index), not a cross-menu global table. Before the fix, the loader
        // routed every menu label through one global table that mis-mapped
        // menu-specific labels and dropped labels it did not carry.
        use crate::server::record::Record;

        let apply = |rt: &str, field: &str, value: &str| -> CaResult<Box<dyn Record>> {
            let mut rec = create_record(rt).unwrap();
            let mut common = Vec::new();
            apply_fields(&mut rec, &[DbFieldDef::new(field, value)], &mut common)?;
            Ok(rec)
        };

        // sel.SELM (record-specific menu selSELM): "Specified" is index 0.
        // The old global table returned 1 (from menuFanout's "Specified").
        let rec = apply("sel", "SELM", "Specified").unwrap();
        assert_eq!(rec.get_field("SELM"), Some(EpicsValue::Enum(0)));

        // A later choice proves the whole menu, not just index 0.
        let rec = apply("sel", "SELM", "High Signal").unwrap();
        assert_eq!(rec.get_field("SELM"), Some(EpicsValue::Enum(1)));

        // A bare numeric index still resolves (C epicsParseUInt16 fallback).
        let rec = apply("sel", "SELM", "2").unwrap();
        assert_eq!(rec.get_field("SELM"), Some(EpicsValue::Enum(2)));

        // ai.LINR (shared menu menuConvert, SHORT-typed): "LINEAR" is index 2.
        // The old global table lacked menuConvert, so the load errored.
        let rec = apply("ai", "LINR", "LINEAR").unwrap();
        assert_eq!(rec.get_field("LINR"), Some(EpicsValue::Short(2)));

        // An unknown choice errors (C S_db_badChoice), never a silent mis-map.
        assert!(apply("sel", "SELM", "Bogus").is_err());
    }

    #[test]
    fn test_parse_breaktable_basic() {
        let input = r#"
breaktable(typeJdegC) {
    0    0
    365  67.0
    1000 178.0
}
record(ai, "T") { field(LINR, "typeJdegC") }
"#;
        let parsed = parse_db_with_breaktables(input, &HashMap::new()).unwrap();
        let breaktables = parsed.breaktables;
        assert_eq!(parsed.records.len(), 1);
        assert_eq!(breaktables.len(), 1);
        let t = &breaktables[0];
        assert_eq!(t.name, "typeJdegC");
        assert_eq!(t.points.len(), 3);
        assert_eq!(t.points[0].raw, 0.0);
        assert_eq!(t.points[0].eng, 0.0);
        // slope[0] = (67-0)/(365-0).
        assert!((t.points[0].slope - (67.0 / 365.0)).abs() < 1e-12);
    }

    #[test]
    fn test_parse_breaktable_quoted_name_and_commas() {
        let input = r#"breaktable("tbl") { 0,0, 10,100 }"#;
        let breaktables = parse_db_with_breaktables(input, &HashMap::new())
            .unwrap()
            .breaktables;
        assert_eq!(breaktables.len(), 1);
        assert_eq!(breaktables[0].name, "tbl");
        assert_eq!(breaktables[0].points.len(), 2);
        assert!((breaktables[0].points[0].slope - 10.0).abs() < 1e-12);
    }

    #[test]
    fn test_parse_breaktable_odd_count_errors() {
        let input = r#"breaktable(bad) { 0 0 10 }"#;
        let Err(err) = parse_db_with_breaktables(input, &HashMap::new()) else {
            panic!("odd point count must be a parse error");
        };
        match err {
            CaError::DbParseError { message, .. } => {
                assert!(message.contains("Raw value missing"), "{message}")
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn test_resolve_linr_breaktable_names_rewrites_to_index() {
        use crate::server::cvt_bpt::{BreakTableRegistry, BrkTable};
        let mut reg = BreakTableRegistry::new();
        reg.insert(BrkTable::build("alpha", &[(0.0, 0.0), (1.0, 1.0)]).unwrap());

        // A registered non-standard table name on an ai/ao LINR is rewritten to
        // its index — the first user-table slot (15), since the standard
        // menuConvert names reserve 3..=14.
        let mut fields = vec![DbFieldDef::new("LINR", PvString::from("alpha"))];
        resolve_linr_breaktable_names("ai", &mut fields, &reg);
        assert_eq!(fields[0].value, "15");

        // A fixed menuConvert label matches no table name and is untouched.
        let mut fixed = vec![DbFieldDef::new("LINR", PvString::from("LINEAR"))];
        resolve_linr_breaktable_names("ai", &mut fixed, &reg);
        assert_eq!(fixed[0].value, "LINEAR");

        // A non-ai/ao record's field is never rewritten.
        let mut other = vec![DbFieldDef::new("LINR", PvString::from("alpha"))];
        resolve_linr_breaktable_names("bo", &mut other, &reg);
        assert_eq!(other[0].value, "alpha");
    }

    #[test]
    fn test_sub_record_register_and_call() {
        use crate::server::record::{Record, RecordInstance, SubroutineFn};
        use crate::server::records::sub_record::SubRecord;
        use std::sync::Arc;

        let mut rec = SubRecord::default();
        rec.put_field("SNAM", EpicsValue::String("double_val".into()))
            .unwrap();
        rec.put_field("VAL", EpicsValue::Double(5.0)).unwrap();

        let mut instance = RecordInstance::new("TEST_SUB".into(), rec);
        let sub_fn: SubroutineFn = Box::new(|record: &mut dyn Record| {
            if let Some(EpicsValue::Double(v)) = record.get_field("VAL") {
                record.put_field("VAL", EpicsValue::Double(v * 2.0))?;
            }
            Ok(0)
        });
        instance.subroutine = Some(Arc::new(sub_fn));

        instance.process_local().unwrap();

        match instance.record.get_field("VAL") {
            Some(EpicsValue::Double(v)) => assert!((v - 10.0).abs() < 1e-10),
            other => panic!("expected Double(10.0), got {:?}", other),
        }
    }

    #[test]
    fn test_new_record_types_in_db() {
        let input = r#"
    record(calcout, "TEST_CO") {
    field(CALC, "A+1")
    }
    record(dfanout, "TEST_DF") {
    field(VAL, "5.0")
    }
    record(compress, "TEST_CMP") {
    field(DESC, "test compress")
    }
    record(histogram, "TEST_HIST") {
    field(DESC, "test hist")
    }
    record(sel, "TEST_SEL") {
    field(SELM, "0")
    }
    record(sub, "TEST_SUB") {
    field(SNAM, "my_sub")
    }
    "#;
        let records = parse_db(input, &HashMap::new()).unwrap();
        assert_eq!(records.len(), 6);
        // Verify they can all be created
        for def in &records {
            create_record(&def.record_type).unwrap();
        }
    }

    // ===== include / parse_db_file tests =====

    #[test]
    fn test_parse_include_directive() {
        // Normal include
        assert_eq!(
            parse_include_directive(r#"include "foo.template""#),
            Some("foo.template".to_string())
        );
        // With leading whitespace
        assert_eq!(
            parse_include_directive(r#"  include "bar.db""#),
            Some("bar.db".to_string())
        );
        // With trailing comment
        assert_eq!(
            parse_include_directive(r#"include "baz.template" # a comment"#),
            Some("baz.template".to_string())
        );
        // No quote — not an include
        assert_eq!(parse_include_directive("include something"), None);
        // Comment line
        assert_eq!(parse_include_directive(r#"# include "ignored.db""#), None);
        // Not an include keyword
        assert_eq!(parse_include_directive("record(ai, \"X\") {"), None);
        // "includes" is not "include"
        assert_eq!(parse_include_directive(r#"includes "nope.db""#), None);
    }

    #[test]
    fn test_commented_include_ignored() {
        assert_eq!(parse_include_directive(r#"# include "file.db""#), None);
        assert_eq!(parse_include_directive(r#"  # include "file.db""#), None);
    }

    #[test]
    fn test_expand_includes() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        // Create child.db
        let child_path = dir.path().join("child.db");
        let mut f = std::fs::File::create(&child_path).unwrap();
        writeln!(f, r#"record(ai, "CHILD") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "1.0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        // Create parent.db that includes child.db
        let parent_path = dir.path().join("parent.db");
        let mut f = std::fs::File::create(&parent_path).unwrap();
        writeln!(f, r#"record(ao, "PARENT") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "2.0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();
        writeln!(f, r#"include "child.db""#).unwrap();

        let config = DbLoadConfig {
            include_paths: vec![dir.path().to_path_buf()],
            max_include_depth: 32,
        };
        let result = expand_includes(
            &parent_path,
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        )
        .unwrap();
        assert!(result.contains(r#"record(ao, "PARENT")"#));
        assert!(result.contains(r#"record(ai, "CHILD")"#));

        // Verify it parses correctly
        let records = parse_db(&result, &HashMap::new()).unwrap();
        assert_eq!(records.len(), 2);
    }

    #[test]
    fn test_circular_include_error() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        let a_path = dir.path().join("a.template");
        let b_path = dir.path().join("b.template");

        let mut fa = std::fs::File::create(&a_path).unwrap();
        writeln!(fa, r#"include "b.template""#).unwrap();

        let mut fb = std::fs::File::create(&b_path).unwrap();
        writeln!(fb, r#"include "a.template""#).unwrap();

        let config = DbLoadConfig {
            include_paths: vec![dir.path().to_path_buf()],
            max_include_depth: 32,
        };
        let result = expand_includes(&a_path, &HashMap::new(), &config, &mut DbFaults::default());
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("circular include"), "error was: {err}");
    }

    #[test]
    fn test_duplicate_include_allowed() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        let shared_path = dir.path().join("shared.db");
        let mut f = std::fs::File::create(&shared_path).unwrap();
        writeln!(f, r#"record(ai, "SHARED") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        // main.db includes shared.db twice (not circular, just duplicate)
        let main_path = dir.path().join("main.db");
        let mut f = std::fs::File::create(&main_path).unwrap();
        writeln!(f, r#"include "shared.db""#).unwrap();
        writeln!(f, r#"include "shared.db""#).unwrap();

        let config = DbLoadConfig {
            include_paths: vec![dir.path().to_path_buf()],
            max_include_depth: 32,
        };
        let result = expand_includes(
            &main_path,
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        )
        .unwrap();
        // shared.db content appears twice
        assert_eq!(result.matches(r#"record(ai, "SHARED")"#).count(), 2);
    }

    #[test]
    fn test_include_depth_limit() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        // Create a chain: file0 -> file1 -> file2 -> ... -> file33
        for i in 0..34 {
            let path = dir.path().join(format!("file{i}.db"));
            let mut f = std::fs::File::create(&path).unwrap();
            if i < 33 {
                writeln!(f, r#"include "file{}.db""#, i + 1).unwrap();
            } else {
                writeln!(f, r#"record(ai, "DEEP") {{"#).unwrap();
                writeln!(f, r#"    field(VAL, "0")"#).unwrap();
                writeln!(f, r#"}}"#).unwrap();
            }
        }

        let config = DbLoadConfig {
            include_paths: vec![dir.path().to_path_buf()],
            max_include_depth: 32,
        };
        let result = expand_includes(
            &dir.path().join("file0.db"),
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        );
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("depth limit"), "error was: {err}");
    }

    /// R5-3: C `dbIncludeNew` (`dbLexRoutines.c:450-456`) reports an
    /// unopenable include with `yyerror(NULL)`, so the include is skipped
    /// and the rest of the file is still read. This test used to assert
    /// the opposite — that the whole expansion failed.
    #[test]
    fn test_include_not_found_is_recoverable() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        let path = dir.path().join("main.db");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, r#"record(ai, "SIM:A") {{ field(VAL, "1") }}"#).unwrap();
        writeln!(f, r#"include "nonexistent.db""#).unwrap();
        writeln!(f, r#"record(ai, "SIM:B") {{ field(VAL, "2") }}"#).unwrap();

        let config = DbLoadConfig::default();
        let mut faults = DbFaults::default();
        let text = expand_includes(&path, &HashMap::new(), &config, &mut faults)
            .expect("a missing include must not fail the expansion");
        assert!(text.contains("SIM:A") && text.contains("SIM:B"));
        let err = faults
            .first_diagnostic()
            .expect("the load's status must go non-zero");
        assert_eq!(
            err,
            format!("{ERL_ERROR}: Can't open include file 'nonexistent.db'")
        );
    }

    #[test]
    fn test_include_with_macro_filename() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("sub");
        std::fs::create_dir(&subdir).unwrap();

        let child_path = subdir.join("child.db");
        let mut f = std::fs::File::create(&child_path).unwrap();
        writeln!(f, r#"record(ai, "CHILD") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        let main_path = dir.path().join("main.db");
        let mut f = std::fs::File::create(&main_path).unwrap();
        writeln!(f, r#"include "$(DIR)/child.db""#).unwrap();

        let mut macros = HashMap::new();
        macros.insert("DIR".to_string(), macro_safe_path(&subdir));

        let config = DbLoadConfig::default();
        let result =
            expand_includes(&main_path, &macros, &config, &mut DbFaults::default()).unwrap();
        assert!(result.contains(r#"record(ai, "CHILD")"#));
    }

    #[test]
    fn test_include_search_order() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let inc_dir = dir.path().join("inc");
        std::fs::create_dir(&inc_dir).unwrap();

        // Put file in include path only (not in current dir)
        let child_path = inc_dir.join("child.db");
        let mut f = std::fs::File::create(&child_path).unwrap();
        writeln!(f, r#"record(ai, "FROM_INC") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        let main_path = dir.path().join("main.db");
        let mut f = std::fs::File::create(&main_path).unwrap();
        writeln!(f, r#"include "child.db""#).unwrap();

        let config = DbLoadConfig {
            include_paths: vec![inc_dir.clone()],
            max_include_depth: 32,
        };
        let result = expand_includes(
            &main_path,
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        )
        .unwrap();
        assert!(result.contains(r#"record(ai, "FROM_INC")"#));

        // I-R3-3: a same-named file next to the INCLUDING file must not
        // win. C `dbIncludeNew` (`dbLexRoutines.c:450`) goes through
        // `dbOpenFile`, which walks the path list and never looks at the
        // parent file's directory or the process CWD.
        let local_child = dir.path().join("child.db");
        let mut f = std::fs::File::create(&local_child).unwrap();
        writeln!(f, r#"record(ai, "FROM_LOCAL") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        let result = expand_includes(
            &main_path,
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        )
        .unwrap();
        assert!(result.contains(r#"record(ai, "FROM_INC")"#));
        assert!(!result.contains(r#"record(ai, "FROM_LOCAL")"#));

        // …and a name that already carries a separator bypasses the list
        // entirely, exactly as C's `strchr(filename, '/')` gate does.
        let sep_main = dir.path().join("sep_main.db");
        let mut f = std::fs::File::create(&sep_main).unwrap();
        writeln!(f, r#"include "{}""#, local_child.display()).unwrap();
        let result = expand_includes(
            &sep_main,
            &HashMap::new(),
            &config,
            &mut DbFaults::default(),
        )
        .unwrap();
        assert!(result.contains(r#"record(ai, "FROM_LOCAL")"#));
    }

    #[test]
    fn test_addpath_directive_resolves_include() {
        // L-3: an `addpath` directive inside a .db file mutates the
        // include search path for subsequent `include` directives.
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let inc_dir = dir.path().join("extra");
        std::fs::create_dir(&inc_dir).unwrap();

        // child.db lives ONLY in the extra/ dir.
        let child = inc_dir.join("child.db");
        let mut f = std::fs::File::create(&child).unwrap();
        writeln!(f, r#"record(ai, "FROM_ADDPATH") {{ field(VAL, "0") }}"#).unwrap();

        let main = dir.path().join("main.db");
        let mut f = std::fs::File::create(&main).unwrap();
        writeln!(f, r#"addpath "{}""#, inc_dir.display()).unwrap();
        writeln!(f, r#"include "child.db""#).unwrap();

        // No include path in config — only the addpath directive can
        // make this resolve.
        let config = DbLoadConfig::default();
        let result =
            expand_includes(&main, &HashMap::new(), &config, &mut DbFaults::default()).unwrap();
        assert!(result.contains(r#"record(ai, "FROM_ADDPATH")"#));
    }

    #[test]
    fn test_path_directive_replaces_search_path() {
        // L-3: `path` replaces the search path entirely.
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let inc_dir = dir.path().join("p");
        std::fs::create_dir(&inc_dir).unwrap();

        let child = inc_dir.join("c.db");
        let mut f = std::fs::File::create(&child).unwrap();
        writeln!(f, r#"record(ai, "VIA_PATH") {{ field(VAL, "0") }}"#).unwrap();

        let main = dir.path().join("main.db");
        let mut f = std::fs::File::create(&main).unwrap();
        writeln!(f, r#"path "{}""#, inc_dir.display()).unwrap();
        writeln!(f, r#"include "c.db""#).unwrap();

        let config = DbLoadConfig::default();
        let result =
            expand_includes(&main, &HashMap::new(), &config, &mut DbFaults::default()).unwrap();
        assert!(result.contains(r#"record(ai, "VIA_PATH")"#));
    }

    /// A `DTYP=` macro is pure text substitution, exactly like every other
    /// macro: it reaches a record only where the `.db` wrote
    /// `field(DTYP,"$(DTYP)")`. It must NOT rewrite a record that spells its
    /// DTYP literally.
    ///
    /// This replaces `test_dtyp_override_existing_only`, which pinned the
    /// opposite (force-override) behaviour. C `dbLoadRecords` runs its macros
    /// through macLib during lexing (`dbLexRoutines.c`); there is no DTYP
    /// special case, and a force-override corrupts any multi-record file whose
    /// helper records carry a literal DTYP — e.g. the vendored
    /// `scaler-rs/db/scaler.db`, where two `bo` helpers are
    /// `field(DTYP,"Soft Channel")` and only the `scaler` record references
    /// `$(DTYP)`.
    #[test]
    fn dtyp_macro_substitutes_references_and_leaves_literals_alone() {
        let input = r#"
record(bo, "$(P)_calcEnable") {
    field(DTYP, "Soft Channel")
    field(ZNAM, "ENABLE")
}
record(scaler, "$(P)") {
    field(DTYP, "$(DTYP)")
    field(FREQ, "10000000")
}
record(ao, "$(P)_noDtyp") {
    field(VAL, "1")
}
"#;
        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "SCALER1".to_string());
        macros.insert("DTYP".to_string(), "Scaler-rs".to_string());

        let records = parse_db(input, &macros).unwrap();
        assert_eq!(records.len(), 3);

        let dtyp_of = |rec: &DbRecordDef| -> Option<String> {
            rec.fields
                .iter()
                .find(|f| f.name == "DTYP")
                .map(|f| f.value.as_str_lossy().into_owned())
        };

        // Literal DTYP: untouched by the macro. Force-override used to corrupt
        // this into "Scaler-rs", breaking the soft helper records.
        assert_eq!(dtyp_of(&records[0]).as_deref(), Some("Soft Channel"));
        // $(DTYP) reference: substituted.
        assert_eq!(dtyp_of(&records[1]).as_deref(), Some("Scaler-rs"));
        // No DTYP field: none added.
        assert_eq!(dtyp_of(&records[2]), None);
    }

    #[test]
    fn test_parse_db_file_no_includes() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();

        let path = dir.path().join("simple.db");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, r#"record(ai, "$(P)TEMP") {{"#).unwrap();
        writeln!(f, r#"    field(VAL, "25.0")"#).unwrap();
        writeln!(f, r#"}}"#).unwrap();

        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "IOC:".to_string());

        let config = DbLoadConfig::default();
        let records = parse_db_file(&path, &macros, &config).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].name, "IOC:TEMP");
    }

    // epics-base PR #78 — record-name validation regressions.

    #[test]
    fn name_validation_accepts_typical_names() {
        for n in [
            "IOC:TEMP",
            "MOTOR-1",
            "X[1]",
            "scan_5",
            "BL3:STD:01",
            "abc.xyz_record",
        ]
        .iter()
        .copied()
        // ".xyz" inside the bracket-allowed shape would actually fail
        // — the slash above is just to demonstrate the OK/FAIL split.
        {
            // Skip the deliberately-bad sample.
            if n.contains('.') {
                assert!(validate_record_name(n, 1, 1).is_err());
                continue;
            }
            validate_record_name(n, 1, 1).unwrap_or_else(|e| panic!("'{n}' should pass: {e:?}"));
        }
    }

    #[test]
    fn name_validation_rejects_empty() {
        assert!(validate_record_name("", 1, 1).is_err());
    }

    #[test]
    fn name_validation_rejects_bad_chars() {
        // Mirrors base: TAB (0x09) is non-printable so it falls into the
        // warn-and-continue branch, NOT the hard-error set. Only the
        // printable bad chars below produce a hard error.
        for bad in ["spa ce", "do.t", "qu\"ot", "ap'os", "do$llar"] {
            assert!(
                validate_record_name(bad, 1, 1).is_err(),
                "'{bad}' must be rejected"
            );
        }
    }

    #[test]
    fn name_validation_warns_but_passes_on_nonprintable() {
        // 0x09 (TAB) and 0x01 are < 0x20 so they only emit a warning.
        validate_record_name("ta\tb", 1, 1).expect("TAB is warn-only per base spec");
        validate_record_name("hello\x01world", 1, 1).expect("0x01 is warn-only");
    }

    #[test]
    fn name_validation_warns_on_leading_special_but_passes() {
        for warn in ["-x", "+y", "[arr", "{obj"] {
            validate_record_name(warn, 1, 1).expect("leading special is warn-only");
        }
    }

    /// R5-1: a bare `[...]` array constant is a `json_value`
    /// (`dbYacc.y:328-337`), so C loads this record. The port had no `[`
    /// branch in `read_field_value`, consumed `[1` as a bareword and
    /// stopped at the comma, so the whole file parsed to nothing. Both
    /// lines are transcribed from base's own test databases —
    /// `modules/database/test/std/rec/arrayOpTest.db:4` and
    /// `linkInitTest.db:15`.
    #[test]
    fn parse_db_accepts_a_bare_array_constant() {
        let src = r#"
record(waveform, "wfrec") {
    field(NELM, "10")
    field(FTVL, "LONG")
    field(INP, [1, 2, 3])
}
record(lsi, "longstr4") {
  field(SIZV, "100")
  field(INP, ["One","Two","Three","Four"])
}
"#;
        let recs = parse_db(src, &HashMap::new()).expect("base's own test database must load");
        assert_eq!(recs.len(), 2);
        let inp = |r: &DbRecordDef| {
            r.fields
                .iter()
                .find(|f| f.name == "INP")
                .map(|f| f.value.as_str_lossy().into_owned())
                .unwrap()
        };
        assert_eq!(inp(&recs[0]), "[1, 2, 3]");
        assert_eq!(inp(&recs[1]), r#"["One","Two","Three","Four"]"#);
    }

    #[test]
    fn parse_db_propagates_name_validation_error() {
        let bad = r#"record(ai, "BAD NAME") { }"#;
        let res = parse_db(bad, &HashMap::new());
        assert!(matches!(res, Err(CaError::DbParseError { .. })));
    }

    // epics-base PR #336 — alias parsing + name validation.

    #[test]
    fn parse_db_captures_aliases() {
        let src = r#"record(ai, "TARGET") {
            alias("ALIAS1")
            alias("ALIAS2")
            field(VAL, 42)
        }"#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].name, "TARGET");
        assert_eq!(recs[0].aliases, vec!["ALIAS1", "ALIAS2"]);
        assert_eq!(recs[0].fields.len(), 1);
    }

    #[test]
    fn parse_db_rejects_alias_with_bad_name() {
        let src = r#"record(ai, "TARGET") {
            alias("BAD ALIAS")
        }"#;
        let res = parse_db(src, &HashMap::new());
        assert!(matches!(res, Err(CaError::DbParseError { .. })));
    }

    // L-2 — top-level directive grammar coverage.

    #[test]
    fn parse_db_accepts_path_and_addpath() {
        // `path`/`addpath` at file scope are accepted and skipped —
        // include resolution is the expansion layer's job.
        let src = r#"
            path "/opt/epics/db"
            addpath "/extra/db"
            record(ai, "REC") { field(VAL, "1") }
        "#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].name, "REC");
    }

    #[test]
    fn parse_db_accepts_top_level_include() {
        // A bare `include` at file scope is accepted (grammar parity);
        // the file is not loaded at this layer.
        let src = r#"
            include "common.db"
            record(ai, "REC") { field(VAL, "1") }
        "#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
    }

    #[test]
    fn parse_db_global_alias_two_arg() {
        // Standalone `alias("record","newname")` attaches the new name
        // to the target record's alias list.
        let src = r#"
            record(ai, "TARGET") { field(VAL, "1") }
            alias("TARGET", "TARGET_ALIAS")
        "#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].aliases, vec!["TARGET_ALIAS"]);
    }

    #[test]
    fn parse_db_global_alias_forward_reference() {
        // The alias directive may precede its target record.
        let src = r#"
            alias("TARGET", "EARLY_ALIAS")
            record(ai, "TARGET") { field(VAL, "1") }
        "#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs[0].aliases, vec!["EARLY_ALIAS"]);
    }

    /// I-R3-5: C `dbYacc.y:237-241` puts `record_body: /* empty */`
    /// FIRST, ahead of `'{' '}'` and `'{' record_field_list '}'`, and
    /// `tokenRECORD` / `tokenGRECORD` (`:60-61`) share it — so a
    /// bodyless declaration is an all-defaults record. The port
    /// demanded `{` and the rejection discarded every record in the
    /// file.
    #[test]
    fn parse_db_record_without_a_body_is_valid() {
        let src = r#"
record(ai, "TEMP")
record(ai, "PRESS") { field(SCAN, "1 second") }
grecord(ai, "GTEMP")
record(ai, "EMPTY") { }
"#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        let names: Vec<&str> = recs.iter().map(|r| r.name.as_str()).collect();
        assert_eq!(names, vec!["TEMP", "PRESS", "GTEMP", "EMPTY"]);
        assert!(recs[0].fields.is_empty(), "bodyless record takes defaults");
        assert_eq!(recs[1].fields.len(), 1);
        assert!(recs[2].fields.is_empty(), "grecord shares record_body");
        assert!(recs[3].fields.is_empty());

        // Boundary: the bodyless record is the last token in the file,
        // so there is nothing after the head at all.
        let recs = parse_db(r#"record(ai, "LAST")"#, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].name, "LAST");
    }

    /// I-R3-4: C `dbAlias` resolves the target against `savedPdbbase`
    /// (`dbLexRoutines.c:1508`), the accumulated database, so a target
    /// this text does not declare is not the parser's to reject — an
    /// earlier `dbLoadRecords` may own it. The parser hands it out
    /// unresolved and keeps every record it read; C's own failure path
    /// (`yyerror(NULL)`, `dbYacc.y:370-383`) also keeps them.
    #[test]
    fn parse_db_global_alias_unknown_record_is_deferred_not_rejected() {
        let src = r#"
            alias("NOSUCH", "X")
            record(ai, "KEPT") { field(VAL, "1") }
        "#;
        let parsed = parse_db_with_breaktables(src, &HashMap::new()).unwrap();
        assert_eq!(
            parsed.unresolved_aliases,
            vec![("NOSUCH".to_string(), "X".to_string())]
        );
        assert_eq!(parsed.records.len(), 1);
        assert_eq!(parsed.records[0].name, "KEPT");
        // The records-only wrapper still yields what parsed.
        assert_eq!(parse_db(src, &HashMap::new()).unwrap().len(), 1);
    }

    // L-5 — unquoted field values are restricted to the C bareword set.

    #[test]
    fn parse_db_unquoted_bareword_value_ok() {
        let src = r#"record(ai, "REC") { field(VAL, 42) field(EGU, deg-C) }"#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs[0].fields[0].value, "42");
        assert_eq!(recs[0].fields[1].value, "deg-C");
    }

    #[test]
    fn parse_db_unquoted_value_with_space_rejected() {
        // An unquoted multi-word value is two tokens in C — a parse
        // error. The author must quote it.
        let src = r#"record(ai, "REC") { field(DESC, hello world) }"#;
        let res = parse_db(src, &HashMap::new());
        assert!(
            matches!(res, Err(CaError::DbParseError { .. })),
            "unquoted value with space must be rejected, got {res:?}"
        );
    }

    #[test]
    fn parse_db_unquoted_value_with_illegal_char_rejected() {
        // `*` is outside the C bareword set.
        let src = r#"record(ai, "REC") { field(DESC, a*b) }"#;
        let res = parse_db(src, &HashMap::new());
        assert!(matches!(res, Err(CaError::DbParseError { .. })));
    }

    #[test]
    fn parse_db_record_without_alias_has_empty_aliases() {
        let src = r#"record(ai, "PLAIN") { field(VAL, 1) }"#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert!(recs[0].aliases.is_empty());
    }

    /// Regression: `info(asyn:READBACK, "1")` (unquoted tag,
    /// quoted value) is the form ad-core templates use. Base accepts
    /// it; an earlier parser fix tightened the grammar to require a
    /// quoted tag, which broke all NDOverlayN / NDFile / NDArrayBase
    /// templates and the mini-beamline/xrt-beamline IOCs that load
    /// commonPlugins.cmd.
    #[test]
    fn parse_db_info_accepts_unquoted_tag() {
        let src = r#"
record(ai, "REC") {
    field(VAL, "0")
    info(asyn:READBACK, "1")
    info("Q:group", "demo")
    info(autosaveFields, "VAL DESC")
}
"#;
        let recs = parse_db(src, &HashMap::new()).unwrap();
        assert_eq!(recs.len(), 1);
        let tags = &recs[0].info_tags;
        // Unquoted tag, quoted value.
        assert!(
            tags.iter().any(|(k, v)| k == "asyn:READBACK" && v == "1"),
            "unquoted tag must parse: {tags:?}"
        );
        // Quoted tag, quoted value (existing form).
        assert!(tags.iter().any(|(k, v)| k == "Q:group" && v == "demo"));
        // Unquoted tag, unquoted multi-word value.
        assert!(
            tags.iter()
                .any(|(k, v)| k == "autosaveFields" && v == "VAL DESC"),
            "unquoted multi-word value must parse: {tags:?}"
        );
    }

    /// C parity (modules/libcom/test/macLib.plt:52):
    ///   $(a)\$(b)  with a=foo  ->  foo\$(b)
    /// The `\` MUST block the macro-reference detection of the following
    /// `$` so `$(b)` survives as literal text. The `\` itself is
    /// preserved verbatim (macLib level 0 semantic — downstream
    /// caller-side escape passes may discard it).
    #[test]
    fn substitute_macros_backslash_escapes_dollar() {
        let mut macros = HashMap::new();
        macros.insert("a".to_string(), "foo".to_string());
        macros.insert("b".to_string(), "baz".to_string());

        // Anchor test: backslash before $ blocks expansion.
        assert_eq!(substitute_macros(r"$(a)\$(b)", &macros), r"foo\$(b)");

        // Backslash before brace form too.
        assert_eq!(substitute_macros(r"\${a}", &macros), r"\${a}");

        // Without backslash, both expand.
        assert_eq!(substitute_macros("$(a)$(b)", &macros), "foobaz");

        // Backslash escape consumes the IMMEDIATELY next char (one
        // step at a time, matching C macCore.c:741-744). So `\\$(a)`
        // emits the first `\` + second `\` literal (escape pair), then
        // resumes parsing at `$(a)` which expands. Result: `\\foo`.
        assert_eq!(substitute_macros(r"\\$(a)", &macros), r"\\foo");

        // Backslash NOT before $ / { passes through too (escape any next char).
        assert_eq!(
            substitute_macros(r"path\file $(a)", &macros),
            r"path\file foo"
        );
    }

    // Resolved macro values are re-expanded (chained).
    #[test]
    fn substitute_macros_chained_expansion() {
        // P=$(Q), Q=IOC:  →  $(P) expands to IOC:
        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "$(Q)".to_string());
        macros.insert("Q".to_string(), "IOC:".to_string());
        assert_eq!(substitute_macros("$(P)TEMP", &macros), "IOC:TEMP");
    }

    // `$(name,key=val,...)` scoped macro definitions.
    #[test]
    fn substitute_macros_scoped_definitions() {
        // INNER references A and B which are only defined for the
        // duration of this reference.
        let mut macros = HashMap::new();
        macros.insert("INNER".to_string(), "$(A)-$(B)".to_string());
        assert_eq!(substitute_macros("$(INNER,A=1,B=2)", &macros), "1-2");
    }

    /// C `macCore.c:701-703` — "discard quotes and escapes if level
    /// is > 0 (i.e. if these aren't the user's quotes and escapes)".
    /// One case per boundary of that rule, both sides of level 0,
    /// measured against `macExpandString` from libCom 3.25.1.
    #[test]
    fn quotes_and_escapes_survive_only_at_level_zero() {
        let mut macros = HashMap::new();
        macros.insert("X".to_string(), "1".to_string());
        let l0 = |src: &str| expand_macros(src, &macros, MacroExpandOptions::default()).text;
        let l1 = |raw: &str| {
            let mut m = macros.clone();
            m.insert("M".to_string(), raw.to_string());
            expand_macros("$(M)", &m, MacroExpandOptions::default()).text
        };

        for (src, at_l0, at_l1) in [
            (r#""abc""#, r#""abc""#, "abc"),
            ("'abc'", "'abc'", "abc"),
            (r"a\b", r"a\b", "ab"),
            (r"a\\b", r"a\\b", r"a\b"),
            (r#""$(X)""#, r#""1""#, "1"),
            ("'$(X)'", "'$(X)'", "$(X)"),
            ("Beam's energy", "Beam's energy", "Beams energy"),
            (r#"a"b"#, r#"a"b"#, "ab"),
            ("$(X)'s", "1's", "1s"),
        ] {
            assert_eq!(l0(src), at_l0, "level 0 keeps the user's own: {src}");
            assert_eq!(l1(src), at_l1, "level 1 discards: {src}");
        }

        // A default value is translated at `level + 1` too
        // (`macCore.c:909`), so every quote in it goes — not just a
        // surrounding pair, which is what the port used to strip.
        assert_eq!(l0("$(NOSUCH=Beam's energy)"), "Beams energy");
        assert_eq!(l0(r"$(NOSUCH=a\b)"), "ab");
        assert_eq!(l0(r#"$(NOSUCH="q")"#), "q");
    }

    /// The `.db` shape the rule changes: a field default carrying an
    /// apostrophe. C loads `Beams energy` because the default is
    /// translated below level 0; the port used to load the apostrophe.
    #[test]
    fn a_db_field_default_loses_its_apostrophe_like_c() {
        let recs = parse_db(
            "record(ai,\"X:B\") { field(DESC, \"$(BEAM=Beam's energy)\") }",
            &HashMap::new(),
        )
        .expect("record must parse");
        assert_eq!(recs.len(), 1);
        let desc = recs[0]
            .fields
            .iter()
            .find(|f| f.name == "DESC")
            .map(|f| f.value.to_string());
        assert_eq!(desc.as_deref(), Some("Beams energy"));
    }

    #[test]
    fn substitute_macros_scoped_not_leaking() {
        // A scoped macro must not leak past its reference.
        let mut macros = HashMap::new();
        macros.insert("INNER".to_string(), "$(A)".to_string());
        // After the scoped $(INNER,A=9), a bare $(A) is undefined.
        let out = substitute_macros("$(INNER,A=9)|$(A)", &macros);
        assert_eq!(out, "9|$(A,undefined)");
    }

    // Quote state resets at every newline (C expands one fgets line per
    // `macExpandString` call, dbLexRoutines.c:375-391): a quote character on
    // one line must not suppress expansion on the next, while suppression
    // WITHIN a line keeps working.
    #[test]
    fn substitute_macros_per_line_resets_quote_state_at_newline() {
        let mut macros = HashMap::new();
        macros.insert("X".to_string(), "VAL".to_string());
        // Whole-file expansion leaks the apostrophe's quote state forward…
        assert_eq!(
            substitute_macros("don't\n$(X)", &macros),
            "don't\n$(X)",
            "precondition: the whole-file engine suppresses across the newline"
        );
        // …the per-line form does not.
        assert_eq!(
            substitute_macros_per_line("don't\n$(X)", &macros),
            "don't\nVAL"
        );
        // Same-line suppression is unchanged.
        assert_eq!(
            substitute_macros_per_line("'$(X)' $(X)\n$(X)", &macros),
            "'$(X)' VAL\nVAL"
        );
    }

    // The formerly-bypassing path end to end: an apostrophe in a `#` comment
    // must not stop `$(P)` expanding on the record line below it.
    #[test]
    fn parse_db_expands_macros_after_a_comment_apostrophe() {
        let mut macros = HashMap::new();
        macros.insert("P".to_string(), "T:".to_string());
        let db = "# this comment's apostrophe must stay harmless\n\
                  record(ai, \"$(P)X\") {\n}\n";
        let records = parse_db(db, &macros).expect("the comment must not break expansion");
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].name, "T:X");
    }

    // Macros are NOT expanded inside single quotes.
    #[test]
    fn substitute_macros_suppressed_in_single_quotes() {
        let mut macros = HashMap::new();
        macros.insert("X".to_string(), "VAL".to_string());
        // Single quotes suppress.
        assert_eq!(substitute_macros("'$(X)'", &macros), "'$(X)'");
        // Double quotes do NOT suppress.
        assert_eq!(substitute_macros("\"$(X)\"", &macros), "\"VAL\"");
    }

    // The reference name is macro-expanded before lookup.
    #[test]
    fn substitute_macros_indirect_name() {
        // $($(WHICH)) — WHICH selects which macro to read.
        let mut macros = HashMap::new();
        macros.insert("WHICH".to_string(), "SEL".to_string());
        macros.insert("SEL".to_string(), "chosen".to_string());
        assert_eq!(substitute_macros("$($(WHICH))", &macros), "chosen");
    }

    // L-4 — undefined macro with no default emits `$(name,undefined)`.
    #[test]
    fn substitute_macros_undefined_placeholder() {
        let macros = HashMap::new();
        assert_eq!(
            substitute_macros("$(MISSING)", &macros),
            "$(MISSING,undefined)"
        );
    }

    #[test]
    fn substitute_macros_default_with_comma_is_c_parity() {
        // C parity: `$(LIST=a,b,c)` — the name is LIST, the
        // default is `a` (terminates at the first top-level comma),
        // and `b`/`c` are bare scoped names that define nothing.
        let macros = HashMap::new();
        assert_eq!(substitute_macros("$(LIST=a,b,c)", &macros), "a");
    }

    #[test]
    fn substitute_macros_self_reference_terminates() {
        // Re-expansion must not recurse forever on `A=$(A)`.
        // The guard REFUSES the reference rather than resolving it once:
        // the text is C's `$(A,recursive)` placeholder and a `macLib:
        // macro A is recursive` notice goes out with it
        // (`macCore.c:895-904`).
        let mut macros = HashMap::new();
        macros.insert("A".to_string(), "$(A)".to_string());
        assert_eq!(substitute_macros("$(A)", &macros), "$(A,recursive)");
    }

    #[test]
    fn substitute_macros_mutual_reference_terminates() {
        // A=$(B), B=$(A) — a mutual cycle must also terminate, and what
        // it leaves behind is the OTHER member of the pair.
        //
        // `$(A)` copies A's cached value, and the table pass built that
        // value by following `$(B)` and refusing the `$(A)` inside it,
        // so the placeholder names B. Measured on `softIoc R7.0.10`
        // through `dbLoadTemplate` with this exact pair: C loads
        // `$(B,recursive)`. A lazy expander answers `$(A,recursive)`
        // here — the reference it happened to refuse first — which is a
        // wrong loaded VALUE and not a wording difference, and is why
        // the engine keeps a table at all.
        let mut macros = HashMap::new();
        macros.insert("A".to_string(), "$(B)".to_string());
        macros.insert("B".to_string(), "$(A)".to_string());
        assert_eq!(substitute_macros("$(A)", &macros), "$(B,recursive)");
    }

    /// The real `menuScan` body at `R7.0.10`
    /// (`modules/database/src/ioc/db/menuScan.dbd.pod`, the text after
    /// `=cut`). The choice ORDER is the whole point: a menu field's wire
    /// value is the index into this list, so a loader that keeps the
    /// choices unordered or deduplicated would serve a different `.SCAN`
    /// than a C IOC for the same `.dbd`.
    #[test]
    fn a_menu_declaration_keeps_its_choices_in_declaration_order() {
        let src = r#"
menu(menuScan) {
	choice(menuScanPassive,"Passive")
	choice(menuScanEvent,"Event")
	choice(menuScanI_O_Intr,"I/O Intr")
	# Periodic scans follow, ordered from slowest to fastest
	choice(menuScan10_second,"10 second")
	choice(menuScan_1_second,".1 second")
}
"#;
        let parsed = parse_db_with_breaktables(src, &HashMap::new()).expect("menu must parse");
        assert!(parsed.records.is_empty());
        let menu = parsed.dbd.menu("menuScan").expect("menuScan declared");
        assert_eq!(
            menu.choices,
            vec![
                ("menuScanPassive".to_string(), "Passive".to_string()),
                ("menuScanEvent".to_string(), "Event".to_string()),
                ("menuScanI_O_Intr".to_string(), "I/O Intr".to_string()),
                ("menuScan10_second".to_string(), "10 second".to_string()),
                ("menuScan_1_second".to_string(), ".1 second".to_string()),
            ],
            "the `#` line inside the body is a comment, not a choice"
        );
    }

    /// Boundary: `recordtype` with a field body, a `menu(...)` item, and a
    /// `%` C definition. C stores the menu item under the literal key
    /// `"menu"` (`dbYacc.y:144-152`) and strips only the leading `%` from
    /// a cdef (`dbLex.l:94-97`).
    #[test]
    fn a_recordtype_keeps_its_field_items_its_menu_item_and_its_cdefs() {
        let src = r#"
recordtype(ai) {
    %#include "callback.h"
    field(SCAN,DBF_MENU) {
        prompt("Scan Mechanism")
        promptgroup("10 - Common")
        special(SPC_SCAN)
        menu(menuScan)
    }
    field(PREC,DBF_SHORT) {
        prompt("Display Precision")
    }
}
"#;
        let parsed =
            parse_db_with_breaktables(src, &HashMap::new()).expect("recordtype must parse");
        let rt = &parsed.dbd.record_types[0];
        assert_eq!(rt.name, "ai");
        assert_eq!(rt.cdefs, vec!["#include \"callback.h\"".to_string()]);
        assert_eq!(rt.fields.len(), 2);
        assert_eq!(rt.fields[0].name, "SCAN");
        assert_eq!(rt.fields[0].dbf_type, "DBF_MENU");
        assert_eq!(
            rt.fields[0].items,
            vec![
                ("prompt".to_string(), "Scan Mechanism".to_string()),
                ("promptgroup".to_string(), "10 - Common".to_string()),
                ("special".to_string(), "SPC_SCAN".to_string()),
                ("menu".to_string(), "menuScan".to_string()),
            ]
        );
        assert_eq!(rt.fields[1].name, "PREC");
    }

    /// Boundary: `recordtype_body: '{' '}'` is its OWN C production
    /// (`dbRecordtypeEmpty`, `dbYacc.y:110-113`), not a degenerate field
    /// list — a `.dbd` that declares a type with no fields must load.
    #[test]
    fn an_empty_recordtype_body_is_not_a_syntax_error() {
        let parsed = parse_db_with_breaktables("recordtype(stub) {}", &HashMap::new())
            .expect("the empty body is a production of its own");
        assert_eq!(parsed.dbd.record_types[0].name, "stub");
        assert!(parsed.dbd.record_types[0].fields.is_empty());
    }

    /// Boundary: the six single-line declarations, including `variable`'s
    /// two arities — C defaults the one-argument form to `int`
    /// (`dbYacc.y:192-202`).
    #[test]
    fn the_single_line_dbd_declarations_all_parse() {
        let src = r#"
device(ai,CONSTANT,devAiSoft,"Soft Channel")
driver(drvVxi)
link(const,lsetConst)
registrar(asSub)
function(myCalcFunc)
variable(dbRecordsOnceOnly)
variable(CASDEBUG,int)
"#;
        let d = parse_db_with_breaktables(src, &HashMap::new())
            .expect("every dbd declaration must parse")
            .dbd;
        assert_eq!(
            d.devices,
            vec![DbdDevice {
                record_type: "ai".into(),
                link_type: "CONSTANT".into(),
                dset: "devAiSoft".into(),
                choice: "Soft Channel".into(),
            }]
        );
        assert_eq!(d.drivers, vec!["drvVxi".to_string()]);
        assert_eq!(
            d.link_types,
            vec![DbdLinkType {
                key: "const".into(),
                lset: "lsetConst".into()
            }]
        );
        assert_eq!(d.registrars, vec!["asSub".to_string()]);
        assert_eq!(d.functions, vec!["myCalcFunc".to_string()]);
        assert_eq!(
            d.variables,
            vec![
                DbdVariable {
                    name: "dbRecordsOnceOnly".into(),
                    dtype: "int".into()
                },
                DbdVariable {
                    name: "CASDEBUG".into(),
                    dtype: "int".into()
                },
            ],
            "the one-argument form defaults to int, as C's action does"
        );
    }

    /// Boundary: C has one production per arity, so a wrong count is a
    /// parse error rather than a silently-dropped argument.
    #[test]
    fn a_declaration_with_the_wrong_argument_count_is_refused() {
        for src in [
            "driver(a,b)",
            "link(only)",
            "device(ai,CONSTANT,devAiSoft)",
            "variable(a,b,c)",
        ] {
            assert!(
                parse_db_with_breaktables(src, &HashMap::new()).is_err(),
                "{src} must not parse"
            );
        }
    }

    /// Boundary: the mixed file. `database_item` is ONE list in C, so
    /// records and declarations interleave freely and the records must
    /// still come out.
    #[test]
    fn records_and_dbd_declarations_interleave_in_one_file() {
        let src = r#"
menu(menuYesNo) { choice(menuYesNoNO,"NO") choice(menuYesNoYES,"YES") }
record(ai, "A") { field(VAL, "1") }
driver(drvSoft)
record(bo, "B") {}
"#;
        let parsed = parse_db_with_breaktables(src, &HashMap::new()).expect("mixed file");
        let names: Vec<&str> = parsed.records.iter().map(|r| r.name.as_str()).collect();
        assert_eq!(names, vec!["A", "B"]);
        assert_eq!(parsed.dbd.menus.len(), 1);
        assert_eq!(parsed.dbd.drivers, vec!["drvSoft".to_string()]);
    }

    /// Boundary: an ordinary `.db` declares nothing, so the new field must
    /// stay empty rather than pick anything up.
    #[test]
    fn an_ordinary_db_carries_no_dbd_declarations() {
        let parsed =
            parse_db_with_breaktables(r#"record(ai, "A") { field(VAL, "1") }"#, &HashMap::new())
                .unwrap();
        assert!(parsed.dbd.is_empty());
    }

    /// Boundary: `record_field: ... | include` (`dbYacc.y:271`) — an
    /// `include` inside a record body is grammar, not an error.
    #[test]
    fn an_include_inside_a_record_body_is_accepted() {
        let src = r#"record(ai, "A") { field(VAL, "1") include "more.db" field(PREC, "3") }"#;
        let recs = parse_db(src, &HashMap::new()).expect("include is a record_field alternative");
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].fields.len(), 2);
    }
}

#[cfg(test)]
mod suggestion_tests {
    use super::*;

    /// The whole diagnostic the loader would print for a refused field, or
    /// `None` where C prints only the ERROR line.
    fn line(record_type: &str, field: &str, value: &str) -> Option<String> {
        assert_eq!(
            record_type_field_exists(record_type, field),
            Some(false),
            "{record_type}.{field} must be a REFUSED field for this to mean anything"
        );
        suggest_field(record_type, field, value).map(suggestion_line)
    }

    /// C's own `epicsStrSimilarity` expectations, ported from
    /// `epicsStringTest.c:118-141` @`R7.0.10` — the table that pins the
    /// case-insensitive half-weight, which a plain edit distance does not have.
    #[test]
    fn similarity_matches_the_c_test_table() {
        for (expect, a, b) in [
            (1.00, "", ""),
            (1.00, "A", "A"),
            (0.00, "A", "B"),
            (1.00, "exact", "exact"),
            (0.90, "10 second", "10 seconds"),
            (0.71, "Passive", "Pensive"),
            (0.11, "10 second", "Pensive"),
            (0.97, "Set output to IVOV", "Set output To IVOV"),
            // Unrelated except for `i` ~= `I`, which a case-blind metric would
            // score 0 and a case-strict one would score the same as a
            // substitution.
            (0.06, "Passive", "I/O Intr"),
            (0.06, "I/O Intr", "Pensive"),
            (0.50, "YES", "yes"),
            (0.00, "YES", "NO"),
            (0.67, "YES", "Yes"),
            (0.67, "Tes", "yes"),
        ] {
            let got = str_similarity(a, b);
            assert!(
                (got - expect).abs() < 0.01,
                "similarity({a:?}, {b:?}) = {got}, C says {expect}"
            );
        }
    }

    /// Measured against the reference `softIoc` @`R7.0.10`, one
    /// `record(<type>,"C") { field(<name>, "<value>") }` per row, stderr with
    /// the ANSI colouring stripped. Byte-exact, including the four-space
    /// indent, the two spaces before the prompt and the parentheses.
    #[test]
    fn the_suggestion_is_byte_exact_against_the_reference_ioc() {
        for (rt, field, value, expect) in [
            // Confusion map, simple form — it wins outright over anything
            // lexical, which is the point of having it.
            (
                "bo",
                "INP",
                "x",
                Some("    Did you mean \"OUT\"?  (Output Specification)"),
            ),
            (
                "ao",
                "INP",
                "1",
                Some("    Did you mean \"OUT\"?  (Output Specification)"),
            ),
            (
                "ai",
                "OUT",
                "1",
                Some("    Did you mean \"INP\"?  (Input Specification)"),
            ),
            (
                "ai",
                "DOL",
                "1",
                Some("    Did you mean \"INP\"?  (Input Specification)"),
            ),
            (
                "bi",
                "ZRST",
                "off",
                Some("    Did you mean \"ZNAM\"?  (Zero Name)"),
            ),
            (
                "mbbi",
                "ZNAM",
                "off",
                Some("    Did you mean \"ZRST\"?  (Zero String)"),
            ),
            (
                "mbbo",
                "ONAM",
                "on",
                Some("    Did you mean \"ONST\"?  (One String)"),
            ),
            // Confusion map, range form — the trailing character carries its
            // offset across to the partner range.
            (
                "calc",
                "DOLA",
                "1",
                Some("    Did you mean \"INPK\"?  (Input K)"),
            ),
            (
                "calc",
                "DOLF",
                "1",
                Some("    Did you mean \"INPP\"?  (Input P)"),
            ),
            (
                "calc",
                "INP0",
                "1",
                Some("    Did you mean \"INPA\"?  (Input A)"),
            ),
            (
                "calc",
                "INP9",
                "1",
                Some("    Did you mean \"INPJ\"?  (Input J)"),
            ),
            (
                "ao",
                "DOL0",
                "1",
                Some("    Did you mean \"DOL\"?  (Desired Output Link)"),
            ),
            (
                "seq",
                "INPA",
                "1",
                Some("    Did you mean \"DOL0\"?  (Input link 0)"),
            ),
            // Lexical similarity.
            (
                "ai",
                "ASL",
                "1",
                Some("    Did you mean \"ASLO\"?  (Adjustment Slope)"),
            ),
            (
                "ai",
                "PRE",
                "3",
                Some("    Did you mean \"PREC\"?  (Display Precision)"),
            ),
            (
                "ai",
                "DES",
                "hello",
                Some("    Did you mean \"DESC\"?  (Descriptor)"),
            ),
            (
                "ai",
                "FLNKK",
                "X",
                Some("    Did you mean \"FLNK\"?  (Forward Process Link)"),
            ),
            (
                "calc",
                "CALCC",
                "A+B",
                Some("    Did you mean \"CALC\"?  (Calculation)"),
            ),
            (
                "permissive",
                "LABE",
                "hi",
                Some("    Did you mean \"LABL\"?  (Button Label)"),
            ),
            (
                "stringin",
                "VALL",
                "hello",
                Some("    Did you mean \"VAL\"?  (Current Value)"),
            ),
            // The VALUE moves the answer: the same misspelling picks a
            // different field once the value stops parsing as the near-miss's
            // type.
            (
                "ai",
                "ASL",
                "abc",
                Some("    Did you mean \"ASG\"?  (Access Security Group)"),
            ),
            (
                "ai",
                "HIH",
                "1",
                Some("    Did you mean \"HIHI\"?  (Hihi Alarm Limit)"),
            ),
            (
                "ai",
                "HIH",
                "abc",
                Some("    Did you mean \"SDIS\"?  (Scanning Disable)"),
            ),
            // A device menu: the numeric index halves the score and loses,
            // the spelled-out choice multiplies it by 1.5 and wins.
            (
                "ai",
                "DTY",
                "1",
                Some("    Did you mean \"SDLY\"?  (Sim. Mode Async Delay)"),
            ),
            (
                "ai",
                "DTY",
                "Soft Channel",
                Some("    Did you mean \"DTYP\"?  (Device Type)"),
            ),
            (
                "ai",
                "DTYX",
                "1",
                Some("    Did you mean \"DTYP\"?  (Device Type)"),
            ),
            // `menu(menuScan)` resolves through the loaded table, not the
            // generated one, which is empty for it by construction.
            (
                "ai",
                "SCA",
                "Passive",
                Some("    Did you mean \"SCAN\"?  (Scan Mechanism)"),
            ),
            (
                "ai",
                "SCANN",
                "Passive",
                Some("    Did you mean \"SCAN\"?  (Scan Mechanism)"),
            ),
            // `lsi.VAL` is declared `special(SPC_DBADDR)` and so is never
            // proposed, even though its `cvt_dbaddr` resolves the address to
            // `SPC_MOD`.
            (
                "lsi",
                "oval",
                "2 second",
                Some("    Did you mean \"SCAN\"?  (Scan Mechanism)"),
            ),
            // No candidate shares a character with the name, so every score is
            // zero and C prints nothing at all.
            ("ai", "ZZZZ", "1", None),
            ("ai", "Q", "1", None),
        ] {
            assert_eq!(
                line(rt, field, value).as_deref(),
                expect,
                "{rt}.{field} = {value:?}"
            );
        }
    }

    /// A record type with no device support at all.
    ///
    /// C @`R7.0.10` does not answer this case: `dbRecordField` reaches
    /// `DTYP`'s `DBF_DEVICE` arm, dereferences a NULL `dbDeviceMenu` and
    /// SEGFAULTS — measured on the reference `softIoc` for all nine base
    /// record types that declare no `device()` line (`aSub`, `calc`,
    /// `compress`, `dfanout`, `fanout`, `permissive`, `sel`, `seq`, `sub`),
    /// for any bad field name with a non-empty value. The port reads the
    /// absent menu as an empty one — the branch C's own code takes for
    /// `nChoice == 0` — so `DTYP` simply takes the mismatch penalty and the
    /// answer is the one C gives when the value is empty and the crash is
    /// skipped.
    #[test]
    fn a_record_type_with_no_device_support_still_answers() {
        let expect = Some("    Did you mean \"LOLO\"?  (Lolo Alarm Limit)");
        // C's own answer, measured: `field(DOL, "")` takes no type test at all.
        assert_eq!(line("calc", "DOL", "").as_deref(), expect);
        // The value that kills C.
        assert_eq!(line("calc", "DOL", "1").as_deref(), expect);
    }

    /// The map proposes a name; the record type decides whether it exists.
    #[test]
    fn a_confusion_map_proposal_the_type_does_not_declare_is_dropped() {
        // `DOL` -> `INP`, which `calc` does not declare (it has `INPA`..`INPL`),
        // so the map yields nothing and the lexical pass answers instead.
        assert_eq!(confusion_guess("DOL", 2).as_deref(), Some("INP"));
        assert!(crate::server::record::declared_field("calc", "INP").is_none());
        assert_eq!(
            line("calc", "DOL", "").as_deref(),
            Some("    Did you mean \"LOLO\"?  (Lolo Alarm Limit)")
        );
    }

    /// C builds the range proposal from the first `l-3` characters and the one
    /// at `l-3`, and never looks at the tail — so a longer name still proposes,
    /// and is rejected only by not existing.
    #[test]
    fn the_range_map_ignores_the_tail_of_the_name() {
        assert_eq!(confusion_guess("INPAX", 8).as_deref(), Some("DOL0"));
        assert_eq!(confusion_guess("INPA", 8).as_deref(), Some("DOL0"));
        // The prefix must match and the character must be inside the range.
        assert_eq!(confusion_guess("INP", 8), None);
        assert_eq!(confusion_guess("INPZ", 8), None);
        assert_eq!(confusion_guess("XNPA", 8), None);
    }

    /// A field with no `prompt` in its `.dbd` gets the bare line — C only
    /// prints the parenthesised half `if (bestFld->prompt)`.
    #[test]
    fn a_field_without_a_prompt_gets_the_bare_line() {
        let desc = FieldDesc::new("ZZ", crate::types::DbFieldType::Long, false);
        assert_eq!(suggestion_line(&desc), "    Did you mean \"ZZ\"?");
    }
}

/// C's two `.db` menu-value refusals, measured on the reference `softIoc`
/// @`R7.0.10`.
#[cfg(test)]
mod menu_refusal_tests {
    use super::{ERL_ERROR, menu_value_refusal};

    fn line(rt: &str, rec: &str, field: &str, value: &str) -> Option<String> {
        menu_value_refusal(rt, rec, field, value).map(|r| r.line)
    }

    fn hint(rt: &str, field: &str, value: &str) -> Option<String> {
        menu_value_refusal(rt, "R", field, value).and_then(|r| r.suggestion)
    }

    /// C `dbPutString` refuses ANY value still carrying a macro before it
    /// reaches the field-type switch (`dbStaticLib.c:2579-2585`), and
    /// `dbIsMacroOk` is `return(FALSE)` in the only definition base has
    /// (`dbStaticRun.c:228`), so the exemption never fires in an IOC.
    ///
    /// Every row was read off the reference `softIoc` loading
    /// `modules/database/test/std/rec/compressTest.db` with no macros
    /// defined, which is the one file in B's 317-case corpus where C's guard
    /// fires at all.
    #[test]
    fn an_unexpanded_macro_is_refused_before_the_field_type_is_read() {
        for (field, value, suggestion) in [
            // A link field: no other arm of this funnel refuses one, which is
            // why the port used to install it and leave a live CA search for a
            // PV literally named `$(INP,undefined)`.
            ("INP", "$(INP,undefined) NPP", None),
            // Menu fields: `dbRecordField` runs `dbPutStringSuggest` after
            // this refusal exactly as it does after the menu arm's own
            // (`dbLexRoutines.c:1414`).
            (
                "ALG",
                "$(ALG,undefined)",
                Some("    Did you mean \"Average\"?"),
            ),
            (
                "BALG",
                "$(BALG,undefined)",
                Some("    Did you mean \"FIFO Buffer\"?"),
            ),
            // A numeric field, whose own arm answered "No digits to convert"
            // before the guard was put ahead of it.
            ("NSAM", "$(NSAM,undefined)", None),
        ] {
            let refusal = menu_value_refusal("compress", "comp", field, value)
                .unwrap_or_else(|| panic!("compress.{field} = {value:?} must be refused"));
            assert_eq!(
                refusal.notice,
                Some(format!("comp.{field} Has unexpanded macro"))
            );
            assert_eq!(
                refusal.line,
                format!("{ERL_ERROR}: Can't set 'comp.{field}' to '{value}'  : Bad Field value")
            );
            assert_eq!(refusal.suggestion.as_deref(), suggestion);
        }
    }

    /// The test C makes is `strstr(pstring,"$(") || strstr(pstring,"${")`, so
    /// both spellings refuse and a value carrying neither is left to the arms
    /// that were already deciding it.
    #[test]
    fn both_macro_spellings_refuse_and_a_plain_value_still_does_not() {
        assert!(menu_value_refusal("compress", "comp", "INP", "${INP} NPP").is_some());
        assert!(menu_value_refusal("compress", "comp", "INP", "some:pv NPP").is_none());
        assert_eq!(
            hint("ai", "SCAN", "Passiv"),
            Some(String::from("    Did you mean \"Passive\"?"))
        );
    }

    /// Every row was read off `softIoc -d <file>` and copied verbatim,
    /// including the two spaces the empty `pdbentry->message` leaves in the
    /// `Bad Field value` form.
    #[test]
    fn the_refusal_is_byte_exact_against_the_reference_ioc() {
        for (rt, rec, field, value, expect) in [
            (
                "ai",
                "M1",
                "SCAN",
                "Passiv",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'M1.SCAN' to 'Passiv' using menu menuScan : Illegal choice"
                )),
            ),
            (
                "ai",
                "M3",
                "PINI",
                "YESS",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'M3.PINI' to 'YESS' using menu menuPini : Illegal choice"
                )),
            ),
            (
                "ai",
                "N1",
                "PRIO",
                "HIGHH",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'N1.PRIO' to 'HIGHH' using menu menuPriority : Illegal choice",
                )),
            ),
            (
                "ai",
                "L3",
                "LINR",
                "NoSuchTable",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'L3.LINR' to 'NoSuchTable' using menu menuConvert : Illegal choice",
                )),
            ),
            (
                "sel",
                "Y1",
                "SELM",
                "Bogus",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'Y1.SELM' to 'Bogus' using menu selSELM : Illegal choice"
                )),
            ),
            // A number that clears `epicsParseUInt16` but not C's bound takes
            // the OTHER arm: no `dbMsgPrint`, so no menu name and two spaces,
            // and `S_dbLib_badField` rather than `S_db_badChoice`.
            (
                "ai",
                "P2",
                "PINI",
                "7",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'P2.PINI' to '7'  : Bad Field value"
                )),
            ),
            // C's bound is `value > nChoice`, so one past the last choice
            // loads — `menuPini` has six.
            ("ai", "P1", "PINI", "6", None),
            // ... and so does `USHRT_MAX`, which is what lets the `menuScan`
            // "use SCAN" sentinel into `SSCN`.
            ("ai", "P3", "PINI", "65535", None),
            // "empty string is the same as writing numeric zero".
            ("ai", "P4", "PINI", "", None),
            // `epicsParseUInt16(pstring, &value, 0, NULL)` — base 0, so `0x`
            // and octal are numbers.
            ("ai", "P5", "PINI", "0x2", None),
            // `strcmp`, so the choice test is case-SENSITIVE and this falls
            // through to the numeric one.
            (
                "ai",
                "P6",
                "PINI",
                "yes",
                Some(format!(
                    "{ERL_ERROR}: Can't set 'P6.PINI' to 'yes' using menu menuPini : Illegal choice"
                )),
            ),
            // An exact choice, and an in-menu index, both load.
            ("ai", "OK", "PINI", "RUNNING", None),
            ("ai", "OK", "PINI", "3", None),
            ("ai", "OK", "SCAN", "1 second", None),
        ] {
            assert_eq!(
                line(rt, rec, field, value).as_deref(),
                expect.as_deref(),
                "{rt} {rec}.{field} = {value:?}"
            );
        }
    }

    /// Not a menu field, so the menu wording is not what judges it — C reaches
    /// a different arm of `dbPutStringNum` for those.
    #[test]
    fn a_non_menu_field_is_not_judged_by_the_menu_arm() {
        // `DBF_SHORT` still fails, through the numeric arm and with the
        // `S_stdlib_*` wording rather than a menu name.
        assert_eq!(
            line("ai", "R", "PREC", "not a number"),
            Some(format!(
                "{ERL_ERROR}: Can't set 'R.PREC' to 'not a number'  : No digits to convert"
            ))
        );
        assert_eq!(hint("ai", "PREC", "not a number"), None);
        // `DBF_STRING` has a length check, not a parse.
        assert_eq!(line("ai", "R", "DESC", "anything at all"), None);
        // `DBF_DEVICE`: the port's builder-registered device support never
        // reaches the device menu, so the menu cannot judge a DTYP.
        assert_eq!(line("ai", "R", "DTYP", "Soft Chanel"), None);
    }

    /// C `dbPutStringSuggest` (`dbStaticLib.c:2665-2708`), measured on the
    /// reference `softIoc` @`R7.0.10` alongside each refusal above.
    #[test]
    fn the_choice_suggestion_is_byte_exact_against_the_reference_ioc() {
        for (rt, field, value, expect) in [
            (
                "ai",
                "SCAN",
                "Passiv",
                Some("    Did you mean \"Passive\"?"),
            ),
            ("ai", "PINI", "YESS", Some("    Did you mean \"YES\"?")),
            ("ai", "PRIO", "HIGHH", Some("    Did you mean \"HIGH\"?")),
            (
                "ai",
                "LINR",
                "NoSuchTable",
                Some("    Did you mean \"SLOPE\"?"),
            ),
            (
                "sel",
                "SELM",
                "Bogus",
                Some("    Did you mean \"Low Signal\"?"),
            ),
            ("ai", "PINI", "yes", Some("    Did you mean \"YES\"?")),
            // `maxdist` starts at 0.0 and the test is strict `>`: a value that
            // shares nothing with any choice gets the bare refusal, which is
            // what C prints for an out-of-menu index.
            ("ai", "PINI", "7", None),
        ] {
            assert_eq!(
                hint(rt, field, value).as_deref(),
                expect,
                "{rt}.{field} = {value:?}"
            );
        }
    }

    /// Every `dbCommon` menu field must resolve its menu's name, or the
    /// refusal would be printed without the half C words it with.
    #[test]
    fn every_common_menu_field_names_its_menu() {
        use crate::server::record::{dbd_generated::DB_COMMON_FIELDS, declared_field};
        use crate::types::DbfCode;

        for desc in DB_COMMON_FIELDS {
            if desc.declared_dbf != DbfCode::Menu {
                continue;
            }
            assert!(
                super::load_menu_of(desc).is_some_and(|(name, _)| name.is_some()),
                "{}: menu name did not resolve",
                desc.name
            );
        }
        // And a record-own menu field, resolved through the same table.
        let linr = declared_field("ai", "LINR").expect("ai declares LINR");
        assert_eq!(
            super::load_menu_of(linr).and_then(|(n, _)| n),
            Some("menuConvert")
        );
    }

    /// An externally-registered record type's menu field carries its own
    /// choice table — a static from its OWN crate, absent from the generated
    /// `MENUS` — and `FieldDesc::new` derives `declared_dbf` from the served
    /// type, not from the table. Both halves used to drop the field out of
    /// the menu path entirely, and its labels then refused through the
    /// `DBF_ENUM`/`DBF_USHORT` numeric arm as "No digits to convert".
    #[test]
    fn an_external_choice_table_is_a_menu_without_a_name() {
        use crate::server::record::FieldDesc;
        use crate::types::{DbFieldType, DbfCode};

        static EXT_CHOICES: &[&str] = &["Local", "Remote"];
        let mut desc = FieldDesc::new("MODE", DbFieldType::Enum, false);
        desc.menu = Some(EXT_CHOICES);
        assert_eq!(desc.declared_dbf, DbfCode::Enum);
        assert_eq!(super::load_menu_of(&desc), Some((None, EXT_CHOICES)));

        // A valid label is not refused; an invalid one gets C's menu
        // wording, with the field's name standing in for the `.dbd` menu
        // name the external table does not have.
        assert_eq!(
            super::menu_arm(&desc, "MODE", None, EXT_CHOICES, "Remote", "Remote"),
            None
        );
        let (detail, status, _) =
            super::menu_arm(&desc, "MODE", None, EXT_CHOICES, "Bogus", "Bogus").unwrap();
        assert_eq!(detail, "using menu MODE");
        assert_eq!(status, "Illegal choice");

        // `DBF_DEVICE` keeps its own channel even when it carries a table.
        let mut dtyp = FieldDesc::new("DTYP", DbFieldType::Enum, false);
        dtyp.declared_dbf = DbfCode::Device;
        dtyp.menu = Some(EXT_CHOICES);
        assert_eq!(super::load_menu_of(&dtyp), None);
    }
}

/// C's `.db` NUMERIC refusals, measured on the reference `softIoc` @`R7.0.10`.
///
/// The whole point of the round is the pair `1e-310` / `70000`: glibc raises
/// `ERANGE` for a subnormal, so C refuses a value the port stored, while
/// `dbConvertStrict` being zero makes C STORE a truncated `70000` the port
/// would refuse. One C function decides both, so one screen has to.
#[cfg(test)]
mod numeric_refusal_tests {
    use super::{
        DbfCode, ERL_ERROR, db_load_numeric_value, menu_value_refusal, numeric_value_refusal,
    };
    use crate::types::EpicsValue;

    fn status(rt: &str, field: &str, value: &str) -> Option<String> {
        let r = menu_value_refusal(rt, "R", field, value)?;
        // Every numeric refusal is worded the same way: no `dbMsgPrint`, so
        // two spaces, and no suggestion.
        assert_eq!(r.suggestion, None, "{rt}.{field} = {value:?}");
        let head = format!("{ERL_ERROR}: Can't set 'R.{field}' to '{value}'  : ");
        Some(
            r.line
                .strip_prefix(&head)
                .unwrap_or_else(|| panic!("{:?} does not open with {head:?}", r.line))
                .to_string(),
        )
    }

    #[test]
    fn the_refusals_are_byte_exact_against_the_reference_ioc() {
        for (rt, field, value, expect) in [
            // `DBF_DOUBLE`, `epicsParseFloat64` = `epicsParseDouble`. All three
            // ERANGE shapes report OVERFLOW unless the result reached exactly
            // zero, because `epicsStdlib.c:165` reads a non-zero result as
            // "too large" whichever end it came off.
            ("ao", "OROC", "5e-324", "Too large to represent"),
            ("ao", "HOPR", "1e-310", "Too large to represent"),
            ("ao", "HOPR", "-1e-310", "Too large to represent"),
            ("ao", "LOPR", "1e400", "Too large to represent"),
            ("ao", "EGUF", "1e-400", "Too small to represent"),
            ("ao", "EGUL", "abc", "No digits to convert"),
            ("ao", "AOFF", "1.5x", "Extraneous characters"),
            // `DBF_SHORT`/`DBF_UINT64`/`DBF_INT64`, the lax arm: only what
            // `strtol`/`strtoul` itself refuses.
            (
                "ai",
                "PHAS",
                "99999999999999999999",
                "Too large to represent",
            ),
            ("ai", "PHAS", "zz", "No digits to convert"),
            ("ai", "PHAS", "3 x", "Extraneous characters"),
            (
                "ai",
                "UTAG",
                "99999999999999999999999",
                "Too large to represent",
            ),
            (
                "int64out",
                "VAL",
                "9223372036854775808",
                "Too large to represent",
            ),
            // An integer field given a decimal: `strtol` stops at the `.` and
            // the tail is extraneous, so this common `.db` idiom FAILS the load.
            ("longout", "VAL", "1.0", "Extraneous characters"),
            ("ai", "PREC", "3.0", "Extraneous characters"),
            // `DBF_ENUM` takes the unsigned arm, not the menu one, so a label
            // never resolves on an `mbbo` VAL.
            ("mbbo", "VAL", "ONE", "No digits to convert"),
        ] {
            assert_eq!(
                status(rt, field, value).as_deref(),
                Some(expect),
                "{rt}.{field} = {value:?}"
            );
        }
    }

    #[test]
    fn the_values_c_stores_are_not_refused() {
        for (rt, field, value) in [
            // Whitespace either side is skipped at both ends of `epicsParse*`.
            ("ao", "ASLO", "  2.5  "),
            ("ai", "PREC", " 4"),
            ("ai", "PREC", "4 "),
            // Base 0: hex, binary and octal prefixes, on the integer arms and
            // through `strtod` on the double one.
            ("ao", "ROFF", "0x10"),
            ("ao", "HOPR", "0x10"),
            ("longout", "VAL", "0x10"),
            ("waveform", "NELM", "0b11"),
            ("ai", "PREC", "010"),
            // The lax arm casts instead of range-checking: 4464, 25536, 255,
            // 44, `ULLONG_MAX` and 705032704 respectively.
            ("ai", "PHAS", "70000"),
            ("ai", "PHAS", "-40000"),
            ("ai", "TPRO", "-1"),
            ("ai", "TPRO", "300"),
            ("ai", "UTAG", "-1"),
            ("longout", "VAL", "5000000000"),
            ("int64out", "VAL", "-9223372036854775808"),
            ("mbbo", "VAL", "5"),
            ("ai", "TSE", "-2"),
            // `epicsParseDouble` sets no ERANGE for the `inf`/`nan` WORDS.
            ("ao", "HOPR", "1e3"),
            ("ao", "HOPR", "nan"),
            ("ao", "HOPR", "inf"),
            ("ao", "HOPR", ".5"),
            // "empty string is the same as writing numeric zero".
            ("ai", "PREC", ""),
            ("ao", "HOPR", ""),
        ] {
            assert_eq!(
                menu_value_refusal(rt, "R", field, value).map(|r| r.line),
                None,
                "{rt}.{field} = {value:?}"
            );
        }
    }

    /// `DBF_FLOAT` and `DBF_CHAR` have no field in any `epics-base` record, so
    /// their arms are exercised at the switch instead of through a record.
    ///
    /// `epicsParseFloat`'s two extra gates (`epicsStdlib.c:325-329`) are
    /// deliberately asymmetric: the underflow test reads `value > 0`, so a
    /// negative subnormal-for-`float` is STORED, and the overflow test reads
    /// `finite(value)`, so an `inf` literal is stored while `1e39` is not.
    #[test]
    fn the_float_gate_boundaries() {
        let float = |s: &str| numeric_value_refusal(DbfCode::Float, "F", s);
        let flt_min = format!("{:e}", f64::from(f32::MIN_POSITIVE));
        let flt_max = format!("{:e}", f64::from(f32::MAX));

        assert_eq!(float("1e-40"), Some("Too small to represent"));
        assert_eq!(float(&flt_min), Some("Too small to represent"));
        assert_eq!(float("-1e-40"), None);
        assert_eq!(float("1e39"), Some("Too large to represent"));
        assert_eq!(float("-1e39"), Some("Too large to represent"));
        assert_eq!(float(&flt_max), Some("Too large to represent"));
        assert_eq!(float("inf"), None);
        assert_eq!(float("-inf"), None);
        assert_eq!(float("nan"), None);
        assert_eq!(float("1.5"), None);
        // The double parse runs first, so its own ERANGE wins.
        assert_eq!(float("1e-400"), Some("Too small to represent"));
        assert_eq!(float("1e400"), Some("Too large to represent"));
        assert_eq!(float("zz"), Some("No digits to convert"));

        let ch = |s: &str| numeric_value_refusal(DbfCode::Char, "C", s);
        assert_eq!(ch("300"), None); // cast to 44, not refused
        assert_eq!(ch("-1"), None);
        assert_eq!(ch("abc"), Some("No digits to convert"));
        assert_eq!(ch("1.0"), Some("Extraneous characters"));
    }

    /// The lax arms CAST rather than range-check, so the value C stores is not
    /// the value the text names. Every row was read back with `dbgf` on the
    /// reference `softIoc` after the load that produced it.
    #[test]
    fn the_stored_value_is_the_cast_c_makes() {
        for (declared, text, expect) in [
            (DbfCode::Short, "70000", EpicsValue::Short(4464)),
            (DbfCode::Short, "-40000", EpicsValue::Short(25536u16 as i16)),
            (DbfCode::Short, "010", EpicsValue::Short(8)),
            (DbfCode::Short, "-2", EpicsValue::Short(-2)),
            (DbfCode::UChar, "-1", EpicsValue::UChar(255)),
            (DbfCode::UChar, "300", EpicsValue::UChar(44)),
            (DbfCode::Long, "5000000000", EpicsValue::Long(705032704)),
            (DbfCode::Long, "0x10", EpicsValue::Long(16)),
            (DbfCode::ULong, "0b11", EpicsValue::ULong(3)),
            (DbfCode::UInt64, "-1", EpicsValue::UInt64(u64::MAX)),
            (
                DbfCode::Int64,
                "-9223372036854775808",
                EpicsValue::Int64(i64::MIN),
            ),
            (DbfCode::Enum, "5", EpicsValue::UShort(5)),
            (DbfCode::Char, "300", EpicsValue::Char(44)),
            (DbfCode::Char, "-1", EpicsValue::Char(255)),
            (DbfCode::Double, "0x10", EpicsValue::Double(16.0)),
            (DbfCode::Double, "  2.5  ", EpicsValue::Double(2.5)),
            (DbfCode::Float, "1.5", EpicsValue::Float(1.5)),
            // The empty value is the number zero on every arm.
            (DbfCode::Short, "", EpicsValue::Short(0)),
            (DbfCode::Double, "", EpicsValue::Double(0.0)),
        ] {
            assert_eq!(
                db_load_numeric_value(declared, "F", text),
                Ok(Some(expect)),
                "{declared:?} = {text:?}"
            );
        }
    }

    /// The three types whose C arm is not a numeric parse at all.
    #[test]
    fn the_non_numeric_declarations_decline() {
        for code in [
            DbfCode::String,
            DbfCode::Menu,
            DbfCode::Device,
            DbfCode::Inlink,
            DbfCode::Outlink,
            DbfCode::Fwdlink,
            DbfCode::NoAccess,
        ] {
            assert_eq!(numeric_value_refusal(code, "X", "not a number"), None);
        }
    }
}