File: DRCtech.c

package info (click to toggle)
magic 8.1.218+ds.1-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 16,604 kB
  • sloc: ansic: 160,782; sh: 7,068; tcl: 3,982; lisp: 2,554; makefile: 918; cpp: 587; csh: 149; awk: 140
file content (3896 lines) | stat: -rw-r--r-- 104,925 bytes parent folder | download | duplicates (2)
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
/*
 * DRCtech.c --
 *
 * Technology initialization for the DRC module.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/drc/DRCtech.c,v 1.12 2010/10/20 12:04:12 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tcltk/tclmagic.h"
#include "utils/magic.h"
#include "utils/geometry.h"
#include "utils/utils.h"
#include "tiles/tile.h"
#include "utils/hash.h"
#include "database/database.h"
#include "drc/drc.h"
#include "utils/tech.h"
#include "textio/textio.h"
#include "utils/malloc.h"
#include "cif/cif.h"
#include "cif/CIFint.h"

CIFStyle *drcCifStyle = NULL;
bool	 DRCForceReload = FALSE;

/*
 * DRC interaction radius being used (not necessarily the same as
 * what's defined in the current DRC style).
 */
global int DRCTechHalo;
global int DRCStepSize;

/* The following variable can be set to zero to turn off
 * any optimizations of design rule lists.
 */

global int DRCRuleOptimization = TRUE;

/* The following variables count how many rules were specified by
 * the technology file and how many edge rules were optimized away.
 */

static int drcRulesSpecified = 0;
static int drcRulesOptimized = 0;

/*
 * Forward declarations.
 */
int drcWidth(), drcSpacing(), drcEdge(), drcNoOverlap();
int drcExactOverlap(), drcExtend();
int drcSurround(), drcRectOnly(), drcOverhang();
int drcStepSize();
int drcMaxwidth(), drcArea(), drcRectangle(), drcAngles();
int drcCifSetStyle(), drcCifWidth(), drcCifSpacing();
int drcCifMaxwidth(), drcCifArea();

void DRCTechStyleInit();
void drcLoadStyle();
void DRCTechFinal();
void drcTechFinalStyle();

/*
 * ----------------------------------------------------------------------------
 *
 * Given a TileType bit mask, return the plane mask of planes that are
 * coincident with all types.  This is roughly equivalent to the deprecated
 * DBTechMinSetPlanes(), but does not attempt to produce a reduced set of
 * tile types.  Since the collective mask of all possible planes is usually
 * found by a call to DBTechNoisyNameMask, we don't attempt to OR all the
 * plane masks together, but assume this collective mask is available and
 * passed as an argument.
 *
 * ----------------------------------------------------------------------------
 */

PlaneMask
CoincidentPlanes(typeMask, pmask)
    TileTypeBitMask *typeMask;		/* Mask of types to check coincidence */
    PlaneMask pmask;			/* Mask of all possible planes of types */
{
    PlaneMask planes = pmask;
    TileType i;

    /* AND each plane against the collective mask */
    for (i = TT_SELECTBASE; i < DBNumTypes; i++)
	if (TTMaskHasType(typeMask, i))
	    planes &= DBTypePlaneMaskTbl[i];

    return planes;
}
 

/*
 * ----------------------------------------------------------------------------
 *
 * Given a plane mask, return the plane number of the lowest plane in the mask.
 *
 * ----------------------------------------------------------------------------
 */

int
LowestMaskBit(PlaneMask pmask)
{
   PlaneMask pset = pmask;
   int plane = 0;

   if (pmask == 0) return DBNumPlanes;

   while ((pset & 0x1) == 0)
   {
	plane++;
	pset >>= 1;
   }
   return plane;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCPrintStyle --
 *
 *	Print the available and/or current extraction styles.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Output.
 *
 * ----------------------------------------------------------------------------
 */

void
DRCPrintStyle(dolist, doforall, docurrent)
    bool dolist, doforall, docurrent;
{
    DRCKeep *style;

    if (docurrent)
    {
	if (DRCCurStyle == NULL)
	    TxError("Error: No style is set\n");
	else
	{
	    if (!dolist) TxPrintf("The current style is \"");
#ifdef MAGIC_WRAPPER
	    if (dolist)
	        Tcl_SetResult(magicinterp, DRCCurStyle->ds_name, NULL);
	    else
#endif
	    TxPrintf("%s", DRCCurStyle->ds_name);
	    if (!dolist) TxPrintf("\".\n");
	}
    }

    if (doforall)
    {
	if (!dolist) TxPrintf("The DRC styles are: ");

	for (style = DRCStyleList; style; style = style->ds_next)
	{
	    if (dolist)
	    {
#ifdef MAGIC_WRAPPER
		Tcl_AppendElement(magicinterp, style->ds_name);
#else
		if (style != DRCStyleList) TxPrintf(" ");
		TxPrintf("%s", style->ds_name);
#endif
	    }
	    else
	    {
		if (style != DRCStyleList) TxPrintf(", ");
		TxPrintf("%s", style->ds_name);
	    }
	}
	if (!dolist) TxPrintf(".\n");
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCSetStyle --
 *
 *	Set the current DRC style to 'name'.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Output.
 *
 * ----------------------------------------------------------------------------
 */

void
DRCSetStyle(name)
    char *name;
{
    DRCKeep *style, *match;
    int length;

    if (name == NULL) return;

    match = NULL;
    length = strlen(name);
    for (style = DRCStyleList; style; style = style->ds_next)
    {
	if (strncmp(name, style->ds_name, length) == 0)
	{
	    if (match != NULL)
	    {
		TxError("DRC style \"%s\" is ambiguous.\n", name);
		DRCPrintStyle(FALSE, TRUE, TRUE);
		return;
	    }
	    match = style;
	}
    }

    if (match != NULL)
    {
	drcLoadStyle(match->ds_name);
	TxPrintf("DRC style is now \"%s\"\n", name);
	return;
    }

    TxError("\"%s\" is not one of the DRC styles Magic knows.\n", name);
    DRCPrintStyle(FALSE, TRUE, TRUE);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcTechFreeStyle --
 *
 *	This procedure frees all memory associated with a DRC style.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory free'd.
 *
 * ----------------------------------------------------------------------------
 */

void
drcTechFreeStyle()
{
    int i, j;
    char *old;
    DRCCookie *dp;

    if (DRCCurStyle != NULL)
    {
	/* Remove all old rules from the DRC rules table */

	for (i = 0; i < TT_MAXTYPES; i++)
	    for (j = 0; j < TT_MAXTYPES; j++)
	    {
		dp = DRCCurStyle->DRCRulesTbl[i][j];
		while (dp != NULL)
		{
		    char *old = (char *) dp;
		    dp = dp->drcc_next;
		    freeMagic(old);
		}
	    }

	/* Clear the DRCWhyList */

	while (DRCCurStyle->DRCWhyList != NULL)
	{
	    old = (char *) DRCCurStyle->DRCWhyList;
	    StrDup(&(DRCCurStyle->DRCWhyList->dwl_string), (char *) NULL);
	    DRCCurStyle->DRCWhyList = DRCCurStyle->DRCWhyList->dwl_next;
	    freeMagic(old);
	}

	freeMagic(DRCCurStyle);
	DRCCurStyle = NULL;
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcTechNewStyle --
 *
 *	This procedure creates a new DRC style at the end of the list
 *	of styles and initializes it to completely null.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	A new element is added to the end of DRCStyleList, and DRCCurStyle
 *	is set to point to it.
 *
 * ----------------------------------------------------------------------------
 */

void
drcTechNewStyle()
{
   drcTechFreeStyle();
   DRCTechStyleInit();
}

/*
 * ----------------------------------------------------------------------------
 * drcWhyDup --
 *
 * Duplicate a shared "why" string using StrDup() and remember it so we can
 * free it sometime later, in drcWhyClear().
 *
 * Returns:
 *	A copy of the given string.
 *
 * Side effects:
 *	Adds to the DRCWhyList.  Calls StrDup().
 * ----------------------------------------------------------------------------
 */

char *
drcWhyDup(why)
    char * why;
{
    struct drcwhylist * new;

    new = (struct drcwhylist *) mallocMagic((unsigned) (sizeof *new));
    new->dwl_string = StrDup((char **) NULL, why);
    new->dwl_next = DRCCurStyle->DRCWhyList;
    DRCCurStyle->DRCWhyList = new;

    return new->dwl_string;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcFindBucket --
 *
 *	Find the bucket preceding the point where we with to insert a new DRC
 *	cookie.  Don't insert a cookie in the middle of a pair of coupled
 *	(trigger + check) rules.
 *
 * Results:
 *	Returns a pointer to the location where we want to insert a rule
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

DRCCookie *
drcFindBucket(i, j, distance)
    int i, j, distance;
{
    DRCCookie *dp;

    if (DRCCurStyle == NULL) return NULL;

    /* find bucket preceding the new one we wish to insert */

    for (dp = DRCCurStyle->DRCRulesTbl[i][j];
		dp->drcc_next != (DRCCookie *) NULL;
		dp = dp->drcc_next)
    {
	if (dp->drcc_next->drcc_flags & DRC_TRIGGER)
	{
	    if (dp->drcc_next->drcc_next->drcc_dist >= distance)
		break;
	    else
		dp = dp->drcc_next;
	}
	else if (dp->drcc_next->drcc_dist >= distance) break;
    }

    return dp;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcLoadStyle --
 * 
 * Re-read the technology file to load the specified technology DRC style
 * into structure DRCCurStyle.  It incurs a complete reading of the tech
 * file on startup and every time the extraction style is changed, but we
 * can assume that this does not happen often.  The first style in the
 * technology file is assumed to be the default, so that re-reading the
 * tech file is not necessary on startup unless the default DRC style is
 * changed by a call do "drc style".
 *
 * ----------------------------------------------------------------------------
 */

void
drcLoadStyle(stylename)
    char *stylename;
{
    SectionID invdrc;

    if (DRCCurStyle->ds_name == stylename) return;

    drcTechNewStyle();		/* Reinitialize and mark as not loaded */
    DRCCurStyle->ds_name = stylename;

    invdrc = TechSectionGetMask("drc", NULL);
    TechLoad(NULL, invdrc);

    DRCTechScale(DBLambda[0], DBLambda[1]);
}

/*
 * ----------------------------------------------------------------------------
 * DRCReloadCurStyle ---
 *
 * This routine is used by CIFLoadStyle whenever the DRC section makes
 * reference to CIF layers.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	DRC rule database is deleted and regenerated.
 *	
 * ----------------------------------------------------------------------------
 */

void
DRCReloadCurStyle()
{
    char *stylename;
    DRCKeep * style;

    if (DRCCurStyle == NULL) return;

    for (style = DRCStyleList; style != NULL; style = style->ds_next)
    {
	if (!strcmp(style->ds_name, DRCCurStyle->ds_name))
	{
	    DRCCurStyle->ds_name = NULL;
	    drcLoadStyle(style->ds_name);
	    break;
	}
    }
}

/*
 * ----------------------------------------------------------------------------
 * DRCTechInit --
 *
 * Free and re-initialize the technology-specific variables for the DRC module.
 * This routine is *only* called upon a "tech load" command, when all existing
 * DRC data must be cleaned up and free'd.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Clears out all the DRC tables.
 * ----------------------------------------------------------------------------
 */

void
DRCTechInit()
{
    DRCKeep *style;

    /* Clean up any old info */

    drcTechFreeStyle();

    for (style = DRCStyleList; style != NULL; style = style->ds_next)
    {
	freeMagic(style->ds_name);
	freeMagic(style);
    }
    DRCStyleList = NULL;
}

/*
 * ----------------------------------------------------------------------------
 * DRCTechStyleInit --
 *
 * Initialize the technology-specific variables for the DRC module.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Clears out all the DRC tables.
 * ----------------------------------------------------------------------------
 */

void
DRCTechStyleInit()
{
    int i, j, plane;
    DRCCookie *dp;
    PaintResultType result;

    drcRulesOptimized = 0;
    drcRulesSpecified = 0;

    if (DRCCurStyle == NULL)
    {
	DRCCurStyle = (DRCStyle *) mallocMagic(sizeof(DRCStyle));
	DRCCurStyle->ds_name = NULL;
    }

    DRCCurStyle->ds_status = TECH_NOT_LOADED;

    TTMaskZero(&DRCCurStyle->DRCExactOverlapTypes);
    DRCCurStyle->DRCWhyList = NULL;
    DRCCurStyle->DRCTechHalo = 0;
    DRCCurStyle->DRCScaleFactorN = 1;
    DRCCurStyle->DRCScaleFactorD = 1;
    DRCCurStyle->DRCStepSize = 0;

    DRCTechHalo = 0;

    /* Put a dummy rule at the beginning of the rules table for each entry */

    for (i = 0; i < TT_MAXTYPES; i++)
    {
	for (j = 0; j < TT_MAXTYPES; j++)
	{
	    dp = DRCCurStyle->DRCRulesTbl[i][j];
	    dp = (DRCCookie *) mallocMagic((unsigned) (sizeof (DRCCookie)));
	    dp->drcc_dist = -1;
	    dp->drcc_cdist = -1;
	    dp->drcc_next = (DRCCookie *) NULL;
	    TTMaskZero(&dp->drcc_mask);
	    DRCCurStyle->DRCRulesTbl[i][j] = dp;
	}
    }

    /* Copy the default paint table into the DRC paint table.  The DRC
     * paint table will be modified as we read the drc section.  Also
     * make sure that the error layer is super-persistent (once it
     * appears, it can't be gotten rid of by painting).  Also, make
     * some crossings automatically illegal:  two layers can't cross
     * unless the result of painting one on top of the other is to
     * get one of the layers, and it doesn't matter which is painted
     * on top of which.
     */
    
    for (plane = 0; plane < DBNumPlanes; plane++)
	for (i = 0; i < DBNumTypes; i++)
	    for (j = 0; j < DBNumTypes; j++)
	    {
		result = DBPaintResultTbl[plane][i][j];
		if ((i == TT_ERROR_S) || (j == TT_ERROR_S))
		    DRCCurStyle->DRCPaintTable[plane][i][j] = TT_ERROR_S;
		else if ((i == TT_SPACE) || (j == TT_SPACE)
			|| !DBTypeOnPlane(j, plane)
			|| !DBPaintOnTypePlanes(i, j))
		    DRCCurStyle->DRCPaintTable[plane][i][j] = result;

		/* Modified for stackable types (Tim, 10/3/03) */
		else if ((i >= DBNumUserLayers) ||
			((result >= DBNumUserLayers)
			&& (DBTechFindStacking(i, j) == result)))
		{
		    DRCCurStyle->DRCPaintTable[plane][i][j] = result;
		}
			
		else if ((!TTMaskHasType(&DBLayerTypeMaskTbl[i], result)
			&& !TTMaskHasType(&DBLayerTypeMaskTbl[j], result))
			|| ((result != DBPaintResultTbl[plane][j][i])
			&& DBTypeOnPlane(i, plane)
			&& DBPaintOnTypePlanes(j, i)))
		{
		    DRCCurStyle->DRCPaintTable[plane][i][j] = TT_ERROR_S;
		    /* TxError("Error: %s on %s, was %s\n",
				DBTypeLongNameTbl[i], DBTypeLongNameTbl[j],
				DBTypeLongNameTbl[result]); */
		}
		else
		    DRCCurStyle->DRCPaintTable[plane][i][j] = result;
	    }

    drcCifInit();
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCTechLine --
 *
 * Parse a line in the DRC section from the technology file.  Handle DRC
 * styles.  The rules themselves are handled by DRCTechAddRule.
 *
 * Results:
 *      TRUE if line parsed correctly; FALSE if fatal error condition
 *      encountered.
 *
 * Side effects:
 *	Appends information to the DRC tables.
 *
 * ----------------------------------------------------------------------------
 */

bool
DRCTechLine(sectionName, argc, argv)
    char *sectionName;		/* The name of this section */
    int argc;			/* Number of fields on the line */
    char *argv[];		/* Values of the fields */
{
    int j, l;
    DRCKeep *newStyle, *p;
    char *tptr, *cptr;

    if (argc <= 0) return TRUE;
    else if (argc >= 2) l = strlen(argv[1]);

    if (strcmp(argv[0], "style") == 0)
    {
	if (argc != 2)
	{
	    if ((argc != 4) || (strncmp(argv[2], "variant", 7)))
	    {
		wrongNumArgs:
		TechError("Wrong number of arguments in %s statement.\n",
			argv[0]);
		return TRUE;
	    }
	}
	for (newStyle = DRCStyleList; newStyle != NULL;
		newStyle = newStyle->ds_next)
	{
	    /* Here we're only establishing existence;  break on
	     * the first variant found.
	     */
	    if (!strncmp(newStyle->ds_name, argv[1], l))
		break;
	}
	if (newStyle == NULL)
	{
	    if (argc == 2)
	    {
		newStyle = (DRCKeep *)mallocMagic(sizeof(DRCKeep));
		newStyle->ds_next = NULL;
		newStyle->ds_name = StrDup((char **) NULL, argv[1]);

		/* Append to end of style list */
		if (DRCStyleList == NULL)
		    DRCStyleList = newStyle;
		else
		{
		    for (p = DRCStyleList; p->ds_next; p = p->ds_next);
		    p->ds_next = newStyle;
		}
	    }
	    else	/* Handle style variants */
	    {
		DRCKeep *saveStyle = NULL;

		/* 4th argument is a comma-separated list of variants */

		tptr = argv[3];
		while (*tptr != '\0')
		{
		    cptr = strchr(tptr, ',');
		    if (cptr != NULL) *cptr = '\0';
		    newStyle = (DRCKeep *)mallocMagic(sizeof(DRCKeep));
		    newStyle->ds_next = NULL;
		    newStyle->ds_name = (char *)mallocMagic(l
				+ strlen(tptr) + 1);
		    sprintf(newStyle->ds_name, "%s%s", argv[1], tptr);

		    /* Remember the 1st variant as the default */
		    if (saveStyle == NULL) saveStyle = newStyle;
	
		    /* Append to end of style list */
		    if (DRCStyleList == NULL)
			DRCStyleList = newStyle;
		    else
		    {
			for (p = DRCStyleList; p->ds_next; p = p->ds_next);
			p->ds_next = newStyle;
		    }

		    if (cptr == NULL)
			break;
		    else
			tptr = cptr + 1;
		}
		newStyle = saveStyle;
	    }
	}

	if (DRCCurStyle == NULL)   /* Shouldn't happen, but be safe. . .*/
	{
	    drcTechNewStyle();
	    DRCCurStyle->ds_name = newStyle->ds_name;
	    DRCCurStyle->ds_status = TECH_PENDING;
	}
	else if ((DRCCurStyle->ds_status == TECH_PENDING) ||
			(DRCCurStyle->ds_status == TECH_SUSPENDED))
	    DRCCurStyle->ds_status = TECH_LOADED;
	else if (DRCCurStyle->ds_status == TECH_NOT_LOADED)
	{
	    if (DRCCurStyle->ds_name == NULL) {
		DRCCurStyle->ds_name = newStyle->ds_name;
		DRCCurStyle->ds_status = TECH_PENDING;
	    }
	    else if (argc == 2)
	    {
		if (!strcmp(argv[1], DRCCurStyle->ds_name))
		    DRCCurStyle->ds_status = TECH_PENDING;
	    }
	    else if (argc == 4)
	    {
		/* Verify that the style matches one variant */

		if (!strncmp(DRCCurStyle->ds_name, argv[1], l))
		{
		    tptr = argv[3];
		    while (*tptr != '\0')
		    {
			cptr = strchr(tptr, ',');
			if (cptr != NULL) *cptr = '\0';
			if (!strcmp(DRCCurStyle->ds_name + l, tptr))
			{
			    DRCCurStyle->ds_status = TECH_PENDING;
			    return TRUE;
			}
			if (cptr == NULL)
			    return TRUE;
			else
			    tptr = cptr + 1;
		    }
		}
	    }
	}
	return (TRUE);
    }

    if (DRCCurStyle == NULL)
	return FALSE;

    /* For backwards compatibility, if we have encountered a line that	*/
    /* is not "style" prior to setting a style, then we create a style	*/
    /* called "default".						*/

    if (DRCStyleList == NULL)
    {
	char *locargv[2][10] = {"style", "default"};
	
	if (DRCTechLine(sectionName, 2, locargv) == FALSE)
	    return FALSE;
    }
    else if (DRCStyleList->ds_next == NULL)
    {
	/* On reload, if there is only one style, use it.  This is	*/
	/* necessary for the DRC-CIF extensions, since even though	*/
	/* the one-and-only DRC section may have been loaded, the DRC-	*/
	/* CIF parts of it become enabled or disabled depending on what	*/
	/* CIFCurStyle is active.					*/

	DRCCurStyle->ds_status = TECH_PENDING;
    }

    /* Only continue past this point if we are loading the DRC style */

    if ((DRCCurStyle->ds_status != TECH_PENDING) &&
		(DRCCurStyle->ds_status != TECH_SUSPENDED))
	return TRUE;

    /* Process "scalefactor" line next (if any) */

    if (strcmp(argv[0], "scalefactor") == 0)
    {
	int scaleN, scaleD;

	if (argc != 2 && argc != 3) goto wrongNumArgs;
	
	scaleN = atof(argv[1]);

	if (argc == 3)
	    scaleD = atof(argv[2]);
	else
	    scaleD = 1;

	if (scaleN <= 0 || scaleD <= 0)
	{
	    TechError("Scale factor must be greater than 0.\n");
	    TechError("Setting scale factor to default value 1.\n");
	    DRCCurStyle->DRCScaleFactorN = 1;
	    DRCCurStyle->DRCScaleFactorD = 1;
	    return TRUE;
	}
	DRCCurStyle->DRCScaleFactorN = scaleN;
	DRCCurStyle->DRCScaleFactorD = scaleD;
	return TRUE;
    }

    /* Process "variant" lines next. */

    if (strncmp(argv[0], "variant", 7) == 0)
    {
	/* If our style variant is not one of the ones declared */
	/* on the line, then we ignore all input until we	*/
	/* either reach the end of the style, the end of the 	*/
	/* section, or another "variant" line.			*/

	if (argc != 2) goto wrongNumArgs;
	tptr = argv[1];
	while (*tptr != '\0')
	{
	    cptr = strchr(tptr, ',');
	    if (cptr != NULL)
	    {
		*cptr = '\0';
		for (j = 1; isspace(*(cptr - j)); j++)
		    *(cptr - j) = '\0';
	    }

	    if (*tptr == '*')
	    {
		DRCCurStyle->ds_status = TECH_PENDING;
		return TRUE;
	    }
	    else
	    {
		l = strlen(DRCCurStyle->ds_name) - strlen(tptr);
		if (!strcmp(tptr, DRCCurStyle->ds_name + l))
		{
		    DRCCurStyle->ds_status = TECH_PENDING;
		    return TRUE;
		}
	    }

	    if (cptr == NULL)
		break;
	    else
		tptr = cptr + 1;
	}
	DRCCurStyle->ds_status = TECH_SUSPENDED;
    }

    /* Anything below this line is not parsed if we're in TECH_SUSPENDED mode */
    if (DRCCurStyle->ds_status != TECH_PENDING) return TRUE;

    return DRCTechAddRule(sectionName, argc, argv);
}

void
drcAssign(cookie, dist, next, mask, corner, why, cdist, flags, planeto, planefrom)
    DRCCookie *cookie, *next;
    int dist, cdist;
    TileTypeBitMask *mask, *corner;
    char *why;
    int flags, planeto, planefrom;
{
    /* Diagnostic */
    if (planeto >= DBNumPlanes) {
	TxError("Bad plane in DRC assign!\n");
    }
    (cookie)->drcc_dist = dist;
    (cookie)->drcc_next = next;
    (cookie)->drcc_mask = *mask;
    (cookie)->drcc_corner = *corner;
    (cookie)->drcc_why = why;
    (cookie)->drcc_cdist = cdist;
    (cookie)->drcc_flags = flags;
    (cookie)->drcc_edgeplane = planefrom;
    (cookie)->drcc_plane = planeto;
    (cookie)->drcc_mod = 0;
    (cookie)->drcc_cmod = 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCTechAddRule --
 *
 * Add a new entry to the DRC table.
 *
 * Results:
 *	Always returns TRUE so that tech file read-in doesn't abort.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * Organization:
 *	We select a procedure based on the first keyword (argv[0])
 *	and call it to do the work of implementing the rule.  Each
 *	such procedure is of the following form:
 *
 *	int
 *	proc(argc, argv)
 *	    int argc;
 *	    char *argv[];
 *	{
 *	}
 *
 * 	It returns the distance associated with the design rule,
 *	or -1 in the event of a fatal error that should cause
 *	DRCTechAddRule() to return FALSE (currently, none of them
 *	do, so we always return TRUE).  If there is no distance
 *	associated with the design rule, 0 is returned.
 *
 * ----------------------------------------------------------------------------
 */


	/* ARGSUSED */
bool
DRCTechAddRule(sectionName, argc, argv)
    char *sectionName;		/* Unused */
    int argc;
    char *argv[];
{
    int which, distance, mdist;
    char *fmt;
    static struct
    {
	char	*rk_keyword;	/* Initial keyword */
	int	 rk_minargs;	/* Min # arguments */
	int	 rk_maxargs;	/* Max # arguments */
	int    (*rk_proc)();	/* Procedure implementing this keyword */
	char	*rk_err;	/* Error message */
    } ruleKeys[] = {
	"angles",	 4,	4,	drcAngles,
    "layers 45|90 why",
	"edge",		 8,	9,	drcEdge,
    "layers1 layers2 distance okTypes cornerTypes cornerDistance why [plane]",
	"edge4way",	 8,	9,	drcEdge,
    "layers1 layers2 distance okTypes cornerTypes cornerDistance why [plane]",
	"exact_overlap", 2,	2,	drcExactOverlap,
    "layers",
	"extend",	 5,	6,	drcExtend,
    "layers1 layers2 distance why",
	"no_overlap",	 3,	3,	drcNoOverlap,
    "layers1 layers2",
	"overhang",	 5,	5,	drcOverhang,
    "layers1 layers2 distance why",
	"rect_only",	 3,	3,	drcRectOnly,
    "layers why",
	"spacing",	 6,	7,	drcSpacing,
    "layers1 layers2 separation [layers3] adjacency why",
	"stepsize",	 2,	2,	drcStepSize,
    "step_size",
	"surround",	 6,	6,	drcSurround,
    "layers1 layers2 distance presence why",
	"width",	 4,	4,	drcWidth,
    "layers width why",
	"widespacing",	 7,	7,	drcSpacing,
    "layers1 width layers2 separation adjacency why",
        "area",		 5,	5,	drcArea,
    "layers area horizon why",
        "maxwidth",	 4,	5,	drcMaxwidth,
    "layers maxwidth bends why",
	"cifstyle",	 2,	2,	drcCifSetStyle,
    "cif_style",
	"cifwidth",	 4,	4,	drcCifWidth,
    "layers width why",
	"cifspacing",	 6,	6,	drcCifSpacing,
    "layers1 layers2 separation adjacency why",
	"cifarea",	 5,	5,	drcCifArea,
    "layers area horizon why",
	"cifmaxwidth",	 5,	5,	drcCifMaxwidth,
    "layers maxwidth bends why",
	"rectangle",	5,	5,	drcRectangle,
    "layers maxwidth [even|odd|any] why",
	0
    }, *rp;

    drcRulesSpecified += 1;

    which = LookupStruct(argv[0], (LookupTable *) ruleKeys, sizeof ruleKeys[0]);
    if (which < 0)
    {
	TechError("Bad DRC rule type \"%s\"\n", argv[0]);
	TxError("Valid rule types are:\n");
	for (fmt = "%s", rp = ruleKeys; rp->rk_keyword; rp++, fmt = ", %s")
	    TxError(fmt, rp->rk_keyword);
	TxError(".\n");
	return (TRUE);
    }
    rp = &ruleKeys[which];
    if (argc < rp->rk_minargs || argc > rp->rk_maxargs)
    {
	TechError("Rule type \"%s\" usage: %s %s\n",
		rp->rk_keyword, rp->rk_keyword, rp->rk_err);
	return (TRUE);
    }

    distance = (*rp->rk_proc)(argc, argv);
    if (distance < 0)
	return (FALSE);

    /* Update the halo to be the maximum distance (in magic units) of	*/
    /* any design rule							*/

    mdist = distance;

    if (mdist > DRCTechHalo)
	DRCTechHalo = mdist;

    return (TRUE);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcExtend --
 *
 * Process an extension rule.
 * This is of the form:
 *
 *	extend layers1 layers2 distance [exact_width] why
 *
 * indicating that if "layers1" extends from "layers2", then it must
 * have a width of at least "distance".  This is very much like the
 * "overhang" rule, except that the extension is optional.  Example:
 *
 *	extend nfet ndiff 2 "n-transistor length must be at least 2"
 *
 * "extend" implements the following general-purpose edge rule:
 *
 *	edge4way layers2 layers1 distance layers1 0 0 why
 *
 * Option "exact_width" implements an additional rule that checks for
 * maximum extension at distance.  This is intended for use with, for
 * example, a fixed gate length for a specific type of device.
 *
 * Results:
 *	Returns distance.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcExtend(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1];
    char *layers2 = argv[2];
    int distance = atoi(argv[3]);
    char *why;
    TileTypeBitMask set1, setC;
    DRCCookie *dp, *dpnew, *dptrig;
    TileType i, j;
    int plane, plane2;
    TileTypeBitMask set2, setZ, setN, setM;
    PlaneMask pMask1, pMask2, pset, ptest;
    bool exact = FALSE;

    if (!strncmp(argv[4], "exact_", 6))
    {
	exact = TRUE;
	why = drcWhyDup(argv[5]);
    }
    else
	why = drcWhyDup(argv[4]);

    ptest = DBTechNoisyNameMask(layers1, &set1);
    pMask1 = CoincidentPlanes(&set1, ptest);
    
    if (pMask1 == 0)
    {
	TechError("All layers in first set for \"extend\" must be on "
			"the same plane\n");
	return (0);
    }

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pMask2 = CoincidentPlanes(&set2, ptest);

    if (pMask2 == 0)
    {
	TechError("All layers in second set for \"extend\" must be on "
			"the same plane\n");
	return (0);
    }

    /* setM is the union of set1 and set2 */
    TTMaskZero(&setM);
    TTMaskSetMask3(&setM, &set1, &set2);

    /* setN is the inverse of set1, and setC is the inverse of set2 */
    TTMaskCom2(&setN, &set1);
    TTMaskCom2(&setC, &set2);

    /* Zero mask */
    TTMaskZero(&setZ);

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    if (pset = (DBTypesOnSamePlane(i, j) & pMask2))
	    {
		/* Edge depends on whether or not the extension is 	*/
		/* on the same plane as the layer from which it is	*/
		/* measured.						*/

		if ((pset & pMask1) != 0)
		{
		    if (TTMaskHasType(&set2, i) && TTMaskHasType(&set1, j))
		    {
			plane = LowestMaskBit(pset & pMask1);

			/* find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &setM, &setZ, why,
				    0, DRC_FORWARD, plane, plane);

			dp->drcc_next = dpnew;

			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &setM, &setZ, why,
				    0, DRC_REVERSE, plane, plane);
	
			dp->drcc_next = dpnew;

			if (exact)
			{
			    /* find bucket preceding the new one we wish to insert */
			    dp = drcFindBucket(i, j, distance);
			    dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			    drcAssign(dpnew, distance, dp->drcc_next, &setN, &setZ, why,
				    0, DRC_FORWARD | DRC_OUTSIDE, plane, plane);

			    dp->drcc_next = dpnew;

			    dp = drcFindBucket(j, i, distance);
			    dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			    drcAssign(dpnew, distance, dp->drcc_next, &setN, &setZ, why,
				    0, DRC_REVERSE | DRC_OUTSIDE, plane, plane);
	
			    dp->drcc_next = dpnew;
			}
		    }
		}
		else	/* Multi-plane extend rule */
		{
		    if (TTMaskHasType(&set2, i) && TTMaskHasType(&setC, j))
		    {
			plane = LowestMaskBit(pset);
			plane2 = LowestMaskBit(pMask1);

			/* find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &setM, &setZ, why,
				    0, DRC_FORWARD, plane2, plane);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, 1, dpnew, &setN, &setZ, why,
				    0, DRC_FORWARD | DRC_TRIGGER, plane2, plane);
			dp->drcc_next = dptrig;

			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &setM, &setZ, why,
				    0, DRC_REVERSE, plane2, plane);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, 1, dpnew, &setN, &setZ, why,
				    0, DRC_REVERSE | DRC_TRIGGER, plane2, plane);
			dp->drcc_next = dptrig;
		    }
		}
	    }
	}
    }
    return (distance);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcWidth --
 *
 * Process a width rule.
 * This is of the form:
 *
 *	width layers distance why
 *
 * e.g,
 *
 *	width poly,pmc 2 "poly width must be at least 2"
 *
 * Optional "from layers2" is useful when defining a device width;
 * effectively, it represents an overhang rule where the presence of
 * the overhanging material is optional.  The equivalent rule is:
 *
 *	edge4way layers2 layers distance layers 0 0 why
 *
 *
 * Results:
 *	Returns distance.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcWidth(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    int distance = atoi(argv[2]);
    char *why = drcWhyDup(argv[3]);
    TileTypeBitMask set, setC;
    PlaneMask pmask, pset, ptest;
    DRCCookie *dp, *dpnew;
    TileType i, j;
    int plane;

    ptest = DBTechNoisyNameMask(layers, &set);
    pmask = CoincidentPlanes(&set, ptest);
    TTMaskCom2(&setC, &set);

    if (pmask == 0)
    {
	TechError("All layers for \"width\" must be on same plane\n");
	return (0);
    }

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    /*
	     * Must have types in 'set' for at least 'distance'
	     * to the right of any edge between a type in '~set'
	     * and a type in 'set'.
	     */

	    if (pset = (DBTypesOnSamePlane(i, j) & pmask))
	    {
		if (TTMaskHasType(&setC, i) && TTMaskHasType(&set, j))
		{
		    plane = LowestMaskBit(pset);

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, distance, dp->drcc_next, &set, &set,
				why, distance, DRC_FORWARD, plane, plane);
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return (distance);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcArea --
 *
 * Process an area rule.
 * This is of the form:
 *
 *	area layers area horizon why
 *
 * e.g,
 *
 *	area pmc 4 2 "poly contact area must be at least 4"
 *
 * "area" is the total area in lambda^2.
 *
 * "horizon" is the halo distance for the check.  Normally, this would
 * be the area (in lambda^2) divided by the minimum width rule (in
 * lambda).  Anything larger would not be a violation as long as the
 * minimum width rule is satisfied.
 *
 * Results:
 *	Returns distance.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcArea(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    int distance = atoi(argv[2]);
    int	horizon = atoi(argv[3]);
    char *why = drcWhyDup(argv[4]);
    TileTypeBitMask set, setC;
    DRCCookie *dp, *dpnew;
    TileType i, j;
    PlaneMask pmask, ptest, pset;
    int plane;

    ptest = DBTechNoisyNameMask(layers, &set);
    pmask = CoincidentPlanes(&set, ptest);
    TTMaskCom2(&setC, &set);

    if (pmask == 0)
    {
	TechError("All layers for \"area\" must be on same plane\n");
	return (0);
    }

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    /*
	     * Must have types in 'set' for at least 'distance'
	     * to the right of any edge between a type in '~set'
	     * and a type in 'set'.
	     */
	    if (pset = (DBTypesOnSamePlane(i, j) & pmask))
	    {
		if (TTMaskHasType(&setC, i) && TTMaskHasType(&set, j))
		{
		    plane = LowestMaskBit(pset);

		    /* find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, horizon);
		    dpnew = (DRCCookie *) mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, horizon, dp->drcc_next, &set, &set, why,
			    distance, DRC_AREA|DRC_FORWARD, plane, plane);
	
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return (horizon);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcMaxwidth --
 *
 * Process a maxwidth rule.
 * This is of the form:
 *
 *	maxwidth layers distance [bends] why
 *
 * This routine was updated 3/6/05 to match the "canonical" definition of
 * a maxwidth region, which is any rectangle containing <layers> that is
 * at least <distance> in both width and height.  If the keyword
 * "bend_illegal" is present, then the definition reverts to the original
 * (see below) for backwards-compatibility.  Otherwise ("bend_ok" or
 * nothing), the new routine is used.
 *
 *	maxwidth metal1 389 "metal1 width > 35um must be slotted"
 *	maxwidth pmc 4 bend_illegal "poly contact area must be no wider than 4"
 *	maxwidth trench 4 bend_ok "trench width must be exactly 4"
 *
 *      bend_illegal - means that one_dimension must be distance for any 
 *  		point in the region.  This is used for emitters and contacts
 *		that are rectangular (so we can't generate them with the
 *		squares command) and some exact width in one direction.
 *	bend_ok - Used mainly for wide metal rules where metal greater than
 *		some given width must be slotted.  Also, used for things
 *		like trench, where the width is some fixed value:
 *
 *			XXXXX		XXXXXX
 *			X   X		XXXXXX
 *			X   X		X    X
 *			XXXXX		XXXXXX
 *			
 *			 OK		 BAD		
 *
 * Results:
 *	Returns distance.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcMaxwidth(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    int distance = atoi(argv[2]);
    char *bends = argv[3];
    char *why;
    TileTypeBitMask set, setC;
    DRCCookie *dp, *dpnew;
    TileType i, j;
    PlaneMask pmask, ptest, pset;
    int plane;
    int bend;

    ptest = DBTechNoisyNameMask(layers, &set);
    pmask = CoincidentPlanes(&set, ptest);
    TTMaskCom2(&setC, &set);

    if (pmask == 0)
    {
	TechError("All layers for \"maxwidth\" must be on same plane\n");
	return (0);
    }

    if (argc == 4)
    {
	/* "bends" is irrelevent if distance is zero, so choose the	*/
	/* faster algorithm to process					*/

	if (distance == 0)
	    bend = 0;
	else
	    bend = DRC_BENDS;
	why = drcWhyDup(argv[3]);
    }
    else
    {
	if (strcmp(bends,"bend_illegal") == 0) bend = 0;
	else if (strcmp(bends,"bend_ok") == 0) bend = DRC_BENDS;
	else
	{
	    TechError("unknown bend option %s\n",bends);
	    return (0);
	}
	why = drcWhyDup(argv[4]);
    }

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    /*
	     * Must have types in 'set' for at least 'distance'
	     * to the right of any edge between a type in '~set'
	     * and a type in 'set'.
	     */
	    if (pset = (DBTypesOnSamePlane(i, j) & pmask))
	    {
		if (TTMaskHasType(&setC, i) && TTMaskHasType(&set, j))
		{
		    plane = LowestMaskBit(pset);

		    /* find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *) mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, distance, dp->drcc_next, &set, &set, why,
				    distance, DRC_MAXWIDTH | bend, plane, plane);
	
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return (distance);
}

/*
 * ----------------------------------------------------------------------------
 * drcAngles --
 *
 * Process an "angles" rule specifying that geometry for a certain layer
 * must be limited to 90 degrees or 45 degrees.  If not specified, any
 * angle is allowed, although width rules will flag errors on acute angles.
 *
 * ----------------------------------------------------------------------------
 */

int
drcAngles(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    int angles = atoi(argv[2]);
    char *why = drcWhyDup(argv[3]);
    TileTypeBitMask set;
    DRCCookie *dp, *dpnew;
    int plane;
    TileType i, j;

    DBTechNoisyNameMask(layers, &set);

    angles /= 45;
    angles--;		/* angles now 0 for 45s and 1 for 90s */

    if ((angles != 0) && (angles != 1))
    {
	TechError("angles must be 45 or 90\n");
	return 0;
    }

    for (i = 0; i < DBNumTypes; i++)
    {
	if (TTMaskHasType(&set, i))
	{
	    plane = DBPlane(i);

	    /* Insert rule at boundary of tile and TT_SPACE.  This is	*/
	    /* processed for each tile, separately from other rules, so	*/
	    /* we don't really care what the edge is;  TT_SPACE is	*/
	    /* chosen as an arbitrary place to hold the rule.		*/

	    dp = drcFindBucket(TT_SPACE, i, 1);
	    dpnew = (DRCCookie *) mallocMagic((unsigned) (sizeof (DRCCookie)));
	    drcAssign(dpnew, 1, dp->drcc_next, &set, &set, why,
			1, DRC_ANGLES | angles, plane, plane);
	    dp->drcc_next = dpnew;
	}
    }
    return 1;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcSpacing3 --
 *
 * Process a special spacing rule of the form:
 *
 *	spacing layers1 layers2 distance corner_ok layers3 why
 *
 * This spacing rule is not checked when a type from "layers3" fills the
 * corner between "layers1" and "layers2".  This is used, e.g., for
 * diffusion-to-poly spacing, where diffusion and poly may touch at the
 * corner of a FET type.  It is equivalent to:
 *
 *	edge4way layers1 ~(layers3,layers1) distance ~(layers2) 0 0 why
 *
 * ----------------------------------------------------------------------------
 */

int
drcSpacing3(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1], *layers2 = argv[2];
    char *layers3 = argv[5];
    int distance = atoi(argv[3]);
    char *adjacency = argv[4];
    char *why = drcWhyDup(argv[6]);
    TileTypeBitMask set1, set2, set3;
    int plane;
    DRCCookie *dp, *dpnew;
    TileType i, j;
    PlaneMask pmask, pset, ptest;

    ptest = DBTechNoisyNameMask(layers1, &set1);
    pmask = CoincidentPlanes(&set1, ptest);

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pmask &= CoincidentPlanes(&set2, ptest);

    ptest = DBTechNoisyNameMask(layers3, &set3);
    pmask &= CoincidentPlanes(&set3, ptest);

    if (pmask == 0)
    {
	TechError("Spacing check with \"corner_ok\" must have"
			" all types in one plane.\n");
	return (0);
    }

    /* In this usage everything must fall in the same plane. */

    /* We need masks for (~types2)/plane and for (~(types1,types3))/plane */

    TTMaskCom(&set2);
    TTMaskSetMask(&set3, &set1);
    TTMaskCom(&set3);

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    if (pset = (DBTypesOnSamePlane(i, j) & pmask))
	    {
		if (TTMaskHasType(&set1, i) && TTMaskHasType(&set3, j))
		{
		    plane = LowestMaskBit(pset);

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, distance, dp->drcc_next, &set2, &set3,
				why, distance, DRC_FORWARD | DRC_BOTHCORNERS,
				plane, plane);
		    dp->drcc_next = dpnew;

		    /* find bucket preceding new one we wish to insert */
		    dp = drcFindBucket(j, i, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, distance, dp->drcc_next, &set2, &set3,
				why, distance, DRC_REVERSE | DRC_BOTHCORNERS,
				plane, plane);
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return distance;
}

/*
 *-------------------------------------------------------------------
 *
 * drcMaskSpacing ---
 *
 *	This is the core of the drcSpacing routine.  When the spacing
 *	rule layers independently cover more than one plane, this routine
 *	is invoked for each independent plane.
 *
 * Results:
 *	Returns the rule's maximum distance
 *
 * Side effects:
 *	Adds rules to the DRC rule table.
 *	
 *-------------------------------------------------------------------
 */

int
drcMaskSpacing(set1, set2, pmask1, pmask2, wwidth, distance, adjacency,
		why, widerule, multiplane)
    TileTypeBitMask *set1, *set2;
    PlaneMask pmask1, pmask2;
    int wwidth, distance;
    char *adjacency, *why;
    bool widerule, multiplane;
{
    TileTypeBitMask tmp1, tmp2, setR, setRreverse;
    int plane, plane2;
    PlaneMask pset, ptest;
    DRCCookie *dp, *dpnew;
    int needReverse = (widerule) ? TRUE : FALSE;
    TileType i, j, pref;
    bool needtrigger = FALSE;
    bool touchingok = TRUE;
    bool cornerok = FALSE;

    if (!strcmp(adjacency, "surround_ok"))
    {
	if (multiplane)
	{
	    TechError("\"surround_ok\" requires surrounding types to "
		"be in the same plane.\n");
	    return (0);
	}
	else if ((pmask1 & pmask2) == 0)
	{
	    /* New rule implementation (7/8/06):  When types	*/
	    /* are on different planes and "surround_ok" is	*/
	    /* declared, implement as a triggered rule (but not	*/
	    /* for widespacing, which already uses the		*/
	    /* triggering rule mechanism).			*/

	    if (!widerule)
	    {
		needtrigger = TRUE;
		touchingok = FALSE;

	    }
	    else
	    {
		TechError("Widespacing checks cannot use \"surround_ok\".\n");
		return (0);
	    }
	}
	else
	{
	    TechError("\"surround_ok\" used when spacing rule types are in "
			"the same plane.  Did you mean \"touching_ok\"?\n");
	    touchingok = TRUE;	/* Treat like "touching_ok" */
	}
    }
    else if (!strcmp(adjacency, "touching_ok"))
    {
	/* If touching is OK, everything must fall in the same plane. */
	if (multiplane || ((pmask1 & pmask2) == 0))
	{
	    TechError("Spacing check with \"touching_ok\" must have"
			" all types in one plane.  Possibly you want"
			" \"surround_ok\"?\n");
	    return (0);
	}
	else
	    touchingok = TRUE;
    }
    else if (!strcmp(adjacency, "touching_illegal"))
    {
	touchingok = FALSE;
	needtrigger = FALSE;
    }
    else
    {
	TechError("Badly formed drc spacing line:  need \"touching_ok\", "
			"\"touching_illegal\", or \"surround_ok\".\n");
	return (0);
    }

    if (touchingok)
    {
	/* In "touching_ok rules, spacing to set2  is be checked in FORWARD 
	 * direction at edges between set1 and  (setR = ~set1 AND ~set2).
	 *
	 * In addition, spacing to set1 is checked in FORWARD direction 
	 * at edges between set2 and (setRreverse = ~set1 AND ~set2).
	 *
	 * If set1 and set2 are different, above are checked in REVERSE as
	 * well as forward direction.  This is important since touching
	 * material frequently masks violations in one direction.
	 *
	 * setR and setRreverse are set appropriately below.
	 */

	tmp1 = *set1;
	tmp2 = *set2; 

	/* Restrict planes to those that are coincident */
	pmask1 &= pmask2;
	pmask2 = pmask1;
	TTMaskCom(&tmp1);
	TTMaskCom(&tmp2);
	TTMaskAndMask(&tmp1, &tmp2);
	setR = tmp1;
	setRreverse = tmp1;

	/* If set1 != set2, set flag to check rules in both directions */
	if (!TTMaskEqual(set1, set2))
	    needReverse = TRUE;
    }
    else
    {
	/* In "touching_illegal" rules, spacing to set2 will be checked
	 * in FORWARD direction at edges between set1 and (setR=~set1). 
	 *
	 * In addition, spacing to set1 will be checked in FORWARD direction
	 * at edges between set2 and (setRreverse=  ~set2).
	 *
	 * setR and setRreverse are set appropriately below.
	 */
	TTMaskCom2(&setR, set1);
	TTMaskCom2(&setRreverse, set2);
    }

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    if (pset = (DBTypesOnSamePlane(i, j) & pmask1))
	    {
		plane = LowestMaskBit(pset);

		/* LHS is an element of set1, RHS is an element of setR */
		if (TTMaskHasType(set1, i) && TTMaskHasType(&setR, j))
		{
		    plane2 = LowestMaskBit(pmask2);

		    /*
		     * Must not have 'set2' for 'distance' to the right of
		     * an edge between 'set1' and the types not in 'set1'
		     * (touching_illegal case) or in neither
		     * 'set1' nor 'set2' (touching_ok case).
		     */

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);

		    dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
		    TTMaskClearMask3(&tmp1, &DBPlaneTypes[plane2], set2);
		    TTMaskAndMask3(&tmp2, &DBPlaneTypes[plane], &setR);

		    if (widerule)
		    {
			DRCCookie *dptrig;

			/* Create two contiguous rules, one for spacing	*/
			/* and one for width.  These are created in	*/
			/* reverse order due to the stack property of	*/
			/* the linked list.				*/

			drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
				why, distance, DRC_FORWARD, plane2, plane);
			dptrig = (DRCCookie *) mallocMagic((unsigned)
				(sizeof (DRCCookie)));
			drcAssign(dptrig, wwidth, dpnew, set1, set1, why,
				wwidth, DRC_REVERSE | DRC_MAXWIDTH |
				DRC_TRIGGER | DRC_BENDS, plane2, plane);

			dp->drcc_next = dptrig;
		    }
		    else if (needtrigger)
		    {
			DRCCookie *dptrig;
			
			/* Create two contiguous spacing rules */

			drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
			 	why, wwidth, DRC_FORWARD | DRC_BOTHCORNERS,
				plane2, plane);
			dptrig = (DRCCookie *) mallocMagic((unsigned)
				(sizeof (DRCCookie)));
			drcAssign(dptrig, 1, dpnew, set2, &tmp2, why, 1,
			 	DRC_FORWARD | DRC_TRIGGER,
				plane2, plane);

			dp->drcc_next = dptrig;
		    }
		    else
		    {
			drcAssign(dpnew, distance, dp->drcc_next, &tmp1,
				&tmp2, why, wwidth, DRC_FORWARD, plane2, plane);
			dp->drcc_next = dpnew;
		    }

		    if (needReverse)
			dpnew->drcc_flags |= DRC_BOTHCORNERS;

		    if (needReverse)
		    {
			/* Add check in reverse direction, 
			 * NOTE:  am assuming single plane rule here (since reverse
			 * rules only used with touching_ok which must be 
			 * single plane)
			 */
			 
			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
			if (widerule)
			{
			    DRCCookie *dptrig;

			    /* Assign two coupled rules (see above) */

			    drcAssign(dpnew, distance, dp->drcc_next, &tmp1,
					&tmp2, why, distance,
					DRC_REVERSE | DRC_BOTHCORNERS, plane2, plane);
			    dptrig = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
			    drcAssign(dptrig, wwidth, dpnew, set1, set1, why,
					wwidth, DRC_FORWARD | DRC_MAXWIDTH |
					DRC_TRIGGER | DRC_BENDS, plane2, plane);
			    dp->drcc_next = dptrig;
			}
			else if (needtrigger)
			{
			    DRCCookie *dptrig;
			
			    /* Create two contiguous spacing rules */

			    drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
			 		why, wwidth, DRC_REVERSE | DRC_BOTHCORNERS,
					plane2, plane);
			    dptrig = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
			    drcAssign(dptrig, 1, dpnew, set2, &tmp2, why, 1,
			 		DRC_REVERSE | DRC_TRIGGER,
					plane2, plane);

			    dp->drcc_next = dptrig;
			}
			else
			{
			    drcAssign(dpnew,distance,dp->drcc_next,
					&tmp1, &tmp2, why, wwidth, 
					DRC_REVERSE | DRC_BOTHCORNERS, plane2, plane);
			    dp->drcc_next = dpnew;
		 	}
		    }
		}
	    }

	    if (TTMaskEqual(set1, set2)) continue;
	    if (widerule) continue;	/* Can't determine width of set1    */
					/* when looking at the edge of set2 */

	    /*
	     * Now, if set1 and set2 are distinct apply the rule for LHS in set1
	     * and RHS in set2.
	     */

	    if (pset = (DBTypesOnSamePlane(i, j) & pmask2))
	    {
		plane = LowestMaskBit(pset);

		/* LHS is an element of set2, RHS is an element of setRreverse */
		if (TTMaskHasType(set2, i) && TTMaskHasType(&setRreverse, j))
		{
		    plane2 = LowestMaskBit(pmask1);

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    TTMaskClearMask3(&tmp1, &DBPlaneTypes[plane2], set1);
		    TTMaskAndMask3(&tmp2, &DBPlaneTypes[plane], &setRreverse);

		    if (needtrigger)
		    {
			DRCCookie *dptrig;
			
			/* Create two contiguous spacing rules */

		        drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
					why, distance, DRC_FORWARD | DRC_BOTHCORNERS,
					plane2, plane);
			dptrig = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
			drcAssign(dptrig, 1, dpnew, set1, &tmp2, why, 1,
			 		DRC_FORWARD | DRC_TRIGGER,
					plane2, plane);
			dp->drcc_next = dptrig;
		    }
		    else
		    {
			drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
					why, distance, DRC_FORWARD, plane2, plane);
			dp->drcc_next = dpnew;		    
		    }

		    if (needReverse)
			dpnew->drcc_flags |= DRC_BOTHCORNERS;

		    if (needReverse)
		    {
			/* Add check in reverse direction, 
			 * NOTE:  am assuming single plane rule here (since reverse
			 * rules only used with touching_ok which must be 
			 * single plane)
			 */
			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *) mallocMagic(sizeof (DRCCookie));
			 
			if (needtrigger)
			{
			    DRCCookie *dptrig;
			
			    /* Create two contiguous spacing rules */

		            drcAssign(dpnew, distance, dp->drcc_next, &tmp1, &tmp2,
					why, distance, DRC_REVERSE | DRC_BOTHCORNERS,
					plane2, plane);
			    dptrig = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
			    drcAssign(dptrig, 1, dpnew, set1, &tmp2, why, 1,
			 		DRC_REVERSE | DRC_TRIGGER,
					plane2, plane);
			    dp->drcc_next = dptrig;
			}
			else
			{
			    drcAssign(dpnew, distance, dp->drcc_next,
					&tmp1, &tmp2, why, distance, 
					DRC_REVERSE | DRC_BOTHCORNERS, plane2, plane);
			    dp->drcc_next = dpnew;
			}
		    }
		}
	    }

	    /* Finally, if multiplane rule then check that set2 types
	     * are not present just to right of edges with setR on LHS
	     * and set1 on RHS.  This check is necessary to make sure
	     * that a set1 rectangle doesn't coincide exactly with a
	     * set2 rectangle.  
	     * (This check added by Michael Arnold on 4/10/86.)
	     */

	    if (needtrigger) continue; 

	    if (pset = (DBTypesOnSamePlane(i, j) & pmask1))
	    {
		plane = LowestMaskBit(pset);

		/* LHS is an element of setR, RHS is an element of set1 */
		if (TTMaskHasType(&setR, i) && TTMaskHasType(set1, j))
		{
		    /*
		     * Must not have 'set2' for 'distance' to the right of
		     * an edge between the types not in set1 and set1.
		     * (is only checked for cross plane rules - these are
		     * all of type touching_illegal)
		     */

		    /* Walk list to last check.  New checks ("cookies") go
		     * at end of list since we are checking for distance of
		     * 1 and the list is sorted in order of decreasing distance.
		     */
		    for (dp = DRCCurStyle->DRCRulesTbl [i][j];
		    		dp->drcc_next != (DRCCookie *) NULL;
		    		dp = dp->drcc_next); /* null body */

		    /* Insert one check for each plane involved in set2 */
		    plane2 = LowestMaskBit(pmask2);

		    /* filter out checks that are not cross plane */
		    if (i == TT_SPACE) 
		    {
			if (DBTypeOnPlane(j, plane2))
			    continue;
		    }
		    else
		    {
			if (DBTypeOnPlane(i, plane2))
			    continue;
		    }

		    /* create new check and add it to list */
		    dpnew = (DRCCookie *) mallocMagic(sizeof (DRCCookie));
		    TTMaskClearMask3(&tmp1, &DBPlaneTypes[plane2], set2);
		    TTMaskZero(&tmp2);

		    drcAssign(dpnew, 1, dp->drcc_next, &tmp1, &tmp2, why,
				distance, DRC_FORWARD, plane2, plane);
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return (MAX(wwidth, distance));
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcSpacing --
 *
 * Process a spacing rule.
 * This is of the form:
 *
 *	spacing layers1 layers2 distance adjacency why
 *
 * e.g,
 *
 *	spacing metal,pmc/m,dmc/m metal,pmc/m,dmc/m 4 touching_ok \
 *		"metal spacing must be at least 4"
 *
 * Adjacency may be either "touching_ok" or "touching_illegal"
 * In the first case, no violation occurs when types in layers1 are
 * immediately adjacent to types in layers2.  In the second case,
 * such adjacency causes a violation.
 *
 * Results:
 *	Returns distance.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * Notes:
 * Extended to include the rule syntax:
 *
 *	widespacing layers1 width layers2 distance adjacency why
 *
 * This extension covers rules such as "If m1 width > 10um, then spacing to
 * unconnected m1 must be at least 0.6um".  This assumes that the instantiated
 * edge4way rule is a standard "spacing" rule in which "dist" and "cdist"
 * (corner extension) distances are always the same, so we can use the "cdist"
 * record to encode the width of "layers1" which triggers the rule.  We re-use
 * the CheckMaxwidth() code, but with the following differences: 1) it does not
 * trigger any error painting functions, 2) it returns a value stating whether
 * the maxwidth rule applies or not, 3) it uses the DBLayerTypeMaskTbl[] for
 * the tile type in question, not "oktypes", for its search, and 4) it uses
 * the max width in the "cdist" record, not the "dist" record, for the max
 * width rule.  The "dist" record is copied to "cdist" before actually applying
 * the spacing rule, so that the rule acts like a proper spacing rule.
 *
 * Added adjacency rule "surround_ok" to check spacing to a type that
 * may also abut or surround the first layer---the portion of the layer
 * that is abutting or surrounding is not checked.  This allows checks
 * of, e.g., diffusion in well to a separate well edge, or distance from
 * a poly bottom plate to unconnected poly.
 *
 * (Added 11/6/06) Adjacency may be "corner_ok", in which case it calls
 * routing drcSpacing3() (see above).
 *
 * ----------------------------------------------------------------------------
 */

int
drcSpacing(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1], *layers2;
    char *adjacency;
    char *why;
    TileTypeBitMask set1, set2, tmp1, tmp2;
    PlaneMask pmask1, pmask2, pmaskA, pmaskB, ptest;
    int wwidth, distance, plane, plane2;
    bool widerule, multiplane = FALSE;

    if ((argc == 7) && (!strcmp(argv[4], "corner_ok")))
	return drcSpacing3(argc, argv);

    widerule = (strncmp(argv[0], "wide", 4) == 0);

    if (widerule)
    {
	wwidth = atoi(argv[2]);
	layers2 = argv[3];
	distance = atoi(argv[4]);
	adjacency = argv[5];
	why = drcWhyDup(argv[6]);
	/* TxPrintf("Info:  DRCtech:  widespacing rule for %s width %d:"
		" spacing must be %d\n", layers1, wwidth, distance); */
    }
    else
    {
	layers2 = argv[2];
	distance = atoi(argv[3]);
	adjacency = argv[4];
	wwidth = distance;
	why = drcWhyDup(argv[5]);
	if (argc == 7)
	{
	    TechError("Unknown argument in spacing line.\n");
	    return(0);
	}
    }

    /* Either list of types may contain independent types on different	*/
    /* planes.  However, if so, then we may not use touching_ok, and	*/
    /* there are other restrictions.					*/

    ptest = DBTechNoisyNameMask(layers1, &set1);
    pmask1 = CoincidentPlanes(&set1, ptest);

    if ((pmask1 == 0) && (ptest != 0))
    {
	pmask1 = ptest;
	multiplane = TRUE;
	for (plane = 0; plane < DBNumPlanes; plane++)
	    for (plane2 = 0; plane2 < DBNumPlanes; plane2++)
	    {
		if (plane == plane2) continue;
		if (PlaneMaskHasPlane(pmask1, plane) &&
			PlaneMaskHasPlane(pmask1, plane2))
		{
		    TTMaskAndMask3(&tmp1, &DBPlaneTypes[plane],
				&DBPlaneTypes[plane2]);
		    TTMaskAndMask(&tmp1, &set1);
		    if (!TTMaskIsZero(&tmp1))
		    {
			TechError("Types in first list must either be in"
				" one plane or else types must not share"
				" planes.\n");
			return (0);
		    }
		}
	    }
    }

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pmask2 = CoincidentPlanes(&set2, ptest);

    if ((pmask2 == 0) && (ptest != 0))
    {
	pmask2 = ptest;
	multiplane = TRUE;
	for (plane = 0; plane < DBNumPlanes; plane++)
	    for (plane2 = 0; plane2 < DBNumPlanes; plane2++)
	    {
		if (plane == plane2) continue;
		if (PlaneMaskHasPlane(pmask2, plane) &&
			PlaneMaskHasPlane(pmask2, plane2))
		{
		    TTMaskAndMask3(&tmp1, &DBPlaneTypes[plane],
				&DBPlaneTypes[plane2]);
		    TTMaskAndMask(&tmp1, &set2);
		    if (!TTMaskIsZero(&tmp1))
		    {
			TechError("Types in second list must either be in"
				" one plane or else types must not share"
				" planes.\n");
			return (0);
		    }
		}
	    }
    }

    if (multiplane)
    {
	/* Loop over independent plane/layer combinations */

	for (plane = 0; plane < DBNumPlanes; plane++)
	    for (plane2 = 0; plane2 < DBNumPlanes; plane2++)
	    {
		if (PlaneMaskHasPlane(pmask1, plane) &&
			PlaneMaskHasPlane(pmask2, plane2))
		{
		    pmaskA = PlaneNumToMaskBit(plane);
		    pmaskB = PlaneNumToMaskBit(plane2);

		    TTMaskAndMask3(&tmp1, &set1, &DBPlaneTypes[plane]);
		    TTMaskAndMask3(&tmp2, &set2, &DBPlaneTypes[plane2]);

		    return drcMaskSpacing(&tmp1, &tmp2, pmaskA, pmaskB,
				wwidth, distance, adjacency, why,
				widerule, multiplane);
		}
	    }
    }
    else
	return drcMaskSpacing(&set1, &set2, pmask1, pmask2, wwidth,
		distance, adjacency, why, widerule, multiplane);

    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcEdge --
 *
 * Process a primitive edge rule.
 * This is of the form:
 *
 *	edge layers1 layers2 dist OKtypes cornerTypes cornerDist why [plane]
 * or	edge4way layers1 layers2 dist OKtypes cornerTypes cornerDist why [plane]
 *
 * e.g,
 *
 *	edge poly,pmc s 1 diff poly,pmc "poly-diff separation must be 2"
 *
 * An "edge" rule is applied only down and to the left.
 * An "edge4way" rule is applied in all four directions.
 *
 * Results:
 *	Returns greater of dist and cdist.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcEdge(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1], *layers2 = argv[2];
    int distance = atoi(argv[3]);
    char *okTypes = argv[4], *cornerTypes = argv[5];
    int cdist = atoi(argv[6]);
    char *why = drcWhyDup(argv[7]);
    bool fourway = (strcmp(argv[0], "edge4way") == 0);
    TileTypeBitMask set1, set2, setC, setM;
    DRCCookie *dp, *dpnew;
    int plane, checkPlane, tmpPlane;
    PlaneMask pMask1, pMaskM, pMaskC, pset, ptest;
    TileType i, j;

    /*
     * Edge4way rules produce [j][i] entries as well as [i][j]
     * ones, and check both corners rather than just one corner.
     */

    ptest = DBTechNoisyNameMask(layers1, &set1);
    pMask1 = CoincidentPlanes(&set1, ptest);

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pMask1 &= CoincidentPlanes(&set2, ptest);

    if (pMask1 == 0) 
    {
	TechError("All edges in edge rule must lie in shared planes.\n");
	return (0);
    }

    /* Give warning if types1 and types2 intersect */
    if (TTMaskIntersect(&set1, &set2))
	TechError("Warning:  types1 and types2 have nonempty intersection.  "
		"DRC does not check edges with the same type on both "
		"sides.\n");

    ptest = DBTechNoisyNameMask(cornerTypes, &setC);
    pMaskC = CoincidentPlanes(&setC, ptest);

    if ((pMaskC & pMask1) == 0)
    {
	TechError("Corner types aren't in same plane as edges.\n");
	return (0);
    }

    if (argc == 9)
	tmpPlane = DBTechNoisyNamePlane(argv[8]);

    /*
     * OKtypes determine the checkPlane.  If checkPlane exists, it should
     * only be used to check against the plane of OKtypes.
     */

    ptest = DBTechNoisyNameMask(okTypes, &setM);
    pMaskM = CoincidentPlanes(&setM, ptest);
 
    if (pMaskM == 0 || pMaskM == DBTypePlaneMaskTbl[TT_SPACE])
    {
	/* Technically it should be illegal to specify simply "space"
	 * in the types list for a DRC rule, as it is ambiguous.
	 * However, we will assume that the plane of the edge is
	 * intended.  The "plane" argument may be used to do the
	 * qualification (for backwards compatibility); any other use
	 * gets a warning.
	 */

	if (TTMaskEqual(&DBSpaceBits, &setM))
	{
	    if (argc == 9)
		pMaskM = PlaneNumToMaskBit(tmpPlane);
	    else
	    {
		TechError("OK types \"%s\" in more than one plane.\n"
	        	"	Assuming same plane (%s) as edge.\n",
			okTypes, DBPlaneLongNameTbl[LowestMaskBit(pMask1)]);
		pMaskM = pMask1;
	    }
	}

	/* The case okTypes="0" is explicitly allowed according */
	/* to the manual, so we parse it accordingly.		*/

	else if (!strcmp(okTypes, "0"))
	    pMaskM = pMask1;
	else
	{
	    TechError("All OK types must lie in one plane.\n");
	    return (0);
	}
    }

    /* "plane" argument deprecated; kept for backward compatibility only */

    if ((argc == 9) && (PlaneNumToMaskBit(tmpPlane) != pMaskM))
	    TechError("Ignoring bad plane argument.\n");

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    if (pset = (DBTypesOnSamePlane(i, j) & pMask1))
	    {
		if (TTMaskHasType(&set1, i) && TTMaskHasType(&set2, j))
		{
		    /* For fastest DRC, checkPlane and plane should be	*/
		    /* the same, if possible.				*/

		    if (pset & pMaskM != 0)
		    {
			plane = LowestMaskBit(pset & pMaskM);
			checkPlane = plane;
		    }
		    else
		    {
			plane = LowestMaskBit(pset);
			checkPlane = LowestMaskBit(pMaskM);
		    }

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, distance, dp->drcc_next, &setM, &setC,
				why, cdist, DRC_FORWARD, checkPlane, plane);
		    if (fourway) dpnew->drcc_flags |= DRC_BOTHCORNERS;
		    dp->drcc_next = dpnew;

		    if (!fourway) continue;

		    /* find bucket preceding new one we wish to insert */
		    dp = drcFindBucket(j, i, distance);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew,distance,dp->drcc_next, &setM, &setC,
				why, cdist, DRC_REVERSE, checkPlane, plane);
		    dpnew->drcc_flags |= DRC_BOTHCORNERS;
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return (MAX(distance, cdist));
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcOverhang --
 *
 * Process an overhang rule.
 * This is of the form:
 *
 *	overhang layers2 layers1 dist why
 *
 * indicating that layers2 must overhang layers1 by a distance of at least
 * dist.
 *
 * This rule is equivalent to:
 *
 *	edge4way layers1 space/p2|layers2 dist layers1|layers2 \
 *			 space/p2|layers2 dist why
 *
 * ----------------------------------------------------------------------------
 */

int
drcOverhang(argc, argv)
    int argc;
    char *argv[];
{
    char *layers2 = argv[1], *layers1 = argv[2];
    int distance = atoi(argv[3]);
    char *why = drcWhyDup(argv[4]);
    TileTypeBitMask set1, set2, setM, setC, setN, set2inv;
    DRCCookie *dp, *dpnew, *dptrig;
    int plane, plane2;
    TileType i, j;
    PlaneMask pMask1, pMask2, pset, ptest;

    ptest = DBTechNoisyNameMask(layers1, &set1);
    pMask1 = CoincidentPlanes(&set1, ptest);
    if (pMask1 == 0)
    {
	TechError("All layers in first set for \"overhang\" must be on "
			"the same plane\n");
	return (0);
    }
    TTMaskCom2(&setN, &set1);

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pMask2 = CoincidentPlanes(&set2, ptest);
    if (pMask2 == 0)
    {
	TechError("All layers in second set for \"overhang\" must be on "
			"the same plane\n");
	return (0);
    }
    TTMaskCom2(&set2inv, &set2);

    /* Warn if types1 and types2 intersect */
    if (TTMaskIntersect(&set1, &set2))
	TechError("Warning:  inside and outside types have nonempty intersection.  "
		"DRC does not check edges with the same type on both sides.\n");

    /* SetM is the union of set1 and set2 */
    TTMaskZero(&setM);
    TTMaskSetMask3(&setM, &set1, &set2);

    /* Add space to set2 */
    TTMaskSetType(&set2, TT_SPACE);

    /* SetC is the empty set */
    TTMaskZero(&setC);

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;
	    if (pset = (DBTypesOnSamePlane(i, j) & pMask2))
	    {
		if ((pset & pMask1) != 0)
		{
		    if (TTMaskHasType(&set1, i) && TTMaskHasType(&set2, j))
		    {
			plane = LowestMaskBit(pset);

			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic (sizeof (DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &setM,
					&setM, why, distance,
					DRC_FORWARD | DRC_BOTHCORNERS,
					plane, plane);
			dp->drcc_next = dpnew;

			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next,
					&setM, &setM, why, distance,
					DRC_REVERSE | DRC_BOTHCORNERS,
					plane, plane);
			dp->drcc_next = dpnew;
		    }
		}
		else	/* Multi-plane overhang rule */
		{
		    if (TTMaskHasType(&set2, i) && TTMaskHasType(&set2inv, j))
		    {
			plane = LowestMaskBit(pset);
			plane2 = LowestMaskBit(pMask1);

			/* find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &set1, &set1, why,
				    distance, DRC_FORWARD, plane2, plane);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, 1, dpnew, &setN, &setC, why,
				    0, DRC_FORWARD | DRC_TRIGGER, plane2, plane);
			dp->drcc_next = dptrig;

			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &set1, &set1, why,
				    distance, DRC_REVERSE, plane2, plane);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, 1, dpnew, &setN, &setC, why,
				    0, DRC_REVERSE | DRC_TRIGGER, plane2, plane);
			dp->drcc_next = dptrig;
		    }
		}
	    }
	}
    }
    return distance;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcRectOnly --
 *
 * Process a rectangle-only rule.  This rule prohibits non-rectangular
 * geometry, and is used especially for contacts, as the "squares" operator
 * in the CIF/GDS output generator can't handle non-rectangular areas.
 * The rule is of the form:
 *
 *	rect_only layers why
 *
 * and is equivalent to:
 *
 *	edge4way layers ~(layers)/plane 1 ~(layers)/plane (all_layers)/plane 1
 *
 * The rect_only rule avoids the above contrived construction, especially the
 * requirement of specifying "all_layers" as something like (~(x),x)/p, a sure-
 * fire obfuscation.
 *
 * ----------------------------------------------------------------------------
 */

int
drcRectOnly(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    char *why = drcWhyDup(argv[2]);
    TileTypeBitMask set1, set2, setC;
    PlaneMask pmask, pset, ptest;
    DRCCookie *dp, *dpnew;
    int plane;
    TileType i, j;

    ptest = DBTechNoisyNameMask(layers, &set1);
    pmask = CoincidentPlanes(&set1, ptest);

    if (pmask == 0)
    {
	TechError("All types for \"rect_only\"  must be on the same plane.\n");
	return (0);
    }

    /* set2 is the inverse of set1 */
    TTMaskCom2(&set2, &set1);

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;

	    if (pset = (DBTypesOnSamePlane(i, j) & pmask))
	    {
		if (TTMaskHasType(&set1, i) && TTMaskHasType(&set2, j))
		{
		    plane = LowestMaskBit(pset);
		    /* setC = all types in plane */
		    TTMaskZero(&setC);
		    TTMaskSetMask(&setC, &DBPlaneTypes[plane]);

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, 1);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew, 1, dp->drcc_next, &set2, &setC, why, 1,
				DRC_FORWARD | DRC_BOTHCORNERS, plane, plane);
		    dp->drcc_next = dpnew;

		    /* find bucket preceding new one we wish to insert */
		    dp = drcFindBucket(j, i, 1);
		    dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
		    drcAssign(dpnew,1,dp->drcc_next, &set2, &setC, why, 1,
				DRC_REVERSE | DRC_BOTHCORNERS, plane, plane);
		    dp->drcc_next = dpnew;
		}
	    }
	}
    }
    return 1;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcSurround --
 *
 * Process a surround rule.
 * This is of the form:
 *
 *	surround layers1 layers2 dist presence why
 *
 * indicating that layers2 must surround layers1 by at least distance
 * dist in all directions.
 *
 * This rule is equivalent to:
 *
 *	edge4way ~(layers2)/plane2 layers2 dist ~(layers1)/plane1 \
 *		layers2 dist why
 *
 * When presence=absence_illegal, the following additional rule is needed:
 *
 *	edge4way layers1 ~(layers2)/plane1 dist NULL ~(layers2)/plane1 \
 *		dist why
 *
 * Extension added July 12, 2014:  For via rules where an asymmetric
 * surround is allowed, with a smaller surround allowed on two sides if
 * the remaining two sides have a larger surround.  This can be implemented
 * with a trigger rule, and is specified by the syntax above with "presence"
 * being "directional".  Note that the rule expresses that the overhang rule
 * requires the presence of the material on one side of a corner.  If the
 * other side has a non-zero minimum surround requirement, then it should
 * be implemented with an additional (absence_illegal) surround rule.
 * Otherwise, any width of material less than "dist" on one side of a
 * corner will trigger the rule requiring at least "dist" width of the same
 * material on the other side of the corner.
 *
 * ----------------------------------------------------------------------------
 */

int
drcSurround(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1], *layers2 = argv[2];
    int distance = atoi(argv[3]);
    char *presence = argv[4];
    char *why = drcWhyDup(argv[5]);
    TileTypeBitMask set1, set2, setM, invM, setR;
    DRCCookie *dp, *dpnew, *dptrig;
    int plane1, plane2;
    PlaneMask pmask, pmask2, pset, ptest;
    TileType i, j;
    bool isExact = FALSE;
    bool isDirectional = FALSE;

    ptest = DBTechNoisyNameMask(layers1, &setM);
    pmask = CoincidentPlanes(&setM, ptest);
    if (pmask == 0)
    {
	TechError("Inside types in \"surround\" must be on the same plane\n");
	return (0);
    }

    ptest = DBTechNoisyNameMask(layers2, &set2);
    pmask2 = CoincidentPlanes(&set2, ptest);
    if (pmask2 == 0)
    {
	TechError("Outside types in \"surround\" must be on the same plane\n");
	return (0);
    }

    /* "exact_width" rule implemented 9/16/10.  This enforces an exact	*/
    /* surround distance.  "absence_illegal" is implied.  		*/

    if (!strncmp(presence, "exact_", 6)) isExact = TRUE;
    else if (!strncmp(presence, "directional", 11))
    {
	isDirectional = TRUE;
	/* Combined mask */
	TTMaskZero(&setR);
	TTMaskSetMask(&setR, &setM);
	TTMaskSetMask(&setR, &set2);
    }

    /* invert setM */
    TTMaskCom2(&invM, &setM);

    /* set1 is the inverse of set2 */
    TTMaskCom2(&set1, &set2);

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;	/* Ignore false edges */
	    if (pset = (DBTypesOnSamePlane(i, j) & pmask2))
	    {
		if (isDirectional)
		{
		    /* Directional surround is done entirely differently */

		    if (TTMaskHasType(&setM, i) && TTMaskHasType(&invM, j))
		    {
			plane1 = LowestMaskBit(pmask);
			plane2 = LowestMaskBit(pset);

			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));

			/* Insert triggered rule */
			drcAssign(dpnew, distance, dp->drcc_next, &setR,
				&DBAllTypeBits,
				why, distance,
				DRC_REVERSE | DRC_BOTHCORNERS,
				plane1, plane2);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, distance, dpnew, &set2,
				&DBZeroTypeBits, why, 0,
				DRC_FORWARD | DRC_TRIGGER,
				plane1, plane2);
			dp->drcc_next = dptrig;

			/* And the other direction. . . */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));

			/* Insert triggered rule */
			drcAssign(dpnew, distance, dp->drcc_next, &setR,
				&DBAllTypeBits,
				why, distance,
				DRC_FORWARD | DRC_BOTHCORNERS,
				plane1, plane2);
			dptrig = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dptrig, distance, dpnew, &set2,
				&DBZeroTypeBits, why, 0,
				DRC_REVERSE | DRC_TRIGGER,
				plane1, plane2);
			dp->drcc_next = dptrig;
		    }
		}
		else
		{
		    if (TTMaskHasType(&set1, i) && TTMaskHasType(&set2, j))
		    {
			plane1 = LowestMaskBit(pmask);
			plane2 = LowestMaskBit(pset);

			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &invM, &set2,
				why, distance,
				DRC_FORWARD | DRC_BOTHCORNERS,
				plane1, plane2);
			dp->drcc_next = dpnew;

			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &invM, &set2,
				why, distance,
				DRC_REVERSE | DRC_BOTHCORNERS,
				plane1, plane2);
			dp->drcc_next = dpnew;
		    }
		}
	    }
	}
    }

    if (isExact)
    {
	for (i = 0; i < DBNumTypes; i++)
	{
	    for (j = 0; j < DBNumTypes; j++)
	    {
		if (i == j) continue;	/* Ignore false edges */
		if (pset = (DBTypesOnSamePlane(i, j) & pmask))
		{
		    if (TTMaskHasType(&setM, i) && TTMaskHasType(&set2, j))
		    {
			plane1 = LowestMaskBit(pset);

			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &set1, &set2,
					why, distance,
					DRC_FORWARD | DRC_BOTHCORNERS | DRC_OUTSIDE,
					plane1, plane1);
			dp->drcc_next = dpnew;

			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *)mallocMagic(sizeof(DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, &set1, &set2,
					why, distance,
					DRC_REVERSE | DRC_BOTHCORNERS | DRC_OUTSIDE,
					plane1, plane1);
			dp->drcc_next = dpnew;
		    }
		}
	    }
	}
    }

    if ((!isExact) && strcmp(presence, "absence_illegal")) return distance;

    /* Add an extra rule when presence of the surrounding	*/
    /* layer is required.  Rule is different if planes match.	*/

    if (pset = pmask & pmask2)
    {
	TTMaskZero(&invM);
	TTMaskSetMask(&invM, &setM);
	TTMaskSetMask(&invM, &set2);
	TTMaskCom(&invM);
	TTMaskZero(&set1);

	for (i = 0; i < DBNumTypes; i++)
	    for (j = 0; j < DBNumTypes; j++)
	    {
		if (i == j) continue;
	        if (pset = (DBTypesOnSamePlane(i, j) & pmask & pmask2))
		{
		    plane1 = LowestMaskBit(pset);
		    if (TTMaskHasType(&setM, i) && TTMaskHasType(&invM, j))
		    {
			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
	 		drcAssign(dpnew, distance, dp->drcc_next,
					&set1, &invM, why, distance,
					DRC_FORWARD | DRC_BOTHCORNERS,
					plane1, plane1);
			dp->drcc_next = dpnew;

			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
			drcAssign(dpnew,distance,dp->drcc_next,
					&set1, &invM, why, distance,
					DRC_REVERSE | DRC_BOTHCORNERS,
					plane1, plane1);
			dp->drcc_next = dpnew;
		    }
		}
	    }
    }
    else
    {
	for (i = 0; i < DBNumTypes; i++)
	    for (j = 0; j < DBNumTypes; j++)
	    {
		if (i == j) continue;
	        if (pset = (DBTypesOnSamePlane(i, j) & pmask))
		{
		    if (TTMaskHasType(&setM, i) && TTMaskHasType(&invM, j))
		    {
			plane1 = LowestMaskBit(pset);
			plane2 = LowestMaskBit(pmask2);

			/* Find bucket preceding the new one we wish to insert */
			dp = drcFindBucket(i, j, distance);
			dpnew = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
			drcAssign(dpnew, distance, dp->drcc_next,
					&set2, &invM, why, distance,
					DRC_FORWARD | DRC_BOTHCORNERS,
					plane2, plane1);
			dp->drcc_next = dpnew;

			/* find bucket preceding new one we wish to insert */
			dp = drcFindBucket(j, i, distance);
			dpnew = (DRCCookie *) mallocMagic((unsigned)
					(sizeof (DRCCookie)));
			drcAssign(dpnew,distance,dp->drcc_next,
					&set2, &invM, why, distance,
					DRC_REVERSE | DRC_BOTHCORNERS,
					plane2, plane1);
			dp->drcc_next = dpnew;
		    }
		}
	    }
    }

    return distance;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcNoOverlap --
 *
 * Process a no-overlap rule.
 * This is of the form:
 *
 *	no_overlap layers1 layers2
 *
 * e.g,
 *
 *	no_overlap poly m2contact
 *
 * Results:
 *	Returns 0.
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcNoOverlap(argc, argv)
    int argc;
    char *argv[];
{
    char *layers1 = argv[1], *layers2 = argv[2];
    TileTypeBitMask set1, set2;
    TileType i, j;
    int plane;

    /*
     * Grab up two sets of tile types, and make sure that if
     * any type from one set is painted over any type from the
     * other, then an error results.
     */

    DBTechNoisyNameMask(layers1, &set1);
    DBTechNoisyNameMask(layers2, &set2);

    for (i = 0; i < DBNumTypes; i++)
	for (j = 0; j < DBNumTypes; j++)
	    if (TTMaskHasType(&set1, i) && TTMaskHasType(&set2, j))
		for (plane = 0; plane < DBNumPlanes; plane++)
		{
		    DRCCurStyle->DRCPaintTable[plane][j][i] = TT_ERROR_S;
		    DRCCurStyle->DRCPaintTable[plane][i][j] = TT_ERROR_S;
		}

    return (0);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcExactOverlap --
 *
 * Process an exact overlap
 * This is of the form:
 *
 *	exact_overlap layers
 *
 * e.g,
 *
 *	exact_overlap pmc,dmc
 *
 * Results:
 *	Returns 0.
 *
 * Side effects:
 *	Updates DRCExactOverlapTypes.
 *
 * ----------------------------------------------------------------------------
 */

int
drcExactOverlap(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    TileTypeBitMask set;

    /*
     * Grab up a bunch of tile types, and remember these: tiles
     * of these types cannot overlap themselves in different cells
     * unless they overlap exactly.
     */

    DBTechNoisyNameMask(layers, &set);
    TTMaskSetMask(&DRCCurStyle->DRCExactOverlapTypes, &set);
    return (0);
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcRectangle --
 *
 * Process a rectangle rule.  This is of the form:
 *
 *	rectangle layers maxwidth [even|odd|any] why
 *
 * The rule checks to make sure that the region is rectangular and that the
 * width and length are even or odd, as specified.  These two criteria ensure 
 * that the squares rule of the cifout section can properly produce via 
 * holes without misaligning them between cells and without putting the via 
 * holes off grid.  The maxwidth is required to make the extent of this rule
 * a finite size, so that we can set the DRChalo to something finite.
 *
 * Results:
 *	maxwidth
 *
 * Side effects:
 *	Updates the DRC technology variables.
 *
 * ----------------------------------------------------------------------------
 */

int
drcRectangle(argc, argv)
    int argc;
    char *argv[];
{
    char *layers = argv[1];
    char *why = drcWhyDup(argv[4]);
    TileTypeBitMask types, nottypes;
    int maxwidth;
    static char *drcRectOpt[4] = {"any", "even", "odd", 0};
    int i, j, even, plane;
    PlaneMask pMask, pset, ptest;

    /* parse arguments */
    ptest = DBTechNoisyNameMask(layers, &types);
    pMask = CoincidentPlanes(&types, ptest);

    if (pMask == 0) {
	TechError("Layers in rectangle rule must lie in a single plane.");
	return 0;
    }
    TTMaskCom2(&nottypes, &types);

    if (sscanf(argv[2], "%d", &maxwidth) != 1) {
	TechError("bad maxwidth in rectangle rule");
	return 0;
    }
    even = Lookup(argv[3], drcRectOpt);
    if (even < 0) {
	TechError("bad [even|odd|any] selection in rectangle rule");
	return 0;
    }
    even--;  /* -1: any, 0: even, 1: odd */

    /* Install 2 edge rules: one that checks rectangle-ness, and one that
     * checks size
     */
    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    if (i == j) continue;

	    if (pset = (DBTypesOnSamePlane(i, j) & pMask))
	    {
		if (TTMaskHasType(&types, i) && TTMaskHasType(&nottypes, j))
		{
		    DRCCookie *dp, *dpnew;

		    plane = LowestMaskBit(pset);

		    /* 
		     * A rule that checks rectangle-ness. 
		     *   left:  oktypes, right: other types
		     * This rule needs to be checked in all 4 directions
		     */
		    int distance = 1;

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(i, j, distance);
		    dpnew = (DRCCookie *) mallocMagic((unsigned) (sizeof (DRCCookie)));
		    drcAssign(dpnew, distance, dp->drcc_next, 
				&nottypes, &DBAllTypeBits, why, distance, 
				DRC_FORWARD, plane, plane);
		    dp->drcc_next = dpnew;

		    /* Find bucket preceding the new one we wish to insert */
		    dp = drcFindBucket(j, i, distance); /* note: j, i not i, j */
		    dpnew = (DRCCookie *) mallocMagic((unsigned) (sizeof (DRCCookie)));
		    drcAssign(dpnew, distance, dp->drcc_next, 
		    		&nottypes, &DBAllTypeBits, why, distance, 
		    		DRC_REVERSE, plane, plane);
		    dp->drcc_next = dpnew;

		    if (maxwidth > 0) {
			/* 
		 	 * A rule that checks size.
		 	 *   left:  other types, right: oktypes
		 	 */
			distance = maxwidth;

			/* note: j, i not i, j */
			for (dp = DRCCurStyle->DRCRulesTbl[j][i];
				dp->drcc_next != (DRCCookie *) NULL &&
				dp->drcc_next->drcc_dist < distance;
				dp = dp->drcc_next); /* null body */

			dpnew = (DRCCookie *)mallocMagic(sizeof (DRCCookie));
			drcAssign(dpnew, distance, dp->drcc_next, 
				&types, &DBZeroTypeBits, why, even, 
				DRC_RECTSIZE, plane, plane);
			dp->drcc_next = dpnew;
		    }
		}
	    }
	}
    }
    return maxwidth;
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcStepSize --
 *
 * Process a declaration of the step size.
 * This is of the form:
 *
 *	stepsize step_size
 *
 * e.g,
 *
 *	stepsize 1000
 *
 * Results:
 *	Returns 0.
 *
 * Side effects:
 *	Updates DRCStepSize.
 *
 * ----------------------------------------------------------------------------
 */

int
drcStepSize(argc, argv)
    int argc;
    char *argv[];
{
    if (DRCCurStyle == NULL) return 0;

    DRCCurStyle->DRCStepSize = atoi(argv[1]);
    if (DRCCurStyle->DRCStepSize <= 0)
    {
	TechError("Step size must be a positive integer.\n");
	DRCCurStyle->DRCStepSize = 0;
    }
    else if (DRCCurStyle->DRCStepSize < 16)
    {
	TechError("Warning: abnormally small DRC step size (%d)\n",
		DRCCurStyle->DRCStepSize);
    }

    return (0);
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCTechFinal --
 *
 * Called after all lines of the drc section in the technology file have been
 * read.  Ensures that a valid style is in effect, and then calls
 * drcTechFinalStyle().
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	See drcTechFinalStyle();
 *
 * ----------------------------------------------------------------------------
 */

void
DRCTechFinal()
{
    DRCStyle *ds;

    /* Create a "default" style if there isn't one */

    if (DRCStyleList == NULL)
    {
	DRCStyleList = (DRCKeep *)mallocMagic(sizeof(DRCKeep));
	DRCStyleList->ds_next = NULL;
	DRCStyleList->ds_name = StrDup((char **)NULL, "default");

	drcTechNewStyle();
	DRCCurStyle->ds_name = DRCStyleList->ds_name;
	DRCCurStyle->ds_status = TECH_LOADED;
    }
    drcTechFinalStyle(DRCCurStyle);
}

/*
 * ----------------------------------------------------------------------------
 * drcScaleDown ---
 *
 *	DRC distances may be specified with a scale factor so that physically
 *	based rules can be recorded, but the rules used will be rounded (up)
 *	to the nearest lambda.  The fractional part of the true distance in
 *	lambda is saved, so that the original value can be recovered when
 *	the magic grid is rescaled.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Scales all the DRC distances by dividing by the DRC scale factor.
 * ----------------------------------------------------------------------------
 */

void
drcScaleDown(style, scalefactor)
    DRCStyle *style;
    int scalefactor;
{
    TileType i, j;
    DRCCookie  *dp;
    int dist;

    if (scalefactor > 1)
    {
	for (i = 0; i < TT_MAXTYPES; i++)
	    for (j = 0; j < TT_MAXTYPES; j++)
		for (dp = style->DRCRulesTbl[i][j]; dp != NULL; dp = dp->drcc_next)
		{
		    if  (dp->drcc_dist > 0)
		    {
			dist = dp->drcc_dist;
			dp->drcc_dist /= scalefactor;
			if ((dp->drcc_mod = (unsigned char)(dist % scalefactor)) != 0)
			    if (!(dp->drcc_flags & DRC_MAXWIDTH))
				dp->drcc_dist++;
		    }
		    if  (dp->drcc_cdist > 0)
		    {
			int locscale = scalefactor;
			if (dp->drcc_flags & DRC_AREA)
			    locscale *= scalefactor;

			dist = dp->drcc_cdist;
			dp->drcc_cdist /= locscale;
			if ((dp->drcc_cmod = (unsigned char)(dist % locscale)) != 0)
			    dp->drcc_cdist++;
		    }
		}
    }
}

/*
 * ----------------------------------------------------------------------------
 * drcScaleUp ---
 *
 *	Recovers the original (pre-scaled) values for drcc_dist and
 *	drcc_cdist in the DRC cookies.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Scales all the DRC distances by multiplying by the DRC scale factor.
 * ----------------------------------------------------------------------------
 */

void
drcScaleUp(style, scalefactor)
    DRCStyle *style;
    int scalefactor;
{
    TileType i, j;
    DRCCookie  *dp;
    int dist;

    if (style == NULL) return;

    if (scalefactor > 1)
    {
	for (i = 0; i < TT_MAXTYPES; i++)
	    for (j = 0; j < TT_MAXTYPES; j++)
		for (dp = style->DRCRulesTbl[i][j]; dp != NULL; dp = dp->drcc_next)
		{
		    if  (dp->drcc_dist > 0)
		    {
			dist = dp->drcc_dist;
			if (dp->drcc_mod != 0)
			    if (!(dp->drcc_flags & DRC_MAXWIDTH))
				dp->drcc_dist--;
			dp->drcc_dist *= scalefactor;
			dp->drcc_dist += (short)dp->drcc_mod;
			dp->drcc_mod = 0;
		    }
		    if  (dp->drcc_cdist > 0)
		    {
			dist = dp->drcc_cdist;
			if (dp->drcc_cmod != 0)
			    dp->drcc_cdist--;
			dp->drcc_cdist *= scalefactor;
			if (dp->drcc_flags & DRC_AREA)
			    dp->drcc_cdist *= scalefactor;
			dp->drcc_cdist += (short)dp->drcc_cmod;
			dp->drcc_cmod = 0;
		    }
		}
    }
}

/*
 * ----------------------------------------------------------------------------
 *
 * drcTechFinalStyle --
 *
 * Called after all lines of the drc section in the technology file have been
 * read.  The preliminary DRC Rules Table is pruned by removing rules covered
 * by other (longer distance) rules, and by removing the dummy rule at the
 * front of each list.  Where edges are completely illegal, the rule list is
 * pruned to a single rule.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	May remove DRCCookies from the linked lists of the DRCRulesTbl.
 *
 * ----------------------------------------------------------------------------
 */

void
drcTechFinalStyle(style)
   DRCStyle *style;
{
    TileTypeBitMask tmpMask, nextMask;
    DRCCookie  *dummy, *dp, *next, *dptrig;
    DRCCookie **dpp, **dp2back;
    TileType i, j;

    /* If the scale factor is not 1, then divide all distances by	*/
    /* the scale factor, take the ceiling, and save the (negative)	*/
    /* remainder.							*/

    drcScaleUp(style, style->DRCScaleFactorD);
    drcScaleDown(style, style->DRCScaleFactorN);

    /* Scale DRCTechHalo to match */
    DRCTechHalo *= style->DRCScaleFactorD;
    DRCTechHalo /= style->DRCScaleFactorN;

    /* Set maximum halo */
    style->DRCTechHalo = DRCTechHalo;

    /* A reasonable chunk size for design-rule checking is about
     * 16 times the maximum design-rule interaction distance.  This
     * results in a halo overhead of about 27%.  If there's no DRC  
     * information at all (TechHalo is zero), just pick any size.
     * (Update 1/13/09:  "any size" needs a bit of modification,
     * because 64 will be way too small for a layout with a small
     * scalefactor.  Assuming that the CIF output style is valid,
     * use its scalefactor to adjust the step size).
     */
    if (style->DRCStepSize == 0)
    {
	if (style->DRCTechHalo == 0)
	{
	    if (CIFCurStyle != NULL)
		style->DRCStepSize = 6400 / CIFCurStyle->cs_scaleFactor;
	    else
		style->DRCStepSize = 64;
	}
	else
	    style->DRCStepSize = 16 * style->DRCTechHalo;
    }
    DRCStepSize = style->DRCStepSize;

    /* Remove dummy buckets */
    for (i = 0; i < TT_MAXTYPES; i++)
    {
	for (j = 0; j < TT_MAXTYPES; j++)
	{
	    dpp = &(style->DRCRulesTbl [i][j]);
	    dummy = *dpp;
	    *dpp = dummy->drcc_next;
	    freeMagic((char *) dummy); 
	}
    }
    drcCifFinal();

    if (!DRCRuleOptimization) return;

    /* Check for edges that are completely illegal.  Where this is the
     * case, eliminate all of the edge's rules except one.
     */
    
    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    DRCCookie *keep = NULL, *dptest, *dptemp, *dpnew;
	    
	    for (dp = style->DRCRulesTbl[i][j]; dp != NULL; dp = dp->drcc_next)
	    {
		if (dp->drcc_flags & (DRC_NONSTANDARD || DRC_OUTSIDE)) continue;
		if (dp->drcc_flags & DRC_REVERSE)
		{
		    if ((i == TT_SPACE) || TTMaskHasType(&dp->drcc_mask, i)) continue;
		}
		else
		{
		    if ((j == TT_SPACE) || TTMaskHasType(&dp->drcc_mask, j)) continue;
		}

		/* Rules where okTypes are in a different plane don't count,	*/
		/* unless i or j also appear in the checked plane.		*/

		if (dp->drcc_plane != dp->drcc_edgeplane)
		{
		    if (dp->drcc_flags & DRC_REVERSE)
		    {
			if ((i == TT_SPACE) || !DBTypeOnPlane(i, dp->drcc_plane))
			    continue;
		    }
		    else
		    {
			if ((j == TT_SPACE) || !DBTypeOnPlane(j, dp->drcc_plane))
			    continue;
		    }
		}

		// if (DBIsContact(i) || DBIsContact(j) || i == TT_SPACE ||
		//	j == TT_SPACE) continue;

		dpnew = NULL;
		keep = dp;

		/* This edge is illegal.  Throw away all rules except the one
		 * needed that is always violated.
		 */
	    
		dptest = style->DRCRulesTbl[i][j];
		while (dptest != NULL)
		{
		    dptemp = dptest->drcc_next;
		    if ((dptest == keep) || (dptest->drcc_edgeplane !=
				keep->drcc_edgeplane))
		    {
			dptest->drcc_next = NULL;
			if (dpnew == NULL)
			    style->DRCRulesTbl[i][j] = dptest;
			else
			    dpnew->drcc_next = dptest;
			dpnew = dptest;

			/* "keep" can't be a trigger rule! */
			if (dptest == keep)
			    keep->drcc_flags &= ~DRC_TRIGGER;
		    }
		    else
		    {
			/* Don't free the shared drcc_why string here! */
			freeMagic((char *)dptest);
			drcRulesOptimized++;
		    }
		    dptest = dptemp;
		}
	    }

	    /* TxPrintf("Edge %s-%s is illegal.\n", DBTypeShortName(i),
		DBTypeShortName(j));
	    */
	}
    }

    /*
     * Remove any rule A "covered" by another rule B, i.e.,
     *		B's distance >= A's distance,
     *		B's corner distance >= A's corner distance,
     *		B's RHS type mask is a subset of A's RHS type mask, and
     *		B's corner mask == A's corner mask
     *		B's check plane == A's check plane
     *		either both A and B or neither is a REVERSE direction rule
     *		if A is BOTHCORNERS then B must be, too
     */

    for (i = 0; i < DBNumTypes; i++)
    {
	for (j = 0; j < DBNumTypes; j++)
	{
	    for (dp = style->DRCRulesTbl[i][j]; dp != NULL; dp = dp->drcc_next)
	    {
		/* Don't optimize on trigger rules; optimize on the	*/
		/* rule that gets triggered.				*/
		if (dp->drcc_flags & DRC_TRIGGER)
		{
		    dptrig = dp;
		    dp = dp->drcc_next;
		}
		else
		    dptrig = NULL;
		
		/*
		 * Check following buckets to see if any is a superset.
		 */
		if (dp->drcc_flags & DRC_NONSTANDARD) continue;
		for (next = dp->drcc_next; next != NULL;
			next = next->drcc_next)
		{
		    if (next->drcc_flags & DRC_TRIGGER)
		    {
			/* A triggered rule cannot be considered */
			/* a superset of a non-triggered rule or */
			/* a rule with a different trigger, so   */
			/* we skip all triggered rules and their */
			/* triggering rule.			 */
			next = next->drcc_next;
			continue;
		    }
		    tmpMask = nextMask = next->drcc_mask;
		    TTMaskAndMask(&tmpMask, &dp->drcc_mask);
		    if (!TTMaskEqual(&tmpMask, &nextMask)) continue;
		    if (!TTMaskEqual(&dp->drcc_corner, &next->drcc_corner))
			continue;
		    if (dp->drcc_dist > next->drcc_dist) continue;
		    if (dp->drcc_cdist > next->drcc_cdist) continue;
		    if (dp->drcc_plane != next->drcc_plane) continue;
		    if (dp->drcc_flags & DRC_REVERSE)
		    {
			if (!(next->drcc_flags & DRC_REVERSE)) continue;
		    }
		    else if (next->drcc_flags & DRC_REVERSE) continue;
		    if ((next->drcc_flags & DRC_BOTHCORNERS)
			    && (dp->drcc_flags & DRC_BOTHCORNERS) == 0)
			continue;
		    if (next->drcc_flags & DRC_NONSTANDARD) continue;
		    if (dp->drcc_dist == next->drcc_dist)
		    {
			if ((next->drcc_flags & DRC_OUTSIDE) &&
				!(dp->drcc_flags & DRC_OUTSIDE)) continue;
			if (!(next->drcc_flags & DRC_OUTSIDE) &&
				(dp->drcc_flags & DRC_OUTSIDE)) continue;
		    }
		    break;
		}

		if (next == NULL) continue;

		/* "dp" is a subset of "next".  Eliminate it. */

		/* For triggered rules, eliminate both the rule */
		/* and the trigger.				*/
		if (dptrig != NULL) dp = dptrig;

		/* TxPrintf("For edge %s-%s, \"%s\" covers \"%s\"\n",
		    DBTypeShortName(i), DBTypeShortName(j),
		    next->drcc_why, dp->drcc_why);
		*/
		dp2back = &(style->DRCRulesTbl[i][j]);
		while (*dp2back != dp)
		    dp2back = &(*dp2back)->drcc_next;

		/* Trigger rules */
		if (dptrig != NULL)
		{
		    dptrig = dp->drcc_next;
		    freeMagic((char *)dp->drcc_next);
		    *dp2back = dp->drcc_next->drcc_next;

		    /* Replace this entry so on the next cycle	*/
		    /* dp will be the next rule.  This works	*/
		    /* even though dp is free'd (below), due to	*/
		    /* the one-delayed free mechanism.		*/
		    dp->drcc_next = *dp2back;
		}
		else
		    *dp2back = dp->drcc_next;

		/* Don't free the shared drcc_why string here! */
		freeMagic((char *) dp);
		drcRulesOptimized += 1;
	    }
	}
    }
}


/*
 * ----------------------------------------------------------------------------
 *
 * DRCTechRuleStats --
 *
 * 	Print out some statistics about the design rule database.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	A bunch of stuff gets printed on the terminal.
 *
 * ----------------------------------------------------------------------------
 */

#define MAXBIN 10

void
DRCTechRuleStats()
{
    int counts[MAXBIN+1];
    int edgeRules, overflow;
    int i, j;
    DRCCookie *dp;

    /* Count up the total number of edge rules, and histogram them
     * by the number of rules per edge.
     */
    
    edgeRules = 0;
    overflow = 0;
    for (i=0; i<=MAXBIN; i++) counts[i] = 0;

    for (i=0; i<DBNumTypes; i++)
	for (j=0; j<DBNumTypes; j++)
	{
	    int thisCount = 0;
	    for (dp = DRCCurStyle->DRCRulesTbl[i][j]; dp != NULL; dp = dp->drcc_next)
		thisCount++;
	    edgeRules += thisCount;
	    if (!DBTypesOnSamePlane(i, j)) continue;
	    if (thisCount <= MAXBIN) counts[thisCount] += 1;
	    else overflow += 1;
	}
    
    /* Print out the results. */

    TxPrintf("Total number of rules specified in tech file: %d\n",
	drcRulesSpecified);
    TxPrintf("Edge rules optimized away: %d\n", drcRulesOptimized);
    TxPrintf("Edge rules left in database: %d\n", edgeRules);
    TxPrintf("Histogram of # edges vs. rules per edge:\n");
    for (i=0; i<=MAXBIN; i++)
    {
	TxPrintf("  %2d rules/edge: %d.\n", i, counts[i]);
    }
    TxPrintf(" >%2d rules/edge: %d.\n", MAXBIN, overflow);
}

/*
 * ----------------------------------------------------------------------------
 *
 * DRCTechScale --
 *
 * 	Multiply all DRC rule widths and spacings by a factor of scaled/scalen.
 *	(Don't need to use DBScaleValue() because all values must be positive
 *	and cannot be (M)INFINITY.)
 *
 * ----------------------------------------------------------------------------
 */

void
DRCTechScale(scalen, scaled)
    int scalen, scaled;
{
    DRCCookie  *dp;
    TileType i, j;
    int scalegcf;

    if (DRCCurStyle == NULL) return;
    else if (scalen == scaled == 1) return;

    /* Revert DRC rules to original (unscaled) values */
    drcScaleUp(DRCCurStyle, DRCCurStyle->DRCScaleFactorN);
    drcScaleDown(DRCCurStyle, DRCCurStyle->DRCScaleFactorD);

    DRCCurStyle->DRCScaleFactorD *= scaled;
    DRCCurStyle->DRCScaleFactorN *= scalen;

    /* Reduce scalefactor ratio by greatest common factor */
    scalegcf = FindGCF(DRCCurStyle->DRCScaleFactorD, DRCCurStyle->DRCScaleFactorN);
    DRCCurStyle->DRCScaleFactorD /= scalegcf;
    DRCCurStyle->DRCScaleFactorN /= scalegcf;

    /* Rescale all rules to the new scalefactor */
    drcScaleUp(DRCCurStyle, DRCCurStyle->DRCScaleFactorD);
    drcScaleDown(DRCCurStyle, DRCCurStyle->DRCScaleFactorN);

    DRCTechHalo *= scaled;
    DRCTechHalo /= scalen;

    DRCStepSize *= scaled;
    DRCStepSize /= scalen;

    DRCCurStyle->DRCTechHalo *= scaled;
    DRCCurStyle->DRCTechHalo /= scalen;

    DRCCurStyle->DRCStepSize *= scaled;
    DRCCurStyle->DRCStepSize /= scalen;
}

/* The following routines are used by the "tech" command (and in other places,
 * such as the LEF file reader) to query the DRC database.
 */

/*
 *-----------------------------------------------------------------------------
 *  DRCGetDefaultLayerWidth ---
 *
 *	Determine a default layer width from the DRC width rules
 *	of a layer.  Continue processing until we have processed all
 *	rules, since rules are ordered from shortest to longest distance,
 *	and the maximum distance rule will mask any rules with a shorter
 *	distance.
 *
 * Results:
 *	The minimum width of the magic layer, in magic internal units
 *
 * Side effects:
 *	None.
 *
 *-----------------------------------------------------------------------------
 */

int
DRCGetDefaultLayerWidth(ttype)
    TileType ttype;
{
    int routeWidth = 0;
    DRCCookie *cptr;
    TileTypeBitMask *set;

    for (cptr = DRCCurStyle->DRCRulesTbl[TT_SPACE][ttype]; cptr != (DRCCookie *) NULL;
	cptr = cptr->drcc_next)
    {
	/* FORWARD rules only, and no MAXWIDTH */
	if ((cptr->drcc_flags & (DRC_REVERSE | DRC_MAXWIDTH)) == 0)
	{
	    set = &cptr->drcc_mask;
	    if (TTMaskHasType(set, ttype) && TTMaskEqual(set, &cptr->drcc_corner))
		if ((cptr->drcc_plane == DBPlane(ttype)) &&
			(cptr->drcc_dist == cptr->drcc_cdist))
		{
		    routeWidth = cptr->drcc_dist;
		    /* Diagnostic */
		    /*
		    TxPrintf("DRC: Layer %s has default width %d\n",
		    DBTypeLongNameTbl[ttype], routeWidth);
		    */
		}
	}
    }
    return routeWidth;
}

/*
 *-----------------------------------------------------------------------------
 *  DRCGetDefaultLayerSpacing ---
 *
 *	Determine a default layer-to-layer spacing from the DRC width
 *	rules of a layer.  Continue processing all rules, since rules
 *	are ordered from shortest to longest distance, and the largest
 *	distance matching the criteria sets the rule.
 *
 * Results:
 *	The minimum spacing between the specified magic layer types,
 *	in magic internal units
 *
 * Side effects:
 *	None.
 *
 *-----------------------------------------------------------------------------
 */

int
DRCGetDefaultLayerSpacing(ttype1, ttype2)
    TileType ttype1, ttype2;
{
    int routeSpacing = 0;
    DRCCookie *cptr;
    TileTypeBitMask *set;

    for (cptr = DRCCurStyle->DRCRulesTbl[ttype1][TT_SPACE]; cptr != (DRCCookie *) NULL;
	cptr = cptr->drcc_next)
    {
	if (cptr->drcc_flags & DRC_TRIGGER) {		/* Skip widespacing rules */
	    cptr = cptr->drcc_next;
	    continue;
	}
	if ((cptr->drcc_flags & DRC_REVERSE) == 0)	/* FORWARD only */
	{
	    set = &cptr->drcc_mask;
	    if (!TTMaskHasType(set, ttype2))
	        if (PlaneMaskHasPlane(DBTypePlaneMaskTbl[ttype2], cptr->drcc_plane) &&
			(cptr->drcc_dist == cptr->drcc_cdist))
		{
		    routeSpacing = cptr->drcc_dist;
		    /* Diagnostic */
		    /*
		    TxPrintf("DRC: Layer %s has default spacing %d to layer %s\n",
			DBTypeLongNameTbl[ttype1], routeSpacing,
			DBTypeLongNameTbl[ttype2]);
		    */
		}
	}
    }
    return routeSpacing;
}

/*
 *-----------------------------------------------------------------------------
 *  DRCGetDefaultLayerSurround ---
 *
 *	Determine the default minimum required surround amount
 *	of layer type 2 around layer type 1.
 *	Continue processing all rules, since rules are ordered from
 *	shortest to longest distance, and the largest value of the
 *	surround material sets the minimum required width.
 *
 * Results:
 *	The minimum spacing between the specified magic layer types,
 *	in magic internal units
 *
 * Side effects:
 *	None.
 *
 *-----------------------------------------------------------------------------
 */

int
DRCGetDefaultLayerSurround(ttype1, ttype2)
    TileType ttype1, ttype2;
{
    int layerSurround = 0;
    DRCCookie *cptr;
    TileTypeBitMask *set;

    for (cptr = DRCCurStyle->DRCRulesTbl[ttype1][TT_SPACE]; cptr != (DRCCookie *) NULL;
	cptr = cptr->drcc_next)
    {
	if ((cptr->drcc_flags & DRC_REVERSE) == 0)	/* FORWARD only */
	{
	    set = &cptr->drcc_mask;
	    if (!TTMaskHasType(set, TT_SPACE))
	        if (PlaneMaskHasPlane(DBTypePlaneMaskTbl[ttype2], cptr->drcc_plane) &&
			(cptr->drcc_dist == cptr->drcc_cdist))
		{
		    layerSurround = cptr->drcc_dist;
		    /* Diagnostic */
		    /*
		    TxPrintf("DRC: Layer %s has default surround %d over layer %s\n",
			DBTypeLongNameTbl[ttype2], layerSurround,
			DBTypeLongNameTbl[ttype1]);
		    */
		}
	}
    }
    return layerSurround;
}