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
22 #include <wine/unicode.h>
23 #include <wine/debug.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};
44 const WCHAR
*short_name
;
45 const WCHAR
*long_name
;
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};
72 {REG_NONE
, type_none
},
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
);
87 ERR("Out of memory!\n");
93 static void *heap_xrealloc(void *buf
, size_t size
)
98 new_buf
= HeapReAlloc(GetProcessHeap(), 0, buf
, size
);
100 new_buf
= HeapAlloc(GetProcessHeap(), 0, size
);
104 ERR("Out of memory!\n");
111 static BOOL
heap_free(void *buf
)
113 return HeapFree(GetProcessHeap(), 0, buf
);
116 static void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
120 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
);
126 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
127 * back to WriteFile(), assuming the console encoding is still the right
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
);
139 static void output_formatstring(const WCHAR
*fmt
, __ms_va_list va_args
)
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
));
152 output_writeconsole(str
, len
);
156 static void __cdecl
output_message(unsigned int id
, ...)
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());
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
)
187 WCHAR answer
[MAX_PATH
];
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
;
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])
206 if (answer
[0] == Nbuffer
[0])
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
)
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
;
233 static DWORD
wchar_get_type(const WCHAR
*type_name
)
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
;
249 /* hexchar_to_byte from programs/regedit/hexedit.c */
250 static inline BYTE
hexchar_to_byte(WCHAR ch
)
252 if (ch
>= '0' && ch
<= '9')
254 else if (ch
>= 'a' && ch
<= 'f')
255 return ch
- 'a' + 10;
256 else if (ch
>= 'A' && ch
<= 'F')
257 return ch
- 'A' + 10;
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
;
268 if (!data
) data
= &empty
;
276 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
277 out_data
= heap_xalloc(*reg_count
);
278 lstrcpyW((LPWSTR
)out_data
,data
);
282 /* case REG_DWORD_LITTLE_ENDIAN: */
283 case REG_DWORD_BIG_ENDIAN
: /* Yes, this is correct! */
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
);
292 *reg_count
= sizeof(DWORD
);
293 out_data
= heap_xalloc(*reg_count
);
294 ((LPDWORD
)out_data
)[0] = val
;
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
);
305 hex1
= hexchar_to_byte(data
[i
++]);
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)
316 out_data
[destByteIndex
++] = (hex0
<< 4) | hex1
;
320 /* cleanup, print error */
322 output_message(STRING_MISSING_HEXDATA
);
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;
338 else if (data
[i
] == separator
)
339 buffer
[destindex
] = 0;
341 buffer
[destindex
] = data
[i
];
343 if (destindex
&& !buffer
[destindex
- 1] && (!buffer
[destindex
] || destindex
== 1))
346 output_message(STRING_INVALID_STRING
);
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
;
357 output_message(STRING_UNHANDLED_TYPE
, reg_type
, 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
);
373 if (key
[0] == '\\' && key
[1] == '\\' && key
[2] != '\\')
375 output_message(STRING_NO_REMOTE
);
382 static int reg_add(HKEY root
, WCHAR
*path
, WCHAR
*value_name
, BOOL value_empty
,
383 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
387 if (RegCreateKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
389 output_message(STRING_INVALID_KEY
);
393 if (value_name
|| value_empty
|| data
)
397 BYTE
* reg_data
= NULL
;
401 if (RegQueryValueExW(key
, value_name
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
403 if (!ask_confirm(STRING_OVERWRITE_VALUE
, value_name
))
406 output_message(STRING_CANCELLED
);
412 reg_type
= wchar_get_type(type
);
416 output_message(STRING_UNSUPPORTED_TYPE
, type
);
419 if ((reg_type
== REG_DWORD
|| reg_type
== REG_DWORD_BIG_ENDIAN
) && !data
)
422 output_message(STRING_INVALID_CMDLINE
);
426 if (!(reg_data
= get_regdata(data
, reg_type
, separator
, ®_count
)))
432 RegSetValueExW(key
, value_name
, 0, reg_type
, reg_data
, reg_count
);
437 output_message(STRING_SUCCESS
);
442 static int reg_delete(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
443 BOOL value_empty
, BOOL value_all
, BOOL force
)
451 if (value_name
|| value_empty
)
452 ret
= ask_confirm(STRING_DELETE_VALUE
, value_name
);
454 ret
= ask_confirm(STRING_DELETE_VALUEALL
, key_name
);
456 ret
= ask_confirm(STRING_DELETE_SUBKEY
, key_name
);
460 output_message(STRING_CANCELLED
);
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
);
473 output_message(STRING_SUCCESS
);
477 if (RegOpenKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
479 output_message(STRING_CANNOT_FIND
);
485 DWORD max_value_len
= 256, value_len
;
489 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
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
);
502 output_message(STRING_VALUEALL_FAILED
, key_name
);
506 else if (rc
== ERROR_MORE_DATA
)
509 value_name
= heap_xrealloc(value_name
, max_value_len
* sizeof(WCHAR
));
513 heap_free(value_name
);
515 else if (value_name
|| value_empty
)
517 if (RegDeleteValueW(key
, value_empty
? NULL
: value_name
) != ERROR_SUCCESS
)
520 output_message(STRING_CANNOT_FIND
);
526 output_message(STRING_SUCCESS
);
530 static WCHAR
*reg_data_to_wchar(DWORD type
, const BYTE
*src
, DWORD size_bytes
)
532 WCHAR
*buffer
= NULL
;
539 buffer
= heap_xalloc(size_bytes
);
540 strcpyW(buffer
, (WCHAR
*)src
);
546 static const WCHAR fmt
[] = {'%','0','2','X',0};
548 buffer
= heap_xalloc((size_bytes
* 2 + 1) * sizeof(WCHAR
));
550 for (i
= 0; i
< size_bytes
; i
++)
551 ptr
+= sprintfW(ptr
, fmt
, src
[i
]);
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
);
567 const int two_wchars
= 2 * sizeof(WCHAR
);
569 const WCHAR
*tmp
= (const WCHAR
*)src
;
572 if (size_bytes
<= two_wchars
)
574 buffer
= heap_xalloc(sizeof(WCHAR
));
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
++)
586 buffer
[destindex
] = tmp
[i
];
589 buffer
[destindex
++] = '\\';
590 buffer
[destindex
] = '0';
593 buffer
[destindex
] = 0;
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
;
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};
619 if (value_name
&& value_name
[0])
620 output_string(fmt
, value_name
);
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
));
630 reg_data
= reg_data_to_wchar(type
, data
, data_size
);
631 output_string(fmt
, reg_data
);
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
)
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
);
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
)
660 DWORD max_data_bytes
= 2048, data_size
;
662 DWORD type
, path_len
, i
;
664 WCHAR fmt
[] = {'%','1','\n',0};
665 WCHAR newlineW
[] = {'\n',0};
666 WCHAR
*subkey_name
, *subkey_path
;
669 data
= heap_xalloc(max_data_bytes
);
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
);
683 if (rc
== ERROR_SUCCESS
)
685 output_string(fmt
, path
);
686 output_value(value_name
, type
, data
, data_size
);
687 output_string(newlineW
);
695 if (rc
== ERROR_FILE_NOT_FOUND
)
697 if (value_name
&& *value_name
)
699 output_message(STRING_CANNOT_FIND
);
702 output_string(fmt
, path
);
703 output_value(NULL
, REG_SZ
, NULL
, 0);
708 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
710 path_len
= strlenW(path
);
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
);
725 heap_free(subkey_path
);
731 heap_free(subkey_name
);
735 static int query_all(HKEY key
, WCHAR
*path
, BOOL recurse
)
738 DWORD max_value_len
= 256, value_len
;
739 DWORD max_data_bytes
= 2048, data_size
;
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};
749 output_string(fmt
, path
);
751 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
752 data
= heap_xalloc(max_data_bytes
);
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
);
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
);
775 value_name
= heap_xrealloc(value_name
, max_value_len
* sizeof(WCHAR
));
782 heap_free(value_name
);
785 output_string(newlineW
);
787 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
789 path_len
= strlenW(path
);
794 subkey_len
= MAX_SUBKEY_LEN
;
795 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
796 if (rc
== ERROR_SUCCESS
)
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
);
806 heap_free(subkey_path
);
808 else output_string(fmt_path
, path
, subkey_name
);
814 heap_free(subkey_name
);
817 output_string(newlineW
);
822 static int reg_query(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
823 BOOL value_empty
, BOOL recurse
)
826 WCHAR newlineW
[] = {'\n',0};
829 if (RegOpenKeyExW(root
, path
, 0, KEY_READ
, &key
) != ERROR_SUCCESS
)
831 output_message(STRING_CANNOT_FIND
);
835 output_string(newlineW
);
837 if (value_name
|| value_empty
)
839 ret
= query_value(key
, value_name
, key_name
, recurse
);
841 output_message(STRING_MATCHES_FOUND
, num_values_found
);
844 ret
= query_all(key
, key_name
, recurse
);
851 static WCHAR
*get_long_key(HKEY root
, WCHAR
*path
)
853 DWORD i
, array_size
= ARRAY_SIZE(root_rels
), len
;
855 WCHAR fmt
[] = {'%','s','\\','%','s',0};
857 for (i
= 0; i
< array_size
; i
++)
859 if (root
== root_rels
[i
].key
)
863 len
= strlenW(root_rels
[i
].long_name
);
867 long_key
= heap_xalloc((len
+ 1) * sizeof(WCHAR
));
868 strcpyW(long_key
, root_rels
[i
].long_name
);
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
);
878 static BOOL
parse_registry_key(const WCHAR
*key
, HKEY
*root
, WCHAR
**path
, WCHAR
**long_key
)
883 *root
= path_get_rootkey(key
);
886 output_message(STRING_INVALID_KEY
);
890 *path
= strchrW(key
, '\\');
891 if (*path
) (*path
)++;
893 *long_key
= get_long_key(*root
, *path
);
898 static BOOL
is_help_switch(const WCHAR
*s
)
903 if ((s
[0] == '/' || s
[0] == '-') && (s
[1] == 'h' || s
[1] == '?'))
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
;
928 if (!lstrcmpiW(str
, deleteW
))
930 *op_help
= STRING_DELETE_USAGE
;
934 if (!lstrcmpiW(str
, queryW
))
936 *op_help
= STRING_QUERY_USAGE
;
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
;
955 output_message(STRING_INVALID_SYNTAX
);
956 output_message(STRING_REG_HELP
);
960 if (is_help_switch(argvW
[1]))
962 output_message(STRING_USAGE
);
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
);
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]));
984 else if (show_op_help
)
986 output_message(op_help
);
990 if (!parse_registry_key(argvW
[2], &root
, &path
, &key_name
))
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
))
1004 else if (!lstrcmpiW(ptr
, switchVAW
))
1009 else if (!ptr
[0] || ptr
[1])
1011 output_message(STRING_INVALID_CMDLINE
);
1015 switch(tolowerW(argvW
[i
][1]))
1018 if (value_name
|| !(value_name
= argvW
[++i
]))
1020 output_message(STRING_INVALID_CMDLINE
);
1025 if (type
|| !(type
= argvW
[++i
]))
1027 output_message(STRING_INVALID_CMDLINE
);
1032 if (data
|| !(data
= argvW
[++i
]))
1034 output_message(STRING_INVALID_CMDLINE
);
1039 if (op
== REG_QUERY
)
1046 if (!ptr
|| strlenW(ptr
) != 1)
1048 output_message(STRING_INVALID_CMDLINE
);
1057 output_message(STRING_INVALID_CMDLINE
);
1063 if ((value_name
&& value_empty
) || (value_name
&& value_all
) || (value_empty
&& value_all
))
1065 output_message(STRING_INVALID_CMDLINE
);
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
);
1074 ret
= reg_query(root
, path
, key_name
, value_name
, value_empty
, recurse
);