Implement csrsrv!CsrSrvInitializeServerDll based on current code from CsrpInitWin32Cs...
[reactos.git] / reactos / subsys / csr / args.c
1 /* $Id$
2 *
3 * args.c - Client/Server Runtime - command line parsing
4 *
5 * ReactOS Operating System
6 *
7 * --------------------------------------------------------------------
8 *
9 * This software is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this software; see the file COPYING.LIB. If not, write
21 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
22 * MA 02139, USA.
23 *
24 * --------------------------------------------------------------------
25 */
26 #include "csr.h"
27
28 #define NDEBUG
29 #include <debug.h>
30
31 /**********************************************************************
32 * NAME PRIVATE
33 * CsrParseCommandLine/2
34 */
35 NTSTATUS FASTCALL CsrParseCommandLine (PPEB Peb,
36 PCOMMAND_LINE_ARGUMENT Argument)
37 {
38 HANDLE ProcessHeap = Peb->ProcessHeap;
39 PRTL_USER_PROCESS_PARAMETERS RtlProcessParameters = RtlNormalizeProcessParams (Peb->ProcessParameters);
40 INT i = 0;
41 INT afterlastspace = 0;
42
43
44 DPRINT("CSR: %s called\n", __FUNCTION__);
45
46 RtlZeroMemory (Argument, sizeof (COMMAND_LINE_ARGUMENT));
47
48 Argument->Vector = (PWSTR *) RtlAllocateHeap (ProcessHeap,
49 0,
50 (CSRP_MAX_ARGUMENT_COUNT * sizeof Argument->Vector[0]));
51 if(NULL == Argument->Vector)
52 {
53 DPRINT("CSR: %s: no memory for Argument->Vector\n", __FUNCTION__);
54 return STATUS_NO_MEMORY;
55 }
56
57 Argument->Buffer.Length =
58 Argument->Buffer.MaximumLength =
59 RtlProcessParameters->CommandLine.Length
60 + sizeof Argument->Buffer.Buffer [0]; /* zero terminated */
61 Argument->Buffer.Buffer =
62 (PWSTR) RtlAllocateHeap (ProcessHeap,
63 0,
64 Argument->Buffer.MaximumLength);
65 if(NULL == Argument->Buffer.Buffer)
66 {
67 DPRINT("CSR: %s: no memory for Argument->Buffer.Buffer\n", __FUNCTION__);
68 return STATUS_NO_MEMORY;
69 }
70
71 RtlCopyMemory (Argument->Buffer.Buffer,
72 RtlProcessParameters->CommandLine.Buffer,
73 RtlProcessParameters->CommandLine.Length);
74
75 while (Argument->Buffer.Buffer [i])
76 {
77 if (Argument->Buffer.Buffer[i] == L' ')
78 {
79 Argument->Count ++;
80 Argument->Buffer.Buffer [i] = L'\0';
81 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
82 i++;
83 while (Argument->Buffer.Buffer [i] == L' ')
84 {
85 i++;
86 }
87 afterlastspace = i;
88 }
89 else
90 {
91 i++;
92 }
93 }
94
95 if (Argument->Buffer.Buffer [afterlastspace] != L'\0')
96 {
97 Argument->Count ++;
98 Argument->Buffer.Buffer [i] = L'\0';
99 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
100 }
101 #if !defined(NDEBUG)
102 for (i=0; i<Argument->Count; i++)
103 {
104 DPRINT("CSR: Argument[%d] = '%S'\n", i, Argument->Vector [i]);
105 }
106 #endif
107 return STATUS_SUCCESS;
108 }
109 /**********************************************************************
110 * NAME PRIVATE
111 * CsrFreeCommandLine/2
112 */
113
114 VOID FASTCALL CsrFreeCommandLine (PPEB Peb,
115 PCOMMAND_LINE_ARGUMENT Argument)
116 {
117 DPRINT("CSR: %s called\n", __FUNCTION__);
118
119 RtlFreeHeap (Peb->ProcessHeap,
120 0,
121 Argument->Vector);
122 RtlFreeHeap (Peb->ProcessHeap,
123 0,
124 Argument->Buffer.Buffer);
125 }
126 /* EOF */