abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cmdHist.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [cmdHist.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures working with history.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: cmdHist.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "base/abc/abc.h"
22 #include "base/main/mainInt.h"
23 #include "cmd.h"
24 #include "cmdInt.h"
25 
27 
28 
29 ////////////////////////////////////////////////////////////////////////
30 /// DECLARATIONS ///
31 ////////////////////////////////////////////////////////////////////////
32 
33 ////////////////////////////////////////////////////////////////////////
34 /// FUNCTION DEFINITIONS ///
35 ////////////////////////////////////////////////////////////////////////
36 
37 /**Function*************************************************************
38 
39  Synopsis []
40 
41  Description []
42 
43  SideEffects []
44 
45  SeeAlso []
46 
47 ***********************************************************************/
48 void Cmd_HistoryAddCommand( Abc_Frame_t * p, const char * command )
49 {
50  int nLastLooked = 10; // do not add history if the same entry appears among the last entries
51  int nLastSaved = 1000; // when saving a file, save no more than this number of last entries
52  char Buffer[ABC_MAX_STR];
53  int Len;
54  if ( p->fBatchMode )
55  return;
56  Len = strlen(command);
57  strcpy( Buffer, command );
58  if ( Buffer[Len-1] == '\n' )
59  Buffer[Len-1] = 0;
60  if ( strlen(Buffer) > 3 &&
61  strncmp(Buffer,"set",3) &&
62  strncmp(Buffer,"unset",5) &&
63  strncmp(Buffer,"time",4) &&
64  strncmp(Buffer,"quit",4) &&
65  strncmp(Buffer,"alias",5) &&
66 // strncmp(Buffer,"source",6) &&
67  strncmp(Buffer,"history",7) && strncmp(Buffer,"hi ", 3) && strcmp(Buffer,"hi") &&
68  Buffer[strlen(Buffer)-1] != '?' )
69  {
70  char * pStr = NULL;
71  int i, Start = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory) - nLastLooked );
72  // do not enter if the same command appears among nLastLooked commands
73  Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Start )
74  if ( !strcmp(pStr, Buffer) )
75  break;
76  if ( i == Vec_PtrSize(p->aHistory) )
77  { // add new entry
78  Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) );
79  Cmd_HistoryWrite( p, nLastSaved );
80  }
81  else
82  { // put at the end
83  Vec_PtrRemove( p->aHistory, pStr );
84  Vec_PtrPush( p->aHistory, pStr );
85  }
86  }
87 }
88 
89 /**Function*************************************************************
90 
91  Synopsis []
92 
93  Description []
94 
95  SideEffects []
96 
97  SeeAlso []
98 
99 ***********************************************************************/
101 {
102 #if defined(WIN32)
103  char Buffer[ABC_MAX_STR];
104  FILE * pFile;
105  assert( Vec_PtrSize(p->aHistory) == 0 );
106  pFile = fopen( "abc.history", "rb" );
107  if ( pFile == NULL )
108  return;
109  while ( fgets( Buffer, ABC_MAX_STR, pFile ) != NULL )
110  {
111  int Len = strlen(Buffer);
112  if ( Buffer[Len-1] == '\n' )
113  Buffer[Len-1] = 0;
114  Vec_PtrPush( p->aHistory, Extra_UtilStrsav(Buffer) );
115  }
116  fclose( pFile );
117 #endif
118 }
119 
120 /**Function*************************************************************
121 
122  Synopsis []
123 
124  Description []
125 
126  SideEffects []
127 
128  SeeAlso []
129 
130 ***********************************************************************/
131 void Cmd_HistoryWrite( Abc_Frame_t * p, int Limit )
132 {
133 #if defined(WIN32)
134  FILE * pFile;
135  char * pStr;
136  int i;
137  pFile = fopen( "abc.history", "wb" );
138  if ( pFile == NULL )
139  {
140  Abc_Print( 0, "Cannot open file \"abc.history\" for writing.\n" );
141  return;
142  }
143  Limit = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory)-Limit );
144  Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit )
145  fprintf( pFile, "%s\n", pStr );
146  fclose( pFile );
147 #endif
148 }
149 
150 /**Function*************************************************************
151 
152  Synopsis []
153 
154  Description []
155 
156  SideEffects []
157 
158  SeeAlso []
159 
160 ***********************************************************************/
161 void Cmd_HistoryPrint( Abc_Frame_t * p, int Limit )
162 {
163 #if defined(WIN32)
164  char * pStr;
165  int i;
166  Limit = Abc_MaxInt( 0, Vec_PtrSize(p->aHistory)-Limit );
167  printf( "================== Command history ==================\n" );
168  Vec_PtrForEachEntryStart( char *, p->aHistory, pStr, i, Limit )
169  printf( "%s\n", pStr );
170  printf( "=====================================================\n" );
171 #endif
172 }
173 
174 ////////////////////////////////////////////////////////////////////////
175 /// END OF FILE ///
176 ////////////////////////////////////////////////////////////////////////
178 
#define Vec_PtrForEachEntryStart(Type, vVec, pEntry, i, Start)
Definition: vecPtr.h:57
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
static void Vec_PtrRemove(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:714
void Cmd_HistoryWrite(Abc_Frame_t *p, int Limit)
Definition: cmdHist.c:131
char * Extra_UtilStrsav(const char *s)
int strcmp()
void Cmd_HistoryPrint(Abc_Frame_t *p, int Limit)
Definition: cmdHist.c:161
typedefABC_NAMESPACE_HEADER_START struct Abc_Frame_t_ Abc_Frame_t
INCLUDES ///.
#define ABC_MAX_STR
Definition: mainInt.h:50
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define Len
Definition: deflate.h:78
void Cmd_HistoryRead(Abc_Frame_t *p)
Definition: cmdHist.c:100
static void Abc_Print(int level, const char *format,...)
Definition: abc_global.h:313
char * strcpy()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
int strncmp()
#define assert(ex)
Definition: util_old.h:213
int strlen()
ABC_NAMESPACE_IMPL_START void Cmd_HistoryAddCommand(Abc_Frame_t *p, const char *command)
DECLARATIONS ///.
Definition: cmdHist.c:48