reg: Use FormatMessage for easier internationalization support.
[wine.git] / programs / reg / reg.c
blob33ae0cd90003997cbdaf345d447bc40c9842c994
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 <wine/debug.h>
22 #include "reg.h"
24 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
26 WINE_DEFAULT_DEBUG_CHANNEL(reg);
28 static const WCHAR short_hklm[] = {'H','K','L','M',0};
29 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
30 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
31 static const WCHAR short_hku[] = {'H','K','U',0};
32 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
33 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
34 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
35 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
36 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
37 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
39 static const struct
41 HKEY key;
42 const WCHAR *short_name;
43 const WCHAR *long_name;
45 root_rels[] =
47 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
48 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
49 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
50 {HKEY_USERS, short_hku, long_hku},
51 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
54 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
55 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
56 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
57 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
58 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
59 static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
60 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
61 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
63 static const struct
65 DWORD type;
66 const WCHAR *name;
68 type_rels[] =
70 {REG_NONE, type_none},
71 {REG_SZ, type_sz},
72 {REG_EXPAND_SZ, type_expand_sz},
73 {REG_BINARY, type_binary},
74 {REG_DWORD, type_dword},
75 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
76 {REG_DWORD_BIG_ENDIAN, type_dword_be},
77 {REG_MULTI_SZ, type_multi_sz},
80 static void output_writeconsole(const WCHAR *str, DWORD wlen)
82 DWORD count, ret;
84 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
85 if (!ret)
87 DWORD len;
88 char *msgA;
90 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
91 * back to WriteFile(), assuming the console encoding is still the right
92 * one in that case.
94 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
95 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
96 if (!msgA) return;
98 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
99 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
100 HeapFree(GetProcessHeap(), 0, msgA);
104 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
106 WCHAR *str;
107 DWORD len;
109 SetLastError(NO_ERROR);
110 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
111 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
112 if (len == 0 && GetLastError() != NO_ERROR)
114 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
115 return;
117 output_writeconsole(str, len);
118 LocalFree(str);
121 static void reg_message(int msg)
123 WCHAR msg_buffer[8192];
125 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
126 sizeof(msg_buffer)/sizeof(WCHAR));
127 output_writeconsole(msg_buffer, strlenW(msg_buffer));
130 static void __cdecl output_string(const WCHAR *fmt, ...)
132 __ms_va_list va_args;
134 __ms_va_start(va_args, fmt);
135 output_formatstring(fmt, va_args);
136 __ms_va_end(va_args);
139 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
141 DWORD length = strlenW(rootkey_name);
143 return (!strncmpiW(input_path, rootkey_name, length) &&
144 (input_path[length] == 0 || input_path[length] == '\\'));
147 static HKEY path_get_rootkey(const WCHAR *path)
149 DWORD i;
151 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
153 if (path_rootname_cmp(path, root_rels[i].short_name) ||
154 path_rootname_cmp(path, root_rels[i].long_name))
155 return root_rels[i].key;
158 return NULL;
161 static DWORD wchar_get_type(const WCHAR *type_name)
163 DWORD i;
165 if (!type_name)
166 return REG_SZ;
168 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
170 if (!strcmpiW(type_rels[i].name, type_name))
171 return type_rels[i].type;
174 return ~0u;
177 /* hexchar_to_byte from programs/regedit/hexedit.c */
178 static inline BYTE hexchar_to_byte(WCHAR ch)
180 if (ch >= '0' && ch <= '9')
181 return ch - '0';
182 else if (ch >= 'a' && ch <= 'f')
183 return ch - 'a' + 10;
184 else if (ch >= 'A' && ch <= 'F')
185 return ch - 'A' + 10;
186 else
187 return -1;
190 static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
192 LPBYTE out_data = NULL;
193 *reg_count = 0;
195 switch (reg_type)
197 case REG_SZ:
199 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
200 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
201 lstrcpyW((LPWSTR)out_data,data);
202 break;
204 case REG_DWORD:
206 LPWSTR rest;
207 DWORD val;
208 val = strtolW(data, &rest, 0);
209 if (rest == data) {
210 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};
211 output_string(nonnumber);
212 break;
214 *reg_count = sizeof(DWORD);
215 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
216 ((LPDWORD)out_data)[0] = val;
217 break;
219 case REG_BINARY:
221 static const WCHAR nohex[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','h','e','x',' ','d','a','t','a','.','\n',0};
222 BYTE hex0, hex1;
223 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
224 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
225 out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
226 if(datalen % 2)
228 hex1 = hexchar_to_byte(data[i++]);
229 if(hex1 == 0xFF)
230 goto no_hex_data;
231 out_data[destByteIndex++] = hex1;
233 for(;i + 1 < datalen;i += 2)
235 hex0 = hexchar_to_byte(data[i]);
236 hex1 = hexchar_to_byte(data[i + 1]);
237 if(hex0 == 0xFF || hex1 == 0xFF)
238 goto no_hex_data;
239 out_data[destByteIndex++] = (hex0 << 4) | hex1;
241 break;
242 no_hex_data:
243 /* cleanup, print error */
244 HeapFree(GetProcessHeap(), 0, out_data);
245 output_string(nohex);
246 out_data = NULL;
247 break;
249 default:
251 static const WCHAR unhandled[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','1','!','x','!',' ',' ','d','a','t','a',' ','%','2','\n',0};
252 output_string(unhandled, reg_type, data);
256 return out_data;
259 static BOOL sane_path(const WCHAR *key)
261 unsigned int i = strlenW(key);
263 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
265 reg_message(STRING_INVALID_KEY);
266 return FALSE;
269 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
271 reg_message(STRING_NO_REMOTE);
272 return FALSE;
275 return TRUE;
278 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
279 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
281 LPWSTR p;
282 HKEY root,subkey;
284 if (!sane_path(key_name))
285 return 1;
287 p = strchrW(key_name,'\\');
288 if (!p)
290 reg_message(STRING_INVALID_KEY);
291 return 1;
293 p++;
295 root = path_get_rootkey(key_name);
296 if (!root)
298 reg_message(STRING_INVALID_KEY);
299 return 1;
302 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
304 reg_message(STRING_INVALID_KEY);
305 return 1;
308 if (value_name || data)
310 DWORD reg_type;
311 DWORD reg_count = 0;
312 BYTE* reg_data = NULL;
314 if (!force)
316 if (RegQueryValueW(subkey,value_name,NULL,NULL)==ERROR_SUCCESS)
318 /* FIXME: Prompt for overwrite */
322 reg_type = wchar_get_type(type);
323 if (reg_type == ~0u)
325 RegCloseKey(subkey);
326 reg_message(STRING_UNSUPPORTED_TYPE);
327 return 1;
330 if (data)
331 reg_data = get_regdata(data,reg_type,separator,&reg_count);
333 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
334 HeapFree(GetProcessHeap(),0,reg_data);
337 RegCloseKey(subkey);
338 reg_message(STRING_SUCCESS);
340 return 0;
343 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
344 BOOL value_all, BOOL force)
346 LPWSTR p;
347 HKEY root,subkey;
349 if (!sane_path(key_name))
350 return 1;
352 p = strchrW(key_name,'\\');
353 if (!p)
355 reg_message(STRING_INVALID_KEY);
356 return 1;
358 p++;
360 root = path_get_rootkey(key_name);
361 if (!root)
363 reg_message(STRING_INVALID_KEY);
364 return 1;
367 if (value_name && value_empty)
369 reg_message(STRING_INVALID_CMDLINE);
370 return 1;
373 if (value_empty && value_all)
375 reg_message(STRING_INVALID_CMDLINE);
376 return 1;
379 if (!force)
381 /* FIXME: Prompt for delete */
384 /* Delete subtree only if no /v* option is given */
385 if (!value_name && !value_empty && !value_all)
387 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
389 reg_message(STRING_CANNOT_FIND);
390 return 1;
392 reg_message(STRING_SUCCESS);
393 return 0;
396 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
398 reg_message(STRING_CANNOT_FIND);
399 return 1;
402 if (value_all)
404 LPWSTR szValue;
405 DWORD maxValue;
406 DWORD count;
407 LONG rc;
409 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
410 &maxValue, NULL, NULL, NULL);
411 if (rc != ERROR_SUCCESS)
413 /* FIXME: failure */
414 RegCloseKey(subkey);
415 return 1;
417 maxValue++;
418 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
420 while (1)
422 count = maxValue;
423 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
424 if (rc == ERROR_SUCCESS)
426 rc = RegDeleteValueW(subkey, szValue);
427 if (rc != ERROR_SUCCESS)
428 break;
430 else break;
432 if (rc != ERROR_SUCCESS)
434 /* FIXME delete failed */
437 else if (value_name)
439 if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
441 RegCloseKey(subkey);
442 reg_message(STRING_CANNOT_FIND);
443 return 1;
446 else if (value_empty)
448 RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
451 RegCloseKey(subkey);
452 reg_message(STRING_SUCCESS);
453 return 0;
456 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
457 BOOL subkey)
459 static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
460 '-',' ','%','1',' ','%','2',' ','%','3','!','d','!',' ','%','4','!','d','!','\n',0};
461 output_string(stubW, key_name, value_name, value_empty, subkey);
463 return 1;
466 int wmain(int argc, WCHAR *argvW[])
468 int i;
470 static const WCHAR addW[] = {'a','d','d',0};
471 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
472 static const WCHAR queryW[] = {'q','u','e','r','y',0};
473 static const WCHAR slashDW[] = {'/','d',0};
474 static const WCHAR slashFW[] = {'/','f',0};
475 static const WCHAR slashHW[] = {'/','h',0};
476 static const WCHAR slashSW[] = {'/','s',0};
477 static const WCHAR slashTW[] = {'/','t',0};
478 static const WCHAR slashVW[] = {'/','v',0};
479 static const WCHAR slashVAW[] = {'/','v','a',0};
480 static const WCHAR slashVEW[] = {'/','v','e',0};
481 static const WCHAR slashHelpW[] = {'/','?',0};
483 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
484 || !lstrcmpiW(argvW[1], slashHW))
486 reg_message(STRING_USAGE);
487 return 0;
490 if (!lstrcmpiW(argvW[1], addW))
492 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
493 BOOL value_empty = FALSE, force = FALSE;
495 if (argc < 3)
497 reg_message(STRING_INVALID_CMDLINE);
498 return 1;
500 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
501 !lstrcmpiW(argvW[2], slashHW)))
503 reg_message(STRING_ADD_USAGE);
504 return 0;
507 key_name = argvW[2];
508 for (i = 1; i < argc; i++)
510 if (!lstrcmpiW(argvW[i], slashVW))
511 value_name = argvW[++i];
512 else if (!lstrcmpiW(argvW[i], slashVEW))
513 value_empty = TRUE;
514 else if (!lstrcmpiW(argvW[i], slashTW))
515 type = argvW[++i];
516 else if (!lstrcmpiW(argvW[i], slashSW))
517 separator = argvW[++i][0];
518 else if (!lstrcmpiW(argvW[i], slashDW))
519 data = argvW[++i];
520 else if (!lstrcmpiW(argvW[i], slashFW))
521 force = TRUE;
523 return reg_add(key_name, value_name, value_empty, type, separator,
524 data, force);
526 else if (!lstrcmpiW(argvW[1], deleteW))
528 WCHAR *key_name, *value_name = NULL;
529 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
531 if (argc < 3)
533 reg_message(STRING_INVALID_CMDLINE);
534 return 1;
536 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
537 !lstrcmpiW(argvW[2], slashHW)))
539 reg_message(STRING_DELETE_USAGE);
540 return 0;
543 key_name = argvW[2];
544 for (i = 1; i < argc; i++)
546 if (!lstrcmpiW(argvW[i], slashVW))
547 value_name = argvW[++i];
548 else if (!lstrcmpiW(argvW[i], slashVEW))
549 value_empty = TRUE;
550 else if (!lstrcmpiW(argvW[i], slashVAW))
551 value_all = TRUE;
552 else if (!lstrcmpiW(argvW[i], slashFW))
553 force = TRUE;
555 return reg_delete(key_name, value_name, value_empty, value_all, force);
557 else if (!lstrcmpiW(argvW[1], queryW))
559 WCHAR *key_name, *value_name = NULL;
560 BOOL value_empty = FALSE, subkey = FALSE;
562 if (argc < 3)
564 reg_message(STRING_INVALID_CMDLINE);
565 return 1;
567 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
568 !lstrcmpiW(argvW[2], slashHW)))
570 reg_message(STRING_QUERY_USAGE);
571 return 0;
574 key_name = argvW[2];
575 for (i = 1; i < argc; i++)
577 if (!lstrcmpiW(argvW[i], slashVW))
578 value_name = argvW[++i];
579 else if (!lstrcmpiW(argvW[i], slashVEW))
580 value_empty = TRUE;
581 else if (!lstrcmpiW(argvW[i], slashSW))
582 subkey = TRUE;
584 return reg_query(key_name, value_name, value_empty, subkey);
586 else
588 reg_message(STRING_INVALID_CMDLINE);
589 return 1;