Added base support for user settings.
[reactos.git] / rosapps / sysutils / regexpl / RegistryExplorer.cpp
1 /* $Id: RegistryExplorer.cpp,v 1.6 2001/04/16 05:11:54 narnaoud Exp $
2 *
3 * regexpl - Console Registry Explorer
4 *
5 * Copyright (C) 2000,2001 Nedko Arnaoudov <nedkohome@atia.com>
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 CShellCommandsLinkedList CommandsList(Console);
89
90 CShellCommandExit ExitCommand;
91 CommandsList.AddCommand(&ExitCommand);
92
93 CShellCommandVersion VersionCommand;
94 CommandsList.AddCommand(&VersionCommand);
95
96 CShellCommandHelp HelpCommand(CommandsList);
97 CommandsList.AddCommand(&HelpCommand);
98
99 CShellCommandDir DirCommand(Tree);
100 CommandsList.AddCommand(&DirCommand);
101
102 CShellCommandChangeKey ChangeKeyCommand(Tree);
103 CommandsList.AddCommand(&ChangeKeyCommand);
104
105 CShellCommandValue ValueCommand(Tree);
106 CommandsList.AddCommand(&ValueCommand);
107
108 CShellCommandOwner OwnerCommand(Tree);
109 CommandsList.AddCommand(&OwnerCommand);
110
111 CShellCommandDACL DACLCommand(Tree);
112 CommandsList.AddCommand(&DACLCommand);
113
114 CShellCommandSACL SACLCommand(Tree);
115 CommandsList.AddCommand(&SACLCommand);
116
117 //CShellCommandDOKA DOKACommand(Tree);
118 //CommandsList.AddCommand(&DOKACommand);
119
120 CShellCommandConnect ConnectCommand(Tree);
121 CommandsList.AddCommand(&ConnectCommand);
122
123 CShellCommandNewKey NewKeyCommand(Tree);
124 CommandsList.AddCommand(&NewKeyCommand);
125
126 CShellCommandDeleteKey DeleteKeyCommand(Tree);
127 CommandsList.AddCommand(&DeleteKeyCommand);
128
129 CShellCommandSetValue SetValueCommand(Tree);
130 CommandsList.AddCommand(&SetValueCommand);
131
132 CShellCommandDeleteValue DeleteValueCommand(Tree);
133 CommandsList.AddCommand(&DeleteValueCommand);
134
135 CArgumentParser Parser;
136
137 int nRetCode = 0;
138
139 HRESULT hr;
140
141 CSettings *pSettings = NULL;
142 CPrompt *pPrompt = NULL;
143
144 pSettings = new CSettings();
145 if (!pSettings)
146 {
147 _ftprintf(stderr,_T("Cannot initialize settings. Out of memory.\n"));
148 goto Abort;
149 }
150
151 hr = pSettings->Load(SETTINGS_REGISTRY_KEY);
152 if (FAILED(hr))
153 {
154 _ftprintf(stderr,_T("Cannot load settings. Error is 0x%X.\n"),(unsigned int)hr);
155 goto Abort;
156 }
157
158 pPrompt = new CPrompt(Tree,hr);
159 if (!pPrompt)
160 {
161 _ftprintf(stderr,_T("Cannot initialize prompt. Out of memory.\n"));
162 goto Abort;
163 }
164
165 if (FAILED(hr))
166 {
167 _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
168 goto Abort;
169 }
170
171 // input buffer size in chars
172 #define INPUT_BUFFER_SIZE 1024
173 //#define INPUT_BUFFER_SIZE 128
174 //#define INPUT_BUFFER_SIZE 10
175
176 TCHAR *pchCommand;
177
178 pchCommand = Console.Init(INPUT_BUFFER_SIZE,10);
179 if (pchCommand == NULL)
180 {
181 _ftprintf(stderr,_T("Cannot initialize console.\n"));
182 goto Abort;
183 }
184
185 Console.SetReplaceCompletionCallback(CompletionCallback);
186
187 WORD wOldConsoleAttribute;
188 if (!Console.GetTextAttribute(wOldConsoleAttribute)) goto Abort;
189
190 Console.SetTitle(_T("Registry Explorer"));
191 Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
192
193 VERIFY(SetConsoleCtrlHandler((PHANDLER_ROUTINE)HandlerRoutine,TRUE));
194
195 if (!Console.Write(HELLO_MSG
196 //(_L(__TIMESTAMP__))
197 )) goto Abort;
198
199 //Tree.SetDesiredOpenKeyAccess(KEY_READ);
200
201 hr = pPrompt->SetPrompt(pSettings->GetPrompt());
202 if (FAILED(hr))
203 {
204 _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
205 goto Abort;
206 }
207
208 GetCommand:
209 // prompt
210 // TODO: make prompt user-customizable
211 Console.EnableWrite();
212 pPrompt->ShowPrompt(Console);
213 Console.FlushInputBuffer();
214
215 blnCommandExecutionInProgress = FALSE;
216
217 // Set command line color
218 Console.SetTextAttribute(pSettings->GetPromptTextAttributes());
219 if (!Console.ReadLine())
220 goto Abort;
221
222 // Set normal color
223 Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
224
225 Console.BeginScrollingOperation();
226 blnCommandExecutionInProgress = TRUE;
227
228 // Parse command line (1st step - convert to multi sz)
229 Parser.SetArgumentList(pchCommand);
230
231 int nCommandReturnValue;
232 switch(CommandsList.Execute(Parser,nCommandReturnValue))
233 {
234 case -1: // not recognized command
235 {
236 Parser.ResetArgumentIteration();
237 TCHAR *pchCommandItself = Parser.GetNextArgument();
238 size_t cmdlen = _tcslen(pchCommandItself);
239 if ((!cmdlen)||
240 (pchCommandItself[cmdlen-1] != _T('\\'))||
241 (Parser.GetNextArgument())||
242 (!Tree.ChangeCurrentKey(pchCommandItself)))
243 {
244 Console.Write(_T("Unknown command \""));
245 Console.Write(pchCommandItself);
246 Console.Write(_T("\"\n"));
247 }
248 }
249 case -2: // empty line
250 goto GetCommand;
251 case 0: // exit command
252 nRetCode = 0;
253 Console.SetTextAttribute(wOldConsoleAttribute);
254 goto Exit;
255 default:
256 Console.Write(_T("\n"));
257 goto GetCommand;
258 }
259
260 Abort:
261 _ftprintf(stderr,_T("Abnormal program termination.\nPlease report bugs to ") EMAIL _T("\n"));
262 nRetCode = 1;
263
264 Exit:
265
266 if (pSettings)
267 delete pSettings;
268
269 if (pPrompt)
270 delete pPrompt;
271
272 return nRetCode;
273 }