less: reuse former vi's key reading code. Improve SIGWINCH handling.

function                                             old     new   delta
less_main                                           2056    2097     +41
getch_nowait                                         248     273     +25
read_key                                             310     321     +11
static.esccmds                                        61      69      +8
count_lines                                           72      74      +2
less_gets                                            166     142     -24
less_getch                                           172      43    -129
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 6/5 up/down: 91/-170)           Total: -79 bytes
   text    data     bss     dec     hex filename
This commit is contained in:
Denis Vlasenko 2008-10-25 23:27:29 +00:00
parent 39b0135c59
commit 5f6aaf39cf
4 changed files with 67 additions and 91 deletions

View File

@ -193,7 +193,7 @@ struct globals {
#if ENABLE_FEATURE_VI_CRASHME #if ENABLE_FEATURE_VI_CRASHME
char readbuffer[128]; char readbuffer[128];
#else #else
char readbuffer[8]; char readbuffer[KEYCODE_BUFFER_SIZE];
#endif #endif
#define STATUS_BUFFER_LEN 200 #define STATUS_BUFFER_LEN 200
char status_buffer[STATUS_BUFFER_LEN]; // messages to the user char status_buffer[STATUS_BUFFER_LEN]; // messages to the user

View File

@ -950,6 +950,8 @@ enum {
KEYCODE_FUN11 = -22, KEYCODE_FUN11 = -22,
KEYCODE_FUN12 = -23, KEYCODE_FUN12 = -23,
#endif #endif
/* How long the longest ESC sequence we know? */
KEYCODE_BUFFER_SIZE = 4
}; };
int read_key(int fd, smalluint *nbuffered, char *buffer) FAST_FUNC; int read_key(int fd, smalluint *nbuffered, char *buffer) FAST_FUNC;

View File

@ -33,14 +33,16 @@ int FAST_FUNC read_key(int fd, smalluint *nbuffered, char *buffer)
'[','B' |0x80,KEYCODE_DOWN , '[','B' |0x80,KEYCODE_DOWN ,
'[','C' |0x80,KEYCODE_RIGHT , '[','C' |0x80,KEYCODE_RIGHT ,
'[','D' |0x80,KEYCODE_LEFT , '[','D' |0x80,KEYCODE_LEFT ,
'[','H' |0x80,KEYCODE_HOME , '[','H' |0x80,KEYCODE_HOME , /* xterm */
'[','F' |0x80,KEYCODE_END , '[','F' |0x80,KEYCODE_END , /* xterm */
'[','1','~' |0x80,KEYCODE_HOME , '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
'[','2','~' |0x80,KEYCODE_INSERT , '[','2','~' |0x80,KEYCODE_INSERT ,
'[','3','~' |0x80,KEYCODE_DELETE , '[','3','~' |0x80,KEYCODE_DELETE ,
'[','4','~' |0x80,KEYCODE_END , '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
'[','5','~' |0x80,KEYCODE_PAGEUP , '[','5','~' |0x80,KEYCODE_PAGEUP ,
'[','6','~' |0x80,KEYCODE_PAGEDOWN, '[','6','~' |0x80,KEYCODE_PAGEDOWN,
'[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
'[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
#if 0 #if 0
'[','1','1','~'|0x80,KEYCODE_FUN1 , '[','1','1','~'|0x80,KEYCODE_FUN1 ,
'[','1','2','~'|0x80,KEYCODE_FUN2 , '[','1','2','~'|0x80,KEYCODE_FUN2 ,
@ -58,7 +60,9 @@ int FAST_FUNC read_key(int fd, smalluint *nbuffered, char *buffer)
0 0
}; };
n = *nbuffered; n = 0;
if (nbuffered)
n = *nbuffered;
if (n == 0) { if (n == 0) {
/* If no data, block waiting for input. If we read more /* If no data, block waiting for input. If we read more
* than the minimal ESC sequence size, the "n=0" below * than the minimal ESC sequence size, the "n=0" below
@ -141,6 +145,7 @@ int FAST_FUNC read_key(int fd, smalluint *nbuffered, char *buffer)
* by now. */ * by now. */
ret: ret:
*nbuffered = n; if (nbuffered)
*nbuffered = n;
return c; return c;
} }

View File

@ -36,34 +36,9 @@
/* The escape code to clear to end of line */ /* The escape code to clear to end of line */
#define CLEAR_2_EOL "\033[K" #define CLEAR_2_EOL "\033[K"
/* These are the escape sequences corresponding to special keys */
enum { enum {
REAL_KEY_UP = 'A',
REAL_KEY_DOWN = 'B',
REAL_KEY_RIGHT = 'C',
REAL_KEY_LEFT = 'D',
REAL_PAGE_UP = '5',
REAL_PAGE_DOWN = '6',
REAL_KEY_HOME = '7', // vt100? linux vt? or what?
REAL_KEY_END = '8',
REAL_KEY_HOME_ALT = '1', // ESC [1~ (vt100? linux vt? or what?)
REAL_KEY_END_ALT = '4', // ESC [4~
REAL_KEY_HOME_XTERM = 'H',
REAL_KEY_END_XTERM = 'F',
/* These are the special codes assigned by this program to the special keys */
KEY_UP = 20,
KEY_DOWN = 21,
KEY_RIGHT = 22,
KEY_LEFT = 23,
PAGE_UP = 24,
PAGE_DOWN = 25,
KEY_HOME = 26,
KEY_END = 27,
/* Absolute max of lines eaten */ /* Absolute max of lines eaten */
MAXLINES = CONFIG_FEATURE_LESS_MAXLINES, MAXLINES = CONFIG_FEATURE_LESS_MAXLINES,
/* This many "after the end" lines we will show (at max) */ /* This many "after the end" lines we will show (at max) */
TILDES = 1, TILDES = 1,
}; };
@ -133,6 +108,8 @@ struct globals {
#define max_displayed_line (G.max_displayed_line) #define max_displayed_line (G.max_displayed_line)
#define width (G.width ) #define width (G.width )
#define winch_counter (G.winch_counter ) #define winch_counter (G.winch_counter )
/* This one is 100% not cached by compiler on read access */
#define WINCH_COUNTER (*(volatile unsigned *)&winch_counter)
#define eof_error (G.eof_error ) #define eof_error (G.eof_error )
#define readpos (G.readpos ) #define readpos (G.readpos )
#define readeof (G.readeof ) #define readeof (G.readeof )
@ -824,9 +801,10 @@ static void reinitialize(void)
buffer_fill_and_print(); buffer_fill_and_print();
} }
static ssize_t getch_nowait(char* input, int sz) static ssize_t getch_nowait(void)
{ {
ssize_t rd; char input[KEYCODE_BUFFER_SIZE];
int rd;
struct pollfd pfd[2]; struct pollfd pfd[2];
pfd[0].fd = STDIN_FILENO; pfd[0].fd = STDIN_FILENO;
@ -842,34 +820,39 @@ static ssize_t getch_nowait(char* input, int sz)
* (switch fd into O_NONBLOCK'ed mode to avoid it) * (switch fd into O_NONBLOCK'ed mode to avoid it)
*/ */
rd = 1; rd = 1;
if (max_fline <= cur_fline + max_displayed_line /* Are we interested in stdin? */
&& eof_error > 0 /* did NOT reach eof yet */ //TODO: reuse code for determining this
if (!(option_mask32 & FLAG_S)
? !(max_fline > cur_fline + max_displayed_line)
: !(max_fline >= cur_fline
&& max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
) { ) {
/* We are interested in stdin */ if (eof_error > 0) /* did NOT reach eof yet */
rd = 0; rd = 0; /* yes, we are interested in stdin */
} }
/* position cursor if line input is done */ /* Position cursor if line input is done */
if (less_gets_pos >= 0) if (less_gets_pos >= 0)
move_cursor(max_displayed_line + 2, less_gets_pos + 1); move_cursor(max_displayed_line + 2, less_gets_pos + 1);
fflush(stdout); fflush(stdout);
#if ENABLE_FEATURE_LESS_WINCH #if ENABLE_FEATURE_LESS_WINCH
while (1) { while (1) {
int r; int r;
/* NB: SIGWINCH interrupts poll() */
r = poll(pfd + rd, 2 - rd, -1); r = poll(pfd + rd, 2 - rd, -1);
if (/*r < 0 && errno == EINTR &&*/ winch_counter) { if (/*r < 0 && errno == EINTR &&*/ winch_counter)
input[0] = '\\'; /* anything which has no defined function */ return '\\'; /* anything which has no defined function */
return 1;
}
if (r) break; if (r) break;
} }
#else #else
safe_poll(pfd + rd, 2 - rd, -1); safe_poll(pfd + rd, 2 - rd, -1);
#endif #endif
input[0] = '\0'; /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
rd = safe_read(kbd_fd, input, sz); /* NB: kbd_fd is in O_NONBLOCK mode */ * would not block even if there is no input available */
if (rd < 0 && errno == EAGAIN) { rd = read_key(kbd_fd, NULL, input);
/* No keyboard input -> we have input on stdin! */ if (rd == -1 && errno == EAGAIN) {
/* No keyboard input available. Since poll() did return,
* we should have input on stdin */
read_lines(); read_lines();
buffer_fill_and_print(); buffer_fill_and_print();
goto again; goto again;
@ -883,51 +866,29 @@ static ssize_t getch_nowait(char* input, int sz)
* special return codes. Note that this function works best with raw input. */ * special return codes. Note that this function works best with raw input. */
static int less_getch(int pos) static int less_getch(int pos)
{ {
unsigned char input[16]; int i;
unsigned i;
again: again:
less_gets_pos = pos; less_gets_pos = pos;
memset(input, 0, sizeof(input)); i = getch_nowait();
getch_nowait((char *)input, sizeof(input));
less_gets_pos = -1; less_gets_pos = -1;
/* Detect escape sequences (i.e. arrow keys) and handle /* Discard Ctrl-something chars */
* them accordingly */ if (i >= 0 && i < ' ' && i != 0x0d && i != 8)
if (input[0] == '\033' && input[1] == '[') {
i = input[2] - REAL_KEY_UP;
if (i < 4)
return 20 + i;
i = input[2] - REAL_PAGE_UP;
if (i < 4)
return 24 + i;
if (input[2] == REAL_KEY_HOME_XTERM)
return KEY_HOME;
if (input[2] == REAL_KEY_HOME_ALT)
return KEY_HOME;
if (input[2] == REAL_KEY_END_XTERM)
return KEY_END;
if (input[2] == REAL_KEY_END_ALT)
return KEY_END;
return 0;
}
/* Reject almost all control chars */
i = input[0];
if (i < ' ' && i != 0x0d && i != 8)
goto again; goto again;
return i; return i;
} }
static char* less_gets(int sz) static char* less_gets(int sz)
{ {
char c; int c;
unsigned i = 0; unsigned i = 0;
char *result = xzalloc(1); char *result = xzalloc(1);
while (1) { while (1) {
c = '\0'; c = '\0';
less_gets_pos = sz + i; less_gets_pos = sz + i;
getch_nowait(&c, 1); c = getch_nowait();
if (c == 0x0d) { if (c == 0x0d) {
result[i] = '\0'; result[i] = '\0';
less_gets_pos = -1; less_gets_pos = -1;
@ -939,7 +900,7 @@ static char* less_gets(int sz)
printf("\x8 \x8"); printf("\x8 \x8");
i--; i--;
} }
if (c < ' ') if (c < ' ') /* filters out KEYCODE_xxx too (<0) */
continue; continue;
if (i >= width - sz - 1) if (i >= width - sz - 1)
continue; /* len limit */ continue; /* len limit */
@ -1151,8 +1112,8 @@ static void number_process(int first_digit)
{ {
unsigned i; unsigned i;
int num; int num;
int keypress;
char num_input[sizeof(int)*4]; /* more than enough */ char num_input[sizeof(int)*4]; /* more than enough */
char keypress;
num_input[0] = first_digit; num_input[0] = first_digit;
@ -1163,15 +1124,14 @@ static void number_process(int first_digit)
/* Receive input until a letter is given */ /* Receive input until a letter is given */
i = 1; i = 1;
while (i < sizeof(num_input)-1) { while (i < sizeof(num_input)-1) {
num_input[i] = less_getch(i + 1); keypress = less_getch(i + 1);
if (!num_input[i] || !isdigit(num_input[i])) if ((unsigned)keypress > 255 || !isdigit(num_input[i]))
break; break;
bb_putchar(num_input[i]); num_input[i] = keypress;
bb_putchar(keypress);
i++; i++;
} }
/* Take the final letter out of the digits string */
keypress = num_input[i];
num_input[i] = '\0'; num_input[i] = '\0';
num = bb_strtou(num_input, NULL, 10); num = bb_strtou(num_input, NULL, 10);
/* on format error, num == -1 */ /* on format error, num == -1 */
@ -1182,10 +1142,10 @@ static void number_process(int first_digit)
/* We now know the number and the letter entered, so we process them */ /* We now know the number and the letter entered, so we process them */
switch (keypress) { switch (keypress) {
case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015':
buffer_down(num); buffer_down(num);
break; break;
case KEY_UP: case 'b': case 'w': case 'y': case 'u': case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u':
buffer_up(num); buffer_up(num);
break; break;
case 'g': case '<': case 'G': case '>': case 'g': case '<': case 'G': case '>':
@ -1413,16 +1373,16 @@ static void match_left_bracket(char bracket)
static void keypress_process(int keypress) static void keypress_process(int keypress)
{ {
switch (keypress) { switch (keypress) {
case KEY_DOWN: case 'e': case 'j': case 0x0d: case KEYCODE_DOWN: case 'e': case 'j': case 0x0d:
buffer_down(1); buffer_down(1);
break; break;
case KEY_UP: case 'y': case 'k': case KEYCODE_UP: case 'y': case 'k':
buffer_up(1); buffer_up(1);
break; break;
case PAGE_DOWN: case ' ': case 'z': case 'f': case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f':
buffer_down(max_displayed_line + 1); buffer_down(max_displayed_line + 1);
break; break;
case PAGE_UP: case 'w': case 'b': case KEYCODE_PAGEUP: case 'w': case 'b':
buffer_up(max_displayed_line + 1); buffer_up(max_displayed_line + 1);
break; break;
case 'd': case 'd':
@ -1431,10 +1391,10 @@ static void keypress_process(int keypress)
case 'u': case 'u':
buffer_up((max_displayed_line + 1) / 2); buffer_up((max_displayed_line + 1) / 2);
break; break;
case KEY_HOME: case 'g': case 'p': case '<': case '%': case KEYCODE_HOME: case 'g': case 'p': case '<': case '%':
buffer_line(0); buffer_line(0);
break; break;
case KEY_END: case 'G': case '>': case KEYCODE_END: case 'G': case '>':
cur_fline = MAXLINES; cur_fline = MAXLINES;
read_lines(); read_lines();
buffer_line(cur_fline); buffer_line(cur_fline);
@ -1586,7 +1546,8 @@ int less_main(int argc, char **argv)
reinitialize(); reinitialize();
while (1) { while (1) {
#if ENABLE_FEATURE_LESS_WINCH #if ENABLE_FEATURE_LESS_WINCH
if (winch_counter) { while (WINCH_COUNTER) {
again:
winch_counter--; winch_counter--;
get_terminal_width_height(kbd_fd, &width, &max_displayed_line); get_terminal_width_height(kbd_fd, &width, &max_displayed_line);
/* 20: two tabstops + 4 */ /* 20: two tabstops + 4 */
@ -1597,8 +1558,16 @@ int less_main(int argc, char **argv)
max_displayed_line -= 2; max_displayed_line -= 2;
free(buffer); free(buffer);
buffer = xmalloc((max_displayed_line+1) * sizeof(char *)); buffer = xmalloc((max_displayed_line+1) * sizeof(char *));
/* Avoid re-wrap and/or redraw if we already know
* we need to do it again. These ops are expensive */
if (WINCH_COUNTER)
goto again;
re_wrap(); re_wrap();
if (WINCH_COUNTER)
goto again;
buffer_fill_and_print(); buffer_fill_and_print();
/* This took some time. Loop back and check,
* were there another SIGWINCH? */
} }
#endif #endif
keypress = less_getch(-1); /* -1: do not position cursor */ keypress = less_getch(-1); /* -1: do not position cursor */