Code

Introduce an unlink(2) wrapper which gives warning if unlink failed
[git.git] / editor.c
1 #include "cache.h"
2 #include "strbuf.h"
3 #include "run-command.h"
5 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
6 {
7         const char *editor, *terminal;
9         editor = getenv("GIT_EDITOR");
10         if (!editor && editor_program)
11                 editor = editor_program;
12         if (!editor)
13                 editor = getenv("VISUAL");
14         if (!editor)
15                 editor = getenv("EDITOR");
17         terminal = getenv("TERM");
18         if (!editor && (!terminal || !strcmp(terminal, "dumb")))
19                 return error("Terminal is dumb but no VISUAL nor EDITOR defined.");
21         if (!editor)
22                 editor = "vi";
24         if (strcmp(editor, ":")) {
25                 size_t len = strlen(editor);
26                 int i = 0;
27                 int failed;
28                 const char *args[6];
29                 struct strbuf arg0 = STRBUF_INIT;
31                 if (strcspn(editor, "$ \t'") != len) {
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;
42                 failed = run_command_v_opt_cd_env(args, 0, NULL, env);
43                 strbuf_release(&arg0);
44                 if (failed)
45                         return error("There was a problem with the editor '%s'.",
46                                         editor);
47         }
49         if (!buffer)
50                 return 0;
51         if (strbuf_read_file(buffer, path, 0) < 0)
52                 return error("could not read file '%s': %s",
53                                 path, strerror(errno));
54         return 0;
55 }