2002-04-12 01:58:40 +02:00
|
|
|
/*
|
|
|
|
* Windows regedit.exe registry editor implementation.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Andriy Palamarchuk
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-04-12 01:58:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include "regproc.h"
|
|
|
|
|
2005-06-04 12:01:25 +02:00
|
|
|
static const char *usage =
|
2003-12-08 23:48:07 +01:00
|
|
|
"Usage:\n"
|
|
|
|
" regedit filename\n"
|
|
|
|
" regedit /E filename [regpath]\n"
|
|
|
|
" regedit /D regpath\n"
|
|
|
|
"\n"
|
|
|
|
"filename - registry file name\n"
|
|
|
|
"regpath - name of the registry key\n"
|
|
|
|
"\n"
|
2007-05-15 14:01:05 +02:00
|
|
|
"When called without any switches, adds the content of the specified\n"
|
|
|
|
"file to the registry\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
"\n"
|
|
|
|
"Switches:\n"
|
|
|
|
" /E - exports contents of the specified registry key to the specified\n"
|
|
|
|
" file. Exports the whole registry if no key is specified.\n"
|
|
|
|
" /D - deletes specified registry key\n"
|
|
|
|
" /S - silent execution, can be used with any other switch.\n"
|
2007-05-09 18:18:29 +02:00
|
|
|
" Default. The only existing mode, exists for compatibility with Windows regedit.\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
" /V - advanced mode, can be used with any other switch.\n"
|
|
|
|
" Ignored, exists for compatibility with Windows regedit.\n"
|
|
|
|
" /L - location of system.dat file. Can be used with any other switch.\n"
|
|
|
|
" Ignored. Exists for compatibility with Windows regedit.\n"
|
|
|
|
" /R - location of user.dat file. Can be used with any other switch.\n"
|
|
|
|
" Ignored. Exists for compatibility with Windows regedit.\n"
|
|
|
|
" /? - print this help. Any other switches are ignored.\n"
|
2007-05-09 18:18:29 +02:00
|
|
|
" /C - create registry from file. Not implemented.\n"
|
2003-12-08 23:48:07 +01:00
|
|
|
"\n"
|
|
|
|
"The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
|
|
|
|
"This program is command-line compatible with Microsoft Windows\n"
|
2005-06-23 11:47:17 +02:00
|
|
|
"regedit.\n";
|
2002-04-12 01:58:40 +02:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
|
|
|
|
} REGEDIT_ACTION;
|
|
|
|
|
2003-08-07 05:10:13 +02:00
|
|
|
BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s);
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
/**
|
|
|
|
* Process unknown switch.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* chu - the switch character in upper-case.
|
|
|
|
* s - the command line string where s points to the switch character.
|
|
|
|
*/
|
2005-06-04 12:01:25 +02:00
|
|
|
static void error_unknown_switch(char chu, char *s)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
2003-12-08 23:48:07 +01:00
|
|
|
if (isalpha(chu)) {
|
2003-12-05 05:42:06 +01:00
|
|
|
fprintf(stderr,"%s: Undefined switch /%c!\n", getAppName(), chu);
|
2002-04-12 01:58:40 +02:00
|
|
|
} else {
|
2003-12-05 05:42:06 +01:00
|
|
|
fprintf(stderr,"%s: Alphabetic character is expected after '%c' "
|
2007-05-09 18:18:29 +02:00
|
|
|
"in switch specification\n", getAppName(), *(s - 1));
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-08-07 05:10:13 +02:00
|
|
|
BOOL ProcessCmdLine(LPSTR lpCmdLine)
|
2002-04-12 01:58:40 +02:00
|
|
|
{
|
|
|
|
REGEDIT_ACTION action = ACTION_UNDEF;
|
|
|
|
LPSTR s = lpCmdLine; /* command line pointer */
|
|
|
|
CHAR ch = *s; /* current character */
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
setAppName("regedit");
|
2003-12-08 23:48:07 +01:00
|
|
|
while (ch && ((ch == '-') || (ch == '/'))) {
|
2002-04-12 01:58:40 +02:00
|
|
|
char chu;
|
|
|
|
char ch2;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
s++;
|
|
|
|
ch = *s;
|
|
|
|
ch2 = *(s+1);
|
|
|
|
chu = toupper(ch);
|
2003-12-08 23:48:07 +01:00
|
|
|
if (!ch2 || isspace(ch2)) {
|
|
|
|
if (chu == 'S' || chu == 'V') {
|
2002-04-12 01:58:40 +02:00
|
|
|
/* ignore these switches */
|
|
|
|
} else {
|
2003-12-08 23:48:07 +01:00
|
|
|
switch (chu) {
|
2002-04-12 01:58:40 +02:00
|
|
|
case 'D':
|
|
|
|
action = ACTION_DELETE;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
action = ACTION_EXPORT;
|
|
|
|
break;
|
|
|
|
case '?':
|
2003-12-05 05:42:06 +01:00
|
|
|
fprintf(stderr,usage);
|
2002-04-12 01:58:40 +02:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_unknown_switch(chu, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
} else {
|
2003-12-08 23:48:07 +01:00
|
|
|
if (ch2 == ':') {
|
|
|
|
switch (chu) {
|
2002-04-12 01:58:40 +02:00
|
|
|
case 'L':
|
|
|
|
/* fall through */
|
|
|
|
case 'R':
|
|
|
|
s += 2;
|
2003-12-08 23:48:07 +01:00
|
|
|
while (*s && !isspace(*s)) {
|
2002-04-12 01:58:40 +02:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_unknown_switch(chu, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2002-05-04 20:30:27 +02:00
|
|
|
/* this is a file name, starting from '/' */
|
|
|
|
s--;
|
2002-04-12 01:58:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* skip spaces to the next parameter */
|
|
|
|
ch = *s;
|
2003-12-08 23:48:07 +01:00
|
|
|
while (ch && isspace(ch)) {
|
2002-04-12 01:58:40 +02:00
|
|
|
s++;
|
|
|
|
ch = *s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-15 05:47:04 +02:00
|
|
|
if (*s && action == ACTION_UNDEF)
|
|
|
|
action = ACTION_ADD;
|
|
|
|
|
2002-04-12 01:58:40 +02:00
|
|
|
if (action == ACTION_UNDEF)
|
2003-08-07 05:10:13 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return PerformRegAction(action, s);
|
2003-08-15 05:47:04 +02:00
|
|
|
}
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2003-08-07 05:10:13 +02:00
|
|
|
BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
|
|
|
|
{
|
2003-12-08 23:48:07 +01:00
|
|
|
switch (action) {
|
|
|
|
case ACTION_ADD: {
|
|
|
|
CHAR filename[MAX_PATH];
|
|
|
|
FILE *reg_file;
|
2002-04-12 01:58:40 +02:00
|
|
|
|
2003-12-08 23:48:07 +01:00
|
|
|
get_file_name(&s, filename);
|
|
|
|
if (!filename[0]) {
|
2007-05-09 18:18:29 +02:00
|
|
|
fprintf(stderr,"%s: No file name was specified\n", getAppName());
|
2003-12-08 23:48:07 +01:00
|
|
|
fprintf(stderr,usage);
|
2002-06-21 01:09:33 +02:00
|
|
|
exit(1);
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
|
2003-12-08 23:48:07 +01:00
|
|
|
while(filename[0]) {
|
2004-01-13 06:07:07 +01:00
|
|
|
char* realname = NULL;
|
|
|
|
int size;
|
|
|
|
size=SearchPath(NULL,filename,NULL,0,NULL,NULL);
|
|
|
|
if (size>0)
|
|
|
|
{
|
|
|
|
realname=HeapAlloc(GetProcessHeap(),0,size);
|
|
|
|
size=SearchPath(NULL,filename,NULL,size,realname,NULL);
|
|
|
|
}
|
|
|
|
if (size==0)
|
|
|
|
{
|
2006-10-09 18:38:30 +02:00
|
|
|
fprintf(stderr,"%s: File not found \"%s\" (%d)\n",
|
2004-01-13 06:07:07 +01:00
|
|
|
getAppName(),filename,GetLastError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
reg_file = fopen(realname, "r");
|
|
|
|
if (reg_file==NULL)
|
|
|
|
{
|
2003-12-08 23:48:07 +01:00
|
|
|
perror("");
|
|
|
|
fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-01-13 06:07:07 +01:00
|
|
|
processRegLines(reg_file, doSetValue);
|
|
|
|
if (realname)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),0,realname);
|
|
|
|
fclose(reg_file);
|
|
|
|
}
|
2003-12-08 23:48:07 +01:00
|
|
|
get_file_name(&s, filename);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_DELETE: {
|
2002-04-12 01:58:40 +02:00
|
|
|
CHAR reg_key_name[KEY_MAX_LEN];
|
|
|
|
|
|
|
|
get_file_name(&s, reg_key_name);
|
2003-12-08 23:48:07 +01:00
|
|
|
if (!reg_key_name[0]) {
|
2007-05-09 18:18:29 +02:00
|
|
|
fprintf(stderr,"%s: No registry key was specified for removal\n",
|
2003-12-08 23:48:07 +01:00
|
|
|
getAppName());
|
|
|
|
fprintf(stderr,usage);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
delete_registry_key(reg_key_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_EXPORT: {
|
|
|
|
CHAR filename[MAX_PATH];
|
|
|
|
|
|
|
|
filename[0] = '\0';
|
|
|
|
get_file_name(&s, filename);
|
|
|
|
if (!filename[0]) {
|
2007-05-09 18:18:29 +02:00
|
|
|
fprintf(stderr,"%s: No file name was specified\n", getAppName());
|
2003-12-08 23:48:07 +01:00
|
|
|
fprintf(stderr,usage);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s[0]) {
|
|
|
|
CHAR reg_key_name[KEY_MAX_LEN];
|
|
|
|
|
|
|
|
get_file_name(&s, reg_key_name);
|
|
|
|
export_registry_key(filename, reg_key_name);
|
|
|
|
} else {
|
|
|
|
export_registry_key(filename, NULL);
|
|
|
|
}
|
|
|
|
break;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|
|
|
|
default:
|
2003-12-05 05:42:06 +01:00
|
|
|
fprintf(stderr,"%s: Unhandled action!\n", getAppName());
|
2002-04-12 01:58:40 +02:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
2003-08-15 05:47:04 +02:00
|
|
|
return TRUE;
|
2002-04-12 01:58:40 +02:00
|
|
|
}
|