Ejemplo n.º 1
0
BOOL CommandManager::Execute( LPCTSTR cmdline, tstring& replyText )
{
    //分割并整理命令行的各个部分
    TStringVector parts;
    splitByChar(cmdline, parts, ' ');

    TStringVector::iterator iter = parts.begin();
    while (iter != parts.end())
    {
        tstring& part = *iter;
        trim(part);
        if (part.size() == 0)
        {
            iter = parts.erase(iter);
        }
        else
        {
            iter++;
        }
    }

    //检查是否有可用的部分
    if (parts.size() == 0)
    {
        replyText = _T("invalid command.");
        return FALSE;
    }

    //查找可用的命令
    tstring& cmdname = parts[0];
    makeLower(cmdname);

    CommandMap::iterator cmdIter = m_cmdMap.find(cmdname);
    if (cmdIter == m_cmdMap.end())
    {
        replyText = _T("no such command.");
        return FALSE;
    }

    //执行命令
    ICmd* pCmd = cmdIter->second;
    BOOL bExecuteOK = pCmd->Execute(parts, replyText, m_env);

    return bExecuteOK;
}
Ejemplo n.º 2
0
BOOL Shell::ExecuteCommand( LPCTSTR cmdlinestr, tstring& reply )
{
	//分割并整理命令行的各个部分
	tstring cmdline = cmdlinestr;
	trim(cmdline, ' ');
	trim(cmdline, '\n');
	trim(cmdline, '\r');
	TStringVector parts;
	splitByChar(cmdline.c_str(), parts, ' ');

	//清理无效参数
	TStringVector::iterator iter = parts.begin();
	while (iter != parts.end())
	{
		tstring& part = *iter;
		trim(part);
		if (part.size() == 0)
		{
			iter = parts.erase(iter);
		}
		else
		{
			iter++;
		}
	}

	//检查是否有可用的部分
	if (parts.size() == 0)
	{
		reply = _T("");
		return TRUE;
	}

	//查找可用的命令
	tstring& cmdname = parts[0];
	makeLower(cmdname);

	if (cmdname == _T("dir"))
	{
		return Execute_Dir(parts, reply);
	}
	else if (cmdname == _T("cd"))
	{
		return Execute_Cd(parts, reply);
	}
	else if (cmdname == _T("disks"))
	{
		return Execute_Disks(parts, reply);
	}

	//将命令发到客户端去执行
	CommData commandCommData;
	commandCommData.SetMsgID(MSGID_EXECUTE_CMDLINE);
	commandCommData.SetData(_T("cmdline"), cmdline.c_str());
	CommData replyCommData;
	if (! AskAndWaitForReply(commandCommData, replyCommData))
	{
		reply = _T("等待客户端回应超时");
		return FALSE;
	}
	else
	{
		tstring result;
		replyCommData.GetStrData(_T("result"), result);
		if (result == _T("OK"))
		{
			reply = s2ws(std::string((LPCSTR)(LPBYTE)replyCommData.GetByteData(), replyCommData.GetByteData().Size()));
			return TRUE;
		}
		else
		{
			reply = result;
			return FALSE;
		}
	}
}