t/Makefile: check helper scripts for non-portable shell commands too
[git/git-svn.git] / prompt.c
blobd7bb17cb663c2f47ad59d34ecea17c6cc977e815
1 #include "cache.h"
2 #include "run-command.h"
3 #include "strbuf.h"
4 #include "prompt.h"
5 #include "compat/terminal.h"
7 static char *do_askpass(const char *cmd, const char *prompt)
9 struct child_process pass;
10 const char *args[3];
11 static struct strbuf buffer = STRBUF_INIT;
12 int err = 0;
14 args[0] = cmd;
15 args[1] = prompt;
16 args[2] = NULL;
18 memset(&pass, 0, sizeof(pass));
19 pass.argv = args;
20 pass.out = -1;
22 if (start_command(&pass))
23 return NULL;
25 strbuf_reset(&buffer);
26 if (strbuf_read(&buffer, pass.out, 20) < 0)
27 err = 1;
29 close(pass.out);
31 if (finish_command(&pass))
32 err = 1;
34 if (err) {
35 error("unable to read askpass response from '%s'", cmd);
36 strbuf_release(&buffer);
37 return NULL;
40 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
42 return buffer.buf;
45 char *git_prompt(const char *prompt, int flags)
47 char *r = NULL;
49 if (flags & PROMPT_ASKPASS) {
50 const char *askpass;
52 askpass = getenv("GIT_ASKPASS");
53 if (!askpass)
54 askpass = askpass_program;
55 if (!askpass)
56 askpass = getenv("SSH_ASKPASS");
57 if (askpass && *askpass)
58 r = do_askpass(askpass, prompt);
61 if (!r)
62 r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
63 if (!r) {
64 /* prompts already contain ": " at the end */
65 die("could not read %s%s", prompt, strerror(errno));
67 return r;
70 char *git_getpass(const char *prompt)
72 return git_prompt(prompt, PROMPT_ASKPASS);