|
13 | 13 | #include <shellapi.h>
|
14 | 14 | #include <stdio.h>
|
15 | 15 |
|
16 |
| -static void |
17 |
| -PrintError(LPCWSTR wszPrefix, DWORD dwError) |
| 16 | +static void print_error(LPCWSTR prefix, DWORD error_number) |
18 | 17 | {
|
19 |
| - LPWSTR lpsz = NULL; |
20 |
| - DWORD cch = 0; |
21 |
| - |
22 |
| - cch = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
23 |
| - | FORMAT_MESSAGE_FROM_SYSTEM |
24 |
| - | FORMAT_MESSAGE_IGNORE_INSERTS, |
25 |
| - NULL, dwError, LANG_NEUTRAL, |
26 |
| - (LPTSTR)&lpsz, 0, NULL); |
27 |
| - if (cch < 1) { |
28 |
| - cch = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
29 |
| - | FORMAT_MESSAGE_FROM_STRING |
30 |
| - | FORMAT_MESSAGE_ARGUMENT_ARRAY, |
31 |
| - L"Code 0x%1!08x!", |
32 |
| - 0, LANG_NEUTRAL, (LPTSTR)&lpsz, 0, |
33 |
| - (va_list*)&dwError); |
34 |
| - } |
35 |
| - fwprintf(stderr, L"%s: %s", wszPrefix, lpsz); |
36 |
| - LocalFree((HLOCAL)lpsz); |
| 18 | + LPWSTR buffer = NULL; |
| 19 | + DWORD count = 0; |
| 20 | + |
| 21 | + count = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 22 | + | FORMAT_MESSAGE_FROM_SYSTEM |
| 23 | + | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 24 | + NULL, error_number, LANG_NEUTRAL, |
| 25 | + (LPTSTR)&buffer, 0, NULL); |
| 26 | + if (count < 1) |
| 27 | + count = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 28 | + | FORMAT_MESSAGE_FROM_STRING |
| 29 | + | FORMAT_MESSAGE_ARGUMENT_ARRAY, |
| 30 | + L"Code 0x%1!08x!", |
| 31 | + 0, LANG_NEUTRAL, (LPTSTR)&buffer, 0, |
| 32 | + (va_list*)&error_number); |
| 33 | + fwprintf(stderr, L"%s: %s", prefix, buffer); |
| 34 | + LocalFree((HLOCAL)buffer); |
37 | 35 | }
|
38 | 36 |
|
39 |
| -int |
40 |
| -main(void) |
| 37 | +int main(void) |
41 | 38 | {
|
42 |
| - int r = 1, wait = 1; |
43 |
| - WCHAR exepath[MAX_PATH], exe[MAX_PATH]; |
44 |
| - LPWSTR cmd = NULL, path2 = NULL, exep = exe; |
45 |
| - UINT codepage = 0; |
46 |
| - int len; |
47 |
| - |
48 |
| - /* get the installation location */ |
49 |
| - GetModuleFileName(NULL, exepath, MAX_PATH); |
50 |
| - PathRemoveFileSpec(exepath); |
51 |
| - PathRemoveFileSpec(exepath); |
52 |
| - |
53 |
| - /* set the default exe module */ |
54 |
| - wcscpy(exe, exepath); |
55 |
| - PathAppend(exe, L"bin\\git.exe"); |
56 |
| - |
57 |
| - /* if not set, set TERM to msys */ |
58 |
| - if (GetEnvironmentVariable(L"TERM", NULL, 0) == 0) { |
59 |
| - SetEnvironmentVariable(L"TERM", L"msys"); |
60 |
| - } |
61 |
| - |
62 |
| - /* if not set, set PLINK_PROTOCOL to ssh */ |
63 |
| - if (GetEnvironmentVariable(L"PLINK_PROTOCOL", NULL, 0) == 0) { |
64 |
| - SetEnvironmentVariable(L"PLINK_PROTOCOL", L"ssh"); |
65 |
| - } |
66 |
| - |
67 |
| - /* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE% |
68 |
| - * With roaming profiles: HOMEPATH is the roaming location and |
69 |
| - * USERPROFILE is the local location |
70 |
| - */ |
71 |
| - if (GetEnvironmentVariable(L"HOME", NULL, 0) == 0) { |
72 |
| - LPWSTR e = NULL; |
73 |
| - len = GetEnvironmentVariable(L"HOMEPATH", NULL, 0); |
74 |
| - if (len == 0) { |
75 |
| - len = GetEnvironmentVariable(L"USERPROFILE", NULL, 0); |
76 |
| - if (len != 0) { |
77 |
| - e = (LPWSTR)malloc(len * sizeof(WCHAR)); |
78 |
| - GetEnvironmentVariable(L"USERPROFILE", e, len); |
79 |
| - SetEnvironmentVariable(L"HOME", e); |
80 |
| - free(e); |
81 |
| - } |
82 |
| - } else { |
83 |
| - int n; |
84 |
| - len += GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0); |
85 |
| - e = (LPWSTR)malloc(sizeof(WCHAR) * (len + 2)); |
86 |
| - n = GetEnvironmentVariable(L"HOMEDRIVE", e, len); |
87 |
| - GetEnvironmentVariable(L"HOMEPATH", &e[n], len-n); |
88 |
| - SetEnvironmentVariable(L"HOME", e); |
89 |
| - free(e); |
| 39 | + int r = 1, wait = 1; |
| 40 | + WCHAR exepath[MAX_PATH], exe[MAX_PATH]; |
| 41 | + LPWSTR cmd = NULL, path2 = NULL, exep = exe; |
| 42 | + UINT codepage = 0; |
| 43 | + int len; |
| 44 | + |
| 45 | + /* get the installation location */ |
| 46 | + GetModuleFileName(NULL, exepath, MAX_PATH); |
| 47 | + PathRemoveFileSpec(exepath); |
| 48 | + PathRemoveFileSpec(exepath); |
| 49 | + |
| 50 | + /* set the default exe module */ |
| 51 | + wcscpy(exe, exepath); |
| 52 | + PathAppend(exe, L"bin\\git.exe"); |
| 53 | + |
| 54 | + /* if not set, set TERM to msys */ |
| 55 | + if (!GetEnvironmentVariable(L"TERM", NULL, 0)) |
| 56 | + SetEnvironmentVariable(L"TERM", L"msys"); |
| 57 | + |
| 58 | + /* if not set, set PLINK_PROTOCOL to ssh */ |
| 59 | + if (!GetEnvironmentVariable(L"PLINK_PROTOCOL", NULL, 0)) |
| 60 | + SetEnvironmentVariable(L"PLINK_PROTOCOL", L"ssh"); |
| 61 | + |
| 62 | + /* set HOME to %HOMEDRIVE%%HOMEPATH% or %USERPROFILE% |
| 63 | + * With roaming profiles: HOMEPATH is the roaming location and |
| 64 | + * USERPROFILE is the local location |
| 65 | + */ |
| 66 | + if (!GetEnvironmentVariable(L"HOME", NULL, 0)) { |
| 67 | + LPWSTR e = NULL; |
| 68 | + len = GetEnvironmentVariable(L"HOMEPATH", NULL, 0); |
| 69 | + if (len == 0) { |
| 70 | + len = GetEnvironmentVariable(L"USERPROFILE", NULL, 0); |
| 71 | + if (len != 0) { |
| 72 | + e = (LPWSTR)malloc(len * sizeof(WCHAR)); |
| 73 | + GetEnvironmentVariable(L"USERPROFILE", e, len); |
| 74 | + SetEnvironmentVariable(L"HOME", e); |
| 75 | + free(e); |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + int n; |
| 80 | + len += GetEnvironmentVariable(L"HOMEDRIVE", NULL, 0); |
| 81 | + e = (LPWSTR)malloc(sizeof(WCHAR) * (len + 2)); |
| 82 | + n = GetEnvironmentVariable(L"HOMEDRIVE", e, len); |
| 83 | + GetEnvironmentVariable(L"HOMEPATH", &e[n], len-n); |
| 84 | + SetEnvironmentVariable(L"HOME", e); |
| 85 | + free(e); |
| 86 | + } |
90 | 87 | }
|
91 |
| - } |
92 |
| - |
93 |
| - /* extend the PATH */ |
94 |
| - len = GetEnvironmentVariable(L"PATH", NULL, 0); |
95 |
| - len = sizeof(WCHAR) * (len + 2 * MAX_PATH); |
96 |
| - path2 = (LPWSTR)malloc(len); |
97 |
| - wcscpy(path2, exepath); |
98 |
| - PathAppend(path2, L"bin;"); |
99 |
| - /* should do this only if it exists */ |
100 |
| - wcscat(path2, exepath); |
101 |
| - PathAppend(path2, L"mingw\\bin;"); |
102 |
| - GetEnvironmentVariable(L"PATH", &path2[wcslen(path2)], |
103 |
| - (len/sizeof(WCHAR))-wcslen(path2)); |
104 |
| - SetEnvironmentVariable(L"PATH", path2); |
105 |
| - free(path2); |
106 |
| - |
107 |
| - |
108 |
| - /* fix up the command line to call git.exe |
109 |
| - * We have to be very careful about quoting here so we just |
110 |
| - * trim off the first argument and replace it leaving the rest |
111 |
| - * untouched. |
112 |
| - */ |
113 |
| - { |
114 |
| - int wargc = 0, gui = 0; |
115 |
| - LPWSTR cmdline = NULL; |
116 |
| - LPWSTR *wargv = NULL, p = NULL; |
117 |
| - cmdline = GetCommandLine(); |
118 |
| - wargv = CommandLineToArgvW(cmdline, &wargc); |
119 |
| - cmd = (LPWSTR)malloc(sizeof(WCHAR) * (wcslen(cmdline) + MAX_PATH)); |
120 |
| - if (wargc > 1 && wcsicmp(L"gui", wargv[1]) == 0) { |
121 |
| - wait = 0; |
122 |
| - if (wargc > 2 && wcsicmp(L"citool", wargv[2]) == 0) { |
123 |
| - wait = 1; |
124 |
| - wcscpy(cmd, L"git.exe"); |
125 |
| - } else { |
126 |
| - WCHAR script[MAX_PATH]; |
127 |
| - gui = 1; |
128 |
| - wcscpy(script, exepath); |
129 |
| - PathAppend(script, L"libexec\\git-core\\git-gui"); |
130 |
| - PathQuoteSpaces(script); |
131 |
| - wcscpy(cmd, L"wish.exe "); |
132 |
| - wcscat(cmd, script); |
133 |
| - wcscat(cmd, L" --"); |
134 |
| - exep = NULL; /* find the module from the commandline */ |
135 |
| - } |
136 |
| - } else { |
137 |
| - wcscpy(cmd, L"git.exe"); |
138 |
| - } |
139 |
| - /* find the first space after the initial parameter then append all */ |
140 |
| - p = wcschr(&cmdline[wcslen(wargv[0])], L' '); |
141 |
| - if (p && *p) { |
142 |
| - /* for git gui subcommands, remove the 'gui' word */ |
143 |
| - if (gui) { |
144 |
| - while (*p == L' ') ++p; |
145 |
| - p = wcschr(p, L' '); |
146 |
| - } |
147 |
| - if (p && *p) |
148 |
| - wcscat(cmd, p); |
| 88 | + |
| 89 | + /* extend the PATH */ |
| 90 | + len = GetEnvironmentVariable(L"PATH", NULL, 0); |
| 91 | + len = sizeof(WCHAR) * (len + 2 * MAX_PATH); |
| 92 | + path2 = (LPWSTR)malloc(len); |
| 93 | + wcscpy(path2, exepath); |
| 94 | + PathAppend(path2, L"bin;"); |
| 95 | + /* should do this only if it exists */ |
| 96 | + wcscat(path2, exepath); |
| 97 | + PathAppend(path2, L"mingw\\bin;"); |
| 98 | + GetEnvironmentVariable(L"PATH", &path2[wcslen(path2)], |
| 99 | + (len/sizeof(WCHAR))-wcslen(path2)); |
| 100 | + SetEnvironmentVariable(L"PATH", path2); |
| 101 | + free(path2); |
| 102 | + |
| 103 | + |
| 104 | + /* fix up the command line to call git.exe |
| 105 | + * We have to be very careful about quoting here so we just |
| 106 | + * trim off the first argument and replace it leaving the rest |
| 107 | + * untouched. |
| 108 | + */ |
| 109 | + { |
| 110 | + int wargc = 0, gui = 0; |
| 111 | + LPWSTR cmdline = NULL; |
| 112 | + LPWSTR *wargv = NULL, p = NULL; |
| 113 | + cmdline = GetCommandLine(); |
| 114 | + wargv = CommandLineToArgvW(cmdline, &wargc); |
| 115 | + cmd = (LPWSTR)malloc(sizeof(WCHAR) * |
| 116 | + (wcslen(cmdline) + MAX_PATH)); |
| 117 | + if (wargc > 1 && wcsicmp(L"gui", wargv[1]) == 0) { |
| 118 | + wait = 0; |
| 119 | + if (wargc > 2 && wcsicmp(L"citool", wargv[2]) == 0) { |
| 120 | + wait = 1; |
| 121 | + wcscpy(cmd, L"git.exe"); |
| 122 | + } |
| 123 | + else { |
| 124 | + WCHAR script[MAX_PATH]; |
| 125 | + gui = 1; |
| 126 | + wcscpy(script, exepath); |
| 127 | + PathAppend(script, |
| 128 | + L"libexec\\git-core\\git-gui"); |
| 129 | + PathQuoteSpaces(script); |
| 130 | + wcscpy(cmd, L"wish.exe "); |
| 131 | + wcscat(cmd, script); |
| 132 | + wcscat(cmd, L" --"); |
| 133 | + /* find the module from the commandline */ |
| 134 | + exep = NULL; |
| 135 | + } |
| 136 | + } |
| 137 | + else |
| 138 | + wcscpy(cmd, L"git.exe"); |
| 139 | + |
| 140 | + /* append all after first space after the initial parameter */ |
| 141 | + p = wcschr(&cmdline[wcslen(wargv[0])], L' '); |
| 142 | + if (p && *p) { |
| 143 | + /* for git gui subcommands, remove the 'gui' word */ |
| 144 | + if (gui) { |
| 145 | + while (*p == L' ') ++p; |
| 146 | + p = wcschr(p, L' '); |
| 147 | + } |
| 148 | + if (p && *p) |
| 149 | + wcscat(cmd, p); |
| 150 | + } |
| 151 | + LocalFree(wargv); |
149 | 152 | }
|
150 |
| - LocalFree(wargv); |
151 |
| - } |
152 |
| - |
153 |
| - /* set the console to ANSI/GUI codepage */ |
154 |
| - codepage = GetConsoleCP(); |
155 |
| - SetConsoleCP(GetACP()); |
156 |
| - |
157 |
| - { |
158 |
| - STARTUPINFO si; |
159 |
| - PROCESS_INFORMATION pi; |
160 |
| - BOOL br = FALSE; |
161 |
| - ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); |
162 |
| - ZeroMemory(&si, sizeof(STARTUPINFO)); |
163 |
| - si.cb = sizeof(STARTUPINFO); |
164 |
| - br = CreateProcess(exep,/* module: null means use command line */ |
165 |
| - cmd, /* modified command line */ |
166 |
| - NULL, /* process handle inheritance */ |
167 |
| - NULL, /* thread handle inheritance */ |
168 |
| - TRUE, /* handles inheritable? */ |
169 |
| - CREATE_UNICODE_ENVIRONMENT, |
170 |
| - NULL, /* environment: use parent */ |
171 |
| - NULL, /* starting directory: use parent */ |
172 |
| - &si, &pi); |
173 |
| - if (br) { |
174 |
| - if (wait) |
175 |
| - WaitForSingleObject(pi.hProcess, INFINITE); |
176 |
| - if (!GetExitCodeProcess(pi.hProcess, (DWORD *)&r)) |
177 |
| - PrintError(L"error reading exit code", GetLastError()); |
178 |
| - CloseHandle(pi.hProcess); |
179 |
| - } else { |
180 |
| - PrintError(L"error launching git", GetLastError()); |
181 |
| - r = 1; |
| 153 | + |
| 154 | + /* set the console to ANSI/GUI codepage */ |
| 155 | + codepage = GetConsoleCP(); |
| 156 | + SetConsoleCP(GetACP()); |
| 157 | + |
| 158 | + { |
| 159 | + STARTUPINFO si; |
| 160 | + PROCESS_INFORMATION pi; |
| 161 | + BOOL br = FALSE; |
| 162 | + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); |
| 163 | + ZeroMemory(&si, sizeof(STARTUPINFO)); |
| 164 | + si.cb = sizeof(STARTUPINFO); |
| 165 | + br = CreateProcess(/* module: null means use command line */ |
| 166 | + exep, |
| 167 | + cmd, /* modified command line */ |
| 168 | + NULL, /* process handle inheritance */ |
| 169 | + NULL, /* thread handle inheritance */ |
| 170 | + TRUE, /* handles inheritable? */ |
| 171 | + CREATE_UNICODE_ENVIRONMENT, |
| 172 | + NULL, /* environment: use parent */ |
| 173 | + NULL, /* starting directory: use parent */ |
| 174 | + &si, &pi); |
| 175 | + if (br) { |
| 176 | + if (wait) |
| 177 | + WaitForSingleObject(pi.hProcess, INFINITE); |
| 178 | + if (!GetExitCodeProcess(pi.hProcess, (DWORD *)&r)) |
| 179 | + print_error(L"error reading exit code", |
| 180 | + GetLastError()); |
| 181 | + CloseHandle(pi.hProcess); |
| 182 | + } |
| 183 | + else { |
| 184 | + print_error(L"error launching git", GetLastError()); |
| 185 | + r = 1; |
| 186 | + } |
182 | 187 | }
|
183 |
| - } |
184 | 188 |
|
185 |
| - free(cmd); |
| 189 | + free(cmd); |
186 | 190 |
|
187 |
| - /* reset the console codepage */ |
188 |
| - SetConsoleCP(codepage); |
189 |
| - ExitProcess(r); |
| 191 | + /* reset the console codepage */ |
| 192 | + SetConsoleCP(codepage); |
| 193 | + ExitProcess(r); |
190 | 194 | }
|
191 |
| - |
192 |
| -/* |
193 |
| - * Local variables: |
194 |
| - * mode: c |
195 |
| - * indent-tabs-mode: nil |
196 |
| - * c-basic-offset: 4 |
197 |
| - * tab-width: 4 |
198 |
| - * End: |
199 |
| - */ |
0 commit comments