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
21 #include <wine/debug.h>
23 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
25 static const WCHAR short_hklm
[] = {'H','K','L','M',0};
26 static const WCHAR short_hkcu
[] = {'H','K','C','U',0};
27 static const WCHAR short_hkcr
[] = {'H','K','C','R',0};
28 static const WCHAR short_hku
[] = {'H','K','U',0};
29 static const WCHAR short_hkcc
[] = {'H','K','C','C',0};
30 static const WCHAR long_hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
31 static const WCHAR long_hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
32 static const WCHAR long_hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
33 static const WCHAR long_hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0};
34 static const WCHAR long_hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
39 const WCHAR
*short_name
;
40 const WCHAR
*long_name
;
44 {HKEY_LOCAL_MACHINE
, short_hklm
, long_hklm
},
45 {HKEY_CURRENT_USER
, short_hkcu
, long_hkcu
},
46 {HKEY_CLASSES_ROOT
, short_hkcr
, long_hkcr
},
47 {HKEY_USERS
, short_hku
, long_hku
},
48 {HKEY_CURRENT_CONFIG
, short_hkcc
, long_hkcc
},
51 static const WCHAR type_none
[] = {'R','E','G','_','N','O','N','E',0};
52 static const WCHAR type_sz
[] = {'R','E','G','_','S','Z',0};
53 static const WCHAR type_expand_sz
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
54 static const WCHAR type_binary
[] = {'R','E','G','_','B','I','N','A','R','Y',0};
55 static const WCHAR type_dword
[] = {'R','E','G','_','D','W','O','R','D',0};
56 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};
57 static const WCHAR type_dword_be
[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
58 static const WCHAR type_multi_sz
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
60 const struct reg_type_rels type_rels
[] =
62 {REG_NONE
, type_none
},
64 {REG_EXPAND_SZ
, type_expand_sz
},
65 {REG_BINARY
, type_binary
},
66 {REG_DWORD
, type_dword
},
67 {REG_DWORD_LITTLE_ENDIAN
, type_dword_le
},
68 {REG_DWORD_BIG_ENDIAN
, type_dword_be
},
69 {REG_MULTI_SZ
, type_multi_sz
},
72 void *heap_xalloc(size_t size
)
74 void *buf
= heap_alloc(size
);
77 ERR("Out of memory!\n");
83 void *heap_xrealloc(void *buf
, size_t size
)
85 void *new_buf
= heap_realloc(buf
, size
);
89 ERR("Out of memory!\n");
96 void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
100 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
);
106 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
107 * back to WriteFile(), assuming the console encoding is still the right
110 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, wlen
, NULL
, 0, NULL
, NULL
);
111 msgA
= heap_xalloc(len
);
113 WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, wlen
, msgA
, len
, NULL
, NULL
);
114 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
119 static void output_formatstring(const WCHAR
*fmt
, __ms_va_list va_args
)
124 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
125 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
126 if (len
== 0 && GetLastError() != ERROR_NO_WORK_DONE
)
128 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
131 output_writeconsole(str
, len
);
135 void WINAPIV
output_message(unsigned int id
, ...)
138 __ms_va_list va_args
;
140 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
142 WINE_FIXME("LoadString failed with %d\n", GetLastError());
145 __ms_va_start(va_args
, id
);
146 output_formatstring(fmt
, va_args
);
147 __ms_va_end(va_args
);
150 void WINAPIV
output_string(const WCHAR
*fmt
, ...)
152 __ms_va_list va_args
;
154 __ms_va_start(va_args
, fmt
);
155 output_formatstring(fmt
, va_args
);
156 __ms_va_end(va_args
);
159 /* ask_confirm() adapted from programs/cmd/builtins.c */
160 BOOL
ask_confirm(unsigned int msgid
, WCHAR
*reg_info
)
166 WCHAR answer
[MAX_PATH
];
170 hmod
= GetModuleHandleW(NULL
);
171 LoadStringW(hmod
, STRING_YES
, Ybuffer
, ARRAY_SIZE(Ybuffer
));
172 LoadStringW(hmod
, STRING_NO
, Nbuffer
, ARRAY_SIZE(Nbuffer
));
173 LoadStringW(hmod
, STRING_DEFAULT_VALUE
, defval
, ARRAY_SIZE(defval
));
175 str
= (reg_info
&& *reg_info
) ? reg_info
: defval
;
179 output_message(msgid
, str
);
180 output_message(STRING_YESNO
);
181 ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE
), answer
, ARRAY_SIZE(answer
), &count
, NULL
);
182 answer
[0] = towupper(answer
[0]);
183 if (answer
[0] == Ybuffer
[0])
185 if (answer
[0] == Nbuffer
[0])
190 static inline BOOL
path_rootname_cmp(const WCHAR
*input_path
, const WCHAR
*rootkey_name
)
192 DWORD length
= lstrlenW(rootkey_name
);
194 return (!wcsnicmp(input_path
, rootkey_name
, length
) &&
195 (input_path
[length
] == 0 || input_path
[length
] == '\\'));
198 HKEY
path_get_rootkey(const WCHAR
*path
)
202 for (i
= 0; i
< ARRAY_SIZE(root_rels
); i
++)
204 if (path_rootname_cmp(path
, root_rels
[i
].short_name
) ||
205 path_rootname_cmp(path
, root_rels
[i
].long_name
))
206 return root_rels
[i
].key
;
212 static BOOL
sane_path(const WCHAR
*key
)
214 unsigned int i
= lstrlenW(key
);
216 if (i
< 3 || (key
[i
- 1] == '\\' && key
[i
- 2] == '\\'))
218 output_message(STRING_INVALID_KEY
);
222 if (key
[0] == '\\' && key
[1] == '\\' && key
[2] != '\\')
224 output_message(STRING_NO_REMOTE
);
231 WCHAR
*build_subkey_path(WCHAR
*path
, DWORD path_len
, WCHAR
*subkey_name
, DWORD subkey_len
)
234 static const WCHAR fmt
[] = {'%','s','\\','%','s',0};
236 subkey_path
= heap_xalloc((path_len
+ subkey_len
+ 2) * sizeof(WCHAR
));
237 swprintf(subkey_path
, path_len
+ subkey_len
+ 2, fmt
, path
, subkey_name
);
242 static WCHAR
*get_long_key(HKEY root
, WCHAR
*path
)
244 DWORD i
, array_size
= ARRAY_SIZE(root_rels
), len
;
246 WCHAR fmt
[] = {'%','s','\\','%','s',0};
248 for (i
= 0; i
< array_size
; i
++)
250 if (root
== root_rels
[i
].key
)
254 len
= lstrlenW(root_rels
[i
].long_name
);
258 long_key
= heap_xalloc((len
+ 1) * sizeof(WCHAR
));
259 lstrcpyW(long_key
, root_rels
[i
].long_name
);
263 len
+= lstrlenW(path
) + 1; /* add one for the backslash */
264 long_key
= heap_xalloc((len
+ 1) * sizeof(WCHAR
));
265 swprintf(long_key
, len
+ 1, fmt
, root_rels
[i
].long_name
, path
);
269 BOOL
parse_registry_key(const WCHAR
*key
, HKEY
*root
, WCHAR
**path
, WCHAR
**long_key
)
274 *path
= wcschr(key
, '\\');
275 if (*path
) (*path
)++;
277 *root
= path_get_rootkey(key
);
280 if (*path
) *(*path
- 1) = 0;
281 output_message(STRING_INVALID_SYSTEM_KEY
, key
);
285 *long_key
= get_long_key(*root
, *path
);
290 BOOL
is_switch(const WCHAR
*s
, const WCHAR c
)
295 if ((s
[0] == '/' || s
[0] == '-') && (s
[1] == c
|| s
[1] == towupper(c
)))
301 static BOOL
is_help_switch(const WCHAR
*s
)
303 return (is_switch(s
, '?') || is_switch(s
, 'h'));
315 static enum operations
get_operation(const WCHAR
*str
, int *op_help
)
317 struct op_info
{ const WCHAR
*op
; int id
; int help_id
; };
319 static const WCHAR add
[] = {'a','d','d',0};
320 static const WCHAR
delete[] = {'d','e','l','e','t','e',0};
321 static const WCHAR import
[] = {'i','m','p','o','r','t',0};
322 static const WCHAR export
[] = {'e','x','p','o','r','t',0};
323 static const WCHAR query
[] = {'q','u','e','r','y',0};
325 static const struct op_info op_array
[] =
327 { add
, REG_ADD
, STRING_ADD_USAGE
},
328 { delete, REG_DELETE
, STRING_DELETE_USAGE
},
329 { import
, REG_IMPORT
, STRING_IMPORT_USAGE
},
330 { export
, REG_EXPORT
, STRING_EXPORT_USAGE
},
331 { query
, REG_QUERY
, STRING_QUERY_USAGE
},
335 const struct op_info
*ptr
;
337 for (ptr
= op_array
; ptr
->op
; ptr
++)
339 if (!lstrcmpiW(str
, ptr
->op
))
341 *op_help
= ptr
->help_id
;
349 int __cdecl
wmain(int argc
, WCHAR
*argvW
[])
351 int i
, op
, op_help
, ret
;
352 static const WCHAR switchVAW
[] = {'v','a',0};
353 static const WCHAR switchVEW
[] = {'v','e',0};
354 WCHAR
*key_name
, *path
, *value_name
= NULL
, *type
= NULL
, *data
= NULL
, separator
= '\0';
355 BOOL value_empty
= FALSE
, value_all
= FALSE
, recurse
= FALSE
, force
= FALSE
;
360 output_message(STRING_INVALID_SYNTAX
);
361 output_message(STRING_REG_HELP
);
365 if (is_help_switch(argvW
[1]))
367 output_message(STRING_USAGE
);
371 op
= get_operation(argvW
[1], &op_help
);
373 if (op
== REG_INVALID
)
375 output_message(STRING_INVALID_OPTION
, argvW
[1]);
376 output_message(STRING_REG_HELP
);
379 else if (argc
== 2) /* Valid operation, no arguments supplied */
381 output_message(STRING_INVALID_SYNTAX
);
382 output_message(STRING_FUNC_HELP
, wcsupr(argvW
[1]));
386 if (is_help_switch(argvW
[2]))
388 output_message(op_help
);
392 if (op
== REG_IMPORT
)
393 return reg_import(argc
, argvW
);
395 if (op
== REG_EXPORT
)
396 return reg_export(argc
, argvW
);
398 if (!parse_registry_key(argvW
[2], &root
, &path
, &key_name
))
401 for (i
= 3; i
< argc
; i
++)
403 if (argvW
[i
][0] == '/' || argvW
[i
][0] == '-')
405 WCHAR
*ptr
= &argvW
[i
][1];
407 if (!lstrcmpiW(ptr
, switchVEW
))
412 else if (!lstrcmpiW(ptr
, switchVAW
))
417 else if (!ptr
[0] || ptr
[1])
419 output_message(STRING_INVALID_CMDLINE
);
423 switch(towlower(argvW
[i
][1]))
426 if (value_name
|| !(value_name
= argvW
[++i
]))
428 output_message(STRING_INVALID_CMDLINE
);
433 if (type
|| !(type
= argvW
[++i
]))
435 output_message(STRING_INVALID_CMDLINE
);
440 if (data
|| !(data
= argvW
[++i
]))
442 output_message(STRING_INVALID_CMDLINE
);
454 if (!ptr
|| lstrlenW(ptr
) != 1)
456 output_message(STRING_INVALID_CMDLINE
);
465 output_message(STRING_INVALID_CMDLINE
);
471 if ((value_name
&& value_empty
) || (value_name
&& value_all
) || (value_empty
&& value_all
))
473 output_message(STRING_INVALID_CMDLINE
);
478 ret
= reg_add(root
, path
, value_name
, value_empty
, type
, separator
, data
, force
);
479 else if (op
== REG_DELETE
)
480 ret
= reg_delete(root
, path
, key_name
, value_name
, value_empty
, value_all
, force
);
482 ret
= reg_query(root
, path
, key_name
, value_name
, value_empty
, recurse
);