FFFF minix/minix/drivers/tty/pty/pty.c at master · yaricom/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
860 lines (716 loc) · 24.8 KB

pty.c

File metadata and controls

860 lines (716 loc) · 24.8 KB
/* Output is finished, reply to the writer. */
chardriver_reply_task(tp->tty_outcaller, tp->tty_outid,
tp->tty_outcum);
tp->tty_outcum = 0;
tp->tty_outcaller = NONE;
}
}
pty_finish(pp);
return 1;
}
/*===========================================================================*
* pty_slave_echo *
*===========================================================================*/
static void pty_slave_echo(tty_t *tp, int c)
{
/* Echo one character. (Like pty_write, but only one character, optionally.) */
pty_t *pp = tp->tty_priv;
int count, ocount;
ocount = buflen(pp->obuf) - pp->ocount;
if (ocount == 0) return; /* output buffer full */
count = 1;
*pp->ohead = c; /* add one character */
out_process(tp, pp->obuf, pp->ohead, bufend(pp->obuf), &count, &ocount);
if (count == 0) return;
pp->ocount += ocount;
if ((pp->ohead += ocount) >= bufend(pp->obuf)) pp->ohead -= buflen(pp->obuf);
pty_start(pp);
}
/*===========================================================================*
* pty_start *
*===========================================================================*/
static void pty_start(pty_t *pp)
{
/* Transfer bytes written to the output buffer to the PTY reader. */
int count;
char c;
/* While there are things to do. */
for (;;) {
count = bufend(pp->obuf) - pp->otail;
if (count > pp->ocount) count = pp->ocount;
if (count == 0 || pp->rdleft == 0) break;
/* If there is output at all, and packet mode is enabled, then prepend
* the output with a zero byte. This is absolutely minimal "support"
* for the TIOCPKT receipt mode to get telnetd(8) going. Implementing
* full support for all the TIOCPKT bits will require more work.
*/
if (pp->rdcum == 0 && (pp->state & PTY_PKTMODE)) {
c = 0;
if (sys_safecopyto(pp->rdcaller, pp->rdgrant, 0, (vir_bytes)&c,
sizeof(c)) != OK)
break;
pp->rdcum++;
pp->rdleft--;
}
if (count > pp->rdleft) count = pp->rdleft;
if (count == 0) break;
/* Copy from the output buffer to the readers address space. */
if (sys_safecopyto(pp->rdcaller, pp->rdgrant, pp->rdcum,
(vir_bytes)pp->otail, count) != OK)
break;
/* Bookkeeping. */
pp->ocount -= count;
if ((pp->otail += count) == bufend(pp->obuf)) pp->otail = pp->obuf;
pp->rdcum += count;
pp->rdleft -= count;
}
}
/*===========================================================================*
* pty_finish *
*===========================================================================*/
static void pty_finish(pty_t *pp)
{
/* Finish the read request of a PTY reader if there is at least one byte
* transferred.
*/
if (pp->rdcum > 0) {
chardriver_reply_task(pp->rdcaller, pp->rdid, pp->rdcum);
pp->rdleft = pp->rdcum = 0;
pp->rdcaller = NONE;
}
}
/*===========================================================================*
* pty_slave_read *
*===========================================================================*/
static int pty_slave_read(tty_t *tp, int try)
{
/* Offer bytes from the PTY writer for input on the TTY. (Do it one byte at
* a time, 99% of the writes will be for one byte, so no sense in being smart.)
*/
pty_t *pp = tp->tty_priv;
char c;
if (pp->state & PTY_CLOSED) {
if (try) return 1;
if (tp->tty_inleft > 0) {
chardriver_reply_task(tp->tty_incaller, tp->tty_inid,
tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
}
return 1;
}
if (try) {
if (pp->wrleft > 0)
return 1;
return 0;
}
while (pp->wrleft > 0) {
int s;
/* Transfer one character to 'c'. */
if ((s = sys_safecopyfrom(pp->wrcaller, pp->wrgrant, pp->wrcum,
(vir_bytes) &c, 1)) != OK) {
printf("pty: safecopy failed (error %d)\n", s);
break;
}
/* Input processing. */
if (in_process(tp, &c, 1) == 0) break;
/* PTY writer bookkeeping. */
pp->wrcum++;
if (--pp->wrleft == 0) {
chardriver_reply_task(pp->wrcaller, pp->wrid, pp->wrcum);
pp->wrcum = 0;
pp->wrcaller = NONE;
}
}
return 0;
}
/*===========================================================================*
* pty_slave_mayopen *
*===========================================================================*/
static int pty_slave_mayopen(tty_t *tp, devminor_t line)
{
/* Check if the user is not mixing Unix98 and non-Unix98 terminal ends. */
pty_t *pp;
int unix98_line, unix98_pty;
pp = tp->tty_priv;
/* A non-Unix98 slave may be opened even if the corresponding master is not
* opened yet, but PTY_UNIX98 is always clear for free ptys. A Unix98 slave
* may not be opened before its master, but this should not occur anyway.
*/
unix98_line = (line >= UNIX98_MINOR && line < UNIX98_MINOR + NR_PTYS * 2);
unix98_pty = !!(pp->state & PTY_UNIX98);
return (unix98_line == unix98_pty);
}
/*===========================================================================*
* pty_slave_open *
*===========================================================================*/
static int pty_slave_open(tty_t *tp, int UNUSED(try))
{
/* The tty side has been opened. */
pty_t *pp = tp->tty_priv;
/* TTY_ACTIVE may already be set, which would indicate that the slave is
* reopened after being fully closed while the master is still open. In that
* case TTY_CLOSED will also be set, so clear that one.
*/
pp->state |= TTY_ACTIVE;
pp->state &= ~TTY_CLOSED;
return 0;
}
/*===========================================================================*
* pty_slave_close *
*===========================================================================*/
static int pty_slave_close(tty_t *tp, int UNUSED(try))
{
/* The tty side has closed, so shut down the pty side. */
pty_t *pp = tp->tty_priv;
if (!(pp->state & PTY_ACTIVE)) return 0;
if (pp->rdleft > 0) {
chardriver_reply_task(pp->rdcaller, pp->rdid, pp->rdcum);
pp->rdleft = pp->rdcum = 0;
pp->rdcaller = NONE;
}
if (pp->wrleft > 0) {
chardriver_reply_task(pp->wrcaller, pp->wrid, pp->wrcum);
pp->wrleft = pp->wrcum = 0;
pp->wrcaller = NONE;
}
if (pp->state & PTY_CLOSED) pty_reset(tp);
else pp->state |= TTY_CLOSED;
return 0;
}
/*===========================================================================*
* pty_slave_icancel *
*===========================================================================*/
static int pty_slave_icancel(tty_t *tp, int UNUSED(try))
{
/* Discard waiting input. */
pty_t *pp = tp->tty_priv;
if (pp->wrleft > 0) {
chardriver_reply_task(pp->wrcaller, pp->wrid, pp->wrcum + pp->wrleft);
pp->wrcum = pp->wrleft = 0;
pp->wrcaller = NONE;
}
return 0;
}
/*===========================================================================*
* pty_slave_ocancel *
*===========================================================================*/
static int pty_slave_ocancel(tty_t *tp, int UNUSED(try))
{
/* Drain the output buffer. */
pty_t *pp = tp->tty_priv;
pp->ocount = 0;
pp->otail = pp->ohead;
return 0;
}
/*===========================================================================*
* pty_init *
*===========================================================================*/
void pty_init(tty_t *tp)
{
pty_t *pp;
int line;
/* Associate PTY and TTY structures. */
line = tp - tty_table;
pp = tp->tty_priv = &pty_table[line];
pp->tty = tp;
pp->select_ops = 0;
pp->rdcaller = NONE;
pp->wrcaller = NONE;
/* Set up output queue. */
pp->ohead = pp->otail = pp->obuf;
/* Fill in TTY function hooks. */
tp->tty_devread = pty_slave_read;
tp->tty_devwrite = pty_slave_write;
tp->tty_echo = pty_slave_echo;
tp->tty_icancel = pty_slave_icancel;< 33CA /div>
tp->tty_ocancel = pty_slave_ocancel;
tp->tty_mayopen = pty_slave_mayopen;
tp->tty_open = pty_slave_open;
tp->tty_close = pty_slave_close;
tp->tty_select_ops = 0;
}
0