| 58 | } |
| 59 | |
| 60 | static int launch_specified_editor(const char *editor, const char *path, |
| 61 | struct strbuf *buffer, const char *const *env) |
| 62 | { |
| 63 | if (!editor) |
| 64 | return error("Terminal is dumb, but EDITOR unset"); |
| 65 | |
| 66 | if (strcmp(editor, ":")) { |
| 67 | struct strbuf realpath = STRBUF_INIT; |
| 68 | struct child_process p = CHILD_PROCESS_INIT; |
| 69 | int ret, sig; |
| 70 | int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2); |
| 71 | |
| 72 | if (print_waiting_for_editor) { |
| 73 | /* |
| 74 | * A dumb terminal cannot erase the line later on. Add a |
| 75 | * newline to separate the hint from subsequent output. |
| 76 | * |
| 77 | * Make sure that our message is separated with a whitespace |
| 78 | * from further cruft that may be written by the editor. |
| 79 | */ |
| 80 | const char term = is_terminal_dumb() ? '\n' : ' '; |
| 81 | |
| 82 | fprintf(stderr, |
| 83 | _("hint: Waiting for your editor to close the file...%c"), |
| 84 | term); |
| 85 | fflush(stderr); |
| 86 | } |
| 87 | |
| 88 | strbuf_realpath(&realpath, path, 1); |
| 89 | |
| 90 | strvec_pushl(&p.args, editor, realpath.buf, NULL); |
| 91 | if (env) |
| 92 | strvec_pushv(&p.env, (const char **)env); |
| 93 | p.use_shell = 1; |
| 94 | p.trace2_child_class = "editor"; |
| 95 | if (start_command(&p) < 0) { |
| 96 | strbuf_release(&realpath); |
| 97 | return error("unable to start editor '%s'", editor); |
| 98 | } |
| 99 | |
| 100 | sigchain_push(SIGINT, SIG_IGN); |
| 101 | sigchain_push(SIGQUIT, SIG_IGN); |
| 102 | ret = finish_command(&p); |
| 103 | strbuf_release(&realpath); |
| 104 | sig = ret - 128; |
| 105 | sigchain_pop(SIGINT); |
| 106 | sigchain_pop(SIGQUIT); |
| 107 | if (sig == SIGINT || sig == SIGQUIT) |
| 108 | raise(sig); |
| 109 | if (print_waiting_for_editor && !is_terminal_dumb()) |
| 110 | /* |
| 111 | * Erase the entire line to avoid wasting the |
| 112 | * vertical space. |
| 113 | */ |
| 114 | term_clear_line(); |
| 115 | if (ret) |
| 116 | return error("there was a problem with the editor '%s'", |
| 117 | editor); |
no test coverage detected