parse.c

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

C
1,171
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Command line parsing for CL clone tool.
*
****************************************************************************/


#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "cl.h"
#include "cmdline.h"
#include "cmdscan.h"
#include "context.h"
#include "error.h"
#include "file.h"
#include "macro.h"
#include "memory.h"
#include "message.h"
#include "parse.h"
#include "system.h"


#pragma disable_message (202);

/*
 * Initialize the OPT_STORAGE structure.
 */
void InitParse( OPT_STORAGE *cmdOpts )
/************************************/
{
    OPT_INIT( cmdOpts );
}


/*
 * Destroy the OPT_STORAGE structure.
 */
void FiniParse( OPT_STORAGE *cmdOpts )
/************************************/
{
    OPT_FINI( cmdOpts );
}


/*
 * Gripe about a command line error.
 */
static void cmd_line_error( void )
/********************************/
{
    char *              str;

    GoToMarkContext();
    str = CmdScanString();
    Warning( "Ignoring invalid option '%s'", str );
}


/*
 * Parse the command string contained in the current context.
 */
void CmdStringParse( OPT_STORAGE *cmdOpts, int *itemsParsed )
/***********************************************************/
{
    int                 ch;
    char *              filename;

    for( ;; ) {
        /*** Find the start of the next item ***/
        CmdScanWhitespace();
        ch = GetCharContext();
        if( ch == '\0' )  break;
        MarkPosContext();               /* mark start of switch */

        /*** Handle switches, command files, and input files ***/
        if( ch == '-'  ||  ch == '/' ) {        /* switch */
            if( OPT_PROCESS( cmdOpts ) != 0 ) {
                cmd_line_error();
            }
        } else if( ch == '@' ) {                /* command file */
            filename = CmdScanFileNameWithoutQuotes();
            PushContext();
            if( OpenFileContext( filename ) ) {
                FatalError( "Cannot open '%s'.", filename );
            }
            FreeMem( filename );
            CmdStringParse( cmdOpts, itemsParsed );
            PopContext();
        } else {                                /* input file */
            UngetCharContext();
            filename = CmdScanFileName();
            AddFile( TYPE_DEFAULT_FILE, filename );
            FreeMem( filename );
        }
        (*itemsParsed)++;
    }
    CloseContext();
}


/*
 * Parse the /D option.
 */
static int parse_D( OPT_STRING **p )
/**********************************/
{
    char *              str;
    char *              eq;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/D requires an argument" );
        return( 0 );
    }
    for( ;; ) {                 /* convert all '#' chars to '=' chars */
        eq = strchr( str, '#' );
        if( eq == NULL )  break;
        *eq = '=';
    }
    if( DefineMacro( str ) ) {
        return( 1 );
    } else {
        Warning( "Ignoring invalid macro definition '%s'", str );
        return( 0 );
    }
}


/*
 * Add another string to an OPT_STRING.
 */
static void add_string( OPT_STRING **p, char *str )
/*************************************************/
{
    OPT_STRING *        buf;
    OPT_STRING *        curElem;

    /*** Make a new list item ***/
    buf = AllocMem( sizeof(OPT_STRING) + strlen(str) );
    strcpy( buf->data, str );
    buf->next = NULL;

    /*** Put it at the end of the list ***/
    if( *p == NULL ) {
        *p = buf;
    } else {
        curElem = *p;
        while( curElem->next != NULL )  curElem = curElem->next;
        curElem->next = buf;
    }
}


/*
 * Parse the /F option.
 */
static int parse_F( OPT_STRING **p )
/**********************************/
{
    char *              str;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/F requires an argument" );
        return( 0 );
    }
    add_string( p, str );
    return( 1 );
}


/*
 * Parse the /FI option.
 */
static int parse_FI( OPT_STRING **p )
/***********************************/
{
    char *              str;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/FI requires an argument" );
        return( 0 );
    }
    add_string( p, str );
    return( 1 );
}


/*
 * Destroy an OPT_STRING.
 */
static void OPT_CLEAN_STRING( OPT_STRING **p )
/********************************************/
{
    OPT_STRING *        s;

    while( *p != NULL ) {
        s = *p;
        *p = s->next;
        FreeMem( s );
    }
}


/*
 * Parse the /Fm option.
 */
static int parse_Fm( OPT_STRING **p )
/***********************************/
{
    char *              str;

    str = CmdScanString();
    if( str == NULL ) {
        OPT_CLEAN_STRING( p );
    } else {
        if( *p != NULL ) {
            Warning( "Overriding /Fm%s with /Fm%s", (*p)->data, str );
        }
        add_string( p, str );
    }
    return( 1 );
}


/*
 * Parse the /Gs option.
 */
static int parse_Gs( OPT_STRING **p )
/***********************************/
{
    char *              str;

    str = CmdScanString();
    if( str != NULL ) {
        add_string( p, str );
    }
    return( 1 );
}


/*
 * Parse the /I option.
 */
static int parse_I( OPT_STRING **p )
/**********************************/
{
    char *              str;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/I requires an argument" );
        return( 0 );
    }
    add_string( p, str );
    return( 1 );
}


/*
 * Parse the /link option.
 */
static int parse_link( OPT_STRING **p )
/*************************************/
{
    char *              str;
    int                 gotOne = 0;

    if( !CmdScanRecogChar( ' ' )  &&  !CmdScanRecogChar( '\t' ) ) {
        FatalError( "Whitespace required after /link" );
        return( 0 );
    }
    for( ;; ) {
        CmdScanWhitespace();
        str = CmdScanString();
        if( str == NULL ) {
            if( !gotOne ) {
                FatalError( "/link requires at least one argument" );
                return( 0 );
            } else {
                break;
            }
        }
        add_string( p, str );
        gotOne = 1;
    }
    return( 1 );
}


/*
 * Parse the /passwopts option.
 */
static int parse_passwopts( OPT_STRING **p )
{
    char *str;
    char *src;
    char *dst;

    if (!CmdScanRecogChar(':'))
    {
        FatalError("/passwopts:{argument} requires an argument");
        return 0;
    }

    str = CmdScanString();
    if (str == NULL)
    {
        FatalError("/passwopts requires an argument");
        return 0;
    }

    /*
     * If quoted, stip out the quote characters.
     */
    if (*str == '\"')
    {
        for (dst = str, src = str + 1; *src && (*src != '\"'); )
        {
            *dst++ = *src++;
        }

        if (*src != '\"')
        {
            FatalError("/passwopts argument is missing closing quote");
            return 0;
        }

        *dst = 0x00;
    }

    add_string( p, str );
    return( 1 );
} /* parse_passwopts() */


/*
 * Scan a filename.  No leading whitespace is permitted.
 */
static int OPT_GET_FILE( OPT_STRING **p )
/***************************************/
{
    char *              filename;

    filename = CmdScanFileName();
    if( filename != NULL ) {
        add_string( p, filename );
        return( 1 );
    } else {
        OPT_CLEAN_STRING( p );
        return( 0 );
    }
}


/*
 * Parse the /Tc option.
 */
static int parse_Tc( OPT_STRING **p )
/***********************************/
{
    CmdScanWhitespace();
    if( OPT_GET_FILE( p ) ) {
        AddFile( TYPE_C_FILE, (*p)->data );
        return( 1 );
    } else {
        FatalError( "/Tc requires an argument" );
        return( 0 );
    }
}


/*
 * Parse the /Tp option.
 */
static int parse_Tp( OPT_STRING **p )
/***********************************/
{
    CmdScanWhitespace();
    if( OPT_GET_FILE( p ) ) {
        AddFile( TYPE_CPP_FILE, (*p)->data );
        return( 1 );
    } else {
        FatalError( "/Tp requires an argument" );
        return( 0 );
    }
}


/*
 * Parse the /U option.
 */
static int parse_U( OPT_STRING **p )
/**********************************/
{
    char *              str;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/U requires an argument" );
        return( 0 );
    }
    UndefineMacro( str );
    return( 1 );
}


/*
 * Parse the /V option.
 */
static int parse_V( OPT_STRING **p )
/**********************************/
{
    char *              str;

    CmdScanWhitespace();
    str = CmdScanString();
    if( str == NULL ) {
        FatalError( "/V requires an argument" );
        return( 0 );
    }
    /* it's unsupported, so just skip over it; error msg will come later */
    return( 1 );
}


/*
 * Ensure the parameter to /Ob is valid.
 */
static void check_inlining_level( unsigned *p )
/*********************************************/
{
    if( *p != 0  &&  *p != 1  &&  *p != 2 ) {
        Warning( "Invalid value '%d' for /Ob -- assuming '0'", *p );
        *p = 0;
    }
}


/*
 * Ensure the parameter to /Zp is valid.
 */
static void check_packing( unsigned *p )
/**************************************/
{
    if( *p != 1  &&  *p != 2  &&  *p != 4  &&  *p != 8  &&  *p != 16 ) {
        Warning( "Invalid value '%d' for /Zp -- assuming '8'", *p );
        *p = 8;
    }
}


/*
 * Ensure the parameter to /W is valid.
 */
static void check_warn_level( unsigned *p )
/*****************************************/
{
    if( *p != 0  &&  *p != 1  &&  *p != 2  &&  *p != 3  &&  *p != 4 ) {
        Warning( "Invalid value '%d' for /W -- assuming '1'", *p );
        *p = 1;
    }
}


#ifdef __TARGET_386__
/*
 * Warn when one of /G3, /G4, /G5, and /GB overrides another.
 */
static void handle_arch_i86( OPT_STORAGE *cmdOpts, int x )
/********************************************************/
{
    static int          hasBeenCalled;
    static int          prevValue;
    char                oldCpu, newCpu;

    x = x;
    if( hasBeenCalled ) {
        if( prevValue != cmdOpts->arch_i86 ) {
            switch( prevValue ) {           /* what is the old CPU? */
              case OPT_arch_i86_G3:
                oldCpu = '3';
                break;
              case OPT_arch_i86_G4:
                oldCpu = '4';
                break;
              case OPT_arch_i86_G5:
                oldCpu = '5';
                break;
              case OPT_arch_i86_GB:
                oldCpu = 'B';
                break;
              default:
                Zoinks();
            }
            switch( cmdOpts->arch_i86 ) {   /* what is the new CPU? */
              case OPT_arch_i86_G3:
                newCpu = '3';
                break;
              case OPT_arch_i86_G4:
                newCpu = '4';
                break;
              case OPT_arch_i86_G5:
                newCpu = '5';
                break;
              case OPT_arch_i86_GB:
                newCpu = 'B';
                break;
              default:
                Zoinks();
            }
            Warning( "Overriding /G%c with /G%c", oldCpu, newCpu );
        }
    } else {
        hasBeenCalled = 1;
    }
    prevValue = cmdOpts->arch_i86;
}
#endif


/*
 * Warn when one of /Z7, /Zd, and /Zi overrides another.
 */
static void handle_debug_info( OPT_STORAGE *cmdOpts, int x )
/**********************************************************/
{
    static int          hasBeenCalled;
    static int          prevValue;

    x = x;
    if( cmdOpts->debug_info == OPT_debug_info_Zi ) {
        Warning( "Replacing unsupported /Zi with /Z7" );
        cmdOpts->debug_info = OPT_debug_info_Z7;
    }
    if( hasBeenCalled ) {
        if( prevValue == OPT_debug_info_Z7 ) {
            if( cmdOpts->debug_info == OPT_debug_info_Zd ) {
                Warning( "Overriding /Z7 with /Zd" );
            }
        } else if( prevValue == OPT_debug_info_Zd ) {
            if( cmdOpts->debug_info == OPT_debug_info_Z7 ) {
                Warning( "Overriding /Zd with /Z7" );
            }
        }
    } else {
        hasBeenCalled = 1;
    }
    Warning( "Using Dwarf debugging information" );
    prevValue = cmdOpts->debug_info;
}


/*
 * Warn when the previous /F value is overridden.

⌨️ 快捷键说明

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