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
27 static const char *usage
=
30 " regedit /E filename [regpath]\n"
31 " regedit /D regpath\n"
33 "filename - registry file name\n"
34 "regpath - name of the registry key\n"
36 "When called without any switches, adds the content of the specified\n"
37 "file to the registry\n"
40 " /E - exports contents of the specified registry key to the specified\n"
41 " file. Exports the whole registry if no key is specified.\n"
42 " /D - deletes specified registry key\n"
43 " /S - silent execution, can be used with any other switch.\n"
44 " Default. The only existing mode, exists for compatibility with Windows regedit.\n"
45 " /V - advanced mode, can be used with any other switch.\n"
46 " Ignored, exists for compatibility with Windows regedit.\n"
47 " /L - location of system.dat file. Can be used with any other switch.\n"
48 " Ignored. Exists for compatibility with Windows regedit.\n"
49 " /R - location of user.dat file. Can be used with any other switch.\n"
50 " Ignored. Exists for compatibility with Windows regedit.\n"
51 " /? - print this help. Any other switches are ignored.\n"
52 " /C - create registry from file. Not implemented.\n"
54 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
55 "This program is command-line compatible with Microsoft Windows\n"
59 ACTION_UNDEF
, ACTION_ADD
, ACTION_EXPORT
, ACTION_DELETE
63 const CHAR
*getAppName(void)
68 /******************************************************************************
69 * Copies file name from command line string to the buffer.
70 * Rewinds the command line string pointer to the next non-space character
71 * after the file name.
72 * Buffer contains an empty string if no filename was found;
75 * command_line - command line current position pointer
76 * where *s[0] is the first symbol of the file name.
77 * file_name - buffer to write the file name to.
79 static void get_file_name(CHAR
**command_line
, CHAR
*file_name
)
81 CHAR
*s
= *command_line
;
82 int pos
= 0; /* position of pointer "s" in *command_line */
94 fprintf(stderr
,"%s: Unexpected end of file name!\n",
102 while(s
[0] && !isspace(s
[0])) {
107 memcpy(file_name
, *command_line
, pos
* sizeof((*command_line
)[0]));
108 /* Terminate the string and remove the trailing backslash */
109 if (pos
> 0 && file_name
[pos
- 1] == '\\') {
110 file_name
[pos
- 1] = '\0';
112 file_name
[pos
] = '\0';
119 while(s
[0] && isspace(s
[0])) {
123 (*command_line
) += pos
;
126 static BOOL
PerformRegAction(REGEDIT_ACTION action
, LPSTR s
)
130 CHAR filename
[MAX_PATH
];
133 get_file_name(&s
, filename
);
135 fprintf(stderr
,"%s: No file name was specified\n", getAppName());
136 fprintf(stderr
,usage
);
141 char* realname
= NULL
;
143 if (strcmp(filename
, "-") == 0)
151 size
= SearchPathA(NULL
, filename
, NULL
, 0, NULL
, NULL
);
154 realname
= HeapAlloc(GetProcessHeap(), 0, size
);
155 size
= SearchPathA(NULL
, filename
, NULL
, size
, realname
, NULL
);
159 fprintf(stderr
, "%s: File not found \"%s\" (%d)\n",
160 getAppName(), filename
, GetLastError());
163 reg_file
= fopen(realname
, "rb");
164 if (reg_file
== NULL
)
167 fprintf(stderr
, "%s: Can't open file \"%s\"\n", getAppName(), filename
);
171 import_registry_file(reg_file
);
174 HeapFree(GetProcessHeap(),0,realname
);
177 get_file_name(&s
, filename
);
181 case ACTION_DELETE
: {
182 CHAR reg_key_name
[KEY_MAX_LEN
];
184 get_file_name(&s
, reg_key_name
);
185 if (!reg_key_name
[0]) {
186 fprintf(stderr
,"%s: No registry key was specified for removal\n",
188 fprintf(stderr
,usage
);
192 WCHAR
* reg_key_nameW
= GetWideString(reg_key_name
);
193 delete_registry_key(reg_key_nameW
);
194 HeapFree(GetProcessHeap(), 0, reg_key_nameW
);
198 case ACTION_EXPORT
: {
199 CHAR filename
[MAX_PATH
];
203 get_file_name(&s
, filename
);
205 fprintf(stderr
,"%s: No file name was specified\n", getAppName());
206 fprintf(stderr
,usage
);
210 filenameW
= GetWideString(filename
);
212 CHAR reg_key_name
[KEY_MAX_LEN
];
213 WCHAR
* reg_key_nameW
;
215 get_file_name(&s
, reg_key_name
);
216 reg_key_nameW
= GetWideString(reg_key_name
);
217 export_registry_key(filenameW
, reg_key_nameW
, REG_FORMAT_4
);
218 HeapFree(GetProcessHeap(), 0, reg_key_nameW
);
220 export_registry_key(filenameW
, NULL
, REG_FORMAT_4
);
222 HeapFree(GetProcessHeap(), 0, filenameW
);
226 fprintf(stderr
,"%s: Unhandled action!\n", getAppName());
234 * Process unknown switch.
237 * chu - the switch character in upper-case.
238 * s - the command line string where s points to the switch character.
240 static void error_unknown_switch(char chu
, char *s
)
243 fprintf(stderr
,"%s: Undefined switch /%c!\n", getAppName(), chu
);
245 fprintf(stderr
,"%s: Alphabetic character is expected after '%c' "
246 "in switch specification\n", getAppName(), *(s
- 1));
251 BOOL
ProcessCmdLine(LPSTR lpCmdLine
)
253 REGEDIT_ACTION action
= ACTION_UNDEF
;
254 LPSTR s
= lpCmdLine
; /* command line pointer */
255 CHAR ch
= *s
; /* current character */
257 while (ch
&& ((ch
== '-') || (ch
== '/'))) {
263 if (!ch
|| isspace(ch
))
265 /* '-' is a file name. It indicates we should use stdin */
271 if (!ch2
|| isspace(ch2
)) {
272 if (chu
== 'S' || chu
== 'V') {
273 /* ignore these switches */
277 action
= ACTION_DELETE
;
280 action
= ACTION_EXPORT
;
283 fprintf(stderr
,usage
);
287 error_unknown_switch(chu
, s
);
299 while (*s
&& !isspace(*s
)) {
304 error_unknown_switch(chu
, s
);
308 /* this is a file name, starting from '/' */
313 /* skip spaces to the next parameter */
315 while (ch
&& isspace(ch
)) {
321 if (*s
&& action
== ACTION_UNDEF
)
324 if (action
== ACTION_UNDEF
)
327 return PerformRegAction(action
, s
);