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 // ShellCommandNewKey.cpp: implementation of the CShellCommandNewKey class.
23 //
24 //////////////////////////////////////////////////////////////////////
25 
26 #include "ph.h"
27 #include "ShellCommandNewKey.h"
28 #include "RegistryExplorer.h"
29 
30 #define NK_CMD			_T("NK")
31 #define NK_CMD_SHORT_DESC	NK_CMD _T(" command is used to create new key.\n")
32 
33 //////////////////////////////////////////////////////////////////////
34 // Construction/Destruction
35 //////////////////////////////////////////////////////////////////////
36 
37 CShellCommandNewKey::CShellCommandNewKey(CRegistryTree& rTree):m_rTree(rTree)
38 {
39 }
40 
41 CShellCommandNewKey::~CShellCommandNewKey()
42 {
43 }
44 
45 BOOL CShellCommandNewKey::Match(const TCHAR *pchCommand)
46 {
47 	return _tcsicmp(pchCommand,NK_CMD) == 0;
48 }
49 
50 int CShellCommandNewKey::Execute(CConsole &rConsole, CArgumentParser& rArguments)
51 {
52 	TCHAR *pszNewKey = NULL, *pszArg;
53 
54 	BOOL blnHelp = FALSE;
55 	BOOL blnExitAfterHelp = FALSE;
56 	BOOL blnVolatile = FALSE;
57 
58 	while((pszArg = rArguments.GetNextArgument()) != NULL)
59 	{
60 		if ((_tcsicmp(pszArg,_T("/?")) == 0)
61 			||(_tcsicmp(pszArg,_T("-?")) == 0))
62 		{
63 			blnHelp = TRUE;
64 		}
65 		else if ((_tcsicmp(pszArg,_T("/v")) == 0)
66 			||(_tcsicmp(pszArg,_T("-v")) == 0))
67 		{
68 			blnVolatile = TRUE;
69 		}
70 		else
71 		{
72 			if (pszNewKey)
73 			{
74 				rConsole.Write(_T("Wrong parameter : \""));
75 				rConsole.Write(pszArg);
76 				rConsole.Write(_T("\"\n\n"));
77 				blnHelp = TRUE;
78 			}
79 			else
80 			{
81 				pszNewKey = pszArg;
82 			}
83 		}
84 	}
85 
86 	if (!pszNewKey)
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 = pszNewKey;
103   while(*pch) // search end of string
104     pch++;
105 
106   if (pch > pszNewKey) // last non-null char
107     pch--;
108 
109   while(*pch == _T('\\')) // ignore ending backslashes
110     *pch = 0;
111 
112   while((pch > pszNewKey)&&(*pch != _T('\\')))
113     pch--;
114 
115   ASSERT(pch >= pszNewKey);
116 
117   const TCHAR *pszPath;
118   TCHAR *pszSubkeyName = pch;
119   if (*pch == _T('\\'))
120     pszSubkeyName++;
121 
122   if (pch == pszNewKey)
123   {
124     pszPath = _T(".");
125   }
126   else
127   {
128     if (pch-1 == pszNewKey)
129     {
130       rConsole.Write(NK_CMD COMMAND_NA_ON_ROOT);
131       return 0;
132     }
133     else
134     {
135       *pch = 0;
136       pszPath = pszNewKey;
137     }
138   }
139 
140   {
141     size_t s = _tcslen(pszSubkeyName);
142     if (s && (pszSubkeyName[0] == _T('\"')) && (pszSubkeyName[s-1] == _T('\"')))
143     {
144       pszSubkeyName[s-1] = 0;
145       pszSubkeyName++;
146     }
147   }
148 
149 	if (!m_rTree.NewKey(pszSubkeyName,pszPath,blnVolatile))
150 	{
151 		rConsole.Write(_T("Cannot create key.\n"));
152 		rConsole.Write(m_rTree.GetLastErrorDescription());
153 	}
154   else
155   {
156     InvalidateCompletion();
157   }
158 
159 	return 0;
160 }
161 
162 const TCHAR * CShellCommandNewKey::GetHelpString()
163 {
164 	return NK_CMD_SHORT_DESC
165 			_T("Syntax: ") NK_CMD _T(" [/v] [/?] [PATH]KEY_NAME\n\n")
166       _T("    PATH     - optional path to key which subkey will be created. Default is current key.\n")
167       _T("    KEY_NAME - name of subkey to be created.\n")
168 			_T("    /? - This help.\n")
169 			_T("    /v - Create volatile key. The information is stored in memory and is not\n")
170 			_T("         preserved when the corresponding registry hive is unloaded.\n");
171 }
172 
173 const TCHAR * CShellCommandNewKey::GetHelpShortDescriptionString()
174 {
175 	return NK_CMD_SHORT_DESC;
176 }
177