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