ntdll: Move the keyed event functions to the Unix library.
[wine.git] / programs / reg / reg.c
blob997a6703831be7becfba9338d306cbecb012f6cb
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 <stdio.h>
22 #include <stdlib.h>
23 #include <wine/debug.h>
24 #include <wine/heap.h>
25 #include "reg.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(reg);
29 static const WCHAR short_hklm[] = {'H','K','L','M',0};
30 static const WCHAR short_hkcu[] = {'H','K','C','U',0};
31 static const WCHAR short_hkcr[] = {'H','K','C','R',0};
32 static const WCHAR short_hku[] = {'H','K','U',0};
33 static const WCHAR short_hkcc[] = {'H','K','C','C',0};
34 static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
35 static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
36 static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
37 static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
38 static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
40 static const struct
42 HKEY key;
43 const WCHAR *short_name;
44 const WCHAR *long_name;
46 root_rels[] =
48 {HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
49 {HKEY_CURRENT_USER, short_hkcu, long_hkcu},
50 {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
51 {HKEY_USERS, short_hku, long_hku},
52 {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
55 static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
56 static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
57 static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
58 static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
59 static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
60 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};
61 static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
62 static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
64 static const struct
66 DWORD type;
67 const WCHAR *name;
69 type_rels[] =
71 {REG_NONE, type_none},
72 {REG_SZ, type_sz},
73 {REG_EXPAND_SZ, type_expand_sz},
74 {REG_BINARY, type_binary},
75 {REG_DWORD, type_dword},
76 {REG_DWORD_LITTLE_ENDIAN, type_dword_le},
77 {REG_DWORD_BIG_ENDIAN, type_dword_be},
78 {REG_MULTI_SZ, type_multi_sz},
81 static const WCHAR newlineW[] = {'\n',0};
83 void *heap_xalloc(size_t size)
85 void *buf = heap_alloc(size);
86 if (!buf)
88 ERR("Out of memory!\n");
89 exit(1);
91 return buf;
94 void *heap_xrealloc(void *buf, size_t size)
96 void *new_buf = heap_realloc(buf, size);
98 if (!new_buf)
100 ERR("Out of memory!\n");
101 exit(1);
104 return new_buf;
107 void output_writeconsole(const WCHAR *str, DWORD wlen)
109 DWORD count, ret;
111 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
112 if (!ret)
114 DWORD len;
115 char *msgA;
117 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
118 * back to WriteFile(), assuming the console encoding is still the right
119 * one in that case.
121 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
122 msgA = heap_xalloc(len);
124 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
125 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
126 heap_free(msgA);
130 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
132 WCHAR *str;
133 DWORD len;
135 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
136 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
137 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
139 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
140 return;
142 output_writeconsole(str, len);
143 LocalFree(str);
146 void WINAPIV output_message(unsigned int id, ...)
148 WCHAR fmt[1024];
149 __ms_va_list va_args;
151 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
153 WINE_FIXME("LoadString failed with %d\n", GetLastError());
154 return;
156 __ms_va_start(va_args, id);
157 output_formatstring(fmt, va_args);
158 __ms_va_end(va_args);
161 static void WINAPIV output_string(const WCHAR *fmt, ...)
163 __ms_va_list va_args;
165 __ms_va_start(va_args, fmt);
166 output_formatstring(fmt, va_args);
167 __ms_va_end(va_args);
170 /* ask_confirm() adapted from programs/cmd/builtins.c */
171 BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
173 HMODULE hmod;
174 WCHAR Ybuffer[4];
175 WCHAR Nbuffer[4];
176 WCHAR defval[32];
177 WCHAR answer[MAX_PATH];
178 WCHAR *str;
179 DWORD count;
181 hmod = GetModuleHandleW(NULL);
182 LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
183 LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
184 LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
186 str = (reg_info && *reg_info) ? reg_info : defval;
188 while (1)
190 output_message(msgid, str);
191 output_message(STRING_YESNO);
192 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
193 answer[0] = towupper(answer[0]);
194 if (answer[0] == Ybuffer[0])
195 return TRUE;
196 if (answer[0] == Nbuffer[0])
197 return FALSE;
201 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
203 DWORD length = lstrlenW(rootkey_name);
205 return (!wcsnicmp(input_path, rootkey_name, length) &&
206 (input_path[length] == 0 || input_path[length] == '\\'));
209 HKEY path_get_rootkey(const WCHAR *path)
211 DWORD i;
213 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
215 if (path_rootname_cmp(path, root_rels[i].short_name) ||
216 path_rootname_cmp(path, root_rels[i].long_name))
217 return root_rels[i].key;
220 return NULL;
223 static DWORD wchar_get_type(const WCHAR *type_name)
225 DWORD i;
227 if (!type_name)
228 return REG_SZ;
230 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
232 if (!wcsicmp(type_rels[i].name, type_name))
233 return type_rels[i].type;
236 return ~0u;
239 /* hexchar_to_byte from programs/regedit/hexedit.c */
240 static inline BYTE hexchar_to_byte(WCHAR ch)
242 if (ch >= '0' && ch <= '9')
243 return ch - '0';
244 else if (ch >= 'a' && ch <= 'f')
245 return ch - 'a' + 10;
246 else if (ch >= 'A' && ch <= 'F')
247 return ch - 'A' + 10;
248 else
249 return -1;
252 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
254 static const WCHAR empty;
255 LPBYTE out_data = NULL;
256 *reg_count = 0;
258 if (!data) data = &empty;
260 switch (reg_type)
262 case REG_NONE:
263 case REG_SZ:
264 case REG_EXPAND_SZ:
266 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
267 out_data = heap_xalloc(*reg_count);
268 lstrcpyW((LPWSTR)out_data,data);
269 break;
271 case REG_DWORD:
272 /* case REG_DWORD_LITTLE_ENDIAN: */
273 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
275 LPWSTR rest;
276 unsigned long val;
277 val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
278 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
279 output_message(STRING_MISSING_INTEGER);
280 break;
282 *reg_count = sizeof(DWORD);
283 out_data = heap_xalloc(*reg_count);
284 ((LPDWORD)out_data)[0] = val;
285 break;
287 case REG_BINARY:
289 BYTE hex0, hex1;
290 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
291 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
292 out_data = heap_xalloc(*reg_count);
293 if(datalen % 2)
295 hex1 = hexchar_to_byte(data[i++]);
296 if(hex1 == 0xFF)
297 goto no_hex_data;
298 out_data[destByteIndex++] = hex1;
300 for(;i + 1 < datalen;i += 2)
302 hex0 = hexchar_to_byte(data[i]);
303 hex1 = hexchar_to_byte(data[i + 1]);
304 if(hex0 == 0xFF || hex1 == 0xFF)
305 goto no_hex_data;
306 out_data[destByteIndex++] = (hex0 << 4) | hex1;
308 break;
309 no_hex_data:
310 /* cleanup, print error */
311 heap_free(out_data);
312 output_message(STRING_MISSING_HEXDATA);
313 out_data = NULL;
314 break;
316 case REG_MULTI_SZ:
318 int i, destindex, len = lstrlenW(data);
319 WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
321 for (i = 0, destindex = 0; i < len; i++, destindex++)
323 if (!separator && data[i] == '\\' && data[i + 1] == '0')
325 buffer[destindex] = 0;
326 i++;
328 else if (data[i] == separator)
329 buffer[destindex] = 0;
330 else
331 buffer[destindex] = data[i];
333 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
335 heap_free(buffer);
336 output_message(STRING_INVALID_STRING);
337 return NULL;
340 buffer[destindex] = 0;
341 if (destindex && buffer[destindex - 1])
342 buffer[++destindex] = 0;
343 *reg_count = (destindex + 1) * sizeof(WCHAR);
344 return (BYTE *)buffer;
346 default:
347 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
350 return out_data;
353 static BOOL sane_path(const WCHAR *key)
355 unsigned int i = lstrlenW(key);
357 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
359 output_message(STRING_INVALID_KEY);
360 return FALSE;
363 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
365 output_message(STRING_NO_REMOTE);
366 return FALSE;
369 return TRUE;
372 static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
373 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
375 HKEY key;
377 if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
379 output_message(STRING_INVALID_KEY);
380 return 1;
383 if (value_name || value_empty || data)
385 DWORD reg_type;
386 DWORD reg_count = 0;
387 BYTE* reg_data = NULL;
389 if (!force)
391 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
393 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
395 RegCloseKey(key);
396 output_message(STRING_CANCELLED);
397 return 0;
402 reg_type = wchar_get_type(type);
403 if (reg_type == ~0u)
405 RegCloseKey(key);
406 output_message(STRING_UNSUPPORTED_TYPE, type);
407 return 1;
409 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
411 RegCloseKey(key);
412 output_message(STRING_INVALID_CMDLINE);
413 return 1;
416 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
418 RegCloseKey(key);
419 return 1;
422 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
423 heap_free(reg_data);
426 RegCloseKey(key);
427 output_message(STRING_SUCCESS);
429 return 0;
432 static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
433 BOOL value_empty, BOOL value_all, BOOL force)
435 HKEY key;
437 if (!force)
439 BOOL ret;
441 if (value_name || value_empty)
442 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
443 else if (value_all)
444 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
445 else
446 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
448 if (!ret)
450 output_message(STRING_CANCELLED);
451 return 0;
455 /* Delete subtree only if no /v* option is given */
456 if (!value_name && !value_empty && !value_all)
458 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
460 output_message(STRING_CANNOT_FIND);
461 return 1;
463 output_message(STRING_SUCCESS);
464 return 0;
467 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
469 output_message(STRING_CANNOT_FIND);
470 return 1;
473 if (value_all)
475 DWORD max_value_len = 256, value_len;
476 WCHAR *value_name;
477 LONG rc;
479 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
481 while (1)
483 value_len = max_value_len;
484 rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
485 if (rc == ERROR_SUCCESS)
487 rc = RegDeleteValueW(key, value_name);
488 if (rc != ERROR_SUCCESS)
490 heap_free(value_name);
491 RegCloseKey(key);
492 output_message(STRING_VALUEALL_FAILED, key_name);
493 return 1;
496 else if (rc == ERROR_MORE_DATA)
498 max_value_len *= 2;
499 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
501 else break;
503 heap_free(value_name);
505 else if (value_name || value_empty)
507 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
509 RegCloseKey(key);
510 output_message(STRING_CANNOT_FIND);
511 return 1;
515 RegCloseKey(key);
516 output_message(STRING_SUCCESS);
517 return 0;
520 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
522 WCHAR *buffer = NULL;
523 int i;
525 switch (type)
527 case REG_SZ:
528 case REG_EXPAND_SZ:
529 buffer = heap_xalloc(size_bytes);
530 lstrcpyW(buffer, (WCHAR *)src);
531 break;
532 case REG_NONE:
533 case REG_BINARY:
535 WCHAR *ptr;
536 static const WCHAR fmt[] = {'%','0','2','X',0};
538 buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
539 ptr = buffer;
540 for (i = 0; i < size_bytes; i++)
541 ptr += swprintf(ptr, 3, fmt, src[i]);
542 break;
544 case REG_DWORD:
545 /* case REG_DWORD_LITTLE_ENDIAN: */
546 case REG_DWORD_BIG_ENDIAN:
548 const int zero_x_dword = 10;
549 static const WCHAR fmt[] = {'0','x','%','x',0};
551 buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
552 swprintf(buffer, zero_x_dword + 1, fmt, *(DWORD *)src);
553 break;
555 case REG_MULTI_SZ:
557 const int two_wchars = 2 * sizeof(WCHAR);
558 DWORD tmp_size;
559 const WCHAR *tmp = (const WCHAR *)src;
560 int len, destindex;
562 if (size_bytes <= two_wchars)
564 buffer = heap_xalloc(sizeof(WCHAR));
565 *buffer = 0;
566 return buffer;
569 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
570 buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
571 len = tmp_size / sizeof(WCHAR);
573 for (i = 0, destindex = 0; i < len; i++, destindex++)
575 if (tmp[i])
576 buffer[destindex] = tmp[i];
577 else
579 buffer[destindex++] = '\\';
580 buffer[destindex] = '0';
583 buffer[destindex] = 0;
584 break;
587 return buffer;
590 static const WCHAR *reg_type_to_wchar(DWORD type)
592 int i, array_size = ARRAY_SIZE(type_rels);
594 for (i = 0; i < array_size; i++)
596 if (type == type_rels[i].type)
597 return type_rels[i].name;
599 return NULL;
602 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
604 static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
605 WCHAR defval[32];
606 WCHAR *reg_data;
608 if (value_name && value_name[0])
609 output_string(fmt, value_name);
610 else
612 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
613 output_string(fmt, defval);
615 output_string(fmt, reg_type_to_wchar(type));
617 if (data)
619 reg_data = reg_data_to_wchar(type, data, data_size);
620 output_string(fmt, reg_data);
621 heap_free(reg_data);
623 else
625 LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
626 output_string(fmt, defval);
628 output_string(newlineW);
631 WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
633 WCHAR *subkey_path;
634 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
636 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
637 swprintf(subkey_path, path_len + subkey_len + 2, fmt, path, subkey_name);
639 return subkey_path;
642 static unsigned int num_values_found = 0;
644 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
646 LONG rc;
647 DWORD max_data_bytes = 2048, data_size;
648 DWORD subkey_len;
649 DWORD type, path_len, i;
650 BYTE *data;
651 WCHAR fmt[] = {'%','1','\n',0};
652 WCHAR *subkey_name, *subkey_path;
653 HKEY subkey;
655 data = heap_xalloc(max_data_bytes);
657 for (;;)
659 data_size = max_data_bytes;
660 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
661 if (rc == ERROR_MORE_DATA)
663 max_data_bytes = data_size;
664 data = heap_xrealloc(data, max_data_bytes);
666 else break;
669 if (rc == ERROR_SUCCESS)
671 output_string(fmt, path);
672 output_value(value_name, type, data, data_size);
673 output_string(newlineW);
674 num_values_found++;
677 heap_free(data);
679 if (!recurse)
681 if (rc == ERROR_FILE_NOT_FOUND)
683 if (value_name && *value_name)
685 output_message(STRING_CANNOT_FIND);
686 return 1;
688 output_string(fmt, path);
689 output_value(NULL, REG_SZ, NULL, 0);
691 return 0;
694 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
696 path_len = lstrlenW(path);
698 i = 0;
699 for (;;)
701 subkey_len = MAX_SUBKEY_LEN;
702 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
703 if (rc == ERROR_SUCCESS)
705 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
706 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
708 query_value(subkey, value_name, subkey_path, recurse);
709 RegCloseKey(subkey);
711 heap_free(subkey_path);
712 i++;
714 else break;
717 heap_free(subkey_name);
718 return 0;
721 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
723 LONG rc;
724 DWORD max_value_len = 256, value_len;
725 DWORD max_data_bytes = 2048, data_size;
726 DWORD subkey_len;
727 DWORD i, type, path_len;
728 WCHAR fmt[] = {'%','1','\n',0};
729 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
730 WCHAR *value_name, *subkey_name, *subkey_path;
731 BYTE *data;
732 HKEY subkey;
734 output_string(fmt, path);
736 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
737 data = heap_xalloc(max_data_bytes);
739 i = 0;
740 for (;;)
742 value_len = max_value_len;
743 data_size = max_data_bytes;
744 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
745 if (rc == ERROR_SUCCESS)
747 output_value(value_name, type, data, data_size);
748 i++;
750 else if (rc == ERROR_MORE_DATA)
752 if (data_size > max_data_bytes)
754 max_data_bytes = data_size;
755 data = heap_xrealloc(data, max_data_bytes);
757 else
759 max_value_len *= 2;
760 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
763 else break;
766 heap_free(data);
767 heap_free(value_name);
769 if (i || recurse)
770 output_string(newlineW);
772 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
774 path_len = lstrlenW(path);
776 i = 0;
777 for (;;)
779 subkey_len = MAX_SUBKEY_LEN;
780 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
781 if (rc == ERROR_SUCCESS)
783 if (recurse)
785 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
786 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
788 query_all(subkey, subkey_path, recurse);
789 RegCloseKey(subkey);
791 heap_free(subkey_path);
793 else output_string(fmt_path, path, subkey_name);
794 i++;
796 else break;
799 heap_free(subkey_name);
801 if (i && !recurse)
802 output_string(newlineW);
804 return 0;
807 static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
808 BOOL value_empty, BOOL recurse)
810 HKEY key;
811 int ret;
813 if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
815 output_message(STRING_CANNOT_FIND);
816 return 1;
819 output_string(newlineW);
821 if (value_name || value_empty)
823 ret = query_value(key, value_name, key_name, recurse);
824 if (recurse)
825 output_message(STRING_MATCHES_FOUND, num_values_found);
827 else
828 ret = query_all(key, key_name, recurse);
830 RegCloseKey(key);
832 return ret;
835 static WCHAR *get_long_key(HKEY root, WCHAR *path)
837 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
838 WCHAR *long_key;
839 WCHAR fmt[] = {'%','s','\\','%','s',0};
841 for (i = 0; i < array_size; i++)
843 if (root == root_rels[i].key)
844 break;
847 len = lstrlenW(root_rels[i].long_name);
849 if (!path)
851 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
852 lstrcpyW(long_key, root_rels[i].long_name);
853 return long_key;
856 len += lstrlenW(path) + 1; /* add one for the backslash */
857 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
858 swprintf(long_key, len + 1, fmt, root_rels[i].long_name, path);
859 return long_key;
862 BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
864 if (!sane_path(key))
865 return FALSE;
867 *path = wcschr(key, '\\');
868 if (*path) (*path)++;
870 *root = path_get_rootkey(key);
871 if (!*root)
873 if (*path) *(*path - 1) = 0;
874 output_message(STRING_INVALID_SYSTEM_KEY, key);
875 return FALSE;
878 *long_key = get_long_key(*root, *path);
880 return TRUE;
883 static BOOL is_switch(const WCHAR *s, const WCHAR c)
885 if (lstrlenW(s) > 2)
886 return FALSE;
888 if ((s[0] == '/' || s[0] == '-') && (s[1] == c || s[1] == towupper(c)))
889 return TRUE;
891 return FALSE;
894 static BOOL is_help_switch(const WCHAR *s)
896 if (is_switch(s, '?') || is_switch(s, 'h'))
897 return TRUE;
899 return FALSE;
902 enum operations {
903 REG_ADD,
904 REG_DELETE,
905 REG_IMPORT,
906 REG_EXPORT,
907 REG_QUERY,
908 REG_INVALID
911 static enum operations get_operation(const WCHAR *str, int *op_help)
913 struct op_info { const WCHAR *op; int id; int help_id; };
915 static const WCHAR add[] = {'a','d','d',0};
916 static const WCHAR delete[] = {'d','e','l','e','t','e',0};
917 static const WCHAR import[] = {'i','m','p','o','r','t',0};
918 static const WCHAR export[] = {'e','x','p','o','r','t',0};
919 static const WCHAR query[] = {'q','u','e','r','y',0};
921 static const struct op_info op_array[] =
923 { add, REG_ADD, STRING_ADD_USAGE },
924 { delete, REG_DELETE, STRING_DELETE_USAGE },
925 { import, REG_IMPORT, STRING_IMPORT_USAGE },
926 { export, REG_EXPORT, STRING_EXPORT_USAGE },
927 { query, REG_QUERY, STRING_QUERY_USAGE },
928 { NULL, -1, 0 }
931 const struct op_info *ptr;
933 for (ptr = op_array; ptr->op; ptr++)
935 if (!lstrcmpiW(str, ptr->op))
937 *op_help = ptr->help_id;
938 return ptr->id;
942 return REG_INVALID;
945 int __cdecl wmain(int argc, WCHAR *argvW[])
947 int i, op, op_help, ret;
948 BOOL show_op_help = FALSE;
949 static const WCHAR switchVAW[] = {'v','a',0};
950 static const WCHAR switchVEW[] = {'v','e',0};
951 WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
952 BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
953 HKEY root;
955 if (argc == 1)
957 output_message(STRING_INVALID_SYNTAX);
958 output_message(STRING_REG_HELP);
959 return 1;
962 if (is_help_switch(argvW[1]))
964 output_message(STRING_USAGE);
965 return 0;
968 op = get_operation(argvW[1], &op_help);
970 if (op == REG_INVALID)
972 output_message(STRING_INVALID_OPTION, argvW[1]);
973 output_message(STRING_REG_HELP);
974 return 1;
977 if (argc > 2)
978 show_op_help = is_help_switch(argvW[2]);
980 if (argc == 2 || ((show_op_help || op == REG_IMPORT) && argc > 3))
982 output_message(STRING_INVALID_SYNTAX);
983 output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
984 return 1;
986 else if (show_op_help)
988 output_message(op_help);
989 return 0;
992 if (op == REG_IMPORT)
993 return reg_import(argvW[2]);
995 if (op == REG_EXPORT)
996 return reg_export(argc, argvW);
998 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
999 return 1;
1001 for (i = 3; i < argc; i++)
1003 if (argvW[i][0] == '/' || argvW[i][0] == '-')
1005 WCHAR *ptr = &argvW[i][1];
1007 if (!lstrcmpiW(ptr, switchVEW))
1009 value_empty = TRUE;
1010 continue;
1012 else if (!lstrcmpiW(ptr, switchVAW))
1014 value_all = TRUE;
1015 continue;
1017 else if (!ptr[0] || ptr[1])
1019 output_message(STRING_INVALID_CMDLINE);
1020 return 1;
1023 switch(towlower(argvW[i][1]))
1025 case 'v':
1026 if (value_name || !(value_name = argvW[++i]))
1028 output_message(STRING_INVALID_CMDLINE);
1029 return 1;
1031 break;
1032 case 't':
1033 if (type || !(type = argvW[++i]))
1035 output_message(STRING_INVALID_CMDLINE);
1036 return 1;
1038 break;
1039 case 'd':
1040 if (data || !(data = argvW[++i]))
1042 output_message(STRING_INVALID_CMDLINE);
1043 return 1;
1045 break;
1046 case 's':
1047 if (op == REG_QUERY)
1049 recurse = TRUE;
1050 break;
1053 ptr = argvW[++i];
1054 if (!ptr || lstrlenW(ptr) != 1)
1056 output_message(STRING_INVALID_CMDLINE);
1057 return 1;
1059 separator = ptr[0];
1060 break;
1061 case 'f':
1062 force = TRUE;
1063 break;
1064 default:
1065 output_message(STRING_INVALID_CMDLINE);
1066 return 1;
1071 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
1073 output_message(STRING_INVALID_CMDLINE);
1074 return 1;
1077 if (op == REG_ADD)
1078 ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
1079 else if (op == REG_DELETE)
1080 ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
1081 else
1082 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
1083 return ret;