[PRINTING]
[reactos.git] / reactos / base / applications / sc / sc.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/sc/sc.c
5 * PURPOSE: parse command line
6 * COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "sc.h"
11
12 SC_HANDLE hSCManager;
13
14 VOID
15 ReportLastError(VOID)
16 {
17 LPVOID lpMsgBuf;
18 DWORD RetVal;
19
20 DWORD ErrorCode = GetLastError();
21 if (ErrorCode != ERROR_SUCCESS)
22 {
23 RetVal = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
24 FORMAT_MESSAGE_FROM_SYSTEM |
25 FORMAT_MESSAGE_IGNORE_INSERTS,
26 NULL,
27 ErrorCode,
28 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
29 (LPTSTR) &lpMsgBuf,
30 0,
31 NULL );
32
33 if (RetVal != 0)
34 {
35 _tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
36 LocalFree(lpMsgBuf);
37 }
38 }
39 }
40
41
42 static INT
43 ScControl(LPCTSTR Server, // remote machine name
44 LPCTSTR Command, // sc command
45 LPCTSTR *ServiceArgs, // any options
46 DWORD ArgCount) // argument counter
47 {
48 LPCTSTR ServiceName = NULL;
49
50 if (Server)
51 {
52 _tprintf(_T("Remote service control is not yet implemented\n"));
53 return 2;
54 }
55
56 if (!lstrcmpi(Command, _T("query")))
57 {
58 Query(ServiceArgs,
59 ArgCount,
60 FALSE);
61 }
62 else if (!lstrcmpi(Command, _T("queryex")))
63 {
64 Query(ServiceArgs,
65 ArgCount,
66 TRUE);
67 }
68 else if (!lstrcmpi(Command, _T("start")))
69 {
70 if (ArgCount > 0)
71 {
72 ServiceName = *ServiceArgs++;
73 ArgCount--;
74
75 Start(ServiceName,
76 ServiceArgs,
77 ArgCount);
78 }
79 else
80 StartUsage();
81 }
82 else if (!lstrcmpi(Command, _T("pause")))
83 {
84 if (ArgCount > 0)
85 {
86 ServiceName = *ServiceArgs++;
87 ArgCount--;
88
89 Control(SERVICE_CONTROL_PAUSE,
90 ServiceName,
91 ServiceArgs,
92 ArgCount);
93 }
94 else
95 PauseUsage();
96 }
97 else if (!lstrcmpi(Command, _T("interrogate")))
98 {
99 if (ArgCount > 0)
100 {
101 ServiceName = *ServiceArgs++;
102 ArgCount--;
103
104 Control(SERVICE_CONTROL_INTERROGATE,
105 ServiceName,
106 ServiceArgs,
107 ArgCount);
108 }
109 else
110 InterrogateUsage();
111 }
112 else if (!lstrcmpi(Command, _T("stop")))
113 {
114 if (ArgCount > 0)
115 {
116 ServiceName = *ServiceArgs++;
117 ArgCount--;
118
119 Control(SERVICE_CONTROL_STOP,
120 ServiceName,
121 ServiceArgs,
122 ArgCount);
123 }
124 else
125 StopUsage();
126 }
127 else if (!lstrcmpi(Command, _T("continue")))
128 {
129 if (ArgCount > 0)
130 {
131 ServiceName = *ServiceArgs++;
132 ArgCount--;
133
134 Control(SERVICE_CONTROL_CONTINUE,
135 ServiceName,
136 ServiceArgs,
137 ArgCount);
138 }
139 else
140 ContinueUsage();
141 }
142 else if (!lstrcmpi(Command, _T("delete")))
143 {
144 if (ArgCount > 0)
145 {
146 ServiceName = *ServiceArgs++;
147 ArgCount--;
148
149 Delete(ServiceName);
150 }
151 else
152 DeleteUsage();
153 }
154 else if (!lstrcmpi(Command, _T("create")))
155 {
156 Create(ServiceArgs, ArgCount);
157 }
158 else if (!lstrcmpi(Command, _T("control")))
159 {
160 INT CtlValue;
161
162 if (ArgCount > 1)
163 {
164 ServiceName = *ServiceArgs++;
165 ArgCount--;
166
167 CtlValue = _ttoi(ServiceArgs[0]);
168 ServiceArgs++;
169 ArgCount--;
170
171 if ((CtlValue >= 128) && (CtlValue <= 255))
172 Control(CtlValue,
173 ServiceName,
174 ServiceArgs,
175 ArgCount);
176 else
177 ControlUsage();
178 }
179 else
180 ControlUsage();
181 }
182 else if (!lstrcmpi(Command, _T("sdshow")))
183 {
184 if (ArgCount > 0)
185 {
186 ServiceName = *ServiceArgs++;
187 ArgCount--;
188
189 SdShow(ServiceName);
190 }
191 else
192 SdShowUsage();
193 }
194 else if (!lstrcmpi(Command, _T("sdset")))
195 {
196 LPCTSTR SecurityDescriptor;
197
198 if (ArgCount > 1)
199 {
200 ServiceName = *ServiceArgs++;
201 ArgCount--;
202
203 SecurityDescriptor = *ServiceArgs++;
204 ArgCount--;
205
206 SdSet(ServiceName, SecurityDescriptor);
207 }
208 else
209 SdSetUsage();
210 }
211 else if (!lstrcmpi(Command, _T("qc")))
212 {
213 if (ArgCount > 0)
214 {
215 ServiceName = *ServiceArgs++;
216 ArgCount--;
217
218 QueryConfig(ServiceName);
219 }
220 else
221 QueryConfigUsage();
222 }
223 else if (!lstrcmpi(Command, _T("qdescription")))
224 {
225 if (ArgCount > 0)
226 {
227 ServiceName = *ServiceArgs++;
228 ArgCount--;
229
230 QueryDescription(ServiceName);
231 }
232 else
233 QueryDescriptionUsage();
234 }
235 else if (!lstrcmpi(Command, _T("qfailure")))
236 {
237 if (ArgCount > 0)
238 {
239 ServiceName = *ServiceArgs++;
240 ArgCount--;
241
242 QueryFailure(ServiceName);
243 }
244 else
245 QueryFailureUsage();
246 }
247 else if (!lstrcmpi(Command, _T("description")))
248 {
249 LPCTSTR Description = NULL;
250
251 if (ArgCount > 0)
252 {
253 ServiceName = *ServiceArgs++;
254 ArgCount--;
255
256 if (ArgCount > 0)
257 {
258 Description = *ServiceArgs++;
259 ArgCount--;
260 }
261
262 SetDescription(ServiceName, Description);
263 }
264 else
265 SetDescriptionUsage();
266 }
267 else if (!lstrcmpi(Command, _T("config")))
268 {
269 SetConfig(ServiceArgs, ArgCount);
270 }
271 else if (!lstrcmpi(Command, _T("failure")))
272 {
273 SetFailure(ServiceArgs, ArgCount);
274 }
275 else if (!lstrcmpi(Command, _T("GetDisplayName")))
276 {
277 if (ArgCount > 0)
278 {
279 ServiceName = *ServiceArgs++;
280 ArgCount--;
281
282 GetDisplayName(ServiceName);
283 }
284 else
285 GetDisplayNameUsage();
286 }
287 else if (!lstrcmpi(Command, _T("GetKeyName")))
288 {
289 if (ArgCount > 0)
290 {
291 ServiceName = *ServiceArgs++;
292 ArgCount--;
293
294 GetKeyName(ServiceName);
295 }
296 else
297 GetKeyNameUsage();
298 }
299 else if (!lstrcmpi(Command, _T("EnumDepend")))
300 {
301 if (ArgCount > 0)
302 {
303 ServiceName = *ServiceArgs++;
304 ArgCount--;
305
306 EnumDepend(ServiceName);
307 }
308 else
309 EnumDependUsage();
310 }
311 else
312 {
313 MainUsage();
314 }
315
316 return 0;
317 }
318
319 int _tmain(int argc, LPCTSTR argv[])
320 {
321 LPCTSTR Server = NULL; // remote machine
322 LPCTSTR Command = NULL; // sc command
323 LPCTSTR *Args = NULL; // Any remaining args
324
325 if (argc < 2)
326 {
327 MainUsage();
328 return -1;
329 }
330
331 /* get server name */
332 if ((argv[1][0] == '\\') && (argv[1][1] == '\\'))
333 {
334 if (argc < 3)
335 {
336 MainUsage();
337 return -1;
338 }
339
340 Server = argv[1];
341 Command = argv[2];
342 if (argc > 3)
343 Args = &argv[3];
344
345 return ScControl(Server,
346 Command,
347 Args,
348 argc-3);
349 }
350 else
351 {
352 Command = argv[1];
353 if (argc > 2)
354 Args = &argv[2];
355
356 return ScControl(Server,
357 Command,
358 Args,
359 argc-2);
360 }
361 }