source: trunk/minix/drivers/tty/tty.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 60.3 KB
Line 
1/* This file contains the terminal driver, both for the IBM console and regular
2 * ASCII terminals. It handles only the device-independent part of a TTY, the
3 * device dependent parts are in console.c, rs232.c, etc. This file contains
4 * two main entry points, tty_task() and tty_wakeup(), and several minor entry
5 * points for use by the device-dependent code.
6 *
7 * The device-independent part accepts "keyboard" input from the device-
8 * dependent part, performs input processing (special key interpretation),
9 * and sends the input to a process reading from the TTY. Output to a TTY
10 * is sent to the device-dependent code for output processing and "screen"
11 * display. Input processing is done by the device by calling 'in_process'
12 * on the input characters, output processing may be done by the device itself
13 * or by calling 'out_process'. The TTY takes care of input queuing, the
14 * device does the output queuing. If a device receives an external signal,
15 * like an interrupt, then it causes tty_wakeup() to be run by the CLOCK task
16 * to, you guessed it, wake up the TTY to check if input or output can
17 * continue.
18 *
19 * The valid messages and their parameters are:
20 *
21 * HARD_INT: output has been completed or input has arrived
22 * SYS_SIG: e.g., MINIX wants to shutdown; run code to cleanly stop
23 * DEV_READ: a process wants to read from a terminal
24 * DEV_WRITE: a process wants to write on a terminal
25 * DEV_IOCTL: a process wants to change a terminal's parameters
26 * DEV_OPEN: a tty line has been opened
27 * DEV_CLOSE: a tty line has been closed
28 * DEV_SELECT: start select notification request
29 * DEV_STATUS: FS wants to know status for SELECT or REVIVE
30 * CANCEL: terminate a previous incomplete system call immediately
31 *
32 * m_type TTY_LINE IO_ENDPT COUNT TTY_SPEK TTY_FLAGS ADDRESS
33 * ---------------------------------------------------------------------------
34 * | HARD_INT | | | | | | |
35 * |-------------+---------+---------+---------+---------+---------+---------|
36 * | SYS_SIG | sig set | | | | | |
37 * |-------------+---------+---------+---------+---------+---------+---------|
38 * | DEV_READ |minor dev| proc nr | count | O_NONBLOCK| buf ptr |
39 * |-------------+---------+---------+---------+---------+---------+---------|
40 * | DEV_WRITE |minor dev| proc nr | count | | | buf ptr |
41 * |-------------+---------+---------+---------+---------+---------+---------|
42 * | DEV_IOCTL |minor dev| proc nr |func code|erase etc| flags | |
43 * |-------------+---------+---------+---------+---------+---------+---------|
44 * | DEV_OPEN |minor dev| proc nr | O_NOCTTY| | | |
45 * |-------------+---------+---------+---------+---------+---------+---------|
46 * | DEV_CLOSE |minor dev| proc nr | | | | |
47 * |-------------+---------+---------+---------+---------+---------+---------|
48 * | DEV_STATUS | | | | | | |
49 * |-------------+---------+---------+---------+---------+---------+---------|
50 * | CANCEL |minor dev| proc nr | | | | |
51 * ---------------------------------------------------------------------------
52 *
53 * Changes:
54 * Jan 20, 2004 moved TTY driver to user-space (Jorrit N. Herder)
55 * Sep 20, 2004 local timer management/ sync alarms (Jorrit N. Herder)
56 * Jul 13, 2004 support for function key observers (Jorrit N. Herder)
57 */
58
59#include "../drivers.h"
60#include "../drivers.h"
61#include <termios.h>
62#if ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT
63#include <sgtty.h>
64#endif
65#include <sys/ioc_tty.h>
66#include <signal.h>
67#include <minix/callnr.h>
68#if (CHIP == INTEL)
69#include <minix/keymap.h>
70#endif
71#include "tty.h"
72
73#include <sys/time.h>
74#include <sys/select.h>
75
76extern int irq_hook_id;
77
78unsigned long kbd_irq_set = 0;
79unsigned long rs_irq_set = 0;
80
81/* Address of a tty structure. */
82#define tty_addr(line) (&tty_table[line])
83
84/* Macros for magic tty types. */
85#define isconsole(tp) ((tp) < tty_addr(NR_CONS))
86#define ispty(tp) ((tp) >= tty_addr(NR_CONS+NR_RS_LINES))
87
88/* Macros for magic tty structure pointers. */
89#define FIRST_TTY tty_addr(0)
90#define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
91
92/* A device exists if at least its 'devread' function is defined. */
93#define tty_active(tp) ((tp)->tty_devread != NULL)
94
95/* RS232 lines or pseudo terminals can be completely configured out. */
96#if NR_RS_LINES == 0
97#define rs_init(tp) ((void) 0)
98#endif
99#if NR_PTYS == 0
100#define pty_init(tp) ((void) 0)
101#define do_pty(tp, mp) ((void) 0)
102#endif
103
104struct kmessages kmess;
105
106FORWARD _PROTOTYPE( void tty_timed_out, (timer_t *tp) );
107FORWARD _PROTOTYPE( void expire_timers, (void) );
108FORWARD _PROTOTYPE( void settimer, (tty_t *tty_ptr, int enable) );
109FORWARD _PROTOTYPE( void do_cancel, (tty_t *tp, message *m_ptr) );
110FORWARD _PROTOTYPE( void do_ioctl, (tty_t *tp, message *m_ptr) );
111FORWARD _PROTOTYPE( void do_open, (tty_t *tp, message *m_ptr) );
112FORWARD _PROTOTYPE( void do_close, (tty_t *tp, message *m_ptr) );
113FORWARD _PROTOTYPE( void do_read, (tty_t *tp, message *m_ptr) );
114FORWARD _PROTOTYPE( void do_write, (tty_t *tp, message *m_ptr) );
115FORWARD _PROTOTYPE( void do_select, (tty_t *tp, message *m_ptr) );
116FORWARD _PROTOTYPE( void do_status, (message *m_ptr) );
117FORWARD _PROTOTYPE( void in_transfer, (tty_t *tp) );
118FORWARD _PROTOTYPE( int tty_echo, (tty_t *tp, int ch) );
119FORWARD _PROTOTYPE( void rawecho, (tty_t *tp, int ch) );
120FORWARD _PROTOTYPE( int back_over, (tty_t *tp) );
121FORWARD _PROTOTYPE( void reprint, (tty_t *tp) );
122FORWARD _PROTOTYPE( void dev_ioctl, (tty_t *tp) );
123FORWARD _PROTOTYPE( void setattr, (tty_t *tp) );
124FORWARD _PROTOTYPE( void tty_icancel, (tty_t *tp) );
125FORWARD _PROTOTYPE( void tty_init, (void) );
126#if ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT
127FORWARD _PROTOTYPE( int compat_getp, (tty_t *tp, struct sgttyb *sg) );
128FORWARD _PROTOTYPE( int compat_getc, (tty_t *tp, struct tchars *sg) );
129FORWARD _PROTOTYPE( int compat_setp, (tty_t *tp, struct sgttyb *sg) );
130FORWARD _PROTOTYPE( int compat_setc, (tty_t *tp, struct tchars *sg) );
131FORWARD _PROTOTYPE( int tspd2sgspd, (speed_t tspd) );
132FORWARD _PROTOTYPE( speed_t sgspd2tspd, (int sgspd) );
133#if ENABLE_BINCOMPAT
134FORWARD _PROTOTYPE( void do_ioctl_compat, (tty_t *tp, message *m_ptr) );
135#endif
136#endif
137
138/* Default attributes. */
139PRIVATE struct termios termios_defaults = {
140 TINPUT_DEF, TOUTPUT_DEF, TCTRL_DEF, TLOCAL_DEF, TSPEED_DEF, TSPEED_DEF,
141 {
142 TEOF_DEF, TEOL_DEF, TERASE_DEF, TINTR_DEF, TKILL_DEF, TMIN_DEF,
143 TQUIT_DEF, TTIME_DEF, TSUSP_DEF, TSTART_DEF, TSTOP_DEF,
144 TREPRINT_DEF, TLNEXT_DEF, TDISCARD_DEF,
145 },
146};
147PRIVATE struct winsize winsize_defaults; /* = all zeroes */
148
149/* Global variables for the TTY task (declared extern in tty.h). */
150PUBLIC tty_t tty_table[NR_CONS+NR_RS_LINES+NR_PTYS];
151PUBLIC int ccurrent; /* currently active console */
152PUBLIC timer_t *tty_timers; /* queue of TTY timers */
153PUBLIC clock_t tty_next_timeout; /* time that the next alarm is due */
154PUBLIC struct machine machine; /* kernel environment variables */
155
156/*===========================================================================*
157 * tty_task *
158 *===========================================================================*/
159PUBLIC void main(void)
160{
161/* Main routine of the terminal task. */
162
163 message tty_mess; /* buffer for all incoming messages */
164 unsigned line;
165 int r, s;
166 register struct proc *rp;
167 register tty_t *tp;
168
169#if DEBUG
170 kputc('H');
171 kputc('e');
172 kputc('l');
173 kputc('l');
174 kputc('o');
175 kputc(',');
176 kputc(' ');
177 printf("TTY\n");
178#endif
179
180 /* Get kernel environment (protected_mode, pc_at and ega are needed). */
181 if (OK != (s=sys_getmachine(&machine))) {
182 panic("TTY","Couldn't obtain kernel environment.", s);
183 }
184
185 /* Initialize the TTY driver. */
186 tty_init();
187
188 /* Final one-time keyboard initialization. */
189 kb_init_once();
190
191 printf("\n");
192
193 while (TRUE) {
194
195 /* Check for and handle any events on any of the ttys. */
196 for (tp = FIRST_TTY; tp < END_TTY; tp++) {
197 if (tp->tty_events) handle_events(tp);
198 }
199
200 /* Get a request message. */
201 r= receive(ANY, &tty_mess);
202 if (r != 0)
203 panic("TTY", "receive failed with %d", r);
204
205 /* First handle all kernel notification types that the TTY supports.
206 * - An alarm went off, expire all timers and handle the events.
207 * - A hardware interrupt also is an invitation to check for events.
208 * - A new kernel message is available for printing.
209 * - Reset the console on system shutdown.
210 * Then see if this message is different from a normal device driver
211 * request and should be handled separately. These extra functions
212 * do not operate on a device, in constrast to the driver requests.
213 */
214 switch (tty_mess.m_type) {
215 case SYN_ALARM: /* fall through */
216 expire_timers(); /* run watchdogs of expired timers */
217 continue; /* contine to check for events */
218 case DEV_PING:
219 notify(tty_mess.m_source);
220 continue;
221 case HARD_INT: { /* hardware interrupt notification */
222 if (tty_mess.NOTIFY_ARG & kbd_irq_set)
223 kbd_interrupt(&tty_mess);/* fetch chars from keyboard */
224#if NR_RS_LINES > 0
225 if (tty_mess.NOTIFY_ARG & rs_irq_set)
226 rs_interrupt(&tty_mess);/* serial I/O */
227#endif
228 expire_timers(); /* run watchdogs of expired timers */
229 continue; /* contine to check for events */
230 }
231 case PROC_EVENT: {
232 cons_stop(); /* switch to primary console */
233 printf("TTY got PROC_EVENT, assuming SIGTERM\n");
234#if DEAD_CODE
235 if (irq_hook_id != -1) {
236 sys_irqdisable(&irq_hook_id);
237 sys_irqrmpolicy(KEYBOARD_IRQ, &irq_hook_id);
238 }
239#endif
240 continue;
241 }
242 case SYS_SIG: { /* system signal */
243 sigset_t sigset = (sigset_t) tty_mess.NOTIFY_ARG;
244 if (sigismember(&sigset, SIGKMESS)) do_new_kmess(&tty_mess);
245 continue;
246 }
247 case DIAGNOSTICS: /* a server wants to print some */
248 do_diagnostics(&tty_mess);
249 continue;
250 case GET_KMESS:
251 do_get_kmess(&tty_mess);
252 continue;
253 case FKEY_CONTROL: /* (un)register a fkey observer */
254 do_fkey_ctl(&tty_mess);
255 continue;
256 default: /* should be a driver request */
257 ; /* do nothing; end switch */
258 }
259
260 /* Only device requests should get to this point. All requests,
261 * except DEV_STATUS, have a minor device number. Check this
262 * exception and get the minor device number otherwise.
263 */
264 if (tty_mess.m_type == DEV_STATUS) {
265 do_status(&tty_mess);
266 continue;
267 }
268 line = tty_mess.TTY_LINE;
269 if (line == KBD_MINOR) {
270 do_kbd(&tty_mess);
271 continue;
272 } else if (line == KBDAUX_MINOR) {
273 do_kbdaux(&tty_mess);
274 continue;
275 } else if (line == VIDEO_MINOR) {
276 do_video(&tty_mess);
277 continue;
278 } else if ((line - CONS_MINOR) < NR_CONS) {
279 tp = tty_addr(line - CONS_MINOR);
280 } else if (line == LOG_MINOR) {
281 tp = tty_addr(0);
282 } else if ((line - RS232_MINOR) < NR_RS_LINES) {
283 tp = tty_addr(line - RS232_MINOR + NR_CONS);
284 } else if ((line - TTYPX_MINOR) < NR_PTYS) {
285 tp = tty_addr(line - TTYPX_MINOR + NR_CONS + NR_RS_LINES);
286 } else if ((line - PTYPX_MINOR) < NR_PTYS) {
287 tp = tty_addr(line - PTYPX_MINOR + NR_CONS + NR_RS_LINES);
288 if (tty_mess.m_type != DEV_IOCTL) {
289 do_pty(tp, &tty_mess);
290 continue;
291 }
292 } else {
293 tp = NULL;
294 }
295
296 /* If the device doesn't exist or is not configured return ENXIO. */
297 if (tp == NULL || ! tty_active(tp)) {
298 printf("Warning, TTY got illegal request %d from %d\n",
299 tty_mess.m_type, tty_mess.m_source);
300 if (tty_mess.m_source != LOG_PROC_NR)
301 {
302 tty_reply(TASK_REPLY, tty_mess.m_source,
303 tty_mess.IO_ENDPT, ENXIO);
304 }
305 continue;
306 }
307
308 /* Execute the requested device driver function. */
309 switch (tty_mess.m_type) {
310 case DEV_READ: do_read(tp, &tty_mess); break;
311 case DEV_WRITE: do_write(tp, &tty_mess); break;
312 case DEV_IOCTL: do_ioctl(tp, &tty_mess); break;
313 case DEV_OPEN: do_open(tp, &tty_mess); break;
314 case DEV_CLOSE: do_close(tp, &tty_mess); break;
315 case DEV_SELECT: do_select(tp, &tty_mess); break;
316 case CANCEL: do_cancel(tp, &tty_mess); break;
317 default:
318 printf("Warning, TTY got unexpected request %d from %d\n",
319 tty_mess.m_type, tty_mess.m_source);
320 tty_reply(TASK_REPLY, tty_mess.m_source,
321 tty_mess.IO_ENDPT, EINVAL);
322 }
323 }
324}
325
326/*===========================================================================*
327 * do_status *
328 *===========================================================================*/
329PRIVATE void do_status(m_ptr)
330message *m_ptr;
331{
332 register struct tty *tp;
333 int event_found;
334 int status;
335 int ops;
336
337 /* Check for select or revive events on any of the ttys. If we found an,
338 * event return a single status message for it. The FS will make another
339 * call to see if there is more.
340 */
341 event_found = 0;
342 for (tp = FIRST_TTY; tp < END_TTY; tp++) {
343 if ((ops = select_try(tp, tp->tty_select_ops)) &&
344 tp->tty_select_proc == m_ptr->m_source) {
345
346 /* I/O for a selected minor device is ready. */
347 m_ptr->m_type = DEV_IO_READY;
348 m_ptr->DEV_MINOR = tp->tty_minor;
349 m_ptr->DEV_SEL_OPS = ops;
350
351 tp->tty_select_ops &= ~ops; /* unmark select event */
352 event_found = 1;
353 break;
354 }
355 else if (tp->tty_inrevived && tp->tty_incaller == m_ptr->m_source) {
356
357 /* Suspended request finished. Send a REVIVE. */
358 m_ptr->m_type = DEV_REVIVE;
359 m_ptr->REP_ENDPT = tp->tty_inproc;
360 m_ptr->REP_STATUS = tp->tty_incum;
361
362 tp->tty_inleft = tp->tty_incum = 0;
363 tp->tty_inrevived = 0; /* unmark revive event */
364 event_found = 1;
365 break;
366 }
367 else if (tp->tty_outrevived && tp->tty_outcaller == m_ptr->m_source) {
368
369 /* Suspended request finished. Send a REVIVE. */
370 m_ptr->m_type = DEV_REVIVE;
371 m_ptr->REP_ENDPT = tp->tty_outproc;
372 m_ptr->REP_STATUS = tp->tty_outcum;
373
374 tp->tty_outcum = 0;
375 tp->tty_outrevived = 0; /* unmark revive event */
376 event_found = 1;
377 break;
378 }
379 }
380
381#if NR_PTYS > 0
382 if (!event_found)
383 event_found = pty_status(m_ptr);
384#endif
385 if (!event_found)
386 event_found= kbd_status(m_ptr);
387
388 if (! event_found) {
389 /* No events of interest were found. Return an empty message. */
390 m_ptr->m_type = DEV_NO_STATUS;
391 }
392
393 /* Almost done. Send back the reply message to the caller. */
394 if ((status = send(m_ptr->m_source, m_ptr)) != OK) {
395 panic("TTY","send in do_status failed, status\n", status);
396 }
397}
398
399/*===========================================================================*
400 * do_read *
401 *===========================================================================*/
402PRIVATE void do_read(tp, m_ptr)
403register tty_t *tp; /* pointer to tty struct */
404register message *m_ptr; /* pointer to message sent to the task */
405{
406/* A process wants to read from a terminal. */
407 int r, status;
408 phys_bytes phys_addr;
409 int more_verbose= (tp == tty_addr(NR_CONS));
410
411 /* Check if there is already a process hanging in a read, check if the
412 * parameters are correct, do I/O.
413 */
414 if (tp->tty_inleft > 0) {
415 if (more_verbose) printf("do_read: EIO\n");
416 r = EIO;
417 } else
418 if (m_ptr->COUNT <= 0) {
419 if (more_verbose) printf("do_read: EINVAL\n");
420 r = EINVAL;
421 } else
422 if (sys_umap(m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
423 &phys_addr) != OK) {
424 if (more_verbose) printf("do_read: EFAULT\n");
425 r = EFAULT;
426 } else {
427 /* Copy information from the message to the tty struct. */
428 tp->tty_inrepcode = TASK_REPLY;
429 tp->tty_incaller = m_ptr->m_source;
430 tp->tty_inproc = m_ptr->IO_ENDPT;
431 tp->tty_in_vir = (vir_bytes) m_ptr->ADDRESS;
432 tp->tty_inleft = m_ptr->COUNT;
433
434 if (!(tp->tty_termios.c_lflag & ICANON)
435 && tp->tty_termios.c_cc[VTIME] > 0) {
436 if (tp->tty_termios.c_cc[VMIN] == 0) {
437 /* MIN & TIME specify a read timer that finishes the
438 * read in TIME/10 seconds if no bytes are available.
439 */
440 settimer(tp, TRUE);
441 tp->tty_min = 1;
442 } else {
443 /* MIN & TIME specify an inter-byte timer that may
444 * have to be cancelled if there are no bytes yet.
445 */
446 if (tp->tty_eotct == 0) {
447 settimer(tp, FALSE);
448 tp->tty_min = tp->tty_termios.c_cc[VMIN];
449 }
450 }
451 }
452
453 /* Anything waiting in the input buffer? Clear it out... */
454 in_transfer(tp);
455 /* ...then go back for more. */
456 handle_events(tp);
457 if (tp->tty_inleft == 0) {
458 if (tp->tty_select_ops)
459 select_retry(tp);
460 return; /* already done */
461 }
462
463 /* There were no bytes in the input queue available, so either suspend
464 * the caller or break off the read if nonblocking.
465 */
466 if (m_ptr->TTY_FLAGS & O_NONBLOCK) {
467 r = EAGAIN; /* cancel the read */
468 tp->tty_inleft = tp->tty_incum = 0;
469 } else {
470 r = SUSPEND; /* suspend the caller */
471 tp->tty_inrepcode = REVIVE;
472 }
473 }
474 if (more_verbose) printf("do_read: replying %d\n", r);
475 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
476 if (tp->tty_select_ops)
477 select_retry(tp);
478}
479
480/*===========================================================================*
481 * do_write *
482 *===========================================================================*/
483PRIVATE void do_write(tp, m_ptr)
484register tty_t *tp;
485register message *m_ptr; /* pointer to message sent to the task */
486{
487/* A process wants to write on a terminal. */
488 int r;
489 phys_bytes phys_addr;
490
491 /* Check if there is already a process hanging in a write, check if the
492 * parameters are correct, do I/O.
493 */
494 if (tp->tty_outleft > 0) {
495 r = EIO;
496 } else
497 if (m_ptr->COUNT <= 0) {
498 r = EINVAL;
499 } else
500 if (sys_umap(m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
501 &phys_addr) != OK) {
502 r = EFAULT;
503 } else {
504 /* Copy message parameters to the tty structure. */
505 tp->tty_outrepcode = TASK_REPLY;
506 tp->tty_outcaller = m_ptr->m_source;
507 tp->tty_outproc = m_ptr->IO_ENDPT;
508 tp->tty_out_vir = (vir_bytes) m_ptr->ADDRESS;
509 tp->tty_outleft = m_ptr->COUNT;
510
511 /* Try to write. */
512 handle_events(tp);
513 if (tp->tty_outleft == 0)
514 return; /* already done */
515
516 /* None or not all the bytes could be written, so either suspend the
517 * caller or break off the write if nonblocking.
518 */
519 if (m_ptr->TTY_FLAGS & O_NONBLOCK) { /* cancel the write */
520 r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
521 tp->tty_outleft = tp->tty_outcum = 0;
522 } else {
523 r = SUSPEND; /* suspend the caller */
524 tp->tty_outrepcode = REVIVE;
525 }
526 }
527 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
528}
529
530/*===========================================================================*
531 * do_ioctl *
532 *===========================================================================*/
533PRIVATE void do_ioctl(tp, m_ptr)
534register tty_t *tp;
535message *m_ptr; /* pointer to message sent to task */
536{
537/* Perform an IOCTL on this terminal. Posix termios calls are handled
538 * by the IOCTL system call
539 */
540
541 int r;
542 union {
543 int i;
544#if ENABLE_SRCCOMPAT
545 struct sgttyb sg;
546 struct tchars tc;
547#endif
548 } param;
549 size_t size;
550
551 /* Size of the ioctl parameter. */
552 switch (m_ptr->TTY_REQUEST) {
553 case TCGETS: /* Posix tcgetattr function */
554 case TCSETS: /* Posix tcsetattr function, TCSANOW option */
555 case TCSETSW: /* Posix tcsetattr function, TCSADRAIN option */
556 case TCSETSF: /* Posix tcsetattr function, TCSAFLUSH option */
557 size = sizeof(struct termios);
558 break;
559
560 case TCSBRK: /* Posix tcsendbreak function */
561 case TCFLOW: /* Posix tcflow function */
562 case TCFLSH: /* Posix tcflush function */
563 case TIOCGPGRP: /* Posix tcgetpgrp function */
564 case TIOCSPGRP: /* Posix tcsetpgrp function */
565 size = sizeof(int);
566 break;
567
568 case TIOCGWINSZ: /* get window size (not Posix) */
569 case TIOCSWINSZ: /* set window size (not Posix) */
570 size = sizeof(struct winsize);
571 break;
572
573#if ENABLE_SRCCOMPAT
574 case TIOCGETP: /* BSD-style get terminal properties */
575 case TIOCSETP: /* BSD-style set terminal properties */
576 size = sizeof(struct sgttyb);
577 break;
578
579 case TIOCGETC: /* BSD-style get terminal special characters */
580 case TIOCSETC: /* BSD-style get terminal special characters */
581 size = sizeof(struct tchars);
582 break;
583#endif
584#if (MACHINE == IBM_PC)
585 case KIOCSMAP: /* load keymap (Minix extension) */
586 size = sizeof(keymap_t);
587 break;
588
589 case TIOCSFON: /* load font (Minix extension) */
590 size = sizeof(u8_t [8192]);
591 break;
592
593#endif
594 case TCDRAIN: /* Posix tcdrain function -- no parameter */
595 default: size = 0;
596 }
597
598 r = OK;
599 switch (m_ptr->TTY_REQUEST) {
600 case TCGETS:
601 /* Get the termios attributes. */
602 r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_termios,
603 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
604 (vir_bytes) size);
605 break;
606
607 case TCSETSW:
608 case TCSETSF:
609 case TCDRAIN:
610 if (tp->tty_outleft > 0) {
611 /* Wait for all ongoing output processing to finish. */
612 tp->tty_iocaller = m_ptr->m_source;
613 tp->tty_ioproc = m_ptr->IO_ENDPT;
614 tp->tty_ioreq = m_ptr->REQUEST;
615 tp->tty_iovir = (vir_bytes) m_ptr->ADDRESS;
616 r = SUSPEND;
617 break;
618 }
619 if (m_ptr->TTY_REQUEST == TCDRAIN) break;
620 if (m_ptr->TTY_REQUEST == TCSETSF) tty_icancel(tp);
621 /*FALL THROUGH*/
622 case TCSETS:
623 /* Set the termios attributes. */
624 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
625 SELF, D, (vir_bytes) &tp->tty_termios, (vir_bytes) size);
626 if (r != OK) break;
627 setattr(tp);
628 break;
629
630 case TCFLSH:
631 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
632 SELF, D, (vir_bytes) &param.i, (vir_bytes) size);
633 if (r != OK) break;
634 switch (param.i) {
635 case TCIFLUSH: tty_icancel(tp); break;
636 case TCOFLUSH: (*tp->tty_ocancel)(tp, 0); break;
637 case TCIOFLUSH: tty_icancel(tp); (*tp->tty_ocancel)(tp, 0); break;
638 default: r = EINVAL;
639 }
640 break;
641
642 case TCFLOW:
643 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
644 SELF, D, (vir_bytes) &param.i, (vir_bytes) size);
645 if (r != OK) break;
646 switch (param.i) {
647 case TCOOFF:
648 case TCOON:
649 tp->tty_inhibited = (param.i == TCOOFF);
650 tp->tty_events = 1;
651 break;
652 case TCIOFF:
653 (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTOP]);
654 break;
655 case TCION:
656 (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTART]);
657 break;
658 default:
659 r = EINVAL;
660 }
661 break;
662
663 case TCSBRK:
664 if (tp->tty_break != NULL) (*tp->tty_break)(tp,0);
665 break;
666
667 case TIOCGWINSZ:
668 r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_winsize,
669 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
670 (vir_bytes) size);
671 break;
672
673 case TIOCSWINSZ:
674 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
675 SELF, D, (vir_bytes) &tp->tty_winsize, (vir_bytes) size);
676 sigchar(tp, SIGWINCH);
677 break;
678
679#if ENABLE_SRCCOMPAT
680 case TIOCGETP:
681 compat_getp(tp, &param.sg);
682 r = sys_vircopy(SELF, D, (vir_bytes) &param.sg,
683 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
684 (vir_bytes) size);
685 break;
686
687 case TIOCSETP:
688 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
689 SELF, D, (vir_bytes) &param.sg, (vir_bytes) size);
690 if (r != OK) break;
691 compat_setp(tp, &param.sg);
692 break;
693
694 case TIOCGETC:
695 compat_getc(tp, &param.tc);
696 r = sys_vircopy(SELF, D, (vir_bytes) &param.tc,
697 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
698 (vir_bytes) size);
699 break;
700
701 case TIOCSETC:
702 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
703 SELF, D, (vir_bytes) &param.tc, (vir_bytes) size);
704 if (r != OK) break;
705 compat_setc(tp, &param.tc);
706 break;
707#endif
708
709#if (MACHINE == IBM_PC)
710 case KIOCSMAP:
711 /* Load a new keymap (only /dev/console). */
712 if (isconsole(tp)) r = kbd_loadmap(m_ptr);
713 break;
714
715 case TIOCSFON:
716 /* Load a font into an EGA or VGA card (hs@hck.hr) */
717 if (isconsole(tp)) r = con_loadfont(m_ptr);
718 break;
719#endif
720
721#if (MACHINE == ATARI)
722 case VDU_LOADFONT:
723 r = vdu_loadfont(m_ptr);
724 break;
725#endif
726
727/* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
728 * not defined.
729 */
730 case TIOCGPGRP:
731 case TIOCSPGRP:
732 default:
733#if ENABLE_BINCOMPAT
734 do_ioctl_compat(tp, m_ptr);
735 return;
736#else
737 r = ENOTTY;
738#endif
739 }
740
741 /* Send the reply. */
742 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
743}
744
745/*===========================================================================*
746 * do_open *
747 *===========================================================================*/
748PRIVATE void do_open(tp, m_ptr)
749register tty_t *tp;
750message *m_ptr; /* pointer to message sent to task */
751{
752/* A tty line has been opened. Make it the callers controlling tty if
753 * O_NOCTTY is *not* set and it is not the log device. 1 is returned if
754 * the tty is made the controlling tty, otherwise OK or an error code.
755 */
756 int r = OK;
757
758 if (m_ptr->TTY_LINE == LOG_MINOR) {
759 /* The log device is a write-only diagnostics device. */
760 if (m_ptr->COUNT & R_BIT) r = EACCES;
761 } else {
762 if (!(m_ptr->COUNT & O_NOCTTY)) {
763 tp->tty_pgrp = m_ptr->IO_ENDPT;
764 r = 1;
765 }
766 tp->tty_openct++;
767 }
768 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
769}
770
771/*===========================================================================*
772 * do_close *
773 *===========================================================================*/
774PRIVATE void do_close(tp, m_ptr)
775register tty_t *tp;
776message *m_ptr; /* pointer to message sent to task */
777{
778/* A tty line has been closed. Clean up the line if it is the last close. */
779
780 if (m_ptr->TTY_LINE != LOG_MINOR && --tp->tty_openct == 0) {
781 tp->tty_pgrp = 0;
782 tty_icancel(tp);
783 (*tp->tty_ocancel)(tp, 0);
784 (*tp->tty_close)(tp, 0);
785 tp->tty_termios = termios_defaults;
786 tp->tty_winsize = winsize_defaults;
787 setattr(tp);
788 }
789 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, OK);
790}
791
792/*===========================================================================*
793 * do_cancel *
794 *===========================================================================*/
795PRIVATE void do_cancel(tp, m_ptr)
796register tty_t *tp;
797message *m_ptr; /* pointer to message sent to task */
798{
799/* A signal has been sent to a process that is hanging trying to read or write.
800 * The pending read or write must be finished off immediately.
801 */
802
803 int proc_nr;
804 int mode;
805
806 /* Check the parameters carefully, to avoid cancelling twice. */
807 proc_nr = m_ptr->IO_ENDPT;
808 mode = m_ptr->COUNT;
809 if ((mode & R_BIT) && tp->tty_inleft != 0 && proc_nr == tp->tty_inproc) {
810 /* Process was reading when killed. Clean up input. */
811 tty_icancel(tp);
812 tp->tty_inleft = tp->tty_incum = 0;
813 }
814 if ((mode & W_BIT) && tp->tty_outleft != 0 && proc_nr == tp->tty_outproc) {
815 /* Process was writing when killed. Clean up output. */
816 (*tp->tty_ocancel)(tp, 0);
817 tp->tty_outleft = tp->tty_outcum = 0;
818 }
819 if (tp->tty_ioreq != 0 && proc_nr == tp->tty_ioproc) {
820 /* Process was waiting for output to drain. */
821 tp->tty_ioreq = 0;
822 }
823 tp->tty_events = 1;
824 tty_reply(TASK_REPLY, m_ptr->m_source, proc_nr, EINTR);
825}
826
827PUBLIC int select_try(struct tty *tp, int ops)
828{
829 int ready_ops = 0;
830
831 /* Special case. If line is hung up, no operations will block.
832 * (and it can be seen as an exceptional condition.)
833 */
834 if (tp->tty_termios.c_ospeed == B0) {
835 ready_ops |= ops;
836 }
837
838 if (ops & SEL_RD) {
839 /* will i/o not block on read? */
840 if (tp->tty_inleft > 0) {
841 ready_ops |= SEL_RD; /* EIO - no blocking */
842 } else if (tp->tty_incount > 0) {
843 /* Is a regular read possible? tty_incount
844 * says there is data. But a read will only succeed
845 * in canonical mode if a newline has been seen.
846 */
847 if (!(tp->tty_termios.c_lflag & ICANON) ||
848 tp->tty_eotct > 0) {
849 ready_ops |= SEL_RD;
850 }
851 }
852 }
853
854 if (ops & SEL_WR) {
855 if (tp->tty_outleft > 0) ready_ops |= SEL_WR;
856 else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= SEL_WR;
857 }
858 return ready_ops;
859}
860
861PUBLIC int select_retry(struct tty *tp)
862{
863 if (tp->tty_select_ops && select_try(tp, tp->tty_select_ops))
864 notify(tp->tty_select_proc);
865 return OK;
866}
867
868/*===========================================================================*
869 * handle_events *
870 *===========================================================================*/
871PUBLIC void handle_events(tp)
872tty_t *tp; /* TTY to check for events. */
873{
874/* Handle any events pending on a TTY. These events are usually device
875 * interrupts.
876 *
877 * Two kinds of events are prominent:
878 * - a character has been received from the console or an RS232 line.
879 * - an RS232 line has completed a write request (on behalf of a user).
880 * The interrupt handler may delay the interrupt message at its discretion
881 * to avoid swamping the TTY task. Messages may be overwritten when the
882 * lines are fast or when there are races between different lines, input
883 * and output, because MINIX only provides single buffering for interrupt
884 * messages (in proc.c). This is handled by explicitly checking each line
885 * for fresh input and completed output on each interrupt.
886 */
887 char *buf;
888 unsigned count;
889 int status;
890
891 do {
892 tp->tty_events = 0;
893
894 /* Read input and perform input processing. */
895 (*tp->tty_devread)(tp, 0);
896
897 /* Perform output processing and write output. */
898 (*tp->tty_devwrite)(tp, 0);
899
900 /* Ioctl waiting for some event? */
901 if (tp->tty_ioreq != 0) dev_ioctl(tp);
902 } while (tp->tty_events);
903
904 /* Transfer characters from the input queue to a waiting process. */
905 in_transfer(tp);
906
907 /* Reply if enough bytes are available. */
908 if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
909 if (tp->tty_inrepcode == REVIVE) {
910 notify(tp->tty_incaller);
911 tp->tty_inrevived = 1;
912 } else {
913 tty_reply(tp->tty_inrepcode, tp->tty_incaller,
914 tp->tty_inproc, tp->tty_incum);
915 tp->tty_inleft = tp->tty_incum = 0;
916 }
917 }
918 if (tp->tty_select_ops)
919 {
920 select_retry(tp);
921 }
922#if NR_PTYS > 0
923 if (ispty(tp))
924 select_retry_pty(tp);
925#endif
926}
927
928/*===========================================================================*
929 * in_transfer *
930 *===========================================================================*/
931PRIVATE void in_transfer(tp)
932register tty_t *tp; /* pointer to terminal to read from */
933{
934/* Transfer bytes from the input queue to a process reading from a terminal. */
935
936 int ch;
937 int count;
938 char buf[64], *bp;
939
940 /* Force read to succeed if the line is hung up, looks like EOF to reader. */
941 if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
942
943 /* Anything to do? */
944 if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
945
946 bp = buf;
947 while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
948 ch = *tp->tty_intail;
949
950 if (!(ch & IN_EOF)) {
951 /* One character to be delivered to the user. */
952 *bp = ch & IN_CHAR;
953 tp->tty_inleft--;
954 if (++bp == bufend(buf)) {
955 /* Temp buffer full, copy to user space. */
956 sys_vircopy(SELF, D, (vir_bytes) buf,
957 tp->tty_inproc, D, tp->tty_in_vir,
958 (vir_bytes) buflen(buf));
959 tp->tty_in_vir += buflen(buf);
960 tp->tty_incum += buflen(buf);
961 bp = buf;
962 }
963 }
964
965 /* Remove the character from the input queue. */
966 if (++tp->tty_intail == bufend(tp->tty_inbuf))
967 tp->tty_intail = tp->tty_inbuf;
968 tp->tty_incount--;
969 if (ch & IN_EOT) {
970 tp->tty_eotct--;
971 /* Don't read past a line break in canonical mode. */
972 if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
973 }
974 }
975
976 if (bp > buf) {
977 /* Leftover characters in the buffer. */
978 count = bp - buf;
979 sys_vircopy(SELF, D, (vir_bytes) buf,
980 tp->tty_inproc, D, tp->tty_in_vir, (vir_bytes) count);
981 tp->tty_in_vir += count;
982 tp->tty_incum += count;
983 }
984
985 /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
986 if (tp->tty_inleft == 0) {
987 if (tp->tty_inrepcode == REVIVE) {
988 notify(tp->tty_incaller);
989 tp->tty_inrevived = 1;
990 } else {
991 tty_reply(tp->tty_inrepcode, tp->tty_incaller,
992 tp->tty_inproc, tp->tty_incum);
993 tp->tty_inleft = tp->tty_incum = 0;
994 }
995 }
996}
997
998/*===========================================================================*
999 * in_process *
1000 *===========================================================================*/
1001PUBLIC int in_process(tp, buf, count)
1002register tty_t *tp; /* terminal on which character has arrived */
1003char *buf; /* buffer with input characters */
1004int count; /* number of input characters */
1005{
1006/* Characters have just been typed in. Process, save, and echo them. Return
1007 * the number of characters processed.
1008 */
1009
1010 int ch, sig, ct;
1011 int timeset = FALSE;
1012 static unsigned char csize_mask[] = { 0x1F, 0x3F, 0x7F, 0xFF };
1013
1014 for (ct = 0; ct < count; ct++) {
1015 /* Take one character. */
1016 ch = *buf++ & BYTE;
1017
1018 /* Strip to seven bits? */
1019 if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
1020
1021 /* Input extensions? */
1022 if (tp->tty_termios.c_lflag & IEXTEN) {
1023
1024 /* Previous character was a character escape? */
1025 if (tp->tty_escaped) {
1026 tp->tty_escaped = NOT_ESCAPED;
1027 ch |= IN_ESC; /* protect character */
1028 }
1029
1030 /* LNEXT (^V) to escape the next character? */
1031 if (ch == tp->tty_termios.c_cc[VLNEXT]) {
1032 tp->tty_escaped = ESCAPED;
1033 rawecho(tp, '^');
1034 rawecho(tp, '\b');
1035 continue; /* do not store the escape */
1036 }
1037
1038 /* REPRINT (^R) to reprint echoed characters? */
1039 if (ch == tp->tty_termios.c_cc[VREPRINT]) {
1040 reprint(tp);
1041 continue;
1042 }
1043 }
1044
1045 /* _POSIX_VDISABLE is a normal character value, so better escape it. */
1046 if (ch == _POSIX_VDISABLE) ch |= IN_ESC;
1047
1048 /* Map CR to LF, ignore CR, or map LF to CR. */
1049 if (ch == '\r') {
1050 if (tp->tty_termios.c_iflag & IGNCR) continue;
1051 if (tp->tty_termios.c_iflag & ICRNL) ch = '\n';
1052 } else
1053 if (ch == '\n') {
1054 if (tp->tty_termios.c_iflag & INLCR) ch = '\r';
1055 }
1056
1057 /* Canonical mode? */
1058 if (tp->tty_termios.c_lflag & ICANON) {
1059
1060 /* Erase processing (rub out of last character). */
1061 if (ch == tp->tty_termios.c_cc[VERASE]) {
1062 (void) back_over(tp);
1063 if (!(tp->tty_termios.c_lflag & ECHOE)) {
1064 (void) tty_echo(tp, ch);
1065 }
1066 continue;
1067 }
1068
1069 /* Kill processing (remove current line). */
1070 if (ch == tp->tty_termios.c_cc[VKILL]) {
1071 while (back_over(tp)) {}
1072 if (!(tp->tty_termios.c_lflag & ECHOE)) {
1073 (void) tty_echo(tp, ch);
1074 if (tp->tty_termios.c_lflag & ECHOK)
1075 rawecho(tp, '\n');
1076 }
1077 continue;
1078 }
1079
1080 /* EOF (^D) means end-of-file, an invisible "line break". */
1081 if (ch == tp->tty_termios.c_cc[VEOF]) ch |= IN_EOT | IN_EOF;
1082
1083 /* The line may be returned to the user after an LF. */
1084 if (ch == '\n') ch |= IN_EOT;
1085
1086 /* Same thing with EOL, whatever it may be. */
1087 if (ch == tp->tty_termios.c_cc[VEOL]) ch |= IN_EOT;
1088 }
1089
1090 /* Start/stop input control? */
1091 if (tp->tty_termios.c_iflag & IXON) {
1092
1093 /* Output stops on STOP (^S). */
1094 if (ch == tp->tty_termios.c_cc[VSTOP]) {
1095 tp->tty_inhibited = STOPPED;
1096 tp->tty_events = 1;
1097 continue;
1098 }
1099
1100 /* Output restarts on START (^Q) or any character if IXANY. */
1101 if (tp->tty_inhibited) {
1102 if (ch == tp->tty_termios.c_cc[VSTART]
1103 || (tp->tty_termios.c_iflag & IXANY)) {
1104 tp->tty_inhibited = RUNNING;
1105 tp->tty_events = 1;
1106 if (ch == tp->tty_termios.c_cc[VSTART])
1107 continue;
1108 }
1109 }
1110 }
1111
1112 if (tp->tty_termios.c_lflag & ISIG) {
1113 /* Check for INTR (^?) and QUIT (^\) characters. */
1114 if (ch == tp->tty_termios.c_cc[VINTR]
1115 || ch == tp->tty_termios.c_cc[VQUIT]) {
1116 sig = SIGINT;
1117 if (ch == tp->tty_termios.c_cc[VQUIT]) sig = SIGQUIT;
1118 sigchar(tp, sig);
1119 (void) tty_echo(tp, ch);
1120 continue;
1121 }
1122 }
1123
1124 /* Is there space in the input buffer? */
1125 if (tp->tty_incount == buflen(tp->tty_inbuf)) {
1126 /* No space; discard in canonical mode, keep in raw mode. */
1127 if (tp->tty_termios.c_lflag & ICANON) continue;
1128 break;
1129 }
1130
1131 if (!(tp->tty_termios.c_lflag & ICANON)) {
1132 /* In raw mode all characters are "line breaks". */
1133 ch |= IN_EOT;
1134
1135 /* Start an inter-byte timer? */
1136 if (!timeset && tp->tty_termios.c_cc[VMIN] > 0
1137 && tp->tty_termios.c_cc[VTIME] > 0) {
1138 settimer(tp, TRUE);
1139 timeset = TRUE;
1140 }
1141 }
1142
1143 /* Perform the intricate function of echoing. */
1144 if (tp->tty_termios.c_lflag & (ECHO|ECHONL)) ch = tty_echo(tp, ch);
1145
1146 /* Save the character in the input queue. */
1147 *tp->tty_inhead++ = ch;
1148 if (tp->tty_inhead == bufend(tp->tty_inbuf))
1149 tp->tty_inhead = tp->tty_inbuf;
1150 tp->tty_incount++;
1151 if (ch & IN_EOT) tp->tty_eotct++;
1152
1153 /* Try to finish input if the queue threatens to overflow. */
1154 if (tp->tty_incount == buflen(tp->tty_inbuf)) in_transfer(tp);
1155 }
1156 return ct;
1157}
1158
1159/*===========================================================================*
1160 * echo *
1161 *===========================================================================*/
1162PRIVATE int tty_echo(tp, ch)
1163register tty_t *tp; /* terminal on which to echo */
1164register int ch; /* pointer to character to echo */
1165{
1166/* Echo the character if echoing is on. Some control characters are echoed
1167 * with their normal effect, other control characters are echoed as "^X",
1168 * normal characters are echoed normally. EOF (^D) is echoed, but immediately
1169 * backspaced over. Return the character with the echoed length added to its
1170 * attributes.
1171 */
1172 int len, rp;
1173
1174 ch &= ~IN_LEN;
1175 if (!(tp->tty_termios.c_lflag & ECHO)) {
1176 if (ch == ('\n' | IN_EOT) && (tp->tty_termios.c_lflag
1177 & (ICANON|ECHONL)) == (ICANON|ECHONL))
1178 (*tp->tty_echo)(tp, '\n');
1179 return(ch);
1180 }
1181
1182 /* "Reprint" tells if the echo output has been messed up by other output. */
1183 rp = tp->tty_incount == 0 ? FALSE : tp->tty_reprint;
1184
1185 if ((ch & IN_CHAR) < ' ') {
1186 switch (ch & (IN_ESC|IN_EOF|IN_EOT|IN_CHAR)) {
1187 case '\t':
1188 len = 0;
1189 do {
1190 (*tp->tty_echo)(tp, ' ');
1191 len++;
1192 } while (len < TAB_SIZE && (tp->tty_position & TAB_MASK) != 0);
1193 break;
1194 case '\r' | IN_EOT:
1195 case '\n' | IN_EOT:
1196 (*tp->tty_echo)(tp, ch & IN_CHAR);
1197 len = 0;
1198 break;
1199 default:
1200 (*tp->tty_echo)(tp, '^');
1201 (*tp->tty_echo)(tp, '@' + (ch & IN_CHAR));
1202 len = 2;
1203 }
1204 } else
1205 if ((ch & IN_CHAR) == '\177') {
1206 /* A DEL prints as "^?". */
1207 (*tp->tty_echo)(tp, '^');
1208 (*tp->tty_echo)(tp, '?');
1209 len = 2;
1210 } else {
1211 (*tp->tty_echo)(tp, ch & IN_CHAR);
1212 len = 1;
1213 }
1214 if (ch & IN_EOF) while (len > 0) { (*tp->tty_echo)(tp, '\b'); len--; }
1215
1216 tp->tty_reprint = rp;
1217 return(ch | (len << IN_LSHIFT));
1218}
1219
1220/*===========================================================================*
1221 * rawecho *
1222 *===========================================================================*/
1223PRIVATE void rawecho(tp, ch)
1224register tty_t *tp;
1225int ch;
1226{
1227/* Echo without interpretation if ECHO is set. */
1228 int rp = tp->tty_reprint;
1229 if (tp->tty_termios.c_lflag & ECHO) (*tp->tty_echo)(tp, ch);
1230 tp->tty_reprint = rp;
1231}
1232
1233/*===========================================================================*
1234 * back_over *
1235 *===========================================================================*/
1236PRIVATE int back_over(tp)
1237register tty_t *tp;
1238{
1239/* Backspace to previous character on screen and erase it. */
1240 u16_t *head;
1241 int len;
1242
1243 if (tp->tty_incount == 0) return(0); /* queue empty */
1244 head = tp->tty_inhead;
1245 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
1246 if (*--head & IN_EOT) return(0); /* can't erase "line breaks" */
1247 if (tp->tty_reprint) reprint(tp); /* reprint if messed up */
1248 tp->tty_inhead = head;
1249 tp->tty_incount--;
1250 if (tp->tty_termios.c_lflag & ECHOE) {
1251 len = (*head & IN_LEN) >> IN_LSHIFT;
1252 while (len > 0) {
1253 rawecho(tp, '\b');
1254 rawecho(tp, ' ');
1255 rawecho(tp, '\b');
1256 len--;
1257 }
1258 }
1259 return(1); /* one character erased */
1260}
1261
1262/*===========================================================================*
1263 * reprint *
1264 *===========================================================================*/
1265PRIVATE void reprint(tp)
1266register tty_t *tp; /* pointer to tty struct */
1267{
1268/* Restore what has been echoed to screen before if the user input has been
1269 * messed up by output, or if REPRINT (^R) is typed.
1270 */
1271 int count;
1272 u16_t *head;
1273
1274 tp->tty_reprint = FALSE;
1275
1276 /* Find the last line break in the input. */
1277 head = tp->tty_inhead;
1278 count = tp->tty_incount;
1279 while (count > 0) {
1280 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
1281 if (head[-1] & IN_EOT) break;
1282 head--;
1283 count--;
1284 }
1285 if (count == tp->tty_incount) return; /* no reason to reprint */
1286
1287 /* Show REPRINT (^R) and move to a new line. */
1288 (void) tty_echo(tp, tp->tty_termios.c_cc[VREPRINT] | IN_ESC);
1289 rawecho(tp, '\r');
1290 rawecho(tp, '\n');
1291
1292 /* Reprint from the last break onwards. */
1293 do {
1294 if (head == bufend(tp->tty_inbuf)) head = tp->tty_inbuf;
1295 *head = tty_echo(tp, *head);
1296 head++;
1297 count++;
1298 } while (count < tp->tty_incount);
1299}
1300
1301/*===========================================================================*
1302 * out_process *
1303 *===========================================================================*/
1304PUBLIC void out_process(tp, bstart, bpos, bend, icount, ocount)
1305tty_t *tp;
1306char *bstart, *bpos, *bend; /* start/pos/end of circular buffer */
1307int *icount; /* # input chars / input chars used */
1308int *ocount; /* max output chars / output chars used */
1309{
1310/* Perform output processing on a circular buffer. *icount is the number of
1311 * bytes to process, and the number of bytes actually processed on return.
1312 * *ocount is the space available on input and the space used on output.
1313 * (Naturally *icount < *ocount.) The column position is updated modulo
1314 * the TAB size, because we really only need it for tabs.
1315 */
1316
1317 int tablen;
1318 int ict = *icount;
1319 int oct = *ocount;
1320 int pos = tp->tty_position;
1321
1322 while (ict > 0) {
1323 switch (*bpos) {
1324 case '\7':
1325 break;
1326 case '\b':
1327 pos--;
1328 break;
1329 case '\r':
1330 pos = 0;
1331 break;
1332 case '\n':
1333 if ((tp->tty_termios.c_oflag & (OPOST|ONLCR))
1334 == (OPOST|ONLCR)) {
1335 /* Map LF to CR+LF if there is space. Note that the
1336 * next character in the buffer is overwritten, so
1337 * we stop at this point.
1338 */
1339 if (oct >= 2) {
1340 *bpos = '\r';
1341 if (++bpos == bend) bpos = bstart;
1342 *bpos = '\n';
1343 pos = 0;
1344 ict--;
1345 oct -= 2;
1346 }
1347 goto out_done; /* no space or buffer got changed */
1348 }
1349 break;
1350 case '\t':
1351 /* Best guess for the tab length. */
1352 tablen = TAB_SIZE - (pos & TAB_MASK);
1353
1354 if ((tp->tty_termios.c_oflag & (OPOST|XTABS))
1355 == (OPOST|XTABS)) {
1356 /* Tabs must be expanded. */
1357 if (oct >= tablen) {
1358 pos += tablen;
1359 ict--;
1360 oct -= tablen;
1361 do {
1362 *bpos = ' ';
1363 if (++bpos == bend) bpos = bstart;
1364 } while (--tablen != 0);
1365 }
1366 goto out_done;
1367 }
1368 /* Tabs are output directly. */
1369 pos += tablen;
1370 break;
1371 default:
1372 /* Assume any other character prints as one character. */
1373 pos++;
1374 }
1375 if (++bpos == bend) bpos = bstart;
1376 ict--;
1377 oct--;
1378 }
1379out_done:
1380 tp->tty_position = pos & TAB_MASK;
1381
1382 *icount -= ict; /* [io]ct are the number of chars not used */
1383 *ocount -= oct; /* *[io]count are the number of chars that are used */
1384}
1385
1386/*===========================================================================*
1387 * dev_ioctl *
1388 *===========================================================================*/
1389PRIVATE void dev_ioctl(tp)
1390tty_t *tp;
1391{
1392/* The ioctl's TCSETSW, TCSETSF and TCDRAIN wait for output to finish to make
1393 * sure that an attribute change doesn't affect the processing of current
1394 * output. Once output finishes the ioctl is executed as in do_ioctl().
1395 */
1396 int result;
1397
1398 if (tp->tty_outleft > 0) return; /* output not finished */
1399
1400 if (tp->tty_ioreq != TCDRAIN) {
1401 if (tp->tty_ioreq == TCSETSF) tty_icancel(tp);
1402 result = sys_vircopy(tp->tty_ioproc, D, tp->tty_iovir,
1403 SELF, D, (vir_bytes) &tp->tty_termios,
1404 (vir_bytes) sizeof(tp->tty_termios));
1405 setattr(tp);
1406 }
1407 tp->tty_ioreq = 0;
1408 tty_reply(REVIVE, tp->tty_iocaller, tp->tty_ioproc, result);
1409}
1410
1411/*===========================================================================*
1412 * setattr *
1413 *===========================================================================*/
1414PRIVATE void setattr(tp)
1415tty_t *tp;
1416{
1417/* Apply the new line attributes (raw/canonical, line speed, etc.) */
1418 u16_t *inp;
1419 int count;
1420
1421 if (!(tp->tty_termios.c_lflag & ICANON)) {
1422 /* Raw mode; put a "line break" on all characters in the input queue.
1423 * It is undefined what happens to the input queue when ICANON is
1424 * switched off, a process should use TCSAFLUSH to flush the queue.
1425 * Keeping the queue to preserve typeahead is the Right Thing, however
1426 * when a process does use TCSANOW to switch to raw mode.
1427 */
1428 count = tp->tty_eotct = tp->tty_incount;
1429 inp = tp->tty_intail;
1430 while (count > 0) {
1431 *inp |= IN_EOT;
1432 if (++inp == bufend(tp->tty_inbuf)) inp = tp->tty_inbuf;
1433 --count;
1434 }
1435 }
1436
1437 /* Inspect MIN and TIME. */
1438 settimer(tp, FALSE);
1439 if (tp->tty_termios.c_lflag & ICANON) {
1440 /* No MIN & TIME in canonical mode. */
1441 tp->tty_min = 1;
1442 } else {
1443 /* In raw mode MIN is the number of chars wanted, and TIME how long
1444 * to wait for them. With interesting exceptions if either is zero.
1445 */
1446 tp->tty_min = tp->tty_termios.c_cc[VMIN];
1447 if (tp->tty_min == 0 && tp->tty_termios.c_cc[VTIME] > 0)
1448 tp->tty_min = 1;
1449 }
1450
1451 if (!(tp->tty_termios.c_iflag & IXON)) {
1452 /* No start/stop output control, so don't leave output inhibited. */
1453 tp->tty_inhibited = RUNNING;
1454 tp->tty_events = 1;
1455 }
1456
1457 /* Setting the output speed to zero hangs up the phone. */
1458 if (tp->tty_termios.c_ospeed == B0) sigchar(tp, SIGHUP);
1459
1460 /* Set new line speed, character size, etc at the device level. */
1461 (*tp->tty_ioctl)(tp, 0);
1462}
1463
1464/*===========================================================================*
1465 * tty_reply *
1466 *===========================================================================*/
1467PUBLIC void tty_reply(code, replyee, proc_nr, status)
1468int code; /* TASK_REPLY or REVIVE */
1469int replyee; /* destination address for the reply */
1470int proc_nr; /* to whom should the reply go? */
1471int status; /* reply code */
1472{
1473/* Send a reply to a process that wanted to read or write data. */
1474 message tty_mess;
1475
1476 tty_mess.m_type = code;
1477 tty_mess.REP_ENDPT = proc_nr;
1478 tty_mess.REP_STATUS = status;
1479
1480 if ((status = send(replyee, &tty_mess)) != OK) {
1481 printf("TTY: couldn't reply to %d\n", replyee);
1482 panic("TTY","tty_reply failed, status\n", status);
1483 }
1484}
1485
1486/*===========================================================================*
1487 * sigchar *
1488 *===========================================================================*/
1489PUBLIC void sigchar(tp, sig)
1490register tty_t *tp;
1491int sig; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
1492{
1493/* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
1494 * a tty close, "stty 0", or a real RS-232 hangup. MM will send the signal to
1495 * the process group (INT, QUIT), all processes (KILL), or the session leader
1496 * (HUP).
1497 */
1498 int status;
1499
1500 if (tp->tty_pgrp != 0)
1501 if (OK != (status = sys_kill(tp->tty_pgrp, sig)))
1502 panic("TTY","Error, call to sys_kill failed", status);
1503
1504 if (!(tp->tty_termios.c_lflag & NOFLSH)) {
1505 tp->tty_incount = tp->tty_eotct = 0; /* kill earlier input */
1506 tp->tty_intail = tp->tty_inhead;
1507 (*tp->tty_ocancel)(tp, 0); /* kill all output */
1508 tp->tty_inhibited = RUNNING;
1509 tp->tty_events = 1;
1510 }
1511}
1512
1513/*===========================================================================*
1514 * tty_icancel *
1515 *===========================================================================*/
1516PRIVATE void tty_icancel(tp)
1517register tty_t *tp;
1518{
1519/* Discard all pending input, tty buffer or device. */
1520
1521 tp->tty_incount = tp->tty_eotct = 0;
1522 tp->tty_intail = tp->tty_inhead;
1523 (*tp->tty_icancel)(tp, 0);
1524}
1525
1526/*===========================================================================*
1527 * tty_init *
1528 *===========================================================================*/
1529PRIVATE void tty_init()
1530{
1531/* Initialize tty structure and call device initialization routines. */
1532
1533 register tty_t *tp;
1534 int s;
1535 struct sigaction sa;
1536
1537 /* Initialize the terminal lines. */
1538 for (tp = FIRST_TTY,s=0; tp < END_TTY; tp++,s++) {
1539
1540 tp->tty_index = s;
1541
1542 tmr_inittimer(&tp->tty_tmr);
1543
1544 tp->tty_intail = tp->tty_inhead = tp->tty_inbuf;
1545 tp->tty_min = 1;
1546 tp->tty_termios = termios_defaults;
1547 tp->tty_icancel = tp->tty_ocancel = tp->tty_ioctl = tp->tty_close =
1548 tty_devnop;
1549 if (tp < tty_addr(NR_CONS)) {
1550 scr_init(tp);
1551
1552 /* Initialize the keyboard driver. */
1553 kb_init(tp);
1554
1555 tp->tty_minor = CONS_MINOR + s;
1556 } else
1557 if (tp < tty_addr(NR_CONS+NR_RS_LINES)) {
1558 rs_init(tp);
1559 tp->tty_minor = RS232_MINOR + s-NR_CONS;
1560 } else {
1561 pty_init(tp);
1562 tp->tty_minor = s - (NR_CONS+NR_RS_LINES) + TTYPX_MINOR;
1563 }
1564 }
1565
1566#if DEAD_CODE
1567 /* Install signal handlers. Ask PM to transform signal into message. */
1568 sa.sa_handler = SIG_MESS;
1569 sigemptyset(&sa.sa_mask);
1570 sa.sa_flags = 0;
1571 if (sigaction(SIGTERM,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1572 if (sigaction(SIGKMESS,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1573 if (sigaction(SIGKSTOP,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1574#endif
1575#if DEBUG
1576 printf("end of tty_init\n");
1577#endif
1578}
1579
1580/*===========================================================================*
1581 * tty_timed_out *
1582 *===========================================================================*/
1583PRIVATE void tty_timed_out(timer_t *tp)
1584{
1585/* This timer has expired. Set the events flag, to force processing. */
1586 tty_t *tty_ptr;
1587 tty_ptr = &tty_table[tmr_arg(tp)->ta_int];
1588 tty_ptr->tty_min = 0; /* force read to succeed */
1589 tty_ptr->tty_events = 1;
1590}
1591
1592/*===========================================================================*
1593 * expire_timers *
1594 *===========================================================================*/
1595PRIVATE void expire_timers(void)
1596{
1597/* A synchronous alarm message was received. Check if there are any expired
1598 * timers. Possibly set the event flag and reschedule another alarm.
1599 */
1600 clock_t now; /* current time */
1601 int s;
1602
1603 /* Get the current time to compare the timers against. */
1604 if ((s=getuptime(&now)) != OK)
1605 panic("TTY","Couldn't get uptime from clock.", s);
1606
1607 /* Scan the queue of timers for expired timers. This dispatch the watchdog
1608 * functions of expired timers. Possibly a new alarm call must be scheduled.
1609 */
1610 tmrs_exptimers(&tty_timers, now, NULL);
1611 if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
1612 else { /* set new sync alarm */
1613 tty_next_timeout = tty_timers->tmr_exp_time;
1614 if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
1615 panic("TTY","Couldn't set synchronous alarm.", s);
1616 }
1617}
1618
1619/*===========================================================================*
1620 * settimer *
1621 *===========================================================================*/
1622PRIVATE void settimer(tty_ptr, enable)
1623tty_t *tty_ptr; /* line to set or unset a timer on */
1624int enable; /* set timer if true, otherwise unset */
1625{
1626 clock_t now; /* current time */
1627 clock_t exp_time;
1628 int s;
1629
1630 /* Get the current time to calculate the timeout time. */
1631 if ((s=getuptime(&now)) != OK)
1632 panic("TTY","Couldn't get uptime from clock.", s);
1633 if (enable) {
1634 exp_time = now + tty_ptr->tty_termios.c_cc[VTIME] * (HZ/10);
1635 /* Set a new timer for enabling the TTY events flags. */
1636 tmrs_settimer(&tty_timers, &tty_ptr->tty_tmr,
1637 exp_time, tty_timed_out, NULL);
1638 } else {
1639 /* Remove the timer from the active and expired lists. */
1640 tmrs_clrtimer(&tty_timers, &tty_ptr->tty_tmr, NULL);
1641 }
1642
1643 /* Now check if a new alarm must be scheduled. This happens when the front
1644 * of the timers queue was disabled or reinserted at another position, or
1645 * when a new timer was added to the front.
1646 */
1647 if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
1648 else if (tty_timers->tmr_exp_time != tty_next_timeout) {
1649 tty_next_timeout = tty_timers->tmr_exp_time;
1650 if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
1651 panic("TTY","Couldn't set synchronous alarm.", s);
1652 }
1653}
1654
1655/*===========================================================================*
1656 * tty_devnop *
1657 *===========================================================================*/
1658PUBLIC int tty_devnop(tp, try)
1659tty_t *tp;
1660int try;
1661{
1662 /* Some functions need not be implemented at the device level. */
1663}
1664
1665/*===========================================================================*
1666 * do_select *
1667 *===========================================================================*/
1668PRIVATE void do_select(tp, m_ptr)
1669register tty_t *tp; /* pointer to tty struct */
1670register message *m_ptr; /* pointer to message sent to the task */
1671{
1672 int ops, ready_ops = 0, watch;
1673
1674 ops = m_ptr->IO_ENDPT & (SEL_RD|SEL_WR|SEL_ERR);
1675 watch = (m_ptr->IO_ENDPT & SEL_NOTIFY) ? 1 : 0;
1676
1677 ready_ops = select_try(tp, ops);
1678
1679 if (!ready_ops && ops && watch) {
1680 tp->tty_select_ops |= ops;
1681 tp->tty_select_proc = m_ptr->m_source;
1682 }
1683
1684 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, ready_ops);
1685
1686 return;
1687}
1688
1689#if ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT
1690/*===========================================================================*
1691 * compat_getp *
1692 *===========================================================================*/
1693PRIVATE int compat_getp(tp, sg)
1694tty_t *tp;
1695struct sgttyb *sg;
1696{
1697/* Translate an old TIOCGETP to the termios equivalent. */
1698 int flgs;
1699
1700 sg->sg_erase = tp->tty_termios.c_cc[VERASE];
1701 sg->sg_kill = tp->tty_termios.c_cc[VKILL];
1702 sg->sg_ospeed = tspd2sgspd(cfgetospeed(&tp->tty_termios));
1703 sg->sg_ispeed = tspd2sgspd(cfgetispeed(&tp->tty_termios));
1704
1705 flgs = 0;
1706
1707 /* XTABS - if OPOST and XTABS */
1708 if ((tp->tty_termios.c_oflag & (OPOST|XTABS)) == (OPOST|XTABS))
1709 flgs |= 0006000;
1710
1711 /* BITS5..BITS8 - map directly to CS5..CS8 */
1712 flgs |= (tp->tty_termios.c_cflag & CSIZE) << (8-2);
1713
1714 /* EVENP - if PARENB and not PARODD */
1715 if ((tp->tty_termios.c_cflag & (PARENB|PARODD)) == PARENB)
1716 flgs |= 0000200;
1717
1718 /* ODDP - if PARENB and PARODD */
1719 if ((tp->tty_termios.c_cflag & (PARENB|PARODD)) == (PARENB|PARODD))
1720 flgs |= 0000100;
1721
1722 /* RAW - if not ICANON and not ISIG */
1723 if (!(tp->tty_termios.c_lflag & (ICANON|ISIG)))
1724 flgs |= 0000040;
1725
1726 /* CRMOD - if ICRNL */
1727 if (tp->tty_termios.c_iflag & ICRNL)
1728 flgs |= 0000020;
1729
1730 /* ECHO - if ECHO */
1731 if (tp->tty_termios.c_lflag & ECHO)
1732 flgs |= 0000010;
1733
1734 /* CBREAK - if not ICANON and ISIG */
1735 if ((tp->tty_termios.c_lflag & (ICANON|ISIG)) == ISIG)
1736 flgs |= 0000002;
1737
1738 sg->sg_flags = flgs;
1739 return(OK);
1740}
1741
1742/*===========================================================================*
1743 * compat_getc *
1744 *===========================================================================*/
1745PRIVATE int compat_getc(tp, tc)
1746tty_t *tp;
1747struct tchars *tc;
1748{
1749/* Translate an old TIOCGETC to the termios equivalent. */
1750
1751 tc->t_intrc = tp->tty_termios.c_cc[VINTR];
1752 tc->t_quitc = tp->tty_termios.c_cc[VQUIT];
1753 tc->t_startc = tp->tty_termios.c_cc[VSTART];
1754 tc->t_stopc = tp->tty_termios.c_cc[VSTOP];
1755 tc->t_brkc = tp->tty_termios.c_cc[VEOL];
1756 tc->t_eofc = tp->tty_termios.c_cc[VEOF];
1757 return(OK);
1758}
1759
1760/*===========================================================================*
1761 * compat_setp *
1762 *===========================================================================*/
1763PRIVATE int compat_setp(tp, sg)
1764tty_t *tp;
1765struct sgttyb *sg;
1766{
1767/* Translate an old TIOCSETP to the termios equivalent. */
1768 struct termios termios;
1769 int flags;
1770
1771 termios = tp->tty_termios;
1772
1773 termios.c_cc[VERASE] = sg->sg_erase;
1774 termios.c_cc[VKILL] = sg->sg_kill;
1775 cfsetispeed(&termios, sgspd2tspd(sg->sg_ispeed & BYTE));
1776 cfsetospeed(&termios, sgspd2tspd(sg->sg_ospeed & BYTE));
1777 flags = sg->sg_flags;
1778
1779 /* Input flags */
1780
1781 /* BRKINT - not changed */
1782 /* ICRNL - set if CRMOD is set and not RAW */
1783 /* (CRMOD also controls output) */
1784 termios.c_iflag &= ~ICRNL;
1785 if ((flags & 0000020) && !(flags & 0000040))
1786 termios.c_iflag |= ICRNL;
1787
1788 /* IGNBRK - not changed */
1789 /* IGNCR - forced off (ignoring cr's is not supported) */
1790 termios.c_iflag &= ~IGNCR;
1791
1792 /* IGNPAR - not changed */
1793 /* INLCR - forced off (mapping nl's to cr's is not supported) */
1794 termios.c_iflag &= ~INLCR;
1795
1796 /* INPCK - not changed */
1797 /* ISTRIP - not changed */
1798 /* IXOFF - not changed */
1799 /* IXON - forced on if not RAW */
1800 termios.c_iflag &= ~IXON;
1801 if (!(flags & 0000040))
1802 termios.c_iflag |= IXON;
1803
1804 /* PARMRK - not changed */
1805
1806 /* Output flags */
1807
1808 /* OPOST - forced on if not RAW */
1809 termios.c_oflag &= ~OPOST;
1810 if (!(flags & 0000040))
1811 termios.c_oflag |= OPOST;
1812
1813 /* ONLCR - forced on if CRMOD */
1814 termios.c_oflag &= ~ONLCR;
1815 if (flags & 0000020)
1816 termios.c_oflag |= ONLCR;
1817
1818 /* XTABS - forced on if XTABS */
1819 termios.c_oflag &= ~XTABS;
1820 if (flags & 0006000)
1821 termios.c_oflag |= XTABS;
1822
1823 /* CLOCAL - not changed */
1824 /* CREAD - forced on (receiver is always enabled) */
1825 termios.c_cflag |= CREAD;
1826
1827 /* CSIZE - CS5-CS8 correspond directly to BITS5-BITS8 */
1828 termios.c_cflag = (termios.c_cflag & ~CSIZE) | ((flags & 0001400) >> (8-2));
1829
1830 /* CSTOPB - not changed */
1831 /* HUPCL - not changed */
1832 /* PARENB - set if EVENP or ODDP is set */
1833 termios.c_cflag &= ~PARENB;
1834 if (flags & (0000200|0000100))
1835 termios.c_cflag |= PARENB;
1836
1837 /* PARODD - set if ODDP is set */
1838 termios.c_cflag &= ~PARODD;
1839 if (flags & 0000100)
1840 termios.c_cflag |= PARODD;
1841
1842 /* Local flags */
1843
1844 /* ECHO - set if ECHO is set */
1845 termios.c_lflag &= ~ECHO;
1846 if (flags & 0000010)
1847 termios.c_lflag |= ECHO;
1848
1849 /* ECHOE - not changed */
1850 /* ECHOK - not changed */
1851 /* ECHONL - not changed */
1852 /* ICANON - set if neither CBREAK nor RAW */
1853 termios.c_lflag &= ~ICANON;
1854 if (!(flags & (0000002|0000040)))
1855 termios.c_lflag |= ICANON;
1856
1857 /* IEXTEN - set if not RAW */
1858 /* ISIG - set if not RAW */
1859 termios.c_lflag &= ~(IEXTEN|ISIG);
1860 if (!(flags & 0000040))
1861 termios.c_lflag |= (IEXTEN|ISIG);
1862
1863 /* NOFLSH - not changed */
1864 /* TOSTOP - not changed */
1865
1866 tp->tty_termios = termios;
1867 setattr(tp);
1868 return(OK);
1869}
1870
1871/*===========================================================================*
1872 * compat_setc *
1873 *===========================================================================*/
1874PRIVATE int compat_setc(tp, tc)
1875tty_t *tp;
1876struct tchars *tc;
1877{
1878/* Translate an old TIOCSETC to the termios equivalent. */
1879 struct termios termios;
1880
1881 termios = tp->tty_termios;
1882
1883 termios.c_cc[VINTR] = tc->t_intrc;
1884 termios.c_cc[VQUIT] = tc->t_quitc;
1885 termios.c_cc[VSTART] = tc->t_startc;
1886 termios.c_cc[VSTOP] = tc->t_stopc;
1887 termios.c_cc[VEOL] = tc->t_brkc;
1888 termios.c_cc[VEOF] = tc->t_eofc;
1889
1890 tp->tty_termios = termios;
1891 setattr(tp);
1892 return(OK);
1893}
1894
1895/* Table of termios line speed to sgtty line speed translations. All termios
1896 * speeds are present even if sgtty didn't know about them. (Now it does.)
1897 */
1898PRIVATE struct s2s {
1899 speed_t tspd;
1900 u8_t sgspd;
1901} ts2sgs[] = {
1902 { B0, 0 },
1903 { B50, 50 },
1904 { B75, 75 },
1905 { B110, 1 },
1906 { B134, 134 },
1907 { B200, 2 },
1908 { B300, 3 },
1909 { B600, 6 },
1910 { B1200, 12 },
1911 { B1800, 18 },
1912 { B2400, 24 },
1913 { B4800, 48 },
1914 { B9600, 96 },
1915 { B19200, 192 },
1916 { B38400, 195 },
1917 { B57600, 194 },
1918 { B115200, 193 },
1919};
1920
1921/*===========================================================================*
1922 * tspd2sgspd *
1923 *===========================================================================*/
1924PRIVATE int tspd2sgspd(tspd)
1925speed_t tspd;
1926{
1927/* Translate a termios speed to sgtty speed. */
1928 struct s2s *s;
1929
1930 for (s = ts2sgs; s < ts2sgs + sizeof(ts2sgs)/sizeof(ts2sgs[0]); s++) {
1931 if (s->tspd == tspd) return(s->sgspd);
1932 }
1933 return 96;
1934}
1935
1936/*===========================================================================*
1937 * sgspd2tspd *
1938 *===========================================================================*/
1939PRIVATE speed_t sgspd2tspd(sgspd)
1940int sgspd;
1941{
1942/* Translate a sgtty speed to termios speed. */
1943 struct s2s *s;
1944
1945 for (s = ts2sgs; s < ts2sgs + sizeof(ts2sgs)/sizeof(ts2sgs[0]); s++) {
1946 if (s->sgspd == sgspd) return(s->tspd);
1947 }
1948 return B9600;
1949}
1950
1951#if ENABLE_BINCOMPAT
1952/*===========================================================================*
1953 * do_ioctl_compat *
1954 *===========================================================================*/
1955PRIVATE void do_ioctl_compat(tp, m_ptr)
1956tty_t *tp;
1957message *m_ptr;
1958{
1959/* Handle the old sgtty ioctl's that packed the sgtty or tchars struct into
1960 * the Minix message. Efficient then, troublesome now.
1961 */
1962 int minor, proc, func, result, r;
1963 long flags, erki, spek;
1964 u8_t erase, kill, intr, quit, xon, xoff, brk, eof, ispeed, ospeed;
1965 struct sgttyb sg;
1966 struct tchars tc;
1967 message reply_mess;
1968
1969 minor = m_ptr->TTY_LINE;
1970 proc = m_ptr->IO_ENDPT;
1971 func = m_ptr->REQUEST;
1972 spek = m_ptr->m2_l1;
1973 flags = m_ptr->m2_l2;
1974
1975 switch(func)
1976 {
1977 case (('t'<<8) | 8): /* TIOCGETP */
1978 r = compat_getp(tp, &sg);
1979 erase = sg.sg_erase;
1980 kill = sg.sg_kill;
1981 ispeed = sg.sg_ispeed;
1982 ospeed = sg.sg_ospeed;
1983 flags = sg.sg_flags;
1984 erki = ((long)ospeed<<24) | ((long)ispeed<<16) | ((long)erase<<8) |kill;
1985 break;
1986 case (('t'<<8) | 18): /* TIOCGETC */
1987 r = compat_getc(tp, &tc);
1988 intr = tc.t_intrc;
1989 quit = tc.t_quitc;
1990 xon = tc.t_startc;
1991 xoff = tc.t_stopc;
1992 brk = tc.t_brkc;
1993 eof = tc.t_eofc;
1994 erki = ((long)intr<<24) | ((long)quit<<16) | ((long)xon<<8) | xoff;
1995 flags = (eof << 8) | brk;
1996 break;
1997 case (('t'<<8) | 17): /* TIOCSETC */
1998 tc.t_stopc = (spek >> 0) & 0xFF;
1999 tc.t_startc = (spek >> 8) & 0xFF;
2000 tc.t_quitc = (spek >> 16) & 0xFF;
2001 tc.t_intrc = (spek >> 24) & 0xFF;
2002 tc.t_brkc = (flags >> 0) & 0xFF;
2003 tc.t_eofc = (flags >> 8) & 0xFF;
2004 r = compat_setc(tp, &tc);
2005 break;
2006 case (('t'<<8) | 9): /* TIOCSETP */
2007 sg.sg_erase = (spek >> 8) & 0xFF;
2008 sg.sg_kill = (spek >> 0) & 0xFF;
2009 sg.sg_ispeed = (spek >> 16) & 0xFF;
2010 sg.sg_ospeed = (spek >> 24) & 0xFF;
2011 sg.sg_flags = flags;
2012 r = compat_setp(tp, &sg);
2013 break;
2014 default:
2015 r = ENOTTY;
2016 }
2017 reply_mess.m_type = TASK_REPLY;
2018 reply_mess.REP_ENDPT = m_ptr->IO_ENDPT;
2019 reply_mess.REP_STATUS = r;
2020 reply_mess.m2_l1 = erki;
2021 reply_mess.m2_l2 = flags;
2022 send(m_ptr->m_source, &reply_mess);
2023}
2024#endif /* ENABLE_BINCOMPAT */
2025#endif /* ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT */
2026
Note: See TracBrowser for help on using the repository browser.