Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1369 lines
36 KiB

  1. /*++
  2. Copyright (c) 1985 - 1999, Microsoft Corporation
  3. Module Name:
  4. cmdline.c
  5. Abstract:
  6. This file implements command line editing and aliasing.
  7. Author:
  8. Therese Stowell (thereses) 22-Mar-1991
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #pragma hdrstop
  14. #define CONSOLE_NAME_PATH_SEPARATOR ((WCHAR)L'\\')
  15. #define CONSOLE_NAME_PATH_TERMINATOR ((WCHAR)L'\0')
  16. #if !defined(BUILD_WOW64)
  17. #define FNAME_LENGTH 256
  18. BOOLEAN ExeNameInitialized;
  19. RTL_CRITICAL_SECTION ExeNameCriticalSection;
  20. WCHAR ExeNameBuffer[FNAME_LENGTH];
  21. USHORT ExeNameLength; // in chars, not bytes
  22. WCHAR StartDirBuffer[MAX_PATH + 1];
  23. USHORT StartDirLength; // in chars, not bytes
  24. VOID
  25. InitExeName(
  26. VOID)
  27. {
  28. PPEB Peb;
  29. PLDR_DATA_TABLE_ENTRY ImageEntry;
  30. if (ExeNameInitialized) {
  31. return;
  32. }
  33. RtlInitializeCriticalSection(&ExeNameCriticalSection);
  34. ExeNameInitialized = TRUE;
  35. Peb = NtCurrentPeb();
  36. ImageEntry = (PLDR_DATA_TABLE_ENTRY)Peb->Ldr->InLoadOrderModuleList.Flink;
  37. ImageEntry = CONTAINING_RECORD(ImageEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
  38. ExeNameLength = ImageEntry->BaseDllName.Length/sizeof(WCHAR);
  39. RtlCopyMemory(ExeNameBuffer,
  40. ImageEntry->BaseDllName.Buffer,
  41. ImageEntry->BaseDllName.Length
  42. );
  43. ExeNameBuffer[ExeNameLength] = CONSOLE_NAME_PATH_TERMINATOR;
  44. StartDirLength = Peb->ProcessParameters->CurrentDirectory.DosPath.Length/sizeof(WCHAR);
  45. RtlCopyMemory(StartDirBuffer,
  46. Peb->ProcessParameters->CurrentDirectory.DosPath.Buffer,
  47. Peb->ProcessParameters->CurrentDirectory.DosPath.Length);
  48. StartDirBuffer[StartDirLength] = CONSOLE_NAME_PATH_TERMINATOR;
  49. }
  50. USHORT
  51. GetCurrentExeName(
  52. OUT LPWSTR Buffer,
  53. IN ULONG BufferLength)
  54. {
  55. if (!ExeNameInitialized) {
  56. Buffer[0] = UNICODE_NULL;
  57. return 0;
  58. }
  59. RtlEnterCriticalSection(&ExeNameCriticalSection);
  60. if (BufferLength > ExeNameLength * sizeof(WCHAR)) {
  61. BufferLength = ExeNameLength * sizeof(WCHAR);
  62. }
  63. RtlCopyMemory(Buffer, ExeNameBuffer, BufferLength);
  64. RtlLeaveCriticalSection(&ExeNameCriticalSection);
  65. return (USHORT)BufferLength;
  66. }
  67. #endif //!defined(BUILD_WOW64)
  68. #if !defined(BUILD_WOW6432)
  69. //
  70. // Returns bytes, not chars.
  71. //
  72. USHORT
  73. GetExeName(
  74. IN OUT LPVOID ExeName,
  75. IN BOOLEAN UnicodeApi,
  76. OUT PBOOLEAN UnicodeExe)
  77. {
  78. if (ExeName == NULL) {
  79. return 0;
  80. }
  81. if (UnicodeApi) {
  82. *UnicodeExe = TRUE;
  83. return (USHORT)wcslen((LPWSTR)ExeName) * sizeof(WCHAR);
  84. } else {
  85. *UnicodeExe = FALSE;
  86. return (USHORT)strlen((LPSTR)ExeName);
  87. }
  88. }
  89. BOOL
  90. APIENTRY
  91. AddConsoleAliasInternal(
  92. IN LPVOID Source,
  93. IN UINT SourceLength,
  94. IN LPVOID Target,
  95. IN UINT TargetLength,
  96. IN LPVOID ExeName,
  97. IN BOOLEAN Unicode
  98. )
  99. /*++
  100. Parameters:
  101. Source - String to substitute alias string for, in input stream.
  102. Target - String to substitute for source. NULL to delete an existing
  103. alias.
  104. ExeName - Exe to set alias in.
  105. Return Value:
  106. TRUE - The operation was successful.
  107. FALSE/NULL - The operation failed. Extended error status is available
  108. using GetLastError.
  109. --*/
  110. {
  111. PCSR_CAPTURE_HEADER CaptureBuffer;
  112. CONSOLE_API_MSG m;
  113. ULONG CapturedMsgPointers;
  114. PCONSOLE_ADDALIAS_MSG a = &m.u.AddConsoleAliasW;
  115. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  116. a->SourceLength = (USHORT)SourceLength;
  117. a->Unicode = Unicode;
  118. CapturedMsgPointers = 2;
  119. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  120. if (a->ExeLength == 0) {
  121. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  122. return FALSE;
  123. }
  124. if (Target != NULL) {
  125. a->TargetLength = (USHORT)TargetLength;
  126. CapturedMsgPointers += 1;
  127. } else {
  128. a->Target = NULL;
  129. a->TargetLength = 0;
  130. }
  131. CaptureBuffer = CsrAllocateCaptureBuffer( CapturedMsgPointers,
  132. a->SourceLength + a->TargetLength + a->ExeLength
  133. );
  134. if (CaptureBuffer == NULL) {
  135. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  136. return FALSE;
  137. }
  138. CsrCaptureMessageBuffer( CaptureBuffer,
  139. (PCHAR) Source,
  140. a->SourceLength,
  141. (PVOID *) &a->Source
  142. );
  143. CsrCaptureMessageBuffer( CaptureBuffer,
  144. (PCHAR) ExeName,
  145. a->ExeLength,
  146. (PVOID *) &a->Exe
  147. );
  148. if (Target != NULL) {
  149. CsrCaptureMessageBuffer( CaptureBuffer,
  150. (PCHAR) Target,
  151. a->TargetLength,
  152. (PVOID *) &a->Target
  153. );
  154. }
  155. CsrClientCallServer( (PCSR_API_MSG)&m,
  156. CaptureBuffer,
  157. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  158. ConsolepAddAlias
  159. ),
  160. sizeof( *a )
  161. );
  162. CsrFreeCaptureBuffer( CaptureBuffer );
  163. if (!NT_SUCCESS( m.ReturnValue )) {
  164. SET_LAST_NT_ERROR(m.ReturnValue);
  165. return FALSE;
  166. }
  167. return TRUE;
  168. }
  169. #endif //!defined(BUILD_WOW6432)
  170. #if !defined(BUILD_WOW64)
  171. BOOL
  172. APIENTRY
  173. AddConsoleAliasW(
  174. IN LPWSTR Source,
  175. IN LPWSTR Target,
  176. IN LPWSTR ExeName)
  177. {
  178. USHORT TargetLength;
  179. if (Target != NULL) {
  180. TargetLength = (USHORT) (lstrlenW(Target) * sizeof(WCHAR));
  181. } else {
  182. TargetLength = 0;
  183. }
  184. return AddConsoleAliasInternal(Source,
  185. lstrlenW(Source) * sizeof(WCHAR),
  186. Target,
  187. TargetLength,
  188. ExeName,
  189. TRUE);
  190. }
  191. BOOL
  192. APIENTRY
  193. AddConsoleAliasA(
  194. IN LPTSTR Source,
  195. IN LPTSTR Target,
  196. IN LPTSTR ExeName)
  197. {
  198. USHORT TargetLength;
  199. if (Target != NULL) {
  200. TargetLength = (USHORT)lstrlenA(Target);
  201. } else {
  202. TargetLength = 0;
  203. }
  204. return AddConsoleAliasInternal(Source,
  205. lstrlenA(Source),
  206. Target,
  207. TargetLength,
  208. ExeName,
  209. FALSE);
  210. }
  211. #endif //!defined(BUILD_WOW64)
  212. #if !defined(BUILD_WOW6432)
  213. DWORD
  214. APIENTRY
  215. GetConsoleAliasInternal(
  216. IN LPVOID Source,
  217. IN UINT SourceLength,
  218. OUT LPVOID TargetBuffer,
  219. IN UINT TargetBufferLength,
  220. IN LPVOID ExeName,
  221. IN BOOLEAN Unicode
  222. )
  223. /*++
  224. Parameters:
  225. Source - Name of alias to query. NULL means query all alias names.
  226. Target - Where to store value of alias. If Source is NULL, then
  227. value is one or more null terminated strings terminated by an extra
  228. null byte. Each null terminated string is the name of an alias.
  229. Return Value:
  230. Non-zero - The operation was successful, and the return value is the
  231. number of characters stored in the Target buffer.
  232. Zero - The operation failed. Extended error status is available
  233. using GetLastError.
  234. --*/
  235. {
  236. PCSR_CAPTURE_HEADER CaptureBuffer;
  237. CONSOLE_API_MSG m;
  238. ULONG CapturedMsgPointers;
  239. PCONSOLE_GETALIAS_MSG a = &m.u.GetConsoleAliasW;
  240. if (Source == NULL) {
  241. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  242. return FALSE;
  243. }
  244. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  245. a->Unicode = Unicode;
  246. CapturedMsgPointers = 3;
  247. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  248. if (a->ExeLength == 0) {
  249. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  250. return FALSE;
  251. }
  252. a->SourceLength = (USHORT) SourceLength;
  253. a->TargetLength = (USHORT) TargetBufferLength;
  254. CaptureBuffer = CsrAllocateCaptureBuffer( CapturedMsgPointers,
  255. a->SourceLength + a->TargetLength + a->ExeLength
  256. );
  257. if (CaptureBuffer == NULL) {
  258. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  259. return FALSE;
  260. }
  261. CsrCaptureMessageBuffer( CaptureBuffer,
  262. (PCHAR) ExeName,
  263. a->ExeLength,
  264. (PVOID *) &a->Exe
  265. );
  266. CsrCaptureMessageBuffer( CaptureBuffer,
  267. (PCHAR) Source,
  268. a->SourceLength,
  269. (PVOID *) &a->Source
  270. );
  271. CsrCaptureMessageBuffer( CaptureBuffer,
  272. (PCHAR) NULL,
  273. a->TargetLength,
  274. (PVOID *) &a->Target
  275. );
  276. CsrClientCallServer( (PCSR_API_MSG)&m,
  277. CaptureBuffer,
  278. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  279. ConsolepGetAlias
  280. ),
  281. sizeof( *a )
  282. );
  283. if (NT_SUCCESS( m.ReturnValue )) {
  284. RtlCopyMemory( TargetBuffer, a->Target, a->TargetLength );
  285. }
  286. else {
  287. SET_LAST_NT_ERROR(m.ReturnValue);
  288. if (m.ReturnValue != STATUS_BUFFER_TOO_SMALL) {
  289. a->TargetLength = 0;
  290. }
  291. }
  292. CsrFreeCaptureBuffer( CaptureBuffer );
  293. return a->TargetLength;
  294. }
  295. #endif !defined(BUILD_WOW6432)
  296. #if !defined(BUILD_WOW64)
  297. DWORD
  298. APIENTRY
  299. GetConsoleAliasW(
  300. IN LPWSTR Source,
  301. OUT LPWSTR TargetBuffer,
  302. IN DWORD TargetBufferLength,
  303. IN LPWSTR ExeName
  304. )
  305. {
  306. return GetConsoleAliasInternal(Source,
  307. lstrlenW(Source)*sizeof(WCHAR),
  308. TargetBuffer,
  309. TargetBufferLength,
  310. ExeName,
  311. TRUE
  312. );
  313. }
  314. DWORD
  315. APIENTRY
  316. GetConsoleAliasA(
  317. IN LPTSTR Source,
  318. OUT LPTSTR TargetBuffer,
  319. IN DWORD TargetBufferLength,
  320. IN LPTSTR ExeName
  321. )
  322. {
  323. return GetConsoleAliasInternal(Source,
  324. lstrlenA(Source),
  325. TargetBuffer,
  326. TargetBufferLength,
  327. ExeName,
  328. FALSE
  329. );
  330. }
  331. #endif //!defined(BUILD_WOW64)
  332. #if !defined(BUILD_WOW6432)
  333. DWORD
  334. APIENTRY
  335. GetConsoleAliasesLengthInternal(
  336. IN LPVOID ExeName,
  337. IN BOOLEAN Unicode
  338. )
  339. {
  340. CONSOLE_API_MSG m;
  341. PCONSOLE_GETALIASESLENGTH_MSG a = &m.u.GetConsoleAliasesLengthW;
  342. PCSR_CAPTURE_HEADER CaptureBuffer;
  343. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  344. a->Unicode = Unicode;
  345. a->ExeLength = GetExeName(ExeName,Unicode,&a->UnicodeExe);
  346. if (a->ExeLength == 0) {
  347. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  348. return 0;
  349. }
  350. CaptureBuffer = CsrAllocateCaptureBuffer(1, a->ExeLength);
  351. if (CaptureBuffer == NULL) {
  352. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  353. return FALSE;
  354. }
  355. CsrCaptureMessageBuffer( CaptureBuffer,
  356. (PCHAR) ExeName,
  357. a->ExeLength,
  358. (PVOID *) &a->Exe
  359. );
  360. CsrClientCallServer( (PCSR_API_MSG)&m,
  361. CaptureBuffer,
  362. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  363. ConsolepGetAliasesLength
  364. ),
  365. sizeof( *a )
  366. );
  367. CsrFreeCaptureBuffer( CaptureBuffer );
  368. if (NT_SUCCESS( m.ReturnValue )) {
  369. return a->AliasesLength;
  370. } else {
  371. SET_LAST_NT_ERROR(m.ReturnValue);
  372. return 0;
  373. }
  374. }
  375. #endif //!defined(BUILD_WOW6432)
  376. #if !defined(BUILD_WOW64)
  377. DWORD
  378. APIENTRY
  379. GetConsoleAliasesLengthW(
  380. IN LPWSTR ExeName
  381. )
  382. {
  383. return GetConsoleAliasesLengthInternal(ExeName,
  384. TRUE
  385. );
  386. }
  387. DWORD
  388. APIENTRY
  389. GetConsoleAliasesLengthA(
  390. IN LPTSTR ExeName
  391. )
  392. {
  393. return GetConsoleAliasesLengthInternal(ExeName,
  394. FALSE
  395. );
  396. }
  397. #endif //!defined(BUILD_WOW64)
  398. #if !defined(BUILD_WOW6432)
  399. DWORD
  400. APIENTRY
  401. GetConsoleAliasExesLengthInternal(
  402. IN BOOLEAN Unicode
  403. )
  404. {
  405. CONSOLE_API_MSG m;
  406. PCONSOLE_GETALIASEXESLENGTH_MSG a = &m.u.GetConsoleAliasExesLengthW;
  407. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  408. a->Unicode = Unicode;
  409. CsrClientCallServer( (PCSR_API_MSG)&m,
  410. NULL,
  411. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  412. ConsolepGetAliasExesLength
  413. ),
  414. sizeof( *a )
  415. );
  416. if (NT_SUCCESS( m.ReturnValue )) {
  417. return a->AliasExesLength;
  418. } else {
  419. SET_LAST_NT_ERROR(m.ReturnValue);
  420. return 0;
  421. }
  422. }
  423. #endif //!defined(BUILD_WOW6432)
  424. #if !defined(BUILD_WOW64)
  425. DWORD
  426. APIENTRY
  427. GetConsoleAliasExesLengthW( VOID )
  428. {
  429. return GetConsoleAliasExesLengthInternal( TRUE );
  430. }
  431. DWORD
  432. APIENTRY
  433. GetConsoleAliasExesLengthA( VOID )
  434. {
  435. return GetConsoleAliasExesLengthInternal( FALSE );
  436. }
  437. #endif //!defined(BUILD_WOW64)
  438. #if !defined(BUILD_WOW6432)
  439. DWORD
  440. APIENTRY
  441. GetConsoleAliasesInternal(
  442. OUT LPVOID AliasBuffer,
  443. IN DWORD AliasBufferLength,
  444. IN LPVOID ExeName,
  445. IN BOOLEAN Unicode
  446. )
  447. {
  448. CONSOLE_API_MSG m;
  449. PCONSOLE_GETALIASES_MSG a = &m.u.GetConsoleAliasesW;
  450. PCSR_CAPTURE_HEADER CaptureBuffer;
  451. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  452. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  453. if (a->ExeLength == 0) {
  454. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  455. return 0;
  456. }
  457. a->Unicode = Unicode;
  458. a->AliasesBufferLength = AliasBufferLength;
  459. CaptureBuffer = CsrAllocateCaptureBuffer(2, a->ExeLength + AliasBufferLength);
  460. if (CaptureBuffer == NULL) {
  461. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  462. return 0;
  463. }
  464. CsrCaptureMessageBuffer( CaptureBuffer,
  465. (PCHAR) ExeName,
  466. a->ExeLength,
  467. (PVOID *) &a->Exe
  468. );
  469. CsrCaptureMessageBuffer( CaptureBuffer,
  470. (PCHAR) NULL,
  471. a->AliasesBufferLength,
  472. (PVOID *) &a->AliasesBuffer
  473. );
  474. CsrClientCallServer( (PCSR_API_MSG)&m,
  475. CaptureBuffer,
  476. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  477. ConsolepGetAliases
  478. ),
  479. sizeof( *a )
  480. );
  481. if (NT_SUCCESS( m.ReturnValue )) {
  482. try {
  483. RtlCopyMemory( AliasBuffer, a->AliasesBuffer, a->AliasesBufferLength );
  484. } except( EXCEPTION_EXECUTE_HANDLER ) {
  485. CsrFreeCaptureBuffer( CaptureBuffer );
  486. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  487. return 0;
  488. }
  489. CsrFreeCaptureBuffer( CaptureBuffer );
  490. return a->AliasesBufferLength;
  491. } else {
  492. CsrFreeCaptureBuffer( CaptureBuffer );
  493. SET_LAST_NT_ERROR(m.ReturnValue);
  494. return 0;
  495. }
  496. }
  497. #endif //!defined(BUILD_WOW6432)
  498. #if !defined(BUILD_WOW64)
  499. DWORD
  500. GetConsoleAliasesW(
  501. OUT LPWSTR AliasBuffer,
  502. IN DWORD AliasBufferLength,
  503. IN LPWSTR ExeName
  504. )
  505. {
  506. return GetConsoleAliasesInternal(AliasBuffer,
  507. AliasBufferLength,
  508. ExeName,
  509. TRUE
  510. );
  511. }
  512. DWORD
  513. GetConsoleAliasesA(
  514. OUT LPTSTR AliasBuffer,
  515. IN DWORD AliasBufferLength,
  516. IN LPTSTR ExeName
  517. )
  518. {
  519. return GetConsoleAliasesInternal(AliasBuffer,
  520. AliasBufferLength,
  521. ExeName,
  522. FALSE
  523. );
  524. }
  525. #endif //!defined(BUILD_WOW64)
  526. #if !defined(BUILD_WOW6432)
  527. DWORD
  528. APIENTRY
  529. GetConsoleAliasExesInternal(
  530. OUT LPVOID ExeNameBuffer,
  531. IN DWORD ExeNameBufferLength,
  532. IN BOOLEAN Unicode
  533. )
  534. {
  535. CONSOLE_API_MSG m;
  536. PCONSOLE_GETALIASEXES_MSG a = &m.u.GetConsoleAliasExesW;
  537. PCSR_CAPTURE_HEADER CaptureBuffer;
  538. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  539. a->AliasExesBufferLength = ExeNameBufferLength;
  540. a->Unicode = Unicode;
  541. CaptureBuffer = CsrAllocateCaptureBuffer( 1,
  542. ExeNameBufferLength
  543. );
  544. if (CaptureBuffer == NULL) {
  545. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  546. return FALSE;
  547. }
  548. CsrCaptureMessageBuffer( CaptureBuffer,
  549. (PCHAR) NULL,
  550. a->AliasExesBufferLength,
  551. (PVOID *) &a->AliasExesBuffer
  552. );
  553. CsrClientCallServer( (PCSR_API_MSG)&m,
  554. CaptureBuffer,
  555. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  556. ConsolepGetAliasExes
  557. ),
  558. sizeof( *a )
  559. );
  560. if (NT_SUCCESS( m.ReturnValue )) {
  561. try {
  562. RtlCopyMemory( ExeNameBuffer, a->AliasExesBuffer, a->AliasExesBufferLength );
  563. } except( EXCEPTION_EXECUTE_HANDLER ) {
  564. CsrFreeCaptureBuffer( CaptureBuffer );
  565. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  566. return 0;
  567. }
  568. CsrFreeCaptureBuffer( CaptureBuffer );
  569. return a->AliasExesBufferLength;
  570. } else {
  571. CsrFreeCaptureBuffer( CaptureBuffer );
  572. SET_LAST_NT_ERROR(m.ReturnValue);
  573. return 0;
  574. }
  575. }
  576. #endif //!defined(BUILD_WOW6432)
  577. #if !defined(BUILD_WOW64)
  578. DWORD
  579. GetConsoleAliasExesW(
  580. OUT LPWSTR ExeNameBuffer,
  581. IN DWORD ExeNameBufferLength
  582. )
  583. {
  584. return GetConsoleAliasExesInternal(ExeNameBuffer,
  585. ExeNameBufferLength,
  586. TRUE
  587. );
  588. }
  589. DWORD
  590. GetConsoleAliasExesA(
  591. OUT LPTSTR ExeNameBuffer,
  592. IN DWORD ExeNameBufferLength
  593. )
  594. {
  595. return GetConsoleAliasExesInternal(ExeNameBuffer,
  596. ExeNameBufferLength,
  597. FALSE
  598. );
  599. }
  600. #endif //!defined(BUILD_WOW64)
  601. #if !defined(BUILD_WOW6432)
  602. VOID
  603. APIENTRY
  604. ExpungeConsoleCommandHistoryInternal(
  605. IN LPVOID ExeName,
  606. IN BOOLEAN Unicode
  607. )
  608. {
  609. CONSOLE_API_MSG m;
  610. PCONSOLE_EXPUNGECOMMANDHISTORY_MSG a = &m.u.ExpungeConsoleCommandHistoryW;
  611. PCSR_CAPTURE_HEADER CaptureBuffer;
  612. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  613. a->Unicode = Unicode;
  614. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  615. if (a->ExeLength == 0) {
  616. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  617. return;
  618. }
  619. CaptureBuffer = CsrAllocateCaptureBuffer(1, a->ExeLength);
  620. if (CaptureBuffer == NULL) {
  621. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  622. return;
  623. }
  624. CsrCaptureMessageBuffer( CaptureBuffer,
  625. (PCHAR) ExeName,
  626. a->ExeLength,
  627. (PVOID *) &a->Exe
  628. );
  629. CsrClientCallServer( (PCSR_API_MSG)&m,
  630. CaptureBuffer,
  631. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  632. ConsolepExpungeCommandHistory
  633. ),
  634. sizeof( *a )
  635. );
  636. CsrFreeCaptureBuffer( CaptureBuffer );
  637. }
  638. #endif //!defined(BUILD_WOW6432)
  639. #if !defined(BUILD_WOW64)
  640. VOID
  641. ExpungeConsoleCommandHistoryW(
  642. IN LPWSTR ExeName
  643. )
  644. {
  645. ExpungeConsoleCommandHistoryInternal(ExeName,TRUE);
  646. }
  647. VOID
  648. ExpungeConsoleCommandHistoryA(
  649. IN LPTSTR ExeName
  650. )
  651. {
  652. ExpungeConsoleCommandHistoryInternal(ExeName,FALSE);
  653. }
  654. #endif //!defined(BUILD_WOW64)
  655. #if !defined(BUILD_WOW6432)
  656. BOOL
  657. APIENTRY
  658. SetConsoleNumberOfCommandsInternal(
  659. IN DWORD Number,
  660. IN LPVOID ExeName,
  661. IN BOOLEAN Unicode
  662. )
  663. {
  664. CONSOLE_API_MSG m;
  665. PCONSOLE_SETNUMBEROFCOMMANDS_MSG a = &m.u.SetConsoleNumberOfCommandsW;
  666. PCSR_CAPTURE_HEADER CaptureBuffer;
  667. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  668. a->NumCommands = Number;
  669. a->Unicode = Unicode;
  670. a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe);
  671. if (a->ExeLength == 0) {
  672. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  673. return 0;
  674. }
  675. CaptureBuffer = CsrAllocateCaptureBuffer(1, a->ExeLength);
  676. if (CaptureBuffer == NULL) {
  677. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  678. return FALSE;
  679. }
  680. CsrCaptureMessageBuffer( CaptureBuffer,
  681. (PCHAR) ExeName,
  682. a->ExeLength,
  683. (PVOID *) &a->Exe
  684. );
  685. CsrClientCallServer( (PCSR_API_MSG)&m,
  686. CaptureBuffer,
  687. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  688. ConsolepSetNumberOfCommands
  689. ),
  690. sizeof( *a )
  691. );
  692. CsrFreeCaptureBuffer( CaptureBuffer );
  693. if (NT_SUCCESS( m.ReturnValue )) {
  694. return TRUE;
  695. } else {
  696. SET_LAST_NT_ERROR(m.ReturnValue);
  697. return FALSE;
  698. }
  699. }
  700. #endif //!defined(BUILD_WOW6432)
  701. #if !defined(BUILD_WOW64)
  702. BOOL
  703. SetConsoleNumberOfCommandsW(
  704. IN DWORD Number,
  705. IN LPWSTR ExeName
  706. )
  707. {
  708. return SetConsoleNumberOfCommandsInternal(Number,
  709. ExeName,
  710. TRUE
  711. );
  712. }
  713. BOOL
  714. SetConsoleNumberOfCommandsA(
  715. IN DWORD Number,
  716. IN LPTSTR ExeName
  717. )
  718. {
  719. return SetConsoleNumberOfCommandsInternal(Number,
  720. ExeName,
  721. FALSE
  722. );
  723. }
  724. #endif //!defined(BUILD_WOW64)
  725. #if !defined(BUILD_WOW6432)
  726. DWORD
  727. APIENTRY
  728. GetConsoleCommandHistoryLengthInternal(
  729. IN LPVOID ExeName,
  730. IN BOOLEAN Unicode
  731. )
  732. {
  733. CONSOLE_API_MSG m;
  734. PCONSOLE_GETCOMMANDHISTORYLENGTH_MSG a = &m.u.GetConsoleCommandHistoryLengthW;
  735. PCSR_CAPTURE_HEADER CaptureBuffer;
  736. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  737. a->Unicode = Unicode;
  738. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  739. if (a->ExeLength == 0) {
  740. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  741. return 0;
  742. }
  743. CaptureBuffer = CsrAllocateCaptureBuffer(1, a->ExeLength);
  744. if (CaptureBuffer == NULL) {
  745. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  746. return 0;
  747. }
  748. CsrCaptureMessageBuffer( CaptureBuffer,
  749. (PCHAR) ExeName,
  750. a->ExeLength,
  751. (PVOID *) &a->Exe
  752. );
  753. CsrClientCallServer( (PCSR_API_MSG)&m,
  754. CaptureBuffer,
  755. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  756. ConsolepGetCommandHistoryLength
  757. ),
  758. sizeof( *a )
  759. );
  760. CsrFreeCaptureBuffer( CaptureBuffer );
  761. return a->CommandHistoryLength;
  762. }
  763. #endif //!defined(BUILD_WOW6432)
  764. #if !defined(BUILD_WOW64)
  765. DWORD
  766. GetConsoleCommandHistoryLengthW(
  767. IN LPWSTR ExeName
  768. )
  769. {
  770. return GetConsoleCommandHistoryLengthInternal(ExeName,
  771. TRUE
  772. );
  773. }
  774. DWORD
  775. GetConsoleCommandHistoryLengthA(
  776. IN LPTSTR ExeName
  777. )
  778. {
  779. return GetConsoleCommandHistoryLengthInternal(ExeName,
  780. FALSE
  781. );
  782. }
  783. #endif //!defined(BUILD_WOW64)
  784. #if !defined(BUILD_WOW6432)
  785. DWORD
  786. APIENTRY
  787. GetConsoleCommandHistoryInternal(
  788. OUT LPVOID Commands,
  789. IN DWORD CommandBufferLength,
  790. IN LPVOID ExeName,
  791. IN BOOLEAN Unicode
  792. )
  793. {
  794. CONSOLE_API_MSG m;
  795. PCONSOLE_GETCOMMANDHISTORY_MSG a = &m.u.GetConsoleCommandHistoryW;
  796. PCSR_CAPTURE_HEADER CaptureBuffer;
  797. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  798. a->CommandBufferLength = CommandBufferLength;
  799. a->ExeLength = GetExeName(ExeName, Unicode, &a->UnicodeExe);
  800. a->Unicode = Unicode;
  801. if (a->ExeLength == 0) {
  802. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  803. return 0;
  804. }
  805. CaptureBuffer = CsrAllocateCaptureBuffer(2, CommandBufferLength + a->ExeLength);
  806. if (CaptureBuffer == NULL) {
  807. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  808. return 0;
  809. }
  810. CsrCaptureMessageBuffer( CaptureBuffer,
  811. (PCHAR) ExeName,
  812. a->ExeLength,
  813. (PVOID *) &a->Exe
  814. );
  815. CsrCaptureMessageBuffer( CaptureBuffer,
  816. (PCHAR) NULL,
  817. a->CommandBufferLength,
  818. (PVOID *) &a->CommandBuffer
  819. );
  820. CsrClientCallServer( (PCSR_API_MSG)&m,
  821. CaptureBuffer,
  822. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  823. ConsolepGetCommandHistory
  824. ),
  825. sizeof( *a )
  826. );
  827. if (NT_SUCCESS( m.ReturnValue )) {
  828. try {
  829. RtlCopyMemory( Commands, a->CommandBuffer, a->CommandBufferLength );
  830. } except( EXCEPTION_EXECUTE_HANDLER ) {
  831. CsrFreeCaptureBuffer( CaptureBuffer );
  832. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  833. return 0;
  834. }
  835. CsrFreeCaptureBuffer( CaptureBuffer );
  836. return a->CommandBufferLength;
  837. } else {
  838. CsrFreeCaptureBuffer( CaptureBuffer );
  839. SET_LAST_NT_ERROR(m.ReturnValue);
  840. return 0;
  841. }
  842. }
  843. #endif //!defined(BUILD_WOW6432)
  844. #if !defined(BUILD_WOW64)
  845. DWORD
  846. GetConsoleCommandHistoryW(
  847. OUT LPWSTR Commands,
  848. IN DWORD CommandBufferLength,
  849. IN LPWSTR ExeName
  850. )
  851. {
  852. return GetConsoleCommandHistoryInternal(Commands,
  853. CommandBufferLength,
  854. ExeName,
  855. TRUE
  856. );
  857. }
  858. DWORD
  859. GetConsoleCommandHistoryA(
  860. OUT LPTSTR Commands,
  861. IN DWORD CommandBufferLength,
  862. IN LPTSTR ExeName
  863. )
  864. {
  865. return GetConsoleCommandHistoryInternal(Commands,
  866. CommandBufferLength,
  867. ExeName,
  868. FALSE
  869. );
  870. }
  871. #endif //!defined(BUILD_WOW64)
  872. #if !defined(BUILD_WOW6432)
  873. BOOL
  874. APIENTRY
  875. SetConsoleCommandHistoryMode(
  876. IN DWORD Flags
  877. )
  878. {
  879. CONSOLE_API_MSG m;
  880. PCONSOLE_SETCOMMANDHISTORYMODE_MSG a = &m.u.SetConsoleCommandHistoryMode;
  881. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  882. a->Flags = Flags;
  883. CsrClientCallServer( (PCSR_API_MSG)&m,
  884. NULL,
  885. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  886. ConsolepSetCommandHistoryMode
  887. ),
  888. sizeof( *a )
  889. );
  890. if (NT_SUCCESS( m.ReturnValue )) {
  891. return TRUE;
  892. } else {
  893. SET_LAST_NT_ERROR(m.ReturnValue);
  894. return FALSE;
  895. }
  896. }
  897. DWORD
  898. APIENTRY
  899. GetConsoleTitleInternal(
  900. OUT PVOID lpConsoleTitle,
  901. IN DWORD nSize,
  902. IN BOOLEAN Unicode
  903. )
  904. {
  905. CONSOLE_API_MSG m;
  906. PCONSOLE_GETTITLE_MSG a = &m.u.GetConsoleTitle;
  907. PCSR_CAPTURE_HEADER CaptureBuffer;
  908. if (nSize == 0) {
  909. return 0;
  910. }
  911. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  912. a->TitleLength = nSize;
  913. a->Unicode = Unicode;
  914. CaptureBuffer = CsrAllocateCaptureBuffer( 1,
  915. a->TitleLength
  916. );
  917. if (CaptureBuffer == NULL) {
  918. return 0;
  919. }
  920. CsrCaptureMessageBuffer( CaptureBuffer,
  921. NULL,
  922. a->TitleLength,
  923. (PVOID *) &a->Title
  924. );
  925. CsrClientCallServer( (PCSR_API_MSG)&m,
  926. CaptureBuffer,
  927. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  928. ConsolepGetTitle
  929. ),
  930. sizeof( *a )
  931. );
  932. if (NT_SUCCESS( m.ReturnValue )) {
  933. try {
  934. RtlCopyMemory( lpConsoleTitle, a->Title, a->TitleLength);
  935. } except( EXCEPTION_EXECUTE_HANDLER ) {
  936. CsrFreeCaptureBuffer( CaptureBuffer );
  937. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  938. return 0;
  939. }
  940. }
  941. else {
  942. CsrFreeCaptureBuffer( CaptureBuffer );
  943. SET_LAST_NT_ERROR (m.ReturnValue);
  944. return 0;
  945. }
  946. CsrFreeCaptureBuffer( CaptureBuffer );
  947. return a->TitleLength;
  948. }
  949. #endif //!defined(BUILD_WOW6432)
  950. #if !defined(BUILD_WOW64)
  951. DWORD
  952. APIENTRY
  953. GetConsoleTitleA(
  954. LPSTR lpConsoleTitle,
  955. DWORD nSize
  956. )
  957. {
  958. DWORD NumBytes;
  959. NumBytes = GetConsoleTitleInternal(lpConsoleTitle,
  960. nSize-sizeof(CHAR), // allow room for null
  961. FALSE
  962. );
  963. if (NumBytes) {
  964. lpConsoleTitle[NumBytes] = '\0'; // NULL terminate
  965. }
  966. return NumBytes;
  967. }
  968. DWORD
  969. APIENTRY
  970. GetConsoleTitleW(
  971. LPWSTR lpConsoleTitle,
  972. DWORD nSize
  973. )
  974. {
  975. DWORD NumBytes;
  976. NumBytes = GetConsoleTitleInternal(lpConsoleTitle,
  977. (nSize-1)*sizeof(WCHAR), // allow room for null
  978. TRUE
  979. );
  980. if (NumBytes) {
  981. NumBytes /= sizeof(WCHAR);
  982. lpConsoleTitle[NumBytes] = CONSOLE_NAME_PATH_TERMINATOR; // NULL terminate
  983. }
  984. return NumBytes;
  985. }
  986. #endif //!defined(BUILD_WOW64)
  987. #if !defined(BUILD_WOW6432)
  988. BOOL
  989. APIENTRY
  990. SetConsoleTitleInternal(
  991. IN CONST VOID *lpConsoleTitle,
  992. IN DWORD TitleLength, // in bytes
  993. IN BOOLEAN Unicode
  994. )
  995. {
  996. CONSOLE_API_MSG m;
  997. PCONSOLE_SETTITLE_MSG a = &m.u.SetConsoleTitle;
  998. PCSR_CAPTURE_HEADER CaptureBuffer;
  999. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  1000. a->TitleLength = TitleLength;
  1001. a->Unicode = Unicode;
  1002. CaptureBuffer = CsrAllocateCaptureBuffer( 1,
  1003. a->TitleLength
  1004. );
  1005. if (CaptureBuffer == NULL) {
  1006. return FALSE;
  1007. }
  1008. CsrCaptureMessageBuffer( CaptureBuffer,
  1009. (PCHAR) lpConsoleTitle,
  1010. a->TitleLength,
  1011. (PVOID *) &a->Title
  1012. );
  1013. CsrClientCallServer( (PCSR_API_MSG)&m,
  1014. CaptureBuffer,
  1015. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  1016. ConsolepSetTitle
  1017. ),
  1018. sizeof( *a )
  1019. );
  1020. CsrFreeCaptureBuffer( CaptureBuffer );
  1021. if (NT_SUCCESS( m.ReturnValue )) {
  1022. return TRUE;
  1023. }
  1024. else {
  1025. SET_LAST_NT_ERROR (m.ReturnValue);
  1026. return FALSE;
  1027. }
  1028. }
  1029. #endif //!defined(BUILD_WOW6432)
  1030. #if !defined(BUILD_WOW64)
  1031. BOOL
  1032. APIENTRY
  1033. SetConsoleTitleA(
  1034. LPCSTR lpConsoleTitle
  1035. )
  1036. {
  1037. DWORD TitleLength;
  1038. try {
  1039. TitleLength = strlen(lpConsoleTitle);
  1040. } except( EXCEPTION_EXECUTE_HANDLER ) {
  1041. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  1042. return FALSE;
  1043. }
  1044. return SetConsoleTitleInternal(lpConsoleTitle,
  1045. TitleLength,
  1046. FALSE);
  1047. }
  1048. BOOL
  1049. APIENTRY
  1050. SetConsoleTitleW(
  1051. LPCWSTR lpConsoleTitle
  1052. )
  1053. {
  1054. DWORD TitleLength;
  1055. try {
  1056. TitleLength = wcslen(lpConsoleTitle)*sizeof(WCHAR);
  1057. } except( EXCEPTION_EXECUTE_HANDLER ) {
  1058. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  1059. return FALSE;
  1060. }
  1061. return SetConsoleTitleInternal(lpConsoleTitle,
  1062. TitleLength,
  1063. TRUE);
  1064. }
  1065. DWORD
  1066. GetConsoleInputExeNameA(
  1067. IN DWORD nBufferLength,
  1068. OUT LPSTR lpBuffer
  1069. )
  1070. {
  1071. PUNICODE_STRING Unicode;
  1072. ANSI_STRING AnsiString;
  1073. NTSTATUS Status;
  1074. UNICODE_STRING UnicodeString;
  1075. WCHAR TempExeNameBuffer[FNAME_LENGTH];
  1076. DWORD n;
  1077. n = GetConsoleInputExeNameW(FNAME_LENGTH, TempExeNameBuffer);
  1078. if (n != 0 && n < FNAME_LENGTH) {
  1079. RtlInitUnicodeString(&UnicodeString, TempExeNameBuffer);
  1080. AnsiString.Buffer = lpBuffer;
  1081. AnsiString.Length = 0;
  1082. AnsiString.MaximumLength = (USHORT)nBufferLength;
  1083. Status = RtlUnicodeStringToAnsiString(&AnsiString, &UnicodeString, FALSE);
  1084. if (!NT_SUCCESS(Status)) {
  1085. if (Status == STATUS_BUFFER_OVERFLOW) {
  1086. SET_LAST_ERROR(ERROR_BUFFER_OVERFLOW);
  1087. return n+1;
  1088. } else {
  1089. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  1090. }
  1091. }
  1092. }
  1093. return n;
  1094. }
  1095. DWORD
  1096. GetConsoleInputExeNameW(
  1097. IN DWORD nBufferLength,
  1098. OUT LPWSTR lpBuffer
  1099. )
  1100. {
  1101. DWORD n;
  1102. n = ExeNameLength;
  1103. if (n >= nBufferLength) {
  1104. SET_LAST_ERROR(ERROR_BUFFER_OVERFLOW);
  1105. return n+1;
  1106. }
  1107. try {
  1108. RtlEnterCriticalSection(&ExeNameCriticalSection);
  1109. RtlCopyMemory(lpBuffer, ExeNameBuffer, n*sizeof(WCHAR));
  1110. lpBuffer[n] = UNICODE_NULL;
  1111. } finally {
  1112. RtlLeaveCriticalSection(&ExeNameCriticalSection);
  1113. }
  1114. return TRUE;
  1115. }
  1116. BOOL
  1117. SetConsoleInputExeNameA(
  1118. IN LPSTR lpExeName
  1119. )
  1120. {
  1121. PUNICODE_STRING Unicode;
  1122. ANSI_STRING AnsiString;
  1123. NTSTATUS Status;
  1124. Unicode = &NtCurrentTeb()->StaticUnicodeString;
  1125. RtlInitAnsiString(&AnsiString,lpExeName);
  1126. Status = RtlAnsiStringToUnicodeString(Unicode,&AnsiString,FALSE);
  1127. if (!NT_SUCCESS(Status)) {
  1128. if (Status == STATUS_BUFFER_OVERFLOW) {
  1129. SET_LAST_ERROR(ERROR_FILENAME_EXCED_RANGE);
  1130. } else {
  1131. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  1132. }
  1133. return FALSE;
  1134. }
  1135. return SetConsoleInputExeNameW(Unicode->Buffer);
  1136. }
  1137. BOOL
  1138. SetConsoleInputExeNameW(
  1139. IN LPWSTR lpExeName
  1140. )
  1141. {
  1142. DWORD n;
  1143. n = lstrlenW(lpExeName);
  1144. if (n == 0 || n >= FNAME_LENGTH) {
  1145. SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
  1146. return FALSE;
  1147. }
  1148. RtlEnterCriticalSection(&ExeNameCriticalSection);
  1149. try {
  1150. RtlCopyMemory(ExeNameBuffer,lpExeName,n*sizeof(WCHAR));
  1151. ExeNameLength = (USHORT)n;
  1152. } finally {
  1153. RtlLeaveCriticalSection(&ExeNameCriticalSection);
  1154. }
  1155. return TRUE;
  1156. }
  1157. #endif //!defined(BUILD_WOW64)