15 |
*/ |
*/ |
16 |
|
|
17 |
#include "libbb.h" |
#include "libbb.h" |
|
#if ENABLE_FEATURE_USE_TERMIOS |
|
|
#include <termios.h> |
|
|
#endif /* FEATURE_USE_TERMIOS */ |
|
18 |
|
|
19 |
|
/* Support for FEATURE_USE_TERMIOS */ |
|
#if ENABLE_FEATURE_USE_TERMIOS |
|
20 |
|
|
21 |
struct globals { |
struct globals { |
22 |
int cin_fileno; |
int cin_fileno; |
29 |
#define new_settings (G.new_settings ) |
#define new_settings (G.new_settings ) |
30 |
#define cin_fileno (G.cin_fileno ) |
#define cin_fileno (G.cin_fileno ) |
31 |
|
|
32 |
#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp) |
#define setTermSettings(fd, argp) do { \ |
33 |
|
if (ENABLE_FEATURE_USE_TERMIOS) tcsetattr(fd, TCSANOW, argp); \ |
34 |
|
} while(0) |
35 |
#define getTermSettings(fd, argp) tcgetattr(fd, argp) |
#define getTermSettings(fd, argp) tcgetattr(fd, argp) |
36 |
|
|
37 |
static void gotsig(int sig UNUSED_PARAM) |
static void gotsig(int sig UNUSED_PARAM) |
41 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
42 |
} |
} |
43 |
|
|
|
#else /* !FEATURE_USE_TERMIOS */ |
|
|
#define INIT_G() ((void)0) |
|
|
#define setTermSettings(fd, argp) ((void)0) |
|
|
#endif /* FEATURE_USE_TERMIOS */ |
|
|
|
|
44 |
#define CONVERTED_TAB_SIZE 8 |
#define CONVERTED_TAB_SIZE 8 |
45 |
|
|
46 |
int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
69 |
if (!cin) |
if (!cin) |
70 |
return bb_cat(argv); |
return bb_cat(argv); |
71 |
|
|
72 |
#if ENABLE_FEATURE_USE_TERMIOS |
if (ENABLE_FEATURE_USE_TERMIOS) { |
73 |
cin_fileno = fileno(cin); |
cin_fileno = fileno(cin); |
74 |
getTermSettings(cin_fileno, &initial_settings); |
getTermSettings(cin_fileno, &initial_settings); |
75 |
new_settings = initial_settings; |
new_settings = initial_settings; |
76 |
new_settings.c_lflag &= ~ICANON; |
new_settings.c_lflag &= ~ICANON; |
77 |
new_settings.c_lflag &= ~ECHO; |
new_settings.c_lflag &= ~ECHO; |
78 |
new_settings.c_cc[VMIN] = 1; |
new_settings.c_cc[VMIN] = 1; |
79 |
new_settings.c_cc[VTIME] = 0; |
new_settings.c_cc[VTIME] = 0; |
80 |
setTermSettings(cin_fileno, &new_settings); |
setTermSettings(cin_fileno, &new_settings); |
81 |
bb_signals(0 |
bb_signals(0 |
82 |
+ (1 << SIGINT) |
+ (1 << SIGINT) |
83 |
+ (1 << SIGQUIT) |
+ (1 << SIGQUIT) |
84 |
+ (1 << SIGTERM) |
+ (1 << SIGTERM) |
85 |
, gotsig); |
, gotsig); |
86 |
#endif |
} |
87 |
|
|
88 |
do { |
do { |
89 |
file = stdin; |
file = stdin; |
110 |
if (input != 'r' && please_display_more_prompt) { |
if (input != 'r' && please_display_more_prompt) { |
111 |
len = printf("--More-- "); |
len = printf("--More-- "); |
112 |
if (st.st_size > 0) { |
if (st.st_size > 0) { |
113 |
len += printf("(%d%% of %"OFF_FMT"d bytes)", |
len += printf("(%u%% of %"OFF_FMT"u bytes)", |
114 |
(int) (ftello(file)*100 / st.st_size), |
(int) (ftello(file)*100 / st.st_size), |
115 |
st.st_size); |
st.st_size); |
116 |
} |
} |
117 |
fflush(stdout); |
fflush_all(); |
118 |
|
|
119 |
/* |
/* |
120 |
* We've just displayed the "--More--" prompt, so now we need |
* We've just displayed the "--More--" prompt, so now we need |
123 |
for (;;) { |
for (;;) { |
124 |
input = getc(cin); |
input = getc(cin); |
125 |
input = tolower(input); |
input = tolower(input); |
126 |
#if !ENABLE_FEATURE_USE_TERMIOS |
if (!ENABLE_FEATURE_USE_TERMIOS) |
127 |
printf("\033[A"); /* up cursor */ |
printf("\033[A"); /* cursor up */ |
|
#endif |
|
128 |
/* Erase the last message */ |
/* Erase the last message */ |
129 |
printf("\r%*s\r", len, ""); |
printf("\r%*s\r", len, ""); |
130 |
|
|
146 |
|
|
147 |
/* The user may have resized the terminal. |
/* The user may have resized the terminal. |
148 |
* Re-read the dimensions. */ |
* Re-read the dimensions. */ |
149 |
#if ENABLE_FEATURE_USE_TERMIOS |
if (ENABLE_FEATURE_USE_TERMIOS) { |
150 |
get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height); |
get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height); |
151 |
terminal_height -= 1; |
terminal_height -= 1; |
152 |
#endif |
} |
153 |
} |
} |
154 |
|
|
155 |
/* Crudely convert tabs into spaces, which are |
/* Crudely convert tabs into spaces, which are |
189 |
putchar(c); |
putchar(c); |
190 |
} |
} |
191 |
fclose(file); |
fclose(file); |
192 |
fflush(stdout); |
fflush_all(); |
193 |
} while (*argv && *++argv); |
} while (*argv && *++argv); |
194 |
end: |
end: |
195 |
setTermSettings(cin_fileno, &initial_settings); |
setTermSettings(cin_fileno, &initial_settings); |