inout.c

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

C
968
字号
/***************************************************************************
*
*                            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:  Input/output for listing, errors and source files.
*
****************************************************************************/


#include "ftnstd.h"
#include "progsw.h"
#include "cpopt.h"
#include "errcod.h"
#include "global.h"
#include "omodes.h"
#include "cioconst.h"
#include "csetinfo.h"
#include "fmemmgr.h"
#include "ferror.h"
#include "comio.h"
#include "inout.h"
#include "sdfile.h"

#include "banner.h"
#ifdef _BANEXTRA
#undef _BANEXTRA
#endif
#define _BANEXTRA

#include <stdio.h>
#include <string.h>
#include <time.h>

extern  void            BISetSrcFile( void );
extern  void            Suicide(void);
extern  void            PrtOptions(void);
extern  lib_handle      IncSearch(char *);
extern  int             LibRead(lib_handle);
extern  bool            LibEof(lib_handle);
extern  bool            LibError(lib_handle,char *);
extern  void            IncMemClose(lib_handle);
extern  void            SDSetAttr(file_attr);
extern  void            SDInitAttr(void);
extern  void            SDScratch(char *);
extern  file_handle     SDOpen(char *,int);
extern  void            SDClose(file_handle);
extern  uint            SDRead(file_handle,byte *,uint);
extern  void            SDWrite(file_handle,byte *,uint);
extern  bool            SDError(file_handle,char *);
extern  bool            SDEof(file_handle fp);
extern  char            *SDSrcExtn(char *fn);
extern  char            *SDFName(char *fn);
extern  void            SDInitIO(void);
extern  void            MsgFormat(char *,char *,...);
extern  int             CopyMaxStr(char *,char *,int);
extern  int             MakeName(char *,char *,char *);
#if _EDITOR == _ON
extern  bool            SDIsInternal(file_handle);
extern  file_handle     EdOpenf(char *,int);
#endif

extern  char            FFCtrlSeq[];
extern  char            SkipCtrlSeq[];
extern  char            NormalCtrlSeq[];
extern  char            SDTermOut[];
#if ! defined( __UNIX__ )
extern  char            SDPrtName[];
#endif
extern  char            ForExtn[];
extern  char            ErrExtn[];
extern  char            LstExtn[];
extern  file_attr       DskAttr;
extern  file_attr       PrtAttr;
extern  file_attr       TrmAttr;
extern  file_attr       ErrAttr;
extern  file_handle     FStdOut;
extern  character_set   CharSetInfo;

#define _Copyright "1984"

#define       VERSION _WFC_VERSION_
#if _CPU == 8086
    #define _Banner "FORTRAN 77/16 Optimizing Compiler"
#elif _CPU == 386
    #define _Banner "FORTRAN 77/32 Optimizing Compiler"
#elif _CPU == _AXP
    #define _Banner "FORTRAN 77 Alpha AXP Optimizing Compiler"
#elif _CPU == _PPC
    #define _Banner "FORTRAN 77 PowerPC Optimizing Compiler"
#else
    #error Unknown System
#endif


static char           *ListBuff;      // listing file buffer
static file_handle    ListFile;       // file pointer for the listing file
static int            ListCursor;     // offset into "ListBuff"

static byte           ListCount;      // # of lines printed to listing file
static byte           ListFlag;       // flag for listing file

static char           *ErrBuff;       // error file buffer
static file_handle    ErrFile;        // file pointer for the error file
static int            ErrCursor;      // offset into "ErrBuff"

static char           *TermBuff;      // terminal file buffer
static file_handle    TermFile;       // file pointer for terminal
static int            TermCursor;     // offset into "TermBuff"

//========================================================================
//
//  Initialization routines
//
//========================================================================


void    InitComIO( void ) {
//===================

    TermCursor = 0;
    ErrCursor  = 0;
    ListCursor = 0;
    // Point "terminal" buffer and ".ERR" file buffer to static area
    // so that we can report an error before memory initialization.
    TermBuff = TokenBuff;
    ErrBuff = &TokenBuff[ 256 ];
    ListBuff = NULL;
    CurrFile = NULL;
    ErrFile = NULL;
    ListFile = NULL;
    ListFlag = 0;
    ListCursor = 0;
    ListCount = 0;
    SDInitIO();
    TermFile = FStdOut;
}


void    InitMemIO( void ) {
//===================

    // We've initialized memory - now we can allocate file buffers.
    TermBuff = FMemAlloc( TERM_BUFF_SIZE );
    if( ErrFile == NULL ) {
        // we haven't opened the error file yet so set ErrBuff to NULL
        // so that when we do open the error file we can allocate the
        // buffer at that time
        ErrBuff = NULL;
    } else {
        // the error file has been opened so allocate a buffer for it
        ErrBuff = FMemAlloc( ERR_BUFF_SIZE );
    }
}


void    FiniComIO( void ) {
//===================

    if( TermBuff != &TokenBuff ) {
        FMemFree( TermBuff );
    }
}


//========================================================================
//
//  Source file routines
//
//========================================================================


void    OpenSrc( void ) {
//=================

    file_handle fp;
    char        err_msg[ERR_BUFF_SIZE+1];
    char        bld_name[MAX_FILE+1];
    bool        erase_err;

    erase_err = ErrFile == NULL;
    SDInitAttr();
#if _EDITOR == _ON
    fp = NULL;
    if( SrcExtn == ForExtn ) {
        // try editor buffer without file extension
        fp = EdOpenf( SrcName, READ_FILE );
    }
    if( fp != NULL ) {
        SrcExtn = NULL;
        SrcInclude( SrcName );
        CurrFile->fileptr = fp;
    } else {
        // try editor buffer with file extension
        MakeName( SrcName, SrcExtn, bld_name );
        fp = EdOpenf( bld_name, READ_FILE );
        if( fp != NULL ) {
            SrcInclude( bld_name );
            CurrFile->fileptr = fp;
        } else {
            // try file called <include_name>.FOR.
#else
            MakeName( SrcName, SrcExtn, bld_name );
#endif
            fp = SDOpen( bld_name, READ_FILE );
            if( fp != NULL ) {
                SrcInclude( bld_name );
                CurrFile->fileptr = fp;
            } else {
                SDError( NULL, err_msg );
                InfoError( SM_OPENING_FILE, bld_name, err_msg );
            }
#if _EDITOR == _ON
        }
    }
#endif
    if( erase_err ) {
        CloseErr();
        Erase( ErrExtn );
    }
}


void    IOPurge( void ) {
//=================

// make sure all the input files are closed

    while( CurrFile != NULL ) {
        Conclude();
    }
}


static  uint    SrcRead( void ) {
//=========================

    uint        len;
    file_handle fp;
    char        msg[81];

    fp = CurrFile->fileptr;
    if( CurrFile->flags & INC_LIB_MEMBER ) {
        len = LibRead( fp );
        if( LibEof( fp ) ) {
            ProgSw |= PS_INC_EOF;
        } else if( LibError( fp, msg ) ) {
            InfoError( SM_IO_READ_ERR, CurrFile->name, msg );
            ProgSw |= PS_INC_EOF;
        }
    } else {
        len = SDRead( fp, SrcBuff, SRCLEN );
        if( SDEof( fp ) ) {
            ProgSw |= PS_INC_EOF;
        } else if( SDError( fp, msg ) ) {
            InfoError( SM_IO_READ_ERR, CurrFile->name, msg );
            ProgSw |= PS_INC_EOF;
        }
    }
    return( len );
}


void    ReadSrc( void ) {
//=================

    uint        len;

    // If we are loading source as a result of an undefined
    // subprogram (as opposed to using an C$INCLUDE option),
    // then indicate EOF since the main source file may have
    // the C$DATA option in it in which case "CurrFile" will
    // not be NULL after calling "Conclude()".
    if( CurrFile->flags & INC_DATA_OPTION ) {
        ProgSw |= PS_SOURCE_EOF;
    } else {
        len = SrcRead();
        if( ProgSw & PS_INC_EOF ) {
            CurrFile->flags |= CONC_PENDING;
            if( CurrFile->link == NULL ) {
                ProgSw |= PS_SOURCE_EOF;
            }
        } else {
            CurrFile->rec++;
            SrcBuff[ len ] = NULLCHAR;
        }
    }
}


static  bool    AlreadyOpen( char *name ) {
//=========================================

    source_t    *src;

    src = CurrFile;
    for(;;) {
        if( src == NULL ) return( FALSE );
        if( strcmp( name, src->name ) == 0 ) break;
        src = src->link;
    }
    InfoError( CO_ALREADY_OPEN, name );
    return( TRUE );
}


void    Include( char *inc_name ) {
//=================================

    file_handle fp;
    char        bld_name[MAX_FILE+1];
    char        err_msg[ERR_BUFF_SIZE+1];

    SDInitAttr();
    CopyMaxStr( inc_name, bld_name, MAX_FILE );
    MakeName( bld_name, SDSrcExtn( bld_name ), bld_name );
    if( AlreadyOpen( inc_name ) ) return;
    if( AlreadyOpen( bld_name ) ) return;
#if _EDITOR == _ON
    fp = EdOpenf( inc_name, READ_FILE );
    if( fp != NULL ) {
        SrcInclude( inc_name );
        CurrFile->fileptr = fp;
    } else {   // guess editor buffer <include_name>.FOR.
        fp = EdOpenf( bld_name, READ_FILE );
        if( fp != NULL ) {
            SrcInclude( bld_name );
            CurrFile->fileptr = fp;
        } else {
#endif
            // try file called <include_name>.FOR.
            fp = SDOpen( bld_name, READ_FILE );
            if( fp != NULL ) {
                SrcInclude( bld_name );
                CurrFile->fileptr = fp;
            } else {
                // get error message before next i/o
                SDError( NULL, err_msg );
                // try library
                fp = IncSearch( inc_name );
                if( fp != NULL ) {
                    // SrcInclude( inc_name ) now done in LIBSUPP
                    CurrFile->fileptr = fp;
                    CurrFile->flags |= INC_LIB_MEMBER;
                } else {
                    // could not open include file
                    InfoError( SM_OPENING_FILE, bld_name, err_msg );
                }
            }
#if _EDITOR == _ON
        }
    }
#endif
    // clear RetCode so that we don't get "file not found" returned
    // because we could not open include file
    RetCode = _SUCCESSFUL;
    {
        extern  void    AddDependencyInfo(source_t *);

        AddDependencyInfo( CurrFile );
    }
}


bool    SetLst( bool new ) {
//==========================

    bool        old;

    old = ( ListFlag & LF_QUIET ) == 0;
    if( new ) {
        ListFlag &= ~LF_QUIET;
    } else {
        ListFlag |= LF_QUIET;
    }
    return( old );
}


void    SrcInclude( char *name ) {
//================================

    source_t    *src;

    src = FMemAlloc( sizeof( source_t ) );
    src->name = FMemAlloc( strlen( name ) + 1 );
    strcpy( src->name, name );
    src->rec = 0;
    src->link = CurrFile;
    src->options = NewOptions;
    src->flags = 0;
    if( CurrFile != NULL ) {
        NewOptions = Options;
        if( ( Options & OPT_INCLIST ) == 0 ) {
            SetLst( FALSE );
        }
    }
    CurrFile = src;
    if( CurrFile->link ) {
        // tell the browser which file we are going into (not for the main
        // source file since we have not yet initialized the dwarf library)
        BISetSrcFile();
    }
}


void    Conclude( void ) {
//==================

    source_t    *old;

    old = CurrFile;
    CurrFile = CurrFile->link;
    if( CurrFile != NULL ) {
        NewOptions = old->options;
        Options = NewOptions;
        if( ( ( CurrFile->link == NULL ) && ( Options & OPT_LIST ) ) ||
              ( Options & OPT_INCLIST ) ) {
            SetLst( TRUE );
        } else {
            SetLst( FALSE );
        }
    }
    if( old->flags & INC_LIB_MEMBER ) {
        IncMemClose( old->fileptr );
    } else {
        SDClose( old->fileptr );
    }
    FMemFree( old->name );
    FMemFree( old );
    ProgSw &= ~PS_INC_EOF;
    BISetSrcFile();             // tell browser which file we return to
}


//========================================================================
//
//  Error file routines
//
//========================================================================


static  file_handle Open( char *fn, char *extn, int mode ) {
//==========================================================

    file_handle ptr;
    char        buffer[MAX_FILE+1];
    char        errmsg[81];

    MakeName( fn, extn, buffer );
    ptr = SDOpen( buffer, mode );
    if( SDError( ptr, errmsg ) ) {
        InfoError( SM_OPENING_FILE, &buffer, &errmsg );
    }
    return( ptr );
}


void    OpenErr( void ) {
//=================

    if( ( Options & OPT_ERRFILE ) &&
        ( ( ProgSw & PS_ERR_OPEN_TRIED ) == 0 ) ) {

⌨️ 快捷键说明

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