📄 cmd.c
字号:
/****************************************************************************** SCT, Serial Communication Tracer ** CopyRight (C) Bruce (ZhaoFei), zhaofei@zflogic.com* http://www.zflogic.com** 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.* ***************************************************************************/#include "stdhdr.h"#include "util.h"#define CMD_L#include "cmd.h"#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) { //compare cmd to cmd if(!strcmp(cmdtbl[i].cmd,cmdtbl[j].cmd)){ printf("***error*** Two commands conflicts as: %s\n",cmdtbl[i].cmd); return -1; } //compare alias to alias11:wq if(cmdtbl[i].alias && cmdtbl[j].alias && !strcmp(cmdtbl[i].alias,cmdtbl[j].alias)) { printf("***error*** Two alias conflics as: %s\n",cmdtbl[i].alias); return -1; } } //compare alias to cmd if(cmdtbl[i].alias && !strcmp(cmdtbl[i].alias,cmdtbl[j].cmd)) { printf("***error*** Alias conflics to command as: %s\n",cmdtbl[i].alias); 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,j,tmp,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 j=0; printf("Command list:\n"); //print remain commands memset(cmdbuf,0,sizeof(cmdbuf)); memset(cmdbuf,' ',20); memcpy(cmdbuf,"?",1); printf(cmdbuf); j++; memset(cmdbuf,' ',20); memcpy(cmdbuf,"help",4); printf(cmdbuf); j++; memset(cmdbuf,' ',20); memcpy(cmdbuf,"quit (bye)",10); printf(cmdbuf); j++; //print user commands for(i=0;i<ncmd;i++) { if(cmdtbl[i].alias) sprintf(cmdbuf,"%s (%s)", cmdtbl[i].cmd, cmdtbl[i].alias); else sprintf(cmdbuf,"%s",cmdtbl[i].cmd); tmp = strlen(cmdbuf); memset(&cmdbuf[tmp],' ',20-tmp); cmdbuf[20] = 0; printf(cmdbuf); if(!((i+j+1)%4)) printf("\n"); } 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") || !strcmp(parv[1],"bye")) 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) || (cmdtbl[i].alias && !strcmp(parv[1],cmdtbl[i].alias))) { printf(cmdtbl[i].usage); break; } } if (i == ncmd) printf("Unrecog command\n"); } } else if (!strcmp(parv[0],"quit") || !strcmp(parv[0],"bye")){ break; } else { for(i=0;i<ncmd;i++) if(!strcmp(parv[0],cmdtbl[i].cmd) || (cmdtbl[i].alias && !strcmp(parv[0],cmdtbl[i].alias))){ ((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 + -