Annotation of sbbs/src/xpdev/conwrap.c, revision 1.1

1.1     ! root        1: /* conwrap.c */
        !             2: 
        !             3: /* DOS's kbhit and getch functions for Unix - Casey Martin 2000 */
        !             4: 
        !             5: /* $Id: conwrap.c,v 1.12 2005/04/29 22:41:55 deuce Exp $ */
        !             6: 
        !             7: /****************************************************************************
        !             8:  * @format.tab-size 4          (Plain Text/Source Code File Header)                    *
        !             9:  * @format.use-tabs true       (see http://www.synchro.net/ptsc_hdr.html)              *
        !            10:  *                                                                                                                                                     *
        !            11:  * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html         *
        !            12:  *                                                                                                                                                     *
        !            13:  * This library is free software; you can redistribute it and/or                       *
        !            14:  * modify it under the terms of the GNU Lesser General Public License          *
        !            15:  * as published by the Free Software Foundation; either version 2                      *
        !            16:  * of the License, or (at your option) any later version.                                      *
        !            17:  * See the GNU Lesser General Public License for more details: lgpl.txt or     *
        !            18:  * http://www.fsf.org/copyleft/lesser.html                                                                     *
        !            19:  *                                                                                                                                                     *
        !            20:  * Anonymous FTP access to the most recent released source is available at     *
        !            21:  * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net     *
        !            22:  *                                                                                                                                                     *
        !            23:  * Anonymous CVS access to the development source and modification history     *
        !            24:  * is available at cvs.synchro.net:/cvsroot/sbbs, example:                                     *
        !            25:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs login                       *
        !            26:  *     (just hit return, no password is necessary)                                                     *
        !            27:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src                *
        !            28:  *                                                                                                                                                     *
        !            29:  * For Synchronet coding style and modification guidelines, see                                *
        !            30:  * http://www.synchro.net/source.html                                                                          *
        !            31:  *                                                                                                                                                     *
        !            32:  * You are encouraged to submit any modifications (preferably in Unix diff     *
        !            33:  * format) via e-mail to [email protected]                                                                      *
        !            34:  *                                                                                                                                                     *
        !            35:  * Note: If this box doesn't appear square, then you need to fix your tabs.    *
        !            36:  ****************************************************************************/
        !            37: 
        !            38: #if defined(__unix__)
        !            39: 
        !            40: #include <stdlib.h>
        !            41: #include <string.h>    /* memcpy */
        !            42: #include <unistd.h>
        !            43: #include <termios.h>
        !            44: 
        !            45: #include <sys/time.h>
        !            46: #include <sys/types.h>
        !            47: #include <signal.h>
        !            48: 
        !            49: #include "conwrap.h"                           /* Verify prototypes */
        !            50: 
        !            51: static struct termios current;         /* our current term settings                    */
        !            52: static struct termios original;     /* old termios settings                                    */
        !            53: static int beensetup = 0;           /* has _termios_setup() been called?       */
        !            54: static int istty = 0;                          /* is stdin a tty?                                              */              
        !            55: 
        !            56: /* Resets the termios to its previous state */
        !            57: void _termios_reset(void)
        !            58: {
        !            59:        tcsetattr(STDIN_FILENO, TCSANOW, &original);
        !            60: }
        !            61: 
        !            62: /************************************************
        !            63:   This pair of functions handles Ctrl-Z presses
        !            64: ************************************************/
        !            65: #if defined(__BORLANDC__)
        !            66:         #pragma argsused
        !            67: #endif
        !            68: void _sighandler_stop(int sig)
        !            69: {
        !            70:     /* clean up the terminal */
        !            71:     _termios_reset();
        !            72: 
        !            73:     /* ... and stop */
        !            74:        kill(getpid(), SIGSTOP);
        !            75: }
        !            76: #if defined(__BORLANDC__)
        !            77:         #pragma argsused
        !            78: #endif
        !            79: void _sighandler_cont(int sig)
        !            80: {
        !            81:     /* restore terminal */
        !            82:        tcsetattr(STDIN_FILENO, TCSANOW, &current);
        !            83: }
        !            84: 
        !            85: 
        !            86: /* Prepares termios for non-blocking action */
        !            87: void _termios_setup(void)
        !            88: {
        !            89:        beensetup = 1;
        !            90:     
        !            91:        tcgetattr(STDIN_FILENO, &original);
        !            92:   
        !            93:        memcpy(&current, &original, sizeof(struct termios));
        !            94:        current.c_cc[VMIN] = 1;           /* read() will return with one char */
        !            95:        current.c_cc[VTIME] = 0;          /* read() blocks forever */
        !            96:        current.c_lflag &= ~ICANON;       /* character mode */
        !            97:     current.c_lflag &= ~ECHO;         /* turn off echoing */
        !            98:        tcsetattr(STDIN_FILENO, TCSANOW, &current);
        !            99: 
        !           100:     /* Let's install an exit function, also.  This way, we can reset
        !           101:      * the termios silently
        !           102:         */
        !           103:     atexit(_termios_reset);
        !           104: 
        !           105:     /* install the Ctrl-Z handler */
        !           106:     signal(SIGTSTP, _sighandler_stop);
        !           107:     signal(SIGCONT, _sighandler_cont);
        !           108: }
        !           109: 
        !           110: 
        !           111: int kbhit(void)
        !           112: {
        !           113:        fd_set inp;
        !           114:        struct timeval timeout = {0, 0};
        !           115: 
        !           116:        if(!istty) {
        !           117:                istty = isatty(STDIN_FILENO);
        !           118:                if(!istty)
        !           119:                        return 0;
        !           120:        }
        !           121: 
        !           122:     if(!beensetup)
        !           123:        _termios_setup();
        !           124: 
        !           125:        /* set up select() args */
        !           126:        FD_ZERO(&inp);
        !           127:        FD_SET(STDIN_FILENO, &inp);
        !           128: 
        !           129:        if(select(STDIN_FILENO+1, &inp, NULL, NULL, &timeout)<1)
        !           130:                return 0;
        !           131:        return 1;
        !           132: }
        !           133: 
        !           134: int getch(void)
        !           135: {
        !           136:        char c;
        !           137: 
        !           138:     if(!beensetup)
        !           139:        _termios_setup();
        !           140: 
        !           141:     /* get a char out of stdin */
        !           142:     read(STDIN_FILENO, &c, 1);
        !           143: 
        !           144:     return c;
        !           145: }
        !           146: 
        !           147: #endif /* __unix__ */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.