Commit bffbd05a by Alan Mishchenko

Added recording history of used commands into file 'abc.history' (Windows only).

parent 19d50b98
...@@ -79,9 +79,7 @@ void Cmd_Init( Abc_Frame_t * pAbc ) ...@@ -79,9 +79,7 @@ void Cmd_Init( Abc_Frame_t * pAbc )
pAbc->tAliases = st_init_table(strcmp, st_strhash); pAbc->tAliases = st_init_table(strcmp, st_strhash);
pAbc->tFlags = st_init_table(strcmp, st_strhash); pAbc->tFlags = st_init_table(strcmp, st_strhash);
pAbc->aHistory = Vec_PtrAlloc( 100 ); pAbc->aHistory = Vec_PtrAlloc( 100 );
#if defined(WIN32)
Cmd_HistoryRead( pAbc ); Cmd_HistoryRead( pAbc );
#endif
Cmd_CommandAdd( pAbc, "Basic", "time", CmdCommandTime, 0 ); Cmd_CommandAdd( pAbc, "Basic", "time", CmdCommandTime, 0 );
Cmd_CommandAdd( pAbc, "Basic", "echo", CmdCommandEcho, 0 ); Cmd_CommandAdd( pAbc, "Basic", "echo", CmdCommandEcho, 0 );
...@@ -124,9 +122,7 @@ void Cmd_End( Abc_Frame_t * pAbc ) ...@@ -124,9 +122,7 @@ void Cmd_End( Abc_Frame_t * pAbc )
{ {
st_generator * gen; st_generator * gen;
char * pKey, * pValue; char * pKey, * pValue;
#if defined(WIN32)
Cmd_HistoryWrite( pAbc, ABC_INFINITY ); Cmd_HistoryWrite( pAbc, ABC_INFINITY );
#endif
// st_free_table( pAbc->tCommands, (void (*)()) 0, CmdCommandFree ); // st_free_table( pAbc->tCommands, (void (*)()) 0, CmdCommandFree );
// st_free_table( pAbc->tAliases, (void (*)()) 0, CmdCommandAliasFree ); // st_free_table( pAbc->tAliases, (void (*)()) 0, CmdCommandAliasFree );
......
...@@ -47,8 +47,8 @@ ABC_NAMESPACE_IMPL_START ...@@ -47,8 +47,8 @@ ABC_NAMESPACE_IMPL_START
***********************************************************************/ ***********************************************************************/
void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command ) void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command )
{ {
int nLastLooked = 10; // defines how many entries back are looked int nLastLooked = 10; // do not add history if the same entry appears among the last entries
int nLastSaved = 100; // defines how many last entries are saved int nLastSaved = 100; // when saving a file, save no more than this number of last entries
char Buffer[ABC_MAX_STR]; char Buffer[ABC_MAX_STR];
int Len = strlen(command); int Len = strlen(command);
...@@ -61,9 +61,9 @@ void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command ) ...@@ -61,9 +61,9 @@ void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command )
strncmp(Buffer,"history",7) && strncmp(Buffer,"hi ", 3) && strcmp(Buffer,"hi") ) strncmp(Buffer,"history",7) && strncmp(Buffer,"hi ", 3) && strcmp(Buffer,"hi") )
{ {
char * pStr; char * pStr;
int i; int i, Start = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory) - nLastLooked );
// do not enter if the same command appears among the last five commands // do not enter if the same command appears among nLastLooked commands
Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Abc_MaxInt(0, Vec_PtrSize(p->aHistory)-nLastLooked) ) Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Start )
if ( !strcmp(pStr, Buffer) ) if ( !strcmp(pStr, Buffer) )
break; break;
if ( i == Vec_PtrSize(p->aHistory) ) if ( i == Vec_PtrSize(p->aHistory) )
...@@ -87,15 +87,13 @@ void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command ) ...@@ -87,15 +87,13 @@ void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command )
***********************************************************************/ ***********************************************************************/
void Cmd_HistoryRead( Abc_Frame_t * p ) void Cmd_HistoryRead( Abc_Frame_t * p )
{ {
#if defined(WIN32)
char Buffer[ABC_MAX_STR]; char Buffer[ABC_MAX_STR];
FILE * pFile; FILE * pFile;
assert( Vec_PtrSize(p->aHistory) == 0 ); assert( Vec_PtrSize(p->aHistory) == 0 );
pFile = fopen( "abc.history", "rb" ); pFile = fopen( "abc.history", "rb" );
if ( pFile == NULL ) if ( pFile == NULL )
{
// Abc_Print( 0, "Cannot open file \"abc.history\" for reading.\n" );
return; return;
}
while ( fgets( Buffer, ABC_MAX_STR, pFile ) != NULL ) while ( fgets( Buffer, ABC_MAX_STR, pFile ) != NULL )
{ {
int Len = strlen(Buffer); int Len = strlen(Buffer);
...@@ -104,6 +102,7 @@ void Cmd_HistoryRead( Abc_Frame_t * p ) ...@@ -104,6 +102,7 @@ void Cmd_HistoryRead( Abc_Frame_t * p )
Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) ); Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) );
} }
fclose( pFile ); fclose( pFile );
#endif
} }
/**Function************************************************************* /**Function*************************************************************
...@@ -119,6 +118,7 @@ void Cmd_HistoryRead( Abc_Frame_t * p ) ...@@ -119,6 +118,7 @@ void Cmd_HistoryRead( Abc_Frame_t * p )
***********************************************************************/ ***********************************************************************/
void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit ) void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit )
{ {
#if defined(WIN32)
FILE * pFile; FILE * pFile;
char * pStr; char * pStr;
int i; int i;
...@@ -132,6 +132,7 @@ void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit ) ...@@ -132,6 +132,7 @@ void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit )
Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit ) Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit )
fprintf( pFile, "%s\n", pStr ); fprintf( pFile, "%s\n", pStr );
fclose( pFile ); fclose( pFile );
#endif
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment