Updates to test program.
[reactos.git] / reactos / apps / tests / regdump / regcmds.c
1 /* $Id: regcmds.c,v 1.2 2003/01/01 11:16:18 robd Exp $
2 *
3 * ReactOS regedit
4 *
5 * regcmds.c
6 *
7 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
8 *
9 * Original Work Copyright 2002 Andriy Palamarchuk
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <tchar.h>
29 #include <stdio.h>
30
31 #ifdef WIN32_REGDBG
32 #else
33 #include <ctype.h>
34 #endif
35
36 #include "regproc.h"
37
38
39 ////////////////////////////////////////////////////////////////////////////////
40 // Global Variables:
41 //
42
43 static char *usage =
44 "Usage:\n"
45 " regedit filename\n"
46 " regedit /E filename [regpath]\n"
47 " regedit /D regpath\n"
48 "\n"
49 "filename - registry file name\n"
50 "regpath - name of the registry key\n"
51 "\n"
52 "When is called without any switches adds contents of the specified\n"
53 "registry file to the registry\n"
54 "\n"
55 "Switches:\n"
56 " /E - exports contents of the specified registry key to the specified\n"
57 " file. Exports the whole registry if no key is specified.\n"
58 " /D - deletes specified registry key\n"
59 " /S - silent execution, can be used with any other switch.\n"
60 " The only existing mode, exists for compatibility with Windows regedit.\n"
61 " /V - advanced mode, can be used with any other switch.\n"
62 " Ignored, exists for compatibility with Windows regedit.\n"
63 " /L - location of system.dat file. Can be used with any other switch.\n"
64 " Ignored. Exists for compatibility with Windows regedit.\n"
65 " /R - location of user.dat file. Can be used with any other switch.\n"
66 " Ignored. Exists for compatibility with Windows regedit.\n"
67 " /? - print this help. Any other switches are ignored.\n"
68 " /C - create registry from. Not implemented.\n"
69 "\n"
70 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
71 "This program is command-line compatible with Microsoft Windows\n"
72 "regedit. The difference with Windows regedit - this application has\n"
73 "command-line interface only.\n";
74
75 typedef enum {
76 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE, ACTION_VIEW
77 } REGEDIT_ACTION;
78
79 /**
80 * Process unknown switch.
81 *
82 * Params:
83 * chu - the switch character in upper-case.
84 * s - the command line string where s points to the switch character.
85 */
86 void error_unknown_switch(char chu, char *s)
87 {
88 if (isalpha(chu)) {
89 printf("Undefined switch /%c!\n", chu);
90 } else {
91 printf("Alphabetic character is expected after '%c' "
92 "in switch specification\n", *(s - 1));
93 }
94 //exit(1);
95 }
96
97 BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
98 {
99 TCHAR filename[MAX_PATH];
100 TCHAR reg_key_name[KEY_MAX_LEN];
101
102 switch (action) {
103 case ACTION_ADD:
104 get_file_name(&s, filename, MAX_PATH);
105 if (!filename[0]) {
106 printf("No file name is specified\n%s", usage);
107 return FALSE;
108 //exit(1);
109 }
110 while (filename[0]) {
111 if (!import_registry_file(filename)) {
112 perror("");
113 printf("Can't open file \"%s\"\n", filename);
114 return FALSE;
115 //exit(1);
116 }
117 get_file_name(&s, filename, MAX_PATH);
118 }
119 break;
120 case ACTION_DELETE:
121 get_file_name(&s, reg_key_name, KEY_MAX_LEN);
122 if (!reg_key_name[0]) {
123 printf("No registry key is specified for removal\n%s", usage);
124 return FALSE;
125 //exit(1);
126 }
127 delete_registry_key(reg_key_name);
128 break;
129 case ACTION_EXPORT:
130 filename[0] = '\0';
131 get_file_name(&s, filename, MAX_PATH);
132 if (!filename[0]) {
133 printf("No file name is specified\n%s", usage);
134 return FALSE;
135 //exit(1);
136 }
137 if (s[0]) {
138 get_file_name(&s, reg_key_name, KEY_MAX_LEN);
139 export_registry_key(filename, reg_key_name);
140 } else {
141 export_registry_key(filename, NULL);
142 }
143 break;
144 default:
145 printf("Unhandled action!\n");
146 return FALSE;
147 }
148 return TRUE;
149 }
150
151 BOOL ProcessCmdLine(LPSTR lpCmdLine)
152 {
153 REGEDIT_ACTION action = ACTION_UNDEF;
154 LPSTR s = lpCmdLine; /* command line pointer */
155 CHAR ch = *s; /* current character */
156
157 while (ch && ((ch == '-') || (ch == '/'))) {
158 char chu;
159 char ch2;
160
161 s++;
162 ch = *s;
163 ch2 = *(s+1);
164 chu = toupper(ch);
165 if (!ch2 || isspace(ch2)) {
166 if (chu == 'S' || chu == 'V') {
167 /* ignore these switches */
168 } else {
169 switch (chu) {
170 case 'D':
171 action = ACTION_DELETE;
172 break;
173 case 'E':
174 action = ACTION_EXPORT;
175 break;
176 case 'V':
177 action = ACTION_VIEW;
178 break;
179 case '?':
180 printf(usage);
181 return FALSE;
182 //exit(0);
183 break;
184 default:
185 error_unknown_switch(chu, s);
186 return FALSE;
187 break;
188 }
189 }
190 s++;
191 } else {
192 if (ch2 == ':') {
193 switch (chu) {
194 case 'L':
195 /* fall through */
196 case 'R':
197 s += 2;
198 while (*s && !isspace(*s)) {
199 s++;
200 }
201 break;
202 default:
203 error_unknown_switch(chu, s);
204 return FALSE;
205 break;
206 }
207 } else {
208 /* this is a file name, starting from '/' */
209 s--;
210 break;
211 }
212 }
213 /* skip spaces to the next parameter */
214 ch = *s;
215 while (ch && isspace(ch)) {
216 s++;
217 ch = *s;
218 }
219 }
220 if (action == ACTION_UNDEF) {
221 action = ACTION_ADD;
222 }
223 return PerformRegAction(action, s);
224 }