int cook_prompt_if_necessary () { char *pre_cooked, *filtered, *uncoloured; filtered = NULL; if (saved_rl_state.cooked_prompt) return FALSE; /* cooked already */ pre_cooked = mysavestring(saved_rl_state.raw_prompt); unbackspace(pre_cooked); if ( (prompt_regexp && ! match_regexp(pre_cooked, prompt_regexp, FALSE)) || /* raw prompt doesn't match '--only-cook' regexp */ (strcmp((filtered = pass_through_filter(TAG_PROMPT, pre_cooked)), "_THIS_CANNOT_BE_A_PROMPT_")== 0)) { /* filter has "refused" the prompt */ saved_rl_state.cooked_prompt = (impatient_prompt ? mysavestring(pre_cooked) : mysavestring("")); /* don't cook, eat raw (and eat nothing if patient) */ free(pre_cooked); free(filtered); /* free(NULL) is not an error */ return FALSE; } free(pre_cooked); if(substitute_prompt) { uncoloured = mysavestring(substitute_prompt); free(filtered); } else { uncoloured = filtered; } if (colour_the_prompt) { saved_rl_state.cooked_prompt = colourise(uncoloured); free(uncoloured); } else { saved_rl_state.cooked_prompt = uncoloured; } return TRUE; }
char *process_new_output(const char* buffer, struct rl_state* UNUSED(state)) { char*last_nl, *old_prompt_plus_new_output, *new_prompt, *result; old_prompt_plus_new_output = append_and_free_old(saved_rl_state.raw_prompt, buffer); last_nl = strrchr(old_prompt_plus_new_output, '\n'); if (last_nl != NULL) { /* newline seen, will get new prompt: */ new_prompt = mysavestring(last_nl +1); /* chop off the part after the last newline - this will be the new prompt */ *last_nl = '\0'; old_prompt_plus_new_output = append_and_free_old (old_prompt_plus_new_output, "\n"); result = (impatient_prompt ? mysavestring (old_prompt_plus_new_output): pass_through_filter(TAG_OUTPUT, old_prompt_plus_new_output)); if (remember_for_completion) { feed_line_into_completion_list(result); /* feed output into completion list *after* filtering */ } } else { new_prompt = mysavestring(old_prompt_plus_new_output); result = mysavestring(""); } free(old_prompt_plus_new_output); saved_rl_state.raw_prompt = new_prompt; if (saved_rl_state.cooked_prompt) { free (saved_rl_state.cooked_prompt); saved_rl_state.cooked_prompt = NULL; } return result; }
/* format line and add it to history list, avoiding duplicates if necessary */ static void my_add_history(char *line) { int lookback, count, here; char *new_entry, *filtered_line; filtered_line = pass_through_filter(TAG_HISTORY, line); switch (history_duplicate_avoidance_policy) { case KEEP_ALL_DOUBLES: lookback = 0; break; case ELIMINATE_SUCCESIVE_DOUBLES: lookback = 1; break; case ELIMINATE_ALL_DOUBLES: lookback = history_length; break; } new_entry = filtered_line; lookback = min(history_length, lookback); for (count = 0, here = history_length + history_base - 1; count < lookback ; count++, here--) { /* DPRINTF3(DEBUG_READLINE, "strcmping <%s> and <%s> (count = %d)", line, history_get(here)->line ,count); */ if (strncmp(new_entry, history_get(here) -> line, 10000) == 0) { HIST_ENTRY *entry = remove_history (here - history_base); /* according to the history library doc this should be remove_history(here) @@@?*/ DPRINTF2(DEBUG_READLINE, "removing duplicate entry #%d (%s)", here, entry->line); free_foreign(entry->line); free_foreign(entry); } } add_history(new_entry); free(new_entry); }
/* format line and add it to history list, avoiding duplicates if necessary */ static void my_add_history(char *line) { int lookback, count, here; char *new_entry, *filtered_line; filtered_line = pass_through_filter(TAG_HISTORY, line); switch (history_duplicate_avoidance_policy) { case KEEP_ALL_DOUBLES: lookback = 0; break; case ELIMINATE_SUCCESIVE_DOUBLES: lookback = 1; break; case ELIMINATE_ALL_DOUBLES: lookback = history_length; break; } new_entry = filtered_line; lookback = min(history_length, lookback); for (count = 0, here = history_length - 1; count < lookback ; count++, here--) { DPRINTF4(DEBUG_READLINE, "strcmping <%s> and <%s> (count = %d, here = %d)", line, history_get(history_base + here)->line ,count, here); if (strncmp(new_entry, history_get(history_base + here) -> line, 10000) == 0) { /* history_get uses the logical offset history_base .. */ HIST_ENTRY *entry = remove_history (here); /* .. but remove_history doesn't! */ DPRINTF2(DEBUG_READLINE, "removing duplicate entry #%d (%s)", here, entry->line); free_foreign(entry->line); free_foreign(entry); } } add_history(new_entry); free(new_entry); }
int cook_prompt_if_necessary () { char *pre_cooked, *rubbish_from_alternate_screen, *filtered, *uncoloured, *cooked, *p, *non_rubbish = NULL; static char **term_ctrl_seqs[] = {&term_rmcup, &term_rmkx, NULL}; /* (NULL-terminated) list of (pointers to) term control sequences that may be used by clients to return from an 'alternate screen'. If we spot one of those, assume that it, and anything before it, is rubbish and better left untouched */ char ***tcptr; filtered = NULL; DPRINTF2(DEBUG_READLINE, "Prompt <%s>: %s", saved_rl_state.raw_prompt, prompt_is_still_uncooked ? "still raw" : "cooked already"); if (saved_rl_state.cooked_prompt) /* if (!prompt_is_still_uncooked) bombs with multi-line paste. Apparently prompt_is_still_uncooked can be FALSE while saved_rl_state.cooked_prompt = NULL. Ouch!@@@! */ return FALSE; /* cooked already */ pre_cooked = mysavestring(saved_rl_state.raw_prompt); for (tcptr = term_ctrl_seqs; *tcptr; tcptr++) { /* find last occurence of one of term_ctrl_seq */ if (**tcptr && (p = mystrstr(pre_cooked, **tcptr))) { p += strlen(**tcptr); /* p now points 1 char past term control sequence */ if (p > non_rubbish) non_rubbish = p; } } /* non_rubbish now points 1 past the last 'alternate screen terminating' control char in prompt */ if (non_rubbish) { rubbish_from_alternate_screen = pre_cooked; pre_cooked = mysavestring(non_rubbish); *non_rubbish = '\0'; /* 0-terminate rubbish_from_alternate_screen */ } else { rubbish_from_alternate_screen = mysavestring(""); } unbackspace(pre_cooked); /* programs that display a running counter would otherwise make rlwrap keep prompts like " 1%\r 2%\r 3%\ ......" */ if ( /* raw prompt doesn't match '--only-cook' regexp */ (prompt_regexp && ! match_regexp(pre_cooked, prompt_regexp, FALSE)) || /* now filter it, but filter may "refuse" the prompt */ (strcmp((filtered = pass_through_filter(TAG_PROMPT, pre_cooked)), "_THIS_CANNOT_BE_A_PROMPT_")== 0)) { /* don't cook, eat raw (and eat nothing if patient) */ saved_rl_state.cooked_prompt = (impatient_prompt ? mysavestring(pre_cooked) : mysavestring("")); /* NB: if impatient, the rubbish_from_alternate_screen has been output already, no need to send it again */ free(pre_cooked); free(filtered); /* free(NULL) is never an error */ return FALSE; } free(pre_cooked); if(substitute_prompt) { uncoloured = mysavestring(substitute_prompt); free(filtered); } else { uncoloured = filtered; } if (colour_the_prompt) { cooked = colourise(uncoloured); free(uncoloured); } else { cooked = uncoloured; } if (! impatient_prompt) /* in this case our rubbish hasn't been output yet. Output it now, but don't store it in the prompt, as this may be re-printed e.g. after resuming a suspended rlwrap */ write_patiently(STDOUT_FILENO,rubbish_from_alternate_screen, strlen(rubbish_from_alternate_screen), "to stdout"); saved_rl_state.cooked_prompt = cooked; return TRUE; }
static int handle_hotkey2(int UNUSED(count), int hotkey, int ignore_history) { char *prefix, *postfix, *history, *histpos_as_string, *executing_keyseq; char *new_prefix, *new_postfix, *new_history, *new_histpos_as_string, *message; char *filter_food, *filtered, **fragments, *new_rl_line_buffer; int length, new_histpos; unsigned long int hash; static const unsigned int MAX_HISTPOS_DIGITS = 6; /* one million history items should suffice */ #ifdef HAVE_RL_EXECUTING_KEYSEQ /* i.e. if readline version is >= 6.3 */ executing_keyseq = mysavestring(rl_executing_keyseq); #else executing_keyseq = mysavestring("?"); *executing_keyseq = hotkey; /* The filter will only get the *last* byte of the key sequence that triggered rl_handle_hotkey */ #endif DPRINTF3(DEBUG_READLINE, "hotkey press (ignore_history == %d): %x (%s)", ignore_history, hotkey, mangle_string_for_debug_log(executing_keyseq, MANGLE_LENGTH)); if (hotkey == '\t') /* this would go horribly wrong with all the splitting on '\t' going on.... @@@ or pass key as a string e.g. "009" */ myerror(FATAL | NOERRNO, "Sorry, you cannot use TAB as an hotkey in rlwrap"); prefix = mysavestring(rl_line_buffer); prefix[rl_point] = '\0'; /* chop off just before cursor */ postfix = mysavestring(rl_line_buffer + rl_point); if (ignore_history) { histpos_as_string = mysavestring("0"); history = mysavestring(""); } else { histpos_as_string = as_string(where_history()); assert(strlen(histpos_as_string) <= MAX_HISTPOS_DIGITS); history = entire_history_as_one_string(); hash = hash_multiple(2, history, histpos_as_string); } /* filter_food = key + tab + prefix + tab + postfix + tab + history + tab + histpos + '\0' */ length = strlen(rl_line_buffer) + strlen(history) + MAX_HISTPOS_DIGITS + 5; filter_food = mymalloc(length); sprintf(filter_food, "%s\t%s\t%s\t%s\t%s", executing_keyseq, prefix, postfix, history, histpos_as_string); /* this is the format that the filter expects */ /* let the filter filter ...! */ filtered= pass_through_filter(TAG_HOTKEY, filter_food); /* OK, we now have to read back everything. There should be exactly 5 TAB-separated components*/ fragments = split_on_single_char(filtered, '\t', 5); message = fragments[0]; new_prefix = fragments[1]; new_postfix = fragments[2]; new_history = fragments[3]; new_histpos_as_string = fragments[4]; if (!ignore_history && hash_multiple(2, new_history, new_histpos_as_string) != hash) { /* history has been rewritten */ char **linep, **history_lines = split_on_single_char(new_history, '\n', 0); DPRINTF3(DEBUG_READLINE, "hash=%lx, new_history is %d bytes long, histpos <%s>", hash, (int) strlen(new_history), new_histpos_as_string); clear_history(); for (linep = history_lines; *linep; linep++) add_history(*linep); new_histpos = my_atoi(new_histpos_as_string); history_set_pos(new_histpos); free_splitlist(history_lines); } new_rl_line_buffer = add2strings(new_prefix, new_postfix); if ( (length = strlen(new_rl_line_buffer)) > 0 && new_rl_line_buffer[length - 1] == '\n') { new_rl_line_buffer[length - 1] = '\0'; rl_done = TRUE; return_key = (char) '\n'; assert(strchr(new_rl_line_buffer, '\n') == NULL); } rl_delete_text(0, strlen(rl_line_buffer)); rl_point = 0; rl_insert_text(new_rl_line_buffer); rl_point = strlen(new_rl_line_buffer); if (*message && *message != hotkey) { /* if message has been set (i.e. != hotkey) , and isn't empty: */ message = append_and_free_old(mysavestring(message), " "); /* put space (for readability) between the message and the input line .. */ message_in_echo_area(message); /* .. then write it to echo area */ } clear_line(); rl_on_new_line(); rl_redisplay(); free_splitlist(fragments); /* this will free all the fragments (and the list itself) in one go */ free_multiple(prefix, postfix, filter_food, executing_keyseq, filtered, new_rl_line_buffer, history, histpos_as_string, FMEND); return 0; }
static void line_handler(char *line) { char *rewritten_line, *filtered_line; if (line == NULL) { /* EOF on input, forward it */ DPRINTF1(DEBUG_READLINE, "EOF detected, writing character %d", term_eof); /* colour_the_prompt = FALSE; don't mess with the cruft that may come out of dying command @@@ but command may not die!*/ write_EOF_to_master_pty(); } else { if (*line && /* forget empty lines */ redisplay && /* forget passwords */ !forget_line && /* forget lines entered by CTRL-O */ !match_regexp(line, forget_regexp, TRUE)) { /* forget lines (case-inseitively) matching -g option regexp */ my_add_history(line); } forget_line = FALSE; /* until CTRL-O is used again */ /* Time for a design decision: when we send the accepted line to the client command, it will most probably be echoed back. We have two choices: we leave the entered line on screen and suppress just enough of the clients output (I believe this is what rlfe does), or we remove the entered input (but not the prompt!) and let it be replaced by the echo. This is what we do; as a bonus, if the program doesn't echo, e.g. at a password prompt, the **** which has been put there by our homegrown_redisplay function will be removed (@@@is this what we want?) I think this method is more natural for multi-line input as well, (we will actually see our multi-line input as multiple lines) but not everyone will agree with that. O.K, we know for sure that cursor is at start of line. When clients output arrives, it will be printed at just the right place - but first we 'll erase the user input (as it may be about to be changed by the filter) */ rl_delete_text(0, rl_end); /* clear line (after prompt) */ rl_point = 0; my_redisplay(); /* and redisplay (this time without user input, cf the comments for the line_handler() function below) */ rewritten_line = (multiline_separator ? search_and_replace(multiline_separator, "\n", line, 0, NULL, NULL) : mysavestring(line)); if (redisplay) filtered_line = pass_through_filter(TAG_INPUT, rewritten_line); else { /* password, none of filters business */ pass_through_filter(TAG_INPUT, "***"); /* filter some input anyway, to keep filter in sync (result is discarded). */ filtered_line = mysavestring(rewritten_line); } free(rewritten_line); /* do we have to adapt clients winzsize now? */ if (deferred_adapt_commands_window_size) { adapt_tty_winsize(STDIN_FILENO, master_pty_fd); kill(-command_pid, SIGWINCH); deferred_adapt_commands_window_size = FALSE; } /*OK, now feed line to underlying command and wait for the echo to come back */ put_in_output_queue(filtered_line); DPRINTF2(DEBUG_READLINE, "putting %d bytes %s in output queue", (int) strlen(rewritten_line), mangle_string_for_debug_log(rewritten_line, 40)); write_EOL_to_master_pty(return_key ? &return_key : "\n"); accepted_lines++; free_foreign(line); /* free_foreign because line was malloc'ed by readline, not by rlwrap */ free(filtered_line); /* we're done with them */ return_key = 0; within_line_edit = FALSE; if(!RL_ISSTATE(RL_STATE_MACROINPUT)) /* when called during playback of a multi-line macro, line_handler() will be called more than once whithout re-entering main_loop(). If we'd remove it here, the second call would crash */ rl_callback_handler_remove(); set_echo(FALSE); free(saved_rl_state.input_buffer); free(saved_rl_state.raw_prompt); free(saved_rl_state.cooked_prompt); saved_rl_state.input_buffer = mysavestring(""); saved_rl_state.raw_prompt = mysavestring(""); saved_rl_state.cooked_prompt = NULL; saved_rl_state.point = 0; saved_rl_state.already_saved = TRUE; redisplay = TRUE; if (one_shot_rlwrap) write_EOF_to_master_pty(); } }
inline bool string_generate(OutputIterator& sink , Container const& c) { return string_generate(sink, c, pass_through_filter()); }
inline bool string_generate(OutputIterator& sink , std::basic_string<Char, Traits, Allocator> const& str) { return string_generate(sink, str.c_str(), pass_through_filter()); }
inline bool string_generate(OutputIterator& sink, Char const* str) { return string_generate(sink, str, pass_through_filter()); }
char * read_options_and_command_name(int argc, char **argv) { int c; char *opt_C = NULL; int option_count = 0; int opt_b = FALSE; int opt_f = FALSE; int remaining = -1; /* remaining number of arguments on command line */ int longindex = -1; /* index of current option in longopts[], set by getopt_long */ full_program_name = mysavestring(argv[0]); program_name = mybasename(full_program_name); /* normally "rlwrap"; needed by myerror() */ rl_basic_word_break_characters = " \t\n\r(){}[],+-=&^%$#@\";|\\"; opterr = 0; /* we do our own error reporting */ while (1) { #ifdef HAVE_GETOPT_LONG c = getopt_long(argc, argv, optstring, longopts, &longindex); #else c = getopt(argc, argv, optstring); #endif if (c == EOF) break; option_count++; last_option_didnt_have_optional_argument = FALSE; remaining = argc - optind; last_opt = c; switch (c) { case 'a': always_readline = TRUE; if (check_optarg('a', remaining)) password_prompt_search_string = mysavestring(optarg); break; case 'A': ansi_colour_aware = TRUE; break; case 'b': rl_basic_word_break_characters = add3strings("\r\n \t", optarg, ""); opt_b = TRUE; break; case 'c': complete_filenames = TRUE; break; case 'C': opt_C = mysavestring(optarg); break; case 'd': #ifdef DEBUG if (option_count > 1) myerror("-d or --debug option has to be the *first* rlwrap option"); if (check_optarg('d', remaining)) debug = atoi(optarg); else debug = DEBUG_DEFAULT; #else myerror ("To use -d( for debugging), configure %s with --enable-debug and rebuild", program_name); #endif break; case 'D': history_duplicate_avoidance_policy=atoi(optarg); if (history_duplicate_avoidance_policy < 0 || history_duplicate_avoidance_policy > 2) myerror("%s option with illegal value %d, should be 0, 1 or 2", current_option('D', longindex), history_duplicate_avoidance_policy); break; case 'f': if (strncmp(optarg, ".", 10) == 0) feed_history_into_completion_list = TRUE; else feed_file_into_completion_list(optarg); opt_f = TRUE; break; case 'F': myerror("The -F (--history-format) option is obsolete. Use -z \"history_format '%s'\" instead", optarg); case 'g': forget_regexp = mysavestring(optarg); match_regexp("just testing", forget_regexp, 1); break; case 'h': usage(EXIT_SUCCESS); /* will call exit() */ case 'H': history_filename = mysavestring(optarg); break; case 'i': if (opt_f) myerror("-i option has to precede -f options"); completion_is_case_sensitive = FALSE; break; case 'I': pass_on_sigINT_as_sigTERM = TRUE; break; case 'l': open_logfile(optarg); break; case 'n': nowarn = TRUE; break; case 'm': #ifndef HAVE_SYSTEM mywarn("the -m option doesn't work on this system"); #endif multiline_separator = (check_optarg('m', remaining) ? mysavestring(optarg) : " \\ "); break; case 'N': commands_children_not_wrapped = TRUE; break; case 'o': one_shot_rlwrap = TRUE; impatient_prompt = FALSE; wait_before_prompt = 200; break; case 'O': prompt_regexp = mysavestring(optarg); match_regexp("just testing", prompt_regexp, 1); break; case 'p': colour_the_prompt = TRUE; initialise_colour_codes(check_optarg('p', remaining) ? colour_name_to_ansi_code(optarg) : colour_name_to_ansi_code("Red")); break; case 'P': pre_given = mysavestring(optarg); always_readline = TRUE; /* pre_given does not work well with transparent mode */ break; case 'q': rl_basic_quote_characters = mysavestring(optarg); break; case 'r': remember_for_completion = TRUE; break; case 'R': renice = TRUE; break; case 's': histsize = atoi(optarg); if (histsize < 0) { write_histfile = 0; histsize = -histsize; } break; case 'S': substitute_prompt = mysavestring(optarg);break; case 't': client_term_name=mysavestring(optarg);break; #ifdef DEBUG case 'T': test_terminal(); exit(EXIT_SUCCESS); #endif case 'v': printf("rlwrap %s\n", VERSION); exit(EXIT_SUCCESS); case 'w': wait_before_prompt = atoi(optarg); if (wait_before_prompt < 0) { wait_before_prompt *= -1; impatient_prompt = FALSE; } break; case 'z': filter_command = mysavestring(optarg); break; case '?': assert(optind > 0); myerror("unrecognised option %s\ntry '%s --help' for more information", argv[optind-1], full_program_name); case ':': assert(optind > 0); myerror ("option %s requires an argument \ntry '%s --help' for more information", argv[optind-1], full_program_name); default: usage(EXIT_FAILURE); } } if (!complete_filenames && !opt_b) { /* use / and . as default breaking characters whenever we don't complete filenames */ rl_basic_word_break_characters = add2strings(rl_basic_word_break_characters, "/."); } if (optind >= argc) { /* rlwrap -a -b -c with no command specified */ if (filter_command) { /* rlwrap -z filter with no command specified */ mysignal(SIGALRM, &handle_sigALRM); /* needed for read_patiently2 */ spawn_filter(filter_command); pass_through_filter(TAG_OUTPUT,""); /* ignore result but allow TAG_OUTPUT_OUT_OF_BAND */ cleanup_rlwrap_and_exit(EXIT_SUCCESS); } else { usage(EXIT_FAILURE); } } if (opt_C) { int countback = atoi(opt_C); /* investigate whether -C option argument is numeric */ if (countback > 0) { /* e.g -C 1 or -C 12 */ if (argc - countback < optind) /* -C 666 */ myerror("when using -C %d you need at least %d non-option arguments", countback, countback); else if (argv[argc - countback][0] == '-') /* -C 2 perl -d blah.pl */ myerror("the last argument minus %d appears to be an option!", countback); else { /* -C 1 perl test.cgi */ command_name = mysavestring(mybasename(argv[argc - countback])); } } else if (countback == 0) { /* -C name1 name2 or -C 0 */ if (opt_C[0] == '0' && opt_C[1] == '\0') /* -C 0 */ myerror("-C 0 makes no sense"); else if (strlen(mybasename(opt_C)) != strlen(opt_C)) /* -C dir/name */ myerror("-C option argument should not contain directory components"); else if (opt_C[0] == '-') /* -C -d (?) */ myerror("-C option needs argument"); else /* -C name */ command_name = opt_C; } else { /* -C -2 */ myerror ("-C option needs string or positive number as argument, perhaps you meant -C %d?", -countback); } } else { /* no -C option given, use command name */ command_name = mysavestring(mybasename(argv[optind])); } assert(command_name != NULL); return command_name; }
/* * 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() */