File: editline.c

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (1985 lines) | stat: -rw-r--r-- 42,672 bytes parent folder | download | duplicates (3)
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
/****************************************************************************/
/*                                                                          */
/* Copyright 1992 Simmule Turner and Rich Salz.  All rights reserved.       */
/*                                                                          */
/* This software is not subject to any license of the American Telephone    */
/* and Telegraph Company or of the Regents of the University of California. */
/*                                                                          */
/* Permission is granted to anyone to use this software for any purpose on  */
/* any computer system, and to alter it and redistribute it freely, subject */
/* to the following restrictions:                                           */
/* 1. The authors are not responsible for the consequences of use of this   */
/*    software, no matter how awful, even if they arise from flaws in it.   */
/* 2. The origin of this software must not be misrepresented, either by     */
/*    explicit claim or by omission.  Since few users ever read sources,    */
/*    credits must appear in the documentation.                             */
/* 3. Altered versions must be plainly marked as such, and must not be      */
/*    misrepresented as being the original software.  Since few users       */
/*    ever read sources, credits must appear in the documentation.          */
/* 4. This notice may not be removed or altered.                            */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*  This is a line-editing library, it can be linked into almost any        */
/*  program to provide command-line editing and recall.                     */
/*                                                                          */
/*  Posted to comp.sources.misc Sun, 2 Aug 1992 03:05:27 GMT                */
/*      by rsalz@osf.org (Rich $alz)                                        */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*  The version contained here has some modifications by awb@cstr.ed.ac.uk  */
/*  (Alan W Black) in order to integrate it with the Edinburgh Speech Tools */
/*  library and Scheme-in-one-defun in particular, though these changes     */
/*  have a much more general use that just us.  All modifications to        */
/*  to this work are continued with the same copyright above.  That is      */
/*  this version of editline does not have the the "no commercial use"      */
/*  restriction that some of the rest of the EST library may have           */
/*  awb Dec 30 1998                                                         */
/*                                                                          */
/*  Specific additions (there are other smaller ones too, all marked):      */
/*    some ansificiation and prototypes added                               */
/*    storage and retrieval of history over sessions                        */
/*    user definable history completion                                     */
/*    possibles listing in completion                                       */
/*    reverse incremental search                                            */
/*    lines longer than window width (mostly)                               */
/*    reasonable support for 8 bit chars in languages other than English    */
/*                                                                          */
/****************************************************************************/

/*  $Revision: 1.6 $
**
**  Main editing routines for editline library.
*/
#include "editline.h"
#include "EST_unix.h"
#include <ctype.h>

/*
**  Manifest constants.
*/
#define SCREEN_WIDTH	80
#define SCREEN_ROWS	24
#define NO_ARG		(-1)
#define DEL		127
#define ESC		0x1b
#define CTL(x)		(char)((x) & 0x1F)
#define ISCTL(x)	((x) && (x) < ' ')
#define UNCTL(x)	(char)((x) + 64)
#define META(x)		(char)((x) | 0x80)
#define ISMETA(x)	((x) & 0x80)
#define UNMETA(x)	(char)((x) & 0x7F)
/* modified by awb to allow specifcation of history size at run time  */
/* (though only once)                                                 */
int editline_histsize=256;
char *editline_history_file;
/* If this is defined it'll be called for completion first, before the */
/* internal file name completion will be                               */
EL_USER_COMPLETION_FUNCTION_TYPE*el_user_completion_function = NULL;

/*
**  The type of case-changing to perform.
*/
typedef enum _CASE {
    TOupper, TOlower, TOcapitalize
} CASE;

/*
**  Key to command mapping.
*/
typedef struct _KEYMAP {
    ECHAR	Key;
    STATUS	(*Function)();
} KEYMAP;

/*
**  Command history structure.
*/
typedef struct _HISTORY {
    int		Size;
    int		Pos;
    ECHAR	**Lines;
} HISTORY;

/*
**  Globals.
*/
int		rl_eof;
int		rl_erase;
int		rl_intr;
int		rl_kill;

ECHAR		el_NIL[] = "";
extern CONST ECHAR	*el_Input;
STATIC ECHAR		*Line = NULL;
STATIC CONST char	*Prompt = NULL;
STATIC ECHAR		*Yanked = NULL;
STATIC char		*Screen = NULL;
/* STATIC char		NEWLINE[]= CRLF; */
STATIC HISTORY		H;
int		rl_quit;
STATIC int		Repeat;
STATIC int		End;
STATIC int		Mark;
STATIC int		OldPoint;
STATIC int		Point;
extern int		el_PushBack;
extern int		el_Pushed;
FORWARD KEYMAP		Map[33];
FORWARD KEYMAP		MetaMap[64];
STATIC ESIZE_T		Length;
STATIC ESIZE_T		ScreenCount;
STATIC ESIZE_T		ScreenSize;
STATIC ECHAR		*backspace = NULL;
STATIC ECHAR		*upline = NULL;
STATIC ECHAR		*clrpage = NULL;
STATIC ECHAR		*downline = NULL;
STATIC ECHAR		*move_right = NULL;
STATIC ECHAR             *newline = NULL;
STATIC ECHAR             *bol = NULL;
STATIC ECHAR             *nextline = NULL;
STATIC int		TTYwidth;
STATIC int		TTYrows;
STATIC int              RequireNLforWrap = 1;
STATIC int              el_intr_pending = 0;
int                     el_no_echo = 0;  /* e.g under emacs */

/* A little ansification with prototypes -- awb */
extern void TTYflush();
STATIC void TTYput(ECHAR c);
STATIC void TTYputs(ECHAR *p);
STATIC void TTYshow(ECHAR c);
STATIC void TTYstring(ECHAR *p);
extern unsigned int TTYget();
STATIC void TTYinfo();
STATIC void print_columns(int ac, char **av);
STATIC void reposition(int reset);
STATIC void left(STATUS Change);
STATIC void right(STATUS Change);
STATIC STATUS ring_bell();
#if 0
STATIC STATUS do_macro(unsigned int c);
#endif
STATIC STATUS do_forward(STATUS move);
STATIC STATUS do_case(ECHAR type);
STATIC STATUS case_down_word();
STATIC STATUS case_up_word();
STATIC void ceol();
STATIC void clear_line();
STATIC STATUS insert_string(ECHAR *p);
STATIC ECHAR *next_hist();
STATIC ECHAR *prev_hist();
STATIC STATUS do_insert_hist(ECHAR *p);
STATIC STATUS do_hist(ECHAR *(*move)());
STATIC STATUS h_next();
STATIC STATUS h_prev();
STATIC STATUS h_first();
STATIC STATUS h_last();
STATIC int substrcmp(char *text, char *pat, int len);
STATIC ECHAR *search_hist(ECHAR *search, ECHAR *(*move)());
STATIC STATUS h_search();
STATIC STATUS fd_char();
STATIC void save_yank(int begin, int i);
STATIC STATUS delete_string(int count);
STATIC STATUS bk_char();
STATIC STATUS bk_del_char();
STATIC STATUS redisplay();
STATIC STATUS kill_line();
STATIC char *rsearch_hist(char *patt, int *lpos,int *cpos);
STATIC STATUS h_risearch();
STATIC STATUS insert_char(int c);
STATIC STATUS meta();
STATIC STATUS emacs(unsigned int c);
STATIC STATUS TTYspecial(unsigned int c);
STATIC ECHAR *editinput();
STATIC void hist_add(ECHAR *p);
STATIC STATUS beg_line();
STATIC STATUS del_char();
STATIC STATUS end_line();
STATIC ECHAR *find_word();
STATIC STATUS c_complete();
STATIC STATUS c_possible();
STATIC STATUS accept_line();
STATIC STATUS transpose();
STATIC STATUS quote();
STATIC STATUS wipe();
STATIC STATUS mk_set();
STATIC STATUS exchange();
STATIC STATUS yank();
STATIC STATUS copy_region();
STATIC STATUS move_to_char();
STATIC STATUS fd_word();
STATIC STATUS fd_kill_word();
STATIC STATUS bk_word();
STATIC STATUS bk_kill_word();
STATIC int argify(ECHAR *line, ECHAR ***avp);
STATIC STATUS last_argument();

/* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
int		rl_meta_chars = 0;

/*
**  Declarations.
*/
STATIC ECHAR	*editinput();
#if	defined(USE_TERMCAP)
extern char	*getenv();
extern char	*tgetstr();
extern int	tgetent();
extern int	tgetnum();
#endif	/* defined(USE_TERMCAP) */

/*
**  TTY input/output functions.
*/

void TTYflush()
{
    if (ScreenCount) {
	if (el_no_echo == 0)
	    (void)write(1, Screen, ScreenCount);
	ScreenCount = 0;
    }
}

STATIC void TTYput(ECHAR c)
{
    Screen[ScreenCount] = c;
    if (++ScreenCount >= ScreenSize - 1) {
	ScreenSize += SCREEN_INC;
	RENEW(Screen, char, ScreenSize);
    }
}

STATIC void TTYputs(ECHAR *p)
{
    while (*p)
	TTYput(*p++);
}

STATIC void TTYshow(ECHAR c)
{
    if (c == DEL) {
	TTYput('^');
	TTYput('?');
    }
    else if (ISCTL(c)) {
	TTYput('^');
	TTYput(UNCTL(c));
    }
    else if (rl_meta_chars && ISMETA(c)) {
	TTYput('M');
	TTYput('-');
	TTYput(UNMETA(c));
    }
    else
	TTYput(c);
}

STATIC void TTYstring(ECHAR *p)
{
    while (*p)
	TTYshow(*p++);
}

#if 0
/* Old one line version */
#define TTYback()	(backspace ? TTYputs((ECHAR *)backspace) : TTYput('\b'))
#endif

STATIC int printlen(CONST char *p)
{
    int len = 0;

    for (len=0; *p; p++)
	if ((*p == DEL) || (ISCTL(*p)))
	    len += 2;
	else if (rl_meta_chars && ISMETA(*p))
	    len += 3;
	else
	    len += 1;

    return len;
}

STATIC int screen_pos()
{
    /* Returns the number of characters printed from beginning of line  */
    /* includes the size of the prompt and and meta/ctl char expansions */
    int p = strlen(Prompt);
    int i;
    
    for (i=0; i < Point; i++)
	if ((Line[i] == DEL) ||
	    (ISCTL(Line[i])))
	    p += 2;
	else if (rl_meta_chars && ISMETA(Line[i]))
	    p += 3;
	else
	    p += 1;

    return p;
}

STATIC void TTYback()
{
    /* awb: added upline (if supported) when back goes over line boundary */
    int i;
    int sp = screen_pos();

    if (upline && sp && (sp%TTYwidth == 0))
    {   /* move up a line and move to the end */
	TTYputs(upline);
	TTYputs(bol);
	for (i=0; i < TTYwidth; i++)
	    TTYputs(move_right);
    }
    else if (backspace)
	TTYputs((ECHAR *)backspace);
    else
	TTYput('\b');
}

STATIC void TTYinfo()
{
    static int		init;
#if	defined(USE_TERMCAP)
    char		*term;
    char		*buff;
    char		*buff2;
    char		*bp;
#endif	/* defined(USE_TERMCAP) */
#if	defined(TIOCGWINSZ)
    struct winsize	W;
#endif	/* defined(TIOCGWINSZ) */

    if (init) {
#if	defined(TIOCGWINSZ)
	/* Perhaps we got resized. */
	if (ioctl(0, TIOCGWINSZ, &W) >= 0
	 && W.ws_col > 0 && W.ws_row > 0) {
	    TTYwidth = (int)W.ws_col;
	    TTYrows = (int)W.ws_row;
	}
#endif	/* defined(TIOCGWINSZ) */
	return;
    }
    init++;

    TTYwidth = TTYrows = 0;
#if	defined(USE_TERMCAP)
    buff = walloc(char,2048);
    buff2 = walloc(char,2048);
    bp = &buff2[0];
    if ((term = getenv("TERM")) == NULL)
	term = "dumb";
    if (tgetent(buff, term) < 0) {
       TTYwidth = SCREEN_WIDTH;
       TTYrows = SCREEN_ROWS;
       return;
    }
    backspace = (ECHAR *)tgetstr("le", &bp);
    upline = (ECHAR *)tgetstr("up", &bp);
    clrpage = (ECHAR *)tgetstr("cl", &bp);
    nextline = (ECHAR *)tgetstr("nl", &bp);
    if (nextline==NULL)
      nextline = (ECHAR *)"\n";
    if (strncmp(term, "pcansi", 6)==0 || strncmp(term, "cygwin", 6)==0)
    {
	bol = (ECHAR *)"\033[0G";
	RequireNLforWrap = 0; /* doesn't require nl to get to next line */
    }
    else
	bol = (ECHAR *)tgetstr("cr", &bp);
    if (bol==NULL)
      bol = (ECHAR *)"\r";

    newline= walloc(ECHAR, 20);
    strcpy((char *)newline,(char *)bol);
    strcat((char *)newline,(char *)nextline);

    downline = (ECHAR *)newline;
    move_right = (ECHAR *)tgetstr("nd", &bp);
    if (!move_right || !downline) 
	upline = NULL;  /* terminal doesn't support enough so fall back */
    TTYwidth = tgetnum("co");
    TTYrows = tgetnum("li");
#endif	/* defined(USE_TERMCAP) */

#if	defined(TIOCGWINSZ)
    if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
	TTYwidth = (int)W.ws_col;
	TTYrows = (int)W.ws_row;
    }
#endif	/* defined(TIOCGWINSZ) */

    if (TTYwidth <= 0 || TTYrows <= 0) {
	TTYwidth = SCREEN_WIDTH;
	TTYrows = SCREEN_ROWS;
    }
}


/*
**  Print an array of words in columns.
*/
STATIC void print_columns(int ac, char **av)
{
    ECHAR	*p;
    int		i,c;
    int		j;
    int		k;
    int		len;
    int		skip;
    int		longest;
    int		cols;
    char info1[1024];

    if (ac > 99)
    {
	TTYputs((ECHAR *)newline);
	sprintf(info1,"There are %d possibilities.  Do you really \n",ac);
	TTYputs((ECHAR *)info1);
	TTYputs((ECHAR *)"want to see them all (y/n) ? ");
	while (((c = TTYget()) != EOF) && ((strchr("YyNn ",c) == NULL)))
	    ring_bell();
	if (strchr("Nn",c) != NULL)
	{
	    TTYputs((ECHAR *)newline);
	    return;
	}
    }

    /* Find longest name, determine column count from that. */
    for (longest = 0, i = 0; i < ac; i++)
	if ((j = strlen((char *)av[i])) > longest)
	    longest = j;
    cols = TTYwidth / (longest + 3);
    if (cols < 1) cols = 1;

    TTYputs((ECHAR *)newline);
    for (skip = ac / cols + 1, i = 0; i < skip; i++) {
	for (j = i; j < ac; j += skip) {
	    for (p = (ECHAR *)av[j], len = strlen((char *)p), k = len; 
		 --k >= 0; p++)
		TTYput(*p);
	    if (j + skip < ac)
		while (++len < longest + 3)
		    TTYput(' ');
	}
	TTYputs((ECHAR *)newline);
    }
}

STATIC void reposition(int reset)
{
    int		i,PPoint;
    int pos;
    char ppp[2];

    if (reset)
    {
	TTYputs(bol);
	for (i=screen_pos()/TTYwidth; i > 0; i--)
	    if (upline) TTYputs(upline);
    }
    TTYputs((ECHAR *)Prompt);
    pos = printlen(Prompt);
    ppp[1] = '\0';
    for (i = 0; i < End; i++)
    {
	ppp[0] = Line[i];
	TTYshow(Line[i]);
	pos += printlen(ppp);
	if ((pos%TTYwidth) == 0)
	    if (RequireNLforWrap && downline) TTYputs(downline);
    }
    PPoint = Point;
    for (Point = End; 
	 Point > PPoint; 
	 Point--)
    {
	if (rl_meta_chars && ISMETA(Line[Point]))
	{
	    TTYback();
	    TTYback();
	}
	else if (ISCTL(Line[Point]))
	    TTYback();
	TTYback();
    }
    Point = PPoint;
}

STATIC void left(STATUS Change)
{
    TTYback();
    if (Point) {
	if (ISCTL(Line[Point - 1]))
	    TTYback();
        else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
	    TTYback();
	    TTYback();
	}
    }
    if (Change == CSmove)
	Point--;
}

STATIC void right(STATUS Change)
{
    TTYshow(Line[Point]);
    if (Change == CSmove)
	Point++;
    if ((screen_pos())%TTYwidth == 0)
	if (downline && RequireNLforWrap) TTYputs(downline);    
}

STATIC STATUS ring_bell()
{
    TTYput('\07');
    TTYflush();
    return CSstay;
}

#if 0 
STATIC STATUS do_macro(unsigned int c)
{
    ECHAR		name[4];

    name[0] = '_';
    name[1] = c;
    name[2] = '_';
    name[3] = '\0';

    if ((el_Input = (ECHAR *)getenv((char *)name)) == NULL) {
	el_Input = el_NIL;
	return ring_bell();
    }
    return CSstay;
}
#endif

STATIC STATUS do_forward(STATUS move)
{
    int		i;
    ECHAR	*p;
    (void) move;

    i = 0;
    do {
	p = &Line[Point];
	for ( ; Point < End && (*p == ' ' || !isalnum(*p)); p++)
	    right(CSmove);

	for (; Point < End && isalnum(*p); p++)
	    right(CSmove);

	if (Point == End)
	    break;
    } while (++i < Repeat);

    return CSstay;
}

STATIC STATUS do_case(ECHAR type)
{
    int		i;
    int		end;
    int		count;
    ECHAR	*p;
    int OP;

    OP = Point;
    (void)do_forward(CSstay);
    if (OP != Point) {
	if ((count = Point - OP) < 0)
	    count = -count;
	for ( ; Point > OP; Point --)
	    TTYback();
	if ((end = Point + count) > End)
	    end = End;
	for (i = Point, p = &Line[Point]; Point < end; p++) {
	    if ((type == TOupper) ||
		((type == TOcapitalize) && (Point == i)))
	    {
		if (islower(*p))
		    *p = toupper(*p);
	    }
	    else if (isupper(*p))
		*p = tolower(*p);
	    right(CSmove);
	}
    }
    return CSstay;
}

STATIC STATUS case_down_word()
{
    return do_case(TOlower);
}

STATIC STATUS case_up_word()
{
    return do_case(TOupper);
}

STATIC STATUS case_cap_word()
{
    return do_case(TOcapitalize);
}

STATIC void ceol()
{
    int		extras;
    int		i, PPoint;
    ECHAR	*p;

    PPoint = Point;
    for (extras = 0, i = Point, p = &Line[i]; i < End; i++, p++) {
	Point++;
	TTYput(' ');
	if (ISCTL(*p)) {
	    TTYput(' ');
	    extras++;
	}
	else if (rl_meta_chars && ISMETA(*p)) {
	    TTYput(' ');
	    TTYput(' ');
	    extras += 2;
	}
	else if ((screen_pos())%TTYwidth == 0)
	    if (downline && RequireNLforWrap) TTYputs(downline);
    }

    Point = End;
    for (Point = End; 
	 Point > PPoint; 
	 Point--)
    {
	if (rl_meta_chars && ISMETA(Line[Point-1]))
	{
	    TTYback();
	    TTYback();
	}
	else if (ISCTL(Line[Point-1]))
	    TTYback();
	TTYback();
    }
    Point = PPoint;

}

STATIC void clear_line()
{
    int i;
    TTYputs(bol);
    for (i=screen_pos()/TTYwidth; i > 0; i--)
	if (upline) TTYputs(upline);
    for (i=0; i < strlen(Prompt); i++)
	TTYput(' ');
    Point = 0;
    ceol();
    TTYputs(bol);
    /* In case the prompt is more than one line long */
    for (i=screen_pos()/TTYwidth; i > 0; i--)
	if (upline) TTYputs(upline);
    Point = 0;
    End = 0;
    Line[0] = '\0';
}

STATIC STATUS insert_string(ECHAR *p)
{
    ESIZE_T	len;
    int		i,pos0,pos1;
    ECHAR	*new;
    ECHAR	*q;

    len = strlen((char *)p);
    if (End + len >= Length) {
	if ((new = NEW(ECHAR, Length + len + MEM_INC)) == NULL)
	    return CSstay;
	if (Length) {
	    COPYFROMTO(new, Line, Length);
	    DISPOSE(Line);
	}
	Line = new;
	Length += len + MEM_INC;
    }

    for (q = &Line[Point], i = End - Point; --i >= 0; )
	q[len + i] = q[i];
    COPYFROMTO(&Line[Point], p, len);
    End += len;
    Line[End] = '\0';
    pos0 = screen_pos();
    pos1 = printlen((char *)&Line[Point]);
    TTYstring(&Line[Point]);
    Point += len;
    if ((pos0+pos1)%TTYwidth == 0)
	if (downline && RequireNLforWrap) TTYputs(downline);
    /* if the line is longer than TTYwidth this may put the cursor   */
    /* on the next line and confuse some other parts, so put it back */ 
    /* at Point                                                      */
    if (upline && (Point != End))
    {
	pos0 = screen_pos();
	pos1 = printlen((char *)&Line[Point]);
	for (i=((pos0%TTYwidth)+pos1)/TTYwidth; i > 0; i--)
	    if (upline) TTYputs(upline);
	TTYputs(bol);
	for (i=0 ; i < (pos0%TTYwidth); i++)
	    TTYputs(move_right);
    }

    return Point == End ? CSstay : CSmove;
}


STATIC ECHAR *next_hist()
{
    return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
}

STATIC ECHAR *prev_hist()
{
    return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
}

STATIC STATUS do_insert_hist(ECHAR *p)
{
    int i;
    if (p == NULL)
	return ring_bell();
    for (i=screen_pos()/TTYwidth; i > 0; i--)
	if (upline) TTYputs(upline);
    Point = 0;
    reposition(1);
    ceol();
    End = 0;
    return insert_string(p);
}

STATIC STATUS do_hist(ECHAR *(*move)())
{
    ECHAR	*p;
    int		i;

    i = 0;
    do {
	if ((p = (*move)()) == NULL)
	    return ring_bell();
    } while (++i < Repeat);
    return do_insert_hist(p);
}

STATIC STATUS h_next()
{
    return do_hist(next_hist);
}

STATIC STATUS h_prev()
{
    return do_hist(prev_hist);
}

STATIC STATUS h_first()
{
    return do_insert_hist(H.Lines[H.Pos = 0]);
}

STATIC STATUS h_last()
{
    return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
}

/*
**  Return zero if pat appears as a substring in text.
*/
STATIC int substrcmp(char *text, char *pat, int len)
{
    ECHAR	c;

    if ((c = *pat) == '\0')
        return *text == '\0';
    for ( ; *text; text++)
        if (*text == c && strncmp(text, pat, len) == 0)
            return 0;
    return 1;
}

STATIC ECHAR *search_hist(ECHAR *search, ECHAR *(*move)())
{
    static ECHAR	*old_search;
    int		len;
    int		pos;
    int		(*match)();
    char	*pat;

    /* Save or get remembered search pattern. */
    if (search && *search) {
	if (old_search)
	    DISPOSE(old_search);
	old_search = (ECHAR *)STRDUP((const char *)search);
    }
    else {
	if (old_search == NULL || *old_search == '\0')
            return NULL;
	search = old_search;
    }

    /* Set up pattern-finder. */
    if (*search == '^') {
	match = strncmp;
	pat = (char *)(search + 1);
    }
    else {
	match = substrcmp;
	pat = (char *)search;
    }
    len = strlen(pat);

    for (pos = H.Pos; (*move)() != NULL; )
	if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
            return H.Lines[H.Pos];
    H.Pos = pos;
    return NULL;
}

STATIC STATUS h_search()
{
    static int	Searching;
    CONST char	*old_prompt;
    ECHAR	*(*move)();
    ECHAR	*p;

    if (Searching)
	return ring_bell();
    Searching = 1;

    clear_line();
    old_prompt = Prompt;
    Prompt = "Search: ";
    TTYputs((ECHAR *)Prompt);
    move = Repeat == NO_ARG ? prev_hist : next_hist;
    p = search_hist(editinput(), move);
    clear_line();
    Prompt = old_prompt;
    TTYputs((ECHAR *)Prompt);

    Searching = 0;
    return do_insert_hist(p);
}

STATIC STATUS fd_char()
{
    int		i;

    i = 0;
    do {
	if (Point >= End)
	    break;
	right(CSmove);
    } while (++i < Repeat);
    return CSstay;
}

STATIC void save_yank(int begin, int i)
{
    if (Yanked) {
	DISPOSE(Yanked);
	Yanked = NULL;
    }

    if (i < 1)
	return;

    if ((Yanked = NEW(ECHAR, (ESIZE_T)i + 1)) != NULL) {
	COPYFROMTO(Yanked, &Line[begin], i);
	Yanked[i] = '\0';
    }
}

STATIC STATUS delete_string(int count)
{
    int		i;
    int pos0,pos1,q;
    char	*tLine;

    if (count <= 0 || End == Point)
	return ring_bell();

    if (Point + count > End && (count = End - Point) <= 0)
	return CSstay;

    if (count > 1)
	save_yank(Point, count);

    tLine = STRDUP((char *)Line);
    ceol();
    for (q = Point, i = End - (Point + count) + 1; --i >= 0; q++)
	Line[q] = tLine[q+count];
    wfree(tLine);
    End -= count;
    pos0 = screen_pos();
    pos1 = printlen((char *)&Line[Point]);
    TTYstring(&Line[Point]);
    if ((pos1 > 0) && (pos0+pos1)%TTYwidth == 0)
	if (downline && RequireNLforWrap) TTYputs(downline);
    /* if the line is longer than TTYwidth this may put the cursor   */
    /* on the next line and confuse some other parts, so put it back */ 
    /* at Point                                                      */
    if (upline)
    {
	for (i=((pos0%TTYwidth)+pos1)/TTYwidth; i > 0; i--)
	    if (upline) TTYputs(upline);
	TTYputs(bol);
	for (i=0 ; i < (pos0%TTYwidth); i++)
	    TTYputs(move_right);
    }

    return CSmove;
}

STATIC STATUS bk_char()
{
    int		i;

    i = 0;
    do {
	if (Point == 0)
	    break;
	left(CSmove);
    } while (++i < Repeat);

    return CSstay;
}

STATIC STATUS bk_del_char()
{
    int		i;

    i = 0;
    do {
	if (Point == 0)
	    break;
	left(CSmove);
    } while (++i < Repeat);

    return delete_string(i);
}

STATIC STATUS redisplay()
{
    if (clrpage) TTYputs(clrpage);
    else
	TTYputs((ECHAR *)newline);
/*    TTYputs((ECHAR *)Prompt);
    TTYstring(Line); */
    return CSmove;
}

STATIC STATUS kill_line()
{
    int		i;

    if (Repeat != NO_ARG) {
	if (Repeat < Point) {
	    i = Point;
	    Point = Repeat;
	    reposition(1);
	    (void)delete_string(i - Point);
	}
	else if (Repeat > Point) {
	    right(CSmove);
	    (void)delete_string(Repeat - Point - 1);
	}
	return CSmove;
    }

    save_yank(Point, End - Point);
    ceol();
    Line[Point] = '\0';
    End = Point;
    return CSstay;
}

STATIC char *rsearch_hist(char *patt, int *lpos,int *cpos)
{
    /* Extension by awb to do reverse incremental searches */

    for (; *lpos > 0; (*lpos)--)
    {
	for ( ; (*cpos) >= 0 ; (*cpos)--)
	{
/*	    fprintf(stderr,"comparing %d %s %s\n",*lpos,patt,H.Lines[*lpos]+*cpos); */
	    if (strncmp(patt,(char *)H.Lines[*lpos]+*cpos,strlen(patt)) == 0)
	    {   /* found a match */
		return (char *)H.Lines[*lpos];
	    }
	}
	if ((*lpos) > 0)
	    *cpos = strlen((char *)H.Lines[(*lpos)-1]);
    }
    return NULL;  /* no match found */
}

STATIC STATUS h_risearch()
{
    STATUS	s;
    CONST char	*old_prompt;
    char *pat, *hist, *nhist;
    char *nprompt;
    int patend, i;
    ECHAR	c;
    int lpos,cpos;

    old_prompt = Prompt;

    nprompt = walloc(char,80+160);
    pat = walloc(char,80);
    patend=0;
    pat[0] = '\0';
    hist = "";
    lpos = H.Pos;   /* where the search has to start from */
    cpos = strlen((char *)H.Lines[lpos]);
    do 
    {
	sprintf(nprompt,"(reverse-i-search)`%s': ",pat);
	Prompt = nprompt;
	kill_line();
	do_insert_hist((ECHAR *)hist);
	if (patend != 0)
	    for (i=strlen((char *)H.Lines[lpos]); i>cpos; i--) bk_char();
	c = TTYget();
	if ((c >= ' ') || (c == CTL('R')))
	{
	    if (c == CTL('R'))
		cpos--;
	    else if (patend < 79)
	    {
		pat[patend]=c;
		patend++;
		pat[patend]='\0';
	    }
	    else  /* too long */
	    {
		ring_bell();
		continue;
	    }
	    nhist = rsearch_hist(pat,&lpos,&cpos);
	    if (nhist != NULL)
	    {
		hist = nhist;
		H.Pos = lpos;
	    }
	    else
	    {   /* oops, no match */
		ring_bell();
		if (c != CTL('R'))
		{
		    patend--;
		    pat[patend] = '\0';
		}
	    }
	}
    } while ((c >= ' ') || (c == CTL('R')));
    
    /* Tidy up */
    clear_line();
    Prompt = old_prompt;
    TTYputs((ECHAR *)Prompt);
    wfree(nprompt);

    kill_line();
    s = do_insert_hist((ECHAR *)hist);
    if (patend != 0)
	for (i=strlen((char *)H.Lines[lpos]); i>cpos; i--) s = bk_char();
    if (c != ESC)
	return emacs(c);
    else
	return s;
}

STATIC STATUS insert_char(int c)
{
    STATUS	s;
    ECHAR	buff[2];
    ECHAR	*p;
    ECHAR	*q;
    int		i;

    if (Repeat == NO_ARG || Repeat < 2) {
	buff[0] = c;
	buff[1] = '\0';
	return insert_string(buff);
    }

    if ((p = NEW(ECHAR, Repeat + 1)) == NULL)
	return CSstay;
    for (i = Repeat, q = p; --i >= 0; )
	*q++ = c;
    *q = '\0';
    Repeat = 0;
    s = insert_string(p);
    DISPOSE(p);
    return s;
}

STATIC STATUS meta()
{
    unsigned int	c;
    KEYMAP		*kp;

    if ((c = TTYget()) == EOF)
	return CSeof;
#if	defined(ANSI_ARROWS)
    /* Also include VT-100 arrows. */
    if (c == '[' || c == 'O')
	switch (c = TTYget()) {
	default:	return ring_bell();
	case EOF:	return CSeof;
	case 'A':	return h_prev();
	case 'B':	return h_next();
	case 'C':	return fd_char();
	case 'D':	return bk_char();
	}
#endif	/* defined(ANSI_ARROWS) */

    if (isdigit(c)) {
	for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
	    Repeat = Repeat * 10 + c - '0';
	el_Pushed = 1;
	el_PushBack = c;
	return CSstay;
    }

/*    if (isupper(c))
         return do_macro(c); */
    for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
	if (kp->Key == c)
	    return (*kp->Function)();
    if (rl_meta_chars == 0)
    {
	insert_char(META(c));
	return CSmove;
    }

    return ring_bell();
}

STATIC STATUS emacs(unsigned int c)
{
    STATUS		s;
    KEYMAP		*kp;

    if (ISMETA(c) && rl_meta_chars) 
    {    
	el_Pushed = 1;
	el_PushBack = UNMETA(c);
	return meta();
    }
    for (kp = Map; kp->Function; kp++)
	if (kp->Key == c)
	    break;
    s = kp->Function ? (*kp->Function)() : insert_char((int)c);
    if (!el_Pushed)
	/* No pushback means no repeat count; hacky, but true. */
	Repeat = NO_ARG;
    return s;
}

STATIC STATUS TTYspecial(unsigned int c)
{
    int i;
    
    if (ISMETA(c))
	return CSdispatch;

    if (c == rl_erase || c == DEL)
	return bk_del_char();
    if (c == rl_kill) {
	if (Point != 0) {
	    for (i=screen_pos()/TTYwidth; i > 0; i--)
		if (upline) TTYputs(upline);
	    Point = 0;
	    reposition(1);
	}
	Repeat = NO_ARG;
	return kill_line();
    }
    if (c == rl_intr || c == rl_quit) {
	Point = End = 0;
	Line[0] = '\0';
	if (c == rl_intr) 
	{
	    el_intr_pending = 1;
	    return CSdone;
	}
	else 
	    return redisplay();
    }
    if (c == rl_eof && Point == 0 && End == 0)
	return CSeof;

    return CSdispatch;
}

STATIC ECHAR *editinput()
{
    unsigned int	c;

    Repeat = NO_ARG;
    OldPoint = Point = Mark = End = 0;
    Line[0] = '\0';

    while ((c = TTYget()) != EOF)
      {
	switch (TTYspecial(c)) {
	case CSdone:
	    return Line;
	case CSeof:
	    return NULL;
	case CSmove:
	    reposition(1);
	    break;
	case CSstay:
	    break;
	case CSdispatch:
	    switch (emacs(c)) {
	    case CSdone:
		return Line;
	    case CSeof:
		return NULL;
	    case CSmove:
		reposition(1);
		break;
	    case CSstay:
	    case CSdispatch:
		break;
	    }
	    break;
	}
      }
    return NULL;
}

STATIC void hist_add(ECHAR *p)
{
    int		i;

    if ((p = (ECHAR *)STRDUP((char *)p)) == NULL)
	return;
    if (H.Size < editline_histsize)
	H.Lines[H.Size++] = p;
    else {
	DISPOSE(H.Lines[0]);
	for (i = 0; i < editline_histsize - 1; i++)
	    H.Lines[i] = H.Lines[i + 1];
	H.Lines[i] = p;
    }
    H.Pos = H.Size - 1;
}

/* Added by awb 29/12/98 to get saved history file */
void write_history(const char *history_file)
{
    FILE *fd;
    int i;

    if ((fd = fopen(history_file,"wb")) == NULL)
    {
	fprintf(stderr,"editline: can't access history file \"%s\"\n",
		history_file);
	return;
    }

    for (i=0; i < H.Size; i++)
	fprintf(fd,"%s\n",H.Lines[i]);
    fclose(fd);
}

void read_history(const char *history_file)
{
    FILE *fd;
    char buff[2048];
    int c,i;

    H.Lines = NEW(ECHAR *,editline_histsize);
    H.Size = 0;
    H.Pos = 0;
    
    if ((fd = fopen(history_file,"rb")) == NULL)
	return; /* doesn't have a history file yet */

    while ((c=getc(fd)) != EOF)
    {
	ungetc(c,fd);
	for (i=0; ((c=getc(fd)) != '\n') && (c != EOF); i++)
	    if (i < 2047)
		buff[i] = c;
	buff[i] = '\0';
	add_history(buff);
    }

    fclose(fd);
}

/*
**  For compatibility with FSF readline.
*/
/* ARGSUSED0 */
void
rl_reset_terminal(char *p)
{
}

void
rl_initialize()
{
}

char *readline(CONST char *prompt)
{
    ECHAR	*line;

    if (Line == NULL) {
	Length = MEM_INC;
	if ((Line = NEW(ECHAR, Length)) == NULL)
	    return NULL;
    }

    TTYinfo();
    rl_ttyset(0);
    hist_add(el_NIL);
    ScreenSize = SCREEN_INC;
    Screen = NEW(char, ScreenSize);
    Prompt = prompt ? prompt : (char *)el_NIL;
    el_intr_pending = 0;
    if (el_no_echo == 1)
    {
	el_no_echo = 0;
	TTYputs((ECHAR *)Prompt);
	TTYflush();
	el_no_echo = 1;
    }
    else
	TTYputs((ECHAR *)Prompt);
    line = editinput();
    if (line != NULL) {
	line = (ECHAR *)STRDUP((char *)line);
	TTYputs((ECHAR *)newline);
	TTYflush();
    }
    rl_ttyset(1);
    DISPOSE(Screen);
    DISPOSE(H.Lines[--H.Size]);
    if (el_intr_pending)
	do_user_intr();
    return (char *)line;
}

void
add_history(p)
    char	*p;
{
    if (p == NULL || *p == '\0')
	return;

#if	defined(UNIQUE_HISTORY)
    if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
        return;
#endif	/* defined(UNIQUE_HISTORY) */
    hist_add((ECHAR *)p);
}


STATIC STATUS beg_line()
{
    int i;
    if (Point) {
	for (i=screen_pos()/TTYwidth; i > 0; i--)
	    if (upline) TTYputs(upline);
	Point = 0;
	return CSmove;
    }
    return CSstay;
}

STATIC STATUS del_char()
{
    return delete_string(Repeat == NO_ARG ? 1 : Repeat);
}

STATIC STATUS end_line()
{
    if (Point != End) {
	while (Point < End)
	{
	    TTYput(Line[Point]);
	    Point++;
	}
	return CSmove;
    }
    return CSstay;
}

/*
**  Move back to the beginning of the current word and return an
**  allocated copy of it.
*/
STATIC ECHAR *find_word()
{
    static char	SEPS[] = "#;&|^$=`'{}()<>\n\t ";
    ECHAR	*p;
    ECHAR	*new;
    ESIZE_T	len;

    for (p = &Line[Point]; p > Line && strchr(SEPS, (char)p[-1]) == NULL; p--)
	continue;
    len = Point - (p - Line) + 1;
    if ((new = NEW(ECHAR, len)) == NULL)
	return NULL;
    COPYFROMTO(new, p, len);
    new[len - 1] = '\0';
    return new;
}

void el_redisplay()
{
    reposition(0);  /* redisplay assuming already on newline */
}

char *el_current_sym()
{
    /* Get current symbol at point -- awb*/
    char *symbol = NULL;
    int i,j;

    if (End == 0)
	return NULL;
    if (Point == End)
	i=Point-1;
    else
	i=Point;
	
    for ( ;
	 ((i >= 0) &&
	  (strchr("()' \t\n\r",Line[i]) != NULL));
	 i--);
    /* i will be on final or before final character */
    if (i < 0)
	return NULL;
    /* But if its not at the end of the current symbol move it there */
    for (; i < End; i++)
	if (strchr("()' \t\n\r\"",Line[i]) != NULL)
	    break;
    for (j=i-1; j >=0; j--)
	if (strchr("()' \t\n\r\"",Line[j]) != NULL)
	    break;

    symbol = walloc(char,i-j);
    strncpy(symbol,(char *)&Line[j+1],i-(j+1));
    symbol[i-(j+1)] = '\0';

    return symbol;
}

static char *completion_to_ambiguity(int index,char **possibles)
{
    /* Find the string that extends from index in possibles until an */
    /* ambiguity is found                           -- awb     */
    char *p;
    int e,i;
    int extending;

    extending = 1;
    e = index;

    for ( ; extending; e++)
    {
	for (i=0; possibles[i] != NULL; i++)
	    if (possibles[i][e] != possibles[0][e])
	    {
		extending = 0;
		e--;
		break;
	    }
    }

    if (e==index)
	return NULL;  /* already at ambiguity */
    else
    {
	p = walloc(char,(e-index)+1);
 	strncpy(p,possibles[0]+index,e-index);
	p[e-index] = '\0';
	return p;
    }
}

static char **el_file_completion_function(char * text, int start, int end)
{
    /* Interface to editline rl_list_possib which looks up possible */
    /* file name completions.                                       */
    char *word;
    char **matches1;
    char **matches2;
    int ac,i;

    word = walloc(char,(end-start)+1);
    strncpy(word,text+start,end-start);
    word[end-start]='\0';

    ac = rl_list_possib(word,&matches1);
    wfree(word);
    if (ac == 0)
	return NULL;
    else
    {
	matches2 = walloc(char *,ac+1);
	for (i=0; i < ac; i++)
	    matches2[i] = matches1[i];
	matches2[i] = NULL;
	wfree(matches1);
	return matches2;
    }
}

STATIC STATUS c_complete()
{
    /* Modified by awb 30/12/98 to allow listing of possibles and */
    /* a user definable completion method                         */
    char	*p;
    char	*word;
    int		start;
    char        **possibles=NULL;
    int         possiblesc=0;
    int started_with_quote = 0;
    STATUS	s;
    int i;

    for (start=Point; start > 0; start--)
	if (strchr("()' \t\n\r\"",Line[start-1]) != NULL)
	    break;
    word = walloc(char,(Point-start)+1);
    strncpy(word,(char *)(Line+start),Point-start);
    word[Point-start]='\0';
    if ((start > 0) && (Line[start-1] == '"'))
	started_with_quote = 1;

    if (el_user_completion_function)
	/* May need to look at previous char so pass in Line */
	possibles = el_user_completion_function((char *)Line,start,Point);
    if (possibles == NULL)
    {
	possibles = el_file_completion_function((char *)Line,start,Point);
	/* As filename completions only complete the final file name */
	/* not the full path we need to set a new start position     */
	for (start=Point; start > 0; start--)
	    if (strchr("()' \t\n\r\"/",Line[start-1]) != NULL)
		break;
    }
    if (possibles)
	for (possiblesc=0; possibles[possiblesc] != NULL; possiblesc++);
    
    if ((!possibles) || (possiblesc == 0)) /* none or none at all */
	s = ring_bell();
    else if (possiblesc == 1)  /* a single expansion */
    {
	p = walloc(char,strlen(possibles[0])-(Point-start)+2);
	sprintf(p,"%s ",possibles[0]+(Point-start));
	if ((strlen(p) > 1) && (p[strlen(p)-2] == '/'))
	    p[strlen(p)-1] = '\0';
	else if (started_with_quote)
	    p[strlen(p)-1] = '"';
	
	s = insert_string((ECHAR *)p);
	wfree(p);
    }
    else if ((p = completion_to_ambiguity(Point-start,possibles)) != NULL)
    {    /* an expansion to a later ambiguity */
	s = insert_string((ECHAR *)p);
	wfree(p);
	ring_bell();
    }
    else /* list of possibilities and we can't expand any further */
    {
	print_columns(possiblesc,possibles);  /* display options */
	reposition(0);  /* display whole line again */
	s = CSmove;
    }

    for (i=0; possibles && possibles[i] != NULL; i++)
	wfree(possibles[i]);
    wfree(possibles);
    wfree(word);

    return s;
}

#if 0
/* Original version without automatic listing of possible completions */
STATIC STATUS c_complete_old()
{
    ECHAR	*p;
    ECHAR	*word;
    int		unique;
    STATUS	s;

    word = find_word();
    p = (ECHAR *)rl_complete((char *)word, &unique);
    if (word)
	DISPOSE(word);
    if (p && *p) {
	s = insert_string(p);
	if (!unique)
	    (void)ring_bell();
	DISPOSE(p);
	return s;
    }
    return ring_bell();
}
#endif

STATIC STATUS c_possible()
{
    ECHAR	**av;
    ECHAR	*word;
    int		ac;

    word = find_word();
    /* The (char ***) ((void *) &av) below is to avoid a warning
     * from GCC about casting an unsigned char *** to char ***
     */
    ac = rl_list_possib((char *)word, (char ***) ((void *) &av));
    if (word)
	DISPOSE(word);
    if (ac) {
	print_columns(ac, (char **)av);
	reposition(0);
	while (--ac >= 0)
	    DISPOSE(av[ac]);
	DISPOSE(av);
	return CSmove;
    }
    return ring_bell();
}

STATIC STATUS accept_line()
{
    Line[End] = '\0';
    return CSdone;
}

#ifdef SYSTEM_IS_WIN32
STATIC STATUS end_of_input()
{
    Line[End] = '\0';
    return CSeof;
}
#endif

STATIC STATUS transpose()
{
    ECHAR	c;

    if (Point) {
	if (Point == End)
	    left(CSmove);
	c = Line[Point - 1];
	left(CSstay);
	Line[Point - 1] = Line[Point];
	TTYshow(Line[Point - 1]);
	Line[Point++] = c;
	TTYshow(c);
    }
    return CSstay;
}

STATIC STATUS quote()
{
    unsigned int	c;

    return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
}

STATIC STATUS wipe()
{
    int		i;

    if (Mark > End)
	return ring_bell();

    if (Point > Mark) {
	i = Point;
	Point = Mark;
	Mark = i;
	reposition(1);
    }

    return delete_string(Mark - Point);
}

STATIC STATUS mk_set()
{
    Mark = Point;
    return CSstay;
}

STATIC STATUS exchange()
{
    unsigned int	c;

    if ((c = TTYget()) != CTL('X'))
	return c == EOF ? CSeof : ring_bell();

    if ((c = Mark) <= End) {
	Mark = Point;
	Point = c;
	return CSmove;
    }
    return CSstay;
}

STATIC STATUS yank()
{
    if (Yanked && *Yanked)
	return insert_string(Yanked);
    return CSstay;
}

STATIC STATUS copy_region()
{
    if (Mark > End)
	return ring_bell();

    if (Point > Mark)
	save_yank(Mark, Point - Mark);
    else
	save_yank(Point, Mark - Point);

    return CSstay;
}

STATIC STATUS move_to_char()
{
    unsigned int	c;
    int			i;
    ECHAR		*p;

    if ((c = TTYget()) == EOF)
	return CSeof;
    for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
	if (*p == c) {
	    Point = i;
	    return CSmove;
	}
    return CSstay;
}

STATIC STATUS fd_word()
{
    return do_forward(CSmove);
}

STATIC STATUS fd_kill_word()
{
    int		i;
    int OP;

    OP = Point;
    (void)do_forward(CSmove);
    if (OP != Point) {
	i = Point - OP;
	for ( ; Point > OP; Point --)
	    TTYback();
	return delete_string(i);
    }
    return CSmove;
}

STATIC STATUS bk_word()
{
    int		i;
    ECHAR	*p;

    i = 0;
    do {
	for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
	    left(CSmove);

	for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
	    left(CSmove);

	if (Point == 0)
	    break;
    } while (++i < Repeat);

    return CSstay;
}

STATIC STATUS bk_kill_word()
{
    (void)bk_word();
    if (OldPoint != Point)
	return delete_string(OldPoint - Point);
    return CSstay;
}

STATIC int argify(ECHAR *line, ECHAR ***avp)
{
    ECHAR	*c;
    ECHAR	**p;
    ECHAR	**new;
    int		ac;
    int		i;

    i = MEM_INC;
    if ((*avp = p = NEW(ECHAR*, i))== NULL)
	 return 0;

    for (c = line; isspace(*c); c++)
	continue;
    if (*c == '\n' || *c == '\0')
	return 0;

    for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
	if (isspace(*c)) {
	    *c++ = '\0';
	    if (*c && *c != '\n') {
		if (ac + 1 == i) {
		    new = NEW(ECHAR*, i + MEM_INC);
		    if (new == NULL) {
			p[ac] = NULL;
			return ac;
		    }
		    COPYFROMTO(new, p, i * sizeof (char **));
		    i += MEM_INC;
		    DISPOSE(p);
		    *avp = p = new;
		}
		p[ac++] = c;
	    }
	}
	else
	    c++;
    }
    *c = '\0';
    p[ac] = NULL;
    return ac;
}

STATIC STATUS last_argument()
{
    ECHAR	**av;
    ECHAR	*p;
    STATUS	s;
    int		ac;

    if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
	return ring_bell();

    if ((p = (ECHAR *)STRDUP((char *)p)) == NULL)
	return CSstay;
    ac = argify(p, &av);

    if (Repeat != NO_ARG)
	s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
    else
	s = ac ? insert_string(av[ac - 1]) : CSstay;

    if (ac)
	DISPOSE(av);
    DISPOSE(p);
    return s;
}

STATIC KEYMAP	Map[33] = {
    {	CTL('@'),	ring_bell	},
    {	CTL('A'),	beg_line	},
    {	CTL('B'),	bk_char		},
    {	CTL('D'),	del_char	},
    {	CTL('E'),	end_line	},
    {	CTL('F'),	fd_char		},
    {	CTL('G'),	ring_bell	},
    {	CTL('H'),	bk_del_char	},
    {	CTL('I'),	c_complete	},
    {	CTL('J'),	accept_line	},
    {	CTL('K'),	kill_line	},
    {	CTL('L'),	redisplay	},
    {	CTL('M'),	accept_line	},
    {	CTL('N'),	h_next		},
    {	CTL('O'),	ring_bell	},
    {	CTL('P'),	h_prev		},
    {	CTL('Q'),	ring_bell	},
    {	CTL('R'),	h_risearch	},
    {	CTL('S'),	h_search	},
    {	CTL('T'),	transpose	},
    {	CTL('U'),	ring_bell	},
    {	CTL('V'),	quote		},
    {	CTL('W'),	wipe		},
    {	CTL('X'),	exchange	},
    {	CTL('Y'),	yank		},
#ifdef SYSTEM_IS_WIN32
    {	CTL('Z'),	end_of_input	},
#else
    {	CTL('Z'),	ring_bell	},
#endif
    {	CTL('['),	meta		},
    {	CTL(']'),	move_to_char	},
    {	CTL('^'),	ring_bell	},
    {	CTL('_'),	ring_bell	},
    {	0,		NULL		}
};

STATIC KEYMAP	MetaMap[64]= {
    {	CTL('H'),	bk_kill_word	},
    {	DEL,		bk_kill_word	},
    {	' ',		mk_set	},
    {	'.',		last_argument	},
    {	'<',		h_first		},
    {	'>',		h_last		},
    {	'?',		c_possible	},
    {	'b',		bk_word		},
    {	'c',		case_cap_word	},
    {	'd',		fd_kill_word	},
    {	'f',		fd_word		},
    {	'l',		case_down_word	},
    {	'u',		case_up_word	},
    {	'y',		yank		},
    {	'w',		copy_region	},
    {	0,		NULL		}
};

void el_bind_key_in_metamap(char c, Keymap_Function func)
{
    /* Add given function to key map for META keys */
    int i;

    for (i=0; MetaMap[i].Key != 0; i++)
    {
	if (MetaMap[i].Key == c)
	{
	    MetaMap[i].Function = func;
	    return;
	}
    }

    /* A new key so have to add it to end */
    if (i == 63)
    {
	fprintf(stderr,"editline: MetaMap table full, requires increase\n");
	return;
    }
    
    MetaMap[i].Function = func;
    MetaMap[i].Key = c;
    MetaMap[i+1].Function = 0;  /* Zero the last location */
    MetaMap[i+1].Key = 0;       /* Zero the last location */

}