Main.cpp 26 KB

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