mshtml: Added IHTMLMetaElement::charset property implementation.
[wine.git] / programs / reg / reg.c
blob1b46661cd24030215784b5838cf2f96cad56e611
1 /*
2 * Copyright 2008 Andrew Riedi
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.
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.
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
19 #include <windows.h>
20 #include <wine/unicode.h>
21 #include "reg.h"
23 static int reg_printfW(const WCHAR *msg, ...)
25 va_list va_args;
26 int wlen;
27 DWORD count, ret;
28 WCHAR msg_buffer[8192];
30 va_start(va_args, msg);
31 vsprintfW(msg_buffer, msg, va_args);
32 va_end(va_args);
34 wlen = lstrlenW(msg_buffer);
35 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
36 if (!ret)
38 DWORD len;
39 char *msgA;
41 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
42 * back to WriteFile(), assuming the console encoding is still the right
43 * one in that case.
45 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
46 NULL, 0, NULL, NULL);
47 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
48 if (!msgA)
49 return 0;
51 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
52 NULL, NULL);
53 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
54 HeapFree(GetProcessHeap(), 0, msgA);
57 return count;
60 static int reg_message(int msg)
62 static const WCHAR formatW[] = {'%','s',0};
63 WCHAR msg_buffer[8192];
65 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
66 sizeof(msg_buffer)/sizeof(WCHAR));
67 return reg_printfW(formatW, msg_buffer);
70 static HKEY get_rootkey(LPWSTR key)
72 static const WCHAR szHKLM[] = {'H','K','L','M',0};
73 static const WCHAR szHKEY_LOCAL_MACHINE[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
74 static const WCHAR szHKCU[] = {'H','K','C','U',0};
75 static const WCHAR szHKEY_CURRENT_USER[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
76 static const WCHAR szHKCR[] = {'H','K','C','R',0};
77 static const WCHAR szHKEY_CLASSES_ROOT[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
78 static const WCHAR szHKU[] = {'H','K','U',0};
79 static const WCHAR szHKEY_USERS[] = {'H','K','E','Y','_','U','S','E','R','S',0};
80 static const WCHAR szHKCC[] = {'H','K','C','C',0};
81 static const WCHAR szHKEY_CURRENT_CONFIG[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
83 if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKLM,4)==CSTR_EQUAL ||
84 CompareStringW(CP_ACP,NORM_IGNORECASE,key,18,szHKEY_LOCAL_MACHINE,18)==CSTR_EQUAL)
85 return HKEY_LOCAL_MACHINE;
86 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCU,4)==CSTR_EQUAL ||
87 CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CURRENT_USER,17)==CSTR_EQUAL)
88 return HKEY_CURRENT_USER;
89 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCR,4)==CSTR_EQUAL ||
90 CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CLASSES_ROOT,17)==CSTR_EQUAL)
91 return HKEY_CLASSES_ROOT;
92 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,3,szHKU,3)==CSTR_EQUAL ||
93 CompareStringW(CP_ACP,NORM_IGNORECASE,key,10,szHKEY_USERS,10)==CSTR_EQUAL)
94 return HKEY_USERS;
95 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCC,4)==CSTR_EQUAL ||
96 CompareStringW(CP_ACP,NORM_IGNORECASE,key,19,szHKEY_CURRENT_CONFIG,19)==CSTR_EQUAL)
97 return HKEY_CURRENT_CONFIG;
98 else return NULL;
101 static DWORD get_regtype(LPWSTR type)
103 static const WCHAR szREG_SZ[] = {'R','E','G','_','S','Z',0};
104 static const WCHAR szREG_MULTI_SZ[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
105 static const WCHAR szREG_DWORD_BIG_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
106 static const WCHAR szREG_DWORD[] = {'R','E','G','_','D','W','O','R','D',0};
107 static const WCHAR szREG_BINARY[] = {'R','E','G','_','B','I','N','A','R','Y',0};
108 static const WCHAR szREG_DWORD_LITTLE_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
109 static const WCHAR szREG_NONE[] = {'R','E','G','_','N','O','N','E',0};
110 static const WCHAR szREG_EXPAND_SZ[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
112 if (!type)
113 return REG_SZ;
115 if (lstrcmpiW(type,szREG_SZ)==0) return REG_SZ;
116 if (lstrcmpiW(type,szREG_DWORD)==0) return REG_DWORD;
117 if (lstrcmpiW(type,szREG_MULTI_SZ)==0) return REG_MULTI_SZ;
118 if (lstrcmpiW(type,szREG_EXPAND_SZ)==0) return REG_EXPAND_SZ;
119 if (lstrcmpiW(type,szREG_DWORD_BIG_ENDIAN)==0) return REG_DWORD_BIG_ENDIAN;
120 if (lstrcmpiW(type,szREG_DWORD_LITTLE_ENDIAN)==0) return REG_DWORD_LITTLE_ENDIAN;
121 if (lstrcmpiW(type,szREG_BINARY)==0) return REG_BINARY;
122 if (lstrcmpiW(type,szREG_NONE)==0) return REG_NONE;
124 return -1;
127 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
129 LPBYTE out_data = NULL;
130 *reg_count = 0;
132 switch (reg_type)
134 case REG_SZ:
136 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
137 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
138 lstrcpyW((LPWSTR)out_data,data);
139 break;
141 case REG_DWORD:
143 LPWSTR rest;
144 DWORD val;
145 val = strtolW(data, &rest, 0);
146 if (rest == data) {
147 static const WCHAR nonnumber[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','n','u','m','b','e','r','.','\n',0};
148 reg_printfW(nonnumber);
149 break;
151 *reg_count = sizeof(DWORD);
152 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
153 ((LPDWORD)out_data)[0] = val;
154 break;
156 default:
158 static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
159 reg_printfW(unhandled, reg_type,data);
163 return out_data;
166 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
167 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
169 static const WCHAR stubW[] = {'A','D','D',' ','-',' ','%','s',
170 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
171 LPWSTR p;
172 HKEY root,subkey;
174 reg_printfW(stubW, key_name, value_name, value_empty, type, data, force);
176 if (key_name[0]=='\\' && key_name[1]=='\\')
178 reg_message(STRING_NO_REMOTE);
179 return 1;
182 p = strchrW(key_name,'\\');
183 if (!p)
185 reg_message(STRING_INVALID_KEY);
186 return 1;
188 p++;
190 root = get_rootkey(key_name);
191 if (!root)
193 reg_message(STRING_INVALID_KEY);
194 return 1;
197 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
199 reg_message(STRING_INVALID_KEY);
200 return 1;
203 if (value_name || data)
205 DWORD reg_type;
206 DWORD reg_count = 0;
207 BYTE* reg_data = NULL;
209 if (!force)
211 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
213 /* FIXME: Prompt for overwrite */
217 reg_type = get_regtype(type);
218 if (reg_type == -1)
220 RegCloseKey(subkey);
221 reg_message(STRING_INVALID_CMDLINE);
222 return 1;
225 if (data)
226 reg_data = get_regdata(data,reg_type,separator,&reg_count);
228 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
229 HeapFree(GetProcessHeap(),0,reg_data);
232 RegCloseKey(subkey);
233 reg_message(STRING_SUCCESS);
235 return 0;
238 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
239 BOOL value_all, BOOL force)
241 LPWSTR p;
242 HKEY root,subkey;
244 static const WCHAR stubW[] = {'D','E','L','E','T','E',
245 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
246 ,0};
247 reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
249 if (key_name[0]=='\\' && key_name[1]=='\\')
251 reg_message(STRING_NO_REMOTE);
252 return 1;
255 p = strchrW(key_name,'\\');
256 if (!p)
258 reg_message(STRING_INVALID_KEY);
259 return 1;
261 p++;
263 root = get_rootkey(key_name);
264 if (!root)
266 reg_message(STRING_INVALID_KEY);
267 return 1;
270 if (value_name && value_empty)
272 reg_message(STRING_INVALID_CMDLINE);
273 return 1;
276 if (value_empty && value_all)
278 reg_message(STRING_INVALID_CMDLINE);
279 return 1;
282 if (!force)
284 /* FIXME: Prompt for delete */
287 /* Delete subtree only if no /v* option is given */
288 if (!value_name && !value_empty && !value_all)
290 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
292 reg_message(STRING_CANNOT_FIND);
293 return 1;
295 reg_message(STRING_SUCCESS);
296 return 0;
299 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
301 reg_message(STRING_CANNOT_FIND);
302 return 1;
305 if (value_all)
307 LPWSTR szValue;
308 DWORD maxValue;
309 DWORD count;
310 LONG rc;
312 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
313 &maxValue, NULL, NULL, NULL);
314 if (rc != ERROR_SUCCESS)
316 /* FIXME: failure */
317 RegCloseKey(subkey);
318 return 1;
320 maxValue++;
321 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
323 while (1)
325 count = maxValue;
326 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
327 if (rc == ERROR_SUCCESS)
329 rc = RegDeleteValueW(subkey, szValue);
330 if (rc != ERROR_SUCCESS)
331 break;
333 else break;
335 if (rc != ERROR_SUCCESS)
337 /* FIXME delete failed */
340 else if (value_name)
342 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
344 RegCloseKey(subkey);
345 reg_message(STRING_CANNOT_FIND);
346 return 1;
349 else if (value_empty)
351 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
354 RegCloseKey(subkey);
355 reg_message(STRING_SUCCESS);
356 return 0;
359 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
360 BOOL subkey)
362 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
363 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
364 reg_printfW(stubW, key_name, value_name, value_empty, subkey);
366 return 1;
369 int wmain(int argc, WCHAR *argvW[])
371 int i;
373 static const WCHAR addW[] = {'a','d','d',0};
374 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
375 static const WCHAR queryW[] = {'q','u','e','r','y',0};
376 static const WCHAR slashDW[] = {'/','d',0};
377 static const WCHAR slashFW[] = {'/','f',0};
378 static const WCHAR slashHW[] = {'/','h',0};
379 static const WCHAR slashSW[] = {'/','s',0};
380 static const WCHAR slashTW[] = {'/','t',0};
381 static const WCHAR slashVW[] = {'/','v',0};
382 static const WCHAR slashVAW[] = {'/','v','a',0};
383 static const WCHAR slashVEW[] = {'/','v','e',0};
384 static const WCHAR slashHelpW[] = {'/','?',0};
386 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
387 || !lstrcmpiW(argvW[1], slashHW))
389 reg_message(STRING_USAGE);
390 return 0;
393 if (!lstrcmpiW(argvW[1], addW))
395 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
396 BOOL value_empty = FALSE, force = FALSE;
398 if (argc < 3)
400 reg_message(STRING_INVALID_CMDLINE);
401 return 1;
403 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
404 !lstrcmpiW(argvW[2], slashHW)))
406 reg_message(STRING_ADD_USAGE);
407 return 0;
410 key_name = argvW[2];
411 for (i = 1; i < argc; i++)
413 if (!lstrcmpiW(argvW[i], slashVW))
414 value_name = argvW[++i];
415 else if (!lstrcmpiW(argvW[i], slashVEW))
416 value_empty = TRUE;
417 else if (!lstrcmpiW(argvW[i], slashTW))
418 type = argvW[++i];
419 else if (!lstrcmpiW(argvW[i], slashSW))
420 separator = argvW[++i][0];
421 else if (!lstrcmpiW(argvW[i], slashDW))
422 data = argvW[++i];
423 else if (!lstrcmpiW(argvW[i], slashFW))
424 force = TRUE;
426 return reg_add(key_name, value_name, value_empty, type, separator,
427 data, force);
429 else if (!lstrcmpiW(argvW[1], deleteW))
431 WCHAR *key_name, *value_name = NULL;
432 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
434 if (argc < 3)
436 reg_message(STRING_INVALID_CMDLINE);
437 return 1;
439 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
440 !lstrcmpiW(argvW[2], slashHW)))
442 reg_message(STRING_DELETE_USAGE);
443 return 0;
446 key_name = argvW[2];
447 for (i = 1; i < argc; i++)
449 if (!lstrcmpiW(argvW[i], slashVW))
450 value_name = argvW[++i];
451 else if (!lstrcmpiW(argvW[i], slashVEW))
452 value_empty = TRUE;
453 else if (!lstrcmpiW(argvW[i], slashVAW))
454 value_all = TRUE;
455 else if (!lstrcmpiW(argvW[i], slashFW))
456 force = TRUE;
458 return reg_delete(key_name, value_name, value_empty, value_all, force);
460 else if (!lstrcmpiW(argvW[1], queryW))
462 WCHAR *key_name, *value_name = NULL;
463 BOOL value_empty = FALSE, subkey = FALSE;
465 if (argc < 3)
467 reg_message(STRING_INVALID_CMDLINE);
468 return 1;
470 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
471 !lstrcmpiW(argvW[2], slashHW)))
473 reg_message(STRING_QUERY_USAGE);
474 return 0;
477 key_name = argvW[2];
478 for (i = 1; i < argc; i++)
480 if (!lstrcmpiW(argvW[i], slashVW))
481 value_name = argvW[++i];
482 else if (!lstrcmpiW(argvW[i], slashVEW))
483 value_empty = TRUE;
484 else if (!lstrcmpiW(argvW[i], slashSW))
485 subkey = TRUE;
487 return reg_query(key_name, value_name, value_empty, subkey);
489 else
491 reg_message(STRING_INVALID_CMDLINE);
492 return 1;