source: branches/minix3-book/drivers/tty/tty.c051013@ 4

Last change on this file since 4 was 4, checked in by Mattia Monga, 14 years ago

Importazione sorgenti libro

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