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

📄 cmd.c

📁 应用平台 Unix/Linux  <br> sct(Serial Communication Tracer的缩写)是一个很有用的Linux/Unix串行通信调试工具
💻 C
字号:
/******************************************************************************  SCT, Serial Communication Tracer  **  CopyRight (C)  Bruce (ZhaoFei), makeidea@21cn.com*                               http://www.makeidea.net**  All sources in the package are copyrighted under the the terms of GNU GPL******************************************************************************                                                                         *  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; if not, write to the Free Software*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA**  If you intend to use this pragram commercially on a regular basis you*  must contact the author for an appropriate donation.*                                                                       ***************************************************************************/#define CMD_L#include "stdhdr.h"#undef CMD_L#define MAXCMDHISTORY	20void parse(int *parc, char *parv[], char *src,int maxpar){	int i;	bool willbeword;	*parc=0;	willbeword=true;	i=0;		if (src == NULL)		return ;	while(src[i]!=0 && *parc<maxpar){		if(src[i]==' '){			willbeword=true;			src[i]=0;		} else{			if(willbeword){				parv[*parc]=&src[i];				(*parc)++;				willbeword=false;			}		}		i++;	}}void parseex(int *parc,char *parv[],char *src,int maxpar,char separator){	int i;	bool willbeword;	*parc = 0;	parv[*parc] = NULL;	willbeword = true;	i=0;		if (src == NULL)		return ;		while ( *parc < maxpar) {		if (src[i] == ' ') {			src[i]=0;		} else if (src[i] == separator) {			src[i]=0;			willbeword = true;			(*parc)++;			parv[*parc] = NULL;		} else if ((src[i] == 0) && (willbeword == false)) {			(*parc)++;			break;		} else {			if (willbeword) {				parv[*parc]=&src[i];				willbeword=false;			}		}		i++;	}}int checkcmdtbl(struct cmd_t *cmdtbl)	//return -1 for error ,else for num of cmd{	int i,j,ncmd;		i=0;	while(!(cmdtbl[i].cmd==NULL 		   || cmdtbl[i].func==NULL		   || i==MAXCMD)) i++;	ncmd=i;	if(ncmd==0)		return 0;	//check cmd name for over length or conflicts	for(i=0;i<ncmd;i++)		if(strlen(cmdtbl[i].cmd)>128){			printf("***error*** one of user commands to long to over 128\n");			return -1;		}	for(i=0;i<ncmd;i++)		for(j=0;j<ncmd;j++){			if( (i!=j)				&&!strcmp(cmdtbl[i].cmd,cmdtbl[j].cmd)){				printf("***error*** user commands conflicts as follow:\n");				printf(cmdtbl[i].cmd);				printf("\n");				return -1;			}		}//check cmd name for conflicts with reamain word	for(i=0;i<ncmd;i++){		if(!strcmp(cmdtbl[i].cmd,"?")){			printf("user command conficts with remain word '?'\n");			return -1;		} 		if(!strcmp(cmdtbl[i].cmd,"quit")){			printf("user command conficts with remain word 'quit'\n");			return -1;		}		if(!strcmp(cmdtbl[i].cmd,"help")){			printf("user command conficts with remain word 'help'\n");			return -1;		}	}	return ncmd;}bool docmd(char *prompt,struct cmd_t *cmdtbl){	int i,ncmd,nhis=0,phis=-1;	char ch,cmdbuf[256],hisbuf[MAXCMDHISTORY][256];	int parc;	char *parv[64];		if ( (ncmd=checkcmdtbl(cmdtbl)) < 0)		return false;		if (ncmd == 0){		printf("Command list empty!\n");		return true;	}		printf("\nYou have entered command shell now\n");	printf("Input '?' for command list or 'help command_name' for help\n\n");	do{		printf(prompt);		i=0;		do{			ch=getchar();			cmdbuf[i++]=ch;		} while (ch != 0x0a);		i>0 ? i--:i; //trim 0x0a		cmdbuf[i]=0;			parse(&parc,parv,cmdbuf,sizeof(parv));		if(parc==0)			continue;		if(!strcmp(parv[0],"?")) {	//get command list			printf("Command list:\n");			//print remain commands			printf("?\n");			printf("help\n");			printf("quit\n");			//print user commands			for(i=0;i<ncmd;i++) {				printf(cmdtbl[i].cmd);				printf("\n");			}		} else if (!strcmp(parv[0],"help")) {	//get help info			if (parc != 2) {				printf("Please input 'help  command_name', \					\n   you can input '?' to get command list\n");				continue;			}			//help for remain command			if(!strcmp(parv[1],"?"))				printf("? - get command list  \					\n   usage: ?\n");			else if(!strcmp(parv[1],"help"))				printf("help - get help for the command \					\n   usage: help command\					\n      eg: help stat \n");			else if(!strcmp(parv[1],"quit"))				printf("quit - quit program \					\n   usage: quit \n");			//help for customize command					else {						for(i=0;i<ncmd;i++)					if(!strcmp(parv[1],cmdtbl[i].cmd)) {						printf(cmdtbl[i].usage);						break;					}				if (i == ncmd)					printf("Unrecog command\n");			} 		} else if (!strcmp(parv[0],"quit")){			break;		} else {			for(i=0;i<ncmd;i++)				if(!strcmp(parv[0],cmdtbl[i].cmd)){					((CMD_ROUTINE )cmdtbl[i].func)(parc,parv);					break;				}			if (i == ncmd)				printf("Command not found, input '?' for command list\n");		}	}while(1);	return true;}

⌨️ 快捷键说明

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