fix typos.
[reactos.git] / rosapps / sysutils / regexpl / RegistryExplorer.cpp
1 /* $Id$
2 *
3 * regexpl - Console Registry Explorer
4 *
5 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 // RegistryExplorer.cpp : Defines the entry point for the Regiistry Explorer.
24 //
25
26 #include "ph.h"
27 #include "RegistryExplorer.h"
28 #include "Console.h"
29 #include "RegistryKey.h"
30 #include "RegistryTree.h"
31 #include "SecurityDescriptor.h"
32 #include "ArgumentParser.h"
33 #include "ShellCommandsLinkedList.h"
34 #include "ShellCommandExit.h"
35 #include "ShellCommandVersion.h"
36 #include "ShellCommandHelp.h"
37 #include "ShellCommandDir.h"
38 #include "ShellCommandChangeKey.h"
39 #include "ShellCommandValue.h"
40 #include "ShellCommandOwner.h"
41 #include "ShellCommandDACL.h"
42 #include "ShellCommandSACL.h"
43 //#include "ShellCommandDOKA.h"
44 #include "ShellCommandConnect.h"
45 #include "ShellCommandNewKey.h"
46 #include "ShellCommandDeleteKey.h"
47 #include "ShellCommandSetValue.h"
48 #include "ShellCommandDeleteValue.h"
49 #include "Prompt.h"
50 #include "Settings.h"
51
52 TCHAR pchCurrentKey[PROMPT_BUFFER_SIZE];
53
54 CRegistryTree Tree;
55 CConsole Console;
56
57 BOOL blnCommandExecutionInProgress = FALSE;
58
59 BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
60 {
61 switch(dwCtrlType)
62 {
63 case CTRL_C_EVENT:
64 case CTRL_BREAK_EVENT:
65 if (blnCommandExecutionInProgress)
66 {
67 /* Beep(1000,100);
68 Beep(2000,100);
69 Beep(3000,100);
70 Beep(4000,100);
71 Beep(5000,100);
72 Beep(4000,100);
73 Beep(3000,100);
74 Beep(2000,100);
75 Beep(1000,100);*/
76 Console.Write(_T("\n"));
77 Console.DisableWrite();
78 }
79 return TRUE;
80 default:
81 return FALSE;
82 }
83 }
84
85 //int _tmain(/*int argc, TCHAR* argv[], TCHAR* envp[]*/)
86 int main ()
87 {
88 int nRetCode = 0;
89
90 HRESULT hr;
91
92 CSettings *pSettings = NULL;
93 CPrompt *pPrompt = NULL;
94
95 CShellCommandsLinkedList CommandsList(Console);
96
97 CShellCommandExit ExitCommand;
98 CommandsList.AddCommand(&ExitCommand);
99
100 CShellCommandVersion VersionCommand;
101 CommandsList.AddCommand(&VersionCommand);
102
103 CShellCommandHelp HelpCommand(CommandsList);
104 CommandsList.AddCommand(&HelpCommand);
105
106 CShellCommandDir DirCommand(Tree);
107 CommandsList.AddCommand(&DirCommand);
108
109 CShellCommandChangeKey ChangeKeyCommand(Tree);
110 CommandsList.AddCommand(&ChangeKeyCommand);
111
112 CShellCommandValue ValueCommand(Tree);
113 CommandsList.AddCommand(&ValueCommand);
114
115 CShellCommandOwner OwnerCommand(Tree);
116 CommandsList.AddCommand(&OwnerCommand);
117
118 CShellCommandDACL DACLCommand(Tree);
119 CommandsList.AddCommand(&DACLCommand);
120
121 CShellCommandSACL SACLCommand(Tree);
122 CommandsList.AddCommand(&SACLCommand);
123
124 CShellCommandConnect ConnectCommand(Tree);
125 CommandsList.AddCommand(&ConnectCommand);
126
127 CShellCommandNewKey NewKeyCommand(Tree);
128 CommandsList.AddCommand(&NewKeyCommand);
129
130 CShellCommandDeleteKey DeleteKeyCommand(Tree);
131 CommandsList.AddCommand(&DeleteKeyCommand);
132
133 CShellCommandSetValue SetValueCommand(Tree);
134 CommandsList.AddCommand(&SetValueCommand);
135
136 CShellCommandDeleteValue DeleteValueCommand(Tree);
137 CommandsList.AddCommand(&DeleteValueCommand);
138
139 CArgumentParser Parser;
140
141 pSettings = new CSettings();
142 if (!pSettings)
143 {
144 _ftprintf(stderr,_T("Cannot initialize settings. Out of memory.\n"));
145 goto Abort;
146 }
147
148 hr = pSettings->Load(SETTINGS_REGISTRY_KEY);
149 if (FAILED(hr))
150 {
151 _ftprintf(stderr,_T("Cannot load settings. Error is 0x%X.\n"),(unsigned int)hr);
152 goto Abort;
153 }
154
155 pPrompt = new CPrompt(Tree,hr);
156 if (!pPrompt)
157 {
158 _ftprintf(stderr,_T("Cannot initialize prompt. Out of memory.\n"));
159 goto Abort;
160 }
161
162 if (FAILED(hr))
163 {
164 _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
165 goto Abort;
166 }
167
168 // input buffer size in chars
169 #define INPUT_BUFFER_SIZE 1024
170 //#define INPUT_BUFFER_SIZE 128
171 //#define INPUT_BUFFER_SIZE 10
172
173 TCHAR *pchCommand;
174
175 pchCommand = Console.Init(INPUT_BUFFER_SIZE,10);
176 if (pchCommand == NULL)
177 {
178 _ftprintf(stderr,_T("Cannot initialize console.\n"));
179 nRetCode = 1;
180 goto Exit;
181 }
182
183 Console.SetReplaceCompletionCallback(CompletionCallback);
184
185 WORD wOldConsoleAttribute;
186 if (!Console.GetTextAttribute(wOldConsoleAttribute)) goto Abort;
187
188 Console.SetTitle(_T("Registry Explorer"));
189 Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
190
191 VERIFY(SetConsoleCtrlHandler((PHANDLER_ROUTINE)HandlerRoutine,TRUE));
192
193 if (!Console.Write(HELLO_MSG
194 //(_L(__TIMESTAMP__))
195 )) goto Abort;
196
197 //Tree.SetDesiredOpenKeyAccess(KEY_READ);
198
199 hr = pPrompt->SetPrompt(pSettings->GetPrompt());
200 if (FAILED(hr))
201 {
202 _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
203 goto Abort;
204 }
205
206 GetCommand:
207 // prompt
208 // TODO: make prompt user-customizable
209 Console.EnableWrite();
210 pPrompt->ShowPrompt(Console);
211 Console.FlushInputBuffer();
212
213 blnCommandExecutionInProgress = FALSE;
214
215 // Set command line color
216 Console.SetTextAttribute(pSettings->GetCommandTextAttributes());
217 if (!Console.ReadLine())
218 goto Abort;
219
220 // Set normal color
221 Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
222
223 Console.BeginScrollingOperation();
224 blnCommandExecutionInProgress = TRUE;
225
226 // Parse command line (1st step - convert to multi sz)
227 Parser.SetArgumentList(pchCommand);
228
229 int nCommandReturnValue;
230 switch(CommandsList.Execute(Parser,nCommandReturnValue))
231 {
232 case -1: // not recognized command
233 {
234 Parser.ResetArgumentIteration();
235 TCHAR *pchCommandItself = Parser.GetNextArgument();
236 size_t cmdlen = _tcslen(pchCommandItself);
237 if ((!cmdlen)||
238 (pchCommandItself[cmdlen-1] != _T('\\'))||
239 (Parser.GetNextArgument())||
240 (!Tree.ChangeCurrentKey(pchCommandItself)))
241 {
242 Console.Write(_T("Unknown command \""));
243 Console.Write(pchCommandItself);
244 Console.Write(_T("\"\n"));
245 }
246 }
247 case -2: // empty line
248 goto GetCommand;
249 case 0: // exit command
250 nRetCode = 0;
251 Console.SetTextAttribute(wOldConsoleAttribute);
252 goto Exit;
253 default:
254 Console.Write(_T("\n"));
255 goto GetCommand;
256 }
257
258 Abort:
259 _ftprintf(stderr,_T("Abnormal program termination.\nPlease report bugs to ") EMAIL _T("\n"));
260 nRetCode = 1;
261
262 Exit:
263
264 if (pSettings)
265 delete pSettings;
266
267 if (pPrompt)
268 delete pPrompt;
269
270 return nRetCode;
271 }