tdisp.c

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

C
1,450
字号
/****************************************************************************
*
*                            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:  Terminal display routines.
*
****************************************************************************/


#ifdef AIX
    #define _TPARM_COMPAT
#endif
#if defined(HP) && ( OSVER >= 1100 )
    #define __10_10_COMPAT_CODE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifndef HP
    #include <termios.h>
#else
    #include <stdarg.h>
#endif
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#ifdef _AIX
    #define alloca __alloca
    #define _HAS_NO_CHAR_BIT_FIELDS
#endif
#include <ctype.h>

#if defined( __WATCOMC__ )
#elif !defined( HP )
#else
    #define TIOCGWINSZ      _IOR('t', 107, struct winsize) /* get window size */
    struct  winsize {
        unsigned short  ws_row;         /* Rows, in characters          */
        unsigned short  ws_col;         /* Columns, in characters       */
        unsigned short  ws_xpixel;      /* Horizontal size, pixels      */
        unsigned short  ws_ypixel;      /* Vertical size, pixels        */
    };
#endif

#include <curses.h>
#include <term.h>
#ifdef __WATCOMC__
#include <process.h>
#endif
#include <sys/ioctl.h>

#include "uidef.h"
#include "uiattrs.h"
#include "qdebug.h"

#include "uivirt.h"
#include "qnxuiext.h"

#include "tixparse.h"
#ifndef __WATCOMC__
#include "clibext.h"
#endif

#ifdef AIX
    struct _bool_struct       _aix_cur_bools;
    struct _bool_struct      *cur_bools = &_aix_cur_bools;
#endif

#if defined(HP) && ( OSVER >= 1100 )
    struct __bool_struct      _hp_cur_bools;
    struct __bool_struct *    __cur_bools     = &_hp_cur_bools;
    //struct __num_struct     _hp_cur_nums;
    //struct __num_struct *   __cur_nums      = &_hp_cur_nums;
    struct __str_struct               _hp_cur_strs;
    struct __str_struct       *       __cur_strs      = &_hp_cur_strs;
#endif

// MACRO for checking if a particular escape sequence exists in terminfo for the
// current terminal
#define _capable_of( sequence )         ( (sequence) != NULL )

#define __flush()       {fflush( UIConFile );}
#if defined( SUN )
#define __putp( str )   {tputs( str, 1, (int (*)(char))_con_putchar );}
#elif defined( HP )  && ( OSVER >= 1100 ) && !defined( __GNUC__ )
#define __putp( str )   {tputs( str, 1, _con_putchar );}
#elif defined( HP )
#define __putp( str )   {tputs( str, 1, (void (*)(int))_con_putchar );}
#elif defined( AIX )
#define __putp( str )   {tputs( str, 1, _con_putchar );}
#else
#define __putp( str )   {tputs( str, 1, _con_putchar );}
#endif
#define __putc( c )     {fputc( c, UIConFile );}

extern char     *GetTermType( void );
extern EVENT    tk_keyboardevent( void );

bool    UserForcedTermRefresh= FALSE;

static int _con_putchar( int ch )
{
    fputc( ch, UIConFile );
    return( 0 );
}

#if defined(SUN) && defined(UNIX64)

// Define a tparm interface on sun64 that takes 2 / 3 / 10 arguments.
// Sun64 tparm interface does not allow any default arguments for tparm
#define UNIX_TPARM(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
            tparm((str), (arg1), (arg2), (arg3), (arg4), (arg5), (arg6), (arg7), (arg8), (arg9))
#define UNIX_TPARM2(str, arg1) tparm( (str), (arg1), 0, 0, 0, 0, 0, 0, 0, 0 )
#define UNIX_TPARM3(str, arg1, arg2) tparm((str), (arg1), (arg2), 0, 0, 0, 0, 0, 0, 0)

#else

// dont need this on other unix platforms
#define UNIX_TPARM(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
            tparm((str), (arg1), (arg2), (arg3), (arg4), (arg5), (arg6), (arg7), (arg8), (arg9))
#define UNIX_TPARM2(str, arg1) tparm((str), (arg1))
#define UNIX_TPARM3(str, arg1, arg2) tparm((str), (arg1), (arg2))

#endif

bool TInfCheck( void )
/********************/
{
    extern unsigned     UIDisableShiftChanges;

    // Check to see if the term variable is set
    if( GetTermType()[0]!='\0' ){
        UIDisableShiftChanges= TRUE;
        return( TRUE );
    }
    return( FALSE );
}

/*-
 * set initial mode of term
 */


// Whether to use code-changing optimizations in ti
static bool OptimizeTerminfo= TRUE;

/* TermInfo terminal codes
*/

static void TI_SETATTR( void );

// The following are used to remember which attrs we want
static bool TIABold=    0;      // bold
static bool TIABlink=   0;      // blinking
static bool TIAULine=   0;      // underline
static bool TIARev=     0;      // inverted (reverse)
static bool TIAACS=     0;      // alternate character set

// True if clearing/filling operations will use the current back colour
static bool TI_FillColourSet= FALSE;

// Macros for various terminfo capabilities
#define TI_CURSOR_OFF()         if( _capable_of( cursor_invisible ) ) { \
                                    __putp( cursor_invisible );         \
                                }
#define TI_CURSOR_NORMAL()      if( _capable_of( cursor_normal ) ) {    \
                                    __putp( cursor_normal );            \
                                }
#define TI_CURSOR_BOLD()        if( _capable_of( cursor_visible ) ) {   \
                                    __putp( cursor_visible );           \
                                } else {                                \
                                    TI_CURSOR_NORMAL();                 \
                                }
#define TI_BOLD()       { TIABold= 1; __putp( enter_bold_mode ); }
#define TI_NOBOLD()     { TIABold= 0; TI_SETATTR(); }
#define TI_REVERSE()    { TIARev= 1; TI_SETATTR(); }
#define TI_NOREVERSE()  { TIARev= 0; TI_SETATTR(); }
#define TI_BLINK()      { TIABlink= 1; __putp( enter_blink_mode ); }
#define TI_NOBLINK()    { TIABlink= 0; TI_SETATTR(); }
#define TI_ULINE()      { TIAULine= 1; __putp( enter_underline_mode ); }
#define TI_NOULINE()    { TIAULine= 0; __putp( exit_underline_mode ); }
#define TI_ACS_ON()     { TIAACS= 1; __putp( enter_alt_charset_mode ); }
#define TI_ACS_OFF()    { TIAACS= 0;                                    \
                            if( _capable_of( exit_alt_charset_mode )) { \
                                __putp( exit_alt_charset_mode );        \
                            } else {                                    \
                                TI_SETATTR();                           \
                            }                                           \
                        }
#define TI_WRAP()               if( _capable_of( enter_am_mode ) ) {    \
                                    __putp( enter_am_mode );            \
                                }
#define TI_NOWRAP()             if( _capable_of( exit_am_mode ) ) {     \
                                    __putp( exit_am_mode );             \
                                }
#define TI_CA_ENABLE()          if( _capable_of( enter_ca_mode ) ) {    \
                                    __putp( enter_ca_mode );            \
                                }
#define TI_CA_DISABLE()         if( _capable_of( exit_ca_mode ) ) {     \
                                    __putp( exit_ca_mode );             \
                                }
#define TI_KP_ENABLE()          if( _capable_of( keypad_xmit ) ) {    \
                                    __putp( keypad_xmit );            \
                                }
#define TI_KP_DISABLE()         if( _capable_of( keypad_local ) ) {     \
                                    __putp( keypad_local );             \
                                }

#define TI_RESTORE_ATTR()                               \
{                                                       \
    TIAACS= TIABold= TIABlink= TIAULine= 0;             \
    __putp( exit_attribute_mode );                      \
}

#define TI_RESTORE_COLOUR()                     \
{                                               \
    if( _capable_of( orig_colors ) ) {          \
        __putp( orig_colors );                  \
    } else if( _capable_of( orig_pair ) ) {     \
        __putp( orig_pair );                    \
    }                                           \
}

#define TI_ENABLE_ACS()         if( _capable_of( ena_acs ) ) {  \
                                    __putp( ena_acs );          \
                                }

#define TI_HOME()               TI_CURSOR_MOVE( 0, 0 )

// This weird "do...while" thing is so that TI_CLS acts like a statement

#define TI_CLS()                do {                                      \
                                    if( _capable_of( clear_screen ) ) {   \
                                        __putp( clear_screen );           \
                                        OldRow= OldCol= 0;                \
                                    } else if( _capable_of( clr_eos ) ) { \
                                        TI_HOME();                        \
                                        __putp( clr_eos );                \
                                    }                                     \
                                } while(0)

#define TI_INIT1_STRING()       __putp( init_1string )
#define TI_INIT2_STRING()       __putp( init_2string )
#define TI_INIT3_STRING()       __putp( init_3string )

#define TI_RESET1_STRING()      __putp( reset_1string )
#define TI_RESET2_STRING()      __putp( reset_2string )
#define TI_RESET3_STRING()      __putp( reset_3string )

#define TI_CLEAR_MARGINS()      __putp( clear_margins )

/* Terminal Capabilities
*/

// True if terminal is monochrome
#define TCAP_MONOCHROME ( !( ( _capable_of( set_background ) && _capable_of( set_foreground ) ) || \
                             ( _capable_of( set_a_background ) && _capable_of( set_a_foreground ) ) || \
                             ( _capable_of( set_color_pair ) ) ) )

// True if terminal won't scroll on us
#define TCAP_NOSCROLL   ( !auto_right_margin || _capable_of( exit_am_mode ) )

// True if terminal can clear screen fast
#define TCAP_CLS        ( (clear_screen && _capable_of( clear_screen ) ) || \
                                (clr_eos && _capable_of( clr_eos ) ) )

static unsigned         TI_repeat_cutoff = ~0U;
static bool             TI_ignore_bottom_right= TRUE;

static void TI_SETATTR( void )
/****************************/
{
    // we have to reset attributes as some terminals can't turn off
    // attributes with "set_attribues"
    __putp( exit_attribute_mode );

    if( _capable_of( set_attributes ) ) {
        char    *x;

        // Arguments to tparm
        // standout
        // underline
        // reverse
        // blink
        // half intensity
        // bold
        // invisible
        // protected
        // alt. char set
        x = UNIX_TPARM( set_attributes, 0, \
                        TIAULine, TIARev, \
                        TIABlink, 0, TIABold, \
                        0, 0, TIAACS );
        __putp( x );

        UIDebugPrintf0( "\n[******]" );
        UIDebugPrintf1( "%s", set_attributes );
        UIDebugPrintf1( "%s", x );
        UIDebugPrintf0( "[~~~~~~]\n" );
    } else {
/*
        fprintf(stderr, "Doing attributes %s %s %s %s %s\n", \
                TIAULine ? "Underline" : "", \
                TIARev ? "Reverse" : "", \
                TIABlink ? "Blink" : "", \
                TIABold ? "Bold" : "", \
                TIAACS ? "Alternate CS" : "");
*/

        // Believe it or not, some terminals don't have the set_attributes
        // code in the database, so we have to simulate it occasionally
        if( TIAULine && _capable_of( enter_underline_mode ) )
                __putp( enter_underline_mode );

        if( TIARev && _capable_of( enter_reverse_mode ) )
                __putp( enter_reverse_mode );

        if( TIABlink && _capable_of( enter_blink_mode ))
                __putp( enter_blink_mode );

        if( TIABold && _capable_of( enter_bold_mode ))
                __putp( enter_bold_mode );

        if( TIAACS && _capable_of( enter_alt_charset_mode ))
                __putp( enter_alt_charset_mode );
    }
}

/* Find out where using the repeat_char code becomes efficient if we
 * are going to be doing optimizations.
 */
static void ti_find_cutoff( void )
/********************************/
{
    if( _capable_of( repeat_char ) && OptimizeTerminfo ){
        int     i;
        for( i=2; i<80; i++ ){
            if( strlen( UNIX_TPARM3( repeat_char, 'X', i ) )<i ){
                TI_repeat_cutoff= i;

⌨️ 快捷键说明

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