66ce69f1005a8913fce827c72428405c8ef5a7a2
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
24 static const LPCWSTR usage
=
26 L
" regedit filenames\n"
27 L
" regedit /E filename [regpath]\n"
28 L
" regedit /D regpath\n"
30 L
"filenames - List of registry files names\n"
31 L
"filename - Registry file name\n"
32 L
"regpath - Name of the registry key\n"
34 L
"When is called without any switches adds contents of the specified\n"
35 L
"registry files to the registry.\n"
38 L
" /E - Exports contents of the specified registry key to the specified\n"
39 L
" file. Exports the whole registry if no key is specified.\n"
40 L
" /D - Deletes specified registry key\n"
41 L
" /S - Silent execution, can be used with any other switch.\n"
42 L
" The only existing mode, exists for compatibility with Windows regedit.\n"
43 L
" /V - Advanced mode, can be used with any other switch.\n"
44 L
" Ignored, exists for compatibility with Windows regedit.\n"
45 L
" /L - Location of system.dat file. Can be used with any other switch.\n"
46 L
" Ignored. Exists for compatibility with Windows regedit.\n"
47 L
" /R - Location of user.dat file. Can be used with any other switch.\n"
48 L
" Ignored. Exists for compatibility with Windows regedit.\n"
49 L
" /? - Print this help. Any other switches are ignored.\n"
50 L
" /C - Create registry from. Not implemented.\n"
52 L
"The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
53 L
"This program is command-line compatible with Microsoft Windows\n"
58 ACTION_UNDEF
, ACTION_ADD
, ACTION_EXPORT
, ACTION_DELETE
62 LPCWSTR
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 void get_file_name(LPWSTR
*command_line
, LPWSTR file_name
)
80 WCHAR
*s
= *command_line
;
81 size_t pos
= 0; /* position of pointer "s" in *command_line */
97 fwprintf(stderr
, L
"%s: Unexpected end of file name!\n", getAppName());
106 while(s
[0] && !iswspace(s
[0]))
112 memcpy(file_name
, *command_line
, pos
* sizeof(WCHAR
));
113 /* remove the last backslash */
114 if (file_name
[pos
- 1] == L
'\\')
116 file_name
[pos
- 1] = L
'\0';
120 file_name
[pos
] = L
'\0';
128 while(s
[0] && iswspace(s
[0]))
133 (*command_line
) += pos
;
136 BOOL
PerformRegAction(REGEDIT_ACTION action
, LPWSTR s
, BOOL silent
)
142 WCHAR szTitle
[512], szText
[512];
143 WCHAR filename
[MAX_PATH
];
146 get_file_name(&s
, filename
);
149 fwprintf(stderr
, L
"%s: No file name is specified\n", getAppName());
150 // fwprintf(stderr, usage);
151 MessageBoxW(NULL
, usage
, NULL
, MB_OK
| MB_ICONINFORMATION
);
155 LoadStringW(hInst
, IDS_APP_TITLE
, szTitle
, COUNT_OF(szTitle
));
159 /* Request import confirmation */
162 LoadStringW(hInst
, IDS_IMPORT_PROMPT
, szText
, COUNT_OF(szText
));
164 if (InfoMessageBox(NULL
, MB_YESNO
| MB_ICONWARNING
, szTitle
, szText
, filename
) != IDYES
)
168 fp
= _wfopen(filename
, L
"r");
171 import_registry_file(fp
);
173 /* Show successful import */
176 LoadStringW(hInst
, IDS_IMPORT_OK
, szText
, COUNT_OF(szText
));
177 InfoMessageBox(NULL
, MB_OK
| MB_ICONINFORMATION
, szTitle
, szText
, filename
);
184 //LPSTR p = GetMultiByteString(filename);
186 fwprintf(stderr
, L
"%s: Can't open file \"%s\"\n", getAppName(), filename
/*p*/);
187 //HeapFree(GetProcessHeap(), 0, p);
189 /* Error opening the file */
192 LoadStringW(hInst
, IDS_IMPORT_ERROR
, szText
, COUNT_OF(szText
));
193 InfoMessageBox(NULL
, MB_OK
| MB_ICONERROR
, szTitle
, szText
, filename
);
198 get_file_name(&s
, filename
);
205 WCHAR reg_key_name
[KEY_MAX_LEN
];
206 get_file_name(&s
, reg_key_name
);
207 if (!reg_key_name
[0])
209 fwprintf(stderr
, L
"%s: No registry key is specified for removal\n", getAppName());
210 // fwprintf(stderr, usage);
211 MessageBoxW(NULL
, usage
, NULL
, MB_OK
| MB_ICONINFORMATION
);
214 delete_registry_key(reg_key_name
);
220 WCHAR filename
[MAX_PATH
];
223 get_file_name(&s
, filename
);
226 fwprintf(stderr
, L
"%s: No file name is specified\n", getAppName());
227 // fwprintf(stderr, usage);
228 MessageBoxW(NULL
, usage
, NULL
, MB_OK
| MB_ICONINFORMATION
);
234 WCHAR reg_key_name
[KEY_MAX_LEN
];
235 get_file_name(&s
, reg_key_name
);
236 export_registry_key(filename
, reg_key_name
, REG_FORMAT_4
);
240 export_registry_key(filename
, NULL
, REG_FORMAT_4
);
246 fwprintf(stderr
, L
"%s: Unhandled action!\n", getAppName());
255 * Process unknown switch.
258 * chu - the switch character in upper-case.
259 * s - the command line string where s points to the switch character.
261 static void error_unknown_switch(WCHAR chu
, LPWSTR s
)
265 fwprintf(stderr
, L
"%s: Undefined switch /%c!\n", getAppName(), chu
);
269 fwprintf(stderr
, L
"%s: Alphabetic character is expected after '%c' "
270 L
"in switch specification\n", getAppName(), *(s
- 1));
275 BOOL
ProcessCmdLine(LPWSTR lpCmdLine
)
278 REGEDIT_ACTION action
= ACTION_UNDEF
;
279 LPWSTR s
= lpCmdLine
; /* command line pointer */
280 WCHAR ch
= *s
; /* current character */
282 while (ch
&& ((ch
== L
'-') || (ch
== L
'/')))
291 if (!ch2
|| iswspace(ch2
))
295 /* Silence dialogs */
298 else if (chu
== L
'V')
300 /* Ignore this switch */
307 action
= ACTION_DELETE
;
310 action
= ACTION_EXPORT
;
313 //fwprintf(stderr, usage);
314 MessageBoxW(NULL
, usage
, NULL
, MB_OK
| MB_ICONINFORMATION
);
318 error_unknown_switch(chu
, s
);
334 while (*s
&& !iswspace(*s
))
340 error_unknown_switch(chu
, s
);
346 /* this is a file name, starting from '/' */
351 /* skip spaces to the next parameter */
353 while (ch
&& iswspace(ch
))
360 if (*s
&& action
== ACTION_UNDEF
)
363 if (action
!= ACTION_UNDEF
)
364 return PerformRegAction(action
, s
, silent
);