attrib: Display filenames with absolute path as in native.
[wine] / programs / regedit / regedit.c
1 /*
2  * Windows regedit.exe registry editor implementation.
3  *
4  * Copyright 2002 Andriy Palamarchuk
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <windows.h>
25 #include "regproc.h"
26
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";
57
58 typedef enum {
59     ACTION_UNDEF, ACTION_ADD, ACTION_EXPORT, ACTION_DELETE
60 } REGEDIT_ACTION;
61
62
63 const CHAR *getAppName(void)
64 {
65     return "regedit";
66 }
67
68 /******************************************************************************
69  * Copies file name from command line string to the buffer.
70  * Rewinds the command line string pointer to the next non-space character
71  * after the file name.
72  * Buffer contains an empty string if no filename was found;
73  *
74  * params:
75  * command_line - command line current position pointer
76  *      where *s[0] is the first symbol of the file name.
77  * file_name - buffer to write the file name to.
78  */
79 static void get_file_name(CHAR **command_line, CHAR *file_name)
80 {
81     CHAR *s = *command_line;
82     int pos = 0;                /* position of pointer "s" in *command_line */
83     file_name[0] = 0;
84
85     if (!s[0]) {
86         return;
87     }
88
89     if (s[0] == '"') {
90         s++;
91         (*command_line)++;
92         while(s[0] != '"') {
93             if (!s[0]) {
94                 fprintf(stderr,"%s: Unexpected end of file name!\n",
95                         getAppName());
96                 exit(1);
97             }
98             s++;
99             pos++;
100         }
101     } else {
102         while(s[0] && !isspace(s[0])) {
103             s++;
104             pos++;
105         }
106     }
107     memcpy(file_name, *command_line, pos * sizeof((*command_line)[0]));
108     /* remove the last backslash */
109     if (file_name[pos - 1] == '\\') {
110         file_name[pos - 1] = '\0';
111     } else {
112         file_name[pos] = '\0';
113     }
114
115     if (s[0]) {
116         s++;
117         pos++;
118     }
119     while(s[0] && isspace(s[0])) {
120         s++;
121         pos++;
122     }
123     (*command_line) += pos;
124 }
125
126 static BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
127 {
128     switch (action) {
129     case ACTION_ADD: {
130             CHAR filename[MAX_PATH];
131             FILE *reg_file;
132
133             get_file_name(&s, filename);
134             if (!filename[0]) {
135                 fprintf(stderr,"%s: No file name was specified\n", getAppName());
136                 fprintf(stderr,usage);
137                 exit(1);
138             }
139
140             while(filename[0]) {
141                 char* realname = NULL;
142
143                 if (strcmp(filename, "-") == 0)
144                 {
145                     reg_file = stdin;
146                 }
147                 else
148                 {
149                     int size;
150
151                     size = SearchPathA(NULL, filename, NULL, 0, NULL, NULL);
152                     if (size > 0)
153                     {
154                         realname = HeapAlloc(GetProcessHeap(), 0, size);
155                         size = SearchPathA(NULL, filename, NULL, size, realname, NULL);
156                     }
157                     if (size == 0)
158                     {
159                         fprintf(stderr, "%s: File not found \"%s\" (%d)\n",
160                                 getAppName(), filename, GetLastError());
161                         exit(1);
162                     }
163                     reg_file = fopen(realname, "rb");
164                     if (reg_file == NULL)
165                     {
166                         perror("");
167                         fprintf(stderr, "%s: Can't open file \"%s\"\n", getAppName(), filename);
168                         exit(1);
169                     }
170                 }
171                 import_registry_file(reg_file);
172                 if (realname)
173                 {
174                     HeapFree(GetProcessHeap(),0,realname);
175                     fclose(reg_file);
176                 }
177                 get_file_name(&s, filename);
178             }
179             break;
180         }
181     case ACTION_DELETE: {
182             CHAR reg_key_name[KEY_MAX_LEN];
183
184             get_file_name(&s, reg_key_name);
185             if (!reg_key_name[0]) {
186                 fprintf(stderr,"%s: No registry key was specified for removal\n",
187                         getAppName());
188                 fprintf(stderr,usage);
189                 exit(1);
190             } else
191             {
192                 WCHAR* reg_key_nameW = GetWideString(reg_key_name);
193                 delete_registry_key(reg_key_nameW);
194                 HeapFree(GetProcessHeap(), 0, reg_key_nameW);
195             }
196             break;
197         }
198     case ACTION_EXPORT: {
199             CHAR filename[MAX_PATH];
200             WCHAR* filenameW;
201
202             filename[0] = '\0';
203             get_file_name(&s, filename);
204             if (!filename[0]) {
205                 fprintf(stderr,"%s: No file name was specified\n", getAppName());
206                 fprintf(stderr,usage);
207                 exit(1);
208             }
209
210             filenameW = GetWideString(filename);
211             if (s[0]) {
212                 CHAR reg_key_name[KEY_MAX_LEN];
213                 WCHAR* reg_key_nameW;
214
215                 get_file_name(&s, reg_key_name);
216                 reg_key_nameW = GetWideString(reg_key_name);
217                 export_registry_key(filenameW, reg_key_nameW, REG_FORMAT_4);
218                 HeapFree(GetProcessHeap(), 0, reg_key_nameW);
219             } else {
220                 export_registry_key(filenameW, NULL, REG_FORMAT_4);
221             }
222             HeapFree(GetProcessHeap(), 0, filenameW);
223             break;
224         }
225     default:
226         fprintf(stderr,"%s: Unhandled action!\n", getAppName());
227         exit(1);
228         break;
229     }
230     return TRUE;
231 }
232
233 /**
234  * Process unknown switch.
235  *
236  * Params:
237  *   chu - the switch character in upper-case.
238  *   s - the command line string where s points to the switch character.
239  */
240 static void error_unknown_switch(char chu, char *s)
241 {
242     if (isalpha(chu)) {
243         fprintf(stderr,"%s: Undefined switch /%c!\n", getAppName(), chu);
244     } else {
245         fprintf(stderr,"%s: Alphabetic character is expected after '%c' "
246                 "in switch specification\n", getAppName(), *(s - 1));
247     }
248     exit(1);
249 }
250
251 BOOL ProcessCmdLine(LPSTR lpCmdLine)
252 {
253     REGEDIT_ACTION action = ACTION_UNDEF;
254     LPSTR s = lpCmdLine;        /* command line pointer */
255     CHAR ch = *s;               /* current character */
256
257     while (ch && ((ch == '-') || (ch == '/'))) {
258         char chu;
259         char ch2;
260
261         s++;
262         ch = *s;
263         if (!ch || isspace(ch))
264         {
265             /* '-' is a file name. It indicates we should use stdin */
266             s--;
267             break;
268         }
269         ch2 = *(s+1);
270         chu = toupper(ch);
271         if (!ch2 || isspace(ch2)) {
272             if (chu == 'S' || chu == 'V') {
273                 /* ignore these switches */
274             } else {
275                 switch (chu) {
276                 case 'D':
277                     action = ACTION_DELETE;
278                     break;
279                 case 'E':
280                     action = ACTION_EXPORT;
281                     break;
282                 case '?':
283                     fprintf(stderr,usage);
284                     exit(0);
285                     break;
286                 default:
287                     error_unknown_switch(chu, s);
288                     break;
289                 }
290             }
291             s++;
292         } else {
293             if (ch2 == ':') {
294                 switch (chu) {
295                 case 'L':
296                     /* fall through */
297                 case 'R':
298                     s += 2;
299                     while (*s && !isspace(*s)) {
300                         s++;
301                     }
302                     break;
303                 default:
304                     error_unknown_switch(chu, s);
305                     break;
306                 }
307             } else {
308                 /* this is a file name, starting from '/' */
309                 s--;
310                 break;
311             }
312         }
313         /* skip spaces to the next parameter */
314         ch = *s;
315         while (ch && isspace(ch)) {
316             s++;
317             ch = *s;
318         }
319     }
320
321     if (*s && action == ACTION_UNDEF)
322         action = ACTION_ADD;
323
324     if (action == ACTION_UNDEF)
325         return FALSE;
326
327     return PerformRegAction(action, s);
328 }