Authors: Robert Dickenson <robd@reactos.org>, Steven Edwards <Steven_Ed4153@yahoo...
[wine/multimedia.git] / programs / regedit / regedit.c
blob704b2e32cd02a79e4a6a8ce52a9d6e8b8a02c252
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <windows.h>
24 #include "regproc.h"
26 static char *usage =
27 "Usage:\n"
28 " regedit filename\n"
29 " regedit /E filename [regpath]\n"
30 " regedit /D regpath\n"
31 "\n"
32 "filename - registry file name\n"
33 "regpath - name of the registry key\n"
34 "\n"
35 "When is called without any switches adds contents of the specified\n"
36 "registry file to the registry\n"
37 "\n"
38 "Switches:\n"
39 " /E - exports contents of the specified registry key to the specified\n"
40 " file. Exports the whole registry if no key is specified.\n"
41 " /D - deletes specified registry key\n"
42 " /S - silent execution, can be used with any other switch.\n"
43 " The only existing mode, exists for compatibility with Windows regedit.\n"
44 " /V - advanced mode, can be used with any other switch.\n"
45 " Ignored, exists for compatibility with Windows regedit.\n"
46 " /L - location of system.dat file. Can be used with any other switch.\n"
47 " Ignored. Exists for compatibility with Windows regedit.\n"
48 " /R - location of user.dat file. Can be used with any other switch.\n"
49 " Ignored. Exists for compatibility with Windows regedit.\n"
50 " /? - print this help. Any other switches are ignored.\n"
51 " /C - create registry from. Not implemented.\n"
52 "\n"
53 "The switches are case-insensitive, can be prefixed either by '-' or '/'.\n"
54 "This program is command-line compatible with Microsoft Windows\n"
55 "regedit. The difference with Windows regedit - this application has\n"
56 "command-line interface only.\n";
58 typedef enum {
59 ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
60 } REGEDIT_ACTION;
62 BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s);
64 /**
65 * Process unknown switch.
67 * Params:
68 * chu - the switch character in upper-case.
69 * s - the command line string where s points to the switch character.
71 void error_unknown_switch(char chu, char *s)
73 if (isalpha(chu))
75 printf("%s: Undefined switch /%c!\n", getAppName(), chu);
76 } else {
77 printf("%s: Alphabetic character is expected after '%c' "
78 "in switch specification\n", getAppName(), *(s - 1));
80 exit(1);
83 BOOL ProcessCmdLine(LPSTR lpCmdLine)
85 REGEDIT_ACTION action = ACTION_UNDEF;
86 LPSTR s = lpCmdLine; /* command line pointer */
87 CHAR ch = *s; /* current character */
89 setAppName("regedit");
90 while (ch && ((ch == '-') || (ch == '/')))
92 char chu;
93 char ch2;
95 s++;
96 ch = *s;
97 ch2 = *(s+1);
98 chu = toupper(ch);
99 if (!ch2 || isspace(ch2))
101 if (chu == 'S' || chu == 'V')
103 /* ignore these switches */
104 } else {
105 switch (chu)
107 case 'D':
108 action = ACTION_DELETE;
109 break;
110 case 'E':
111 action = ACTION_EXPORT;
112 break;
113 case '?':
114 printf(usage);
115 exit(0);
116 break;
117 default:
118 error_unknown_switch(chu, s);
119 break;
122 s++;
123 } else {
124 if (ch2 == ':')
126 switch (chu)
128 case 'L':
129 /* fall through */
130 case 'R':
131 s += 2;
132 while (*s && !isspace(*s))
134 s++;
136 break;
137 default:
138 error_unknown_switch(chu, s);
139 break;
141 } else {
142 /* this is a file name, starting from '/' */
143 s--;
144 break;
147 /* skip spaces to the next parameter */
148 ch = *s;
149 while (ch && isspace(ch))
151 s++;
152 ch = *s;
156 if (action == ACTION_UNDEF)
157 return FALSE;
159 return PerformRegAction(action, s);
162 BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
164 switch (action)
166 case ACTION_ADD:
168 CHAR filename[MAX_PATH];
169 FILE *reg_file;
171 get_file_name(&s, filename);
172 if (!filename[0])
174 printf("%s: No file name is specified\n", getAppName());
175 printf(usage);
176 exit(1);
179 while(filename[0])
181 reg_file = fopen(filename, "r");
182 if (reg_file)
184 processRegLines(reg_file, doSetValue);
185 } else {
186 perror("");
187 printf("%s: Can't open file \"%s\"\n", getAppName(), filename);
188 exit(1);
190 get_file_name(&s, filename);
192 break;
194 case ACTION_DELETE:
196 CHAR reg_key_name[KEY_MAX_LEN];
198 get_file_name(&s, reg_key_name);
199 if (!reg_key_name[0])
201 printf("%s: No registry key is specified for removal\n",
202 getAppName());
203 printf(usage);
204 exit(1);
206 delete_registry_key(reg_key_name);
207 break;
209 case ACTION_EXPORT:
211 CHAR filename[MAX_PATH];
213 filename[0] = '\0';
214 get_file_name(&s, filename);
215 if (!filename[0])
217 printf("%s: No file name is specified\n", getAppName());
218 printf(usage);
219 exit(1);
222 if (s[0])
224 CHAR reg_key_name[KEY_MAX_LEN];
226 get_file_name(&s, reg_key_name);
227 export_registry_key(filename, reg_key_name);
228 } else {
229 export_registry_key(filename, NULL);
231 break;
233 default:
234 printf("%s: Unhandled action!\n", getAppName());
235 exit(1);
236 break;
238 return 0;