Improvements to command "history".

This commit is contained in:
Alan Mishchenko 2026-03-27 20:09:54 -07:00
parent b8059c310a
commit bef23270f8
3 changed files with 75 additions and 26 deletions

View File

@ -98,6 +98,7 @@ void Cmd_Init( Abc_Frame_t * pAbc )
Cmd_CommandAdd( pAbc, "Basic", "quit", CmdCommandQuit, 0 ); Cmd_CommandAdd( pAbc, "Basic", "quit", CmdCommandQuit, 0 );
Cmd_CommandAdd( pAbc, "Basic", "abcrc", CmdCommandAbcrc, 0 ); Cmd_CommandAdd( pAbc, "Basic", "abcrc", CmdCommandAbcrc, 0 );
Cmd_CommandAdd( pAbc, "Basic", "history", CmdCommandHistory, 0 ); Cmd_CommandAdd( pAbc, "Basic", "history", CmdCommandHistory, 0 );
Cmd_CommandAdd( pAbc, "Basic", "hi", CmdCommandHistory, 0 );
Cmd_CommandAdd( pAbc, "Basic", "alias", CmdCommandAlias, 0 ); Cmd_CommandAdd( pAbc, "Basic", "alias", CmdCommandAlias, 0 );
Cmd_CommandAdd( pAbc, "Basic", "unalias", CmdCommandUnalias, 0 ); Cmd_CommandAdd( pAbc, "Basic", "unalias", CmdCommandUnalias, 0 );
Cmd_CommandAdd( pAbc, "Basic", "help", CmdCommandHelp, 0 ); Cmd_CommandAdd( pAbc, "Basic", "help", CmdCommandHelp, 0 );
@ -441,38 +442,31 @@ int CmdCommandAbcrc( Abc_Frame_t * pAbc, int argc, char **argv )
int CmdCommandHistory( Abc_Frame_t * pAbc, int argc, char **argv ) int CmdCommandHistory( Abc_Frame_t * pAbc, int argc, char **argv )
{ {
char * pName, * pStr = NULL; char * pName, * pStr = NULL;
int i, c; char ** ppMatches = NULL;
int * pIds = NULL;
int i;
int nPrints = 20; int nPrints = 20;
int nPrinted = 0; int nPrinted = 0;
Extra_UtilGetoptReset(); if ( argc == 2 && !strcmp(argv[1], "-h") )
while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF ) goto usage;
{ if ( argc > 3 )
switch ( c )
{
case 'h':
goto usage;
default :
goto usage;
}
}
if ( argc > globalUtilOptind + 2 )
goto usage; goto usage;
// parse arguments: can be [substring] [number] in either order // parse arguments: can be [substring] [number] in either order
if ( argc == globalUtilOptind + 1 ) if ( argc == 2 )
{ {
// one argument: either number or substring // one argument: either number or substring
pStr = argv[globalUtilOptind]; pStr = argv[1];
if ( pStr && pStr[0] >= '1' && pStr[0] <= '9' ) if ( pStr && pStr[0] >= '1' && pStr[0] <= '9' )
{ {
nPrints = atoi(pStr); nPrints = atoi(pStr);
pStr = NULL; pStr = NULL;
} }
} }
else if ( argc == globalUtilOptind + 2 ) else if ( argc == 3 )
{ {
// two arguments: substring and number // two arguments: substring and number
char * arg1 = argv[globalUtilOptind]; char * arg1 = argv[1];
char * arg2 = argv[globalUtilOptind + 1]; char * arg2 = argv[2];
// Try to parse second argument as number // Try to parse second argument as number
if ( arg2[0] >= '1' && arg2[0] <= '9' ) if ( arg2[0] >= '1' && arg2[0] <= '9' )
@ -500,14 +494,22 @@ int CmdCommandHistory( Abc_Frame_t * pAbc, int argc, char **argv )
fprintf( pAbc->Out, "%4d : %s\n", Vec_PtrSize(pAbc->aHistory)-i, pName ); fprintf( pAbc->Out, "%4d : %s\n", Vec_PtrSize(pAbc->aHistory)-i, pName );
} }
else { else {
// Search string provided, show up to nPrints matching entries // Search string provided, select up to nPrints most recent matching entries
Vec_PtrForEachEntry( char *, pAbc->aHistory, pName, i ) ppMatches = ABC_ALLOC( char *, nPrints );
pIds = ABC_ALLOC( int, nPrints );
Vec_PtrForEachEntryReverse( char *, pAbc->aHistory, pName, i )
if ( strstr(pName, pStr) ) if ( strstr(pName, pStr) )
{ {
fprintf( pAbc->Out, "%4d : %s\n", Vec_PtrSize(pAbc->aHistory)-i, pName ); pIds[nPrinted] = Vec_PtrSize(pAbc->aHistory)-i;
ppMatches[nPrinted] = pName;
if ( ++nPrinted >= nPrints ) if ( ++nPrinted >= nPrints )
break; break;
} }
// Print the selected entries in reverse so larger history indices appear first
for ( i = nPrinted - 1; i >= 0; i-- )
fprintf( pAbc->Out, "%4d : %s\n", pIds[i], ppMatches[i] );
ABC_FREE( pIds );
ABC_FREE( ppMatches );
} }
return 0; return 0;

View File

@ -25,7 +25,6 @@
#include <ctype.h> #include <ctype.h>
ABC_NAMESPACE_IMPL_START ABC_NAMESPACE_IMPL_START
// proper declaration of isspace
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
/// DECLARATIONS /// /// DECLARATIONS ///
@ -393,9 +392,51 @@ int CmdApplyAlias( Abc_Frame_t * pAbc, int *argcp, char ***argvp, int *loop )
***********************************************************************/ ***********************************************************************/
char * CmdHistorySubstitution( Abc_Frame_t * pAbc, char *line, int *changed ) char * CmdHistorySubstitution( Abc_Frame_t * pAbc, char *line, int *changed )
{ {
// as of today, no history substitution char * pBeg = line, * pEnd, * pStr;
*changed = 0; int iRecall, nSize;
return line; while ( *pBeg && isspace((unsigned char)*pBeg) )
pBeg++;
if ( *pBeg == 0 )
{
*changed = 0;
return line;
}
pEnd = pBeg;
while ( *pEnd && !isspace((unsigned char)*pEnd) )
pEnd++;
if ( *pEnd )
{
char * pTemp = pEnd;
while ( *pTemp && isspace((unsigned char)*pTemp) )
pTemp++;
if ( *pTemp )
{
*changed = 0;
return line;
}
}
if ( *pBeg < '1' || *pBeg > '9' )
{
*changed = 0;
return line;
}
for ( pStr = pBeg; pStr < pEnd; pStr++ )
if ( *pStr < '0' || *pStr > '9' )
{
*changed = 0;
return line;
}
iRecall = atoi( pBeg );
nSize = Vec_PtrSize( pAbc->aHistory );
if ( iRecall < 1 || iRecall > nSize )
{
fprintf( pAbc->Err, "History entry %d does not exist.\n", iRecall );
line[0] = 0;
*changed = 0;
return line;
}
*changed = 1;
return (char *)Vec_PtrEntry( pAbc->aHistory, nSize - iRecall );
} }
/**Function************************************************************* /**Function*************************************************************
@ -905,4 +946,3 @@ void Cmd_CommandSGen( Abc_Frame_t * pAbc, int nParts, int nIters, int fVerbose )
/// END OF FILE /// /// END OF FILE ///
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_END

View File

@ -56,6 +56,7 @@ SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endif #endif
#include "base/abc/abc.h" #include "base/abc/abc.h"
#include "base/cmd/cmdInt.h"
#include "mainInt.h" #include "mainInt.h"
#include "base/wlc/wlc.h" #include "base/wlc/wlc.h"
@ -363,9 +364,15 @@ int Abc_RealMain( int argc, char * argv[] )
// execute commands given by the user // execute commands given by the user
while ( !feof(stdin) ) while ( !feof(stdin) )
{ {
int did_subst;
// print command line prompt and // print command line prompt and
// get the command from the user // get the command from the user
sCommand = Abc_UtilsGetUsersInput( pAbc ); sCommand = Abc_UtilsGetUsersInput( pAbc );
sCommand = CmdHistorySubstitution( pAbc, sCommand, &did_subst );
if ( sCommand == NULL )
break;
if ( did_subst )
fprintf( pAbc->Out, "%s\n", sCommand );
// execute the user's command // execute the user's command
fStatus = Cmd_CommandExecute( pAbc, sCommand ); fStatus = Cmd_CommandExecute( pAbc, sCommand );