Home | History | Annotate | Line # | Download | only in dist
      1 /* $XTermId: xterm_io.h,v 1.71 2024/09/30 08:03:20 tom Exp $ */
      2 
      3 /*
      4  * Copyright 2000-2023,2024 by Thomas E. Dickey
      5  *
      6  *                         All Rights Reserved
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sublicense, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be included
     17  * in all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22  * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
     23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  *
     27  * Except as contained in this notice, the name(s) of the above copyright
     28  * holders shall not be used in advertising or otherwise to promote the
     29  * sale, use or other dealings in this Software without prior written
     30  * authorization.
     31  */
     32 
     33 #ifndef	included_xterm_io_h
     34 #define	included_xterm_io_h
     35 
     36 #include <xterm.h>
     37 
     38 /*
     39  * System-specific definitions (keep these chunks one-per-system!).
     40  *
     41  * FIXME:  some, such as those defining USE_TERMIOS should be moved to xterm.h
     42  * as they are integrated with the configure script.
     43  */
     44 #if defined(__minix)
     45 #define USE_POSIX_TERMIOS 1
     46 #undef HAVE_POSIX_OPENPT	/* present, does not work */
     47 #endif
     48 
     49 #ifdef CSRG_BASED
     50 #define USE_TERMIOS
     51 #endif
     52 
     53 #ifdef __CYGWIN__
     54 #define ATT
     55 #define SVR4
     56 #define SYSV
     57 #define USE_SYSV_TERMIO
     58 #endif
     59 
     60 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__INTERIX) || defined(__APPLE__) || defined(__UNIXWARE__) || defined(__hpux)
     61 #ifndef USE_POSIX_TERMIOS
     62 #define USE_POSIX_TERMIOS
     63 #endif
     64 #endif
     65 
     66 #if defined(AIXV4)
     67 #define USE_POSIX_TERMIOS
     68 #ifndef SYSV
     69 #define SYSV
     70 #endif
     71 #endif
     72 
     73 #ifdef __linux__
     74 #define USE_TERMIOS
     75 #define HAVE_POSIX_OPENPT 1
     76 #define HAVE_PTSNAME 1
     77 #define HAVE_GRANTPT_PTY_ISATTY 1
     78 #endif
     79 
     80 #ifdef __SCO__
     81 #define USE_TERMIOS
     82 #ifndef _SVID3
     83 #define _SVID3
     84 #endif
     85 #endif
     86 
     87 #ifdef Lynx
     88 #define USE_SYSV_TERMIO
     89 #endif
     90 
     91 #ifdef macII
     92 #undef SYSV			/* pretend to be bsd (sgtty.h) */
     93 #endif /* macII */
     94 
     95 #ifdef __GNU__
     96 #define USE_POSIX_TERMIOS
     97 #define HAVE_POSIX_OPENPT 1
     98 #define HAVE_PTSNAME 1
     99 #define HAVE_GRANTPT_PTY_ISATTY 1
    100 #endif
    101 
    102 #if defined(__GLIBC__) && !(defined(__linux__) || defined(__GNU__))
    103 #define USE_POSIX_TERMIOS	/* GNU/KFreeBSD and GNU/KNetBSD */
    104 #endif
    105 
    106 #ifdef __QNX__
    107 #define USE_POSIX_TERMIOS
    108 #endif
    109 
    110 #if defined(__osf__)
    111 #define USE_POSIX_TERMIOS
    112 #undef SYSV
    113 #endif
    114 
    115 /*
    116  * Indirect system dependencies
    117  */
    118 #if defined(SVR4) && !defined(__sgi)
    119 #define USE_TERMIOS
    120 #endif
    121 
    122 #ifdef SYSV
    123 #define USE_SYSV_TERMIO
    124 #endif
    125 
    126 #if defined(USE_POSIX_TERMIOS) && !defined(USE_TERMIOS)
    127 #define USE_TERMIOS
    128 #endif
    129 
    130 /*
    131  * Low-level ioctl, where it is needed or non-conflicting with termio/etc.
    132  */
    133 #ifdef __QNX__
    134 #include <ioctl.h>
    135 #else
    136 #include <sys/ioctl.h>
    137 #endif
    138 
    139 /*
    140  * Terminal I/O includes (termio, termios, sgtty headers).
    141  */
    142 #if defined(USE_POSIX_TERMIOS) && !defined(__hpux)
    143 #include <termios.h>
    144 #elif defined(USE_TERMIOS)
    145 #include <termios.h>
    146 /* this hacked termios support only works on SYSV */
    147 #define USE_ANY_SYSV_TERMIO
    148 #define termio termios
    149 #ifndef __CYGWIN__
    150 #undef  TCGETA
    151 #define TCGETA TCGETS
    152 #undef  TCSETA
    153 #define TCSETA TCSETS
    154 #undef  TCSETAW
    155 #define TCSETAW TCSETSW
    156 #endif
    157 #elif defined(USE_SYSV_TERMIO)
    158 # define USE_ANY_SYSV_TERMIO
    159 # ifdef Lynx
    160 #  include <termio.h>
    161 # else
    162 #  include <sys/termio.h>
    163 # endif
    164 #elif defined(SYSV) || defined(ISC)
    165 # include <sys/termio.h>
    166 #endif /* USE_POSIX_TERMIOS */
    167 
    168 /*
    169  * Stream includes, which declare struct winsize or ttysize.
    170  */
    171 #ifdef SYSV
    172 #ifdef USE_USG_PTYS
    173 #include <sys/stream.h>		/* get typedef used in ptem.h */
    174 #ifdef HAVE_SYS_PTEM_H
    175 #include <sys/ptem.h>		/* get struct winsize */
    176 #endif
    177 #endif /* USE_USG_PTYS */
    178 #endif /* SYSV */
    179 
    180 /*
    181  * Special cases (structures and definitions that have to be adjusted).
    182  */
    183 #if defined(__CYGWIN__) && !defined(TIOCSPGRP)
    184 #include <termios.h>
    185 #define TIOCSPGRP (_IOW('t', 118, pid_t))
    186 #endif
    187 
    188 #ifdef __hpux
    189 #include <sys/bsdtty.h>		/* defines TIOCSLTC */
    190 #endif
    191 
    192 #ifdef ISC
    193 #define TIOCGPGRP TCGETPGRP
    194 #define TIOCSPGRP TCSETPGRP
    195 #endif
    196 
    197 #ifdef Lynx
    198 #include <resource.h>
    199 #elif !(defined(SYSV) || defined(__linux__) || (defined(__QNX__)&&!defined(__QNXNTO__)))
    200 #include <sys/resource.h>
    201 #endif
    202 
    203 #ifdef macII
    204 #undef FIOCLEX
    205 #undef FIONCLEX
    206 #endif /* macII */
    207 
    208 #if defined(__QNX__) || defined(__GNU__) || defined(__osf__)
    209 #undef TIOCSLTC			/* <sgtty.h> conflicts with <termios.h> */
    210 #undef TIOCSLTC
    211 #endif
    212 
    213 #if defined (__sgi) || (defined(__linux__) && defined(__sparc__)) || defined(__UNIXWARE__)
    214 #undef TIOCLSET			/* defined, but not usable */
    215 #endif
    216 
    217 #if defined(sun) || defined(__UNIXWARE__)
    218 #include <sys/filio.h>
    219 #endif
    220 
    221 #if defined(TIOCSLTC) && ! (defined(__linux__) || defined(Lynx) || defined(SVR4))
    222 #define HAS_LTCHARS
    223 #endif
    224 
    225 #if !defined(TTYSIZE_STRUCT)
    226 #if defined(TIOCSWINSZ)
    227 #define USE_STRUCT_WINSIZE 1
    228 #define TTYSIZE_STRUCT struct winsize
    229 #define GET_TTYSIZE(fd, data) ioctl(fd, TIOCGWINSZ, (char *) &data)
    230 #define SET_TTYSIZE(fd, data) ioctl(fd, TIOCSWINSZ, (char *) &data)
    231 #define TTYSIZE_COLS(data) data.ws_col
    232 #define TTYSIZE_ROWS(data) data.ws_row
    233 #endif /* TIOCSWINSZ */
    234 #endif /* TTYSIZE_STRUCT */
    235 
    236 #ifndef USE_STRUCT_WINSIZE
    237 #error "There is a configuration error with struct winsize ifdef"
    238 #endif
    239 
    240 /* "resize" depends upon order of assignments in this macro */
    241 #ifdef USE_STRUCT_WINSIZE
    242 #define setup_winsize(ts, rows, cols, height, width) \
    243     (ts).ws_xpixel = (ttySize_t) (width), \
    244     (ts).ws_ypixel = (ttySize_t) (height), \
    245     TTYSIZE_ROWS(ts) = (ttySize_t) (rows), \
    246     TTYSIZE_COLS(ts) = (ttySize_t) (cols)
    247 #else
    248 #define setup_winsize(ts, rows, cols, height, width) \
    249     TTYSIZE_ROWS(ts) = (ttySize_t) (rows), \
    250     TTYSIZE_COLS(ts) = (ttySize_t) (cols)
    251 #endif
    252 
    253 #if OPT_TRACE
    254 
    255 #ifdef USE_STRUCT_WINSIZE
    256 #define trace_winsize(ts, id) \
    257     TRACE(("%s@%d, TTYSIZE %s chars %dx%d pixels %dx%d\n", \
    258     	   __FILE__, __LINE__, id, \
    259 	   TTYSIZE_ROWS(ts), TTYSIZE_COLS(ts), (ts).ws_ypixel, (ts).ws_xpixel))
    260 #else
    261 #define trace_winsize(ts, id) \
    262     TRACE(("%s@%d, TTYSIZE %s chars %dx%d\n", __FILE__, __LINE__, id, \
    263     	   TTYSIZE_ROWS(ts), TTYSIZE_COLS(ts)))
    264 #endif
    265 
    266 #define TRACE_GET_TTYSIZE(fd, id) { \
    267 	    TTYSIZE_STRUCT debug_ttysize; \
    268 	    if (GET_TTYSIZE(fd, debug_ttysize) == 0) \
    269 		trace_winsize(debug_ttysize, id); \
    270 	    else \
    271 		TRACE(("%s@%d, TTYSIZE failed %s\n", __FILE__, __LINE__, strerror(errno))); \
    272 	}
    273 #else
    274 #define trace_winsize(ts, id)	/* nothing */
    275 #define TRACE_GET_TTYSIZE(fd, id)	/* nothing */
    276 #endif
    277 
    278 typedef unsigned short ttySize_t;
    279 
    280 #ifdef USE_ANY_SYSV_TERMIO
    281 #define TERMIO_STRUCT struct termio
    282 #define ttySetAttr(fd, datap) ioctl(fd, TCSETA, datap)
    283 #define ttyGetAttr(fd, datap) ioctl(fd, TCGETA, datap)
    284 #define ttyFlush(fd)          ioctl(fd, TCFLSH, 1)
    285 #elif defined(USE_POSIX_TERMIOS)
    286 #define TERMIO_STRUCT struct termios
    287 #define ttySetAttr(fd, datap) tcsetattr(fd, TCSANOW, datap)
    288 #define ttyGetAttr(fd, datap) tcgetattr(fd, datap)
    289 #define ttyFlush(fd)          tcflush(fd, TCOFLUSH)
    290 #else
    291 #error Neither termio or termios is enabled
    292 #endif /* USE_ANY_SYSV_TERMIO */
    293 
    294 #endif /* included_xterm_io_h */
    295