2002-04-12 01:58:40 +02:00
|
|
|
/*
|
|
|
|
* Windows regedit.exe registry editor implementation.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Andriy Palamarchuk
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-01-23 12:02:39 +01:00
|
|
|
#include <stdlib.h>
|
2002-04-12 01:58:40 +02:00
|
|
|
#include <windows.h>
|
2016-07-05 07:15:22 +02:00
|
|
|
#include <shellapi.h>
|
|
|
|
#include "wine/unicode.h"
|
2016-07-05 07:15:35 +02:00
|
|
|
#include "wine/debug.h"
|
2002-04-12 01:58:40 +02:00
|
|
|
#include "regproc.h"
|
|
|
|
|
2016-07-05 07:15:35 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(regedit);
|
|
|
|
|
2017-01-07 14:38:15 +01:00
|
|
|
static void output_writeconsole(const WCHAR *str, DWORD wlen)
|
2016-07-05 07:15:35 +02:00
|
|
|
{
|
|
|
|
DWORD count, ret;
|
|
|
|
|
|
|
|
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
|
|
|
/* WriteConsole() fails on Windows if its output is redirected. If this occurs,
|
|
|
|
* we should call WriteFile() and assume the console encoding is still correct.
|
|
|
|
*/
|
|
|
|
len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
|
2017-07-27 14:25:53 +02:00
|
|
|
msgA = heap_xalloc(len);
|
2016-07-05 07:15:35 +02:00
|
|
|
|
|
|
|
WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
|
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
2017-07-27 14:25:53 +02:00
|
|
|
heap_free(msgA);
|
2016-07-05 07:15:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
|
|
|
|
{
|
|
|
|
WCHAR *str;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:21:53 +01:00
|
|
|
void WINAPIV output_message(unsigned int id, ...)
|
2016-07-05 07:15:35 +02:00
|
|
|
{
|
|
|
|
WCHAR fmt[1536];
|
|
|
|
__ms_va_list va_args;
|
|
|
|
|
|
|
|
if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, sizeof(fmt)/sizeof(*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);
|
|
|
|
}
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2017-11-02 10:21:53 +01:00
|
|
|
void WINAPIV error_exit(unsigned int id, ...)
|
2017-07-12 13:09:05 +02:00
|
|
|
{
|
|
|
|
WCHAR fmt[1536];
|
|
|
|
__ms_va_list va_args;
|
|
|
|
|
|
|
|
if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, sizeof(fmt)/sizeof(*fmt)))
|
|
|
|
{
|
|
|
|
WINE_FIXME("LoadString failed with %u\n", GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__ms_va_start(va_args, id);
|
|
|
|
output_formatstring(fmt, va_args);
|
|
|
|
__ms_va_end(va_args);
|
|
|
|
|
|
|
|
exit(0); /* regedit.exe always terminates with error code zero */
|
|
|
|
}
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
typedef enum {
|
2016-06-22 14:59:13 +02:00
|
|
|
ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
|
2002-04-12 01:58:40 +02:00
|
|
|
} REGEDIT_ACTION;
|
|
|
|
|
2016-07-05 11:13:35 +02:00
|
|
|
static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
|
2003-08-07 05:10:13 +02:00
|
|
|
{
|
2003-12-08 23:48:07 +01:00
|
|
|
switch (action) {
|
|
|
|
case ACTION_ADD: {
|
2016-07-05 07:15:22 +02:00
|
|
|
WCHAR *filename = argv[*i];
|
|
|
|
WCHAR hyphen[] = {'-',0};
|
2016-08-29 14:06:08 +02:00
|
|
|
WCHAR *realname = NULL;
|
2003-12-08 23:48:07 +01:00
|
|
|
FILE *reg_file;
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
if (!strcmpW(filename, hyphen))
|
|
|
|
reg_file = stdin;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
WCHAR rb_mode[] = {'r','b',0};
|
2007-10-09 21:04:28 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
|
|
|
|
if (size > 0)
|
2004-01-13 06:07:07 +01:00
|
|
|
{
|
2017-07-27 14:25:53 +02:00
|
|
|
realname = heap_xalloc(size * sizeof(WCHAR));
|
2016-07-05 07:15:22 +02:00
|
|
|
size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
|
2004-01-13 06:07:07 +01:00
|
|
|
}
|
2016-07-05 07:15:22 +02:00
|
|
|
if (size == 0)
|
2004-01-13 06:07:07 +01:00
|
|
|
{
|
2016-07-05 07:15:35 +02:00
|
|
|
output_message(STRING_FILE_NOT_FOUND, filename);
|
2017-07-27 14:25:53 +02:00
|
|
|
heap_free(realname);
|
2016-07-05 11:13:35 +02:00
|
|
|
return;
|
2016-07-05 07:15:22 +02:00
|
|
|
}
|
|
|
|
reg_file = _wfopen(realname, rb_mode);
|
|
|
|
if (reg_file == NULL)
|
|
|
|
{
|
|
|
|
WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
|
|
|
|
_wperror(regedit);
|
2016-07-05 07:15:35 +02:00
|
|
|
output_message(STRING_CANNOT_OPEN_FILE, filename);
|
2017-07-27 14:25:53 +02:00
|
|
|
heap_free(realname);
|
2016-07-05 11:13:35 +02:00
|
|
|
return;
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
2016-08-29 14:06:08 +02:00
|
|
|
}
|
|
|
|
import_registry_file(reg_file);
|
|
|
|
if (realname)
|
|
|
|
{
|
2017-07-27 14:25:53 +02:00
|
|
|
heap_free(realname);
|
2016-08-29 14:06:08 +02:00
|
|
|
fclose(reg_file);
|
2003-12-08 23:48:07 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-07-05 07:15:22 +02:00
|
|
|
case ACTION_DELETE:
|
|
|
|
delete_registry_key(argv[*i]);
|
2003-12-08 23:48:07 +01:00
|
|
|
break;
|
|
|
|
case ACTION_EXPORT: {
|
2016-07-05 07:15:22 +02:00
|
|
|
WCHAR *filename = argv[*i];
|
|
|
|
WCHAR *key_name = argv[++(*i)];
|
2003-12-08 23:48:07 +01:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
if (key_name && *key_name)
|
2017-09-18 10:27:43 +02:00
|
|
|
export_registry_key(filename, key_name, REG_FORMAT_5);
|
2016-07-05 07:15:22 +02:00
|
|
|
else
|
2017-09-18 10:27:43 +02:00
|
|
|
export_registry_key(filename, NULL, REG_FORMAT_5);
|
2003-12-08 23:48:07 +01:00
|
|
|
break;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
default:
|
2017-07-12 13:09:05 +02:00
|
|
|
error_exit(STRING_UNHANDLED_ACTION);
|
2002-04-12 01:58:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
BOOL ProcessCmdLine(WCHAR *cmdline)
|
2007-06-13 13:18:50 +02:00
|
|
|
{
|
2016-07-05 07:15:22 +02:00
|
|
|
WCHAR **argv;
|
|
|
|
int argc, i;
|
2016-06-22 14:59:13 +02:00
|
|
|
REGEDIT_ACTION action = ACTION_ADD;
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
argv = CommandLineToArgvW(cmdline, &argc);
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
if (!argv)
|
|
|
|
return FALSE;
|
2016-06-22 14:59:13 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
if (argc == 1)
|
2016-06-22 14:59:13 +02:00
|
|
|
{
|
2016-07-05 07:15:22 +02:00
|
|
|
LocalFree(argv);
|
|
|
|
return FALSE;
|
2016-06-22 14:59:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
for (i = 1; i < argc; i++)
|
2016-06-22 14:59:13 +02:00
|
|
|
{
|
|
|
|
if (argv[i][0] != '/' && argv[i][0] != '-')
|
|
|
|
break; /* No flags specified. */
|
|
|
|
|
|
|
|
if (!argv[i][1] && argv[i][0] == '-')
|
|
|
|
break; /* '-' is a filename. It indicates we should use stdin. */
|
|
|
|
|
|
|
|
if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
|
|
|
|
break; /* This is a file path beginning with '/'. */
|
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
switch (toupperW(argv[i][1]))
|
2007-10-09 21:04:28 +02:00
|
|
|
{
|
2016-06-22 14:59:13 +02:00
|
|
|
case '?':
|
2017-07-12 13:09:05 +02:00
|
|
|
error_exit(STRING_USAGE);
|
2007-10-09 21:04:28 +02:00
|
|
|
break;
|
2016-06-22 14:59:13 +02:00
|
|
|
case 'D':
|
|
|
|
action = ACTION_DELETE;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
action = ACTION_EXPORT;
|
|
|
|
break;
|
2016-06-24 09:56:19 +02:00
|
|
|
case 'C':
|
2016-06-22 14:59:13 +02:00
|
|
|
case 'L':
|
2017-11-23 13:13:10 +01:00
|
|
|
case 'M':
|
2016-06-22 14:59:13 +02:00
|
|
|
case 'R':
|
|
|
|
/* unhandled */;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
case 'V':
|
|
|
|
/* ignored */;
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-05 07:15:35 +02:00
|
|
|
output_message(STRING_INVALID_SWITCH, argv[i]);
|
2017-07-12 13:09:05 +02:00
|
|
|
error_exit(STRING_HELP);
|
2007-06-13 13:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 14:03:29 +02:00
|
|
|
if (i == argc)
|
|
|
|
{
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case ACTION_ADD:
|
|
|
|
case ACTION_EXPORT:
|
2016-07-05 07:15:35 +02:00
|
|
|
output_message(STRING_NO_FILENAME);
|
2016-06-22 14:03:29 +02:00
|
|
|
break;
|
|
|
|
case ACTION_DELETE:
|
2016-07-05 07:15:35 +02:00
|
|
|
output_message(STRING_NO_REG_KEY);
|
2016-06-22 14:03:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-07-12 13:09:05 +02:00
|
|
|
error_exit(STRING_HELP);
|
2016-06-22 14:03:29 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 09:56:29 +02:00
|
|
|
for (; i < argc; i++)
|
|
|
|
PerformRegAction(action, argv, &i);
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-07-05 07:15:22 +02:00
|
|
|
LocalFree(argv);
|
2007-06-13 13:18:50 +02:00
|
|
|
|
2016-06-22 14:59:13 +02:00
|
|
|
return TRUE;
|
2007-06-13 13:18:50 +02:00
|
|
|
}
|