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