dwrite: Implement GetWeight() for IDWriteFont.
[wine] / programs / net / net.c
1 /*
2  * Copyright 2007 Tim Schwartz
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <windows.h>
20 #include <lm.h>
21 #include <wine/unicode.h>
22 #include <wine/debug.h>
23
24 #include "resources.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(net);
27
28 #define NET_START 0001
29 #define NET_STOP  0002
30
31 static int output_write(const WCHAR* str, int len)
32 {
33     DWORD ret, count;
34     ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
35     if (!ret)
36     {
37         DWORD lenA;
38         char* strA;
39
40         /* On Windows WriteConsoleW() fails if the output is redirected. So fall
41          * back to WriteFile(), assuming the console encoding is still the right
42          * one in that case.
43          */
44         lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
45                                    NULL, 0, NULL, NULL);
46         strA = HeapAlloc(GetProcessHeap(), 0, lenA);
47         if (!strA)
48             return 0;
49
50         WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
51                             NULL, NULL);
52         WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, len, &count, FALSE);
53         HeapFree(GetProcessHeap(), 0, strA);
54     }
55     return count;
56 }
57
58 static int output_vprintf(const WCHAR* fmt, __ms_va_list va_args)
59 {
60     WCHAR str[8192];
61     int len;
62
63     SetLastError(NO_ERROR);
64     len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str,
65                          sizeof(str)/sizeof(*str), &va_args);
66     if (len == 0 && GetLastError() != NO_ERROR)
67         WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
68     else
69         output_write(str, len);
70     return 0;
71 }
72
73 static int __cdecl output_printf(const WCHAR* fmt, ...)
74 {
75     __ms_va_list arguments;
76
77     __ms_va_start(arguments, fmt);
78     output_vprintf(fmt, arguments);
79     __ms_va_end(arguments);
80     return 0;
81 }
82
83 static int __cdecl output_string(int msg, ...)
84 {
85     WCHAR fmt[8192];
86     __ms_va_list arguments;
87
88     LoadStringW(GetModuleHandleW(NULL), msg, fmt, sizeof(fmt)/sizeof(fmt[0]));
89     __ms_va_start(arguments, msg);
90     output_vprintf(fmt, arguments);
91     __ms_va_end(arguments);
92     return 0;
93 }
94
95 static BOOL output_error_string(DWORD error)
96 {
97     LPWSTR pBuffer;
98     if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
99             FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
100             NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
101     {
102         output_write(pBuffer, lstrlenW(pBuffer));
103         LocalFree(pBuffer);
104         return TRUE;
105     }
106     return FALSE;
107 }
108
109 static BOOL net_use(int argc, const WCHAR* argv[])
110 {
111     USE_INFO_2 *buffer, *connection;
112     DWORD read, total, resume_handle, rc, i;
113     WCHAR* status[STRING_RECONN-STRING_OK+1];
114     resume_handle = 0;
115     buffer = NULL;
116
117     if(argc<3)
118     {
119         HMODULE hmod = GetModuleHandleW(NULL);
120
121         /* Load the status strings */
122         for (i = 0; i < sizeof(status)/sizeof(*status); i++)
123         {
124             status[i] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status));
125             LoadStringW(hmod, STRING_OK+i, status[i], 1024);
126         }
127
128         do {
129             rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
130             if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
131             {
132                 break;
133             }
134
135             if(total == 0)
136             {
137                 output_string(STRING_NO_ENTRIES);
138                 break;
139             }
140
141             output_string(STRING_USE_HEADER);
142             for (i = 0, connection = buffer; i < read; ++i, ++connection)
143                 output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
144                                 connection->ui2_remote, connection->ui2_refcount);
145
146             if (buffer != NULL) NetApiBufferFree(buffer);
147         } while (rc == ERROR_MORE_DATA);
148
149         /* Release the status strings */
150         for (i = 0; i < sizeof(status)/sizeof(*status); i++)
151             HeapFree(GetProcessHeap(), 0, status[i]);
152
153         return TRUE;
154     }
155
156     return FALSE;
157 }
158
159 static BOOL net_enum_services(void)
160 {
161     static const WCHAR runningW[]={' ',' ',' ',' ','%','1','\n',0};
162     SC_HANDLE SCManager;
163     LPENUM_SERVICE_STATUS_PROCESSW services;
164     DWORD size, i, count, resume;
165     BOOL success = FALSE;
166
167     SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
168     if(!SCManager)
169     {
170         output_string(STRING_NO_SCM);
171         return FALSE;
172     }
173
174     EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &size, &count, NULL, NULL);
175     if(GetLastError() != ERROR_MORE_DATA)
176     {
177         output_error_string(GetLastError());
178         goto end;
179     }
180     services = HeapAlloc(GetProcessHeap(), 0, size);
181     resume = 0;
182     if(!EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, (LPBYTE)services, size, &size, &count, &resume, NULL))
183     {
184         output_error_string(GetLastError());
185         goto end;
186     }
187     output_string(STRING_RUNNING_HEADER);
188     for(i = 0; i < count; i++)
189     {
190         output_printf(runningW, services[i].lpDisplayName);
191         WINE_TRACE("service=%s state=%d controls=%x\n",
192                    wine_dbgstr_w(services[i].lpServiceName),
193                    services[i].ServiceStatusProcess.dwCurrentState,
194                    services[i].ServiceStatusProcess.dwControlsAccepted);
195     }
196     success = TRUE;
197
198  end:
199     CloseServiceHandle(SCManager);
200     return success;
201 }
202
203 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
204 {
205     LPENUM_SERVICE_STATUSW dependencies = NULL;
206     DWORD buffer_size = 0;
207     DWORD count = 0, counter;
208     BOOL result;
209     SC_HANDLE dependent_serviceHandle;
210     SERVICE_STATUS_PROCESS ssp;
211
212     result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
213
214     if(!result && (GetLastError() == ERROR_MORE_DATA))
215     {
216         dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
217         if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
218         {
219             for(counter = 0; counter < count; counter++)
220             {
221                 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
222                 dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
223                 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
224                 CloseServiceHandle(dependent_serviceHandle);
225                 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
226            }
227         }
228     }
229
230     if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
231     HeapFree(GetProcessHeap(), 0, dependencies);
232     return result;
233 }
234
235 static BOOL net_service(int operation, const WCHAR* service_name)
236 {
237     SC_HANDLE SCManager, serviceHandle;
238     BOOL result = 0;
239     WCHAR service_display_name[4096];
240     DWORD buffer_size;
241
242     SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
243     if(!SCManager)
244     {
245         output_string(STRING_NO_SCM);
246         return FALSE;
247     }
248     serviceHandle = OpenServiceW(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
249     if(!serviceHandle)
250     {
251         output_string(STRING_NO_SVCHANDLE);
252         CloseServiceHandle(SCManager);
253         return FALSE;
254     }
255
256     buffer_size = sizeof(service_display_name)/sizeof(*service_display_name);
257     GetServiceDisplayNameW(SCManager, service_name, service_display_name, &buffer_size);
258     if (!service_display_name[0]) lstrcpyW(service_display_name, service_name);
259
260     switch(operation)
261     {
262     case NET_START:
263         output_string(STRING_START_SVC, service_display_name);
264         result = StartServiceW(serviceHandle, 0, NULL);
265
266         if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
267         else
268         {
269             if (!output_error_string(GetLastError()))
270                 output_string(STRING_START_SVC_FAIL, service_display_name);
271         }
272         break;
273     case NET_STOP:
274         output_string(STRING_STOP_SVC, service_display_name);
275         result = StopService(SCManager, serviceHandle);
276
277         if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
278         else
279         {
280             if (!output_error_string(GetLastError()))
281                 output_string(STRING_STOP_SVC_FAIL, service_display_name);
282         }
283         break;
284     }
285
286     CloseServiceHandle(serviceHandle);
287     CloseServiceHandle(SCManager);
288     return result;
289 }
290
291 static int arg_is(const WCHAR* str1, const WCHAR* str2)
292 {
293     return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
294 }
295
296 int wmain(int argc, const WCHAR* argv[])
297 {
298     static const WCHAR helpW[]={'h','e','l','p',0};
299     static const WCHAR shelpW[]={'/','h','e','l','p',0};
300     static const WCHAR startW[]={'s','t','a','r','t',0};
301     static const WCHAR stopW[]={'s','t','o','p',0};
302     static const WCHAR useW[]={'u','s','e',0};
303
304     if (argc < 2)
305     {
306         output_string(STRING_USAGE);
307         return 1;
308     }
309
310     if(arg_is(argv[1], helpW))
311     {
312         if(argc > 3)
313         {
314             output_string(STRING_USAGE);
315             return 1;
316         }
317         if(argc == 2)
318             output_string(STRING_USAGE);
319         else if(arg_is(argv[2], startW))
320             output_string(STRING_START_USAGE);
321         else if(arg_is(argv[2], stopW))
322             output_string(STRING_STOP_USAGE);
323         else
324             output_string(STRING_USAGE);
325     }
326     else if(arg_is(argv[1], startW))
327     {
328         if(argc > 3)
329         {
330             output_string(STRING_START_USAGE);
331             return 1;
332         }
333         if (argc == 2)
334         {
335             if (!net_enum_services())
336                 return 1;
337         }
338         else if(arg_is(argv[2], shelpW))
339             output_string(STRING_START_USAGE);
340         else if(!net_service(NET_START, argv[2]))
341             return 1;
342     }
343     else if(arg_is(argv[1], stopW))
344     {
345         if(argc != 3)
346         {
347             output_string(STRING_STOP_USAGE);
348             return 1;
349         }
350         if(arg_is(argv[2], shelpW))
351             output_string(STRING_STOP_USAGE);
352         else if(!net_service(NET_STOP, argv[2]))
353             return 1;
354     }
355     else if(arg_is(argv[1], useW))
356     {
357         if(!net_use(argc, argv)) return 1;
358     }
359     else
360         output_string(STRING_USAGE);
361
362     return 0;
363 }