forked from git-for-windows/build-extra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit-git-bash.c
104 lines (85 loc) · 2.19 KB
/
edit-git-bash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define STRICT
#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <stdlib.h>
#ifdef STANDALONE_EXE
#include <shellapi.h>
#include <stdio.h>
#elif defined(_WIN64)
#error "The .dll needs to be 32-bit to match InnoSetup's architecture"
#endif
#ifdef DEBUG
static void print_error(LPCWSTR prefix, DWORD error_number)
{
LPWSTR buffer = NULL;
DWORD count = 0;
count = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_number, LANG_NEUTRAL,
(LPTSTR)&buffer, 0, NULL);
if (count < 1)
count = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_ARGUMENT_ARRAY,
L"Code 0x%1!08x!",
0, LANG_NEUTRAL, (LPTSTR)&buffer, 0,
(va_list*)&error_number);
MessageBox(NULL, buffer, prefix, MB_OK);
LocalFree((HLOCAL)buffer);
}
#endif
#ifdef STANDALONE_EXE
static
#else
WINAPI __declspec(dllexport)
#endif
int edit_git_bash(LPWSTR git_bash_path, LPWSTR new_command_line)
{
HANDLE handle;
int len, alloc, result = 0;
WCHAR *buffer;
len = wcslen(new_command_line);
alloc = 2 * (len + 16);
buffer = calloc(alloc, 1);
if (!buffer)
return 1;
buffer[0] = (WCHAR) len;
memcpy(buffer + 1, new_command_line, 2 * len);
if (!(handle = BeginUpdateResource(git_bash_path, FALSE)))
return 2;
if (!UpdateResource(handle, RT_STRING, MAKEINTRESOURCE(1),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
buffer, alloc))
result = 3;
if (!EndUpdateResource(handle, FALSE))
return 4;
return result;
}
#ifdef STANDALONE_EXE
int main(int argc, char **argv)
{
int wargc, result;
LPWSTR *wargv;
#ifdef DEBUG
LPTSTR cmdLine = GetCommandLineW();
fwprintf(stderr, L"Command Line: %s\n", cmdLine);
wargv = CommandLineToArgvW(cmdLine, &wargc);
for (int i = 1; i < wargc; ++i)
fwprintf(stderr, L"Arg %d: %s\n", i, wargv[i]);
#else
wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
#endif
if (wargc != 3) {
fwprintf(stderr, L"Usage: %s <path-to-exe> <new-commad-line>\n",
wargv[0]);
exit(1);
}
result = edit_git_bash(wargv[1], wargv[2]);
if (result)
fwprintf(stderr, L"Error editing %s: %d\n", wargv[1], result);
return result;
}
#endif