unicode: Use codepage files from Microsoft's Open Specification data.
[wine.git] / programs / regedit / regedit.c
blob4b555f4ef521ddd9d0461c7ef31759001c1672b6
1 /*
2 * Windows regedit.exe registry editor implementation.
4 * Copyright 2002 Andriy Palamarchuk
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
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"
29 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
31 static void output_writeconsole(const WCHAR *str, DWORD wlen)
33 DWORD count, ret;
35 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
36 if (!ret)
38 DWORD len;
39 char *msgA;
41 /* WriteConsole() fails on Windows if its output is redirected. If this occurs,
42 * we should call WriteFile() and assume the console encoding is still correct.
44 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
45 msgA = heap_xalloc(len);
47 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
48 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
49 heap_free(msgA);
53 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
55 WCHAR *str;
56 DWORD len;
58 SetLastError(NO_ERROR);
59 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
60 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
61 if (len == 0 && GetLastError() != NO_ERROR)
63 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
64 return;
66 output_writeconsole(str, len);
67 LocalFree(str);
70 void WINAPIV output_message(unsigned int id, ...)
72 WCHAR fmt[1536];
73 __ms_va_list va_args;
75 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
77 WINE_FIXME("LoadString failed with %d\n", GetLastError());
78 return;
80 __ms_va_start(va_args, id);
81 output_formatstring(fmt, va_args);
82 __ms_va_end(va_args);
85 void WINAPIV error_exit(unsigned int id, ...)
87 WCHAR fmt[1536];
88 __ms_va_list va_args;
90 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
92 WINE_FIXME("LoadString failed with %u\n", GetLastError());
93 return;
95 __ms_va_start(va_args, id);
96 output_formatstring(fmt, va_args);
97 __ms_va_end(va_args);
99 exit(0); /* regedit.exe always terminates with error code zero */
102 typedef enum {
103 ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
104 } REGEDIT_ACTION;
106 static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
108 switch (action) {
109 case ACTION_ADD: {
110 WCHAR *filename = argv[*i];
111 WCHAR hyphen[] = {'-',0};
112 WCHAR *realname = NULL;
113 FILE *reg_file;
115 if (!lstrcmpW(filename, hyphen))
116 reg_file = stdin;
117 else
119 int size;
120 WCHAR rb_mode[] = {'r','b',0};
122 size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
123 if (size > 0)
125 realname = heap_xalloc(size * sizeof(WCHAR));
126 size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
128 if (size == 0)
130 output_message(STRING_FILE_NOT_FOUND, filename);
131 heap_free(realname);
132 return;
134 reg_file = _wfopen(realname, rb_mode);
135 if (reg_file == NULL)
137 WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
138 _wperror(regedit);
139 output_message(STRING_CANNOT_OPEN_FILE, filename);
140 heap_free(realname);
141 return;
144 import_registry_file(reg_file);
145 if (realname)
147 heap_free(realname);
148 fclose(reg_file);
150 break;
152 case ACTION_DELETE:
153 delete_registry_key(argv[*i]);
154 break;
155 case ACTION_EXPORT: {
156 WCHAR *filename = argv[*i];
157 WCHAR *key_name = argv[++(*i)];
159 if (key_name && *key_name)
160 export_registry_key(filename, key_name, REG_FORMAT_5);
161 else
162 export_registry_key(filename, NULL, REG_FORMAT_5);
163 break;
165 default:
166 error_exit(STRING_UNHANDLED_ACTION);
167 break;
171 BOOL ProcessCmdLine(WCHAR *cmdline)
173 WCHAR **argv;
174 int argc, i;
175 REGEDIT_ACTION action = ACTION_ADD;
177 argv = CommandLineToArgvW(cmdline, &argc);
179 if (!argv)
180 return FALSE;
182 if (argc == 1)
184 LocalFree(argv);
185 return FALSE;
188 for (i = 1; i < argc; i++)
190 if (argv[i][0] != '/' && argv[i][0] != '-')
191 break; /* No flags specified. */
193 if (!argv[i][1] && argv[i][0] == '-')
194 break; /* '-' is a filename. It indicates we should use stdin. */
196 if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
197 break; /* This is a file path beginning with '/'. */
199 switch (towupper(argv[i][1]))
201 case '?':
202 error_exit(STRING_USAGE);
203 break;
204 case 'D':
205 action = ACTION_DELETE;
206 break;
207 case 'E':
208 action = ACTION_EXPORT;
209 break;
210 case 'C':
211 case 'L':
212 case 'M':
213 case 'R':
214 /* unhandled */;
215 break;
216 case 'S':
217 case 'V':
218 /* ignored */;
219 break;
220 default:
221 output_message(STRING_INVALID_SWITCH, argv[i]);
222 error_exit(STRING_HELP);
226 if (i == argc)
228 switch (action)
230 case ACTION_ADD:
231 case ACTION_EXPORT:
232 output_message(STRING_NO_FILENAME);
233 break;
234 case ACTION_DELETE:
235 output_message(STRING_NO_REG_KEY);
236 break;
238 error_exit(STRING_HELP);
241 for (; i < argc; i++)
242 PerformRegAction(action, argv, &i);
244 LocalFree(argv);
246 return TRUE;