void restore_rl_state() { char *newprompt; move_cursor_to_start_of_prompt(impatient_prompt ? ERASE : DONT_ERASE); cook_prompt_if_necessary(); newprompt = mark_invisible(saved_rl_state.cooked_prompt); /* bracket (colour) control sequences with \001 and \002 */ rl_expand_prompt(newprompt); mirror_slaves_echo_mode(); /* don't show passwords etc */ DPRINTF1(DEBUG_READLINE,"newprompt now %s", mangle_string_for_debug_log(newprompt,MANGLE_LENGTH)); rl_callback_handler_install(newprompt, &line_handler); DPRINTF0(DEBUG_AD_HOC, "freeing newprompt"); free(newprompt); /* readline docs don't say it, but we can free newprompt now (readline apparently uses its own copy) */ rl_insert_text(saved_rl_state.input_buffer); rl_point = saved_rl_state.point; saved_rl_state.already_saved = 0; DPRINTF0(DEBUG_AD_HOC, "Starting redisplay"); rl_redisplay(); rl_prep_terminal(1); prompt_is_still_uncooked = FALSE; /* has been done right now */ }
/* TUI readline command. Switch the output mode between TUI/standard gdb. */ static int tui_rl_switch_mode (int notused1, int notused2) { if (tui_active) { tui_disable (); rl_prep_terminal (0); } else { rl_deprep_terminal (); tui_enable (); } /* Clear the readline in case switching occurred in middle of something. */ if (rl_end) rl_kill_text (0, rl_end); /* Since we left the curses mode, the terminal mode is restored to some previous state. That state may not be suitable for readline to work correctly (it may be restored in line mode). We force an exit of the current readline so that readline is re-entered and it will be able to setup the terminal for its needs. By re-entering in readline, we also redisplay its prompt in the non-curses mode. */ rl_newline (1, '\n'); /* Make sure the \n we are returning does not repeat the last command. */ dont_repeat (); return 0; }
void jl_prep_terminal(int meta_flag) { FILE *rl_in = rl_instream; rl_instream = stdin; rl_prep_terminal(1); rl_instream = rl_in; #ifdef __WIN32__ if (jl_uv_stdin->type == UV_TTY) uv_tty_set_mode((uv_tty_t*)jl_uv_stdin,1); //raw (and libuv-processed) if (!repl_sigint_handler_installed) { if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)repl_sigint_handler,1)) repl_sigint_handler_installed = 1; } #else if (jl_sigint_act.sa_sigaction == NULL) { struct sigaction oldact, repl_sigint_act; memset(&repl_sigint_act, 0, sizeof(struct sigaction)); sigemptyset(&repl_sigint_act.sa_mask); repl_sigint_act.sa_sigaction = repl_sigint_handler; repl_sigint_act.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &repl_sigint_act, &oldact) < 0) { JL_PRINTF(JL_STDERR, "sigaction: %s\n", strerror(errno)); jl_exit(1); } if (repl_sigint_act.sa_sigaction != oldact.sa_sigaction && jl_sigint_act.sa_sigaction != oldact.sa_sigaction) jl_sigint_act = oldact; } #endif }
void log_prepare(const char *fn, const char *mode = NULL) { rl_initialize(); rl_prep_terminal(0); if(!mode) mode = "a"; if(logfile) fclose(logfile); logfile = fopen(fn,mode); }
static char * call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) { size_t n; char *p, *q; int signal; #ifdef SAVE_LOCALE char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); if (!saved_locale) Py_FatalError("not enough memory to save locale"); setlocale(LC_CTYPE, ""); #endif if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { rl_instream = sys_stdin; rl_outstream = sys_stdout; #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER rl_prep_terminal (1); #endif } p = readline_until_enter_or_signal(prompt, &signal); /* we got an interrupt signal */ if (signal) { RESTORE_LOCALE(saved_locale) return NULL; } /* We got an EOF, return a empty string. */ if (p == NULL) { p = PyMem_Malloc(1); if (p != NULL) *p = '\0'; RESTORE_LOCALE(saved_locale) return p; }
void cmd_getnum(const unsigned long int idx, const unsigned long int spice) { xmlNodePtr db_node = NULL; xmlChar *key = NULL, *value = NULL, *value_nl = NULL, *line = NULL, *line_randomed = NULL, *tmp = NULL; unsigned long int lines = 0, line_req = 1, value_len = 0, line_len = 0, line_randomed_len = 0, erase_len = 0, i = 0; char rc = 0; char *rand_str = NULL; char **fork_argv = NULL; int child; int pipefd[2]; db_node = find_key(idx); if (db_node) { key = xmlGetProp(db_node, BAD_CAST "name"); value = xmlGetProp(db_node, BAD_CAST "value"); value_nl = parse_newlines(value, 0); xmlFree(value); value = NULL; value_len = xmlStrlen(value_nl); /* count how many (new)lines are in the string */ for (i=0; i < value_len; i++) if (value_nl[i] == '\n') lines++; lines++; #ifndef _READLINE /* clear the prompt temporarily */ if (el_set(e, EL_PROMPT, el_prompt_null) != 0) { perror("ERROR: el_set(EL_PROMPT)"); } if (el_set(e, EL_UNBUFFERED, 1) != 0) { perror("ERROR: el_set(EL_UNBUFFERED)"); xmlFree(key); key = NULL; xmlFree(value_nl); value_nl = NULL; return; } #else rl_prep_terminal(1); #endif while (rc != 'q' && rc != 4) { /* quit for 'q' or EOT */ if (batchmode) puts(""); printf("[%s] ", key); /* print the key */ /* if multiline, prefix the line with a line number */ if (lines > 1) printf("[%lu/%lu] ", line_req, lines); /* get a line out from the value */ line = get_line(value_nl, line_req); line_len = xmlStrlen(line); if (spice > 0) { /* if random padding is requested */ line_randomed_len = line_len + line_len * spice + spice + 1; line_randomed = calloc(1, line_randomed_len); malloc_check(line_randomed); /* begin with the random string */ rand_str = get_random_str(spice, 1); if (!rand_str) { xmlFree(key); key = NULL; xmlFree(value_nl); value_nl = NULL; xmlFree(line); line = NULL; free(line_randomed); line_randomed = NULL; return; } (void) strlcat((char *)line_randomed, rand_str, line_randomed_len); free(rand_str); rand_str = NULL; for (i = 0; i < line_len; i++) { /* append a character from the line */ tmp = xmlUTF8Strsub(line, i, 1); (void) strlcat((char *)line_randomed, (const char *)tmp, line_randomed_len); xmlFree(tmp); tmp = NULL; /* append a random string */ rand_str = get_random_str(spice, 1); if (!rand_str) { xmlFree(key); key = NULL; xmlFree(value_nl); value_nl = NULL; xmlFree(line); line = NULL; free(line_randomed); line_randomed = NULL; return; } (void) strlcat((char *)line_randomed, rand_str, line_randomed_len); free(rand_str); rand_str = NULL; } line_randomed[line_randomed_len - 1] = '\0'; xmlFree(line); line = NULL; line = line_randomed; } printf("%s", line); #ifdef _READLINE rl_redisplay(); #endif if (batchmode) { rc = 'q'; puts(""); } else { /* this is the prompt, after displaying the value */ #ifndef _READLINE el_getc(e, &rc); #else rc = rl_read_key(); #endif /* erase (overwrite) the previously written value with spaces */ printf("\r"); erase_len = ERASE_LEN; for (i = 0; i < erase_len; i++) putchar(' '); printf("\r"); /* process the keypress */ switch (rc) { /* line forward */ case 'f': case 'n': case 'j': case '+': case ' ': case '>': case ']': case '}': case 10: /* editline */ case 13: /* readline */ if (line_req < lines) line_req++; break; /* line backward */ case 'b': case 'p': case 'k': case '-': case '<': case '[': case '{': case 8: if (line_req - 1 > 0) line_req--; break; /* jump to the requested line */ case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if ((unsigned char)(rc - 48) <= lines) line_req = rc - 48; break; case 't': /* This is duplicated in cmd_clipboard.c */ /* Copy value to tmux paste buffer */ switch (child = fork()) { case -1: perror("\nERROR: Couldn't fork(2) for tmux(1)"); break; case 0: /* Child */ close(0); /* This is also needed for Editline's UNBUFFERED mode to continue to work properly. */ close(1); if (bio_chain) BIO_free_all(bio_chain); if (db_params.db_file) { if (close(db_params.db_file) == -1) { perror("ERROR: child: close(database file)"); exit(EXIT_FAILURE); } else { db_params.db_file = -1; } } fork_argv = malloc(5 * sizeof(char *)); malloc_check(fork_argv); fork_argv[0] = "tmux"; fork_argv[1] = "set-buffer"; fork_argv[2] = "--"; fork_argv[3] = (char *) line; fork_argv[4] = NULL; if (execvp(fork_argv[0], fork_argv) == -1) dprintf(STDERR_FILENO, "ERROR: tmux: %s\n", strerror(errno)); quit(EXIT_FAILURE); break; default: /* Parent */ rc = 'q'; break; } break; case 'x': case 'X': /* This is duplicated in cmd_clipboard.c */ /* Copy value to X11 clipboard, using xclip(1) */ pipe(pipefd); switch (child = fork()) { case -1: perror("\nERROR: Couldn't fork(2) for xclip(1)"); break; case 0: /* Child */ close(0); /* This is also needed for Editline's UNBUFFERED mode to continue to work properly. */ close(1); close(pipefd[1]); if (bio_chain) BIO_free_all(bio_chain); if (db_params.db_file) { if (close(db_params.db_file) == -1) { perror("ERROR: child: close(database file)"); exit(EXIT_FAILURE); } else { db_params.db_file = -1; } } fork_argv = malloc(4 * sizeof(char *)); malloc_check(fork_argv); fork_argv[0] = "xclip"; fork_argv[1] = "-selection"; if (rc == 'x') { fork_argv[2] = "primary"; } else if (rc == 'X') { fork_argv[2] = "clipboard"; } fork_argv[3] = NULL; /* stdin becomes the read end of the pipe in the child, * and the exec'd process will have the same environment. */ dup2(pipefd[0], 0); if (execvp(fork_argv[0], fork_argv) == -1) dprintf(STDERR_FILENO, "ERROR: xclip: %s\n", strerror(errno)); quit(EXIT_FAILURE); break; default: /* Parent */ /* Write the value to the pipe's write end, which will * appear in the child's stdin (pipe's read end). */ close(pipefd[0]); write(pipefd[1], line, line_len); close(pipefd[1]); rc = 'q'; break; } break; default: break; } } xmlFree(line); line = NULL; } xmlFree(key); key = NULL; xmlFree(value_nl); value_nl = NULL; #ifndef _READLINE /* re-enable the default prompt */ if (el_set(e, EL_PROMPT, prompt_str) != 0) { perror("ERROR: el_set(EL_PROMPT)"); } el_set(e, EL_UNBUFFERED, 0); #else rl_deprep_terminal(); #endif } else puts("Invalid index!"); } /* cmd_getnum() */
static char * call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) { size_t n; char *p, *q; PyOS_sighandler_t old_inthandler; #ifdef SAVE_LOCALE char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); if (!saved_locale) Py_FatalError("not enough memory to save locale"); setlocale(LC_CTYPE, ""); #endif old_inthandler = PyOS_setsig(SIGINT, onintr); if (setjmp(jbuf)) { #ifdef HAVE_SIGRELSE /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ sigrelse(SIGINT); #endif PyOS_setsig(SIGINT, old_inthandler); return NULL; } if (event_hook) rl_event_hook = (Function *)on_event_hook; else rl_event_hook = PyOS_InputHook; if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { rl_instream = sys_stdin; rl_outstream = sys_stdout; #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER rl_prep_terminal (1); #endif } p = readline(prompt); PyOS_setsig(SIGINT, old_inthandler); /* We must return a buffer allocated with PyMem_Malloc. */ if (p == NULL) { p = PyMem_Malloc(1); if (p != NULL) *p = '\0'; return p; } n = strlen(p); if (n > 0) { char *line; HISTORY_STATE *state = history_get_history_state(); if (state->length > 0) line = history_get(state->length)->line; else line = ""; if (strcmp(p, line)) add_history(p); /* the history docs don't say so, but the address of state changes each time history_get_history_state is called which makes me think it's freshly malloc'd memory... on the other hand, the address of the last line stays the same as long as history isn't extended, so it appears to be malloc'd but managed by the history package... */ free(state); } /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and release the original. */ q = p; p = PyMem_Malloc(n+2); if (p != NULL) { strncpy(p, q, n); p[n] = '\n'; p[n+1] = '\0'; } free(q); #ifdef SAVE_LOCALE setlocale(LC_CTYPE, saved_locale); /* Restore locale */ free(saved_locale); #endif return p; }