Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "strbuf.h" |
| 3 | #include "run-command.h" |
| 4 | |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 5 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 6 | { |
| 7 | const char *editor, *terminal; |
| 8 | |
| 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"); |
| 16 | |
| 17 | terminal = getenv("TERM"); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 18 | if (!editor && (!terminal || !strcmp(terminal, "dumb"))) |
| 19 | return error("Terminal is dumb but no VISUAL nor EDITOR defined."); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 20 | |
| 21 | if (!editor) |
| 22 | editor = "vi"; |
| 23 | |
| 24 | if (strcmp(editor, ":")) { |
| 25 | size_t len = strlen(editor); |
| 26 | int i = 0; |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 27 | int failed; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 28 | const char *args[6]; |
| 29 | struct strbuf arg0; |
| 30 | |
| 31 | strbuf_init(&arg0, 0); |
| 32 | if (strcspn(editor, "$ \t'") != len) { |
| 33 | /* there are specials */ |
| 34 | strbuf_addf(&arg0, "%s \"$@\"", editor); |
| 35 | args[i++] = "sh"; |
| 36 | args[i++] = "-c"; |
| 37 | args[i++] = arg0.buf; |
| 38 | } |
| 39 | args[i++] = editor; |
| 40 | args[i++] = path; |
| 41 | args[i] = NULL; |
| 42 | |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 43 | failed = run_command_v_opt_cd_env(args, 0, NULL, env); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 44 | strbuf_release(&arg0); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 45 | if (failed) |
| 46 | return error("There was a problem with the editor '%s'.", |
| 47 | editor); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 51 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 52 | if (strbuf_read_file(buffer, path, 0) < 0) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 53 | return error("could not read file '%s': %s", |
| 54 | path, strerror(errno)); |
| 55 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 56 | } |