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