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>
24 #include <wine/heap.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};
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 const WCHAR newlineW
[] = {'\n',0};
83 void *heap_xalloc(size_t size
)
85 void *buf
= heap_alloc(size
);
88 ERR("Out of memory!\n");
94 void *heap_xrealloc(void *buf
, size_t size
)
96 void *new_buf
= heap_realloc(buf
, size
);
100 ERR("Out of memory!\n");
107 void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
111 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
);
117 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
118 * back to WriteFile(), assuming the console encoding is still the right
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
);
130 static void output_formatstring(const WCHAR
*fmt
, __ms_va_list va_args
)
135 SetLastError(NO_ERROR
);
136 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
137 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
138 if (len
== 0 && GetLastError() != NO_ERROR
)
140 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
143 output_writeconsole(str
, len
);
147 void WINAPIV
output_message(unsigned int id
, ...)
150 __ms_va_list va_args
;
152 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
154 WINE_FIXME("LoadString failed with %d\n", GetLastError());
157 __ms_va_start(va_args
, id
);
158 output_formatstring(fmt
, va_args
);
159 __ms_va_end(va_args
);
162 static void WINAPIV
output_string(const WCHAR
*fmt
, ...)
164 __ms_va_list va_args
;
166 __ms_va_start(va_args
, fmt
);
167 output_formatstring(fmt
, va_args
);
168 __ms_va_end(va_args
);
171 /* ask_confirm() adapted from programs/cmd/builtins.c */
172 BOOL
ask_confirm(unsigned int msgid
, WCHAR
*reg_info
)
178 WCHAR answer
[MAX_PATH
];
182 hmod
= GetModuleHandleW(NULL
);
183 LoadStringW(hmod
, STRING_YES
, Ybuffer
, ARRAY_SIZE(Ybuffer
));
184 LoadStringW(hmod
, STRING_NO
, Nbuffer
, ARRAY_SIZE(Nbuffer
));
185 LoadStringW(hmod
, STRING_DEFAULT_VALUE
, defval
, ARRAY_SIZE(defval
));
187 str
= (reg_info
&& *reg_info
) ? reg_info
: defval
;
191 output_message(msgid
, str
);
192 output_message(STRING_YESNO
);
193 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE
), answer
, ARRAY_SIZE(answer
), &count
, NULL
);
194 answer
[0] = toupperW(answer
[0]);
195 if (answer
[0] == Ybuffer
[0])
197 if (answer
[0] == Nbuffer
[0])
202 static inline BOOL
path_rootname_cmp(const WCHAR
*input_path
, const WCHAR
*rootkey_name
)
204 DWORD length
= strlenW(rootkey_name
);
206 return (!strncmpiW(input_path
, rootkey_name
, length
) &&
207 (input_path
[length
] == 0 || input_path
[length
] == '\\'));
210 HKEY
path_get_rootkey(const WCHAR
*path
)
214 for (i
= 0; i
< ARRAY_SIZE(root_rels
); i
++)
216 if (path_rootname_cmp(path
, root_rels
[i
].short_name
) ||
217 path_rootname_cmp(path
, root_rels
[i
].long_name
))
218 return root_rels
[i
].key
;
224 static DWORD
wchar_get_type(const WCHAR
*type_name
)
231 for (i
= 0; i
< ARRAY_SIZE(type_rels
); i
++)
233 if (!strcmpiW(type_rels
[i
].name
, type_name
))
234 return type_rels
[i
].type
;
240 /* hexchar_to_byte from programs/regedit/hexedit.c */
241 static inline BYTE
hexchar_to_byte(WCHAR ch
)
243 if (ch
>= '0' && ch
<= '9')
245 else if (ch
>= 'a' && ch
<= 'f')
246 return ch
- 'a' + 10;
247 else if (ch
>= 'A' && ch
<= 'F')
248 return ch
- 'A' + 10;
253 static LPBYTE
get_regdata(const WCHAR
*data
, DWORD reg_type
, WCHAR separator
, DWORD
*reg_count
)
255 static const WCHAR empty
;
256 LPBYTE out_data
= NULL
;
259 if (!data
) data
= &empty
;
267 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
268 out_data
= heap_xalloc(*reg_count
);
269 lstrcpyW((LPWSTR
)out_data
,data
);
273 /* case REG_DWORD_LITTLE_ENDIAN: */
274 case REG_DWORD_BIG_ENDIAN
: /* Yes, this is correct! */
278 val
= wcstoul(data
, &rest
, (tolowerW(data
[1]) == 'x') ? 16 : 10);
279 if (*rest
|| data
[0] == '-' || (val
== ~0u && errno
== ERANGE
)) {
280 output_message(STRING_MISSING_INTEGER
);
283 *reg_count
= sizeof(DWORD
);
284 out_data
= heap_xalloc(*reg_count
);
285 ((LPDWORD
)out_data
)[0] = val
;
291 int i
= 0, destByteIndex
= 0, datalen
= lstrlenW(data
);
292 *reg_count
= ((datalen
+ datalen
% 2) / 2) * sizeof(BYTE
);
293 out_data
= heap_xalloc(*reg_count
);
296 hex1
= hexchar_to_byte(data
[i
++]);
299 out_data
[destByteIndex
++] = hex1
;
301 for(;i
+ 1 < datalen
;i
+= 2)
303 hex0
= hexchar_to_byte(data
[i
]);
304 hex1
= hexchar_to_byte(data
[i
+ 1]);
305 if(hex0
== 0xFF || hex1
== 0xFF)
307 out_data
[destByteIndex
++] = (hex0
<< 4) | hex1
;
311 /* cleanup, print error */
313 output_message(STRING_MISSING_HEXDATA
);
319 int i
, destindex
, len
= strlenW(data
);
320 WCHAR
*buffer
= heap_xalloc((len
+ 2) * sizeof(WCHAR
));
322 for (i
= 0, destindex
= 0; i
< len
; i
++, destindex
++)
324 if (!separator
&& data
[i
] == '\\' && data
[i
+ 1] == '0')
326 buffer
[destindex
] = 0;
329 else if (data
[i
] == separator
)
330 buffer
[destindex
] = 0;
332 buffer
[destindex
] = data
[i
];
334 if (destindex
&& !buffer
[destindex
- 1] && (!buffer
[destindex
] || destindex
== 1))
337 output_message(STRING_INVALID_STRING
);
341 buffer
[destindex
] = 0;
342 if (destindex
&& buffer
[destindex
- 1])
343 buffer
[++destindex
] = 0;
344 *reg_count
= (destindex
+ 1) * sizeof(WCHAR
);
345 return (BYTE
*)buffer
;
348 output_message(STRING_UNHANDLED_TYPE
, reg_type
, data
);
354 static BOOL
sane_path(const WCHAR
*key
)
356 unsigned int i
= strlenW(key
);
358 if (i
< 3 || (key
[i
- 1] == '\\' && key
[i
- 2] == '\\'))
360 output_message(STRING_INVALID_KEY
);
364 if (key
[0] == '\\' && key
[1] == '\\' && key
[2] != '\\')
366 output_message(STRING_NO_REMOTE
);
373 static int reg_add(HKEY root
, WCHAR
*path
, WCHAR
*value_name
, BOOL value_empty
,
374 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
378 if (RegCreateKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
380 output_message(STRING_INVALID_KEY
);
384 if (value_name
|| value_empty
|| data
)
388 BYTE
* reg_data
= NULL
;
392 if (RegQueryValueExW(key
, value_name
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
394 if (!ask_confirm(STRING_OVERWRITE_VALUE
, value_name
))
397 output_message(STRING_CANCELLED
);
403 reg_type
= wchar_get_type(type
);
407 output_message(STRING_UNSUPPORTED_TYPE
, type
);
410 if ((reg_type
== REG_DWORD
|| reg_type
== REG_DWORD_BIG_ENDIAN
) && !data
)
413 output_message(STRING_INVALID_CMDLINE
);
417 if (!(reg_data
= get_regdata(data
, reg_type
, separator
, ®_count
)))
423 RegSetValueExW(key
, value_name
, 0, reg_type
, reg_data
, reg_count
);
428 output_message(STRING_SUCCESS
);
433 static int reg_delete(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
434 BOOL value_empty
, BOOL value_all
, BOOL force
)
442 if (value_name
|| value_empty
)
443 ret
= ask_confirm(STRING_DELETE_VALUE
, value_name
);
445 ret
= ask_confirm(STRING_DELETE_VALUEALL
, key_name
);
447 ret
= ask_confirm(STRING_DELETE_SUBKEY
, key_name
);
451 output_message(STRING_CANCELLED
);
456 /* Delete subtree only if no /v* option is given */
457 if (!value_name
&& !value_empty
&& !value_all
)
459 if (RegDeleteTreeW(root
, path
) != ERROR_SUCCESS
)
461 output_message(STRING_CANNOT_FIND
);
464 output_message(STRING_SUCCESS
);
468 if (RegOpenKeyW(root
, path
, &key
) != ERROR_SUCCESS
)
470 output_message(STRING_CANNOT_FIND
);
476 DWORD max_value_len
= 256, value_len
;
480 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
484 value_len
= max_value_len
;
485 rc
= RegEnumValueW(key
, 0, value_name
, &value_len
, NULL
, NULL
, NULL
, NULL
);
486 if (rc
== ERROR_SUCCESS
)
488 rc
= RegDeleteValueW(key
, value_name
);
489 if (rc
!= ERROR_SUCCESS
)
491 heap_free(value_name
);
493 output_message(STRING_VALUEALL_FAILED
, key_name
);
497 else if (rc
== ERROR_MORE_DATA
)
500 value_name
= heap_xrealloc(value_name
, max_value_len
* sizeof(WCHAR
));
504 heap_free(value_name
);
506 else if (value_name
|| value_empty
)
508 if (RegDeleteValueW(key
, value_empty
? NULL
: value_name
) != ERROR_SUCCESS
)
511 output_message(STRING_CANNOT_FIND
);
517 output_message(STRING_SUCCESS
);
521 static WCHAR
*reg_data_to_wchar(DWORD type
, const BYTE
*src
, DWORD size_bytes
)
523 WCHAR
*buffer
= NULL
;
530 buffer
= heap_xalloc(size_bytes
);
531 strcpyW(buffer
, (WCHAR
*)src
);
537 static const WCHAR fmt
[] = {'%','0','2','X',0};
539 buffer
= heap_xalloc((size_bytes
* 2 + 1) * sizeof(WCHAR
));
541 for (i
= 0; i
< size_bytes
; i
++)
542 ptr
+= sprintfW(ptr
, fmt
, src
[i
]);
546 /* case REG_DWORD_LITTLE_ENDIAN: */
547 case REG_DWORD_BIG_ENDIAN
:
549 const int zero_x_dword
= 10;
550 static const WCHAR fmt
[] = {'0','x','%','x',0};
552 buffer
= heap_xalloc((zero_x_dword
+ 1) * sizeof(WCHAR
));
553 sprintfW(buffer
, fmt
, *(DWORD
*)src
);
558 const int two_wchars
= 2 * sizeof(WCHAR
);
560 const WCHAR
*tmp
= (const WCHAR
*)src
;
563 if (size_bytes
<= two_wchars
)
565 buffer
= heap_xalloc(sizeof(WCHAR
));
570 tmp_size
= size_bytes
- two_wchars
; /* exclude both null terminators */
571 buffer
= heap_xalloc(tmp_size
* 2 + sizeof(WCHAR
));
572 len
= tmp_size
/ sizeof(WCHAR
);
574 for (i
= 0, destindex
= 0; i
< len
; i
++, destindex
++)
577 buffer
[destindex
] = tmp
[i
];
580 buffer
[destindex
++] = '\\';
581 buffer
[destindex
] = '0';
584 buffer
[destindex
] = 0;
591 static const WCHAR
*reg_type_to_wchar(DWORD type
)
593 int i
, array_size
= ARRAY_SIZE(type_rels
);
595 for (i
= 0; i
< array_size
; i
++)
597 if (type
== type_rels
[i
].type
)
598 return type_rels
[i
].name
;
603 static void output_value(const WCHAR
*value_name
, DWORD type
, BYTE
*data
, DWORD data_size
)
605 static const WCHAR fmt
[] = {' ',' ',' ',' ','%','1',0};
609 if (value_name
&& value_name
[0])
610 output_string(fmt
, value_name
);
613 LoadStringW(GetModuleHandleW(NULL
), STRING_DEFAULT_VALUE
, defval
, ARRAY_SIZE(defval
));
614 output_string(fmt
, defval
);
616 output_string(fmt
, reg_type_to_wchar(type
));
620 reg_data
= reg_data_to_wchar(type
, data
, data_size
);
621 output_string(fmt
, reg_data
);
626 LoadStringW(GetModuleHandleW(NULL
), STRING_VALUE_NOT_SET
, defval
, ARRAY_SIZE(defval
));
627 output_string(fmt
, defval
);
629 output_string(newlineW
);
632 WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
635 static const WCHAR fmt
[] = {'%','s','\\','%','s',0};
637 subkey_path
= heap_xalloc((path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
638 sprintfW(subkey_path
, fmt
, path
, subkey_name
);
643 static unsigned int num_values_found
= 0;
645 static int query_value(HKEY key
, WCHAR
*value_name
, WCHAR
*path
, BOOL recurse
)
648 DWORD max_data_bytes
= 2048, data_size
;
650 DWORD type
, path_len
, i
;
652 WCHAR fmt
[] = {'%','1','\n',0};
653 WCHAR
*subkey_name
, *subkey_path
;
656 data
= heap_xalloc(max_data_bytes
);
660 data_size
= max_data_bytes
;
661 rc
= RegQueryValueExW(key
, value_name
, NULL
, &type
, data
, &data_size
);
662 if (rc
== ERROR_MORE_DATA
)
664 max_data_bytes
= data_size
;
665 data
= heap_xrealloc(data
, max_data_bytes
);
670 if (rc
== ERROR_SUCCESS
)
672 output_string(fmt
, path
);
673 output_value(value_name
, type
, data
, data_size
);
674 output_string(newlineW
);
682 if (rc
== ERROR_FILE_NOT_FOUND
)
684 if (value_name
&& *value_name
)
686 output_message(STRING_CANNOT_FIND
);
689 output_string(fmt
, path
);
690 output_value(NULL
, REG_SZ
, NULL
, 0);
695 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
697 path_len
= strlenW(path
);
702 subkey_len
= MAX_SUBKEY_LEN
;
703 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
704 if (rc
== ERROR_SUCCESS
)
706 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
707 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
709 query_value(subkey
, value_name
, subkey_path
, recurse
);
712 heap_free(subkey_path
);
718 heap_free(subkey_name
);
722 static int query_all(HKEY key
, WCHAR
*path
, BOOL recurse
)
725 DWORD max_value_len
= 256, value_len
;
726 DWORD max_data_bytes
= 2048, data_size
;
728 DWORD i
, type
, path_len
;
729 WCHAR fmt
[] = {'%','1','\n',0};
730 WCHAR fmt_path
[] = {'%','1','\\','%','2','\n',0};
731 WCHAR
*value_name
, *subkey_name
, *subkey_path
;
735 output_string(fmt
, path
);
737 value_name
= heap_xalloc(max_value_len
* sizeof(WCHAR
));
738 data
= heap_xalloc(max_data_bytes
);
743 value_len
= max_value_len
;
744 data_size
= max_data_bytes
;
745 rc
= RegEnumValueW(key
, i
, value_name
, &value_len
, NULL
, &type
, data
, &data_size
);
746 if (rc
== ERROR_SUCCESS
)
748 output_value(value_name
, type
, data
, data_size
);
751 else if (rc
== ERROR_MORE_DATA
)
753 if (data_size
> max_data_bytes
)
755 max_data_bytes
= data_size
;
756 data
= heap_xrealloc(data
, max_data_bytes
);
761 value_name
= heap_xrealloc(value_name
, max_value_len
* sizeof(WCHAR
));
768 heap_free(value_name
);
771 output_string(newlineW
);
773 subkey_name
= heap_xalloc(MAX_SUBKEY_LEN
* sizeof(WCHAR
));
775 path_len
= strlenW(path
);
780 subkey_len
= MAX_SUBKEY_LEN
;
781 rc
= RegEnumKeyExW(key
, i
, subkey_name
, &subkey_len
, NULL
, NULL
, NULL
, NULL
);
782 if (rc
== ERROR_SUCCESS
)
786 subkey_path
= build_subkey_path(path
, path_len
, subkey_name
, subkey_len
);
787 if (!RegOpenKeyExW(key
, subkey_name
, 0, KEY_READ
, &subkey
))
789 query_all(subkey
, subkey_path
, recurse
);
792 heap_free(subkey_path
);
794 else output_string(fmt_path
, path
, subkey_name
);
800 heap_free(subkey_name
);
803 output_string(newlineW
);
808 static int reg_query(HKEY root
, WCHAR
*path
, WCHAR
*key_name
, WCHAR
*value_name
,
809 BOOL value_empty
, BOOL recurse
)
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
= heap_xalloc((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
= heap_xalloc((len
+ 1) * sizeof(WCHAR
));
859 sprintfW(long_key
, fmt
, root_rels
[i
].long_name
, path
);
863 BOOL
parse_registry_key(const WCHAR
*key
, HKEY
*root
, WCHAR
**path
, WCHAR
**long_key
)
868 *path
= strchrW(key
, '\\');
869 if (*path
) (*path
)++;
871 *root
= path_get_rootkey(key
);
874 if (*path
) *(*path
- 1) = 0;
875 output_message(STRING_INVALID_SYSTEM_KEY
, key
);
879 *long_key
= get_long_key(*root
, *path
);
884 static BOOL
is_switch(const WCHAR
*s
, const WCHAR c
)
889 if ((s
[0] == '/' || s
[0] == '-') && (s
[1] == c
|| s
[1] == toupperW(c
)))
895 static BOOL
is_help_switch(const WCHAR
*s
)
897 if (is_switch(s
, '?') || is_switch(s
, 'h'))
912 static enum operations
get_operation(const WCHAR
*str
, int *op_help
)
914 struct op_info
{ const WCHAR
*op
; int id
; int help_id
; };
916 static const WCHAR add
[] = {'a','d','d',0};
917 static const WCHAR
delete[] = {'d','e','l','e','t','e',0};
918 static const WCHAR import
[] = {'i','m','p','o','r','t',0};
919 static const WCHAR export
[] = {'e','x','p','o','r','t',0};
920 static const WCHAR query
[] = {'q','u','e','r','y',0};
922 static const struct op_info op_array
[] =
924 { add
, REG_ADD
, STRING_ADD_USAGE
},
925 { delete, REG_DELETE
, STRING_DELETE_USAGE
},
926 { import
, REG_IMPORT
, STRING_IMPORT_USAGE
},
927 { export
, REG_EXPORT
, STRING_EXPORT_USAGE
},
928 { query
, REG_QUERY
, STRING_QUERY_USAGE
},
932 const struct op_info
*ptr
;
934 for (ptr
= op_array
; ptr
->op
; ptr
++)
936 if (!lstrcmpiW(str
, ptr
->op
))
938 *op_help
= ptr
->help_id
;
946 int wmain(int argc
, WCHAR
*argvW
[])
948 int i
, op
, op_help
, ret
;
949 BOOL show_op_help
= FALSE
;
950 static const WCHAR switchVAW
[] = {'v','a',0};
951 static const WCHAR switchVEW
[] = {'v','e',0};
952 WCHAR
*key_name
, *path
, *value_name
= NULL
, *type
= NULL
, *data
= NULL
, separator
= '\0';
953 BOOL value_empty
= FALSE
, value_all
= FALSE
, recurse
= FALSE
, force
= FALSE
;
958 output_message(STRING_INVALID_SYNTAX
);
959 output_message(STRING_REG_HELP
);
963 if (is_help_switch(argvW
[1]))
965 output_message(STRING_USAGE
);
969 op
= get_operation(argvW
[1], &op_help
);
971 if (op
== REG_INVALID
)
973 output_message(STRING_INVALID_OPTION
, argvW
[1]);
974 output_message(STRING_REG_HELP
);
979 show_op_help
= is_help_switch(argvW
[2]);
981 if (argc
== 2 || ((show_op_help
|| op
== REG_IMPORT
) && argc
> 3))
983 output_message(STRING_INVALID_SYNTAX
);
984 output_message(STRING_FUNC_HELP
, struprW(argvW
[1]));
987 else if (show_op_help
)
989 output_message(op_help
);
993 if (op
== REG_IMPORT
)
994 return reg_import(argvW
[2]);
996 if (op
== REG_EXPORT
)
997 return reg_export(argc
, argvW
);
999 if (!parse_registry_key(argvW
[2], &root
, &path
, &key_name
))
1002 for (i
= 3; i
< argc
; i
++)
1004 if (argvW
[i
][0] == '/' || argvW
[i
][0] == '-')
1006 WCHAR
*ptr
= &argvW
[i
][1];
1008 if (!lstrcmpiW(ptr
, switchVEW
))
1013 else if (!lstrcmpiW(ptr
, switchVAW
))
1018 else if (!ptr
[0] || ptr
[1])
1020 output_message(STRING_INVALID_CMDLINE
);
1024 switch(tolowerW(argvW
[i
][1]))
1027 if (value_name
|| !(value_name
= argvW
[++i
]))
1029 output_message(STRING_INVALID_CMDLINE
);
1034 if (type
|| !(type
= argvW
[++i
]))
1036 output_message(STRING_INVALID_CMDLINE
);
1041 if (data
|| !(data
= argvW
[++i
]))
1043 output_message(STRING_INVALID_CMDLINE
);
1048 if (op
== REG_QUERY
)
1055 if (!ptr
|| strlenW(ptr
) != 1)
1057 output_message(STRING_INVALID_CMDLINE
);
1066 output_message(STRING_INVALID_CMDLINE
);
1072 if ((value_name
&& value_empty
) || (value_name
&& value_all
) || (value_empty
&& value_all
))
1074 output_message(STRING_INVALID_CMDLINE
);
1079 ret
= reg_add(root
, path
, value_name
, value_empty
, type
, separator
, data
, force
);
1080 else if (op
== REG_DELETE
)
1081 ret
= reg_delete(root
, path
, key_name
, value_name
, value_empty
, value_all
, force
);
1083 ret
= reg_query(root
, path
, key_name
, value_name
, value_empty
, recurse
);