Exemplo n.º 1
0
/* Grab a character from input without requiring the return key. If the
   character is ASCII \033, get more characters and assign certain sequences
   special return codes. Note that this function works best with raw input. */
static int tless_getch(void)
{
	int input;

	set_tty_raw();

	input = getc(inp);
	/* Detect escape sequences (i.e. arrow keys) and handle
	   them accordingly */

	if (input == '\033' && getc(inp) == '[') {
		input = getc(inp);
		set_tty_cooked();
		if (input == REAL_KEY_UP)
			return KEY_UP;
		else if (input == REAL_KEY_DOWN)
			return KEY_DOWN;
		else if (input == REAL_KEY_RIGHT)
			return KEY_RIGHT;
		else if (input == REAL_KEY_LEFT)
			return KEY_LEFT;
		else if (input == REAL_PAGE_UP)
			return PAGE_UP;
		else if (input == REAL_PAGE_DOWN)
			return PAGE_DOWN;
	}
	/* The input is a normal ASCII value */
	else {
		set_tty_cooked();
		return input;
	}
	return 0;
}
Exemplo n.º 2
0
/* Exit the program gracefully */
static void less_exit(int code)
{
	set_tty_cooked();
	clear_line();
	if (code < 0)
		kill_myself_with_sig(- code); /* does not return */
	exit(code);
}
Exemplo n.º 3
0
Arquivo: less.c Projeto: OPSF/uClinux
/* Exit the program gracefully */
static void less_exit(int code)
{
    bb_putchar('\n');
    set_tty_cooked();
    if (code < 0)
        kill_myself_with_sig(- code); /* does not return */
    exit(code);
}
Exemplo n.º 4
0
Arquivo: less.c Projeto: OPSF/uClinux
static ssize_t getch_nowait(char* input, int sz)
{
    ssize_t rd;
    struct pollfd pfd[2];

    pfd[0].fd = STDIN_FILENO;
    pfd[0].events = POLLIN;
    pfd[1].fd = kbd_fd;
    pfd[1].events = POLLIN;
again:
    tcsetattr(kbd_fd, TCSANOW, &term_less);
    /* NB: select/poll returns whenever read will not block. Therefore:
     * if eof is reached, select/poll will return immediately
     * because read will immediately return 0 bytes.
     * Even if select/poll says that input is available, read CAN block
     * (switch fd into O_NONBLOCK'ed mode to avoid it)
     */
    rd = 1;
    if (max_fline <= cur_fline + max_displayed_line
            && eof_error > 0 /* did NOT reach eof yet */
       ) {
        /* We are interested in stdin */
        rd = 0;
    }
    /* position cursor if line input is done */
    if (less_gets_pos >= 0)
        move_cursor(max_displayed_line + 2, less_gets_pos + 1);
    fflush(stdout);
    safe_poll(pfd + rd, 2 - rd, -1);

    input[0] = '\0';
    rd = safe_read(kbd_fd, input, sz); /* NB: kbd_fd is in O_NONBLOCK mode */
    if (rd < 0 && errno == EAGAIN) {
        /* No keyboard input -> we have input on stdin! */
        read_lines();
        buffer_fill_and_print();
        goto again;
    }
    set_tty_cooked();
    return rd;
}
Exemplo n.º 5
0
static int getch_nowait(void)
{
	int rd;
	struct pollfd pfd[2];

	pfd[0].fd = STDIN_FILENO;
	pfd[0].events = POLLIN;
	pfd[1].fd = kbd_fd;
	pfd[1].events = POLLIN;
 again:
	tcsetattr(kbd_fd, TCSANOW, &term_less);
	/* NB: select/poll returns whenever read will not block. Therefore:
	 * if eof is reached, select/poll will return immediately
	 * because read will immediately return 0 bytes.
	 * Even if select/poll says that input is available, read CAN block
	 * (switch fd into O_NONBLOCK'ed mode to avoid it)
	 */
	rd = 1;
	/* Are we interested in stdin? */
//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)
	) {
		if (eof_error > 0) /* did NOT reach eof yet */
			rd = 0; /* yes, we are interested in stdin */
	}
	/* Position cursor if line input is done */
	if (less_gets_pos >= 0)
		move_cursor(max_displayed_line + 2, less_gets_pos + 1);
	fflush_all();

	if (kbd_input[0] == 0) { /* if nothing is buffered */
#if ENABLE_FEATURE_LESS_WINCH
		while (1) {
			int r;
			/* NB: SIGWINCH interrupts poll() */
			r = poll(pfd + rd, 2 - rd, -1);
			if (/*r < 0 && errno == EINTR &&*/ winch_counter)
				return '\\'; /* anything which has no defined function */
			if (r) break;
		}
#else
		safe_poll(pfd + rd, 2 - rd, -1);
#endif
	}

	/* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
	 * would not block even if there is no input available */
	rd = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2);
	if (rd == -1) {
		if (errno == EAGAIN) {
			/* No keyboard input available. Since poll() did return,
			 * we should have input on stdin */
			read_lines();
			buffer_fill_and_print();
			goto again;
		}
		/* EOF/error (ssh session got killed etc) */
		less_exit(0);
	}
	set_tty_cooked();
	return rd;
}