1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * regexpl - Console Registry Explorer
 *
 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
 *
 * 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.
 */

// ShellCommandDir.cpp: implementation of the CShellCommandDir class.
//
//////////////////////////////////////////////////////////////////////

#include "ph.h"
#include "RegistryExplorer.h"
#include "ShellCommandDir.h"
#include "RegistryTree.h"
#include "RegistryKey.h"
#include "Pattern.h"

// *** THIS SHOULD GO IN A MINGW/ROS HEADER (tchar.h ???) - Begin
#if 1	// #ifndef _ui64tot ???
	#ifdef  _UNICODE
			#define _ui64tot    _ui64tow
	#else
			#define _ui64tot    _ui64toa
	#endif
#endif
// *** THIS SHOULD GO IN A MINGW/ROS HEADER - End

#define DIR_CMD				_T("DIR")
#define DIR_CMD_LENGTH		COMMAND_LENGTH(DIR_CMD)
#define DIR_CMD_SHORT_DESC	DIR_CMD _T(" command lists keys and values of any key.\n")

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShellCommandDir::CShellCommandDir(CRegistryTree& rTree):m_rTree(rTree)
{
}

CShellCommandDir::~CShellCommandDir()
{
}

BOOL CShellCommandDir::Match(const TCHAR *pchCommand)
{
	if (_tcsicmp(pchCommand,DIR_CMD) == 0)
		return TRUE;
	if (_tcsnicmp(pchCommand,DIR_CMD _T(".."),DIR_CMD_LENGTH+2*sizeof(TCHAR)) == 0)
		return TRUE;
	if (_tcsnicmp(pchCommand,DIR_CMD _T("/"),DIR_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
		return TRUE;
	if (_tcsnicmp(pchCommand,DIR_CMD _T("\\"),DIR_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
		return TRUE;
	return FALSE;
}

int CShellCommandDir::Execute(CConsole &rConsole, CArgumentParser& rArguments)
{
	rArguments.ResetArgumentIteration();

	BOOL blnDo = TRUE,blnBadParameter, blnHelp = FALSE;
	TCHAR *pszParameter;
	TCHAR *pszCommandItself = rArguments.GetNextArgument();
  TCHAR *pszKey = NULL;

	if ((_tcsnicmp(pszCommandItself,DIR_CMD _T(".."),DIR_CMD_LENGTH+2*sizeof(TCHAR)) == 0)||
		(_tcsnicmp(pszCommandItself,DIR_CMD _T("\\"),DIR_CMD_LENGTH+1*sizeof(TCHAR)) == 0))
	{
		pszKey = pszCommandItself + DIR_CMD_LENGTH;
	}
	else if (_tcsnicmp(pszCommandItself,DIR_CMD _T("/"),DIR_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
	{
		pszParameter = pszCommandItself + DIR_CMD_LENGTH;
		goto CheckDirArgument;
	}

	while((pszParameter = rArguments.GetNextArgument()) != NULL)
	{
CheckDirArgument:
		blnBadParameter = FALSE;
		if ((_tcsicmp(pszParameter,_T("/?")) == 0)
			||(_tcsicmp(pszParameter,_T("-?")) == 0))
		{
			blnHelp = TRUE;
			blnDo = pszKey != NULL;
		}
		else if (!pszKey)
		{
			pszKey = pszParameter;
			blnDo = TRUE;
		}
		else
		{
			blnBadParameter = TRUE;
		}
		if (blnBadParameter)
		{
			rConsole.Write(_T("Bad parameter: "));
			rConsole.Write(pszParameter);
			rConsole.Write(_T("\n"));
		}
	}

  const TCHAR *pszPattern = PATTERN_MATCH_ALL;
  const TCHAR *pszPath = _T(".");

  if (pszKey)
  {
    pszPath = pszKey;

    TCHAR *pch = pszKey;
    while(*pch) // search end of string
      pch++;

    if (pch > pszKey) // last non-null char
      pch--;

    if (*pch != _T('\\'))
    {
      while ((pch > pszKey) && (*pch != _T('\\')))
        pch--;

      if (*pch == _T('\\'))
      {
        pszPattern = pch+1;

        if (pch > pszKey)
        {
          ASSERT(*pch == _T('\\'));
          *pch = 0;
        }
        else if (*pch == _T('\\'))
        {
          pszPath = _T("\\");
        }
      }
      else
      {
        pszPattern = pszKey;
        pszPath = _T(".");
      }
    }
  }

	CRegistryKey Key;

  if (!m_rTree.GetKey(pszPath,KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE,Key))
  {
    const TCHAR *pszErrorMsg = m_rTree.GetLastErrorDescription();
    rConsole.Write(pszErrorMsg);
    blnDo = FALSE;
  }

	if (blnHelp)
	{
		rConsole.Write(GetHelpString());
	}

	LONG nError;

	if (!blnDo)
    return 0;

  rConsole.Write(_T("\n Key is "));
  rConsole.Write(Key.GetKeyName());

  if (!Key.IsRoot())
  {
    rConsole.Write(_T("\n Last modify time is "));
    rConsole.Write(Key.GetLastWriteTime());
  }

  rConsole.Write(_T("\n\n"));
  unsigned __int64 nTotalItems = 0;

  try
  {
    ASSERT(nTotalItems == 0);
    rConsole.Write(_T("\t(KEY)\t\t\t\t..\\\n"));	// parent key abstraction
    nTotalItems = 1;

    DWORD dwMaxSubkeyNameLength;
    nError = Key.GetSubkeyNameMaxLength(dwMaxSubkeyNameLength);
    if (nError != ERROR_SUCCESS)
      throw nError;

    TCHAR *pszSubkeyNameBuffer = new (std::nothrow) TCHAR[dwMaxSubkeyNameLength];
    if (!pszSubkeyNameBuffer)
      throw ERROR_OUTOFMEMORY;

    Key.InitSubkeyEnumeration(pszSubkeyNameBuffer,dwMaxSubkeyNameLength);
    while ((nError = Key.GetNextSubkeyName()) == ERROR_SUCCESS)
    {
      if (PatternMatch(pszPattern,pszSubkeyNameBuffer))
      {
        rConsole.Write(_T("\t(KEY)\t\t\t\t"));
        rConsole.Write(pszSubkeyNameBuffer);
        rConsole.Write(_T("\\\n"));
        nTotalItems++;
      }
    }

    delete[] pszSubkeyNameBuffer;

    if (nError != ERROR_NO_MORE_ITEMS)
      throw nError;

    DWORD dwMaxValueNameBufferSize;
    nError = Key.GetMaxValueNameLength(dwMaxValueNameBufferSize);
    if (nError != ERROR_SUCCESS)
      throw nError;

    TCHAR *pchValueNameBuffer = new (std::nothrow) TCHAR[dwMaxValueNameBufferSize];
    if (!pchValueNameBuffer)
      throw ERROR_OUTOFMEMORY;


    DWORD Type;
    Key.InitValueEnumeration(pchValueNameBuffer,
                             dwMaxValueNameBufferSize,
                             NULL,
                             0,
                             &Type);

    DWORD dwValueNameActualLength;
    const TCHAR *pszValueTypeName;
    unsigned int nTabSize = rConsole.GetTabWidth();
    unsigned int nTabs;
    while((nError = Key.GetNextValue(&dwValueNameActualLength)) == ERROR_SUCCESS)
    {
      if (PatternMatch(pszPattern,pchValueNameBuffer))
      {
        rConsole.Write(_T("\t"));
        pszValueTypeName = CRegistryKey::GetValueTypeName(Type);
        nTabs = _tcslen(pszValueTypeName)/nTabSize;
        nTabs = (nTabs < 4)?(4-nTabs):1;
        rConsole.Write(pszValueTypeName);
        while(nTabs--)
          rConsole.Write(_T("\t"));
        rConsole.Write((dwValueNameActualLength == 0)?_T("(Default)"):pchValueNameBuffer);
        rConsole.Write(_T("\n"));
        nTotalItems++;
      }
    }

    delete[] pchValueNameBuffer;

    if (nError != ERROR_NO_MORE_ITEMS)
      throw nError;

  }	// try
  catch (LONG nError)
  {
    rConsole.Write(_T("Error "));
    TCHAR Buffer[256];
    rConsole.Write(_itoa(nError,Buffer,10));
    rConsole.Write(_T("\n"));
  }

  rConsole.Write(_T("\n Total: "));
  TCHAR Buffer[256];
  rConsole.Write(_ui64tot(nTotalItems,Buffer,10));
  rConsole.Write(_T(" item(s) listed.\n"));

	return 0;
}

const TCHAR * CShellCommandDir::GetHelpString()
{
	return DIR_CMD_SHORT_DESC
			_T("Syntax: ") DIR_CMD _T(" [<PATH>\\][<PATTERN>] [/?]\n\n")
			_T("    <PATH> - Optional relative path to the key on which command will be executed\n")
      _T("    <PATTERN> - Optional pattern. Default is the all matching pattern.")
			_T("    /?    - This help.\n\n")
			_T("Without parameters, command lists keys and values of current key.\n");
}

const TCHAR * CShellCommandDir::GetHelpShortDescriptionString()
{
	return DIR_CMD_SHORT_DESC;
}