2008-03-19 07:36:28 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Andrew Riedi
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
2008-11-20 20:49:30 +01:00
|
|
|
#include <wine/unicode.h>
|
2016-02-09 12:54:03 +01:00
|
|
|
#include <wine/debug.h>
|
2016-02-22 10:29:18 +01:00
|
|
|
#include <errno.h>
|
2008-03-19 07:36:28 +01:00
|
|
|
#include "reg.h"
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
|
|
|
|
|
2016-02-09 12:54:03 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(reg);
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
static const WCHAR short_hklm[] = {'H','K','L','M',0};
|
|
|
|
static const WCHAR short_hkcu[] = {'H','K','C','U',0};
|
|
|
|
static const WCHAR short_hkcr[] = {'H','K','C','R',0};
|
|
|
|
static const WCHAR short_hku[] = {'H','K','U',0};
|
|
|
|
static const WCHAR short_hkcc[] = {'H','K','C','C',0};
|
|
|
|
static const WCHAR long_hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
|
|
|
|
static const WCHAR long_hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
|
|
|
|
static const WCHAR long_hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
|
|
|
|
static const WCHAR long_hku[] = {'H','K','E','Y','_','U','S','E','R','S',0};
|
|
|
|
static const WCHAR long_hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
HKEY key;
|
|
|
|
const WCHAR *short_name;
|
|
|
|
const WCHAR *long_name;
|
|
|
|
}
|
|
|
|
root_rels[] =
|
|
|
|
{
|
|
|
|
{HKEY_LOCAL_MACHINE, short_hklm, long_hklm},
|
|
|
|
{HKEY_CURRENT_USER, short_hkcu, long_hkcu},
|
|
|
|
{HKEY_CLASSES_ROOT, short_hkcr, long_hkcr},
|
|
|
|
{HKEY_USERS, short_hku, long_hku},
|
|
|
|
{HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc},
|
|
|
|
};
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
static const WCHAR type_none[] = {'R','E','G','_','N','O','N','E',0};
|
|
|
|
static const WCHAR type_sz[] = {'R','E','G','_','S','Z',0};
|
|
|
|
static const WCHAR type_expand_sz[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
|
|
|
|
static const WCHAR type_binary[] = {'R','E','G','_','B','I','N','A','R','Y',0};
|
|
|
|
static const WCHAR type_dword[] = {'R','E','G','_','D','W','O','R','D',0};
|
|
|
|
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};
|
|
|
|
static const WCHAR type_dword_be[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
|
|
|
|
static const WCHAR type_multi_sz[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
const WCHAR *name;
|
|
|
|
}
|
|
|
|
type_rels[] =
|
|
|
|
{
|
|
|
|
{REG_NONE, type_none},
|
|
|
|
{REG_SZ, type_sz},
|
|
|
|
{REG_EXPAND_SZ, type_expand_sz},
|
|
|
|
{REG_BINARY, type_binary},
|
|
|
|
{REG_DWORD, type_dword},
|
|
|
|
{REG_DWORD_LITTLE_ENDIAN, type_dword_le},
|
|
|
|
{REG_DWORD_BIG_ENDIAN, type_dword_be},
|
|
|
|
{REG_MULTI_SZ, type_multi_sz},
|
|
|
|
};
|
|
|
|
|
2016-02-09 12:54:03 +01:00
|
|
|
static void output_writeconsole(const WCHAR *str, DWORD wlen)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
|
|
|
DWORD count, ret;
|
|
|
|
|
2016-02-09 12:54:02 +01:00
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
|
2008-03-19 07:36:28 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
2011-08-24 14:29:31 +02:00
|
|
|
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
|
|
|
|
* back to WriteFile(), assuming the console encoding is still the right
|
|
|
|
* one in that case.
|
|
|
|
*/
|
2016-02-09 12:54:02 +01:00
|
|
|
len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
|
2008-03-19 07:36:28 +01:00
|
|
|
msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
|
2016-02-09 12:54:02 +01:00
|
|
|
if (!msgA) return;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:02 +01:00
|
|
|
WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
|
2008-03-19 07:36:28 +01:00
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, msgA);
|
|
|
|
}
|
2016-02-09 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
|
|
|
|
{
|
2016-02-09 12:54:03 +01:00
|
|
|
WCHAR *str;
|
|
|
|
DWORD len;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:03 +01:00
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
|
|
|
|
if (len == 0 && GetLastError() != NO_ERROR)
|
|
|
|
{
|
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
output_writeconsole(str, len);
|
|
|
|
LocalFree(str);
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 12:54:04 +01:00
|
|
|
static void __cdecl output_message(unsigned int id, ...)
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
WCHAR fmt[1024];
|
|
|
|
__ms_va_list va_args;
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2016-02-09 12:54:04 +01:00
|
|
|
if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
|
|
|
|
{
|
|
|
|
WINE_FIXME("LoadString failed with %d\n", GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__ms_va_start(va_args, id);
|
|
|
|
output_formatstring(fmt, va_args);
|
|
|
|
__ms_va_end(va_args);
|
2016-02-09 12:54:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __cdecl output_string(const WCHAR *fmt, ...)
|
|
|
|
{
|
|
|
|
__ms_va_list va_args;
|
|
|
|
|
|
|
|
__ms_va_start(va_args, fmt);
|
|
|
|
output_formatstring(fmt, va_args);
|
|
|
|
__ms_va_end(va_args);
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 12:54:07 +01:00
|
|
|
/* ask_confirm() adapted from programs/cmd/builtins.c */
|
|
|
|
static BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
|
|
|
|
{
|
|
|
|
HMODULE hmod;
|
|
|
|
WCHAR Ybuffer[4];
|
|
|
|
WCHAR Nbuffer[4];
|
2016-02-10 12:23:17 +01:00
|
|
|
WCHAR defval[32];
|
2016-02-09 12:54:07 +01:00
|
|
|
WCHAR answer[MAX_PATH];
|
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
hmod = GetModuleHandleW(NULL);
|
|
|
|
LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
|
|
|
|
LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
|
2016-02-10 12:23:17 +01:00
|
|
|
LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
|
2016-02-09 12:54:07 +01:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2016-02-10 12:23:17 +01:00
|
|
|
output_message(msgid, reg_info ? reg_info : defval);
|
2016-02-09 12:54:07 +01:00
|
|
|
output_message(STRING_YESNO);
|
|
|
|
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
|
|
|
|
answer[0] = toupperW(answer[0]);
|
|
|
|
if (answer[0] == Ybuffer[0])
|
|
|
|
return TRUE;
|
|
|
|
if (answer[0] == Nbuffer[0])
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
|
2014-11-19 08:54:41 +01:00
|
|
|
{
|
2015-10-04 21:14:46 +02:00
|
|
|
DWORD length = strlenW(rootkey_name);
|
2014-11-19 08:54:41 +01:00
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
return (!strncmpiW(input_path, rootkey_name, length) &&
|
|
|
|
(input_path[length] == 0 || input_path[length] == '\\'));
|
2014-11-19 08:54:41 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
static HKEY path_get_rootkey(const WCHAR *path)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2015-10-04 21:14:46 +02:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(root_rels); i++)
|
|
|
|
{
|
|
|
|
if (path_rootname_cmp(path, root_rels[i].short_name) ||
|
|
|
|
path_rootname_cmp(path, root_rels[i].long_name))
|
|
|
|
return root_rels[i].key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
static DWORD wchar_get_type(const WCHAR *type_name)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2015-02-19 13:19:12 +01:00
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (!type_name)
|
2009-01-28 14:14:07 +01:00
|
|
|
return REG_SZ;
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE(type_rels); i++)
|
|
|
|
{
|
|
|
|
if (!strcmpiW(type_rels[i].name, type_name))
|
|
|
|
return type_rels[i].type;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
return ~0u;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:18 +02:00
|
|
|
/* hexchar_to_byte from programs/regedit/hexedit.c */
|
|
|
|
static inline BYTE hexchar_to_byte(WCHAR ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
return ch - '0';
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
return ch - 'a' + 10;
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
return ch - 'A' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-28 14:14:07 +01:00
|
|
|
static LPBYTE get_regdata(LPWSTR data, DWORD reg_type, WCHAR separator, DWORD *reg_count)
|
|
|
|
{
|
|
|
|
LPBYTE out_data = NULL;
|
|
|
|
*reg_count = 0;
|
|
|
|
|
|
|
|
switch (reg_type)
|
|
|
|
{
|
2016-02-15 13:15:31 +01:00
|
|
|
case REG_NONE:
|
2009-01-28 14:14:07 +01:00
|
|
|
case REG_SZ:
|
2016-02-15 13:15:32 +01:00
|
|
|
case REG_EXPAND_SZ:
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
|
|
|
*reg_count = (lstrlenW(data) + 1) * sizeof(WCHAR);
|
|
|
|
out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
|
|
|
|
lstrcpyW((LPWSTR)out_data,data);
|
|
|
|
break;
|
|
|
|
}
|
2010-02-03 15:04:55 +01:00
|
|
|
case REG_DWORD:
|
2016-02-15 13:15:33 +01:00
|
|
|
/* case REG_DWORD_LITTLE_ENDIAN: */
|
|
|
|
case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
|
2010-02-03 15:04:55 +01:00
|
|
|
{
|
|
|
|
LPWSTR rest;
|
|
|
|
DWORD val;
|
2016-02-22 10:29:14 +01:00
|
|
|
val = strtoulW(data, &rest, (tolowerW(data[1]) == 'x') ? 16 : 10);
|
2016-02-22 10:29:18 +01:00
|
|
|
if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_MISSING_INTEGER);
|
2010-02-03 15:04:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*reg_count = sizeof(DWORD);
|
|
|
|
out_data = HeapAlloc(GetProcessHeap(),0,*reg_count);
|
|
|
|
((LPDWORD)out_data)[0] = val;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-06 16:07:18 +02:00
|
|
|
case REG_BINARY:
|
|
|
|
{
|
|
|
|
BYTE hex0, hex1;
|
|
|
|
int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
|
|
|
|
*reg_count = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
|
|
|
|
out_data = HeapAlloc(GetProcessHeap(), 0, *reg_count);
|
|
|
|
if(datalen % 2)
|
|
|
|
{
|
|
|
|
hex1 = hexchar_to_byte(data[i++]);
|
|
|
|
if(hex1 == 0xFF)
|
|
|
|
goto no_hex_data;
|
|
|
|
out_data[destByteIndex++] = hex1;
|
|
|
|
}
|
|
|
|
for(;i + 1 < datalen;i += 2)
|
|
|
|
{
|
|
|
|
hex0 = hexchar_to_byte(data[i]);
|
|
|
|
hex1 = hexchar_to_byte(data[i + 1]);
|
|
|
|
if(hex0 == 0xFF || hex1 == 0xFF)
|
|
|
|
goto no_hex_data;
|
|
|
|
out_data[destByteIndex++] = (hex0 << 4) | hex1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
no_hex_data:
|
|
|
|
/* cleanup, print error */
|
|
|
|
HeapFree(GetProcessHeap(), 0, out_data);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_MISSING_HEXDATA);
|
2015-09-06 16:07:18 +02:00
|
|
|
out_data = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-15 13:15:34 +01:00
|
|
|
case REG_MULTI_SZ:
|
|
|
|
/* FIXME: Needs handling */
|
2009-01-28 14:14:07 +01:00
|
|
|
default:
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_UNHANDLED_TYPE, reg_type, data);
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return out_data;
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
static BOOL sane_path(const WCHAR *key)
|
|
|
|
{
|
2015-02-19 13:19:11 +01:00
|
|
|
unsigned int i = strlenW(key);
|
|
|
|
|
|
|
|
if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2015-02-19 13:19:11 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_NO_REMOTE);
|
2015-02-19 13:19:10 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-19 07:36:28 +01:00
|
|
|
static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
|
|
|
|
WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
|
|
|
|
{
|
2009-01-28 14:14:07 +01:00
|
|
|
LPWSTR p;
|
|
|
|
HKEY root,subkey;
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
if (!sane_path(key_name))
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
|
|
|
|
p = strchrW(key_name,'\\');
|
|
|
|
if (!p)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
root = path_get_rootkey(key_name);
|
2009-01-28 14:14:07 +01:00
|
|
|
if (!root)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 12:23:15 +01:00
|
|
|
if (value_name && value_empty)
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-28 14:14:07 +01:00
|
|
|
if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value_name || data)
|
|
|
|
{
|
|
|
|
DWORD reg_type;
|
|
|
|
DWORD reg_count = 0;
|
|
|
|
BYTE* reg_data = NULL;
|
|
|
|
|
|
|
|
if (!force)
|
|
|
|
{
|
2016-02-09 12:54:07 +01:00
|
|
|
if (RegQueryValueExW(subkey, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
2016-02-09 12:54:07 +01:00
|
|
|
if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
|
|
|
|
{
|
2016-02-15 13:15:29 +01:00
|
|
|
RegCloseKey(subkey);
|
2016-02-09 12:54:07 +01:00
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:19:12 +01:00
|
|
|
reg_type = wchar_get_type(type);
|
|
|
|
if (reg_type == ~0u)
|
2009-01-28 14:14:07 +01:00
|
|
|
{
|
|
|
|
RegCloseKey(subkey);
|
2016-02-09 12:54:05 +01:00
|
|
|
output_message(STRING_UNSUPPORTED_TYPE, type);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:07 +01:00
|
|
|
}
|
2016-02-15 13:15:30 +01:00
|
|
|
if (reg_type == REG_DWORD && !data)
|
|
|
|
{
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2016-02-15 13:15:35 +01:00
|
|
|
if (data && !(reg_data = get_regdata(data, reg_type, separator, ®_count)))
|
|
|
|
{
|
|
|
|
RegCloseKey(subkey);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-01-28 14:14:07 +01:00
|
|
|
|
|
|
|
RegSetValueExW(subkey,value_name,0,reg_type,reg_data,reg_count);
|
|
|
|
HeapFree(GetProcessHeap(),0,reg_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(subkey);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-01-28 14:14:07 +01:00
|
|
|
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
|
|
|
|
BOOL value_all, BOOL force)
|
|
|
|
{
|
2009-01-28 14:14:18 +01:00
|
|
|
LPWSTR p;
|
|
|
|
HKEY root,subkey;
|
|
|
|
|
2015-02-19 13:19:10 +01:00
|
|
|
if (!sane_path(key_name))
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
|
|
|
|
p = strchrW(key_name,'\\');
|
|
|
|
if (!p)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
2015-10-04 21:14:46 +02:00
|
|
|
root = path_get_rootkey(key_name);
|
2009-01-28 14:14:18 +01:00
|
|
|
if (!root)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_KEY);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 12:23:14 +01:00
|
|
|
if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!force)
|
|
|
|
{
|
2016-02-10 12:23:18 +01:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (value_name || value_empty)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUE, value_name);
|
|
|
|
else if (value_all)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
|
|
|
|
else
|
|
|
|
ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
2009-12-06 22:45:48 +01:00
|
|
|
/* Delete subtree only if no /v* option is given */
|
|
|
|
if (!value_name && !value_empty && !value_all)
|
2009-01-28 14:14:18 +01:00
|
|
|
{
|
|
|
|
if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value_all)
|
|
|
|
{
|
|
|
|
LPWSTR szValue;
|
|
|
|
DWORD maxValue;
|
|
|
|
DWORD count;
|
|
|
|
LONG rc;
|
|
|
|
|
|
|
|
rc = RegQueryInfoKeyW(subkey, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
&maxValue, NULL, NULL, NULL);
|
|
|
|
if (rc != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
/* FIXME: failure */
|
|
|
|
RegCloseKey(subkey);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
maxValue++;
|
|
|
|
szValue = HeapAlloc(GetProcessHeap(),0,maxValue*sizeof(WCHAR));
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
count = maxValue;
|
2009-12-06 22:45:48 +01:00
|
|
|
rc = RegEnumValueW(subkey, 0, szValue, &count, NULL, NULL, NULL, NULL);
|
2009-01-28 14:14:18 +01:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
2009-12-06 22:45:48 +01:00
|
|
|
rc = RegDeleteValueW(subkey, szValue);
|
2009-01-28 14:14:18 +01:00
|
|
|
if (rc != ERROR_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
if (rc != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
/* FIXME delete failed */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (value_name)
|
|
|
|
{
|
|
|
|
if (RegDeleteValueW(subkey,value_name) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
RegCloseKey(subkey);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_CANNOT_FIND);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 1;
|
2009-01-28 14:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (value_empty)
|
|
|
|
{
|
|
|
|
RegSetValueExW(subkey,NULL,0,REG_SZ,NULL,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(subkey);
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
2009-07-01 07:41:41 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int reg_query(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
|
|
|
|
BOOL subkey)
|
|
|
|
{
|
2008-11-14 21:14:38 +01:00
|
|
|
static const WCHAR stubW[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
|
2016-02-09 12:54:03 +01:00
|
|
|
'-',' ','%','1',' ','%','2',' ','%','3','!','d','!',' ','%','4','!','d','!','\n',0};
|
2016-02-09 12:54:02 +01:00
|
|
|
output_string(stubW, key_name, value_name, value_empty, subkey);
|
2008-03-19 07:36:28 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain(int argc, WCHAR *argvW[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-11-14 21:14:38 +01:00
|
|
|
static const WCHAR addW[] = {'a','d','d',0};
|
|
|
|
static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
|
|
|
|
static const WCHAR queryW[] = {'q','u','e','r','y',0};
|
|
|
|
static const WCHAR slashDW[] = {'/','d',0};
|
|
|
|
static const WCHAR slashFW[] = {'/','f',0};
|
2009-07-01 07:41:45 +02:00
|
|
|
static const WCHAR slashHW[] = {'/','h',0};
|
2008-11-14 21:14:38 +01:00
|
|
|
static const WCHAR slashSW[] = {'/','s',0};
|
|
|
|
static const WCHAR slashTW[] = {'/','t',0};
|
|
|
|
static const WCHAR slashVW[] = {'/','v',0};
|
|
|
|
static const WCHAR slashVAW[] = {'/','v','a',0};
|
|
|
|
static const WCHAR slashVEW[] = {'/','v','e',0};
|
2009-06-28 00:45:26 +02:00
|
|
|
static const WCHAR slashHelpW[] = {'/','?',0};
|
2008-03-19 07:36:28 +01:00
|
|
|
|
2009-07-01 07:41:45 +02:00
|
|
|
if (argc < 2 || !lstrcmpW(argvW[1], slashHelpW)
|
|
|
|
|| !lstrcmpiW(argvW[1], slashHW))
|
2008-03-19 07:36:28 +01:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_USAGE);
|
2009-06-28 00:45:26 +02:00
|
|
|
return 0;
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!lstrcmpiW(argvW[1], addW))
|
|
|
|
{
|
|
|
|
WCHAR *key_name, *value_name = NULL, *type = NULL, separator = '\0', *data = NULL;
|
|
|
|
BOOL value_empty = FALSE, force = FALSE;
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
2008-03-19 07:36:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-07-01 07:41:45 +02:00
|
|
|
else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
|
|
|
|
!lstrcmpiW(argvW[2], slashHW)))
|
2009-06-28 00:45:26 +02:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_ADD_USAGE);
|
2009-06-28 00:45:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
|
|
|
|
key_name = argvW[2];
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!lstrcmpiW(argvW[i], slashVW))
|
|
|
|
value_name = argvW[++i];
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashVEW))
|
|
|
|
value_empty = TRUE;
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashTW))
|
|
|
|
type = argvW[++i];
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashSW))
|
2016-02-17 13:41:05 +01:00
|
|
|
{
|
|
|
|
WCHAR *ptr = argvW[++i];
|
|
|
|
|
2016-02-17 13:41:07 +01:00
|
|
|
if (!ptr || strlenW(ptr) != 1)
|
2016-02-17 13:41:05 +01:00
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
separator = ptr[0];
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
else if (!lstrcmpiW(argvW[i], slashDW))
|
2016-02-11 13:34:22 +01:00
|
|
|
{
|
|
|
|
if (!(data = argvW[++i]))
|
|
|
|
{
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
else if (!lstrcmpiW(argvW[i], slashFW))
|
|
|
|
force = TRUE;
|
|
|
|
}
|
|
|
|
return reg_add(key_name, value_name, value_empty, type, separator,
|
|
|
|
data, force);
|
|
|
|
}
|
|
|
|
else if (!lstrcmpiW(argvW[1], deleteW))
|
|
|
|
{
|
|
|
|
WCHAR *key_name, *value_name = NULL;
|
|
|
|
BOOL value_empty = FALSE, value_all = FALSE, force = FALSE;
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
2008-03-19 07:36:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-07-01 07:41:45 +02:00
|
|
|
else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
|
|
|
|
!lstrcmpiW(argvW[2], slashHW)))
|
2009-06-28 00:45:26 +02:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_DELETE_USAGE);
|
2009-06-28 00:45:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
|
|
|
|
key_name = argvW[2];
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!lstrcmpiW(argvW[i], slashVW))
|
|
|
|
value_name = argvW[++i];
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashVEW))
|
|
|
|
value_empty = TRUE;
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashVAW))
|
|
|
|
value_all = TRUE;
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashFW))
|
|
|
|
force = TRUE;
|
|
|
|
}
|
|
|
|
return reg_delete(key_name, value_name, value_empty, value_all, force);
|
|
|
|
}
|
|
|
|
else if (!lstrcmpiW(argvW[1], queryW))
|
|
|
|
{
|
|
|
|
WCHAR *key_name, *value_name = NULL;
|
|
|
|
BOOL value_empty = FALSE, subkey = FALSE;
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
2008-03-19 07:36:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-07-01 07:41:45 +02:00
|
|
|
else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
|
|
|
|
!lstrcmpiW(argvW[2], slashHW)))
|
2009-06-28 00:45:26 +02:00
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_QUERY_USAGE);
|
2009-06-28 00:45:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
|
|
|
|
key_name = argvW[2];
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!lstrcmpiW(argvW[i], slashVW))
|
|
|
|
value_name = argvW[++i];
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashVEW))
|
|
|
|
value_empty = TRUE;
|
|
|
|
else if (!lstrcmpiW(argvW[i], slashSW))
|
|
|
|
subkey = TRUE;
|
|
|
|
}
|
|
|
|
return reg_query(key_name, value_name, value_empty, subkey);
|
|
|
|
}
|
2009-06-28 00:38:30 +02:00
|
|
|
else
|
|
|
|
{
|
2016-02-09 12:54:04 +01:00
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
2009-06-28 00:38:30 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-03-19 07:36:28 +01:00
|
|
|
}
|