wininet: Handle async mode in HTTPREQ_ReadFile.
[wine.git] / programs / reg / reg.c
blobc9903ac442bfbe649d285f0afa35fe88bc3bdfef
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 <windows.h>
20 #include <wine/unicode.h>
21 #include <wine/debug.h>
22 #include <errno.h>
23 #include "reg.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};
40 static const struct
42 HKEY key;
43 const WCHAR *short_name;
44 const WCHAR *long_name;
46 root_rels[] =
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};
64 static const struct
66 DWORD type;
67 const WCHAR *name;
69 type_rels[] =
71 {REG_NONE, type_none},
72 {REG_SZ, type_sz},
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)
83 DWORD count, ret;
85 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
86 if (!ret)
88 DWORD len;
89 char *msgA;
91 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
92 * back to WriteFile(), assuming the console encoding is still the right
93 * one in that case.
95 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
96 msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
97 if (!msgA) return;
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)
107 WCHAR *str;
108 DWORD len;
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));
116 return;
118 output_writeconsole(str, len);
119 LocalFree(str);
122 static void __cdecl output_message(unsigned int id, ...)
124 WCHAR fmt[1024];
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());
130 return;
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)
149 HMODULE hmod;
150 WCHAR Ybuffer[4];
151 WCHAR Nbuffer[4];
152 WCHAR defval[32];
153 WCHAR answer[MAX_PATH];
154 WCHAR *str;
155 DWORD count;
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 && strlenW(reg_info)) ? reg_info : defval;
164 while (1)
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])
171 return TRUE;
172 if (answer[0] == Nbuffer[0])
173 return FALSE;
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)
187 DWORD i;
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;
196 return NULL;
199 static DWORD wchar_get_type(const WCHAR *type_name)
201 DWORD i;
203 if (!type_name)
204 return REG_SZ;
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;
212 return ~0u;
215 /* hexchar_to_byte from programs/regedit/hexedit.c */
216 static inline BYTE hexchar_to_byte(WCHAR ch)
218 if (ch >= '0' && ch <= '9')
219 return ch - '0';
220 else if (ch >= 'a' && ch <= 'f')
221 return ch - 'a' + 10;
222 else if (ch >= 'A' && ch <= 'F')
223 return ch - 'A' + 10;
224 else
225 return -1;
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;
232 *reg_count = 0;
234 if (!data) data = &empty;
236 switch (reg_type)
238 case REG_NONE:
239 case REG_SZ:
240 case REG_EXPAND_SZ:
242 *reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
243 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
244 lstrcpyW((LPWSTR)out_data,data);
245 break;
247 case REG_DWORD:
248 /* case REG_DWORD_LITTLE_ENDIAN: */
249 case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
251 LPWSTR rest;
252 DWORD val;
253 val = strtoulW(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
254 if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
255 output_message(STRING_MISSING_INTEGER);
256 break;
258 *reg_count = sizeof(DWORD);
259 out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
260 ((LPDWORD)out_data)[0] = val;
261 break;
263 case REG_BINARY:
265 BYTE hex0, hex1;
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);
269 if(datalen % 2)
271 hex1 = hexchar_to_byte(data[i++]);
272 if(hex1 == 0xFF)
273 goto no_hex_data;
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)
281 goto no_hex_data;
282 out_data[destByteIndex++] = (hex0 << 4) | hex1;
284 break;
285 no_hex_data:
286 /* cleanup, print error */
287 HeapFree(GetProcessHeap(), 0, out_data);
288 output_message(STRING_MISSING_HEXDATA);
289 out_data = NULL;
290 break;
292 case REG_MULTI_SZ:
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;
302 i++;
304 else if (data[i] == separator)
305 buffer[destindex] = 0;
306 else
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);
313 return NULL;
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;
322 default:
323 output_message(STRING_UNHANDLED_TYPE, reg_type, data);
326 return out_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);
336 return FALSE;
339 if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
341 output_message(STRING_NO_REMOTE);
342 return FALSE;
345 return TRUE;
348 static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
349 WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
351 LPWSTR p;
352 HKEY root,subkey;
354 if (!sane_path(key_name))
355 return 1;
357 p = strchrW(key_name,'\\');
358 if (p) p++;
360 root = path_get_rootkey(key_name);
361 if (!root)
363 output_message(STRING_INVALID_KEY);
364 return 1;
367 if (value_name && value_empty)
369 output_message(STRING_INVALID_CMDLINE);
370 return 1;
373 if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
375 output_message(STRING_INVALID_KEY);
376 return 1;
379 if (value_name || value_empty || data)
381 DWORD reg_type;
382 DWORD reg_count = 0;
383 BYTE* reg_data = NULL;
385 if (!force)
387 if (RegQueryValueExW(subkey, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
389 if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
391 RegCloseKey(subkey);
392 output_message(STRING_CANCELLED);
393 return 0;
398 reg_type = wchar_get_type(type);
399 if (reg_type == ~0u)
401 RegCloseKey(subkey);
402 output_message(STRING_UNSUPPORTED_TYPE, type);
403 return 1;
405 if ((reg_type == REG_DWORD || reg_type == REG_DWORD_BIG_ENDIAN) && !data)
407 RegCloseKey(subkey);
408 output_message(STRING_INVALID_CMDLINE);
409 return 1;
412 if (!(reg_data = get_regdata(data, reg_type, separator, &reg_count)))
414 RegCloseKey(subkey);
415 return 1;
418 RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
419 HeapFree(GetProcessHeap(),0,reg_data);
422 RegCloseKey(subkey);
423 output_message(STRING_SUCCESS);
425 return 0;
428 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
429 BOOL value_all, BOOL force)
431 LPWSTR p;
432 HKEY root,subkey;
434 if (!sane_path(key_name))
435 return 1;
437 p = strchrW(key_name,'\\');
438 if (p) p++;
440 root = path_get_rootkey(key_name);
441 if (!root)
443 output_message(STRING_INVALID_KEY);
444 return 1;
447 if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
449 output_message(STRING_INVALID_CMDLINE);
450 return 1;
453 if (!force)
455 BOOL ret;
457 if (value_name || value_empty)
458 ret = ask_confirm(STRING_DELETE_VALUE, value_name);
459 else if (value_all)
460 ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
461 else
462 ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
464 if (!ret)
466 output_message(STRING_CANCELLED);
467 return 0;
471 /* Delete subtree only if no /v* option is given */
472 if (!value_name && !value_empty && !value_all)
474 if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
476 output_message(STRING_CANNOT_FIND);
477 return 1;
479 output_message(STRING_SUCCESS);
480 return 0;
483 if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
485 output_message(STRING_CANNOT_FIND);
486 return 1;
489 if (value_all)
491 LPWSTR szValue;
492 DWORD maxValue;
493 DWORD count;
494 LONG rc;
496 rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
497 &maxValue, NULL, NULL, NULL);
498 if (rc != ERROR_SUCCESS)
500 RegCloseKey(subkey);
501 output_message(STRING_GENERAL_FAILURE);
502 return 1;
504 maxValue++;
505 szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
507 while (1)
509 count = maxValue;
510 rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
511 if (rc == ERROR_SUCCESS)
513 rc = RegDeleteValueW(subkey, szValue);
514 if (rc != ERROR_SUCCESS)
516 HeapFree(GetProcessHeap(), 0, szValue);
517 RegCloseKey(subkey);
518 output_message(STRING_VALUEALL_FAILED, key_name);
519 return 1;
522 else break;
525 else if (value_name || value_empty)
527 if (RegDeleteValueW(subkey, value_empty ? NULL : value_name) != ERROR_SUCCESS)
529 RegCloseKey(subkey);
530 output_message(STRING_CANNOT_FIND);
531 return 1;
535 RegCloseKey(subkey);
536 output_message(STRING_SUCCESS);
537 return 0;
540 static WCHAR *reg_data_to_wchar(DWORD type, const BYTE *src, DWORD size_bytes)
542 WCHAR *buffer = NULL;
543 int i;
545 switch (type)
547 case REG_SZ:
548 case REG_EXPAND_SZ:
549 buffer = HeapAlloc(GetProcessHeap(), 0, size_bytes);
550 strcpyW(buffer, (WCHAR *)src);
551 break;
552 case REG_NONE:
553 case REG_BINARY:
555 WCHAR *ptr;
556 WCHAR fmt[] = {'%','0','2','X',0};
558 buffer = HeapAlloc(GetProcessHeap(), 0, (size_bytes * 2 + 1) * sizeof(WCHAR));
559 ptr = buffer;
560 for (i = 0; i < size_bytes; i++)
561 ptr += sprintfW(ptr, fmt, src[i]);
562 break;
564 case REG_DWORD:
565 /* case REG_DWORD_LITTLE_ENDIAN: */
566 case REG_DWORD_BIG_ENDIAN:
568 const int zero_x_dword = 10;
569 WCHAR fmt[] = {'0','x','%','x',0};
571 buffer = HeapAlloc(GetProcessHeap(), 0, (zero_x_dword + 1) * sizeof(WCHAR));
572 sprintfW(buffer, fmt, *(DWORD *)src);
573 break;
575 case REG_MULTI_SZ:
577 const int two_wchars = 2 * sizeof(WCHAR);
578 DWORD tmp_size;
579 const WCHAR *tmp = (const WCHAR *)src;
580 int len, destindex;
582 if (size_bytes <= two_wchars)
584 buffer = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
585 *buffer = 0;
586 return buffer;
589 tmp_size = size_bytes - two_wchars; /* exclude both null terminators */
590 buffer = HeapAlloc(GetProcessHeap(), 0, tmp_size * 2 + sizeof(WCHAR));
591 len = tmp_size / sizeof(WCHAR);
593 for (i = 0, destindex = 0; i < len; i++, destindex++)
595 if (tmp[i])
596 buffer[destindex] = tmp[i];
597 else
599 buffer[destindex++] = '\\';
600 buffer[destindex] = '0';
603 buffer[destindex] = 0;
604 break;
607 return buffer;
610 static const WCHAR *reg_type_to_wchar(DWORD type)
612 int i, array_size = ARRAY_SIZE(type_rels);
614 for (i = 0; i < array_size; i++)
616 if (type == type_rels[i].type)
617 return type_rels[i].name;
619 return NULL;
622 static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size)
624 WCHAR fmt[] = {' ',' ',' ',' ','%','1',0};
625 WCHAR *reg_data;
626 WCHAR newlineW[] = {'\n',0};
628 if (value_name && value_name[0])
629 output_string(fmt, value_name);
630 else
632 WCHAR defval[32];
633 LoadStringW(GetModuleHandleW(NULL), STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
634 output_string(fmt, defval);
636 output_string(fmt, reg_type_to_wchar(type));
637 reg_data = reg_data_to_wchar(type, data, data_size);
638 output_string(fmt, reg_data);
639 HeapFree(GetProcessHeap(), 0, reg_data);
640 output_string(newlineW);
643 static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
645 WCHAR *subkey_path;
646 WCHAR fmt[] = {'%','s','\\','%','s',0};
648 subkey_path = HeapAlloc(GetProcessHeap(), 0, (path_len + subkey_len + 2) * sizeof(WCHAR));
649 if (!subkey_path)
651 ERR("Failed to allocate memory for subkey_path\n");
652 return NULL;
654 sprintfW(subkey_path, fmt, path, subkey_name);
655 return subkey_path;
658 static int query_value(HKEY key, WCHAR *value_name, WCHAR *path, BOOL recurse)
660 LONG rc;
661 DWORD num_subkeys, max_subkey_len, subkey_len;
662 DWORD max_data_bytes, data_size;
663 DWORD type, path_len, i;
664 BYTE *data;
665 WCHAR fmt[] = {'%','1','\n',0};
666 WCHAR newlineW[] = {'\n',0};
667 WCHAR *subkey_name, *subkey_path;
668 HKEY subkey;
670 rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, &num_subkeys, &max_subkey_len,
671 NULL, NULL, NULL, &max_data_bytes, NULL, NULL);
672 if (rc)
674 ERR("RegQueryInfoKey failed: %d\n", rc);
675 return 1;
678 data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
679 if (!data)
681 ERR("Failed to allocate memory for data\n");
682 return 1;
685 data_size = max_data_bytes;
686 rc = RegQueryValueExW(key, value_name, NULL, &type, data, &data_size);
687 if (rc == ERROR_SUCCESS)
689 output_string(fmt, path);
690 output_value(value_name, type, data, data_size);
691 output_string(newlineW);
694 HeapFree(GetProcessHeap(), 0, data);
696 if (!recurse)
698 if (rc == ERROR_FILE_NOT_FOUND)
700 output_message(STRING_CANNOT_FIND);
701 return 1;
703 return 0;
706 max_subkey_len++;
707 subkey_name = HeapAlloc(GetProcessHeap(), 0, max_subkey_len * sizeof(WCHAR));
708 if (!subkey_name)
710 ERR("Failed to allocate memory for subkey_name\n");
711 return 1;
714 path_len = strlenW(path);
716 for (i = 0; i < num_subkeys; i++)
718 subkey_len = max_subkey_len;
719 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
720 if (rc == ERROR_SUCCESS)
722 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
723 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
725 query_value(subkey, value_name, subkey_path, recurse);
726 RegCloseKey(subkey);
728 HeapFree(GetProcessHeap(), 0, subkey_path);
732 HeapFree(GetProcessHeap(), 0, subkey_name);
733 return 0;
736 static int query_all(HKEY key, WCHAR *path, BOOL recurse)
738 LONG rc;
739 DWORD num_subkeys, max_subkey_len, subkey_len;
740 DWORD num_values, max_value_len, value_len;
741 DWORD max_data_bytes, data_size;
742 DWORD i, type, path_len;
743 WCHAR fmt[] = {'%','1','\n',0};
744 WCHAR fmt_path[] = {'%','1','\\','%','2','\n',0};
745 WCHAR *value_name, *subkey_name, *subkey_path;
746 WCHAR newlineW[] = {'\n',0};
747 BYTE *data;
748 HKEY subkey;
750 rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, &num_subkeys, &max_subkey_len, NULL,
751 &num_values, &max_value_len, &max_data_bytes, NULL, NULL);
752 if (rc)
754 ERR("RegQueryInfoKey failed: %d\n", rc);
755 return 1;
758 output_string(fmt, path);
760 max_value_len++;
761 value_name = HeapAlloc(GetProcessHeap(), 0, max_value_len * sizeof(WCHAR));
762 if (!value_name)
764 ERR("Failed to allocate memory for value_name\n");
765 return 1;
768 data = HeapAlloc(GetProcessHeap(), 0, max_data_bytes);
769 if (!data)
771 HeapFree(GetProcessHeap(), 0, value_name);
772 ERR("Failed to allocate memory for data\n");
773 return 1;
776 for (i = 0; i < num_values; i++)
778 value_len = max_value_len;
779 data_size = max_data_bytes;
780 rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size);
781 if (rc == ERROR_SUCCESS)
782 output_value(value_name, type, data, data_size);
785 HeapFree(GetProcessHeap(), 0, data);
786 HeapFree(GetProcessHeap(), 0, value_name);
788 if (num_values || recurse)
789 output_string(newlineW);
791 max_subkey_len++;
792 subkey_name = HeapAlloc(GetProcessHeap(), 0, max_subkey_len * sizeof(WCHAR));
793 if (!subkey_name)
795 ERR("Failed to allocate memory for subkey_name\n");
796 return 1;
799 path_len = strlenW(path);
801 for (i = 0; i < num_subkeys; i++)
803 subkey_len = max_subkey_len;
804 rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
805 if (rc == ERROR_SUCCESS)
807 if (recurse)
809 subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
810 if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
812 query_all(subkey, subkey_path, recurse);
813 RegCloseKey(subkey);
815 HeapFree(GetProcessHeap(), 0, subkey_path);
817 else output_string(fmt_path, path, subkey_name);
821 HeapFree(GetProcessHeap(), 0, subkey_name);
823 if (num_subkeys && !recurse)
824 output_string(newlineW);
826 return 0;
829 static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, BOOL recurse)
831 WCHAR *p;
832 HKEY root, key;
833 int ret;
835 if (!sane_path(key_name))
836 return 1;
838 if (value_name && value_empty)
840 output_message(STRING_INVALID_CMDLINE);
841 return 1;
844 root = path_get_rootkey(key_name);
845 if (!root)
847 output_message(STRING_INVALID_KEY);
848 return 1;
851 p = strchrW(key_name, '\\');
852 if (p) p++;
854 if (RegOpenKeyExW(root, p, 0, KEY_READ, &key) != ERROR_SUCCESS)
856 output_message(STRING_CANNOT_FIND);
857 return 1;
860 if (value_name || value_empty)
861 ret = query_value(key, value_name, key_name, recurse);
862 else
863 ret = query_all(key, key_name, recurse);
865 RegCloseKey(key);
867 return ret;
870 int wmain(int argc, WCHAR *argvW[])
872 int i;
874 static const WCHAR addW[] = {'a','d','d',0};
875 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
876 static const WCHAR queryW[] = {'q','u','e','r','y',0};
877 static const WCHAR slashDW[] = {'/','d',0};
878 static const WCHAR slashFW[] = {'/','f',0};
879 static const WCHAR slashHW[] = {'/','h',0};
880 static const WCHAR slashSW[] = {'/','s',0};
881 static const WCHAR slashTW[] = {'/','t',0};
882 static const WCHAR slashVW[] = {'/','v',0};
883 static const WCHAR slashVAW[] = {'/','v','a',0};
884 static const WCHAR slashVEW[] = {'/','v','e',0};
885 static const WCHAR slashHelpW[] = {'/','?',0};
887 if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
888 || !lstrcmpiW(argvW[1], slashHW))
890 output_message(STRING_USAGE);
891 return 0;
894 if (!lstrcmpiW(argvW[1], addW))
896 WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
897 BOOL value_empty = FALSE, force = FALSE;
899 if (argc < 3)
901 output_message(STRING_INVALID_CMDLINE);
902 return 1;
904 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
905 !lstrcmpiW(argvW[2], slashHW)))
907 output_message(STRING_ADD_USAGE);
908 return 0;
911 key_name = argvW[2];
912 for (i = 1; i < argc; i++)
914 if (!lstrcmpiW(argvW[i], slashVW))
916 if (value_name || !(value_name = argvW[++i]))
918 output_message(STRING_INVALID_CMDLINE);
919 return 1;
922 else if (!lstrcmpiW(argvW[i], slashVEW))
923 value_empty = TRUE;
924 else if (!lstrcmpiW(argvW[i], slashTW))
925 type = argvW[++i];
926 else if (!lstrcmpiW(argvW[i], slashSW))
928 WCHAR *ptr = argvW[++i];
930 if (!ptr || strlenW(ptr) != 1)
932 output_message(STRING_INVALID_CMDLINE);
933 return 1;
935 separator = ptr[0];
937 else if (!lstrcmpiW(argvW[i], slashDW))
939 if (!(data = argvW[++i]))
941 output_message(STRING_INVALID_CMDLINE);
942 return 1;
945 else if (!lstrcmpiW(argvW[i], slashFW))
946 force = TRUE;
948 return reg_add(key_name, value_name, value_empty, type, separator,
949 data, force);
951 else if (!lstrcmpiW(argvW[1], deleteW))
953 WCHAR *key_name, *value_name = NULL;
954 BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
956 if (argc < 3)
958 output_message(STRING_INVALID_CMDLINE);
959 return 1;
961 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
962 !lstrcmpiW(argvW[2], slashHW)))
964 output_message(STRING_DELETE_USAGE);
965 return 0;
968 key_name = argvW[2];
969 for (i = 1; i < argc; i++)
971 if (!lstrcmpiW(argvW[i], slashVW))
973 if (value_name || !(value_name = argvW[++i]))
975 output_message(STRING_INVALID_CMDLINE);
976 return 1;
979 else if (!lstrcmpiW(argvW[i], slashVEW))
980 value_empty = TRUE;
981 else if (!lstrcmpiW(argvW[i], slashVAW))
982 value_all = TRUE;
983 else if (!lstrcmpiW(argvW[i], slashFW))
984 force = TRUE;
986 return reg_delete(key_name, value_name, value_empty, value_all, force);
988 else if (!lstrcmpiW(argvW[1], queryW))
990 WCHAR *key_name, *value_name = NULL;
991 BOOL value_empty = FALSE, recurse = FALSE;
993 if (argc < 3)
995 output_message(STRING_INVALID_CMDLINE);
996 return 1;
998 else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
999 !lstrcmpiW(argvW[2], slashHW)))
1001 output_message(STRING_QUERY_USAGE);
1002 return 0;
1005 key_name = argvW[2];
1006 for (i = 1; i < argc; i++)
1008 if (!lstrcmpiW(argvW[i], slashVW))
1010 if (value_name || !(value_name = argvW[++i]))
1012 output_message(STRING_INVALID_CMDLINE);
1013 return 1;
1016 else if (!lstrcmpiW(argvW[i], slashVEW))
1017 value_empty = TRUE;
1018 else if (!lstrcmpiW(argvW[i], slashSW))
1019 recurse = TRUE;
1021 return reg_query(key_name, value_name, value_empty, recurse);
1023 else
1025 output_message(STRING_INVALID_CMDLINE);
1026 return 1;