Sync with trunk head (r49139)
[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 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
57 } REGEDIT_ACTION;
58
59
60 const CHAR *getAppName(void)
61 {
62 return "regedit";
63 }
64
65 /******************************************************************************
66 * Copies file name from command line string to the buffer.
67 * Rewinds the command line string pointer to the next non-space character
68 * after the file name.
69 * Buffer contains an empty string if no filename was found;
70 *
71 * params:
72 * command_line - command line current position pointer
73 * where *s[0] is the first symbol of the file name.
74 * file_name - buffer to write the file name to.
75 */
76 void get_file_name(LPWSTR *command_line, LPWSTR file_name)
77 {
78 WCHAR *s = *command_line;
79 int pos = 0; /* position of pointer "s" in *command_line */
80 file_name[0] = 0;
81
82 if (!s[0]) {
83 return;
84 }
85
86 if (s[0] == L'"') {
87 s++;
88 (*command_line)++;
89 while(s[0] != L'"') {
90 if (!s[0]) {
91 fprintf(stderr, "%s: Unexpected end of file name!\n", getAppName());
92 exit(1);
93 }
94 s++;
95 pos++;
96 }
97 } else {
98 while(s[0] && !iswspace(s[0])) {
99 s++;
100 pos++;
101 }
102 }
103 memcpy(file_name, *command_line, pos * sizeof((*command_line)[0]));
104 /* remove the last backslash */
105 if (file_name[pos - 1] == L'\\') {
106 file_name[pos - 1] = L'\0';
107 } else {
108 file_name[pos] = L'\0';
109 }
110
111 if (s[0]) {
112 s++;
113 pos++;
114 }
115 while(s[0] && iswspace(s[0])) {
116 s++;
117 pos++;
118 }
119 (*command_line) += pos;
120 }
121
122 BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s)
123 {
124 switch (action) {
125 case ACTION_ADD: {
126 WCHAR filename[MAX_PATH];
127 FILE *fp;
128
129 get_file_name(&s, filename);
130 if (!filename[0]) {
131 fprintf(stderr, "%s: No file name is specified\n", getAppName());
132 fprintf(stderr, usage);
133 exit(4);
134 }
135
136 while(filename[0]) {
137 fp = _wfopen(filename, L"r");
138 if (fp == NULL)
139 {
140 LPSTR p = GetMultiByteString(filename);
141 perror("");
142 fprintf(stderr, "%s: Can't open file \"%s\"\n", getAppName(), p);
143 HeapFree(GetProcessHeap(), 0, p);
144 exit(5);
145 }
146 import_registry_file(fp);
147 get_file_name(&s, filename);
148 }
149 break;
150 }
151 case ACTION_DELETE: {
152 WCHAR reg_key_name[KEY_MAX_LEN];
153 get_file_name(&s, reg_key_name);
154 if (!reg_key_name[0]) {
155 fprintf(stderr, "%s: No registry key is specified for removal\n", getAppName());
156 fprintf(stderr, usage);
157 exit(6);
158 }
159 delete_registry_key(reg_key_name);
160 break;
161 }
162 case ACTION_EXPORT: {
163 WCHAR filename[MAX_PATH];
164
165 filename[0] = _T('\0');
166 get_file_name(&s, filename);
167 if (!filename[0]) {
168 fprintf(stderr, "%s: No file name is specified\n", getAppName());
169 fprintf(stderr, usage);
170 exit(7);
171 }
172
173 if (s[0]) {
174 WCHAR reg_key_name[KEY_MAX_LEN];
175 get_file_name(&s, reg_key_name);
176 export_registry_key(filename, reg_key_name, REG_FORMAT_4);
177 } else {
178 export_registry_key(filename, NULL, REG_FORMAT_4);
179 }
180 break;
181 }
182 default:
183 fprintf(stderr, "%s: Unhandled action!\n", getAppName());
184 exit(8);
185 break;
186 }
187 return TRUE;
188 }
189
190 /**
191 * Process unknown switch.
192 *
193 * Params:
194 * chu - the switch character in upper-case.
195 * s - the command line string where s points to the switch character.
196 */
197 static void error_unknown_switch(WCHAR chu, LPWSTR s)
198 {
199 if (iswalpha(chu)) {
200 fprintf(stderr, "%s: Undefined switch /%c!\n", getAppName(), chu);
201 } else {
202 fprintf(stderr, "%s: Alphabetic character is expected after '%c' "
203 "in swit ch specification\n", getAppName(), *(s - 1));
204 }
205 exit(1);
206 }
207
208 BOOL ProcessCmdLine(LPWSTR lpCmdLine)
209 {
210 REGEDIT_ACTION action = ACTION_UNDEF;
211 LPWSTR s = lpCmdLine; /* command line pointer */
212 WCHAR ch = *s; /* current character */
213
214 while (ch && ((ch == L'-') || (ch == L'/')))
215 {
216 WCHAR chu;
217 WCHAR ch2;
218
219 s++;
220 ch = *s;
221 ch2 = *(s + 1);
222 chu = (WCHAR)towupper(ch);
223 if (!ch2 || iswspace(ch2)) {
224 if (chu == L'S' || chu == L'V')
225 {
226 /* ignore these switches */
227 } else {
228 switch (chu) {
229 case L'D':
230 action = ACTION_DELETE;
231 break;
232 case L'E':
233 action = ACTION_EXPORT;
234 break;
235 case L'?':
236 fprintf(stderr, usage);
237 exit(3);
238 break;
239 default:
240 error_unknown_switch(chu, s);
241 break;
242 }
243 }
244 s++;
245 } else {
246 if (ch2 == L':') {
247 switch (chu) {
248 case L'L':
249 /* fall through */
250 case L'R':
251 s += 2;
252 while (*s && !iswspace(*s)) {
253 s++;
254 }
255 break;
256 default:
257 error_unknown_switch(chu, s);
258 break;
259 }
260 } else {
261 /* this is a file name, starting from '/' */
262 s--;
263 break;
264 }
265 }
266 /* skip spaces to the next parameter */
267 ch = *s;
268 while (ch && iswspace(ch)) {
269 s++;
270 ch = *s;
271 }
272 }
273
274 if (*s && action == ACTION_UNDEF)
275 action = ACTION_ADD;
276
277 if (action == ACTION_UNDEF)
278 return FALSE;
279
280 return PerformRegAction(action, s);
281 }