Main.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. //---------------------------------------------------------------------------
  2. #include <stdexcept>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include "Console.h"
  9. #define MAX_ATTEMPTS 10
  10. //---------------------------------------------------------------------------
  11. #define LENOF(x) ( (sizeof((x))) / (sizeof(*(x))))
  12. //---------------------------------------------------------------------------
  13. using namespace std;
  14. HANDLE ConsoleInput = NULL;
  15. HANDLE ConsoleOutput = NULL;
  16. HANDLE ConsoleStandardOutput = NULL;
  17. HANDLE ConsoleErrorOutput = NULL;
  18. TConsoleCommStruct::TInitEvent::STDINOUT OutputFormat;
  19. TConsoleCommStruct::TInitEvent::STDINOUT InputFormat;
  20. HANDLE Child = NULL;
  21. HANDLE CancelEvent = NULL;
  22. HANDLE InputTimerEvent = NULL;
  23. bool SupportsUtf8ConsoleOutput = false;
  24. unsigned int OutputType = FILE_TYPE_UNKNOWN;
  25. unsigned int InputType = FILE_TYPE_UNKNOWN;
  26. enum { RESULT_GLOBAL_ERROR = 1, RESULT_INIT_ERROR = 2, RESULT_PROCESSING_ERROR = 3,
  27. RESULT_UNKNOWN_ERROR = 4 };
  28. const wchar_t* CONSOLE_CHILD_PARAM = L"consolechild";
  29. //---------------------------------------------------------------------------
  30. inline TConsoleCommStruct* GetCommStruct(HANDLE FileMapping)
  31. {
  32. TConsoleCommStruct* Result;
  33. Result = static_cast<TConsoleCommStruct*>(MapViewOfFile(FileMapping,
  34. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  35. if (Result == NULL)
  36. {
  37. throw runtime_error("Cannot open mapping object.");
  38. }
  39. return Result;
  40. }
  41. //---------------------------------------------------------------------------
  42. inline void FreeCommStruct(TConsoleCommStruct* CommStruct)
  43. {
  44. UnmapViewOfFile(CommStruct);
  45. }
  46. //---------------------------------------------------------------------------
  47. void InitializeConsole(wchar_t* InstanceName, HANDLE& RequestEvent, HANDLE& ResponseEvent,
  48. HANDLE& CancelEvent, HANDLE& FileMapping, HANDLE& Job)
  49. {
  50. unsigned int Process = GetCurrentProcessId();
  51. int Attempts = 0;
  52. wchar_t Name[MAX_PATH];
  53. bool UniqEvent;
  54. do
  55. {
  56. if (Attempts > MAX_ATTEMPTS)
  57. {
  58. throw runtime_error("Cannot find unique name for event object.");
  59. }
  60. int InstanceNumber;
  61. #ifdef CONSOLE_TEST
  62. InstanceNumber = 1;
  63. #else
  64. InstanceNumber = random(1000);
  65. #endif
  66. swprintf(InstanceName, L"_%u_%d", Process, InstanceNumber);
  67. swprintf(Name, L"%s%s", CONSOLE_EVENT_REQUEST, InstanceName);
  68. HANDLE EventHandle = OpenEvent(EVENT_ALL_ACCESS, false, Name);
  69. UniqEvent = (EventHandle == NULL);
  70. if (!UniqEvent)
  71. {
  72. CloseHandle(EventHandle);
  73. }
  74. Attempts++;
  75. }
  76. while (!UniqEvent);
  77. RequestEvent = CreateEvent(NULL, false, false, Name);
  78. if (RequestEvent == NULL)
  79. {
  80. throw runtime_error("Cannot create request event object.");
  81. }
  82. swprintf(Name, L"%s%s", CONSOLE_EVENT_RESPONSE, InstanceName);
  83. ResponseEvent = CreateEvent(NULL, false, false, Name);
  84. if (ResponseEvent == NULL)
  85. {
  86. throw runtime_error("Cannot create response event object.");
  87. }
  88. swprintf(Name, L"%s%s", CONSOLE_EVENT_CANCEL, InstanceName);
  89. CancelEvent = CreateEvent(NULL, false, false, Name);
  90. if (CancelEvent == NULL)
  91. {
  92. throw runtime_error("Cannot create cancel event object.");
  93. }
  94. swprintf(Name, L"%s%s", CONSOLE_MAPPING, InstanceName);
  95. FileMapping = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  96. 0, sizeof(TConsoleCommStruct), Name);
  97. if (FileMapping == NULL)
  98. {
  99. throw runtime_error("Cannot create mapping object.");
  100. }
  101. swprintf(Name, L"%s%s", CONSOLE_JOB, InstanceName);
  102. Job = CreateJobObject(NULL, Name);
  103. if (Job == NULL)
  104. {
  105. throw runtime_error("Cannot create job object.");
  106. }
  107. JOBOBJECT_EXTENDED_LIMIT_INFORMATION ExtendedLimitInformation;
  108. memset(&ExtendedLimitInformation, 0, sizeof(ExtendedLimitInformation));
  109. ExtendedLimitInformation.BasicLimitInformation.LimitFlags =
  110. JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
  111. if (SetInformationJobObject(Job, JobObjectExtendedLimitInformation,
  112. &ExtendedLimitInformation, sizeof(ExtendedLimitInformation)) == 0)
  113. {
  114. CloseHandle(Job);
  115. Job = NULL;
  116. }
  117. TConsoleCommStruct* CommStruct = GetCommStruct(FileMapping);
  118. CommStruct->Size = sizeof(TConsoleCommStruct);
  119. CommStruct->Version = TConsoleCommStruct::CurrentVersion;
  120. CommStruct->Event = TConsoleCommStruct::NONE;
  121. FreeCommStruct(CommStruct);
  122. }
  123. //---------------------------------------------------------------------------
  124. // duplicated in Common.cpp
  125. bool __fastcall CutToken(const wchar_t*& Str, wchar_t* Token)
  126. {
  127. bool Result;
  128. // inspired by Putty's sftp_getcmd() from PSFTP.C
  129. int Length = wcslen(Str);
  130. int Index = 0;
  131. while ((Index < Length) &&
  132. ((Str[Index] == L' ') || (Str[Index] == L'\t')))
  133. {
  134. Index++;
  135. }
  136. if (Index < Length)
  137. {
  138. bool Quoting = false;
  139. while (Index < Length)
  140. {
  141. if (!Quoting && ((Str[Index] == L' ') || (Str[Index] == L'\t')))
  142. {
  143. break;
  144. }
  145. else if ((Str[Index] == L'"') && (Index + 1 < Length) &&
  146. (Str[Index + 1] == L'"'))
  147. {
  148. Index += 2;
  149. *Token = L'"';
  150. Token++;
  151. }
  152. else if (Str[Index] == L'"')
  153. {
  154. Index++;
  155. Quoting = !Quoting;
  156. }
  157. else
  158. {
  159. *Token = Str[Index];
  160. Token++;
  161. Index++;
  162. }
  163. }
  164. if (Index < Length)
  165. {
  166. Index++;
  167. }
  168. Str += Index;
  169. Result = true;
  170. }
  171. else
  172. {
  173. Result = false;
  174. Str += Length;
  175. }
  176. *Token = L'\0';
  177. return Result;
  178. }
  179. //---------------------------------------------------------------------------
  180. char* WideStringToString(const wchar_t* Message)
  181. {
  182. char* Buffer;
  183. int Size = WideCharToMultiByte(CP_UTF8, 0, Message, -1, 0, 0, 0, 0);
  184. if (Size > 0)
  185. {
  186. Buffer = new char[(Size * 2) + 1];
  187. if (WideCharToMultiByte(CP_UTF8, 0, Message, -1, Buffer, Size, 0, 0) > 0)
  188. {
  189. Buffer[Size] = '\0';
  190. }
  191. else
  192. {
  193. delete[] Buffer;
  194. Buffer = NULL;
  195. }
  196. }
  197. return Buffer;
  198. }
  199. //---------------------------------------------------------------------------
  200. void GetProductVersion(wchar_t* ProductVersion)
  201. {
  202. wchar_t Buffer[MAX_PATH];
  203. DWORD ModuleNameLen = GetModuleFileName(NULL, Buffer, MAX_PATH);
  204. if ((ModuleNameLen == 0) || (ModuleNameLen == MAX_PATH))
  205. {
  206. throw runtime_error("Error retrieving executable name.");
  207. }
  208. ProductVersion[0] = '\0';
  209. unsigned long Handle;
  210. unsigned int Size = GetFileVersionInfoSize(Buffer, &Handle);
  211. if (Size > 0)
  212. {
  213. void * VersionInfo = new char[Size];
  214. VS_FIXEDFILEINFO* FixedFileInfo;
  215. unsigned int Length;
  216. if (GetFileVersionInfo(Buffer, Handle, Size, VersionInfo))
  217. {
  218. if (VerQueryValue(VersionInfo, L"\\", (void**)&FixedFileInfo, &Length))
  219. {
  220. int ProductMajor = HIWORD(FixedFileInfo->dwProductVersionMS);
  221. int ProductMinor = LOWORD(FixedFileInfo->dwProductVersionMS);
  222. int ProductBuild = HIWORD(FixedFileInfo->dwProductVersionLS);
  223. if ((ProductMajor >= 1) && (ProductMajor <= 99) &&
  224. (ProductMinor >= 0) && (ProductMinor <= 99) &&
  225. (ProductBuild >= 0) && (ProductBuild <= 99))
  226. {
  227. wsprintf(ProductVersion, L"%d.%d.%d", ProductMajor, ProductMinor, ProductBuild);
  228. }
  229. }
  230. }
  231. delete[] VersionInfo;
  232. }
  233. if (ProductVersion[0] == L'\0')
  234. {
  235. throw runtime_error("Error retrieving product version.");
  236. }
  237. }
  238. //---------------------------------------------------------------------------
  239. void InitializeChild(const wchar_t* CommandLine, const wchar_t* InstanceName, HANDLE& Child)
  240. {
  241. int SkipParam = 0;
  242. wchar_t ChildPath[MAX_PATH] = L"";
  243. size_t CommandLineLen = wcslen(CommandLine);
  244. wchar_t* Buffer = new wchar_t[(CommandLineLen > MAX_PATH ? CommandLineLen : MAX_PATH) + 1];
  245. int Count = 0;
  246. const wchar_t* P = CommandLine;
  247. while (CutToken(P, Buffer))
  248. {
  249. if ((wcschr(L"-/", Buffer[0]) != NULL) &&
  250. (wcsncmpi(Buffer + 1, CONSOLE_CHILD_PARAM, wcslen(CONSOLE_CHILD_PARAM)) == 0) &&
  251. (Buffer[wcslen(CONSOLE_CHILD_PARAM) + 1] == L'='))
  252. {
  253. SkipParam = Count;
  254. wcscpy(ChildPath, Buffer + 1 + wcslen(CONSOLE_CHILD_PARAM) + 1);
  255. }
  256. ++Count;
  257. }
  258. if (wcslen(ChildPath) == 0)
  259. {
  260. DWORD ModuleNameLen = GetModuleFileName(NULL, Buffer, MAX_PATH);
  261. if ((ModuleNameLen == 0) || (ModuleNameLen == MAX_PATH))
  262. {
  263. throw runtime_error("Error retrieving executable name.");
  264. }
  265. const wchar_t* LastDelimiter = wcsrchr(Buffer, L'\\');
  266. const wchar_t* AppFileName;
  267. if (LastDelimiter != NULL)
  268. {
  269. wcsncpy(ChildPath, Buffer, LastDelimiter - Buffer + 1);
  270. ChildPath[LastDelimiter - Buffer + 1] = L'\0';
  271. AppFileName = LastDelimiter + 1;
  272. }
  273. else
  274. {
  275. ChildPath[0] = L'\0';
  276. AppFileName = Buffer;
  277. }
  278. const wchar_t* ExtensionStart = wcsrchr(AppFileName, L'.');
  279. if (ExtensionStart != NULL)
  280. {
  281. wchar_t* End = ChildPath + wcslen(ChildPath);
  282. wcsncpy(End, AppFileName, ExtensionStart - AppFileName);
  283. *(End + (ExtensionStart - AppFileName)) = L'\0';
  284. }
  285. else
  286. {
  287. wcscat(ChildPath, AppFileName);
  288. }
  289. wcscat(ChildPath, L".exe");
  290. }
  291. wchar_t ProductVersion[32];
  292. GetProductVersion(ProductVersion);
  293. wchar_t* Parameters = new wchar_t[(CommandLineLen * 2) + 100 + (Count * 3) + 1];
  294. wsprintf(Parameters, L"\"%s\" /console=%s /consoleinstance=%s ", ChildPath, ProductVersion, InstanceName);
  295. P = CommandLine;
  296. // skip executable path
  297. CutToken(P, Buffer);
  298. int i = 1;
  299. while (CutToken(P, Buffer))
  300. {
  301. if (i != SkipParam)
  302. {
  303. wcscat(Parameters, L"\"");
  304. wchar_t* P2 = Parameters + wcslen(Parameters);
  305. const wchar_t* P3 = Buffer;
  306. const wchar_t* BufferEnd = Buffer + wcslen(Buffer) + 1;
  307. while (P3 != BufferEnd)
  308. {
  309. *P2 = *P3;
  310. ++P2;
  311. if (*P3 == L'"')
  312. {
  313. *P2 = L'"';
  314. ++P2;
  315. }
  316. ++P3;
  317. }
  318. wcscat(Parameters, L"\" ");
  319. }
  320. ++i;
  321. }
  322. delete[] Buffer;
  323. STARTUPINFO StartupInfo = { sizeof(STARTUPINFO) };
  324. PROCESS_INFORMATION ProcessInfomation;
  325. BOOL Result =
  326. CreateProcess(ChildPath, Parameters, NULL, NULL, false, 0, NULL, NULL,
  327. &StartupInfo, &ProcessInfomation);
  328. delete[] Parameters;
  329. if (Result)
  330. {
  331. Child = ProcessInfomation.hProcess;
  332. }
  333. else
  334. {
  335. size_t Len = MAX_PATH + 1024;
  336. DWORD Error = GetLastError();
  337. wchar_t * Buffer = NULL;
  338. Len += FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, Error, 0, (LPTSTR)&Buffer, 0, NULL);
  339. wchar_t* Message = new wchar_t[Len];
  340. wsprintf(Message, L"Cannot start WinSCP application \"%s\".", ChildPath);
  341. if (Buffer != NULL)
  342. {
  343. wcscat(Message, L"\n");
  344. wcscat(Message, Buffer);
  345. LocalFree(Buffer);
  346. }
  347. char* MessageString = WideStringToString(Message);
  348. delete[] Message;
  349. std::string ErrorString(MessageString);
  350. delete[] MessageString;
  351. throw runtime_error(ErrorString);
  352. }
  353. }
  354. //---------------------------------------------------------------------------
  355. void FinalizeChild(HANDLE Child)
  356. {
  357. if (Child != NULL)
  358. {
  359. TerminateProcess(Child, 0);
  360. CloseHandle(Child);
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void FinalizeConsole(const wchar_t* /*InstanceName*/, HANDLE RequestEvent,
  365. HANDLE ResponseEvent, HANDLE CancelEvent, HANDLE FileMapping, HANDLE Job)
  366. {
  367. CloseHandle(RequestEvent);
  368. CloseHandle(ResponseEvent);
  369. CloseHandle(CancelEvent);
  370. CloseHandle(FileMapping);
  371. if (Job != NULL)
  372. {
  373. CloseHandle(Job);
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. static wchar_t LastFromBeginning[sizeof(TConsoleCommStruct::TPrintEvent)] = L""; //???
  378. //---------------------------------------------------------------------------
  379. inline void Flush()
  380. {
  381. if ((OutputType == FILE_TYPE_DISK) || (OutputType == FILE_TYPE_PIPE))
  382. {
  383. fflush(stdout);
  384. }
  385. }
  386. //---------------------------------------------------------------------------
  387. void PrintException(const exception& e)
  388. {
  389. if (ConsoleOutput != NULL)
  390. {
  391. unsigned long Written;
  392. WriteFile(ConsoleOutput, e.what(), strlen(e.what()), &Written, NULL);
  393. }
  394. else
  395. {
  396. puts(e.what());
  397. }
  398. }
  399. //---------------------------------------------------------------------------
  400. void Print(const wchar_t* Message)
  401. {
  402. char* Buffer = WideStringToString(Message);
  403. if (Buffer != NULL)
  404. {
  405. char* Ptr = Buffer;
  406. while ((Ptr = strchr(Ptr, '\n')) != NULL)
  407. {
  408. memmove(Ptr + 1, Ptr, strlen(Ptr) + 1);
  409. *Ptr = '\r';
  410. Ptr += 2;
  411. }
  412. unsigned long Written;
  413. WriteFile(ConsoleOutput, Buffer, strlen(Buffer), &Written, NULL);
  414. delete[] Buffer;
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. void Print(bool FromBeginning, const wchar_t* Message)
  419. {
  420. size_t Len = wcslen(Message);
  421. if ((OutputType == FILE_TYPE_DISK) || (OutputType == FILE_TYPE_PIPE))
  422. {
  423. if (FromBeginning && (Message[0] != L'\n'))
  424. {
  425. wcscpy(LastFromBeginning, Message);
  426. }
  427. else
  428. {
  429. if (LastFromBeginning[0] != L'\0')
  430. {
  431. Print(LastFromBeginning);
  432. LastFromBeginning[0] = L'\0';
  433. }
  434. if (FromBeginning && (Message[0] == L'\n'))
  435. {
  436. Print(L"\n");
  437. wcscpy(LastFromBeginning, Message + 1);
  438. }
  439. else
  440. {
  441. Print(Message);
  442. }
  443. Flush();
  444. }
  445. }
  446. else
  447. {
  448. unsigned long Written;
  449. if (FromBeginning)
  450. {
  451. WriteConsole(ConsoleOutput, L"\r", 1, &Written, NULL);
  452. }
  453. bool WriteResult =
  454. WriteConsole(ConsoleOutput, Message, Len, &Written, NULL);
  455. int Error = GetLastError();
  456. // The current console font does not support some characters in the message,
  457. // fall back to ansi-writting
  458. if (!WriteResult && (Error == ERROR_GEN_FAILURE))
  459. {
  460. int Size = WideCharToMultiByte(CP_ACP, 0, Message, -1, 0, 0, 0, 0);
  461. if (Size > 0)
  462. {
  463. char* Buffer = new char[Size];
  464. if (WideCharToMultiByte(CP_ACP, 0, Message, -1, Buffer, Size, 0, 0) > 0)
  465. {
  466. WriteConsoleA(ConsoleOutput, Buffer, strlen(Buffer), &Written, NULL);
  467. }
  468. delete[] Buffer;
  469. }
  470. }
  471. }
  472. }
  473. //---------------------------------------------------------------------------
  474. inline void ProcessPrintEvent(TConsoleCommStruct::TPrintEvent& Event)
  475. {
  476. Print(Event.FromBeginning, Event.Message);
  477. }
  478. //---------------------------------------------------------------------------
  479. void CancelInput()
  480. {
  481. SetEvent(CancelEvent);
  482. }
  483. //---------------------------------------------------------------------------
  484. void BreakInput()
  485. {
  486. FlushConsoleInputBuffer(ConsoleInput);
  487. // Signal cancel first, otherwise the main thread can get Enter before the event is set
  488. CancelInput();
  489. INPUT_RECORD InputRecord;
  490. memset(&InputRecord, 0, sizeof(InputRecord));
  491. InputRecord.EventType = KEY_EVENT;
  492. InputRecord.Event.KeyEvent.bKeyDown = true;
  493. InputRecord.Event.KeyEvent.wRepeatCount = 1;
  494. InputRecord.Event.KeyEvent.uChar.UnicodeChar = L'\r';
  495. unsigned long Written;
  496. WriteConsoleInput(ConsoleInput, &InputRecord, 1, &Written);
  497. }
  498. //---------------------------------------------------------------------------
  499. DWORD WINAPI InputTimerThreadProc(void* Parameter)
  500. {
  501. unsigned int Timer = reinterpret_cast<unsigned int>(Parameter);
  502. unsigned int Remaining = Timer;
  503. const unsigned int Step = 1000;
  504. const int FirstKey = VK_LBUTTON; // 0x01
  505. const int LastKey = VK_OEM_CLEAR; // 0xFE
  506. // reset key state
  507. for (int Key = FirstKey; Key <= LastKey; Key++)
  508. {
  509. GetAsyncKeyState(Key);
  510. }
  511. while (Remaining > 0)
  512. {
  513. unsigned long WaitResult = WaitForSingleObject(InputTimerEvent, Step);
  514. if (WaitResult == WAIT_OBJECT_0)
  515. {
  516. // input entered
  517. Remaining = 0;
  518. }
  519. else if (WaitResult == WAIT_TIMEOUT)
  520. {
  521. bool Input = false;
  522. for (int Key = FirstKey; Key <= LastKey; Key++)
  523. {
  524. if ((GetAsyncKeyState(Key) & 0x01) != 0)
  525. {
  526. Input = true;
  527. // Finishing the loop nevertheless to reset state of all keys
  528. }
  529. }
  530. if (Input)
  531. {
  532. // If we have new input, reset timer
  533. Remaining = Timer;
  534. }
  535. else if (Remaining > Step)
  536. {
  537. Remaining -= Step;
  538. }
  539. else
  540. {
  541. BreakInput();
  542. Remaining = 0;
  543. }
  544. }
  545. else
  546. {
  547. // abort input on (unlikely) error
  548. BreakInput();
  549. Remaining = 0;
  550. }
  551. }
  552. return 0;
  553. }
  554. //---------------------------------------------------------------------------
  555. void ProcessInputEvent(TConsoleCommStruct::TInputEvent& Event)
  556. {
  557. if ((InputType == FILE_TYPE_DISK) || (InputType == FILE_TYPE_PIPE))
  558. {
  559. unsigned long Bytes = 0;
  560. unsigned long Read;
  561. bool Result;
  562. char Ch;
  563. char Buf[LENOF(Event.Str) * 3];
  564. while (((Result = (ReadFile(ConsoleInput, &Ch, 1, &Read, NULL) != 0)) != false) &&
  565. (Read > 0) && (Bytes < LENOF(Buf) - 1) && (Ch != '\n'))
  566. {
  567. if (Ch != '\r')
  568. {
  569. Buf[Bytes] = Ch;
  570. Bytes++;
  571. }
  572. }
  573. Buf[Bytes] = L'\0';
  574. MultiByteToWideChar(CP_UTF8, 0, Buf, -1, Event.Str, LENOF(Event.Str) - 1);
  575. Event.Str[LENOF(Event.Str) - 1] = L'\0';
  576. Print(false, Event.Str);
  577. Print(false, L"\n");
  578. Event.Result = ((Result && (Read > 0)) || (Bytes > 0));
  579. }
  580. else
  581. {
  582. unsigned long PrevMode, NewMode;
  583. GetConsoleMode(ConsoleInput, &PrevMode);
  584. NewMode = PrevMode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
  585. if (Event.Echo)
  586. {
  587. NewMode |= ENABLE_ECHO_INPUT;
  588. }
  589. else
  590. {
  591. NewMode &= ~ENABLE_ECHO_INPUT;
  592. }
  593. SetConsoleMode(ConsoleInput, NewMode);
  594. HANDLE InputTimerThread = NULL;
  595. try
  596. {
  597. if (Event.Timer > 0)
  598. {
  599. unsigned long ThreadId;
  600. InputTimerEvent = CreateEvent(NULL, false, false, NULL);
  601. InputTimerThread = CreateThread(NULL, 0, InputTimerThreadProc,
  602. reinterpret_cast<void *>(Event.Timer), 0, &ThreadId);
  603. }
  604. unsigned long Read;
  605. Event.Result = ReadConsole(ConsoleInput, Event.Str, LENOF(Event.Str) - 1, &Read, NULL);
  606. Event.Str[Read] = L'\0';
  607. bool PendingCancel = (WaitForSingleObject(CancelEvent, 0) == WAIT_OBJECT_0);
  608. if (PendingCancel || !Event.Echo)
  609. {
  610. WriteFile(ConsoleOutput, "\n", 1, NULL, NULL);
  611. Flush();
  612. }
  613. if (PendingCancel || (Read == 0))
  614. {
  615. Event.Result = false;
  616. }
  617. }
  618. __finally
  619. {
  620. if (InputTimerThread != NULL)
  621. {
  622. SetEvent(InputTimerEvent);
  623. WaitForSingleObject(InputTimerThread, 100);
  624. CloseHandle(InputTimerEvent);
  625. InputTimerEvent = NULL;
  626. CloseHandle(InputTimerThread);
  627. }
  628. SetConsoleMode(ConsoleInput, PrevMode);
  629. }
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. void ProcessChoiceEvent(TConsoleCommStruct::TChoiceEvent& Event)
  634. {
  635. // note that if output is redirected to file, input is still FILE_TYPE_CHAR
  636. if ((InputType == FILE_TYPE_DISK) || (InputType == FILE_TYPE_PIPE))
  637. {
  638. if (Event.Timeouting)
  639. {
  640. Sleep(Event.Timer);
  641. Event.Result = Event.Timeouted;
  642. }
  643. else
  644. {
  645. Event.Result = Event.Break;
  646. }
  647. }
  648. else
  649. {
  650. Event.Result = 0;
  651. unsigned long PrevMode, NewMode;
  652. GetConsoleMode(ConsoleInput, &PrevMode);
  653. NewMode = (PrevMode | ENABLE_PROCESSED_INPUT) & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
  654. SetConsoleMode(ConsoleInput, NewMode);
  655. unsigned int ATimer = Event.Timer;
  656. try
  657. {
  658. do
  659. {
  660. unsigned long Read;
  661. INPUT_RECORD Record;
  662. if ((PeekConsoleInput(ConsoleInput, &Record, 1, &Read) != 0) &&
  663. (Read == 1))
  664. {
  665. if ((ReadConsoleInput(ConsoleInput, &Record, 1, &Read) != 0) &&
  666. (Read == 1))
  667. {
  668. bool PendingCancel = (WaitForSingleObject(CancelEvent, 0) == WAIT_OBJECT_0);
  669. if (PendingCancel)
  670. {
  671. Event.Result = Event.Break;
  672. }
  673. else if ((Record.EventType == KEY_EVENT) &&
  674. Record.Event.KeyEvent.bKeyDown)
  675. {
  676. // This happens when Shift key is pressed
  677. if (Record.Event.KeyEvent.uChar.UnicodeChar != 0)
  678. {
  679. wchar_t CStr[2];
  680. CStr[0] = Record.Event.KeyEvent.uChar.UnicodeChar;
  681. CStr[1] = L'\0';
  682. CharUpperBuff(CStr, 1);
  683. wchar_t C = CStr[0];
  684. if (C == 27)
  685. {
  686. Event.Result = Event.Cancel;
  687. }
  688. else if ((wcschr(Event.Options, C) != NULL) &&
  689. ((Record.Event.KeyEvent.dwControlKeyState &
  690. (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED | LEFT_ALT_PRESSED |
  691. RIGHT_ALT_PRESSED)) == 0))
  692. {
  693. Event.Result = wcschr(Event.Options, C) - Event.Options + 1;
  694. }
  695. }
  696. }
  697. }
  698. }
  699. if (Event.Result == 0)
  700. {
  701. unsigned int TimerSlice = 50;
  702. Sleep(TimerSlice);
  703. if (Event.Timer > 0)
  704. {
  705. if (ATimer > TimerSlice)
  706. {
  707. ATimer -= TimerSlice;
  708. }
  709. else
  710. {
  711. Event.Result = Event.Timeouted;
  712. }
  713. }
  714. }
  715. }
  716. while (Event.Result == 0);
  717. SetConsoleMode(ConsoleInput, PrevMode);
  718. }
  719. catch(...)
  720. {
  721. SetConsoleMode(ConsoleInput, PrevMode);
  722. throw;
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. inline void ProcessTitleEvent(TConsoleCommStruct::TTitleEvent& Event)
  728. {
  729. SetConsoleTitle(Event.Title);
  730. }
  731. //---------------------------------------------------------------------------
  732. inline void ProcessInitEvent(TConsoleCommStruct::TInitEvent& Event)
  733. {
  734. if (Event.UseStdErr)
  735. {
  736. ConsoleOutput = ConsoleErrorOutput;
  737. }
  738. OutputFormat = Event.OutputFormat;
  739. if (OutputFormat != TConsoleCommStruct::TInitEvent::OFF)
  740. {
  741. setmode(fileno(stdout), O_BINARY);
  742. }
  743. InputFormat = Event.InputFormat;
  744. if (InputFormat != TConsoleCommStruct::TInitEvent::OFF)
  745. {
  746. setmode(fileno(stdin), O_BINARY);
  747. }
  748. OutputType = GetFileType(ConsoleOutput);
  749. // Until now we should not have printed anything.
  750. // Only in case of a fatal failure, we might have printed a pure ASCII error messages (and never got here).
  751. if ((OutputType == FILE_TYPE_DISK) || (OutputType == FILE_TYPE_PIPE) ||
  752. SupportsUtf8ConsoleOutput)
  753. {
  754. SetConsoleOutputCP(CP_UTF8);
  755. }
  756. else
  757. {
  758. SetConsoleOutputCP(CP_ACP);
  759. }
  760. Event.InputType = InputType;
  761. Event.OutputType = OutputType;
  762. // default anyway
  763. Event.WantsProgress = false;
  764. }
  765. //---------------------------------------------------------------------------
  766. inline void ProcessTransferOutEvent(TConsoleCommStruct::TTransferEvent& Event)
  767. {
  768. if (OutputFormat == TConsoleCommStruct::TInitEvent::BINARY)
  769. {
  770. fwrite(Event.Data, 1, Event.Len, stdout);
  771. }
  772. else if (OutputFormat == TConsoleCommStruct::TInitEvent::CHUNKED)
  773. {
  774. fprintf(stdout, "%x\r\n", static_cast<int>(Event.Len));
  775. fwrite(Event.Data, 1, Event.Len, stdout);
  776. fputs("\r\n", stdout);
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. inline void ProcessTransferInEvent(TConsoleCommStruct::TTransferEvent& Event)
  781. {
  782. size_t Read = fread(Event.Data, 1, Event.Len, stdin);
  783. if (Read != Event.Len)
  784. {
  785. if (ferror(stdin))
  786. {
  787. Event.Error = true;
  788. }
  789. else
  790. {
  791. Event.Len = Read;
  792. }
  793. }
  794. }
  795. //---------------------------------------------------------------------------
  796. void ProcessEvent(HANDLE ResponseEvent, HANDLE FileMapping)
  797. {
  798. TConsoleCommStruct* CommStruct = GetCommStruct(FileMapping);
  799. try
  800. {
  801. if (CommStruct->Version != TConsoleCommStruct::CurrentVersionConfirmed)
  802. {
  803. throw runtime_error("Incompatible console protocol version");
  804. }
  805. switch (CommStruct->Event)
  806. {
  807. case TConsoleCommStruct::PRINT:
  808. ProcessPrintEvent(CommStruct->PrintEvent);
  809. break;
  810. case TConsoleCommStruct::INPUT:
  811. ProcessInputEvent(CommStruct->InputEvent);
  812. break;
  813. case TConsoleCommStruct::CHOICE:
  814. ProcessChoiceEvent(CommStruct->ChoiceEvent);
  815. break;
  816. case TConsoleCommStruct::TITLE:
  817. ProcessTitleEvent(CommStruct->TitleEvent);
  818. break;
  819. case TConsoleCommStruct::INIT:
  820. ProcessInitEvent(CommStruct->InitEvent);
  821. break;
  822. case TConsoleCommStruct::TRANSFEROUT:
  823. ProcessTransferOutEvent(CommStruct->TransferEvent);
  824. break;
  825. case TConsoleCommStruct::TRANSFERIN:
  826. ProcessTransferInEvent(CommStruct->TransferEvent);
  827. break;
  828. default:
  829. throw runtime_error("Unknown event");
  830. }
  831. FreeCommStruct(CommStruct);
  832. SetEvent(ResponseEvent);
  833. }
  834. catch(...)
  835. {
  836. FreeCommStruct(CommStruct);
  837. throw;
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. BOOL WINAPI HandlerRoutine(DWORD CtrlType)
  842. {
  843. if ((CtrlType == CTRL_C_EVENT) || (CtrlType == CTRL_BREAK_EVENT))
  844. {
  845. CancelInput();
  846. return true;
  847. }
  848. else
  849. {
  850. FinalizeChild(Child);
  851. return false;
  852. }
  853. }
  854. //---------------------------------------------------------------------------
  855. #pragma argsused
  856. int wmain(int /*argc*/, wchar_t* /*argv*/[])
  857. {
  858. unsigned long Result = RESULT_UNKNOWN_ERROR;
  859. try
  860. {
  861. randomize();
  862. OSVERSIONINFO VersionInfo;
  863. VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
  864. GetVersionEx(&VersionInfo);
  865. ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
  866. InputType = GetFileType(ConsoleInput);
  867. SetConsoleCtrlHandler(HandlerRoutine, true);
  868. unsigned int SavedConsoleCP = GetConsoleCP();
  869. unsigned int SavedConsoleOutputCP = GetConsoleOutputCP();
  870. ConsoleStandardOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  871. ConsoleOutput = ConsoleStandardOutput;
  872. ConsoleErrorOutput = GetStdHandle(STD_ERROR_HANDLE);
  873. SupportsUtf8ConsoleOutput =
  874. ((VersionInfo.dwMajorVersion == 6) && (VersionInfo.dwMinorVersion >= 1)) ||
  875. (VersionInfo.dwMajorVersion > 6);
  876. if ((InputType == FILE_TYPE_DISK) || (InputType == FILE_TYPE_PIPE) ||
  877. SupportsUtf8ConsoleOutput)
  878. {
  879. SetConsoleCP(CP_UTF8);
  880. }
  881. else
  882. {
  883. SetConsoleCP(CP_ACP);
  884. }
  885. wchar_t InstanceName[MAX_PATH];
  886. HANDLE RequestEvent, ResponseEvent, FileMapping, Job;
  887. InitializeConsole(InstanceName, RequestEvent, ResponseEvent,
  888. CancelEvent, FileMapping, Job);
  889. wchar_t SavedTitle[512];
  890. GetConsoleTitle(SavedTitle, LENOF(SavedTitle));
  891. try
  892. {
  893. #ifndef CONSOLE_TEST
  894. InitializeChild(GetCommandLine(), InstanceName, Child);
  895. #endif
  896. try
  897. {
  898. bool Continue = true;
  899. do
  900. {
  901. HANDLE Handles[2];
  902. Handles[0] = RequestEvent;
  903. Handles[1] = Child;
  904. unsigned int HandleCount;
  905. #ifndef CONSOLE_TEST
  906. HandleCount = 2;
  907. #else
  908. HandleCount = 1;
  909. #endif
  910. unsigned long WaitResult =
  911. WaitForMultipleObjects(HandleCount, Handles, false, INFINITE);
  912. switch (WaitResult)
  913. {
  914. case WAIT_OBJECT_0:
  915. ProcessEvent(ResponseEvent, FileMapping);
  916. break;
  917. case WAIT_OBJECT_0 + 1:
  918. GetExitCodeProcess(Child, &Result);
  919. CloseHandle(Child);
  920. Child = NULL;
  921. Continue = false;
  922. break;
  923. default:
  924. throw runtime_error("Error waiting for communication from child process.");
  925. }
  926. }
  927. while (Continue);
  928. // flush pending progress message
  929. Print(false, L"");
  930. }
  931. catch(const exception& e)
  932. {
  933. PrintException(e);
  934. Result = RESULT_PROCESSING_ERROR;
  935. }
  936. #ifndef CONSOLE_TEST
  937. FinalizeChild(Child);
  938. #endif
  939. SetConsoleTitle(SavedTitle);
  940. SetConsoleCP(SavedConsoleCP);
  941. SetConsoleOutputCP(SavedConsoleOutputCP);
  942. }
  943. catch(const exception& e)
  944. {
  945. PrintException(e);
  946. Result = RESULT_INIT_ERROR;
  947. }
  948. FinalizeConsole(InstanceName, RequestEvent, ResponseEvent,
  949. CancelEvent, FileMapping, Job);
  950. }
  951. catch(const exception& e)
  952. {
  953. PrintException(e);
  954. Result = RESULT_GLOBAL_ERROR;
  955. }
  956. return Result;
  957. }
  958. //---------------------------------------------------------------------------