summaryrefslogtreecommitdiff
path: root/dviware/dvgtk/unixio.c
blob: 3c54a8a8f9a41aaea947f011957c2c1996041028 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* unixio.c - Unix terminal and signal handling
   $Id: unixio.c,v 0.2 1997/03/28 03:17:33 tjchol01 Exp $
   Authors: Andrew Trevorrow, Ian Dall, Geoffrey Tobin, Tomasz J. Cholewo
 */

#include "dvgt.h"
#include "unixio.h"
#include "screenio.h"		/* for MesgString, MesgLine */

/* Set DV_FD (ioctl file descriptor) to 0 or 1 ? */
#define DV_FD  STDIN_FILENO

sig_flags_t sig_flags;
cmode_flags_t cmode_flags;

/* Alex Dickinson
   Procedures for setting and resetting UNIX tty characteristics.
   Interesting functions are:
   save_init_tty;
   restore_init_tty;
   save_temp_tty;
   restore_temp_tty;
   echoon;
   echooff;
   singlecharon;
   singlecharoff;
   buffercount;
   suspend;
   A side effect of calling save_temp_tty is to set up signal handling
   to reset the terminal characteristics appropriately for the various
   interrupt signals.
 */

/*****************************************************************
*    terminal handling
*/

#if defined(HAVE_TERMIO_H)
#include <fcntl.h>
#include <termio.h>
#define TERMIO
#ifdef NCC
#define MAXCC NCC
#else
#define MAXCC 256
#endif
#define DV_GTTY TCGETA
#define DV_STTY TCSETAW
typedef struct termio dv_tty;
unsigned char saved_cc[NCC];
#else
#include <sgtty.h>
#define DV_GTTY TIOCGETP
#define DV_STTY TIOCSETN
typedef struct sgttyb dv_tty;
#endif /* TERMIOS || TERMIO */

#include <signal.h>
#include <errno.h>

static dv_tty init_tty_state;	/* store initial terminal characteristics */
static dv_tty temp_tty_state;	/* current terminal characteristics */

/* locally defined and used functions and data */

static void setsignals ();
static void reportio PARAMS ((int iostat));
static int iostatus;		/* return status of ioctl system function */

static void 
reportio (int iostat)
{
  if (iostat < 0)
    {				/* an ioctl error */
      char *str = "";
      String cstr;
      switch (errno)
	{
	  /* generic errors */
#ifdef EBADF
	case EBADF:
	  str = "Not a valid, open, file descriptor!";
	  break;
#endif
#ifdef EINTR
	case EINTR:
	  str = "Signal caught.";
	  break;
#endif
#ifdef ENOTTY
	case ENOTTY:
	  str = "No device driver, or doesn't accept control functions!";
	  break;
#endif
	  /* specialised errors */
#ifdef EFAULT
	case EFAULT:
	  str = "Illegal data transfer address!";
	  break;
#endif
#ifdef EINVAL
	case EINVAL:
	  str = "Driver ignorant of this request or arg!";
	  break;
#endif
#ifdef EIO
	case EIO:
	  str = "Physical I/O error!";
	  break;
#endif
#ifdef ENOLINK
	case ENOLINK:
	  str = "Remote machine link no longer active!";
	  break;
#endif
#ifdef ENXIO
	case ENXIO:
	  str = "Subdevice cannot perform requested service!";
	  break;
#endif
	  /* gt - should be ample space in cstr. */
	default:
	  sprintf (cstr, "Unknown error %d !", errno);
	  str = cstr;
	  break;
	}
      MesgString ("ioctl: ");
      MesgString (str);
      MesgLine ();
    }
}				/* reportio */

void 
save_init_tty ()
{
  /* Save the original tty characteristics and set up the signalling. */
  save_temp_tty ();
  init_tty_state = temp_tty_state;
}				/* save_init_tty */

void 
restore_init_tty ()
{
  /* Restore the original tty characteristics. */
  temp_tty_state = init_tty_state;
  restore_temp_tty ();
}				/* restore_init_tty */

void 
save_temp_tty ()
{
  /* Save the current tty characteristics and set up the signalling. */
  iostatus = ioctl (DV_FD, DV_GTTY, &temp_tty_state);
  reportio (iostatus);
  setsignals ();
}				/* save_temp_tty */

void 
restore_temp_tty ()
{
  /* Restore the recent tty characteristics. */
  iostatus = ioctl (DV_FD, DV_STTY, &temp_tty_state);
  reportio (iostatus);
}				/* restore_temp_tty */

void 
singlecharon ()
{
  /* Set driver to read characters as they are typed without waiting for a
     terminator. Echo remains unchanged. */
  dv_tty s;

  if (cmode_flags.cbreak != 1)
    {
      reportio (ioctl (DV_FD, DV_GTTY, &s));
#ifndef TERMIO
      s.sg_flags |= CBREAK;
#else /* TERMIO */
      saved_cc[VEOL] = s.c_cc[VEOL];
      saved_cc[VEOF] = s.c_cc[VEOF];
      s.c_lflag &= ~ICANON;	/* Disable erase/kill processing */
      s.c_cc[VMIN] = 1;		/* Input should wait for at least 1 char */
      s.c_cc[VTIME] = 0;	/* no matter how long that takes.  */
#endif /* TERMIO */
      reportio (ioctl (DV_FD, DV_STTY, &s));
#ifdef FLUSH_STDIN
      fflush (stdin);
#endif /* FLUSH_STDIN */

      cmode_flags.cbreak = 1;
    }
}				/* singlecharon */

void 
singlecharoff ()
{
  /* Turn off single character read mode. */
  dv_tty s;

  if (cmode_flags.cbreak != 0)
    {
      iostatus = ioctl (DV_FD, DV_GTTY, &s);
      reportio (iostatus);
#ifndef TERMIO
      s.sg_flags &= ~CBREAK;
#else /* TERMIO */
      s.c_lflag |= ICANON;	/* Enable erase/kill processing */
      s.c_cc[VEOL] = saved_cc[VEOL];
      s.c_cc[VEOF] = saved_cc[VEOF];
#endif /* TERMIO */
      iostatus = ioctl (DV_FD, DV_STTY, &s);
      reportio (iostatus);

      cmode_flags.cbreak = 0;
    }
}				/* singlecharoff */

void 
echoon ()
{
  /* Turn character echoing on. */
  dv_tty s;

  reportio (ioctl (DV_FD, DV_GTTY, &s));
#ifndef TERMIO
  s.sg_flags |= ECHO;
#else /* TERMIO */
  s.c_lflag |= ECHO;
#endif /* TERMIO */
  reportio (ioctl (DV_FD, DV_STTY, &s));
  cmode_flags.echo = 1;
}				/* echoon */

void 
echooff ()
{
  /* Turn character echoing off. */
  dv_tty s;

  iostatus = ioctl (DV_FD, DV_GTTY, &s);
  reportio (iostatus);
#ifndef TERMIO
  s.sg_flags &= ~ECHO;
#else /* TERMIO */
  s.c_lflag &= ~ECHO;
#endif /* TERMIO */
  iostatus = ioctl (DV_FD, DV_STTY, &s);
  reportio (iostatus);
  cmode_flags.echo = 0;
}				/* echooff */

void 
rawouton ()
{
  dv_tty s;

  iostatus = ioctl (DV_FD, DV_GTTY, &s);
  reportio (iostatus);
#ifdef TERMIO
  s.c_oflag &= ~OPOST;		/* Disables tab interpretation etc. */
#endif /* TERMIO */
  iostatus = ioctl (DV_FD, DV_STTY, &s);
  reportio (iostatus);

  cmode_flags.raw = 1;
}				/* rawouton */

void 
rawoutoff ()
{
  dv_tty s;

  iostatus = ioctl (DV_FD, DV_GTTY, &s);
  reportio (iostatus);
#ifdef TERMIO			/* TERMIO */
  s.c_oflag |= OPOST;
#endif /* TERMIO */
  iostatus = ioctl (DV_FD, DV_STTY, &s);
  reportio (iostatus);

  cmode_flags.raw = 0;
}				/* rawoutoff */

int 
buffercount ()
{
  /* Return true if there are any characters currently in the input buffer. */
#ifndef TERMIO
  long count;
  iostatus = ioctl (DV_FD, FIONREAD, &count);
  reportio (iostatus);
  return (count > 0);
#else /* TERMIO */
  int c, flags, ret;

  /* Save the current fcntl flags and make reads return without waiting */
  flags = fcntl (0, F_GETFL, 0);
  fcntl (0, F_SETFL, flags | O_NDELAY);		/* This might be SysV specific */
  if ((c = getchar ()) != EOF)
    {
      ungetc (c, stdin);
      ret = 1;
    }
  else
    {
      ret = 0;
    }
  fcntl (0, F_SETFL, flags);	/* This might be SysV specific */
  return ret;
#endif /* TERMIO */
}				/* buffercount */

RETSIGTYPE 
handleint (int sig)
{
  /* Catch signals from tty.
     If sig is an interrupt, set the intr flag,
     Otherwise it was a suspend, so set the tstop flag. */
  fflush (stdin);
  if (sig == SIGINT)
    {
      sig_flags.intr = 1;
    }
  else
    {
      sig_flags.tstop = 1;
    }
#ifdef TERMIO			/* TERMIO */
  setsignals ();		/* Because SysV forgets its handlers */
#endif /* TERMIO */
}				/* handleint */

static void 
setsignals ()
{
  /* Signal initialization. */
  signal (SIGINT, handleint);
#ifndef TERMIO
  signal (SIGTSTP, handleint);
#endif /* TERMIO */
}				/* setsignals */

void 
suspend ()
{
  /* Suspend the process */
#ifndef TERMIO
  signal (SIGTSTP, SIG_DFL);
  kill (0, SIGTSTP);
  /* resumed again, goody! */
  setsignals ();
#else
  /* don't know what to do */
#endif /* TERMIO */
}				/* suspend */

#undef IN_UNIXIO_C