reg: Use WINAPIV calling convention for variadic functions.
[wine.git] / programs / reg / reg.c
blob42fd6e9daaf35451c7ca0f96a44cf1ad76c69c0e
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 "reg.h"
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 void *heap_xalloc(size_t size)
82 void *buf = HeapAlloc(GetProcessHeap(), 0, size);
83 if (!buf)
85 ERR("Out of memory!\n");
86 exit(1);
88 return buf;
91 void *heap_xrealloc(void *buf, size_t size)
93 void *new_buf;
95 if (buf)
96 new_buf = HeapReAlloc(GetProcessHeap(), 0, buf, size);
97 else
98 new_buf = HeapAlloc(GetProcessHeap(), 0, size);
100 if (!new_buf)
102 ERR("Out of memory!\n");
103 exit(1);
106 return new_buf;
109 BOOL heap_free(void *buf)
111 return HeapFree(GetProcessHeap(), 0, buf);
114 static void output_writeconsole(const WCHAR *str, DWORD wlen)
116 DWORD count, ret;
118 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
119 if (!ret)
121 DWORD len;
122 char *msgA;
124 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
125 * back to WriteFile(), assuming the console encoding is still the right
126 * one in that case.
128 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
129 msgA = heap_xalloc(len);
131 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
132 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
133 heap_free(msgA);
137 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
139 WCHAR *str;
140 DWORD len;
142 SetLastError(NO_ERROR);
143 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
144 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
145 if (len == 0 && GetLastError() != NO_ERROR)
147 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
148 return;
150 output_writeconsole(str, len);
151 LocalFree(str);
154 void WINAPIV output_message(unsigned int id, ...)
156 WCHAR fmt[1024];
157 __ms_va_list va_args;
159 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
161 WINE_FIXME("LoadString failed with %d\n", GetLastError());
162 return;
164 __ms_va_start(va_args, id);
165 output_formatstring(fmt, va_args);
166 __ms_va_end(va_args);
169 static void WINAPIV output_string(const WCHAR *fmt, ...)
171 __ms_va_list va_args;
173 __ms_va_start(va_args, fmt);
174 output_formatstring(fmt, va_args);
175 __ms_va_end(va_args);
178 /* ask_confirm() adapted from programs/cmd/builtins.c */
179 static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
181 HMODULE hmod;
182 WCHAR Ybuffer[4];
183 WCHAR Nbuffer[4];
184 WCHAR defval[32];
185 WCHAR answer[MAX_PATH];
186 WCHAR *str;
187 DWORD count;
189 hmod = GetModuleHandleW(NULL);
190 LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
191 LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
192 LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
194 str = (reg_info && *reg_info) ? reg_info : defval;
196 while (1)
198 output_message(msgid, str);
199 output_message(STRING_YESNO);
200 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
201 answer[0] = toupperW(answer[0]);
202 if (answer[0] == Ybuffer[0])
203 return TRUE;
204 if (answer[0] == Nbuffer[0])
205 return FALSE;
209 static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
211 DWORD length = strlenW(rootkey_name);
213 return (!strncmpiW(input_path, rootkey_name, length) &&
214 (input_path[length] == 0 || input_path[length] == '\\'));
217 HKEY path_get_rootkey(const WCHAR *path)
219 DWORD i;
221 for (i = 0; i < ARRAY_SIZE(root_rels); i++)
223 if (path_rootname_cmp(path, root_rels[i].short_name) ||
224 path_rootname_cmp(path, root_rels[i].long_name))
225 return root_rels[i].key;
228 return NULL;
231 static DWORD wchar_get_type(const WCHAR *type_name)
233 DWORD i;
235 if (!type_name)
236 return REG_SZ;
238 for (i = 0; i < ARRAY_SIZE(type_rels); i++)
240 if (!strcmpiW(type_rels[i].name, type_name))
241 return type_rels[i].type;
244 return ~0u;
247 /* hexchar_to_byte from programs/regedit/hexedit.c */
248 static inline BYTE hexchar_to_byte(WCHAR ch)
250 if (ch >= '0' && ch <= '9')
251 return ch - '0';
252 else if (ch >= 'a' && ch <= 'f')
253 return ch - 'a' + 10;
254 else if (ch >= 'A' && ch <= 'F')
255 return ch - 'A' + 10;
256 else
257 return -1;
260 static LPBYTE get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
262 static const WCHAR empty;
263 LPBYTE out_data = NULL;
264 *reg_count = 0;
266 if (!data) data = &empty;
268 switch (reg_type)
270 case REG_NONE:
271 case REG_SZ:
272 case REG_EXPAND_SZ:
274 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
275 out_data = heap_xalloc(*reg_count);
276 lstrcpyW((LPWSTR)out_data,data);
277 break;
279 case REG_DWORD:
280 /* case REG_DWORD_LITTLE_ENDIAN: */
281 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
283 LPWSTR rest;
284 unsigned long val;
285 val = wcstoul(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
286 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
287 output_message(STRING_MISSING_INTEGER);
288 break;
290 *reg_count = sizeof(DWORD);
291 out_data = heap_xalloc(*reg_count);
292 ((LPDWORD)out_data)[0] = val;
293 break;
295 case REG_BINARY:
297 BYTE hex0, hex1;
298 int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
299 *reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
300 out_data = heap_xalloc(*reg_count);
301 if(datalen % 2)
303 hex1 = hexchar_to_byte(data[i++]);
304 if(hex1 == 0xFF)
305 goto no_hex_data;
306 out_data[destByteIndex++] = hex1;
308 for(;i + 1 < datalen;i += 2)
310 hex0 = hexchar_to_byte(data[i]);
311 hex1 = hexchar_to_byte(data[i + 1]);
312 if(hex0 == 0xFF || hex1 == 0xFF)
313 goto no_hex_data;
314 out_data[destByteIndex++] = (hex0 << 4) | hex1;
316 break;
317 no_hex_data:
318 /* cleanup, print error */
319 heap_free(out_data);
320 output_message(STRING_MISSING_HEXDATA);
321 out_data = NULL;
322 break;
324 case REG_MULTI_SZ:
326 int i, destindex, len = strlenW(data);
327 WCHAR *buffer = heap_xalloc((len + 2) * sizeof(WCHAR));
329 for (i = 0, destindex = 0; i < len; i++, destindex++)
331 if (!separator && data[i] == '\\' && data[i + 1] == '0')
333 buffer[destindex] = 0;
334 i++;
336 else if (data[i] == separator)
337 buffer[destindex] = 0;
338 else
339 buffer[destindex] = data[i];
341 if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
343 heap_free(buffer);
344 output_message(STRING_INVALID_STRING);
345 return NULL;
348 buffer[destindex] = 0;
349 if (destindex && buffer[destindex - 1])
350 buffer[++destindex] = 0;
351 *reg_count = (destindex + 1) * sizeof(WCHAR);
352 return (BYTE *)buffer;
354 default:
355 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
358 return out_data;
361 static BOOL sane_path(const WCHAR *key)
363 unsigned int i = strlenW(key);
365 if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
367 output_message(STRING_INVALID_KEY);
368 return FALSE;
371 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
373 output_message(STRING_NO_REMOTE);
374 return FALSE;
377 return TRUE;
380 static int reg_add(HKEY root, WCHAR *path, WCHAR *value_name, BOOL value_empty,
381 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
383 HKEY key;
385 if (RegCreateKeyW(root, path, &key) != ERROR_SUCCESS)
387 output_message(STRING_INVALID_KEY);
388 return 1;
391 if (value_name || value_empty || data)
393 DWORD reg_type;
394 DWORD reg_count = 0;
395 BYTE* reg_data = NULL;
397 if (!force)
399 if (RegQueryValueExW(key, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
401 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
403 RegCloseKey(key);
404 output_message(STRING_CANCELLED);
405 return 0;
410 reg_type = wchar_get_type(type);
411 if (reg_type == ~0u)
413 RegCloseKey(key);
414 output_message(STRING_UNSUPPORTED_TYPE, type);
415 return 1;
417 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
419 RegCloseKey(key);
420 output_message(STRING_INVALID_CMDLINE);
421 return 1;
424 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
426 RegCloseKey(key);
427 return 1;
430 RegSetValueExW(key, value_name, 0, reg_type, reg_data, reg_count);
431 heap_free(reg_data);
434 RegCloseKey(key);
435 output_message(STRING_SUCCESS);
437 return 0;
440 static int reg_delete(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
441 BOOL value_empty, BOOL value_all, BOOL force)
443 HKEY key;
445 if (!force)
447 BOOL ret;
449 if (value_name || value_empty)
450 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
451 else if (value_all)
452 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
453 else
454 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
456 if (!ret)
458 output_message(STRING_CANCELLED);
459 return 0;
463 /* Delete subtree only if no /v* option is given */
464 if (!value_name && !value_empty && !value_all)
466 if (RegDeleteTreeW(root, path) != ERROR_SUCCESS)
468 output_message(STRING_CANNOT_FIND);
469 return 1;
471 output_message(STRING_SUCCESS);
472 return 0;
475 if (RegOpenKeyW(root, path, &key) != ERROR_SUCCESS)
477 output_message(STRING_CANNOT_FIND);
478 return 1;
481 if (value_all)
483 DWORD max_value_len = 256, value_len;
484 WCHAR *value_name;
485 LONG rc;
487 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
489 while (1)
491 value_len = max_value_len;
492 rc = RegEnumValueW(key, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
493 if (rc == ERROR_SUCCESS)
495 rc = RegDeleteValueW(key, value_name);
496 if (rc != ERROR_SUCCESS)
498 heap_free(value_name);
499 RegCloseKey(key);
500 output_message(STRING_VALUEALL_FAILED, key_name);
501 return 1;
504 else if (rc == ERROR_MORE_DATA)
506 max_value_len *= 2;
507 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
509 else break;
511 heap_free(value_name);
513 else if (value_name || value_empty)
515 if (RegDeleteValueW(key, value_empty ? NULL : value_name) != ERROR_SUCCESS)
517 RegCloseKey(key);
518 output_message(STRING_CANNOT_FIND);
519 return 1;
523 RegCloseKey(key);
524 output_message(STRING_SUCCESS);
525 return 0;
528 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
530 WCHAR *buffer = NULL;
531 int i;
533 switch (type)
535 case REG_SZ:
536 case REG_EXPAND_SZ:
537 buffer = heap_xalloc(size_bytes);
538 strcpyW(buffer, (WCHAR *)src);
539 break;
540 case REG_NONE:
541 case REG_BINARY:
543 WCHAR *ptr;
544 static const WCHAR fmt[] = {'%','0','2','X',0};
546 buffer = heap_xalloc((size_bytes * 2 + 1) * sizeof(WCHAR));
547 ptr = buffer;
548 for (i = 0; i < size_bytes; i++)
549 ptr += sprintfW(ptr, fmt, src[i]);
550 break;
552 case REG_DWORD:
553 /* case REG_DWORD_LITTLE_ENDIAN: */
554 case REG_DWORD_BIG_ENDIAN:
556 const int zero_x_dword = 10;
557 static const WCHAR fmt[] = {'0','x','%','x',0};
559 buffer = heap_xalloc((zero_x_dword + 1) * sizeof(WCHAR));
560 sprintfW(buffer, fmt, *(DWORD *)src);
561 break;
563 case REG_MULTI_SZ:
565 const int two_wchars = 2 * sizeof(WCHAR);
566 DWORD tmp_size;
567 const WCHAR *tmp = (const WCHAR *)src;
568 int len, destindex;
570 if (size_bytes <= two_wchars)
572 buffer = heap_xalloc(sizeof(WCHAR));
573 *buffer = 0;
574 return buffer;
577 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
578 buffer = heap_xalloc(tmp_size * 2 + sizeof(WCHAR));
579 len = tmp_size / sizeof(WCHAR);
581 for (i = 0, destindex = 0; i < len; i++, destindex++)
583 if (tmp[i])
584 buffer[destindex] = tmp[i];
585 else
587 buffer[destindex++] = '\\';
588 buffer[destindex] = '0';
591 buffer[destindex] = 0;
592 break;
595 return buffer;
598 static const WCHAR *reg_type_to_wchar(DWORD type)
600 int i, array_size = ARRAY_SIZE(type_rels);
602 for (i = 0; i < array_size; i++)
604 if (type == type_rels[i].type)
605 return type_rels[i].name;
607 return NULL;
610 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
612 static const WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
613 static const WCHAR newlineW[] = {'\n',0};
614 WCHAR defval[32];
615 WCHAR *reg_data;
617 if (value_name && value_name[0])
618 output_string(fmt, value_name);
619 else
621 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
622 output_string(fmt, defval);
624 output_string(fmt, reg_type_to_wchar(type));
626 if (data)
628 reg_data = reg_data_to_wchar(type, data, data_size);
629 output_string(fmt, reg_data);
630 heap_free(reg_data);
632 else
634 LoadStringW(GetModuleHandleW(NULL), STRING_VALUE_NOT_SET, defval, ARRAY_SIZE(defval));
635 output_string(fmt, defval);
637 output_string(newlineW);
640 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
642 WCHAR *subkey_path;
643 static const WCHAR fmt[] = {'%','s','\\','%','s',0};
645 subkey_path = heap_xalloc((path_len + subkey_len + 2) * sizeof(WCHAR));
646 sprintfW(subkey_path, fmt, path, subkey_name);
648 return subkey_path;
651 static unsigned int num_values_found = 0;
653 #define MAX_SUBKEY_LEN 257
655 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
657 LONG rc;
658 DWORD max_data_bytes = 2048, data_size;
659 DWORD subkey_len;
660 DWORD type, path_len, i;
661 BYTE *data;
662 WCHAR fmt[] = {'%','1','\n',0};
663 WCHAR newlineW[] = {'\n',0};
664 WCHAR *subkey_name, *subkey_path;
665 HKEY subkey;
667 data = heap_xalloc(max_data_bytes);
669 for (;;)
671 data_size = max_data_bytes;
672 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
673 if (rc == ERROR_MORE_DATA)
675 max_data_bytes = data_size;
676 data = heap_xrealloc(data, max_data_bytes);
678 else break;
681 if (rc == ERROR_SUCCESS)
683 output_string(fmt, path);
684 output_value(value_name, type, data, data_size);
685 output_string(newlineW);
686 num_values_found++;
689 heap_free(data);
691 if (!recurse)
693 if (rc == ERROR_FILE_NOT_FOUND)
695 if (value_name && *value_name)
697 output_message(STRING_CANNOT_FIND);
698 return 1;
700 output_string(fmt, path);
701 output_value(NULL, REG_SZ, NULL, 0);
703 return 0;
706 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
708 path_len = strlenW(path);
710 i = 0;
711 for (;;)
713 subkey_len = MAX_SUBKEY_LEN;
714 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
715 if (rc == ERROR_SUCCESS)
717 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
718 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
720 query_value(subkey, value_name, subkey_path, recurse);
721 RegCloseKey(subkey);
723 heap_free(subkey_path);
724 i++;
726 else break;
729 heap_free(subkey_name);
730 return 0;
733 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
735 LONG rc;
736 DWORD max_value_len = 256, value_len;
737 DWORD max_data_bytes = 2048, data_size;
738 DWORD subkey_len;
739 DWORD i, type, path_len;
740 WCHAR fmt[] = {'%','1','\n',0};
741 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
742 WCHAR *value_name, *subkey_name, *subkey_path;
743 WCHAR newlineW[] = {'\n',0};
744 BYTE *data;
745 HKEY subkey;
747 output_string(fmt, path);
749 value_name = heap_xalloc(max_value_len * sizeof(WCHAR));
750 data = heap_xalloc(max_data_bytes);
752 i = 0;
753 for (;;)
755 value_len = max_value_len;
756 data_size = max_data_bytes;
757 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
758 if (rc == ERROR_SUCCESS)
760 output_value(value_name, type, data, data_size);
761 i++;
763 else if (rc == ERROR_MORE_DATA)
765 if (data_size > max_data_bytes)
767 max_data_bytes = data_size;
768 data = heap_xrealloc(data, max_data_bytes);
770 else
772 max_value_len *= 2;
773 value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR));
776 else break;
779 heap_free(data);
780 heap_free(value_name);
782 if (i || recurse)
783 output_string(newlineW);
785 subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));
787 path_len = strlenW(path);
789 i = 0;
790 for (;;)
792 subkey_len = MAX_SUBKEY_LEN;
793 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
794 if (rc == ERROR_SUCCESS)
796 if (recurse)
798 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
799 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
801 query_all(subkey, subkey_path, recurse);
802 RegCloseKey(subkey);
804 heap_free(subkey_path);
806 else output_string(fmt_path, path, subkey_name);
807 i++;
809 else break;
812 heap_free(subkey_name);
814 if (i && !recurse)
815 output_string(newlineW);
817 return 0;
820 static int reg_query(HKEY root, WCHAR *path, WCHAR *key_name, WCHAR *value_name,
821 BOOL value_empty, BOOL recurse)
823 HKEY key;
824 WCHAR newlineW[] = {'\n',0};
825 int ret;
827 if (RegOpenKeyExW(root, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
829 output_message(STRING_CANNOT_FIND);
830 return 1;
833 output_string(newlineW);
835 if (value_name || value_empty)
837 ret = query_value(key, value_name, key_name, recurse);
838 if (recurse)
839 output_message(STRING_MATCHES_FOUND, num_values_found);
841 else
842 ret = query_all(key, key_name, recurse);
844 RegCloseKey(key);
846 return ret;
849 static WCHAR *get_long_key(HKEY root, WCHAR *path)
851 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
852 WCHAR *long_key;
853 WCHAR fmt[] = {'%','s','\\','%','s',0};
855 for (i = 0; i < array_size; i++)
857 if (root == root_rels[i].key)
858 break;
861 len = strlenW(root_rels[i].long_name);
863 if (!path)
865 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
866 strcpyW(long_key, root_rels[i].long_name);
867 return long_key;
870 len += strlenW(path) + 1; /* add one for the backslash */
871 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
872 sprintfW(long_key, fmt, root_rels[i].long_name, path);
873 return long_key;
876 static BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
878 if (!sane_path(key))
879 return FALSE;
881 *root = path_get_rootkey(key);
882 if (!*root)
884 output_message(STRING_INVALID_KEY);
885 return FALSE;
888 *path = strchrW(key, '\\');
889 if (*path) (*path)++;
891 *long_key = get_long_key(*root, *path);
893 return TRUE;
896 static BOOL is_help_switch(const WCHAR *s)
898 if (strlenW(s) > 2)
899 return FALSE;
901 if ((s[0] == '/' || s[0] == '-') && (s[1] == 'h' || s[1] == '?'))
902 return TRUE;
904 return FALSE;
907 enum operations {
908 REG_ADD,
909 REG_DELETE,
910 REG_IMPORT,
911 REG_QUERY,
912 REG_INVALID
915 static enum operations get_operation(const WCHAR *str, int *op_help)
917 struct op_info { const WCHAR *op; int id; int help_id; };
919 static const WCHAR add[] = {'a','d','d',0};
920 static const WCHAR delete[] = {'d','e','l','e','t','e',0};
921 static const WCHAR import[] = {'i','m','p','o','r','t',0};
922 static const WCHAR query[] = {'q','u','e','r','y',0};
924 static const struct op_info op_array[] =
926 { add, REG_ADD, STRING_ADD_USAGE },
927 { delete, REG_DELETE, STRING_DELETE_USAGE },
928 { import, REG_IMPORT, STRING_IMPORT_USAGE },
929 { query, REG_QUERY, STRING_QUERY_USAGE },
930 { NULL, -1, 0 }
933 const struct op_info *ptr;
935 for (ptr = op_array; ptr->op; ptr++)
937 if (!lstrcmpiW(str, ptr->op))
939 *op_help = ptr->help_id;
940 return ptr->id;
944 return REG_INVALID;
947 int wmain(int argc, WCHAR *argvW[])
949 int i, op, op_help, ret;
950 BOOL show_op_help = FALSE;
951 static const WCHAR switchVAW[] = {'v','a',0};
952 static const WCHAR switchVEW[] = {'v','e',0};
953 WCHAR *key_name, *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
954 BOOL value_empty = FALSE, value_all = FALSE, recurse = FALSE, force = FALSE;
955 HKEY root;
957 if (argc == 1)
959 output_message(STRING_INVALID_SYNTAX);
960 output_message(STRING_REG_HELP);
961 return 1;
964 if (is_help_switch(argvW[1]))
966 output_message(STRING_USAGE);
967 return 0;
970 op = get_operation(argvW[1], &op_help);
972 if (op == REG_INVALID)
974 output_message(STRING_INVALID_OPTION, argvW[1]);
975 output_message(STRING_REG_HELP);
976 return 1;
979 if (argc > 2)
980 show_op_help = is_help_switch(argvW[2]);
982 if (argc == 2 || ((show_op_help || op == REG_IMPORT) && argc > 3))
984 output_message(STRING_INVALID_SYNTAX);
985 output_message(STRING_FUNC_HELP, struprW(argvW[1]));
986 return 1;
988 else if (show_op_help)
990 output_message(op_help);
991 return 0;
994 if (op == REG_IMPORT)
995 return reg_import(argvW[2]);
997 if (!parse_registry_key(argvW[2], &root, &path, &key_name))
998 return 1;
1000 for (i = 3; i < argc; i++)
1002 if (argvW[i][0] == '/' || argvW[i][0] == '-')
1004 WCHAR *ptr = &argvW[i][1];
1006 if (!lstrcmpiW(ptr, switchVEW))
1008 value_empty = TRUE;
1009 continue;
1011 else if (!lstrcmpiW(ptr, switchVAW))
1013 value_all = TRUE;
1014 continue;
1016 else if (!ptr[0] || ptr[1])
1018 output_message(STRING_INVALID_CMDLINE);
1019 return 1;
1022 switch(tolowerW(argvW[i][1]))
1024 case 'v':
1025 if (value_name || !(value_name = argvW[++i]))
1027 output_message(STRING_INVALID_CMDLINE);
1028 return 1;
1030 break;
1031 case 't':
1032 if (type || !(type = argvW[++i]))
1034 output_message(STRING_INVALID_CMDLINE);
1035 return 1;
1037 break;
1038 case 'd':
1039 if (data || !(data = argvW[++i]))
1041 output_message(STRING_INVALID_CMDLINE);
1042 return 1;
1044 break;
1045 case 's':
1046 if (op == REG_QUERY)
1048 recurse = TRUE;
1049 break;
1052 ptr = argvW[++i];
1053 if (!ptr || strlenW(ptr) != 1)
1055 output_message(STRING_INVALID_CMDLINE);
1056 return 1;
1058 separator = ptr[0];
1059 break;
1060 case 'f':
1061 force = TRUE;
1062 break;
1063 default:
1064 output_message(STRING_INVALID_CMDLINE);
1065 return 1;
1070 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
1072 output_message(STRING_INVALID_CMDLINE);
1073 return 1;
1076 if (op == REG_ADD)
1077 ret = reg_add(root, path, value_name, value_empty, type, separator, data, force);
1078 else if (op == REG_DELETE)
1079 ret = reg_delete(root, path, key_name, value_name, value_empty, value_all, force);
1080 else
1081 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
1082 return ret;