cmdline.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 709 行 · 第 1/2 页

C
709
字号

    for( cmd = CmdList; cmd != NULL; cmd = next ) {
        next = cmd->next;
        MemFreeGlobal( cmd );
    }
}

static void ParseCommand( char **pc )
{
    int          doquotes = TRUE;
    int          ignoreSpacesInQuotes = FALSE;
    char        *c = *pc;
    char        *start;
    operation   ops = 0;
    //char        buff[_MAX_PATH];
    char        buff[MAX_IMPORT_STRING];

    start = c;
    eatwhite( c );
    switch( *c++ ) {
    case '-':
        ops = OP_DELETE;
        switch( *c ) {
        case '+':
            ops |= OP_ADD;
            doquotes = FALSE;
            ++c;
            break;
#if defined(__UNIX__)
        case ':':
#else
        case '*':
#endif
            ops |= OP_EXTRACT;
            doquotes = FALSE;
            ++c;
            break;
        }
        break;
    case '+':
        ops = OP_ADD;
        switch( *c ) {
        case '-':
            ops |= OP_DELETE;
            doquotes = FALSE;
            ++c;
            break;
        case '+':
            ops |= OP_IMPORT;
            doquotes = FALSE;
            ignoreSpacesInQuotes = TRUE;
            ++c;
            break;
        }
        break;
#if defined(__UNIX__)
    case ':':
#else
    case '*':
#endif
        ops |= OP_EXTRACT;
        if( *c == '-' ) {
            ops |= OP_DELETE;
            doquotes = FALSE;
            ++c;
        }
        break;
    default:
        FatalError( ERR_BAD_CMDLINE, start );
    }
    eatwhite( c );
    GetString( &c, buff, doquotes, ignoreSpacesInQuotes );
    AddCommand( ops, buff );
    *pc = c;
}

#define MAX_CMDLINE     (10*1024)

static void ParseOneLine( char *c )
{
    char        *buff;
    char        *start;

    buff = MemAlloc( MAX_CMDLINE );
    for( ;; ) {
        eatwhite( c );
        start = c;
        switch( *c ) {
#if !defined(__UNIX__)
        case '/':
            if( !ParseOption( &c, buff ) ) {
                FatalError( ERR_BAD_OPTION, c[1] );
            }
            break;
#endif

#if !defined(__UNIX__)
        case '*':
#else
        case ':':
#endif
        case '+':
        case '-':
            if( CmdList != NULL ||
                Options.input_name != NULL ||
                !ParseOption( &c, buff ) ) {
                ParseCommand( &c );
            }
            break;
        case '@':
            ++c;
            GetString( &c, buff, TRUE, FALSE );
            {
                char *env = getenv(buff);
                if (env) {
                    ParseOneLine(env);
                } else {
                    FILE    *io;
                    DefaultExtension( buff, EXT_CMD );
                    io = fopen( buff, "r" );
                    if( io == NULL ) {
                        FatalError( ERR_CANT_OPEN, buff, strerror( errno ) );
                    }
                    while( fgets( buff, MAX_CMDLINE, io ) != NULL ) {
                        ParseOneLine( buff );
                    }
                    fclose( io );
                }
            }
            break;
        case '\0':
        case '#':
            // comment - blow away line
            MemFree( buff );
            return;
        default:
            GetString( &c, buff, TRUE, FALSE );
            if( strcmp( buff, "?" ) == 0 ) {
                Banner();
                Usage();
            }
            if( Options.input_name ) {
                AddCommand( OP_ADD|OP_DELETE, buff );
            } else {
                DefaultExtension( buff, EXT_LIB );
                Options.input_name = DupStr( buff );
            }
            break;
        }
    }
}

static void ParseArOption( char **init_c, operation *mode )
{
    char        *c;

    c = *init_c;
    while( *c != '\0' && !isspace( *c ) ) {
        switch( tolower( *c ) ) {
        case '?':
            Banner();
            Usage();
            break;
        case 'c':
            if( Options.no_c_warn ) {
                DuplicateOption( *init_c );
            }
            Options.no_c_warn = TRUE;
            break;
        case 'd':
            if( *mode != OP_NONE ) {
                FatalError( ERR_BAD_OPTION, c[0] );
            }
            *mode = OP_DELETE;
            break;
        case 'p':
            //ignore not implemented
            break;
        case 'r':
            if( *mode != OP_NONE ) {
                FatalError( ERR_BAD_OPTION, c[0] );
            }
            *mode = OP_ADD | OP_DELETE;
            break;
        case 't':
            if( *mode != OP_NONE ) {
                FatalError( ERR_BAD_OPTION, c[0] );
            }
            *mode = OP_TABLE;
            Options.list_contents = 1;
            break;
        case 'u':
            if( Options.update ) {
                DuplicateOption( *init_c );
            }
            Options.update = TRUE;
            break;
        case 'v':
            if( Options.verbose ) {
                DuplicateOption( *init_c );
            }
            Options.verbose = TRUE;
            break;
        case 'x':
            if( *mode != OP_NONE ) {
                FatalError( ERR_BAD_OPTION, c[0] );
            }
            *mode = OP_EXTRACT;
            break;
        case '-':
            break;
        default:
            FatalError( ERR_BAD_OPTION, c[0] );
        }
        c++;
    }
    *init_c = c;
}

static void ParseOneArLine( char *c )
{
    char        *buff;
    operation   mode;
    bool        done_options;

    mode = OP_NONE;
    done_options = FALSE;
    buff = MemAlloc( MAX_CMDLINE );
    for( ;; ) {
        eatwhite( c );
        switch( *c ) {
        case '\0':
            if( mode == OP_EXTRACT ) {
                Options.explode = TRUE;
            }
            MemFree( buff );
            return;
        case '-':
            if( !done_options ) {
                if( *(c + 1) == '-' ) {
                    c+=2;
                    done_options = TRUE;
                } else {
                    ParseArOption( &c, &mode );
                }
            break;
            }
            //fall to default
        default:
            if( mode == OP_NONE ) {
                ParseArOption( &c, &mode );
                break;
            }
            GetString( &c, buff, TRUE, FALSE );
            if( Options.input_name ) {
                AddCommand( mode, buff );
                break;
            } else {
                DefaultExtension( buff, EXT_LIB );
                Options.input_name = DupStr( buff );
            }
            break;
        }
    }
}

void ProcessCmdLine( char *argv[] )
{
    char        *parse;
    char        *env;
    lib_cmd     *cmd;

    if( FNCMP( MakeFName( ImageName ), "ar" ) == 0 || WlibGetEnv( AR_MODE_ENV ) != NULL ) {
        Options.ar = TRUE;
    }
    if( Options.ar ) {
        env = WlibGetEnv( "AR" );
    } else {
        env = WlibGetEnv( "WLIB" );
    }
    if( env == NULL && argv[1] == NULL || argv[1][0] == '\0' ) {
        Banner();
        Usage();
    }
    if( env != NULL ) {
        parse = DupStr( env );
        if( Options.ar ) {
            ParseOneLine( parse );
        } else {
            ParseOneArLine( parse );
        }
        MemFree( parse );
    }
    argv++;
    while( *argv != NULL ) {
        parse = DupStr( *argv );
        if( Options.ar ) {
            ParseOneArLine( parse );
        } else {
            ParseOneLine( parse );
        }
        MemFree( parse );
        argv++;
    }
    if( Options.ar && CmdList != NULL && Options.explode ) {
        Options.explode = 0;
    } else if( CmdList == NULL && !(Options.list_contents) && !(Options.explode)
            && !(Options.new_library) ) {
        /* Default action: List the input lib */
        if( Options.output_name == NULL ) {
            Options.list_contents = 1;
            Options.list_file = DupStr("");
        } else { /* Or copy it to the output lib */
            Options.modified = TRUE;
        }
    }

    if( !Options.ar ) {
        Banner();
    }
    if( Options.input_name == NULL ) {
        FatalError( ERR_NO_LIBNAME );
    }
    if( access( Options.input_name, F_OK ) != 0 && !Options.new_library ) {
        if( !Options.no_c_warn ) {
            Warning( ERR_CREATING_LIBRARY, Options.input_name );
        }
        Options.new_library = 1;
    }
    if( Options.new_library ) {
        for( cmd = CmdList; cmd != NULL; cmd = cmd->next ) {
            if( cmd->ops & OP_EXTRACT ) {
                FatalError( ERR_DELETE_AND_CREATE );
            }
        }
    }
}

void InitCmdLine( void )
{
    CmdList = NULL;
    memset( &Options, 0, sizeof( Options ) );
}

void ResetCmdLine( void )
{
    MemFree( Options.output_directory );
    MemFree( Options.list_file );
    MemFree( Options.output_name );
    MemFree( Options.input_name );
    FreeCommands();
    InitCmdLine();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?