1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* Copyright 2008 Andrew Riedi
* Copyright 2016-2017, 2021 Hugh McMaster
*
* 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 "reg.h"
#include <wine/debug.h>
WINE_DEFAULT_DEBUG_CHANNEL(reg);
static const struct
{
HKEY key;
const WCHAR *short_name;
const WCHAR *long_name;
}
root_rels[] =
{
{HKEY_LOCAL_MACHINE, L"HKLM", L"HKEY_LOCAL_MACHINE"},
{HKEY_CURRENT_USER, L"HKCU", L"HKEY_CURRENT_USER"},
{HKEY_CLASSES_ROOT, L"HKCR", L"HKEY_CLASSES_ROOT"},
{HKEY_USERS, L"HKU", L"HKEY_USERS"},
{HKEY_CURRENT_CONFIG, L"HKCC", L"HKEY_CURRENT_CONFIG"},
};
const struct reg_type_rels type_rels[] =
{
{REG_NONE, L"REG_NONE"},
{REG_SZ, L"REG_SZ"},
{REG_EXPAND_SZ, L"REG_EXPAND_SZ"},
{REG_BINARY, L"REG_BINARY"},
{REG_DWORD, L"REG_DWORD"},
{REG_DWORD_LITTLE_ENDIAN, L"REG_DWORD_LITTLE_ENDIAN"},
{REG_DWORD_BIG_ENDIAN, L"REG_DWORD_BIG_ENDIAN"},
{REG_QWORD, L"REG_QWORD"},
{REG_MULTI_SZ, L"REG_MULTI_SZ"},
};
void output_writeconsole(const WCHAR *str, DWORD wlen)
{
DWORD count;
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL))
{
DWORD len;
char *msgA;
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
* back to WriteFile() with OEM code page.
*/
len = WideCharToMultiByte(GetOEMCP(), 0, str, wlen, NULL, 0, NULL, NULL);
msgA = malloc(len);
WideCharToMultiByte(GetOEMCP(), 0, str, wlen, msgA, len, NULL, NULL);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
free(msgA);
}
}
static void output_formatstring(const WCHAR *fmt, va_list va_args)
{
WCHAR *str;
DWORD len;
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
{
WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
return;
}
output_writeconsole(str, len);
LocalFree(str);
}
void WINAPIV output_message(unsigned int id, ...)
{
WCHAR *fmt = NULL;
int len;
va_list va_args;
if (!(len = LoadStringW(GetModuleHandleW(NULL), id, (WCHAR *)&fmt, 0)))
{
WINE_FIXME("LoadString failed with %ld\n", GetLastError());
return;
}
len++;
fmt = malloc(len * sizeof(WCHAR));
if (!fmt) return;
LoadStringW(GetModuleHandleW(NULL), id, fmt, len);
va_start(va_args, id);
output_formatstring(fmt, va_args);
va_end(va_args);
free(fmt);
}
void WINAPIV output_string(const WCHAR *fmt, ...)
{
va_list va_args;
va_start(va_args, fmt);
output_formatstring(fmt, va_args);
va_end(va_args);
}
/* ask_confirm() adapted from programs/cmd/builtins.c */
BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
{
HMODULE hmod;
WCHAR Ybuffer[4];
WCHAR Nbuffer[4];
WCHAR defval[32];
WCHAR answer[MAX_PATH];
WCHAR *str;
DWORD count;
hmod = GetModuleHandleW(NULL);
LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
str = (reg_info && *reg_info) ? reg_info : defval;
while (1)
{
output_message(msgid, str);
output_message(STRING_YESNO);
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
answer[0] = towupper(answer[0]);
if (answer[0] == Ybuffer[0])
return TRUE;
if (answer[0] == Nbuffer[0])
return FALSE;
}
}
static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
{
DWORD length = lstrlenW(rootkey_name);
return (!wcsnicmp(input_path, rootkey_name, length) &&
(input_path[length] == 0 || input_path[length] == '\\'));
}
HKEY path_get_rootkey(const WCHAR *path)
{
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;
}
static BOOL sane_path(const WCHAR *key)
{
unsigned int i = lstrlenW(key);
if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
{
output_message(STRING_INVALID_KEY);
return FALSE;
}
if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
{
output_message(STRING_NO_REMOTE);
return FALSE;
}
return TRUE;
}
WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
{
WCHAR *subkey_path;
subkey_path = malloc((path_len + subkey_len + 2) * sizeof(WCHAR));
swprintf(subkey_path, path_len + subkey_len + 2, L"%s\\%s", path, subkey_name);
return subkey_path;
}
WCHAR *get_long_key(HKEY root, WCHAR *path)
{
int i, len;
WCHAR *long_key;
for (i = 0; i < ARRAY_SIZE(root_rels); i++)
{
if (root == root_rels[i].key)
break;
}
len = lstrlenW(root_rels[i].long_name);
if (!path)
{
long_key = malloc((len + 1) * sizeof(WCHAR));
lstrcpyW(long_key, root_rels[i].long_name);
return long_key;
}
len += lstrlenW(path) + 1; /* add one for the concatenating backslash */
long_key = malloc((len + 1) * sizeof(WCHAR));
swprintf(long_key, len + 1, L"%s\\%s", root_rels[i].long_name, path);
return long_key;
}
BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path)
{
WCHAR *p;
if (!sane_path(key))
return FALSE;
*root = path_get_rootkey(key);
if (!*root)
{
output_message(STRING_INVALID_SYSTEM_KEY);
return FALSE;
}
*path = wcschr(key, '\\');
if (!*path)
return TRUE;
(*path)++;
if (!**path)
{
output_message(STRING_INVALID_SYSTEM_KEY);
return FALSE;
}
p = *path + lstrlenW(*path) - 1;
if (*p == '\\') *p = 0;
return TRUE;
}
BOOL is_char(const WCHAR s, const WCHAR c)
{
return (s == c || s == towupper(c));
}
BOOL is_switch(const WCHAR *s, const WCHAR c)
{
if (lstrlenW(s) > 2)
return FALSE;
return ((s[0] == '/' || s[0] == '-') && is_char(s[1], c));
}
static BOOL is_help_switch(const WCHAR *s)
{
return (is_switch(s, '?') || is_switch(s, 'h'));
}
enum operations {
REG_ADD,
REG_COPY,
REG_DELETE,
REG_EXPORT,
REG_IMPORT,
REG_QUERY,
REG_INVALID
};
static enum operations get_operation(const WCHAR *str, int *op_help)
{
struct op_info { const WCHAR *op; int id; int help_id; };
static const struct op_info op_array[] =
{
{ L"add", REG_ADD, STRING_ADD_USAGE },
{ L"copy", REG_COPY, STRING_COPY_USAGE },
{ L"delete", REG_DELETE, STRING_DELETE_USAGE },
{ L"export", REG_EXPORT, STRING_EXPORT_USAGE },
{ L"import", REG_IMPORT, STRING_IMPORT_USAGE },
{ L"query", REG_QUERY, STRING_QUERY_USAGE },
{ NULL, -1, 0 }
};
const struct op_info *ptr;
for (ptr = op_array; ptr->op; ptr++)
{
if (!lstrcmpiW(str, ptr->op))
{
*op_help = ptr->help_id;
return ptr->id;
}
}
return REG_INVALID;
}
int __cdecl wmain(int argc, WCHAR *argvW[])
{
int op, op_help;
if (argc == 1)
{
output_message(STRING_INVALID_SYNTAX);
output_message(STRING_REG_HELP);
return 1;
}
if (is_help_switch(argvW[1]))
{
output_message(STRING_USAGE);
return 0;
}
op = get_operation(argvW[1], &op_help);
if (op == REG_INVALID)
{
output_message(STRING_INVALID_OPTION, argvW[1]);
output_message(STRING_REG_HELP);
return 1;
}
else if (argc == 2) /* Valid operation, no arguments supplied */
goto invalid;
if (is_help_switch(argvW[2]))
{
if (argc > 3) goto invalid;
output_message(op_help);
output_message(STRING_REG_VIEW_USAGE);
return 0;
}
if (op == REG_ADD)
return reg_add(argc, argvW);
if (op == REG_COPY)
return reg_copy(argc, argvW);
if (op == REG_DELETE)
return reg_delete(argc, argvW);
if (op == REG_EXPORT)
return reg_export(argc, argvW);
if (op == REG_IMPORT)
return reg_import(argc, argvW);
return reg_query(argc, argvW);
invalid:
output_message(STRING_INVALID_SYNTAX);
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
return 1;
}
|