misc.c

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

C
839
字号
/****************************************************************************
*
*                            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:  A hodgepodge of miscellaneous functions.
*
****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>

#ifdef M_I86
 #include <i86.h>
#endif
#include <errno.h>
#ifdef __WATCOMC__
  #include <env.h>
  #include <process.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#include "vi.h"
#ifdef __WIN__
  #include "winvi.h"
#endif
#include "source.h"
#include "posix.h"
#include "win.h"
#ifdef __NT__
  #include <windows.h>
  extern HANDLE       OutputHandle;
#endif

static char *oldPrompt;

static void setPrompt( void )
{
    char        *tmp;

    if( SpawnPrompt[ 0 ] != 0 ) {
        tmp = getenv( PROMPT_ENVIRONMENT_VARIABLE );
        if( tmp != NULL ) {
            oldPrompt = MemAlloc( strlen( tmp ) + 1 );
            strcpy( oldPrompt, tmp );
        } else {
            oldPrompt = NULL;
        }
        setenv( PROMPT_ENVIRONMENT_VARIABLE, SpawnPrompt, TRUE );
    }
}

static void restorePrompt( void )
{
    if( SpawnPrompt[ 0 ] != 0 ) {
        setenv( PROMPT_ENVIRONMENT_VARIABLE, oldPrompt, TRUE );
        if( oldPrompt != NULL ) {
            MemFree( oldPrompt );
            oldPrompt = NULL;
        }
    }
}

static bool clockActive;

static void preSpawn( void ) {
    info        *cinfo;

    clockActive = EditFlags.ClockActive;
    EditFlags.ClockActive = FALSE;
#ifndef __WIN__
    FiniColors();
    if( !EditFlags.LineDisplay ) {
        ClearScreen();
    }
#endif

    /*
     * after a system command, all files could potentially have their
     * read/write attributes changed
     */
    cinfo = InfoHead;
    while( cinfo != NULL ) {
        cinfo->CurrentFile->check_readonly = TRUE;
        cinfo = cinfo->next;
    }
    setPrompt();
}

static void postSpawn( int rc ) {

    restorePrompt();
    VarAddGlobalLong( "Sysrc", (long) rc );
    UpdateCurrentDirectory();

#ifndef __WIN__
    ResetColors();
    // if( (EditFlags.PauseOnSpawnErr && rc != 0 ) ||
    //          !EditFlags.SourceScriptActive ) {
    if( EditFlags.PauseOnSpawnErr && rc != 0 ) {
        MyPrintf("[%s]\n",MSG_PRESSANYKEY);
        GetNextEvent( FALSE );
    }
    ResetSpawnScreen();
    if( !EditFlags.LineDisplay ) {
        ReDisplayScreen();
    }
#endif
    EditFlags.ClockActive = clockActive;
}
#if 0
// ifdef __NT__
#include "batcher.h"
#include <conio.h>
int ExecCmd( char *file_in, char *file_out, char *cmd )
{
    int len;
    unsigned long       stat;
    char *err;
    char buff[256];
    int                 linked;
    err = BatchLink( NULL );
    if( err != NULL ) {
        printf( "link error: %s\n", err );
        exit( 1 );
    }
    BatchSpawn( cmd );
    for( ;; ) {
        len = BatchCollect( buff, sizeof( buff ), &stat );
        if( len == -1 ) {
            printf( "done: status = %d\n", stat );
            break;
        } else if( kbhit() ) {
            if( getch() == 'a' ) {
                BatchAbort();
            } else {
                BatchCancel();
            }
        } else if( len != 0 ) {
            buff[ len ] = '\0';
            printf( "%s", buff );
            fflush( stdout );
        }
    }
    return( 1 );
}
#endif

#ifdef __WIN__
int ExecCmd( char *file_in, char *file_out, char *cmd )
{
    file_in = file_in;
    file_out = file_out;
    preSpawn();
    // fixme - should not ignore file_in and file_out
    SystemRC = MySpawn( cmd );
    postSpawn( SystemRC );
    return( SystemRC );
}
#else
static int doRedirect( int original, char * filename, int mode )
{
    int fh;

    fh = open( filename, mode, S_IREAD | S_IWRITE );
    if( fh != -1 ) {
        close( original );
        if( dup2( fh, original ) == 0 ) {
            return( fh );
        }
    }
    return( -1 );
}

#define MAX_ARGS        128

int doExec( char *std_in, char *std_out, char *cmd )
{
    int         st;
    int         save_in, new_in;
    int         save_out, new_out;
#if 0
    char        buffer[ MAX_INPUT_LINE ];
    char        *argv[ MAX_ARGS ];
    char        *s;
    int         i;
#endif

    preSpawn();
    if( std_in != NULL ) {
        save_in = dup( STDIN_FILENO );
        new_in = doRedirect( STDIN_FILENO, std_in,
                           O_RDONLY | O_BINARY );
        if( new_in == -1 ) {
            close( save_in );
            return( -1 );
        }
    }
    if( std_out != NULL ) {
        save_out = dup( STDOUT_FILENO );
        new_out = doRedirect( STDOUT_FILENO, std_out,
                            O_WRONLY | O_BINARY | O_CREAT | O_TRUNC );
        if( new_out == -1 ) {
            close( save_out );
            if( std_in != NULL ) {
                close( new_in );
                dup2( save_in, STDIN_FILENO );
                close( save_in );
            }
            return( -1 );
        }
    }

#if 0
    strcpy( buffer, cmd );
    s = buffer;
    for( i = 0; i < MAX_ARGS; i++ ) {
        while( isspace( *s ) ) s++;
        if( *s == 0 ) {
            argv[ i ] = NULL;
            break;
        }
        argv[ i ] = s;
        while( *s && !isspace( *s ) ) s++;
        if( *s ) {
            *s++ = 0;
        } else {
            argv[ i + 1 ] = NULL;
            break;
        }
    }

    st = spawnvp( P_WAIT, argv[ 0 ], argv );

#else
    #if defined(__NT__)
        if( cmd == NULL ) {
            st = MySpawn( Comspec );
        } else {
            SetConsoleActiveScreenBuffer( GetStdHandle( STD_OUTPUT_HANDLE ) );
            st = system( cmd );
        }
    #elif defined(__UNIX__)
        st = MySpawn( cmd );
    #else
        st = system( cmd );
    #endif
#endif

    if( std_in != NULL ) {
        close( new_in );
        dup2( save_in, STDIN_FILENO );
        close( save_in );
    }
    if( std_out != NULL ) {
        close( new_out );
        dup2( save_out, STDOUT_FILENO );
        close( save_out );
    }
    postSpawn( st );
    return( st );
}
#if !defined( __DOS__ )
int ExecCmd( char *file_in, char *file_out, char *cmd )
{
    return( doExec( file_in, file_out, cmd ) );
}
#else
int ExecCmd( char *file_in, char *file_out, char *cmd )
{
    if( file_in != NULL || file_out != NULL ) {
        SystemRC = doExec( file_in, file_out, cmd );
    } else {
        preSpawn();
        SystemRC = MySpawn( cmd );
        postSpawn( SystemRC );
    }
    return( SystemRC );
}
#endif
#endif

/*
 * GetResponse - get a response from the user
 */
int GetResponse( char *str, char *res )
{
    int rc;

    rc = PromptForString( str, res, MAX_STR, NULL );
    if( rc == ERR_NO_ERR ) {
        return( GOT_RESPONSE );
    }
    return( rc );

} /* GetResponse */

/*
 * PromptFilesForSave - prompt to save for each file which has
 * been modified.
 */
bool PromptFilesForSave( void )
{
    #ifdef __WIN__
    info        *cinfo;
    int         i;
    int num = 0;
    HWND hwnd_old = NULL;

    if( !EditFlags.SaveOnBuild ) return( TRUE );

    for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next )num ++;

    BringUpFile( InfoHead, TRUE );
    for( i = 0; i<num; i++ ) {
        if( CurrentFile != NULL && CurrentFile->dup_count == 0 &&
            CurrentFile->modified ) {

            /* we have a modified file, so bring to the front */
            BringWindowToTop( Root );
            hwnd_old = SetFocus( Root );

            // file modified -- so prompt for save
            FilePromptForSaveChanges( CurrentFile );
        }
        RotateFileForward();
    }
    if( hwnd_old != NULL ) {
        SetWindowPos( Root, HWND_BOTTOM,0,0,0,0, SWP_NOMOVE|SWP_NOSIZE );
        SetFocus( hwnd_old );
    }
    #endif
    return( TRUE );

} /* PromptFilesForSave */

/*
 * PromptThisFileForSave
 */
bool PromptThisFileForSave( const char *filename )
{
    #ifndef __WIN__
    filename = filename;
    #else
    info        *cinfo;
    HWND hwnd_old = NULL;

    while( isspace( *filename ) ) filename++;
    for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) {
        if( SameFile( cinfo->CurrentFile->name, (char*)filename ) ) {
            if( cinfo->CurrentFile != NULL && cinfo->CurrentFile->dup_count == 0 &&
                cinfo->CurrentFile->modified ) {

                BringUpFile( cinfo, TRUE );

                /* we have a modified file, so bring to the front */
                BringWindowToTop( Root );
                hwnd_old = SetFocus( Root );

                // file modified -- so prompt for save
                FilePromptForSaveChanges( CurrentFile );
            }
        }
    }
    if( hwnd_old != NULL ) {
        SetWindowPos( Root, HWND_BOTTOM,0,0,0,0, SWP_NOMOVE|SWP_NOSIZE );
        SetFocus( hwnd_old );
    }
    #endif
    return( TRUE );

} /* PromptThisFileForSave */

/*
 * QueryFile
 */
bool QueryFile( const char *filename )
{
    info        *cinfo;

    while( isspace( *filename ) ) filename++;
    for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) {
        if( SameFile( cinfo->CurrentFile->name, (char*)filename ) ) return( TRUE );
    }
    return( FALSE );
} /* QueryFile */

/*
 * ExitWithPrompt - try to exit, verifying for every file which has
 * been modified.
 */
bool ExitWithPrompt( bool do_quit )
{
    info        *cinfo;

⌨️ 快捷键说明

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