From: Erik M. <er...@us...> - 2001-10-07 23:32:08
|
Update of /cvsroot/blob/blob/src/diag In directory usw-pr-cvs1:/tmp/cvs-serv9847/src/diag Modified Files: Makefile.am commands.c Added Files: command_hist.c Log Message: add command line history to diag (making cmdhist_init() an initcall while doing so) --- NEW FILE: command_hist.c --- /********************************************************************** * Command history * * AUTOR: SELETZ * * Implements a simple command history * * Copyright (C) 2001 Stefan Eletzhofer <ste...@ww...> * * 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 * */ #ident "$Id: command_hist.c,v 1.1 2001/10/07 23:32:05 erikm Exp $" /********************************************************************** * Includes */ #ifdef HAVE_CONFIG_H # include <blob/config.h> #endif #include <blob/command.h> #include <blob/command_hist.h> #include <blob/errno.h> #include <blob/init.h> #include <blob/util.h> /********************************************************************** * Defines / Makros */ #define CMDHIST_DEBUG 0 #define MAX_HIST 32 /********************************************************************** * Programmglobale Variable */ /********************************************************************** * Modulglobale Variable */ static int cmdhist_entries = 0; static int cmdhist_read = 0; static int cmdhist_write = 0; static char cmdhistory[MAX_HIST][MAX_COMMANDLINE_LENGTH]; /********************************************************************** * Prototypen */ /********************************************************************** * Exported functions */ /********************************************************************* * cmd_push * * AUTOR: SELETZ * REVISED: * * Push a command to the history buffer * */ int cmdhist_push( char *cmd ) { if ( !cmd ) return -EINVAL; if ( strlen( cmd ) > MAX_COMMANDLINE_LENGTH ) return -EINVAL; if ( strlen( cmd ) == 0 ) return 0; strcpy( cmdhistory[ cmdhist_write ], cmd ); cmdhist_write ++; cmdhist_write = cmdhist_write % MAX_HIST; if ( cmdhist_entries < MAX_HIST ) cmdhist_entries++; #if CMDHIST_DEBUG SerialOutputString( "e=" ); SerialOutputDec( cmdhist_entries ); SerialOutputString( " r=" ); SerialOutputDec( cmdhist_read ); SerialOutputString( " w=" ); SerialOutputDec( cmdhist_write ); SerialOutputString( "\n" ); #endif return 0; } /********************************************************************* * cmdhist_reset * * AUTOR: SELETZ * REVISED: * * Resets read ptr * */ int cmdhist_reset( void ) { cmdhist_read = cmdhist_write; return 0; } /********************************************************************* * cmd_next * * AUTOR: seletz * REVISED: * * Gets next command in history * */ int cmdhist_next( char **cmd ) { int ptr; if ( !cmdhist_entries ) return -EINVAL; if ( !cmd ) return -EINVAL; ptr = cmdhist_read; if ( ptr == 0 ) { if ( cmdhist_entries != MAX_HIST ) return -EINVAL; ptr = MAX_HIST - 1; } else { ptr--; } if ( !cmdhistory[ptr][0] ) return -EINVAL; *cmd = cmdhistory[ptr]; cmdhist_read = ptr; #if CMDHIST_DEBUG SerialOutputString( "e=" ); SerialOutputDec( cmdhist_entries ); SerialOutputString( " r=" ); SerialOutputDec( cmdhist_read ); SerialOutputString( " w=" ); SerialOutputDec( cmdhist_write ); SerialOutputString( "\n" ); #endif return 0; } /********************************************************************* * cmd_prev * * AUTOR: SELETZ * REVISED: * * Gets previous command from history * */ int cmdhist_prev( char **cmd ) { int ptr; if ( !cmd ) return -EINVAL; if ( !cmdhist_entries ) return -EINVAL; ptr = cmdhist_read + 1; ptr = ptr % MAX_HIST; if ( ptr == cmdhist_write ) return -EINVAL; if ( !cmdhistory[ptr][0] ) return -EINVAL; *cmd = cmdhistory[ptr]; cmdhist_read = ptr; return 0; } /********************************************************************** * Static functions */ /********************************************************************* * cmdhist_init * * AUTOR: SELETZ * REVISED: * * Initializes this module * */ static void cmdhist_init( void ) { int i; cmdhist_read = 0; cmdhist_write = 0; cmdhist_entries = 0; i=MAX_HIST - 1; while ( i-- ) { cmdhistory[i][0]=0; } } __initlist(cmdhist_init, INIT_LEVEL_OTHER_STUFF); Index: Makefile.am =================================================================== RCS file: /cvsroot/blob/blob/src/diag/Makefile.am,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Makefile.am 2001/10/07 22:09:54 1.2 +++ Makefile.am 2001/10/07 23:32:05 1.3 @@ -36,6 +36,7 @@ # be linked in the wrong order! diag_elf32_SOURCES = \ start.S \ + command_hist.c \ commands.c \ initcalls.c \ main.c Index: commands.c =================================================================== RCS file: /cvsroot/blob/blob/src/diag/commands.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- commands.c 2001/10/07 22:36:11 1.2 +++ commands.c 2001/10/07 23:32:05 1.3 @@ -29,11 +29,106 @@ #include <blob/command.h> +#include <blob/command_hist.h> #include <blob/reboot.h> +#include <blob/serial.h> #include <blob/terminal.h> +#include <blob/time.h> +#include <blob/util.h> __commandlist(reset_terminal, "reset", resethelp); __commandlist(reboot, "reboot", reboothelp); + + + + +/* more or less like SerialInputString(), but with echo and backspace */ +/* unlike the version in libblob, this version has a command history */ +int GetCommand(char *command, int len, int timeout) +{ + u32 startTime, currentTime; + char c; + int i; + int numRead; + int maxRead = len - 1; + + TimerClearOverflow(); + + startTime = TimerGetTime(); + + cmdhist_reset(); + + for(numRead = 0, i = 0; numRead < maxRead;) { + /* try to get a byte from the serial port */ + while(!SerialInputByte(&c)) { + currentTime = TimerGetTime(); + + /* check timeout value */ + if((currentTime - startTime) > + (timeout * TICKS_PER_SECOND)) { + /* timeout */ + command[i++] = '\0'; + cmdhist_push( command ); + return(numRead); + } + } + + if((c == '\r') || (c == '\n')) { + command[i++] = '\0'; + + /* print newline */ + SerialOutputByte('\n'); + cmdhist_push( command ); + return(numRead); + } else if(c == '\b') { /* FIXME: is this backspace? */ + if(i > 0) { + i--; + numRead--; + /* cursor one position back. */ + SerialOutputString("\b \b"); + } + } else if ( c == CMDHIST_KEY_UP ) { + char *cmd = NULL; + /* get cmd from history */ + if ( cmdhist_next( &cmd ) != 0 ) + continue; + + /* clear line */ + while ( numRead-- ) { + SerialOutputString("\b \b"); + } + + /* display it */ + SerialOutputString(cmd); + i = numRead = strlen( cmd ); + strcpy( command, cmd ); + } else if ( c == CMDHIST_KEY_DN ) { + char *cmd = NULL; + /* get cmd from history */ + if ( cmdhist_prev( &cmd ) != 0 ) + continue; + + /* clear line */ + while ( numRead-- ) { + SerialOutputString("\b \b"); + } + + /* display it */ + SerialOutputString(cmd); + i = numRead = strlen( cmd ); + strcpy( command, cmd ); + } else { + command[i++] = c; + numRead++; + + /* print character */ + SerialOutputByte(c); + } + } + + cmdhist_push( command ); + return(numRead); +} |