From: Stefan E. <se...@us...> - 2001-10-05 11:53:49
|
Update of /cvsroot/blob/blob/src In directory usw-pr-cvs1:/tmp/cvs-serv24606 Modified Files: command_hist.c Log Message: - simple command line history Index: command_hist.c =================================================================== RCS file: /cvsroot/blob/blob/src/command_hist.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- command_hist.c 2001/10/05 11:48:11 1.1 +++ command_hist.c 2001/10/05 11:53:46 1.2 @@ -0,0 +1,260 @@ +/********************************************************************** + * 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$" + +/********************************************************************** + * Includes + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "command.h" +#include "errno.h" +#include "init.h" +#include "serial.h" +#include "time.h" +#include "types.h" +#include "util.h" + +/********************************************************************** + * Defines / Makros + */ + +#define CMDHIST_DEBUG 0 + +#define MAX_HIST 32 + +/********************************************************************** + * Programmglobale Variable + */ + +/********************************************************************** + * Modulglobale Variable + */ + +static int is_initialized = 0; +static int cmdhist_entries = 0; +static int cmdhist_read = 0; +static int cmdhist_write = 0; +static char cmdhistory[MAX_HIST][MAX_COMMANDLINE_LENGTH]; + + +/********************************************************************** + * Prototypen + */ + +/********************************************************************** + * Exportierte Funktionen + */ + +/********************************************************************* + * cmdhist_init + * + * AUTOR: SELETZ + * REVISED: + * + * Initializes this module + * + */ +int cmdhist_init( void ) +{ + int i; + + cmdhist_read = 0; + cmdhist_write = 0; + cmdhist_entries = 0; + + i=MAX_HIST - 1; + while ( i-- ) { + cmdhistory[i][0]=0; + } + + is_initialized = 1; + return 0; +} + + +/********************************************************************* + * cmd_push + * + * AUTOR: SELETZ + * REVISED: + * + * Push a command to the history buffer + * + */ +int cmdhist_push( char *cmd ) +{ + if ( !is_initialized ) { + cmdhist_init(); + } + + 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 ( !is_initialized ) { + cmdhist_init(); + } + + 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 ( !is_initialized ) { + cmdhist_init(); + } + + 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; +} + +/********************************************************************** + * Statische Funktionen + */ |