Minor changes for ATAPI Srb Functions
[reactos.git] / 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
183 {
184 MainUsage();
185 }
186
187 return 0;
188 }
189
190 int _tmain(int argc, LPCTSTR argv[])
191 {
192 LPCTSTR Server = NULL; // remote machine
193 LPCTSTR Command = NULL; // sc command
194 LPCTSTR *Args = NULL; // Any remaining args
195
196 if (argc < 2)
197 {
198 MainUsage();
199 return -1;
200 }
201
202 /* get server name */
203 if ((argv[1][0] == '\\') && (argv[1][1] == '\\'))
204 {
205 if (argc < 3)
206 {
207 MainUsage();
208 return -1;
209 }
210
211 Server = argv[1];
212 Command = argv[2];
213 if (argc > 3)
214 Args = &argv[3];
215
216 return ScControl(Server,
217 Command,
218 Args,
219 argc-3);
220 }
221 else
222 {
223 Command = argv[1];
224 if (argc > 2)
225 Args = &argv[2];
226
227 return ScControl(Server,
228 Command,
229 Args,
230 argc-2);
231 }
232 }