OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <windows.h> | |
6 | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 | |
10 #include <algorithm> | |
11 #include <iterator> | |
12 #include <string> | |
13 | |
14 using namespace std; | |
15 | |
16 // Don't use stderr for errors because VS has large buffers on them, leading | |
17 // to confusing error output. | |
18 static void Fatal(const wchar_t* msg) { | |
19 wprintf(L"supalink fatal error: %s\n", msg); | |
20 exit(1); | |
21 } | |
22 | |
23 static wstring ErrorMessageToString(DWORD err) { | |
24 wchar_t* msg_buf = NULL; | |
25 DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
26 FORMAT_MESSAGE_FROM_SYSTEM, | |
27 NULL, | |
28 err, | |
29 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
30 reinterpret_cast<LPTSTR>(&msg_buf), | |
31 0, | |
32 NULL); | |
33 if (!rc) | |
34 return L"unknown error"; | |
35 wstring ret(msg_buf); | |
36 LocalFree(msg_buf); | |
37 return ret; | |
38 } | |
39 | |
40 static const wchar_t* const g_search_for[] = { | |
41 L"link.exe\" ", | |
42 L"link\" ", | |
43 L"link.exe ", | |
44 L"link ", | |
45 }; | |
46 | |
47 static void Fallback(const wchar_t* msg = NULL) { | |
48 if (msg) { | |
49 wprintf(L"supalink failed (%s), trying to fallback to standard link.\n", | |
50 msg); | |
51 wprintf(L"Original command line: %s\n", GetCommandLine()); | |
52 fflush(stdout); | |
53 } | |
54 | |
55 STARTUPINFO startup_info = { sizeof(STARTUPINFO) }; | |
56 PROCESS_INFORMATION process_info; | |
57 DWORD exit_code; | |
58 | |
59 GetStartupInfo(&startup_info); | |
60 | |
61 wstring orig_cmd(GetCommandLine()); | |
62 wstring cmd; | |
63 wstring replace_with = L"link.exe.supalink_orig.exe"; | |
64 wstring orig_lowercase; | |
65 | |
66 // Avoid searching for case-variations by making a lower-case copy. | |
67 transform(orig_cmd.begin(), | |
68 orig_cmd.end(), | |
69 back_inserter(orig_lowercase), | |
70 tolower); | |
71 | |
72 for (size_t i = 0; i < ARRAYSIZE(g_search_for); ++i) { | |
73 wstring linkexe = g_search_for[i]; | |
74 wstring::size_type at = orig_lowercase.find(linkexe, 0); | |
75 if (at == wstring::npos) | |
76 continue; | |
77 if (linkexe[linkexe.size() - 2] == L'"') | |
78 replace_with += L"\" "; | |
79 else | |
80 replace_with += L" "; | |
81 cmd = orig_cmd.replace(at, linkexe.size(), replace_with); | |
82 break; | |
83 } | |
84 if (cmd == L"") { | |
85 wprintf(L"Original run '%s'\n", orig_cmd.c_str()); | |
86 Fatal(L"Couldn't find link.exe (or similar) in command line"); | |
87 } | |
88 | |
89 if (getenv("SUPALINK_DEBUG")) { | |
90 wprintf(L" running '%s'\n", cmd.c_str()); | |
91 fflush(stdout); | |
92 } | |
93 if (!CreateProcess(NULL, | |
94 reinterpret_cast<LPWSTR>(const_cast<wchar_t *>( | |
95 cmd.c_str())), | |
96 NULL, | |
97 NULL, | |
98 TRUE, | |
99 0, | |
100 NULL, | |
101 NULL, | |
102 &startup_info, &process_info)) { | |
103 wstring error = ErrorMessageToString(GetLastError()); | |
104 Fatal(error.c_str()); | |
105 } | |
106 CloseHandle(process_info.hThread); | |
107 WaitForSingleObject(process_info.hProcess, INFINITE); | |
108 GetExitCodeProcess(process_info.hProcess, &exit_code); | |
109 CloseHandle(process_info.hProcess); | |
110 exit(exit_code); | |
111 } | |
112 | |
113 wstring SlurpFile(const wchar_t* path) { | |
114 FILE* f = _wfopen(path, L"rb, ccs=UNICODE"); | |
115 if (!f) Fallback(L"couldn't read file"); | |
116 fseek(f, 0, SEEK_END); | |
117 long len = ftell(f); | |
118 rewind(f); | |
119 wchar_t* data = reinterpret_cast<wchar_t*>(malloc(len)); | |
120 fread(data, 1, len, f); | |
121 fclose(f); | |
122 wstring ret(data, len/sizeof(wchar_t)); | |
123 free(data); | |
124 return ret; | |
125 } | |
126 | |
127 void DumpFile(const wchar_t* path, wstring& contents) { | |
128 FILE* f = _wfopen(path, L"wb, ccs=UTF-16LE"); | |
129 if (!f) Fallback(L"couldn't write file"); | |
130 | |
131 fwrite(contents.c_str(), sizeof(wchar_t), contents.size(), f); | |
132 if (ferror(f)) | |
133 Fatal(L"failed during response rewrite"); | |
134 fclose(f); | |
135 } | |
136 | |
137 // Input command line is assumed to be of the form: | |
138 // | |
139 // link.exe @C:\src\...\RSP00003045884740.rsp /NOLOGO /ERRORREPORT:PROMPT | |
140 // | |
141 // Specifically, we parse & hack the contents of argv[1] and pass the rest | |
142 // onwards. | |
143 int wmain(int argc, wchar_t** argv) { | |
144 ULONGLONG start_time = 0, end_time; | |
145 | |
146 int rsp_file_index = -1; | |
147 | |
148 if (argc < 2) | |
149 Fallback(L"too few commmand line args"); | |
150 | |
151 for (int i = 1; i < argc; ++i) { | |
152 if (argv[i][0] == L'@') { | |
153 rsp_file_index = i; | |
154 break; | |
155 } | |
156 } | |
157 | |
158 if (rsp_file_index == -1) | |
159 Fallback(L"couldn't find a response file in argv"); | |
160 | |
161 if (_wgetenv(L"SUPALINK_DEBUG")) | |
162 start_time = GetTickCount64(); | |
163 | |
164 wstring rsp = SlurpFile(&argv[rsp_file_index][1]); | |
165 | |
166 // The first line of this file is all we try to fix. It's a bunch of | |
167 // quoted space separated items. Simplest thing seems to be replacing " " | |
168 // with "\n". So, just slurp the file, replace, spit it out to the same | |
169 // file and continue on our way. | |
170 | |
171 // Took about .5s when using the naive .replace loop to replace " " with | |
172 // "\r\n" so write the silly loop instead. | |
173 wstring fixed; | |
174 fixed.reserve(rsp.size() * 2); | |
175 | |
176 for (const wchar_t* s = rsp.c_str(); *s;) { | |
177 if (*s == '"' && *(s + 1) == ' ' && *(s + 2) == '"') { | |
178 fixed += L"\"\r\n\""; | |
179 s += 3; | |
180 } else { | |
181 fixed += *s++; | |
182 } | |
183 } | |
184 | |
185 DumpFile(&argv[rsp_file_index][1], fixed); | |
186 | |
187 if (_wgetenv(L"SUPALINK_DEBUG")) { | |
188 wstring backup_copy(&argv[rsp_file_index][1]); | |
189 backup_copy += L".copy"; | |
190 DumpFile(backup_copy.c_str(), fixed); | |
191 | |
192 end_time = GetTickCount64(); | |
193 | |
194 wprintf(L" took %.2fs to modify @rsp file\n", | |
195 (end_time - start_time) / 1000.0); | |
196 } | |
197 | |
198 Fallback(); | |
199 } | |
OLD | NEW |