⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccmain.c

📁 Open Watcom 的 C 编译器源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
}

FNAMEPTR AddFlist( char const *filename )
{
    FNAMEPTR    flist;
    FNAMEPTR    *lnk;
    int         index;

    index = 0;
    lnk = &FNames;
    while( (flist = *lnk) != NULL ) {
        if( strcmp( filename, flist->name ) == 0 ) break;
        lnk = &flist->next;
        index++;
    }
    if( flist == NULL ) {
        flist = (FNAMEPTR)CMemAlloc( strlen( filename )
                            + sizeof( struct fname_list ) );
        flist->next = NULL;
        flist->index = index;
        flist->rwflag = TRUE;
        flist->once   = FALSE;
        flist->fullpath = NULL;
        strcpy( flist->name, filename );
        *lnk = flist;
        flist->mtime = _getFilenameTimeStamp( filename );
    }
    return( flist );
}

FNAMEPTR FileIndexToFName( unsigned file_index )
{
    FNAMEPTR    flist;

    for( flist = FNames; flist; flist = flist->next ) {
        if( flist->index == file_index )break;
    }
    return( flist );
}

char *FNameFullPath( FNAMEPTR flist )
{
    char   fullbuff[2*PATH_MAX];
    char *fullpath;


    if( flist->fullpath == NULL ) {
        fullpath = SrcFullPath( fullbuff, flist->name, sizeof( fullbuff ) );
        if( fullpath != NULL ) {
            fullpath = CStrSave( fullpath );
            flist->fullpath = fullpath;
        } else {
            fullpath = flist->name;
        }
    } else {
        fullpath = flist->fullpath;
    }
    return( fullpath );
}

char *FileIndexToCorrectName( unsigned file_index )
{
    FNAMEPTR flist;
    char *name;

    flist = FileIndexToFName( file_index );
    if( CompFlags.ef_switch_used ){
        name = FNameFullPath( flist );
    }else{
        name = flist->name;
    }
    return ( name );
}

static bool IsFNameOnce( char const *filename )
{
    bool ret;
    FNAMEPTR    flist;

    ret = FALSE;
    flist = FindFlist( filename );
    if( flist != NULL ) {
        ret = flist->once;
    }
    return( ret );
}

void FreeFNames( void )
{
    FNAMEPTR    flist;

    while( (flist = FNames) ) {
        FNames = flist->next;
        if( flist->fullpath != NULL ) {
            CMemFree( flist->fullpath );
        }
        CMemFree( flist );
    }
}

static void AddIncFileList( char *filename )
{
    INCFILE     *ifile;
    INCFILE     *ifilep;
    int         len;

    len = strlen( filename );
    ifile = (INCFILE *)CMemAlloc( sizeof(INCFILE) + len );
    ifile->len = len;
    strcpy( ifile->filename, filename );
    ifile->nextfile = NULL;
    if( IncFileList == NULL ) {
        IncFileList = ifile;
    } else {
        ifilep = IncFileList;
        while( ifilep->nextfile != NULL )  ifilep = ifilep->nextfile;
        ifilep->nextfile = ifile;
    }
}

RDIRPTR AddRDir( char *path )
{
    RDIRPTR   dirlist;
    RDIRPTR  *lnk;

    lnk = &RDirNames;
    while( (dirlist = *lnk) != NULL ) {
        if( stricmp( path, dirlist->name ) == 0 ) break;
        lnk = &dirlist->next;
    }
    if( dirlist == NULL ) {
        dirlist = (RDIRPTR)CMemAlloc( strlen( path )
                            + sizeof( struct rdir_list ) );
        dirlist->next = NULL;
        strcpy( dirlist->name, path );
        *lnk = dirlist;
    }
    return( dirlist );
}

void FreeRDir( void )
{
    RDIRPTR    dirlist;

    while( (dirlist = RDirNames) ) {
        RDirNames = dirlist->next;
        CMemFree( dirlist );
    }
}

static char *IncPathElement(     // GET ONE PATH ELEMENT FROM INCLUDE LIST
    const char *path,           // - include list
    char *prefix )              // - buffer to store element
{
    unsigned    length;

    length = 0;
    for( ; ; ) {
        if( *path == '\0' ) break;
        if( *path == INCLUDE_SEP || *path == ';' ) {
            ++path;
            if( length != 0 ) {
                break;
            }
        } else {
            ++length;
            *prefix++ = *path++;
        }
    }
    if( ( length > 1 ) && IS_PATH_SEP( *(prefix-1) ) ) --prefix;
    *prefix = '\0';
    return( (char *)path );
}

void SrcFileReadOnlyDir( char const *dir )
{ // add dir to ro set
    char *full;                 // - full path
    auto char path[_MAX_PATH];  // - used to extract directory
    auto char buff[_MAX_PATH];  // - expanded path for directory

    while( *dir != '\0') {
        dir =  IncPathElement( dir, path );
        full = SrcFullPath( buff, path, sizeof( buff ) );
        AddRDir( full );
    }
}

bool SrcFileInRDir( FNAMEPTR flist ) {
    RDIRPTR   dirlist;
    bool read_only;             // - TRUE ==> file is in read-only directory
    char *fullpath;             // - full path

    read_only = FALSE;
    fullpath  = FNameFullPath( flist );
    dirlist = RDirNames;
    while( dirlist != NULL ) {
        if( strnicmp(dirlist->name , fullpath, strlen( dirlist->name ) )== 0 ) {
            read_only = TRUE;
            break;
        }
        dirlist = dirlist->next;
    }
    return( read_only );
}

void SrcFileReadOnlyFile( char const *file )
{
    FNAMEPTR    flist;

    if( file == NULL  ) {
        flist = SrcFile->src_flist;
    } else {
        flist= FindFlist( file );
    }
    if( flist  != NULL ) {
        flist->rwflag = FALSE;
    }
}

int FListSrcQue( void )
{
    FNAMEPTR    flist;
    char       *fullpath;
    int         count;
    int         fno;
    // this is all very kludged in
    count = 0;
    flist = FNames;
    while( flist != NULL ) {
        fullpath = FNameFullPath( flist );
        fno = DBSrcFile( fullpath );
        flist = flist->next;
    }
    return( count );
}

static int FCB_Alloc( FILE *fp, char *filename )
{
    int         i;
    FCB         *srcfcb;
    char        *src_buffer;
    FNAMEPTR    flist;

   --IncFileDepth;
    srcfcb = (FCB *) CMemAlloc( sizeof( FCB ) );
    i = PRODUCTION_BUFFER_SIZE;
    src_buffer = FEmalloc( i + 3 );
    if( srcfcb ) {
        srcfcb->src_buf = src_buffer;
        srcfcb->src_ptr = src_buffer;
        src_buffer[0] = '\0';
        if( SrcFile != NULL ) {         // if already have a file open
            SrcFile->src_ptr = ScanCharPtr;     // - save current scan pointer
        }
        ScanCharPtr = src_buffer;               // set scan ptr for new file
        flist = AddFlist( filename );
        srcfcb->src_line = 1;
        SrcFileLineNum = 1;
        srcfcb->src_name = flist->name;
        srcfcb->src_fno  = flist->index;
        TokenFno = flist->index;
        srcfcb->src_flist= flist;
        ErrFName = flist->name;
        srcfcb->src_fp   = fp;
        srcfcb->prev_file = SrcFile;
        srcfcb->src_cnt = 0;
        srcfcb->prev_currchar = CurrChar;
        srcfcb->src_bufsize = i;
        srcfcb->peeking = 0;
        #if _CPU == 370
            srcfcb->colum = 0;     /* init colum, trunc info */
            srcfcb->trunc = 0;
            srcfcb->prevcount = 0;
        #endif
        if( SrcFile != NULL ) {                 /* 28-jan-94 */
            if( SrcFile == MainSrcFile ) {
                // remember name of included file
                AddIncFileList( filename );
            }
        }
        srcfcb->rseekpos = 0;
        SrcFile = srcfcb;
        CurrChar = '\n';    /* set next character to newline */
        if( CompFlags.cpp_output ) {            /* 10-aug-91 */
            if( CppFile == NULL )  OpenCppFile();
            EmitPoundLine( 1, filename, 1 );
            CppFirstChar = 1;
        }
        return( 1 );
    }
    return( 0 );
}

void SetSrcFNameOnce( void )
{
    SrcFile->src_flist->once = TRUE;
}

static void ParseInit( void )
{
    ScanInit();
    CTypeInit();
    MacroInit();
    SymInit();
    SpcSymInit();
    StringInit();
    InitDataQuads();
    ExprInit();
    StmtInit();
    SegInit();                                  /* 02-feb-92 */
}


local void Parse( void )
{
    EmitInit();
    // The first token in a file should be #include if a user wants to
    // use pre-compiled headers. The following call to NextToken() to
    // get the very first token of the file will load the pre-compiled
    // header if the user requested such and it is a #include directive.
    CompFlags.ok_to_use_precompiled_hdr = 1;    /* 27-jun-94 */
    if( ForceInclude ) {                        /* 17-feb-95 */
        if( PCH_FileName != NULL ) {
            CompFlags.use_precompiled_header = 1;
        }
        // we want to keep in the pre-compiled header
        // any macros that are defined in forced include file
        InitialMacroFlag = 0;                   /* 02-jun-95 */
        OpenSrcFile( ForceInclude, 0 );
        CompFlags.use_precompiled_header = 0;
        CompFlags.ok_to_use_precompiled_hdr = 0;
    }
    NextToken();
    // If we didn't get a #include with the above call to NextToken()
    // it's too late to use pre-compiled header now.
    CompFlags.ok_to_use_precompiled_hdr = 0;    /* 27-jun-94 */
    ParsePgm();
    if( DefFile != NULL ) {
        fclose( DefFile );
        DefFile = NULL;
    }
    ChkCallParms();
    EndBlock();     /* end of block 0 */
    MacroFini();
    if( ! CompFlags.quiet_mode ) PrintStats();
    if( CompFlags.warnings_cause_bad_exit )  ErrCount += WngCount;
}

static void CPP_Parse( void )
{
    if( ForceInclude ) {
        PrtChar( '\n' );
        OpenSrcFile( ForceInclude, 0 );
    }
    PrintWhiteSpace = TRUE;
    for(;;) {
        GetNextToken();
        if( CurToken == T_EOF ) break;
        PrtToken();
    }
    MacroFini();
}


void EmitPoundLine( unsigned line_num, char *filename, int newline )
{
    if( CompFlags.cpp_line_wanted ) {
        if( CppPrinting() ) {
            CppPrtf( "#line %u \"", line_num );    /* 04-apr-91 */
            while( *filename ) {
                #if 0                                   /* 15-may-94 */
                    if( *filename == '\\' ) {
                        PrtChar( '\\' );
                    }
                #endif
                PrtChar( *filename );
                ++filename;
            }
            PrtChar( '\"' );
            if( newline )  PrtChar( '\n' );
        }
    }
}

void EmitLine( unsigned line_num, char *filename )
{
    EmitPoundLine( line_num, filename, 0 );
}

int CppPrinting()
{
    if( NestLevel != SkipLevel ) return( 0 );   /* 01-dec-89 */
    return( 1 );
}


void PrtToken(void)
{
    if( ! CppPrinting() ) return;
    switch( CurToken ) {
    case T_BAD_CHAR:                    /* 12-apr-89 */
    case T_BAD_TOKEN:                   /* 12-apr-89 */
    case T_ID:
    case T_CONSTANT:
        CppPrtf( "%s", Buffer );
        break;
    case T_STRING:
        if( CompFlags.wide_char_string ) {              /* 18-feb-90 */
            PrtChar( 'L' );
        }
        CppPrtf( "\"%s\"", Buffer );
        break;
    case T_EOF:
    case T_NULL:
        break;
    case T_WHITE_SPACE:
        if( PrintWhiteSpace ) {
            CppPrtf( "%s", Tokens[ CurToken ] );
        } else {
            PrintWhiteSpace = TRUE; //Toggle
        }
        break;
    default:
        CppPrtf( "%s", Tokens[ CurToken ] );
    }
}


void GetNextToken(void)
{
    CurToken = T_NULL;
    if( MacroPtr != NULL ) {
        GetMacroToken();
        if( CurToken == T_NULL ) {                      /* 29-may-95 */
            // prevents macro expansion from merging with trailing text
            // to form new tokens in pre-processed output
            CurToken = T_WHITE_SPACE;
        }
    } else {
        for(;;) {
            if( CurrChar == EOF_CHAR ) break;
            if( (CharSet[ CurrChar ] & C_WS) == 0 ) break;
            if( CurrChar != '\r' ) PrtChar( CurrChar );
            NextChar();
        }
        CurToken = ScanToken();
    }
}

⌨️ 快捷键说明

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