int rl_sdl_getc_hook() { unsigned int c; c=0; if(cc.displaydevice->get_input(&c)==1) { if(c&(1<<31)) { rl_set_keymap(rl_get_keymap_by_name("emacs-meta")); /* FIXME : this is a dirty trick : */ //c&=!(1<<31); /* FIXME : a dirty trick */ c&=0xFFFFFF^(1<<31); /* FIXME : a dirty trick */ //std::cout << "alt! : "<< (unsigned char)c <<" !\n"; //rl_stuff_char(c); /* warning : this may fail */ rl_stuff_char(c); /* warning : this may fail */ } else { rl_set_keymap(rl_get_keymap_by_name("emacs")); /* FIXME : this is a dirty trick : */ //std::cout << "char in : "<< (unsigned char)c <<" !\n"; rl_stuff_char(c); /* warning : this may fail */ } return 1; } return 0; }
int rl_vi_change_to(int count, int key) { int c, start_pos; if (_rl_uppercase_p (key)) rl_stuff_char ('$'); else if (vi_redoing) rl_stuff_char (_rl_vi_last_motion); start_pos = rl_point; if (rl_vi_domove (key, &c)) { rl_ding (); return -1; } /* These are the motion commands that do not require adjusting the mark. c[wW] are handled by special-case code in rl_vi_domove(), and already leave the mark at the correct location. */ if ((strchr (" l|hwW^0bB", c) == 0) && (rl_mark < rl_end)) rl_mark++; /* The cursor never moves with c[wW]. */ if ((_rl_to_upper (c) == 'W') && rl_point < start_pos) rl_point = start_pos; if (vi_redoing) { if (vi_insert_buffer && *vi_insert_buffer) rl_begin_undo_group (); rl_delete_text (rl_point, rl_mark); if (vi_insert_buffer && *vi_insert_buffer) { rl_insert_text (vi_insert_buffer); rl_end_undo_group (); } } else { rl_begin_undo_group (); /* to make the `u' command work */ rl_kill_text (rl_point, rl_mark); /* `C' does not save the text inserted for undoing or redoing. */ if (_rl_uppercase_p (key) == 0) _rl_vi_doing_insert = 1; _rl_vi_set_last (key, count, rl_arg_sign); rl_vi_insertion_mode (1, key); } return (0); }
int rl_vi_yank_to (int count, int key) { int c, save = rl_point; if (uppercase_p (key)) rl_stuff_char ('$'); if (rl_vi_domove (key, &c)) { DING (); return -1; } /* These are the motion commands that do not require adjusting the mark. */ if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end)) rl_mark++; rl_begin_undo_group (); rl_kill_text (rl_point, rl_mark); rl_end_undo_group (); rl_do_undo (); rl_point = save; return (0); }
/* A simplified loop for vi. Don't dispatch key at end. Don't recognize minus sign? */ static int rl_digit_loop1 (void) { int key, c; while (1) { rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0); key = c = rl_read_key (); if (_rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == (Function *)rl_universal_argument) { rl_numeric_arg *= 4; continue; } c = UNMETA (c); if (digit_p (c)) { if (rl_explicit_arg) rl_numeric_arg = (rl_numeric_arg * 10) + digit_value (c); else rl_numeric_arg = digit_value (c); rl_explicit_arg = 1; } else { rl_clear_message (); rl_stuff_char (key); break; } } return (0); }
static int event_hook_for_readline (void) { /* Make readline() exit on SIGINT */ if (nmc_seen_sigint ()) { rl_echo_signal_char (SIGINT); rl_stuff_char ('\n'); } return 0; }
void jl_readBuffer(char *base, ssize_t nread) { char *start = base; while(*start != 0 && nread > 0) { rl_stuff_char(*start); start++; nread--; } rl_callback_read_char(); }
/*!***************************************************************************** ******************************************************************************* \note sendCommandLineCmd \date Jan 2012 \remarks sends a command to the executed in the command-line thread. Note that this should not be called at high frequency as this won't work. ******************************************************************************* Function Parameters: [in]=input,[out]=output \param[in] name : name of the command ******************************************************************************/ void sendCommandLineCmd(char *name) { int i; strncpy(user_command, name, MAX_CHARS_COMMAND); rl_stuff_char('\n'); }
int rl_vi_delete_to (int count, int key) { int c; if (uppercase_p (key)) rl_stuff_char ('$'); else if (vi_redoing) rl_stuff_char (_rl_vi_last_motion); if (rl_vi_domove (key, &c)) { DING (); return -1; } /* These are the motion commands that do not require adjusting the mark. */ if ((strchr (" l|h^0bB", c) == 0) && (rl_mark < rl_end)) rl_mark++; rl_kill_text (rl_point, rl_mark); return (0); }
/* A simplified loop for vi. Don't dispatch key at end. Don't recognize minus sign? Should this do rl_save_prompt/rl_restore_prompt? */ static int rl_digit_loop1(void) { int key, c; RL_SETSTATE(RL_STATE_NUMERICARG); while (1) { if (rl_numeric_arg > 1000000) { rl_explicit_arg = rl_numeric_arg = 0; rl_ding (); rl_clear_message (); RL_UNSETSTATE(RL_STATE_NUMERICARG); return 1; } rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg); RL_SETSTATE(RL_STATE_MOREINPUT); key = c = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (c >= 0 && _rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == rl_universal_argument) { rl_numeric_arg *= 4; continue; } c = UNMETA (c); if (_rl_digit_p (c)) { if (rl_explicit_arg) rl_numeric_arg = (rl_numeric_arg * 10) + _rl_digit_value (c); else rl_numeric_arg = _rl_digit_value (c); rl_explicit_arg = 1; } else { rl_clear_message (); rl_stuff_char (key); break; } } RL_UNSETSTATE(RL_STATE_NUMERICARG); return (0); }
/* * main loop: listen on stdin (for user input) and master pty (for command output), * and try to write output_queue to master_pty (if it is not empty) * This function never returns. */ void main_loop() { int nfds; fd_set readfds; fd_set writefds; int nread; char buf[BUFFSIZE], *timeoutstr, *old_raw_prompt, *new_output_minus_prompt; int promptlen = 0; int leave_prompt_alone; sigset_t no_signals_blocked; struct timespec select_timeout, *select_timeoutptr; struct timespec immediately = { 0, 0 }; /* zero timeout when child is dead */ struct timespec wait_a_little = {0, 0xBadf00d }; /* tv_usec field will be filled in when initialising */ struct timespec *forever = NULL; wait_a_little.tv_nsec = 1000 * 1000 * wait_before_prompt; sigemptyset(&no_signals_blocked); init_readline(""); last_minute_checks(); pass_through_filter(TAG_OUTPUT,""); /* If something is wrong with filter, get the error NOW */ set_echo(FALSE); /* This will also put the terminal in CBREAK mode */ test_main(); /* ------------------------------ main loop -------------------------------*/ while (TRUE) { /* listen on both stdin and pty_fd */ FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); FD_SET(master_pty_fd, &readfds); /* try to write output_queue to master_pty (but only if it is nonempty) */ FD_ZERO(&writefds); if (output_queue_is_nonempty()) FD_SET(master_pty_fd, &writefds); DPRINTF1(DEBUG_AD_HOC, "prompt_is_still_uncooked = %d", prompt_is_still_uncooked); if (command_is_dead || ignore_queued_input) { select_timeout = immediately; select_timeoutptr = &select_timeout; timeoutstr = "immediately"; } else if (prompt_is_still_uncooked) { select_timeout = wait_a_little; select_timeoutptr = &select_timeout; timeoutstr = "wait_a_little"; } else { select_timeoutptr = forever; /* NULL */ timeoutstr = "forever"; } DPRINTF1(DEBUG_TERMIO, "calling select() with timeout %s", timeoutstr); nfds = my_pselect(1 + master_pty_fd, &readfds, &writefds, NULL, select_timeoutptr, &no_signals_blocked); DPRINTF3(DEBUG_TERMIO, "select() returned %d (stdin|pty in|pty out = %03d), within_line_edit=%d", nfds, 100*(FD_ISSET(STDIN_FILENO, &readfds)?1:0) + 10*(FD_ISSET(master_pty_fd, &readfds)?1:0) + (FD_ISSET(master_pty_fd, &writefds)?1:0), within_line_edit); assert(!filter_pid || filter_is_dead || kill(filter_pid,0) == 0); assert(command_is_dead || kill(command_pid,0) == 0); /* check flags that may have been set by signal handlers */ if (filter_is_dead) filters_last_words(); /* will call myerror with last words */ if (received_WINCH) { /* received_WINCH flag means we've had a WINCH while within_line_edit was FALSE */ DPRINTF0(DEBUG_READLINE, "Starting line edit as a result of WINCH "); within_line_edit = TRUE; restore_rl_state(); received_WINCH = FALSE; continue; } if (nfds < 0) { /* exception */ if (errno == EINTR) { /* interrupted by signal */ continue; } else myerror("select received exception"); } else if (nfds == 0) { /* timeout, which can only happen when .. */ if (ignore_queued_input) { /* ... we have read all the input keystrokes that should be ignored (i.e. those that accumulated on stdin while we were calling an external editor) */ ignore_queued_input = FALSE; continue; } else if (command_is_dead) { /* ... or else, if child is dead, ... */ DPRINTF2(DEBUG_SIGNALS, "select returned 0, command_is_dead=%d, commands_exit_status=%d", command_is_dead, commands_exit_status); cleanup_rlwrap_and_exit(EXIT_SUCCESS); } else if (prompt_is_still_uncooked) { /* cooking time? */ if (we_just_got_a_signal_or_EOF) { we_just_got_a_signal_or_EOF = FALSE; /* 1. If we got a signal/EOF before cooking time, we don't need special action to preserve the cooked prompt. 2. Reset we_just_got_a_signal_or_EOF after a signal or EOF that didn't kill command */ continue; } if (!skip_rlwrap()) { /* ... or else, it is time to cook the prompt */ if (pre_given && accepted_lines == 0) { saved_rl_state.input_buffer = mysavestring(pre_given); /* stuff pre-given text into edit buffer */ saved_rl_state.point = strlen(pre_given); DPRINTF0(DEBUG_READLINE, "Starting line edit (because of -P option)"); within_line_edit = TRUE; restore_rl_state(); continue; } if (accepted_lines == 1 && one_shot_rlwrap) cleanup_rlwrap_and_exit(EXIT_SUCCESS); move_cursor_to_start_of_prompt(ERASE); /* cooked prompt may be shorter than raw prompt, hence the ERASE */ /* move and erase before cooking, as we need to move/erase according to the raw prompt */ cook_prompt_if_necessary(); DPRINTF2(DEBUG_READLINE,"After cooking, raw_prompt=%s, cooked=%s", mangle_string_for_debug_log(saved_rl_state.raw_prompt, MANGLE_LENGTH), mangle_string_for_debug_log(saved_rl_state.cooked_prompt, MANGLE_LENGTH)); my_putstr(saved_rl_state.cooked_prompt); rlwrap_already_prompted = TRUE; } prompt_is_still_uncooked = FALSE; } else { myerror("unexpected select() timeout"); } } else if (nfds > 0) { /* Hah! something to read or write */ /* -------------------------- read pty --------------------------------- */ if (FD_ISSET(master_pty_fd, &readfds)) { /* there is something to read on master pty: */ if ((nread = read(master_pty_fd, buf, BUFFSIZE - 1)) <= 0) { /* read it */ if (command_is_dead || nread == 0) { /* child is dead or has closed its stdout */ if (promptlen > 0) /* commands dying words were not terminated by \n ... */ my_putchar('\n'); /* provide the missing \n */ cleanup_rlwrap_and_exit(EXIT_SUCCESS); } else if (errno == EINTR) /* interrupted by signal ...*/ continue; /* ... don't worry */ else myerror("read error on master pty"); } completely_mirror_slaves_output_settings(); /* some programs (e.g. joe) need this. Gasp!! */ if (skip_rlwrap()) { /* Race condition here! The client may just have finished an emacs session and returned to cooked mode, while its ncurses-riddled output is stil waiting for us to be processed. */ write_patiently(STDOUT_FILENO, buf, nread, "to stdout"); DPRINTF2(DEBUG_TERMIO, "read from pty and wrote to stdout %d bytes in direct mode <%s>", nread, mangle_string_for_debug_log((buf[nread]='\0', buf), MANGLE_LENGTH)); yield(); continue; } DPRINTF2(DEBUG_TERMIO, "read %d bytes from pty into buffer: %s", nread, mangle_string_for_debug_log((buf[nread]='\0', buf), MANGLE_LENGTH)); remove_padding_and_terminate(buf, nread); write_logfile(buf); if (within_line_edit) /* client output arrives while we're editing keyboard input: */ save_rl_state(); /* temporarily disable readline and restore the screen state before readline was called */ assert(saved_rl_state.raw_prompt != NULL); /* We *always* compute the printable part and the new raw prompt, and *always* print the printable part There are four possibilities: 1. impatient before cooking. The raw prompt has been printed, write the new output after it 2. patient before cooking No raw prompt has been printed yet, don't print anything 3. impatient after cooking 3a no current prompt print the new output 3b some current prompt erase it, replace by current raw prompt and print new output 4. patient after cooking don't print anything */ /* sometimes we want to leave the prompt standing, e.g. after accepting a line, or when a signal arrived */ leave_prompt_alone = *saved_rl_state.raw_prompt == '\0' /* saved_rl_state.raw_prompt = "" in two distinct cases: when there is actually no prompt, or just after accepting a line, when the cursor is at the end of the prompt. In both cases, we dont't want to move the cursor */ || prompt_is_still_uncooked /* in this case no prompt has been displayed yet */ || command_is_dead || (we_just_got_a_signal_or_EOF && strrchr(buf, '\n')); /* a signal followed by output with a newline in it: treat it as response to user input, so leave the prompt alone */ DPRINTF3(DEBUG_READLINE, "leave_prompt_alone: %s (raw prompt: %s, prompt_is_still_uncooked: %d)", (leave_prompt_alone? "yes" : "no"), mangle_string_for_debug_log(saved_rl_state.raw_prompt, MANGLE_LENGTH), prompt_is_still_uncooked); if (!leave_prompt_alone) /* && (!impatient_prompt || !saved_rl_state.cooked_prompt)) */ move_cursor_to_start_of_prompt(ERASE); else if (we_just_got_a_signal_or_EOF) { free (saved_rl_state.raw_prompt); saved_rl_state.raw_prompt = mysavestring(""); /* prevent reprinting the prompt */ } if (impatient_prompt && !leave_prompt_alone) old_raw_prompt = mysavestring(saved_rl_state.raw_prompt); new_output_minus_prompt = process_new_output(buf, &saved_rl_state); /* chop off the part after the last newline and put this in saved_rl_state.raw_prompt (or append buf if no newline found)*/ if (impatient_prompt) { /* in impatient mode, ALL command output is passed through the OUTPUT filter, including the prompt The prompt, however, is filtered separately at cooking time and then displayed */ char *filtered = pass_through_filter(TAG_OUTPUT, buf); if(!leave_prompt_alone) { my_putstr(old_raw_prompt); free(old_raw_prompt); } my_putstr(filtered); free (filtered); rlwrap_already_prompted = TRUE; } else { my_putstr(new_output_minus_prompt); rlwrap_already_prompted = FALSE; } free(new_output_minus_prompt); prompt_is_still_uncooked = TRUE; if (within_line_edit) restore_rl_state(); yield(); /* wait for what client has to say .... */ continue; /* ... and don't attempt to process keyboard input as long as it is talking , in order to avoid re-printing the current prompt (i.e. unfinished output line) */ } /* ----------------------------- key pressed: read stdin -------------------------*/ if (FD_ISSET(STDIN_FILENO, &readfds)) { /* key pressed */ unsigned char byte_read; /* the readline function names and documentation talk about "characters" and "keys", but we're reading bytes (i.e. unsigned chars) here, and those may very well be part of a multi-byte character. Example: hebrew "aleph" in utf-8 is 0xd790; pressing this key will make us read 2 bytes 0x90 and then 0xd7, (or maybe the other way round depending on endianness??) The readline library hides all this complexity and allows one to just "pass the bytes around" */ nread = read(STDIN_FILENO, &byte_read, 1); /* read next byte of input */ assert(sizeof(unsigned char) == 1); /* gets optimised away */ if (nread <= 0) DPRINTF1(DEBUG_TERMIO, "read from stdin returned %d", nread); if (nread < 0) if (errno == EINTR) continue; else myerror("Unexpected error"); else if (nread == 0) /* EOF on stdin */ cleanup_rlwrap_and_exit(EXIT_SUCCESS); else if (ignore_queued_input) continue; /* do nothing with it*/ assert(nread == 1); DPRINTF2(DEBUG_TERMIO, "read from stdin: byte 0x%02x (%s)", byte_read, mangle_char_for_debug_log(byte_read, TRUE)); if (skip_rlwrap()) { /* direct mode, just pass it on */ /* remote possibility of a race condition here: when the first half of a multi-byte char is read in direct mode and the second half in readline mode. Oh well... */ DPRINTF0(DEBUG_TERMIO, "passing it on (in transparent mode)"); completely_mirror_slaves_terminal_settings(); /* this is of course 1 keypress too late: we should mirror the terminal settings *before* the user presses a key. (maybe using rl_event_hook??) @@@FIXME @@@ HOW?*/ write_patiently(master_pty_fd, &byte_read, 1, "to master pty"); } else { /* hand it over to readline */ if (!within_line_edit) { /* start a new line edit */ DPRINTF0(DEBUG_READLINE, "Starting line edit"); within_line_edit = TRUE; restore_rl_state(); } if (term_eof && byte_read == term_eof && strlen(rl_line_buffer) == 0) { /* hand a term_eof (usually CTRL-D) directly to command */ char *sent_EOF = mysavestring("?"); *sent_EOF = term_eof; put_in_output_queue(sent_EOF); we_just_got_a_signal_or_EOF = TRUE; free(sent_EOF); } else { rl_stuff_char(byte_read); /* stuff it back in readline's input queue */ DPRINTF0(DEBUG_TERMIO, "passing it to readline"); DPRINTF2(DEBUG_READLINE, "rl_callback_read_char() (_rl_eof_char=%d, term_eof=%d)", _rl_eof_char, term_eof); rl_callback_read_char(); } } } /* -------------------------- write pty --------------------------------- */ if (FD_ISSET(master_pty_fd, &writefds)) { flush_output_queue(); yield(); /* give slave command time to respond. If we don't do this, nothing bad will happen, but the "dialogue" on screen will be out of order */ } } /* if (ndfs > 0) */ } /* while (1) */ } /* void main_loop() */