cmdHist.c 5.21 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [cmdHist.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures working with history.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: cmdHist.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

21 22
#include "base/abc/abc.h"
#include "base/main/mainInt.h"
Alan Mishchenko committed
23 24 25
#include "cmd.h"
#include "cmdInt.h"

26 27 28
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
29 30 31 32
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

33 34
#define ABC_USE_HISTORY 1

Alan Mishchenko committed
35
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
36
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47 48 49
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
50
void Cmd_HistoryAddCommand(    Abc_Frame_t * p, const char * command )
Alan Mishchenko committed
51
{
Alan Mishchenko committed
52 53
    int nLastLooked =   10;  // do not add history if the same entry appears among the last entries
    int nLastSaved  = 1000;  // when saving a file, save no more than this number of last entries
54
    char Buffer[ABC_MAX_STR];
55 56 57 58
    int Len;
    if ( p->fBatchMode )
        return;
    Len = strlen(command);
Alan Mishchenko committed
59
    strcpy( Buffer, command );
60 61
    if ( Buffer[Len-1] == '\n' )
        Buffer[Len-1] = 0;
62 63
    if ( strlen(Buffer) > 3 &&
         strncmp(Buffer,"set",3) && 
64
         strncmp(Buffer,"unset",5) && 
65
         strncmp(Buffer,"time",4) && 
66
         strncmp(Buffer,"quit",4) && 
67
         strncmp(Buffer,"alias",5) && 
68
//         strncmp(Buffer,"source",6) && 
69 70
         strncmp(Buffer,"history",7) && strncmp(Buffer,"hi ", 3) && strcmp(Buffer,"hi") &&
         Buffer[strlen(Buffer)-1] != '?' )
71
    {
Alan Mishchenko committed
72
        char * pStr = NULL;
73 74 75
        int i, Start = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory) - nLastLooked );
        // do not enter if the same command appears among nLastLooked commands
        Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Start )
76 77 78
            if ( !strcmp(pStr, Buffer) )
                break;
        if ( i == Vec_PtrSize(p->aHistory) )
79
        { // add new entry
80
            Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) );
81 82
            Cmd_HistoryWrite( p, nLastSaved );
        }
83 84 85 86 87
        else
        { // put at the end
            Vec_PtrRemove( p->aHistory, pStr );
            Vec_PtrPush( p->aHistory, pStr );
        }
88
    }
Alan Mishchenko committed
89 90
}

91 92 93 94 95 96 97 98 99 100 101 102 103
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cmd_HistoryRead( Abc_Frame_t * p )
{
104
#if defined(WIN32) && defined(ABC_USE_HISTORY)
105
    char Buffer[ABC_MAX_STR];
106 107 108 109 110
    FILE * pFile;
    assert( Vec_PtrSize(p->aHistory) == 0 );
    pFile = fopen( "abc.history", "rb" );
    if ( pFile == NULL )
        return;
111 112 113 114 115
    while ( fgets( Buffer, ABC_MAX_STR, pFile ) != NULL )
    {
        int Len = strlen(Buffer);
        if ( Buffer[Len-1] == '\n' )
            Buffer[Len-1] = 0;
116
        Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) );
117
    }
118
    fclose( pFile );
119
#endif
120 121 122 123 124 125 126 127 128 129 130 131 132
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
133
void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit )
134
{
135
#if defined(WIN32) && defined(ABC_USE_HISTORY)
136
    FILE * pFile;
137
    char * pStr; 
138 139 140 141 142 143 144
    int i;
    pFile = fopen( "abc.history", "wb" );
    if ( pFile == NULL )
    {
        Abc_Print( 0, "Cannot open file \"abc.history\" for writing.\n" );
        return;
    }
145 146
    Limit = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory)-Limit );
    Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit )
147
        fprintf( pFile, "%s\n", pStr );
148
    fclose( pFile );
149
#endif
150 151
}

152 153 154 155 156 157 158 159 160 161 162 163 164
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cmd_HistoryPrint( Abc_Frame_t * p, int Limit )
{
165
#if defined(WIN32) && defined(ABC_USE_HISTORY) 
166 167 168 169 170 171 172 173 174 175
    char * pStr; 
    int i;
    Limit = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory)-Limit );
    printf( "================== Command history ==================\n" );
    Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit )
        printf( "%s\n", pStr );
    printf( "=====================================================\n" );
#endif
}

Alan Mishchenko committed
176 177 178
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////
179 180
ABC_NAMESPACE_IMPL_END