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
26 static const char *usage =
29 " regedit /E filename [regpath]\n"
30 " regedit /D regpath\n"
32 "filename - registry file name\n"
33 "regpath - name of the registry key\n"
35 "When called without any switches, adds the content of the specified\n"
36 "file to the registry\n"
39 " /E - exports contents of the specified registry key to the specified\n"
40 " file. Exports the whole registry if no key is specified.\n"
41 " /D - deletes specified registry key\n"
42 " /S - silent execution, can be used with any other switch.\n"
43 " Default. The only existing mode, exists for compatibility with Windows regedit.\n"
44 " /V - advanced mode, can be used with any other switch.\n"
45 " Ignored, exists for compatibility with Windows regedit.\n"
46 " /L - location of system.dat file. Can be used with any other switch.\n"
47 " Ignored. Exists for compatibility with Windows regedit.\n"
48 " /R - location of user.dat file. Can be used with any other switch.\n"
49 " Ignored. Exists for compatibility with Windows regedit.\n"
50 " /? - print this help. Any other switches are ignored.\n"
51 " /C - create registry from file. Not implemented.\n"
53 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
54 "This program is command-line compatible with Microsoft Windows\n"
58 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
62 const CHAR *getAppName(void)
67 /******************************************************************************
68 * Copies file name from command line string to the buffer.
69 * Rewinds the command line string pointer to the next non-space character
70 * after the file name.
71 * Buffer contains an empty string if no filename was found;
74 * command_line - command line current position pointer
75 * where *s[0] is the first symbol of the file name.
76 * file_name - buffer to write the file name to.
78 static void get_file_name(CHAR **command_line, CHAR *file_name)
80 CHAR *s = *command_line;
81 int pos = 0; /* position of pointer "s" in *command_line */
93 fprintf(stderr,"%s: Unexpected end of file name!\n",
101 while(s[0] && !isspace(s[0])) {
106 memcpy(file_name, *command_line, pos * sizeof((*command_line)[0]));
107 /* remove the last backslash */
108 if (file_name[pos - 1] == '\\') {
109 file_name[pos - 1] = '\0';
111 file_name[pos] = '\0';
118 while(s[0] && isspace(s[0])) {
122 (*command_line) += pos;
125 static BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
129 CHAR filename[MAX_PATH];
132 get_file_name(&s, filename);
134 fprintf(stderr,"%s: No file name was specified\n", getAppName());
135 fprintf(stderr,usage);
140 char* realname = NULL;
142 if (strcmp(filename, "-") == 0)
150 size=SearchPath(NULL, filename, NULL,0, NULL, NULL);
153 realname=HeapAlloc(GetProcessHeap(), 0, size);
154 size=SearchPath(NULL, filename, NULL, size, realname, NULL);
158 fprintf(stderr, "%s: File not found \"%s\" (%d)\n",
159 getAppName(), filename, GetLastError());
162 reg_file = fopen(realname, "r");
166 fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), filename);
170 processRegLines(reg_file);
173 HeapFree(GetProcessHeap(),0,realname);
176 get_file_name(&s, filename);
180 case ACTION_DELETE: {
181 CHAR reg_key_name[KEY_MAX_LEN];
183 get_file_name(&s, reg_key_name);
184 if (!reg_key_name[0]) {
185 fprintf(stderr,"%s: No registry key was specified for removal\n",
187 fprintf(stderr,usage);
190 delete_registry_key(reg_key_name);
193 case ACTION_EXPORT: {
194 CHAR filename[MAX_PATH];
197 get_file_name(&s, filename);
199 fprintf(stderr,"%s: No file name was specified\n", getAppName());
200 fprintf(stderr,usage);
205 CHAR reg_key_name[KEY_MAX_LEN];
207 get_file_name(&s, reg_key_name);
208 export_registry_key(filename, reg_key_name);
210 export_registry_key(filename, NULL);
215 fprintf(stderr,"%s: Unhandled action!\n", getAppName());
223 * Process unknown switch.
226 * chu - the switch character in upper-case.
227 * s - the command line string where s points to the switch character.
229 static void error_unknown_switch(char chu, char *s)
232 fprintf(stderr,"%s: Undefined switch /%c!\n", getAppName(), chu);
234 fprintf(stderr,"%s: Alphabetic character is expected after '%c' "
235 "in switch specification\n", getAppName(), *(s - 1));
240 BOOL ProcessCmdLine(LPSTR lpCmdLine)
242 REGEDIT_ACTION action = ACTION_UNDEF;
243 LPSTR s = lpCmdLine; /* command line pointer */
244 CHAR ch = *s; /* current character */
246 while (ch && ((ch == '-') || (ch == '/'))) {
252 if (!ch || isspace(ch))
254 /* '-' is a file name. It indicates we should use stdin */
260 if (!ch2 || isspace(ch2)) {
261 if (chu == 'S' || chu == 'V') {
262 /* ignore these switches */
266 action = ACTION_DELETE;
269 action = ACTION_EXPORT;
272 fprintf(stderr,usage);
276 error_unknown_switch(chu, s);
288 while (*s && !isspace(*s)) {
293 error_unknown_switch(chu, s);
297 /* this is a file name, starting from '/' */
302 /* skip spaces to the next parameter */
304 while (ch && isspace(ch)) {
310 if (*s && action == ACTION_UNDEF)
313 if (action == ACTION_UNDEF)
316 return PerformRegAction(action, s);