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
|
/* session.c -- user windowing interface to Info.
Copyright 1993-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Originally written by Brian Fox. */
#include "info.h"
#include "display.h"
#include "session.h"
#include "dribble.h"
#include "util.h"
#include "search.h"
#include "nodes.h"
#include "echo-area.h"
#include "footnotes.h"
#include "variables.h"
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef __MINGW32__
# undef read
# define read(f,b,s) w32_read(f,b,s)
# undef _read
# define _read(f,b,s) w32_read(f,b,s)
extern ssize_t w32_read (int, void *, size_t);
#endif
#if defined (HAVE_SYS_TIME_H)
# include <sys/time.h>
# define HAVE_STRUCT_TIMEVAL
#endif /* HAVE_SYS_TIME_H */
/* **************************************************************** */
/* */
/* Running an Info Session */
/* */
/* **************************************************************** */
static void mouse_event_handler (void);
/* The place that we are reading input from. */
static FILE *info_input_stream = NULL;
NODE *allfiles_node = 0;
static void
allfiles_create_node (char *term, REFERENCE **fref)
{
int i;
struct text_buffer text;
text_buffer_init (&text);
text_buffer_printf (&text,
"%s File names matching '%s'\n\n"
"Info File Index\n"
"***************\n\n"
"File names that match '%s':\n",
INFO_NODE_LABEL,
term, term);
/* Mark as an index so that destinations are never hidden. */
text_buffer_add_string (&text, "\0\b[index\0\b]", 11);
text_buffer_printf (&text, "\n* Menu:\n\n");
for (i = 0; fref[i]; i++)
{
text_buffer_printf (&text, "* %4i: (%s)", i+1, fref[i]->filename);
if (fref[i]->nodename)
text_buffer_printf (&text, "%s", fref[i]->nodename);
text_buffer_printf (&text, ".\n");
}
allfiles_node = info_create_node ();
allfiles_node->fullpath = xstrdup ("");
allfiles_node->nodename = xstrdup ("*Info File Index*");
allfiles_node->contents = text_buffer_base (&text);
allfiles_node->nodelen = text_buffer_off (&text);
allfiles_node->body_start = strcspn (allfiles_node->contents, "\n");
scan_node_contents (allfiles_node, 0, 0);
}
/* Begin an info session finding the nodes specified by REFERENCES. For
each loaded node, create a new window. Always split the largest of the
available windows. Display ERROR in echo area if non-null. */
static void
begin_multiple_window_info_session (REFERENCE **references, char *error)
{
register int i;
WINDOW *window = 0;
for (i = 0; references && references[i]; i++)
{
if (!window)
{
window = active_window;
info_select_reference (window, references[i]);
if (!window->node)
window = 0;
}
else
{
/* Find the largest window in WINDOWS, and make that be the active
one. Then split it and add our window and node to the list
of remembered windows and nodes. Then tile the windows. */
WINDOW *win, *largest = NULL;
int max_height = 0;
for (win = windows; win; win = win->next)
if (win->height > max_height)
{
max_height = win->height;
largest = win;
}
if (!largest)
{
display_update_display ();
info_error ("%s", _("Cannot find a window!"));
return;
}
active_window = largest;
window = window_make_window ();
info_select_reference (window, references[i]);
if (!window->node)
{
/* We couldn't find the node referenced. */
window_delete_window (window);
window = 0;
}
if (window)
window_tile_windows (TILE_INTERNALS);
else
{
display_update_display ();
info_error ("%s", msg_win_too_small);
return;
}
}
}
/* Load dir node as a back-up if there were no references given, or if
none of them were valid. */
if (!window)
{
info_set_node_of_window (active_window, get_dir_node ());
return;
}
}
static void
display_startup_message (void)
{
const char *format;
format = replace_in_documentation
/* TRANSLATORS: Try to keep this message (when "expanded") at most 79
characters; anything after the 79th character will not actually be
displayed on an 80-column terminal. */
(_("Welcome to Info version %s. Type \\[get-help-window] for help, \\[get-info-help-node] for tutorial."),
0);
window_message_in_echo_area (format, VERSION, NULL);
}
/* Run an Info session. If USER_FILENAME is null, create a window for each
node referenced in REF_LIST.
ERROR is an optional error message to display at start-up. */
void
info_session (REFERENCE **ref_list, char *error)
{
/* Initialize the Info session. */
initialize_info_session ();
if (!error)
display_startup_message ();
else
show_error_node (error);
begin_multiple_window_info_session (ref_list, error);
info_read_and_dispatch ();
close_info_session ();
}
/* Used when "--all" was used on the command line. Display a file index
with entries in REF_LIST. */
void
info_session_allfiles (REFERENCE **ref_list, char *user_filename, char *error)
{
/* Initialize the Info session. */
initialize_info_session ();
if (!error)
display_startup_message ();
else
show_error_node (error);
allfiles_create_node (user_filename, ref_list);
info_set_node_of_window (active_window, allfiles_node);
info_read_and_dispatch ();
close_info_session ();
}
/* Start an info session with a single node displayed. */
void
info_session_one_node (NODE *node)
{
initialize_info_session ();
info_set_node_of_window (active_window, node);
info_read_and_dispatch ();
close_info_session ();
}
extern COMMAND_FUNCTION info_next_line;
extern COMMAND_FUNCTION info_prev_line;
/* Becomes non-zero when 'q' is typed to an Info window. */
static int quit_info_immediately = 0;
void
info_session_quit (void)
{
quit_info_immediately = 1;
}
void
info_read_and_dispatch (void)
{
COMMAND_FUNCTION *cmd;
int count;
for (quit_info_immediately = 0; !quit_info_immediately; )
{
if (!info_any_buffered_input_p ())
display_update_display ();
/* Some redisplay might be necessary if the cursor has moved and
a different reference (or no reference) has to be highlighted. */
if (hl_ref_rendition.mask)
display_update_one_window (active_window);
display_cursor_at_point (active_window);
cmd = read_key_sequence (info_keymap, 1, 1, 0, &count);
if (cmd)
{
if (!check_info_keyseq_displayed ())
window_clear_echo_area ();
(*cmd) (active_window, count);
/* Don't change the goal column when going up and down. This
means we can go from a long line to a short line and back to
a long line and end back in the same column. */
if (!(cmd == &info_next_line || cmd == &info_prev_line))
active_window->flags |= W_CurrentColGoal; /* Goal is current column. */
}
}
}
/* Found in signals.c */
extern void initialize_info_signal_handler (void );
/* Initialize terminal, read configuration file and set key bindings. */
void
initialize_terminal_and_keymaps (char *init_file)
{
char *term_name = getenv ("TERM");
terminal_initialize_terminal (term_name);
read_init_file (init_file);
}
/* Initialize the first info session by starting the terminal, window,
and display systems. */
void
initialize_info_session (void)
{
if (!terminal_prep_terminal ())
{
/* Terminal too dumb to run interactively. */
char *term_name = getenv ("TERM");
info_error (_("Terminal type '%s' is not smart enough to run Info"),
term_name);
exit (EXIT_FAILURE);
}
terminal_clear_screen ();
window_initialize_windows (screenwidth, screenheight);
initialize_info_signal_handler ();
display_initialize_display (screenwidth, screenheight);
/* If input has not been redirected yet, make it come from unbuffered
standard input. */
if (!info_input_stream)
{
setbuf (stdin, NULL);
info_input_stream = stdin;
}
info_windows_initialized_p = 1;
}
/* On program exit, leave the cursor at the bottom of the window, and
restore the terminal I/O. */
void
close_info_session (void)
{
terminal_goto_xy (0, screenheight - 1);
terminal_clear_to_eol ();
fflush (stdout);
terminal_unprep_terminal ();
close_dribble_file ();
}
/* Tell Info that input is coming from the file FILENAME. */
void
info_set_input_from_file (char *filename)
{
FILE *stream;
/* Input may include binary characters. */
stream = fopen (filename, FOPEN_RBIN);
if (!stream)
return;
if ((info_input_stream != NULL) &&
(info_input_stream != stdin))
fclose (info_input_stream);
info_input_stream = stream;
if (stream != stdin)
display_inhibited = 1;
}
/* **************************************************************** */
/* */
/* Input Character Buffering */
/* */
/* **************************************************************** */
static void fill_input_buffer (int wait);
static int info_gather_typeahead (int);
/* Largest number of characters that we can read in advance. */
#define MAX_INFO_INPUT_BUFFERING 512
static int pop_index = 0; /* Where to remove bytes from input buffer. */
static int push_index = 0; /* Where to add bytes to input buffer. */
static unsigned char info_input_buffer[MAX_INFO_INPUT_BUFFERING];
/* Get a key from the buffer of characters to be read.
Return the key in KEY.
Result is non-zero if there was a key, or 0 if there wasn't. */
static int
get_byte_from_input_buffer (unsigned char *key)
{
if (push_index == pop_index)
return 0;
*key = info_input_buffer[pop_index++];
if (pop_index >= MAX_INFO_INPUT_BUFFERING)
pop_index = 0;
return 1;
}
int
info_any_buffered_input_p (void)
{
fill_input_buffer (0);
return push_index != pop_index;
}
int
control_g_waiting (void)
{
fill_input_buffer (0); \
return info_input_buffer[pop_index] == Control ('g');
}
/* Wrapper around info_gather_typeahead which handles read errors and reaching
end-of-file. */
static void
fill_input_buffer (int wait)
{
while (1)
{
int success;
do
{
success = info_gather_typeahead (wait);
}
while (!success && errno == EINTR); /* Try again if the read was
interrupted due to a signal. */
if (success || !wait)
return;
/* Reading failed. If we were reading from a dribble file with
--restore, switch to standard input. Otherwise quit. */
if (info_input_stream != stdin)
{
fclose (info_input_stream);
info_input_stream = stdin;
display_inhibited = 0;
display_update_display ();
display_cursor_at_point (active_window);
}
else
{
close_info_session ();
exit (EXIT_SUCCESS);
}
}
}
/* Read bytes and stuff them into info_input_buffer. If WAIT is true, wait
for input; otherwise don't do anything if there is no input waiting.
Return 1 on success, 0 on error. ERRNO may be set by read(). */
static int
info_gather_typeahead (int wait)
{
register int i = 0;
int tty, space_avail;
long chars_avail;
unsigned char input[MAX_INFO_INPUT_BUFFERING];
tty = fileno (info_input_stream);
chars_avail = 0;
/* Clear errno. */
errno = 0;
/* There may be characters left over from last time, in which case we don't
want to wait for another key to be pressed. */
if (wait && pop_index == push_index)
{
char c;
/* Wait until there is a byte waiting, and then stuff it into the input
buffer. */
if (read (tty, &c, 1) <= 0)
return 0;
if (info_dribble_file)
dribble (c);
info_input_buffer[push_index++] = c;
if (push_index >= MAX_INFO_INPUT_BUFFERING)
push_index = 0;
/* Continue to see if there are more bytes waiting. */
}
/* Get the amount of space available in INFO_INPUT_BUFFER for new chars. */
if (pop_index > push_index)
space_avail = pop_index - push_index;
else
space_avail = sizeof (info_input_buffer) - (push_index - pop_index);
/* If we can just find out how many characters there are to read, do so. */
#if defined (FIONREAD)
{
ioctl (tty, FIONREAD, &chars_avail);
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
#else /* !FIONREAD */
# if defined (O_NDELAY) && defined (F_GETFL) && defined (F_SETFL)
{
int flags;
flags = fcntl (tty, F_GETFL, 0);
fcntl (tty, F_SETFL, (flags | O_NDELAY));
chars_avail = read (tty, &input[0], space_avail);
fcntl (tty, F_SETFL, flags);
if (chars_avail == -1)
chars_avail = 0;
}
# else /* !O_NDELAY */
# ifdef __DJGPP__
{
extern long pc_term_chars_avail (void);
if (isatty (tty))
chars_avail = pc_term_chars_avail ();
else
{
/* We could be more accurate by calling ltell, but we have no idea
whether tty is buffered by stdio functions, and if so, how many
characters are already waiting in the buffer. So we punt. */
struct stat st;
if (fstat (tty, &st) < 0)
chars_avail = 1;
else
chars_avail = st.st_size;
}
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
# else
# ifdef __MINGW32__
{
extern long w32_chars_avail (int);
chars_avail = w32_chars_avail (tty);
if (chars_avail > space_avail)
chars_avail = space_avail;
if (chars_avail)
chars_avail = read (tty, &input[0], chars_avail);
}
# endif /* _WIN32 */
# endif/* __DJGPP__ */
# endif /* O_NDELAY */
#endif /* !FIONREAD */
while (i < chars_avail)
{
if (info_dribble_file)
dribble (input[i]);
/* Add KEY to the buffer of characters to be read. */
if (input[i] != Control ('g'))
{
info_input_buffer[push_index++] = input[i];
if (push_index >= MAX_INFO_INPUT_BUFFERING)
push_index = 0;
}
else
/* Flush all pending input in the case of C-g pressed. */
push_index = pop_index;
i++;
}
/* If wait is true, there is at least one byte left in the input buffer. */
if (chars_avail <= 0 && !wait)
return 0;
return 1;
}
static int get_input_key_internal (void);
/* Whether to process or skip mouse events in the input stream. */
unsigned char mouse_cb, mouse_cx, mouse_cy;
/* Handle mouse event given that mouse_cb, mouse_cx and mouse_cy contain the
data from the event. See the "XTerm Control Sequences" document for their
meanings. */
void
mouse_event_handler (void)
{
window_clear_echo_area();
if (mouse_cb & 0x40)
{
switch (mouse_cb & 0x03)
{
case 0: /* Mouse button 4 (scroll up). */
set_window_pagetop (active_window, active_window->pagetop - 3);
break;
case 1: /* Mouse button 5 (scroll down). */
set_window_pagetop (active_window, active_window->pagetop + 3);
break;
}
}
}
/* Return number representing a key that has been pressed, which is an index
into info_keymap and echo_area_keymap. */
int
get_input_key (void)
{
int ret = -1;
while (ret == -1)
{
ret = get_input_key_internal ();
if (ret == KEY_MOUSE)
{
get_byte_from_input_buffer (&mouse_cb);
get_byte_from_input_buffer (&mouse_cx);
get_byte_from_input_buffer (&mouse_cy);
}
}
return ret;
}
/* Time in milliseconds to wait for the next byte of a byte sequence
corresponding to a key or key chord. Settable with the 'key-time' user
variable. */
int key_time = 100;
/* Read bytes from input and return what key has been pressed. Return -1 on
reading an unrecognized key. */
static int
get_input_key_internal (void)
{
BYTEMAP_ENTRY *b;
unsigned char c;
int esc_seen = 0;
int pop_start;
int byte_count = 0;
fill_input_buffer (1);
if (pop_index == push_index)
return -1; /* No input waiting. This shouldn't happen. */
b = byte_seq_to_key;
pop_start = pop_index;
while (pop_index != push_index)
{
int in_map = 0;
int unknown = 0;
if (!get_byte_from_input_buffer (&c))
break; /* Incomplete byte sequence. */
byte_count++;
switch (b[c].type)
{
case BYTEMAP_KEY:
return b[c].key;
case BYTEMAP_ESC:
esc_seen = 1;
/* Fall through. */
case BYTEMAP_MAP:
in_map = 1;
b = b[c].next;
break;
case BYTEMAP_NONE:
unknown = 1;
break;
}
if (unknown)
break;
/* If we read an incomplete byte sequence, pause a short while to
see if more bytes follow. */
if (in_map && pop_index == push_index)
{
int ready = 0;
#if defined (FD_SET)
struct timeval timer, *timerp = 0;
fd_set readfds;
FD_ZERO (&readfds);
FD_SET (fileno (info_input_stream), &readfds);
timer.tv_sec = 0;
timer.tv_usec = key_time * 1000;
timerp = &timer;
ready = select (fileno(info_input_stream)+1, &readfds,
NULL, NULL, timerp);
#else
ready = 1;
#endif /* FD_SET */
if (ready)
fill_input_buffer (0);
}
}
/* Incomplete or unknown byte sequence. Start again with the first byte. */
pop_index = pop_start;
if (!esc_seen || (byte_count >= 3 && key_time == 0))
{
/* If the sequence was incomplete, return the first byte.
Also return the first byte for sequences with ESC that are at
least three bytes long if 'key_time' is 0, to give some support for
specifying byte sequences in infokey for those sent by unrecognized
special keys (which would otherwise be skipped below). */
pop_index = pop_start;
get_byte_from_input_buffer (&c);
return c;
}
else
{
get_byte_from_input_buffer (&c); /* Should be ESC */
/* If there are no more characters, then decide that the escape key
itself has been pressed. */
if (pop_index == push_index)
return 033;
/* Skip byte sequences that look like they could have come from
unrecognized keys, e.g. F3 or C-S-Left, to avoid them as being
interpreted as random garbage. These might produce sequences
that look like "ESC O R" or "ESC [ 1 ; 6 ~", depending on
the terminal. */
/* Check if the sequence starts ESC O. */
get_byte_from_input_buffer (&c);
if (c == 'O')
{
/* If no more bytes, call it M-O. */
if (!info_any_buffered_input_p ())
return 'O' + KEYMAP_META_BASE;
/* Otherwise it could be an unrecognized key producing a sequence
ESC O (byte). Ignore it, discarding the next byte. */
get_byte_from_input_buffer (&c);
return -1;
}
/* Unknown CSI-style sequences. */
else if (c == '[')
{
/* If no more bytes, call it M-[. */
if (!get_byte_from_input_buffer (&c))
return '[' + KEYMAP_META_BASE;
/* Skip a control sequence as defined by ECMA-48. */
while (c >= 0x30 && c <= 0x3f)
if (!get_byte_from_input_buffer (&c))
break;
while (c >= 0x20 && c <= 0x2f)
if (!get_byte_from_input_buffer (&c))
break;
return -1;
}
else
{
/* The sequence started with ESC, but wasn't recognized. Treat it
as introducing a sequence produced by a key chord with the meta
key pressed. */
return c + KEYMAP_META_BASE;
}
}
}
#if defined (HAVE_SYS_TIME_H)
# include <sys/time.h>
# define HAVE_STRUCT_TIMEVAL
#endif /* HAVE_SYS_TIME_H */
#if !defined (FD_SET) && defined (__MINGW32__)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
void
pause_or_input (void)
{
#ifdef FD_SET
struct timeval timer;
fd_set readfds;
#endif
if (pop_index != push_index)
return; /* Input is already waiting. */
#ifdef FD_SET
FD_ZERO (&readfds);
FD_SET (fileno (stdin), &readfds);
timer.tv_sec = 2;
timer.tv_usec = 0;
select (fileno (stdin) + 1, &readfds, NULL, NULL, &timer);
#elif defined (__MINGW32__)
/* This is signalled on key release, so flush it and wait again. */
WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
WaitForSingleObject (GetStdHandle (STD_INPUT_HANDLE), 2000);
#endif /* FD_SET */
}
/* **************************************************************** */
/* */
/* Error handling */
/* */
/* **************************************************************** */
/* Non-zero means ring terminal bell on errors. */
int info_error_rings_bell_p = 1;
/* Print AP according to FORMAT. If the window system was initialized,
then the message is printed in the echo area. Otherwise, a message is
output to stderr. */
static void
vinfo_error (const char *format, va_list ap)
{
if (!info_windows_initialized_p || display_inhibited)
{
fprintf (stderr, "%s: ", program_name);
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
fflush (stderr);
}
else
{
if (!echo_area_is_active)
{
if (info_error_rings_bell_p)
terminal_ring_bell ();
vwindow_message_in_echo_area (format, ap);
}
else
{
NODE *temp = build_message_node (format, ap);
if (info_error_rings_bell_p)
terminal_ring_bell ();
inform_in_echo_area (temp->contents);
free (temp->contents);
free (temp);
}
}
}
void
info_error (const char *format, ...)
{
va_list ap;
va_start (ap, format);
vinfo_error (format, ap);
va_end (ap);
}
void
show_error_node (char *error)
{
if (info_error_rings_bell_p)
terminal_ring_bell ();
if (!info_windows_initialized_p)
{
info_error ("%s", error);
}
else if (!echo_area_is_active)
{
window_message_in_echo_area ("%s", error);
}
else
inform_in_echo_area (error);
}
/* **************************************************************** */
/* */
/* Window node history */
/* */
/* **************************************************************** */
static void
put_node_in_window (WINDOW *win, NODE *node)
{
win->node = node;
win->pagetop = 0;
win->point = 0;
free_matches (&win->matches);
free (win->line_starts); win->line_starts = 0;
free (win->log_line_no); win->log_line_no = 0;
win->flags |= W_UpdateWindow;
}
/* Go back one in the node history. */
int
forget_node_fast (WINDOW *win)
{
int i = win->hist_index;
if (i == 0)
return 0;
free_node (win->hist[i - 1]->node);
free (win->hist[i - 1]);
win->hist[i - 1] = 0;
i = --win->hist_index;
if (i == 0)
/* Window history is empty. */
win->node = 0;
else
{
put_node_in_window (win, win->hist[i - 1]->node);
win->point = win->hist[i - 1]->point;
}
return i;
}
void
forget_node (WINDOW *win)
{
int i = forget_node_fast (win);
if (i == 0)
{
win->node = 0;
return; /* Window history is empty. */
}
window_set_node_of_window (win, win->hist[i - 1]->node);
if (auto_footnotes_p)
info_get_or_remove_footnotes (win);
set_window_pagetop (win, win->hist[i - 1]->pagetop);
win->point = win->hist[i - 1]->point;
window_compute_line_map (win);
win->node->display_pos = win->point;
}
/* Remove associated list of nodes of WINDOW. */
void
forget_window_and_nodes (WINDOW *win)
{
size_t i;
for (i = 0; i < win->hist_index; i++)
{
free_node (win->hist[i]->node);
free (win->hist[i]);
}
free (win->hist);
}
/* Like info_set_node_of_window, but only do enough so to extend the
window history, avoiding calculating line starts. */
void
info_set_node_of_window_fast (WINDOW *win, NODE *node)
{
WINDOW_STATE *new;
if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
{
win->hist[win->hist_index - 1]->pagetop = win->pagetop;
win->hist[win->hist_index - 1]->point = win->point;
}
put_node_in_window (win, node);
new = xmalloc (sizeof (WINDOW_STATE));
new->node = win->node;
new->pagetop = win->pagetop;
new->point = win->point;
add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
}
/* Set WINDOW to show NODE. Remember the new window in our list of
Info windows. If we are doing automatic footnote display, try to display
the footnotes for this window. */
void
info_set_node_of_window (WINDOW *win, NODE *node)
{
WINDOW_STATE *new;
/* Remember the current values of pagetop and point if the remembered node
is the same as the current one being displayed. */
if (win->hist_index && win->hist[win->hist_index - 1]->node == win->node)
{
win->hist[win->hist_index - 1]->pagetop = win->pagetop;
win->hist[win->hist_index - 1]->point = win->point;
}
/* Put this node into the window. */
window_set_node_of_window (win, node);
/* Remember this node, the currently displayed pagetop, and the current
location of point in this window. */
new = xmalloc (sizeof (WINDOW_STATE));
new->node = win->node;
new->pagetop = win->pagetop;
new->point = win->point;
add_pointer_to_array (new, win->hist_index, win->hist, win->hist_slots, 16);
/* If doing auto-footnote display/undisplay, show the footnotes belonging
to this window's node. Don't do that if it is a footnote node itself. */
if (auto_footnotes_p
&& !((win->node->flags & N_IsInternal)
&& !strcmp (win->node->nodename, "*Footnotes*")))
info_get_or_remove_footnotes (win);
}
/* Return the file buffer which belongs to WINDOW's node. */
FILE_BUFFER *
file_buffer_of_window (WINDOW *window)
{
/* If this window has no node, then it has no file buffer. */
if (!window->node)
return NULL;
if (window->node->fullpath)
return info_find_file (window->node->fullpath);
return NULL;
}
/* **************************************************************** */
/* */
/* Reading Keys and Dispatching on Them */
/* */
/* **************************************************************** */
static void
dispatch_error (int *keyseq)
{
const char *rep;
rep = pretty_keyseq (keyseq);
if (!echo_area_is_active)
info_error (_("Unknown command (%s)"), rep);
else
{
char *temp = xmalloc (1 + strlen (rep) + strlen (_("\"%s\" is invalid")));
sprintf (temp, _("'%s' is invalid"), rep);
terminal_ring_bell ();
inform_in_echo_area (temp);
free (temp);
}
}
/* Keeping track of key sequences. */
static int *info_keyseq = NULL;
static int info_keyseq_index = 0;
static int info_keyseq_size = 0;
static int info_keyseq_displayed_p = 0;
/* Initialize the length of the current key sequence. */
void
initialize_keyseq (void)
{
info_keyseq_index = 0;
info_keyseq_displayed_p = 0;
}
int
check_info_keyseq_displayed (void)
{
return info_keyseq_displayed_p;
}
/* Add CHARACTER to the current key sequence. */
void
add_char_to_keyseq (int character)
{
if (info_keyseq_index + 2 >= info_keyseq_size)
info_keyseq = xrealloc (info_keyseq,
sizeof (int) * (info_keyseq_size += 10));
info_keyseq[info_keyseq_index++] = character;
info_keyseq[info_keyseq_index] = '\0';
}
/* Display the current value of info_keyseq. If argument EXPECTING is
non-zero, input is expected to be read after the key sequence is
displayed, so add an additional prompting character to the sequence. */
static void
display_info_keyseq (int expecting_future_input)
{
const char *rep;
if (!info_keyseq || info_keyseq_index == 0)
return;
rep = pretty_keyseq_ext (info_keyseq, expecting_future_input);
if (echo_area_is_active)
inform_in_echo_area (rep);
else
{
window_message_in_echo_area (rep, NULL, NULL);
display_cursor_at_point (active_window);
}
info_keyseq_displayed_p = 1;
}
/* Called by interactive commands to read another key when keys have already
been read as part of the current command (and possibly displayed in status
line with display_info_keyseq). */
int
get_another_input_key (void)
{
int ready = !info_keyseq_displayed_p; /* ready if new and pending key */
/* If there isn't any input currently available, then wait a
moment looking for input. If we don't get it fast enough,
prompt a little bit with the current key sequence. */
if (!info_keyseq_displayed_p)
{
ready = 1;
if (!info_any_buffered_input_p ())
{
#if defined (FD_SET)
struct timeval timer;
fd_set readfds;
FD_ZERO (&readfds);
FD_SET (fileno (info_input_stream), &readfds);
timer.tv_sec = 1;
timer.tv_usec = 750;
ready = select (fileno(info_input_stream)+1, &readfds,
NULL, NULL, &timer);
#else
ready = 0;
#endif /* FD_SET */
}
}
if (!ready)
display_info_keyseq (1);
return get_input_key ();
}
/* Non-zero means that an explicit argument has been passed to this
command, as in C-u C-v. */
int info_explicit_arg = 0;
/* As above, but used when C-u is typed in the echo area to avoid
overwriting this information when "C-u ARG M-x" is typed. */
int ea_explicit_arg = 0;
/* Create a default argument. */
void
info_initialize_numeric_arg (void)
{
if (!echo_area_is_active)
info_explicit_arg = 0;
else
ea_explicit_arg = 0;
}
extern COMMAND_FUNCTION info_universal_argument;
extern COMMAND_FUNCTION info_add_digit_to_numeric_arg;
extern COMMAND_FUNCTION info_do_lowercase_version;
extern COMMAND_FUNCTION info_menu_digit;
/* Read a key sequence and look up its command in MAP. Handle C-u style
numeric args, as well as M--, and M-digits. Return argument in COUNT if it
is non-null.
Some commands can be executed directly, in which case null is returned
instead:
If MENU, call info_menu_digit on ACTIVE_WINDOW if a number key was
pressed.
If MOUSE, call mouse_event_handler if a mouse event occurred.
If INSERT, call ea_insert if a printable character was input.
*/
COMMAND_FUNCTION *
read_key_sequence (Keymap map, int menu, int mouse,
int insert, int *count)
{
int key;
int reading_universal_argument = 0;
int numeric_arg = 1, numeric_arg_sign = 1, *which_explicit_arg;
COMMAND_FUNCTION *func;
/* Process the right numeric argument. */
if (!echo_area_is_active)
which_explicit_arg = &info_explicit_arg;
else
which_explicit_arg = &ea_explicit_arg;
*which_explicit_arg = 0;
initialize_keyseq ();
key = get_input_key ();
if (key == KEY_MOUSE)
{
if (mouse)
mouse_event_handler ();
return 0;
}
if (insert
&& (key >= 040 && key < 0200
|| ISO_Latin_p && key >= 0200 && key < 0400))
{
ea_insert (the_echo_area, 1, key);
return 0;
}
add_char_to_keyseq (key);
while (1)
{
int dash_typed = 0, digit_typed = 0;
func = 0;
if (display_was_interrupted_p && !info_any_buffered_input_p ())
display_update_display ();
if (active_window != the_echo_area)
display_cursor_at_point (active_window);
/* If reading a universal argument, both <digit> and M-<digit> help form
the argument. Don't look up the pressed key in the key map. */
if (reading_universal_argument)
{
int k = key;
if (k >= KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
if (k == '-')
{
dash_typed = 1;
}
else if (isdigit (k))
{
digit_typed = 1;
}
else
/* Note: we may still read another C-u after this. */
reading_universal_argument = 0;
}
if (!dash_typed && !digit_typed && map[key].type == ISFUNC)
{
func = map[key].value.function ? map[key].value.function->func : 0;
if (!func)
{
dispatch_error (info_keyseq);
return 0;
}
}
if (dash_typed || digit_typed || func == &info_add_digit_to_numeric_arg)
{
int k = key;
if (k > KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
reading_universal_argument = 1;
if (dash_typed || k == '-')
{
if (!*which_explicit_arg)
{
numeric_arg_sign = -1;
numeric_arg = 1;
}
}
else if (digit_typed || isdigit (k))
{
if (*which_explicit_arg)
numeric_arg = numeric_arg * 10 + (k - '0');
else
numeric_arg = (k - '0');
*which_explicit_arg = 1;
}
}
else if (func == info_do_lowercase_version)
{
int lowerkey;
if (key >= KEYMAP_META_BASE)
{
lowerkey = key;
lowerkey -= KEYMAP_META_BASE;
lowerkey = tolower (lowerkey);
lowerkey += KEYMAP_META_BASE;
}
else
lowerkey = tolower (key);
if (lowerkey == key)
{
dispatch_error (info_keyseq);
return 0;
}
key = lowerkey;
continue;
}
else if (func == &info_universal_argument)
{
/* Multiply by 4. */
/* TODO: Maybe C-u should also terminate the universal argument
sequence, as in Emacs. (C-u 6 4 C-u 1 inserts 64 1's.) */
if (!*which_explicit_arg)
numeric_arg *= 4;
reading_universal_argument = 1;
}
else if (menu && func == &info_menu_digit)
{
/* key can either be digit, or M-digit for --vi-keys. */
int k = key;
if (k > KEYMAP_META_BASE)
k -= KEYMAP_META_BASE;
window_clear_echo_area ();
menu_digit (active_window, k);
return 0;
}
else if (insert
&& (func == &ea_possible_completions || func == &ea_complete)
&& !echo_area_completion_items)
{
ea_insert (the_echo_area, 1, key);
return 0;
}
else if (func)
{
/* Don't update the key sequence if we have finished reading a key
sequence in the echo area. This means that a key sequence like
"C-u 2 Left" appears to take effect immediately, instead of there
being a delay while the message is displayed. */
if (!echo_area_is_active && info_keyseq_displayed_p)
display_info_keyseq (0);
if (count)
*count = numeric_arg * numeric_arg_sign;
/* *which_explicit_arg has not been set yet if only a sequence of
C-u's was typed (each of which has multiplied the argument by
four). */
if (*count != 1 && !*which_explicit_arg)
*which_explicit_arg = 1;
return func;
}
else if (map[key].type == ISKMAP)
{
if (map[key].value.keymap != NULL)
map = map[key].value.keymap;
else
{
dispatch_error (info_keyseq);
return 0;
}
if (info_keyseq_displayed_p)
display_info_keyseq (1);
}
do
key = get_another_input_key ();
while (key == KEY_MOUSE);
add_char_to_keyseq (key);
}
return 0;
}
void
info_abort (void)
{
/* If error printing doesn't oridinarily ring the bell, do it now,
since C-g always rings the bell. Otherwise, let the error printer
do it. */
if (!info_error_rings_bell_p)
terminal_ring_bell ();
info_error ("%s", _("Quit"));
info_initialize_numeric_arg ();
}
/* **************************************************************** */
/* */
/* Dumping and Printing Nodes */
/* */
/* **************************************************************** */
struct info_namelist_entry
{
struct info_namelist_entry *next;
char name[1];
};
static int
info_namelist_add (struct info_namelist_entry **ptop, const char *name)
{
struct info_namelist_entry *p;
for (p = *ptop; p; p = p->next)
if (fncmp (p->name, name) == 0)
return 1;
p = xmalloc (sizeof (*p) + strlen (name));
strcpy (p->name, name);
p->next = *ptop;
*ptop = p;
return 0;
}
static void
info_namelist_free (struct info_namelist_entry *top)
{
while (top)
{
struct info_namelist_entry *next = top->next;
free (top);
top = next;
}
}
enum
{
DUMP_SUCCESS,
DUMP_INFO_ERROR,
DUMP_SYS_ERROR
};
static int dump_node_to_stream (FILE_BUFFER *file_buffer,
char *nodename, FILE *stream, int dump_subnodes);
static void initialize_dumping (void);
/* Dump the nodes specified with REFERENCES to the file named
in OUTPUT_FILENAME. If DUMP_SUBNODES is set, recursively dump
the nodes which appear in the menu of each node dumped. */
void
dump_nodes_to_file (REFERENCE **references,
char *output_filename, int dump_subnodes)
{
int i;
FILE *output_stream;
if (!references)
return;
/* Get the stream to print the nodes to. Special case of an output
filename of "-" means to dump the nodes to stdout. */
if (strcmp (output_filename, "-") == 0)
output_stream = stdout;
else
output_stream = fopen (output_filename, "w");
if (!output_stream)
{
info_error (_("Could not create output file '%s'"), output_filename);
return;
}
/* Print each node to stream. */
for (i = 0; references[i]; i++)
{
FILE_BUFFER *file_buffer;
char *nodename;
initialize_dumping ();
file_buffer = info_find_file (references[i]->filename);
if (!file_buffer)
{
if (info_recent_file_error)
info_error ("%s", info_recent_file_error);
continue;
}
if (references[i]->nodename && *references[i]->nodename)
nodename = references[i]->nodename;
else
nodename = "Top";
if (dump_node_to_stream (file_buffer, nodename,
output_stream, dump_subnodes) == DUMP_SYS_ERROR)
{
info_error (_("error writing to %s: %s"), output_filename,
strerror (errno));
exit (EXIT_FAILURE);
}
}
if (output_stream != stdout)
fclose (output_stream);
debug (1, (_("closing %s"), output_filename));
}
/* A place to remember already dumped nodes. */
static struct info_namelist_entry *dumped_already;
static void
initialize_dumping (void)
{
info_namelist_free (dumped_already);
dumped_already = NULL;
}
/* Get and print the node specified by FILENAME and NODENAME to STREAM.
If DUMP_SUBNODES is non-zero, recursively dump the nodes which appear
in the menu of each node dumped. */
static int
dump_node_to_stream (FILE_BUFFER *file_buffer,
char *nodename,
FILE *stream, int dump_subnodes)
{
register int i;
NODE *node;
node = info_get_node_of_file_buffer (file_buffer, nodename);
if (!node)
{
info_error (msg_cant_find_node, nodename);
return DUMP_INFO_ERROR;
}
/* If we have already dumped this node, don't dump it again. */
if (info_namelist_add (&dumped_already, node->nodename))
{
free_node (node);
return DUMP_SUCCESS;
}
/* Maybe we should print some information about the node being output. */
debug (1, (_("writing node %s..."), node_printed_rep (node)));
if (write_node_to_stream (node, stream))
{
free_node (node);
return DUMP_SYS_ERROR;
}
/* If we are dumping subnodes, get the list of menu items in this node,
and dump each one recursively. */
if (dump_subnodes)
{
REFERENCE **menu = NULL;
/* If this node is an Index, do not dump the menu references. */
if (string_in_line ("Index", node->nodename) == -1)
menu = node->references;
if (menu)
{
for (i = 0; menu[i]; i++)
{
if (REFERENCE_MENU_ITEM != menu[i]->type) continue;
/* We don't dump Info files which are different than the
current one. */
if (!menu[i]->filename)
if (dump_node_to_stream (file_buffer, menu[i]->nodename,
stream, dump_subnodes) == DUMP_SYS_ERROR)
{
free_node (node);
return DUMP_SYS_ERROR;
}
}
}
}
free_node (node);
return DUMP_SUCCESS;
}
int
write_node_to_stream (NODE *node, FILE *stream)
{
return fwrite (node->contents, node->nodelen, 1, stream) != 1;
}
|