regedit.c 6.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include <stdlib.h>
22
#include <windows.h>
23
#include <commctrl.h>
24
#include <shellapi.h>
25
#include "wine/debug.h"
26
#include "wine/heap.h"
27
#include "main.h"
28

29 30
WINE_DEFAULT_DEBUG_CHANNEL(regedit);

31
static void output_writeconsole(const WCHAR *str, DWORD wlen)
32 33 34 35 36 37 38 39 40 41 42 43 44
{
    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);
45
        msgA = heap_xalloc(len);
46 47 48

        WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
        WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
49
        heap_free(msgA);
50 51 52 53 54 55 56 57 58 59
    }
}

static void output_formatstring(const WCHAR *fmt, __ms_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);
60
    if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
61 62 63 64 65 66 67 68
    {
        WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
        return;
    }
    output_writeconsole(str, len);
    LocalFree(str);
}

69
void WINAPIV output_message(unsigned int id, ...)
70 71 72 73
{
    WCHAR fmt[1536];
    __ms_va_list va_args;

74
    if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
75 76 77 78 79 80 81 82
    {
        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);
}
83

84
void WINAPIV error_exit(unsigned int id, ...)
85 86 87 88
{
    WCHAR fmt[1536];
    __ms_va_list va_args;

89
    if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
90 91 92 93 94 95 96 97 98 99 100
    {
        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 */
}

101
typedef enum {
102
    ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
103 104
} REGEDIT_ACTION;

105
static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
106
{
107 108
    switch (action) {
    case ACTION_ADD: {
109 110
            WCHAR *filename = argv[*i];
            WCHAR hyphen[] = {'-',0};
111
            WCHAR *realname = NULL;
112
            FILE *reg_file;
113

114
            if (!lstrcmpW(filename, hyphen))
115 116 117 118 119
                reg_file = stdin;
            else
            {
                int size;
                WCHAR rb_mode[] = {'r','b',0};
120

121 122
                size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
                if (size > 0)
123
                {
124
                    realname = heap_xalloc(size * sizeof(WCHAR));
125
                    size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
126
                }
127
                if (size == 0)
128
                {
129
                    output_message(STRING_FILE_NOT_FOUND, filename);
130
                    heap_free(realname);
131
                    return;
132 133 134 135 136 137
                }
                reg_file = _wfopen(realname, rb_mode);
                if (reg_file == NULL)
                {
                    WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
                    _wperror(regedit);
138
                    output_message(STRING_CANNOT_OPEN_FILE, filename);
139
                    heap_free(realname);
140
                    return;
141
                }
142 143 144 145
            }
            import_registry_file(reg_file);
            if (realname)
            {
146
                heap_free(realname);
147
                fclose(reg_file);
148 149 150
            }
            break;
        }
151 152
    case ACTION_DELETE:
            delete_registry_key(argv[*i]);
153 154
            break;
    case ACTION_EXPORT: {
155 156
            WCHAR *filename = argv[*i];
            WCHAR *key_name = argv[++(*i)];
157

158
            if (key_name && *key_name)
159
                export_registry_key(filename, key_name, REG_FORMAT_5);
160
            else
161
                export_registry_key(filename, NULL, REG_FORMAT_5);
162
            break;
163 164
        }
    default:
165
        error_exit(STRING_UNHANDLED_ACTION);
166 167 168
        break;
    }
}
169

170
BOOL ProcessCmdLine(WCHAR *cmdline)
171
{
172 173
    WCHAR **argv;
    int argc, i;
174
    REGEDIT_ACTION action = ACTION_ADD;
175

176
    argv = CommandLineToArgvW(cmdline, &argc);
177

178 179
    if (!argv)
        return FALSE;
180

181
    if (argc == 1)
182
    {
183 184
        LocalFree(argv);
        return FALSE;
185 186
    }

187
    for (i = 1; i < argc; i++)
188 189 190 191 192 193 194 195 196 197
    {
        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 '/'. */

198
        switch (towupper(argv[i][1]))
199
        {
200
        case '?':
201
            error_exit(STRING_USAGE);
202
            break;
203 204 205 206 207 208
        case 'D':
            action = ACTION_DELETE;
            break;
        case 'E':
            action = ACTION_EXPORT;
            break;
209
        case 'C':
210
        case 'L':
211
        case 'M':
212 213 214 215 216 217 218 219
        case 'R':
            /* unhandled */;
            break;
        case 'S':
        case 'V':
            /* ignored */;
            break;
        default:
220
            output_message(STRING_INVALID_SWITCH, argv[i]);
221
            error_exit(STRING_HELP);
222 223 224
        }
    }

225 226 227 228 229 230
    if (i == argc)
    {
        switch (action)
        {
        case ACTION_ADD:
        case ACTION_EXPORT:
231
            output_message(STRING_NO_FILENAME);
232 233
            break;
        case ACTION_DELETE:
234
            output_message(STRING_NO_REG_KEY);
235 236
            break;
        }
237
        error_exit(STRING_HELP);
238 239
    }

240 241
    for (; i < argc; i++)
        PerformRegAction(action, argv, &i);
242

243
    LocalFree(argv);
244

245
    return TRUE;
246
}