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

📄 cmd_parse.c

📁 cmd line like! cmd line like!
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2002 by Sunplus Technology Co., Ltd.             *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus        *
 *  Technology Co., Ltd. All rights are reserved by Sunplus Technology    *
 *  Co., Ltd. This software may only be used in accordance with the       *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Sunplus Technology Co., Ltd.                       *
 *                                                                        *
 *  Sunplus Technology Co., Ltd. reserves the right to modify this        *
 *  software without notice.                                              *
 *                                                                        *
 *  Sunplus Technology Co., Ltd.                                          *
 *  19, Innovation First Road, Science-Based Industrial Park,             *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 *  Author: Wilson Lu                                                     *
 *                                                                        *
 **************************************************************************/
#include <string.h>
#include <stdio.h>
#include "cmd.h"

/****************************************************************************
*                           C O N S T A N T S                               *
****************************************************************************/
#define CMD_HISTORIES	8
#define CMD_BUF_SIZE	64
#define HELP_CMD_HELP	"Provides help for commands. Try 'help <command>'"
#define HELP_CMD_LIST	"Provides list for commands."
/****************************************************************************
*                         G L O B A L    D A T A                            *
****************************************************************************/
static void cmdHelpDisp(UINT8 *parameters);
static void cmdListDisp(UINT8 *parameters);
static cmd_t  cmdHelp = {"help", cmdHelpDisp, HELP_CMD_HELP, 0x0};
static cmd_t  cmdList = {"ls",   cmdListDisp, HELP_CMD_LIST, 0x0};
static cmd_t  *pcmds = &cmdHelp;

static UINT8  buffer[CMD_BUF_SIZE];
static UINT32 pos;
static UINT32 logIn;

static UINT32 histBuf[CMD_HISTORIES][CMD_BUF_SIZE];
static UINT32 histPos[CMD_HISTORIES];
static UINT32 histIns;
static UINT32 histOutput;
static UINT32 histInsWrap;
static UINT32 histOutputWrap;
/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
void
cmdWhiteSpaceStrip(
	UINT8 **pptr
)
{
	UINT8 *ptr;

	ptr = *pptr;
	while ( *ptr != '\0' ) {
		if ((*ptr != ' ')  &&
		    (*ptr != '\t') &&
		    (*ptr != '\n') &&
		    (*ptr != '\r')) {
			break;
		}
		else {
			ptr++;
		}
	}
	*pptr = ptr;
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
void
cmdWhiteSpaceReplace(
	UINT8 **pptr
)
{
	UINT8 *ptr;

	ptr = *pptr;
	while ( *ptr != '\0' ) {
		if ((*ptr != ' ')  &&
		    (*ptr != '\t') &&
		    (*ptr != '\n') &&
		    (*ptr != '\r')) {
			break;
		}
		else {
			**pptr = '\0';
			ptr++;
		}
	}
	*pptr = ptr;
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
void
cmdTokenStrip(
	UINT8 **pptr
)
{
	UINT8 *ptr;

	ptr = *pptr;
	while ( *ptr != '\0' ) {
		if ((*ptr == ' ')  ||
		    (*ptr == '\t') ||
		    (*ptr == '\n') ||
		    (*ptr == '\r')) {
			break;
		}
		else {
			ptr++;
		}
	}
	*pptr = ptr;
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
SINT32 strtoulex(char *pdt)
{
	char *pt = pdt;
	UINT32 retd = 0;
	SINT32 sign = 1;

	if (pt != NULL) {
		if (pt[0] == '-') {
			sign = -1;
			pt ++;
		}
		
		if (pt[0] == '0' && (pt[1] == 'x' || pt[1] == 'X')) {
			for(pt+=2 ; *pt!=' ' && *pt!=0 && *pt!='\t' && *pt!='\n' && *pt!='\r'; pt++){
				if(*pt>='0' && *pt<='9'){
					retd = retd*16 + (*pt-'0');
				} else if (*pt>='a' && *pt<='f') {
					retd = retd*16 + (*pt-'a') + 10;
				} else if (*pt>='A' && *pt<='F') {
					retd = retd*16 + (*pt-'A') + 10;
				} else {
					retd = 0;
					break;
				}
			}
		} else {
			for ( ; *pt!=' ' && *pt!=0 && *pt!='\t' && *pt!='\n' && *pt!='\r'; pt++) {
				if (*pt>='0' && *pt<='9') {
					retd = retd*10 + (*pt-'0');
				} else {
					retd = 0;
					break;
				}
			}
		}
	}

	return(retd*sign);
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
static void
cmdHelpShow(
	cmd_t *bc, 
	UINT32 maxCmdLen
)
{
	const UINT8 *help = bc->phelp;
	UINT32 helpLen = strlen(help);
	UINT32 spaces = maxCmdLen - strlen(bc->cmd);
	UINT32 showThisLine = 0;
	UINT32 lineOne = TRUE;
	UINT32 c;

	printf_0("%s", bc->cmd);
	if ( helpLen == 0 ) {
		printf_0(" - No help.\n");
		return;
	}

	while ( helpLen ) {
		while ( spaces > 1 ) {
			printf_0(" ");
			spaces--;
		};
		if ( lineOne ) {
			printf_0(" - ");
		}
		spaces = maxCmdLen + 3; /* 3: " - " has three characters */
		lineOne = FALSE;

		/*
		 * See if greater then the line length if so, work back from the end for a
		 * space, tab or lf or cr.
		 */

		if ( helpLen > (78 - maxCmdLen - 3) ) {
			for ( showThisLine = (78 - maxCmdLen - 3) - 1; 
				showThisLine; 
				showThisLine-- )
				if ( (help[showThisLine] == ' ') ||
					 (help[showThisLine] == '\n') ||
					 (help[showThisLine] == '\r') )
					break;

				/*
				 * If showThisLine is 0, it is a very long word !!
				 */

			if ( showThisLine == 0 )
				showThisLine = (78 - maxCmdLen - 3) - 1;
		} else
			showThisLine = helpLen;

		for ( c = 0; c < showThisLine; c++ ) {
			if ( (help[c] == '\r') || (help[c] == '\n') ) {
				showThisLine = c;
			}
			else {
				printf_0("%c", help[c]);
			}
		}

		printf_0("\n");

		help += showThisLine;
		helpLen -= showThisLine;

		/*
		 * Move past the line feeds or what ever else is being skipped.
		 */

		while ( helpLen ) {
			if ( (*help != '\r') && (*help != '\n') ) {
				break;
			}
			if ( *help != ' ' ) {
				help++;
				helpLen--;
				break;
			}
			help++;
			helpLen--;
		}
	}
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
static void
cmdHelpDisp(
	UINT8 *parameters
)
{
	cmd_t *bc = pcmds;
	UINT32 maxCmdLen = 0, i = 0;
	UINT32 len;

	/* See if the user asked for a particular help. */
	if ( *parameters != '\0' ) {
		while ( bc ) {
			if ( strcmp(bc->cmd, parameters) == 0 ) {
				cmdHelpShow(bc, strlen(bc->cmd));
				return;
			}
			bc = bc->pnext;
		}
		printf_0("no help for command '%s' found, try just 'help'\n", parameters);
		return;
	}

	/*
	 * Find the largest command size.
	 */

	while ( bc ) {
		len = strlen(bc->cmd);
		if ( len > maxCmdLen ) {
			maxCmdLen = len;
		}
		bc = bc->pnext;
	}

	maxCmdLen++;
	bc = pcmds;

	/*
	 * Now some nice formatting for the help.
	 */
	
	printf_0("-\n  ");
	while ( bc ) {
		printf_0("%-8s ", bc->cmd);
		i++;
		if( i == 8 ) {
			printf_0("\n  ");
			i = 0;
		}
		
		if(bc->pnext->cmd[0] != bc->cmd[0]){
			if(i == 0)	printf_0("\r-\n  ");
			else{
				printf_0("\n-\n  ");
				i = 0;
			}
		}
		
		bc = bc->pnext;
	}
	
	if ( i != 0 ) {
		printf_0("\n");
	}
}

/**************************************************************************
 *                                                                        *
 *  Function Name:                                                        *
 *                                                                        *
 *  Purposes:                                                             *
 *                                                                        *
 *  Descriptions:                                                         *
 *                                                                        *
 *  Arguments:                                                            *
 *                                                                        *
 *  Returns: None                                                         *
 *                                                                        *
 *  See also:                                                             *
 *                                                                        *
 **************************************************************************/
static void
cmdListDisp(
	UINT8 *parameters
)
{
	cmd_t *bc = pcmds;
	UINT32 i = 0;

	/*
	 * Now some nice formatting for the help.
	 */
	
	printf_0("-\n  ");
	while ( bc ) {
		printf_0("%-8s ", bc->cmd);
		i++;
		if( i == 8 ) {
			printf_0("\n  ");

⌨️ 快捷键说明

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