]>
Commit | Line | Data |
---|---|---|
1 | #include "git-compat-util.h" | |
2 | #include "parse.h" | |
3 | #include "environment.h" | |
4 | #include "run-command.h" | |
5 | #include "strbuf.h" | |
6 | #include "prompt.h" | |
7 | #include "compat/terminal.h" | |
8 | ||
9 | static char *do_askpass(const char *cmd, const char *prompt) | |
10 | { | |
11 | struct child_process pass = CHILD_PROCESS_INIT; | |
12 | static struct strbuf buffer = STRBUF_INIT; | |
13 | int err = 0; | |
14 | ||
15 | strvec_push(&pass.args, cmd); | |
16 | strvec_push(&pass.args, prompt); | |
17 | ||
18 | pass.out = -1; | |
19 | ||
20 | if (start_command(&pass)) | |
21 | return NULL; | |
22 | ||
23 | strbuf_reset(&buffer); | |
24 | if (strbuf_read(&buffer, pass.out, 20) < 0) | |
25 | err = 1; | |
26 | ||
27 | close(pass.out); | |
28 | ||
29 | if (finish_command(&pass)) | |
30 | err = 1; | |
31 | ||
32 | if (err) { | |
33 | error("unable to read askpass response from '%s'", cmd); | |
34 | strbuf_release(&buffer); | |
35 | return NULL; | |
36 | } | |
37 | ||
38 | strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); | |
39 | ||
40 | return buffer.buf; | |
41 | } | |
42 | ||
43 | char *git_prompt(const char *prompt, int flags) | |
44 | { | |
45 | char *r = NULL; | |
46 | ||
47 | if (flags & PROMPT_ASKPASS) { | |
48 | const char *askpass; | |
49 | ||
50 | askpass = getenv("GIT_ASKPASS"); | |
51 | if (!askpass) | |
52 | askpass = askpass_program; | |
53 | if (!askpass) | |
54 | askpass = getenv("SSH_ASKPASS"); | |
55 | if (askpass && *askpass) | |
56 | r = do_askpass(askpass, prompt); | |
57 | } | |
58 | ||
59 | if (!r) { | |
60 | const char *err; | |
61 | ||
62 | if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) { | |
63 | r = git_terminal_prompt(prompt, flags & PROMPT_ECHO); | |
64 | err = strerror(errno); | |
65 | } else { | |
66 | err = "terminal prompts disabled"; | |
67 | } | |
68 | if (!r) { | |
69 | /* prompts already contain ": " at the end */ | |
70 | die("could not read %s%s", prompt, err); | |
71 | } | |
72 | } | |
73 | return r; | |
74 | } | |
75 | ||
76 | int git_read_line_interactively(struct strbuf *line) | |
77 | { | |
78 | int ret; | |
79 | ||
80 | fflush(stdout); | |
81 | ret = strbuf_getline_lf(line, stdin); | |
82 | if (ret != EOF) | |
83 | strbuf_trim_trailing_newline(line); | |
84 | ||
85 | return ret; | |
86 | } |