cmd.c 62.7 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
/**CFile****************************************************************

  FileName    [cmd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Command file.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

***********************************************************************/
20

Alan Mishchenko committed
21
#ifdef WIN32
22
#include <process.h>
Alan Mishchenko committed
23 24
#else
#include <unistd.h>
Alan Mishchenko committed
25 26
#endif

27 28
#include "base/abc/abc.h"
#include "base/main/mainInt.h"
Alan Mishchenko committed
29
#include "cmdInt.h"
30
#include "misc/util/utilSignal.h"
31 32

ABC_NAMESPACE_IMPL_START
Alan Mishchenko committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static int CmdCommandTime          ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandEcho          ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandQuit          ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandWhich         ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandHistory       ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandAlias         ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandUnalias       ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandHelp          ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandSource        ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandSetVariable   ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandUnsetVariable ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandUndo          ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandRecall        ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandEmpty         ( Abc_Frame_t * pAbc, int argc, char ** argv );
52
#if defined(WIN32) && !defined(__cplusplus)
Alan Mishchenko committed
53
static int CmdCommandLs            ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
54
static int CmdCommandScrGen        ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
55
#endif
Alan Mishchenko committed
56
static int CmdCommandVersion       ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
57 58
static int CmdCommandSis           ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int CmdCommandMvsis         ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
59
static int CmdCommandCapo          ( Abc_Frame_t * pAbc, int argc, char ** argv );
60
static int CmdCommandStarter       ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
61

62 63
extern int Cmd_CommandAbcLoadPlugIn( Abc_Frame_t * pAbc, int argc, char ** argv );

Alan Mishchenko committed
64
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
65
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
66 67 68 69 70 71 72 73 74 75 76 77
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Initializes the command package.]

  SideEffects [Commands are added to the command table.]

  SeeAlso     [Cmd_End]

******************************************************************************/
void Cmd_Init( Abc_Frame_t * pAbc )
78
{
79 80 81
    pAbc->tCommands = st__init_table(strcmp, st__strhash);
    pAbc->tAliases  = st__init_table(strcmp, st__strhash);
    pAbc->tFlags    = st__init_table(strcmp, st__strhash);
Alan Mishchenko committed
82
    pAbc->aHistory  = Vec_PtrAlloc( 100 );
83
    Cmd_HistoryRead( pAbc );
Alan Mishchenko committed
84

85 86 87 88 89 90 91 92 93 94
    Cmd_CommandAdd( pAbc, "Basic", "time",          CmdCommandTime,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "echo",          CmdCommandEcho,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "quit",          CmdCommandQuit,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "history",       CmdCommandHistory,         0 );
    Cmd_CommandAdd( pAbc, "Basic", "alias",         CmdCommandAlias,           0 );
    Cmd_CommandAdd( pAbc, "Basic", "unalias",       CmdCommandUnalias,         0 );
    Cmd_CommandAdd( pAbc, "Basic", "help",          CmdCommandHelp,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "source",        CmdCommandSource,          0 );
    Cmd_CommandAdd( pAbc, "Basic", "set",           CmdCommandSetVariable,     0 );
    Cmd_CommandAdd( pAbc, "Basic", "unset",         CmdCommandUnsetVariable,   0 );
95 96 97
    Cmd_CommandAdd( pAbc, "Basic", "undo",          CmdCommandUndo,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "recall",        CmdCommandRecall,          0 );
    Cmd_CommandAdd( pAbc, "Basic", "empty",         CmdCommandEmpty,           0 );
98 99 100
#if defined(WIN32) && !defined(__cplusplus)
    Cmd_CommandAdd( pAbc, "Basic", "ls",            CmdCommandLs,              0 );
    Cmd_CommandAdd( pAbc, "Basic", "scrgen",        CmdCommandScrGen,          0 );
Alan Mishchenko committed
101
#endif
102
    Cmd_CommandAdd( pAbc, "Basic", "version",       CmdCommandVersion,         0 );
103

104 105 106
    Cmd_CommandAdd( pAbc, "Various", "sis",         CmdCommandSis,             1 );
    Cmd_CommandAdd( pAbc, "Various", "mvsis",       CmdCommandMvsis,           1 );
    Cmd_CommandAdd( pAbc, "Various", "capo",        CmdCommandCapo,            0 );
107
    Cmd_CommandAdd( pAbc, "Various", "starter",     CmdCommandStarter,         0 );
Alan Mishchenko committed
108

109
    Cmd_CommandAdd( pAbc, "Various", "load_plugin", Cmd_CommandAbcLoadPlugIn,  0 );
Alan Mishchenko committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

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

  Synopsis    [Ends the command package.]

  Description [Ends the command package. Tables are freed.]

  SideEffects []

  SeeAlso     []

******************************************************************************/
void Cmd_End( Abc_Frame_t * pAbc )
{
125
    st__generator * gen;
Alan Mishchenko committed
126
    char * pKey, * pValue;
127
    Cmd_HistoryWrite( pAbc, ABC_INFINITY );
Alan Mishchenko committed
128

129 130 131
//    st__free_table( pAbc->tCommands, (void (*)()) 0, CmdCommandFree );
//    st__free_table( pAbc->tAliases,  (void (*)()) 0, CmdCommandAliasFree );
//    st__free_table( pAbc->tFlags,    free, free );
Alan Mishchenko committed
132

133
    st__foreach_item( pAbc->tCommands, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
134
        CmdCommandFree( (Abc_Command *)pValue );
135
    st__free_table( pAbc->tCommands );
Alan Mishchenko committed
136

137
    st__foreach_item( pAbc->tAliases, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
138
        CmdCommandAliasFree( (Abc_Alias *)pValue );
139
    st__free_table( pAbc->tAliases );
Alan Mishchenko committed
140

141
    st__foreach_item( pAbc->tFlags, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
142
        ABC_FREE( pKey ), ABC_FREE( pValue );
143
    st__free_table( pAbc->tFlags );
Alan Mishchenko committed
144

145
    Vec_PtrFreeFree( pAbc->aHistory );
Alan Mishchenko committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}



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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandTime( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int c;
Alan Mishchenko committed
164
    int fClear;
Alan Mishchenko committed
165

Alan Mishchenko committed
166
    fClear = 0;
Alan Mishchenko committed
167
    Extra_UtilGetoptReset();
Alan Mishchenko committed
168
    while ( ( c = Extra_UtilGetopt( argc, argv, "ch" ) ) != EOF )
Alan Mishchenko committed
169 170 171
    {
        switch ( c )
        {
Alan Mishchenko committed
172 173 174
        case 'c':
            fClear ^= 1;
            break;
Alan Mishchenko committed
175 176 177 178 179 180 181
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
182 183 184 185 186 187 188
    if ( fClear )
    {
        pAbc->TimeTotal += pAbc->TimeCommand;
        pAbc->TimeCommand = 0.0;
        return 0;
    }

Alan Mishchenko committed
189
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
190 191 192 193
    {
        goto usage;
    }

Alan Mishchenko committed
194

Alan Mishchenko committed
195
    pAbc->TimeTotal += pAbc->TimeCommand;
196
    fprintf( pAbc->Out, "elapse: %3.2f seconds, total: %3.2f seconds\n",
Alan Mishchenko committed
197
        pAbc->TimeCommand, pAbc->TimeTotal );
Alan Mishchenko committed
198 199 200 201
/*
    {
        FILE * pTable;
        pTable = fopen( "runtimes.txt", "a+" );
Alan Mishchenko committed
202
        fprintf( pTable, "%4.2f\n", pAbc->TimeCommand );
Alan Mishchenko committed
203 204 205
        fclose( pTable );
    }
*/
Alan Mishchenko committed
206
    pAbc->TimeCommand = 0.0;
Alan Mishchenko committed
207 208 209
    return 0;

  usage:
Alan Mishchenko committed
210 211 212
    fprintf( pAbc->Err, "usage: time [-ch]\n" );
    fprintf( pAbc->Err, "      \t\tprint the runtime since the last call\n" );
    fprintf( pAbc->Err, "   -c \t\tclears the elapsed time without printing it\n" );
Alan Mishchenko committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    fprintf( pAbc->Err, "   -h \t\tprint the command usage\n" );
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandEcho( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int i;
    int c;
    int n = 1;

Alan Mishchenko committed
234 235
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "hn" ) ) != EOF )
Alan Mishchenko committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
    {
        switch ( c )
        {
        case 'n':
            n = 0;
            break;
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

250 251 252 253 254 255 256 257 258 259 260 261
    if (pAbc->Out == stdout){
        for ( i = globalUtilOptind; i < argc; i++ )
            Abc_Print( 1, "%s ", argv[i] );
        if ( n )
            Abc_Print( 1, "\n" );

    }else{
        for ( i = globalUtilOptind; i < argc; i++ )
            fprintf( pAbc->Out, "%s ", argv[i] );
        if ( n )
            fprintf( pAbc->Out, "\n" );

Alan Mishchenko committed
262
        fflush ( pAbc->Out );
263
    }
Alan Mishchenko committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    return 0;

  usage:
    fprintf( pAbc->Err, "usage: echo [-h] string \n" );
    fprintf( pAbc->Err, "   -n \t\tsuppress newline at the end\n" );
    fprintf( pAbc->Err, "   -h \t\tprint the command usage\n" );
    return ( 1 );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandQuit( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int c;

Alan Mishchenko committed
288 289
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "hs" ) ) != EOF )
Alan Mishchenko committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        case 's':
            return -2;
            break;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
304
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        goto usage;
    return -1;

  usage:
    fprintf( pAbc->Err, "usage: quit [-h] [-s]\n" );
    fprintf( pAbc->Err, "   -h  print the command usage\n" );
    fprintf( pAbc->Err,
                      "   -s  frees all the memory before quitting\n" );
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandWhich( Abc_Frame_t * pAbc, int argc, char **argv )
{
    return 0;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandHistory( Abc_Frame_t * pAbc, int argc, char **argv )
{
345
    char * pName;
346
    int i, c;
347
    int nPrints = 25;
348
    int iRepeat = -1;
Alan Mishchenko committed
349
    Extra_UtilGetoptReset();
350
    while ( ( c = Extra_UtilGetopt( argc, argv, "Nh" ) ) != EOF )
Alan Mishchenko committed
351 352 353
    {
        switch ( c )
        {
354 355 356 357 358 359 360 361 362 363 364
            case 'N':
                if ( globalUtilOptind >= argc )
                {
                    Abc_Print( -1, "Command line switch \"-N\" should be followed by an integer.\n" );
                    goto usage;
                }
                nPrints = atoi(argv[globalUtilOptind]);
                globalUtilOptind++;
                if ( nPrints < 0 ) 
                    goto usage;
                break;
Alan Mishchenko committed
365 366
            case 'h':
                goto usage;
Alan Mishchenko committed
367 368
            default :
                goto usage;
Alan Mishchenko committed
369 370
        }
    }
371 372
//    if ( argc > globalUtilOptind + 1 )
    if ( argc >= globalUtilOptind + 1 )
Alan Mishchenko committed
373
        goto usage;
374 375 376
    // get the number from the command line
//    if ( argc == globalUtilOptind + 1 )
//        iRepeat = atoi(argv[globalUtilOptind]);
Alan Mishchenko committed
377
    // print the commands
378
    if ( iRepeat >= 0 && iRepeat < Vec_PtrSize(pAbc->aHistory) )
Alan Mishchenko committed
379
        fprintf( pAbc->Out, "%s", (char *)Vec_PtrEntry(pAbc->aHistory, Vec_PtrSize(pAbc->aHistory)-1-iRepeat) );
380 381 382
    else if ( nPrints > 0 )
        Vec_PtrForEachEntryStart( char *, pAbc->aHistory, pName, i, Abc_MaxInt(0, Vec_PtrSize(pAbc->aHistory)-nPrints) )
            fprintf( pAbc->Out, "%2d : %s\n", Vec_PtrSize(pAbc->aHistory)-i, pName );
Alan Mishchenko committed
383 384
    return 0;

Alan Mishchenko committed
385
usage:
386 387 388
    fprintf( pAbc->Err, "usage: history [-N <num>] [-h]\n" );
    fprintf( pAbc->Err, "          lists the last commands entered on the command line\n" );
    fprintf( pAbc->Err, " -N num : the maximum number of entries to show [default = %d]\n", nPrints );
Alan Mishchenko committed
389
    fprintf( pAbc->Err, " -h     : print the command usage\n" );
390
//    fprintf( pAbc->Err, " <this> : the history entry to repeat to the command line\n" );
Alan Mishchenko committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    return ( 1 );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandAlias( Abc_Frame_t * pAbc, int argc, char **argv )
{
407 408
    const char *key;
    char *value;
Alan Mishchenko committed
409 410
    int c;

Alan Mishchenko committed
411 412
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }


    if ( argc == 1 )
    {
Alan Mishchenko committed
427
        CmdPrintTable( pAbc->tAliases, 1 );
Alan Mishchenko committed
428 429 430 431 432
        return 0;

    }
    else if ( argc == 2 )
    {
433
        if ( st__lookup( pAbc->tAliases, argv[1], &value ) )
Alan Mishchenko committed
434 435 436 437
            CmdCommandAliasPrint( pAbc, ( Abc_Alias * ) value );
        return 0;
    }

438
    // delete any existing alias
Alan Mishchenko committed
439
    key = argv[1];
440
    if ( st__delete( pAbc->tAliases, &key, &value ) )
Alan Mishchenko committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        CmdCommandAliasFree( ( Abc_Alias * ) value );
    CmdCommandAliasAdd( pAbc, argv[1], argc - 2, argv + 2 );
    return 0;

usage:
    fprintf( pAbc->Err, "usage: alias [-h] [command [string]]\n" );
    fprintf( pAbc->Err, "   -h \t\tprint the command usage\n" );
    return ( 1 );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandUnalias( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int i;
465 466
    const char *key;
    char *value;
Alan Mishchenko committed
467 468
    int c;

Alan Mishchenko committed
469 470
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

    if ( argc < 2 )
    {
        goto usage;
    }

    for ( i = 1; i < argc; i++ )
    {
        key = argv[i];
490
        if ( st__delete( pAbc->tAliases, &key, &value ) )
Alan Mishchenko committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        {
            CmdCommandAliasFree( ( Abc_Alias * ) value );
        }
    }
    return 0;

  usage:
    fprintf( pAbc->Err, "usage: unalias [-h] alias_names\n" );
    fprintf( pAbc->Err, "   -h \t\tprint the command usage\n" );
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandHelp( Abc_Frame_t * pAbc, int argc, char **argv )
{
516
    int fPrintAll;
Alan Mishchenko committed
517 518 519
    int c;

    fPrintAll = 0;
Alan Mishchenko committed
520 521
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "ah" ) ) != EOF )
Alan Mishchenko committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    {
        switch ( c )
        {
        case 'a':
            case 'v':
                fPrintAll ^= 1;
                break;
            break;
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
538
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
        goto usage;

    CmdCommandPrint( pAbc, fPrintAll );
    return 0;

usage:
    fprintf( pAbc->Err, "usage: help [-a] [-h]\n" );
    fprintf( pAbc->Err, "       prints the list of available commands by group\n" );
    fprintf( pAbc->Err, " -a       toggle printing hidden commands [default = %s]\n", fPrintAll? "yes": "no" );
    fprintf( pAbc->Err, " -h       print the command usage\n" );
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandSource( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int c, echo, prompt, silent, interactive, quit_count, lp_count;
    int status = 0;             /* initialize so that lint doesn't complain */
    int lp_file_index, did_subst;
568
    char *prompt_string, *real_filename, line[ABC_MAX_STR], *command;
Alan Mishchenko committed
569 570 571 572
    FILE *fp;

    interactive = silent = prompt = echo = 0;

Alan Mishchenko committed
573 574
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "ipsxh" ) ) != EOF )
Alan Mishchenko committed
575 576 577 578 579 580 581
    {
        switch ( c )
        {
        case 'i':               /* a hack to distinguish EOF from stdin */
            interactive = 1;
            break;
        case 'p':
Alan Mishchenko committed
582
            prompt ^= 1;
Alan Mishchenko committed
583 584
            break;
        case 's':
Alan Mishchenko committed
585
            silent ^= 1;
Alan Mishchenko committed
586 587 588 589
            break;
        case 'x':
            echo ^= 1;
            break;
Alan Mishchenko committed
590 591
        case 'h':
            goto usage;
Alan Mishchenko committed
592 593 594 595 596 597
        default:
            goto usage;
        }
    }

    /* added to avoid core-dumping when no script file is specified */
Alan Mishchenko committed
598
    if ( argc == globalUtilOptind )
Alan Mishchenko committed
599 600 601 602
    {
        goto usage;
    }

Alan Mishchenko committed
603
    lp_file_index = globalUtilOptind;
Alan Mishchenko committed
604 605 606 607 608 609 610
    lp_count = 0;

    /*
     * FIX (Tom, 5/7/95):  I'm not sure what the purpose of this outer do loop
     * is. In particular, lp_file_index is never modified in the loop, so it
     * looks it would just read the same file over again.  Also, SIS had
     * lp_count initialized to -1, and hence, any file sourced by SIS (if -l or
Alan Mishchenko committed
611
     * -t options on "source" were used in SIS) would actually be executed twice.
Alan Mishchenko committed
612
     */
613
    pAbc->fSource = 1;
Alan Mishchenko committed
614 615
    do
    {
Alan Mishchenko committed
616 617 618 619 620 621 622 623 624
        char * pFileName, * pTemp;

        // get the input file name
        pFileName = argv[lp_file_index];
        // fix the wrong symbol
        for ( pTemp = pFileName; *pTemp; pTemp++ )
            if ( *pTemp == '>' )
                *pTemp = '\\';

Alan Mishchenko committed
625 626
        lp_count++;             /* increment the loop counter */

Alan Mishchenko committed
627
        fp = CmdFileOpen( pAbc, pFileName, "r", &real_filename, silent );
Alan Mishchenko committed
628 629
        if ( fp == NULL )
        {
630
            pAbc->fSource = 0;
Alan Mishchenko committed
631
            ABC_FREE( real_filename );
Alan Mishchenko committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            return !silent;     /* error return if not silent */
        }

        quit_count = 0;
        do
        {
            if ( prompt )
            {
                prompt_string = Cmd_FlagReadByName( pAbc, "prompt" );
                if ( prompt_string == NULL )
                    prompt_string = "abc> ";

            }
            else
            {
Alan Mishchenko committed
647
                prompt_string = NULL;
Alan Mishchenko committed
648 649 650 651 652 653
            }

            /* clear errors -- e.g., EOF reached from stdin */
            clearerr( fp );

            /* read another command line */
654
            if ( fgets( line, ABC_MAX_STR, fp ) == NULL )
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
            {
                if ( interactive )
                {
                    if ( quit_count++ < 5 )
                    {
                        fprintf( pAbc->Err, "\nUse \"quit\" to leave ABC.\n" );
                        continue;
                    }
                    status = -1;    /* fake a 'quit' */
                }
                else
                {
                    status = 0; /* successful end of 'source' ; loop? */
                }
                break;
            }
            quit_count = 0;

            if ( echo )
            {
                fprintf( pAbc->Out, "abc - > %s", line );
            }
            command = CmdHistorySubstitution( pAbc, line, &did_subst );
Alan Mishchenko committed
678
            if ( command == NULL )
Alan Mishchenko committed
679 680 681 682 683 684 685 686 687 688 689 690 691
            {
                status = 1;
                break;
            }
            if ( did_subst )
            {
                if ( interactive )
                {
                    fprintf( pAbc->Out, "%s\n", command );
                }
            }
            if ( command != line )
            {
692
                strcpy( line, command );
Alan Mishchenko committed
693 694 695
            }
            if ( interactive && *line != '\0' )
            {
696
                Cmd_HistoryAddCommand( pAbc, line );
Alan Mishchenko committed
697
                if ( pAbc->Hst != NULL )
Alan Mishchenko committed
698 699
                {
                    fprintf( pAbc->Hst, "%s\n", line );
700
                    fflush( pAbc->Hst );
Alan Mishchenko committed
701 702 703
                }
            }

Alan Mishchenko committed
704
            fflush( pAbc->Out );
Alan Mishchenko committed
705 706 707 708 709 710 711 712 713 714 715 716
            status = Cmd_CommandExecute( pAbc, line );
        }
        while ( status == 0 );

        if ( fp != stdin )
        {
            if ( status > 0 )
            {
                fprintf( pAbc->Err,
                                  "** cmd error: aborting 'source %s'\n",
                                  real_filename );
            }
717
            fclose( fp );
Alan Mishchenko committed
718
        }
Alan Mishchenko committed
719
        ABC_FREE( real_filename );
Alan Mishchenko committed
720 721 722

    }
    while ( ( status == 0 ) && ( lp_count <= 0 ) );
723
    pAbc->fSource = 0;
Alan Mishchenko committed
724 725 726
    return status;

  usage:
Alan Mishchenko committed
727 728 729 730
    fprintf( pAbc->Err, "usage: source [-psxh] <file_name>\n" );
    fprintf( pAbc->Err, "\t-p     supply prompt before reading each line [default = %s]\n", prompt? "yes": "no" );
    fprintf( pAbc->Err, "\t-s     silently ignore nonexistant file [default = %s]\n", silent? "yes": "no" );
    fprintf( pAbc->Err, "\t-x     echo each line as it is executed [default = %s]\n", echo? "yes": "no" );
Alan Mishchenko committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    fprintf( pAbc->Err, "\t-h     print the command usage\n" );
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandSetVariable( Abc_Frame_t * pAbc, int argc, char **argv )
{
748 749
    char *flag_value, *value;
    const char* key;
Alan Mishchenko committed
750 751
    int c;

Alan Mishchenko committed
752 753
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    {
        switch ( c )
        {
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }
    if ( argc == 0 || argc > 3 )
    {
        goto usage;
    }
    else if ( argc == 1 )
    {
Alan Mishchenko committed
769
        CmdPrintTable( pAbc->tFlags, 0 );
Alan Mishchenko committed
770 771 772 773 774
        return 0;
    }
    else
    {
        key = argv[1];
775
        if ( st__delete( pAbc->tFlags, &key, &value ) )
Alan Mishchenko committed
776
        {
Alan Mishchenko committed
777 778
            ABC_FREE( key );
            ABC_FREE( value );
Alan Mishchenko committed
779 780
        }

Alan Mishchenko committed
781 782
        flag_value = argc == 2 ? Extra_UtilStrsav( "" ) : Extra_UtilStrsav( argv[2] );
//        flag_value = argc == 2 ? NULL : Extra_UtilStrsav(argv[2]);
783
        st__insert( pAbc->tFlags, Extra_UtilStrsav(argv[1]), flag_value );
Alan Mishchenko committed
784 785 786 787

        if ( strcmp( argv[1], "abcout" ) == 0 )
        {
            if ( pAbc->Out != stdout )
Alan Mishchenko committed
788
                fclose( pAbc->Out );
Alan Mishchenko committed
789 790
            if ( strcmp( flag_value, "" ) == 0 )
                flag_value = "-";
Alan Mishchenko committed
791
            pAbc->Out = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
792 793 794 795 796 797 798 799 800
            if ( pAbc->Out == NULL )
                pAbc->Out = stdout;
#if HAVE_SETVBUF
            setvbuf( pAbc->Out, ( char * ) NULL, _IOLBF, 0 );
#endif
        }
        if ( strcmp( argv[1], "abcerr" ) == 0 )
        {
            if ( pAbc->Err != stderr )
Alan Mishchenko committed
801
                fclose( pAbc->Err );
Alan Mishchenko committed
802 803
            if ( strcmp( flag_value, "" ) == 0 )
                flag_value = "-";
Alan Mishchenko committed
804
            pAbc->Err = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
805 806 807 808 809 810 811 812
            if ( pAbc->Err == NULL )
                pAbc->Err = stderr;
#if HAVE_SETVBUF
            setvbuf( pAbc->Err, ( char * ) NULL, _IOLBF, 0 );
#endif
        }
        if ( strcmp( argv[1], "history" ) == 0 )
        {
Alan Mishchenko committed
813
            if ( pAbc->Hst != NULL )
Alan Mishchenko committed
814
                fclose( pAbc->Hst );
Alan Mishchenko committed
815
            if ( strcmp( flag_value, "" ) == 0 )
Alan Mishchenko committed
816
                pAbc->Hst = NULL;
Alan Mishchenko committed
817 818
            else
            {
Alan Mishchenko committed
819
                pAbc->Hst = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
820
                if ( pAbc->Hst == NULL )
Alan Mishchenko committed
821
                    pAbc->Hst = NULL;
Alan Mishchenko committed
822 823 824 825 826 827
            }
        }
        return 0;
    }

  usage:
Alan Mishchenko committed
828
    fprintf( pAbc->Err, "usage: set [-h] <name> <value>\n" );
Alan Mishchenko committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    fprintf( pAbc->Err, "\t        sets the value of parameter <name>\n" );
    fprintf( pAbc->Err, "\t-h    : print the command usage\n" );
    return 1;

}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandUnsetVariable( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int i;
849 850
    const char *key;
    char *value;
Alan Mishchenko committed
851 852
    int c;

Alan Mishchenko committed
853 854
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

    if ( argc < 2 )
    {
        goto usage;
    }

    for ( i = 1; i < argc; i++ )
    {
        key = argv[i];
874
        if ( st__delete( pAbc->tFlags, &key, &value ) )
Alan Mishchenko committed
875
        {
Alan Mishchenko committed
876 877
            ABC_FREE( key );
            ABC_FREE( value );
Alan Mishchenko committed
878 879 880 881 882 883
        }
    }
    return 0;


  usage:
Alan Mishchenko committed
884 885 886
    fprintf( pAbc->Err, "usage: unset [-h] <name> \n" );
    fprintf( pAbc->Err, "\t        removes the value of parameter <name>\n" );
    fprintf( pAbc->Err, "\t-h    : print the command usage\n" );
Alan Mishchenko committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandUndo( Abc_Frame_t * pAbc, int argc, char **argv )
{
    if ( pAbc->pNtkCur == NULL )
    {
        fprintf( pAbc->Out, "Empty network.\n" );
        return 0;
    }

    // if there are no arguments on the command line
    // set the current network to be the network from the previous step
911
    if ( argc == 1 )
Alan Mishchenko committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
        return CmdCommandRecall( pAbc, argc, argv );

    fprintf( pAbc->Err, "usage: undo\n" );
    fprintf( pAbc->Err, "         sets the current network to be the previously saved network\n" );
    return 1;

}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandRecall( Abc_Frame_t * pAbc, int argc, char **argv )
{
    Abc_Ntk_t * pNtk;
    int iStep, iStepFound;
    int nNetsToSave, c;
    char * pValue;
    int iStepStart, iStepStop;

    if ( pAbc->pNtkCur == NULL )
    {
        fprintf( pAbc->Out, "Empty network.\n" );
        return 0;
    }

945

Alan Mishchenko committed
946 947
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
948 949 950 951 952 953 954 955 956
    {
        switch ( c )
        {
            case 'h':
                goto usage;
            default:
                goto usage;
        }
    }
957

Alan Mishchenko committed
958 959 960 961 962
    // get the number of networks to save
    pValue = Cmd_FlagReadByName( pAbc, "savesteps" );
    // if the value of steps to save is not set, assume 1-level undo
    if ( pValue == NULL )
        nNetsToSave = 1;
963
    else
Alan Mishchenko committed
964 965 966 967
        nNetsToSave = atoi(pValue);

    // if there are no arguments on the command line
    // set the current network to be the network from the previous step
968
    if ( argc == 1 )
Alan Mishchenko committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    {
        // get the previously saved network
        pNtk = Abc_NtkBackup(pAbc->pNtkCur);
        if ( pNtk == NULL )
            fprintf( pAbc->Out, "There is no previously saved network.\n" );
        else // set the current network to be the copy of the previous one
            Abc_FrameSetCurrentNetwork( pAbc, Abc_NtkDup(pNtk) );
         return 0;
    }
    if ( argc == 2 ) // the second argument is the number of the step to return to
    {
        // read the number of the step to return to
        iStep = atoi(argv[1]);
        // check whether it is reasonable
        if ( iStep >= pAbc->nSteps )
        {
            iStepStart = pAbc->nSteps - nNetsToSave;
            if ( iStepStart <= 0 )
                iStepStart = 1;
            iStepStop  = pAbc->nSteps;
            if ( iStepStop <= 0 )
                iStepStop = 1;
            if ( iStepStart == iStepStop )
                fprintf( pAbc->Out, "Can only recall step %d.\n", iStepStop );
            else
                fprintf( pAbc->Out, "Can only recall steps %d-%d.\n", iStepStart, iStepStop );
        }
        else if ( iStep < 0 )
            fprintf( pAbc->Out, "Cannot recall step %d.\n", iStep );
        else if ( iStep == 0 )
            Abc_FrameDeleteAllNetworks( pAbc );
1000
        else
Alan Mishchenko committed
1001 1002 1003 1004 1005
        {
            // scroll backward through the list of networks
            // to determine if such a network exist
            iStepFound = 0;
            for ( pNtk = pAbc->pNtkCur; pNtk; pNtk = Abc_NtkBackup(pNtk) )
1006
                if ( (iStepFound = Abc_NtkStep(pNtk)) == iStep )
Alan Mishchenko committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
                    break;
            if ( pNtk == NULL )
            {
                iStepStart = iStepFound;
                if ( iStepStart <= 0 )
                    iStepStart = 1;
                iStepStop  = pAbc->nSteps;
                if ( iStepStop <= 0 )
                    iStepStop = 1;
                if ( iStepStart == iStepStop )
                    fprintf( pAbc->Out, "Can only recall step %d.\n", iStepStop );
                else
                    fprintf( pAbc->Out, "Can only recall steps %d-%d.\n", iStepStart, iStepStop );
            }
            else
                Abc_FrameSetCurrentNetwork( pAbc, Abc_NtkDup(pNtk) );
        }
        return 0;
    }

usage:

Alan Mishchenko committed
1029
    fprintf( pAbc->Err, "usage: recall -h <num>\n" );
Alan Mishchenko committed
1030 1031
    fprintf( pAbc->Err, "         set the current network to be one of the previous networks\n" );
    fprintf( pAbc->Err, "<num> :  level to return to [default = previous]\n" );
Alan Mishchenko committed
1032
    fprintf( pAbc->Err, "   -h :  print the command usage\n");
Alan Mishchenko committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    return 1;
}


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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandEmpty( Abc_Frame_t * pAbc, int argc, char **argv )
{
    int c;

    if ( pAbc->pNtkCur == NULL )
    {
        fprintf( pAbc->Out, "Empty network.\n" );
        return 0;
    }

Alan Mishchenko committed
1058 1059
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
1060 1061 1062 1063 1064 1065 1066 1067 1068
    {
        switch ( c )
        {
            case 'h':
                goto usage;
            default:
                goto usage;
        }
    }
1069

Alan Mishchenko committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    Abc_FrameDeleteAllNetworks( pAbc );
    Abc_FrameRestart( pAbc );
    return 0;
usage:

    fprintf( pAbc->Err, "usage: empty [-h]\n" );
    fprintf( pAbc->Err, "         removes all the currently stored networks\n" );
    fprintf( pAbc->Err, "   -h :  print the command usage\n");
    return 1;
}


#if 0

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

  Synopsis    [Donald's version.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandUndo( Abc_Frame_t * pAbc, int argc, char **argv )
{
    Abc_Ntk_t * pNtkTemp;
    int id, c;

Alan Mishchenko committed
1100
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }
Alan Mishchenko committed
1111
    if (globalUtilOptind <= argc) {
Alan Mishchenko committed
1112 1113 1114 1115
    pNtkTemp = pAbc->pNtk;
    pAbc->pNtk = pAbc->pNtkSaved;
    pAbc->pNtkSaved = pNtkTemp;
    }
Alan Mishchenko committed
1116
    id = atoi(argv[globalUtilOptind]);
Alan Mishchenko committed
1117
    pNtkTemp = Cmd_HistoryGetSnapshot(pAbc, id);
1118
    if (!pNtkTemp)
Alan Mishchenko committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    fprintf( pAbc->Err, "Snapshot %d does not exist\n", id);
    else
    pAbc->pNtk = Abc_NtkDup(pNtkTemp, Abc_NtkMan(pNtkTemp));

    return 0;
usage:
    fprintf( pAbc->Err, "usage: undo\n" );
    fprintf( pAbc->Err, "       swaps the current network and the backup network\n" );
    return 1;
}

#endif


1133
#if defined(WIN32) && !defined(__cplusplus)
Alan Mishchenko committed
1134
#include <direct.h>
Alan Mishchenko committed
1135 1136 1137 1138 1139 1140

// these structures are defined in <io.h> but are for some reason invisible
typedef unsigned long _fsize_t; // Could be 64 bits for Win32

struct _finddata_t {
    unsigned    attrib;
1141 1142
    time_t      time_create;    // -1 for FAT file systems
    time_t      time_access;    // -1 for FAT file systems
Alan Mishchenko committed
1143 1144 1145 1146 1147 1148 1149 1150 1151
    time_t      time_write;
    _fsize_t    size;
    char        name[260];
};

extern long _findfirst( char *filespec, struct _finddata_t *fileinfo );
extern int  _findnext( long handle, struct _finddata_t *fileinfo );
extern int  _findclose( long handle );

Alan Mishchenko committed
1152
//extern char * _getcwd( char * buffer, int maxlen );
1153
//extern int    _chdir( const char *dirname );
Alan Mishchenko committed
1154 1155 1156 1157 1158 1159

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

  Synopsis    [Command to print the contents of the current directory (Windows).]

  Description []
1160

Alan Mishchenko committed
1161 1162 1163 1164 1165
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
int CmdCommandLs( Abc_Frame_t * pAbc, int argc, char **argv )
{
    struct _finddata_t c_file;
    long   hFile;
    int    fLong = 0;
    int    fOnlyBLIF = 0;
    char   Buffer[25];
    int    Counter = 0;
    int    fPrintedNewLine;
    char   c;

Alan Mishchenko committed
1177 1178
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "lb") ) != EOF )
Alan Mishchenko committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    {
        switch (c)
        {
            case 'l':
              fLong = 1;
              break;
            case 'b':
              fOnlyBLIF = 1;
              break;
            default:
              goto usage;
        }
    }

    // find first .mv file in current directory
    if( (hFile = _findfirst( ((fOnlyBLIF)? "*.mv": "*.*"), &c_file )) == -1L )
    {
        if ( fOnlyBLIF )
            fprintf( pAbc->Out, "No *.mv files in the current directory.\n" );
        else
            fprintf( pAbc->Out, "No files in the current directory.\n" );
    }
    else
    {
        if ( fLong )
        {
            fprintf( pAbc->Out, " File              Date           Size |  File             Date           Size \n" );
            fprintf( pAbc->Out, " ----------------------------------------------------------------------------- \n" );
            do
            {
                strcpy( Buffer, ctime( &(c_file.time_write) ) );
                Buffer[16] = 0;
                fprintf( pAbc->Out, " %-17s %.24s%7ld", c_file.name, Buffer+4, c_file.size );
                if ( ++Counter % 2 == 0 )
                {
                    fprintf( pAbc->Out, "\n" );
                    fPrintedNewLine = 1;
                }
                else
                {
                    fprintf( pAbc->Out, " |" );
                    fPrintedNewLine = 0;
                }
            }
            while( _findnext( hFile, &c_file ) == 0 );
        }
        else
        {
            do
            {
                fprintf( pAbc->Out, " %-18s", c_file.name );
                if ( ++Counter % 4 == 0 )
                {
                    fprintf( pAbc->Out, "\n" );
                    fPrintedNewLine = 1;
                }
                else
                {
                    fprintf( pAbc->Out, " " );
                    fPrintedNewLine = 0;
                }
            }
            while( _findnext( hFile, &c_file ) == 0 );
        }
        if ( !fPrintedNewLine )
            fprintf( pAbc->Out, "\n" );
        _findclose( hFile );
    }
    return 0;

usage:
Alan Mishchenko committed
1250
    fprintf( pAbc->Err, "usage: ls [-l] [-b]\n" );
Alan Mishchenko committed
1251 1252 1253
    fprintf( pAbc->Err, "       print the file names in the current directory\n" );
    fprintf( pAbc->Err, "        -l : print in the long format [default = short]\n" );
    fprintf( pAbc->Err, "        -b : print only .mv files [default = all]\n" );
1254
    return 1;
Alan Mishchenko committed
1255 1256 1257
}


Alan Mishchenko committed
1258 1259 1260 1261 1262
/**Function*************************************************************

  Synopsis    [Generates the script for running ABC.]

  Description []
1263

Alan Mishchenko committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdCommandScrGen( Abc_Frame_t * pAbc, int argc, char **argv )
{
    struct _finddata_t c_file;
    long   hFile;
    FILE * pFile = NULL;
    char * pFileStr = "test.s";
    char * pDirStr = NULL;
    char * pComStr = "ps";
    char * pWriteStr = NULL;
    char   Buffer[1000], Line[2000];
    int    nFileNameMax, nFileNameCur;
    int    Counter = 0;
    int    fUseCurrent;
    char   c;

    fUseCurrent = 0;
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "FDCWch") ) != EOF )
    {
        switch (c)
        {
        case 'F':
            if ( globalUtilOptind >= argc )
            {
                fprintf( pAbc->Err, "Command line switch \"-F\" should be followed by a string.\n" );
                goto usage;
            }
            pFileStr = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'D':
            if ( globalUtilOptind >= argc )
            {
                fprintf( pAbc->Err, "Command line switch \"-D\" should be followed by a string.\n" );
                goto usage;
            }
            pDirStr = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'C':
            if ( globalUtilOptind >= argc )
            {
                fprintf( pAbc->Err, "Command line switch \"-C\" should be followed by a string.\n" );
                goto usage;
            }
            pComStr = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'W':
            if ( globalUtilOptind >= argc )
            {
                fprintf( pAbc->Err, "Command line switch \"-W\" should be followed by a string.\n" );
                goto usage;
            }
            pWriteStr = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'c':
            fUseCurrent ^= 1;
            break;
        default:
            goto usage;
        }
    }

//    printf( "File = %s.\n", pFileStr );
//    printf( "Dir = %s.\n", pDirStr );
//    printf( "Com = %s.\n", pComStr );
    if ( pDirStr == NULL )
        fUseCurrent = 1;

    if ( _getcwd( Buffer, 1000 ) == NULL )
    {
        printf( "Cannot get the current directory.\n" );
        return 0;
    }
    if ( fUseCurrent )
        pFile = fopen( pFileStr, "w" );
    if ( pDirStr )
    {
        if ( _chdir(pDirStr) )
        {
            printf( "Cannot change to directory: %s\n", pDirStr );
            return 0;
        }
    }
    if ( !fUseCurrent )
        pFile = fopen( pFileStr, "w" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file %s.\n", pFileStr );
        if ( pDirStr && _chdir(Buffer) )
        {
            printf( "Cannot change to the current directory: %s\n", Buffer );
            return 0;
        }
        return 0;
    }

    // find the first file in the directory
    if( (hFile = _findfirst( "*.*", &c_file )) == -1L )
    {
        if ( pDirStr )
            printf( "No files in the current directory.\n" );
        else
            printf( "No files in directory: %s\n", pDirStr );
        if ( pDirStr && _chdir(Buffer) )
        {
            printf( "Cannot change to the current directory: %s\n", Buffer );
            return 0;
        }
    }

    // get the longest file name
    {
        nFileNameMax = 0;
        do
        {
            // skip script and txt files
            nFileNameCur = strlen(c_file.name);
            if ( c_file.name[nFileNameCur-1] == '.' )
                continue;
            if ( nFileNameCur > 2 &&
1392 1393
                 c_file.name[nFileNameCur-1] == 's' &&
                 c_file.name[nFileNameCur-2] == '.' )
Alan Mishchenko committed
1394 1395
                 continue;
            if ( nFileNameCur > 4 &&
1396 1397 1398 1399
                 c_file.name[nFileNameCur-1] == 't' &&
                 c_file.name[nFileNameCur-2] == 'x' &&
                 c_file.name[nFileNameCur-3] == 't' &&
                 c_file.name[nFileNameCur-4] == '.' )
Alan Mishchenko committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
                 continue;
            if ( nFileNameMax < nFileNameCur )
                nFileNameMax = nFileNameCur;
        }
        while( _findnext( hFile, &c_file ) == 0 );
        _findclose( hFile );
    }

    // print the script file
    {
        if( (hFile = _findfirst( "*.*", &c_file )) == -1L )
        {
            if ( pDirStr )
                printf( "No files in the current directory.\n" );
            else
                printf( "No files in directory: %s\n", pDirStr );
        }
        fprintf( pFile, "# Script file produced by ABC on %s\n", Extra_TimeStamp() );
1418
        fprintf( pFile, "# Command line was: scrgen -F %s -D %s -C \"%s\"%s%s\n",
Alan Mishchenko committed
1419 1420 1421 1422 1423 1424 1425 1426
            pFileStr, pDirStr, pComStr, pWriteStr?" -W ":"", pWriteStr?pWriteStr:"" );
        do
        {
            // skip script and txt files
            nFileNameCur = strlen(c_file.name);
            if ( c_file.name[nFileNameCur-1] == '.' )
                continue;
            if ( nFileNameCur > 2 &&
1427 1428
                 c_file.name[nFileNameCur-1] == 's' &&
                 c_file.name[nFileNameCur-2] == '.' )
Alan Mishchenko committed
1429 1430
                 continue;
            if ( nFileNameCur > 4 &&
1431 1432 1433 1434
                 c_file.name[nFileNameCur-1] == 't' &&
                 c_file.name[nFileNameCur-2] == 'x' &&
                 c_file.name[nFileNameCur-3] == 't' &&
                 c_file.name[nFileNameCur-4] == '.' )
Alan Mishchenko committed
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
                 continue;
            sprintf( Line, "r %s%s%-*s ; %s", pDirStr?pDirStr:"", pDirStr?"/":"", nFileNameMax, c_file.name, pComStr );
            for ( c = (int)strlen(Line)-1; c >= 0; c-- )
                if ( Line[c] == '\\' )
                    Line[c] = '/';
            fprintf( pFile, "%s", Line );
            if ( pWriteStr )
            {
                sprintf( Line, " ; w %s/%-*s", pWriteStr, nFileNameMax, c_file.name );
                for ( c = (int)strlen(Line)-1; c >= 0; c-- )
                    if ( Line[c] == '\\' )
                        Line[c] = '/';
                fprintf( pFile, "%s", Line );
            }
            fprintf( pFile, "\n", Line );
        }
        while( _findnext( hFile, &c_file ) == 0 );
        _findclose( hFile );
    }
    fclose( pFile );
    if ( pDirStr && _chdir(Buffer) )
    {
        printf( "Cannot change to the current directory: %s\n", Buffer );
        return 0;
    }

    // report
    if ( fUseCurrent )
        printf( "Script file \"%s\" was saved in the current directory.\n", pFileStr );
    else
        printf( "Script file \"%s\" was saved in directory: %s\n", pFileStr, pDirStr );
    return 0;

usage:
    fprintf( pAbc->Err, "usage: scrgen -F <str> -D <str> -C <str> -W <str> -ch\n" );
    fprintf( pAbc->Err, "\t          generates script for running ABC\n" );
    fprintf( pAbc->Err, "\t-F str  : the name of the script file [default = \"test.s\"]\n" );
    fprintf( pAbc->Err, "\t-D str  : the directory to read files from [default = current]\n" );
    fprintf( pAbc->Err, "\t-C str  : the sequence of commands to run [default = \"ps\"]\n" );
    fprintf( pAbc->Err, "\t-W str  : the directory to write the resulting files [default = no writing]\n" );
    fprintf( pAbc->Err, "\t-c      : toggle placing file in current/target dir [default = %s]\n", fUseCurrent? "current": "target" );
    fprintf( pAbc->Err, "\t-h      : print the command usage\n\n");
    fprintf( pAbc->Err, "\tExample : scrgen -F test1.s -D a/in -C \"ps; st; ps\" -W a/out\n" );
1478
    return 1;
Alan Mishchenko committed
1479 1480 1481
}
#endif

Alan Mishchenko committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

#ifdef WIN32
#define unlink _unlink
#endif

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

  Synopsis    [Calls SIS internally.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandSis( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
Alan Mishchenko committed
1502 1503 1504
    Abc_Ntk_t * pNtk, * pNtkNew, * pNetlist;
    char * pNameWin = "sis.exe";
    char * pNameUnix = "sis";
Alan Mishchenko committed
1505 1506 1507 1508
    char Command[1000], Buffer[100];
    char * pSisName;
    int i;

Alan Mishchenko committed
1509
    pNtk = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    if ( pNtk == NULL )
    {
        fprintf( pErr, "Empty network.\n" );
        goto usage;
    }

    if ( strcmp( argv[0], "sis" ) != 0 )
    {
        fprintf( pErr, "Wrong command: \"%s\".\n", argv[0] );
        goto usage;
    }

    if ( argc == 1 )
        goto usage;
    if ( strcmp( argv[1], "-h" ) == 0 )
        goto usage;
    if ( strcmp( argv[1], "-?" ) == 0 )
        goto usage;

Alan Mishchenko committed
1532 1533 1534 1535 1536 1537
    // get the names from the resource file
    if ( Cmd_FlagReadByName(pAbc, "siswin") )
        pNameWin = Cmd_FlagReadByName(pAbc, "siswin");
    if ( Cmd_FlagReadByName(pAbc, "sisunix") )
        pNameUnix = Cmd_FlagReadByName(pAbc, "sisunix");

Alan Mishchenko committed
1538
    // check if SIS is available
Alan Mishchenko committed
1539 1540 1541 1542
    if ( (pFile = fopen( pNameWin, "r" )) )
        pSisName = pNameWin;
    else if ( (pFile = fopen( pNameUnix, "r" )) )
        pSisName = pNameUnix;
Alan Mishchenko committed
1543 1544
    else if ( pFile == NULL )
    {
Alan Mishchenko committed
1545
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pNameWin, pNameUnix );
Alan Mishchenko committed
1546 1547 1548 1549
        goto usage;
    }
    fclose( pFile );

Alan Mishchenko committed
1550 1551 1552 1553 1554 1555
    if ( Abc_NtkIsMappedLogic(pNtk) )
    {
        Abc_NtkMapToSop(pNtk);
        printf( "The current network is unmapped before calling SIS.\n" );
    }

Alan Mishchenko committed
1556
    // write out the current network
Alan Mishchenko committed
1557 1558 1559 1560 1561 1562 1563 1564
    if ( Abc_NtkIsLogic(pNtk) )
        Abc_NtkToSop(pNtk, 0);
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
1565
    Io_WriteBlif( pNetlist, "_sis_in.blif", 1, 0, 0 );
Alan Mishchenko committed
1566
    Abc_NtkDelete( pNetlist );
Alan Mishchenko committed
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582

    // create the file for sis
    sprintf( Command, "%s -x -c ", pSisName );
    strcat ( Command, "\"" );
    strcat ( Command, "read_blif _sis_in.blif" );
    strcat ( Command, "; " );
    for ( i = 1; i < argc; i++ )
    {
        sprintf( Buffer, " %s", argv[i] );
        strcat( Command, Buffer );
    }
    strcat( Command, "; " );
    strcat( Command, "write_blif _sis_out.blif" );
    strcat( Command, "\"" );

    // call SIS
1583
    if ( Util_SignalSystem( Command ) )
Alan Mishchenko committed
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
    {
        fprintf( pErr, "The following command has returned non-zero exit status:\n" );
        fprintf( pErr, "\"%s\"\n", Command );
        unlink( "_sis_in.blif" );
        goto usage;
    }

    // read in the SIS output
    if ( (pFile = fopen( "_sis_out.blif", "r" )) == NULL )
    {
        fprintf( pErr, "Cannot open SIS output file \"%s\".\n", "_sis_out.blif" );
        unlink( "_sis_in.blif" );
        goto usage;
    }
    fclose( pFile );

    // set the new network
Alan Mishchenko committed
1601
    pNtkNew = Io_Read( "_sis_out.blif", IO_FILE_BLIF, 1 );
Alan Mishchenko committed
1602 1603 1604
    // set the original spec of the new network
    if ( pNtk->pSpec )
    {
Alan Mishchenko committed
1605
        ABC_FREE( pNtkNew->pSpec );
Alan Mishchenko committed
1606
        pNtkNew->pSpec = Extra_UtilStrsav( pNtk->pSpec );
Alan Mishchenko committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
    }
    // replace the current network
    Abc_FrameReplaceCurrentNetwork( pAbc, pNtkNew );

    // remove temporary networks
    unlink( "_sis_in.blif" );
    unlink( "_sis_out.blif" );
    return 0;

usage:
    fprintf( pErr, "\n" );
    fprintf( pErr, "Usage: sis [-h] <com>\n");
    fprintf( pErr, "         invokes SIS command for the current ABC network\n" );
    fprintf( pErr, "         (the executable of SIS should be in the same directory)\n" );
    fprintf( pErr, "   -h  : print the command usage\n" );
    fprintf( pErr, " <com> : a SIS command (or a semicolon-separated list of commands in quotes)\n" );
    fprintf( pErr, "         Example 1: sis eliminate 0\n" );
    fprintf( pErr, "         Example 2: sis \"ps; rd; fx; ps\"\n" );
    fprintf( pErr, "         Example 3: sis source script.rugged\n" );
1626
    return 1;                   // error exit
Alan Mishchenko committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
}


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

  Synopsis    [Calls SIS internally.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandMvsis( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
Alan Mishchenko committed
1645
    Abc_Ntk_t * pNtk, * pNtkNew, * pNetlist;
Alan Mishchenko committed
1646
    char Command[1000], Buffer[100];
Alan Mishchenko committed
1647 1648
    char * pNameWin = "mvsis.exe";
    char * pNameUnix = "mvsis";
Alan Mishchenko committed
1649 1650 1651
    char * pMvsisName;
    int i;

Alan Mishchenko committed
1652
    pNtk = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    if ( pNtk == NULL )
    {
        fprintf( pErr, "Empty network.\n" );
        goto usage;
    }

    if ( strcmp( argv[0], "mvsis" ) != 0 )
    {
        fprintf( pErr, "Wrong command: \"%s\".\n", argv[0] );
        goto usage;
    }

    if ( argc == 1 )
        goto usage;
    if ( strcmp( argv[1], "-h" ) == 0 )
        goto usage;
    if ( strcmp( argv[1], "-?" ) == 0 )
        goto usage;

Alan Mishchenko committed
1675 1676 1677 1678 1679 1680
    // get the names from the resource file
    if ( Cmd_FlagReadByName(pAbc, "mvsiswin") )
        pNameWin = Cmd_FlagReadByName(pAbc, "mvsiswin");
    if ( Cmd_FlagReadByName(pAbc, "mvsisunix") )
        pNameUnix = Cmd_FlagReadByName(pAbc, "mvsisunix");

Alan Mishchenko committed
1681
    // check if MVSIS is available
Alan Mishchenko committed
1682 1683 1684 1685
    if ( (pFile = fopen( pNameWin, "r" )) )
        pMvsisName = pNameWin;
    else if ( (pFile = fopen( pNameUnix, "r" )) )
        pMvsisName = pNameUnix;
Alan Mishchenko committed
1686 1687
    else if ( pFile == NULL )
    {
Alan Mishchenko committed
1688
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pNameWin, pNameUnix );
Alan Mishchenko committed
1689 1690 1691 1692
        goto usage;
    }
    fclose( pFile );

Alan Mishchenko committed
1693 1694 1695 1696 1697
    if ( Abc_NtkIsMappedLogic(pNtk) )
    {
        Abc_NtkMapToSop(pNtk);
        printf( "The current network is unmapped before calling MVSIS.\n" );
    }
Alan Mishchenko committed
1698 1699

    // write out the current network
Alan Mishchenko committed
1700 1701 1702 1703 1704 1705 1706 1707
    if ( Abc_NtkIsLogic(pNtk) )
        Abc_NtkToSop(pNtk, 0);
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
1708
    Io_WriteBlif( pNetlist, "_mvsis_in.blif", 1, 0, 0 );
Alan Mishchenko committed
1709
    Abc_NtkDelete( pNetlist );
Alan Mishchenko committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

    // create the file for MVSIS
    sprintf( Command, "%s -x -c ", pMvsisName );
    strcat ( Command, "\"" );
    strcat ( Command, "read_blif _mvsis_in.blif" );
    strcat ( Command, "; " );
    for ( i = 1; i < argc; i++ )
    {
        sprintf( Buffer, " %s", argv[i] );
        strcat( Command, Buffer );
    }
    strcat( Command, "; " );
    strcat( Command, "write_blif _mvsis_out.blif" );
    strcat( Command, "\"" );

    // call MVSIS
1726
    if ( Util_SignalSystem( Command ) )
Alan Mishchenko committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    {
        fprintf( pErr, "The following command has returned non-zero exit status:\n" );
        fprintf( pErr, "\"%s\"\n", Command );
        unlink( "_mvsis_in.blif" );
        goto usage;
    }

    // read in the MVSIS output
    if ( (pFile = fopen( "_mvsis_out.blif", "r" )) == NULL )
    {
        fprintf( pErr, "Cannot open MVSIS output file \"%s\".\n", "_mvsis_out.blif" );
        unlink( "_mvsis_in.blif" );
        goto usage;
    }
    fclose( pFile );

    // set the new network
Alan Mishchenko committed
1744
    pNtkNew = Io_Read( "_mvsis_out.blif", IO_FILE_BLIF, 1 );
Alan Mishchenko committed
1745 1746 1747
    // set the original spec of the new network
    if ( pNtk->pSpec )
    {
Alan Mishchenko committed
1748
        ABC_FREE( pNtkNew->pSpec );
Alan Mishchenko committed
1749
        pNtkNew->pSpec = Extra_UtilStrsav( pNtk->pSpec );
Alan Mishchenko committed
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
    }
    // replace the current network
    Abc_FrameReplaceCurrentNetwork( pAbc, pNtkNew );

    // remove temporary networks
    unlink( "_mvsis_in.blif" );
    unlink( "_mvsis_out.blif" );
    return 0;

usage:
    fprintf( pErr, "\n" );
    fprintf( pErr, "Usage: mvsis [-h] <com>\n");
    fprintf( pErr, "         invokes MVSIS command for the current ABC network\n" );
    fprintf( pErr, "         (the executable of MVSIS should be in the same directory)\n" );
    fprintf( pErr, "   -h  : print the command usage\n" );
    fprintf( pErr, " <com> : a MVSIS command (or a semicolon-separated list of commands in quotes)\n" );
    fprintf( pErr, "         Example 1: mvsis fraig_sweep\n" );
    fprintf( pErr, "         Example 2: mvsis \"ps; fxu; ps\"\n" );
    fprintf( pErr, "         Example 3: mvsis source mvsis.rugged\n" );
1769
    return 1;                   // error exit
Alan Mishchenko committed
1770 1771 1772
}


Alan Mishchenko committed
1773 1774 1775 1776 1777
/**Function*************************************************************

  Synopsis    [Computes dimentions of the graph.]

  Description []
1778

Alan Mishchenko committed
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManGnuplotShow( char * pPlotFileName )
{
    FILE * pFile;
    void * pAbc;
    char * pProgNameGnuplotWin  = "wgnuplot.exe";
    char * pProgNameGnuplotUnix = "gnuplot";
    char * pProgNameGnuplot;

    // read in the Capo plotting output
    if ( (pFile = fopen( pPlotFileName, "r" )) == NULL )
    {
        fprintf( stdout, "Cannot open the plot file \"%s\".\n\n", pPlotFileName );
        return;
    }
    fclose( pFile );

    pAbc = Abc_FrameGetGlobalFrame();

    // get the names from the plotting software
1803 1804 1805 1806
    if ( Cmd_FlagReadByName((Abc_Frame_t *)pAbc, "gnuplotwin") )
        pProgNameGnuplotWin = Cmd_FlagReadByName((Abc_Frame_t *)pAbc, "gnuplotwin");
    if ( Cmd_FlagReadByName((Abc_Frame_t *)pAbc, "gnuplotunix") )
        pProgNameGnuplotUnix = Cmd_FlagReadByName((Abc_Frame_t *)pAbc, "gnuplotunix");
Alan Mishchenko committed
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839

    // check if Gnuplot is available
    if ( (pFile = fopen( pProgNameGnuplotWin, "r" )) )
        pProgNameGnuplot = pProgNameGnuplotWin;
    else if ( (pFile = fopen( pProgNameGnuplotUnix, "r" )) )
        pProgNameGnuplot = pProgNameGnuplotUnix;
    else if ( pFile == NULL )
    {
        fprintf( stdout, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pProgNameGnuplotWin, pProgNameGnuplotUnix );
        return;
    }
    fclose( pFile );

    // spawn the viewer
#ifdef WIN32
    if ( _spawnl( _P_NOWAIT, pProgNameGnuplot, pProgNameGnuplot, pPlotFileName, NULL ) == -1 )
    {
        fprintf( stdout, "Cannot find \"%s\".\n", pProgNameGnuplot );
        return;
    }
#else
    {
        char Command[1000];
        sprintf( Command, "%s %s ", pProgNameGnuplot, pPlotFileName );
        if ( system( Command ) == -1 )
        {
            fprintf( stdout, "Cannot execute \"%s\".\n", Command );
            return;
        }
    }
#endif
}

Alan Mishchenko committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
/**Function********************************************************************

  Synopsis    [Calls Capo internally.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandCapo( Abc_Frame_t * pAbc, int argc, char **argv )
{
    FILE * pFile;
    FILE * pOut, * pErr;
    Abc_Ntk_t * pNtk, * pNetlist;
    char Command[1000], Buffer[100];
    char * pProgNameCapoWin     = "capo.exe";
    char * pProgNameCapoUnix    = "capo";
    char * pProgNameGnuplotWin  = "wgnuplot.exe";
    char * pProgNameGnuplotUnix = "gnuplot";
    char * pProgNameCapo;
    char * pProgNameGnuplot;
    char * pPlotFileName;
    int i;

    pNtk = Abc_FrameReadNtk(pAbc);
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

    if ( pNtk == NULL )
    {
        fprintf( pErr, "Empty network.\n" );
        goto usage;
    }

    if ( strcmp( argv[0], "capo" ) != 0 )
    {
        fprintf( pErr, "Wrong command: \"%s\".\n", argv[0] );
        goto usage;
    }

    if ( argc > 1 )
    {
        if ( strcmp( argv[1], "-h" ) == 0 )
            goto usage;
        if ( strcmp( argv[1], "-?" ) == 0 )
            goto usage;
    }

    // get the names from the resource file
    if ( Cmd_FlagReadByName(pAbc, "capowin") )
        pProgNameCapoWin = Cmd_FlagReadByName(pAbc, "capowin");
    if ( Cmd_FlagReadByName(pAbc, "capounix") )
        pProgNameCapoUnix = Cmd_FlagReadByName(pAbc, "capounix");

    // check if capo is available
    if ( (pFile = fopen( pProgNameCapoWin, "r" )) )
        pProgNameCapo = pProgNameCapoWin;
    else if ( (pFile = fopen( pProgNameCapoUnix, "r" )) )
        pProgNameCapo = pProgNameCapoUnix;
    else if ( pFile == NULL )
    {
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pProgNameCapoWin, pProgNameCapoUnix );
        goto usage;
    }
    fclose( pFile );

    if ( Abc_NtkIsMappedLogic(pNtk) )
    {
        Abc_NtkMapToSop(pNtk);
        printf( "The current network is unmapped before calling Capo.\n" );
    }

    // write out the current network
    if ( Abc_NtkIsLogic(pNtk) )
        Abc_NtkToSop(pNtk, 0);
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
1923
    Io_WriteBlif( pNetlist, "_capo_in.blif", 1, 0, 0 );
Alan Mishchenko committed
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
    Abc_NtkDelete( pNetlist );

    // create the file for Capo
    sprintf( Command, "%s -f _capo_in.blif -log out.txt ", pProgNameCapo );
    pPlotFileName = NULL;
    for ( i = 1; i < argc; i++ )
    {
        sprintf( Buffer, " %s", argv[i] );
        strcat( Command, Buffer );
        if ( !strcmp( argv[i], "-plot" ) )
            pPlotFileName = argv[i+1];
    }

    // call Capo
1938
    if ( Util_SignalSystem( Command ) )
Alan Mishchenko committed
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    {
        fprintf( pErr, "The following command has returned non-zero exit status:\n" );
        fprintf( pErr, "\"%s\"\n", Command );
        unlink( "_capo_in.blif" );
        goto usage;
    }
    // remove temporary networks
    unlink( "_capo_in.blif" );
    if ( pPlotFileName == NULL )
        return 0;

    // get the file name
    sprintf( Buffer, "%s.plt", pPlotFileName );
    pPlotFileName = Buffer;

    // read in the Capo plotting output
    if ( (pFile = fopen( pPlotFileName, "r" )) == NULL )
    {
        fprintf( pErr, "Cannot open the plot file \"%s\".\n\n", pPlotFileName );
        goto usage;
    }
    fclose( pFile );

    // get the names from the plotting software
    if ( Cmd_FlagReadByName(pAbc, "gnuplotwin") )
        pProgNameGnuplotWin = Cmd_FlagReadByName(pAbc, "gnuplotwin");
    if ( Cmd_FlagReadByName(pAbc, "gnuplotunix") )
        pProgNameGnuplotUnix = Cmd_FlagReadByName(pAbc, "gnuplotunix");

    // check if Gnuplot is available
    if ( (pFile = fopen( pProgNameGnuplotWin, "r" )) )
        pProgNameGnuplot = pProgNameGnuplotWin;
    else if ( (pFile = fopen( pProgNameGnuplotUnix, "r" )) )
        pProgNameGnuplot = pProgNameGnuplotUnix;
    else if ( pFile == NULL )
    {
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pProgNameGnuplotWin, pProgNameGnuplotUnix );
        goto usage;
    }
    fclose( pFile );

    // spawn the viewer
#ifdef WIN32
    if ( _spawnl( _P_NOWAIT, pProgNameGnuplot, pProgNameGnuplot, pPlotFileName, NULL ) == -1 )
    {
        fprintf( stdout, "Cannot find \"%s\".\n", pProgNameGnuplot );
        goto usage;
    }
#else
    {
        sprintf( Command, "%s %s ", pProgNameGnuplot, pPlotFileName );
1990
        if ( Util_SignalSystem( Command ) == -1 )
Alan Mishchenko committed
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
        {
            fprintf( stdout, "Cannot execute \"%s\".\n", Command );
            goto usage;
        }
    }
#endif

    // remove temporary networks
//    unlink( pPlotFileName );
    return 0;

usage:
    fprintf( pErr, "\n" );
    fprintf( pErr, "Usage: capo [-h] <com>\n");
    fprintf( pErr, "         peforms placement of the current network using Capo\n" );
    fprintf( pErr, "         a Capo binary should be present in the same directory\n" );
    fprintf( pErr, "         (if plotting, the Gnuplot binary should also be present)\n" );
    fprintf( pErr, "   -h  : print the command usage\n" );
    fprintf( pErr, " <com> : a Capo command\n" );
    fprintf( pErr, "         Example 1: capo\n" );
    fprintf( pErr, "                    (performs placement with default options)\n" );
    fprintf( pErr, "         Example 2: capo -AR <aspec_ratio> -WS <whitespace_percentage> -save\n" );
    fprintf( pErr, "                    (specifies the aspect ratio [default = 1.0] and\n" );
    fprintf( pErr, "                    the whitespace percentage [0%%; 100%%) [default = 15%%])\n" );
    fprintf( pErr, "         Example 3: capo -plot <base_fileName>\n" );
    fprintf( pErr, "                    (produces <base_fileName.plt> and visualize it using Gnuplot)\n" );
    fprintf( pErr, "         Example 4: capo -help\n" );
    fprintf( pErr, "                    (prints the default usage message of the Capo binary)\n" );
    fprintf( pErr, "         Please refer to the Capo webpage for additional information:\n" );
    fprintf( pErr, "         http://vlsicad.eecs.umich.edu/BK/PDtools/\n" );
2021
    return 1;                   // error exit
Alan Mishchenko committed
2022 2023
}

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2035
int CmdCommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv )
2036
{
2037
    extern void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores );
2038 2039
    FILE * pFile;
    char * pFileName;
2040
    char * pCommand = NULL;
2041 2042 2043
    int c, nCores    =  3;
    int fVerbose     =  0;
    Extra_UtilGetoptReset();
2044
    while ( ( c = Extra_UtilGetopt( argc, argv, "NCvh" ) ) != EOF )
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
    {
        switch ( c )
        {
        case 'N':
            if ( globalUtilOptind >= argc )
            {
                Abc_Print( -1, "Command line switch \"-N\" should be followed by an integer.\n" );
                goto usage;
            }
            nCores = atoi(argv[globalUtilOptind]);
            globalUtilOptind++;
            if ( nCores < 0 ) 
                goto usage;
            break;
2059 2060 2061 2062 2063 2064 2065 2066 2067
        case 'C':
            if ( globalUtilOptind >= argc )
            {
                Abc_Print( -1, "Command line switch \"-C\" should be followed by a string (possibly in quotes).\n" );
                goto usage;
            }
            pCommand = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
        case 'v':
            fVerbose ^= 1;
            break;
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }
    if ( argc != globalUtilOptind + 1 )
    {
        Abc_Print( -2, "The file name should be given on the command line.\n" );
        return 1;
    }
    // get the input file name
    pFileName = argv[globalUtilOptind];
    if ( (pFile = Io_FileOpen( pFileName, "open_path", "rb", 0 )) == NULL )
//    if ( (pFile = fopen( pFileName, "rb" )) == NULL )
    {
        Abc_Print( -2, "Cannot open input file \"%s\". ", pFileName );
        if (( pFileName = Extra_FileGetSimilarName( pFileName, ".c", ".s", ".scr", ".script", NULL ) ))
            Abc_Print( -2, "Did you mean \"%s\"?", pFileName );
        Abc_Print( -2, "\n" );
        return 1;
    }
    fclose( pFile );
    // run commands
2095
    Cmd_RunStarter( pFileName, pAbc->sBinary, pCommand, nCores );
2096 2097 2098
    return 0;

usage:
2099
    Abc_Print( -2, "usage: starter [-N num] [-C cmd] [-vh] <file>\n" );
2100 2101
    Abc_Print( -2, "\t         runs command lines listed in <file> concurrently on <num> CPUs\n" );
    Abc_Print( -2, "\t-N num : the number of concurrent jobs including the controler [default = %d]\n", nCores );
2102
    Abc_Print( -2, "\t-C cmd : (optional) ABC command line to execute on benchmarks in <file>\n" );
2103 2104
    Abc_Print( -2, "\t-v     : toggle printing verbose information [default = %s]\n", fVerbose? "yes": "no" );
    Abc_Print( -2, "\t-h     : print the command usage\n");
2105
    Abc_Print( -2, "\t<file> : file name with ABC command lines (or benchmark names, if <cmd> is given)\n");
2106 2107 2108
    return 1;
}

Alan Mishchenko committed
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
/**Function********************************************************************

  Synopsis    [Print the version string.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandVersion( Abc_Frame_t * pAbc, int argc, char **argv )
{
    printf("%s\n", Abc_UtilsGetVersion(pAbc));
    return 0;
}
Alan Mishchenko committed
2125 2126


Alan Mishchenko committed
2127 2128 2129 2130 2131
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


2132
ABC_NAMESPACE_IMPL_END