ci: run `hdr-check` as part of the `Static Analysis` job
[git/git.git] / prompt.c
CommitLineData
d3c58b83 1#include "cache.h"
b2141fc1 2#include "config.h"
d3c58b83
JK
3#include "run-command.h"
4#include "strbuf.h"
5#include "prompt.h"
a5090259 6#include "compat/terminal.h"
d3c58b83 7
1cb0134f 8static char *do_askpass(const char *cmd, const char *prompt)
d3c58b83 9{
d3180279 10 struct child_process pass = CHILD_PROCESS_INIT;
d3c58b83
JK
11 const char *args[3];
12 static struct strbuf buffer = STRBUF_INIT;
84d72733 13 int err = 0;
d3c58b83 14
1cb0134f 15 args[0] = cmd;
d3c58b83
JK
16 args[1] = prompt;
17 args[2] = NULL;
18
d3c58b83
JK
19 pass.argv = args;
20 pass.out = -1;
21
22 if (start_command(&pass))
84d72733 23 return NULL;
d3c58b83 24
e1c1a324 25 strbuf_reset(&buffer);
d3c58b83 26 if (strbuf_read(&buffer, pass.out, 20) < 0)
84d72733 27 err = 1;
d3c58b83
JK
28
29 close(pass.out);
30
31 if (finish_command(&pass))
84d72733
JK
32 err = 1;
33
34 if (err) {
35 error("unable to read askpass response from '%s'", cmd);
36 strbuf_release(&buffer);
37 return NULL;
38 }
d3c58b83
JK
39
40 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
41
e1c1a324 42 return buffer.buf;
d3c58b83 43}
1cb0134f
JK
44
45char *git_prompt(const char *prompt, int flags)
46{
84d72733 47 char *r = NULL;
1cb0134f
JK
48
49 if (flags & PROMPT_ASKPASS) {
50 const char *askpass;
51
52 askpass = getenv("GIT_ASKPASS");
53 if (!askpass)
54 askpass = askpass_program;
55 if (!askpass)
56 askpass = getenv("SSH_ASKPASS");
57 if (askpass && *askpass)
84d72733 58 r = do_askpass(askpass, prompt);
1cb0134f
JK
59 }
60
84d72733 61 if (!r) {
e652c0eb
JK
62 const char *err;
63
64 if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
65 r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
66 err = strerror(errno);
67 } else {
68 err = "terminal prompts disabled";
69 }
70 if (!r) {
71 /* prompts already contain ": " at the end */
72 die("could not read %s%s", prompt, err);
73 }
84d72733 74 }
1cb0134f
JK
75 return r;
76}