use DUP_ARRAY
[git/debian.git] / editor.c
blob008c04fe2f6e0a8b39bc02005f0a576d92784bae
1 #include "cache.h"
2 #include "config.h"
3 #include "strbuf.h"
4 #include "strvec.h"
5 #include "run-command.h"
6 #include "sigchain.h"
8 #ifndef DEFAULT_EDITOR
9 #define DEFAULT_EDITOR "vi"
10 #endif
12 int is_terminal_dumb(void)
14 const char *terminal = getenv("TERM");
15 return !terminal || !strcmp(terminal, "dumb");
18 const char *git_editor(void)
20 const char *editor = getenv("GIT_EDITOR");
21 int terminal_is_dumb = is_terminal_dumb();
23 if (!editor && editor_program)
24 editor = editor_program;
25 if (!editor && !terminal_is_dumb)
26 editor = getenv("VISUAL");
27 if (!editor)
28 editor = getenv("EDITOR");
30 if (!editor && terminal_is_dumb)
31 return NULL;
33 if (!editor)
34 editor = DEFAULT_EDITOR;
36 return editor;
39 const char *git_sequence_editor(void)
41 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
43 if (!editor)
44 git_config_get_string_tmp("sequence.editor", &editor);
45 if (!editor)
46 editor = git_editor();
48 return editor;
51 static int launch_specified_editor(const char *editor, const char *path,
52 struct strbuf *buffer, const char *const *env)
54 if (!editor)
55 return error("Terminal is dumb, but EDITOR unset");
57 if (strcmp(editor, ":")) {
58 struct strbuf realpath = STRBUF_INIT;
59 struct child_process p = CHILD_PROCESS_INIT;
60 int ret, sig;
61 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
63 if (print_waiting_for_editor) {
65 * A dumb terminal cannot erase the line later on. Add a
66 * newline to separate the hint from subsequent output.
68 * Make sure that our message is separated with a whitespace
69 * from further cruft that may be written by the editor.
71 const char term = is_terminal_dumb() ? '\n' : ' ';
73 fprintf(stderr,
74 _("hint: Waiting for your editor to close the file...%c"),
75 term);
76 fflush(stderr);
79 strbuf_realpath(&realpath, path, 1);
81 strvec_pushl(&p.args, editor, realpath.buf, NULL);
82 if (env)
83 strvec_pushv(&p.env, (const char **)env);
84 p.use_shell = 1;
85 p.trace2_child_class = "editor";
86 if (start_command(&p) < 0) {
87 strbuf_release(&realpath);
88 return error("unable to start editor '%s'", editor);
91 sigchain_push(SIGINT, SIG_IGN);
92 sigchain_push(SIGQUIT, SIG_IGN);
93 ret = finish_command(&p);
94 strbuf_release(&realpath);
95 sig = ret - 128;
96 sigchain_pop(SIGINT);
97 sigchain_pop(SIGQUIT);
98 if (sig == SIGINT || sig == SIGQUIT)
99 raise(sig);
100 if (ret)
101 return error("There was a problem with the editor '%s'.",
102 editor);
104 if (print_waiting_for_editor && !is_terminal_dumb())
106 * Erase the entire line to avoid wasting the
107 * vertical space.
109 term_clear_line();
112 if (!buffer)
113 return 0;
114 if (strbuf_read_file(buffer, path, 0) < 0)
115 return error_errno("could not read file '%s'", path);
116 return 0;
119 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
121 return launch_specified_editor(git_editor(), path, buffer, env);
124 int launch_sequence_editor(const char *path, struct strbuf *buffer,
125 const char *const *env)
127 return launch_specified_editor(git_sequence_editor(), path, buffer, env);