d3d11/tests: Add some tests for UpdateSubresource() on a deferred context.
[wine.git] / programs / regedit / regedit.c
blobe25de19b527de62b7b7301c7dd657014ae4ea481
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 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
59 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
60 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
62 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
63 return;
65 output_writeconsole(str, len);
66 LocalFree(str);
69 void WINAPIV output_message(unsigned int id, ...)
71 WCHAR fmt[1536];
72 __ms_va_list va_args;
74 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
76 WINE_FIXME("LoadString failed with %d\n", GetLastError());
77 return;
79 __ms_va_start(va_args, id);
80 output_formatstring(fmt, va_args);
81 __ms_va_end(va_args);
84 void WINAPIV error_exit(unsigned int id, ...)
86 WCHAR fmt[1536];
87 __ms_va_list va_args;
89 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
91 WINE_FIXME("LoadString failed with %u\n", GetLastError());
92 return;
94 __ms_va_start(va_args, id);
95 output_formatstring(fmt, va_args);
96 __ms_va_end(va_args);
98 exit(0); /* regedit.exe always terminates with error code zero */
101 typedef enum {
102 ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
103 } REGEDIT_ACTION;
105 static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
107 switch (action) {
108 case ACTION_ADD: {
109 WCHAR *filename = argv[*i];
110 WCHAR hyphen[] = {'-',0};
111 WCHAR *realname = NULL;
112 FILE *reg_file;
114 if (!lstrcmpW(filename, hyphen))
115 reg_file = stdin;
116 else
118 int size;
119 WCHAR rb_mode[] = {'r','b',0};
121 size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
122 if (size > 0)
124 realname = heap_xalloc(size * sizeof(WCHAR));
125 size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
127 if (size == 0)
129 output_message(STRING_FILE_NOT_FOUND, filename);
130 heap_free(realname);
131 return;
133 reg_file = _wfopen(realname, rb_mode);
134 if (reg_file == NULL)
136 WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
137 _wperror(regedit);
138 output_message(STRING_CANNOT_OPEN_FILE, filename);
139 heap_free(realname);
140 return;
143 import_registry_file(reg_file);
144 if (realname)
146 heap_free(realname);
147 fclose(reg_file);
149 break;
151 case ACTION_DELETE:
152 delete_registry_key(argv[*i]);
153 break;
154 case ACTION_EXPORT: {
155 WCHAR *filename = argv[*i];
156 WCHAR *key_name = argv[++(*i)];
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;
164 default:
165 error_exit(STRING_UNHANDLED_ACTION);
166 break;
170 BOOL ProcessCmdLine(WCHAR *cmdline)
172 WCHAR **argv;
173 int argc, i;
174 REGEDIT_ACTION action = ACTION_ADD;
176 argv = CommandLineToArgvW(cmdline, &argc);
178 if (!argv)
179 return FALSE;
181 if (argc == 1)
183 LocalFree(argv);
184 return FALSE;
187 for (i = 1; i < argc; i++)
189 if (argv[i][0] != '/' && argv[i][0] != '-')
190 break; /* No flags specified. */
192 if (!argv[i][1] && argv[i][0] == '-')
193 break; /* '-' is a filename. It indicates we should use stdin. */
195 if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
196 break; /* This is a file path beginning with '/'. */
198 switch (towupper(argv[i][1]))
200 case '?':
201 error_exit(STRING_USAGE);
202 break;
203 case 'D':
204 action = ACTION_DELETE;
205 break;
206 case 'E':
207 action = ACTION_EXPORT;
208 break;
209 case 'C':
210 case 'L':
211 case 'M':
212 case 'R':
213 /* unhandled */;
214 break;
215 case 'S':
216 case 'V':
217 /* ignored */;
218 break;
219 default:
220 output_message(STRING_INVALID_SWITCH, argv[i]);
221 error_exit(STRING_HELP);
225 if (i == argc)
227 switch (action)
229 case ACTION_ADD:
230 case ACTION_EXPORT:
231 output_message(STRING_NO_FILENAME);
232 break;
233 case ACTION_DELETE:
234 output_message(STRING_NO_REG_KEY);
235 break;
237 error_exit(STRING_HELP);
240 for (; i < argc; i++)
241 PerformRegAction(action, argv, &i);
243 LocalFree(argv);
245 return TRUE;