File: ioctl.c

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (201 lines) | stat: -rw-r--r-- 4,756 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* NetHack 3.6	ioctl.c	$NHDT-Date: 1520099308 2018/03/03 17:48:28 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.13 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Kenneth Lorber, Kensington, Maryland, 2015. */
/* NetHack may be freely redistributed.  See license for details. */

/* This cannot be part of hack.tty.c (as it was earlier) since on some
   systems (e.g. MUNIX) the include files <termio.h> and <sgtty.h>
   define the same constants, and the C preprocessor complains. */

#include "hack.h"

#if defined(BSD_JOB_CONTROL) || defined(_BULL_SOURCE)
#ifdef HPUX
#include <bsdtty.h>
#else
#if defined(AIX_31) && !defined(_ALL_SOURCE)
#define _ALL_SOURCE /* causes struct winsize to be present */
#ifdef _AIX32
#include <sys/ioctl.h>
#endif
#endif
#if defined(_BULL_SOURCE)
#include <termios.h>
struct termios termio;
#undef TIMEOUT            /* defined in you.h and sys/tty.h */
#include <sys/tty.h>      /* define winsize */
#include <sys/ttold.h>    /* define struct ltchars */
#include <sys/bsdioctl.h> /* define TIOGWINSZ */
#else
#ifdef LINUX
#include <bsd/sgtty.h>
#else
#include <sgtty.h>
#endif
#endif
#endif
struct ltchars ltchars;
struct ltchars ltchars0 = { -1, -1, -1, -1, -1, -1 }; /* turn all off */
#else

#ifdef POSIX_TYPES
#include <termios.h>
struct termios termio;
#if defined(BSD) || defined(_AIX32) || defined(__linux__)
#if defined(_AIX32) && !defined(_ALL_SOURCE)
#define _ALL_SOURCE
#endif
#include <sys/ioctl.h>
#endif
#else
#include <termio.h> /* also includes part of <sgtty.h> */
#if defined(TCSETS) && !defined(AIX_31)
struct termios termio;
#else
struct termio termio;
#endif
#endif
#if defined(AMIX) || defined(__APPLE__)
#include <sys/ioctl.h>
#endif /* AMIX */
#endif

#ifdef SUSPEND /* BSD isn't alone anymore... */
#include <signal.h>
#endif

/* AVOID_WIN_IOCTL can be uncommented in unixconf.h
 * to force USE_WIN_IOTCL to remain undefined,
 * instead of the restricted explicit opt-in
 * logic that used to be here.
 */
#if defined(TIOCGWINSZ) && !defined(AVOID_WIN_IOCTL)
#define USE_WIN_IOCTL
#include "tcap.h" /* for LI and CO */
#endif

#ifdef _M_UNIX
extern void NDECL(sco_mapon);
extern void NDECL(sco_mapoff);
#endif
#ifdef __linux__
extern void NDECL(linux_mapon);
extern void NDECL(linux_mapoff);
#endif

#ifdef AUX
void
catch_stp()
{
    signal(SIGTSTP, SIG_DFL);
    dosuspend();
}
#endif /* AUX */

void
getwindowsz()
{
#ifdef USE_WIN_IOCTL
    /*
     * ttysize is found on Suns and BSD
     * winsize is found on Suns, BSD, and Ultrix
     */
    struct winsize ttsz;

    if (ioctl(fileno(stdin), (int) TIOCGWINSZ, (char *) &ttsz) != -1) {
        /*
         * Use the kernel's values for lines and columns if it has
         * any idea.
         */
        if (ttsz.ws_row)
            LI = ttsz.ws_row;
        if (ttsz.ws_col)
            CO = ttsz.ws_col;
    }
#endif
}

void
getioctls()
{
#ifdef BSD_JOB_CONTROL
    (void) ioctl(fileno(stdin), (int) TIOCGLTC, (char *) &ltchars);
    (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars0);
#else
#ifdef POSIX_TYPES
    (void) tcgetattr(fileno(stdin), &termio);
#else
#if defined(TCSETS) && !defined(AIX_31)
    (void) ioctl(fileno(stdin), (int) TCGETS, &termio);
#else
    (void) ioctl(fileno(stdin), (int) TCGETA, &termio);
#endif
#endif
#endif
    getwindowsz();
#ifdef AUX
    (void) signal(SIGTSTP, catch_stp);
#endif
}

void
setioctls()
{
#ifdef BSD_JOB_CONTROL
    (void) ioctl(fileno(stdin), (int) TIOCSLTC, (char *) &ltchars);
#else
#ifdef POSIX_TYPES
    (void) tcsetattr(fileno(stdin), TCSADRAIN, &termio);
#else
#if defined(TCSETS) && !defined(AIX_31)
    (void) ioctl(fileno(stdin), (int) TCSETSW, &termio);
#else
    (void) ioctl(fileno(stdin), (int) TCSETAW, &termio);
#endif
#endif
#endif
}

#ifdef SUSPEND /* No longer implies BSD */
int
dosuspend()
{
#ifdef SYSCF
    /* NB: check_user_string() is port-specific. */
    if (!sysopt.shellers || !sysopt.shellers[0]
        || !check_user_string(sysopt.shellers)) {
        Norep("Suspend command not available.");
        return 0;
    }
#endif
#if defined(SIGTSTP) && !defined(NO_SIGNAL)
    if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
        suspend_nhwindows((char *) 0);
#ifdef _M_UNIX
        sco_mapon();
#endif
#ifdef __linux__
        linux_mapon();
#endif
        (void) signal(SIGTSTP, SIG_DFL);
#ifdef AUX
        (void) kill(0, SIGSTOP);
#else
        (void) kill(0, SIGTSTP);
#endif
#ifdef _M_UNIX
        sco_mapoff();
#endif
#ifdef __linux__
        linux_mapoff();
#endif
        resume_nhwindows();
    } else {
        pline("I don't think your shell has job control.");
    }
#else
    pline("Sorry, it seems we have no SIGTSTP here.  Try ! or S.");
#endif
    return (0);
}
#endif /* SUSPEND */