shell32/tests: Use the available ARRAY_SIZE() macro.
[wine.git] / programs / regedit / regedit.c
blob68001be89240e239c2aa391d4199303226437769
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/unicode.h"
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "main.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
32 static void output_writeconsole(const WCHAR *str, DWORD wlen)
34 DWORD count, ret;
36 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
37 if (!ret)
39 DWORD len;
40 char *msgA;
42 /* WriteConsole() fails on Windows if its output is redirected. If this occurs,
43 * we should call WriteFile() and assume the console encoding is still correct.
45 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
46 msgA = heap_xalloc(len);
48 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
49 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
50 heap_free(msgA);
54 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
56 WCHAR *str;
57 DWORD len;
59 SetLastError(NO_ERROR);
60 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
61 fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
62 if (len == 0 && GetLastError() != NO_ERROR)
64 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
65 return;
67 output_writeconsole(str, len);
68 LocalFree(str);
71 void WINAPIV output_message(unsigned int id, ...)
73 WCHAR fmt[1536];
74 __ms_va_list va_args;
76 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
78 WINE_FIXME("LoadString failed with %d\n", GetLastError());
79 return;
81 __ms_va_start(va_args, id);
82 output_formatstring(fmt, va_args);
83 __ms_va_end(va_args);
86 void WINAPIV error_exit(unsigned int id, ...)
88 WCHAR fmt[1536];
89 __ms_va_list va_args;
91 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
93 WINE_FIXME("LoadString failed with %u\n", GetLastError());
94 return;
96 __ms_va_start(va_args, id);
97 output_formatstring(fmt, va_args);
98 __ms_va_end(va_args);
100 exit(0); /* regedit.exe always terminates with error code zero */
103 typedef enum {
104 ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
105 } REGEDIT_ACTION;
107 static void PerformRegAction(REGEDIT_ACTION action, WCHAR **argv, int *i)
109 switch (action) {
110 case ACTION_ADD: {
111 WCHAR *filename = argv[*i];
112 WCHAR hyphen[] = {'-',0};
113 WCHAR *realname = NULL;
114 FILE *reg_file;
116 if (!strcmpW(filename, hyphen))
117 reg_file = stdin;
118 else
120 int size;
121 WCHAR rb_mode[] = {'r','b',0};
123 size = SearchPathW(NULL, filename, NULL, 0, NULL, NULL);
124 if (size > 0)
126 realname = heap_xalloc(size * sizeof(WCHAR));
127 size = SearchPathW(NULL, filename, NULL, size, realname, NULL);
129 if (size == 0)
131 output_message(STRING_FILE_NOT_FOUND, filename);
132 heap_free(realname);
133 return;
135 reg_file = _wfopen(realname, rb_mode);
136 if (reg_file == NULL)
138 WCHAR regedit[] = {'r','e','g','e','d','i','t',0};
139 _wperror(regedit);
140 output_message(STRING_CANNOT_OPEN_FILE, filename);
141 heap_free(realname);
142 return;
145 import_registry_file(reg_file);
146 if (realname)
148 heap_free(realname);
149 fclose(reg_file);
151 break;
153 case ACTION_DELETE:
154 delete_registry_key(argv[*i]);
155 break;
156 case ACTION_EXPORT: {
157 WCHAR *filename = argv[*i];
158 WCHAR *key_name = argv[++(*i)];
160 if (key_name && *key_name)
161 export_registry_key(filename, key_name, REG_FORMAT_5);
162 else
163 export_registry_key(filename, NULL, REG_FORMAT_5);
164 break;
166 default:
167 error_exit(STRING_UNHANDLED_ACTION);
168 break;
172 BOOL ProcessCmdLine(WCHAR *cmdline)
174 WCHAR **argv;
175 int argc, i;
176 REGEDIT_ACTION action = ACTION_ADD;
178 argv = CommandLineToArgvW(cmdline, &argc);
180 if (!argv)
181 return FALSE;
183 if (argc == 1)
185 LocalFree(argv);
186 return FALSE;
189 for (i = 1; i < argc; i++)
191 if (argv[i][0] != '/' && argv[i][0] != '-')
192 break; /* No flags specified. */
194 if (!argv[i][1] && argv[i][0] == '-')
195 break; /* '-' is a filename. It indicates we should use stdin. */
197 if (argv[i][1] && argv[i][2] && argv[i][2] != ':')
198 break; /* This is a file path beginning with '/'. */
200 switch (toupperW(argv[i][1]))
202 case '?':
203 error_exit(STRING_USAGE);
204 break;
205 case 'D':
206 action = ACTION_DELETE;
207 break;
208 case 'E':
209 action = ACTION_EXPORT;
210 break;
211 case 'C':
212 case 'L':
213 case 'M':
214 case 'R':
215 /* unhandled */;
216 break;
217 case 'S':
218 case 'V':
219 /* ignored */;
220 break;
221 default:
222 output_message(STRING_INVALID_SWITCH, argv[i]);
223 error_exit(STRING_HELP);
227 if (i == argc)
229 switch (action)
231 case ACTION_ADD:
232 case ACTION_EXPORT:
233 output_message(STRING_NO_FILENAME);
234 break;
235 case ACTION_DELETE:
236 output_message(STRING_NO_REG_KEY);
237 break;
239 error_exit(STRING_HELP);
242 for (; i < argc; i++)
243 PerformRegAction(action, argv, &i);
245 LocalFree(argv);
247 return TRUE;