Sync with trunk r63793.
[reactos.git] / dll / win32 / kernel32 / client / console / alias.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/alias.c
5 * PURPOSE: Win32 Console Client Alias support functions
6 * PROGRAMMERS: David Welch (welch@cwcom.net) (welch@mcmail.com)
7 * Christoph von Wittich (christoph_vw@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 */
10
11 /* INCLUDES *******************************************************************/
12
13 #include <k32.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18
19 /* FUNCTIONS ******************************************************************/
20
21 static BOOL
22 IntAddConsoleAlias(LPCVOID Source,
23 DWORD SourceBufferLength,
24 LPCVOID Target,
25 DWORD TargetBufferLength,
26 LPCVOID lpExeName,
27 BOOLEAN bUnicode)
28 {
29 CONSOLE_API_MESSAGE ApiMessage;
30 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest;
31 PCSR_CAPTURE_BUFFER CaptureBuffer;
32 ULONG CapturedStrings;
33
34 DWORD dwNumChars = (lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
35
36 if (lpExeName == NULL || dwNumChars == 0)
37 {
38 SetLastError(ERROR_INVALID_PARAMETER);
39 return FALSE;
40 }
41
42 ConsoleAliasRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
43
44 /* Determine the needed sizes */
45 ConsoleAliasRequest->SourceLength = SourceBufferLength;
46 ConsoleAliasRequest->ExeLength = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
47 ConsoleAliasRequest->Unicode =
48 ConsoleAliasRequest->Unicode2 = bUnicode;
49
50 CapturedStrings = 2;
51
52 if (Target) /* The target can be optional */
53 {
54 ConsoleAliasRequest->TargetLength = TargetBufferLength;
55 CapturedStrings++;
56 }
57 else
58 {
59 ConsoleAliasRequest->TargetLength = 0;
60 }
61
62 /* Allocate a Capture Buffer */
63 CaptureBuffer = CsrAllocateCaptureBuffer(CapturedStrings,
64 ConsoleAliasRequest->SourceLength +
65 ConsoleAliasRequest->ExeLength +
66 ConsoleAliasRequest->TargetLength);
67 if (CaptureBuffer == NULL)
68 {
69 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
70 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
71 return FALSE;
72 }
73
74 /* Capture the strings */
75 CsrCaptureMessageBuffer(CaptureBuffer,
76 (PVOID)Source,
77 ConsoleAliasRequest->SourceLength,
78 (PVOID*)&ConsoleAliasRequest->Source);
79
80 CsrCaptureMessageBuffer(CaptureBuffer,
81 (PVOID)lpExeName,
82 ConsoleAliasRequest->ExeLength,
83 (PVOID*)&ConsoleAliasRequest->ExeName);
84
85 if (Target) /* The target can be optional */
86 {
87 CsrCaptureMessageBuffer(CaptureBuffer,
88 (PVOID)Target,
89 ConsoleAliasRequest->TargetLength,
90 (PVOID*)&ConsoleAliasRequest->Target);
91 }
92 else
93 {
94 ConsoleAliasRequest->Target = NULL;
95 }
96
97 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
98 CaptureBuffer,
99 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAddAlias),
100 sizeof(*ConsoleAliasRequest));
101
102 CsrFreeCaptureBuffer(CaptureBuffer);
103
104 if (!NT_SUCCESS(ApiMessage.Status))
105 {
106 BaseSetLastNTError(ApiMessage.Status);
107 return FALSE;
108 }
109
110 return TRUE;
111 }
112
113
114 /*
115 * @implemented
116 */
117 BOOL
118 WINAPI
119 AddConsoleAliasW(LPCWSTR lpSource,
120 LPCWSTR lpTarget,
121 LPCWSTR lpExeName)
122 {
123 DWORD SourceBufferLength, TargetBufferLength;
124 SourceBufferLength = wcslen(lpSource) * sizeof(WCHAR);
125 TargetBufferLength = (lpTarget ? wcslen(lpTarget) * sizeof(WCHAR) : 0);
126
127 DPRINT1("AddConsoleAliasW entered with lpSource '%S' lpTarget '%S' lpExeName '%S'\n",
128 lpSource, lpTarget, lpExeName);
129
130 return IntAddConsoleAlias(lpSource,
131 SourceBufferLength,
132 lpTarget,
133 TargetBufferLength,
134 lpExeName,
135 TRUE);
136 }
137
138
139 /*
140 * @implemented
141 */
142 BOOL
143 WINAPI
144 AddConsoleAliasA(LPCSTR lpSource,
145 LPCSTR lpTarget,
146 LPCSTR lpExeName)
147 {
148 DWORD SourceBufferLength, TargetBufferLength;
149 SourceBufferLength = strlen(lpSource) * sizeof(CHAR);
150 TargetBufferLength = (lpTarget ? strlen(lpTarget) * sizeof(CHAR) : 0);
151
152 DPRINT1("AddConsoleAliasA entered with lpSource '%s' lpTarget '%s' lpExeName '%s'\n",
153 lpSource, lpTarget, lpExeName);
154
155 return IntAddConsoleAlias(lpSource,
156 SourceBufferLength,
157 lpTarget,
158 TargetBufferLength,
159 lpExeName,
160 FALSE);
161 }
162
163
164 static DWORD
165 IntGetConsoleAlias(LPVOID Source,
166 DWORD SourceBufferLength,
167 LPVOID Target,
168 DWORD TargetBufferLength,
169 LPVOID lpExeName,
170 BOOLEAN bUnicode)
171 {
172 CONSOLE_API_MESSAGE ApiMessage;
173 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest;
174 PCSR_CAPTURE_BUFFER CaptureBuffer;
175
176 DWORD dwNumChars = (lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
177
178 if (Source == NULL || Target == NULL)
179 {
180 SetLastError(ERROR_INVALID_PARAMETER);
181 return 0;
182 }
183
184 if (lpExeName == NULL || dwNumChars == 0)
185 {
186 SetLastError(ERROR_INVALID_PARAMETER);
187 return 0;
188 }
189
190 ConsoleAliasRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
191
192 /* Determine the needed sizes */
193 ConsoleAliasRequest->SourceLength = SourceBufferLength;
194 ConsoleAliasRequest->ExeLength = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
195 ConsoleAliasRequest->Unicode =
196 ConsoleAliasRequest->Unicode2 = bUnicode;
197
198 ConsoleAliasRequest->TargetLength = TargetBufferLength;
199
200 /* Allocate a Capture Buffer */
201 CaptureBuffer = CsrAllocateCaptureBuffer(3, ConsoleAliasRequest->SourceLength +
202 ConsoleAliasRequest->ExeLength +
203 ConsoleAliasRequest->TargetLength);
204 if (!CaptureBuffer)
205 {
206 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
207 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
208 return 0;
209 }
210
211 /* Capture the strings */
212 CsrCaptureMessageBuffer(CaptureBuffer,
213 (PVOID)Source,
214 ConsoleAliasRequest->SourceLength,
215 (PVOID*)&ConsoleAliasRequest->Source);
216
217 CsrCaptureMessageBuffer(CaptureBuffer,
218 (PVOID)lpExeName,
219 ConsoleAliasRequest->ExeLength,
220 (PVOID*)&ConsoleAliasRequest->ExeName);
221
222 /* Allocate space for the target buffer */
223 CsrAllocateMessagePointer(CaptureBuffer,
224 ConsoleAliasRequest->TargetLength,
225 (PVOID*)&ConsoleAliasRequest->Target);
226
227 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
228 CaptureBuffer,
229 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAlias),
230 sizeof(*ConsoleAliasRequest));
231 if (!NT_SUCCESS(ApiMessage.Status))
232 {
233 CsrFreeCaptureBuffer(CaptureBuffer);
234 BaseSetLastNTError(ApiMessage.Status);
235
236 if (ApiMessage.Status == STATUS_BUFFER_TOO_SMALL)
237 return ConsoleAliasRequest->TargetLength;
238 else
239 return 0;
240 }
241
242 /* Copy the returned target string into the user buffer */
243 memcpy(Target,
244 ConsoleAliasRequest->Target,
245 ConsoleAliasRequest->TargetLength);
246
247 /* Release the capture buffer and exit */
248 CsrFreeCaptureBuffer(CaptureBuffer);
249
250 return ConsoleAliasRequest->TargetLength;
251 }
252
253
254 /*
255 * @implemented
256 */
257 DWORD
258 WINAPI
259 GetConsoleAliasW(LPWSTR lpSource,
260 LPWSTR lpTargetBuffer,
261 DWORD TargetBufferLength,
262 LPWSTR lpExeName)
263 {
264 DPRINT1("GetConsoleAliasW entered with lpSource '%S' lpExeName '%S'\n",
265 lpSource, lpExeName);
266
267 return IntGetConsoleAlias(lpSource,
268 wcslen(lpSource) * sizeof(WCHAR),
269 lpTargetBuffer,
270 TargetBufferLength,
271 lpExeName,
272 TRUE);
273 }
274
275
276 /*
277 * @implemented
278 */
279 DWORD
280 WINAPI
281 GetConsoleAliasA(LPSTR lpSource,
282 LPSTR lpTargetBuffer,
283 DWORD TargetBufferLength,
284 LPSTR lpExeName)
285 {
286 DPRINT1("GetConsoleAliasA entered with lpSource '%s' lpExeName '%s'\n",
287 lpSource, lpExeName);
288
289 return IntGetConsoleAlias(lpSource,
290 strlen(lpSource) * sizeof(CHAR),
291 lpTargetBuffer,
292 TargetBufferLength,
293 lpExeName,
294 FALSE);
295 }
296
297
298 static DWORD
299 IntGetConsoleAliases(LPVOID AliasBuffer,
300 DWORD AliasBufferLength,
301 LPVOID lpExeName,
302 BOOLEAN bUnicode)
303 {
304 CONSOLE_API_MESSAGE ApiMessage;
305 PCONSOLE_GETALLALIASES GetAllAliasesRequest = &ApiMessage.Data.GetAllAliasesRequest;
306 PCSR_CAPTURE_BUFFER CaptureBuffer;
307
308 DWORD dwNumChars = (lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
309
310 if (lpExeName == NULL || dwNumChars == 0)
311 {
312 SetLastError(ERROR_INVALID_PARAMETER);
313 return 0;
314 }
315
316 GetAllAliasesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
317
318 /* Determine the needed sizes */
319 GetAllAliasesRequest->ExeLength = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
320 GetAllAliasesRequest->Unicode =
321 GetAllAliasesRequest->Unicode2 = bUnicode;
322
323 GetAllAliasesRequest->AliasesBufferLength = AliasBufferLength;
324
325 /* Allocate a Capture Buffer */
326 CaptureBuffer = CsrAllocateCaptureBuffer(2, GetAllAliasesRequest->ExeLength +
327 GetAllAliasesRequest->AliasesBufferLength);
328 if (!CaptureBuffer)
329 {
330 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
331 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
332 return 0;
333 }
334
335 /* Capture the exe name and allocate space for the aliases buffer */
336 CsrCaptureMessageBuffer(CaptureBuffer,
337 (PVOID)lpExeName,
338 GetAllAliasesRequest->ExeLength,
339 (PVOID*)&GetAllAliasesRequest->ExeName);
340
341 CsrAllocateMessagePointer(CaptureBuffer,
342 GetAllAliasesRequest->AliasesBufferLength,
343 (PVOID*)&GetAllAliasesRequest->AliasesBuffer);
344
345 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
346 CaptureBuffer,
347 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliases),
348 sizeof(*GetAllAliasesRequest));
349 if (!NT_SUCCESS(ApiMessage.Status))
350 {
351 CsrFreeCaptureBuffer(CaptureBuffer);
352 BaseSetLastNTError(ApiMessage.Status);
353 return 0;
354 }
355
356 /* Copy the returned aliases string into the user buffer */
357 memcpy(AliasBuffer,
358 GetAllAliasesRequest->AliasesBuffer,
359 GetAllAliasesRequest->AliasesBufferLength);
360
361 /* Release the capture buffer and exit */
362 CsrFreeCaptureBuffer(CaptureBuffer);
363
364 return GetAllAliasesRequest->AliasesBufferLength;
365 }
366
367
368 /*
369 * @implemented
370 */
371 DWORD
372 WINAPI
373 GetConsoleAliasesW(LPWSTR AliasBuffer,
374 DWORD AliasBufferLength,
375 LPWSTR ExeName)
376 {
377 DPRINT1("GetConsoleAliasesW entered with lpExeName '%S'\n",
378 ExeName);
379
380 return IntGetConsoleAliases(AliasBuffer,
381 AliasBufferLength,
382 ExeName,
383 TRUE);
384 }
385
386
387 /*
388 * @implemented
389 */
390 DWORD
391 WINAPI
392 GetConsoleAliasesA(LPSTR AliasBuffer,
393 DWORD AliasBufferLength,
394 LPSTR ExeName)
395 {
396 DPRINT1("GetConsoleAliasesA entered with lpExeName '%s'\n",
397 ExeName);
398
399 return IntGetConsoleAliases(AliasBuffer,
400 AliasBufferLength,
401 ExeName,
402 FALSE);
403 }
404
405
406 static DWORD
407 IntGetConsoleAliasesLength(LPVOID lpExeName, BOOLEAN bUnicode)
408 {
409 CONSOLE_API_MESSAGE ApiMessage;
410 PCONSOLE_GETALLALIASESLENGTH GetAllAliasesLengthRequest = &ApiMessage.Data.GetAllAliasesLengthRequest;
411 PCSR_CAPTURE_BUFFER CaptureBuffer;
412
413 DWORD dwNumChars = (lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
414
415 if (lpExeName == NULL || dwNumChars == 0)
416 {
417 SetLastError(ERROR_INVALID_PARAMETER);
418 return 0;
419 }
420
421 GetAllAliasesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
422 GetAllAliasesLengthRequest->ExeLength = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
423 GetAllAliasesLengthRequest->Unicode =
424 GetAllAliasesLengthRequest->Unicode2 = bUnicode;
425
426 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetAllAliasesLengthRequest->ExeLength);
427 if (!CaptureBuffer)
428 {
429 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
430 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
431 return 0;
432 }
433
434 CsrCaptureMessageBuffer(CaptureBuffer,
435 (PVOID)lpExeName,
436 GetAllAliasesLengthRequest->ExeLength,
437 (PVOID)&GetAllAliasesLengthRequest->ExeName);
438
439 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
440 CaptureBuffer,
441 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasesLength),
442 sizeof(*GetAllAliasesLengthRequest));
443
444 CsrFreeCaptureBuffer(CaptureBuffer);
445
446 if (!NT_SUCCESS(ApiMessage.Status))
447 {
448 BaseSetLastNTError(ApiMessage.Status);
449 return 0;
450 }
451
452 return GetAllAliasesLengthRequest->Length;
453 }
454
455
456 /*
457 * @implemented
458 */
459 DWORD
460 WINAPI
461 GetConsoleAliasesLengthW(LPWSTR lpExeName)
462 {
463 return IntGetConsoleAliasesLength(lpExeName, TRUE);
464 }
465
466
467 /*
468 * @implemented
469 */
470 DWORD
471 WINAPI
472 GetConsoleAliasesLengthA(LPSTR lpExeName)
473 {
474 return IntGetConsoleAliasesLength(lpExeName, FALSE);
475 }
476
477
478 static DWORD
479 IntGetConsoleAliasExes(PVOID lpExeNameBuffer,
480 DWORD ExeNameBufferLength,
481 BOOLEAN bUnicode)
482 {
483 CONSOLE_API_MESSAGE ApiMessage;
484 PCONSOLE_GETALIASESEXES GetAliasesExesRequest = &ApiMessage.Data.GetAliasesExesRequest;
485 PCSR_CAPTURE_BUFFER CaptureBuffer;
486
487 GetAliasesExesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
488 GetAliasesExesRequest->Length = ExeNameBufferLength;
489 GetAliasesExesRequest->Unicode = bUnicode;
490
491 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExeNameBufferLength);
492 if (!CaptureBuffer)
493 {
494 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
495 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
496 return 0;
497 }
498
499 CsrAllocateMessagePointer(CaptureBuffer,
500 ExeNameBufferLength,
501 (PVOID*)&GetAliasesExesRequest->ExeNames);
502
503 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
504 CaptureBuffer,
505 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExes),
506 sizeof(*GetAliasesExesRequest));
507 if (!NT_SUCCESS(ApiMessage.Status))
508 {
509 CsrFreeCaptureBuffer(CaptureBuffer);
510 BaseSetLastNTError(ApiMessage.Status);
511 return 0;
512 }
513
514 memcpy(lpExeNameBuffer,
515 GetAliasesExesRequest->ExeNames,
516 GetAliasesExesRequest->Length);
517
518 CsrFreeCaptureBuffer(CaptureBuffer);
519
520 return GetAliasesExesRequest->Length;
521 }
522
523 /*
524 * @implemented
525 */
526 DWORD
527 WINAPI
528 GetConsoleAliasExesW(LPWSTR lpExeNameBuffer,
529 DWORD ExeNameBufferLength)
530 {
531 DPRINT1("GetConsoleAliasExesW called\n");
532 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, TRUE);
533 }
534
535
536 /*
537 * @implemented
538 */
539 DWORD
540 WINAPI
541 GetConsoleAliasExesA(LPSTR lpExeNameBuffer,
542 DWORD ExeNameBufferLength)
543 {
544 DPRINT1("GetConsoleAliasExesA called\n");
545 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, FALSE);
546 }
547
548
549 static DWORD
550 IntGetConsoleAliasExesLength(BOOLEAN bUnicode)
551 {
552 CONSOLE_API_MESSAGE ApiMessage;
553 PCONSOLE_GETALIASESEXESLENGTH GetAliasesExesLengthRequest = &ApiMessage.Data.GetAliasesExesLengthRequest;
554
555 GetAliasesExesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
556 GetAliasesExesLengthRequest->Unicode = bUnicode;
557
558 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
559 NULL,
560 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExesLength),
561 sizeof(*GetAliasesExesLengthRequest));
562 if (!NT_SUCCESS(ApiMessage.Status))
563 {
564 BaseSetLastNTError(ApiMessage.Status);
565 return 0;
566 }
567
568 return GetAliasesExesLengthRequest->Length;
569 }
570
571
572 /*
573 * @implemented
574 */
575 DWORD
576 WINAPI
577 GetConsoleAliasExesLengthW(VOID)
578 {
579 DPRINT1("GetConsoleAliasExesLengthW called\n");
580 return IntGetConsoleAliasExesLength(TRUE);
581 }
582
583
584 /*
585 * @implemented
586 */
587 DWORD
588 WINAPI
589 GetConsoleAliasExesLengthA(VOID)
590 {
591 DPRINT1("GetConsoleAliasExesLengthA called\n");
592 return IntGetConsoleAliasExesLength(FALSE);
593 }
594
595 /* EOF */