Added base support for user settings.
[reactos.git] / rosapps / sysutils / regexpl / RegistryExplorer.cpp
index 96996cf..2d9711c 100644 (file)
@@ -1,14 +1,26 @@
-/* $Id: RegistryExplorer.cpp,v 1.1 2000/10/04 21:04:30 ea Exp $
+/* $Id: RegistryExplorer.cpp,v 1.6 2001/04/16 05:11:54 narnaoud Exp $
  *
  * regexpl - Console Registry Explorer
  *
- * Copyright (c) 1999-2000 Nedko Arnaoudov <nedkohome@atia.com>
+ * Copyright (C) 2000,2001 Nedko Arnaoudov <nedkohome@atia.com>
  *
- * License: GNU GPL
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
  */
 
-// Registry Explorer.cpp : Defines the entry point for the console application.
+// RegistryExplorer.cpp : Defines the entry point for the Regiistry Explorer.
 //
 
 #include "ph.h"
 #include "ShellCommandOwner.h"
 #include "ShellCommandDACL.h"
 #include "ShellCommandSACL.h"
-#include "ShellCommandDOKA.h"
+//#include "ShellCommandDOKA.h"
 #include "ShellCommandConnect.h"
 #include "ShellCommandNewKey.h"
 #include "ShellCommandDeleteKey.h"
 #include "ShellCommandSetValue.h"
+#include "ShellCommandDeleteValue.h"
+#include "Prompt.h"
+#include "Settings.h"
 
 TCHAR pchCurrentKey[PROMPT_BUFFER_SIZE];
 
-CRegistryTree Tree(PROMPT_BUFFER_SIZE+1);
+CRegistryTree Tree;
 CConsole Console;
 
-const TCHAR * ppchRootKeys[7] = 
-{
-       _T("HKEY_CLASSES_ROOT"),
-       _T("HKEY_CURRENT_USER"),
-       _T("HKEY_LOCAL_MACHINE"),
-       _T("HKEY_USERS"),
-       _T("HKEY_PERFORMANCE_DATA"),
-       _T("HKEY_CURRENT_CONFIG"),
-       _T("HKEY_DYN_DATA")             //win9x only?
-};
-
-BOOL g_blnCompletionCycle = TRUE;
-
-const TCHAR * CompletionCallback(unsigned __int64 & rnIndex, const BOOL *pblnForward, const TCHAR *pchContext, const TCHAR *pchBegin)
-{
-#define COMPLETION_BUFFER_SIZE 4096
-       static TCHAR pchBuffer[COMPLETION_BUFFER_SIZE];
-       CRegistryKey *pKey = NULL;
-       CRegistryTree *pTree = NULL;
-       unsigned __int64 nTotalKeys = 0;
-       unsigned __int64 nTotalValues = 0;
-       unsigned __int64 nTotalItems = 0;
-       class CompletionMatch
-       {
-       public:
-               CompletionMatch(const TCHAR *pchText, CompletionMatch **pNext)
-               {
-                       BOOL b = _tcschr(pchText,_T(' ')) != NULL;
-                       size_t s = _tcslen(pchText);
-                       m_pchText = new TCHAR [s+(b?3:1)];
-                       if (b)
-                       {
-                               m_pchText[0] = _T('\"');
-                               _tcscpy(m_pchText+1,pchText);
-                               m_pchText[s+1] = _T('\"');
-                               m_pchText[s+2] = 0;
-                       }
-                       else
-                       {
-                               _tcscpy(m_pchText,pchText);
-                       }
-                       if (m_pchText)
-                       {
-                               m_pNext = *pNext;
-                               *pNext = this;
-                       }
-               }
-               ~CompletionMatch()
-               {
-                       if (m_pchText)
-                               delete m_pchText;
-                       if (m_pNext)
-                               delete m_pNext;
-               }
-               const TCHAR *GetText(unsigned __int64 dwReverseIndex)
-               {
-                       if (dwReverseIndex)
-                       {
-                               if (m_pNext)
-                                       return m_pNext->GetText(dwReverseIndex-1);
-                               else
-                                       return NULL;
-                       }
-                       return m_pchText;
-               }
-       private:
-               TCHAR *m_pchText;
-               CompletionMatch *m_pNext;
-       };
-       CompletionMatch *pRootMatch = NULL;
-
-       BOOL blnCompletionOnKeys = TRUE;
-       BOOL blnCompletionOnValues = TRUE;
-
-       while(*pchContext && _istspace(*pchContext))
-       {
-               pchContext++;
-       }
-
-/*     if ((_tcsnicmp(pchContext,DIR_CMD,DIR_CMD_LENGTH) == 0)||
-               (_tcsnicmp(pchContext,CD_CMD,CD_CMD_LENGTH) == 0)||
-               (_tcsnicmp(pchContext,OWNER_CMD,OWNER_CMD_LENGTH) == 0)||
-               (_tcsnicmp(pchContext,DACL_CMD,DACL_CMD_LENGTH) == 0)||
-               (_tcsnicmp(pchContext,SACL_CMD,SACL_CMD_LENGTH) == 0))
-       {
-               blnCompletionOnValues = FALSE;
-       }*/
-//     else if (_tcsnicmp(pchContext,VALUE_CMD,VALUE_CMD_LENGTH) == 0)
-//     {
-//             blnCompletionOnKeys = FALSE;
-//     }
-
-       const TCHAR *pchKey = _tcsrchr(pchBegin,_T('\\'));
-       DWORD nBufferOffset = 0;
-       if (pchKey)
-       {
-               nBufferOffset = pchKey-pchBegin+1;
-               if (nBufferOffset >= COMPLETION_BUFFER_SIZE-1)
-               {
-                       if (pRootMatch) delete pRootMatch;
-                       if (pTree)
-                               delete pTree;
-                       return _T("internal error");
-               }
-               _tcsncpy(pchBuffer,pchBegin,nBufferOffset);
-               pchBuffer[nBufferOffset] = 0;
-               pchBegin = pchKey+1;
-               pTree = new CRegistryTree(Tree);
-               if ((_tcscmp(pTree->GetCurrentPath(),Tree.GetCurrentPath()) != 0)||(!pTree->ChangeCurrentKey(pchBuffer)))
-               {
-                       if (pTree)
-                               delete pTree;
-                       return NULL;
-               }
-               else
-               {
-                       pKey = pTree->GetCurrentKey();
-               }
-       }
-       else
-       {
-               pKey = Tree.GetCurrentKey();
-       }
-
-       if (!pKey)
-       {
-               for(unsigned int i = 0 ; i < sizeof(ppchRootKeys)/sizeof(TCHAR *) ; i++)
-               {
-                       nTotalKeys++;
-                       nTotalItems++;
-                       CompletionMatch *p = new CompletionMatch(ppchRootKeys[i],&pRootMatch);
-                       if (!p || !p->GetText(0))
-                       {
-                               if (pRootMatch) delete pRootMatch;
-                               if (pTree)
-                                       delete pTree;
-                               return _T("Out of memory");
-                       }
-               }
-       }
-       else
-       {
-               CompletionMatch *p;
-               DWORD dwError;
-               if (blnCompletionOnKeys)
-               {
-/*                     if (_tcslen(pchBegin) == 0)
-                       {
-                               p = new CompletionMatch(_T(".."),&pRootMatch);
-                               if (!p || !p->GetText(0))
-                               {
-                                       if (pRootMatch) delete pRootMatch;
-                                       if (pTree)
-                                               delete pTree;
-                                       return _T("Out of memory");
-                               }
-                               nTotalKeys++;
-                               nTotalItems++;
-                       }
-*/                     
-                       pKey->InitSubKeyEnumeration();
-                       TCHAR *pchSubKeyName;
-                       while ((pchSubKeyName = pKey->GetSubKeyName(dwError)) != NULL)
-                       {
-                               if (_tcsnicmp(pchSubKeyName,pchBegin,_tcslen(pchBegin)) == 0)
-                               {
-                                       nTotalKeys++;
-                                       nTotalItems++;
-                                       p = new CompletionMatch(pchSubKeyName,&pRootMatch);
-                                       if (!p || !p->GetText(0))
-                                       {
-                                               if (pRootMatch) delete pRootMatch;
-                                               if (pTree)
-                                                       delete pTree;
-                                               return _T("Out of memory");
-                                       }
-                               }
-                       }
-                       if ((dwError != ERROR_SUCCESS)&&(dwError != ERROR_NO_MORE_ITEMS))
-                       {
-                               if (pRootMatch) delete pRootMatch;
-                               if (pTree)
-                                       delete pTree;
-                               return _T("error");
-                       }
-               }
-               
-               if (blnCompletionOnValues)
-               {
-                       pKey->InitValueEnumeration();
-                       TCHAR *pchValueName;
-                       DWORD dwValueNameLength, dwMaxValueNameLength;
-                       dwError = pKey->GetMaxValueNameLength(dwMaxValueNameLength);
-                       if (dwError != ERROR_SUCCESS)
-                       {
-                               if (pRootMatch) delete pRootMatch;
-                               if (pTree)
-                                       delete pTree;
-                               return _T("error");
-                       }
-                       
-                       dwMaxValueNameLength++;
-                       pchValueName = new TCHAR [dwMaxValueNameLength];
-                       if (!pchValueName)
-                       {
-                               if (pRootMatch) delete pRootMatch;
-                               if (pTree)
-                                       delete pTree;
-                               return _T("Out of memory");
-                       }
-                       for(;;)
-                       {
-                               dwValueNameLength = dwMaxValueNameLength;
-                               //dwValueSize = dwMaxValueSize;
-                               dwError = pKey->GetNextValue(pchValueName,dwValueNameLength,NULL,NULL,NULL);
-                               if (dwError == ERROR_NO_MORE_ITEMS) break;
-                               if (dwError != ERROR_SUCCESS)
-                               {
-                                       if (pRootMatch) delete pRootMatch;
-                                       if (pTree)
-                                               delete pTree;
-                                       return _T("error");
-                               }
-                               
-                               if (((dwValueNameLength == 0) && (_tcslen(pchBegin) == 0))||
-                                       (_tcsnicmp(pchValueName,pchBegin,_tcslen(pchBegin)) == 0))
-                               {
-                                       nTotalValues++;
-                                       nTotalItems++;
-                                       p = new CompletionMatch((dwValueNameLength == 0)?_T("(Default)"):pchValueName,&pRootMatch);
-                                       if (!p || !p->GetText(0))
-                                       {
-                                               if (pRootMatch) delete pRootMatch;
-                                               if (pTree)
-                                                       delete pTree;
-                                               return _T("Out of memory");
-                                       }
-                               }
-                       }       // for
-                       delete [] pchValueName;
-               }
-       }
-       if (rnIndex >= nTotalItems)
-               rnIndex = nTotalItems-1;
-       if (pblnForward)
-       {
-               if (*pblnForward)
-               {
-                       rnIndex++;
-                       if (rnIndex >= nTotalItems)
-                       {
-                               if (g_blnCompletionCycle)
-                                       rnIndex = 0;
-                               else
-                                       rnIndex--;
-                       }
-               }
-               else
-               {
-                       if (rnIndex)
-                               rnIndex--;
-                       else if (g_blnCompletionCycle)
-                               rnIndex = nTotalItems-1;
-               }
-       }
-       
-       const TCHAR *pchName;
-       if (nTotalItems == 0)
-       {
-               if (pRootMatch)
-                       delete pRootMatch;
-               if (pTree)
-                       delete pTree;
-               return NULL;
-       }
-       ASSERT(rnIndex < nTotalItems);
-       pchName = pRootMatch->GetText(nTotalItems-rnIndex-1);
-       if (pchName == NULL)
-       {
-               if (pRootMatch)
-                       delete pRootMatch;
-               if (pTree)
-                       delete pTree;
-               return _T("internal error");
-       }
-       DWORD dwBufferFull = _tcslen(pchName);
-       if (dwBufferFull >= COMPLETION_BUFFER_SIZE-nBufferOffset)
-       {
-               dwBufferFull = COMPLETION_BUFFER_SIZE-1-nBufferOffset;
-       }
-       _tcsncpy(pchBuffer+nBufferOffset,pchName,dwBufferFull);
-       if ((dwBufferFull < COMPLETION_BUFFER_SIZE-1)&&(rnIndex < nTotalKeys))
-               pchBuffer[nBufferOffset+dwBufferFull++] = _T('\\');
-       pchBuffer[nBufferOffset+dwBufferFull] = 0;
-       if (pTree)
-               delete pTree;
-       if (pRootMatch)
-               delete pRootMatch;
-       return pchBuffer;
-}
-
 BOOL blnCommandExecutionInProgress = FALSE;
 
 BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
@@ -400,8 +114,8 @@ int main ()
        CShellCommandSACL SACLCommand(Tree);
        CommandsList.AddCommand(&SACLCommand);
 
-       CShellCommandDOKA DOKACommand(Tree);
-       CommandsList.AddCommand(&DOKACommand);
+       //CShellCommandDOKA DOKACommand(Tree);
+       //CommandsList.AddCommand(&DOKACommand);
 
        CShellCommandConnect ConnectCommand(Tree);
        CommandsList.AddCommand(&ConnectCommand);
@@ -415,10 +129,45 @@ int main ()
        CShellCommandSetValue SetValueCommand(Tree);
        CommandsList.AddCommand(&SetValueCommand);
 
+       CShellCommandDeleteValue DeleteValueCommand(Tree);
+       CommandsList.AddCommand(&DeleteValueCommand);
+
        CArgumentParser Parser;
 
        int nRetCode = 0;
 
+  HRESULT hr;
+
+  CSettings *pSettings = NULL;
+  CPrompt *pPrompt = NULL;
+
+  pSettings = new CSettings();
+  if (!pSettings)
+       {
+               _ftprintf(stderr,_T("Cannot initialize settings. Out of memory.\n"));
+               goto Abort;
+       }
+
+  hr = pSettings->Load(SETTINGS_REGISTRY_KEY);
+  if (FAILED(hr))
+       {
+               _ftprintf(stderr,_T("Cannot load settings. Error is 0x%X.\n"),(unsigned int)hr);
+               goto Abort;
+       }
+  
+  pPrompt = new CPrompt(Tree,hr);
+  if (!pPrompt)
+       {
+               _ftprintf(stderr,_T("Cannot initialize prompt. Out of memory.\n"));
+               goto Abort;
+       }
+
+  if (FAILED(hr))
+       {
+               _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
+               goto Abort;
+       }
+
 // input buffer size in chars
 #define INPUT_BUFFER_SIZE      1024
 //#define INPUT_BUFFER_SIZE    128
@@ -439,7 +188,7 @@ int main ()
        if (!Console.GetTextAttribute(wOldConsoleAttribute)) goto Abort;
 
        Console.SetTitle(_T("Registry Explorer"));
-       Console.SetTextAttribute(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
+       Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
 
        VERIFY(SetConsoleCtrlHandler((PHANDLER_ROUTINE)HandlerRoutine,TRUE));
 
@@ -447,29 +196,35 @@ int main ()
        //(_L(__TIMESTAMP__))
        )) goto Abort;
 
-       Tree.SetDesiredOpenKeyAccess(KEY_READ);
+  //Tree.SetDesiredOpenKeyAccess(KEY_READ);
 
+  hr = pPrompt->SetPrompt(pSettings->GetPrompt());
+  if (FAILED(hr))
+  {
+    _ftprintf(stderr,_T("Cannot initialize prompt. Error is 0x%X.\n"),(unsigned int)hr);
+    goto Abort;
+  }
+  
 GetCommand:
+       // prompt
+       // TODO: make prompt user-customizable
        Console.EnableWrite();
-       Console.Write(_T("["));
-       _tcscpy(pchCurrentKey,Tree.GetCurrentPath());
-       Console.Write(pchCurrentKey);
-       Console.Write(_T("]"));
+       pPrompt->ShowPrompt(Console);
        Console.FlushInputBuffer();
 
        blnCommandExecutionInProgress = FALSE;
 
        // Set command line color
-//     Console.SetTextAttribute(/*FOREGROUND_BLUE|*/FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
-       if (!Console.ReadLine()) goto Abort;
+  Console.SetTextAttribute(pSettings->GetPromptTextAttributes());
+       if (!Console.ReadLine())
+    goto Abort;
 
        // Set normal color
-//     Console.SetTextAttribute(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
+       Console.SetTextAttribute(pSettings->GetNormalTextAttributes());
 
        Console.BeginScrollingOperation();
        blnCommandExecutionInProgress = TRUE;
 
-
        // Parse command line (1st step - convert to multi sz)
        Parser.SetArgumentList(pchCommand);
 
@@ -505,6 +260,14 @@ GetCommand:
 Abort:
        _ftprintf(stderr,_T("Abnormal program termination.\nPlease report bugs to ") EMAIL _T("\n"));
        nRetCode = 1;
+
 Exit:
+
+  if (pSettings)
+    delete pSettings;
+
+  if (pPrompt)
+    delete pPrompt;
+
        return nRetCode;
 }