d3d11: Implement d3d11_immediate_context_IAGetVertexBuffers().
[wine.git] / programs / regedit / regedit.c
blobf57f427c00c318905f9f29a143c6f12be4db543d
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <windows.h>
25 #include "regproc.h"
27 static const char *usage =
28 "Usage:\n"
29 " regedit filename\n"
30 " regedit /E filename [regpath]\n"
31 " regedit /D regpath\n"
32 "\n"
33 "filename - registry file name\n"
34 "regpath - name of the registry key\n"
35 "\n"
36 "When called without any switches, adds the content of the specified\n"
37 "file to the registry\n"
38 "\n"
39 "Switches:\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"
53 "\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"
56 "regedit.\n";
58 typedef enum {
59 ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
60 } REGEDIT_ACTION;
62 static BOOL PerformRegAction(REGEDIT_ACTION action, char **argv, int *i)
64 switch (action) {
65 case ACTION_ADD: {
66 char *filename = argv[*i];
67 FILE *reg_file;
69 if (filename[0]) {
70 char* realname = NULL;
72 if (strcmp(filename, "-") == 0)
74 reg_file = stdin;
76 else
78 int size;
80 size = SearchPathA(NULL, filename, NULL, 0, NULL, NULL);
81 if (size > 0)
83 realname = HeapAlloc(GetProcessHeap(), 0, size);
84 size = SearchPathA(NULL, filename, NULL, size, realname, NULL);
86 if (size == 0)
88 fprintf(stderr, "regedit: File not found \"%s\" (%d)\n",
89 filename, GetLastError());
90 exit(1);
92 reg_file = fopen(realname, "rb");
93 if (reg_file == NULL)
95 perror("");
96 fprintf(stderr, "regedit: Can't open file \"%s\"\n", filename);
97 exit(1);
100 import_registry_file(reg_file);
101 if (realname)
103 HeapFree(GetProcessHeap(),0,realname);
104 fclose(reg_file);
107 break;
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);
114 break;
116 case ACTION_EXPORT: {
117 char *filename = argv[*i];
118 WCHAR* filenameW;
120 filenameW = GetWideString(filename);
121 if (filenameW[0]) {
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);
128 } else {
129 export_registry_key(filenameW, NULL, REG_FORMAT_4);
131 HeapFree(GetProcessHeap(), 0, filenameW);
132 break;
134 default:
135 fprintf(stderr, "regedit: Unhandled action!\n");
136 exit(1);
137 break;
139 return TRUE;
142 char *get_token(char *input, char **next)
144 char *ch = input;
145 char *str;
147 while (*ch && isspace(*ch))
148 ch++;
150 str = ch;
152 if (*ch == '"') {
153 ch++;
154 str = ch;
155 for (;;) {
156 while (*ch && (*ch != '"'))
157 ch++;
159 if (!*ch)
160 break;
162 if (*(ch - 1) == '\\') {
163 ch++;
164 continue;
166 break;
169 else {
170 while (*ch && !isspace(*ch))
171 ch++;
174 if (*ch) {
175 *ch = 0;
176 ch++;
179 *next = ch;
180 return str;
183 BOOL ProcessCmdLine(LPSTR lpCmdLine)
185 char *s = lpCmdLine;
186 char **argv;
187 char *tok;
188 int argc = 0, i = 1;
189 REGEDIT_ACTION action = ACTION_ADD;
191 if (!*lpCmdLine)
192 return FALSE;
194 while (*s) {
195 if (isspace(*s))
196 i++;
197 s++;
200 s = lpCmdLine;
201 argv = HeapAlloc(GetProcessHeap(), 0, i * sizeof(char *));
203 for (i = 0; *s; i++)
205 tok = get_token(s, &s);
206 argv[i] = HeapAlloc(GetProcessHeap(), 0, strlen(tok) + 1);
207 strcpy(argv[i], tok);
208 argc++;
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]))
224 case '?':
225 fprintf(stderr, usage);
226 exit(0);
227 break;
228 case 'D':
229 action = ACTION_DELETE;
230 break;
231 case 'E':
232 action = ACTION_EXPORT;
233 break;
234 case 'C':
235 case 'L':
236 case 'R':
237 /* unhandled */;
238 break;
239 case 'S':
240 case 'V':
241 /* ignored */;
242 break;
243 default:
244 fprintf(stderr, "regedit: Invalid switch [%ls]\n", argv[i]);
245 exit(1);
249 if (i == argc)
251 switch (action)
253 case ACTION_ADD:
254 case ACTION_EXPORT:
255 fprintf(stderr, "regedit: No file name was specified\n\n");
256 break;
257 case ACTION_DELETE:
258 fprintf(stderr,"regedit: No registry key was specified for removal\n\n");
259 break;
261 fprintf(stderr, usage);
262 exit(1);
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);
272 return TRUE;