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_ADD
, ACTION_EXPORT
, ACTION_DELETE
62 static BOOL
PerformRegAction(REGEDIT_ACTION action
, char **argv
, int *i
)
66 char *filename
= argv
[*i
];
70 char* realname
= NULL
;
72 if (strcmp(filename
, "-") == 0)
80 size
= SearchPathA(NULL
, filename
, NULL
, 0, NULL
, NULL
);
83 realname
= HeapAlloc(GetProcessHeap(), 0, size
);
84 size
= SearchPathA(NULL
, filename
, NULL
, size
, realname
, NULL
);
88 fprintf(stderr
, "regedit: File not found \"%s\" (%d)\n",
89 filename
, GetLastError());
92 reg_file
= fopen(realname
, "rb");
96 fprintf(stderr
, "regedit: Can't open file \"%s\"\n", filename
);
100 import_registry_file(reg_file
);
103 HeapFree(GetProcessHeap(),0,realname
);
109 case ACTION_DELETE
: {
110 WCHAR
*reg_key_nameW
= GetWideString(argv
[*i
]);
112 delete_registry_key(reg_key_nameW
);
113 HeapFree(GetProcessHeap(), 0, reg_key_nameW
);
116 case ACTION_EXPORT
: {
117 char *filename
= argv
[*i
];
120 filenameW
= GetWideString(filename
);
122 char *reg_key_name
= argv
[++(*i
)];
123 WCHAR
* reg_key_nameW
;
125 reg_key_nameW
= GetWideString(reg_key_name
);
126 export_registry_key(filenameW
, reg_key_nameW
, REG_FORMAT_4
);
127 HeapFree(GetProcessHeap(), 0, reg_key_nameW
);
129 export_registry_key(filenameW
, NULL
, REG_FORMAT_4
);
131 HeapFree(GetProcessHeap(), 0, filenameW
);
135 fprintf(stderr
, "regedit: Unhandled action!\n");
142 char *get_token(char *input
, char **next
)
147 while (*ch
&& isspace(*ch
))
156 while (*ch
&& (*ch
!= '"'))
162 if (*(ch
- 1) == '\\') {
170 while (*ch
&& !isspace(*ch
))
183 BOOL
ProcessCmdLine(LPSTR lpCmdLine
)
189 REGEDIT_ACTION action
= ACTION_ADD
;
201 argv
= HeapAlloc(GetProcessHeap(), 0, i
* sizeof(char *));
205 tok
= get_token(s
, &s
);
206 argv
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(tok
) + 1);
207 strcpy(argv
[i
], tok
);
211 for (i
= 0; i
< argc
; i
++)
213 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
214 break; /* No flags specified. */
216 if (!argv
[i
][1] && argv
[i
][0] == '-')
217 break; /* '-' is a filename. It indicates we should use stdin. */
219 if (argv
[i
][1] && argv
[i
][2] && argv
[i
][2] != ':')
220 break; /* This is a file path beginning with '/'. */
222 switch (toupper(argv
[i
][1]))
225 fprintf(stderr
, usage
);
229 action
= ACTION_DELETE
;
232 action
= ACTION_EXPORT
;
244 fprintf(stderr
, "regedit: Invalid switch [%ls]\n", argv
[i
]);
255 fprintf(stderr
, "regedit: No file name was specified\n\n");
258 fprintf(stderr
,"regedit: No registry key was specified for removal\n\n");
261 fprintf(stderr
, usage
);
265 for (; i
< argc
; i
++)
266 PerformRegAction(action
, argv
, &i
);
268 for (i
= 0; i
< argc
; i
++)
269 HeapFree(GetProcessHeap(), 0, argv
[i
]);
270 HeapFree(GetProcessHeap(), 0, argv
);