|
| 1 | +#define WIN32_LEAN_AND_MEAN |
| 2 | +#include <windows.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include "git-askpass.h" |
| 6 | + |
| 7 | +INT_PTR CALLBACK PasswordProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) |
| 8 | +{ |
| 9 | + wchar_t *lpszPassword = NULL; |
| 10 | + WORD cchPassword; |
| 11 | + |
| 12 | + switch (message) |
| 13 | + { |
| 14 | + case WM_INITDIALOG: |
| 15 | + /* Display the prompt */ |
| 16 | + SetDlgItemTextW(hDlg, IDC_PROMPT, (wchar_t *) lParam); |
| 17 | + if (GetDlgCtrlID((HWND) wParam) != IDE_PASSWORDEDIT) { |
| 18 | + SetFocus(GetDlgItem(hDlg, IDE_PASSWORDEDIT)); |
| 19 | + return FALSE; |
| 20 | + } |
| 21 | + return TRUE; |
| 22 | + case WM_COMMAND: |
| 23 | + switch(wParam) |
| 24 | + { |
| 25 | + case IDOK: |
| 26 | + /* Get number of characters. */ |
| 27 | + cchPassword = (WORD) SendDlgItemMessage(hDlg, |
| 28 | + IDE_PASSWORDEDIT, |
| 29 | + EM_LINELENGTH, |
| 30 | + (WPARAM) 0, |
| 31 | + (LPARAM) 0); |
| 32 | + |
| 33 | + lpszPassword = (wchar_t *) malloc(sizeof(wchar_t) * (cchPassword + 1)); |
| 34 | + if (!lpszPassword) { |
| 35 | + MessageBoxW(NULL, L"Out of memory asking for a password", L"Error!", MB_OK); |
| 36 | + EndDialog(hDlg, FALSE); |
| 37 | + return TRUE; |
| 38 | + } |
| 39 | + /* Put the number of characters into first word of buffer. */ |
| 40 | + *((LPWORD)lpszPassword) = cchPassword; |
| 41 | + |
| 42 | + /* Get the characters. */ |
| 43 | + SendDlgItemMessageW(hDlg, |
| 44 | + IDE_PASSWORDEDIT, |
| 45 | + EM_GETLINE, |
| 46 | + (WPARAM) 0, /* line 0 */ |
| 47 | + (LPARAM) lpszPassword); |
| 48 | + |
| 49 | + /* Null-terminate the string. */ |
| 50 | + lpszPassword[cchPassword] = 0; |
| 51 | + |
| 52 | + wprintf(L"%ls\n", lpszPassword); |
| 53 | + |
| 54 | + EndDialog(hDlg, TRUE); |
| 55 | + free(lpszPassword); |
| 56 | + return TRUE; |
| 57 | + |
| 58 | + case IDCANCEL: |
| 59 | + EndDialog(hDlg, FALSE); |
| 60 | + return TRUE; |
| 61 | + } |
| 62 | + return 0; |
| 63 | + } |
| 64 | + return FALSE; |
| 65 | + |
| 66 | + UNREFERENCED_PARAMETER(lParam); |
| 67 | +} |
| 68 | + |
| 69 | +int wmain(int argc, wchar_t **wargv) |
| 70 | +{ |
| 71 | + INT_PTR res; |
| 72 | + wchar_t *prompt = NULL; |
| 73 | + |
| 74 | + if (argc < 2) { |
| 75 | + MessageBoxW(NULL, L"Usage: git askpass <prompt>", L"Error!", MB_OK); |
| 76 | + return 1; |
| 77 | + } |
| 78 | + |
| 79 | + if (argc > 2) { |
| 80 | + size_t count = wcslen(wargv[1]), i; |
| 81 | + |
| 82 | + for (i = 2; i < argc; i++) |
| 83 | + count += 1 + wcslen(wargv[i]); |
| 84 | + |
| 85 | + prompt = malloc((count + 1) * sizeof(*prompt)); |
| 86 | + if (!prompt) { |
| 87 | + MessageBoxW(NULL, L"Out of memory asking for a password", L"Error!", MB_OK); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + wcscpy(prompt, wargv[1]); |
| 92 | + count = wcslen(wargv[1]); |
| 93 | + |
| 94 | + for (i = 2; i < argc; i++) { |
| 95 | + prompt[count++] = L' '; |
| 96 | + wcscpy(prompt + count, wargv[i]); |
| 97 | + count += wcslen(wargv[i]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + res = DialogBoxParamW(NULL, /* application instance */ |
| 102 | + MAKEINTRESOURCEW(IDD_PASSWORD), /* dialog box resource */ |
| 103 | + NULL, /* owner window */ |
| 104 | + PasswordProc, /* dialog box window procedure */ |
| 105 | + (LPARAM) (prompt ? prompt : wargv[1])); |
| 106 | + |
| 107 | + free(prompt); |
| 108 | + return !res; |
| 109 | +} |
0 commit comments