Ejemplo n.º 1
0
int CShellCommandSetValue::Execute(CConsole &rConsole, CArgumentParser& rArguments)
{
  LONG nError;

	rArguments.ResetArgumentIteration();
	TCHAR *pszCommandItself = rArguments.GetNextArgument();

	TCHAR *pszParameter;
	TCHAR *pszValueFull = NULL;
	TCHAR *pszValueData = NULL;
	BOOL blnBadParameter = FALSE;
	BOOL blnHelp = FALSE;
	DWORD dwValueSize = 0;
	DWORD dwType = REG_NONE;
	BYTE *pDataBuffer = NULL;

	if ((_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T(".."),SET_VALUE_CMD_LENGTH+2*sizeof(TCHAR)) == 0)||
		(_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T("\\"),SET_VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0))
	{
		pszValueFull = pszCommandItself + SET_VALUE_CMD_LENGTH;
	}
	else if (_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T("/"),SET_VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
	{
		pszParameter = pszCommandItself + SET_VALUE_CMD_LENGTH;
		goto CheckValueArgument;
	}

	while((pszParameter = rArguments.GetNextArgument()) != NULL)
	{
CheckValueArgument:
		blnBadParameter = FALSE;
		if (((*pszParameter == _T('/'))||(*pszParameter == _T('-')))
			&&(*(pszParameter+1) == _T('?')))
		{
			blnHelp = TRUE;
		}
		else if (dwType == REG_NONE)
		{
			if (_tcsicmp(pszParameter,_T("b")) == 0)
			{
				dwType = REG_BINARY;
			}
			else if (_tcsicmp(pszParameter,_T("dw")) == 0)
			{
				dwType = REG_DWORD;
			}
			else if (_tcsicmp(pszParameter,_T("dwle")) == 0)
			{
				dwType = REG_DWORD_LITTLE_ENDIAN;
			}
			else if (_tcsicmp(pszParameter,_T("dwbe")) == 0)
			{
				dwType = REG_DWORD_BIG_ENDIAN;
			}
			else if (_tcsicmp(pszParameter,_T("sz")) == 0)
			{
				dwType = REG_SZ;
			}
			else if (_tcsicmp(pszParameter,_T("esz")) == 0)
			{
				dwType = REG_EXPAND_SZ;
			}
			else
			{
				blnBadParameter = TRUE;
			}
		}
		else if (pszValueData == NULL)
		{
			pszValueData = pszParameter;
		}
		else if (!pszValueFull)
		{
			pszValueFull = pszParameter;
		}
		else
		{
			blnBadParameter = TRUE;
		}
		if (blnBadParameter)
		{
			rConsole.Write(_T("Bad parameter: "));
			rConsole.Write(pszParameter);
			rConsole.Write(_T("\n"));
		}
	}

	if (!pszValueData)
		blnHelp = TRUE;

	CRegistryKey Key;
	TCHAR *pszValueName;
	const TCHAR *pszEmpty = _T("");
	const TCHAR *pszPath;

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

		if (pDataBuffer)
			delete pDataBuffer;

		return 0;
	}

	if (pszValueFull)
	{
		if (_tcscmp(pszValueFull,_T("\\")) == 0)
			goto CommandNAonRoot;

		TCHAR *pchSep = _tcsrchr(pszValueFull,_T('\\'));
		pszValueName = pchSep?(pchSep+1):(pszValueFull);
		pszPath = pchSep?pszValueFull:_T(".");

		//if (_tcsrchr(pszValueName,_T('.')))
		//{
		//	pszValueName = _T("");
		//	pszPath = pszValueFull;
		//}
		//else
		if (pchSep)
			*pchSep = 0;
	}
	else
	{
		pszValueName = (TCHAR*)pszEmpty;
		pszPath = _T(".");
	}

  if (!m_rTree.GetKey(pszPath,KEY_SET_VALUE,Key))
  {
    rConsole.Write(m_rTree.GetLastErrorDescription());
    goto SkipCommand;
  }

	if (Key.IsRoot())
    goto CommandNAonRoot;

  switch (dwType)
  {
  case REG_BINARY:
    {
      HANDLE hFile;
      DWORD dwBytesReaded;
      hFile = CreateFile(pszValueData,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
      if (hFile == INVALID_HANDLE_VALUE)
      {
        rConsole.Write(_T("Cannot open file "));
        rConsole.Write(pszValueData);
        rConsole.Write(_T("\n"));
        goto SkipCommand;
      }
      dwValueSize = GetFileSize(hFile,NULL);
      if (dwValueSize == (DWORD)-1)	// ok, that's right, we compare signed with unsigned here.
        // GetFileSize is documented and declared to return DWORD.
        // Error is indicated by checking if return is -1. Design->documentation bug ???
      {
        rConsole.Write(_T("Cannot get size of file "));
        rConsole.Write(pszValueData);
        rConsole.Write(_T("\n"));
        VERIFY(CloseHandle(hFile));
        goto SkipCommand;
      }
      pDataBuffer = new BYTE [dwValueSize];
      if (!pDataBuffer)
      {
        rConsole.Write(_T("Cannot load file into memory. Out of memory.\n"));
        VERIFY(CloseHandle(hFile));
        goto SkipCommand;
      }
      if (!ReadFile(hFile,pDataBuffer,dwValueSize,&dwBytesReaded,NULL))
      {
        rConsole.Write(_T("Cannot load file into memory. Error reading file.\n"));
        VERIFY(CloseHandle(hFile));
        goto SkipCommand;
      }

      VERIFY(CloseHandle(hFile));
      ASSERT(dwBytesReaded == dwValueSize);
    }
    break;
  case REG_DWORD_LITTLE_ENDIAN:
  case REG_DWORD_BIG_ENDIAN:
    dwValueSize = 4;
    pDataBuffer = (BYTE *) new BYTE [dwValueSize];
    if (!StringToDWORD(*(DWORD *)pDataBuffer,pszValueData))
    {
      rConsole.Write(_T("Cannot convert "));
      rConsole.Write(pszValueData);
      rConsole.Write(_T(" to DWORD \n"));
      goto SkipCommand;
    }
    if (dwType == REG_DWORD_BIG_ENDIAN)
    {
      unsigned char nByte;
      nByte = *pDataBuffer;
      *pDataBuffer = *(pDataBuffer+3);
      *(pDataBuffer+3) = nByte;
      nByte = *(pDataBuffer+1);
      *(pDataBuffer+1) = *(pDataBuffer+2);
      *(pDataBuffer+2) = nByte;
    }
    break;
  case REG_SZ:
  case REG_EXPAND_SZ:
    dwValueSize = _tcslen(pszValueData)+1;
    if (*pszValueData == _T('\"'))
    {
      dwValueSize -= 2;
      *(pszValueData+dwValueSize) = 0;
      pszValueData++;
    }
    dwValueSize *= sizeof(TCHAR);
    pDataBuffer = (BYTE *) new BYTE [dwValueSize];

    {
      const TCHAR *pchSrc = pszValueData;
      TCHAR *pchDest = (TCHAR *)pDataBuffer;
      while(*pchSrc)
      {
        if (pchSrc[0] == _T('^'))
        {
          if (pchSrc[1] == _T('a'))
            *pchDest = _T('\a');
          else if (pchSrc[1] == _T('b'))
            *pchDest = _T('\b');
          else if (pchSrc[1] == _T('f'))
            *pchDest = _T('\f');
          else if (pchSrc[1] == _T('n'))
            *pchDest = _T('\n');
          else if (pchSrc[1] == _T('r'))
            *pchDest = _T('\r');
          else if (pchSrc[1] == _T('t'))
            *pchDest = _T('\t');
          else
            *pchDest = pchSrc[1];

          pchSrc +=2;
          pchDest++;
          dwValueSize--;
        }
        else
        {
          *pchDest = *pchSrc;
          pchSrc++;
          pchDest++;
        }
      }
      *pchDest = _T('\0');
    }
    break;
  default:
    ASSERT(FALSE);
  }

  {
    size_t s = _tcslen(pszValueName);
    if (s && (pszValueName[0] == _T('\"'))&&(pszValueName[s-1] == _T('\"')))
    {
      pszValueName[s-1] = 0;
      pszValueName++;
    }
  }

  nError = Key.SetValue(pszValueName,dwType,pDataBuffer,dwValueSize);
  if (nError != ERROR_SUCCESS)
  {
    char Buffer[254];
    _stprintf(Buffer,_T("Cannot set value. Error is %u\n"),(unsigned int)nError);
    rConsole.Write(Buffer);
  }
  else
  {
    InvalidateCompletion();
  }

SkipCommand:
	if (pDataBuffer)
		delete[] pDataBuffer;
	return 0;

CommandNAonRoot:
  rConsole.Write(SET_VALUE_CMD COMMAND_NA_ON_ROOT);
  return 0;
}
Ejemplo n.º 2
0
int CShellCommandDeleteValue::Execute(CConsole &rConsole, CArgumentParser& rArguments)
{
  rArguments.ResetArgumentIteration();
  TCHAR *pszCommandItself = rArguments.GetNextArgument();

  TCHAR *pszParameter;
  TCHAR *pszValueFull = NULL;
  BOOL blnHelp = FALSE;

  if ((_tcsnicmp(pszCommandItself,DV_CMD _T(".."),DV_CMD_LENGTH+2*sizeof(TCHAR)) == 0)||
      (_tcsnicmp(pszCommandItself,DV_CMD _T("\\"),DV_CMD_LENGTH+1*sizeof(TCHAR)) == 0))
  {
    pszValueFull = pszCommandItself + DV_CMD_LENGTH;
  }
  else if (_tcsnicmp(pszCommandItself,DV_CMD _T("/"),DV_CMD_LENGTH+1*sizeof(TCHAR)) == 0)
  {
    pszParameter = pszCommandItself + DV_CMD_LENGTH;
    goto CheckValueArgument;
  }

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

  CRegistryKey Key;
  TCHAR *pszValueNamePattern;
  const TCHAR *pszEmpty = _T("");
  const TCHAR *pszPath;

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

  if (pszValueFull)
  {
    if (_tcscmp(pszValueFull,_T("\\")) == 0)
      goto CommandNAonRoot;

    TCHAR *pchSep = _tcsrchr(pszValueFull,_T('\\'));
    pszValueNamePattern = pchSep?(pchSep+1):(pszValueFull);
    pszPath = pchSep?pszValueFull:_T(".");

    if (pchSep)
      *pchSep = 0;
  }
  else
  {
    pszValueNamePattern = (TCHAR*)pszEmpty;
    pszPath = _T(".");
  }

  {
    size_t s = _tcslen(pszValueNamePattern);
    if (s && (pszValueNamePattern[0] == _T('\"'))&&(pszValueNamePattern[s-1] == _T('\"')))
    {
      pszValueNamePattern[s-1] = 0;
      pszValueNamePattern++;
    }
  }

  if (!m_rTree.GetKey(pszPath,KEY_QUERY_VALUE|KEY_SET_VALUE,Key))
  {
    rConsole.Write(m_rTree.GetLastErrorDescription());
    return 0;
  }

  if (!Key.IsRoot())
  {	// not root key ???
    TCHAR Buffer[254];
    DWORD dwMaxValueNameLength;
    LONG nError = Key.GetMaxValueNameLength(dwMaxValueNameLength);
    if (nError != ERROR_SUCCESS)
    {
      _stprintf(Buffer,_T("Cannot query info about %s key. Error is %u\n"),Key.GetKeyName(),(unsigned int)nError);
      rConsole.Write(Buffer);
      return 0;
    }

    TCHAR *pszValueName = new (std::nothrow) TCHAR[dwMaxValueNameLength];
    if (!pszValueName)
    {
      rConsole.Write("Out of memory.");
      return 0;
    }

    Key.InitValueEnumeration(pszValueName,dwMaxValueNameLength,NULL,0,NULL);

    while ((nError = Key.GetNextValue()) == ERROR_SUCCESS)
    {
      if (PatternMatch(pszValueNamePattern,pszValueName))
      {
        nError = Key.DeleteValue(pszValueName);
        if (nError != ERROR_SUCCESS)
        {
          _stprintf(Buffer,_T("Cannot delete value. Error is %u\n"),(unsigned int)nError);
          rConsole.Write(Buffer);
        }
        else
        {
          InvalidateCompletion();
        }
        Key.InitValueEnumeration(pszValueName,dwMaxValueNameLength,NULL,0,NULL); // reset iteration
      }
    }
  } // if (pKey)
  else
  {
CommandNAonRoot:
    rConsole.Write(DV_CMD COMMAND_NA_ON_ROOT);
  }

  return 0;
}
Ejemplo n.º 3
0
int CShellCommandDeleteKey::Execute(CConsole &rConsole, CArgumentParser& rArguments)
{
	TCHAR *pchKey = NULL, *pchArg;

	BOOL blnHelp = FALSE;
	BOOL blnExitAfterHelp = FALSE;
	BOOL blnRecursive = FALSE;

	while((pchArg = rArguments.GetNextArgument()) != NULL)
	{
		if ((_tcsicmp(pchArg,_T("/?")) == 0)
			||(_tcsicmp(pchArg,_T("-?")) == 0))
		{
			blnHelp = TRUE;
		}
		else if ((_tcsicmp(pchArg,_T("/s")) == 0)
			||(_tcsicmp(pchArg,_T("-s")) == 0))
		{
			blnRecursive = TRUE;
		}
		else
		{
			if (pchKey)
			{
				rConsole.Write(_T("Wrong parameter : \""));
				rConsole.Write(pchArg);
				rConsole.Write(_T("\"\n\n"));
				blnHelp = TRUE;
			}
			else
			{
				pchKey = pchArg;
			}
		}
	}

	if ((!blnHelp) && (!pchKey))
	{
		rConsole.Write(_T("Key name not specified !\n\n"));
		blnExitAfterHelp = TRUE;
	}

	if (blnHelp)
	{
		rConsole.Write(GetHelpString());
		if (blnExitAfterHelp)
			return 0;
		else
		rConsole.Write(_T("\n"));
	}

  // search for last key name token
  TCHAR *pch = pchKey;
  while(*pch)
    pch++;

  if (pch > pchKey)
    pch--;

  while(*pch == _T('\\'))
    *pch = 0;

  while((pch > pchKey)&&(*pch != _T('\\')))
    pch--;

  ASSERT(pch >= pchKey);

  const TCHAR *pszPath;
  TCHAR *pszPattern = pch;
  if (*pch == _T('\\'))
    pszPattern++;

  if (pch == pchKey)
  {
    pszPath = _T(".");
  }
  else
  {
    if (pch-1 == pchKey)
    {
      rConsole.Write(DK_CMD COMMAND_NA_ON_ROOT);
      return 0;
    }
    else
    {
      *pch = 0;
      pszPath = pchKey;
    }
  }

  {
    size_t s = _tcslen(pszPattern);
    if (s && (pszPattern[0] == _T('\"'))&&(pszPattern[s-1] == _T('\"')))
    {
      pszPattern[s-1] = 0;
      pszPattern++;
    }
  }

	if (!m_rTree.DeleteSubkeys(pszPattern,pszPath,blnRecursive))
	{
		rConsole.Write(_T("Cannot delete key(s).\n"));
		rConsole.Write(m_rTree.GetLastErrorDescription());
	}
  else
  {
    InvalidateCompletion();
  }

	return 0;
}