Main.cpp 28 KB

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