a949c75cb6b673cfe24f2141db921e8d1ef4078e
[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 USHORT SourceBufferLength,
24 LPCVOID Target,
25 USHORT 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 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
35
36 if (lpExeName == NULL || NumChars == 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 = NumChars * (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 USHORT SourceBufferLength = (USHORT)wcslen(lpSource) * sizeof(WCHAR);
124 USHORT TargetBufferLength = (USHORT)(lpTarget ? wcslen(lpTarget) * sizeof(WCHAR) : 0);
125
126 DPRINT1("AddConsoleAliasW entered with lpSource '%S' lpTarget '%S' lpExeName '%S'\n",
127 lpSource, lpTarget, lpExeName);
128
129 return IntAddConsoleAlias(lpSource,
130 SourceBufferLength,
131 lpTarget,
132 TargetBufferLength,
133 lpExeName,
134 TRUE);
135 }
136
137
138 /*
139 * @implemented
140 */
141 BOOL
142 WINAPI
143 AddConsoleAliasA(LPCSTR lpSource,
144 LPCSTR lpTarget,
145 LPCSTR lpExeName)
146 {
147 USHORT SourceBufferLength = (USHORT)strlen(lpSource) * sizeof(CHAR);
148 USHORT TargetBufferLength = (USHORT)(lpTarget ? strlen(lpTarget) * sizeof(CHAR) : 0);
149
150 DPRINT1("AddConsoleAliasA entered with lpSource '%s' lpTarget '%s' lpExeName '%s'\n",
151 lpSource, lpTarget, lpExeName);
152
153 return IntAddConsoleAlias(lpSource,
154 SourceBufferLength,
155 lpTarget,
156 TargetBufferLength,
157 lpExeName,
158 FALSE);
159 }
160
161
162 static DWORD
163 IntGetConsoleAlias(LPVOID Source,
164 USHORT SourceBufferLength,
165 LPVOID Target,
166 USHORT TargetBufferLength,
167 LPVOID lpExeName,
168 BOOLEAN bUnicode)
169 {
170 CONSOLE_API_MESSAGE ApiMessage;
171 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest;
172 PCSR_CAPTURE_BUFFER CaptureBuffer;
173
174 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
175
176 if (Source == NULL || Target == NULL)
177 {
178 SetLastError(ERROR_INVALID_PARAMETER);
179 return 0;
180 }
181
182 if (lpExeName == NULL || NumChars == 0)
183 {
184 SetLastError(ERROR_INVALID_PARAMETER);
185 return 0;
186 }
187
188 ConsoleAliasRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
189
190 /* Determine the needed sizes */
191 ConsoleAliasRequest->SourceLength = SourceBufferLength;
192 ConsoleAliasRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
193 ConsoleAliasRequest->Unicode =
194 ConsoleAliasRequest->Unicode2 = bUnicode;
195
196 ConsoleAliasRequest->TargetLength = TargetBufferLength;
197
198 /* Allocate a Capture Buffer */
199 CaptureBuffer = CsrAllocateCaptureBuffer(3, ConsoleAliasRequest->SourceLength +
200 ConsoleAliasRequest->ExeLength +
201 ConsoleAliasRequest->TargetLength);
202 if (!CaptureBuffer)
203 {
204 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
205 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
206 return 0;
207 }
208
209 /* Capture the strings */
210 CsrCaptureMessageBuffer(CaptureBuffer,
211 (PVOID)Source,
212 ConsoleAliasRequest->SourceLength,
213 (PVOID*)&ConsoleAliasRequest->Source);
214
215 CsrCaptureMessageBuffer(CaptureBuffer,
216 (PVOID)lpExeName,
217 ConsoleAliasRequest->ExeLength,
218 (PVOID*)&ConsoleAliasRequest->ExeName);
219
220 /* Allocate space for the target buffer */
221 CsrAllocateMessagePointer(CaptureBuffer,
222 ConsoleAliasRequest->TargetLength,
223 (PVOID*)&ConsoleAliasRequest->Target);
224
225 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
226 CaptureBuffer,
227 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAlias),
228 sizeof(*ConsoleAliasRequest));
229 if (!NT_SUCCESS(ApiMessage.Status))
230 {
231 CsrFreeCaptureBuffer(CaptureBuffer);
232 BaseSetLastNTError(ApiMessage.Status);
233
234 if (ApiMessage.Status == STATUS_BUFFER_TOO_SMALL)
235 return ConsoleAliasRequest->TargetLength;
236 else
237 return 0;
238 }
239
240 /* Copy the returned target string into the user buffer */
241 RtlCopyMemory(Target,
242 ConsoleAliasRequest->Target,
243 ConsoleAliasRequest->TargetLength);
244
245 /* Release the capture buffer and exit */
246 CsrFreeCaptureBuffer(CaptureBuffer);
247
248 return ConsoleAliasRequest->TargetLength;
249 }
250
251
252 /*
253 * @implemented
254 */
255 DWORD
256 WINAPI
257 GetConsoleAliasW(LPWSTR lpSource,
258 LPWSTR lpTargetBuffer,
259 DWORD TargetBufferLength,
260 LPWSTR lpExeName)
261 {
262 DPRINT1("GetConsoleAliasW entered with lpSource '%S' lpExeName '%S'\n",
263 lpSource, lpExeName);
264
265 return IntGetConsoleAlias(lpSource,
266 (USHORT)wcslen(lpSource) * sizeof(WCHAR),
267 lpTargetBuffer,
268 TargetBufferLength,
269 lpExeName,
270 TRUE);
271 }
272
273
274 /*
275 * @implemented
276 */
277 DWORD
278 WINAPI
279 GetConsoleAliasA(LPSTR lpSource,
280 LPSTR lpTargetBuffer,
281 DWORD TargetBufferLength,
282 LPSTR lpExeName)
283 {
284 DPRINT1("GetConsoleAliasA entered with lpSource '%s' lpExeName '%s'\n",
285 lpSource, lpExeName);
286
287 return IntGetConsoleAlias(lpSource,
288 (USHORT)strlen(lpSource) * sizeof(CHAR),
289 lpTargetBuffer,
290 TargetBufferLength,
291 lpExeName,
292 FALSE);
293 }
294
295
296 static DWORD
297 IntGetConsoleAliases(LPVOID AliasBuffer,
298 DWORD AliasBufferLength,
299 LPVOID lpExeName,
300 BOOLEAN bUnicode)
301 {
302 CONSOLE_API_MESSAGE ApiMessage;
303 PCONSOLE_GETALLALIASES GetAllAliasesRequest = &ApiMessage.Data.GetAllAliasesRequest;
304 PCSR_CAPTURE_BUFFER CaptureBuffer;
305
306 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
307
308 if (lpExeName == NULL || NumChars == 0)
309 {
310 SetLastError(ERROR_INVALID_PARAMETER);
311 return 0;
312 }
313
314 GetAllAliasesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
315
316 /* Determine the needed sizes */
317 GetAllAliasesRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
318 GetAllAliasesRequest->Unicode =
319 GetAllAliasesRequest->Unicode2 = bUnicode;
320
321 GetAllAliasesRequest->AliasesBufferLength = AliasBufferLength;
322
323 /* Allocate a Capture Buffer */
324 CaptureBuffer = CsrAllocateCaptureBuffer(2, GetAllAliasesRequest->ExeLength +
325 GetAllAliasesRequest->AliasesBufferLength);
326 if (!CaptureBuffer)
327 {
328 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
329 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
330 return 0;
331 }
332
333 /* Capture the exe name and allocate space for the aliases buffer */
334 CsrCaptureMessageBuffer(CaptureBuffer,
335 (PVOID)lpExeName,
336 GetAllAliasesRequest->ExeLength,
337 (PVOID*)&GetAllAliasesRequest->ExeName);
338
339 CsrAllocateMessagePointer(CaptureBuffer,
340 GetAllAliasesRequest->AliasesBufferLength,
341 (PVOID*)&GetAllAliasesRequest->AliasesBuffer);
342
343 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
344 CaptureBuffer,
345 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliases),
346 sizeof(*GetAllAliasesRequest));
347 if (!NT_SUCCESS(ApiMessage.Status))
348 {
349 CsrFreeCaptureBuffer(CaptureBuffer);
350 BaseSetLastNTError(ApiMessage.Status);
351 return 0;
352 }
353
354 /* Copy the returned aliases string into the user buffer */
355 RtlCopyMemory(AliasBuffer,
356 GetAllAliasesRequest->AliasesBuffer,
357 GetAllAliasesRequest->AliasesBufferLength);
358
359 /* Release the capture buffer and exit */
360 CsrFreeCaptureBuffer(CaptureBuffer);
361
362 return GetAllAliasesRequest->AliasesBufferLength;
363 }
364
365
366 /*
367 * @implemented
368 */
369 DWORD
370 WINAPI
371 GetConsoleAliasesW(LPWSTR AliasBuffer,
372 DWORD AliasBufferLength,
373 LPWSTR ExeName)
374 {
375 DPRINT1("GetConsoleAliasesW entered with lpExeName '%S'\n",
376 ExeName);
377
378 return IntGetConsoleAliases(AliasBuffer,
379 AliasBufferLength,
380 ExeName,
381 TRUE);
382 }
383
384
385 /*
386 * @implemented
387 */
388 DWORD
389 WINAPI
390 GetConsoleAliasesA(LPSTR AliasBuffer,
391 DWORD AliasBufferLength,
392 LPSTR ExeName)
393 {
394 DPRINT1("GetConsoleAliasesA entered with lpExeName '%s'\n",
395 ExeName);
396
397 return IntGetConsoleAliases(AliasBuffer,
398 AliasBufferLength,
399 ExeName,
400 FALSE);
401 }
402
403
404 static DWORD
405 IntGetConsoleAliasesLength(LPVOID lpExeName, BOOLEAN bUnicode)
406 {
407 CONSOLE_API_MESSAGE ApiMessage;
408 PCONSOLE_GETALLALIASESLENGTH GetAllAliasesLengthRequest = &ApiMessage.Data.GetAllAliasesLengthRequest;
409 PCSR_CAPTURE_BUFFER CaptureBuffer;
410
411 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
412
413 if (lpExeName == NULL || NumChars == 0)
414 {
415 SetLastError(ERROR_INVALID_PARAMETER);
416 return 0;
417 }
418
419 GetAllAliasesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
420 GetAllAliasesLengthRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
421 GetAllAliasesLengthRequest->Unicode =
422 GetAllAliasesLengthRequest->Unicode2 = bUnicode;
423
424 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetAllAliasesLengthRequest->ExeLength);
425 if (!CaptureBuffer)
426 {
427 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
428 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
429 return 0;
430 }
431
432 CsrCaptureMessageBuffer(CaptureBuffer,
433 (PVOID)lpExeName,
434 GetAllAliasesLengthRequest->ExeLength,
435 (PVOID)&GetAllAliasesLengthRequest->ExeName);
436
437 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
438 CaptureBuffer,
439 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasesLength),
440 sizeof(*GetAllAliasesLengthRequest));
441
442 CsrFreeCaptureBuffer(CaptureBuffer);
443
444 if (!NT_SUCCESS(ApiMessage.Status))
445 {
446 BaseSetLastNTError(ApiMessage.Status);
447 return 0;
448 }
449
450 return GetAllAliasesLengthRequest->Length;
451 }
452
453
454 /*
455 * @implemented
456 */
457 DWORD
458 WINAPI
459 GetConsoleAliasesLengthW(LPWSTR lpExeName)
460 {
461 return IntGetConsoleAliasesLength(lpExeName, TRUE);
462 }
463
464
465 /*
466 * @implemented
467 */
468 DWORD
469 WINAPI
470 GetConsoleAliasesLengthA(LPSTR lpExeName)
471 {
472 return IntGetConsoleAliasesLength(lpExeName, FALSE);
473 }
474
475
476 static DWORD
477 IntGetConsoleAliasExes(PVOID lpExeNameBuffer,
478 DWORD ExeNameBufferLength,
479 BOOLEAN bUnicode)
480 {
481 CONSOLE_API_MESSAGE ApiMessage;
482 PCONSOLE_GETALIASESEXES GetAliasesExesRequest = &ApiMessage.Data.GetAliasesExesRequest;
483 PCSR_CAPTURE_BUFFER CaptureBuffer;
484
485 GetAliasesExesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
486 GetAliasesExesRequest->Length = ExeNameBufferLength;
487 GetAliasesExesRequest->Unicode = bUnicode;
488
489 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExeNameBufferLength);
490 if (!CaptureBuffer)
491 {
492 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
493 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
494 return 0;
495 }
496
497 CsrAllocateMessagePointer(CaptureBuffer,
498 ExeNameBufferLength,
499 (PVOID*)&GetAliasesExesRequest->ExeNames);
500
501 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
502 CaptureBuffer,
503 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExes),
504 sizeof(*GetAliasesExesRequest));
505 if (!NT_SUCCESS(ApiMessage.Status))
506 {
507 CsrFreeCaptureBuffer(CaptureBuffer);
508 BaseSetLastNTError(ApiMessage.Status);
509 return 0;
510 }
511
512 RtlCopyMemory(lpExeNameBuffer,
513 GetAliasesExesRequest->ExeNames,
514 GetAliasesExesRequest->Length);
515
516 CsrFreeCaptureBuffer(CaptureBuffer);
517
518 return GetAliasesExesRequest->Length;
519 }
520
521 /*
522 * @implemented
523 */
524 DWORD
525 WINAPI
526 GetConsoleAliasExesW(LPWSTR lpExeNameBuffer,
527 DWORD ExeNameBufferLength)
528 {
529 DPRINT1("GetConsoleAliasExesW called\n");
530 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, TRUE);
531 }
532
533
534 /*
535 * @implemented
536 */
537 DWORD
538 WINAPI
539 GetConsoleAliasExesA(LPSTR lpExeNameBuffer,
540 DWORD ExeNameBufferLength)
541 {
542 DPRINT1("GetConsoleAliasExesA called\n");
543 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, FALSE);
544 }
545
546
547 static DWORD
548 IntGetConsoleAliasExesLength(BOOLEAN bUnicode)
549 {
550 CONSOLE_API_MESSAGE ApiMessage;
551 PCONSOLE_GETALIASESEXESLENGTH GetAliasesExesLengthRequest = &ApiMessage.Data.GetAliasesExesLengthRequest;
552
553 GetAliasesExesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
554 GetAliasesExesLengthRequest->Unicode = bUnicode;
555
556 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
557 NULL,
558 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExesLength),
559 sizeof(*GetAliasesExesLengthRequest));
560 if (!NT_SUCCESS(ApiMessage.Status))
561 {
562 BaseSetLastNTError(ApiMessage.Status);
563 return 0;
564 }
565
566 return GetAliasesExesLengthRequest->Length;
567 }
568
569
570 /*
571 * @implemented
572 */
573 DWORD
574 WINAPI
575 GetConsoleAliasExesLengthW(VOID)
576 {
577 DPRINT1("GetConsoleAliasExesLengthW called\n");
578 return IntGetConsoleAliasExesLength(TRUE);
579 }
580
581
582 /*
583 * @implemented
584 */
585 DWORD
586 WINAPI
587 GetConsoleAliasExesLengthA(VOID)
588 {
589 DPRINT1("GetConsoleAliasExesLengthA called\n");
590 return IntGetConsoleAliasExesLength(FALSE);
591 }
592
593 /* EOF */