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
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2017, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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 OWNER 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.
*/
#include <string.h>
#include <stdlib.h>
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <config.h>
#include <signal.h>
#include <assert.h>
#include <histedit.h>
#include <errno.h>
#include <limits.h>
#if defined(HAVE_POLL_H) && defined(HAVE_POLL)
#include <poll.h>
#elif defined(HAVE_SYS_SELECT_H)
#include <sys/select.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_UXNT_H
#include <uxnt.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
static atom_t ATOM_norm;
static atom_t ATOM_newline;
static atom_t ATOM_eof;
static atom_t ATOM_arghack;
static atom_t ATOM_refresh;
static atom_t ATOM_refresh_beep;
static atom_t ATOM_cursor;
static atom_t ATOM_redisplay;
static atom_t ATOM_error;
static atom_t ATOM_fatal;
static atom_t ATOM_clear;
static atom_t ATOM_setsize;
static atom_t ATOM_setunique;
static functor_t FUNCTOR_line2;
static functor_t FUNCTOR_electric3;
static functor_t FUNCTOR_pair2;
#define STR_OPTIONS (CVT_ATOM|CVT_STRING|CVT_LIST|REP_MB|CVT_EXCEPTION)
/*******************************
* CONTEXT *
*******************************/
typedef struct command
{ struct command *next;
atom_t name; /* name of the command */
record_t closure; /* called goal */
module_t module; /* target module */
} command;
typedef struct binding
{ struct binding *next;
int ch;
command *command;
} binding;
typedef enum electric_state
{ E_NONE = 0, /* idle state */
E_WAIT, /* Wait for timeout */
E_COMMAND /* get second char */
} electric_state;
#define EL_CTX_MAGIC 1329760607
typedef struct el_context
{ struct el_context *next; /* Next in list */
int magic; /* EL_CTX_MAGIC */
int fd; /* Input file we are attached to */
IOSTREAM *istream; /* Input stream */
IOSTREAM *ostream; /* Output stream */
IOSTREAM *estream; /* Error stream */
EditLine *el; /* EditLine context */
char * buffered; /* Buffered long line */
int sig_no; /* For read_char() */
HistEvent ev; /* History event */
History *history; /* Complete history */
char *prompt; /* Current prompt */
IOFUNCTIONS *orig_functions; /* Original functions */
IOFUNCTIONS functions; /* SIO function block */
command *commands; /* User commands */
binding *bindings; /* Bindings to user commands */
int reader; /* Current reader thread */
struct
{ int timeout; /* Time to wait */
int move; /* Amount to move */
electric_state state;
} electric;
#ifndef HAVE_EL_CURSOR
int move_cursor; /* Amount to move the cursor */
#endif
} el_context;
static el_context *el_clist;
static el_context *
get_context(int fd)
{ el_context *c;
for(c=el_clist; c; c=c->next)
{ if ( c->fd == fd )
return c;
}
return NULL;
}
static el_context *
get_context_from_handle(void *handle)
{ el_context *c;
for(c=el_clist; c; c=c->next)
{ IOSTREAM *s;
if ( (s=c->istream) && s->handle == handle )
return c;
}
return NULL;
}
static el_context *
get_context_from_ohandle(void *handle)
{ el_context *c;
for(c=el_clist; c; c=c->next)
{ IOSTREAM *s;
if ( (s=c->ostream) && s->handle == handle )
return c;
if ( (s=c->estream) && s->handle == handle )
return c;
}
return NULL;
}
static el_context *
alloc_context(int fd)
{ el_context *c = PL_malloc(sizeof(*c));
memset(c, 0, sizeof(*c));
c->fd = fd;
c->magic = EL_CTX_MAGIC;
c->next = el_clist;
el_clist = c;
return c;
}
static void
update_prompt(el_context *ctx)
{ char *np = PL_prompt_string(ctx->fd);
if ( ctx->prompt && np && strcmp(np, ctx->prompt) == 0 )
return;
if ( ctx->prompt )
free(ctx->prompt);
ctx->prompt = np ? strdup(np) : NULL;
}
/*******************************
* PORT *
*******************************/
#ifndef HAVE_EL_CURSOR
#define el_cursor(el, cnt) el_cursor_emulated(el, cnt)
static int
el_cursor_emulated(EditLine *el, int count)
{ el_context *ctx;
const LineInfo *li;
el_get(el, EL_CLIENTDATA, (void**)&ctx);
li = el_line(ctx->el);
if ( count < 0 ) /* backward */
{ if ( -count > li->cursor - li->buffer )
count = li->buffer - li->cursor;
} else
{ if ( count > li->lastchar - li->cursor )
count = li->lastchar - li->cursor;
}
ctx->move_cursor = count;
return li->cursor - li->buffer + count;
}
#endif
/*******************************
* SIGNAL HANDLING *
*******************************/
#ifdef O_SIGNALS
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This code is copied from our GNU libreadline wrapper.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
typedef struct
{ int signo; /* number of the signal */
int prepared; /* Is currently prepared */
struct sigaction old_state; /* old state for the signal */
} sigstate;
static void el_sighandler(int sig);
static sigstate el_signals[] =
{ { SIGINT },
#ifdef SIGTSTP
{ SIGTSTP },
{ SIGCONT },
{ SIGTTOU },
{ SIGTTIN },
#endif
{ SIGALRM },
{ SIGTERM },
{ SIGQUIT },
{ SIGWINCH },
{ SIGUSR2 }, /* SWI-Prolog thread alert */
{ -1 }
};
static sigstate cont_signals[] =
{
#ifdef SIGCONT
{ SIGCONT },
#endif
{ -1 }
};
static void
prepare_signals(sigstate *s)
{ for(; s->signo != -1; s++)
{ if ( !s->prepared )
{ struct sigaction new;
memset(&new, 0, sizeof(new));
new.sa_handler = el_sighandler;
sigaction(s->signo, &new, &s->old_state);
s->prepared = TRUE;
}
}
}
static void
restore_signals(sigstate *s)
{ for(; s->signo != -1; s++)
{ sigaction(s->signo, &s->old_state, NULL);
s->prepared = FALSE;
}
}
static void
el_sighandler(int sig)
{ sigstate *s;
el_context *ctx;
for(ctx=el_clist; ctx; ctx=ctx->next)
ctx->sig_no = sig;
switch(sig)
{ case SIGWINCH:
return;
case SIGCONT:
if ( (ctx = get_context(0)) )
el_set(ctx->el, EL_PREP_TERM, 1);
restore_signals(cont_signals);
prepare_signals(el_signals);
return;
case SIGTSTP:
restore_signals(el_signals);
prepare_signals(cont_signals);
if ( (ctx = get_context(0)) )
el_set(ctx->el, EL_PREP_TERM, 0);
kill(getpid(), sig);
return;
case SIGINT:
{ if ( (ctx = get_context(0)) )
{ FILE *err;
int size = el_cursor(ctx->el, 10000);
el_deletestr(ctx->el, size);
el_get(ctx->el, EL_GETFP, 2, &err);
fprintf(err, "^C\n");
}
}
}
restore_signals(el_signals);
if ( (ctx = get_context(0)) )
el_set(ctx->el, EL_PREP_TERM, 0);
for(s=el_signals; s->signo != -1; s++)
{ if ( s->signo == sig )
{ void (*func)(int) = s->old_state.sa_handler;
if ( func == SIG_DFL )
{ PL_raise(sig); /* was: kill(getpid(), sig); */
} else if ( func != SIG_IGN )
{ (*func)(sig);
}
break;
}
}
if ( (ctx = get_context(0)) )
el_set(ctx->el, EL_PREP_TERM, 1);
prepare_signals(el_signals);
}
static const char *
el_siggets(EditLine *el, int *count)
{ const char *line;
FILE *in;
el_get(el, EL_GETFP, 0, &in);
if ( fileno(in) == 0 )
{ prepare_signals(el_signals);
line = el_gets(el, count);
restore_signals(el_signals);
} else
{ line = el_gets(el, count);
}
return line;
}
#else /* O_SIGNALS */
#define el_siggets(el, count) el_gets(el, count)
#endif /* O_SIGNALS */
/*******************************
* ELECTRIC CARET SUPPORT *
*******************************/
static unsigned char
electric_end(EditLine *el, int ch)
{ el_context *ctx;
el_get(el, EL_CLIENTDATA, (void**)&ctx);
el_cursor(el, ctx->electric.move);
ctx->electric.move = 0;
return CC_CURSOR;
}
static void
electric_init(EditLine *el)
{ el_set(el, EL_ADDFN, "electric-end", "Restore electric caret", electric_end);
el_set(el, EL_BIND, "^[^A", "electric-end", NULL);
}
/*******************************
* LOW-LEVEL READ *
*******************************/
static int
wait_on_fd(int fd, int timeout) /* milliseconds */
{
#ifdef HAVE_POLL
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
return poll(fds, 1, timeout) != 0;
#else
fd_set rfds;
struct timeval tv;
#if defined(FD_SETSIZE) && !defined(__WINDOWS__)
if ( fd >= FD_SETSIZE )
{ Sdprintf("input_on_fd(%d) > FD_SETSIZE\n", fd);
return 1;
}
#endif
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = timeout*1000;
return select(fd+1, &rfds, NULL, NULL, &tv) != 0;
#endif
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Copied from read.c from the NetBSD sources for libedit. We need to redo
this to deal with our signal handling and to include event dispatching.
We could have done this without copying if we could get a pointer to the
default read function, but el_get() using EL_GETCFN returns
EL_BUILTIN_GETCFN, which is simple defined as NULL.
*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Christos Zoulas of Cornell University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
read__fixio(int fd, int e)
{ (void)fd;
switch (e)
{ case -1: /* Make sure that the code is reachable */
#ifdef EWOULDBLOCK
case EWOULDBLOCK:
#ifndef TRY_AGAIN
#define TRY_AGAIN
#endif
#endif /* EWOULDBLOCK */
#if defined(POSIX) && defined(EAGAIN)
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EAGAIN:
#ifndef TRY_AGAIN
#define TRY_AGAIN
#endif
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
#endif /* POSIX && EAGAIN */
e = 0;
#ifdef TRY_AGAIN
#if defined(F_SETFL) && defined(O_NDELAY)
if ( (e = fcntl(fd, F_GETFL, 0) ) == -1)
return -1;
if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
return -1;
else
e = 1;
#endif /* F_SETFL && O_NDELAY */
#ifdef FIONBIO
{ int zero = 0;
if (ioctl(fd, FIONBIO, &zero) == -1)
return -1;
else
e = 1;
}
#endif /* FIONBIO */
#endif /* TRY_AGAIN */
return e ? 0 : -1;
case EINTR:
return 0;
default:
return -1;
}
}
static ssize_t
do_read(el_context *ctx, int fd, char *buf, size_t size)
{ ssize_t rc;
int oreader = ctx->reader;
ctx->reader = PL_thread_self();
rc = read(fd, buf, size);
ctx->reader = oreader;
return rc;
}
static void
refresh(el_context *ctx)
{ FILE *err;
el_get(ctx->el, EL_GETFP, 2, &err);
el_resize(ctx->el);
fprintf(err, "\r");
el_set(ctx->el, EL_REFRESH);
}
#ifdef HAVE_EL_WSET
#define el_char_t wchar_t
#else
#define el_char_t unsigned char
#endif
static int
read_char(EditLine *el, el_char_t *cp)
{ ssize_t num_read;
int tried = 0;
char cbuf[MB_LEN_MAX];
size_t cbp = 0;
int save_errno = errno;
el_context *ctx;
FILE *in;
el_get(el, EL_CLIENTDATA, (void**)&ctx); /* What to do if we have no context? */
el_get(el, EL_GETFP, 0, &in);
#ifndef HAVE_EL_CURSOR
if ( ctx->move_cursor )
{ if ( ctx->move_cursor > 0 )
{ *cp = (el_char_t)('F'-'@');
ctx->move_cursor--;
} else
{ *cp = (el_char_t)('B'-'@');
ctx->move_cursor++;
}
return 1;
}
#endif
if ( ctx->electric.move )
{ switch(ctx->electric.state)
{ case E_WAIT: /* "^[" */
ctx->electric.state = E_COMMAND;
wait_on_fd(fileno(in), ctx->electric.timeout);
*cp = (el_char_t)27; /* ESC */
return 1;
case E_COMMAND:
ctx->electric.state = E_NONE;
*cp = (el_char_t)'\1'; /* "^A" */
return 1;
case E_NONE:
break;
}
}
again:
ctx->sig_no = 0;
if ( !PL_dispatch(fileno(in), PL_DISPATCH_WAIT) )
{ Sset_exception(ctx->istream, PL_exception(0));
*cp = (el_char_t)'\0';
return -1;
}
while ( (num_read = do_read(ctx, fileno(in), cbuf + cbp, (size_t)1)) == -1 )
{ int e = errno;
switch (ctx->sig_no)
{ case SIGCONT:
el_set(el, EL_REFRESH);
goto again;
case SIGWINCH:
refresh(ctx);
goto again;
default:
break;
}
if ( PL_handle_signals() < 0 )
{ Sset_exception(ctx->istream, PL_exception(0));
*cp = (el_char_t)'\0';
return -1;
}
if ( e == EINTR )
continue;
if ( !tried && read__fixio(fileno(in), e) == 0 )
{ errno = save_errno;
tried = 1;
} else
{ errno = e;
*cp = (el_char_t)'\0';
return -1;
}
}
if ( ctx->sig_no == SIGWINCH )
refresh(ctx);
/* Test for EOF */
if ( num_read == 0 )
{ *cp = (el_char_t)'\0';
return 0;
}
#ifdef HAVE_EL_WSET
for (;;)
{ mbstate_t mbs;
++cbp;
/* This only works because UTF8 is stateless. */
memset(&mbs, 0, sizeof(mbs));
switch (mbrtowc(cp, cbuf, cbp, &mbs))
{ case (size_t)-1:
if (cbp > 1)
{ /*
* Invalid sequence, discard all bytes
* except the last one.
*/
cbuf[0] = cbuf[cbp - 1];
cbp = 0;
break;
} else
{ /* Invalid byte, discard it. */
cbp = 0;
goto again;
}
case (size_t)-2:
/*
* We don't support other multibyte charsets.
* The second condition shouldn't happen
* and is here merely for additional safety.
*/
if ( /*(el->el_flags & CHARSET_IS_UTF8 ) == 0 || We don't have that */
cbp >= MB_LEN_MAX) {
errno = EILSEQ;
*cp = (el_char_t)'\0';
return -1;
}
/* Incomplete sequence, read another byte. */
goto again;
default:
/* Valid character, process it. */
return 1;
}
}
#else
*cp = cbuf[0]&0xff;
return 1;
#endif
}
/*******************************
* IO FUNCTIONS *
*******************************/
#define ISUTF8_CB(c) (((c)&0xc0) == 0x80)
/* Find the longest prefix of `in` upto `len` of complete
UTF-8 characters.
*/
static size_t
utf8_chars(const char *in, size_t len)
{ const char *e = &in[len];
while ( e > in && ISUTF8_CB(e[-1]) )
e--;
return e-in;
}
static size_t
send_one_buffer(el_context *ctx, const char *line, char *buf, size_t size)
{ size_t linelen = strlen(line);
if ( linelen <= size )
{ memcpy(buf, line, linelen);
ctx->buffered = NULL;
return linelen;
} else
{ size_t slen = utf8_chars(line, size);
memcpy(buf, line, slen);
if ( (ctx->buffered = strdup(&line[slen])) )
return slen;
return -1; /* ENOMEM */
}
}
static ssize_t
Sread_libedit(void *handle, char *buf, size_t size)
{ el_context *ctx = get_context_from_handle(handle);
int ttymode = PL_ttymode(ctx->istream);
int rval;
if ( ctx->buffered )
{ char *old = ctx->buffered;
size_t slen = send_one_buffer(ctx, old, buf, size);
free(old);
return slen;
}
switch( ttymode )
{ case PL_RAWTTY: /* get_single_char/1 */
case PL_NOTTY: /* -tty */
{ int fd = Sfileno(ctx->istream);
PL_write_prompt(ttymode == PL_NOTTY);
PL_dispatch(fd, PL_DISPATCH_WAIT);
rval = read(fd, buf, size);
if ( rval > 0 && buf[rval-1] == '\n' )
PL_prompt_next(fd);
return rval;
}
case PL_COOKEDTTY:
default:
{ int len;
const char *line;
if ( ctx->ostream )
Sflush(ctx->ostream);
update_prompt(ctx);
if ( (line = el_siggets(ctx->el, &len)) && len > 0 )
{ return send_one_buffer(ctx, line, buf, size);
} else if ( len == 0 )
{ return 0;
} else
{ return -1; /* TBD: set errno */
}
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The write handler is defined to deal with writes from background
threads.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static ssize_t
Swrite_libedit(void *handle, char *buf, size_t size)
{ el_context *ctx = get_context_from_ohandle(handle);
if ( ctx->reader &&
ctx->reader != PL_thread_self() )
{ // fprintf(stderr, "background write %p\n", ctx);
ctx->sig_no = SIGWINCH; /* simulate a window change */
}
return (*ctx->orig_functions->write)(handle, buf, size);
}
static char *
prompt(EditLine *el)
{ el_context *ctx;
el_get(el, EL_CLIENTDATA, (void**)&ctx);
return ctx->prompt ? ctx->prompt : "";
}
static foreign_t
pl_wrap(term_t progid, term_t tin, term_t tout, term_t terr)
{ IOSTREAM *in = NULL, *out = NULL, *err = NULL;
int rc = FALSE;
char *prog;
if ( !PL_get_chars(progid, &prog, STR_OPTIONS) )
return FALSE;
if ( PL_get_stream(tin, &in, SIO_INPUT) &&
PL_get_stream(tout, &out, SIO_OUTPUT) &&
PL_get_stream(terr, &err, SIO_OUTPUT) )
{ int fd_in, fd_out, fd_err;
if ( (fd_in = Sfileno(in)) >= 0 && isatty(fd_in) &&
(fd_out = Sfileno(out)) >= 0 &&
(fd_err = Sfileno(err)) >= 0 )
{ el_context *ctx = alloc_context(fd_in);
FILE *fin, *fout, *ferr;
fin = fdopen(fd_in, "r");
fout = fdopen(fd_out, "w");
ferr = fdopen(fd_err, "w");
setlinebuf(fin);
setlinebuf(fout);
setbuf(ferr, NULL);
ctx->istream = in;
ctx->ostream = out;
ctx->estream = err;
ctx->history = history_init();
history(ctx->history, &ctx->ev, H_SETSIZE, 100);
history(ctx->history, &ctx->ev, H_SETUNIQUE, TRUE);
ctx->el = el_init(prog, fin, fout, ferr);
#ifdef HAVE_EL_WSET
el_wset(ctx->el, EL_GETCFN, read_char);
#else
el_set(ctx->el, EL_GETCFN, read_char);
#endif
el_set( ctx->el, EL_PROMPT, prompt);
el_set( ctx->el, EL_HIST, history, ctx->history);
el_set( ctx->el, EL_EDITOR, "emacs");
el_set( ctx->el, EL_CLIENTDATA, ctx);
electric_init(ctx->el);
ctx->orig_functions = in->functions;
ctx->functions = *in->functions;
ctx->functions.read = Sread_libedit;
ctx->functions.write = Swrite_libedit;
in->functions = &ctx->functions;
out->functions = &ctx->functions;
err->functions = &ctx->functions;
rc = TRUE;
} else
{ rc = PL_permission_error("el_wrap", "stream", tin);
}
}
if ( in ) PL_release_stream(in);
if ( out ) PL_release_stream(out);
if ( err ) PL_release_stream(err);
return rc;
}
static foreign_t
pl_is_wrapped(term_t tin)
{ IOSTREAM *in;
int rc;
if ( (rc=PL_get_stream(tin, &in, SIO_INPUT)) )
{ int fd;
el_context *ctx;
if ( (fd=Sfileno(in)) >= 0 &&
(ctx=get_context(fd)) )
rc = TRUE;
else
rc = FALSE;
PL_release_stream_noerror(in);
}
return rc;
}
static int
get_el_context(term_t tin, el_context **ctxp)
{ IOSTREAM *in;
int rc;
if ( (rc=PL_get_stream(tin, &in, SIO_INPUT)) )
{ int fd;
el_context *ctx;
if ( (fd=Sfileno(in)) >= 0 &&
(ctx=get_context(fd)) )
{ *ctxp = ctx;
rc = TRUE;
} else
{ rc = PL_domain_error("libedit_input", tin);
}
PL_release_stream_noerror(in);
}
return rc;
}
static foreign_t
pl_unwrap(term_t tin)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ el_context **cp;
el_context *c;
binding *b, *bn;
command *cm, *cmn;
for(cp=&el_clist, c=*cp; c; cp=&c->next, c=*cp)
{ if ( c == ctx )
{ *cp = ctx->next;
break;
}
}
ctx->magic = 0xbfbfbfbf;
for(b=ctx->bindings; b; b=bn)
{ bn = b->next;
free(b);
}
for(cm=ctx->commands; cm; cm=cmn)
{ cmn = cm->next;
free(cm);
}
if ( ctx->prompt )
free(ctx->prompt);
ctx->istream->functions = ctx->orig_functions;
ctx->ostream->functions = ctx->orig_functions;
ctx->estream->functions = ctx->orig_functions;
history_end(ctx->history);
el_end(ctx->el);
/* FIXME: We should close the FILE*, but fclose() also closes the
* underlying descriptor.
*/
PL_free(ctx);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_source(term_t tin, term_t file)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ char *fname;
if ( PL_is_variable(file) )
fname = NULL;
else if ( !PL_get_file_name(file, &fname,
PL_FILE_OSPATH|PL_FILE_SEARCH|PL_FILE_READ) )
return FALSE;
el_source(ctx->el, fname);
return TRUE;
}
return FALSE;
}
/*******************************
* PROGRAMMING *
*******************************/
#define isoctal(c) ((c) >= '0' && (c) <= '7')
#define octval(c) ((c) - '0')
static int
get_key(const char *s, int *k)
{ switch(s[0])
{ case '\\':
switch(s[1])
{ case 'a': *k = '\a'; break;
case 'b': *k = '\b'; break;
case 'e': *k = 27; break; /* ESC */
case 'f': *k = '\f'; break;
case 'n': *k = '\n'; break;
case 'r': *k = '\r'; break;
case 't': *k = '\r'; break;
case 'v': *k = '\v'; break;
default:
if ( isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3]) )
{ *k = (octval(s[1]) << 6) + (octval(s[2]) << 3) + octval(s[3]);
break;
}
return FALSE;
}
break;
case '^':
if ( s[1] >= '@' && s[2] <= 'Z' )
{ *k = s[1] - '@';
break;
}
return FALSE;
case 0:
return FALSE;
default:
*k = s[0]&0xff;
}
return TRUE;
}
static int
continue_code(term_t t)
{ int rc = CC_ERROR;
atom_t a;
if ( PL_get_atom(t, &a) )
{ if ( a == ATOM_norm ) rc = CC_NORM;
else if ( a == ATOM_newline ) rc = CC_NEWLINE;
else if ( a == ATOM_eof ) rc = CC_EOF;
else if ( a == ATOM_arghack ) rc = CC_ARGHACK;
else if ( a == ATOM_refresh ) rc = CC_REFRESH;
else if ( a == ATOM_refresh_beep ) rc = CC_REFRESH_BEEP;
else if ( a == ATOM_cursor ) rc = CC_CURSOR;
else if ( a == ATOM_redisplay ) rc = CC_REDISPLAY;
else if ( a == ATOM_error ) rc = CC_ERROR;
else if ( a == ATOM_fatal ) rc = CC_FATAL;
}
return rc;
}
static unsigned char
prolog_function(EditLine *el, int ch)
{ el_context *ctx;
binding *b;
int rc = CC_ERROR;
el_get(el, EL_CLIENTDATA, (void**)&ctx);
for(b=ctx->bindings; b; b=b->next)
{ if ( b->ch == ch )
{ static predicate_t pred_call4;
fid_t fid;
if ( !pred_call4 )
pred_call4 = PL_predicate("call", 4, "system");
if ( (fid = PL_open_foreign_frame()) )
{ term_t av;
if ( (av=PL_new_term_refs(4)) &&
PL_recorded(b->command->closure, av+0) &&
PL_unify_stream(av+1, ctx->istream) &&
PL_put_integer(av+2, ch) &&
PL_call_predicate(b->command->module, PL_Q_NODEBUG, pred_call4, av) )
{ if ( PL_is_functor(av+3, FUNCTOR_electric3) )
{ int move, timeout;
if ( PL_get_arg(1, av+3, av+0) &&
PL_get_arg(2, av+3, av+1) &&
PL_get_arg(3, av+3, av+3) &&
PL_get_integer(av+0, &move) &&
PL_get_integer(av+1, &timeout) )
{ el_cursor(el, move);
ctx->electric.timeout = timeout;
ctx->electric.move = -move;
ctx->electric.state = E_WAIT;
}
}
rc = continue_code(av+3);
}
PL_close_foreign_frame(fid);
}
}
}
return rc;
}
static foreign_t
pl_addfn(term_t tin, term_t tname, term_t thelp, term_t goal)
{ el_context *ctx;
char *name, *help;
if ( get_el_context(tin, &ctx) &&
PL_get_chars(tname, &name, STR_OPTIONS) &&
PL_get_chars(thelp, &help, STR_OPTIONS) )
{ command *c;
module_t m = NULL;
if ( !PL_strip_module(goal, &m, goal) )
return FALSE;
if ( !PL_is_callable(goal) )
return PL_type_error("callable", goal);
if ( !(c=malloc(sizeof(*c))) )
return PL_resource_error("memory");
c->module = m;
c->closure = PL_record(goal);
c->next = ctx->commands;
c->name = PL_new_atom(name);
ctx->commands = c;
el_set(ctx->el, EL_ADDFN, name, help, prolog_function);
return TRUE;
}
return FALSE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(*) We recognise the actually used command from the pressed key. For
escape sequences, this is the character after the escape. This means we
cannot bind e.g., both "^[?" and "?". I see no way out using the public
interface of libedit.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
bind_command(el_context *ctx, const char *key, const char *cmd)
{ int k;
if ( key[0] == '^' && key[1] == '[' )
key += 2; /* See (*) */
if ( get_key(key, &k) )
{ atom_t cname = PL_new_atom(cmd);
command *c;
for(c=ctx->commands; c; c=c->next)
{ if ( c->name == cname )
{ binding *b;
if ( !(b=malloc(sizeof(*b))) )
return PL_resource_error("memory");
b->ch = k;
b->command = c;
b->next = ctx->bindings;
ctx->bindings = b;
break;
}
}
PL_unregister_atom(cname);
}
return TRUE;
}
#define EL_BIND_MAX_ARGS 9
static foreign_t
pl_bind(term_t tin, term_t options)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ int rc;
int ac = 0;
char *av[EL_BIND_MAX_ARGS];
term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
while(PL_get_list_ex(tail, head, tail))
{ if ( !PL_get_chars(head, &av[ac++], STR_OPTIONS) )
return FALSE;
if ( ac >= EL_BIND_MAX_ARGS )
return PL_representation_error("el_bind_arguments");
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
switch(ac)
{ case 0:
rc = el_set(ctx->el, EL_BIND, NULL);
break;
case 1:
rc = el_set(ctx->el, EL_BIND, av[0], NULL);
break;
case 2:
if ( !bind_command(ctx, av[0], av[1]) )
return FALSE;
rc = el_set(ctx->el, EL_BIND, av[0], av[1], NULL);
break;
case 3:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], NULL);
break;
case 4:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], NULL);
break;
case 5:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], av[4], NULL);
break;
case 6:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], av[4],
av[5], NULL);
break;
case 7:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], av[4],
av[5], av[6], NULL);
break;
case 8:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], av[4],
av[5], av[6], av[7], NULL);
break;
case 9:
rc = el_set(ctx->el, EL_BIND, av[0], av[1], av[2], av[3], av[4],
av[5], av[6], av[7], av[8], NULL);
break;
default:
assert(0);
}
(void)rc; /* TBD: check? */
return TRUE;
}
return FALSE;
}
static foreign_t
pl_cursor(term_t tin, term_t move)
{ el_context *ctx;
int amount;
if ( PL_get_integer_ex(move, &amount) &&
get_el_context(tin, &ctx) )
{ el_cursor(ctx->el, amount);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_line(term_t tin, term_t line)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ const LineInfo *li = el_line(ctx->el);
term_t before, after;
return ( (before = PL_new_term_ref()) &&
(after = PL_new_term_ref()) &&
PL_unify_chars(before, PL_STRING|REP_MB,
li->cursor - li->buffer, li->buffer) &&
PL_unify_chars(after, PL_STRING|REP_MB,
li->lastchar - li->cursor, li->cursor) &&
PL_unify_term(line, PL_FUNCTOR, FUNCTOR_line2,
PL_TERM, before,
PL_TERM, after)
);
}
return FALSE;
}
static foreign_t
pl_insertstr(term_t tin, term_t insert)
{ el_context *ctx;
char *s;
if ( PL_get_chars(insert, &s, STR_OPTIONS) &&
get_el_context(tin, &ctx) )
{ el_insertstr(ctx->el, s);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_deletestr(term_t tin, term_t count)
{ el_context *ctx;
int amount;
if ( PL_get_integer_ex(count, &amount) &&
get_el_context(tin, &ctx) )
{ el_deletestr(ctx->el, amount);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_getc(term_t tin, term_t c)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ wchar_t wc;
switch(el_wgetc(ctx->el, &wc))
{ case 1:
return PL_unify_integer(c, wc);
case 0:
return PL_unify_integer(c, -1);
case -1:
default:
Sdprintf("el_getc(): I/O error\n");
return PL_unify_integer(c, -1);
}
}
return FALSE;
}
static foreign_t
pl_push(term_t tin, term_t c)
{ el_context *ctx;
int ic;
if ( PL_get_char_ex(c, &ic, FALSE) &&
get_el_context(tin, &ctx) )
{ wchar_t wc[2];
wc[0] = ic;
wc[1] = 0;
el_wpush(ctx->el, wc);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_editmode(term_t tin, term_t on)
{ el_context *ctx;
int m;
if ( PL_get_bool_ex(on, &m) &&
get_el_context(tin, &ctx) )
{ el_set(ctx->el, EL_EDITMODE, m);
return TRUE;
}
return FALSE;
}
/*******************************
* HISTORY *
*******************************/
static foreign_t
pl_add_history(term_t tin, term_t text)
{ el_context *ctx;
char *line;
if ( !PL_get_chars(text, &line, CVT_ATOM|CVT_STRING|REP_UTF8|CVT_EXCEPTION) ||
!get_el_context(tin, &ctx) )
return FALSE;
history(ctx->history, &ctx->ev, H_ENTER, line);
return TRUE;
}
static foreign_t
pl_write_history(term_t tin, term_t file_name)
{ el_context *ctx;
char *fname;
if ( get_el_context(tin, &ctx) &&
PL_get_file_name(file_name, &fname,
PL_FILE_OSPATH|PL_FILE_SEARCH|PL_FILE_WRITE) )
{ history(ctx->history, &ctx->ev, H_SAVE, fname);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_read_history(term_t tin, term_t file_name)
{ el_context *ctx;
char *fname;
if ( get_el_context(tin, &ctx) &&
PL_get_file_name(file_name, &fname,
PL_FILE_OSPATH|PL_FILE_SEARCH|PL_FILE_READ|
PL_FILE_NOERRORS) )
{ history(ctx->history, &ctx->ev, H_LOAD, fname);
return TRUE;
}
return FALSE;
}
static int
append_ev(term_t tail, term_t head, const HistEvent *ev)
{ return ( PL_unify_list(tail, head, tail) &&
PL_unify_term(head, PL_FUNCTOR, FUNCTOR_pair2,
PL_INT, ev->num,
PL_UTF8_STRING, ev->str) );
}
static foreign_t
pl_history_events(term_t tin, term_t events)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ HistEvent ev;
int curr = 0;
int rc = FALSE;
term_t tail = PL_copy_term_ref(events);
term_t head = PL_new_term_ref();
if ( history(ctx->history, &ev, H_CURR) == 0 )
curr = ev.num;
if ( history(ctx->history, &ev, H_FIRST) == 0 )
{ if ( !append_ev(tail, head, &ev) )
goto out;
}
while(history(ctx->history, &ev, H_NEXT) == 0)
{ if ( !append_ev(tail, head, &ev) )
goto out;
}
rc = PL_unify_nil(tail);
out:
history(ctx->history, &ev, H_SET, curr);
return rc;
}
return FALSE;
}
static int
get_int_arg(int i, term_t t, int *v)
{ term_t a;
if ( (a=PL_new_term_ref()) &&
PL_get_arg(i, t, a) &&
PL_get_integer_ex(a, v) )
return TRUE;
return FALSE;
}
static int
get_bool_arg(int i, term_t t, int *v)
{ term_t a;
if ( (a=PL_new_term_ref()) &&
PL_get_arg(i, t, a) &&
PL_get_bool_ex(a, v) )
return TRUE;
return FALSE;
}
static foreign_t
pl_history(term_t tin, term_t option)
{ el_context *ctx;
if ( get_el_context(tin, &ctx) )
{ atom_t name;
size_t arity;
int rc = 0;
if ( PL_get_name_arity(option, &name, &arity) )
{ HistEvent ev;
if ( name == ATOM_setsize )
{ int s;
if ( arity != 1 ) goto err_domain;
if ( !get_int_arg(1, option, &s) ) return FALSE;
rc = history(ctx->history, &ev, H_SETSIZE);
} else if ( name == ATOM_clear )
{ if ( arity != 0 ) goto err_domain;
rc = history(ctx->history, &ev, H_CLEAR);
} else if ( name == ATOM_setunique )
{ int u;
if ( arity != 1 ) goto err_domain;
if ( !get_bool_arg(1, option, &u) ) return FALSE;
rc = history(ctx->history, &ev, H_SETUNIQUE, u);
} else
{ err_domain:
return PL_domain_error("history_action", option);
}
if ( rc == 0 )
return TRUE;
return FALSE; /* What exception? */
}
return PL_type_error("callable", option);
}
return FALSE;
}
/*******************************
* REGISTRATION *
*******************************/
#define MKATOM(n) \
ATOM_ ## n = PL_new_atom(#n)
#define MKFUNCTOR(n, a) \
FUNCTOR_ ## n ## a = PL_new_functor(PL_new_atom(#n), a)
install_t
install_libedit4pl(void)
{ MKATOM(norm);
MKATOM(newline);
MKATOM(eof);
MKATOM(arghack);
MKATOM(refresh);
MKATOM(refresh_beep);
MKATOM(cursor);
MKATOM(redisplay);
MKATOM(error);
MKATOM(fatal);
MKATOM(clear);
MKATOM(setsize);
MKATOM(setunique);
MKFUNCTOR(line, 2);
MKFUNCTOR(electric, 3);
FUNCTOR_pair2 = PL_new_functor(PL_new_atom("-"), 2);
PL_register_foreign("el_wrap", 4, pl_wrap, 0);
PL_register_foreign("el_wrapped", 1, pl_is_wrapped, 0);
PL_register_foreign("el_unwrap", 1, pl_unwrap, 0);
PL_register_foreign("el_source", 2, pl_source, 0);
PL_register_foreign("el_addfn", 4, pl_addfn, 0);
PL_register_foreign("el_bind", 2, pl_bind, 0);
PL_register_foreign("el_cursor", 2, pl_cursor, 0);
PL_register_foreign("el_line", 2, pl_line, 0);
PL_register_foreign("el_insertstr", 2, pl_insertstr, 0);
PL_register_foreign("el_deletestr", 2, pl_deletestr, 0);
PL_register_foreign("el_add_history", 2, pl_add_history, 0);
PL_register_foreign("el_write_history", 2, pl_write_history, 0);
PL_register_foreign("el_read_history", 2, pl_read_history, 0);
PL_register_foreign("el_history_events",2, pl_history_events,0);
PL_register_foreign("el_history", 2, pl_history, 0);
PL_register_foreign("el_getc", 2, pl_getc, 0);
PL_register_foreign("el_push", 2, pl_push, 0);
PL_register_foreign("el_editmode", 2, pl_editmode, 0);
}
|