Typo's.
[minix3.git] / drivers / tty / tty.c
blob07532c4b1bf781e2dabbc4d3fe6a73c43466bce7
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.
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.
19 * The valid messages and their parameters are:
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
32 * m_type TTY_LINE IO_ENDPT COUNT TTY_SPEKS ADDRESS
33 * -----------------------------------------------------------------
34 * | HARD_INT | | | | | |
35 * |-------------+---------+---------+---------+---------+---------|
36 * | SYS_SIG | sig set | | | | |
37 * |-------------+---------+---------+---------+---------+---------|
38 * | DEV_READ |minor dev| proc nr | count | | 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| |
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 * -----------------------------------------------------------------
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)
59 #include "../drivers.h"
60 #include <termios.h>
61 #include <sys/ioc_tty.h>
62 #include <signal.h>
63 #include <minix/callnr.h>
64 #if (CHIP == INTEL)
65 #include <minix/keymap.h>
66 #endif
67 #include "tty.h"
69 #include <sys/time.h>
70 #include <sys/select.h>
72 extern int irq_hook_id;
74 unsigned long kbd_irq_set = 0;
75 unsigned long rs_irq_set = 0;
77 /* Address of a tty structure. */
78 #define tty_addr(line) (&tty_table[line])
80 /* Macros for magic tty types. */
81 #define isconsole(tp) ((tp) < tty_addr(NR_CONS))
82 #define ispty(tp) ((tp) >= tty_addr(NR_CONS+NR_RS_LINES))
84 /* Macros for magic tty structure pointers. */
85 #define FIRST_TTY tty_addr(0)
86 #define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
88 /* A device exists if at least its 'devread' function is defined. */
89 #define tty_active(tp) ((tp)->tty_devread != NULL)
91 /* RS232 lines or pseudo terminals can be completely configured out. */
92 #if NR_RS_LINES == 0
93 #define rs_init(tp) ((void) 0)
94 #endif
95 #if NR_PTYS == 0
96 #define pty_init(tp) ((void) 0)
97 #define do_pty(tp, mp) ((void) 0)
98 #endif
100 struct kmessages kmess;
102 FORWARD _PROTOTYPE( void tty_timed_out, (timer_t *tp) );
103 FORWARD _PROTOTYPE( void expire_timers, (void) );
104 FORWARD _PROTOTYPE( void settimer, (tty_t *tty_ptr, int enable) );
105 FORWARD _PROTOTYPE( void do_cancel, (tty_t *tp, message *m_ptr) );
106 FORWARD _PROTOTYPE( void do_ioctl, (tty_t *tp, message *m_ptr, int s) );
107 FORWARD _PROTOTYPE( void do_open, (tty_t *tp, message *m_ptr) );
108 FORWARD _PROTOTYPE( void do_close, (tty_t *tp, message *m_ptr) );
109 FORWARD _PROTOTYPE( void do_read, (tty_t *tp, message *m_ptr, int s) );
110 FORWARD _PROTOTYPE( void do_write, (tty_t *tp, message *m_ptr, int s) );
111 FORWARD _PROTOTYPE( void do_select, (tty_t *tp, message *m_ptr) );
112 FORWARD _PROTOTYPE( void do_status, (message *m_ptr) );
113 FORWARD _PROTOTYPE( void in_transfer, (tty_t *tp) );
114 FORWARD _PROTOTYPE( int tty_echo, (tty_t *tp, int ch) );
115 FORWARD _PROTOTYPE( void rawecho, (tty_t *tp, int ch) );
116 FORWARD _PROTOTYPE( int back_over, (tty_t *tp) );
117 FORWARD _PROTOTYPE( void reprint, (tty_t *tp) );
118 FORWARD _PROTOTYPE( void dev_ioctl, (tty_t *tp) );
119 FORWARD _PROTOTYPE( void setattr, (tty_t *tp) );
120 FORWARD _PROTOTYPE( void tty_icancel, (tty_t *tp) );
121 FORWARD _PROTOTYPE( void tty_init, (void) );
123 /* Default attributes. */
124 PRIVATE struct termios termios_defaults = {
125 TINPUT_DEF, TOUTPUT_DEF, TCTRL_DEF, TLOCAL_DEF, TSPEED_DEF, TSPEED_DEF,
127 TEOF_DEF, TEOL_DEF, TERASE_DEF, TINTR_DEF, TKILL_DEF, TMIN_DEF,
128 TQUIT_DEF, TTIME_DEF, TSUSP_DEF, TSTART_DEF, TSTOP_DEF,
129 TREPRINT_DEF, TLNEXT_DEF, TDISCARD_DEF,
132 PRIVATE struct winsize winsize_defaults; /* = all zeroes */
134 /* Global variables for the TTY task (declared extern in tty.h). */
135 PUBLIC tty_t tty_table[NR_CONS+NR_RS_LINES+NR_PTYS];
136 PUBLIC int ccurrent; /* currently active console */
137 PUBLIC timer_t *tty_timers; /* queue of TTY timers */
138 PUBLIC clock_t tty_next_timeout; /* time that the next alarm is due */
139 PUBLIC struct machine machine; /* kernel environment variables */
141 /*===========================================================================*
142 * tty_task *
143 *===========================================================================*/
144 PUBLIC void main(void)
146 /* Main routine of the terminal task. */
148 message tty_mess; /* buffer for all incoming messages */
149 unsigned line;
150 int r, s;
151 register struct proc *rp;
152 register tty_t *tp;
154 /* Get kernel environment (protected_mode, pc_at and ega are needed). */
155 if (OK != (s=sys_getmachine(&machine))) {
156 panic("TTY","Couldn't obtain kernel environment.", s);
159 /* Initialize the TTY driver. */
160 tty_init();
162 /* Final one-time keyboard initialization. */
163 kb_init_once();
165 printf("\n");
167 while (TRUE) {
169 /* Check for and handle any events on any of the ttys. */
170 for (tp = FIRST_TTY; tp < END_TTY; tp++) {
171 if (tp->tty_events) handle_events(tp);
174 /* Get a request message. */
175 r= receive(ANY, &tty_mess);
176 if (r != 0)
177 panic("TTY", "receive failed with %d", r);
179 /* First handle all kernel notification types that the TTY supports.
180 * - An alarm went off, expire all timers and handle the events.
181 * - A hardware interrupt also is an invitation to check for events.
182 * - A new kernel message is available for printing.
183 * - Reset the console on system shutdown.
184 * Then see if this message is different from a normal device driver
185 * request and should be handled separately. These extra functions
186 * do not operate on a device, in constrast to the driver requests.
188 switch (tty_mess.m_type) {
189 case SYN_ALARM: /* fall through */
190 expire_timers(); /* run watchdogs of expired timers */
191 continue; /* contine to check for events */
192 case DEV_PING:
193 notify(tty_mess.m_source);
194 continue;
195 case HARD_INT: { /* hardware interrupt notification */
196 if (tty_mess.NOTIFY_ARG & kbd_irq_set)
197 kbd_interrupt(&tty_mess);/* fetch chars from keyboard */
198 #if NR_RS_LINES > 0
199 if (tty_mess.NOTIFY_ARG & rs_irq_set)
200 rs_interrupt(&tty_mess);/* serial I/O */
201 #endif
202 expire_timers(); /* run watchdogs of expired timers */
203 continue; /* contine to check for events */
205 case PROC_EVENT: {
206 cons_stop(); /* switch to primary console */
207 continue;
209 case SYS_SIG: { /* system signal */
210 sigset_t sigset = (sigset_t) tty_mess.NOTIFY_ARG;
211 if (sigismember(&sigset, SIGKMESS)) do_new_kmess(&tty_mess);
212 continue;
214 case DIAGNOSTICS: /* a server wants to print some */
215 printf("WARNING: old DIAGNOSTICS from %d\n", tty_mess.m_source);
216 do_diagnostics(&tty_mess, 0);
217 continue;
218 case DIAGNOSTICS_S:
219 do_diagnostics(&tty_mess, 1);
220 continue;
221 case GET_KMESS:
222 do_get_kmess(&tty_mess);
223 continue;
224 case GET_KMESS_S:
225 do_get_kmess_s(&tty_mess);
226 continue;
227 case FKEY_CONTROL: /* (un)register a fkey observer */
228 do_fkey_ctl(&tty_mess);
229 continue;
230 default: /* should be a driver request */
231 ; /* do nothing; end switch */
234 /* Only device requests should get to this point. All requests,
235 * except DEV_STATUS, have a minor device number. Check this
236 * exception and get the minor device number otherwise.
238 if (tty_mess.m_type == DEV_STATUS) {
239 do_status(&tty_mess);
240 continue;
242 line = tty_mess.TTY_LINE;
243 if (line == KBD_MINOR) {
244 do_kbd(&tty_mess);
245 continue;
246 } else if (line == KBDAUX_MINOR) {
247 do_kbdaux(&tty_mess);
248 continue;
249 } else if (line == VIDEO_MINOR) {
250 do_video(&tty_mess);
251 continue;
252 } else if ((line - CONS_MINOR) < NR_CONS) {
253 tp = tty_addr(line - CONS_MINOR);
254 } else if (line == LOG_MINOR) {
255 tp = tty_addr(0);
256 } else if ((line - RS232_MINOR) < NR_RS_LINES) {
257 tp = tty_addr(line - RS232_MINOR + NR_CONS);
258 } else if ((line - TTYPX_MINOR) < NR_PTYS) {
259 tp = tty_addr(line - TTYPX_MINOR + NR_CONS + NR_RS_LINES);
260 } else if ((line - PTYPX_MINOR) < NR_PTYS) {
261 tp = tty_addr(line - PTYPX_MINOR + NR_CONS + NR_RS_LINES);
262 if (tty_mess.m_type != DEV_IOCTL_S) {
263 do_pty(tp, &tty_mess);
264 continue;
266 } else {
267 tp = NULL;
270 /* If the device doesn't exist or is not configured return ENXIO. */
271 if (tp == NULL || ! tty_active(tp)) {
272 printf("Warning, TTY got illegal request %d from %d\n",
273 tty_mess.m_type, tty_mess.m_source);
274 if (tty_mess.m_source != LOG_PROC_NR)
276 tty_reply(TASK_REPLY, tty_mess.m_source,
277 tty_mess.IO_ENDPT, ENXIO);
279 continue;
282 /* Execute the requested device driver function. */
283 switch (tty_mess.m_type) {
284 case DEV_READ_S: do_read(tp, &tty_mess, 1); break;
285 case DEV_WRITE_S: do_write(tp, &tty_mess, 1); break;
286 case DEV_IOCTL_S: do_ioctl(tp, &tty_mess, 1); break;
287 case DEV_OPEN: do_open(tp, &tty_mess); break;
288 case DEV_CLOSE: do_close(tp, &tty_mess); break;
289 case DEV_SELECT: do_select(tp, &tty_mess); break;
290 case CANCEL: do_cancel(tp, &tty_mess); break;
291 default:
292 printf("Warning, TTY got unexpected request %d from %d\n",
293 tty_mess.m_type, tty_mess.m_source);
294 tty_reply(TASK_REPLY, tty_mess.m_source,
295 tty_mess.IO_ENDPT, EINVAL);
300 /*===========================================================================*
301 * do_status *
302 *===========================================================================*/
303 PRIVATE void do_status(m_ptr)
304 message *m_ptr;
306 register struct tty *tp;
307 int event_found;
308 int status;
309 int ops;
311 /* Check for select or revive events on any of the ttys. If we found an,
312 * event return a single status message for it. The FS will make another
313 * call to see if there is more.
315 event_found = 0;
316 for (tp = FIRST_TTY; tp < END_TTY; tp++) {
317 if ((ops = select_try(tp, tp->tty_select_ops)) &&
318 tp->tty_select_proc == m_ptr->m_source) {
320 /* I/O for a selected minor device is ready. */
321 m_ptr->m_type = DEV_IO_READY;
322 m_ptr->DEV_MINOR = tp->tty_minor;
323 m_ptr->DEV_SEL_OPS = ops;
325 tp->tty_select_ops &= ~ops; /* unmark select event */
326 event_found = 1;
327 break;
329 else if (tp->tty_inrevived && tp->tty_incaller == m_ptr->m_source) {
331 /* Suspended request finished. Send a REVIVE. */
332 m_ptr->m_type = DEV_REVIVE;
333 m_ptr->REP_ENDPT = tp->tty_inproc;
334 m_ptr->REP_IO_GRANT = tp->tty_in_vir_g;
335 m_ptr->REP_STATUS = tp->tty_incum;
337 tp->tty_inleft = tp->tty_incum = 0;
338 tp->tty_inrevived = 0; /* unmark revive event */
339 event_found = 1;
340 break;
342 else if (tp->tty_outrevived && tp->tty_outcaller == m_ptr->m_source) {
344 /* Suspended request finished. Send a REVIVE. */
345 m_ptr->m_type = DEV_REVIVE;
346 m_ptr->REP_ENDPT = tp->tty_outproc;
347 m_ptr->REP_IO_GRANT = tp->tty_out_vir_g;
348 m_ptr->REP_STATUS = tp->tty_outcum;
350 tp->tty_outcum = 0;
351 tp->tty_outrevived = 0; /* unmark revive event */
352 event_found = 1;
353 break;
355 else if (tp->tty_iorevived && tp->tty_iocaller == m_ptr->m_source) {
356 /* Suspended request finished. Send a REVIVE. */
357 m_ptr->m_type = DEV_REVIVE;
358 m_ptr->REP_ENDPT = tp->tty_ioproc;
359 m_ptr->REP_IO_GRANT = tp->tty_iovir_g;
360 m_ptr->REP_STATUS = tp->tty_iostatus;
361 tp->tty_iorevived = 0; /* unmark revive event */
362 event_found = 1;
363 break;
367 #if NR_PTYS > 0
368 if (!event_found)
369 event_found = pty_status(m_ptr);
370 #endif
371 if (!event_found)
372 event_found= kbd_status(m_ptr);
374 if (! event_found) {
375 /* No events of interest were found. Return an empty message. */
376 m_ptr->m_type = DEV_NO_STATUS;
379 /* Almost done. Send back the reply message to the caller. */
380 if ((status = send(m_ptr->m_source, m_ptr)) != OK) {
381 panic("TTY","send in do_status failed, status\n", status);
385 /*===========================================================================*
386 * do_read *
387 *===========================================================================*/
388 PRIVATE void do_read(tp, m_ptr, safe)
389 register tty_t *tp; /* pointer to tty struct */
390 register message *m_ptr; /* pointer to message sent to the task */
391 int safe; /* use safecopies? */
393 /* A process wants to read from a terminal. */
394 int r, status;
396 /* Check if there is already a process hanging in a read, check if the
397 * parameters are correct, do I/O.
399 if (tp->tty_inleft > 0) {
400 r = EIO;
401 } else
402 if (m_ptr->COUNT <= 0) {
403 r = EINVAL;
404 } else {
405 /* Copy information from the message to the tty struct. */
406 tp->tty_inrepcode = TASK_REPLY;
407 tp->tty_incaller = m_ptr->m_source;
408 tp->tty_inproc = m_ptr->IO_ENDPT;
409 tp->tty_in_vir_g = (vir_bytes) m_ptr->ADDRESS;
410 tp->tty_in_vir_offset = 0;
411 tp->tty_in_safe = safe;
412 tp->tty_inleft = m_ptr->COUNT;
414 if (!(tp->tty_termios.c_lflag & ICANON)
415 && tp->tty_termios.c_cc[VTIME] > 0) {
416 if (tp->tty_termios.c_cc[VMIN] == 0) {
417 /* MIN & TIME specify a read timer that finishes the
418 * read in TIME/10 seconds if no bytes are available.
420 settimer(tp, TRUE);
421 tp->tty_min = 1;
422 } else {
423 /* MIN & TIME specify an inter-byte timer that may
424 * have to be cancelled if there are no bytes yet.
426 if (tp->tty_eotct == 0) {
427 settimer(tp, FALSE);
428 tp->tty_min = tp->tty_termios.c_cc[VMIN];
433 /* Anything waiting in the input buffer? Clear it out... */
434 in_transfer(tp);
435 /* ...then go back for more. */
436 handle_events(tp);
437 if (tp->tty_inleft == 0) {
438 if (tp->tty_select_ops)
439 select_retry(tp);
440 return; /* already done */
443 /* There were no bytes in the input queue available, so suspend
444 * the caller.
446 r = SUSPEND; /* suspend the caller */
447 tp->tty_inrepcode = TTY_REVIVE;
449 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
450 if (tp->tty_select_ops)
451 select_retry(tp);
454 /*===========================================================================*
455 * do_write *
456 *===========================================================================*/
457 PRIVATE void do_write(tp, m_ptr, safe)
458 register tty_t *tp;
459 register message *m_ptr; /* pointer to message sent to the task */
460 int safe;
462 /* A process wants to write on a terminal. */
463 int r;
465 /* Check if there is already a process hanging in a write, check if the
466 * parameters are correct, do I/O.
468 if (tp->tty_outleft > 0) {
469 r = EIO;
470 } else
471 if (m_ptr->COUNT <= 0) {
472 r = EINVAL;
473 } else {
474 /* Copy message parameters to the tty structure. */
475 tp->tty_outrepcode = TASK_REPLY;
476 tp->tty_outcaller = m_ptr->m_source;
477 tp->tty_outproc = m_ptr->IO_ENDPT;
478 tp->tty_out_vir_g = (vir_bytes) m_ptr->ADDRESS;
479 tp->tty_out_vir_offset = 0;
480 tp->tty_out_safe = safe;
481 tp->tty_outleft = m_ptr->COUNT;
483 /* Try to write. */
484 handle_events(tp);
485 if (tp->tty_outleft == 0)
486 return; /* already done */
488 /* None or not all the bytes could be written, so suspend the
489 * caller.
491 r = SUSPEND; /* suspend the caller */
492 tp->tty_outrepcode = TTY_REVIVE;
494 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
497 /*===========================================================================*
498 * do_ioctl *
499 *===========================================================================*/
500 PRIVATE void do_ioctl(tp, m_ptr, safe)
501 register tty_t *tp;
502 message *m_ptr; /* pointer to message sent to task */
503 int safe;
505 /* Perform an IOCTL on this terminal. Posix termios calls are handled
506 * by the IOCTL system call
509 int r;
510 union {
511 int i;
512 } param;
513 size_t size;
515 /* Size of the ioctl parameter. */
516 switch (m_ptr->TTY_REQUEST) {
517 case TCGETS: /* Posix tcgetattr function */
518 case TCSETS: /* Posix tcsetattr function, TCSANOW option */
519 case TCSETSW: /* Posix tcsetattr function, TCSADRAIN option */
520 case TCSETSF: /* Posix tcsetattr function, TCSAFLUSH option */
521 size = sizeof(struct termios);
522 break;
524 case TCSBRK: /* Posix tcsendbreak function */
525 case TCFLOW: /* Posix tcflow function */
526 case TCFLSH: /* Posix tcflush function */
527 case TIOCGPGRP: /* Posix tcgetpgrp function */
528 case TIOCSPGRP: /* Posix tcsetpgrp function */
529 size = sizeof(int);
530 break;
532 case TIOCGWINSZ: /* get window size (not Posix) */
533 case TIOCSWINSZ: /* set window size (not Posix) */
534 size = sizeof(struct winsize);
535 break;
537 #if (MACHINE == IBM_PC)
538 case KIOCSMAP: /* load keymap (Minix extension) */
539 size = sizeof(keymap_t);
540 break;
542 case TIOCSFON: /* load font (Minix extension) */
543 size = sizeof(u8_t [8192]);
544 break;
546 #endif
547 case TCDRAIN: /* Posix tcdrain function -- no parameter */
548 default: size = 0;
551 r = OK;
552 switch (m_ptr->TTY_REQUEST) {
553 case TCGETS:
554 /* Get the termios attributes. */
555 if(safe) {
556 r = sys_safecopyto(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
557 (vir_bytes) &tp->tty_termios, (vir_bytes) size, D);
558 } else {
559 r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_termios,
560 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
561 (vir_bytes) size);
563 break;
565 case TCSETSW:
566 case TCSETSF:
567 case TCDRAIN:
568 if (tp->tty_outleft > 0) {
569 /* Wait for all ongoing output processing to finish. */
570 tp->tty_iocaller = m_ptr->m_source;
571 tp->tty_ioproc = m_ptr->IO_ENDPT;
572 tp->tty_ioreq = m_ptr->REQUEST;
573 tp->tty_iovir_g = (vir_bytes) m_ptr->ADDRESS;
574 tp->tty_io_safe = safe;
575 r = SUSPEND;
576 break;
578 if (m_ptr->TTY_REQUEST == TCDRAIN) break;
579 if (m_ptr->TTY_REQUEST == TCSETSF) tty_icancel(tp);
580 /*FALL THROUGH*/
581 case TCSETS:
582 /* Set the termios attributes. */
583 if(safe) {
584 r = sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
585 (vir_bytes) &tp->tty_termios, (vir_bytes) size, D);
586 } else {
587 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
588 SELF, D, (vir_bytes) &tp->tty_termios, (vir_bytes) size);
590 if (r != OK) break;
591 setattr(tp);
592 break;
594 case TCFLSH:
595 if(safe) {
596 r = sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
597 (vir_bytes) &param.i, (vir_bytes) size, D);
598 } else {
599 r = sys_vircopy(m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
600 SELF, D, (vir_bytes) &param.i, (vir_bytes) size);
602 if (r != OK) break;
603 switch (param.i) {
604 case TCIFLUSH: tty_icancel(tp); break;
605 case TCOFLUSH: (*tp->tty_ocancel)(tp, 0); break;
606 case TCIOFLUSH: tty_icancel(tp); (*tp->tty_ocancel)(tp, 0); break;
607 default: r = EINVAL;
609 break;
611 case TCFLOW:
612 if(safe) {
613 r = sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
614 (vir_bytes) &param.i, (vir_bytes) size, D);
615 } else {
616 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
617 SELF, D, (vir_bytes) &param.i, (vir_bytes) size);
619 if (r != OK) break;
620 switch (param.i) {
621 case TCOOFF:
622 case TCOON:
623 tp->tty_inhibited = (param.i == TCOOFF);
624 tp->tty_events = 1;
625 break;
626 case TCIOFF:
627 (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTOP]);
628 break;
629 case TCION:
630 (*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTART]);
631 break;
632 default:
633 r = EINVAL;
635 break;
637 case TCSBRK:
638 if (tp->tty_break != NULL) (*tp->tty_break)(tp,0);
639 break;
641 case TIOCGWINSZ:
642 if(safe) {
643 r = sys_safecopyto(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
644 (vir_bytes) &tp->tty_winsize, (vir_bytes) size, D);
645 } else {
646 r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_winsize,
647 m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
648 (vir_bytes) size);
650 break;
652 case TIOCSWINSZ:
653 if(safe) {
654 r = sys_safecopyfrom(m_ptr->IO_ENDPT, (vir_bytes) m_ptr->ADDRESS, 0,
655 (vir_bytes) &tp->tty_winsize, (vir_bytes) size, D);
656 } else {
657 r = sys_vircopy( m_ptr->IO_ENDPT, D, (vir_bytes) m_ptr->ADDRESS,
658 SELF, D, (vir_bytes) &tp->tty_winsize, (vir_bytes) size);
660 sigchar(tp, SIGWINCH);
661 break;
663 #if (MACHINE == IBM_PC)
664 case KIOCSMAP:
665 /* Load a new keymap (only /dev/console). */
666 if (isconsole(tp)) r = kbd_loadmap(m_ptr, safe);
667 break;
669 case TIOCSFON_OLD:
670 printf("TTY: old TIOCSFON ignored.\n");
671 break;
672 case TIOCSFON:
673 /* Load a font into an EGA or VGA card (hs@hck.hr) */
674 if (isconsole(tp)) r = con_loadfont(m_ptr);
675 break;
676 #endif
678 #if (MACHINE == ATARI)
679 case VDU_LOADFONT:
680 r = vdu_loadfont(m_ptr);
681 break;
682 #endif
684 /* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
685 * not defined.
687 case TIOCGPGRP:
688 case TIOCSPGRP:
689 default:
690 r = ENOTTY;
693 /* Send the reply. */
694 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
697 /*===========================================================================*
698 * do_open *
699 *===========================================================================*/
700 PRIVATE void do_open(tp, m_ptr)
701 register tty_t *tp;
702 message *m_ptr; /* pointer to message sent to task */
704 /* A tty line has been opened. Make it the callers controlling tty if
705 * O_NOCTTY is *not* set and it is not the log device. 1 is returned if
706 * the tty is made the controlling tty, otherwise OK or an error code.
708 int r = OK;
710 if (m_ptr->TTY_LINE == LOG_MINOR) {
711 /* The log device is a write-only diagnostics device. */
712 if (m_ptr->COUNT & R_BIT) r = EACCES;
713 } else {
714 if (!(m_ptr->COUNT & O_NOCTTY)) {
715 tp->tty_pgrp = m_ptr->IO_ENDPT;
716 r = 1;
718 tp->tty_openct++;
720 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, r);
723 /*===========================================================================*
724 * do_close *
725 *===========================================================================*/
726 PRIVATE void do_close(tp, m_ptr)
727 register tty_t *tp;
728 message *m_ptr; /* pointer to message sent to task */
730 /* A tty line has been closed. Clean up the line if it is the last close. */
732 if (m_ptr->TTY_LINE != LOG_MINOR && --tp->tty_openct == 0) {
733 tp->tty_pgrp = 0;
734 tty_icancel(tp);
735 (*tp->tty_ocancel)(tp, 0);
736 (*tp->tty_close)(tp, 0);
737 tp->tty_termios = termios_defaults;
738 tp->tty_winsize = winsize_defaults;
739 setattr(tp);
741 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, OK);
744 /*===========================================================================*
745 * do_cancel *
746 *===========================================================================*/
747 PRIVATE void do_cancel(tp, m_ptr)
748 register tty_t *tp;
749 message *m_ptr; /* pointer to message sent to task */
751 /* A signal has been sent to a process that is hanging trying to read or write.
752 * The pending read or write must be finished off immediately.
755 int proc_nr;
756 int mode;
757 int r = EINTR;
759 /* Check the parameters carefully, to avoid cancelling twice. */
760 proc_nr = m_ptr->IO_ENDPT;
761 mode = m_ptr->COUNT;
762 if ((mode & R_BIT) && tp->tty_inleft != 0 && proc_nr == tp->tty_inproc &&
763 (!tp->tty_in_safe || tp->tty_in_vir_g==(vir_bytes)m_ptr->IO_GRANT)) {
764 /* Process was reading when killed. Clean up input. */
765 tty_icancel(tp);
766 r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
767 tp->tty_inleft = tp->tty_incum = tp->tty_inrevived = 0;
769 if ((mode & W_BIT) && tp->tty_outleft != 0 && proc_nr == tp->tty_outproc &&
770 (!tp->tty_out_safe || tp->tty_out_vir_g==(vir_bytes)m_ptr->IO_GRANT)) {
771 /* Process was writing when killed. Clean up output. */
772 #if DEAD_CODE
773 (*tp->tty_ocancel)(tp, 0);
774 #endif
775 r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
776 tp->tty_outleft = tp->tty_outcum = tp->tty_outrevived = 0;
778 if (tp->tty_ioreq != 0 && proc_nr == tp->tty_ioproc) {
779 /* Process was waiting for output to drain. */
780 tp->tty_ioreq = 0;
782 tp->tty_events = 1;
783 tty_reply(TASK_REPLY, m_ptr->m_source, proc_nr, r);
786 PUBLIC int select_try(struct tty *tp, int ops)
788 int ready_ops = 0;
790 /* Special case. If line is hung up, no operations will block.
791 * (and it can be seen as an exceptional condition.)
793 if (tp->tty_termios.c_ospeed == B0) {
794 ready_ops |= ops;
797 if (ops & SEL_RD) {
798 /* will i/o not block on read? */
799 if (tp->tty_inleft > 0) {
800 ready_ops |= SEL_RD; /* EIO - no blocking */
801 } else if (tp->tty_incount > 0) {
802 /* Is a regular read possible? tty_incount
803 * says there is data. But a read will only succeed
804 * in canonical mode if a newline has been seen.
806 if (!(tp->tty_termios.c_lflag & ICANON) ||
807 tp->tty_eotct > 0) {
808 ready_ops |= SEL_RD;
813 if (ops & SEL_WR) {
814 if (tp->tty_outleft > 0) ready_ops |= SEL_WR;
815 else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= SEL_WR;
817 return ready_ops;
820 PUBLIC int select_retry(struct tty *tp)
822 if (tp->tty_select_ops && select_try(tp, tp->tty_select_ops))
823 notify(tp->tty_select_proc);
824 return OK;
827 /*===========================================================================*
828 * handle_events *
829 *===========================================================================*/
830 PUBLIC void handle_events(tp)
831 tty_t *tp; /* TTY to check for events. */
833 /* Handle any events pending on a TTY. These events are usually device
834 * interrupts.
836 * Two kinds of events are prominent:
837 * - a character has been received from the console or an RS232 line.
838 * - an RS232 line has completed a write request (on behalf of a user).
839 * The interrupt handler may delay the interrupt message at its discretion
840 * to avoid swamping the TTY task. Messages may be overwritten when the
841 * lines are fast or when there are races between different lines, input
842 * and output, because MINIX only provides single buffering for interrupt
843 * messages (in proc.c). This is handled by explicitly checking each line
844 * for fresh input and completed output on each interrupt.
846 char *buf;
847 unsigned count;
848 int status;
850 do {
851 tp->tty_events = 0;
853 /* Read input and perform input processing. */
854 (*tp->tty_devread)(tp, 0);
856 /* Perform output processing and write output. */
857 (*tp->tty_devwrite)(tp, 0);
859 /* Ioctl waiting for some event? */
860 if (tp->tty_ioreq != 0) dev_ioctl(tp);
861 } while (tp->tty_events);
863 /* Transfer characters from the input queue to a waiting process. */
864 in_transfer(tp);
866 /* Reply if enough bytes are available. */
867 if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
868 if (tp->tty_inrepcode == TTY_REVIVE) {
869 notify(tp->tty_incaller);
870 tp->tty_inrevived = 1;
871 } else {
872 tty_reply(tp->tty_inrepcode, tp->tty_incaller,
873 tp->tty_inproc, tp->tty_incum);
874 tp->tty_inleft = tp->tty_incum = 0;
877 if (tp->tty_select_ops)
879 select_retry(tp);
881 #if NR_PTYS > 0
882 if (ispty(tp))
883 select_retry_pty(tp);
884 #endif
887 /*===========================================================================*
888 * in_transfer *
889 *===========================================================================*/
890 PRIVATE void in_transfer(tp)
891 register tty_t *tp; /* pointer to terminal to read from */
893 /* Transfer bytes from the input queue to a process reading from a terminal. */
895 int ch;
896 int count;
897 char buf[64], *bp;
899 /* Force read to succeed if the line is hung up, looks like EOF to reader. */
900 if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
902 /* Anything to do? */
903 if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
905 bp = buf;
906 while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
907 ch = *tp->tty_intail;
909 if (!(ch & IN_EOF)) {
910 /* One character to be delivered to the user. */
911 *bp = ch & IN_CHAR;
912 tp->tty_inleft--;
913 if (++bp == bufend(buf)) {
914 /* Temp buffer full, copy to user space. */
915 if(tp->tty_in_safe) {
916 sys_safecopyto(tp->tty_inproc,
917 tp->tty_in_vir_g, tp->tty_in_vir_offset,
918 (vir_bytes) buf,
919 (vir_bytes) buflen(buf), D);
920 tp->tty_in_vir_offset += buflen(buf);
921 } else {
922 sys_vircopy(SELF, D, (vir_bytes) buf,
923 tp->tty_inproc, D, tp->tty_in_vir_g,
924 (vir_bytes) buflen(buf));
925 tp->tty_in_vir_g += buflen(buf);
927 tp->tty_incum += buflen(buf);
928 bp = buf;
932 /* Remove the character from the input queue. */
933 if (++tp->tty_intail == bufend(tp->tty_inbuf))
934 tp->tty_intail = tp->tty_inbuf;
935 tp->tty_incount--;
936 if (ch & IN_EOT) {
937 tp->tty_eotct--;
938 /* Don't read past a line break in canonical mode. */
939 if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
943 if (bp > buf) {
944 /* Leftover characters in the buffer. */
945 count = bp - buf;
946 if(tp->tty_in_safe) {
947 sys_safecopyto(tp->tty_inproc,
948 tp->tty_in_vir_g, tp->tty_in_vir_offset,
949 (vir_bytes) buf, (vir_bytes) count, D);
950 tp->tty_in_vir_offset += count;
951 } else {
952 sys_vircopy(SELF, D, (vir_bytes) buf,
953 tp->tty_inproc, D, tp->tty_in_vir_g, (vir_bytes) count);
954 tp->tty_in_vir_g += count;
956 tp->tty_incum += count;
959 /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
960 if (tp->tty_inleft == 0) {
961 if (tp->tty_inrepcode == TTY_REVIVE) {
962 notify(tp->tty_incaller);
963 tp->tty_inrevived = 1;
964 } else {
965 tty_reply(tp->tty_inrepcode, tp->tty_incaller,
966 tp->tty_inproc, tp->tty_incum);
967 tp->tty_inleft = tp->tty_incum = 0;
972 /*===========================================================================*
973 * in_process *
974 *===========================================================================*/
975 PUBLIC int in_process(tp, buf, count)
976 register tty_t *tp; /* terminal on which character has arrived */
977 char *buf; /* buffer with input characters */
978 int count; /* number of input characters */
980 /* Characters have just been typed in. Process, save, and echo them. Return
981 * the number of characters processed.
984 int ch, sig, ct;
985 int timeset = FALSE;
986 static unsigned char csize_mask[] = { 0x1F, 0x3F, 0x7F, 0xFF };
988 for (ct = 0; ct < count; ct++) {
989 /* Take one character. */
990 ch = *buf++ & BYTE;
992 /* Strip to seven bits? */
993 if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
995 /* Input extensions? */
996 if (tp->tty_termios.c_lflag & IEXTEN) {
998 /* Previous character was a character escape? */
999 if (tp->tty_escaped) {
1000 tp->tty_escaped = NOT_ESCAPED;
1001 ch |= IN_ESC; /* protect character */
1004 /* LNEXT (^V) to escape the next character? */
1005 if (ch == tp->tty_termios.c_cc[VLNEXT]) {
1006 tp->tty_escaped = ESCAPED;
1007 rawecho(tp, '^');
1008 rawecho(tp, '\b');
1009 continue; /* do not store the escape */
1012 /* REPRINT (^R) to reprint echoed characters? */
1013 if (ch == tp->tty_termios.c_cc[VREPRINT]) {
1014 reprint(tp);
1015 continue;
1019 /* _POSIX_VDISABLE is a normal character value, so better escape it. */
1020 if (ch == _POSIX_VDISABLE) ch |= IN_ESC;
1022 /* Map CR to LF, ignore CR, or map LF to CR. */
1023 if (ch == '\r') {
1024 if (tp->tty_termios.c_iflag & IGNCR) continue;
1025 if (tp->tty_termios.c_iflag & ICRNL) ch = '\n';
1026 } else
1027 if (ch == '\n') {
1028 if (tp->tty_termios.c_iflag & INLCR) ch = '\r';
1031 /* Canonical mode? */
1032 if (tp->tty_termios.c_lflag & ICANON) {
1034 /* Erase processing (rub out of last character). */
1035 if (ch == tp->tty_termios.c_cc[VERASE]) {
1036 (void) back_over(tp);
1037 if (!(tp->tty_termios.c_lflag & ECHOE)) {
1038 (void) tty_echo(tp, ch);
1040 continue;
1043 /* Kill processing (remove current line). */
1044 if (ch == tp->tty_termios.c_cc[VKILL]) {
1045 while (back_over(tp)) {}
1046 if (!(tp->tty_termios.c_lflag & ECHOE)) {
1047 (void) tty_echo(tp, ch);
1048 if (tp->tty_termios.c_lflag & ECHOK)
1049 rawecho(tp, '\n');
1051 continue;
1054 /* EOF (^D) means end-of-file, an invisible "line break". */
1055 if (ch == tp->tty_termios.c_cc[VEOF]) ch |= IN_EOT | IN_EOF;
1057 /* The line may be returned to the user after an LF. */
1058 if (ch == '\n') ch |= IN_EOT;
1060 /* Same thing with EOL, whatever it may be. */
1061 if (ch == tp->tty_termios.c_cc[VEOL]) ch |= IN_EOT;
1064 /* Start/stop input control? */
1065 if (tp->tty_termios.c_iflag & IXON) {
1067 /* Output stops on STOP (^S). */
1068 if (ch == tp->tty_termios.c_cc[VSTOP]) {
1069 tp->tty_inhibited = STOPPED;
1070 tp->tty_events = 1;
1071 continue;
1074 /* Output restarts on START (^Q) or any character if IXANY. */
1075 if (tp->tty_inhibited) {
1076 if (ch == tp->tty_termios.c_cc[VSTART]
1077 || (tp->tty_termios.c_iflag & IXANY)) {
1078 tp->tty_inhibited = RUNNING;
1079 tp->tty_events = 1;
1080 if (ch == tp->tty_termios.c_cc[VSTART])
1081 continue;
1086 if (tp->tty_termios.c_lflag & ISIG) {
1087 /* Check for INTR (^?) and QUIT (^\) characters. */
1088 if (ch == tp->tty_termios.c_cc[VINTR]
1089 || ch == tp->tty_termios.c_cc[VQUIT]) {
1090 sig = SIGINT;
1091 if (ch == tp->tty_termios.c_cc[VQUIT]) sig = SIGQUIT;
1092 sigchar(tp, sig);
1093 (void) tty_echo(tp, ch);
1094 continue;
1098 /* Is there space in the input buffer? */
1099 if (tp->tty_incount == buflen(tp->tty_inbuf)) {
1100 /* No space; discard in canonical mode, keep in raw mode. */
1101 if (tp->tty_termios.c_lflag & ICANON) continue;
1102 break;
1105 if (!(tp->tty_termios.c_lflag & ICANON)) {
1106 /* In raw mode all characters are "line breaks". */
1107 ch |= IN_EOT;
1109 /* Start an inter-byte timer? */
1110 if (!timeset && tp->tty_termios.c_cc[VMIN] > 0
1111 && tp->tty_termios.c_cc[VTIME] > 0) {
1112 settimer(tp, TRUE);
1113 timeset = TRUE;
1117 /* Perform the intricate function of echoing. */
1118 if (tp->tty_termios.c_lflag & (ECHO|ECHONL)) ch = tty_echo(tp, ch);
1120 /* Save the character in the input queue. */
1121 *tp->tty_inhead++ = ch;
1122 if (tp->tty_inhead == bufend(tp->tty_inbuf))
1123 tp->tty_inhead = tp->tty_inbuf;
1124 tp->tty_incount++;
1125 if (ch & IN_EOT) tp->tty_eotct++;
1127 /* Try to finish input if the queue threatens to overflow. */
1128 if (tp->tty_incount == buflen(tp->tty_inbuf)) in_transfer(tp);
1130 return ct;
1133 /*===========================================================================*
1134 * echo *
1135 *===========================================================================*/
1136 PRIVATE int tty_echo(tp, ch)
1137 register tty_t *tp; /* terminal on which to echo */
1138 register int ch; /* pointer to character to echo */
1140 /* Echo the character if echoing is on. Some control characters are echoed
1141 * with their normal effect, other control characters are echoed as "^X",
1142 * normal characters are echoed normally. EOF (^D) is echoed, but immediately
1143 * backspaced over. Return the character with the echoed length added to its
1144 * attributes.
1146 int len, rp;
1148 ch &= ~IN_LEN;
1149 if (!(tp->tty_termios.c_lflag & ECHO)) {
1150 if (ch == ('\n' | IN_EOT) && (tp->tty_termios.c_lflag
1151 & (ICANON|ECHONL)) == (ICANON|ECHONL))
1152 (*tp->tty_echo)(tp, '\n');
1153 return(ch);
1156 /* "Reprint" tells if the echo output has been messed up by other output. */
1157 rp = tp->tty_incount == 0 ? FALSE : tp->tty_reprint;
1159 if ((ch & IN_CHAR) < ' ') {
1160 switch (ch & (IN_ESC|IN_EOF|IN_EOT|IN_CHAR)) {
1161 case '\t':
1162 len = 0;
1163 do {
1164 (*tp->tty_echo)(tp, ' ');
1165 len++;
1166 } while (len < TAB_SIZE && (tp->tty_position & TAB_MASK) != 0);
1167 break;
1168 case '\r' | IN_EOT:
1169 case '\n' | IN_EOT:
1170 (*tp->tty_echo)(tp, ch & IN_CHAR);
1171 len = 0;
1172 break;
1173 default:
1174 (*tp->tty_echo)(tp, '^');
1175 (*tp->tty_echo)(tp, '@' + (ch & IN_CHAR));
1176 len = 2;
1178 } else
1179 if ((ch & IN_CHAR) == '\177') {
1180 /* A DEL prints as "^?". */
1181 (*tp->tty_echo)(tp, '^');
1182 (*tp->tty_echo)(tp, '?');
1183 len = 2;
1184 } else {
1185 (*tp->tty_echo)(tp, ch & IN_CHAR);
1186 len = 1;
1188 if (ch & IN_EOF) while (len > 0) { (*tp->tty_echo)(tp, '\b'); len--; }
1190 tp->tty_reprint = rp;
1191 return(ch | (len << IN_LSHIFT));
1194 /*===========================================================================*
1195 * rawecho *
1196 *===========================================================================*/
1197 PRIVATE void rawecho(tp, ch)
1198 register tty_t *tp;
1199 int ch;
1201 /* Echo without interpretation if ECHO is set. */
1202 int rp = tp->tty_reprint;
1203 if (tp->tty_termios.c_lflag & ECHO) (*tp->tty_echo)(tp, ch);
1204 tp->tty_reprint = rp;
1207 /*===========================================================================*
1208 * back_over *
1209 *===========================================================================*/
1210 PRIVATE int back_over(tp)
1211 register tty_t *tp;
1213 /* Backspace to previous character on screen and erase it. */
1214 u16_t *head;
1215 int len;
1217 if (tp->tty_incount == 0) return(0); /* queue empty */
1218 head = tp->tty_inhead;
1219 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
1220 if (*--head & IN_EOT) return(0); /* can't erase "line breaks" */
1221 if (tp->tty_reprint) reprint(tp); /* reprint if messed up */
1222 tp->tty_inhead = head;
1223 tp->tty_incount--;
1224 if (tp->tty_termios.c_lflag & ECHOE) {
1225 len = (*head & IN_LEN) >> IN_LSHIFT;
1226 while (len > 0) {
1227 rawecho(tp, '\b');
1228 rawecho(tp, ' ');
1229 rawecho(tp, '\b');
1230 len--;
1233 return(1); /* one character erased */
1236 /*===========================================================================*
1237 * reprint *
1238 *===========================================================================*/
1239 PRIVATE void reprint(tp)
1240 register tty_t *tp; /* pointer to tty struct */
1242 /* Restore what has been echoed to screen before if the user input has been
1243 * messed up by output, or if REPRINT (^R) is typed.
1245 int count;
1246 u16_t *head;
1248 tp->tty_reprint = FALSE;
1250 /* Find the last line break in the input. */
1251 head = tp->tty_inhead;
1252 count = tp->tty_incount;
1253 while (count > 0) {
1254 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
1255 if (head[-1] & IN_EOT) break;
1256 head--;
1257 count--;
1259 if (count == tp->tty_incount) return; /* no reason to reprint */
1261 /* Show REPRINT (^R) and move to a new line. */
1262 (void) tty_echo(tp, tp->tty_termios.c_cc[VREPRINT] | IN_ESC);
1263 rawecho(tp, '\r');
1264 rawecho(tp, '\n');
1266 /* Reprint from the last break onwards. */
1267 do {
1268 if (head == bufend(tp->tty_inbuf)) head = tp->tty_inbuf;
1269 *head = tty_echo(tp, *head);
1270 head++;
1271 count++;
1272 } while (count < tp->tty_incount);
1275 /*===========================================================================*
1276 * out_process *
1277 *===========================================================================*/
1278 PUBLIC void out_process(tp, bstart, bpos, bend, icount, ocount)
1279 tty_t *tp;
1280 char *bstart, *bpos, *bend; /* start/pos/end of circular buffer */
1281 int *icount; /* # input chars / input chars used */
1282 int *ocount; /* max output chars / output chars used */
1284 /* Perform output processing on a circular buffer. *icount is the number of
1285 * bytes to process, and the number of bytes actually processed on return.
1286 * *ocount is the space available on input and the space used on output.
1287 * (Naturally *icount < *ocount.) The column position is updated modulo
1288 * the TAB size, because we really only need it for tabs.
1291 int tablen;
1292 int ict = *icount;
1293 int oct = *ocount;
1294 int pos = tp->tty_position;
1296 while (ict > 0) {
1297 switch (*bpos) {
1298 case '\7':
1299 break;
1300 case '\b':
1301 pos--;
1302 break;
1303 case '\r':
1304 pos = 0;
1305 break;
1306 case '\n':
1307 if ((tp->tty_termios.c_oflag & (OPOST|ONLCR))
1308 == (OPOST|ONLCR)) {
1309 /* Map LF to CR+LF if there is space. Note that the
1310 * next character in the buffer is overwritten, so
1311 * we stop at this point.
1313 if (oct >= 2) {
1314 *bpos = '\r';
1315 if (++bpos == bend) bpos = bstart;
1316 *bpos = '\n';
1317 pos = 0;
1318 ict--;
1319 oct -= 2;
1321 goto out_done; /* no space or buffer got changed */
1323 break;
1324 case '\t':
1325 /* Best guess for the tab length. */
1326 tablen = TAB_SIZE - (pos & TAB_MASK);
1328 if ((tp->tty_termios.c_oflag & (OPOST|XTABS))
1329 == (OPOST|XTABS)) {
1330 /* Tabs must be expanded. */
1331 if (oct >= tablen) {
1332 pos += tablen;
1333 ict--;
1334 oct -= tablen;
1335 do {
1336 *bpos = ' ';
1337 if (++bpos == bend) bpos = bstart;
1338 } while (--tablen != 0);
1340 goto out_done;
1342 /* Tabs are output directly. */
1343 pos += tablen;
1344 break;
1345 default:
1346 /* Assume any other character prints as one character. */
1347 pos++;
1349 if (++bpos == bend) bpos = bstart;
1350 ict--;
1351 oct--;
1353 out_done:
1354 tp->tty_position = pos & TAB_MASK;
1356 *icount -= ict; /* [io]ct are the number of chars not used */
1357 *ocount -= oct; /* *[io]count are the number of chars that are used */
1360 /*===========================================================================*
1361 * dev_ioctl *
1362 *===========================================================================*/
1363 PRIVATE void dev_ioctl(tp)
1364 tty_t *tp;
1366 /* The ioctl's TCSETSW, TCSETSF and TCDRAIN wait for output to finish to make
1367 * sure that an attribute change doesn't affect the processing of current
1368 * output. Once output finishes the ioctl is executed as in do_ioctl().
1370 int result;
1372 if (tp->tty_outleft > 0) return; /* output not finished */
1374 if (tp->tty_ioreq != TCDRAIN) {
1375 if (tp->tty_ioreq == TCSETSF) tty_icancel(tp);
1376 if(tp->tty_io_safe) {
1377 result = sys_safecopyfrom(tp->tty_ioproc, tp->tty_iovir_g, 0,
1378 (vir_bytes) &tp->tty_termios,
1379 (vir_bytes) sizeof(tp->tty_termios), D);
1380 } else {
1381 result = sys_vircopy(tp->tty_ioproc, D, tp->tty_iovir_g,
1382 SELF, D, (vir_bytes) &tp->tty_termios,
1383 (vir_bytes) sizeof(tp->tty_termios));
1385 setattr(tp);
1387 tp->tty_ioreq = 0;
1388 notify(tp->tty_iocaller);
1389 tp->tty_iorevived = 1;
1390 tp->tty_iostatus = result;
1393 /*===========================================================================*
1394 * setattr *
1395 *===========================================================================*/
1396 PRIVATE void setattr(tp)
1397 tty_t *tp;
1399 /* Apply the new line attributes (raw/canonical, line speed, etc.) */
1400 u16_t *inp;
1401 int count;
1403 if (!(tp->tty_termios.c_lflag & ICANON)) {
1404 /* Raw mode; put a "line break" on all characters in the input queue.
1405 * It is undefined what happens to the input queue when ICANON is
1406 * switched off, a process should use TCSAFLUSH to flush the queue.
1407 * Keeping the queue to preserve typeahead is the Right Thing, however
1408 * when a process does use TCSANOW to switch to raw mode.
1410 count = tp->tty_eotct = tp->tty_incount;
1411 inp = tp->tty_intail;
1412 while (count > 0) {
1413 *inp |= IN_EOT;
1414 if (++inp == bufend(tp->tty_inbuf)) inp = tp->tty_inbuf;
1415 --count;
1419 /* Inspect MIN and TIME. */
1420 settimer(tp, FALSE);
1421 if (tp->tty_termios.c_lflag & ICANON) {
1422 /* No MIN & TIME in canonical mode. */
1423 tp->tty_min = 1;
1424 } else {
1425 /* In raw mode MIN is the number of chars wanted, and TIME how long
1426 * to wait for them. With interesting exceptions if either is zero.
1428 tp->tty_min = tp->tty_termios.c_cc[VMIN];
1429 if (tp->tty_min == 0 && tp->tty_termios.c_cc[VTIME] > 0)
1430 tp->tty_min = 1;
1433 if (!(tp->tty_termios.c_iflag & IXON)) {
1434 /* No start/stop output control, so don't leave output inhibited. */
1435 tp->tty_inhibited = RUNNING;
1436 tp->tty_events = 1;
1439 /* Setting the output speed to zero hangs up the phone. */
1440 if (tp->tty_termios.c_ospeed == B0) sigchar(tp, SIGHUP);
1442 /* Set new line speed, character size, etc at the device level. */
1443 (*tp->tty_ioctl)(tp, 0);
1446 /*===========================================================================*
1447 * tty_reply *
1448 *===========================================================================*/
1449 PUBLIC void
1450 tty_reply_f(
1451 file, line, code, replyee, proc_nr, status)
1452 char *file;
1453 int line;
1454 int code; /* TASK_REPLY or REVIVE */
1455 int replyee; /* destination address for the reply */
1456 int proc_nr; /* to whom should the reply go? */
1457 int status; /* reply code */
1459 /* Send a reply to a process that wanted to read or write data. */
1460 message tty_mess;
1462 tty_mess.m_type = code;
1463 tty_mess.REP_ENDPT = proc_nr;
1464 tty_mess.REP_STATUS = status;
1466 /* TTY is not supposed to send a TTY_REVIVE message. The
1467 * REVIVE message is gone, TTY_REVIVE is only used as an internal
1468 * placeholder for something that is not supposed to be a message.
1470 if(code == TTY_REVIVE) {
1471 panicing = 1;
1472 printf("%s:%d: ", file, line);
1473 panic("TTY","tty_reply sending TTY_REVIVE", NO_NUM);
1476 if ((status = send(replyee, &tty_mess)) != OK) {
1477 printf("tty: tty_reply to %d failed: %d\n", replyee, status);
1481 /*===========================================================================*
1482 * sigchar *
1483 *===========================================================================*/
1484 PUBLIC void sigchar(tp, sig)
1485 register tty_t *tp;
1486 int sig; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
1488 /* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
1489 * a tty close, "stty 0", or a real RS-232 hangup. MM will send the signal to
1490 * the process group (INT, QUIT), all processes (KILL), or the session leader
1491 * (HUP).
1493 int status;
1495 if (tp->tty_pgrp != 0) {
1496 if (OK != (status = sys_kill(tp->tty_pgrp, sig))) {
1497 panic("TTY","Error, call to sys_kill failed", status);
1501 if (!(tp->tty_termios.c_lflag & NOFLSH)) {
1502 tp->tty_incount = tp->tty_eotct = 0; /* kill earlier input */
1503 tp->tty_intail = tp->tty_inhead;
1504 (*tp->tty_ocancel)(tp, 0); /* kill all output */
1505 tp->tty_inhibited = RUNNING;
1506 tp->tty_events = 1;
1510 /*===========================================================================*
1511 * tty_icancel *
1512 *===========================================================================*/
1513 PRIVATE void tty_icancel(tp)
1514 register tty_t *tp;
1516 /* Discard all pending input, tty buffer or device. */
1518 tp->tty_incount = tp->tty_eotct = 0;
1519 tp->tty_intail = tp->tty_inhead;
1520 (*tp->tty_icancel)(tp, 0);
1523 /*===========================================================================*
1524 * tty_init *
1525 *===========================================================================*/
1526 PRIVATE void tty_init()
1528 /* Initialize tty structure and call device initialization routines. */
1530 register tty_t *tp;
1531 int s;
1532 struct sigaction sa;
1534 /* Initialize the terminal lines. */
1535 for (tp = FIRST_TTY,s=0; tp < END_TTY; tp++,s++) {
1537 tp->tty_index = s;
1539 tmr_inittimer(&tp->tty_tmr);
1541 tp->tty_intail = tp->tty_inhead = tp->tty_inbuf;
1542 tp->tty_min = 1;
1543 tp->tty_termios = termios_defaults;
1544 tp->tty_icancel = tp->tty_ocancel = tp->tty_ioctl = tp->tty_close =
1545 tty_devnop;
1546 if (tp < tty_addr(NR_CONS)) {
1547 scr_init(tp);
1549 /* Initialize the keyboard driver. */
1550 kb_init(tp);
1552 tp->tty_minor = CONS_MINOR + s;
1553 } else
1554 if (tp < tty_addr(NR_CONS+NR_RS_LINES)) {
1555 rs_init(tp);
1556 tp->tty_minor = RS232_MINOR + s-NR_CONS;
1557 } else {
1558 pty_init(tp);
1559 tp->tty_minor = s - (NR_CONS+NR_RS_LINES) + TTYPX_MINOR;
1563 #if DEAD_CODE
1564 /* Install signal handlers. Ask PM to transform signal into message. */
1565 sa.sa_handler = SIG_MESS;
1566 sigemptyset(&sa.sa_mask);
1567 sa.sa_flags = 0;
1568 if (sigaction(SIGTERM,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1569 if (sigaction(SIGKMESS,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1570 if (sigaction(SIGKSTOP,&sa,NULL)<0) panic("TTY","sigaction failed", errno);
1571 #endif
1572 #if DEBUG
1573 printf("end of tty_init\n");
1574 #endif
1577 /*===========================================================================*
1578 * tty_timed_out *
1579 *===========================================================================*/
1580 PRIVATE void tty_timed_out(timer_t *tp)
1582 /* This timer has expired. Set the events flag, to force processing. */
1583 tty_t *tty_ptr;
1584 tty_ptr = &tty_table[tmr_arg(tp)->ta_int];
1585 tty_ptr->tty_min = 0; /* force read to succeed */
1586 tty_ptr->tty_events = 1;
1589 /*===========================================================================*
1590 * expire_timers *
1591 *===========================================================================*/
1592 PRIVATE void expire_timers(void)
1594 /* A synchronous alarm message was received. Check if there are any expired
1595 * timers. Possibly set the event flag and reschedule another alarm.
1597 clock_t now; /* current time */
1598 int s;
1600 /* Get the current time to compare the timers against. */
1601 if ((s=getuptime(&now)) != OK)
1602 panic("TTY","Couldn't get uptime from clock.", s);
1604 /* Scan the queue of timers for expired timers. This dispatch the watchdog
1605 * functions of expired timers. Possibly a new alarm call must be scheduled.
1607 tmrs_exptimers(&tty_timers, now, NULL);
1608 if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
1609 else { /* set new sync alarm */
1610 tty_next_timeout = tty_timers->tmr_exp_time;
1611 if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
1612 panic("TTY","Couldn't set synchronous alarm.", s);
1616 /*===========================================================================*
1617 * settimer *
1618 *===========================================================================*/
1619 PRIVATE void settimer(tty_ptr, enable)
1620 tty_t *tty_ptr; /* line to set or unset a timer on */
1621 int enable; /* set timer if true, otherwise unset */
1623 clock_t now; /* current time */
1624 clock_t exp_time;
1625 int s;
1627 /* Get the current time to calculate the timeout time. */
1628 if ((s=getuptime(&now)) != OK)
1629 panic("TTY","Couldn't get uptime from clock.", s);
1630 if (enable) {
1631 exp_time = now + tty_ptr->tty_termios.c_cc[VTIME] * (HZ/10);
1632 /* Set a new timer for enabling the TTY events flags. */
1633 tmrs_settimer(&tty_timers, &tty_ptr->tty_tmr,
1634 exp_time, tty_timed_out, NULL);
1635 } else {
1636 /* Remove the timer from the active and expired lists. */
1637 tmrs_clrtimer(&tty_timers, &tty_ptr->tty_tmr, NULL);
1640 /* Now check if a new alarm must be scheduled. This happens when the front
1641 * of the timers queue was disabled or reinserted at another position, or
1642 * when a new timer was added to the front.
1644 if (tty_timers == NULL) tty_next_timeout = TMR_NEVER;
1645 else if (tty_timers->tmr_exp_time != tty_next_timeout) {
1646 tty_next_timeout = tty_timers->tmr_exp_time;
1647 if ((s=sys_setalarm(tty_next_timeout, 1)) != OK)
1648 panic("TTY","Couldn't set synchronous alarm.", s);
1652 /*===========================================================================*
1653 * tty_devnop *
1654 *===========================================================================*/
1655 PUBLIC int tty_devnop(tp, try)
1656 tty_t *tp;
1657 int try;
1659 /* Some functions need not be implemented at the device level. */
1662 /*===========================================================================*
1663 * do_select *
1664 *===========================================================================*/
1665 PRIVATE void do_select(tp, m_ptr)
1666 register tty_t *tp; /* pointer to tty struct */
1667 register message *m_ptr; /* pointer to message sent to the task */
1669 int ops, ready_ops = 0, watch;
1671 ops = m_ptr->IO_ENDPT & (SEL_RD|SEL_WR|SEL_ERR);
1672 watch = (m_ptr->IO_ENDPT & SEL_NOTIFY) ? 1 : 0;
1674 ready_ops = select_try(tp, ops);
1676 if (!ready_ops && ops && watch) {
1677 tp->tty_select_ops |= ops;
1678 tp->tty_select_proc = m_ptr->m_source;
1681 tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->IO_ENDPT, ready_ops);
1683 return;