newlib/winsup/cygwin/fhandler_termios.cc

301 lines
6.6 KiB
C++
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* fhandler_termios.cc
Copyright 1996, 1997, 1998 Cygnus Solutions.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
2000-02-17 20:38:33 +01:00
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include "cygerrno.h"
#include "fhandler.h"
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "tty.h"
2000-02-17 20:38:33 +01:00
/* Common functions shared by tty/console */
void
fhandler_termios::tcinit (tty_min *this_tc, int force)
{
/* Initial termios values */
tc = this_tc;
if (force || !TTYISSETF (INITIALIZED))
{
tc->ti.c_iflag = BRKINT | ICRNL | IXON;
tc->ti.c_oflag = OPOST | ONLCR;
tc->ti.c_cflag = B38400 | CS8 | CREAD;
tc->ti.c_lflag = ISIG | ICANON | ECHO | IEXTEN;
tc->ti.c_cc[VDISCARD] = CFLUSH;
tc->ti.c_cc[VEOL] = CEOL;
tc->ti.c_cc[VEOL2] = CEOL2;
tc->ti.c_cc[VEOF] = CEOF;
tc->ti.c_cc[VERASE] = CERASE;
tc->ti.c_cc[VINTR] = CINTR;
tc->ti.c_cc[VKILL] = CKILL;
tc->ti.c_cc[VLNEXT] = CLNEXT;
tc->ti.c_cc[VMIN] = 1;
tc->ti.c_cc[VQUIT] = CQUIT;
tc->ti.c_cc[VREPRINT] = CRPRNT;
tc->ti.c_cc[VSTART] = CSTART;
tc->ti.c_cc[VSTOP] = CSTOP;
tc->ti.c_cc[VSUSP] = CSUSP;
tc->ti.c_cc[VSWTC] = CSWTCH;
tc->ti.c_cc[VTIME] = 0;
tc->ti.c_cc[VWERASE] = CWERASE;
tc->ti.c_ispeed = tc->ti.c_ospeed = B38400;
tc->pgid = myself->pgid;
TTYSETF (INITIALIZED);
}
}
int
fhandler_termios::tcsetpgrp (const pid_t pgid)
{
termios_printf ("pgid %d, sid %d, tsid %d", pgid,
myself->sid, tc->getsid ());
if (myself->sid != tc->getsid ())
{
set_errno (EPERM);
return -1;
}
tc->setpgid (pgid);
return 0;
}
int
fhandler_termios::tcgetpgrp ()
{
return tc->pgid;
}
void
fhandler_termios::set_ctty (int ttynum, int flags)
{
if ((myself->ctty < 0 || myself->ctty == ttynum) && !(flags & O_NOCTTY))
{
myself->ctty = ttynum;
syscall_printf ("attached tty%d sid %d, pid %d, tty->pgid %d, tty->sid %d",
ttynum, myself->sid, myself->pid, tc->pgid, tc->getsid ());
* include/cygwin/version.h: Bump DLL minor version number to 5 due to all of the changes below. Redefine process structure to avoid a fixed size table. Redefine pinfo/_pinfo classes. Use these throughout. * dcrt0.cc (dll_crt0_1): Accomodate set_myself argument change. (__api_fatal): Accomodate _pinfo::record_death argument change. * exceptions.cc (really_exit): Ditto. (sig_handle_tty_stop): Use pinfo constructor to access process info. (events_init): Don't create pinfo_mutex since it is no longer required. * external.cc (fillout_pinfo): Use winpids class to iterate over all system pids. (cygwin_internal): lock_pinfo_for_update and unlock_pinfo are now noops. * fhandler_termios.cc (fhandler_termios::set_ctty): Use pinfo constructor to access process info. * fork.cc (fork): Reorganize to initialize child info after the child has started since that is when we know the child's winpid, which is necessary to allocate the pinfo shared memory. * mmap.cc (recreate_mmaps_after_fork): Change arg type to _pinfo. * pinfo.cc: Rename pinfo methods to _pinfo throughout. Eliminate pinfo_list stuff. (set_myself): Accept a pid argument now. Call pinfo initializer to initialize myself. Detect when this is an "execed" process and create an "indirect" pid block. (pinfo_init): Accomodate set_myself arg change. (procinfo): Remove. (pinfo::lock_pinfo): Remove. (pinfo::unlock_pinfo): Remove. (pinfo::init): New method. Allocates shared memory space for process pinfo structure. (pinfo::record_death): Don't call locking functions. (cygwin_winpid_to_pid): Simplify by using new pinfo constructor. (EnumProcessesW95): New function for iterating over processes on Windows 95. (winpids::winpids): New constructor for winpids class. Sets up a list of process ids. (enum_init): Initialize w95/wnt pid enumerators. * shared.cc (shared-info::initialize): Remove pid initialization. * shared.h: Move pinfo stuff into pinfo.h. (class shared_info): Remove pinfo_list element. * signal.cc (kill_worker): Use pinfo constructor to access process info. (kill_pgrp): Ditto. Use winpids methods to access list of processes. * sigproc.cc: Throughout, modify to use _pinfo where appropriate. (proc_exists (pid_t)): New function. Determines if a process exists based on the pid. (proc_exists (_pinfo *p): Use new proc_exists function above. (proc_subproc): Copy pinfo stuff around rather than _pinfo pointers. Try to be careful about releasing shared memory when we don't need it anymore. Remove pinfo locks. (remove_zombies): Remove pinfo memory when zombie is going away. * sigproc.h: Reflect _pinfo/pinfo changes in sigproc.cc. * spawn.cc (spawn_guts): Eliminate pinfo *child argument. Reorganize to only initialize child pinfo after process has been started and we know the windows pid. (_spawnve): Reflect spawn_guts changes. * syscalls.cc (setpgid): Use pinfo constructor to access process info. (getpgid): Ditto. (internal_getlogin): Use _pinfo. * winsup.h: Eliminate pinfo_mutex. Eliminate spawn_guts declaration since it is static now. Reflect set_myself argument change. * include/sys/cygwin.h: Add some PID_* enums to accomodate new pinfo stuff. * include/cygwin/version.h: Update minor version for cygdrive changes below.
2000-07-29 18:24:59 +02:00
pinfo p (tc->getsid ());
2000-02-17 20:38:33 +01:00
if (myself->sid == myself->pid &&
(p == myself || !proc_exists (p)))
{
paranoid_printf ("resetting tty%d sid. Was %d, now %d. pgid was %d, now %d.",
ttynum, tc->getsid(), myself->sid, tc->getpgid (), myself->pgid);
/* We are the session leader */
tc->setsid (myself->sid);
tc->setpgid (myself->pgid);
}
else
myself->sid = tc->getsid ();
if (tc->getpgid () == 0)
tc->setpgid (myself->pgid);
}
}
int
fhandler_termios::bg_check (int sig)
2000-02-17 20:38:33 +01:00
{
if (!myself->pgid || tc->getpgid () == myself->pgid ||
myself->ctty != tc->ntty ||
((sig == SIGTTOU) && !(tc->ti.c_lflag & TOSTOP)))
return 1;
if (sig < 0)
sig = -sig;
termios_printf("bg I/O pgid %d, tpgid %d, ctty %d",
myself->pgid, tc->getpgid (), myself->ctty);
if (tc->getsid () == 0)
{
/* The pty has been closed by the master. Return an EOF
indication. FIXME: There is nothing to stop somebody
from reallocating this pty. I think this is the case
which is handled by unlockpt on a Unix system. */
termios_printf ("closed by master");
return 0;
}
/* If the process group is no more or if process is ignoring or blocks 'sig',
return with error */
int pgid_gone = !pid_exists (myself->pgid);
2000-02-17 20:38:33 +01:00
int sigs_ignored =
((void *) myself->getsig(sig).sa_handler == (void *) SIG_IGN) ||
(myself->getsigmask () & SIGTOMASK (sig));
if (pgid_gone)
goto setEIO;
else if (!sigs_ignored)
/* nothing */;
else if (sig == SIGTTOU)
return 1; /* Just allow the output */
else
goto setEIO; /* This is an output error */
_raise (sig);
return 1;
setEIO:
set_errno (EIO);
return -1;
}
#define set_input_done(x) input_done = input_done || (x)
int
fhandler_termios::line_edit (const char *rptr, int nread, int always_accept)
{
char c;
int input_done = 0;
int iscanon = tc->ti.c_lflag & ICANON;
while (nread-- > 0)
{
c = *rptr++;
termios_printf ("char %c", c);
/* Check for special chars */
if (c == '\r')
{
if (tc->ti.c_iflag & IGNCR)
continue;
if (tc->ti.c_iflag & ICRNL)
{
c = '\n';
set_input_done (iscanon);
}
}
else if (c == '\n')
{
if (tc->ti.c_iflag & INLCR)
c = '\r';
else
set_input_done (iscanon);
}
if (tc->ti.c_iflag & ISTRIP)
c &= 0x7f;
if (tc->ti.c_lflag & ISIG)
{
int sig;
if (c == tc->ti.c_cc[VINTR])
sig = SIGINT;
else if (c == tc->ti.c_cc[VQUIT])
sig = SIGQUIT;
else if (c == tc->ti.c_cc[VSUSP])
sig = SIGTSTP;
else
goto not_a_sig;
termios_printf ("got interrupt %d, sending signal %d", c, sig);
kill_pgrp (tc->getpgid (), sig);
tc->ti.c_lflag &= ~FLUSHO;
goto restart_output;
}
not_a_sig:
if (tc->ti.c_iflag & IXON)
{
if (c == tc->ti.c_cc[VSTOP])
{
if (!tc->OutputStopped)
{
tc->OutputStopped = 1;
acquire_output_mutex (INFINITE);
}
2000-02-17 20:38:33 +01:00
continue;
}
else if (c == tc->ti.c_cc[VSTART])
{
restart_output:
tc->OutputStopped = 0;
release_output_mutex ();
2000-02-17 20:38:33 +01:00
continue;
}
else if ((tc->ti.c_iflag & IXANY) && tc->OutputStopped)
goto restart_output;
}
if (tc->ti.c_lflag & IEXTEN && c == tc->ti.c_cc[VDISCARD])
{
tc->ti.c_lflag ^= FLUSHO;
continue;
}
if (!iscanon)
/* nothing */;
else if (c == tc->ti.c_cc[VERASE])
{
if (eat_readahead (1))
doecho ("\b \b", 3);
continue;
}
else if (c == tc->ti.c_cc[VWERASE])
{
int ch;
do
if (!eat_readahead (1))
break;
else
doecho ("\b \b", 3);
while ((ch = peek_readahead (1)) >= 0 && !isspace (ch));
continue;
}
else if (c == tc->ti.c_cc[VKILL])
{
int nchars = eat_readahead (-1);
while (nchars--)
doecho ("\b \b", 3);
continue;
}
else if (c == tc->ti.c_cc[VREPRINT])
{
doecho ("\n\r", 2);
doecho (rabuf, ralen);
continue;
}
else if (c == tc->ti.c_cc[VEOF])
{
termios_printf ("EOF");
input_done = 1;
continue;
}
else if (c == tc->ti.c_cc[VEOL] ||
c == tc->ti.c_cc[VEOL2] ||
c == '\n')
{
set_input_done (1);
termios_printf ("EOL");
}
if (tc->ti.c_iflag & IUCLC && isupper (c))
c = cyg_tolower (c);
2000-02-17 20:38:33 +01:00
if (tc->ti.c_lflag & ECHO)
doecho (&c, 1);
put_readahead (c);
}
if (!iscanon || always_accept)
set_input_done (ralen > 0);
if (input_done)
(void) accept_input ();
return input_done;
}