⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.cpp

📁 小型的操作系统开发的原代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//***********************************************************************/
//    Author                    : Garry
//    Original Date             : May,27 2004
//    Module Name               : shell.cpp
//    Module Funciton           : 
//                                This module countains shell procedures.
//    Last modified Author      :
//    Last modified Date        :
//    Last modified Content     :
//                                1.
//                                2.
//    Lines number              :
//***********************************************************************/
#ifndef __STDAFX_H__
#include "..\INCLUDE\STDAFX.H"
#endif

#ifndef __KAPI_H__
#include "..\INCLUDE\KAPI.H"
#endif

#ifndef __IOCTRL_S_H__
#include "..\INCLUDE\IOCTRL_S.H"
#endif

#ifndef __SYSD_S_H__
#include "..\INCLUDE\SYSD_S.H"
#endif

#ifndef __RT8139_H__
#include "..\INCLUDE\RT8139.H"
#endif

#ifndef __EXTCMD_H__
#include "..\EXTCMD.H"
#endif

//
//Global variables.
//
#define MAX_HOSTNAME_LEN  16
#define VERSION_INFO "Hello China [Version 1.500]"

BYTE               HostName[MAX_HOSTNAME_LEN] = {0};          //Host name.
__TASK_CTRL_BLOCK  tcbShell = {0,0,0,0,0,
                   0,0,0,0,0,0,0,0,0,0,0,
				   0,0,0,0,0,0,0,0,0,0,0,
				   0,0,0,0,0,0,0,0,0,0,0,
				   0,0,0,0,0,0,0,0,0,0xffffffff};        //The shell's task control block.
				   

__KTHREAD_CONTROL_BLOCK* g_pShellCtrlBlock   = NULL;     //The shell's kernal thread control
                                                         //block pointer.

__KERNEL_THREAD_OBJECT*  g_lpShellThread     = NULL;     //The system shell's kernel thread 
                                                         //object.

static BYTE        CmdBuffer[MAX_BUFFER_LEN] = {0};  //Command buffer.
static WORD        BufferPtr = 0;         //Buffer pointer,points to the first
                                          //free byte of the CmdBuffer.

//
//Global functions.
//

//
//The following function form the command parameter object link from the command
//line string.
//

__CMD_PARA_OBJ* FormParameterObj(LPSTR pszCmd)
{
	__CMD_PARA_OBJ*     pObjBuffer = NULL;    //Local variables.
	__CMD_PARA_OBJ*     pBasePtr   = NULL;
	__CMD_PARA_OBJ*     pTmpObj    = NULL;
	DWORD               dwCounter  = 0x0000;
	DWORD               index      = 0x0000;

	if(NULL == pszCmd)    //Parameter check.
		return NULL;

	pObjBuffer = (__CMD_PARA_OBJ*)KMemAlloc(4096,KMEM_SIZE_TYPE_4K);
	if(NULL == pObjBuffer)
		goto __TERMINAL;

	pBasePtr = pObjBuffer;
	MemZero(pBasePtr,4096);

	while(*pszCmd)
	{
		if(' ' == *pszCmd)
		{
			pszCmd ++;
			continue; 
		}                                 //Filter the space.
		
		if(('-' == *pszCmd) || ('/' == *pszCmd))
		{
			pszCmd ++;
			pObjBuffer->byFunctionLabel = *pszCmd;
			pszCmd ++;                    //Skip the function label byte.
			continue;
		}
		else
		{
			/*while((' ' != *pszCmd) && *pszCmd)  //To find the first parameter.
			{
				pszCmd ++;
			}
			if(!*pszCmd)
				break;
			while(' ' == *pszCmd)    //Filter the space.
				pszCmd ++;

			if(!*pszCmd)
				break;*/
			index = 0x0000;
			while(('-' != *pszCmd) && ('/' != *pszCmd) && *pszCmd)
			{
				while((' ' != *pszCmd) && (*pszCmd) && (dwCounter <= CMD_PARAMETER_LEN))
				{
					pObjBuffer->Parameter[index][dwCounter] = *pszCmd;
					pszCmd ++;
					dwCounter ++;
				}
				pObjBuffer->Parameter[index][dwCounter] = 0x00;  //Set the terminal flag.
				index ++;               //Ready to copy the next parameter to parameter object.
				dwCounter = 0;

				if(!*pszCmd)
					break;
				while(' ' != *pszCmd)
					pszCmd ++;          //Skip the no space characters if the parameter's length
				                        //is longer than the const CMD_PARAMETER_LEN.
				while(' ' == *pszCmd)
					pszCmd ++;          //Skip the space character.
			}

			pTmpObj = pObjBuffer;       //Update the current parameter object.
			pObjBuffer = (__CMD_PARA_OBJ*)NextParaAddr(pTmpObj,index);
			pTmpObj->byParameterNum = LOBYTE(LOWORD(index));
			if(!*pszCmd)
				break;
			pTmpObj->pNext = pObjBuffer;
		}
	}

__TERMINAL:
	return pBasePtr;
}

//
//The following routine releases the parameter object created by FormParameterObj routine.
//

VOID ReleaseParameterObj(__CMD_PARA_OBJ* lpParamObj)
{
	if(NULL == lpParamObj)  //Parameter check.
		return;

	KMemFree((LPVOID)lpParamObj,KMEM_SIZE_TYPE_4K,4096);  //Release the memory.
	return;
}

//
//Command handler predefinitions.
//

VOID VerHandler(LPSTR);          //Handles the version command.
VOID MemHandler(LPSTR);          //Handles the memory command.
VOID SysInfoHandler(LPSTR);      //Handles the sysinfo command.
VOID HlpHandler(LPSTR);
VOID DateHandler(LPSTR);
VOID TimeHandler(LPSTR);
VOID CpuHandler(LPSTR);
VOID SptHandler(LPSTR);
VOID ClsHandler(LPSTR);
VOID RunTimeHandler(LPSTR);
VOID TestHandler(LPSTR);
VOID UnTestHandler(LPSTR);
VOID MemViewHandler(LPSTR);
VOID SendMsgHandler(LPSTR);
VOID KtViewHandler(LPSTR);
VOID SysNameHandler(LPSTR);
VOID IoCtrlApp(LPSTR);
VOID SysDiagApp(LPSTR);

#define CMD_OBJ_NUM  18

__CMD_OBJ  CmdObj[CMD_OBJ_NUM] = {
	{"version"  ,    VerHandler},
	{"memory"   ,    MemHandler},
	{"sysinfo"  ,    SysInfoHandler},
	{"sysname"  ,    SysNameHandler},
	{"help"     ,    HlpHandler},
	{"date"     ,    DateHandler},
	{"time"     ,    TimeHandler},
	{"cpuinfo"  ,    CpuHandler},
	{"support"  ,    SptHandler},
	{"runtime"  ,    RunTimeHandler},
	{"test"     ,    TestHandler},
	{"untest"   ,    UnTestHandler},
	{"memview"  ,    MemViewHandler},
	{"sendmsg"  ,    SendMsgHandler},
	{"ktview"   ,    KtViewHandler},
	{"ioctrl"   ,    IoCtrlApp},
	{"sysdiag"  ,    SysDiagApp},
	{"cls"      ,    ClsHandler}
};

//
//Global Functions.
//

VOID HlpHandler(LPSTR)           //Command 'help' 's handler.
{
	LPSTR strHelpTitle   = "    The following command is availiable currently:";
	LPSTR strHelpVer     = "    version      : Print out the version information.";
	LPSTR strHelpMem     = "    memory       : Print out the memory layout.";
	LPSTR strHelpSysInfo = "    sysinfo      : Print out the system context.";
	LPSTR strSysName     = "    sysname      : Change the system host name.";
	LPSTR strHelpHelp    = "    help         : Print out this screen.";
	LPSTR strDate        = "    date         : Display or reset system date.";
	LPSTR strTime        = "    time         : Display or reset system time.";
	LPSTR strSupport     = "    support      : Print out technical support information.";
	LPSTR strRunTime     = "    runtime      : Display the total run time since last reboot.";
	LPSTR strMemView     = "    memview      : View a block memory's content.";
	LPSTR strSendMsg     = "    sendmsg      : Send a message to a kernal thread.";
	LPSTR strKtView      = "    ktview       : View all the kernal threads' information.";
	LPSTR strIoCtrlApp   = "    ioctrl       : Start IO control application.";
	LPSTR strSysDiagApp  = "    sysdiag      : System or hardware diag application.";
	LPSTR strCls         = "    cls          : Clear the whole display buffer.";

	PrintLine(strHelpTitle);              //Print out the help information line by line.
	PrintLine(strHelpVer);
	PrintLine(strHelpMem);
	PrintLine(strHelpSysInfo);
	PrintLine(strSysName);
	PrintLine(strHelpHelp);
	PrintLine(strDate);
	PrintLine(strTime);
	PrintLine(strSupport);
	PrintLine(strRunTime);
	PrintLine(strMemView);
	PrintLine(strSendMsg);
	PrintLine(strKtView);
	PrintLine(strIoCtrlApp);
	PrintLine(strSysDiagApp);
	PrintLine(strCls);
}

//
//sysname handler.
//This handler changes the system name,and save it to system config database.
//

static VOID SaveSysName(LPSTR)
{
}

VOID SysNameHandler(LPSTR pszSysName)
{
	__CMD_PARA_OBJ*    pCmdObj = NULL;

	pCmdObj = FormParameterObj(pszSysName);
	if(NULL == pCmdObj)
	{
		PrintLine("Not enough system resource to interpret the command.");
		goto __TERMINAL;
	}
	if((0 == pCmdObj->byParameterNum) || (0 == pCmdObj->Parameter[0][0]))
	{
		PrintLine("Invalid command parameter.");
		goto __TERMINAL;
	}

	if(StrLen(pCmdObj->Parameter[0]) >= MAX_HOSTNAME_LEN)
	{
		PrintLine("System name must not exceed 16 bytes.");
		goto __TERMINAL;
	}

	SaveSysName(pCmdObj->Parameter[0]);
	StrCpy(pCmdObj->Parameter[0],&HostName[0]);
__TERMINAL:
	if(NULL != pCmdObj)
		KMemFree((LPVOID)pCmdObj,KMEM_SIZE_TYPE_4K,4096);
	return;
}

//
//Local helper function.
//The function print out all of the kernal threads' information.
//
static LPSTR                      pszThreadID   = "    Thread ID     : ";
static LPSTR                      pszContext    = "    Context:";
static LPSTR                      pszEax        = "        EAX       : ";
static LPSTR                      pszEbx        = "        EBX       : ";
static LPSTR                      pszEcx        = "        ECX       : ";
static LPSTR                      pszEdx        = "        EDX       : ";
static LPSTR                      pszEsi        = "        ESI       : ";
static LPSTR                      pszEdi        = "        EDI       : ";
static LPSTR                      pszEbp        = "        EBP       : ";
static LPSTR                      pszEsp        = "        ESP       : ";
static LPSTR                      pszEFlags     = "        EFlags    : ";
static LPSTR                      pszStartAddr  = "    Start Address : ";
static LPSTR                      pszStackSize  = "    Stack Size    : ";
static LPSTR                      pszCurrMsgNum = "    Message num   : ";

static VOID PrintAllKt(__KTHREAD_CONTROL_BLOCK** ppControlBlock)
{
	BYTE                       Buffer[32];

	for(DWORD i = 0;i < MAX_KTHREAD_NUM;i ++)
	{
		if(NULL == ppControlBlock[i])
			continue;
		PrintLine(pszThreadID);
		Int2Str(ppControlBlock[i]->dwKThreadID,Buffer);
		PrintStr(Buffer);

		PrintLine(pszContext);
		PrintLine(pszEax);
		Hex2Str(ppControlBlock[i]->dwEAX,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEbx);
		Hex2Str(ppControlBlock[i]->dwEBX,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEcx);
		Hex2Str(ppControlBlock[i]->dwECX,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEdx);
		Hex2Str(ppControlBlock[i]->dwEDX,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEsi);
		Hex2Str(ppControlBlock[i]->dwESI,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEdi);
		Hex2Str(ppControlBlock[i]->dwEDI,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEbp);
		Hex2Str(ppControlBlock[i]->dwEBP,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEsp);
		Hex2Str(ppControlBlock[i]->dwESP,Buffer);
		PrintStr(Buffer);

		PrintLine(pszEFlags);
		Hex2Str(ppControlBlock[i]->dwEFlags,Buffer);
		PrintStr(Buffer);

		PrintLine(pszStartAddr);
		Hex2Str((DWORD)ppControlBlock[i]->pKThreadRoutine,Buffer);
		PrintStr(Buffer);

		PrintLine(pszStackSize);
		Hex2Str(ppControlBlock[i]->dwStackSize,Buffer);
		PrintStr(Buffer);

		PrintLine(pszCurrMsgNum);
		Hex2Str(ppControlBlock[i]->wCurrentMsgNum,Buffer);
		PrintStr(Buffer);

		ChangeLine();
		GotoHome();
	}
}

static VOID KtViewUsage()
{
	PrintLine("    Usage :");
	PrintLine("    ktview -i [-?] kthread_id");
	PrintLine("    Where :");
	PrintLine("        -i         : View the kernal thread's information.");
	PrintLine("        kthread_id : Kernal thread's ID");
	PrintLine("        -?         : Print out the help information of the command.");
}

static VOID PrintKtByID(DWORD dwKThreadID)
{
	__KTHREAD_CONTROL_BLOCK* pControlBlock = NULL;
	DWORD dwIndex                          = 0L;
	BYTE  Buffer[12];

	if((dwKThreadID < 1) || (dwKThreadID > MAX_KTHREAD_NUM))
	{
		PrintLine("Invalid kernal thread ID");
		return;
	}

	dwIndex = dwKThreadID - 1;
	pControlBlock = g_pKThreadQueue[dwIndex];
	if(NULL == pControlBlock)
	{
		PrintLine("The kernal thread is not exist.");
		return;
	}
	PrintLine(pszThreadID);
	Int2Str(pControlBlock->dwKThreadID,Buffer);
	PrintStr(Buffer);

	PrintLine(pszContext);
	PrintLine(pszEax);
	Hex2Str(pControlBlock->dwEAX,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEbx);
	Hex2Str(pControlBlock->dwEBX,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEcx);
	Hex2Str(pControlBlock->dwECX,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEdx);
	Hex2Str(pControlBlock->dwEDX,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEsi);
	Hex2Str(pControlBlock->dwESI,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEdi);
	Hex2Str(pControlBlock->dwEDI,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEbp);
	Hex2Str(pControlBlock->dwEBP,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEsp);
	Hex2Str(pControlBlock->dwESP,Buffer);
	PrintStr(Buffer);

	PrintLine(pszEFlags);
	Hex2Str(pControlBlock->dwEFlags,Buffer);
	PrintStr(Buffer);

	PrintLine(pszStartAddr);
	Hex2Str((DWORD)pControlBlock->pKThreadRoutine,Buffer);
	PrintStr(Buffer);

	PrintLine(pszStackSize);
	Hex2Str(pControlBlock->dwStackSize,Buffer);
	PrintStr(Buffer);

	PrintLine(pszCurrMsgNum);
	Hex2Str(pControlBlock->wCurrentMsgNum,Buffer);
	PrintStr(Buffer);

	ChangeLine();
	GotoHome();
}

VOID KtViewHandler(LPSTR pszPara)
{
	__CMD_PARA_OBJ*  pCmdObj = NULL;
	DWORD            dwID    = 0L;
	BOOL             bResult = FALSE;
	
	if((NULL == pszPara) || 0 == *pszPara)
	{
		PrintAllKt(&g_pKThreadQueue[0]);
		goto __TERMINAL;
	}

	pCmdObj = FormParameterObj(pszPara);
	if(NULL == pCmdObj)
	{
		PrintLine("Can not allocate the resource to interpret the command.");
		goto __TERMINAL;
	}

	switch(pCmdObj->byFunctionLabel)
	{
	case 'i':
	case 0:
		bResult = Str2Hex(pCmdObj->Parameter[0],&dwID);
		if(FALSE == bResult)
		{
			PrintLine("Can not interpret the command's parameter.");
			goto __TERMINAL;
		}
		PrintKtByID(dwID);
		break;
	case '?':
	default:
		KtViewUsage();
		break;
	}

__TERMINAL:
	if(NULL != pCmdObj)
		KMemFree((LPVOID)pCmdObj,KMEM_SIZE_TYPE_4K,4096);
	return;
}

//
//The helper functions,print out the usage information.
//

static VOID SendMsgUsage()
{
	PrintLine("    Usage :");
	PrintLine("      sendmsg kthread_id command [parameter1] [parameter2]");
	PrintLine("    Where :");
	PrintLine("      kthread_id  : Kernal thread ID.");
	PrintLine("      command     : Command number.");
	PrintLine("      parameter1  : The first parameter(optional).");
	PrintLine("      parameter2  : The second parameter(optional).");
}

VOID SendMsgHandler(LPSTR pszPara)
{
	__CMD_PARA_OBJ*          pCmdObj       = NULL;
	DWORD                    dwID          = 0L;
	__KTHREAD_MSG            msg;
	BOOL                     bResult       = FALSE;
	DWORD                    dwCommand     = 0L;
	__KTHREAD_CONTROL_BLOCK* pControlBlock = NULL;

	if((NULL == pszPara) || (0 == *pszPara))
	{
		SendMsgUsage();
		goto __TERMINAL;
	}

	pCmdObj = FormParameterObj(pszPara);
	if(NULL == pCmdObj)
	{
		PrintLine("Can not allocate resource to interpret the command.");
		goto __TERMINAL;
	}

	if(pCmdObj->byParameterNum < 2)
	{
		PrintLine("Miss command code.");
		goto __TERMINAL;
	}

	bResult = Str2Hex(pCmdObj->Parameter[0],&dwID);
	if((FALSE == bResult)
		|| (dwID < 1)
		|| (dwID > MAX_KTHREAD_NUM))
	{
		PrintLine("Invalid kernal thread ID.");
		goto __TERMINAL;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -