more: by Tristan Schmelcher <tpkschme@engmail.uwaterloo.ca>:
- Fixed a number of deficiencies in the line wrapping. - Fixed a bug where the page could scroll multiple times per keypress. - Re-read terminal dimensions after user input, in case they resized the terminal while we were waiting. - Added an 'R' key to print the rest of the file without prompting. - Crudely support tabs in the line-wrapping code by converting them to 8 spaces.
This commit is contained in:
parent
ab9c44b1a4
commit
033e592cbc
@ -29,11 +29,11 @@ struct globals {
|
|||||||
};
|
};
|
||||||
#define G (*(struct globals*)bb_common_bufsiz1)
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
||||||
//#define G (*ptr_to_globals)
|
//#define G (*ptr_to_globals)
|
||||||
|
#define INIT_G() ((void)0)
|
||||||
|
//#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
|
||||||
#define initial_settings (G.initial_settings)
|
#define initial_settings (G.initial_settings)
|
||||||
#define new_settings (G.new_settings )
|
#define new_settings (G.new_settings )
|
||||||
#define cin_fileno (G.cin_fileno )
|
#define cin_fileno (G.cin_fileno )
|
||||||
#define INIT_G() ((void)0)
|
|
||||||
//#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
|
|
||||||
|
|
||||||
#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
|
#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
|
||||||
#define getTermSettings(fd, argp) tcgetattr(fd, argp)
|
#define getTermSettings(fd, argp) tcgetattr(fd, argp)
|
||||||
@ -50,16 +50,20 @@ static void gotsig(int sig)
|
|||||||
#define setTermSettings(fd, argp) ((void)0)
|
#define setTermSettings(fd, argp) ((void)0)
|
||||||
#endif /* FEATURE_USE_TERMIOS */
|
#endif /* FEATURE_USE_TERMIOS */
|
||||||
|
|
||||||
|
#define CONVERTED_TAB_SIZE 8
|
||||||
|
|
||||||
int more_main(int argc, char **argv);
|
int more_main(int argc, char **argv);
|
||||||
int more_main(int argc, char **argv)
|
int more_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c, lines, input = 0;
|
int c = c; /* for gcc */
|
||||||
int please_display_more_prompt = 0;
|
int lines;
|
||||||
|
int input = 0;
|
||||||
|
int spaces = 0;
|
||||||
|
int please_display_more_prompt;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
FILE *file;
|
FILE *file;
|
||||||
FILE *cin;
|
FILE *cin;
|
||||||
int len, page_height;
|
int len;
|
||||||
int terminal_width;
|
int terminal_width;
|
||||||
int terminal_height;
|
int terminal_height;
|
||||||
|
|
||||||
@ -87,7 +91,6 @@ int more_main(int argc, char **argv)
|
|||||||
signal(SIGQUIT, gotsig);
|
signal(SIGQUIT, gotsig);
|
||||||
signal(SIGTERM, gotsig);
|
signal(SIGTERM, gotsig);
|
||||||
#endif
|
#endif
|
||||||
please_display_more_prompt = 2;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
file = stdin;
|
file = stdin;
|
||||||
@ -99,19 +102,21 @@ int more_main(int argc, char **argv)
|
|||||||
st.st_size = 0;
|
st.st_size = 0;
|
||||||
fstat(fileno(file), &st);
|
fstat(fileno(file), &st);
|
||||||
|
|
||||||
please_display_more_prompt &= ~1;
|
please_display_more_prompt = 0;
|
||||||
/* never returns w, h <= 1 */
|
/* never returns w, h <= 1 */
|
||||||
get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
|
get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
|
||||||
terminal_width -= 1;
|
|
||||||
terminal_height -= 1;
|
terminal_height -= 1;
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
lines = 0;
|
lines = 0;
|
||||||
page_height = terminal_height;
|
while (spaces || (c = getc(file)) != EOF) {
|
||||||
while ((c = getc(file)) != EOF) {
|
int wrap;
|
||||||
if ((please_display_more_prompt & 3) == 3) {
|
if (spaces)
|
||||||
|
spaces--;
|
||||||
|
loop_top:
|
||||||
|
if (input != 'r' && please_display_more_prompt) {
|
||||||
len = printf("--More-- ");
|
len = printf("--More-- ");
|
||||||
if (/*file != stdin &&*/ st.st_size > 0) {
|
if (st.st_size > 0) {
|
||||||
len += printf("(%d%% of %"OFF_FMT"d bytes)",
|
len += printf("(%d%% of %"OFF_FMT"d bytes)",
|
||||||
(int) (ftello(file)*100 / st.st_size),
|
(int) (ftello(file)*100 / st.st_size),
|
||||||
st.st_size);
|
st.st_size);
|
||||||
@ -122,23 +127,44 @@ int more_main(int argc, char **argv)
|
|||||||
* We've just displayed the "--More--" prompt, so now we need
|
* We've just displayed the "--More--" prompt, so now we need
|
||||||
* to get input from the user.
|
* to get input from the user.
|
||||||
*/
|
*/
|
||||||
input = getc(cin);
|
for (;;) {
|
||||||
|
input = getc(cin);
|
||||||
|
input = tolower(input);
|
||||||
#if !ENABLE_FEATURE_USE_TERMIOS
|
#if !ENABLE_FEATURE_USE_TERMIOS
|
||||||
printf("\033[A"); /* up cursor */
|
printf("\033[A"); /* up cursor */
|
||||||
#endif
|
#endif
|
||||||
/* Erase the "More" message */
|
/* Erase the last message */
|
||||||
printf("\r%*s\r", len, "");
|
printf("\r%*s\r", len, "");
|
||||||
|
|
||||||
|
/* Due to various multibyte escape
|
||||||
|
* sequences, it's not ok to accept
|
||||||
|
* any input as a command to scroll
|
||||||
|
* the screen. We only allow known
|
||||||
|
* commands, else we show help msg. */
|
||||||
|
if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
|
||||||
|
break;
|
||||||
|
len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
|
||||||
|
}
|
||||||
len = 0;
|
len = 0;
|
||||||
lines = 0;
|
lines = 0;
|
||||||
/* Bottom line on page will become top line
|
please_display_more_prompt = 0;
|
||||||
* after one page forward. Thus -1: */
|
|
||||||
page_height = terminal_height - 1;
|
|
||||||
please_display_more_prompt &= ~1;
|
|
||||||
|
|
||||||
if (input == 'q')
|
if (input == 'q')
|
||||||
goto end;
|
goto end;
|
||||||
|
|
||||||
|
/* The user may have resized the terminal.
|
||||||
|
* Re-read the dimensions. */
|
||||||
|
get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
|
||||||
|
terminal_height -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Crudely convert tabs into spaces, which are
|
||||||
|
* a bajillion times easier to deal with. */
|
||||||
|
if (c == '\t') {
|
||||||
|
spaces = CONVERTED_TAB_SIZE - 1;
|
||||||
|
c = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* There are two input streams to worry about here:
|
* There are two input streams to worry about here:
|
||||||
*
|
*
|
||||||
@ -149,34 +175,24 @@ int more_main(int argc, char **argv)
|
|||||||
* see if any characters have been hit in the _input_ stream. This
|
* see if any characters have been hit in the _input_ stream. This
|
||||||
* allows the user to quit while in the middle of a file.
|
* allows the user to quit while in the middle of a file.
|
||||||
*/
|
*/
|
||||||
if (c == '\n') {
|
wrap = (++len > terminal_width);
|
||||||
/* increment by just one line if we are at
|
if (c == '\n' || wrap) {
|
||||||
* the end of this line */
|
/* Then outputting this character
|
||||||
if (input == '\n')
|
* will move us to a new line. */
|
||||||
please_display_more_prompt |= 1;
|
if (++lines >= terminal_height || input == '\n')
|
||||||
/* Adjust the terminal height for any overlap, so that
|
please_display_more_prompt = 1;
|
||||||
* no lines get lost off the top. */
|
|
||||||
if (len >= terminal_width) {
|
|
||||||
int quot, rem;
|
|
||||||
quot = len / terminal_width;
|
|
||||||
rem = len - (quot * terminal_width);
|
|
||||||
page_height -= (quot - 1);
|
|
||||||
if (rem)
|
|
||||||
page_height--;
|
|
||||||
}
|
|
||||||
if (++lines >= page_height) {
|
|
||||||
please_display_more_prompt |= 1;
|
|
||||||
}
|
|
||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
/*
|
if (c != '\n' && wrap) {
|
||||||
* If we just read a newline from the file being 'mored' and any
|
/* Then outputting this will also put a character on
|
||||||
* key other than a return is hit, scroll by one page
|
* the beginning of that new line. Thus we first want to
|
||||||
*/
|
* display the prompt (if any), so we skip the putchar()
|
||||||
putc(c, stdout);
|
* and go back to the top of the loop, without reading
|
||||||
/* My small mind cannot fathom tabs, backspaces,
|
* a new character. */
|
||||||
* and UTF-8 */
|
goto loop_top;
|
||||||
len++;
|
}
|
||||||
|
/* My small mind cannot fathom backspaces and UTF-8 */
|
||||||
|
putchar(c);
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
Loading…
Reference in New Issue
Block a user