aboutsummaryrefslogtreecommitdiff
path: root/miscutils/less.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--miscutils/less.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index e90691b49..8f9b329ba 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -130,6 +130,10 @@
130 130
131#include <sched.h> /* sched_yield() */ 131#include <sched.h> /* sched_yield() */
132 132
133#if ENABLE_PLATFORM_MINGW32
134#include <conio.h>
135#endif
136
133#include "libbb.h" 137#include "libbb.h"
134#include "common_bufsiz.h" 138#include "common_bufsiz.h"
135#if ENABLE_FEATURE_LESS_REGEXP 139#if ENABLE_FEATURE_LESS_REGEXP
@@ -536,6 +540,11 @@ static void read_lines(void)
536 last_line_pos = 0; 540 last_line_pos = 0;
537 break; 541 break;
538 } 542 }
543#if ENABLE_PLATFORM_MINGW32
544 if (c == '\r') {
545 continue;
546 }
547#endif
539 /* NUL is substituted by '\n'! */ 548 /* NUL is substituted by '\n'! */
540 if (c == '\0') c = '\n'; 549 if (c == '\0') c = '\n';
541 *p++ = c; 550 *p++ = c;
@@ -632,7 +641,12 @@ static void update_num_lines(void)
632 /* only do this for regular files */ 641 /* only do this for regular files */
633 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) { 642 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
634 count = 0; 643 count = 0;
644#if !ENABLE_PLATFORM_MINGW32
635 fd = open("/proc/self/fd/0", O_RDONLY); 645 fd = open("/proc/self/fd/0", O_RDONLY);
646#else
647 /* don't even try to access /proc on WIN32 */
648 fd = -1;
649#endif
636 if (fd < 0 && num_lines == REOPEN_AND_COUNT) { 650 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
637 /* "filename" is valid only if REOPEN_AND_COUNT */ 651 /* "filename" is valid only if REOPEN_AND_COUNT */
638 fd = open(filename, O_RDONLY); 652 fd = open(filename, O_RDONLY);
@@ -815,7 +829,12 @@ static void print_found(const char *line)
815 match_status = 1; 829 match_status = 1;
816 } 830 }
817 831
832#if !ENABLE_PLATFORM_MINGW32
818 printf("%s%s\n", growline ? growline : "", str); 833 printf("%s%s\n", growline ? growline : "", str);
834#else
835 /* skip newline, we use explicit positioning on WIN32 */
836 printf("%s%s", growline ? growline : "", str);
837#endif
819 free(growline); 838 free(growline);
820} 839}
821#else 840#else
@@ -851,7 +870,12 @@ static void print_ascii(const char *str)
851 *p = '\0'; 870 *p = '\0';
852 print_hilite(buf); 871 print_hilite(buf);
853 } 872 }
873#if !ENABLE_PLATFORM_MINGW32
854 puts(str); 874 puts(str);
875#else
876 /* skip newline, we use explicit positioning on WIN32 */
877 printf("%s", str);
878#endif
855} 879}
856 880
857/* Print the buffer */ 881/* Print the buffer */
@@ -861,6 +885,10 @@ static void buffer_print(void)
861 885
862 move_cursor(0, 0); 886 move_cursor(0, 0);
863 for (i = 0; i <= max_displayed_line; i++) { 887 for (i = 0; i <= max_displayed_line; i++) {
888#if ENABLE_PLATFORM_MINGW32
889 /* make sure we're on the right line */
890 move_cursor(i+1, 0);
891#endif
864 printf(CLEAR_2_EOL); 892 printf(CLEAR_2_EOL);
865 if (option_mask32 & FLAG_N) 893 if (option_mask32 & FLAG_N)
866 print_lineno(buffer[i]); 894 print_lineno(buffer[i]);
@@ -1047,9 +1075,13 @@ static void reinitialize(void)
1047 if (G.winsize_err) 1075 if (G.winsize_err)
1048 printf("\033[999;999H" "\033[6n"); 1076 printf("\033[999;999H" "\033[6n");
1049#endif 1077#endif
1078#if ENABLE_PLATFORM_MINGW32
1079 reset_screen();
1080#endif
1050 buffer_fill_and_print(); 1081 buffer_fill_and_print();
1051} 1082}
1052 1083
1084#if !ENABLE_PLATFORM_MINGW32
1053static int64_t getch_nowait(void) 1085static int64_t getch_nowait(void)
1054{ 1086{
1055 int rd; 1087 int rd;
@@ -1111,6 +1143,46 @@ static int64_t getch_nowait(void)
1111 set_tty_cooked(); 1143 set_tty_cooked();
1112 return key64; 1144 return key64;
1113} 1145}
1146#else
1147static int64_t getch_nowait(void)
1148{
1149 int64_t c;
1150
1151retry:
1152 c = _getch();
1153 if (c == 0 || c == 0xe0) {
1154 switch (_getch()) {
1155 case 0x48:
1156 c = KEYCODE_UP;
1157 break;
1158 case 0x50:
1159 c = KEYCODE_DOWN;
1160 break;
1161 case 0x49:
1162 c = KEYCODE_PAGEUP;
1163 break;
1164 case 0x51:
1165 c = KEYCODE_PAGEDOWN;
1166 break;
1167 case 0x47:
1168 c = KEYCODE_HOME;
1169 break;
1170 case 0x4f:
1171 c = KEYCODE_END;
1172 break;
1173 default:
1174 goto retry;
1175 }
1176 }
1177
1178 /* Position cursor if line input is done */
1179 if (less_gets_pos >= 0)
1180 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
1181 fflush_all();
1182
1183 return c;
1184}
1185#endif
1114 1186
1115/* Grab a character from input without requiring the return key. 1187/* Grab a character from input without requiring the return key.
1116 * May return KEYCODE_xxx values. 1188 * May return KEYCODE_xxx values.
@@ -1766,8 +1838,10 @@ static void sigwinch_handler(int sig UNUSED_PARAM)
1766int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 1838int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1767int less_main(int argc, char **argv) 1839int less_main(int argc, char **argv)
1768{ 1840{
1841#if !ENABLE_PLATFORM_MINGW32
1769 char *tty_name; 1842 char *tty_name;
1770 int tty_fd; 1843 int tty_fd;
1844#endif
1771 1845
1772 INIT_G(); 1846 INIT_G();
1773 1847
@@ -1801,6 +1875,7 @@ int less_main(int argc, char **argv)
1801 if (option_mask32 & FLAG_TILDE) 1875 if (option_mask32 & FLAG_TILDE)
1802 empty_line_marker = ""; 1876 empty_line_marker = "";
1803 1877
1878#if !ENABLE_PLATFORM_MINGW32
1804 /* Some versions of less can survive w/o controlling tty, 1879 /* Some versions of less can survive w/o controlling tty,
1805 * try to do the same. This also allows to specify an alternative 1880 * try to do the same. This also allows to specify an alternative
1806 * tty via "less 1<>TTY". 1881 * tty via "less 1<>TTY".
@@ -1826,6 +1901,9 @@ int less_main(int argc, char **argv)
1826 } 1901 }
1827 G.kbd_fd_orig_flags = ndelay_on(tty_fd); 1902 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
1828 kbd_fd = tty_fd; /* save in a global */ 1903 kbd_fd = tty_fd; /* save in a global */
1904#else
1905 kbd_fd = 0;
1906#endif
1829 1907
1830 tcgetattr(kbd_fd, &term_orig); 1908 tcgetattr(kbd_fd, &term_orig);
1831 term_less = term_orig; 1909 term_less = term_orig;