Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/miscutils/less.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/miscutils/less.c
r2725 r3232 22 22 */ 23 23 24 //config:config LESS 25 //config: bool "less" 26 //config: default y 27 //config: help 28 //config: 'less' is a pager, meaning that it displays text files. It possesses 29 //config: a wide array of features, and is an improvement over 'more'. 30 //config: 31 //config:config FEATURE_LESS_MAXLINES 32 //config: int "Max number of input lines less will try to eat" 33 //config: default 9999999 34 //config: depends on LESS 35 //config: 36 //config:config FEATURE_LESS_BRACKETS 37 //config: bool "Enable bracket searching" 38 //config: default y 39 //config: depends on LESS 40 //config: help 41 //config: This option adds the capability to search for matching left and right 42 //config: brackets, facilitating programming. 43 //config: 44 //config:config FEATURE_LESS_FLAGS 45 //config: bool "Enable -m/-M" 46 //config: default y 47 //config: depends on LESS 48 //config: help 49 //config: The -M/-m flag enables a more sophisticated status line. 50 //config: 51 //config:config FEATURE_LESS_MARKS 52 //config: bool "Enable marks" 53 //config: default y 54 //config: depends on LESS 55 //config: help 56 //config: Marks enable positions in a file to be stored for easy reference. 57 //config: 58 //config:config FEATURE_LESS_REGEXP 59 //config: bool "Enable regular expressions" 60 //config: default y 61 //config: depends on LESS 62 //config: help 63 //config: Enable regular expressions, allowing complex file searches. 64 //config: 65 //config:config FEATURE_LESS_WINCH 66 //config: bool "Enable automatic resizing on window size changes" 67 //config: default y 68 //config: depends on LESS 69 //config: help 70 //config: Makes less track window size changes. 71 //config: 72 //config:config FEATURE_LESS_ASK_TERMINAL 73 //config: bool "Use 'tell me cursor position' ESC sequence to measure window" 74 //config: default y 75 //config: depends on FEATURE_LESS_WINCH 76 //config: help 77 //config: Makes less track window size changes. 78 //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set, 79 //config: this option makes less perform a last-ditch effort to find it: 80 //config: position cursor to 999,999 and ask terminal to report real 81 //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin. 82 //config: 83 //config: This is not clean but helps a lot on serial lines and such. 84 //config: 85 //config:config FEATURE_LESS_DASHCMD 86 //config: bool "Enable flag changes ('-' command)" 87 //config: default y 88 //config: depends on LESS 89 //config: help 90 //config: This enables the ability to change command-line flags within 91 //config: less itself ('-' keyboard command). 92 //config: 93 //config:config FEATURE_LESS_LINENUMS 94 //config: bool "Enable dynamic switching of line numbers" 95 //config: default y 96 //config: depends on FEATURE_LESS_DASHCMD 97 //config: help 98 //config: Enables "-N" command. 99 100 //usage:#define less_trivial_usage 101 //usage: "[-E" IF_FEATURE_LESS_FLAGS("Mm") "Nh~I?] [FILE]..." 102 //usage:#define less_full_usage "\n\n" 103 //usage: "View FILE (or stdin) one screenful at a time\n" 104 //usage: "\n -E Quit once the end of a file is reached" 105 //usage: IF_FEATURE_LESS_FLAGS( 106 //usage: "\n -M,-m Display status line with line numbers" 107 //usage: "\n and percentage through the file" 108 //usage: ) 109 //usage: "\n -N Prefix line number to each line" 110 //usage: "\n -I Ignore case in all searches" 111 //usage: "\n -~ Suppress ~s displayed past EOF" 112 24 113 #include <sched.h> /* sched_yield() */ 25 114 … … 97 186 regex_t pattern; 98 187 smallint pattern_valid; 188 #endif 189 #if ENABLE_FEATURE_LESS_ASK_TERMINAL 190 smallint winsize_err; 99 191 #endif 100 192 smallint terminated; … … 618 710 619 711 /* Each part of the line that matches has the HIGHLIGHT 620 621 622 712 * and NORMAL escape sequences placed around it. 713 * NB: we regex against line, but insert text 714 * from quarantined copy (buf[]) */ 623 715 str = buf; 624 716 growline = NULL; … … 629 721 char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL, 630 722 growline ? growline : "", 631 match_structs.rm_so, str,632 match_structs.rm_eo - match_structs.rm_so,723 (int)match_structs.rm_so, str, 724 (int)(match_structs.rm_eo - match_structs.rm_so), 633 725 str + match_structs.rm_so); 634 726 free(growline); … … 805 897 max_lineno = 0; 806 898 open_file_and_read_lines(); 899 #if ENABLE_FEATURE_LESS_ASK_TERMINAL 900 if (G.winsize_err) 901 printf("\033[999;999H" "\033[6n"); 902 #endif 807 903 buffer_fill_and_print(); 808 904 } 809 905 810 static int getch_nowait(void)906 static int64_t getch_nowait(void) 811 907 { 812 908 int rd; 909 int64_t key64; 813 910 struct pollfd pfd[2]; 814 911 … … 858 955 /* We have kbd_fd in O_NONBLOCK mode, read inside read_key() 859 956 * would not block even if there is no input available */ 860 rd= read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);861 if ( rd== -1) {957 key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2); 958 if ((int)key64 == -1) { 862 959 if (errno == EAGAIN) { 863 960 /* No keyboard input available. Since poll() did return, … … 871 968 } 872 969 set_tty_cooked(); 873 return rd;970 return key64; 874 971 } 875 972 … … 877 974 * May return KEYCODE_xxx values. 878 975 * Note that this function works best with raw input. */ 879 static int less_getch(int pos) 880 { 881 int i; 976 static int64_t less_getch(int pos) 977 { 978 int64_t key64; 979 int key; 882 980 883 981 again: 884 982 less_gets_pos = pos; 885 i= getch_nowait();983 key = key64 = getch_nowait(); 886 984 less_gets_pos = -1; 887 985 888 /* Discard Ctrl-something chars */ 889 if (i >= 0 && i < ' ' && i != 0x0d && i != 8) 986 /* Discard Ctrl-something chars. 987 * (checking only lower 32 bits is a size optimization: 988 * upper 32 bits are used only by KEYCODE_CURSOR_POS) 989 */ 990 if (key >= 0 && key < ' ' && key != 0x0d && key != 8) 890 991 goto again; 891 return i; 992 993 return key64; 892 994 } 893 995 … … 1429 1531 #endif 1430 1532 case 'r': case 'R': 1533 /* TODO: (1) also bind ^R, ^L to this? 1534 * (2) re-measure window size? 1535 */ 1431 1536 buffer_print(); 1432 1537 break; … … 1504 1609 int less_main(int argc, char **argv) 1505 1610 { 1506 int keypress;1507 1508 1611 INIT_G(); 1509 1612 … … 1548 1651 term_less.c_cc[VTIME] = 0; 1549 1652 1550 get_terminal_width_height(kbd_fd, &width, &max_displayed_line);1653 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line); 1551 1654 /* 20: two tabstops + 4 */ 1552 1655 if (width < 20 || max_displayed_line < 3) … … 1563 1666 reinitialize(); 1564 1667 while (1) { 1668 int64_t keypress; 1669 1565 1670 #if ENABLE_FEATURE_LESS_WINCH 1566 1671 while (WINCH_COUNTER) { 1567 1672 again: 1568 1673 winch_counter--; 1569 get_terminal_width_height(kbd_fd, &width, &max_displayed_line); 1674 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line); 1675 IF_FEATURE_LESS_ASK_TERMINAL(got_size:) 1570 1676 /* 20: two tabstops + 4 */ 1571 1677 if (width < 20) … … 1587 1693 * were there another SIGWINCH? */ 1588 1694 } 1589 #endif1590 1695 keypress = less_getch(-1); /* -1: do not position cursor */ 1696 # if ENABLE_FEATURE_LESS_ASK_TERMINAL 1697 if ((int32_t)keypress == KEYCODE_CURSOR_POS) { 1698 uint32_t rc = (keypress >> 32); 1699 width = (rc & 0x7fff); 1700 max_displayed_line = ((rc >> 16) & 0x7fff); 1701 goto got_size; 1702 } 1703 # endif 1704 #else 1705 keypress = less_getch(-1); /* -1: do not position cursor */ 1706 #endif 1591 1707 keypress_process(keypress); 1592 1708 }
Note:
See TracChangeset
for help on using the changeset viewer.