Do not use VISUAL editor on dumb terminals
[git/git.git] / editor.c
CommitLineData
d82f33e2
SB
1#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
4
7198203a 5int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
d82f33e2 6{
d33738d7
JN
7 const char *editor = getenv("GIT_EDITOR");
8 const char *terminal = getenv("TERM");
9 int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
d82f33e2 10
d82f33e2
SB
11 if (!editor && editor_program)
12 editor = editor_program;
d33738d7 13 if (!editor && !terminal_is_dumb)
d82f33e2
SB
14 editor = getenv("VISUAL");
15 if (!editor)
16 editor = getenv("EDITOR");
17
d33738d7
JN
18 if (!editor && terminal_is_dumb)
19 return error("terminal is dumb, but EDITOR unset");
d82f33e2
SB
20
21 if (!editor)
22 editor = "vi";
23
24 if (strcmp(editor, ":")) {
25 size_t len = strlen(editor);
26 int i = 0;
7198203a 27 int failed;
d82f33e2 28 const char *args[6];
f285a2d7 29 struct strbuf arg0 = STRBUF_INIT;
d82f33e2 30
eab58f1e 31 if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) {
d82f33e2
SB
32 /* there are specials */
33 strbuf_addf(&arg0, "%s \"$@\"", editor);
34 args[i++] = "sh";
35 args[i++] = "-c";
36 args[i++] = arg0.buf;
37 }
38 args[i++] = editor;
39 args[i++] = path;
40 args[i] = NULL;
41
7198203a 42 failed = run_command_v_opt_cd_env(args, 0, NULL, env);
d82f33e2 43 strbuf_release(&arg0);
7198203a
SB
44 if (failed)
45 return error("There was a problem with the editor '%s'.",
46 editor);
d82f33e2
SB
47 }
48
49 if (!buffer)
7198203a 50 return 0;
d82f33e2 51 if (strbuf_read_file(buffer, path, 0) < 0)
7198203a
SB
52 return error("could not read file '%s': %s",
53 path, strerror(errno));
54 return 0;
d82f33e2 55}