📄 terminal.cc
字号:
/* YAKS, a Khepera simulator including a separate GA and ANN (Genetic Algoritm, Artificial Neural Net). Copyright (C) 2000 Johan Carlsson (johanc@ida.his.se) 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 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.*/#include "terminal.h"/** * Constructor, creates a terminal with the default prompt "|YAKS >" */Terminal::Terminal(){ commands = NULL; prompt = strdup("|YAKS >");}/** * Deconstructor, deallocated all memory allocated. * */Terminal::~Terminal(){ CommandS *next; free(prompt); if(commands){ while(commands->next!=NULL){ next = commands->next; free(commands->command); free(commands->description); free(commands); commands = next; } free(commands->command); free(commands->description); free(commands); }}/** * Converts a string to upper case * @param str The string to be converted, directly modified. */void Terminal::upCase(char *str){ int i=0; while(str[i]!='\0'){ str[i]=(char)toupper(str[i]); i++; }}/** * Checks all user added commands for a match and calls the function if a match is found. * @param com The string/command to search for */void Terminal::runUserCommand(const char *com){ CommandS *item; item = commands; if(item){ while(item->next!=NULL){ if(strcmp(com,item->command)==0) item->func(); item = item->next; } if(strcmp(com,item->command)==0) item->func(); }}/** * Checks all internal added commands for a match and calls the function if a match is found. * @param com The string/command to search for */void Terminal::runInternalCommand(const char *com){ if(strlen(com) == 1){ switch(com[0]){ case 'M': printMenu(); break; case 'P': free(prompt); prompt = readline("New prompt >"); break; } }}/** * Starts an interactive terminal which will run until user types exit. */void Terminal::runTerminal(){ char *commandLine; commandLine = readline(prompt); while(strcmp(commandLine,"exit")!=0){ runInternalCommand(commandLine); runUserCommand(commandLine); add_history(commandLine); free(commandLine); commandLine = readline(prompt); } free(commandLine);}/** * Set the prompt used in terminal mode. * @param newPrompt The new prompt. */void Terminal::setPrompt(const char *newPrompt){ if(prompt) free(prompt); prompt = strdup(newPrompt);}/** * Prints all commands available in the terminal. */void Terminal::printMenu(){ CommandS *item; item = commands; while(item->next!=NULL){ printf("%s - %s\n",item->command,item->description); item = item->next; } printf("%s - %s\n",item->command,item->description);}/** * Add a command to the terminal * @param str The string which is the command. * @param desc The description of the command. * @param f A pointer to a function that will be called when user types the command specified by \a str. */void Terminal::addItem(const char* str,const char* desc,void (*f)(void)){ CommandS *item; item = commands; if(commands!=NULL){ while(item->next!=NULL) item=item->next; item->next = (CommandS*)malloc(sizeof(CommandS)); item = item->next; } else{ commands = (CommandS*)malloc(sizeof(CommandS)); item = commands; } item->next=NULL; item->command = strdup(str); item->description = strdup(desc); item->func = f;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -