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
26 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
31 static void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
35 if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
))
40 /* WriteConsole() fails on Windows if its output is redirected. If this occurs,
41 * we should call WriteFile() with OEM code page.
43 len
= WideCharToMultiByte(GetOEMCP(), 0, str
, wlen
, NULL
, 0, NULL
, NULL
);
47 WideCharToMultiByte(GetOEMCP(), 0, str
, wlen
, msgA
, len
, NULL
, NULL
);
48 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
53 static void output_formatstring(const WCHAR
*fmt
, va_list va_args
)
58 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
59 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
60 if (len
== 0 && GetLastError() != ERROR_NO_WORK_DONE
)
62 WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
65 output_writeconsole(str
, len
);
69 void WINAPIV
output_message(unsigned int id
, ...)
74 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
76 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
79 va_start(va_args
, id
);
80 output_formatstring(fmt
, va_args
);
84 void WINAPIV
error_exit(unsigned int id
, ...)
89 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
91 WINE_FIXME("LoadString failed with %lu\n", GetLastError());
94 va_start(va_args
, id
);
95 output_formatstring(fmt
, va_args
);
98 exit(0); /* regedit.exe always terminates with error code zero */
102 ACTION_ADD
, ACTION_EXPORT
, ACTION_DELETE
105 static void PerformRegAction(REGEDIT_ACTION action
, WCHAR
**argv
, int *i
)
109 WCHAR
*filename
= argv
[*i
];
110 WCHAR
*realname
= NULL
;
113 if (!lstrcmpW(filename
, L
"-"))
119 size
= SearchPathW(NULL
, filename
, NULL
, 0, NULL
, NULL
);
122 realname
= malloc(size
* sizeof(WCHAR
));
123 size
= SearchPathW(NULL
, filename
, NULL
, size
, realname
, NULL
);
127 output_message(STRING_FILE_NOT_FOUND
, filename
);
131 reg_file
= _wfopen(realname
, L
"rb");
132 if (reg_file
== NULL
)
134 _wperror(L
"regedit");
135 output_message(STRING_CANNOT_OPEN_FILE
, filename
);
140 import_registry_file(reg_file
);
149 delete_registry_key(argv
[*i
]);
151 case ACTION_EXPORT
: {
152 WCHAR
*filename
= argv
[*i
];
153 WCHAR
*key_name
= argv
[++(*i
)];
155 if (key_name
&& *key_name
)
156 export_registry_key(filename
, key_name
, REG_FORMAT_5
);
158 export_registry_key(filename
, NULL
, REG_FORMAT_5
);
162 error_exit(STRING_UNHANDLED_ACTION
);
167 BOOL
ProcessCmdLine(WCHAR
*cmdline
)
171 REGEDIT_ACTION action
= ACTION_ADD
;
173 argv
= CommandLineToArgvW(cmdline
, &argc
);
184 for (i
= 1; i
< argc
; i
++)
186 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
187 break; /* No flags specified. */
189 if (!argv
[i
][1] && argv
[i
][0] == '-')
190 break; /* '-' is a filename. It indicates we should use stdin. */
192 if (argv
[i
][1] && argv
[i
][2] && argv
[i
][2] != ':')
193 break; /* This is a file path beginning with '/'. */
195 switch (towupper(argv
[i
][1]))
198 error_exit(STRING_USAGE
);
201 action
= ACTION_DELETE
;
204 action
= ACTION_EXPORT
;
217 output_message(STRING_INVALID_SWITCH
, argv
[i
]);
218 error_exit(STRING_HELP
);
228 output_message(STRING_NO_FILENAME
);
231 output_message(STRING_NO_REG_KEY
);
234 error_exit(STRING_HELP
);
237 for (; i
< argc
; i
++)
238 PerformRegAction(action
, argv
, &i
);