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

  FileName    [cmdUtils.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Various utilities of the command package.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

21 22
#include "base/abc/abc.h"
#include "base/main/mainInt.h"
Alan Mishchenko committed
23
#include "cmdInt.h"
24 25 26 27
#include <ctype.h>

ABC_NAMESPACE_IMPL_START
    // proper declaration of isspace
Alan Mishchenko committed
28 29 30 31 32 33 34 35

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

static int CmdCommandPrintCompare( Abc_Command ** ppC1, Abc_Command ** ppC2 );

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int cmdCheckShellEscape( Abc_Frame_t * pAbc, int argc, char ** argv)
{
52
    int RetValue;
Alan Mishchenko committed
53 54 55 56
    if (argv[0][0] == '!') 
    {
        const int size = 4096;
        int i;
57
        char * buffer = ABC_ALLOC(char, 10000);
Alan Mishchenko committed
58 59 60 61 62 63 64 65
        strncpy (buffer, &argv[0][1], size);
        for (i = 1; i < argc; ++i)
        {
                strncat (buffer, " ", size);
                strncat (buffer, argv[i], size);
        }
        if (buffer[0] == 0) 
            strncpy (buffer, "/bin/sh", size);
66 67
        RetValue = system (buffer);
        ABC_FREE( buffer );
Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

        // NOTE: Since we reconstruct the cmdline by concatenating
        // the parts, we lose information. So a command like
        // `!ls "file name"` will be sent to the system as
        // `ls file name` which is a BUG

        return 1;
    }
    else
    {
        return 0;
    }
}

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

  Synopsis    [Executes one command.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
93
int CmdCommandDispatch( Abc_Frame_t * pAbc, int * pargc, char *** pargv )
Alan Mishchenko committed
94
{
Alan Mishchenko committed
95 96 97 98
    int argc = *pargc;
    char ** argv = *pargv;
    char ** argv2;

Alan Mishchenko committed
99 100 101 102 103
    Abc_Ntk_t * pNetCopy;
    int (*pFunc) ( Abc_Frame_t *, int, char ** );
    Abc_Command * pCommand;
    char * value;
    int fError;
Alan Mishchenko committed
104
    double clk;
Alan Mishchenko committed
105 106 107 108 109 110 111 112

    if ( argc == 0 )
        return 0;

    if ( cmdCheckShellEscape( pAbc, argc, argv ) == 1 )
        return 0;

    // get the command
113
    if ( ! st__lookup( pAbc->tCommands, argv[0], (char **)&pCommand ) )
Alan Mishchenko committed
114
    {   // the command is not in the table
115 116 117 118 119 120 121 122 123 124
        // if there is only one word with an extension, assume this is file to be read
        if ( argc == 1 && strstr( argv[0], "." ) )
        {
            // add command 'read' assuming that this is the file name
            argv2 = CmdAddToArgv( argc, argv );
            CmdFreeArgv( argc, argv );
            argc = argc+1;
            argv = argv2;
            *pargc = argc;
            *pargv = argv;
125
            if ( ! st__lookup( pAbc->tCommands, argv[0], (char **)&pCommand ) )
126 127 128 129 130
                assert( 0 );
        }
        else
        {
            fprintf( pAbc->Err, "** cmd error: unknown command '%s'\n", argv[0] );
131 132
            fprintf( pAbc->Err, "(this is likely caused by using an alias defined in \"abc.rc\"\n" );
            fprintf( pAbc->Err, "without having this file in the current or parent directory)\n" );
133 134
            return 1;
        }
Alan Mishchenko committed
135 136 137 138 139
    }

    // get the backup network if the command is going to change the network
    if ( pCommand->fChange ) 
    {
Alan Mishchenko committed
140
        if ( pAbc->pNtkCur && Abc_FrameIsFlagEnabled( "backup" ) )
Alan Mishchenko committed
141 142 143 144 145 146 147 148 149 150
        {
            pNetCopy = Abc_NtkDup( pAbc->pNtkCur );
            Abc_FrameSetCurrentNetwork( pAbc, pNetCopy );
            // swap the current network and the backup network 
            // to prevent the effect of resetting the short names
            Abc_FrameSwapCurrentAndBackup( pAbc );
        }
    }

    // execute the command
Alan Mishchenko committed
151
    clk = Extra_CpuTimeDouble();
Alan Mishchenko committed
152
    pFunc = (int (*)(Abc_Frame_t *, int, char **))pCommand->pFunc;
Alan Mishchenko committed
153
    fError = (*pFunc)( pAbc, argc, argv );
Alan Mishchenko committed
154
    pAbc->TimeCommand += Extra_CpuTimeDouble() - clk;
Alan Mishchenko committed
155 156 157 158 159

    // automatic execution of arbitrary command after each command 
    // usually this is a passive command ... 
    if ( fError == 0 && !pAbc->fAutoexac )
    {
160
        if ( st__lookup( pAbc->tFlags, "autoexec", &value ) )
Alan Mishchenko committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        {
            pAbc->fAutoexac = 1;
            fError = Cmd_CommandExecute( pAbc, value );
            pAbc->fAutoexac = 0;
        }
    }
    return fError;
}

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

  Synopsis    [Splits the command line string into individual commands.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
181
const char * CmdSplitLine( Abc_Frame_t * pAbc, const char *sCommand, int *argc, char ***argv )
Alan Mishchenko committed
182
{
183 184
    const char *p, *start;
    char c;
Alan Mishchenko committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    int i, j;
    char *new_arg;
    Vec_Ptr_t * vArgs;
    int single_quote, double_quote;

    vArgs = Vec_PtrAlloc( 10 );

    p = sCommand;
    for ( ;; )
    {
        // skip leading white space 
        while ( isspace( ( int ) *p ) )
        {
            p++;
        }

        // skip until end of this token 
        single_quote = double_quote = 0;
        for ( start = p; ( c = *p ) != '\0'; p++ )
        {
            if ( c == ';' || c == '#' || isspace( ( int ) c ) )
            {
                if ( !single_quote && !double_quote )
                {
                    break;
                }
            }
            if ( c == '\'' )
            {
                single_quote = !single_quote;
            }
            if ( c == '"' )
            {
                double_quote = !double_quote;
            }
        }
        if ( single_quote || double_quote )
        {
            ( void ) fprintf( pAbc->Err, "** cmd warning: ignoring unbalanced quote ...\n" );
        }
        if ( start == p )
            break;

Alan Mishchenko committed
228
        new_arg = ABC_ALLOC( char, p - start + 1 );
Alan Mishchenko committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        j = 0;
        for ( i = 0; i < p - start; i++ )
        {
            c = start[i];
            if ( ( c != '\'' ) && ( c != '\"' ) )
            {
                new_arg[j++] = isspace( ( int ) c ) ? ' ' : start[i];
            }
        }
        new_arg[j] = '\0';
        Vec_PtrPush( vArgs, new_arg );
    }

    *argc = vArgs->nSize;
    *argv = (char **)Vec_PtrReleaseArray( vArgs );
    Vec_PtrFree( vArgs );
    if ( *p == ';' )
    {
        p++;
    }
    else if ( *p == '#' )
    {
        for ( ; *p != 0; p++ ); // skip to end of line 
    }
    return p;
}

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

  Synopsis    [Replaces parts of the command line string by aliases if given.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdApplyAlias( Abc_Frame_t * pAbc, int *argcp, char ***argvp, int *loop )
{
    int i, argc, stopit, added, offset, did_subst, subst, fError, newc, j;
270 271
    const char *arg;
    char **argv, **newv;
Alan Mishchenko committed
272 273 274 275 276
    Abc_Alias *alias;

    argc = *argcp;
    argv = *argvp;
    stopit = 0;
Alan Mishchenko committed
277
    for ( ; *loop < 200; ( *loop )++ )
Alan Mishchenko committed
278 279 280
    {
        if ( argc == 0 )
            return 0;
281
        if ( stopit != 0 || st__lookup( pAbc->tAliases, argv[0],  (char **) &alias ) == 0 )
Alan Mishchenko committed
282 283 284 285 286 287 288
        {
            return 0;
        }
        if ( strcmp( argv[0], alias->argv[0] ) == 0 )
        {
            stopit = 1;
        }
Alan Mishchenko committed
289
        ABC_FREE( argv[0] );
Alan Mishchenko committed
290 291 292 293 294
        added = alias->argc - 1;

        /* shift all the arguments to the right */
        if ( added != 0 )
        {
Alan Mishchenko committed
295
            argv = ABC_REALLOC( char *, argv, argc + added );
Alan Mishchenko committed
296 297 298 299 300 301
            for ( i = argc - 1; i >= 1; i-- )
            {
                argv[i + added] = argv[i];
            }
            for ( i = 1; i <= added; i++ )
            {
Alan Mishchenko committed
302
                argv[i] = NULL;
Alan Mishchenko committed
303 304 305 306 307 308 309
            }
            argc += added;
        }
        subst = 0;
        for ( i = 0, offset = 0; i < alias->argc; i++, offset++ )
        {
            arg = CmdHistorySubstitution( pAbc, alias->argv[i], &did_subst );
Alan Mishchenko committed
310
            if ( arg == NULL )
Alan Mishchenko committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
            {
                *argcp = argc;
                *argvp = argv;
                return ( 1 );
            }
            if ( did_subst != 0 )
            {
                subst = 1;
            }
            fError = 0;
            do
            {
                arg = CmdSplitLine( pAbc, arg, &newc, &newv );
                /*
                 * If there's a complete `;' terminated command in `arg',
                 * when split_line() returns arg[0] != '\0'.
                 */
                if ( arg[0] == '\0' )
                { /* just a bunch of words */
                    break;
                }
                fError = CmdApplyAlias( pAbc, &newc, &newv, loop );
                if ( fError == 0 )
                {
Alan Mishchenko committed
335
                       fError = CmdCommandDispatch( pAbc, &newc, &newv );
Alan Mishchenko committed
336 337 338 339 340 341 342 343 344 345 346 347 348
                }
                CmdFreeArgv( newc, newv );
            }
            while ( fError == 0 );
            if ( fError != 0 )
            {
                *argcp = argc;
                *argvp = argv;
                return ( 1 );
            }
            added = newc - 1;
            if ( added != 0 )
            {
Alan Mishchenko committed
349
                argv = ABC_REALLOC( char *, argv, argc + added );
Alan Mishchenko committed
350 351 352 353 354 355 356 357 358 359
                for ( j = argc - 1; j > offset; j-- )
                {
                    argv[j + added] = argv[j];
                }
                argc += added;
            }
            for ( j = 0; j <= added; j++ )
            {
                argv[j + offset] = newv[j];
            }
Alan Mishchenko committed
360
            ABC_FREE( newv );
Alan Mishchenko committed
361 362 363 364 365 366
            offset += added;
        }
        if ( subst == 1 )
        {
            for ( i = offset; i < argc; i++ )
            {
Alan Mishchenko committed
367
                ABC_FREE( argv[i] );
Alan Mishchenko committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            }
            argc = offset;
        }
        *argcp = argc;
        *argvp = argv;
    }

    fprintf( pAbc->Err, "** cmd warning: alias loop\n" );
    return 1;
}

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

  Synopsis    [Performs history substitution (now, disabled).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * CmdHistorySubstitution( Abc_Frame_t * pAbc, char *line, int *changed )
{
    // as of today, no history substitution 
    *changed = 0;
    return line;
}

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

  Synopsis    [Opens the file with path (now, disabled).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
FILE * CmdFileOpen( Abc_Frame_t * pAbc, char *sFileName, char *sMode, char **pFileNameReal, int silent )
{
    char * sRealName, * sPathUsr, * sPathLib, * sPathAll;
    FILE * pFile;
    
    if (strcmp(sFileName, "-") == 0) {
        if (strcmp(sMode, "w") == 0) {
Alan Mishchenko committed
415
            sRealName = Extra_UtilStrsav( "stdout" );
Alan Mishchenko committed
416 417 418
            pFile = stdout;
        }
        else {
Alan Mishchenko committed
419
            sRealName = Extra_UtilStrsav( "stdin" );
Alan Mishchenko committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            pFile = stdin;
        }
    }
    else {
        sRealName = NULL;
        if (strcmp(sMode, "r") == 0) {
            
            /* combine both pathes if exist */
            sPathUsr = Cmd_FlagReadByName(pAbc,"open_path");
            sPathLib = Cmd_FlagReadByName(pAbc,"lib_path");
            
            if ( sPathUsr == NULL && sPathLib == NULL ) {
                sPathAll = NULL;
            }
            else if ( sPathUsr == NULL ) {
Alan Mishchenko committed
435
                sPathAll = Extra_UtilStrsav( sPathLib );
Alan Mishchenko committed
436 437
            }
            else if ( sPathLib == NULL ) {
Alan Mishchenko committed
438
                sPathAll = Extra_UtilStrsav( sPathUsr );
Alan Mishchenko committed
439 440
            }
            else {
Alan Mishchenko committed
441
                sPathAll = ABC_ALLOC( char, strlen(sPathLib)+strlen(sPathUsr)+5 );
Alan Mishchenko committed
442 443
                sprintf( sPathAll, "%s:%s",sPathUsr, sPathLib );
            }
Alan Mishchenko committed
444 445
            if ( sPathAll != NULL ) {
                sRealName = Extra_UtilFileSearch(sFileName, sPathAll, "r");
Alan Mishchenko committed
446
                ABC_FREE( sPathAll );
Alan Mishchenko committed
447 448
            }
        }
Alan Mishchenko committed
449 450
        if (sRealName == NULL) {
            sRealName = Extra_UtilTildeExpand(sFileName);
Alan Mishchenko committed
451
        }
452

Alan Mishchenko committed
453
        if ((pFile = fopen(sRealName, sMode)) == NULL) {
Alan Mishchenko committed
454
            if (! silent) {
455 456
//                perror(sRealName);
                Abc_Print( 1, "Cannot open file \"%s\".\n", sRealName );
Alan Mishchenko committed
457 458
            }
        }
459 460 461
        else
        {
            // print the path/name of the resource file 'abc.rc' that is being loaded
462
            if ( !silent && strlen(sRealName) >= 6 && strcmp( sRealName + strlen(sRealName) - 6, "abc.rc" ) == 0 )            
463
                Abc_Print( 1, "Loading resource file \"%s\".\n", sRealName );
464
        }
Alan Mishchenko committed
465 466 467 468
    }
    if ( pFileNameReal )
        *pFileNameReal = sRealName;
    else
Alan Mishchenko committed
469
        ABC_FREE(sRealName);
Alan Mishchenko committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    
    return pFile;
}

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

  Synopsis    [Frees the previously allocated argv array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void CmdFreeArgv( int argc, char **argv )
{
    int i;
    for ( i = 0; i < argc; i++ )
Alan Mishchenko committed
489 490
        ABC_FREE( argv[i] );
    ABC_FREE( argv );
Alan Mishchenko committed
491
}
492 493 494 495 496 497 498 499
char ** CmdDupArgv( int argc, char **argv )
{
    char ** argvNew = ABC_ALLOC( char *, argc );
    int i;
    for ( i = 0; i < argc; i++ )
        argvNew[i] = Abc_UtilStrsav( argv[i] );
    return argvNew;
}
Alan Mishchenko committed
500 501 502

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

Alan Mishchenko committed
503 504 505 506 507 508 509 510 511 512 513 514 515
  Synopsis    [Frees the previously allocated argv array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char ** CmdAddToArgv( int argc, char ** argv )
{
    char ** argv2;
    int i;
Alan Mishchenko committed
516
    argv2 = ABC_ALLOC( char *, argc + 1 ); 
Alan Mishchenko committed
517
    argv2[0] = Extra_UtilStrsav( "read" ); 
Alan Mishchenko committed
518
//    argv2[0] = Extra_UtilStrsav( "&r" ); 
Alan Mishchenko committed
519 520 521 522 523 524 525
    for ( i = 0; i < argc; i++ )
        argv2[i+1] = Extra_UtilStrsav( argv[i] ); 
    return argv2;
}

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

Alan Mishchenko committed
526 527 528 529 530 531 532 533 534 535 536
  Synopsis    [Frees the previously allocated command.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void CmdCommandFree( Abc_Command * pCommand )
{
Alan Mishchenko committed
537 538 539
    ABC_FREE( pCommand->sGroup );
    ABC_FREE( pCommand->sName );
    ABC_FREE( pCommand );
Alan Mishchenko committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553
}


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

  Synopsis    [Prints commands alphabetically by group.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
554
void CmdCommandPrint( Abc_Frame_t * pAbc, int fPrintAll, int fDetails )
Alan Mishchenko committed
555
{
556 557
    const char *key;
    char *value;
558
    st__generator * gen;
Alan Mishchenko committed
559 560
    Abc_Command ** ppCommands;
    Abc_Command * pCommands;
561
    int nCommands, iGroupStart, i, j;
Alan Mishchenko committed
562 563
    char * sGroupCur;
    int LenghtMax, nColumns, iCom = 0;
564
    FILE *backupErr = pAbc->Err;
Alan Mishchenko committed
565 566

    // put all commands into one array
567
    nCommands = st__count( pAbc->tCommands );
Alan Mishchenko committed
568
    ppCommands = ABC_ALLOC( Abc_Command *, nCommands );
Alan Mishchenko committed
569
    i = 0;
570
    st__foreach_item( pAbc->tCommands, gen, &key, &value )
Alan Mishchenko committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    {
        pCommands = (Abc_Command *)value;
        if ( fPrintAll || pCommands->sName[0] != '_' )
            ppCommands[i++] = pCommands;
    }
    nCommands = i;

    // sort command by group and then by name, alphabetically
    qsort( (void *)ppCommands, nCommands, sizeof(Abc_Command *), 
            (int (*)(const void *, const void *)) CmdCommandPrintCompare );
    assert( CmdCommandPrintCompare( ppCommands, ppCommands + nCommands - 1 ) <= 0 );

    // get the longest command name
    LenghtMax = 0;
    for ( i = 0; i < nCommands; i++ )
        if ( LenghtMax < (int)strlen(ppCommands[i]->sName) )
             LenghtMax = (int)strlen(ppCommands[i]->sName); 
    // get the number of columns
    nColumns = 79 / (LenghtMax + 2);

    // print the starting message 
592
    fprintf( pAbc->Out, "      Welcome to ABC compiled on %s %s!", __DATE__, __TIME__ );
Alan Mishchenko committed
593 594 595

    // print the command by group
    sGroupCur = NULL;
596 597
    iGroupStart = 0;
    pAbc->Err = pAbc->Out;
Alan Mishchenko committed
598 599 600 601 602 603 604 605 606 607 608
    for ( i = 0; i < nCommands; i++ )
        if ( sGroupCur && strcmp( sGroupCur, ppCommands[i]->sGroup ) == 0 )
        { // this command belongs to the same group as the previous one
            if ( iCom++ % nColumns == 0 )
                fprintf( pAbc->Out, "\n" ); 
            // print this command
            fprintf( pAbc->Out, " %-*s", LenghtMax, ppCommands[i]->sName );
        }
        else
        { // this command starts the new group of commands
            // start the new group
609 610 611 612 613
            if ( fDetails && i != iGroupStart )
            { // print help messages for all commands in the previous groups
                fprintf( pAbc->Out, "\n" );
                for ( j = iGroupStart; j < i; j++ )
                {
614
                    char *tmp_cmd;
615 616
                    fprintf( pAbc->Out, "\n" );
                    // fprintf( pAbc->Out, "--- %s ---\n", ppCommands[j]->sName );
617
                    tmp_cmd = ABC_ALLOC(char, strlen(ppCommands[j]->sName)+4);
618 619 620 621 622 623 624 625
                    (void) sprintf(tmp_cmd, "%s -h", ppCommands[j]->sName);
                    (void) Cmd_CommandExecute( pAbc, tmp_cmd );
                    ABC_FREE(tmp_cmd);
                }
                fprintf( pAbc->Out, "\n" );
                fprintf( pAbc->Out, "   ----------------------------------------------------------------------" );
                iGroupStart = i;
            }
Alan Mishchenko committed
626 627 628 629 630 631 632 633 634 635
            fprintf( pAbc->Out, "\n" );
            fprintf( pAbc->Out, "\n" );
            fprintf( pAbc->Out, "%s commands:\n", ppCommands[i]->sGroup );
            // print this command
            fprintf( pAbc->Out, " %-*s", LenghtMax, ppCommands[i]->sName );
            // remember current command group
            sGroupCur = ppCommands[i]->sGroup;
            // reset the command counter
            iCom = 1;
        }
636 637 638 639 640
    if ( fDetails && i != iGroupStart )
    { // print help messages for all commands in the previous groups
        fprintf( pAbc->Out, "\n" );
        for ( j = iGroupStart; j < i; j++ )
        {
641
            char *tmp_cmd;
642 643
            fprintf( pAbc->Out, "\n" );
            // fprintf( pAbc->Out, "--- %s ---\n", ppCommands[j]->sName );
644
            tmp_cmd = ABC_ALLOC(char, strlen(ppCommands[j]->sName)+4);
645 646 647 648 649 650
            (void) sprintf(tmp_cmd, "%s -h", ppCommands[j]->sName);
            (void) Cmd_CommandExecute( pAbc, tmp_cmd );
            ABC_FREE(tmp_cmd);
        }
    }
    pAbc->Err = backupErr;
Alan Mishchenko committed
651
    fprintf( pAbc->Out, "\n" );
Alan Mishchenko committed
652
    ABC_FREE( ppCommands );
Alan Mishchenko committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
}
 
/**Function*************************************************************

  Synopsis    [Comparision function used for sorting commands.]

  Description []
                
  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdCommandPrintCompare( Abc_Command ** ppC1, Abc_Command ** ppC2 )
{
    Abc_Command * pC1 = *ppC1;
    Abc_Command * pC2 = *ppC2;
    int RetValue;

    RetValue = strcmp( pC1->sGroup, pC2->sGroup );
    if ( RetValue < 0 )
        return -1;
    if ( RetValue > 0 )
        return 1;
    // the command belong to the same group

    // put commands with "_" at the end of the list
    if ( pC1->sName[0] != '_' && pC2->sName[0] == '_' )
        return -1;
    if ( pC1->sName[0] == '_' && pC2->sName[0] != '_' )
        return 1;

    RetValue = strcmp( pC1->sName, pC2->sName );
    if ( RetValue < 0 )
        return -1;
    if ( RetValue > 0 )
        return 1;
     // should not be two indentical commands
    assert( 0 );
    return 0;
}
Alan Mishchenko committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
 
/**Function*************************************************************

  Synopsis    [Comparision function used for sorting commands.]

  Description []
                
  SideEffects []

  SeeAlso     []

***********************************************************************/
int CmdNamePrintCompare( char ** ppC1, char ** ppC2 )
{
    return strcmp( *ppC1, *ppC2 );
}

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

  Synopsis    [Comparision function used for sorting commands.]

  Description []
                
  SideEffects []

  SeeAlso     []

***********************************************************************/
722
void CmdPrintTable( st__table * tTable, int fAliases )
Alan Mishchenko committed
723
{
724
    st__generator * gen;
725 726 727
    const char ** ppNames;
    const char * key;
    char* value;
Alan Mishchenko committed
728 729 730
    int nNames, i;

    // collect keys in the array
731
    ppNames = ABC_ALLOC( const char *, st__count(tTable) );
Alan Mishchenko committed
732
    nNames = 0;
733
    st__foreach_item( tTable, gen, &key, &value )
Alan Mishchenko committed
734 735 736 737 738 739 740 741 742
        ppNames[nNames++] = key;

    // sort array by name
    qsort( (void *)ppNames, nNames, sizeof(char *), 
        (int (*)(const void *, const void *))CmdNamePrintCompare );

    // print in this order
    for ( i = 0; i < nNames; i++ )
    {
743
        st__lookup( tTable, ppNames[i], &value );
Alan Mishchenko committed
744 745 746 747 748
        if ( fAliases )
            CmdCommandAliasPrint( Abc_FrameGetGlobalFrame(), (Abc_Alias *)value );
        else
            fprintf( stdout, "%-15s %-15s\n", ppNames[i], value );
    }
Alan Mishchenko committed
749
    ABC_FREE( ppNames );
Alan Mishchenko committed
750
}
Alan Mishchenko committed
751 752 753 754

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////
755 756
ABC_NAMESPACE_IMPL_END