reg: Rename reg.h to resource.h.
[wine.git] / programs / reg / reg.c
blob595dcab8e727913b4bc3085ddc494869aa3c8a7a
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 <errno.h>
21 #include <stdlib.h>
22 #include <wine/unicode.h>
23 #include <wine/debug.h>
24 #include "resource.h"
26 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
28 WINE_DEFAULT_DEBUG_CHANNEL(reg);
30 static const WCHAR short_hklm[] = {'H','K','L','M',0};
31 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
32 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
33 static const WCHAR short_hku[] = {'H','K','U',0};
34 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
35 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
36 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
37 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
38 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
39 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
41 static const struct
43 HKEY key;
44 const WCHAR *short_name;
45 const WCHAR *long_name;
47 root_rels[] =
49 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
50 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
51 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
52 {HKEY_USERS, short_hku, long_hku},
53 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
56 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
57 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
58 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
59 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
60 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
61 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};
62 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
63 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
65 static const struct
67 DWORD type;
68 const WCHAR *name;
70 type_rels[] =
72 {REG_NONE, type_none},
73 {REG_SZ, type_sz},
74 {REG_EXPAND_SZ, type_expand_sz},
75 {REG_BINARY, type_binary},
76 {REG_DWORD, type_dword},
77 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
78 {REG_DWORD_BIG_ENDIAN, type_dword_be},
79 {REG_MULTI_SZ, type_multi_sz},
82 static void *heap_xalloc(size_t size)
84 void *buf = HeapAlloc(GetProcessHeap(), 0, size);
85 if (!buf)
87 ERR("Out of memory!\n");
88 exit(1);
90 return buf;
93 static void *heap_xrealloc(void *buf, size_t size)
95 void *new_buf;
97 if (buf)
98 new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, size);
99 else
100 new_buf = HeapAlloc(GetProcessHeap(), 0, size);
102 if (!new_buf)
104 ERR("Out of memory!\n");
105 exit(1);
108 return new_buf;
111 static BOOL heap_free(void *buf)
113 return HeapFree(GetProcessHeap(), 0, buf);
116 static void output_writeconsole(const WCHAR *str, DWORD wlen)
118 DWORD count, ret;
120 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
121 if (!ret)
123 DWORD len;
124 char *msgA;
126 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
127 * back to WriteFile(), assuming the console encoding is still the right
128 * one in that case.
130 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
131 msgA = heap_xalloc(len);
133 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
134 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
135 heap_free(msgA);
139 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
141 WCHAR *str;
142 DWORD len;
144 SetLastError(NO_ERROR);
145 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
146 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
147 if (len == 0 && GetLastError() != NO_ERROR)
149 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
150 return;
152 output_writeconsole(str, len);
153 LocalFree(str);
156 static void __cdecl output_message(unsigned int id, ...)
158 WCHAR fmt[1024];
159 __ms_va_list va_args;
161 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
163 WINE_FIXME("LoadString failed with %d\n", GetLastError());
164 return;
166 __ms_va_start(va_args, id);
167 output_formatstring(fmt, va_args);
168 __ms_va_end(va_args);
171 static void __cdecl output_string(const WCHAR *fmt, ...)
173 __ms_va_list va_args;
175 __ms_va_start(va_args, fmt);
176 output_formatstring(fmt, va_args);
177 __ms_va_end(va_args);
180 /* ask_confirm() adapted from programs/cmd/builtins.c */
181 static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
183 HMODULE hmod;
184 WCHAR Ybuffer[4];
185 WCHAR Nbuffer[4];
186 WCHAR defval[32];
187 WCHAR answer[MAX_PATH];
188 WCHAR *str;
189 DWORD count;
191 hmod = GetModuleHandleW(NULL);
192 LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
193 LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
194 LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
196 str = (reg_info && *reg_info) ? reg_info : defval;
198 while (1)
200 output_message(msgid, str);
201 output_message(STRING_YESNO);
202 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
203 answer[0] = toupperW(answer[0]);
204 if (answer[0] == Ybuffer[0])
205 return TRUE;
206 if (answer[0] == Nbuffer[0])
207 return FALSE;
211 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
213 DWORD length = strlenW(rootkey_name);
215 return (!strncmpiW(input_path, rootkey_name, length) &&
216 (input_path[length] == 0 || input_path[length] == '\\'));
219 static HKEY path_get_rootkey(const WCHAR *path)
221 DWORD i;
223 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
225 if (path_rootname_cmp(path, root_rels[i].short_name) ||
226 path_rootname_cmp(path, root_rels[i].long_name))
227 return root_rels[i].key;
230 return NULL;
233 static DWORD wchar_get_type(const WCHAR *type_name)
235 DWORD i;
237 if (!type_name)
238 return REG_SZ;
240 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
242 if (!strcmpiW(type_rels[i].name, type_name))
243 return type_rels[i].type;
246 return ~0u;
249 /* hexchar_to_byte from programs/regedit/hexedit.c */
250 static inline BYTE hexchar_to_byte(WCHAR ch)
252 if (ch >= '0' && ch <= '9')
253 return ch - '0';
254 else if (ch >= 'a' && ch <= 'f')
255 return ch - 'a' + 10;
256 else if (ch >= 'A' && ch <= 'F')
257 return ch - 'A' + 10;
258 else
259 return -1;
262 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
264 static const WCHAR empty;
265 LPBYTE out_data = NULL;
266 *reg_count = 0;
268 if (!data) data = &empty;
270 switch (reg_type)
272 case REG_NONE:
273 case REG_SZ:
274 case REG_EXPAND_SZ:
276 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
277 out_data = heap_xalloc(*reg_count);
278 lstrcpyW((LPWSTR)out_data,data);
279 break;
281 case REG_DWORD:
282 /* case REG_DWORD_LITTLE_ENDIAN: */
283 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
285 LPWSTR rest;
286 unsigned long val;
287 val = wcstoul(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
288 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
289 output_message(STRING_MISSING_INTEGER);
290 break;
292 *reg_count = sizeof(DWORD);
293 out_data = heap_xalloc(*reg_count);
294 ((LPDWORD)out_data)[0] = val;
295 break;
297 case REG_BINARY:
299 BYTE hex0, hex1;
300 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
301 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
302 out_data = heap_xalloc(*reg_count);
303 if(datalen % 2)
305 hex1 = hexchar_to_byte(data[i++]);
306 if(hex1 == 0xFF)
307 goto no_hex_data;
308 out_data[destByteIndex++] = hex1;
310 for(;i + 1 < datalen;i += 2)
312 hex0 = hexchar_to_byte(data[i]);
313 hex1 = hexchar_to_byte(data[i + 1]);
314 if(hex0 == 0xFF || hex1 == 0xFF)
315 goto no_hex_data;
316 out_data[destByteIndex++] = (hex0 << 4) | hex1;
318 break;
319 no_hex_data:
320 /* cleanup, print error */
321 heap_free(out_data);
322 output_message(STRING_MISSING_HEXDATA);
323 out_data = NULL;
324 break;
326 case REG_MULTI_SZ:
328 int i, destindex, len = strlenW(data);
329 WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
331 for (i = 0, destindex = 0; i < len; i++, destindex++)
333 if (!separator && data[i] == '\\' && data[i + 1] == '0')
335 buffer[destindex] = 0;
336 i++;
338 else if (data[i] == separator)
339 buffer[destindex] = 0;
340 else
341 buffer[destindex] = data[i];
343 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
345 heap_free(buffer);
346 output_message(STRING_INVALID_STRING);
347 return NULL;
350 buffer[destindex] = 0;
351 if (destindex && buffer[destindex - 1])
352 buffer[++destindex] = 0;
353 *reg_count = (destindex + 1) * sizeof(WCHAR);
354 return (BYTE *)buffer;
356 default:
357 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
360 return out_data;
363 static BOOL sane_path(const WCHAR *key)
365 unsigned int i = strlenW(key);
367 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
369 output_message(STRING_INVALID_KEY);
370 return FALSE;
373 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
375 output_message(STRING_NO_REMOTE);
376 return FALSE;
379 return TRUE;
382 static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
383 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
385 HKEY key;
387 if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
389 output_message(STRING_INVALID_KEY);
390 return 1;
393 if (value_name || value_empty || data)
395 DWORD reg_type;
396 DWORD reg_count = 0;
397 BYTE* reg_data = NULL;
399 if (!force)
401 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
403 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
405 RegCloseKey(key);
406 output_message(STRING_CANCELLED);
407 return 0;
412 reg_type = wchar_get_type(type);
413 if (reg_type == ~0u)
415 RegCloseKey(key);
416 output_message(STRING_UNSUPPORTED_TYPE, type);
417 return 1;
419 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
421 RegCloseKey(key);
422 output_message(STRING_INVALID_CMDLINE);
423 return 1;
426 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
428 RegCloseKey(key);
429 return 1;
432 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
433 heap_free(reg_data);
436 RegCloseKey(key);
437 output_message(STRING_SUCCESS);
439 return 0;
442 static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
443 BOOL value_empty, BOOL value_all, BOOL force)
445 HKEY key;
447 if (!force)
449 BOOL ret;
451 if (value_name || value_empty)
452 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
453 else if (value_all)
454 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
455 else
456 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
458 if (!ret)
460 output_message(STRING_CANCELLED);
461 return 0;
465 /* Delete subtree only if no /v* option is given */
466 if (!value_name && !value_empty && !value_all)
468 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
470 output_message(STRING_CANNOT_FIND);
471 return 1;
473 output_message(STRING_SUCCESS);
474 return 0;
477 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
479 output_message(STRING_CANNOT_FIND);
480 return 1;
483 if (value_all)
485 DWORD max_value_len = 256, value_len;
486 WCHAR *value_name;
487 LONG rc;
489 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
491 while (1)
493 value_len = max_value_len;
494 rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
495 if (rc == ERROR_SUCCESS)
497 rc = RegDeleteValueW(key, value_name);
498 if (rc != ERROR_SUCCESS)
500 heap_free(value_name);
501 RegCloseKey(key);
502 output_message(STRING_VALUEALL_FAILED, key_name);
503 return 1;
506 else if (rc == ERROR_MORE_DATA)
508 max_value_len *= 2;
509 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
511 else break;
513 heap_free(value_name);
515 else if (value_name || value_empty)
517 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
519 RegCloseKey(key);
520 output_message(STRING_CANNOT_FIND);
521 return 1;
525 RegCloseKey(key);
526 output_message(STRING_SUCCESS);
527 return 0;
530 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
532 WCHAR *buffer = NULL;
533 int i;
535 switch (type)
537 case REG_SZ:
538 case REG_EXPAND_SZ:
539 buffer = heap_xalloc(size_bytes);
540 strcpyW(buffer, (WCHAR *)src);
541 break;
542 case REG_NONE:
543 case REG_BINARY:
545 WCHAR *ptr;
546 static const WCHAR fmt[] = {'%','0','2','X',0};
548 buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
549 ptr = buffer;
550 for (i = 0; i < size_bytes; i++)
551 ptr += sprintfW(ptr, fmt, src[i]);
552 break;
554 case REG_DWORD:
555 /* case REG_DWORD_LITTLE_ENDIAN: */
556 case REG_DWORD_BIG_ENDIAN:
558 const int zero_x_dword = 10;
559 static const WCHAR fmt[] = {'0','x','%','x',0};
561 buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
562 sprintfW(buffer, fmt, *(DWORD *)src);
563 break;
565 case REG_MULTI_SZ:
567 const int two_wchars = 2 * sizeof(WCHAR);
568 DWORD tmp_size;
569 const WCHAR *tmp = (const WCHAR *)src;
570 int len, destindex;
572 if (size_bytes <= two_wchars)
574 buffer = heap_xalloc(sizeof(WCHAR));
575 *buffer = 0;
576 return buffer;
579 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
580 buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
581 len = tmp_size / sizeof(WCHAR);
583 for (i = 0, destindex = 0; i < len; i++, destindex++)
585 if (tmp[i])
586 buffer[destindex] = tmp[i];
587 else
589 buffer[destindex++] = '\\';
590 buffer[destindex] = '0';
593 buffer[destindex] = 0;
594 break;
597 return buffer;
600 static const WCHAR *reg_type_to_wchar(DWORD type)
602 int i, array_size = ARRAY_SIZE(type_rels);
604 for (i = 0; i < array_size; i++)
606 if (type == type_rels[i].type)
607 return type_rels[i].name;
609 return NULL;
612 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
614 static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
615 static const WCHAR newlineW[] = {'\n',0};
616 WCHAR defval[32];
617 WCHAR *reg_data;
619 if (value_name && value_name[0])
620 output_string(fmt, value_name);
621 else
623 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
624 output_string(fmt, defval);
626 output_string(fmt, reg_type_to_wchar(type));
628 if (data)
630 reg_data = reg_data_to_wchar(type, data, data_size);
631 output_string(fmt, reg_data);
632 heap_free(reg_data);
634 else
636 LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
637 output_string(fmt, defval);
639 output_string(newlineW);
642 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
644 WCHAR *subkey_path;
645 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
647 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
648 sprintfW(subkey_path, fmt, path, subkey_name);
650 return subkey_path;
653 static unsigned int num_values_found = 0;
655 #define MAX_SUBKEY_LEN 257
657 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
659 LONG rc;
660 DWORD max_data_bytes = 2048, data_size;
661 DWORD subkey_len;
662 DWORD type, path_len, i;
663 BYTE *data;
664 WCHAR fmt[] = {'%','1','\n',0};
665 WCHAR newlineW[] = {'\n',0};
666 WCHAR *subkey_name, *subkey_path;
667 HKEY subkey;
669 data = heap_xalloc(max_data_bytes);
671 for (;;)
673 data_size = max_data_bytes;
674 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
675 if (rc == ERROR_MORE_DATA)
677 max_data_bytes = data_size;
678 data = heap_xrealloc(data, max_data_bytes);
680 else break;
683 if (rc == ERROR_SUCCESS)
685 output_string(fmt, path);
686 output_value(value_name, type, data, data_size);
687 output_string(newlineW);
688 num_values_found++;
691 heap_free(data);
693 if (!recurse)
695 if (rc == ERROR_FILE_NOT_FOUND)
697 if (value_name && *value_name)
699 output_message(STRING_CANNOT_FIND);
700 return 1;
702 output_string(fmt, path);
703 output_value(NULL, REG_SZ, NULL, 0);
705 return 0;
708 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
710 path_len = strlenW(path);
712 i = 0;
713 for (;;)
715 subkey_len = MAX_SUBKEY_LEN;
716 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
717 if (rc == ERROR_SUCCESS)
719 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
720 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
722 query_value(subkey, value_name, subkey_path, recurse);
723 RegCloseKey(subkey);
725 heap_free(subkey_path);
726 i++;
728 else break;
731 heap_free(subkey_name);
732 return 0;
735 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
737 LONG rc;
738 DWORD max_value_len = 256, value_len;
739 DWORD max_data_bytes = 2048, data_size;
740 DWORD subkey_len;
741 DWORD i, type, path_len;
742 WCHAR fmt[] = {'%','1','\n',0};
743 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
744 WCHAR *value_name, *subkey_name, *subkey_path;
745 WCHAR newlineW[] = {'\n',0};
746 BYTE *data;
747 HKEY subkey;
749 output_string(fmt, path);
751 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
752 data = heap_xalloc(max_data_bytes);
754 i = 0;
755 for (;;)
757 value_len = max_value_len;
758 data_size = max_data_bytes;
759 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
760 if (rc == ERROR_SUCCESS)
762 output_value(value_name, type, data, data_size);
763 i++;
765 else if (rc == ERROR_MORE_DATA)
767 if (data_size > max_data_bytes)
769 max_data_bytes = data_size;
770 data = heap_xrealloc(data, max_data_bytes);
772 else
774 max_value_len *= 2;
775 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
778 else break;
781 heap_free(data);
782 heap_free(value_name);
784 if (i || recurse)
785 output_string(newlineW);
787 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
789 path_len = strlenW(path);
791 i = 0;
792 for (;;)
794 subkey_len = MAX_SUBKEY_LEN;
795 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
796 if (rc == ERROR_SUCCESS)
798 if (recurse)
800 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
801 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
803 query_all(subkey, subkey_path, recurse);
804 RegCloseKey(subkey);
806 heap_free(subkey_path);
808 else output_string(fmt_path, path, subkey_name);
809 i++;
811 else break;
814 heap_free(subkey_name);
816 if (i && !recurse)
817 output_string(newlineW);
819 return 0;
822 static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
823 BOOL value_empty, BOOL recurse)
825 HKEY key;
826 WCHAR newlineW[] = {'\n',0};
827 int ret;
829 if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
831 output_message(STRING_CANNOT_FIND);
832 return 1;
835 output_string(newlineW);
837 if (value_name || value_empty)
839 ret = query_value(key, value_name, key_name, recurse);
840 if (recurse)
841 output_message(STRING_MATCHES_FOUND, num_values_found);
843 else
844 ret = query_all(key, key_name, recurse);
846 RegCloseKey(key);
848 return ret;
851 static WCHAR *get_long_key(HKEY root, WCHAR *path)
853 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
854 WCHAR *long_key;
855 WCHAR fmt[] = {'%','s','\\','%','s',0};
857 for (i = 0; i < array_size; i++)
859 if (root == root_rels[i].key)
860 break;
863 len = strlenW(root_rels[i].long_name);
865 if (!path)
867 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
868 strcpyW(long_key, root_rels[i].long_name);
869 return long_key;
872 len += strlenW(path) + 1; /* add one for the backslash */
873 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
874 sprintfW(long_key, fmt, root_rels[i].long_name, path);
875 return long_key;
878 static BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
880 if (!sane_path(key))
881 return FALSE;
883 *root = path_get_rootkey(key);
884 if (!*root)
886 output_message(STRING_INVALID_KEY);
887 return FALSE;
890 *path = strchrW(key, '\\');
891 if (*path) (*path)++;
893 *long_key = get_long_key(*root, *path);
895 return TRUE;
898 static BOOL is_help_switch(const WCHAR *s)
900 if (strlenW(s) > 2)
901 return FALSE;
903 if ((s[0] == '/' || s[0] == '-') && (s[1] == 'h' || s[1] == '?'))
904 return TRUE;
906 return FALSE;
909 enum operations {
910 REG_ADD,
911 REG_DELETE,
912 REG_QUERY,
913 REG_INVALID
916 static const WCHAR addW[] = {'a','d','d',0};
917 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
918 static const WCHAR queryW[] = {'q','u','e','r','y',0};
920 static enum operations get_operation(const WCHAR *str, int *op_help)
922 if (!lstrcmpiW(str, addW))
924 *op_help = STRING_ADD_USAGE;
925 return REG_ADD;
928 if (!lstrcmpiW(str, deleteW))
930 *op_help = STRING_DELETE_USAGE;
931 return REG_DELETE;
934 if (!lstrcmpiW(str, queryW))
936 *op_help = STRING_QUERY_USAGE;
937 return REG_QUERY;
940 return REG_INVALID;
943 int wmain(int argc, WCHAR *argvW[])
945 int i, op, op_help, ret;
946 BOOL show_op_help = FALSE;
947 static const WCHAR switchVAW[] = {'v','a',0};
948 static const WCHAR switchVEW[] = {'v','e',0};
949 WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
950 BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
951 HKEY root;
953 if (argc == 1)
955 output_message(STRING_INVALID_SYNTAX);
956 output_message(STRING_REG_HELP);
957 return 1;
960 if (is_help_switch(argvW[1]))
962 output_message(STRING_USAGE);
963 return 0;
966 op = get_operation(argvW[1], &op_help);
968 if (op == REG_INVALID)
970 output_message(STRING_INVALID_OPTION, argvW[1]);
971 output_message(STRING_REG_HELP);
972 return 1;
975 if (argc > 2)
976 show_op_help = is_help_switch(argvW[2]);
978 if (argc == 2 || (show_op_help && argc > 3))
980 output_message(STRING_INVALID_SYNTAX);
981 output_message(STRING_FUNC_HELP, struprW(argvW[1]));
982 return 1;
984 else if (show_op_help)
986 output_message(op_help);
987 return 0;
990 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
991 return 1;
993 for (i = 3; i < argc; i++)
995 if (argvW[i][0] == '/' || argvW[i][0] == '-')
997 WCHAR *ptr = &argvW[i][1];
999 if (!lstrcmpiW(ptr, switchVEW))
1001 value_empty = TRUE;
1002 continue;
1004 else if (!lstrcmpiW(ptr, switchVAW))
1006 value_all = TRUE;
1007 continue;
1009 else if (!ptr[0] || ptr[1])
1011 output_message(STRING_INVALID_CMDLINE);
1012 return 1;
1015 switch(tolowerW(argvW[i][1]))
1017 case 'v':
1018 if (value_name || !(value_name = argvW[++i]))
1020 output_message(STRING_INVALID_CMDLINE);
1021 return 1;
1023 break;
1024 case 't':
1025 if (type || !(type = argvW[++i]))
1027 output_message(STRING_INVALID_CMDLINE);
1028 return 1;
1030 break;
1031 case 'd':
1032 if (data || !(data = argvW[++i]))
1034 output_message(STRING_INVALID_CMDLINE);
1035 return 1;
1037 break;
1038 case 's':
1039 if (op == REG_QUERY)
1041 recurse = TRUE;
1042 break;
1045 ptr = argvW[++i];
1046 if (!ptr || strlenW(ptr) != 1)
1048 output_message(STRING_INVALID_CMDLINE);
1049 return 1;
1051 separator = ptr[0];
1052 break;
1053 case 'f':
1054 force = TRUE;
1055 break;
1056 default:
1057 output_message(STRING_INVALID_CMDLINE);
1058 return 1;
1063 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
1065 output_message(STRING_INVALID_CMDLINE);
1066 return 1;
1069 if (op == REG_ADD)
1070 ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
1071 else if (op == REG_DELETE)
1072 ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
1073 else
1074 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
1075 return ret;