aboutsummaryrefslogtreecommitdiff
path: root/gdb/utils.c
blob: f22beea94cc2a543ba36b1865b33c905fc7df2fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
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
/* General utility routines for GDB, the GNU debugger.

   Copyright (C) 1986-2021 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"
#include <ctype.h>
#include "gdbsupport/gdb_wait.h"
#include "event-top.h"
#include "gdbthread.h"
#include "fnmatch.h"
#include "gdb_bfd.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */

#ifdef TUI
#include "tui/tui.h"		/* For tui_get_command_dimension.   */
#endif

#ifdef __GO32__
#include <pc.h>
#endif

#include <signal.h>
#include "gdbcmd.h"
#include "serial.h"
#include "bfd.h"
#include "target.h"
#include "gdb-demangle.h"
#include "expression.h"
#include "language.h"
#include "charset.h"
#include "annotate.h"
#include "filenames.h"
#include "symfile.h"
#include "gdb_obstack.h"
#include "gdbcore.h"
#include "top.h"
#include "main.h"
#include "solist.h"

#include "inferior.h"		/* for signed_pointer_to_address */

#include "gdb_curses.h"

#include "readline/readline.h"

#include <chrono>

#include "interps.h"
#include "gdb_regex.h"
#include "gdbsupport/job-control.h"
#include "gdbsupport/selftest.h"
#include "gdbsupport/gdb_optional.h"
#include "cp-support.h"
#include <algorithm>
#include "gdbsupport/pathstuff.h"
#include "cli/cli-style.h"
#include "gdbsupport/scope-exit.h"
#include "gdbarch.h"
#include "cli-out.h"
#include "gdbsupport/gdb-safe-ctype.h"

void (*deprecated_error_begin_hook) (void);

/* Prototypes for local functions */

static void vfprintf_maybe_filtered (struct ui_file *, const char *,
				     va_list, bool, bool)
  ATTRIBUTE_PRINTF (2, 0);

static void fputs_maybe_filtered (const char *, struct ui_file *, int);

static void prompt_for_continue (void);

static void set_screen_size (void);
static void set_width (void);

/* Time spent in prompt_for_continue in the currently executing command
   waiting for user to respond.
   Initialized in make_command_stats_cleanup.
   Modified in prompt_for_continue and defaulted_query.
   Used in report_command_stats.  */

static std::chrono::steady_clock::duration prompt_for_continue_wait_time;

/* A flag indicating whether to timestamp debugging messages.  */

static bool debug_timestamp = false;

/* True means that strings with character values >0x7F should be printed
   as octal escapes.  False means just print the value (e.g. it's an
   international character, and the terminal or window can cope.)  */

bool sevenbit_strings = false;
static void
show_sevenbit_strings (struct ui_file *file, int from_tty,
		       struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file, _("Printing of 8-bit characters "
			    "in strings as \\nnn is %s.\n"),
		    value);
}

/* String to be printed before warning messages, if any.  */

const char *warning_pre_print = "\nwarning: ";

bool pagination_enabled = true;
static void
show_pagination_enabled (struct ui_file *file, int from_tty,
			 struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file, _("State of pagination is %s.\n"), value);
}




/* Print a warning message.  The first argument STRING is the warning
   message, used as an fprintf format string, the second is the
   va_list of arguments for that string.  A warning is unfiltered (not
   paginated) so that the user does not need to page through each
   screen full of warnings when there are lots of them.  */

void
vwarning (const char *string, va_list args)
{
  if (deprecated_warning_hook)
    (*deprecated_warning_hook) (string, args);
  else
    {
      gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
      if (target_supports_terminal_ours ())
	{
	  term_state.emplace ();
	  target_terminal::ours_for_output ();
	}
      if (filtered_printing_initialized ())
	wrap_here ("");		/* Force out any buffered output.  */
      gdb_flush (gdb_stdout);
      if (warning_pre_print)
	fputs_unfiltered (warning_pre_print, gdb_stderr);
      vfprintf_unfiltered (gdb_stderr, string, args);
      fprintf_unfiltered (gdb_stderr, "\n");
    }
}

/* Print an error message and return to command level.
   The first argument STRING is the error message, used as a fprintf string,
   and the remaining args are passed as arguments to it.  */

void
verror (const char *string, va_list args)
{
  throw_verror (GENERIC_ERROR, string, args);
}

void
error_stream (const string_file &stream)
{
  error (("%s"), stream.c_str ());
}

/* Emit a message and abort.  */

static void ATTRIBUTE_NORETURN
abort_with_message (const char *msg)
{
  if (current_ui == NULL)
    fputs (msg, stderr);
  else
    fputs_unfiltered (msg, gdb_stderr);

  abort ();		/* ARI: abort */
}

/* Dump core trying to increase the core soft limit to hard limit first.  */

void
dump_core (void)
{
#ifdef HAVE_SETRLIMIT
  struct rlimit rlim = { (rlim_t) RLIM_INFINITY, (rlim_t) RLIM_INFINITY };

  setrlimit (RLIMIT_CORE, &rlim);
#endif /* HAVE_SETRLIMIT */

  abort ();		/* ARI: abort */
}

/* Check whether GDB will be able to dump core using the dump_core
   function.  Returns zero if GDB cannot or should not dump core.
   If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
   If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected.  */

int
can_dump_core (enum resource_limit_kind limit_kind)
{
#ifdef HAVE_GETRLIMIT
  struct rlimit rlim;

  /* Be quiet and assume we can dump if an error is returned.  */
  if (getrlimit (RLIMIT_CORE, &rlim) != 0)
    return 1;

  switch (limit_kind)
    {
    case LIMIT_CUR:
      if (rlim.rlim_cur == 0)
	return 0;
      /* Fall through.  */

    case LIMIT_MAX:
      if (rlim.rlim_max == 0)
	return 0;
    }
#endif /* HAVE_GETRLIMIT */

  return 1;
}

/* Print a warning that we cannot dump core.  */

void
warn_cant_dump_core (const char *reason)
{
  fprintf_unfiltered (gdb_stderr,
		      _("%s\nUnable to dump core, use `ulimit -c"
			" unlimited' before executing GDB next time.\n"),
		      reason);
}

/* Check whether GDB will be able to dump core using the dump_core
   function, and print a warning if we cannot.  */

static int
can_dump_core_warn (enum resource_limit_kind limit_kind,
		    const char *reason)
{
  int core_dump_allowed = can_dump_core (limit_kind);

  if (!core_dump_allowed)
    warn_cant_dump_core (reason);

  return core_dump_allowed;
}

/* Allow the user to configure the debugger behavior with respect to
   what to do when an internal problem is detected.  */

const char internal_problem_ask[] = "ask";
const char internal_problem_yes[] = "yes";
const char internal_problem_no[] = "no";
static const char *const internal_problem_modes[] =
{
  internal_problem_ask,
  internal_problem_yes,
  internal_problem_no,
  NULL
};

/* Print a message reporting an internal error/warning.  Ask the user
   if they want to continue, dump core, or just exit.  Return
   something to indicate a quit.  */

struct internal_problem
{
  const char *name;
  int user_settable_should_quit;
  const char *should_quit;
  int user_settable_should_dump_core;
  const char *should_dump_core;
};

/* Report a problem, internal to GDB, to the user.  Once the problem
   has been reported, and assuming GDB didn't quit, the caller can
   either allow execution to resume or throw an error.  */

static void ATTRIBUTE_PRINTF (4, 0)
internal_vproblem (struct internal_problem *problem,
		   const char *file, int line, const char *fmt, va_list ap)
{
  static int dejavu;
  int quit_p;
  int dump_core_p;
  std::string reason;

  /* Don't allow infinite error/warning recursion.  */
  {
    static const char msg[] = "Recursive internal problem.\n";

    switch (dejavu)
      {
      case 0:
	dejavu = 1;
	break;
      case 1:
	dejavu = 2;
	abort_with_message (msg);
      default:
	dejavu = 3;
	/* Newer GLIBC versions put the warn_unused_result attribute
	   on write, but this is one of those rare cases where
	   ignoring the return value is correct.  Casting to (void)
	   does not fix this problem.  This is the solution suggested
	   at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509.  */
	if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg))
	  abort (); /* ARI: abort */
	exit (1);
      }
  }

  /* Create a string containing the full error/warning message.  Need
     to call query with this full string, as otherwize the reason
     (error/warning) and question become separated.  Format using a
     style similar to a compiler error message.  Include extra detail
     so that the user knows that they are living on the edge.  */
  {
    std::string msg = string_vprintf (fmt, ap);
    reason = string_printf ("%s:%d: %s: %s\n"
			    "A problem internal to GDB has been detected,\n"
			    "further debugging may prove unreliable.",
			    file, line, problem->name, msg.c_str ());
  }

  /* Fall back to abort_with_message if gdb_stderr is not set up.  */
  if (current_ui == NULL)
    {
      fputs (reason.c_str (), stderr);
      abort_with_message ("\n");
    }

  /* Try to get the message out and at the start of a new line.  */
  gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
  if (target_supports_terminal_ours ())
    {
      term_state.emplace ();
      target_terminal::ours_for_output ();
    }
  if (filtered_printing_initialized ())
    begin_line ();

  /* Emit the message unless query will emit it below.  */
  if (problem->should_quit != internal_problem_ask
      || !confirm
      || !filtered_printing_initialized ())
    fprintf_unfiltered (gdb_stderr, "%s\n", reason.c_str ());

  if (problem->should_quit == internal_problem_ask)
    {
      /* Default (yes/batch case) is to quit GDB.  When in batch mode
	 this lessens the likelihood of GDB going into an infinite
	 loop.  */
      if (!confirm || !filtered_printing_initialized ())
	quit_p = 1;
      else
	quit_p = query (_("%s\nQuit this debugging session? "),
			reason.c_str ());
    }
  else if (problem->should_quit == internal_problem_yes)
    quit_p = 1;
  else if (problem->should_quit == internal_problem_no)
    quit_p = 0;
  else
    internal_error (__FILE__, __LINE__, _("bad switch"));

  fputs_unfiltered (_("\nThis is a bug, please report it."), gdb_stderr);
  if (REPORT_BUGS_TO[0])
    fprintf_unfiltered (gdb_stderr, _("  For instructions, see:\n%s."),
			REPORT_BUGS_TO);
  fputs_unfiltered ("\n\n", gdb_stderr);

  if (problem->should_dump_core == internal_problem_ask)
    {
      if (!can_dump_core_warn (LIMIT_MAX, reason.c_str ()))
	dump_core_p = 0;
      else if (!filtered_printing_initialized ())
	dump_core_p = 1;
      else
	{
	  /* Default (yes/batch case) is to dump core.  This leaves a GDB
	     `dropping' so that it is easier to see that something went
	     wrong in GDB.  */
	  dump_core_p = query (_("%s\nCreate a core file of GDB? "),
			       reason.c_str ());
	}
    }
  else if (problem->should_dump_core == internal_problem_yes)
    dump_core_p = can_dump_core_warn (LIMIT_MAX, reason.c_str ());
  else if (problem->should_dump_core == internal_problem_no)
    dump_core_p = 0;
  else
    internal_error (__FILE__, __LINE__, _("bad switch"));

  if (quit_p)
    {
      if (dump_core_p)
	dump_core ();
      else
	exit (1);
    }
  else
    {
      if (dump_core_p)
	{
#ifdef HAVE_WORKING_FORK
	  if (fork () == 0)
	    dump_core ();
#endif
	}
    }

  dejavu = 0;
}

static struct internal_problem internal_error_problem = {
  "internal-error", 1, internal_problem_ask, 1, internal_problem_ask
};

void
internal_verror (const char *file, int line, const char *fmt, va_list ap)
{
  internal_vproblem (&internal_error_problem, file, line, fmt, ap);
  throw_quit (_("Command aborted."));
}

static struct internal_problem internal_warning_problem = {
  "internal-warning", 1, internal_problem_ask, 1, internal_problem_ask
};

void
internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
{
  internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
}

static struct internal_problem demangler_warning_problem = {
  "demangler-warning", 1, internal_problem_ask, 0, internal_problem_no
};

void
demangler_vwarning (const char *file, int line, const char *fmt, va_list ap)
{
  internal_vproblem (&demangler_warning_problem, file, line, fmt, ap);
}

void
demangler_warning (const char *file, int line, const char *string, ...)
{
  va_list ap;

  va_start (ap, string);
  demangler_vwarning (file, line, string, ap);
  va_end (ap);
}

/* When GDB reports an internal problem (error or warning) it gives
   the user the opportunity to quit GDB and/or create a core file of
   the current debug session.  This function registers a few commands
   that make it possible to specify that GDB should always or never
   quit or create a core file, without asking.  The commands look
   like:

   maint set PROBLEM-NAME quit ask|yes|no
   maint show PROBLEM-NAME quit
   maint set PROBLEM-NAME corefile ask|yes|no
   maint show PROBLEM-NAME corefile

   Where PROBLEM-NAME is currently "internal-error" or
   "internal-warning".  */

static void
add_internal_problem_command (struct internal_problem *problem)
{
  struct cmd_list_element **set_cmd_list;
  struct cmd_list_element **show_cmd_list;
  char *set_doc;
  char *show_doc;

  set_cmd_list = XNEW (struct cmd_list_element *);
  show_cmd_list = XNEW (struct cmd_list_element *);
  *set_cmd_list = NULL;
  *show_cmd_list = NULL;

  set_doc = xstrprintf (_("Configure what GDB does when %s is detected."),
			problem->name);

  show_doc = xstrprintf (_("Show what GDB does when %s is detected."),
			 problem->name);

  add_basic_prefix_cmd (problem->name, class_maintenance, set_doc,
			set_cmd_list,
			concat ("maintenance set ", problem->name, " ",
				(char *) NULL),
			0/*allow-unknown*/, &maintenance_set_cmdlist);

  add_show_prefix_cmd (problem->name, class_maintenance, show_doc,
		       show_cmd_list,
		       concat ("maintenance show ", problem->name, " ",
			       (char *) NULL),
		       0/*allow-unknown*/, &maintenance_show_cmdlist);

  if (problem->user_settable_should_quit)
    {
      set_doc = xstrprintf (_("Set whether GDB should quit "
			      "when an %s is detected."),
			    problem->name);
      show_doc = xstrprintf (_("Show whether GDB will quit "
			       "when an %s is detected."),
			     problem->name);
      add_setshow_enum_cmd ("quit", class_maintenance,
			    internal_problem_modes,
			    &problem->should_quit,
			    set_doc,
			    show_doc,
			    NULL, /* help_doc */
			    NULL, /* setfunc */
			    NULL, /* showfunc */
			    set_cmd_list,
			    show_cmd_list);

      xfree (set_doc);
      xfree (show_doc);
    }

  if (problem->user_settable_should_dump_core)
    {
      set_doc = xstrprintf (_("Set whether GDB should create a core "
			      "file of GDB when %s is detected."),
			    problem->name);
      show_doc = xstrprintf (_("Show whether GDB will create a core "
			       "file of GDB when %s is detected."),
			     problem->name);
      add_setshow_enum_cmd ("corefile", class_maintenance,
			    internal_problem_modes,
			    &problem->should_dump_core,
			    set_doc,
			    show_doc,
			    NULL, /* help_doc */
			    NULL, /* setfunc */
			    NULL, /* showfunc */
			    set_cmd_list,
			    show_cmd_list);

      xfree (set_doc);
      xfree (show_doc);
    }
}

/* Return a newly allocated string, containing the PREFIX followed
   by the system error message for errno (separated by a colon).  */

static std::string
perror_string (const char *prefix)
{
  const char *err = safe_strerror (errno);
  return std::string (prefix) + ": " + err;
}

/* Print the system error message for errno, and also mention STRING
   as the file name for which the error was encountered.  Use ERRCODE
   for the thrown exception.  Then return to command level.  */

void
throw_perror_with_name (enum errors errcode, const char *string)
{
  std::string combined = perror_string (string);

  /* I understand setting these is a matter of taste.  Still, some people
     may clear errno but not know about bfd_error.  Doing this here is not
     unreasonable.  */
  bfd_set_error (bfd_error_no_error);
  errno = 0;

  throw_error (errcode, _("%s."), combined.c_str ());
}

/* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR.  */

void
perror_with_name (const char *string)
{
  throw_perror_with_name (GENERIC_ERROR, string);
}

/* Same as perror_with_name except that it prints a warning instead
   of throwing an error.  */

void
perror_warning_with_name (const char *string)
{
  std::string combined = perror_string (string);
  warning (_("%s"), combined.c_str ());
}

/* Print the system error message for ERRCODE, and also mention STRING
   as the file name for which the error was encountered.  */

void
print_sys_errmsg (const char *string, int errcode)
{
  const char *err = safe_strerror (errcode);
  /* We want anything which was printed on stdout to come out first, before
     this message.  */
  gdb_flush (gdb_stdout);
  fprintf_unfiltered (gdb_stderr, "%s: %s.\n", string, err);
}

/* Control C eventually causes this to be called, at a convenient time.  */

void
quit (void)
{
  if (sync_quit_force_run)
    {
      sync_quit_force_run = 0;
      quit_force (NULL, 0);
    }

#ifdef __MSDOS__
  /* No steenking SIGINT will ever be coming our way when the
     program is resumed.  Don't lie.  */
  throw_quit ("Quit");
#else
  if (job_control
      /* If there is no terminal switching for this target, then we can't
	 possibly get screwed by the lack of job control.  */
      || !target_supports_terminal_ours ())
    throw_quit ("Quit");
  else
    throw_quit ("Quit (expect signal SIGINT when the program is resumed)");
#endif
}

/* See defs.h.  */

void
maybe_quit (void)
{
  if (sync_quit_force_run)
    quit ();

  quit_handler ();
}


/* Called when a memory allocation fails, with the number of bytes of
   memory requested in SIZE.  */

void
malloc_failure (long size)
{
  if (size > 0)
    {
      internal_error (__FILE__, __LINE__,
		      _("virtual memory exhausted: can't allocate %ld bytes."),
		      size);
    }
  else
    {
      internal_error (__FILE__, __LINE__, _("virtual memory exhausted."));
    }
}

/* See common/errors.h.  */

void
flush_streams ()
{
  gdb_stdout->flush ();
  gdb_stderr->flush ();
}

/* My replacement for the read system call.
   Used like `read' but keeps going if `read' returns too soon.  */

int
myread (int desc, char *addr, int len)
{
  int val;
  int orglen = len;

  while (len > 0)
    {
      val = read (desc, addr, len);
      if (val < 0)
	return val;
      if (val == 0)
	return orglen - len;
      len -= val;
      addr += val;
    }
  return orglen;
}

/* See utils.h.  */

ULONGEST
uinteger_pow (ULONGEST v1, LONGEST v2)
{
  if (v2 < 0)
    {
      if (v1 == 0)
	error (_("Attempt to raise 0 to negative power."));
      else
	return 0;
    }
  else
    {
      /* The Russian Peasant's Algorithm.  */
      ULONGEST v;

      v = 1;
      for (;;)
	{
	  if (v2 & 1L)
	    v *= v1;
	  v2 >>= 1;
	  if (v2 == 0)
	    return v;
	  v1 *= v1;
	}
    }
}

void
print_spaces (int n, struct ui_file *file)
{
  fputs_unfiltered (n_spaces (n), file);
}

/* Print a host address.  */

void
gdb_print_host_address_1 (const void *addr, struct ui_file *stream)
{
  fprintf_filtered (stream, "%s", host_address_to_string (addr));
}



/* An RAII class that sets up to handle input and then tears down
   during destruction.  */

class scoped_input_handler
{
public:

  scoped_input_handler ()
    : m_quit_handler (&quit_handler, default_quit_handler),
      m_ui (NULL)
  {
    target_terminal::ours ();
    ui_register_input_event_handler (current_ui);
    if (current_ui->prompt_state == PROMPT_BLOCKED)
      m_ui = current_ui;
  }

  ~scoped_input_handler ()
  {
    if (m_ui != NULL)
      ui_unregister_input_event_handler (m_ui);
  }

  DISABLE_COPY_AND_ASSIGN (scoped_input_handler);

private:

  /* Save and restore the terminal state.  */
  target_terminal::scoped_restore_terminal_state m_term_state;

  /* Save and restore the quit handler.  */
  scoped_restore_tmpl<quit_handler_ftype *> m_quit_handler;

  /* The saved UI, if non-NULL.  */
  struct ui *m_ui;
};



/* This function supports the query, nquery, and yquery functions.
   Ask user a y-or-n question and return 0 if answer is no, 1 if
   answer is yes, or default the answer to the specified default
   (for yquery or nquery).  DEFCHAR may be 'y' or 'n' to provide a
   default answer, or '\0' for no default.
   CTLSTR is the control string and should end in "? ".  It should
   not say how to answer, because we do that.
   ARGS are the arguments passed along with the CTLSTR argument to
   printf.  */

static int ATTRIBUTE_PRINTF (1, 0)
defaulted_query (const char *ctlstr, const char defchar, va_list args)
{
  int retval;
  int def_value;
  char def_answer, not_def_answer;
  const char *y_string, *n_string;

  /* Set up according to which answer is the default.  */
  if (defchar == '\0')
    {
      def_value = 1;
      def_answer = 'Y';
      not_def_answer = 'N';
      y_string = "y";
      n_string = "n";
    }
  else if (defchar == 'y')
    {
      def_value = 1;
      def_answer = 'Y';
      not_def_answer = 'N';
      y_string = "[y]";
      n_string = "n";
    }
  else
    {
      def_value = 0;
      def_answer = 'N';
      not_def_answer = 'Y';
      y_string = "y";
      n_string = "[n]";
    }

  /* Automatically answer the default value if the user did not want
     prompts or the command was issued with the server prefix.  */
  if (!confirm || server_command)
    return def_value;

  /* If input isn't coming from the user directly, just say what
     question we're asking, and then answer the default automatically.  This
     way, important error messages don't get lost when talking to GDB
     over a pipe.  */
  if (current_ui->instream != current_ui->stdin_stream
      || !input_interactive_p (current_ui)
      /* Restrict queries to the main UI.  */
      || current_ui != main_ui)
    {
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
      wrap_here ("");
      vfprintf_filtered (gdb_stdout, ctlstr, args);

      printf_filtered (_("(%s or %s) [answered %c; "
			 "input not from terminal]\n"),
		       y_string, n_string, def_answer);

      return def_value;
    }

  if (deprecated_query_hook)
    {
      target_terminal::scoped_restore_terminal_state term_state;
      return deprecated_query_hook (ctlstr, args);
    }

  /* Format the question outside of the loop, to avoid reusing args.  */
  std::string question = string_vprintf (ctlstr, args);
  std::string prompt
    = string_printf (_("%s%s(%s or %s) %s"),
		     annotation_level > 1 ? "\n\032\032pre-query\n" : "",
		     question.c_str (), y_string, n_string,
		     annotation_level > 1 ? "\n\032\032query\n" : "");

  /* Used to add duration we waited for user to respond to
     prompt_for_continue_wait_time.  */
  using namespace std::chrono;
  steady_clock::time_point prompt_started = steady_clock::now ();

  scoped_input_handler prepare_input;

  while (1)
    {
      char *response, answer;

      gdb_flush (gdb_stdout);
      response = gdb_readline_wrapper (prompt.c_str ());

      if (response == NULL)	/* C-d  */
	{
	  printf_filtered ("EOF [assumed %c]\n", def_answer);
	  retval = def_value;
	  break;
	}

      answer = response[0];
      xfree (response);

      if (answer >= 'a')
	answer -= 040;
      /* Check answer.  For the non-default, the user must specify
	 the non-default explicitly.  */
      if (answer == not_def_answer)
	{
	  retval = !def_value;
	  break;
	}
      /* Otherwise, if a default was specified, the user may either
	 specify the required input or have it default by entering
	 nothing.  */
      if (answer == def_answer
	  || (defchar != '\0' && answer == '\0'))
	{
	  retval = def_value;
	  break;
	}
      /* Invalid entries are not defaulted and require another selection.  */
      printf_filtered (_("Please answer %s or %s.\n"),
		       y_string, n_string);
    }

  /* Add time spend in this routine to prompt_for_continue_wait_time.  */
  prompt_for_continue_wait_time += steady_clock::now () - prompt_started;

  if (annotation_level > 1)
    printf_filtered (("\n\032\032post-query\n"));
  return retval;
}


/* Ask user a y-or-n question and return 0 if answer is no, 1 if
   answer is yes, or 0 if answer is defaulted.
   Takes three args which are given to printf to print the question.
   The first, a control string, should end in "? ".
   It should not say how to answer, because we do that.  */

int
nquery (const char *ctlstr, ...)
{
  va_list args;
  int ret;

  va_start (args, ctlstr);
  ret = defaulted_query (ctlstr, 'n', args);
  va_end (args);
  return ret;
}

/* Ask user a y-or-n question and return 0 if answer is no, 1 if
   answer is yes, or 1 if answer is defaulted.
   Takes three args which are given to printf to print the question.
   The first, a control string, should end in "? ".
   It should not say how to answer, because we do that.  */

int
yquery (const char *ctlstr, ...)
{
  va_list args;
  int ret;

  va_start (args, ctlstr);
  ret = defaulted_query (ctlstr, 'y', args);
  va_end (args);
  return ret;
}

/* Ask user a y-or-n question and return 1 iff answer is yes.
   Takes three args which are given to printf to print the question.
   The first, a control string, should end in "? ".
   It should not say how to answer, because we do that.  */

int
query (const char *ctlstr, ...)
{
  va_list args;
  int ret;

  va_start (args, ctlstr);
  ret = defaulted_query (ctlstr, '\0', args);
  va_end (args);
  return ret;
}

/* A helper for parse_escape that converts a host character to a
   target character.  C is the host character.  If conversion is
   possible, then the target character is stored in *TARGET_C and the
   function returns 1.  Otherwise, the function returns 0.  */

static int
host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c)
{
  char the_char = c;
  int result = 0;

  auto_obstack host_data;

  convert_between_encodings (target_charset (gdbarch), host_charset (),
			     (gdb_byte *) &the_char, 1, 1,
			     &host_data, translit_none);

  if (obstack_object_size (&host_data) == 1)
    {
      result = 1;
      *target_c = *(char *) obstack_base (&host_data);
    }

  return result;
}

/* Parse a C escape sequence.  STRING_PTR points to a variable
   containing a pointer to the string to parse.  That pointer
   should point to the character after the \.  That pointer
   is updated past the characters we use.  The value of the
   escape sequence is returned.

   A negative value means the sequence \ newline was seen,
   which is supposed to be equivalent to nothing at all.

   If \ is followed by a null character, we return a negative
   value and leave the string pointer pointing at the null character.

   If \ is followed by 000, we return 0 and leave the string pointer
   after the zeros.  A value of 0 does not mean end of string.  */

int
parse_escape (struct gdbarch *gdbarch, const char **string_ptr)
{
  int target_char = -2;	/* Initialize to avoid GCC warnings.  */
  int c = *(*string_ptr)++;

  switch (c)
    {
      case '\n':
	return -2;
      case 0:
	(*string_ptr)--;
	return 0;

      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
	{
	  int i = host_hex_value (c);
	  int count = 0;
	  while (++count < 3)
	    {
	      c = (**string_ptr);
	      if (ISDIGIT (c) && c != '8' && c != '9')
		{
		  (*string_ptr)++;
		  i *= 8;
		  i += host_hex_value (c);
		}
	      else
		{
		  break;
		}
	    }
	  return i;
	}

    case 'a':
      c = '\a';
      break;
    case 'b':
      c = '\b';
      break;
    case 'f':
      c = '\f';
      break;
    case 'n':
      c = '\n';
      break;
    case 'r':
      c = '\r';
      break;
    case 't':
      c = '\t';
      break;
    case 'v':
      c = '\v';
      break;

    default:
      break;
    }

  if (!host_char_to_target (gdbarch, c, &target_char))
    error (_("The escape sequence `\\%c' is equivalent to plain `%c',"
	     " which has no equivalent\nin the `%s' character set."),
	   c, c, target_charset (gdbarch));
  return target_char;
}

/* Print the character C on STREAM as part of the contents of a literal
   string whose delimiter is QUOTER.  Note that this routine should only
   be called for printing things which are independent of the language
   of the program being debugged.

   printchar will normally escape backslashes and instances of QUOTER. If
   QUOTER is 0, printchar won't escape backslashes or any quoting character.
   As a side effect, if you pass the backslash character as the QUOTER,
   printchar will escape backslashes as usual, but not any other quoting
   character. */

static void
printchar (int c, do_fputc_ftype do_fputc, ui_file *stream, int quoter)
{
  c &= 0xFF;			/* Avoid sign bit follies */

  if (c < 0x20 ||		/* Low control chars */
      (c >= 0x7F && c < 0xA0) ||	/* DEL, High controls */
      (sevenbit_strings && c >= 0x80))
    {				/* high order bit set */
      do_fputc ('\\', stream);

      switch (c)
	{
	case '\n':
	  do_fputc ('n', stream);
	  break;
	case '\b':
	  do_fputc ('b', stream);
	  break;
	case '\t':
	  do_fputc ('t', stream);
	  break;
	case '\f':
	  do_fputc ('f', stream);
	  break;
	case '\r':
	  do_fputc ('r', stream);
	  break;
	case '\033':
	  do_fputc ('e', stream);
	  break;
	case '\007':
	  do_fputc ('a', stream);
	  break;
	default:
	  {
	    do_fputc ('0' + ((c >> 6) & 0x7), stream);
	    do_fputc ('0' + ((c >> 3) & 0x7), stream);
	    do_fputc ('0' + ((c >> 0) & 0x7), stream);
	    break;
	  }
	}
    }
  else
    {
      if (quoter != 0 && (c == '\\' || c == quoter))
	do_fputc ('\\', stream);
      do_fputc (c, stream);
    }
}

/* Print the character C on STREAM as part of the contents of a
   literal string whose delimiter is QUOTER.  Note that these routines
   should only be call for printing things which are independent of
   the language of the program being debugged.  */

void
fputstr_filtered (const char *str, int quoter, struct ui_file *stream)
{
  while (*str)
    printchar (*str++, fputc_filtered, stream, quoter);
}

void
fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream)
{
  while (*str)
    printchar (*str++, fputc_unfiltered, stream, quoter);
}

void
fputstrn_filtered (const char *str, int n, int quoter,
		   struct ui_file *stream)
{
  for (int i = 0; i < n; i++)
    printchar (str[i], fputc_filtered, stream, quoter);
}

void
fputstrn_unfiltered (const char *str, int n, int quoter,
		     do_fputc_ftype do_fputc, struct ui_file *stream)
{
  for (int i = 0; i < n; i++)
    printchar (str[i], do_fputc, stream, quoter);
}


/* Number of lines per page or UINT_MAX if paging is disabled.  */
static unsigned int lines_per_page;
static void
show_lines_per_page (struct ui_file *file, int from_tty,
		     struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file,
		    _("Number of lines gdb thinks are in a page is %s.\n"),
		    value);
}

/* Number of chars per line or UINT_MAX if line folding is disabled.  */
static unsigned int chars_per_line;
static void
show_chars_per_line (struct ui_file *file, int from_tty,
		     struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file,
		    _("Number of characters gdb thinks "
		      "are in a line is %s.\n"),
		    value);
}

/* Current count of lines printed on this page, chars on this line.  */
static unsigned int lines_printed, chars_printed;

/* True if pagination is disabled for just one command.  */

static bool pagination_disabled_for_command;

/* Buffer and start column of buffered text, for doing smarter word-
   wrapping.  When someone calls wrap_here(), we start buffering output
   that comes through fputs_filtered().  If we see a newline, we just
   spit it out and forget about the wrap_here().  If we see another
   wrap_here(), we spit it out and remember the newer one.  If we see
   the end of the line, we spit out a newline, the indent, and then
   the buffered output.  */

static bool filter_initialized = false;

/* Contains characters which are waiting to be output (they have
   already been counted in chars_printed).  */
static std::string wrap_buffer;

/* String to indent by if the wrap occurs.  Must not be NULL if wrap_column
   is non-zero.  */
static const char *wrap_indent;

/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
   is not in effect.  */
static int wrap_column;

/* The style applied at the time that wrap_here was called.  */
static ui_file_style wrap_style;


/* Initialize the number of lines per page and chars per line.  */

void
init_page_info (void)
{
  if (batch_flag)
    {
      lines_per_page = UINT_MAX;
      chars_per_line = UINT_MAX;
    }
  else
#if defined(TUI)
  if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
#endif
    {
      int rows, cols;

#if defined(__GO32__)
      rows = ScreenRows ();
      cols = ScreenCols ();
      lines_per_page = rows;
      chars_per_line = cols;
#else
      /* Make sure Readline has initialized its terminal settings.  */
      rl_reset_terminal (NULL);

      /* Get the screen size from Readline.  */
      rl_get_screen_size (&rows, &cols);
      lines_per_page = rows;
      chars_per_line = cols;

      /* Readline should have fetched the termcap entry for us.
	 Only try to use tgetnum function if rl_get_screen_size
	 did not return a useful value. */
      if (((rows <= 0) && (tgetnum ((char *) "li") < 0))
	/* Also disable paging if inside Emacs.  $EMACS was used
	   before Emacs v25.1, $INSIDE_EMACS is used since then.  */
	  || getenv ("EMACS") || getenv ("INSIDE_EMACS"))
	{
	  /* The number of lines per page is not mentioned in the terminal
	     description or EMACS environment variable is set.  This probably
	     means that paging is not useful, so disable paging.  */
	  lines_per_page = UINT_MAX;
	}

      /* If the output is not a terminal, don't paginate it.  */
      if (!gdb_stdout->isatty ())
	lines_per_page = UINT_MAX;
#endif
    }

  /* We handle SIGWINCH ourselves.  */
  rl_catch_sigwinch = 0;

  set_screen_size ();
  set_width ();
}

/* Return nonzero if filtered printing is initialized.  */
int
filtered_printing_initialized (void)
{
  return filter_initialized;
}

set_batch_flag_and_restore_page_info::set_batch_flag_and_restore_page_info ()
  : m_save_lines_per_page (lines_per_page),
    m_save_chars_per_line (chars_per_line),
    m_save_batch_flag (batch_flag)
{
  batch_flag = 1;
  init_page_info ();
}

set_batch_flag_and_restore_page_info::~set_batch_flag_and_restore_page_info ()
{
  batch_flag = m_save_batch_flag;
  chars_per_line = m_save_chars_per_line;
  lines_per_page = m_save_lines_per_page;

  set_screen_size ();
  set_width ();
}

/* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE.  */

static void
set_screen_size (void)
{
  int rows = lines_per_page;
  int cols = chars_per_line;

  /* If we get 0 or negative ROWS or COLS, treat as "infinite" size.
     A negative number can be seen here with the "set width/height"
     commands and either:

     - the user specified "unlimited", which maps to UINT_MAX, or
     - the user specified some number between INT_MAX and UINT_MAX.

     Cap "infinity" to approximately sqrt(INT_MAX) so that we don't
     overflow in rl_set_screen_size, which multiplies rows and columns
     to compute the number of characters on the screen.  */

  const int sqrt_int_max = INT_MAX >> (sizeof (int) * 8 / 2);

  if (rows <= 0 || rows > sqrt_int_max)
    {
      rows = sqrt_int_max;
      lines_per_page = UINT_MAX;
    }

  if (cols <= 0 || cols > sqrt_int_max)
    {
      cols = sqrt_int_max;
      chars_per_line = UINT_MAX;
    }

  /* Update Readline's idea of the terminal size.  */
  rl_set_screen_size (rows, cols);
}

/* Reinitialize WRAP_BUFFER.  */

static void
set_width (void)
{
  if (chars_per_line == 0)
    init_page_info ();

  wrap_buffer.clear ();
  filter_initialized = true;
}

static void
set_width_command (const char *args, int from_tty, struct cmd_list_element *c)
{
  set_screen_size ();
  set_width ();
}

static void
set_height_command (const char *args, int from_tty, struct cmd_list_element *c)
{
  set_screen_size ();
}

/* See utils.h.  */

void
set_screen_width_and_height (int width, int height)
{
  lines_per_page = height;
  chars_per_line = width;

  set_screen_size ();
  set_width ();
}

/* The currently applied style.  */

static ui_file_style applied_style;

/* Emit an ANSI style escape for STYLE.  If STREAM is nullptr, emit to
   the wrap buffer; otherwise emit to STREAM.  */

static void
emit_style_escape (const ui_file_style &style,
		   struct ui_file *stream = nullptr)
{
  applied_style = style;

  if (stream == nullptr)
    wrap_buffer.append (style.to_ansi ());
  else
    stream->puts (style.to_ansi ().c_str ());
}

/* Set the current output style.  This will affect future uses of the
   _filtered output functions.  */

static void
set_output_style (struct ui_file *stream, const ui_file_style &style)
{
  if (!stream->can_emit_style_escape ())
    return;

  /* Note that we may not pass STREAM here, when we want to emit to
     the wrap buffer, not directly to STREAM.  */
  if (stream == gdb_stdout)
    stream = nullptr;
  emit_style_escape (style, stream);
}

/* See utils.h.  */

void
reset_terminal_style (struct ui_file *stream)
{
  if (stream->can_emit_style_escape ())
    {
      /* Force the setting, regardless of what we think the setting
	 might already be.  */
      applied_style = ui_file_style ();
      wrap_buffer.append (applied_style.to_ansi ());
    }
}

/* Wait, so the user can read what's on the screen.  Prompt the user
   to continue by pressing RETURN.  'q' is also provided because
   telling users what to do in the prompt is more user-friendly than
   expecting them to think of Ctrl-C/SIGINT.  */

static void
prompt_for_continue (void)
{
  char cont_prompt[120];
  /* Used to add duration we waited for user to respond to
     prompt_for_continue_wait_time.  */
  using namespace std::chrono;
  steady_clock::time_point prompt_started = steady_clock::now ();
  bool disable_pagination = pagination_disabled_for_command;

  /* Clear the current styling.  */
  if (gdb_stdout->can_emit_style_escape ())
    emit_style_escape (ui_file_style (), gdb_stdout);

  if (annotation_level > 1)
    printf_unfiltered (("\n\032\032pre-prompt-for-continue\n"));

  strcpy (cont_prompt,
	  "--Type <RET> for more, q to quit, "
	  "c to continue without paging--");
  if (annotation_level > 1)
    strcat (cont_prompt, "\n\032\032prompt-for-continue\n");

  /* We must do this *before* we call gdb_readline_wrapper, else it
     will eventually call us -- thinking that we're trying to print
     beyond the end of the screen.  */
  reinitialize_more_filter ();

  scoped_input_handler prepare_input;

  /* Call gdb_readline_wrapper, not readline, in order to keep an
     event loop running.  */
  gdb::unique_xmalloc_ptr<char> ignore (gdb_readline_wrapper (cont_prompt));

  /* Add time spend in this routine to prompt_for_continue_wait_time.  */
  prompt_for_continue_wait_time += steady_clock::now () - prompt_started;

  if (annotation_level > 1)
    printf_unfiltered (("\n\032\032post-prompt-for-continue\n"));

  if (ignore != NULL)
    {
      char *p = ignore.get ();

      while (*p == ' ' || *p == '\t')
	++p;
      if (p[0] == 'q')
	/* Do not call quit here; there is no possibility of SIGINT.  */
	throw_quit ("Quit");
      if (p[0] == 'c')
	disable_pagination = true;
    }

  /* Now we have to do this again, so that GDB will know that it doesn't
     need to save the ---Type <return>--- line at the top of the screen.  */
  reinitialize_more_filter ();
  pagination_disabled_for_command = disable_pagination;

  dont_repeat ();		/* Forget prev cmd -- CR won't repeat it.  */
}

/* Initialize timer to keep track of how long we waited for the user.  */

void
reset_prompt_for_continue_wait_time (void)
{
  using namespace std::chrono;

  prompt_for_continue_wait_time = steady_clock::duration::zero ();
}

/* Fetch the cumulative time spent in prompt_for_continue.  */

std::chrono::steady_clock::duration
get_prompt_for_continue_wait_time ()
{
  return prompt_for_continue_wait_time;
}

/* Reinitialize filter; ie. tell it to reset to original values.  */

void
reinitialize_more_filter (void)
{
  lines_printed = 0;
  chars_printed = 0;
  pagination_disabled_for_command = false;
}

/* Flush the wrap buffer to STREAM, if necessary.  */

static void
flush_wrap_buffer (struct ui_file *stream)
{
  if (stream == gdb_stdout && !wrap_buffer.empty ())
    {
      stream->puts (wrap_buffer.c_str ());
      wrap_buffer.clear ();
    }
}

/* See utils.h.  */

void
gdb_flush (struct ui_file *stream)
{
  flush_wrap_buffer (stream);
  stream->flush ();
}

/* See utils.h.  */

int
get_chars_per_line ()
{
  return chars_per_line;
}

/* Indicate that if the next sequence of characters overflows the line,
   a newline should be inserted here rather than when it hits the end.
   If INDENT is non-null, it is a string to be printed to indent the
   wrapped part on the next line.  INDENT must remain accessible until
   the next call to wrap_here() or until a newline is printed through
   fputs_filtered().

   If the line is already overfull, we immediately print a newline and
   the indentation, and disable further wrapping.

   If we don't know the width of lines, but we know the page height,
   we must not wrap words, but should still keep track of newlines
   that were explicitly printed.

   INDENT should not contain tabs, as that will mess up the char count
   on the next line.  FIXME.

   This routine is guaranteed to force out any output which has been
   squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
   used to force out output from the wrap_buffer.  */

void
wrap_here (const char *indent)
{
  /* This should have been allocated, but be paranoid anyway.  */
  gdb_assert (filter_initialized);

  flush_wrap_buffer (gdb_stdout);
  if (chars_per_line == UINT_MAX)	/* No line overflow checking.  */
    {
      wrap_column = 0;
    }
  else if (chars_printed >= chars_per_line)
    {
      puts_filtered ("\n");
      if (indent != NULL)
	puts_filtered (indent);
      wrap_column = 0;
    }
  else
    {
      wrap_column = chars_printed;
      if (indent == NULL)
	wrap_indent = "";
      else
	wrap_indent = indent;
      wrap_style = applied_style;
    }
}

/* Print input string to gdb_stdout, filtered, with wrap, 
   arranging strings in columns of n chars.  String can be
   right or left justified in the column.  Never prints 
   trailing spaces.  String should never be longer than
   width.  FIXME: this could be useful for the EXAMINE 
   command, which currently doesn't tabulate very well.  */

void
puts_filtered_tabular (char *string, int width, int right)
{
  int spaces = 0;
  int stringlen;
  char *spacebuf;

  gdb_assert (chars_per_line > 0);
  if (chars_per_line == UINT_MAX)
    {
      fputs_filtered (string, gdb_stdout);
      fputs_filtered ("\n", gdb_stdout);
      return;
    }

  if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
    fputs_filtered ("\n", gdb_stdout);

  if (width >= chars_per_line)
    width = chars_per_line - 1;

  stringlen = strlen (string);

  if (chars_printed > 0)
    spaces = width - (chars_printed - 1) % width - 1;
  if (right)
    spaces += width - stringlen;

  spacebuf = (char *) alloca (spaces + 1);
  spacebuf[spaces] = '\0';
  while (spaces--)
    spacebuf[spaces] = ' ';

  fputs_filtered (spacebuf, gdb_stdout);
  fputs_filtered (string, gdb_stdout);
}


/* Ensure that whatever gets printed next, using the filtered output
   commands, starts at the beginning of the line.  I.e. if there is
   any pending output for the current line, flush it and start a new
   line.  Otherwise do nothing.  */

void
begin_line (void)
{
  if (chars_printed > 0)
    {
      puts_filtered ("\n");
    }
}


/* Like fputs but if FILTER is true, pause after every screenful.

   Regardless of FILTER can wrap at points other than the final
   character of a line.

   Unlike fputs, fputs_maybe_filtered does not return a value.
   It is OK for LINEBUFFER to be NULL, in which case just don't print
   anything.

   Note that a longjmp to top level may occur in this routine (only if
   FILTER is true) (since prompt_for_continue may do so) so this
   routine should not be called when cleanups are not in place.  */

static void
fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
		      int filter)
{
  const char *lineptr;

  if (linebuffer == 0)
    return;

  /* Don't do any filtering if it is disabled.  */
  if (stream != gdb_stdout
      || !pagination_enabled
      || pagination_disabled_for_command
      || batch_flag
      || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)
      || top_level_interpreter () == NULL
      || top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
    {
      flush_wrap_buffer (stream);
      stream->puts (linebuffer);
      return;
    }

  auto buffer_clearer
    = make_scope_exit ([&] ()
		       {
			 wrap_buffer.clear ();
			 wrap_column = 0;
			 wrap_indent = "";
		       });

  /* Go through and output each character.  Show line extension
     when this is necessary; prompt user for new page when this is
     necessary.  */

  lineptr = linebuffer;
  while (*lineptr)
    {
      /* Possible new page.  Note that PAGINATION_DISABLED_FOR_COMMAND
	 might be set during this loop, so we must continue to check
	 it here.  */
      if (filter && (lines_printed >= lines_per_page - 1)
	  && !pagination_disabled_for_command)
	prompt_for_continue ();

      while (*lineptr && *lineptr != '\n')
	{
	  int skip_bytes;

	  /* Print a single line.  */
	  if (*lineptr == '\t')
	    {
	      wrap_buffer.push_back ('\t');
	      /* Shifting right by 3 produces the number of tab stops
		 we have already passed, and then adding one and
		 shifting left 3 advances to the next tab stop.  */
	      chars_printed = ((chars_printed >> 3) + 1) << 3;
	      lineptr++;
	    }
	  else if (*lineptr == '\033'
		   && skip_ansi_escape (lineptr, &skip_bytes))
	    {
	      wrap_buffer.append (lineptr, skip_bytes);
	      /* Note that we don't consider this a character, so we
		 don't increment chars_printed here.  */
	      lineptr += skip_bytes;
	    }
	  else if (*lineptr == '\r')
	    {
	      wrap_buffer.push_back (*lineptr);
	      chars_printed = 0;
	      lineptr++;
	    }
	  else
	    {
	      wrap_buffer.push_back (*lineptr);
	      chars_printed++;
	      lineptr++;
	    }

	  if (chars_printed >= chars_per_line)
	    {
	      unsigned int save_chars = chars_printed;

	      /* If we change the style, below, we'll want to reset it
		 before continuing to print.  If there is no wrap
		 column, then we'll only reset the style if the pager
		 prompt is given; and to avoid emitting style
		 sequences in the middle of a run of text, we track
		 this as well.  */
	      ui_file_style save_style;
	      bool did_paginate = false;

	      chars_printed = 0;
	      lines_printed++;
	      if (wrap_column)
		{
		  save_style = wrap_style;
		  if (stream->can_emit_style_escape ())
		    emit_style_escape (ui_file_style (), stream);
		  /* If we aren't actually wrapping, don't output
		     newline -- if chars_per_line is right, we
		     probably just overflowed anyway; if it's wrong,
		     let us keep going.  */
		  /* XXX: The ideal thing would be to call
		     'stream->putc' here, but we can't because it
		     currently calls 'fputc_unfiltered', which ends up
		     calling us, which generates an infinite
		     recursion.  */
		  stream->puts ("\n");
		}
	      else
		{
		  save_style = applied_style;
		  flush_wrap_buffer (stream);
		}

	      /* Possible new page.  Note that
		 PAGINATION_DISABLED_FOR_COMMAND might be set during
		 this loop, so we must continue to check it here.  */
	      if (lines_printed >= lines_per_page - 1
		  && !pagination_disabled_for_command)
		{
		  prompt_for_continue ();
		  did_paginate = true;
		}

	      /* Now output indentation and wrapped string.  */
	      if (wrap_column)
		{
		  stream->puts (wrap_indent);
		  if (stream->can_emit_style_escape ())
		    emit_style_escape (save_style, stream);
		  /* FIXME, this strlen is what prevents wrap_indent from
		     containing tabs.  However, if we recurse to print it
		     and count its chars, we risk trouble if wrap_indent is
		     longer than (the user settable) chars_per_line.
		     Note also that this can set chars_printed > chars_per_line
		     if we are printing a long string.  */
		  chars_printed = strlen (wrap_indent)
		    + (save_chars - wrap_column);
		  wrap_column = 0;	/* And disable fancy wrap */
		}
	      else if (did_paginate && stream->can_emit_style_escape ())
		emit_style_escape (save_style, stream);
	    }
	}

      if (*lineptr == '\n')
	{
	  chars_printed = 0;
	  wrap_here ((char *) 0);	/* Spit out chars, cancel
					   further wraps.  */
	  lines_printed++;
	  /* XXX: The ideal thing would be to call
	     'stream->putc' here, but we can't because it
	     currently calls 'fputc_unfiltered', which ends up
	     calling us, which generates an infinite
	     recursion.  */
	  stream->puts ("\n");
	  lineptr++;
	}
    }

  buffer_clearer.release ();
}

void
fputs_filtered (const char *linebuffer, struct ui_file *stream)
{
  fputs_maybe_filtered (linebuffer, stream, 1);
}

void
fputs_unfiltered (const char *linebuffer, struct ui_file *stream)
{
  fputs_maybe_filtered (linebuffer, stream, 0);
}

/* See utils.h.  */

void
fputs_styled (const char *linebuffer, const ui_file_style &style,
	      struct ui_file *stream)
{
  /* This just makes it so we emit somewhat fewer escape
     sequences.  */
  if (style.is_default ())
    fputs_maybe_filtered (linebuffer, stream, 1);
  else
    {
      set_output_style (stream, style);
      fputs_maybe_filtered (linebuffer, stream, 1);
      set_output_style (stream, ui_file_style ());
    }
}

/* See utils.h.  */

void
fputs_styled_unfiltered (const char *linebuffer, const ui_file_style &style,
			 struct ui_file *stream)
{
  /* This just makes it so we emit somewhat fewer escape
     sequences.  */
  if (style.is_default ())
    fputs_maybe_filtered (linebuffer, stream, 0);
  else
    {
      set_output_style (stream, style);
      fputs_maybe_filtered (linebuffer, stream, 0);
      set_output_style (stream, ui_file_style ());
    }
}

/* See utils.h.  */

void
fputs_highlighted (const char *str, const compiled_regex &highlight,
		   struct ui_file *stream)
{
  regmatch_t pmatch;

  while (*str && highlight.exec (str, 1, &pmatch, 0) == 0)
    {
      size_t n_highlight = pmatch.rm_eo - pmatch.rm_so;

      /* Output the part before pmatch with current style.  */
      while (pmatch.rm_so > 0)
	{
	  fputc_filtered (*str, stream);
	  pmatch.rm_so--;
	  str++;
	}

      /* Output pmatch with the highlight style.  */
      set_output_style (stream, highlight_style.style ());
      while (n_highlight > 0)
	{
	  fputc_filtered (*str, stream);
	  n_highlight--;
	  str++;
	}
      set_output_style (stream, ui_file_style ());
    }

  /* Output the trailing part of STR not matching HIGHLIGHT.  */
  if (*str)
    fputs_filtered (str, stream);
}

int
putchar_unfiltered (int c)
{
  return fputc_unfiltered (c, gdb_stdout);
}

/* Write character C to gdb_stdout using GDB's paging mechanism and return C.
   May return nonlocally.  */

int
putchar_filtered (int c)
{
  return fputc_filtered (c, gdb_stdout);
}

int
fputc_unfiltered (int c, struct ui_file *stream)
{
  char buf[2];

  buf[0] = c;
  buf[1] = 0;
  fputs_unfiltered (buf, stream);
  return c;
}

int
fputc_filtered (int c, struct ui_file *stream)
{
  char buf[2];

  buf[0] = c;
  buf[1] = 0;
  fputs_filtered (buf, stream);
  return c;
}

/* puts_debug is like fputs_unfiltered, except it prints special
   characters in printable fashion.  */

void
puts_debug (char *prefix, char *string, char *suffix)
{
  int ch;

  /* Print prefix and suffix after each line.  */
  static int new_line = 1;
  static int return_p = 0;
  static const char *prev_prefix = "";
  static const char *prev_suffix = "";

  if (*string == '\n')
    return_p = 0;

  /* If the prefix is changing, print the previous suffix, a new line,
     and the new prefix.  */
  if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line)
    {
      fputs_unfiltered (prev_suffix, gdb_stdlog);
      fputs_unfiltered ("\n", gdb_stdlog);
      fputs_unfiltered (prefix, gdb_stdlog);
    }

  /* Print prefix if we printed a newline during the previous call.  */
  if (new_line)
    {
      new_line = 0;
      fputs_unfiltered (prefix, gdb_stdlog);
    }

  prev_prefix = prefix;
  prev_suffix = suffix;

  /* Output characters in a printable format.  */
  while ((ch = *string++) != '\0')
    {
      switch (ch)
	{
	default:
	  if (gdb_isprint (ch))
	    fputc_unfiltered (ch, gdb_stdlog);

	  else
	    fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff);
	  break;

	case '\\':
	  fputs_unfiltered ("\\\\", gdb_stdlog);
	  break;
	case '\b':
	  fputs_unfiltered ("\\b", gdb_stdlog);
	  break;
	case '\f':
	  fputs_unfiltered ("\\f", gdb_stdlog);
	  break;
	case '\n':
	  new_line = 1;
	  fputs_unfiltered ("\\n", gdb_stdlog);
	  break;
	case '\r':
	  fputs_unfiltered ("\\r", gdb_stdlog);
	  break;
	case '\t':
	  fputs_unfiltered ("\\t", gdb_stdlog);
	  break;
	case '\v':
	  fputs_unfiltered ("\\v", gdb_stdlog);
	  break;
	}

      return_p = ch == '\r';
    }

  /* Print suffix if we printed a newline.  */
  if (new_line)
    {
      fputs_unfiltered (suffix, gdb_stdlog);
      fputs_unfiltered ("\n", gdb_stdlog);
    }
}


/* Print a variable number of ARGS using format FORMAT.  If this
   information is going to put the amount written (since the last call
   to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
   call prompt_for_continue to get the users permission to continue.

   Unlike fprintf, this function does not return a value.

   We implement three variants, vfprintf (takes a vararg list and stream),
   fprintf (takes a stream to write on), and printf (the usual).

   Note also that this may throw a quit (since prompt_for_continue may
   do so).  */

static void
vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
			 va_list args, bool filter, bool gdbfmt)
{
  if (gdbfmt)
    {
      ui_out_flags flags = disallow_ui_out_field;
      if (!filter)
	flags |= unfiltered_output;
      cli_ui_out (stream, flags).vmessage (applied_style, format, args);
    }
  else
    {
      std::string str = string_vprintf (format, args);
      fputs_maybe_filtered (str.c_str (), stream, filter);
    }
}


void
vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
{
  vfprintf_maybe_filtered (stream, format, args, true, true);
}

void
vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
{
  if (debug_timestamp && stream == gdb_stdlog)
    {
      using namespace std::chrono;
      int len, need_nl;

      string_file sfile;
      cli_ui_out (&sfile, 0).vmessage (ui_file_style (), format, args);
      std::string linebuffer = std::move (sfile.string ());

      steady_clock::time_point now = steady_clock::now ();
      seconds s = duration_cast<seconds> (now.time_since_epoch ());
      microseconds us = duration_cast<microseconds> (now.time_since_epoch () - s);

      len = linebuffer.size ();
      need_nl = (len > 0 && linebuffer[len - 1] != '\n');

      std::string timestamp = string_printf ("%ld.%06ld %s%s",
					     (long) s.count (),
					     (long) us.count (),
					     linebuffer.c_str (),
					     need_nl ? "\n": "");
      fputs_unfiltered (timestamp.c_str (), stream);
    }
  else
    vfprintf_maybe_filtered (stream, format, args, false, true);
}

void
vprintf_filtered (const char *format, va_list args)
{
  vfprintf_maybe_filtered (gdb_stdout, format, args, true, false);
}

void
vprintf_unfiltered (const char *format, va_list args)
{
  vfprintf_unfiltered (gdb_stdout, format, args);
}

void
fprintf_filtered (struct ui_file *stream, const char *format, ...)
{
  va_list args;

  va_start (args, format);
  vfprintf_filtered (stream, format, args);
  va_end (args);
}

void
fprintf_unfiltered (struct ui_file *stream, const char *format, ...)
{
  va_list args;

  va_start (args, format);
  vfprintf_unfiltered (stream, format, args);
  va_end (args);
}

/* See utils.h.  */

void
fprintf_styled (struct ui_file *stream, const ui_file_style &style,
		const char *format, ...)
{
  va_list args;

  set_output_style (stream, style);
  va_start (args, format);
  vfprintf_filtered (stream, format, args);
  va_end (args);
  set_output_style (stream, ui_file_style ());
}

/* See utils.h.  */

void
vfprintf_styled (struct ui_file *stream, const ui_file_style &style,
		 const char *format, va_list args)
{
  set_output_style (stream, style);
  vfprintf_filtered (stream, format, args);
  set_output_style (stream, ui_file_style ());
}

/* See utils.h.  */

void
vfprintf_styled_no_gdbfmt (struct ui_file *stream, const ui_file_style &style,
			   bool filter, const char *format, va_list args)
{
  std::string str = string_vprintf (format, args);
  if (!str.empty ())
    {
      if (!style.is_default ())
	set_output_style (stream, style);
      fputs_maybe_filtered (str.c_str (), stream, filter);
      if (!style.is_default ())
	set_output_style (stream, ui_file_style ());
    }
}

void
printf_filtered (const char *format, ...)
{
  va_list args;

  va_start (args, format);
  vfprintf_filtered (gdb_stdout, format, args);
  va_end (args);
}


void
printf_unfiltered (const char *format, ...)
{
  va_list args;

  va_start (args, format);
  vfprintf_unfiltered (gdb_stdout, format, args);
  va_end (args);
}

/* Easy -- but watch out!

   This routine is *not* a replacement for puts()!  puts() appends a newline.
   This one doesn't, and had better not!  */

void
puts_filtered (const char *string)
{
  fputs_filtered (string, gdb_stdout);
}

void
puts_unfiltered (const char *string)
{
  fputs_unfiltered (string, gdb_stdout);
}

/* Return a pointer to N spaces and a null.  The pointer is good
   until the next call to here.  */
char *
n_spaces (int n)
{
  char *t;
  static char *spaces = 0;
  static int max_spaces = -1;

  if (n > max_spaces)
    {
      xfree (spaces);
      spaces = (char *) xmalloc (n + 1);
      for (t = spaces + n; t != spaces;)
	*--t = ' ';
      spaces[n] = '\0';
      max_spaces = n;
    }

  return spaces + max_spaces - n;
}

/* Print N spaces.  */
void
print_spaces_filtered (int n, struct ui_file *stream)
{
  fputs_filtered (n_spaces (n), stream);
}

/* C++/ObjC demangler stuff.  */

/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
   LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
   If the name is not mangled, or the language for the name is unknown, or
   demangling is off, the name is printed in its "raw" form.  */

void
fprintf_symbol_filtered (struct ui_file *stream, const char *name,
			 enum language lang, int arg_mode)
{
  char *demangled;

  if (name != NULL)
    {
      /* If user wants to see raw output, no problem.  */
      if (!demangle)
	{
	  fputs_filtered (name, stream);
	}
      else
	{
	  demangled = language_demangle (language_def (lang), name, arg_mode);
	  fputs_filtered (demangled ? demangled : name, stream);
	  if (demangled != NULL)
	    {
	      xfree (demangled);
	    }
	}
    }
}

/* True if CH is a character that can be part of a symbol name.  I.e.,
   either a number, a letter, or a '_'.  */

static bool
valid_identifier_name_char (int ch)
{
  return (ISALNUM (ch) || ch == '_');
}

/* Skip to end of token, or to END, whatever comes first.  Input is
   assumed to be a C++ operator name.  */

static const char *
cp_skip_operator_token (const char *token, const char *end)
{
  const char *p = token;
  while (p != end && !ISSPACE (*p) && *p != '(')
    {
      if (valid_identifier_name_char (*p))
	{
	  while (p != end && valid_identifier_name_char (*p))
	    p++;
	  return p;
	}
      else
	{
	  /* Note, ordered such that among ops that share a prefix,
	     longer comes first.  This is so that the loop below can
	     bail on first match.  */
	  static const char *ops[] =
	    {
	      "[",
	      "]",
	      "~",
	      ",",
	      "-=", "--", "->", "-",
	      "+=", "++", "+",
	      "*=", "*",
	      "/=", "/",
	      "%=", "%",
	      "|=", "||", "|",
	      "&=", "&&", "&",
	      "^=", "^",
	      "!=", "!",
	      "<<=", "<=", "<<", "<",
	      ">>=", ">=", ">>", ">",
	      "==", "=",
	    };

	  for (const char *op : ops)
	    {
	      size_t oplen = strlen (op);
	      size_t lencmp = std::min<size_t> (oplen, end - p);

	      if (strncmp (p, op, lencmp) == 0)
		return p + lencmp;
	    }
	  /* Some unidentified character.  Return it.  */
	  return p + 1;
	}
    }

  return p;
}

/* Advance STRING1/STRING2 past whitespace.  */

static void
skip_ws (const char *&string1, const char *&string2, const char *end_str2)
{
  while (ISSPACE (*string1))
    string1++;
  while (string2 < end_str2 && ISSPACE (*string2))
    string2++;
}

/* True if STRING points at the start of a C++ operator name.  START
   is the start of the string that STRING points to, hence when
   reading backwards, we must not read any character before START.  */

static bool
cp_is_operator (const char *string, const char *start)
{
  return ((string == start
	   || !valid_identifier_name_char (string[-1]))
	  && strncmp (string, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
	  && !valid_identifier_name_char (string[CP_OPERATOR_LEN]));
}

/* If *NAME points at an ABI tag, skip it and return true.  Otherwise
   leave *NAME unmodified and return false.  (see GCC's abi_tag
   attribute), such names are demangled as e.g.,
   "function[abi:cxx11]()".  */

static bool
skip_abi_tag (const char **name)
{
  const char *p = *name;

  if (startswith (p, "[abi:"))
    {
      p += 5;

      while (valid_identifier_name_char (*p))
	p++;

      if (*p == ']')
	{
	  p++;
	  *name = p;
	  return true;
	}
    }
  return false;
}

/* See utils.h.  */

int
strncmp_iw_with_mode (const char *string1, const char *string2,
		      size_t string2_len, strncmp_iw_mode mode,
		      enum language language,
		      completion_match_for_lcd *match_for_lcd)
{
  const char *string1_start = string1;
  const char *end_str2 = string2 + string2_len;
  bool skip_spaces = true;
  bool have_colon_op = (language == language_cplus
			|| language == language_rust
			|| language == language_fortran);

  while (1)
    {
      if (skip_spaces
	  || ((ISSPACE (*string1) && !valid_identifier_name_char (*string2))
	      || (ISSPACE (*string2) && !valid_identifier_name_char (*string1))))
	{
	  skip_ws (string1, string2, end_str2);
	  skip_spaces = false;
	}

      /* Skip [abi:cxx11] tags in the symbol name if the lookup name
	 doesn't include them.  E.g.:

	 string1: function[abi:cxx1](int)
	 string2: function

	 string1: function[abi:cxx1](int)
	 string2: function(int)

	 string1: Struct[abi:cxx1]::function()
	 string2: Struct::function()

	 string1: function(Struct[abi:cxx1], int)
	 string2: function(Struct, int)
      */
      if (string2 == end_str2
	  || (*string2 != '[' && !valid_identifier_name_char (*string2)))
	{
	  const char *abi_start = string1;

	  /* There can be more than one tag.  */
	  while (*string1 == '[' && skip_abi_tag (&string1))
	    ;

	  if (match_for_lcd != NULL && abi_start != string1)
	    match_for_lcd->mark_ignored_range (abi_start, string1);

	  while (ISSPACE (*string1))
	    string1++;
	}

      if (*string1 == '\0' || string2 == end_str2)
	break;

      /* Handle the :: operator.  */
      if (have_colon_op && string1[0] == ':' && string1[1] == ':')
	{
	  if (*string2 != ':')
	    return 1;

	  string1++;
	  string2++;

	  if (string2 == end_str2)
	    break;

	  if (*string2 != ':')
	    return 1;

	  string1++;
	  string2++;

	  while (ISSPACE (*string1))
	    string1++;
	  while (string2 < end_str2 && ISSPACE (*string2))
	    string2++;
	  continue;
	}

      /* Handle C++ user-defined operators.  */
      else if (language == language_cplus
	       && *string1 == 'o')
	{
	  if (cp_is_operator (string1, string1_start))
	    {
	      /* An operator name in STRING1.  Check STRING2.  */
	      size_t cmplen
		= std::min<size_t> (CP_OPERATOR_LEN, end_str2 - string2);
	      if (strncmp (string1, string2, cmplen) != 0)
		return 1;

	      string1 += cmplen;
	      string2 += cmplen;

	      if (string2 != end_str2)
		{
		  /* Check for "operatorX" in STRING2.  */
		  if (valid_identifier_name_char (*string2))
		    return 1;

		  skip_ws (string1, string2, end_str2);
		}

	      /* Handle operator().  */
	      if (*string1 == '(')
		{
		  if (string2 == end_str2)
		    {
		      if (mode == strncmp_iw_mode::NORMAL)
			return 0;
		      else
			{
			  /* Don't break for the regular return at the
			     bottom, because "operator" should not
			     match "operator()", since this open
			     parentheses is not the parameter list
			     start.  */
			  return *string1 != '\0';
			}
		    }

		  if (*string1 != *string2)
		    return 1;

		  string1++;
		  string2++;
		}

	      while (1)
		{
		  skip_ws (string1, string2, end_str2);

		  /* Skip to end of token, or to END, whatever comes
		     first.  */
		  const char *end_str1 = string1 + strlen (string1);
		  const char *p1 = cp_skip_operator_token (string1, end_str1);
		  const char *p2 = cp_skip_operator_token (string2, end_str2);

		  cmplen = std::min (p1 - string1, p2 - string2);
		  if (p2 == end_str2)
		    {
		      if (strncmp (string1, string2, cmplen) != 0)
			return 1;
		    }
		  else
		    {
		      if (p1 - string1 != p2 - string2)
			return 1;
		      if (strncmp (string1, string2, cmplen) != 0)
			return 1;
		    }

		  string1 += cmplen;
		  string2 += cmplen;

		  if (*string1 == '\0' || string2 == end_str2)
		    break;
		  if (*string1 == '(' || *string2 == '(')
		    break;
		}

	      continue;
	    }
	}

      if (case_sensitivity == case_sensitive_on && *string1 != *string2)
	break;
      if (case_sensitivity == case_sensitive_off
	  && (TOLOWER ((unsigned char) *string1)
	      != TOLOWER ((unsigned char) *string2)))
	break;

      /* If we see any non-whitespace, non-identifier-name character
	 (any of "()<>*&" etc.), then skip spaces the next time
	 around.  */
      if (!ISSPACE (*string1) && !valid_identifier_name_char (*string1))
	skip_spaces = true;

      string1++;
      string2++;
    }

  if (string2 == end_str2)
    {
      if (mode == strncmp_iw_mode::NORMAL)
	{
	  /* Strip abi tag markers from the matched symbol name.
	     Usually the ABI marker will be found on function name
	     (automatically added because the function returns an
	     object marked with an ABI tag).  However, it's also
	     possible to see a marker in one of the function
	     parameters, for example.

	     string2 (lookup name):
	       func
	     symbol name:
	       function(some_struct[abi:cxx11], int)

	     and for completion LCD computation we want to say that
	     the match was for:
	       function(some_struct, int)
	  */
	  if (match_for_lcd != NULL)
	    {
	      while ((string1 = strstr (string1, "[abi:")) != NULL)
		{
		  const char *abi_start = string1;

		  /* There can be more than one tag.  */
		  while (skip_abi_tag (&string1) && *string1 == '[')
		    ;

		  if (abi_start != string1)
		    match_for_lcd->mark_ignored_range (abi_start, string1);
		}
	    }

	  return 0;
	}
      else
	return (*string1 != '\0' && *string1 != '(');
    }
  else
    return 1;
}

/* See utils.h.  */

int
strncmp_iw (const char *string1, const char *string2, size_t string2_len)
{
  return strncmp_iw_with_mode (string1, string2, string2_len,
			       strncmp_iw_mode::NORMAL, language_minimal);
}

/* See utils.h.  */

int
strcmp_iw (const char *string1, const char *string2)
{
  return strncmp_iw_with_mode (string1, string2, strlen (string2),
			       strncmp_iw_mode::MATCH_PARAMS, language_minimal);
}

/* This is like strcmp except that it ignores whitespace and treats
   '(' as the first non-NULL character in terms of ordering.  Like
   strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
   STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
   according to that ordering.

   If a list is sorted according to this function and if you want to
   find names in the list that match some fixed NAME according to
   strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
   where this function would put NAME.

   This function must be neutral to the CASE_SENSITIVITY setting as the user
   may choose it during later lookup.  Therefore this function always sorts
   primarily case-insensitively and secondarily case-sensitively.

   Here are some examples of why using strcmp to sort is a bad idea:

   Whitespace example:

   Say your partial symtab contains: "foo<char *>", "goo".  Then, if
   we try to do a search for "foo<char*>", strcmp will locate this
   after "foo<char *>" and before "goo".  Then lookup_partial_symbol
   will start looking at strings beginning with "goo", and will never
   see the correct match of "foo<char *>".

   Parenthesis example:

   In practice, this is less like to be an issue, but I'll give it a
   shot.  Let's assume that '$' is a legitimate character to occur in
   symbols.  (Which may well even be the case on some systems.)  Then
   say that the partial symbol table contains "foo$" and "foo(int)".
   strcmp will put them in this order, since '$' < '('.  Now, if the
   user searches for "foo", then strcmp will sort "foo" before "foo$".
   Then lookup_partial_symbol will notice that strcmp_iw("foo$",
   "foo") is false, so it won't proceed to the actual match of
   "foo(int)" with "foo".  */

int
strcmp_iw_ordered (const char *string1, const char *string2)
{
  const char *saved_string1 = string1, *saved_string2 = string2;
  enum case_sensitivity case_pass = case_sensitive_off;

  for (;;)
    {
      /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'.
	 Provide stub characters if we are already at the end of one of the
	 strings.  */
      char c1 = 'X', c2 = 'X';

      while (*string1 != '\0' && *string2 != '\0')
	{
	  while (ISSPACE (*string1))
	    string1++;
	  while (ISSPACE (*string2))
	    string2++;

	  switch (case_pass)
	  {
	    case case_sensitive_off:
	      c1 = TOLOWER ((unsigned char) *string1);
	      c2 = TOLOWER ((unsigned char) *string2);
	      break;
	    case case_sensitive_on:
	      c1 = *string1;
	      c2 = *string2;
	      break;
	  }
	  if (c1 != c2)
	    break;

	  if (*string1 != '\0')
	    {
	      string1++;
	      string2++;
	    }
	}

      switch (*string1)
	{
	  /* Characters are non-equal unless they're both '\0'; we want to
	     make sure we get the comparison right according to our
	     comparison in the cases where one of them is '\0' or '('.  */
	case '\0':
	  if (*string2 == '\0')
	    break;
	  else
	    return -1;
	case '(':
	  if (*string2 == '\0')
	    return 1;
	  else
	    return -1;
	default:
	  if (*string2 == '\0' || *string2 == '(')
	    return 1;
	  else if (c1 > c2)
	    return 1;
	  else if (c1 < c2)
	    return -1;
	  /* PASSTHRU */
	}

      if (case_pass == case_sensitive_on)
	return 0;
      
      /* Otherwise the strings were equal in case insensitive way, make
	 a more fine grained comparison in a case sensitive way.  */

      case_pass = case_sensitive_on;
      string1 = saved_string1;
      string2 = saved_string2;
    }
}

/* See utils.h.  */

bool
streq (const char *lhs, const char *rhs)
{
  return !strcmp (lhs, rhs);
}

/* See utils.h.  */

int
streq_hash (const void *lhs, const void *rhs)
{
  return streq ((const char *) lhs, (const char *) rhs);
}



/*
   ** subset_compare()
   **    Answer whether string_to_compare is a full or partial match to
   **    template_string.  The partial match must be in sequence starting
   **    at index 0.
 */
int
subset_compare (const char *string_to_compare, const char *template_string)
{
  int match;

  if (template_string != NULL && string_to_compare != NULL
      && strlen (string_to_compare) <= strlen (template_string))
    match =
      (startswith (template_string, string_to_compare));
  else
    match = 0;
  return match;
}

static void
show_debug_timestamp (struct ui_file *file, int from_tty,
		      struct cmd_list_element *c, const char *value)
{
  fprintf_filtered (file, _("Timestamping debugging messages is %s.\n"),
		    value);
}


/* See utils.h.  */

CORE_ADDR
address_significant (gdbarch *gdbarch, CORE_ADDR addr)
{
  /* Clear insignificant bits of a target address and sign extend resulting
     address, avoiding shifts larger or equal than the width of a CORE_ADDR.
     The local variable ADDR_BIT stops the compiler reporting a shift overflow
     when it won't occur.  Skip updating of target address if current target
     has not set gdbarch significant_addr_bit.  */
  int addr_bit = gdbarch_significant_addr_bit (gdbarch);

  if (addr_bit && (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)))
    {
      CORE_ADDR sign = (CORE_ADDR) 1 << (addr_bit - 1);
      addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
      addr = (addr ^ sign) - sign;
    }

  return addr;
}

const char *
paddress (struct gdbarch *gdbarch, CORE_ADDR addr)
{
  /* Truncate address to the size of a target address, avoiding shifts
     larger or equal than the width of a CORE_ADDR.  The local
     variable ADDR_BIT stops the compiler reporting a shift overflow
     when it won't occur.  */
  /* NOTE: This assumes that the significant address information is
     kept in the least significant bits of ADDR - the upper bits were
     either zero or sign extended.  Should gdbarch_address_to_pointer or
     some ADDRESS_TO_PRINTABLE() be used to do the conversion?  */

  int addr_bit = gdbarch_addr_bit (gdbarch);

  if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
    addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
  return hex_string (addr);
}

/* This function is described in "defs.h".  */

const char *
print_core_address (struct gdbarch *gdbarch, CORE_ADDR address)
{
  int addr_bit = gdbarch_addr_bit (gdbarch);

  if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
    address &= ((CORE_ADDR) 1 << addr_bit) - 1;

  /* FIXME: cagney/2002-05-03: Need local_address_string() function
     that returns the language localized string formatted to a width
     based on gdbarch_addr_bit.  */
  if (addr_bit <= 32)
    return hex_string_custom (address, 8);
  else
    return hex_string_custom (address, 16);
}

/* Callback hash_f for htab_create_alloc or htab_create_alloc_ex.  */

hashval_t
core_addr_hash (const void *ap)
{
  const CORE_ADDR *addrp = (const CORE_ADDR *) ap;

  return *addrp;
}

/* Callback eq_f for htab_create_alloc or htab_create_alloc_ex.  */

int
core_addr_eq (const void *ap, const void *bp)
{
  const CORE_ADDR *addr_ap = (const CORE_ADDR *) ap;
  const CORE_ADDR *addr_bp = (const CORE_ADDR *) bp;

  return *addr_ap == *addr_bp;
}

/* Convert a string back into a CORE_ADDR.  */
CORE_ADDR
string_to_core_addr (const char *my_string)
{
  CORE_ADDR addr = 0;

  if (my_string[0] == '0' && TOLOWER (my_string[1]) == 'x')
    {
      /* Assume that it is in hex.  */
      int i;

      for (i = 2; my_string[i] != '\0'; i++)
	{
	  if (ISDIGIT (my_string[i]))
	    addr = (my_string[i] - '0') + (addr * 16);
	  else if (ISXDIGIT (my_string[i]))
	    addr = (TOLOWER (my_string[i]) - 'a' + 0xa) + (addr * 16);
	  else
	    error (_("invalid hex \"%s\""), my_string);
	}
    }
  else
    {
      /* Assume that it is in decimal.  */
      int i;

      for (i = 0; my_string[i] != '\0'; i++)
	{
	  if (ISDIGIT (my_string[i]))
	    addr = (my_string[i] - '0') + (addr * 10);
	  else
	    error (_("invalid decimal \"%s\""), my_string);
	}
    }

  return addr;
}

#if GDB_SELF_TEST

static void
gdb_realpath_check_trailer (const char *input, const char *trailer)
{
  gdb::unique_xmalloc_ptr<char> result = gdb_realpath (input);

  size_t len = strlen (result.get ());
  size_t trail_len = strlen (trailer);

  SELF_CHECK (len >= trail_len
	      && strcmp (result.get () + len - trail_len, trailer) == 0);
}

static void
gdb_realpath_tests ()
{
  /* A file which contains a directory prefix.  */
  gdb_realpath_check_trailer ("./xfullpath.exp", "/xfullpath.exp");
  /* A file which contains a directory prefix.  */
  gdb_realpath_check_trailer ("../../defs.h", "/defs.h");
  /* A one-character filename.  */
  gdb_realpath_check_trailer ("./a", "/a");
  /* A file in the root directory.  */
  gdb_realpath_check_trailer ("/root_file_which_should_exist",
			      "/root_file_which_should_exist");
  /* A file which does not have a directory prefix.  */
  gdb_realpath_check_trailer ("xfullpath.exp", "xfullpath.exp");
  /* A one-char filename without any directory prefix.  */
  gdb_realpath_check_trailer ("a", "a");
  /* An empty filename.  */
  gdb_realpath_check_trailer ("", "");
}

/* Test the gdb_argv::as_array_view method.  */

static void
gdb_argv_as_array_view_test ()
{
  {
    gdb_argv argv;

    gdb::array_view<char *> view = argv.as_array_view ();

    SELF_CHECK (view.data () == nullptr);
    SELF_CHECK (view.size () == 0);
  }
  {
    gdb_argv argv ("une bonne 50");

    gdb::array_view<char *> view = argv.as_array_view ();

    SELF_CHECK (view.size () == 3);
    SELF_CHECK (strcmp (view[0], "une") == 0);
    SELF_CHECK (strcmp (view[1], "bonne") == 0);
    SELF_CHECK (strcmp (view[2], "50") == 0);
  }
}

#endif /* GDB_SELF_TEST */

/* Allocation function for the libiberty hash table which uses an
   obstack.  The obstack is passed as DATA.  */

void *
hashtab_obstack_allocate (void *data, size_t size, size_t count)
{
  size_t total = size * count;
  void *ptr = obstack_alloc ((struct obstack *) data, total);

  memset (ptr, 0, total);
  return ptr;
}

/* Trivial deallocation function for the libiberty splay tree and hash
   table - don't deallocate anything.  Rely on later deletion of the
   obstack.  DATA will be the obstack, although it is not needed
   here.  */

void
dummy_obstack_deallocate (void *object, void *data)
{
  return;
}

/* Simple, portable version of dirname that does not modify its
   argument.  */

std::string
ldirname (const char *filename)
{
  std::string dirname;
  const char *base = lbasename (filename);

  while (base > filename && IS_DIR_SEPARATOR (base[-1]))
    --base;

  if (base == filename)
    return dirname;

  dirname = std::string (filename, base - filename);

  /* On DOS based file systems, convert "d:foo" to "d:.", so that we
     create "d:./bar" later instead of the (different) "d:/bar".  */
  if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
      && !IS_DIR_SEPARATOR (filename[0]))
    dirname[base++ - filename] = '.';

  return dirname;
}

/* See utils.h.  */

void
gdb_argv::reset (const char *s)
{
  char **argv = buildargv (s);

  freeargv (m_argv);
  m_argv = argv;
}

#define AMBIGUOUS_MESS1	".\nMatching formats:"
#define AMBIGUOUS_MESS2	\
  ".\nUse \"set gnutarget format-name\" to specify the format."

std::string
gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
{
  char **p;

  /* Check if errmsg just need simple return.  */
  if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
    return bfd_errmsg (error_tag);

  std::string ret (bfd_errmsg (error_tag));
  ret += AMBIGUOUS_MESS1;

  for (p = matching; *p; p++)
    {
      ret += " ";
      ret += *p;
    }
  ret += AMBIGUOUS_MESS2;

  xfree (matching);

  return ret;
}

/* Return ARGS parsed as a valid pid, or throw an error.  */

int
parse_pid_to_attach (const char *args)
{
  unsigned long pid;
  char *dummy;

  if (!args)
    error_no_arg (_("process-id to attach"));

  dummy = (char *) args;
  pid = strtoul (args, &dummy, 0);
  /* Some targets don't set errno on errors, grrr!  */
  if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
    error (_("Illegal process-id: %s."), args);

  return pid;
}

/* Substitute all occurrences of string FROM by string TO in *STRINGP.  *STRINGP
   must come from xrealloc-compatible allocator and it may be updated.  FROM
   needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
   located at the start or end of *STRINGP.  */

void
substitute_path_component (char **stringp, const char *from, const char *to)
{
  char *string = *stringp, *s;
  const size_t from_len = strlen (from);
  const size_t to_len = strlen (to);

  for (s = string;;)
    {
      s = strstr (s, from);
      if (s == NULL)
	break;

      if ((s == string || IS_DIR_SEPARATOR (s[-1])
	   || s[-1] == DIRNAME_SEPARATOR)
	  && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
	      || s[from_len] == DIRNAME_SEPARATOR))
	{
	  char *string_new;

	  string_new
	    = (char *) xrealloc (string, (strlen (string) + to_len + 1));

	  /* Relocate the current S pointer.  */
	  s = s - string + string_new;
	  string = string_new;

	  /* Replace from by to.  */
	  memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
	  memcpy (s, to, to_len);

	  s += to_len;
	}
      else
	s++;
    }

  *stringp = string;
}

#ifdef HAVE_WAITPID

#ifdef SIGALRM

/* SIGALRM handler for waitpid_with_timeout.  */

static void
sigalrm_handler (int signo)
{
  /* Nothing to do.  */
}

#endif

/* Wrapper to wait for child PID to die with TIMEOUT.
   TIMEOUT is the time to stop waiting in seconds.
   If TIMEOUT is zero, pass WNOHANG to waitpid.
   Returns PID if it was successfully waited for, otherwise -1.

   Timeouts are currently implemented with alarm and SIGALRM.
   If the host does not support them, this waits "forever".
   It would be odd though for a host to have waitpid and not SIGALRM.  */

pid_t
wait_to_die_with_timeout (pid_t pid, int *status, int timeout)
{
  pid_t waitpid_result;

  gdb_assert (pid > 0);
  gdb_assert (timeout >= 0);

  if (timeout > 0)
    {
#ifdef SIGALRM
#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
      struct sigaction sa, old_sa;

      sa.sa_handler = sigalrm_handler;
      sigemptyset (&sa.sa_mask);
      sa.sa_flags = 0;
      sigaction (SIGALRM, &sa, &old_sa);
#else
      sighandler_t ofunc;

      ofunc = signal (SIGALRM, sigalrm_handler);
#endif

      alarm (timeout);
#endif

      waitpid_result = waitpid (pid, status, 0);

#ifdef SIGALRM
      alarm (0);
#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
      sigaction (SIGALRM, &old_sa, NULL);
#else
      signal (SIGALRM, ofunc);
#endif
#endif
    }
  else
    waitpid_result = waitpid (pid, status, WNOHANG);

  if (waitpid_result == pid)
    return pid;
  else
    return -1;
}

#endif /* HAVE_WAITPID */

/* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files.
   Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS.

   It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and
   HAVE_CASE_INSENSITIVE_FILE_SYSTEM.  */

int
gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
{
  gdb_assert ((flags & FNM_FILE_NAME) != 0);

  /* It is unclear how '\' escaping vs. directory separator should coexist.  */
  gdb_assert ((flags & FNM_NOESCAPE) != 0);

#ifdef HAVE_DOS_BASED_FILE_SYSTEM
  {
    char *pattern_slash, *string_slash;

    /* Replace '\' by '/' in both strings.  */

    pattern_slash = (char *) alloca (strlen (pattern) + 1);
    strcpy (pattern_slash, pattern);
    pattern = pattern_slash;
    for (; *pattern_slash != 0; pattern_slash++)
      if (IS_DIR_SEPARATOR (*pattern_slash))
	*pattern_slash = '/';

    string_slash = (char *) alloca (strlen (string) + 1);
    strcpy (string_slash, string);
    string = string_slash;
    for (; *string_slash != 0; string_slash++)
      if (IS_DIR_SEPARATOR (*string_slash))
	*string_slash = '/';
  }
#endif /* HAVE_DOS_BASED_FILE_SYSTEM */

#ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
  flags |= FNM_CASEFOLD;
#endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */

  return fnmatch (pattern, string, flags);
}

/* Return the number of path elements in PATH.
   / = 1
   /foo = 2
   /foo/ = 2
   foo/bar = 2
   foo/ = 1  */

int
count_path_elements (const char *path)
{
  int count = 0;
  const char *p = path;

  if (HAS_DRIVE_SPEC (p))
    {
      p = STRIP_DRIVE_SPEC (p);
      ++count;
    }

  while (*p != '\0')
    {
      if (IS_DIR_SEPARATOR (*p))
	++count;
      ++p;
    }

  /* Backup one if last character is /, unless it's the only one.  */
  if (p > path + 1 && IS_DIR_SEPARATOR (p[-1]))
    --count;

  /* Add one for the file name, if present.  */
  if (p > path && !IS_DIR_SEPARATOR (p[-1]))
    ++count;

  return count;
}

/* Remove N leading path elements from PATH.
   N must be non-negative.
   If PATH has more than N path elements then return NULL.
   If PATH has exactly N path elements then return "".
   See count_path_elements for a description of how we do the counting.  */

const char *
strip_leading_path_elements (const char *path, int n)
{
  int i = 0;
  const char *p = path;

  gdb_assert (n >= 0);

  if (n == 0)
    return p;

  if (HAS_DRIVE_SPEC (p))
    {
      p = STRIP_DRIVE_SPEC (p);
      ++i;
    }

  while (i < n)
    {
      while (*p != '\0' && !IS_DIR_SEPARATOR (*p))
	++p;
      if (*p == '\0')
	{
	  if (i + 1 == n)
	    return "";
	  return NULL;
	}
      ++p;
      ++i;
    }

  return p;
}

/* See utils.h.  */

void
copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
	      const gdb_byte *source, ULONGEST source_offset,
	      ULONGEST nbits, int bits_big_endian)
{
  unsigned int buf, avail;

  if (nbits == 0)
    return;

  if (bits_big_endian)
    {
      /* Start from the end, then work backwards.  */
      dest_offset += nbits - 1;
      dest += dest_offset / 8;
      dest_offset = 7 - dest_offset % 8;
      source_offset += nbits - 1;
      source += source_offset / 8;
      source_offset = 7 - source_offset % 8;
    }
  else
    {
      dest += dest_offset / 8;
      dest_offset %= 8;
      source += source_offset / 8;
      source_offset %= 8;
    }

  /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
     SOURCE_OFFSET bits from the source.  */
  buf = *(bits_big_endian ? source-- : source++) >> source_offset;
  buf <<= dest_offset;
  buf |= *dest & ((1 << dest_offset) - 1);

  /* NBITS: bits yet to be written; AVAIL: BUF's fill level.  */
  nbits += dest_offset;
  avail = dest_offset + 8 - source_offset;

  /* Flush 8 bits from BUF, if appropriate.  */
  if (nbits >= 8 && avail >= 8)
    {
      *(bits_big_endian ? dest-- : dest++) = buf;
      buf >>= 8;
      avail -= 8;
      nbits -= 8;
    }

  /* Copy the middle part.  */
  if (nbits >= 8)
    {
      size_t len = nbits / 8;

      /* Use a faster method for byte-aligned copies.  */
      if (avail == 0)
	{
	  if (bits_big_endian)
	    {
	      dest -= len;
	      source -= len;
	      memcpy (dest + 1, source + 1, len);
	    }
	  else
	    {
	      memcpy (dest, source, len);
	      dest += len;
	      source += len;
	    }
	}
      else
	{
	  while (len--)
	    {
	      buf |= *(bits_big_endian ? source-- : source++) << avail;
	      *(bits_big_endian ? dest-- : dest++) = buf;
	      buf >>= 8;
	    }
	}
      nbits %= 8;
    }

  /* Write the last byte.  */
  if (nbits)
    {
      if (avail < nbits)
	buf |= *source << avail;

      buf &= (1 << nbits) - 1;
      *dest = (*dest & (~0U << nbits)) | buf;
    }
}

void _initialize_utils ();
void
_initialize_utils ()
{
  add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\
Set number of characters where GDB should wrap lines of its output."), _("\
Show number of characters where GDB should wrap lines of its output."), _("\
This affects where GDB wraps its output to fit the screen width.\n\
Setting this to \"unlimited\" or zero prevents GDB from wrapping its output."),
			    set_width_command,
			    show_chars_per_line,
			    &setlist, &showlist);

  add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\
Set number of lines in a page for GDB output pagination."), _("\
Show number of lines in a page for GDB output pagination."), _("\
This affects the number of lines after which GDB will pause\n\
its output and ask you whether to continue.\n\
Setting this to \"unlimited\" or zero causes GDB never pause during output."),
			    set_height_command,
			    show_lines_per_page,
			    &setlist, &showlist);

  add_setshow_boolean_cmd ("pagination", class_support,
			   &pagination_enabled, _("\
Set state of GDB output pagination."), _("\
Show state of GDB output pagination."), _("\
When pagination is ON, GDB pauses at end of each screenful of\n\
its output and asks you whether to continue.\n\
Turning pagination off is an alternative to \"set height unlimited\"."),
			   NULL,
			   show_pagination_enabled,
			   &setlist, &showlist);

  add_setshow_boolean_cmd ("sevenbit-strings", class_support,
			   &sevenbit_strings, _("\
Set printing of 8-bit characters in strings as \\nnn."), _("\
Show printing of 8-bit characters in strings as \\nnn."), NULL,
			   NULL,
			   show_sevenbit_strings,
			   &setprintlist, &showprintlist);

  add_setshow_boolean_cmd ("timestamp", class_maintenance,
			    &debug_timestamp, _("\
Set timestamping of debugging messages."), _("\
Show timestamping of debugging messages."), _("\
When set, debugging messages will be marked with seconds and microseconds."),
			   NULL,
			   show_debug_timestamp,
			   &setdebuglist, &showdebuglist);

  add_internal_problem_command (&internal_error_problem);
  add_internal_problem_command (&internal_warning_problem);
  add_internal_problem_command (&demangler_warning_problem);

#if GDB_SELF_TEST
  selftests::register_test ("gdb_realpath", gdb_realpath_tests);
  selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test);
#endif
}