aboutsummaryrefslogtreecommitdiff
path: root/src/history.c
blob: 44fe48acc1ad3a2471ab518b558a27f2b91772bb (plain) (blame)
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
/*
 * *****************************************************************************
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * *****************************************************************************
 *
 * Adapted from the following:
 *
 * linenoise.c -- guerrilla line editing library against the idea that a
 * line editing lib needs to be 20,000 lines of C code.
 *
 * You can find the original source code at:
 *   http://github.com/antirez/linenoise
 *
 * You can find the fork that this code is based on at:
 *   https://github.com/rain-1/linenoise-mob
 *
 * ------------------------------------------------------------------------
 *
 * This code is also under the following license:
 *
 * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *  *  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  *  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ------------------------------------------------------------------------
 *
 * Does a number of crazy assumptions that happen to be true in 99.9999% of
 * the 2010 UNIX computers around.
 *
 * References:
 * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
 * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
 *
 * Todo list:
 * - Filter bogus Ctrl+<char> combinations.
 * - Win32 support
 *
 * Bloat:
 * - History search like Ctrl+r in readline?
 *
 * List of escape sequences used by this program, we do everything just
 * with three sequences. In order to be so cheap we may have some
 * flickering effect with some slow terminal, but the lesser sequences
 * the more compatible.
 *
 * EL (Erase Line)
 *    Sequence: ESC [ n K
 *    Effect: if n is 0 or missing, clear from cursor to end of line
 *    Effect: if n is 1, clear from beginning of line to cursor
 *    Effect: if n is 2, clear entire line
 *
 * CUF (CUrsor Forward)
 *    Sequence: ESC [ n C
 *    Effect: moves cursor forward n chars
 *
 * CUB (CUrsor Backward)
 *    Sequence: ESC [ n D
 *    Effect: moves cursor backward n chars
 *
 * The following is used to get the terminal width if getting
 * the width with the TIOCGWINSZ ioctl fails
 *
 * DSR (Device Status Report)
 *    Sequence: ESC [ 6 n
 *    Effect: reports the current cusor position as ESC [ n ; m R
 *            where n is the row and m is the column
 *
 * When multi line mode is enabled, we also use two additional escape
 * sequences. However multi line editing is disabled by default.
 *
 * CUU (CUrsor Up)
 *    Sequence: ESC [ n A
 *    Effect: moves cursor up of n chars.
 *
 * CUD (CUrsor Down)
 *    Sequence: ESC [ n B
 *    Effect: moves cursor down of n chars.
 *
 * When bc_history_clearScreen() is called, two additional escape sequences
 * are used in order to clear the screen and position the cursor at home
 * position.
 *
 * CUP (CUrsor Position)
 *    Sequence: ESC [ H
 *    Effect: moves the cursor to upper left corner
 *
 * ED (Erase Display)
 *    Sequence: ESC [ 2 J
 *    Effect: clear the whole screen
 *
 * *****************************************************************************
 *
 * Code for line history.
 *
 */

#if BC_ENABLE_HISTORY

#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>

#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifndef _WIN32
#include <strings.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#endif // _WIN32

#include <status.h>
#include <vector.h>
#include <history.h>
#include <read.h>
#include <file.h>
#include <vm.h>

#if BC_DEBUG_CODE

/// A file for outputting to when debugging.
BcFile bc_history_debug_fp;

/// A buffer for the above file.
char *bc_history_debug_buf;

#endif // BC_DEBUG_CODE

/**
 * Checks if the code is a wide character.
 * @param cp  The codepoint to check.
 * @return    True if @a cp is a wide character, false otherwise.
 */
static bool bc_history_wchar(uint32_t cp) {

	size_t i;

	for (i = 0; i < bc_history_wchars_len; ++i) {

		// Ranges are listed in ascending order.  Therefore, once the
		// whole range is higher than the codepoint we're testing, the
		// codepoint won't be found in any remaining range => bail early.
		if (bc_history_wchars[i][0] > cp) return false;

		// Test this range.
		if (bc_history_wchars[i][0] <= cp && cp <= bc_history_wchars[i][1])
			return true;
	}

	return false;
}

/**
 * Checks if the code is a combining character.
 * @param cp  The codepoint to check.
 * @return    True if @a cp is a combining character, false otherwise.
 */
static bool bc_history_comboChar(uint32_t cp) {

	size_t i;

	for (i = 0; i < bc_history_combo_chars_len; ++i) {

		// Combining chars are listed in ascending order, so once we pass
		// the codepoint of interest, we know it's not a combining char.
		if (bc_history_combo_chars[i] > cp) return false;
		if (bc_history_combo_chars[i] == cp) return true;
	}

	return false;
}

/**
 * Gets the length of previous UTF8 character.
 * @param buf  The buffer of characters.
 * @param pos  The index into the buffer.
 */
static size_t bc_history_prevCharLen(const char *buf, size_t pos) {
	size_t end = pos;
	for (pos -= 1; pos < end && (buf[pos] & 0xC0) == 0x80; --pos);
	return end - (pos >= end ? 0 : pos);
}

/**
 * Converts UTF-8 to a Unicode code point.
 * @param s    The string.
 * @param len  The length of the string.
 * @param cp   An out parameter for the codepoint.
 * @return     The number of bytes eaten by the codepoint.
 */
static size_t bc_history_codePoint(const char *s, size_t len, uint32_t *cp) {

	if (len) {

		uchar byte = (uchar) s[0];

		// This is literally the UTF-8 decoding algorithm. Look that up if you
		// don't understand this.

		if ((byte & 0x80) == 0) {
			*cp = byte;
			return 1;
		}
		else if ((byte & 0xE0) == 0xC0) {

			if (len >= 2) {
				*cp = (((uint32_t) (s[0] & 0x1F)) << 6) |
					   ((uint32_t) (s[1] & 0x3F));
				return 2;
			}
		}
		else if ((byte & 0xF0) == 0xE0) {

			if (len >= 3) {
				*cp = (((uint32_t) (s[0] & 0x0F)) << 12) |
					  (((uint32_t) (s[1] & 0x3F)) << 6) |
					   ((uint32_t) (s[2] & 0x3F));
				return 3;
			}
		}
		else if ((byte & 0xF8) == 0xF0) {

			if (len >= 4) {
				*cp = (((uint32_t) (s[0] & 0x07)) << 18) |
					  (((uint32_t) (s[1] & 0x3F)) << 12) |
					  (((uint32_t) (s[2] & 0x3F)) << 6) |
					   ((uint32_t) (s[3] & 0x3F));
				return 4;
			}
		}
		else {
			*cp = 0xFFFD;
			return 1;
		}
	}

	*cp = 0;

	return 1;
}

/**
 * Gets the length of next grapheme.
 * @param buf      The buffer.
 * @param buf_len  The length of the buffer.
 * @param pos      The index into the buffer.
 * @param col_len  An out parameter for the length of the grapheme on screen.
 * @return         The number of bytes in the grapheme.
 */
static size_t bc_history_nextLen(const char *buf, size_t buf_len,
                                 size_t pos, size_t *col_len)
{
	uint32_t cp;
	size_t beg = pos;
	size_t len = bc_history_codePoint(buf + pos, buf_len - pos, &cp);

	if (bc_history_comboChar(cp)) {

		BC_UNREACHABLE

		if (col_len != NULL) *col_len = 0;

		return 0;
	}

	// Store the width of the character on screen.
	if (col_len != NULL) *col_len = bc_history_wchar(cp) ? 2 : 1;

	pos += len;

	// Find the first non-combining character.
	while (pos < buf_len) {

		len = bc_history_codePoint(buf + pos, buf_len - pos, &cp);

		if (!bc_history_comboChar(cp)) return pos - beg;

		pos += len;
	}

	return pos - beg;
}

/**
 * Gets the length of previous grapheme.
 * @param buf  The buffer.
 * @param pos  The index into the buffer.
 * @return     The number of bytes in the grapheme.
 */
static size_t bc_history_prevLen(const char *buf, size_t pos) {

	size_t end = pos;

	// Find the first non-combining character.
	while (pos > 0) {

		uint32_t cp;
		size_t len = bc_history_prevCharLen(buf, pos);

		pos -= len;
		bc_history_codePoint(buf + pos, len, &cp);

		// The original linenoise-mob had an extra parameter col_len, like
		// bc_history_nextLen(), which, if not NULL, was set in this if
		// statement. However, we always passed NULL, so just skip that.
		if (!bc_history_comboChar(cp)) return end - pos;
	}

	BC_UNREACHABLE

	return 0;
}

/**
 * Reads @a n characters from stdin.
 * @param buf  The buffer to read into. The caller is responsible for making
 *             sure this is big enough for @a n.
 * @param n    The number of characters to read.
 * @return     The number of characters read or less than 0 on error.
 */
static ssize_t bc_history_read(char *buf, size_t n) {

	ssize_t ret;

	BC_SIG_LOCK;

#ifndef _WIN32

	do {
		// We don't care about being interrupted.
		ret = read(STDIN_FILENO, buf, n);
	} while (ret == EINTR);

#else // _WIN32

	bool good;
	DWORD read;
	HANDLE hn = GetStdHandle(STD_INPUT_HANDLE);

	good = ReadConsole(hn, buf, (DWORD) n, &read, NULL);

	ret = (read != n) ? -1 : 1;

#endif // _WIN32

	BC_SIG_UNLOCK;

	return ret;
}

/**
 * Reads a Unicode code point into a buffer.
 * @param buf      The buffer to read into.
 * @param buf_len  The length of the buffer.
 * @param cp       An out parameter for the codepoint.
 * @param nread    An out parameter for the number of bytes read.
 * @return         BC_STATUS_EOF or BC_STATUS_SUCCESS.
 */
static BcStatus bc_history_readCode(char *buf, size_t buf_len,
                                    uint32_t *cp, size_t *nread)
{
	ssize_t n;

	assert(buf_len >= 1);

	// Read a byte.
	n = bc_history_read(buf, 1);
	if (BC_ERR(n <= 0)) goto err;

	// Get the byte.
	uchar byte = ((uchar*) buf)[0];

	// Once again, this is the UTF-8 decoding algorithm, but it has reads
	// instead of actual decoding.
	if ((byte & 0x80) != 0) {

		if ((byte & 0xE0) == 0xC0) {

			assert(buf_len >= 2);

			n = bc_history_read(buf + 1, 1);

			if (BC_ERR(n <= 0)) goto err;
		}
		else if ((byte & 0xF0) == 0xE0) {

			assert(buf_len >= 3);

			n = bc_history_read(buf + 1, 2);

			if (BC_ERR(n <= 0)) goto err;
		}
		else if ((byte & 0xF8) == 0xF0) {

			assert(buf_len >= 3);

			n = bc_history_read(buf + 1, 3);

			if (BC_ERR(n <= 0)) goto err;
		}
		else {
			n = -1;
			goto err;
		}
	}

	// Convert to the codepoint.
	*nread = bc_history_codePoint(buf, buf_len, cp);

	return BC_STATUS_SUCCESS;

err:
	// If we get here, we either had a fatal error of EOF.
	if (BC_ERR(n < 0)) bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);
	else *nread = (size_t) n;
	return BC_STATUS_EOF;
}

/**
 * Gets the column length from beginning of buffer to current byte position.
 * @param buf      The buffer.
 * @param buf_len  The length of the buffer.
 * @param pos      The index into the buffer.
 * @return         The number of columns between the beginning of @a buffer to
 *                 @a pos.
 */
static size_t bc_history_colPos(const char *buf, size_t buf_len, size_t pos) {

	size_t ret = 0, off = 0;

	// While we haven't reached the offset, get the length of the next grapheme.
	while (off < pos && off < buf_len) {

		size_t col_len, len;

		len = bc_history_nextLen(buf, buf_len, off, &col_len);

		off += len;
		ret += col_len;
	}

	return ret;
}

/**
 * Returns true if the terminal name is in the list of terminals we know are
 * not able to understand basic escape sequences.
 * @return  True if the terminal is a bad terminal.
 */
static inline bool bc_history_isBadTerm(void) {

	size_t i;
	bool ret = false;
	char *term = bc_vm_getenv("TERM");

	if (term == NULL) return false;

	for (i = 0; !ret && bc_history_bad_terms[i]; ++i)
		ret = (!strcasecmp(term, bc_history_bad_terms[i]));

	bc_vm_getenvFree(term);

	return ret;
}

/**
 * Enables raw mode (1960's black magic).
 * @param h  The history data.
 */
static void bc_history_enableRaw(BcHistory *h) {

	// I don't do anything for Windows because in Windows, you set their
	// equivalent of raw mode and leave it, so I do it in bc_history_init().

#ifndef _WIN32
	struct termios raw;
	int err;

	assert(BC_TTYIN);

	if (h->rawMode) return;

	BC_SIG_LOCK;

	if (BC_ERR(tcgetattr(STDIN_FILENO, &h->orig_termios) == -1))
		bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);

	BC_SIG_UNLOCK;

	// Modify the original mode.
	raw = h->orig_termios;

	// Input modes: no break, no CR to NL, no parity check, no strip char,
	// no start/stop output control.
	raw.c_iflag &= (unsigned int) (~(BRKINT | ICRNL | INPCK | ISTRIP | IXON));

	// Control modes: set 8 bit chars.
	raw.c_cflag |= (CS8);

	// Local modes - choing off, canonical off, no extended functions,
	// no signal chars (^Z,^C).
	raw.c_lflag &= (unsigned int) (~(ECHO | ICANON | IEXTEN | ISIG));

	// Control chars - set return condition: min number of bytes and timer.
	// We want read to give every single byte, w/o timeout (1 byte, no timer).
	raw.c_cc[VMIN] = 1;
	raw.c_cc[VTIME] = 0;

	BC_SIG_LOCK;

	// Put terminal in raw mode after flushing.
	do {
		err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
	} while (BC_ERR(err < 0) && errno == EINTR);

	BC_SIG_UNLOCK;

	if (BC_ERR(err < 0)) bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);
#endif // _WIN32

	h->rawMode = true;
}

/**
 * Disables raw mode.
 * @param h  The history data.
 */
static void bc_history_disableRaw(BcHistory *h) {

	sig_atomic_t lock;

	if (!h->rawMode) return;

	BC_SIG_TRYLOCK(lock);

#ifndef _WIN32
	if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &h->orig_termios) != -1))
		h->rawMode = false;
#endif // _WIN32

	BC_SIG_TRYUNLOCK(lock);
}

/**
 * Uses the ESC [6n escape sequence to query the horizontal cursor position
 * and return it. On error -1 is returned, on success the position of the
 * cursor.
 * @return  The horizontal cursor position.
 */
static size_t bc_history_cursorPos(void) {

	char buf[BC_HIST_SEQ_SIZE];
	char *ptr, *ptr2;
	size_t cols, rows, i;

	// Report cursor location.
	bc_file_write(&vm.fout, bc_flush_none, "\x1b[6n", 4);
	bc_file_flush(&vm.fout, bc_flush_none);

	// Read the response: ESC [ rows ; cols R.
	for (i = 0; i < sizeof(buf) - 1; ++i) {
		if (bc_history_read(buf + i, 1) != 1 || buf[i] == 'R') break;
	}

	buf[i] = '\0';

	// This is basically an error; we didn't get what we were expecting.
	if (BC_ERR(buf[0] != BC_ACTION_ESC || buf[1] != '[')) return SIZE_MAX;

	// Parse the rows.
	ptr = buf + 2;
	rows = strtoul(ptr, &ptr2, 10);

	// Here we also didn't get what we were expecting.
	if (BC_ERR(!rows || ptr2[0] != ';')) return SIZE_MAX;

	// Parse the columns.
	ptr = ptr2 + 1;
	cols = strtoul(ptr, NULL, 10);

	if (BC_ERR(!cols)) return SIZE_MAX;

	return cols <= UINT16_MAX ? cols : 0;
}

/**
 * Tries to get the number of columns in the current terminal, or assume 80
 * if it fails.
 * @return  The number of columns in the terminal.
 */
static size_t bc_history_columns(void) {

#ifndef _WIN32

	struct winsize ws;
	int ret;

	BC_SIG_LOCK;

	ret = ioctl(vm.fout.fd, TIOCGWINSZ, &ws);

	BC_SIG_UNLOCK;

	if (BC_ERR(ret == -1 || !ws.ws_col)) {

		// Calling ioctl() failed. Try to query the terminal itself.
		size_t start, cols;

		// Get the initial position so we can restore it later.
		start = bc_history_cursorPos();
		if (BC_ERR(start == SIZE_MAX)) return BC_HIST_DEF_COLS;

		// Go to right margin and get position.
		bc_file_write(&vm.fout, bc_flush_none, "\x1b[999C", 6);
		bc_file_flush(&vm.fout, bc_flush_none);
		cols = bc_history_cursorPos();
		if (BC_ERR(cols == SIZE_MAX)) return BC_HIST_DEF_COLS;

		// Restore position.
		if (cols > start) {
			bc_file_printf(&vm.fout, "\x1b[%zuD", cols - start);
			bc_file_flush(&vm.fout, bc_flush_none);
		}

		return cols;
	}

	return ws.ws_col;

#else // _WIN32

	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
		return 80;

	return ((size_t) (csbi.srWindow.Right)) - csbi.srWindow.Left + 1;

#endif // _WIN32
}

/**
 * Gets the column length of prompt text. This is probably unnecessary because
 * the prompts that I use are ASCII, but I kept it just in case.
 * @param prompt  The prompt.
 * @param plen    The length of the prompt.
 * @return        The column length of the prompt.
 */
static size_t bc_history_promptColLen(const char *prompt, size_t plen) {

	char buf[BC_HIST_MAX_LINE + 1];
	size_t buf_len = 0, off = 0;

	// The original linenoise-mob checked for ANSI escapes here on the prompt. I
	// know the prompts do not have ANSI escapes. I deleted the code.
	while (off < plen) buf[buf_len++] = prompt[off++];

	return bc_history_colPos(buf, buf_len, buf_len);
}

/**
 * Rewrites the currently edited line accordingly to the buffer content,
 * cursor position, and number of columns of the terminal.
 * @param h  The history data.
 */
static void bc_history_refresh(BcHistory *h) {

	char* buf = h->buf.v;
	size_t colpos, len = BC_HIST_BUF_LEN(h), pos = h->pos, extras_len = 0;

	bc_file_flush(&vm.fout, bc_flush_none);

	// Get to the prompt column position from the left.
	while(h->pcol + bc_history_colPos(buf, len, pos) >= h->cols) {

		size_t chlen = bc_history_nextLen(buf, len, 0, NULL);

		buf += chlen;
		len -= chlen;
		pos -= chlen;
	}

	// Get to the prompt column position from the right.
	while (h->pcol + bc_history_colPos(buf, len, len) > h->cols)
		len -= bc_history_prevLen(buf, len);

	// Cursor to left edge.
	bc_file_write(&vm.fout, bc_flush_none, "\r", 1);

	// Take the extra stuff into account. This is where history makes sure to
	// preserve stuff that was printed without a newline.
	if (h->extras.len > 1) {

		extras_len = h->extras.len - 1;

		bc_vec_grow(&h->buf, extras_len);

		len += extras_len;
		pos += extras_len;

		bc_file_write(&vm.fout, bc_flush_none, h->extras.v, extras_len);
	}

	// Write the prompt, if desired.
	if (BC_PROMPT) bc_file_write(&vm.fout, bc_flush_none, h->prompt, h->plen);

	bc_file_write(&vm.fout, bc_flush_none, h->buf.v, len - extras_len);

	// Erase to right.
	bc_file_write(&vm.fout, bc_flush_none, "\x1b[0K", 4);

	// We need to be sure to grow this.
	if (pos >= h->buf.len - extras_len)
		bc_vec_grow(&h->buf, pos + extras_len);

	// Move cursor to original position.
	colpos = bc_history_colPos(h->buf.v, len - extras_len, pos) + h->pcol;

	// Set the cursor position again.
	if (colpos) bc_file_printf(&vm.fout, "\r\x1b[%zuC", colpos);

	bc_file_flush(&vm.fout, bc_flush_none);
}

/**
 * Inserts the character(s) 'c' at cursor current position.
 * @param h     The history data.
 * @param cbuf  The character buffer to copy from.
 * @param clen  The number of characters to copy.
 */
static void bc_history_edit_insert(BcHistory *h, const char *cbuf, size_t clen)
{
	bc_vec_grow(&h->buf, clen);

	// If we are at the end of the line...
	if (h->pos == BC_HIST_BUF_LEN(h)) {

		size_t colpos = 0, len;

		// Copy into the buffer.
		memcpy(bc_vec_item(&h->buf, h->pos), cbuf, clen);

		// Adjust the buffer.
		h->pos += clen;
		h->buf.len += clen - 1;
		bc_vec_pushByte(&h->buf, '\0');

		// Set the length and column position.
		len = BC_HIST_BUF_LEN(h) + h->extras.len - 1;
		colpos = bc_history_promptColLen(h->prompt, h->plen);
		colpos += bc_history_colPos(h->buf.v, len, len);

		// Do we have the trivial case?
		if (colpos < h->cols) {

			// Avoid a full update of the line in the trivial case.
			bc_file_write(&vm.fout, bc_flush_none, cbuf, clen);
			bc_file_flush(&vm.fout, bc_flush_none);
		}
		else bc_history_refresh(h);
	}
	else {

		// Amount that we need to move.
		size_t amt = BC_HIST_BUF_LEN(h) - h->pos;

		// Move the stuff.
		memmove(h->buf.v + h->pos + clen, h->buf.v + h->pos, amt);
		memcpy(h->buf.v + h->pos, cbuf, clen);

		// Adjust the buffer.
		h->pos += clen;
		h->buf.len += clen;
		h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';

		bc_history_refresh(h);
	}
}

/**
 * Moves the cursor to the left.
 * @param h  The history data.
 */
static void bc_history_edit_left(BcHistory *h) {

	// Stop at the left end.
	if (h->pos <= 0) return;

	h->pos -= bc_history_prevLen(h->buf.v, h->pos);

	bc_history_refresh(h);
}

/**
 * Moves the cursor to the right.
 * @param h  The history data.
*/
static void bc_history_edit_right(BcHistory *h) {

	// Stop at the right end.
	if (h->pos == BC_HIST_BUF_LEN(h)) return;

	h->pos += bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);

	bc_history_refresh(h);
}

/**
 * Moves the cursor to the end of the current word.
 * @param h  The history data.
 */
static void bc_history_edit_wordEnd(BcHistory *h) {

	size_t len = BC_HIST_BUF_LEN(h);

	// Don't overflow.
	if (!len || h->pos >= len) return;

	// Find the word, then find the end of it.
	while (h->pos < len && isspace(h->buf.v[h->pos])) h->pos += 1;
	while (h->pos < len && !isspace(h->buf.v[h->pos])) h->pos += 1;

	bc_history_refresh(h);
}

/**
 * Moves the cursor to the start of the current word.
 * @param h  The history data.
 */
static void bc_history_edit_wordStart(BcHistory *h) {

	size_t len = BC_HIST_BUF_LEN(h);

	// Stop with no data.
	if (!len) return;

	// Find the word, the find the beginning of the word.
	while (h->pos > 0 && isspace(h->buf.v[h->pos - 1])) h->pos -= 1;
	while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1])) h->pos -= 1;

	bc_history_refresh(h);
}

/**
 * Moves the cursor to the start of the line.
 * @param h  The history data.
 */
static void bc_history_edit_home(BcHistory *h) {

	// Stop at the beginning.
	if (!h->pos) return;

	h->pos = 0;

	bc_history_refresh(h);
}

/**
 * Moves the cursor to the end of the line.
 * @param h  The history data.
 */
static void bc_history_edit_end(BcHistory *h) {

	// Stop at the end of the line.
	if (h->pos == BC_HIST_BUF_LEN(h)) return;

	h->pos = BC_HIST_BUF_LEN(h);

	bc_history_refresh(h);
}

/**
 * Substitutes the currently edited line with the next or previous history
 * entry as specified by 'dir' (direction).
 * @param h    The history data.
 * @param dir  The direction to substitute; true means previous, false next.
 */
static void bc_history_edit_next(BcHistory *h, bool dir) {

	const char *dup, *str;

	// Stop if there is no history.
	if (h->history.len <= 1) return;

	BC_SIG_LOCK;

	// Duplicate the buffer.
	if (h->buf.v[0]) dup = bc_vm_strdup(h->buf.v);
	else dup = "";

	// Update the current history entry before overwriting it with the next one.
	bc_vec_replaceAt(&h->history, h->history.len - 1 - h->idx, &dup);

	BC_SIG_UNLOCK;

	// Show the new entry.
	h->idx += (dir == BC_HIST_PREV ? 1 : SIZE_MAX);

	// Se the index appropriately at the ends.
	if (h->idx == SIZE_MAX) {
		h->idx = 0;
		return;
	}
	else if (h->idx >= h->history.len) {
		h->idx = h->history.len - 1;
		return;
	}

	// Get the string.
	str = *((char**) bc_vec_item(&h->history, h->history.len - 1 - h->idx));
	bc_vec_string(&h->buf, strlen(str), str);

	assert(h->buf.len > 0);

	// Set the position at the end.
	h->pos = BC_HIST_BUF_LEN(h);

	bc_history_refresh(h);
}

/**
 * Deletes the character at the right of the cursor without altering the cursor
 * position. Basically, this is what happens with the "Delete" keyboard key.
 * @param h  The history data.
 */
static void bc_history_edit_delete(BcHistory *h) {

	size_t chlen, len = BC_HIST_BUF_LEN(h);

	// If there is no character, skip.
	if (!len || h->pos >= len) return;

	// Get the length of the character.
	chlen = bc_history_nextLen(h->buf.v, len, h->pos, NULL);

	// Move characters after it into its place.
	memmove(h->buf.v + h->pos, h->buf.v + h->pos + chlen, len - h->pos - chlen);

	// Make the buffer valid again.
	h->buf.len -= chlen;
	h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';

	bc_history_refresh(h);
}

/**
 * Deletes the character to the left of the cursor and moves the cursor back one
 * space. Basically, this is what happens with the "Backspace" keyboard key.
 * @param h  The history data.
 */
static void bc_history_edit_backspace(BcHistory *h) {

	size_t chlen, len = BC_HIST_BUF_LEN(h);

	// If there are no characters, skip.
	if (!h->pos || !len) return;

	// Get the length of the previous character.
	chlen = bc_history_prevLen(h->buf.v, h->pos);

	// Move everything back one.
	memmove(h->buf.v + h->pos - chlen, h->buf.v + h->pos, len - h->pos);

	// Make the buffer valid again.
	h->pos -= chlen;
	h->buf.len -= chlen;
	h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';

	bc_history_refresh(h);
}

/**
 * Deletes the previous word, maintaining the cursor at the start of the
 * current word.
 * @param h  The history data.
 */
static void bc_history_edit_deletePrevWord(BcHistory *h) {

	size_t diff, old_pos = h->pos;

	// If at the beginning of the line, skip.
	if (!old_pos) return;

	// Find the word, then the beginning of the word.
	while (h->pos > 0 && isspace(h->buf.v[h->pos - 1])) --h->pos;
	while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1])) --h->pos;

	// Get the difference in position.
	diff = old_pos - h->pos;

	// Move the data back.
	memmove(h->buf.v + h->pos, h->buf.v + old_pos,
	        BC_HIST_BUF_LEN(h) - old_pos + 1);

	// Make the buffer valid again.
	h->buf.len -= diff;

	bc_history_refresh(h);
}

/**
 * Deletes the next word, maintaining the cursor at the same position.
 * @param h  The history data.
 */
static void bc_history_edit_deleteNextWord(BcHistory *h) {

	size_t next_end = h->pos, len = BC_HIST_BUF_LEN(h);

	// If at the end of the line, skip.
	if (next_end == len) return;

	// Find the word, then the end of the word.
	while (next_end < len && isspace(h->buf.v[next_end])) ++next_end;
	while (next_end < len && !isspace(h->buf.v[next_end])) ++next_end;

	// Move the stuff into position.
	memmove(h->buf.v + h->pos, h->buf.v + next_end, len - next_end);

	// Make the buffer valid again.
	h->buf.len -= next_end - h->pos;

	bc_history_refresh(h);
}

/**
 * Swaps two characters, the one under the cursor and the one to the left.
 * @param h  The history data.
 */
static void bc_history_swap(BcHistory *h) {

	size_t pcl, ncl;
	char auxb[5];

	// Get the length of the previous and next characters.
	pcl = bc_history_prevLen(h->buf.v, h->pos);
	ncl = bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);

	// To perform a swap we need:
	// * Nonzero char length to the left.
	// * To not be at the end of the line.
	if (pcl && h->pos != BC_HIST_BUF_LEN(h) && pcl < 5 && ncl < 5) {

		// Swap.
		memcpy(auxb, h->buf.v + h->pos - pcl, pcl);
		memcpy(h->buf.v + h->pos - pcl, h->buf.v + h->pos, ncl);
		memcpy(h->buf.v + h->pos - pcl + ncl, auxb, pcl);

		// Reset the position.
		h->pos += ((~pcl) + 1) + ncl;

		bc_history_refresh(h);
	}
}

/**
 * Raises the specified signal. This is a convenience function.
 * @param h    The history data.
 * @param sig  The signal to raise.
 */
static void bc_history_raise(BcHistory *h, int sig) {

	// We really don't want to be in raw mode when longjmp()'s are flying.
	bc_history_disableRaw(h);
	raise(sig);
}

/**
 * Handles escape sequences. This function will make sense if you know VT100
 * escape codes; otherwise, it will be confusing.
 * @param h  The history data.
 */
static void bc_history_escape(BcHistory *h) {

	char c, seq[3];

	// Read a character into seq.
	if (BC_ERR(BC_HIST_READ(seq, 1))) return;

	c = seq[0];

	// ESC ? sequences.
	if (c != '[' && c != 'O') {
		if (c == 'f') bc_history_edit_wordEnd(h);
		else if (c == 'b') bc_history_edit_wordStart(h);
		else if (c == 'd') bc_history_edit_deleteNextWord(h);
	}
	else {

		// Read a character into seq.
		if (BC_ERR(BC_HIST_READ(seq + 1, 1)))
			bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);

		// ESC [ sequences.
		if (c == '[') {

			c = seq[1];

			if (c >= '0' && c <= '9') {

				// Extended escape, read additional byte.
				if (BC_ERR(BC_HIST_READ(seq + 2, 1)))
					bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);

				if (seq[2] == '~' && c == '3') bc_history_edit_delete(h);
				else if(seq[2] == ';') {

					// Read two characters into seq.
					if (BC_ERR(BC_HIST_READ(seq, 2)))
						bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);

					if (seq[0] != '5') return;
					else if (seq[1] == 'C') bc_history_edit_wordEnd(h);
					else if (seq[1] == 'D') bc_history_edit_wordStart(h);
				}
			}
			else {

				switch(c) {

					// Up.
					case 'A':
					{
						bc_history_edit_next(h, BC_HIST_PREV);
						break;
					}

					// Down.
					case 'B':
					{
						bc_history_edit_next(h, BC_HIST_NEXT);
						break;
					}

					// Right.
					case 'C':
					{
						bc_history_edit_right(h);
						break;
					}

					// Left.
					case 'D':
					{
						bc_history_edit_left(h);
						break;
					}

					// Home.
					case 'H':
					case '1':
					{
						bc_history_edit_home(h);
						break;
					}

					// End.
					case 'F':
					case '4':
					{
						bc_history_edit_end(h);
						break;
					}

					case 'd':
					{
						bc_history_edit_deleteNextWord(h);
						break;
					}
				}
			}
		}
		// ESC O sequences.
		else {

			switch (seq[1]) {

				case 'A':
				{
					bc_history_edit_next(h, BC_HIST_PREV);
					break;
				}

				case 'B':
				{
					bc_history_edit_next(h, BC_HIST_NEXT);
					break;
				}

				case 'C':
				{
					bc_history_edit_right(h);
					break;
				}

				case 'D':
				{
					bc_history_edit_left(h);
					break;
				}

				case 'F':
				{
					bc_history_edit_end(h);
					break;
				}

				case 'H':
				{
					bc_history_edit_home(h);
					break;
				}
			}
		}
	}
}

/**
 * Adds a line to the history.
 * @param h     The history data.
 * @param line  The line to add.
 */
static void bc_history_add(BcHistory *h, char *line) {

	// If there is something already there...
	if (h->history.len) {

		// Get the previous.
		char *s = *((char**) bc_vec_item_rev(&h->history, 0));

		// Check for, and discard, duplicates.
		if (!strcmp(s, line)) {

			BC_SIG_LOCK;

			free(line);

			BC_SIG_UNLOCK;

			return;
		}
	}

	bc_vec_push(&h->history, &line);
}

/**
 * Adds an empty line to the history. This is separate from bc_history_add()
 * because we don't want it allocating.
 * @param h  The history data.
 */
static void bc_history_add_empty(BcHistory *h) {

	const char *line = "";

	// If there is something already there...
	if (h->history.len) {

		// Get the previous.
		char *s = *((char**) bc_vec_item_rev(&h->history, 0));

		// Check for, and discard, duplicates.
		if (!s[0]) return;
	}

	bc_vec_push(&h->history, &line);
}

/**
 * Resets the history state to nothing.
 * @param h  The history data.
 */
static void bc_history_reset(BcHistory *h) {

	h->oldcolpos = h->pos = h->idx = 0;
	h->cols = bc_history_columns();

	// The latest history entry is always our current buffer, that
	// initially is just an empty string.
	bc_history_add_empty(h);

	// Buffer starts empty.
	bc_vec_empty(&h->buf);
}

/**
 * Prints a control character.
 * @param h  The history data.
 * @param c  The control character to print.
 */
static void bc_history_printCtrl(BcHistory *h, unsigned int c) {

	char str[3] = "^A";
	const char newline[2] = "\n";

	// Set the correct character.
	str[1] = (char) (c + 'A' - BC_ACTION_CTRL_A);

	// Concatenate the string.
	bc_vec_concat(&h->buf, str);

	bc_history_refresh(h);

	// Pop the string.
	bc_vec_npop(&h->buf, sizeof(str));
	bc_vec_pushByte(&h->buf, '\0');

#ifndef _WIN32
	if (c != BC_ACTION_CTRL_C && c != BC_ACTION_CTRL_D)
#endif // _WIN32
	{
		// We sometimes want to print a newline; for the times we don't; it's
		// because newlines are taken care of elsewhere.
		bc_file_write(&vm.fout, bc_flush_none, newline, sizeof(newline) - 1);
		bc_history_refresh(h);
	}
}

/**
 * Edits a line of history. This function is the core of the line editing
 * capability of bc history. It expects 'fd' to be already in "raw mode" so that
 * every key pressed will be returned ASAP to read().
 * @param h       The history data.
 * @param prompt  The prompt.
 * @return        BC_STATUS_SUCCESS or BC_STATUS_EOF.
 */
static BcStatus bc_history_edit(BcHistory *h, const char *prompt) {

	bc_history_reset(h);

	// Don't write the saved output the first time. This is because it has
	// already been written to output. In other words, don't uncomment the
	// line below or add anything like it.
	// bc_file_write(&vm.fout, bc_flush_none, h->extras.v, h->extras.len - 1);

	// Write the prompt if desired.
	if (BC_PROMPT) {

		h->prompt = prompt;
		h->plen = strlen(prompt);
		h->pcol = bc_history_promptColLen(prompt, h->plen);

		bc_file_write(&vm.fout, bc_flush_none, prompt, h->plen);
		bc_file_flush(&vm.fout, bc_flush_none);
	}

	// This is the input loop.
	for (;;) {

		BcStatus s;
		char cbuf[32];
		unsigned int c = 0;
		size_t nread = 0;

		// Read a code.
		s = bc_history_readCode(cbuf, sizeof(cbuf), &c, &nread);
		if (BC_ERR(s)) return s;

		switch (c) {

			case BC_ACTION_LINE_FEED:
			case BC_ACTION_ENTER:
			{
				// Return the line.
				bc_vec_pop(&h->history);
				return s;
			}

			case BC_ACTION_TAB:
			{
				// My tab handling is dumb; it just prints 8 spaces every time.
				memcpy(cbuf, bc_history_tab, bc_history_tab_len + 1);
				bc_history_edit_insert(h, cbuf, bc_history_tab_len);
				break;
			}

#ifndef _WIN32
			case BC_ACTION_CTRL_C:
			{
				bc_history_printCtrl(h, c);

				// Quit if the user wants it.
				if (!BC_SIGINT) {
					vm.status = BC_STATUS_QUIT;
					BC_JMP;
				}

				// Print the ready message.
				bc_file_write(&vm.fout, bc_flush_none, vm.sigmsg, vm.siglen);
				bc_file_write(&vm.fout, bc_flush_none, bc_program_ready_msg,
				              bc_program_ready_msg_len);
				bc_history_reset(h);
				bc_history_refresh(h);

				break;
			}
#endif // _WIN32

			case BC_ACTION_BACKSPACE:
			case BC_ACTION_CTRL_H:
			{
				bc_history_edit_backspace(h);
				break;
			}

#ifndef _WIN32
			// Act as end-of-file.
			case BC_ACTION_CTRL_D:
			{
				bc_history_printCtrl(h, c);
				return BC_STATUS_EOF;
			}
#endif // _WIN32

			// Swaps current character with previous.
			case BC_ACTION_CTRL_T:
			{
				bc_history_swap(h);
				break;
			}

			case BC_ACTION_CTRL_B:
			{
				bc_history_edit_left(h);
				break;
			}

			case BC_ACTION_CTRL_F:
			{
				bc_history_edit_right(h);
				break;
			}

			case BC_ACTION_CTRL_P:
			{
				bc_history_edit_next(h, BC_HIST_PREV);
				break;
			}

			case BC_ACTION_CTRL_N:
			{
				bc_history_edit_next(h, BC_HIST_NEXT);
				break;
			}

			case BC_ACTION_ESC:
			{
				bc_history_escape(h);
				break;
			}

			// Delete the whole line.
			case BC_ACTION_CTRL_U:
			{
				bc_vec_string(&h->buf, 0, "");
				h->pos = 0;

				bc_history_refresh(h);

				break;
			}

			// Delete from current to end of line.
			case BC_ACTION_CTRL_K:
			{
				bc_vec_npop(&h->buf, h->buf.len - h->pos);
				bc_vec_pushByte(&h->buf, '\0');
				bc_history_refresh(h);
				break;
			}

			// Go to the start of the line.
			case BC_ACTION_CTRL_A:
			{
				bc_history_edit_home(h);
				break;
			}

			// Go to the end of the line.
			case BC_ACTION_CTRL_E:
			{
				bc_history_edit_end(h);
				break;
			}

			// Clear screen.
			case BC_ACTION_CTRL_L:
			{
				bc_file_write(&vm.fout, bc_flush_none, "\x1b[H\x1b[2J", 7);
				bc_history_refresh(h);
				break;
			}

			// Delete previous word.
			case BC_ACTION_CTRL_W:
			{
				bc_history_edit_deletePrevWord(h);
				break;
			}

			default:
			{
				// If we have a control character, print it and raise signals as
				// needed.
				if ((c >= BC_ACTION_CTRL_A && c <= BC_ACTION_CTRL_Z) ||
				    c == BC_ACTION_CTRL_BSLASH)
				{
					bc_history_printCtrl(h, c);
#ifndef _WIN32
					if (c == BC_ACTION_CTRL_Z) bc_history_raise(h, SIGTSTP);
					if (c == BC_ACTION_CTRL_S) bc_history_raise(h, SIGSTOP);
					if (c == BC_ACTION_CTRL_BSLASH)
						bc_history_raise(h, SIGQUIT);
#else // _WIN32
					vm.status = BC_STATUS_QUIT;
					BC_JMP;
#endif // _WIN32
				}
				// Otherwise, just insert.
				else bc_history_edit_insert(h, cbuf, nread);
				break;
			}
		}
	}

	return BC_STATUS_SUCCESS;
}

/**
 * Returns true if stdin has more data. This is for multi-line pasting, and it
 * does not work on Windows.
 * @param h  The history data.
 */
static inline bool bc_history_stdinHasData(BcHistory *h) {
#ifndef _WIN32
	int n;
	return pselect(1, &h->rdset, NULL, NULL, &h->ts, &h->sigmask) > 0 ||
	       (ioctl(STDIN_FILENO, FIONREAD, &n) >= 0 && n > 0);
#else // _WIN32
	return false;
#endif // _WIN32
}

BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt) {

	BcStatus s;
	char* line;

	assert(vm.fout.len == 0);

	bc_history_enableRaw(h);

	do {

		// Do the edit.
		s = bc_history_edit(h, prompt);

		// Print a newline and flush.
		bc_file_write(&vm.fout, bc_flush_none, "\n", 1);
		bc_file_flush(&vm.fout, bc_flush_none);

		// If we actually have data...
		if (h->buf.v[0]) {

			BC_SIG_LOCK;

			// Duplicate it.
			line = bc_vm_strdup(h->buf.v);

			BC_SIG_UNLOCK;

			// Store it.
			bc_history_add(h, line);
		}
		// Add an empty string.
		else bc_history_add_empty(h);

		// Concatenate the line to the return vector.
		bc_vec_concat(vec, h->buf.v);
		bc_vec_concat(vec, "\n");

	} while (!s && bc_history_stdinHasData(h));

	assert(!s || s == BC_STATUS_EOF);

	bc_history_disableRaw(h);

	return s;
}

void bc_history_string_free(void *str) {
	char *s = *((char**) str);
	BC_SIG_ASSERT_LOCKED;
	if (s[0]) free(s);
}

void bc_history_init(BcHistory *h) {

	BC_SIG_ASSERT_LOCKED;

	bc_vec_init(&h->buf, sizeof(char), BC_DTOR_NONE);
	bc_vec_init(&h->history, sizeof(char*), BC_DTOR_HISTORY_STRING);
	bc_vec_init(&h->extras, sizeof(char), BC_DTOR_NONE);

#ifndef _WIN32
	FD_ZERO(&h->rdset);
	FD_SET(STDIN_FILENO, &h->rdset);
	h->ts.tv_sec = 0;
	h->ts.tv_nsec = 0;

	sigemptyset(&h->sigmask);
	sigaddset(&h->sigmask, SIGINT);
#endif // _WIN32

	h->rawMode = false;
	h->badTerm = bc_history_isBadTerm();

#ifdef _WIN32
	if (!h->badTerm) {
		SetConsoleCP(CP_UTF8);
		SetConsoleOutputCP(CP_UTF8);
		GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &h->orig_console_mode);
		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
		               ENABLE_VIRTUAL_TERMINAL_INPUT);
	}
#endif // _WIN32
}

void bc_history_free(BcHistory *h) {
	BC_SIG_ASSERT_LOCKED;
#ifndef _WIN32
	bc_history_disableRaw(h);
#else // _WIN32
	SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), h->orig_console_mode);
#endif // _WIN32
#ifndef NDEBUG
	bc_vec_free(&h->buf);
	bc_vec_free(&h->history);
	bc_vec_free(&h->extras);
#endif // NDEBUG
}

#if BC_DEBUG_CODE

/**
 * Prints scan codes. This special mode is used by bc history in order to print
 * scan codes on screen for debugging / development purposes.
 * @param h  The history data.
 */
void bc_history_printKeyCodes(BcHistory *h) {

	char quit[4];

	bc_vm_printf("Linenoise key codes debugging mode.\n"
	             "Press keys to see scan codes. "
	             "Type 'quit' at any time to exit.\n");

	bc_history_enableRaw(h);
	memset(quit, ' ', 4);

	while(true) {

		char c;
		ssize_t nread;

		nread = bc_history_read(&c, 1);
		if (nread <= 0) continue;

		// Shift string to left.
		memmove(quit, quit + 1, sizeof(quit) - 1);

		// Insert current char on the right.
		quit[sizeof(quit) - 1] = c;
		if (!memcmp(quit, "quit", sizeof(quit))) break;

		bc_vm_printf("'%c' %lu (type quit to exit)\n",
		             isprint(c) ? c : '?', (unsigned long) c);

		// Go left edge manually, we are in raw mode.
		bc_vm_putchar('\r', bc_flush_none);
		bc_file_flush(&vm.fout, bc_flush_none);
	}

	bc_history_disableRaw(h);
}
#endif // BC_DEBUG_CODE

#endif // BC_ENABLE_HISTORY