|
|
1.1 root 1: /* conwrap.c */
2:
3: /* DOS's kbhit and getch functions for Unix - Casey Martin 2000 */
4:
1.1.1.2 ! root 5: /* $Id: conwrap.c,v 1.13 2007/09/17 17:23:58 rswindell Exp $ */
1.1 root 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, ¤t);
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(¤t, &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, ¤t);
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:
1.1.1.2 ! root 110: void _echo_on(void)
! 111: {
! 112: tcgetattr(STDIN_FILENO, ¤t);
! 113: current.c_lflag |= ECHO; /* turn on echoing */
! 114: tcsetattr(STDIN_FILENO, TCSANOW, ¤t);
! 115: }
! 116:
! 117: void _echo_off(void)
! 118: {
! 119: tcgetattr(STDIN_FILENO, ¤t);
! 120: current.c_lflag &= ~ECHO; /* turn off echoing */
! 121: tcsetattr(STDIN_FILENO, TCSANOW, ¤t);
! 122: }
1.1 root 123:
124: int kbhit(void)
125: {
126: fd_set inp;
127: struct timeval timeout = {0, 0};
128:
129: if(!istty) {
130: istty = isatty(STDIN_FILENO);
131: if(!istty)
132: return 0;
133: }
134:
135: if(!beensetup)
136: _termios_setup();
137:
138: /* set up select() args */
139: FD_ZERO(&inp);
140: FD_SET(STDIN_FILENO, &inp);
141:
142: if(select(STDIN_FILENO+1, &inp, NULL, NULL, &timeout)<1)
143: return 0;
144: return 1;
145: }
146:
147: int getch(void)
148: {
149: char c;
150:
151: if(!beensetup)
152: _termios_setup();
153:
154: /* get a char out of stdin */
155: read(STDIN_FILENO, &c, 1);
156:
157: return c;
158: }
159:
160: #endif /* __unix__ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.