Compile CSRSS and WIN32CSR with NDK headers.
[reactos.git] / reactos / subsys / csrss / csrss.c
1 /* $Id$
2 *
3 * csrss.c - Client/Server Runtime subsystem
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 * 19990417 (Emanuele Aliberti)
27 * Do nothing native application skeleton
28 * 19990528 (Emanuele Aliberti)
29 * Compiled successfully with egcs 1.1.2
30 * 19990605 (Emanuele Aliberti)
31 * First standalone run under ReactOS (it
32 * actually does nothing but running).
33 */
34
35 #include <windows.h>
36 #define NTOS_MODE_USER
37 #include <ndk/ntndk.h>
38 #include <csrss/csrss.h>
39 #include <rosrtl/string.h>
40 #include <reactos/buildno.h>
41
42 #include "api.h"
43
44 #define NDEBUG
45 #include <debug.h>
46
47 #define CSRP_MAX_ARGUMENT_COUNT 512
48
49 typedef struct _COMMAND_LINE_ARGUMENT
50 {
51 ULONG Count;
52 UNICODE_STRING Buffer;
53 PWSTR * Vector;
54
55 } COMMAND_LINE_ARGUMENT, *PCOMMAND_LINE_ARGUMENT;
56
57 PRTL_USER_PROCESS_PARAMETERS RtlProcessParameters = NULL;
58
59 /**********************************************************************
60 * NAME PRIVATE
61 * CsrpParseCommandLine/3
62 */
63 static NTSTATUS STDCALL
64 CsrpParseCommandLine (HANDLE ProcessHeap,
65 PRTL_USER_PROCESS_PARAMETERS RtlProcessParameters,
66 PCOMMAND_LINE_ARGUMENT Argument)
67 {
68 INT i = 0;
69 INT afterlastspace = 0;
70
71
72 DPRINT("CSR: %s called\n", __FUNCTION__);
73
74 RtlZeroMemory (Argument, sizeof (COMMAND_LINE_ARGUMENT));
75
76 Argument->Vector = (PWSTR *) RtlAllocateHeap (ProcessHeap,
77 0,
78 (CSRP_MAX_ARGUMENT_COUNT * sizeof Argument->Vector[0]));
79 if(NULL == Argument->Vector)
80 {
81 DPRINT("CSR: %s: no memory for Argument->Vector\n", __FUNCTION__);
82 return STATUS_NO_MEMORY;
83 }
84
85 Argument->Buffer.Length =
86 Argument->Buffer.MaximumLength =
87 RtlProcessParameters->CommandLine.Length
88 + sizeof Argument->Buffer.Buffer [0]; /* zero terminated */
89 Argument->Buffer.Buffer =
90 (PWSTR) RtlAllocateHeap (ProcessHeap,
91 0,
92 Argument->Buffer.MaximumLength);
93 if(NULL == Argument->Buffer.Buffer)
94 {
95 DPRINT("CSR: %s: no memory for Argument->Buffer.Buffer\n", __FUNCTION__);
96 return STATUS_NO_MEMORY;
97 }
98
99 RtlCopyMemory (Argument->Buffer.Buffer,
100 RtlProcessParameters->CommandLine.Buffer,
101 RtlProcessParameters->CommandLine.Length);
102
103 while (Argument->Buffer.Buffer [i])
104 {
105 if (Argument->Buffer.Buffer[i] == L' ')
106 {
107 Argument->Count ++;
108 Argument->Buffer.Buffer [i] = L'\0';
109 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
110 i++;
111 while (Argument->Buffer.Buffer [i] == L' ')
112 {
113 i++;
114 }
115 afterlastspace = i;
116 }
117 else
118 {
119 i++;
120 }
121 }
122
123 if (Argument->Buffer.Buffer [afterlastspace] != L'\0')
124 {
125 Argument->Count ++;
126 Argument->Buffer.Buffer [i] = L'\0';
127 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
128 }
129
130 return STATUS_SUCCESS;
131 }
132
133 /**********************************************************************
134 * NAME PRIVATE
135 * CsrpFreeCommandLine/2
136 */
137
138 static VOID STDCALL
139 CsrpFreeCommandLine (HANDLE ProcessHeap,
140 PCOMMAND_LINE_ARGUMENT Argument)
141 {
142 DPRINT("CSR: %s called\n", __FUNCTION__);
143
144 RtlFreeHeap (ProcessHeap,
145 0,
146 Argument->Vector);
147 RtlFreeHeap (ProcessHeap,
148 0,
149 Argument->Buffer.Buffer);
150 }
151
152
153 /* Native process' entry point */
154
155 VOID STDCALL NtProcessStartup(PPEB Peb)
156 {
157 COMMAND_LINE_ARGUMENT CmdLineArg = {0};
158 NTSTATUS Status = STATUS_SUCCESS;
159
160 PrintString("ReactOS Client/Server Run-Time %s (Build %s)\n",
161 KERNEL_RELEASE_STR,
162 KERNEL_VERSION_BUILD_STR);
163
164 RtlProcessParameters = RtlNormalizeProcessParams (Peb->ProcessParameters);
165
166 /*==================================================================
167 * Parse the command line: TODO actually parse the cl, because
168 * it is required to load hosted server DLLs.
169 *================================================================*/
170 Status = CsrpParseCommandLine (Peb->ProcessHeap,
171 RtlProcessParameters,
172 & CmdLineArg);
173 if(STATUS_SUCCESS != Status)
174 {
175 DPRINT1("CSR: %s: CsrpParseCommandLine failed (Status=0x%08lx)\n",
176 __FUNCTION__, Status);
177 }
178 /*==================================================================
179 * Initialize the Win32 environment subsystem server.
180 *================================================================*/
181 if (CsrServerInitialization (CmdLineArg.Count, CmdLineArg.Vector) == TRUE)
182 {
183 CsrpFreeCommandLine (Peb->ProcessHeap, & CmdLineArg);
184 /*
185 * Terminate the current thread only.
186 */
187 NtTerminateThread (NtCurrentThread(), 0);
188 }
189 else
190 {
191 DisplayString (L"CSR: CsrServerInitialization failed.\n");
192
193 CsrpFreeCommandLine (Peb->ProcessHeap, & CmdLineArg);
194 /*
195 * Tell the SM we failed.
196 */
197 NtTerminateProcess (NtCurrentProcess(), 0);
198 }
199 }
200
201 /* EOF */