2 * Windows regedit.exe registry editor implementation.
4 * Copyright (C) 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
23 static const LPCWSTR usage
=
25 L
" regedit filenames\n"
26 L
" regedit /E filename [regpath]\n"
27 L
" regedit /D regpath\n"
29 L
"filenames - List of registry files names\n"
30 L
"filename - Registry file name\n"
31 L
"regpath - Name of the registry key\n"
33 L
"When is called without any switches adds contents of the specified\n"
34 L
"registry files to the registry.\n"
37 L
" /E - Exports contents of the specified registry key to the specified\n"
38 L
" file. Exports the whole registry if no key is specified.\n"
39 L
" /D - Deletes specified registry key\n"
40 L
" /S - Silent execution, can be used with any other switch.\n"
41 L
" The only existing mode, exists for compatibility with Windows regedit.\n"
42 L
" /V - Advanced mode, can be used with any other switch.\n"
43 L
" Ignored, exists for compatibility with Windows regedit.\n"
44 L
" /L - Location of system.dat file. Can be used with any other switch.\n"
45 L
" Ignored. Exists for compatibility with Windows regedit.\n"
46 L
" /R - Location of user.dat file. Can be used with any other switch.\n"
47 L
" Ignored. Exists for compatibility with Windows regedit.\n"
48 L
" /? - Print this help. Any other switches are ignored.\n"
49 L
" /C - Create registry from. Not implemented.\n"
51 L
"The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
52 L
"This program is command-line compatible with Microsoft Windows\n"
57 ACTION_UNDEF
, ACTION_ADD
, ACTION_EXPORT
, ACTION_DELETE
61 LPCWSTR
getAppName(void)
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;
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.
77 void get_file_name(LPWSTR
*command_line
, LPWSTR file_name
)
79 WCHAR
*s
= *command_line
;
80 size_t pos
= 0; /* position of pointer "s" in *command_line */
96 fwprintf(stderr
, L
"%s: Unexpected end of file name!\n", getAppName());
105 while(s
[0] && !iswspace(s
[0]))
111 memcpy(file_name
, *command_line
, pos
* sizeof(WCHAR
));
112 /* remove the last backslash */
113 if (file_name
[pos
- 1] == L
'\\')
115 file_name
[pos
- 1] = L
'\0';
119 file_name
[pos
] = L
'\0';
127 while(s
[0] && iswspace(s
[0]))
132 (*command_line
) += pos
;
135 BOOL
PerformRegAction(REGEDIT_ACTION action
, LPWSTR s
, BOOL silent
)
142 WCHAR filename
[MAX_PATH
];
145 get_file_name(&s
, filename
);
148 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, NULL
, L
"No file name is specified.");
149 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, usage
);
155 /* Request import confirmation */
160 LoadStringW(hInst
, IDS_IMPORT_PROMPT
, szText
, COUNT_OF(szText
));
162 choice
= InfoMessageBox(NULL
, MB_YESNOCANCEL
| MB_ICONWARNING
, szTitle
, szText
, filename
);
169 /* The cancel case is useful if the user is importing more than one registry file
170 at a time, and wants to back out anytime during the import process. This way, the
171 user doesn't have to resort to ending the regedit process abruptly just to cancel
180 fp
= _wfopen(filename
, L
"r");
183 if (fp
== NULL
|| !import_registry_file(fp
))
185 /* Error opening the file */
188 LoadStringW(hInst
, IDS_IMPORT_ERROR
, szText
, COUNT_OF(szText
));
189 InfoMessageBox(NULL
, MB_OK
| MB_ICONERROR
, szTitle
, szText
, filename
);
194 /* Show successful import */
197 LoadStringW(hInst
, IDS_IMPORT_OK
, szText
, COUNT_OF(szText
));
198 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, szText
, filename
);
206 get_file_name(&s
, filename
);
213 WCHAR reg_key_name
[KEY_MAX_LEN
];
214 get_file_name(&s
, reg_key_name
);
215 if (!reg_key_name
[0])
217 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, NULL
, L
"No registry key is specified for removal.");
218 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, usage
);
221 delete_registry_key(reg_key_name
);
227 WCHAR filename
[MAX_PATH
];
230 get_file_name(&s
, filename
);
233 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, NULL
, L
"No file name is specified.");
234 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, usage
);
240 WCHAR reg_key_name
[KEY_MAX_LEN
];
241 get_file_name(&s
, reg_key_name
);
242 export_registry_key(filename
, reg_key_name
, REG_FORMAT_4
);
246 export_registry_key(filename
, NULL
, REG_FORMAT_4
);
252 fwprintf(stderr
, L
"%s: Unhandled action!\n", getAppName());
261 * Process unknown switch.
264 * chu - the switch character in upper-case.
265 * s - the command line string where s points to the switch character.
267 static void error_unknown_switch(WCHAR chu
, LPWSTR s
)
271 fwprintf(stderr
, L
"%s: Undefined switch /%c!\n", getAppName(), chu
);
275 fwprintf(stderr
, L
"%s: Alphabetic character is expected after '%c' "
276 L
"in switch specification\n", getAppName(), *(s
- 1));
281 BOOL
ProcessCmdLine(LPWSTR lpCmdLine
)
284 REGEDIT_ACTION action
= ACTION_UNDEF
;
285 LPWSTR s
= lpCmdLine
; /* command line pointer */
286 WCHAR ch
= *s
; /* current character */
288 while (ch
&& ((ch
== L
'-') || (ch
== L
'/')))
297 if (!ch2
|| iswspace(ch2
))
301 /* Silence dialogs */
304 else if (chu
== L
'V')
306 /* Ignore this switch */
313 action
= ACTION_DELETE
;
316 action
= ACTION_EXPORT
;
319 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, usage
);
323 error_unknown_switch(chu
, s
);
339 while (*s
&& !iswspace(*s
))
345 error_unknown_switch(chu
, s
);
351 /* this is a file name, starting from '/' */
356 /* skip spaces to the next parameter */
358 while (ch
&& iswspace(ch
))
365 if (*s
&& action
== ACTION_UNDEF
)
368 if (action
!= ACTION_UNDEF
)
369 return PerformRegAction(action
, s
, silent
);