reg: Move 'import' syntax checks to reg_import().
[wine.git] / programs / reg / reg.c
blobdaeec5f5718a77d4ffb17c55f2b727b5d7af14a3
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 <stdlib.h>
20 #include "reg.h"
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};
36 static const struct
38 HKEY key;
39 const WCHAR *short_name;
40 const WCHAR *long_name;
42 root_rels[] =
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},
63 {REG_SZ, type_sz},
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);
75 if (!buf)
77 ERR("Out of memory!\n");
78 exit(1);
80 return buf;
83 void *heap_xrealloc(void *buf, size_t size)
85 void *new_buf = heap_realloc(buf, size);
87 if (!new_buf)
89 ERR("Out of memory!\n");
90 exit(1);
93 return new_buf;
96 void output_writeconsole(const WCHAR *str, DWORD wlen)
98 DWORD count, ret;
100 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
101 if (!ret)
103 DWORD len;
104 char *msgA;
106 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
107 * back to WriteFile(), assuming the console encoding is still the right
108 * one in that case.
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);
115 heap_free(msgA);
119 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
121 WCHAR *str;
122 DWORD len;
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));
129 return;
131 output_writeconsole(str, len);
132 LocalFree(str);
135 void WINAPIV output_message(unsigned int id, ...)
137 WCHAR fmt[1024];
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());
143 return;
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)
162 HMODULE hmod;
163 WCHAR Ybuffer[4];
164 WCHAR Nbuffer[4];
165 WCHAR defval[32];
166 WCHAR answer[MAX_PATH];
167 WCHAR *str;
168 DWORD count;
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;
177 while (1)
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])
184 return TRUE;
185 if (answer[0] == Nbuffer[0])
186 return FALSE;
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)
200 DWORD i;
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;
209 return NULL;
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);
219 return FALSE;
222 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
224 output_message(STRING_NO_REMOTE);
225 return FALSE;
228 return TRUE;
231 WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
233 WCHAR *subkey_path;
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);
239 return subkey_path;
242 static WCHAR *get_long_key(HKEY root, WCHAR *path)
244 DWORD i, array_size = ARRAY_SIZE(root_rels), len;
245 WCHAR *long_key;
246 WCHAR fmt[] = {'%','s','\\','%','s',0};
248 for (i = 0; i < array_size; i++)
250 if (root == root_rels[i].key)
251 break;
254 len = lstrlenW(root_rels[i].long_name);
256 if (!path)
258 long_key = heap_xalloc((len + 1) * sizeof(WCHAR));
259 lstrcpyW(long_key, root_rels[i].long_name);
260 return long_key;
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);
266 return long_key;
269 BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key)
271 if (!sane_path(key))
272 return FALSE;
274 *path = wcschr(key, '\\');
275 if (*path) (*path)++;
277 *root = path_get_rootkey(key);
278 if (!*root)
280 if (*path) *(*path - 1) = 0;
281 output_message(STRING_INVALID_SYSTEM_KEY, key);
282 return FALSE;
285 *long_key = get_long_key(*root, *path);
287 return TRUE;
290 BOOL is_switch(const WCHAR *s, const WCHAR c)
292 if (lstrlenW(s) > 2)
293 return FALSE;
295 if ((s[0] == '/' || s[0] == '-') && (s[1] == c || s[1] == towupper(c)))
296 return TRUE;
298 return FALSE;
301 static BOOL is_help_switch(const WCHAR *s)
303 return (is_switch(s, '?') || is_switch(s, 'h'));
306 enum operations {
307 REG_ADD,
308 REG_DELETE,
309 REG_IMPORT,
310 REG_EXPORT,
311 REG_QUERY,
312 REG_INVALID
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 },
332 { NULL, -1, 0 }
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;
342 return ptr->id;
346 return REG_INVALID;
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;
356 HKEY root;
358 if (argc == 1)
360 output_message(STRING_INVALID_SYNTAX);
361 output_message(STRING_REG_HELP);
362 return 1;
365 if (is_help_switch(argvW[1]))
367 output_message(STRING_USAGE);
368 return 0;
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);
377 return 1;
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]));
383 return 1;
386 if (is_help_switch(argvW[2]))
388 output_message(op_help);
389 return 0;
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))
399 return 1;
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))
409 value_empty = TRUE;
410 continue;
412 else if (!lstrcmpiW(ptr, switchVAW))
414 value_all = TRUE;
415 continue;
417 else if (!ptr[0] || ptr[1])
419 output_message(STRING_INVALID_CMDLINE);
420 return 1;
423 switch(towlower(argvW[i][1]))
425 case 'v':
426 if (value_name || !(value_name = argvW[++i]))
428 output_message(STRING_INVALID_CMDLINE);
429 return 1;
431 break;
432 case 't':
433 if (type || !(type = argvW[++i]))
435 output_message(STRING_INVALID_CMDLINE);
436 return 1;
438 break;
439 case 'd':
440 if (data || !(data = argvW[++i]))
442 output_message(STRING_INVALID_CMDLINE);
443 return 1;
445 break;
446 case 's':
447 if (op == REG_QUERY)
449 recurse = TRUE;
450 break;
453 ptr = argvW[++i];
454 if (!ptr || lstrlenW(ptr) != 1)
456 output_message(STRING_INVALID_CMDLINE);
457 return 1;
459 separator = ptr[0];
460 break;
461 case 'f':
462 force = TRUE;
463 break;
464 default:
465 output_message(STRING_INVALID_CMDLINE);
466 return 1;
471 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
473 output_message(STRING_INVALID_CMDLINE);
474 return 1;
477 if (op == REG_ADD)
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);
481 else
482 ret = reg_query(root, path, key_name, value_name, value_empty, recurse);
483 return ret;