aboutsummaryrefslogtreecommitdiff
path: root/miscutils/less.c
diff options
context:
space:
mode:
Diffstat (limited to 'miscutils/less.c')
-rw-r--r--miscutils/less.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index 223c2558d..ad23b7d0d 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -145,6 +145,10 @@
145 145
146#include <sched.h> /* sched_yield() */ 146#include <sched.h> /* sched_yield() */
147 147
148#if ENABLE_PLATFORM_MINGW32
149#include <conio.h>
150#endif
151
148#include "libbb.h" 152#include "libbb.h"
149#include "common_bufsiz.h" 153#include "common_bufsiz.h"
150#if ENABLE_FEATURE_LESS_REGEXP 154#if ENABLE_FEATURE_LESS_REGEXP
@@ -236,7 +240,9 @@ struct globals {
236 smallint winsize_err; 240 smallint winsize_err;
237#endif 241#endif
238 smallint terminated; 242 smallint terminated;
243#if !ENABLE_PLATFORM_MINGW32
239 struct termios term_orig, term_less; 244 struct termios term_orig, term_less;
245#endif
240 char kbd_input[KEYCODE_BUFFER_SIZE]; 246 char kbd_input[KEYCODE_BUFFER_SIZE];
241}; 247};
242#define G (*ptr_to_globals) 248#define G (*ptr_to_globals)
@@ -298,7 +304,9 @@ struct globals {
298static void set_tty_cooked(void) 304static void set_tty_cooked(void)
299{ 305{
300 fflush_all(); 306 fflush_all();
307#if !ENABLE_PLATFORM_MINGW32
301 tcsetattr(kbd_fd, TCSANOW, &term_orig); 308 tcsetattr(kbd_fd, TCSANOW, &term_orig);
309#endif
302} 310}
303 311
304/* Move the cursor to a position (x,y), where (0,0) is the 312/* Move the cursor to a position (x,y), where (0,0) is the
@@ -575,6 +583,11 @@ static void read_lines(void)
575 last_line_pos = 0; 583 last_line_pos = 0;
576 break; 584 break;
577 } 585 }
586#if ENABLE_PLATFORM_MINGW32
587 if (c == '\r') {
588 continue;
589 }
590#endif
578 /* NUL is substituted by '\n'! */ 591 /* NUL is substituted by '\n'! */
579 if (c == '\0') c = '\n'; 592 if (c == '\0') c = '\n';
580 *p++ = c; 593 *p++ = c;
@@ -671,7 +684,12 @@ static void update_num_lines(void)
671 /* only do this for regular files */ 684 /* only do this for regular files */
672 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) { 685 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
673 count = 0; 686 count = 0;
687#if !ENABLE_PLATFORM_MINGW32
674 fd = open("/proc/self/fd/0", O_RDONLY); 688 fd = open("/proc/self/fd/0", O_RDONLY);
689#else
690 /* don't even try to access /proc on WIN32 */
691 fd = -1;
692#endif
675 if (fd < 0 && num_lines == REOPEN_AND_COUNT) { 693 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
676 /* "filename" is valid only if REOPEN_AND_COUNT */ 694 /* "filename" is valid only if REOPEN_AND_COUNT */
677 fd = open(filename, O_RDONLY); 695 fd = open(filename, O_RDONLY);
@@ -854,7 +872,12 @@ static void print_found(const char *line)
854 match_status = 1; 872 match_status = 1;
855 } 873 }
856 874
875#if !ENABLE_PLATFORM_MINGW32
857 printf("%s%s\n", growline ? growline : "", str); 876 printf("%s%s\n", growline ? growline : "", str);
877#else
878 /* skip newline, we use explicit positioning on WIN32 */
879 printf("%s%s", growline ? growline : "", str);
880#endif
858 free(growline); 881 free(growline);
859} 882}
860#else 883#else
@@ -890,7 +913,12 @@ static void print_ascii(const char *str)
890 *p = '\0'; 913 *p = '\0';
891 print_hilite(buf); 914 print_hilite(buf);
892 } 915 }
916#if !ENABLE_PLATFORM_MINGW32
893 puts(str); 917 puts(str);
918#else
919 /* skip newline, we use explicit positioning on WIN32 */
920 printf("%s", str);
921#endif
894} 922}
895 923
896/* Print the buffer */ 924/* Print the buffer */
@@ -900,6 +928,10 @@ static void buffer_print(void)
900 928
901 move_cursor(0, 0); 929 move_cursor(0, 0);
902 for (i = 0; i <= max_displayed_line; i++) { 930 for (i = 0; i <= max_displayed_line; i++) {
931#if ENABLE_PLATFORM_MINGW32
932 /* make sure we're on the right line */
933 move_cursor(i+1, 0);
934#endif
903 printf(CLEAR_2_EOL); 935 printf(CLEAR_2_EOL);
904 if (option_mask32 & FLAG_N) 936 if (option_mask32 & FLAG_N)
905 print_lineno(buffer[i]); 937 print_lineno(buffer[i]);
@@ -1087,9 +1119,13 @@ static void reinitialize(void)
1087 if (G.winsize_err) 1119 if (G.winsize_err)
1088 printf(ESC"[999;999H" ESC"[6n"); 1120 printf(ESC"[999;999H" ESC"[6n");
1089#endif 1121#endif
1122#if ENABLE_PLATFORM_MINGW32
1123 reset_screen();
1124#endif
1090 buffer_fill_and_print(); 1125 buffer_fill_and_print();
1091} 1126}
1092 1127
1128#if !ENABLE_PLATFORM_MINGW32
1093static int64_t getch_nowait(void) 1129static int64_t getch_nowait(void)
1094{ 1130{
1095 int rd; 1131 int rd;
@@ -1151,6 +1187,46 @@ static int64_t getch_nowait(void)
1151 set_tty_cooked(); 1187 set_tty_cooked();
1152 return key64; 1188 return key64;
1153} 1189}
1190#else
1191static int64_t getch_nowait(void)
1192{
1193 int64_t c;
1194
1195retry:
1196 c = _getch();
1197 if (c == 0 || c == 0xe0) {
1198 switch (_getch()) {
1199 case 0x48:
1200 c = KEYCODE_UP;
1201 break;
1202 case 0x50:
1203 c = KEYCODE_DOWN;
1204 break;
1205 case 0x49:
1206 c = KEYCODE_PAGEUP;
1207 break;
1208 case 0x51:
1209 c = KEYCODE_PAGEDOWN;
1210 break;
1211 case 0x47:
1212 c = KEYCODE_HOME;
1213 break;
1214 case 0x4f:
1215 c = KEYCODE_END;
1216 break;
1217 default:
1218 goto retry;
1219 }
1220 }
1221
1222 /* Position cursor if line input is done */
1223 if (less_gets_pos >= 0)
1224 move_cursor(max_displayed_line + 2, less_gets_pos + 1);
1225 fflush_all();
1226
1227 return c;
1228}
1229#endif
1154 1230
1155/* Grab a character from input without requiring the return key. 1231/* Grab a character from input without requiring the return key.
1156 * May return KEYCODE_xxx values. 1232 * May return KEYCODE_xxx values.
@@ -1791,10 +1867,12 @@ static void keypress_process(int keypress)
1791 number_process(keypress); 1867 number_process(keypress);
1792} 1868}
1793 1869
1870#if !ENABLE_PLATFORM_MINGW32
1794static void sig_catcher(int sig) 1871static void sig_catcher(int sig)
1795{ 1872{
1796 less_exit(- sig); 1873 less_exit(- sig);
1797} 1874}
1875#endif
1798 1876
1799#if ENABLE_FEATURE_LESS_WINCH 1877#if ENABLE_FEATURE_LESS_WINCH
1800static void sigwinch_handler(int sig UNUSED_PARAM) 1878static void sigwinch_handler(int sig UNUSED_PARAM)
@@ -1806,7 +1884,9 @@ static void sigwinch_handler(int sig UNUSED_PARAM)
1806int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 1884int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1807int less_main(int argc, char **argv) 1885int less_main(int argc, char **argv)
1808{ 1886{
1887#if !ENABLE_PLATFORM_MINGW32
1809 char *tty_name; 1888 char *tty_name;
1889#endif
1810 int tty_fd; 1890 int tty_fd;
1811 1891
1812 INIT_G(); 1892 INIT_G();
@@ -1865,6 +1945,7 @@ int less_main(int argc, char **argv)
1865 if (option_mask32 & FLAG_TILDE) 1945 if (option_mask32 & FLAG_TILDE)
1866 empty_line_marker = ""; 1946 empty_line_marker = "";
1867 1947
1948#if !ENABLE_PLATFORM_MINGW32
1868 /* Some versions of less can survive w/o controlling tty, 1949 /* Some versions of less can survive w/o controlling tty,
1869 * try to do the same. This also allows to specify an alternative 1950 * try to do the same. This also allows to specify an alternative
1870 * tty via "less 1<>TTY". 1951 * tty via "less 1<>TTY".
@@ -1890,8 +1971,13 @@ int less_main(int argc, char **argv)
1890 } 1971 }
1891 G.kbd_fd_orig_flags = ndelay_on(tty_fd); 1972 G.kbd_fd_orig_flags = ndelay_on(tty_fd);
1892 kbd_fd = tty_fd; /* save in a global */ 1973 kbd_fd = tty_fd; /* save in a global */
1974#else
1975 kbd_fd = tty_fd = 0;
1976#endif
1893 1977
1978#if !ENABLE_PLATFORM_MINGW32
1894 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL_INPUT); 1979 get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL_INPUT);
1980#endif
1895 1981
1896 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line); 1982 IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line);
1897 /* 20: two tabstops + 4 */ 1983 /* 20: two tabstops + 4 */