cmd.c 77.6 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

////////////////////////////////////////////////////////////////////////
///                        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 );
41
static int CmdCommandAbcrc         ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
42 43 44 45 46 47 48 49 50 51
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)
53
static int CmdCommandScanDir       ( Abc_Frame_t * pAbc, int argc, char ** argv );
54
static int CmdCommandRenameFiles   ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
55
static int CmdCommandLs            ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
56
static int CmdCommandScrGen        ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
57
#endif
Alan Mishchenko committed
58
static int CmdCommandVersion       ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
59 60
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
61
static int CmdCommandCapo          ( Abc_Frame_t * pAbc, int argc, char ** argv );
62
static int CmdCommandStarter       ( Abc_Frame_t * pAbc, int argc, char ** argv );
63
static int CmdCommandAutoTuner     ( Abc_Frame_t * pAbc, int argc, char ** argv );
Alan Mishchenko committed
64

65 66
extern int Cmd_CommandAbcLoadPlugIn( Abc_Frame_t * pAbc, int argc, char ** argv );

Alan Mishchenko committed
67
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
68
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
69 70 71 72 73 74 75 76 77 78 79 80
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Initializes the command package.]

  SideEffects [Commands are added to the command table.]

  SeeAlso     [Cmd_End]

******************************************************************************/
void Cmd_Init( Abc_Frame_t * pAbc )
81
{
82 83 84
    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
85
    pAbc->aHistory  = Vec_PtrAlloc( 100 );
86
    Cmd_HistoryRead( pAbc );
Alan Mishchenko committed
87

88 89 90
    Cmd_CommandAdd( pAbc, "Basic", "time",          CmdCommandTime,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "echo",          CmdCommandEcho,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "quit",          CmdCommandQuit,            0 );
91
    Cmd_CommandAdd( pAbc, "Basic", "abcrc",         CmdCommandAbcrc,           0 );
92 93 94 95 96 97 98
    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 );
99 100 101
    Cmd_CommandAdd( pAbc, "Basic", "undo",          CmdCommandUndo,            0 );
    Cmd_CommandAdd( pAbc, "Basic", "recall",        CmdCommandRecall,          0 );
    Cmd_CommandAdd( pAbc, "Basic", "empty",         CmdCommandEmpty,           0 );
102
#if defined(WIN32) && !defined(__cplusplus)
103
    Cmd_CommandAdd( pAbc, "Basic", "scandir",       CmdCommandScanDir,         0 );
104
    Cmd_CommandAdd( pAbc, "Basic", "renamefiles",   CmdCommandRenameFiles,     0 );
105 106
    Cmd_CommandAdd( pAbc, "Basic", "ls",            CmdCommandLs,              0 );
    Cmd_CommandAdd( pAbc, "Basic", "scrgen",        CmdCommandScrGen,          0 );
Alan Mishchenko committed
107
#endif
108
    Cmd_CommandAdd( pAbc, "Basic", "version",       CmdCommandVersion,         0 );
109

110 111 112
    Cmd_CommandAdd( pAbc, "Various", "sis",         CmdCommandSis,             1 );
    Cmd_CommandAdd( pAbc, "Various", "mvsis",       CmdCommandMvsis,           1 );
    Cmd_CommandAdd( pAbc, "Various", "capo",        CmdCommandCapo,            0 );
113
    Cmd_CommandAdd( pAbc, "Various", "starter",     CmdCommandStarter,         0 );
114
    Cmd_CommandAdd( pAbc, "Various", "autotuner",   CmdCommandAutoTuner,       0 );
Alan Mishchenko committed
115

116
    Cmd_CommandAdd( pAbc, "Various", "load_plugin", Cmd_CommandAbcLoadPlugIn,  0 );
Alan Mishchenko committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

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

  Synopsis    [Ends the command package.]

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

  SideEffects []

  SeeAlso     []

******************************************************************************/
void Cmd_End( Abc_Frame_t * pAbc )
{
132
    st__generator * gen;
Alan Mishchenko committed
133
    char * pKey, * pValue;
134
    Cmd_HistoryWrite( pAbc, ABC_INFINITY );
Alan Mishchenko committed
135

136 137 138
//    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
139

140
    st__foreach_item( pAbc->tCommands, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
141
        CmdCommandFree( (Abc_Command *)pValue );
142
    st__free_table( pAbc->tCommands );
Alan Mishchenko committed
143

144
    st__foreach_item( pAbc->tAliases, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
145
        CmdCommandAliasFree( (Abc_Alias *)pValue );
146
    st__free_table( pAbc->tAliases );
Alan Mishchenko committed
147

148
    st__foreach_item( pAbc->tFlags, gen, (const char **)&pKey, (char **)&pValue )
Alan Mishchenko committed
149
        ABC_FREE( pKey ), ABC_FREE( pValue );
150
    st__free_table( pAbc->tFlags );
Alan Mishchenko committed
151

152
    Vec_PtrFreeFree( pAbc->aHistory );
Alan Mishchenko committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}



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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

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

Alan Mishchenko committed
173
    fClear = 0;
Alan Mishchenko committed
174
    Extra_UtilGetoptReset();
Alan Mishchenko committed
175
    while ( ( c = Extra_UtilGetopt( argc, argv, "ch" ) ) != EOF )
Alan Mishchenko committed
176 177 178
    {
        switch ( c )
        {
Alan Mishchenko committed
179 180 181
        case 'c':
            fClear ^= 1;
            break;
Alan Mishchenko committed
182 183 184 185 186 187 188
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
189 190 191 192 193 194 195
    if ( fClear )
    {
        pAbc->TimeTotal += pAbc->TimeCommand;
        pAbc->TimeCommand = 0.0;
        return 0;
    }

Alan Mishchenko committed
196
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
197 198 199 200
    {
        goto usage;
    }

Alan Mishchenko committed
201

Alan Mishchenko committed
202
    pAbc->TimeTotal += pAbc->TimeCommand;
203
    fprintf( pAbc->Out, "elapse: %3.2f seconds, total: %3.2f seconds\n",
Alan Mishchenko committed
204
        pAbc->TimeCommand, pAbc->TimeTotal );
Alan Mishchenko committed
205 206 207 208
/*
    {
        FILE * pTable;
        pTable = fopen( "runtimes.txt", "a+" );
Alan Mishchenko committed
209
        fprintf( pTable, "%4.2f\n", pAbc->TimeCommand );
Alan Mishchenko committed
210 211 212
        fclose( pTable );
    }
*/
Alan Mishchenko committed
213
    pAbc->TimeCommand = 0.0;
Alan Mishchenko committed
214 215 216
    return 0;

  usage:
Alan Mishchenko committed
217 218 219
    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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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
241 242
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "hn" ) ) != EOF )
Alan Mishchenko committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256
    {
        switch ( c )
        {
        case 'n':
            n = 0;
            break;
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

257 258 259 260 261 262 263 264 265 266 267 268
    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
269
        fflush ( pAbc->Out );
270
    }
Alan Mishchenko committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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
295 296
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "hs" ) ) != EOF )
Alan Mishchenko committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        case 's':
            return -2;
            break;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
311
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
312 313 314 315
        goto usage;
    return -1;

  usage:
316
    fprintf( pAbc->Err, "usage: quit [-sh]\n" );
Alan Mishchenko committed
317
    fprintf( pAbc->Err, "   -h  print the command usage\n" );
318
    fprintf( pAbc->Err, "   -s  frees all the memory before quitting\n" );
Alan Mishchenko committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
333
int CmdCommandAbcrc( Abc_Frame_t * pAbc, int argc, char **argv )
Alan Mishchenko committed
334
{
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    int c;

    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }
    Abc_UtilsSource( pAbc );
Alan Mishchenko committed
350
    return 0;
351 352 353 354 355

  usage:
    fprintf( pAbc->Err, "usage: abcrc [-h]\n" );
    fprintf( pAbc->Err, "   -h  sources \"abc.rc\" from the current/parent/grandparent directory\n" );
    return 1;
Alan Mishchenko committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandHistory( Abc_Frame_t * pAbc, int argc, char **argv )
{
371
    char * pName;
372
    int i, c;
373
    int nPrints = 20;
Alan Mishchenko committed
374
    Extra_UtilGetoptReset();
375
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
376 377 378 379 380
    {
        switch ( c )
        {
            case 'h':
                goto usage;
Alan Mishchenko committed
381 382
            default :
                goto usage;
Alan Mishchenko committed
383 384
        }
    }
385
    if ( argc > globalUtilOptind + 1 )
Alan Mishchenko committed
386
        goto usage;
387
    // get the number from the command line
388 389
    if ( argc == globalUtilOptind + 1 )
        nPrints = atoi(argv[globalUtilOptind]);
Alan Mishchenko committed
390
    // print the commands
391 392
    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
393 394
    return 0;

Alan Mishchenko committed
395
usage:
396 397 398 399
    fprintf( pAbc->Err, "usage: history [-h] <num>\n" );
    fprintf( pAbc->Err, "\t        lists the last commands entered on the command line\n" );
    fprintf( pAbc->Err, "\t-h    : print the command usage\n" );
    fprintf( pAbc->Err, "\t<num> : the maximum number of entries to show [default = %d]\n", nPrints );
Alan Mishchenko committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    return ( 1 );
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandAlias( Abc_Frame_t * pAbc, int argc, char **argv )
{
416 417
    const char *key;
    char *value;
Alan Mishchenko committed
418 419
    int c;

Alan Mishchenko committed
420 421
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }


    if ( argc == 1 )
    {
Alan Mishchenko committed
436
        CmdPrintTable( pAbc->tAliases, 1 );
Alan Mishchenko committed
437 438 439 440 441
        return 0;

    }
    else if ( argc == 2 )
    {
442
        if ( st__lookup( pAbc->tAliases, argv[1], &value ) )
Alan Mishchenko committed
443 444 445 446
            CmdCommandAliasPrint( pAbc, ( Abc_Alias * ) value );
        return 0;
    }

447
    // delete any existing alias
Alan Mishchenko committed
448
    key = argv[1];
449
    if ( st__delete( pAbc->tAliases, &key, &value ) )
Alan Mishchenko committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        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;
474 475
    const char *key;
    char *value;
Alan Mishchenko committed
476 477
    int c;

Alan Mishchenko committed
478 479
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

    if ( argc < 2 )
    {
        goto usage;
    }

    for ( i = 1; i < argc; i++ )
    {
        key = argv[i];
499
        if ( st__delete( pAbc->tAliases, &key, &value ) )
Alan Mishchenko committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
        {
            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 )
{
525
    int fPrintAll, fDetails;
Alan Mishchenko committed
526 527 528
    int c;

    fPrintAll = 0;
529
    fDetails = 0;
Alan Mishchenko committed
530
    Extra_UtilGetoptReset();
531
    while ( ( c = Extra_UtilGetopt( argc, argv, "adh" ) ) != EOF )
Alan Mishchenko committed
532 533 534 535 536 537 538 539
    {
        switch ( c )
        {
        case 'a':
            case 'v':
                fPrintAll ^= 1;
                break;
            break;
540 541 542
        case 'd':
            fDetails ^= 1;
            break;
Alan Mishchenko committed
543 544 545 546 547 548 549 550
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

Alan Mishchenko committed
551
    if ( argc != globalUtilOptind )
Alan Mishchenko committed
552 553
        goto usage;

554
    CmdCommandPrint( pAbc, fPrintAll, fDetails );
Alan Mishchenko committed
555 556 557
    return 0;

usage:
558
    fprintf( pAbc->Err, "usage: help [-a] [-d] [-h]\n" );
Alan Mishchenko committed
559 560
    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" );
561
    fprintf( pAbc->Err, " -d       print usage details to all commands [default = %s]\n", fDetails? "yes": "no" );
Alan Mishchenko committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    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;
582
    char *prompt_string, *real_filename, line[ABC_MAX_STR], *command;
Alan Mishchenko committed
583 584 585 586
    FILE *fp;

    interactive = silent = prompt = echo = 0;

Alan Mishchenko committed
587 588
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "ipsxh" ) ) != EOF )
Alan Mishchenko committed
589 590 591 592 593 594 595
    {
        switch ( c )
        {
        case 'i':               /* a hack to distinguish EOF from stdin */
            interactive = 1;
            break;
        case 'p':
Alan Mishchenko committed
596
            prompt ^= 1;
Alan Mishchenko committed
597 598
            break;
        case 's':
Alan Mishchenko committed
599
            silent ^= 1;
Alan Mishchenko committed
600 601 602 603
            break;
        case 'x':
            echo ^= 1;
            break;
Alan Mishchenko committed
604 605
        case 'h':
            goto usage;
Alan Mishchenko committed
606 607 608 609 610 611
        default:
            goto usage;
        }
    }

    /* added to avoid core-dumping when no script file is specified */
Alan Mishchenko committed
612
    if ( argc == globalUtilOptind )
Alan Mishchenko committed
613 614 615 616
    {
        goto usage;
    }

Alan Mishchenko committed
617
    lp_file_index = globalUtilOptind;
Alan Mishchenko committed
618 619 620 621 622 623 624
    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
625
     * -t options on "source" were used in SIS) would actually be executed twice.
Alan Mishchenko committed
626
     */
627
    pAbc->fSource = 1;
Alan Mishchenko committed
628 629
    do
    {
Alan Mishchenko committed
630 631 632 633 634 635 636 637 638
        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
639 640
        lp_count++;             /* increment the loop counter */

Alan Mishchenko committed
641
        fp = CmdFileOpen( pAbc, pFileName, "r", &real_filename, silent );
Alan Mishchenko committed
642 643
        if ( fp == NULL )
        {
644
            pAbc->fSource = 0;
Alan Mishchenko committed
645
            ABC_FREE( real_filename );
Alan Mishchenko committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
            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
661
                prompt_string = NULL;
Alan Mishchenko committed
662 663 664 665 666 667
            }

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

            /* read another command line */
668
            if ( fgets( line, ABC_MAX_STR, fp ) == NULL )
Alan Mishchenko committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
            {
                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
692
            if ( command == NULL )
Alan Mishchenko committed
693 694 695 696 697 698 699 700 701 702 703 704 705
            {
                status = 1;
                break;
            }
            if ( did_subst )
            {
                if ( interactive )
                {
                    fprintf( pAbc->Out, "%s\n", command );
                }
            }
            if ( command != line )
            {
706
                strcpy( line, command );
Alan Mishchenko committed
707 708 709
            }
            if ( interactive && *line != '\0' )
            {
710
                Cmd_HistoryAddCommand( pAbc, line );
Alan Mishchenko committed
711
                if ( pAbc->Hst != NULL )
Alan Mishchenko committed
712 713
                {
                    fprintf( pAbc->Hst, "%s\n", line );
714
                    fflush( pAbc->Hst );
Alan Mishchenko committed
715 716 717
                }
            }

Alan Mishchenko committed
718
            fflush( pAbc->Out );
Alan Mishchenko committed
719 720 721 722 723 724 725 726 727 728 729 730
            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 );
            }
731
            fclose( fp );
Alan Mishchenko committed
732
        }
Alan Mishchenko committed
733
        ABC_FREE( real_filename );
Alan Mishchenko committed
734 735 736

    }
    while ( ( status == 0 ) && ( lp_count <= 0 ) );
737
    pAbc->fSource = 0;
Alan Mishchenko committed
738 739 740
    return status;

  usage:
Alan Mishchenko committed
741 742 743 744
    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
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
    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 )
{
762 763
    char *flag_value, *value;
    const char* key;
Alan Mishchenko committed
764 765
    int c;

Alan Mishchenko committed
766 767
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    {
        switch ( c )
        {
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }
    if ( argc == 0 || argc > 3 )
    {
        goto usage;
    }
    else if ( argc == 1 )
    {
Alan Mishchenko committed
783
        CmdPrintTable( pAbc->tFlags, 0 );
Alan Mishchenko committed
784 785 786 787 788
        return 0;
    }
    else
    {
        key = argv[1];
789
        if ( st__delete( pAbc->tFlags, &key, &value ) )
Alan Mishchenko committed
790
        {
Alan Mishchenko committed
791 792
            ABC_FREE( key );
            ABC_FREE( value );
Alan Mishchenko committed
793 794
        }

Alan Mishchenko committed
795 796
        flag_value = argc == 2 ? Extra_UtilStrsav( "" ) : Extra_UtilStrsav( argv[2] );
//        flag_value = argc == 2 ? NULL : Extra_UtilStrsav(argv[2]);
797
        st__insert( pAbc->tFlags, Extra_UtilStrsav(argv[1]), flag_value );
Alan Mishchenko committed
798 799 800 801

        if ( strcmp( argv[1], "abcout" ) == 0 )
        {
            if ( pAbc->Out != stdout )
Alan Mishchenko committed
802
                fclose( pAbc->Out );
Alan Mishchenko committed
803 804
            if ( strcmp( flag_value, "" ) == 0 )
                flag_value = "-";
Alan Mishchenko committed
805
            pAbc->Out = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
806 807 808 809 810 811 812 813 814
            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
815
                fclose( pAbc->Err );
Alan Mishchenko committed
816 817
            if ( strcmp( flag_value, "" ) == 0 )
                flag_value = "-";
Alan Mishchenko committed
818
            pAbc->Err = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
819 820 821 822 823 824 825 826
            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
827
            if ( pAbc->Hst != NULL )
Alan Mishchenko committed
828
                fclose( pAbc->Hst );
Alan Mishchenko committed
829
            if ( strcmp( flag_value, "" ) == 0 )
Alan Mishchenko committed
830
                pAbc->Hst = NULL;
Alan Mishchenko committed
831 832
            else
            {
Alan Mishchenko committed
833
                pAbc->Hst = CmdFileOpen( pAbc, flag_value, "w", NULL, 0 );
Alan Mishchenko committed
834
                if ( pAbc->Hst == NULL )
Alan Mishchenko committed
835
                    pAbc->Hst = NULL;
Alan Mishchenko committed
836 837 838 839 840 841
            }
        }
        return 0;
    }

  usage:
Alan Mishchenko committed
842
    fprintf( pAbc->Err, "usage: set [-h] <name> <value>\n" );
Alan Mishchenko committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    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;
863 864
    const char *key;
    char *value;
Alan Mishchenko committed
865 866
    int c;

Alan Mishchenko committed
867 868
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }

    if ( argc < 2 )
    {
        goto usage;
    }

    for ( i = 1; i < argc; i++ )
    {
        key = argv[i];
888
        if ( st__delete( pAbc->tFlags, &key, &value ) )
Alan Mishchenko committed
889
        {
Alan Mishchenko committed
890 891
            ABC_FREE( key );
            ABC_FREE( value );
Alan Mishchenko committed
892 893 894 895 896 897
        }
    }
    return 0;


  usage:
Alan Mishchenko committed
898 899 900
    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
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    return 1;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandUndo( Abc_Frame_t * pAbc, int argc, char **argv )
{
917 918 919
    if ( argc == 2 && !strcmp(argv[1], "-h") )
        goto usage;

Alan Mishchenko committed
920 921 922 923 924 925 926 927
    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
928
    if ( argc == 1 )
Alan Mishchenko committed
929 930
        return CmdCommandRecall( pAbc, argc, argv );

931
usage:
Alan Mishchenko committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    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;

Alan Mishchenko committed
957 958
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
959 960 961 962 963 964 965 966 967
    {
        switch ( c )
        {
            case 'h':
                goto usage;
            default:
                goto usage;
        }
    }
968

969 970 971 972 973 974
    if ( pAbc->pNtkCur == NULL )
    {
        fprintf( pAbc->Out, "Empty network.\n" );
        return 0;
    }

Alan Mishchenko committed
975 976 977 978 979
    // 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;
980
    else
Alan Mishchenko committed
981 982 983 984
        nNetsToSave = atoi(pValue);

    // if there are no arguments on the command line
    // set the current network to be the network from the previous step
985
    if ( argc == 1 )
Alan Mishchenko committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    {
        // 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 );
1017
        else
Alan Mishchenko committed
1018 1019 1020 1021 1022
        {
            // 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) )
1023
                if ( (iStepFound = Abc_NtkStep(pNtk)) == iStep )
Alan Mishchenko committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
                    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
1046
    fprintf( pAbc->Err, "usage: recall -h <num>\n" );
Alan Mishchenko committed
1047 1048
    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
1049
    fprintf( pAbc->Err, "   -h :  print the command usage\n");
Alan Mishchenko committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    return 1;
}


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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

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

Alan Mishchenko committed
1069 1070
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
1071 1072 1073 1074 1075 1076 1077 1078 1079
    {
        switch ( c )
        {
            case 'h':
                goto usage;
            default:
                goto usage;
        }
    }
1080

1081 1082 1083 1084 1085 1086
    if ( pAbc->pNtkCur == NULL )
    {
        fprintf( pAbc->Out, "Empty network.\n" );
        return 0;
    }

Alan Mishchenko committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    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
1117
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
Alan Mishchenko committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    {
        switch ( c )
        {
        case 'h':
            goto usage;
            break;
        default:
            goto usage;
        }
    }
Alan Mishchenko committed
1128
    if (globalUtilOptind <= argc) {
Alan Mishchenko committed
1129 1130 1131 1132
    pNtkTemp = pAbc->pNtk;
    pAbc->pNtk = pAbc->pNtkSaved;
    pAbc->pNtkSaved = pNtkTemp;
    }
Alan Mishchenko committed
1133
    id = atoi(argv[globalUtilOptind]);
Alan Mishchenko committed
1134
    pNtkTemp = Cmd_HistoryGetSnapshot(pAbc, id);
1135
    if (!pNtkTemp)
Alan Mishchenko committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
    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


1150
#if defined(WIN32) && !defined(__cplusplus)
Alan Mishchenko committed
1151
#include <direct.h>
1152
#include <io.h>
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

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

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

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdCommandScanDir( Abc_Frame_t * pAbc, int argc, char **argv )
{
    struct _finddata_t c_file;
    char * pDirStr = NULL;
1169
    char*  pDirCur = NULL;
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    long   hFile;
    char   c;

    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "D") ) != EOF )
    {
        switch (c)
        {
            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;
            default:
                goto usage;
        }
    }
1191 1192


1193 1194
    if ( pDirStr )
    {
1195 1196 1197 1198 1199
        if( (pDirCur = _getcwd( NULL, 0 )) == NULL )
        {
            printf( "Cannot read current directory\n" );
            return 0;
        }
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 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        if ( _chdir(pDirStr) )
        {
            printf( "Cannot change to directory: %s\n", pDirStr );
            return 0;
        }
    }

    if( (hFile = _findfirst( "*.txt", &c_file )) == -1L )
    {
        if ( pDirStr )
            printf( "No .txt files in the current directory.\n" );
        else
            printf( "No .txt files in directory: %s\n", pDirStr );
    }
    else
    {
        do
        {
            FILE * pFile = fopen( c_file.name, "rb" );
            char * pStr1 = "Property UNDECIDED.  Time =";
            char * pStr2 = "Property proved.  Time =";
            char * pStr3 = "Time =";
            char * pBuffer, * pPlace, * pThis, * pThat;
            char FileName[100];
            float Time = 0;
            // get the file name
            sprintf( FileName, "%s", c_file.name );
            pThis = strrchr( FileName, '_' );
            pThat = strchr( FileName, '.' );
            if ( pThis == NULL || pThat == NULL || pThis >= pThat )
            {
//                printf( "Something is wrong with the file name %s\n", c_file.name );
                continue;
            }
            *pThat = 0;
            pThis++;
            // get the time
            if ( pFile == NULL )
            {
                printf( "Cannot open file %s\n", c_file.name );
                continue;
            }
            fclose( pFile );
            pBuffer = Extra_FileReadContents( c_file.name );
            pPlace = strstr( pBuffer, pStr1 );
            if ( pPlace == NULL )
            {
                pPlace = strstr( pBuffer, pStr2 );
                if ( pPlace == NULL )
                {
                    pPlace = strstr( pBuffer, pStr3 );
                    if ( pPlace == NULL )
                    {
//                        printf( "Cannot find substrings in file %s\n", c_file.name );
                        ABC_FREE( pBuffer );
                        continue;
                    }
                    else
                        pPlace += strlen( pStr3 );
                }
                else
                    pPlace += strlen( pStr2 );
            }
            else
                pPlace += strlen( pStr1 );
            sscanf( pPlace, "%f", &Time );
            printf( "%s %.2f\n", pThis, Time );
            ABC_FREE( pBuffer );
        }
        while( _findnext( hFile, &c_file ) == 0 );
        _findclose( hFile );
    }
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
    if ( pDirStr )
    {
        if ( _chdir(pDirCur) )
        {
            ABC_FREE( pDirCur );
            printf( "Cannot change to directory: %s\n", pDirCur );
            return 0;
        }
        ABC_FREE( pDirCur );
    }
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    return 0;

usage:
    fprintf( pAbc->Err, "usage: scandir [-D string]\n" );
    fprintf( pAbc->Err, "            performs custom scanning of the files in the given directory\n" );
    fprintf( pAbc->Err, "\t-D str  : the directory to read files from [default = current]\n" );
    return 1;
}



Alan Mishchenko committed
1293 1294
/**Function*************************************************************

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
  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmfFindNumber( char * pName )
{
    char * pTemp;
    for ( pTemp = pName; *pTemp; pTemp++ )
        if ( *pTemp == '.' )
            break;
    if ( *pTemp == 0 )
        return -1;
    for ( --pTemp; pTemp > pName; pTemp-- )
        if ( *pTemp < '0' || *pTemp > '9' )
        {
            pTemp++;
            break;
        }
    if ( *pTemp == '.' )
        return -2;
    return atoi( pTemp );
}

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

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
  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void CnfDupFileUnzip( char * pOldName )
{
    extern char * Io_MvLoadFileBz2( char * pFileName, int * pnFileSize );
    char pNewName[1000];
    FILE * pFile;
    int nFileSize;
    char * pBuffer = Io_MvLoadFileBz2( pOldName, &nFileSize );
    assert( strlen(pOldName) < 1000 );
    sprintf( pNewName, "%s.v", pOldName );
    pFile = fopen( pNewName, "wb" );
    fwrite( pBuffer, nFileSize, 1, pFile );
    fclose( pFile );
    ABC_FREE( pBuffer );
}

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

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 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 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
  Synopsis    [Command to print the contents of the current directory (Windows).]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdCommandRenameFiles( Abc_Frame_t * pAbc, int argc, char **argv )
{
    struct _finddata_t c_file;
    long   hFile;
    char pNewName[1000];
    char * pDirStr = NULL;
    char * pDirCur = NULL;
    char * pNameNew = NULL;
    char * pNameExt = NULL;
    int c, i, nBase = 0;
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "DENB") ) != EOF )
    {
        switch (c)
        {
            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 'E':
                if ( globalUtilOptind >= argc )
                {
                    fprintf( pAbc->Err, "Command line switch \"-E\" should be followed by a string.\n" );
                    goto usage;
                }
                pNameExt = argv[globalUtilOptind];
                globalUtilOptind++;
                break;
            case 'N':
                if ( globalUtilOptind >= argc )
                {
                    fprintf( pAbc->Err, "Command line switch \"-N\" should be followed by a string.\n" );
                    goto usage;
                }
                pNameNew = argv[globalUtilOptind];
                globalUtilOptind++;
                break;
            case 'B':
                if ( globalUtilOptind >= argc )
                {
                    fprintf( pAbc->Err, "Command line switch \"-B\" should be followed by a positive integer.\n" );
                    goto usage;
                }
                nBase = atoi(argv[globalUtilOptind]);
                globalUtilOptind++;
                if ( nBase < 0 )
                    goto usage;
                break;
            default:
                goto usage;
        }
    }

    if ( pNameExt == NULL )
    {
        printf( "Extension of the files should be given on the command line.\n" );
        return 0;
    }

    if ( pDirStr )
    {
        if( (pDirCur = _getcwd( NULL, 0 )) == NULL )
        {
            printf( "Cannot read current directory\n" );
            return 0;
        }
        if ( _chdir(pDirStr) )
        {
            printf( "Cannot change to directory: %s\n", pDirStr );
            return 0;
        }
    }

    sprintf( pNewName, "*.%s", pNameExt );
    if( (hFile = _findfirst( pNewName, &c_file )) == -1L )
    {
        if ( pDirStr )
            printf( "No .aig files in the current directory.\n" );
        else
            printf( "No .aig files in directory: %s\n", pDirStr );
    }
    else
    {
        char * pName, * pOldName;
        int nDigits, * pOrder;
        Vec_Ptr_t * vNames = Vec_PtrAlloc( 1000 );
        Vec_Int_t * vNums  = Vec_IntAlloc( 1000 );
        // collect names
        do {
            Vec_PtrPush( vNames, Abc_UtilStrsav(c_file.name) );
        } while( _findnext( hFile, &c_file ) == 0 );
        _findclose( hFile );
        // sort files by number
        Vec_PtrForEachEntry( char *, vNames, pName, i )
        {
            Vec_IntPush( vNums, CmfFindNumber(pName) );
            if ( Vec_IntEntryLast(vNums) < 0 )
            {
                printf( "Directory \"%s\" contains file (%s) with extension %s without number\n", pDirStr, pName, pNameExt );
                Vec_PtrFreeFree( vNames );
                Vec_IntFree( vNums );
                return 0;
            }
        }
        // sort by number
        pOrder = Abc_QuickSortCost( Vec_IntArray(vNums), Vec_IntSize(vNums), 0 );
        // rename files in that order
1472 1473
//        nDigits = Abc_Base10Log( nBase + Vec_IntSize(vNums) );
        nDigits = Abc_Base10Log( nBase + Vec_IntEntry(vNums, pOrder[Vec_IntSize(vNums)-1]) + 1 );
1474 1475 1476
        for ( i = 0; i < Vec_IntSize(vNums); i++ )
        {
            pOldName = (char *)Vec_PtrEntry( vNames, pOrder[i] );
1477
            sprintf( pNewName, "%s%0*d.%s", pNameNew ? pNameNew : "", nDigits, nBase+Vec_IntEntry(vNums, pOrder[i]), pNameExt );
1478
            rename( pOldName, pNewName );
1479 1480
            printf( "%s -> %s\n", pOldName, pNewName );
//            CnfDupFileUnzip( pOldName );
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
        }
        // cleanup
        Vec_PtrFreeFree( vNames );
        Vec_IntFree( vNums );
        ABC_FREE( pOrder );
    }
    if ( pDirStr )
    {
        if ( _chdir(pDirCur) )
        {
            ABC_FREE( pDirCur );
            printf( "Cannot change to directory: %s\n", pDirCur );
            return 0;
        }
        ABC_FREE( pDirCur );
    }
    return 0;

usage:
    fprintf( pAbc->Err, "usage: renamefiles [-DEN str] [-B num]\n" );
    fprintf( pAbc->Err, "            performs renaming of files in the given directory\n" );
    fprintf( pAbc->Err, "\t-D str  : the directory to read files from [default = current]\n" );
    fprintf( pAbc->Err, "\t-E str  : the extension of files to look for [default = none]\n" );
    fprintf( pAbc->Err, "\t-N str  : the root of the resulting files [default = none]\n" );
    fprintf( pAbc->Err, "\t-B num  : the base number for all files [default = %d]\n", nBase );
    return 1;
}


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

Alan Mishchenko committed
1512 1513 1514
  Synopsis    [Command to print the contents of the current directory (Windows).]

  Description []
1515

Alan Mishchenko committed
1516 1517 1518 1519 1520
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
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
1532 1533
    Extra_UtilGetoptReset();
    while ( (c = Extra_UtilGetopt(argc, argv, "lb") ) != EOF )
Alan Mishchenko committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    {
        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
1605
    fprintf( pAbc->Err, "usage: ls [-l] [-b]\n" );
Alan Mishchenko committed
1606 1607 1608
    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" );
1609
    return 1;
Alan Mishchenko committed
1610 1611 1612
}


Alan Mishchenko committed
1613 1614 1615 1616 1617
/**Function*************************************************************

  Synopsis    [Generates the script for running ABC.]

  Description []
1618

Alan Mishchenko committed
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
  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 &&
1747 1748
                 c_file.name[nFileNameCur-1] == 's' &&
                 c_file.name[nFileNameCur-2] == '.' )
Alan Mishchenko committed
1749 1750
                 continue;
            if ( nFileNameCur > 4 &&
1751 1752 1753 1754
                 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
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
                 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() );
1773
        fprintf( pFile, "# Command line was: scrgen -F %s -D %s -C \"%s\"%s%s\n",
Alan Mishchenko committed
1774 1775 1776 1777 1778 1779 1780 1781
            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 &&
1782 1783
                 c_file.name[nFileNameCur-1] == 's' &&
                 c_file.name[nFileNameCur-2] == '.' )
Alan Mishchenko committed
1784 1785
                 continue;
            if ( nFileNameCur > 4 &&
1786 1787 1788 1789
                 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
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 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
                 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" );
1833
    return 1;
Alan Mishchenko committed
1834 1835 1836
}
#endif

Alan Mishchenko committed
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856

#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
1857 1858 1859
    Abc_Ntk_t * pNtk, * pNtkNew, * pNetlist;
    char * pNameWin = "sis.exe";
    char * pNameUnix = "sis";
Alan Mishchenko committed
1860 1861 1862 1863
    char Command[1000], Buffer[100];
    char * pSisName;
    int i;

Alan Mishchenko committed
1864
    pNtk = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
1865 1866 1867
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

1868 1869 1870 1871 1872 1873 1874
    if ( argc == 1 )
        goto usage;
    if ( strcmp( argv[1], "-h" ) == 0 )
        goto usage;
    if ( strcmp( argv[1], "-?" ) == 0 )
        goto usage;

Alan Mishchenko committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
    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;
    }

Alan Mishchenko committed
1887 1888 1889 1890 1891 1892
    // 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
1893
    // check if SIS is available
Alan Mishchenko committed
1894 1895 1896 1897
    if ( (pFile = fopen( pNameWin, "r" )) )
        pSisName = pNameWin;
    else if ( (pFile = fopen( pNameUnix, "r" )) )
        pSisName = pNameUnix;
Alan Mishchenko committed
1898 1899
    else if ( pFile == NULL )
    {
Alan Mishchenko committed
1900
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pNameWin, pNameUnix );
Alan Mishchenko committed
1901 1902 1903 1904
        goto usage;
    }
    fclose( pFile );

Alan Mishchenko committed
1905 1906 1907 1908 1909 1910
    if ( Abc_NtkIsMappedLogic(pNtk) )
    {
        Abc_NtkMapToSop(pNtk);
        printf( "The current network is unmapped before calling SIS.\n" );
    }

Alan Mishchenko committed
1911
    // write out the current network
Alan Mishchenko committed
1912
    if ( Abc_NtkIsLogic(pNtk) )
1913
        Abc_NtkToSop(pNtk, -1, ABC_INFINITY);
Alan Mishchenko committed
1914 1915 1916 1917 1918 1919
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
1920
    Io_WriteBlif( pNetlist, "_sis_in.blif", 1, 0, 0 );
Alan Mishchenko committed
1921
    Abc_NtkDelete( pNetlist );
Alan Mishchenko committed
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937

    // 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
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
    {
        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
1956
    pNtkNew = Io_Read( "_sis_out.blif", IO_FILE_BLIF, 1, 0 );
Alan Mishchenko committed
1957 1958 1959
    // set the original spec of the new network
    if ( pNtk->pSpec )
    {
Alan Mishchenko committed
1960
        ABC_FREE( pNtkNew->pSpec );
Alan Mishchenko committed
1961
        pNtkNew->pSpec = Extra_UtilStrsav( pNtk->pSpec );
Alan Mishchenko committed
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
    }
    // replace the current network
    Abc_FrameReplaceCurrentNetwork( pAbc, pNtkNew );

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

usage:
    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" );
1980
    return 1;                   // error exit
Alan Mishchenko committed
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
}


/**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
1999
    Abc_Ntk_t * pNtk, * pNtkNew, * pNetlist;
Alan Mishchenko committed
2000
    char Command[1000], Buffer[100];
Alan Mishchenko committed
2001 2002
    char * pNameWin = "mvsis.exe";
    char * pNameUnix = "mvsis";
Alan Mishchenko committed
2003 2004 2005
    char * pMvsisName;
    int i;

Alan Mishchenko committed
2006
    pNtk = Abc_FrameReadNtk(pAbc);
Alan Mishchenko committed
2007 2008 2009
    pOut = Abc_FrameReadOut(pAbc);
    pErr = Abc_FrameReadErr(pAbc);

2010 2011 2012 2013 2014 2015 2016
    if ( argc == 1 )
        goto usage;
    if ( strcmp( argv[1], "-h" ) == 0 )
        goto usage;
    if ( strcmp( argv[1], "-?" ) == 0 )
        goto usage;

Alan Mishchenko committed
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    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;
    }

Alan Mishchenko committed
2029 2030 2031 2032 2033 2034
    // 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
2035
    // check if MVSIS is available
Alan Mishchenko committed
2036 2037 2038 2039
    if ( (pFile = fopen( pNameWin, "r" )) )
        pMvsisName = pNameWin;
    else if ( (pFile = fopen( pNameUnix, "r" )) )
        pMvsisName = pNameUnix;
Alan Mishchenko committed
2040 2041
    else if ( pFile == NULL )
    {
Alan Mishchenko committed
2042
        fprintf( pErr, "Cannot find \"%s\" or \"%s\" in the current directory.\n", pNameWin, pNameUnix );
Alan Mishchenko committed
2043 2044 2045 2046
        goto usage;
    }
    fclose( pFile );

Alan Mishchenko committed
2047 2048 2049 2050 2051
    if ( Abc_NtkIsMappedLogic(pNtk) )
    {
        Abc_NtkMapToSop(pNtk);
        printf( "The current network is unmapped before calling MVSIS.\n" );
    }
Alan Mishchenko committed
2052 2053

    // write out the current network
Alan Mishchenko committed
2054
    if ( Abc_NtkIsLogic(pNtk) )
2055
        Abc_NtkToSop(pNtk, -1, ABC_INFINITY);
Alan Mishchenko committed
2056 2057 2058 2059 2060 2061
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
2062
    Io_WriteBlif( pNetlist, "_mvsis_in.blif", 1, 0, 0 );
Alan Mishchenko committed
2063
    Abc_NtkDelete( pNetlist );
Alan Mishchenko committed
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079

    // 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
2080
    if ( Util_SignalSystem( Command ) )
Alan Mishchenko committed
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
    {
        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
2098
    pNtkNew = Io_Read( "_mvsis_out.blif", IO_FILE_BLIF, 1, 0 );
Alan Mishchenko committed
2099 2100 2101
    // set the original spec of the new network
    if ( pNtk->pSpec )
    {
Alan Mishchenko committed
2102
        ABC_FREE( pNtkNew->pSpec );
Alan Mishchenko committed
2103
        pNtkNew->pSpec = Extra_UtilStrsav( pNtk->pSpec );
Alan Mishchenko committed
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
    }
    // replace the current network
    Abc_FrameReplaceCurrentNetwork( pAbc, pNtkNew );

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

usage:
    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" );
2122
    return 1;                   // error exit
Alan Mishchenko committed
2123 2124 2125
}


Alan Mishchenko committed
2126 2127 2128 2129 2130
/**Function*************************************************************

  Synopsis    [Computes dimentions of the graph.]

  Description []
2131

Alan Mishchenko committed
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManGnuplotShow( char * pPlotFileName )
{
    FILE * pFile;
    void * pAbc;
    char * pProgNameGnuplotWin  = "wgnuplot.exe";
    char * pProgNameGnuplotUnix = "gnuplot";
Alan Mishchenko committed
2143
    char * pProgNameGnuplot = NULL;
Alan Mishchenko committed
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155

    // 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
2156 2157 2158 2159
    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
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192

    // 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
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
/**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);

2223 2224 2225 2226 2227 2228 2229 2230
    if ( argc > 1 )
    {
        if ( strcmp( argv[1], "-h" ) == 0 )
            goto usage;
        if ( strcmp( argv[1], "-?" ) == 0 )
            goto usage;
    }

Alan Mishchenko committed
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
    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;
    }

    // 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) )
2269
        Abc_NtkToSop(pNtk, -1, ABC_INFINITY);
Alan Mishchenko committed
2270 2271 2272 2273 2274 2275
    pNetlist = Abc_NtkToNetlist(pNtk);
    if ( pNetlist == NULL )
    {
        fprintf( pErr, "Cannot produce the intermediate network.\n" );
        goto usage;
    }
2276
    Io_WriteBlif( pNetlist, "_capo_in.blif", 1, 0, 0 );
Alan Mishchenko committed
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
    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
2291
    if ( Util_SignalSystem( Command ) )
Alan Mishchenko committed
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
    {
        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
Alan Mishchenko committed
2322
    pProgNameGnuplot = NULL;
Alan Mishchenko committed
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
    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 );
2344
        if ( Util_SignalSystem( Command ) == -1 )
Alan Mishchenko committed
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
        {
            fprintf( stdout, "Cannot execute \"%s\".\n", Command );
            goto usage;
        }
    }
#endif

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

usage:
    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" );
2374
    return 1;                   // error exit
Alan Mishchenko committed
2375 2376
}

2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
2388
int CmdCommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv )
2389
{
2390
    extern void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores );
2391 2392
    FILE * pFile;
    char * pFileName;
2393
    char * pCommand = NULL;
2394 2395 2396
    int c, nCores    =  3;
    int fVerbose     =  0;
    Extra_UtilGetoptReset();
2397
    while ( ( c = Extra_UtilGetopt( argc, argv, "NCvh" ) ) != EOF )
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
    {
        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;
2412 2413 2414 2415 2416 2417 2418 2419 2420
        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;
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
        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
2448
    Cmd_RunStarter( pFileName, pAbc->sBinary, pCommand, nCores );
2449 2450 2451
    return 0;

usage:
2452
    Abc_Print( -2, "usage: starter [-N num] [-C cmd] [-vh] <file>\n" );
2453 2454
    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 );
2455
    Abc_Print( -2, "\t-C cmd : (optional) ABC command line to execute on benchmarks in <file>\n" );
2456 2457
    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");
2458
    Abc_Print( -2, "\t<file> : file name with ABC command lines (or benchmark names, if <cmd> is given)\n");
2459 2460 2461
    return 1;
}

2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdCommandAutoTuner( Abc_Frame_t * pAbc, int argc, char ** argv )
{
    extern void Cmd_RunAutoTuner( char * pConfig, char * pFileList, int nCores );
    FILE * pFile;
    char * pFileConf = NULL;
    char * pFileList = NULL;
    char * pFileName;
    int c, nCores    =  3;
    int fVerbose     =  0;
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "NCFvh" ) ) != EOF )
    {
        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;
        case 'C':
            if ( globalUtilOptind >= argc )
            {
                Abc_Print( -1, "Command line switch \"-C\" should be followed by a string (possibly in quotes).\n" );
                goto usage;
            }
            pFileConf = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'F':
            if ( globalUtilOptind >= argc )
            {
                Abc_Print( -1, "Command line switch \"-F\" should be followed by a string (possibly in quotes).\n" );
                goto usage;
            }
            pFileList = argv[globalUtilOptind];
            globalUtilOptind++;
            break;
        case 'v':
            fVerbose ^= 1;
            break;
        case 'h':
            goto usage;
        default:
            goto usage;
        }
    }
    if ( pFileConf == NULL )
    {
        Abc_Print( -2, "File containing configuration for autotuning is not given.\n" );
        return 1;
    }
    if ( pFileList == NULL )
    {
        Abc_Print( -2, "File contining list of files for autotuning is not given.\n" );
        return 1;
    }
    // get the input file name
    pFileName = pFileConf;
    if ( (pFile = Io_FileOpen( pFileName, "open_path", "rb", 0 )) == NULL )
//    if ( (pFile = fopen( pFileName, "rb" )) == NULL )
    {
        Abc_Print( -2, "Cannot open configuration 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 );
    // get the input file name
    pFileName = pFileList;
    if ( (pFile = Io_FileOpen( pFileName, "open_path", "rb", 0 )) == NULL )
//    if ( (pFile = fopen( pFileName, "rb" )) == NULL )
    {
        Abc_Print( -2, "Cannot open the file list \"%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
    Cmd_RunAutoTuner( pFileConf, pFileList, nCores );
    return 0;

usage:
    Abc_Print( -2, "usage: autotuner [-N num] [-C file] [-F file] [-vh]\n" );
2565
    Abc_Print( -2, "\t         performs autotuning\n" );
2566
    Abc_Print( -2, "\t-N num : the number of concurrent jobs including the controler [default = %d]\n", nCores );
2567 2568
    Abc_Print( -2, "\t-C cmd : configuration file with settings for autotuning\n" );
    Abc_Print( -2, "\t-F cmd : list of AIGER files to be used for autotuning\n" );
2569 2570 2571 2572 2573
    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");
    return 1;
}

Alan Mishchenko committed
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
/**Function********************************************************************

  Synopsis    [Print the version string.]

  Description []

  SideEffects []

  SeeAlso     []

******************************************************************************/
int CmdCommandVersion( Abc_Frame_t * pAbc, int argc, char **argv )
{
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
    int c;

    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "h" ) ) != EOF )
    {
        switch ( c )
        {
            case 'h':
                goto usage;
            default:
                goto usage;
        }
    }

Alan Mishchenko committed
2601 2602
    printf("%s\n", Abc_UtilsGetVersion(pAbc));
    return 0;
2603 2604 2605 2606 2607 2608

usage:
    fprintf( pAbc->Err, "usage: version [-h]\n" );
    fprintf( pAbc->Err, "         print the version string\n" );
    fprintf( pAbc->Err, "   -h :  print the command usage\n");
    return 1;
Alan Mishchenko committed
2609
}
Alan Mishchenko committed
2610 2611


Alan Mishchenko committed
2612 2613 2614 2615 2616
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


2617
ABC_NAMESPACE_IMPL_END