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
20 #include <wine/unicode.h>
21 #include <wine/debug.h>
25 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
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};
43 const WCHAR
*short_name
;
44 const WCHAR
*long_name
;
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};
71 {REG_NONE
, type_none
},
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 void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
85 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
);
91 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
92 * back to WriteFile(), assuming the console encoding is still the right
95 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, wlen
, NULL
, 0, NULL
, NULL
);
96 msgA
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(char));
99 WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, wlen
, msgA
, len
, NULL
, NULL
);
100 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
101 HeapFree(GetProcessHeap(), 0, msgA
);
105 static void output_formatstring(const WCHAR
*fmt
, __ms_va_list va_args
)
110 SetLastError(NO_ERROR
);
111 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
112 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
113 if (len
== 0 && GetLastError() != NO_ERROR
)
115 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
118 output_writeconsole(str
, len
);
122 static void __cdecl
output_message(unsigned int id
, ...)
125 __ms_va_list va_args
;
127 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
129 WINE_FIXME("LoadString failed with %d\n", GetLastError());
132 __ms_va_start(va_args
, id
);
133 output_formatstring(fmt
, va_args
);
134 __ms_va_end(va_args
);
137 static void __cdecl
output_string(const WCHAR
*fmt
, ...)
139 __ms_va_list va_args
;
141 __ms_va_start(va_args
, fmt
);
142 output_formatstring(fmt
, va_args
);
143 __ms_va_end(va_args
);
146 /* ask_confirm() adapted from programs/cmd/builtins.c */
147 static BOOL
ask_confirm(unsigned int msgid
, WCHAR
*reg_info
)
153 WCHAR answer
[MAX_PATH
];
157 hmod
= GetModuleHandleW(NULL
);
158 LoadStringW(hmod
, STRING_YES
, Ybuffer
, ARRAY_SIZE(Ybuffer
));
159 LoadStringW(hmod
, STRING_NO
, Nbuffer
, ARRAY_SIZE(Nbuffer
));
160 LoadStringW(hmod
, STRING_DEFAULT_VALUE
, defval
, ARRAY_SIZE(defval
));
162 str
= (reg_info
&& *reg_info
) ? reg_info
: defval
;
166 output_message(msgid
, str
);
167 output_message(STRING_YESNO
);
168 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE
), answer
, ARRAY_SIZE(answer
), &count
, NULL
);
169 answer
[0] = toupperW(answer
[0]);
170 if (answer
[0] == Ybuffer
[0])
172 if (answer
[0] == Nbuffer
[0])
177 static inline BOOL
path_rootname_cmp(const WCHAR
*input_path
, const WCHAR
*rootkey_name
)
179 DWORD length
= strlenW(rootkey_name
);
181 return (!strncmpiW(input_path
, rootkey_name
, length
) &&
182 (input_path
[length
] == 0 || input_path
[length
] == '\\'));
185 static HKEY
path_get_rootkey(const WCHAR
*path
)
189 for (i
= 0; i
< ARRAY_SIZE(root_rels
); i
++)
191 if (path_rootname_cmp(path
, root_rels
[i
].short_name
) ||
192 path_rootname_cmp(path
, root_rels
[i
].long_name
))
193 return root_rels
[i
].key
;
199 static DWORD
wchar_get_type(const WCHAR
*type_name
)
206 for (i
= 0; i
< ARRAY_SIZE(type_rels
); i
++)
208 if (!strcmpiW(type_rels
[i
].name
, type_name
))
209 return type_rels
[i
].type
;
215 /* hexchar_to_byte from programs/regedit/hexedit.c */
216 static inline BYTE
hexchar_to_byte(WCHAR ch
)
218 if (ch
>= '0' && ch
<= '9')
220 else if (ch
>= 'a' && ch
<= 'f')
221 return ch
- 'a' + 10;
222 else if (ch
>= 'A' && ch
<= 'F')
223 return ch
- 'A' + 10;
228 static LPBYTE
get_regdata(const WCHAR
*data
, DWORD reg_type
, WCHAR separator
, DWORD
*reg_count
)
230 static const WCHAR empty
;
231 LPBYTE out_data
= NULL
;
234 if (!data
) data
= &empty
;
242 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
243 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
244 lstrcpyW((LPWSTR
)out_data
,data
);
248 /* case REG_DWORD_LITTLE_ENDIAN: */
249 case REG_DWORD_BIG_ENDIAN
: /* Yes, this is correct! */
253 val
= strtoulW(data
, &rest
, (tolowerW(data
[1]) == 'x') ? 16 : 10);
254 if (*rest
|| data
[0] == '-' || (val
== ~0u && errno
== ERANGE
) || val
> ~0u) {
255 output_message(STRING_MISSING_INTEGER
);
258 *reg_count
= sizeof(DWORD
);
259 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
260 ((LPDWORD
)out_data
)[0] = val
;
266 int i
= 0, destByteIndex
= 0, datalen
= lstrlenW(data
);
267 *reg_count
= ((datalen
+ datalen
% 2) / 2) * sizeof(BYTE
);
268 out_data
= HeapAlloc(GetProcessHeap(), 0, *reg_count
);
271 hex1
= hexchar_to_byte(data
[i
++]);
274 out_data
[destByteIndex
++] = hex1
;
276 for(;i
+ 1 < datalen
;i
+= 2)
278 hex0
= hexchar_to_byte(data
[i
]);
279 hex1
= hexchar_to_byte(data
[i
+ 1]);
280 if(hex0
== 0xFF || hex1
== 0xFF)
282 out_data
[destByteIndex
++] = (hex0
<< 4) | hex1
;
286 /* cleanup, print error */
287 HeapFree(GetProcessHeap(), 0, out_data
);
288 output_message(STRING_MISSING_HEXDATA
);
294 int i
, destindex
, len
= strlenW(data
);
295 WCHAR
*buffer
= HeapAlloc(GetProcessHeap(), 0, (len
+ 2) * sizeof(WCHAR
));
297 for (i
= 0, destindex
= 0; i
< len
; i
++, destindex
++)
299 if (!separator
&& data
[i
] == '\\' && data
[i
+ 1] == '0')
301 buffer
[destindex
] = 0;
304 else if (data
[i
] == separator
)
305 buffer
[destindex
] = 0;
307 buffer
[destindex
] = data
[i
];
309 if (destindex
&& !buffer
[destindex
- 1] && (!buffer
[destindex
] || destindex
== 1))
311 HeapFree(GetProcessHeap(), 0, buffer
);
312 output_message(STRING_INVALID_STRING
);
316 buffer
[destindex
] = 0;
317 if (destindex
&& buffer
[destindex
- 1])
318 buffer
[++destindex
] = 0;
319 *reg_count
= (destindex
+ 1) * sizeof(WCHAR
);
320 return (BYTE
*)buffer
;
323 output_message(STRING_UNHANDLED_TYPE
, reg_type
, data
);
329 static BOOL
sane_path(const WCHAR
*key
)
331 unsigned int i
= strlenW(key
);
333 if (i
< 3 || (key
[i
- 1] == '\\' && key
[i
- 2] == '\\'))
335 output_message(STRING_INVALID_KEY
);
339 if (key
[0] == '\\' && key
[1] == '\\' && key
[2] != '\\')
341 output_message(STRING_NO_REMOTE
);
348 static int reg_add(HKEY root
, WCHAR
*path
, WCHAR
*value_name
, BOOL value_empty
,
349 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
353 if (RegCreateKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
355 output_message(STRING_INVALID_KEY
);
359 if (value_name
|| value_empty
|| data
)
363 BYTE
* reg_data
= NULL
;
367 if (RegQueryValueExW(key
, value_name
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
369 if (!ask_confirm(STRING_OVERWRITE_VALUE
, value_name
))
372 output_message(STRING_CANCELLED
);
378 reg_type
= wchar_get_type(type
);
382 output_message(STRING_UNSUPPORTED_TYPE
, type
);
385 if ((reg_type
== REG_DWORD
|| reg_type
== REG_DWORD_BIG_ENDIAN
) && !data
)
388 output_message(STRING_INVALID_CMDLINE
);
392 if (!(reg_data
= get_regdata(data
, reg_type
, separator
, ®_count
)))
398 RegSetValueExW(key
, value_name
, 0, reg_type
, reg_data
, reg_count
);
399 HeapFree(GetProcessHeap(),0,reg_data
);
403 output_message(STRING_SUCCESS
);
408 static int reg_delete(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
409 BOOL value_empty
, BOOL value_all
, BOOL force
)
417 if (value_name
|| value_empty
)
418 ret
= ask_confirm(STRING_DELETE_VALUE
, value_name
);
420 ret
= ask_confirm(STRING_DELETE_VALUEALL
, key_name
);
422 ret
= ask_confirm(STRING_DELETE_SUBKEY
, key_name
);
426 output_message(STRING_CANCELLED
);
431 /* Delete subtree only if no /v* option is given */
432 if (!value_name
&& !value_empty
&& !value_all
)
434 if (RegDeleteTreeW(root
, path
) != ERROR_SUCCESS
)
436 output_message(STRING_CANNOT_FIND
);
439 output_message(STRING_SUCCESS
);
443 if (RegOpenKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
445 output_message(STRING_CANNOT_FIND
);
456 rc
= RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
457 NULL
, &maxValue
, NULL
, NULL
, NULL
);
458 if (rc
!= ERROR_SUCCESS
)
461 output_message(STRING_GENERAL_FAILURE
);
465 szValue
= HeapAlloc(GetProcessHeap(),0,maxValue
*sizeof(WCHAR
));
470 rc
= RegEnumValueW(key
, 0, szValue
, &count
, NULL
, NULL
, NULL
, NULL
);
471 if (rc
== ERROR_SUCCESS
)
473 rc
= RegDeleteValueW(key
, szValue
);
474 if (rc
!= ERROR_SUCCESS
)
476 HeapFree(GetProcessHeap(), 0, szValue
);
478 output_message(STRING_VALUEALL_FAILED
, key_name
);
484 HeapFree(GetProcessHeap(), 0, szValue
);
486 else if (value_name
|| value_empty
)
488 if (RegDeleteValueW(key
, value_empty
? NULL
: value_name
) != ERROR_SUCCESS
)
491 output_message(STRING_CANNOT_FIND
);
497 output_message(STRING_SUCCESS
);
501 static WCHAR
*reg_data_to_wchar(DWORD type
, const BYTE
*src
, DWORD size_bytes
)
503 WCHAR
*buffer
= NULL
;
510 buffer
= HeapAlloc(GetProcessHeap(), 0, size_bytes
);
511 strcpyW(buffer
, (WCHAR
*)src
);
517 WCHAR fmt
[] = {'%','0','2','X',0};
519 buffer
= HeapAlloc(GetProcessHeap(), 0, (size_bytes
* 2 + 1) * sizeof(WCHAR
));
521 for (i
= 0; i
< size_bytes
; i
++)
522 ptr
+= sprintfW(ptr
, fmt
, src
[i
]);
526 /* case REG_DWORD_LITTLE_ENDIAN: */
527 case REG_DWORD_BIG_ENDIAN
:
529 const int zero_x_dword
= 10;
530 WCHAR fmt
[] = {'0','x','%','x',0};
532 buffer
= HeapAlloc(GetProcessHeap(), 0, (zero_x_dword
+ 1) * sizeof(WCHAR
));
533 sprintfW(buffer
, fmt
, *(DWORD
*)src
);
538 const int two_wchars
= 2 * sizeof(WCHAR
);
540 const WCHAR
*tmp
= (const WCHAR
*)src
;
543 if (size_bytes
<= two_wchars
)
545 buffer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
));
550 tmp_size
= size_bytes
- two_wchars
; /* exclude both null terminators */
551 buffer
= HeapAlloc(GetProcessHeap(), 0, tmp_size
* 2 + sizeof(WCHAR
));
552 len
= tmp_size
/ sizeof(WCHAR
);
554 for (i
= 0, destindex
= 0; i
< len
; i
++, destindex
++)
557 buffer
[destindex
] = tmp
[i
];
560 buffer
[destindex
++] = '\\';
561 buffer
[destindex
] = '0';
564 buffer
[destindex
] = 0;
571 static const WCHAR
*reg_type_to_wchar(DWORD type
)
573 int i
, array_size
= ARRAY_SIZE(type_rels
);
575 for (i
= 0; i
< array_size
; i
++)
577 if (type
== type_rels
[i
].type
)
578 return type_rels
[i
].name
;
583 static void output_value(const WCHAR
*value_name
, DWORD type
, BYTE
*data
, DWORD data_size
)
585 WCHAR fmt
[] = {' ',' ',' ',' ','%','1',0};
588 WCHAR newlineW
[] = {'\n',0};
590 if (value_name
&& value_name
[0])
591 output_string(fmt
, value_name
);
594 LoadStringW(GetModuleHandleW(NULL
), STRING_DEFAULT_VALUE
, defval
, ARRAY_SIZE(defval
));
595 output_string(fmt
, defval
);
597 output_string(fmt
, reg_type_to_wchar(type
));
601 reg_data
= reg_data_to_wchar(type
, data
, data_size
);
602 output_string(fmt
, reg_data
);
603 HeapFree(GetProcessHeap(), 0, reg_data
);
607 LoadStringW(GetModuleHandleW(NULL
), STRING_VALUE_NOT_SET
, defval
, ARRAY_SIZE(defval
));
608 output_string(fmt
, defval
);
610 output_string(newlineW
);
613 static WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
616 WCHAR fmt
[] = {'%','s','\\','%','s',0};
618 subkey_path
= HeapAlloc(GetProcessHeap(), 0, (path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
621 ERR("Failed to allocate memory for subkey_path\n");
624 sprintfW(subkey_path
, fmt
, path
, subkey_name
);
628 static unsigned int num_values_found
= 0;
630 static int query_value(HKEY key
, WCHAR
*value_name
, WCHAR
*path
, BOOL recurse
)
633 DWORD num_subkeys
, max_subkey_len
, subkey_len
;
634 DWORD max_data_bytes
, data_size
;
635 DWORD type
, path_len
, i
;
637 WCHAR fmt
[] = {'%','1','\n',0};
638 WCHAR newlineW
[] = {'\n',0};
639 WCHAR
*subkey_name
, *subkey_path
;
642 rc
= RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, &num_subkeys
, &max_subkey_len
,
643 NULL
, NULL
, NULL
, &max_data_bytes
, NULL
, NULL
);
646 ERR("RegQueryInfoKey failed: %d\n", rc
);
650 data
= HeapAlloc(GetProcessHeap(), 0, max_data_bytes
);
653 ERR("Failed to allocate memory for data\n");
657 data_size
= max_data_bytes
;
658 rc
= RegQueryValueExW(key
, value_name
, NULL
, &type
, data
, &data_size
);
659 if (rc
== ERROR_SUCCESS
)
661 output_string(fmt
, path
);
662 output_value(value_name
, type
, data
, data_size
);
663 output_string(newlineW
);
667 HeapFree(GetProcessHeap(), 0, data
);
671 if (rc
== ERROR_FILE_NOT_FOUND
)
673 if (value_name
&& *value_name
)
675 output_message(STRING_CANNOT_FIND
);
678 output_string(fmt
, path
);
679 output_value(NULL
, REG_SZ
, NULL
, 0);
685 subkey_name
= HeapAlloc(GetProcessHeap(), 0, max_subkey_len
* sizeof(WCHAR
));
688 ERR("Failed to allocate memory for subkey_name\n");
692 path_len
= strlenW(path
);
694 for (i
= 0; i
< num_subkeys
; i
++)
696 subkey_len
= max_subkey_len
;
697 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
698 if (rc
== ERROR_SUCCESS
)
700 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
701 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
703 query_value(subkey
, value_name
, subkey_path
, recurse
);
706 HeapFree(GetProcessHeap(), 0, subkey_path
);
710 HeapFree(GetProcessHeap(), 0, subkey_name
);
714 static int query_all(HKEY key
, WCHAR
*path
, BOOL recurse
)
717 DWORD num_subkeys
, max_subkey_len
, subkey_len
;
718 DWORD num_values
, max_value_len
, value_len
;
719 DWORD max_data_bytes
, data_size
;
720 DWORD i
, type
, path_len
;
721 WCHAR fmt
[] = {'%','1','\n',0};
722 WCHAR fmt_path
[] = {'%','1','\\','%','2','\n',0};
723 WCHAR
*value_name
, *subkey_name
, *subkey_path
;
724 WCHAR newlineW
[] = {'\n',0};
728 rc
= RegQueryInfoKeyW(key
, NULL
, NULL
, NULL
, &num_subkeys
, &max_subkey_len
, NULL
,
729 &num_values
, &max_value_len
, &max_data_bytes
, NULL
, NULL
);
732 ERR("RegQueryInfoKey failed: %d\n", rc
);
736 output_string(fmt
, path
);
739 value_name
= HeapAlloc(GetProcessHeap(), 0, max_value_len
* sizeof(WCHAR
));
742 ERR("Failed to allocate memory for value_name\n");
746 data
= HeapAlloc(GetProcessHeap(), 0, max_data_bytes
);
749 HeapFree(GetProcessHeap(), 0, value_name
);
750 ERR("Failed to allocate memory for data\n");
754 for (i
= 0; i
< num_values
; i
++)
756 value_len
= max_value_len
;
757 data_size
= max_data_bytes
;
758 rc
= RegEnumValueW(key
, i
, value_name
, &value_len
, NULL
, &type
, data
, &data_size
);
759 if (rc
== ERROR_SUCCESS
)
760 output_value(value_name
, type
, data
, data_size
);
763 HeapFree(GetProcessHeap(), 0, data
);
764 HeapFree(GetProcessHeap(), 0, value_name
);
766 if (num_values
|| recurse
)
767 output_string(newlineW
);
770 subkey_name
= HeapAlloc(GetProcessHeap(), 0, max_subkey_len
* sizeof(WCHAR
));
773 ERR("Failed to allocate memory for subkey_name\n");
777 path_len
= strlenW(path
);
779 for (i
= 0; i
< num_subkeys
; i
++)
781 subkey_len
= max_subkey_len
;
782 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
783 if (rc
== ERROR_SUCCESS
)
787 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
788 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
790 query_all(subkey
, subkey_path
, recurse
);
793 HeapFree(GetProcessHeap(), 0, subkey_path
);
795 else output_string(fmt_path
, path
, subkey_name
);
799 HeapFree(GetProcessHeap(), 0, subkey_name
);
801 if (num_subkeys
&& !recurse
)
802 output_string(newlineW
);
807 static int reg_query(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
808 BOOL value_empty
, BOOL recurse
)
811 WCHAR newlineW
[] = {'\n',0};
814 if (RegOpenKeyExW(root
, path
, 0, KEY_READ
, &key
) != ERROR_SUCCESS
)
816 output_message(STRING_CANNOT_FIND
);
820 output_string(newlineW
);
822 if (value_name
|| value_empty
)
824 ret
= query_value(key
, value_name
, key_name
, recurse
);
826 output_message(STRING_MATCHES_FOUND
, num_values_found
);
829 ret
= query_all(key
, key_name
, recurse
);
836 static WCHAR
*get_long_key(HKEY root
, WCHAR
*path
)
838 DWORD i
, array_size
= ARRAY_SIZE(root_rels
), len
;
840 WCHAR fmt
[] = {'%','s','\\','%','s',0};
842 for (i
= 0; i
< array_size
; i
++)
844 if (root
== root_rels
[i
].key
)
848 len
= strlenW(root_rels
[i
].long_name
);
852 long_key
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
853 strcpyW(long_key
, root_rels
[i
].long_name
);
857 len
+= strlenW(path
) + 1; /* add one for the backslash */
858 long_key
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
859 sprintfW(long_key
, fmt
, root_rels
[i
].long_name
, path
);
863 static BOOL
parse_registry_key(const WCHAR
*key
, HKEY
*root
, WCHAR
**path
, WCHAR
**long_key
)
868 *root
= path_get_rootkey(key
);
871 output_message(STRING_INVALID_KEY
);
875 *path
= strchrW(key
, '\\');
876 if (*path
) (*path
)++;
878 *long_key
= get_long_key(*root
, *path
);
883 static BOOL
is_help_switch(const WCHAR
*s
)
888 if ((s
[0] == '/' || s
[0] == '-') && (s
[1] == 'h' || s
[1] == '?'))
901 static const WCHAR addW
[] = {'a','d','d',0};
902 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
903 static const WCHAR queryW
[] = {'q','u','e','r','y',0};
905 static enum operations
get_operation(const WCHAR
*str
, int *op_help
)
907 if (!lstrcmpiW(str
, addW
))
909 *op_help
= STRING_ADD_USAGE
;
913 if (!lstrcmpiW(str
, deleteW
))
915 *op_help
= STRING_DELETE_USAGE
;
919 if (!lstrcmpiW(str
, queryW
))
921 *op_help
= STRING_QUERY_USAGE
;
928 int wmain(int argc
, WCHAR
*argvW
[])
930 int i
, op
, op_help
, ret
;
931 BOOL show_op_help
= FALSE
;
932 static const WCHAR switchVAW
[] = {'v','a',0};
933 static const WCHAR switchVEW
[] = {'v','e',0};
934 WCHAR
*key_name
, *path
, *value_name
= NULL
, *type
= NULL
, *data
= NULL
, separator
= '\0';
935 BOOL value_empty
= FALSE
, value_all
= FALSE
, recurse
= FALSE
, force
= FALSE
;
940 output_message(STRING_INVALID_SYNTAX
);
941 output_message(STRING_REG_HELP
);
945 if (is_help_switch(argvW
[1]))
947 output_message(STRING_USAGE
);
951 op
= get_operation(argvW
[1], &op_help
);
953 if (op
== REG_INVALID
)
955 output_message(STRING_INVALID_OPTION
, argvW
[1]);
956 output_message(STRING_REG_HELP
);
961 show_op_help
= is_help_switch(argvW
[2]);
963 if (argc
== 2 || (show_op_help
&& argc
> 3))
965 output_message(STRING_INVALID_SYNTAX
);
966 output_message(STRING_FUNC_HELP
, struprW(argvW
[1]));
969 else if (show_op_help
)
971 output_message(op_help
);
975 if (!parse_registry_key(argvW
[2], &root
, &path
, &key_name
))
978 for (i
= 3; i
< argc
; i
++)
980 if (argvW
[i
][0] == '/' || argvW
[i
][0] == '-')
982 WCHAR
*ptr
= &argvW
[i
][1];
984 if (!lstrcmpiW(ptr
, switchVEW
))
989 else if (!lstrcmpiW(ptr
, switchVAW
))
994 else if (!ptr
[0] || ptr
[1])
996 output_message(STRING_INVALID_CMDLINE
);
1000 switch(tolowerW(argvW
[i
][1]))
1003 if (value_name
|| !(value_name
= argvW
[++i
]))
1005 output_message(STRING_INVALID_CMDLINE
);
1010 if (type
|| !(type
= argvW
[++i
]))
1012 output_message(STRING_INVALID_CMDLINE
);
1017 if (data
|| !(data
= argvW
[++i
]))
1019 output_message(STRING_INVALID_CMDLINE
);
1024 if (op
== REG_QUERY
)
1031 if (!ptr
|| strlenW(ptr
) != 1)
1033 output_message(STRING_INVALID_CMDLINE
);
1042 output_message(STRING_INVALID_CMDLINE
);
1048 if ((value_name
&& value_empty
) || (value_name
&& value_all
) || (value_empty
&& value_all
))
1050 output_message(STRING_INVALID_CMDLINE
);
1055 ret
= reg_add(root
, path
, value_name
, value_empty
, type
, separator
, data
, force
);
1056 else if (op
== REG_DELETE
)
1057 ret
= reg_delete(root
, path
, key_name
, value_name
, value_empty
, value_all
, force
);
1058 else if (op
== REG_QUERY
)
1059 ret
= reg_query(root
, path
, key_name
, value_name
, value_empty
, recurse
);