atl: Fix formatting of generated class name for 64-bit.
[wine/multimedia.git] / programs / reg / reg.c
blobf65a3c8bc93072e67ea2068c722e272d47eb4663
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 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
42 NULL, 0, NULL, NULL);
43 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
44 if (!msgA)
45 return 0;
47 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
48 NULL, NULL);
49 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
50 HeapFree(GetProcessHeap(), 0, msgA);
53 return count;
56 static int reg_message(int msg)
58 static const WCHAR formatW[] = {'%','s',0};
59 WCHAR msg_buffer[8192];
61 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
62 sizeof(msg_buffer)/sizeof(WCHAR));
63 return reg_printfW(formatW, msg_buffer);
66 static HKEY get_rootkey(LPWSTR key)
68 static const WCHAR szHKLM[] = {'H','K','L','M',0};
69 static const WCHAR szHKCU[] = {'H','K','C','U',0};
70 static const WCHAR szHKCR[] = {'H','K','C','R',0};
71 static const WCHAR szHKU[] = {'H','K','U',0};
72 static const WCHAR szHKCC[] = {'H','K','C','C',0};
74 if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKLM,4)==2)
75 return HKEY_LOCAL_MACHINE;
76 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCU,4)==2)
77 return HKEY_CURRENT_USER;
78 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCR,4)==2)
79 return HKEY_CLASSES_ROOT;
80 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,3,szHKU,3)==2)
81 return HKEY_USERS;
82 else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCC,4)==2)
83 return HKEY_CURRENT_CONFIG;
84 else return NULL;
87 static DWORD get_regtype(LPWSTR type)
89 static const WCHAR szREG_SZ[] = {'R','E','G','_','S','Z',0};
90 static const WCHAR szREG_MULTI_SZ[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
91 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};
92 static const WCHAR szREG_DWORD[] = {'R','E','G','_','D','W','O','R','D',0};
93 static const WCHAR szREG_BINARY[] = {'R','E','G','_','B','I','N','A','R','Y',0};
94 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};
95 static const WCHAR szREG_NONE[] = {'R','E','G','_','N','O','N','E',0};
96 static const WCHAR szREG_EXPAND_SZ[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
98 if (!type)
99 return REG_SZ;
101 if (lstrcmpiW(type,szREG_SZ)==0) return REG_SZ;
102 if (lstrcmpiW(type,szREG_DWORD)==0) return REG_DWORD;
103 if (lstrcmpiW(type,szREG_MULTI_SZ)==0) return REG_MULTI_SZ;
104 if (lstrcmpiW(type,szREG_EXPAND_SZ)==0) return REG_EXPAND_SZ;
105 if (lstrcmpiW(type,szREG_DWORD_BIG_ENDIAN)==0) return REG_DWORD_BIG_ENDIAN;
106 if (lstrcmpiW(type,szREG_DWORD_LITTLE_ENDIAN)==0) return REG_DWORD_LITTLE_ENDIAN;
107 if (lstrcmpiW(type,szREG_BINARY)==0) return REG_BINARY;
108 if (lstrcmpiW(type,szREG_NONE)==0) return REG_NONE;
110 return -1;
113 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
115 LPBYTE out_data = NULL;
116 *reg_count = 0;
118 switch (reg_type)
120 case REG_SZ:
122 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
123 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
124 lstrcpyW((LPWSTR)out_data,data);
125 break;
127 case REG_DWORD:
129 LPWSTR rest;
130 DWORD val;
131 val = strtolW(data, &rest, 0);
132 if (rest == data) {
133 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};
134 reg_printfW(nonnumber);
135 break;
137 *reg_count = sizeof(DWORD);
138 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
139 ((LPDWORD)out_data)[0] = val;
140 break;
142 default:
144 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};
145 reg_printfW(unhandled, reg_type,data);
149 return out_data;
152 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
153 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
155 static const WCHAR stubW[] = {'A','D','D',' ','-',' ','%','s',
156 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
157 LPWSTR p;
158 HKEY root,subkey;
160 reg_printfW(stubW, key_name, value_name, value_empty, type, data, force);
162 if (key_name[0]=='\\' && key_name[1]=='\\')
164 reg_message(STRING_NO_REMOTE);
165 return 1;
168 p = strchrW(key_name,'\\');
169 if (!p)
171 reg_message(STRING_INVALID_KEY);
172 return 1;
174 p++;
176 root = get_rootkey(key_name);
177 if (!root)
179 reg_message(STRING_INVALID_KEY);
180 return 1;
183 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
185 reg_message(STRING_INVALID_KEY);
186 return 1;
189 if (value_name || data)
191 DWORD reg_type;
192 DWORD reg_count = 0;
193 BYTE* reg_data = NULL;
195 if (!force)
197 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
199 /* FIXME: Prompt for overwrite */
203 reg_type = get_regtype(type);
204 if (reg_type == -1)
206 RegCloseKey(subkey);
207 reg_message(STRING_INVALID_CMDLINE);
208 return 1;
211 if (data)
212 reg_data = get_regdata(data,reg_type,separator,&reg_count);
214 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
215 HeapFree(GetProcessHeap(),0,reg_data);
218 RegCloseKey(subkey);
219 reg_message(STRING_SUCCESS);
221 return 0;
224 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
225 BOOL value_all, BOOL force)
227 LPWSTR p;
228 HKEY root,subkey;
230 static const WCHAR stubW[] = {'D','E','L','E','T','E',
231 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
232 ,0};
233 reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
235 if (key_name[0]=='\\' && key_name[1]=='\\')
237 reg_message(STRING_NO_REMOTE);
238 return 1;
241 p = strchrW(key_name,'\\');
242 if (!p)
244 reg_message(STRING_INVALID_KEY);
245 return 1;
247 p++;
249 root = get_rootkey(key_name);
250 if (!root)
252 reg_message(STRING_INVALID_KEY);
253 return 1;
256 if (value_name && value_empty)
258 reg_message(STRING_INVALID_CMDLINE);
259 return 1;
262 if (value_empty && value_all)
264 reg_message(STRING_INVALID_CMDLINE);
265 return 1;
268 if (!force)
270 /* FIXME: Prompt for delete */
273 /* Delete subtree only if no /v* option is given */
274 if (!value_name && !value_empty && !value_all)
276 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
278 reg_message(STRING_CANNOT_FIND);
279 return 1;
281 reg_message(STRING_SUCCESS);
282 return 0;
285 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
287 reg_message(STRING_CANNOT_FIND);
288 return 1;
291 if (value_all)
293 LPWSTR szValue;
294 DWORD maxValue;
295 DWORD count;
296 LONG rc;
298 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
299 &maxValue, NULL, NULL, NULL);
300 if (rc != ERROR_SUCCESS)
302 /* FIXME: failure */
303 RegCloseKey(subkey);
304 return 1;
306 maxValue++;
307 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
309 while (1)
311 count = maxValue;
312 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
313 if (rc == ERROR_SUCCESS)
315 rc = RegDeleteValueW(subkey, szValue);
316 if (rc != ERROR_SUCCESS)
317 break;
319 else break;
321 if (rc != ERROR_SUCCESS)
323 /* FIXME delete failed */
326 else if (value_name)
328 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
330 RegCloseKey(subkey);
331 reg_message(STRING_CANNOT_FIND);
332 return 1;
335 else if (value_empty)
337 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
340 RegCloseKey(subkey);
341 reg_message(STRING_SUCCESS);
342 return 0;
345 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
346 BOOL subkey)
348 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
349 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
350 reg_printfW(stubW, key_name, value_name, value_empty, subkey);
352 return 1;
355 int wmain(int argc, WCHAR *argvW[])
357 int i;
359 static const WCHAR addW[] = {'a','d','d',0};
360 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
361 static const WCHAR queryW[] = {'q','u','e','r','y',0};
362 static const WCHAR slashDW[] = {'/','d',0};
363 static const WCHAR slashFW[] = {'/','f',0};
364 static const WCHAR slashHW[] = {'/','h',0};
365 static const WCHAR slashSW[] = {'/','s',0};
366 static const WCHAR slashTW[] = {'/','t',0};
367 static const WCHAR slashVW[] = {'/','v',0};
368 static const WCHAR slashVAW[] = {'/','v','a',0};
369 static const WCHAR slashVEW[] = {'/','v','e',0};
370 static const WCHAR slashHelpW[] = {'/','?',0};
372 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
373 || !lstrcmpiW(argvW[1], slashHW))
375 reg_message(STRING_USAGE);
376 return 0;
379 if (!lstrcmpiW(argvW[1], addW))
381 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
382 BOOL value_empty = FALSE, force = FALSE;
384 if (argc < 3)
386 reg_message(STRING_INVALID_CMDLINE);
387 return 1;
389 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
390 !lstrcmpiW(argvW[2], slashHW)))
392 reg_message(STRING_ADD_USAGE);
393 return 0;
396 key_name = argvW[2];
397 for (i = 1; i < argc; i++)
399 if (!lstrcmpiW(argvW[i], slashVW))
400 value_name = argvW[++i];
401 else if (!lstrcmpiW(argvW[i], slashVEW))
402 value_empty = TRUE;
403 else if (!lstrcmpiW(argvW[i], slashTW))
404 type = argvW[++i];
405 else if (!lstrcmpiW(argvW[i], slashSW))
406 separator = argvW[++i][0];
407 else if (!lstrcmpiW(argvW[i], slashDW))
408 data = argvW[++i];
409 else if (!lstrcmpiW(argvW[i], slashFW))
410 force = TRUE;
412 return reg_add(key_name, value_name, value_empty, type, separator,
413 data, force);
415 else if (!lstrcmpiW(argvW[1], deleteW))
417 WCHAR *key_name, *value_name = NULL;
418 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
420 if (argc < 3)
422 reg_message(STRING_INVALID_CMDLINE);
423 return 1;
425 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
426 !lstrcmpiW(argvW[2], slashHW)))
428 reg_message(STRING_DELETE_USAGE);
429 return 0;
432 key_name = argvW[2];
433 for (i = 1; i < argc; i++)
435 if (!lstrcmpiW(argvW[i], slashVW))
436 value_name = argvW[++i];
437 else if (!lstrcmpiW(argvW[i], slashVEW))
438 value_empty = TRUE;
439 else if (!lstrcmpiW(argvW[i], slashVAW))
440 value_all = TRUE;
441 else if (!lstrcmpiW(argvW[i], slashFW))
442 force = TRUE;
444 return reg_delete(key_name, value_name, value_empty, value_all, force);
446 else if (!lstrcmpiW(argvW[1], queryW))
448 WCHAR *key_name, *value_name = NULL;
449 BOOL value_empty = FALSE, subkey = FALSE;
451 if (argc < 3)
453 reg_message(STRING_INVALID_CMDLINE);
454 return 1;
456 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
457 !lstrcmpiW(argvW[2], slashHW)))
459 reg_message(STRING_QUERY_USAGE);
460 return 0;
463 key_name = argvW[2];
464 for (i = 1; i < argc; i++)
466 if (!lstrcmpiW(argvW[i], slashVW))
467 value_name = argvW[++i];
468 else if (!lstrcmpiW(argvW[i], slashVEW))
469 value_empty = TRUE;
470 else if (!lstrcmpiW(argvW[i], slashSW))
471 subkey = TRUE;
473 return reg_query(key_name, value_name, value_empty, subkey);
475 else
477 reg_message(STRING_INVALID_CMDLINE);
478 return 1;