[CLT2012]
[reactos.git] / base / applications / regedit / regedit.c
1 /*
2 * Windows regedit.exe registry editor implementation.
3 *
4 * Copyright 2002 Andriy Palamarchuk
5 *
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.
10 *
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.
15 *
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <regedit.h>
22
23
24 static const char *usage =
25 "Usage:\n"
26 " regedit filename\n"
27 " regedit /E filename [regpath]\n"
28 " regedit /D regpath\n"
29 "\n"
30 "filename - registry file name\n"
31 "regpath - name of the registry key\n"
32 "\n"
33 "When is called without any switches adds contents of the specified\n"
34 "registry file to the registry\n"
35 "\n"
36 "Switches:\n"
37 " /E - exports contents of the specified registry key to the specified\n"
38 " file. Exports the whole registry if no key is specified.\n"
39 " /D - deletes specified registry key\n"
40 " /S - silent execution, can be used with any other switch.\n"
41 " The only existing mode, exists for compatibility with Windows regedit.\n"
42 " /V - advanced mode, can be used with any other switch.\n"
43 " Ignored, exists for compatibility with Windows regedit.\n"
44 " /L - location of system.dat file. Can be used with any other switch.\n"
45 " Ignored. Exists for compatibility with Windows regedit.\n"
46 " /R - location of user.dat file. Can be used with any other switch.\n"
47 " Ignored. Exists for compatibility with Windows regedit.\n"
48 " /? - print this help. Any other switches are ignored.\n"
49 " /C - create registry from. Not implemented.\n"
50 "\n"
51 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
52 "This program is command-line compatible with Microsoft Windows\n"
53 "regedit.\n";
54
55 typedef enum
56 {
57 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
58 } REGEDIT_ACTION;
59
60
61 const CHAR *getAppName(void)
62 {
63 return "regedit";
64 }
65
66 /******************************************************************************
67 * Copies file name from command line string to the buffer.
68 * Rewinds the command line string pointer to the next non-space character
69 * after the file name.
70 * Buffer contains an empty string if no filename was found;
71 *
72 * params:
73 * command_line - command line current position pointer
74 * where *s[0] is the first symbol of the file name.
75 * file_name - buffer to write the file name to.
76 */
77 void get_file_name(LPWSTR *command_line, LPWSTR file_name)
78 {
79 WCHAR *s = *command_line;
80 int pos = 0; /* position of pointer "s" in *command_line */
81 file_name[0] = 0;
82
83 if (!s[0])
84 {
85 return;
86 }
87
88 if (s[0] == L'"')
89 {
90 s++;
91 (*command_line)++;
92 while(s[0] != L'"')
93 {
94 if (!s[0])
95 {
96 fprintf(stderr, "%s: Unexpected end of file name!\n", getAppName());
97 exit(1);
98 }
99 s++;
100 pos++;
101 }
102 }
103 else
104 {
105 while(s[0] && !iswspace(s[0]))
106 {
107 s++;
108 pos++;
109 }
110 }
111 memcpy(file_name, *command_line, pos * sizeof((*command_line)[0]));
112 /* remove the last backslash */
113 if (file_name[pos - 1] == L'\\')
114 {
115 file_name[pos - 1] = L'\0';
116 }
117 else
118 {
119 file_name[pos] = L'\0';
120 }
121
122 if (s[0])
123 {
124 s++;
125 pos++;
126 }
127 while(s[0] && iswspace(s[0]))
128 {
129 s++;
130 pos++;
131 }
132 (*command_line) += pos;
133 }
134
135 BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s)
136 {
137 TCHAR szTitle[256], szText[256];
138 switch (action)
139 {
140 case ACTION_ADD:
141 {
142 WCHAR filename[MAX_PATH];
143 FILE *fp;
144
145 get_file_name(&s, filename);
146 if (!filename[0])
147 {
148 fprintf(stderr, "%s: No file name is specified\n", getAppName());
149 fprintf(stderr, usage);
150 exit(4);
151 }
152
153 while(filename[0])
154 {
155 fp = _wfopen(filename, L"r");
156 if (fp == NULL)
157 {
158 LPSTR p = GetMultiByteString(filename);
159 perror("");
160 fprintf(stderr, "%s: Can't open file \"%s\"\n", getAppName(), p);
161 HeapFree(GetProcessHeap(), 0, p);
162 exit(5);
163 }
164 import_registry_file(fp);
165 get_file_name(&s, filename);
166 LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle));
167 LoadString(hInst, IDS_IMPORTED_OK, szText, sizeof(szTitle));
168 /* show successful import */
169 MessageBox(NULL, szText, szTitle, MB_OK);
170 }
171 break;
172 }
173 case ACTION_DELETE:
174 {
175 WCHAR reg_key_name[KEY_MAX_LEN];
176 get_file_name(&s, reg_key_name);
177 if (!reg_key_name[0])
178 {
179 fprintf(stderr, "%s: No registry key is specified for removal\n", getAppName());
180 fprintf(stderr, usage);
181 exit(6);
182 }
183 delete_registry_key(reg_key_name);
184 break;
185 }
186 case ACTION_EXPORT:
187 {
188 WCHAR filename[MAX_PATH];
189
190 filename[0] = _T('\0');
191 get_file_name(&s, filename);
192 if (!filename[0])
193 {
194 fprintf(stderr, "%s: No file name is specified\n", getAppName());
195 fprintf(stderr, usage);
196 exit(7);
197 }
198
199 if (s[0])
200 {
201 WCHAR reg_key_name[KEY_MAX_LEN];
202 get_file_name(&s, reg_key_name);
203 export_registry_key(filename, reg_key_name, REG_FORMAT_4);
204 }
205 else
206 {
207 export_registry_key(filename, NULL, REG_FORMAT_4);
208 }
209 break;
210 }
211 default:
212 fprintf(stderr, "%s: Unhandled action!\n", getAppName());
213 exit(8);
214 break;
215 }
216 return TRUE;
217 }
218
219 /**
220 * Process unknown switch.
221 *
222 * Params:
223 * chu - the switch character in upper-case.
224 * s - the command line string where s points to the switch character.
225 */
226 static void error_unknown_switch(WCHAR chu, LPWSTR s)
227 {
228 if (iswalpha(chu))
229 {
230 fprintf(stderr, "%s: Undefined switch /%c!\n", getAppName(), chu);
231 }
232 else
233 {
234 fprintf(stderr, "%s: Alphabetic character is expected after '%c' "
235 "in swit ch specification\n", getAppName(), *(s - 1));
236 }
237 exit(1);
238 }
239
240 BOOL ProcessCmdLine(LPWSTR lpCmdLine)
241 {
242 REGEDIT_ACTION action = ACTION_UNDEF;
243 LPWSTR s = lpCmdLine; /* command line pointer */
244 WCHAR ch = *s; /* current character */
245
246 while (ch && ((ch == L'-') || (ch == L'/')))
247 {
248 WCHAR chu;
249 WCHAR ch2;
250
251 s++;
252 ch = *s;
253 ch2 = *(s + 1);
254 chu = (WCHAR)towupper(ch);
255 if (!ch2 || iswspace(ch2))
256 {
257 if (chu == L'S' || chu == L'V')
258 {
259 /* ignore these switches */
260 }
261 else
262 {
263 switch (chu)
264 {
265 case L'D':
266 action = ACTION_DELETE;
267 break;
268 case L'E':
269 action = ACTION_EXPORT;
270 break;
271 case L'?':
272 fprintf(stderr, usage);
273 exit(3);
274 break;
275 default:
276 error_unknown_switch(chu, s);
277 break;
278 }
279 }
280 s++;
281 }
282 else
283 {
284 if (ch2 == L':')
285 {
286 switch (chu)
287 {
288 case L'L':
289 /* fall through */
290 case L'R':
291 s += 2;
292 while (*s && !iswspace(*s))
293 {
294 s++;
295 }
296 break;
297 default:
298 error_unknown_switch(chu, s);
299 break;
300 }
301 }
302 else
303 {
304 /* this is a file name, starting from '/' */
305 s--;
306 break;
307 }
308 }
309 /* skip spaces to the next parameter */
310 ch = *s;
311 while (ch && iswspace(ch))
312 {
313 s++;
314 ch = *s;
315 }
316 }
317
318 if (*s && action == ACTION_UNDEF)
319 {
320 TCHAR szTitle[256], szText[256];
321 LoadString(hInst, IDS_APP_TITLE, szTitle, sizeof(szTitle));
322 LoadString(hInst, IDS_IMPORT_PROMPT, szText, sizeof(szTitle));
323 /* request import confirmation */
324 if (MessageBox(NULL, szText, szTitle, MB_YESNO) == IDYES)
325 {
326 action = ACTION_ADD;
327 }
328 else return TRUE;
329 }
330 if (action == ACTION_UNDEF)
331 return FALSE;
332
333 return PerformRegAction(action, s);
334 }