<sys/socket.h>: turn off MSG_NOSIGNAL
[minix3.git] / drivers / pty / tty.c
blob48bb9006847020b8a468b8dc13d6b5c3eecd6ef7
1 /* TTY part of PTY. */
2 #include <assert.h>
3 #include <minix/drivers.h>
4 #include <minix/driver.h>
5 #include <termios.h>
6 #include <sys/ttycom.h>
7 #include <sys/ttydefaults.h>
8 #include <sys/fcntl.h>
9 #include <signal.h>
10 #include "tty.h"
12 #include <sys/time.h>
13 #include <sys/select.h>
15 /* Address of a tty structure. */
16 #define tty_addr(line) (&tty_table[line])
18 /* Macros for magic tty structure pointers. */
19 #define FIRST_TTY tty_addr(0)
20 #define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
22 /* A device exists if at least its 'devread' function is defined. */
23 #define tty_active(tp) ((tp)->tty_devread != NULL)
25 struct kmessages kmess;
27 static void tty_timed_out(minix_timer_t *tp);
28 static void settimer(tty_t *tty_ptr, int enable);
29 static void in_transfer(tty_t *tp);
30 static int tty_echo(tty_t *tp, int ch);
31 static void rawecho(tty_t *tp, int ch);
32 static int back_over(tty_t *tp);
33 static void reprint(tty_t *tp);
34 static void dev_ioctl(tty_t *tp);
35 static void setattr(tty_t *tp);
36 static void tty_icancel(tty_t *tp);
38 static int do_open(devminor_t minor, int access, endpoint_t user_endpt);
39 static int do_close(devminor_t minor);
40 static ssize_t do_read(devminor_t minor, u64_t position, endpoint_t endpt,
41 cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
42 static ssize_t do_write(devminor_t minor, u64_t position, endpoint_t endpt,
43 cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
44 static int do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
45 cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id);
46 static int do_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id);
47 static int do_select(devminor_t minor, unsigned int ops, endpoint_t endpt);
49 static struct chardriver tty_tab = {
50 .cdr_open = do_open,
51 .cdr_close = do_close,
52 .cdr_read = do_read,
53 .cdr_write = do_write,
54 .cdr_ioctl = do_ioctl,
55 .cdr_cancel = do_cancel,
56 .cdr_select = do_select
59 /* Default attributes. */
60 static struct termios termios_defaults = {
61 .c_iflag = TTYDEF_IFLAG,
62 .c_oflag = TTYDEF_OFLAG,
63 .c_cflag = TTYDEF_CFLAG,
64 .c_lflag = TTYDEF_LFLAG,
65 .c_ispeed = TTYDEF_SPEED,
66 .c_ospeed = TTYDEF_SPEED,
67 .c_cc = {
68 [VEOF] = CEOF,
69 [VEOL] = CEOL,
70 [VERASE] = CERASE,
71 [VINTR] = CINTR,
72 [VKILL] = CKILL,
73 [VMIN] = CMIN,
74 [VQUIT] = CQUIT,
75 [VTIME] = CTIME,
76 [VSUSP] = CSUSP,
77 [VSTART] = CSTART,
78 [VSTOP] = CSTOP,
79 [VREPRINT] = CREPRINT,
80 [VLNEXT] = CLNEXT,
81 [VDISCARD] = CDISCARD,
82 [VSTATUS] = CSTATUS
85 static struct winsize winsize_defaults; /* = all zeroes */
87 /* Global variables for the TTY task (declared extern in tty.h). */
88 tty_t tty_table[NR_PTYS];
89 u32_t system_hz;
91 static void tty_startup(void);
92 static int tty_init(int, sef_init_info_t *);
94 /*===========================================================================*
95 * tty_task *
96 *===========================================================================*/
97 int main(void)
99 /* Main routine of the terminal task. */
101 message tty_mess; /* buffer for all incoming messages */
102 int ipc_status;
103 int line;
104 int r;
105 register tty_t *tp;
107 tty_startup();
109 while (TRUE) {
110 /* Check for and handle any events on any of the ttys. */
111 for (tp = FIRST_TTY; tp < END_TTY; tp++) {
112 if (tp->tty_events) handle_events(tp);
115 /* Get a request message. */
116 r= driver_receive(ANY, &tty_mess, &ipc_status);
117 if (r != 0)
118 panic("driver_receive failed with: %d", r);
120 if (is_ipc_notify(ipc_status)) {
121 switch (_ENDPOINT_P(tty_mess.m_source)) {
122 case CLOCK:
123 /* run watchdogs of expired timers */
124 expire_timers(tty_mess.m_notify.timestamp);
125 break;
126 default:
127 /* do nothing */
128 break;
131 /* done, get new message */
132 continue;
135 if (!IS_CDEV_RQ(tty_mess.m_type)) {
136 chardriver_process(&tty_tab, &tty_mess, ipc_status);
137 continue;
140 /* Only device requests should get to this point.
141 * All requests have a minor device number.
143 if (OK != chardriver_get_minor(&tty_mess, &line))
144 continue;
146 if ((line >= PTYPX_MINOR) && (line < (PTYPX_MINOR + NR_PTYS)) &&
147 tty_mess.m_type != CDEV_IOCTL) {
148 /* Terminals and pseudo terminals belong together. We can only
149 * make a distinction between the two based on position in the
150 * tty_table and not on minor number. Hence this special case.
152 do_pty(&tty_mess, ipc_status);
153 continue;
156 /* Execute the requested device driver function. */
157 chardriver_process(&tty_tab, &tty_mess, ipc_status);
160 return 0;
163 tty_t *
164 line2tty(devminor_t line)
166 /* Convert a terminal line to tty_table pointer */
168 tty_t* tp;
170 if ((line - TTYPX_MINOR) < NR_PTYS) {
171 tp = tty_addr(line - TTYPX_MINOR);
172 } else if ((line - PTYPX_MINOR) < NR_PTYS) {
173 tp = tty_addr(line - PTYPX_MINOR);
174 } else {
175 tp = NULL;
178 return(tp);
181 /*===========================================================================*
182 * tty_startup *
183 *===========================================================================*/
184 static void tty_startup(void)
186 /* Register init callbacks. */
187 sef_setcb_init_fresh(tty_init);
188 sef_setcb_init_restart(tty_init);
190 /* No live update support for now. */
192 /* No signal support for now. */
194 /* Let SEF perform startup. */
195 sef_startup();
198 /*===========================================================================*
199 * do_read *
200 *===========================================================================*/
201 static ssize_t do_read(devminor_t minor, u64_t UNUSED(position),
202 endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
203 cdev_id_t id)
205 /* A process wants to read from a terminal. */
206 tty_t *tp;
207 int r;
209 if ((tp = line2tty(minor)) == NULL)
210 return ENXIO;
212 /* Check if there is already a process hanging in a read, check if the
213 * parameters are correct, do I/O.
215 if (tp->tty_incaller != NONE || tp->tty_inleft > 0)
216 return EIO;
217 if (size <= 0)
218 return EINVAL;
220 /* Copy information from the message to the tty struct. */
221 tp->tty_incaller = endpt;
222 tp->tty_inid = id;
223 tp->tty_ingrant = grant;
224 assert(tp->tty_incum == 0);
225 tp->tty_inleft = size;
227 if (!(tp->tty_termios.c_lflag & ICANON) && tp->tty_termios.c_cc[VTIME] > 0) {
228 if (tp->tty_termios.c_cc[VMIN] == 0) {
229 /* MIN & TIME specify a read timer that finishes the
230 * read in TIME/10 seconds if no bytes are available.
232 settimer(tp, TRUE);
233 tp->tty_min = 1;
234 } else {
235 /* MIN & TIME specify an inter-byte timer that may
236 * have to be cancelled if there are no bytes yet.
238 if (tp->tty_eotct == 0) {
239 settimer(tp, FALSE);
240 tp->tty_min = tp->tty_termios.c_cc[VMIN];
245 /* Anything waiting in the input buffer? Clear it out... */
246 in_transfer(tp);
247 /* ...then go back for more. */
248 handle_events(tp);
249 if (tp->tty_inleft == 0)
250 return EDONTREPLY; /* already done */
252 /* There were no bytes in the input queue available. */
253 if (flags & CDEV_NONBLOCK) {
254 tty_icancel(tp);
255 r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
256 tp->tty_inleft = tp->tty_incum = 0;
257 tp->tty_incaller = NONE;
258 return r;
261 if (tp->tty_select_ops)
262 select_retry(tp);
264 return EDONTREPLY; /* suspend the caller */
267 /*===========================================================================*
268 * do_write *
269 *===========================================================================*/
270 static ssize_t do_write(devminor_t minor, u64_t UNUSED(position),
271 endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
272 cdev_id_t id)
274 /* A process wants to write on a terminal. */
275 tty_t *tp;
276 int r;
278 if ((tp = line2tty(minor)) == NULL)
279 return ENXIO;
281 /* Check if there is already a process hanging in a write, check if the
282 * parameters are correct, do I/O.
284 if (tp->tty_outcaller != NONE || tp->tty_outleft > 0)
285 return EIO;
286 if (size <= 0)
287 return EINVAL;
289 /* Copy message parameters to the tty structure. */
290 tp->tty_outcaller = endpt;
291 tp->tty_outid = id;
292 tp->tty_outgrant = grant;
293 assert(tp->tty_outcum == 0);
294 tp->tty_outleft = size;
296 /* Try to write. */
297 handle_events(tp);
298 if (tp->tty_outleft == 0)
299 return EDONTREPLY; /* already done */
301 /* None or not all the bytes could be written. */
302 if (flags & CDEV_NONBLOCK) {
303 r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
304 tp->tty_outleft = tp->tty_outcum = 0;
305 tp->tty_outcaller = NONE;
306 return r;
309 if (tp->tty_select_ops)
310 select_retry(tp);
312 return EDONTREPLY; /* suspend the caller */
315 /*===========================================================================*
316 * do_ioctl *
317 *===========================================================================*/
318 static int do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
319 cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
321 /* Perform an IOCTL on this terminal. POSIX termios calls are handled
322 * by the IOCTL system call.
324 tty_t *tp;
325 int i, r;
327 if ((tp = line2tty(minor)) == NULL)
328 return ENXIO;
330 r = OK;
331 switch (request) {
332 case TIOCGETA:
333 /* Get the termios attributes. */
334 r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
335 sizeof(struct termios));
336 break;
338 case TIOCSETAW:
339 case TIOCSETAF:
340 case TIOCDRAIN:
341 if (tp->tty_outleft > 0) {
342 if (flags & CDEV_NONBLOCK)
343 return EAGAIN;
344 /* Wait for all ongoing output processing to finish. */
345 tp->tty_iocaller = endpt;
346 tp->tty_ioid = id;
347 tp->tty_ioreq = request;
348 tp->tty_iogrant = grant;
349 return EDONTREPLY; /* suspend the caller */
351 if (request == TIOCDRAIN) break;
352 if (request == TIOCSETAF) tty_icancel(tp);
353 /*FALL THROUGH*/
354 case TIOCSETA:
355 /* Set the termios attributes. */
356 r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
357 sizeof(struct termios));
358 if (r != OK) break;
359 setattr(tp);
360 break;
362 case TIOCFLUSH:
363 r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
364 if (r != OK) break;
365 if(i & FREAD) { tty_icancel(tp); }
366 if(i & FWRITE) { (*tp->tty_ocancel)(tp, 0); }
367 break;
368 case TIOCSTART:
369 tp->tty_inhibited = 0;
370 tp->tty_events = 1;
371 break;
372 case TIOCSTOP:
373 tp->tty_inhibited = 1;
374 tp->tty_events = 1;
375 break;
376 case TIOCSBRK: /* tcsendbreak - turn break on */
377 if (tp->tty_break_on != NULL) (*tp->tty_break_on)(tp,0);
378 break;
379 case TIOCCBRK: /* tcsendbreak - turn break off */
380 if (tp->tty_break_off != NULL) (*tp->tty_break_off)(tp,0);
381 break;
383 case TIOCGWINSZ:
384 r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
385 sizeof(struct winsize));
386 break;
388 case TIOCSWINSZ:
389 r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
390 sizeof(struct winsize));
391 sigchar(tp, SIGWINCH, 0);
392 break;
393 case TIOCGETD: /* get line discipline */
395 int disc = TTYDISC;
396 r = sys_safecopyto(endpt, grant, 0,
397 (vir_bytes) &disc, (vir_bytes) sizeof(disc));
398 break;
400 case TIOCSETD: /* set line discipline */
401 printf("TTY: TIOCSETD: can't set any other line discipline.\n");
402 r = ENOTTY;
403 break;
405 case TIOCSCTTY:
406 /* Process sets this tty as its controlling tty */
407 tp->tty_pgrp = user_endpt;
408 break;
410 /* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
411 * not defined.
413 case TIOCGPGRP:
414 case TIOCSPGRP:
415 default:
416 r = ENOTTY;
419 return r;
422 /*===========================================================================*
423 * do_open *
424 *===========================================================================*/
425 static int do_open(devminor_t minor, int access, endpoint_t user_endpt)
427 /* A tty line has been opened. Make it the callers controlling tty if
428 * CDEV_NOCTTY is *not* set and it is not the log device. CDEV_CTTY is returned
429 * if the tty is made the controlling tty, otherwise OK or an error code.
431 tty_t *tp;
432 int r = OK;
434 if ((tp = line2tty(minor)) == NULL)
435 return ENXIO;
437 if (!(access & CDEV_NOCTTY)) {
438 tp->tty_pgrp = user_endpt;
439 r = CDEV_CTTY;
441 tp->tty_openct++;
442 if (tp->tty_openct == 1) {
443 /* Tell the device that the tty is opened */
444 (*tp->tty_open)(tp, 0);
447 return r;
450 /*===========================================================================*
451 * do_close *
452 *===========================================================================*/
453 static int do_close(devminor_t minor)
455 /* A tty line has been closed. Clean up the line if it is the last close. */
456 tty_t *tp;
458 if ((tp = line2tty(minor)) == NULL)
459 return ENXIO;
461 if (--tp->tty_openct == 0) {
462 tp->tty_pgrp = 0;
463 tty_icancel(tp);
464 (*tp->tty_ocancel)(tp, 0);
465 (*tp->tty_close)(tp, 0);
466 tp->tty_termios = termios_defaults;
467 tp->tty_winsize = winsize_defaults;
468 setattr(tp);
471 return OK;
474 /*===========================================================================*
475 * do_cancel *
476 *===========================================================================*/
477 static int do_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
479 /* A signal has been sent to a process that is hanging trying to read or write.
480 * The pending read or write must be finished off immediately.
482 tty_t *tp;
483 int r;
485 if ((tp = line2tty(minor)) == NULL)
486 return ENXIO;
488 /* Check the parameters carefully, to avoid cancelling twice. */
489 r = EDONTREPLY;
490 if (tp->tty_inleft != 0 && endpt == tp->tty_incaller && id == tp->tty_inid) {
491 /* Process was reading when killed. Clean up input. */
492 tty_icancel(tp);
493 r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
494 tp->tty_inleft = tp->tty_incum = 0;
495 tp->tty_incaller = NONE;
496 } else if (tp->tty_outleft != 0 && endpt == tp->tty_outcaller &&
497 id == tp->tty_outid) {
498 /* Process was writing when killed. Clean up output. */
499 r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
500 tp->tty_outleft = tp->tty_outcum = 0;
501 tp->tty_outcaller = NONE;
502 } else if (tp->tty_ioreq != 0 && endpt == tp->tty_iocaller &&
503 id == tp->tty_ioid) {
504 /* Process was waiting for output to drain. */
505 r = EINTR;
506 tp->tty_ioreq = 0;
507 tp->tty_iocaller = NONE;
509 if (r != EDONTREPLY)
510 tp->tty_events = 1;
511 /* Only reply if we found a matching request. */
512 return r;
515 int select_try(struct tty *tp, int ops)
517 int ready_ops = 0;
519 /* Special case. If line is hung up, no operations will block.
520 * (and it can be seen as an exceptional condition.)
522 if (tp->tty_termios.c_ospeed == B0) {
523 ready_ops |= ops;
526 if (ops & CDEV_OP_RD) {
527 /* will i/o not block on read? */
528 if (tp->tty_inleft > 0) {
529 ready_ops |= CDEV_OP_RD; /* EIO - no blocking */
530 } else if (tp->tty_incount > 0) {
531 /* Is a regular read possible? tty_incount
532 * says there is data. But a read will only succeed
533 * in canonical mode if a newline has been seen.
535 if (!(tp->tty_termios.c_lflag & ICANON) ||
536 tp->tty_eotct > 0) {
537 ready_ops |= CDEV_OP_RD;
542 if (ops & CDEV_OP_WR) {
543 if (tp->tty_outleft > 0) ready_ops |= CDEV_OP_WR;
544 else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= CDEV_OP_WR;
546 return ready_ops;
549 int select_retry(struct tty *tp)
551 int ops;
553 if (tp->tty_select_ops && (ops = select_try(tp, tp->tty_select_ops))) {
554 chardriver_reply_select(tp->tty_select_proc,
555 tp->tty_select_minor, ops);
556 tp->tty_select_ops &= ~ops;
558 return OK;
561 /*===========================================================================*
562 * do_select *
563 *===========================================================================*/
564 static int do_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
566 tty_t *tp;
567 int ready_ops, watch;
569 if ((tp = line2tty(minor)) == NULL)
570 return ENXIO;
572 watch = (ops & CDEV_NOTIFY);
573 ops &= (CDEV_OP_RD | CDEV_OP_WR | CDEV_OP_ERR);
575 ready_ops = select_try(tp, ops);
577 ops &= ~ready_ops;
578 if (ops && watch) {
579 /* Translated minor numbers are a problem with late select replies. We
580 * have to save the minor number used to do the select, since otherwise
581 * VFS won't be able to make sense of those late replies. We do not
582 * support selecting on two different minors for the same object.
584 if (tp->tty_select_ops != 0 && tp->tty_select_minor != minor) {
585 printf("TTY: select on one object with two minors (%d, %d)\n",
586 tp->tty_select_minor, minor);
587 return EBADF;
589 tp->tty_select_ops |= ops;
590 tp->tty_select_proc = endpt;
591 tp->tty_select_minor = minor;
594 return ready_ops;
597 /*===========================================================================*
598 * handle_events *
599 *===========================================================================*/
600 void handle_events(tp)
601 tty_t *tp; /* TTY to check for events. */
603 /* Handle any events pending on a TTY. */
605 do {
606 tp->tty_events = 0;
608 /* Read input and perform input processing. */
609 (*tp->tty_devread)(tp, 0);
611 /* Perform output processing and write output. */
612 (*tp->tty_devwrite)(tp, 0);
614 /* Ioctl waiting for some event? */
615 if (tp->tty_ioreq != 0) dev_ioctl(tp);
616 } while (tp->tty_events);
618 /* Transfer characters from the input queue to a waiting process. */
619 in_transfer(tp);
621 /* Reply if enough bytes are available. */
622 if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
623 chardriver_reply_task(tp->tty_incaller, tp->tty_inid, tp->tty_incum);
624 tp->tty_inleft = tp->tty_incum = 0;
625 tp->tty_incaller = NONE;
627 if (tp->tty_select_ops)
629 select_retry(tp);
631 select_retry_pty(tp);
634 /*===========================================================================*
635 * in_transfer *
636 *===========================================================================*/
637 static void in_transfer(tp)
638 register tty_t *tp; /* pointer to terminal to read from */
640 /* Transfer bytes from the input queue to a process reading from a terminal. */
642 int ch;
643 int count;
644 char buf[64], *bp;
646 /* Force read to succeed if the line is hung up, looks like EOF to reader. */
647 if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
649 /* Anything to do? */
650 if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
652 bp = buf;
653 while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
654 ch = *tp->tty_intail;
656 if (!(ch & IN_EOF)) {
657 /* One character to be delivered to the user. */
658 *bp = ch & IN_CHAR;
659 tp->tty_inleft--;
660 if (++bp == bufend(buf)) {
661 /* Temp buffer full, copy to user space. */
662 sys_safecopyto(tp->tty_incaller,
663 tp->tty_ingrant, tp->tty_incum,
664 (vir_bytes) buf, (vir_bytes) buflen(buf));
665 tp->tty_incum += buflen(buf);
666 bp = buf;
670 /* Remove the character from the input queue. */
671 if (++tp->tty_intail == bufend(tp->tty_inbuf))
672 tp->tty_intail = tp->tty_inbuf;
673 tp->tty_incount--;
674 if (ch & IN_EOT) {
675 tp->tty_eotct--;
676 /* Don't read past a line break in canonical mode. */
677 if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
681 if (bp > buf) {
682 /* Leftover characters in the buffer. */
683 count = bp - buf;
684 sys_safecopyto(tp->tty_incaller, tp->tty_ingrant, tp->tty_incum,
685 (vir_bytes) buf, (vir_bytes) count);
686 tp->tty_incum += count;
689 /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
690 if (tp->tty_inleft == 0) {
691 chardriver_reply_task(tp->tty_incaller, tp->tty_inid, tp->tty_incum);
692 tp->tty_inleft = tp->tty_incum = 0;
693 tp->tty_incaller = NONE;
697 /*===========================================================================*
698 * in_process *
699 *===========================================================================*/
700 int in_process(tp, buf, count)
701 register tty_t *tp; /* terminal on which character has arrived */
702 char *buf; /* buffer with input characters */
703 int count; /* number of input characters */
705 /* Characters have just been typed in. Process, save, and echo them. Return
706 * the number of characters processed.
709 int ch, sig, ct;
710 int timeset = FALSE;
712 for (ct = 0; ct < count; ct++) {
713 /* Take one character. */
714 ch = *buf++ & BYTE;
716 /* Strip to seven bits? */
717 if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
719 /* Input extensions? */
720 if (tp->tty_termios.c_lflag & IEXTEN) {
722 /* Previous character was a character escape? */
723 if (tp->tty_escaped) {
724 tp->tty_escaped = NOT_ESCAPED;
725 ch |= IN_ESC; /* protect character */
728 /* LNEXT (^V) to escape the next character? */
729 if (ch == tp->tty_termios.c_cc[VLNEXT]) {
730 tp->tty_escaped = ESCAPED;
731 rawecho(tp, '^');
732 rawecho(tp, '\b');
733 continue; /* do not store the escape */
736 /* REPRINT (^R) to reprint echoed characters? */
737 if (ch == tp->tty_termios.c_cc[VREPRINT]) {
738 reprint(tp);
739 continue;
743 /* _POSIX_VDISABLE is a normal character value, so better escape it. */
744 if (ch == _POSIX_VDISABLE) ch |= IN_ESC;
746 /* Map CR to LF, ignore CR, or map LF to CR. */
747 if (ch == '\r') {
748 if (tp->tty_termios.c_iflag & IGNCR) continue;
749 if (tp->tty_termios.c_iflag & ICRNL) ch = '\n';
750 } else
751 if (ch == '\n') {
752 if (tp->tty_termios.c_iflag & INLCR) ch = '\r';
755 /* Canonical mode? */
756 if (tp->tty_termios.c_lflag & ICANON) {
758 /* Erase processing (rub out of last character). */
759 if (ch == tp->tty_termios.c_cc[VERASE]) {
760 (void) back_over(tp);
761 if (!(tp->tty_termios.c_lflag & ECHOE)) {
762 (void) tty_echo(tp, ch);
764 continue;
767 /* Kill processing (remove current line). */
768 if (ch == tp->tty_termios.c_cc[VKILL]) {
769 while (back_over(tp)) {}
770 if (!(tp->tty_termios.c_lflag & ECHOE)) {
771 (void) tty_echo(tp, ch);
772 if (tp->tty_termios.c_lflag & ECHOK)
773 rawecho(tp, '\n');
775 continue;
778 /* EOF (^D) means end-of-file, an invisible "line break". */
779 if (ch == tp->tty_termios.c_cc[VEOF]) ch |= IN_EOT | IN_EOF;
781 /* The line may be returned to the user after an LF. */
782 if (ch == '\n') ch |= IN_EOT;
784 /* Same thing with EOL, whatever it may be. */
785 if (ch == tp->tty_termios.c_cc[VEOL]) ch |= IN_EOT;
788 /* Start/stop input control? */
789 if (tp->tty_termios.c_iflag & IXON) {
791 /* Output stops on STOP (^S). */
792 if (ch == tp->tty_termios.c_cc[VSTOP]) {
793 tp->tty_inhibited = STOPPED;
794 tp->tty_events = 1;
795 continue;
798 /* Output restarts on START (^Q) or any character if IXANY. */
799 if (tp->tty_inhibited) {
800 if (ch == tp->tty_termios.c_cc[VSTART]
801 || (tp->tty_termios.c_iflag & IXANY)) {
802 tp->tty_inhibited = RUNNING;
803 tp->tty_events = 1;
804 if (ch == tp->tty_termios.c_cc[VSTART])
805 continue;
810 if (tp->tty_termios.c_lflag & ISIG) {
811 /* Check for INTR, QUIT and STATUS characters. */
812 int sig = -1;
813 if (ch == tp->tty_termios.c_cc[VINTR])
814 sig = SIGINT;
815 else if(ch == tp->tty_termios.c_cc[VQUIT])
816 sig = SIGQUIT;
817 else if(ch == tp->tty_termios.c_cc[VSTATUS])
818 sig = SIGINFO;
820 if(sig >= 0) {
821 sigchar(tp, sig, 1);
822 (void) tty_echo(tp, ch);
823 continue;
827 /* Is there space in the input buffer? */
828 if (tp->tty_incount == buflen(tp->tty_inbuf)) {
829 /* No space; discard in canonical mode, keep in raw mode. */
830 if (tp->tty_termios.c_lflag & ICANON) continue;
831 break;
834 if (!(tp->tty_termios.c_lflag & ICANON)) {
835 /* In raw mode all characters are "line breaks". */
836 ch |= IN_EOT;
838 /* Start an inter-byte timer? */
839 if (!timeset && tp->tty_termios.c_cc[VMIN] > 0
840 && tp->tty_termios.c_cc[VTIME] > 0) {
841 settimer(tp, TRUE);
842 timeset = TRUE;
846 /* Perform the intricate function of echoing. */
847 if (tp->tty_termios.c_lflag & (ECHO|ECHONL)) ch = tty_echo(tp, ch);
849 /* Save the character in the input queue. */
850 *tp->tty_inhead++ = ch;
851 if (tp->tty_inhead == bufend(tp->tty_inbuf))
852 tp->tty_inhead = tp->tty_inbuf;
853 tp->tty_incount++;
854 if (ch & IN_EOT) tp->tty_eotct++;
856 /* Try to finish input if the queue threatens to overflow. */
857 if (tp->tty_incount == buflen(tp->tty_inbuf)) in_transfer(tp);
859 return ct;
862 /*===========================================================================*
863 * echo *
864 *===========================================================================*/
865 static int tty_echo(tp, ch)
866 register tty_t *tp; /* terminal on which to echo */
867 register int ch; /* pointer to character to echo */
869 /* Echo the character if echoing is on. Some control characters are echoed
870 * with their normal effect, other control characters are echoed as "^X",
871 * normal characters are echoed normally. EOF (^D) is echoed, but immediately
872 * backspaced over. Return the character with the echoed length added to its
873 * attributes.
875 int len, rp;
877 ch &= ~IN_LEN;
878 if (!(tp->tty_termios.c_lflag & ECHO)) {
879 if (ch == ('\n' | IN_EOT) && (tp->tty_termios.c_lflag
880 & (ICANON|ECHONL)) == (ICANON|ECHONL))
881 (*tp->tty_echo)(tp, '\n');
882 return(ch);
885 /* "Reprint" tells if the echo output has been messed up by other output. */
886 rp = tp->tty_incount == 0 ? FALSE : tp->tty_reprint;
888 if ((ch & IN_CHAR) < ' ') {
889 switch (ch & (IN_ESC|IN_EOF|IN_EOT|IN_CHAR)) {
890 case '\t':
891 len = 0;
892 do {
893 (*tp->tty_echo)(tp, ' ');
894 len++;
895 } while (len < TAB_SIZE && (tp->tty_position & TAB_MASK) != 0);
896 break;
897 case '\r' | IN_EOT:
898 case '\n' | IN_EOT:
899 (*tp->tty_echo)(tp, ch & IN_CHAR);
900 len = 0;
901 break;
902 default:
903 (*tp->tty_echo)(tp, '^');
904 (*tp->tty_echo)(tp, '@' + (ch & IN_CHAR));
905 len = 2;
907 } else
908 if ((ch & IN_CHAR) == '\177') {
909 /* A DEL prints as "^?". */
910 (*tp->tty_echo)(tp, '^');
911 (*tp->tty_echo)(tp, '?');
912 len = 2;
913 } else {
914 (*tp->tty_echo)(tp, ch & IN_CHAR);
915 len = 1;
917 if (ch & IN_EOF) while (len > 0) { (*tp->tty_echo)(tp, '\b'); len--; }
919 tp->tty_reprint = rp;
920 return(ch | (len << IN_LSHIFT));
923 /*===========================================================================*
924 * rawecho *
925 *===========================================================================*/
926 static void rawecho(tp, ch)
927 register tty_t *tp;
928 int ch;
930 /* Echo without interpretation if ECHO is set. */
931 int rp = tp->tty_reprint;
932 if (tp->tty_termios.c_lflag & ECHO) (*tp->tty_echo)(tp, ch);
933 tp->tty_reprint = rp;
936 /*===========================================================================*
937 * back_over *
938 *===========================================================================*/
939 static int back_over(tp)
940 register tty_t *tp;
942 /* Backspace to previous character on screen and erase it. */
943 u16_t *head;
944 int len;
946 if (tp->tty_incount == 0) return(0); /* queue empty */
947 head = tp->tty_inhead;
948 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
949 if (*--head & IN_EOT) return(0); /* can't erase "line breaks" */
950 if (tp->tty_reprint) reprint(tp); /* reprint if messed up */
951 tp->tty_inhead = head;
952 tp->tty_incount--;
953 if (tp->tty_termios.c_lflag & ECHOE) {
954 len = (*head & IN_LEN) >> IN_LSHIFT;
955 while (len > 0) {
956 rawecho(tp, '\b');
957 rawecho(tp, ' ');
958 rawecho(tp, '\b');
959 len--;
962 return(1); /* one character erased */
965 /*===========================================================================*
966 * reprint *
967 *===========================================================================*/
968 static void reprint(tp)
969 register tty_t *tp; /* pointer to tty struct */
971 /* Restore what has been echoed to screen before if the user input has been
972 * messed up by output, or if REPRINT (^R) is typed.
974 int count;
975 u16_t *head;
977 tp->tty_reprint = FALSE;
979 /* Find the last line break in the input. */
980 head = tp->tty_inhead;
981 count = tp->tty_incount;
982 while (count > 0) {
983 if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
984 if (head[-1] & IN_EOT) break;
985 head--;
986 count--;
988 if (count == tp->tty_incount) return; /* no reason to reprint */
990 /* Show REPRINT (^R) and move to a new line. */
991 (void) tty_echo(tp, tp->tty_termios.c_cc[VREPRINT] | IN_ESC);
992 rawecho(tp, '\r');
993 rawecho(tp, '\n');
995 /* Reprint from the last break onwards. */
996 do {
997 if (head == bufend(tp->tty_inbuf)) head = tp->tty_inbuf;
998 *head = tty_echo(tp, *head);
999 head++;
1000 count++;
1001 } while (count < tp->tty_incount);
1004 /*===========================================================================*
1005 * out_process *
1006 *===========================================================================*/
1007 void out_process(tp, bstart, bpos, bend, icount, ocount)
1008 tty_t *tp;
1009 char *bstart, *bpos, *bend; /* start/pos/end of circular buffer */
1010 int *icount; /* # input chars / input chars used */
1011 int *ocount; /* max output chars / output chars used */
1013 /* Perform output processing on a circular buffer. *icount is the number of
1014 * bytes to process, and the number of bytes actually processed on return.
1015 * *ocount is the space available on input and the space used on output.
1016 * (Naturally *icount < *ocount.) The column position is updated modulo
1017 * the TAB size, because we really only need it for tabs.
1020 int tablen;
1021 int ict = *icount;
1022 int oct = *ocount;
1023 int pos = tp->tty_position;
1025 while (ict > 0) {
1026 switch (*bpos) {
1027 case '\7':
1028 break;
1029 case '\b':
1030 pos--;
1031 break;
1032 case '\r':
1033 pos = 0;
1034 break;
1035 case '\n':
1036 if ((tp->tty_termios.c_oflag & (OPOST|ONLCR))
1037 == (OPOST|ONLCR)) {
1038 /* Map LF to CR+LF if there is space. Note that the
1039 * next character in the buffer is overwritten, so
1040 * we stop at this point.
1042 if (oct >= 2) {
1043 *bpos = '\r';
1044 if (++bpos == bend) bpos = bstart;
1045 *bpos = '\n';
1046 pos = 0;
1047 ict--;
1048 oct -= 2;
1050 goto out_done; /* no space or buffer got changed */
1052 break;
1053 case '\t':
1054 /* Best guess for the tab length. */
1055 tablen = TAB_SIZE - (pos & TAB_MASK);
1057 if ((tp->tty_termios.c_oflag & (OPOST|OXTABS))
1058 == (OPOST|OXTABS)) {
1059 /* Tabs must be expanded. */
1060 if (oct >= tablen) {
1061 pos += tablen;
1062 ict--;
1063 oct -= tablen;
1064 do {
1065 *bpos = ' ';
1066 if (++bpos == bend) bpos = bstart;
1067 } while (--tablen != 0);
1069 goto out_done;
1071 /* Tabs are output directly. */
1072 pos += tablen;
1073 break;
1074 default:
1075 /* Assume any other character prints as one character. */
1076 pos++;
1078 if (++bpos == bend) bpos = bstart;
1079 ict--;
1080 oct--;
1082 out_done:
1083 tp->tty_position = pos & TAB_MASK;
1085 *icount -= ict; /* [io]ct are the number of chars not used */
1086 *ocount -= oct; /* *[io]count are the number of chars that are used */
1089 /*===========================================================================*
1090 * dev_ioctl *
1091 *===========================================================================*/
1092 static void dev_ioctl(tp)
1093 tty_t *tp;
1095 /* The ioctl's TCSETSW, TCSETSF and TIOCDRAIN wait for output to finish to make
1096 * sure that an attribute change doesn't affect the processing of current
1097 * output. Once output finishes the ioctl is executed as in do_ioctl().
1099 int result = EINVAL;
1101 if (tp->tty_outleft > 0) return; /* output not finished */
1103 if (tp->tty_ioreq != TIOCDRAIN) {
1104 if (tp->tty_ioreq == TIOCSETAF) tty_icancel(tp);
1105 result = sys_safecopyfrom(tp->tty_iocaller, tp->tty_iogrant, 0,
1106 (vir_bytes) &tp->tty_termios,
1107 (vir_bytes) sizeof(tp->tty_termios));
1108 if (result == OK) setattr(tp);
1110 tp->tty_ioreq = 0;
1111 chardriver_reply_task(tp->tty_iocaller, tp->tty_ioid, result);
1112 tp->tty_iocaller = NONE;
1115 /*===========================================================================*
1116 * setattr *
1117 *===========================================================================*/
1118 static void setattr(tp)
1119 tty_t *tp;
1121 /* Apply the new line attributes (raw/canonical, line speed, etc.) */
1122 u16_t *inp;
1123 int count;
1125 if (!(tp->tty_termios.c_lflag & ICANON)) {
1126 /* Raw mode; put a "line break" on all characters in the input queue.
1127 * It is undefined what happens to the input queue when ICANON is
1128 * switched off, a process should use TCSAFLUSH to flush the queue.
1129 * Keeping the queue to preserve typeahead is the Right Thing, however
1130 * when a process does use TCSANOW to switch to raw mode.
1132 count = tp->tty_eotct = tp->tty_incount;
1133 inp = tp->tty_intail;
1134 while (count > 0) {
1135 *inp |= IN_EOT;
1136 if (++inp == bufend(tp->tty_inbuf)) inp = tp->tty_inbuf;
1137 --count;
1141 /* Inspect MIN and TIME. */
1142 settimer(tp, FALSE);
1143 if (tp->tty_termios.c_lflag & ICANON) {
1144 /* No MIN & TIME in canonical mode. */
1145 tp->tty_min = 1;
1146 } else {
1147 /* In raw mode MIN is the number of chars wanted, and TIME how long
1148 * to wait for them. With interesting exceptions if either is zero.
1150 tp->tty_min = tp->tty_termios.c_cc[VMIN];
1151 if (tp->tty_min == 0 && tp->tty_termios.c_cc[VTIME] > 0)
1152 tp->tty_min = 1;
1155 if (!(tp->tty_termios.c_iflag & IXON)) {
1156 /* No start/stop output control, so don't leave output inhibited. */
1157 tp->tty_inhibited = RUNNING;
1158 tp->tty_events = 1;
1161 /* Setting the output speed to zero hangs up the phone. */
1162 if (tp->tty_termios.c_ospeed == B0) sigchar(tp, SIGHUP, 1);
1164 /* Set new line speed, character size, etc at the device level. */
1165 (*tp->tty_ioctl)(tp, 0);
1168 /*===========================================================================*
1169 * sigchar *
1170 *===========================================================================*/
1171 void sigchar(tp, sig, mayflush)
1172 register tty_t *tp;
1173 int sig; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
1174 int mayflush;
1176 /* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
1177 * a tty close, "stty 0", or a real RS-232 hangup. PM will send the signal to
1178 * the process group (INT, QUIT), all processes (KILL), or the session leader
1179 * (HUP).
1181 int status;
1183 if (tp->tty_pgrp != 0) {
1184 if (OK != (status = sys_kill(tp->tty_pgrp, sig))) {
1185 panic("Error; call to sys_kill failed: %d", status);
1189 if (mayflush && !(tp->tty_termios.c_lflag & NOFLSH)) {
1190 tp->tty_incount = tp->tty_eotct = 0; /* kill earlier input */
1191 tp->tty_intail = tp->tty_inhead;
1192 (*tp->tty_ocancel)(tp, 0); /* kill all output */
1193 tp->tty_inhibited = RUNNING;
1194 tp->tty_events = 1;
1198 /*===========================================================================*
1199 * tty_icancel *
1200 *===========================================================================*/
1201 static void tty_icancel(tp)
1202 register tty_t *tp;
1204 /* Discard all pending input, tty buffer or device. */
1206 tp->tty_incount = tp->tty_eotct = 0;
1207 tp->tty_intail = tp->tty_inhead;
1208 (*tp->tty_icancel)(tp, 0);
1211 /*===========================================================================*
1212 * tty_devnop *
1213 *===========================================================================*/
1214 static int tty_devnop(tty_t *UNUSED(tp), int UNUSED(try))
1216 /* Some functions need not be implemented at the device level. */
1217 return 0;
1220 /*===========================================================================*
1221 * tty_init *
1222 *===========================================================================*/
1223 static int tty_init(int UNUSED(type), sef_init_info_t *UNUSED(info))
1225 /* Initialize tty structure and call device initialization routines. */
1227 register tty_t *tp;
1228 int s;
1230 system_hz = sys_hz();
1232 /* Initialize the terminal lines. */
1233 memset(tty_table, '\0' , sizeof(tty_table));
1235 for (tp = FIRST_TTY,s=0; tp < END_TTY; tp++,s++) {
1237 tp->tty_index = s;
1238 init_timer(&tp->tty_tmr);
1240 tp->tty_intail = tp->tty_inhead = tp->tty_inbuf;
1241 tp->tty_min = 1;
1242 tp->tty_incaller = tp->tty_outcaller = tp->tty_iocaller = NONE;
1243 tp->tty_termios = termios_defaults;
1244 tp->tty_icancel = tp->tty_ocancel = tp->tty_ioctl = tp->tty_close =
1245 tp->tty_open = tty_devnop;
1246 pty_init(tp);
1247 tp->tty_minor = s + TTYPX_MINOR;
1250 return OK;
1253 /*===========================================================================*
1254 * tty_timed_out *
1255 *===========================================================================*/
1256 static void tty_timed_out(minix_timer_t *tp)
1258 /* This timer has expired. Set the events flag, to force processing. */
1259 tty_t *tty_ptr;
1260 tty_ptr = &tty_table[tmr_arg(tp)->ta_int];
1261 tty_ptr->tty_min = 0; /* force read to succeed */
1262 tty_ptr->tty_events = 1;
1265 /*===========================================================================*
1266 * settimer *
1267 *===========================================================================*/
1268 static void settimer(tty_ptr, enable)
1269 tty_t *tty_ptr; /* line to set or unset a timer on */
1270 int enable; /* set timer if true, otherwise unset */
1272 clock_t ticks;
1274 if (enable) {
1275 ticks = tty_ptr->tty_termios.c_cc[VTIME] * (system_hz/10);
1277 /* Set a new timer for enabling the TTY events flags. */
1278 set_timer(&tty_ptr->tty_tmr, ticks, tty_timed_out, tty_ptr->tty_index);
1279 } else {
1280 /* Remove the timer from the active and expired lists. */
1281 cancel_timer(&tty_ptr->tty_tmr);