mainUtils.c 9.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile****************************************************************

  FileName    [mainUtils.c]

  SystemName  [ABC: Logic synthesis and verification system.]
 
  PackageName [The main package.]

  Synopsis    [Miscellaneous utilities.]
10
 
Alan Mishchenko committed
11 12 13 14 15 16 17 18 19 20
  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21
#include "base/abc/abc.h"
Alan Mishchenko committed
22 23
#include "mainInt.h"

Alan Mishchenko committed
24
#ifdef ABC_USE_READLINE
Alan Mishchenko committed
25
#include <readline/readline.h>
Alan Mishchenko committed
26
#include <readline/history.h>
Alan Mishchenko committed
27 28
#endif

29 30
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
31 32 33
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
34 35

static char * DateReadFromDateString( char * datestr );
Alan Mishchenko committed
36 37

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
38
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Abc_UtilsGetVersion( Abc_Frame_t * pAbc )
{
    static char Version[1000];
55 56 57 58
#if __GNUC__
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wdate-time"
#endif
Alan Mishchenko committed
59
    sprintf(Version, "%s (compiled %s %s)", ABC_VERSION, __DATE__, __TIME__);
60 61 62
#if __GNUC__
    #pragma GCC diagnostic pop
#endif
Alan Mishchenko committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    return Version;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Abc_UtilsGetUsersInput( Abc_Frame_t * pAbc )
{
79
    static char Prompt[5000];
Alan Mishchenko committed
80
    sprintf( Prompt, "abc %02d> ", pAbc->nSteps );
Alan Mishchenko committed
81
#ifdef ABC_USE_READLINE
82 83
    {
    static char * line = NULL;
Alan Mishchenko committed
84
    if (line != NULL) ABC_FREE(line);
Alan Mishchenko committed
85 86 87 88
    line = readline(Prompt);  
    if (line == NULL){ printf("***EOF***\n"); exit(0); }
    add_history(line);
    return line;
89 90
    }
#else
Alan Mishchenko committed
91 92
    {
    char * pRetValue;
93
    fprintf( pAbc->Out, "%s", Prompt );
Alan Mishchenko committed
94
    pRetValue = fgets( Prompt, 5000, stdin );
95
    return Prompt;
Alan Mishchenko committed
96
    }
Alan Mishchenko committed
97
#endif
Alan Mishchenko committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_UtilsPrintHello( Abc_Frame_t * pAbc )
{
    fprintf( pAbc->Out, "%s\n", pAbc->sVersion );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_UtilsPrintUsage( Abc_Frame_t * pAbc, char * ProgName )
{
    fprintf( pAbc->Err, "\n" );
    fprintf( pAbc->Err,
131
             "usage: %s [-c cmd] [-q cmd] [-C cmd] [-Q cmd] [-f script] [-h] [-o file] [-s] [-t type] [-T type] [-x] [-b] [file]\n",
Alan Mishchenko committed
132 133
             ProgName);
    fprintf( pAbc->Err, "    -c cmd\texecute commands `cmd'\n");
134 135
    fprintf( pAbc->Err, "    -q cmd\texecute commands `cmd' quietly\n");
    fprintf( pAbc->Err, "    -C cmd\texecute commands `cmd', then continue in interactive mode\n");
136
    fprintf( pAbc->Err, "    -Q cmd\texecute commands `cmd' quietly, then continue in interactive mode\n");
Alan Mishchenko committed
137 138 139 140 141 142 143 144
    fprintf( pAbc->Err, "    -F script\texecute commands from a script file and echo commands\n");
    fprintf( pAbc->Err, "    -f script\texecute commands from a script file\n");
    fprintf( pAbc->Err, "    -h\t\tprint the command usage\n");
    fprintf( pAbc->Err, "    -o file\tspecify output filename to store the result\n");
    fprintf( pAbc->Err, "    -s\t\tdo not read any initialization file\n");
    fprintf( pAbc->Err, "    -t type\tspecify input type (blif_mv (default), blif_mvs, blif, or none)\n");
    fprintf( pAbc->Err, "    -T type\tspecify output type (blif_mv (default), blif_mvs, blif, or none)\n");
    fprintf( pAbc->Err, "    -x\t\tequivalent to '-t none -T none'\n");
145
    fprintf( pAbc->Err, "    -b\t\trunning in bridge mode\n");
Alan Mishchenko committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    fprintf( pAbc->Err, "\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_UtilsSource( Abc_Frame_t * pAbc )
{  
#ifdef WIN32
163
    if ( Cmd_CommandExecute(pAbc, "source abc.rc") )
Alan Mishchenko committed
164 165 166 167 168 169 170
    {
        if ( Cmd_CommandExecute(pAbc, "source ..\\abc.rc") == 0 )
            printf( "Loaded \"abc.rc\" from the parent directory.\n" );
        else if ( Cmd_CommandExecute(pAbc, "source ..\\..\\abc.rc") == 0 )
            printf( "Loaded \"abc.rc\" from the grandparent directory.\n" );
    }
#else
Alan Mishchenko committed
171 172

#if 0
Alan Mishchenko committed
173
    {
Alan Mishchenko committed
174
        char * sPath1, * sPath2;
Alan Mishchenko committed
175

Alan Mishchenko committed
176 177 178 179
         // If .rc is present in both the home and current directories, then read
         // it from the home directory.  Otherwise, read it from wherever it's located.
        sPath1 = Extra_UtilFileSearch(".rc", "~/", "r");
        sPath2 = Extra_UtilFileSearch(".rc", ".",  "r");
Alan Mishchenko committed
180
  
Alan Mishchenko committed
181 182
        if ( sPath1 && sPath2 ) {
            /* ~/.rc == .rc : Source the file only once */
Alan Mishchenko committed
183 184
            (void) Cmd_CommandExecute(pAbc, "source -s ~/.rc");
        }
Alan Mishchenko committed
185 186 187 188 189 190 191
        else {
            if (sPath1) {
                (void) Cmd_CommandExecute(pAbc, "source -s ~/.rc");
            }
            if (sPath2) {
                (void) Cmd_CommandExecute(pAbc, "source -s .rc");
            }
Alan Mishchenko committed
192
        }
Alan Mishchenko committed
193 194
        if ( sPath1 ) ABC_FREE(sPath1);
        if ( sPath2 ) ABC_FREE(sPath2);
Alan Mishchenko committed
195
    
Alan Mishchenko committed
196 197 198 199 200 201 202 203 204 205 206 207 208
        /* execute the abc script which can be open with the "open_path" */
        Cmd_CommandExecute( pAbc, "source -s abc.rc" );
    }
#endif

    {
        char * sPath1, * sPath2;
        char * home;

         // If .rc is present in both the home and current directories, then read
         // it from the home directory.  Otherwise, read it from wherever it's located.
        home = getenv("HOME");
        if (home){
Alan Mishchenko committed
209
            char * sPath3 = ABC_ALLOC(char, strlen(home) + 2);
Alan Mishchenko committed
210 211
            (void) sprintf(sPath3, "%s/", home);
            sPath1 = Extra_UtilFileSearch(".abc.rc", sPath3, "r");
Alan Mishchenko committed
212
            ABC_FREE(sPath3);
Alan Mishchenko committed
213 214 215 216 217 218 219
        }else
            sPath1 = NULL;

        sPath2 = Extra_UtilFileSearch(".abc.rc", ".",  "r");

        if ( sPath1 && sPath2 ) {
            /* ~/.rc == .rc : Source the file only once */
Alan Mishchenko committed
220
            char *tmp_cmd = ABC_ALLOC(char, strlen(sPath1)+12);
Alan Mishchenko committed
221 222 223
            (void) sprintf(tmp_cmd, "source -s %s", sPath1);
            // (void) Cmd_CommandExecute(pAbc, "source -s ~/.abc.rc");
            (void) Cmd_CommandExecute(pAbc, tmp_cmd);
Alan Mishchenko committed
224
            ABC_FREE(tmp_cmd);
Alan Mishchenko committed
225 226 227
        }
        else {
            if (sPath1) {
Alan Mishchenko committed
228
                char *tmp_cmd = ABC_ALLOC(char, strlen(sPath1)+12);
Alan Mishchenko committed
229 230 231
                (void) sprintf(tmp_cmd, "source -s %s", sPath1);
                // (void) Cmd_CommandExecute(pAbc, "source -s ~/.abc.rc");
                (void) Cmd_CommandExecute(pAbc, tmp_cmd);
Alan Mishchenko committed
232
                ABC_FREE(tmp_cmd);
Alan Mishchenko committed
233 234
            }
            if (sPath2) {
Alan Mishchenko committed
235
                char *tmp_cmd = ABC_ALLOC(char, strlen(sPath2)+12);
Alan Mishchenko committed
236 237 238
                (void) sprintf(tmp_cmd, "source -s %s", sPath2);
                // (void) Cmd_CommandExecute(pAbc, "source -s .abc.rc");
                (void) Cmd_CommandExecute(pAbc, tmp_cmd);
Alan Mishchenko committed
239
                ABC_FREE(tmp_cmd);
Alan Mishchenko committed
240 241
            }
        }
Alan Mishchenko committed
242 243
        if ( sPath1 ) ABC_FREE(sPath1);
        if ( sPath2 ) ABC_FREE(sPath2);
Alan Mishchenko committed
244 245 246

        /* execute the abc script which can be open with the "open_path" */
        Cmd_CommandExecute( pAbc, "source -s abc.rc" );
Alan Mishchenko committed
247
    }
248
    
Alan Mishchenko committed
249 250 251 252 253 254 255 256 257 258 259 260 261
#endif //WIN32
}

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

  Synopsis    [Returns the date in a brief format assuming its coming from
  the program `date'.]

  Description [optional]

  SideEffects []

******************************************************************************/
262
char * DateReadFromDateString( char * datestr )
Alan Mishchenko committed
263
{
alanminko committed
264
  static char result[100];
Alan Mishchenko committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  char        day[10];
  char        month[10];
  char        zone[10];
  char       *at;
  int         date;
  int         hour;
  int         minute;
  int         second;
  int         year;

  if (sscanf(datestr, "%s %s %2d %2d:%2d:%2d %s %4d",
             day, month, &date, &hour, &minute, &second, zone, &year) == 8) {
    if (hour >= 12) {
      if (hour >= 13) hour -= 12;
      at = "PM";
    }
    else {
      if (hour == 0) hour = 12;
      at = "AM";
    }
    (void) sprintf(result, "%d-%3s-%02d at %d:%02d %s", 
                   date, month, year % 100, hour, minute, at);
    return result;
  }
  else {
    return datestr;
  }
}



////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


301
ABC_NAMESPACE_IMPL_END