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