1 /*
2  * regexpl - Console Registry Explorer
3  *
4  * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 // ShellCommandDeleteKey.cpp: implementation of the CShellCommandDeleteKey class.
23 //
24 //////////////////////////////////////////////////////////////////////
25 
26 #include "ph.h"
27 #include "ShellCommandDeleteKey.h"
28 #include "RegistryExplorer.h"
29 
30 #define DK_CMD			_T("DK")
31 #define DK_CMD_SHORT_DESC	DK_CMD _T(" command is used to delete key(s).\n")
32 
33 //////////////////////////////////////////////////////////////////////
34 // Construction/Destruction
35 //////////////////////////////////////////////////////////////////////
36 
37 CShellCommandDeleteKey::CShellCommandDeleteKey(CRegistryTree& rTree):m_rTree(rTree)
38 {
39 }
40 
41 CShellCommandDeleteKey::~CShellCommandDeleteKey()
42 {
43 }
44 
45 BOOL CShellCommandDeleteKey::Match(const TCHAR *pchCommand)
46 {
47 	return _tcsicmp(pchCommand,DK_CMD) == 0;
48 }
49 
50 int CShellCommandDeleteKey::Execute(CConsole &rConsole, CArgumentParser& rArguments)
51 {
52 	TCHAR *pchKey = NULL, *pchArg;
53 
54 	BOOL blnHelp = FALSE;
55 	BOOL blnExitAfterHelp = FALSE;
56 	BOOL blnRecursive = FALSE;
57 
58 	while((pchArg = rArguments.GetNextArgument()) != NULL)
59 	{
60 		if ((_tcsicmp(pchArg,_T("/?")) == 0)
61 			||(_tcsicmp(pchArg,_T("-?")) == 0))
62 		{
63 			blnHelp = TRUE;
64 		}
65 		else if ((_tcsicmp(pchArg,_T("/s")) == 0)
66 			||(_tcsicmp(pchArg,_T("-s")) == 0))
67 		{
68 			blnRecursive = TRUE;
69 		}
70 		else
71 		{
72 			if (pchKey)
73 			{
74 				rConsole.Write(_T("Wrong parameter : \""));
75 				rConsole.Write(pchArg);
76 				rConsole.Write(_T("\"\n\n"));
77 				blnHelp = TRUE;
78 			}
79 			else
80 			{
81 				pchKey = pchArg;
82 			}
83 		}
84 	}
85 
86 	if ((!blnHelp) && (!pchKey))
87 	{
88 		rConsole.Write(_T("Key name not specified !\n\n"));
89 		blnExitAfterHelp = TRUE;
90 	}
91 
92 	if (blnHelp)
93 	{
94 		rConsole.Write(GetHelpString());
95 		if (blnExitAfterHelp)
96 			return 0;
97 		else
98 		rConsole.Write(_T("\n"));
99 	}
100 
101   // search for last key name token
102   TCHAR *pch = pchKey;
103   while(*pch)
104     pch++;
105 
106   if (pch > pchKey)
107     pch--;
108 
109   while(*pch == _T('\\'))
110     *pch = 0;
111 
112   while((pch > pchKey)&&(*pch != _T('\\')))
113     pch--;
114 
115   ASSERT(pch >= pchKey);
116 
117   const TCHAR *pszPath;
118   TCHAR *pszPattern = pch;
119   if (*pch == _T('\\'))
120     pszPattern++;
121 
122   if (pch == pchKey)
123   {
124     pszPath = _T(".");
125   }
126   else
127   {
128     if (pch-1 == pchKey)
129     {
130       rConsole.Write(DK_CMD COMMAND_NA_ON_ROOT);
131       return 0;
132     }
133     else
134     {
135       *pch = 0;
136       pszPath = pchKey;
137     }
138   }
139 
140   {
141     size_t s = _tcslen(pszPattern);
142     if (s && (pszPattern[0] == _T('\"'))&&(pszPattern[s-1] == _T('\"')))
143     {
144       pszPattern[s-1] = 0;
145       pszPattern++;
146     }
147   }
148 
149 	if (!m_rTree.DeleteSubkeys(pszPattern,pszPath,blnRecursive))
150 	{
151 		rConsole.Write(_T("Cannot delete key(s).\n"));
152 		rConsole.Write(m_rTree.GetLastErrorDescription());
153 	}
154   else
155   {
156     InvalidateCompletion();
157   }
158 
159 	return 0;
160 }
161 
162 const TCHAR * CShellCommandDeleteKey::GetHelpString()
163 {
164 	return DK_CMD_SHORT_DESC
165 			_T("Syntax: ") DK_CMD _T(" [/s] [/?] [PATH]KEY_NAME\n\n")
166       _T("    PATH     - optional path to key which subkey(s) will be deleted. Default is current key.")
167       _T("    KEY_NAME - name of key to be deleted. Wildcards can be used.")
168 			_T("    /?       - This help.\n\n")
169 			_T("    /s       - Delete key and all subkeys.\n");
170 }
171 
172 const TCHAR * CShellCommandDeleteKey::GetHelpShortDescriptionString()
173 {
174 	return DK_CMD_SHORT_DESC;
175 }
176