diff options
Diffstat (limited to 'miscutils/less.c')
-rw-r--r-- | miscutils/less.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index c1d5e1b39..75ded97cf 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
@@ -127,6 +127,10 @@ | |||
127 | 127 | ||
128 | #include <sched.h> /* sched_yield() */ | 128 | #include <sched.h> /* sched_yield() */ |
129 | 129 | ||
130 | #if ENABLE_PLATFORM_MINGW32 | ||
131 | #include <conio.h> | ||
132 | #endif | ||
133 | |||
130 | #include "libbb.h" | 134 | #include "libbb.h" |
131 | #include "common_bufsiz.h" | 135 | #include "common_bufsiz.h" |
132 | #if ENABLE_FEATURE_LESS_REGEXP | 136 | #if ENABLE_FEATURE_LESS_REGEXP |
@@ -533,6 +537,11 @@ static void read_lines(void) | |||
533 | last_line_pos = 0; | 537 | last_line_pos = 0; |
534 | break; | 538 | break; |
535 | } | 539 | } |
540 | #if ENABLE_PLATFORM_MINGW32 | ||
541 | if (c == '\r') { | ||
542 | continue; | ||
543 | } | ||
544 | #endif | ||
536 | /* NUL is substituted by '\n'! */ | 545 | /* NUL is substituted by '\n'! */ |
537 | if (c == '\0') c = '\n'; | 546 | if (c == '\0') c = '\n'; |
538 | *p++ = c; | 547 | *p++ = c; |
@@ -629,7 +638,12 @@ static void update_num_lines(void) | |||
629 | /* only do this for regular files */ | 638 | /* only do this for regular files */ |
630 | if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) { | 639 | if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) { |
631 | count = 0; | 640 | count = 0; |
641 | #if !ENABLE_PLATFORM_MINGW32 | ||
632 | fd = open("/proc/self/fd/0", O_RDONLY); | 642 | fd = open("/proc/self/fd/0", O_RDONLY); |
643 | #else | ||
644 | /* don't even try to access /proc on WIN32 */ | ||
645 | fd = -1; | ||
646 | #endif | ||
633 | if (fd < 0 && num_lines == REOPEN_AND_COUNT) { | 647 | if (fd < 0 && num_lines == REOPEN_AND_COUNT) { |
634 | /* "filename" is valid only if REOPEN_AND_COUNT */ | 648 | /* "filename" is valid only if REOPEN_AND_COUNT */ |
635 | fd = open(filename, O_RDONLY); | 649 | fd = open(filename, O_RDONLY); |
@@ -812,7 +826,12 @@ static void print_found(const char *line) | |||
812 | match_status = 1; | 826 | match_status = 1; |
813 | } | 827 | } |
814 | 828 | ||
829 | #if !ENABLE_PLATFORM_MINGW32 | ||
815 | printf("%s%s\n", growline ? growline : "", str); | 830 | printf("%s%s\n", growline ? growline : "", str); |
831 | #else | ||
832 | /* skip newline, we use explicit positioning on WIN32 */ | ||
833 | printf("%s%s", growline ? growline : "", str); | ||
834 | #endif | ||
816 | free(growline); | 835 | free(growline); |
817 | } | 836 | } |
818 | #else | 837 | #else |
@@ -848,7 +867,12 @@ static void print_ascii(const char *str) | |||
848 | *p = '\0'; | 867 | *p = '\0'; |
849 | print_hilite(buf); | 868 | print_hilite(buf); |
850 | } | 869 | } |
870 | #if !ENABLE_PLATFORM_MINGW32 | ||
851 | puts(str); | 871 | puts(str); |
872 | #else | ||
873 | /* skip newline, we use explicit positioning on WIN32 */ | ||
874 | printf("%s", str); | ||
875 | #endif | ||
852 | } | 876 | } |
853 | 877 | ||
854 | /* Print the buffer */ | 878 | /* Print the buffer */ |
@@ -858,6 +882,10 @@ static void buffer_print(void) | |||
858 | 882 | ||
859 | move_cursor(0, 0); | 883 | move_cursor(0, 0); |
860 | for (i = 0; i <= max_displayed_line; i++) { | 884 | for (i = 0; i <= max_displayed_line; i++) { |
885 | #if ENABLE_PLATFORM_MINGW32 | ||
886 | /* make sure we're on the right line */ | ||
887 | move_cursor(i+1, 0); | ||
888 | #endif | ||
861 | printf(CLEAR_2_EOL); | 889 | printf(CLEAR_2_EOL); |
862 | if (option_mask32 & FLAG_N) | 890 | if (option_mask32 & FLAG_N) |
863 | print_lineno(buffer[i]); | 891 | print_lineno(buffer[i]); |
@@ -1044,9 +1072,13 @@ static void reinitialize(void) | |||
1044 | if (G.winsize_err) | 1072 | if (G.winsize_err) |
1045 | printf("\033[999;999H" "\033[6n"); | 1073 | printf("\033[999;999H" "\033[6n"); |
1046 | #endif | 1074 | #endif |
1075 | #if ENABLE_PLATFORM_MINGW32 | ||
1076 | reset_screen(); | ||
1077 | #endif | ||
1047 | buffer_fill_and_print(); | 1078 | buffer_fill_and_print(); |
1048 | } | 1079 | } |
1049 | 1080 | ||
1081 | #if !ENABLE_PLATFORM_MINGW32 | ||
1050 | static int64_t getch_nowait(void) | 1082 | static int64_t getch_nowait(void) |
1051 | { | 1083 | { |
1052 | int rd; | 1084 | int rd; |
@@ -1108,6 +1140,46 @@ static int64_t getch_nowait(void) | |||
1108 | set_tty_cooked(); | 1140 | set_tty_cooked(); |
1109 | return key64; | 1141 | return key64; |
1110 | } | 1142 | } |
1143 | #else | ||
1144 | static int64_t getch_nowait(void) | ||
1145 | { | ||
1146 | int64_t c; | ||
1147 | |||
1148 | retry: | ||
1149 | c = _getch(); | ||
1150 | if (c == 0 || c == 0xe0) { | ||
1151 | switch (_getch()) { | ||
1152 | case 0x48: | ||
1153 | c = KEYCODE_UP; | ||
1154 | break; | ||
1155 | case 0x50: | ||
1156 | c = KEYCODE_DOWN; | ||
1157 | break; | ||
1158 | case 0x49: | ||
1159 | c = KEYCODE_PAGEUP; | ||
1160 | break; | ||
1161 | case 0x51: | ||
1162 | c = KEYCODE_PAGEDOWN; | ||
1163 | break; | ||
1164 | case 0x47: | ||
1165 | c = KEYCODE_HOME; | ||
1166 | break; | ||
1167 | case 0x4f: | ||
1168 | c = KEYCODE_END; | ||
1169 | break; | ||
1170 | default: | ||
1171 | goto retry; | ||
1172 | } | ||
1173 | } | ||
1174 | |||
1175 | /* Position cursor if line input is done */ | ||
1176 | if (less_gets_pos >= 0) | ||
1177 | move_cursor(max_displayed_line + 2, less_gets_pos + 1); | ||
1178 | fflush_all(); | ||
1179 | |||
1180 | return c; | ||
1181 | } | ||
1182 | #endif | ||
1111 | 1183 | ||
1112 | /* Grab a character from input without requiring the return key. | 1184 | /* Grab a character from input without requiring the return key. |
1113 | * May return KEYCODE_xxx values. | 1185 | * May return KEYCODE_xxx values. |
@@ -1763,8 +1835,10 @@ static void sigwinch_handler(int sig UNUSED_PARAM) | |||
1763 | int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 1835 | int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
1764 | int less_main(int argc, char **argv) | 1836 | int less_main(int argc, char **argv) |
1765 | { | 1837 | { |
1838 | #if !ENABLE_PLATFORM_MINGW32 | ||
1766 | char *tty_name; | 1839 | char *tty_name; |
1767 | int tty_fd; | 1840 | int tty_fd; |
1841 | #endif | ||
1768 | 1842 | ||
1769 | INIT_G(); | 1843 | INIT_G(); |
1770 | 1844 | ||
@@ -1798,6 +1872,7 @@ int less_main(int argc, char **argv) | |||
1798 | if (option_mask32 & FLAG_TILDE) | 1872 | if (option_mask32 & FLAG_TILDE) |
1799 | empty_line_marker = ""; | 1873 | empty_line_marker = ""; |
1800 | 1874 | ||
1875 | #if !ENABLE_PLATFORM_MINGW32 | ||
1801 | /* Some versions of less can survive w/o controlling tty, | 1876 | /* Some versions of less can survive w/o controlling tty, |
1802 | * try to do the same. This also allows to specify an alternative | 1877 | * try to do the same. This also allows to specify an alternative |
1803 | * tty via "less 1<>TTY". | 1878 | * tty via "less 1<>TTY". |
@@ -1823,6 +1898,9 @@ int less_main(int argc, char **argv) | |||
1823 | } | 1898 | } |
1824 | G.kbd_fd_orig_flags = ndelay_on(tty_fd); | 1899 | G.kbd_fd_orig_flags = ndelay_on(tty_fd); |
1825 | kbd_fd = tty_fd; /* save in a global */ | 1900 | kbd_fd = tty_fd; /* save in a global */ |
1901 | #else | ||
1902 | kbd_fd = 0; | ||
1903 | #endif | ||
1826 | 1904 | ||
1827 | tcgetattr(kbd_fd, &term_orig); | 1905 | tcgetattr(kbd_fd, &term_orig); |
1828 | term_less = term_orig; | 1906 | term_less = term_orig; |