summaryrefslogtreecommitdiffstats
path: root/kdecore/kpty.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kdecore/kpty.cpp')
-rw-r--r--kdecore/kpty.cpp192
1 files changed, 114 insertions, 78 deletions
diff --git a/kdecore/kpty.cpp b/kdecore/kpty.cpp
index 27cc3c51b..af83e9eab 100644
--- a/kdecore/kpty.cpp
+++ b/kdecore/kpty.cpp
@@ -95,24 +95,24 @@ extern "C" {
# endif
#endif
-#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
+#if defined(HAVE_TCGETATTR)
+# define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
+#elif defined(TIOCGETA)
# define _tcgetattr(fd, ttmode) ioctl(fd, TIOCGETA, (char *)ttmode)
+#elif defined(TCGETS)
+# define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
#else
-# if defined(_HPUX_SOURCE) || defined(__Lynx__) || defined (__CYGWIN__)
-# define _tcgetattr(fd, ttmode) tcgetattr(fd, ttmode)
-# else
-# define _tcgetattr(fd, ttmode) ioctl(fd, TCGETS, (char *)ttmode)
-# endif
+# error
#endif
-#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__bsdi__) || defined(__APPLE__) || defined (__DragonFly__)
+#if defined(HAVE_TCSETATTR) && defined(TCSANOW)
+# define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
+#elif defined(TIOCSETA)
# define _tcsetattr(fd, ttmode) ioctl(fd, TIOCSETA, (char *)ttmode)
+#elif defined(TCSETS)
+# define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
#else
-# if defined(_HPUX_SOURCE) || defined(__CYGWIN__)
-# define _tcsetattr(fd, ttmode) tcsetattr(fd, TCSANOW, ttmode)
-# else
-# define _tcsetattr(fd, ttmode) ioctl(fd, TCSETS, (char *)ttmode)
-# endif
+# error
#endif
#if defined (_HPUX_SOURCE)
@@ -201,87 +201,37 @@ KPty::~KPty()
delete d;
}
-bool KPty::open()
+bool KPty::setPty(int pty_master)
{
- if (d->masterFd >= 0)
- return true;
+ kdWarning(175)
+ << "setPty()" << endl;
+ // a pty is already open
+ if(d->masterFd >= 0) {
+ kdWarning(175)
+ << "d->masterFd >= 0" << endl;
+ return false;
+ }
+ d->masterFd = pty_master;
+ return _attachPty(pty_master);
+}
+bool KPty::_attachPty(int pty_master)
+{
QCString ptyName;
- // Find a master pty that we can open ////////////////////////////////
-
- // Because not all the pty animals are created equal, they want to
- // be opened by several different methods.
-
- // We try, as we know them, one by one.
-
+ kdWarning(175)
+ << "_attachPty() " << pty_master << endl;
#if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
-#ifdef _AIX
- d->masterFd = ::open("/dev/ptc",O_RDWR);
-#else
- d->masterFd = ::open("/dev/ptmx",O_RDWR);
-#endif
- if (d->masterFd >= 0)
- {
char *ptsn = ptsname(d->masterFd);
if (ptsn) {
grantpt(d->masterFd);
d->ttyName = ptsn;
- goto gotpty;
} else {
::close(d->masterFd);
d->masterFd = -1;
}
- }
#endif
- // Linux device names, FIXME: Trouble on other systems?
- for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
- {
- for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
- {
- ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
- d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
-
- d->masterFd = ::open(ptyName.data(), O_RDWR);
- if (d->masterFd >= 0)
- {
-#ifdef __sun
- /* Need to check the process group of the pty.
- * If it exists, then the slave pty is in use,
- * and we need to get another one.
- */
- int pgrp_rtn;
- if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
- ::close(d->masterFd);
- d->masterFd = -1;
- continue;
- }
-#endif /* sun */
- if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
- {
- if (!geteuid())
- {
- struct group* p = getgrnam(TTY_GROUP);
- if (!p)
- p = getgrnam("wheel");
- gid_t gid = p ? p->gr_gid : getgid ();
-
- chown(d->ttyName.data(), getuid(), gid);
- chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
- }
- goto gotpty;
- }
- ::close(d->masterFd);
- d->masterFd = -1;
- }
- }
- }
-
- kdWarning(175) << "Can't open a pseudo teletype" << endl;
- return false;
-
- gotpty:
struct stat st;
if (stat(d->ttyName.data(), &st))
return false; // this just cannot happen ... *cough* Yeah right, I just
@@ -354,6 +304,92 @@ bool KPty::open()
return true;
}
+bool KPty::open()
+{
+ if (d->masterFd >= 0)
+ return true;
+
+ QCString ptyName;
+
+ // Find a master pty that we can open ////////////////////////////////
+
+ // Because not all the pty animals are created equal, they want to
+ // be opened by several different methods.
+
+ // We try, as we know them, one by one.
+
+#if defined(HAVE_PTSNAME) && defined(HAVE_GRANTPT)
+#ifdef _AIX
+ d->masterFd = ::open("/dev/ptc",O_RDWR);
+#else
+ d->masterFd = ::open("/dev/ptmx",O_RDWR);
+#endif
+ if (d->masterFd >= 0)
+ {
+ char *ptsn = ptsname(d->masterFd);
+ if (ptsn) {
+ grantpt(d->masterFd);
+ d->ttyName = ptsn;
+ goto gotpty;
+ } else {
+ ::close(d->masterFd);
+ d->masterFd = -1;
+ }
+ }
+#endif
+
+ // Linux device names, FIXME: Trouble on other systems?
+ for (const char* s3 = "pqrstuvwxyzabcdefghijklmno"; *s3; s3++)
+ {
+ for (const char* s4 = "0123456789abcdefghijklmnopqrstuvwxyz"; *s4; s4++)
+ {
+ ptyName.sprintf("/dev/pty%c%c", *s3, *s4);
+ d->ttyName.sprintf("/dev/tty%c%c", *s3, *s4);
+
+ d->masterFd = ::open(ptyName.data(), O_RDWR);
+ if (d->masterFd >= 0)
+ {
+#ifdef __sun
+ /* Need to check the process group of the pty.
+ * If it exists, then the slave pty is in use,
+ * and we need to get another one.
+ */
+ int pgrp_rtn;
+ if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) {
+ ::close(d->masterFd);
+ d->masterFd = -1;
+ continue;
+ }
+#endif /* sun */
+ if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits
+ {
+ if (!geteuid())
+ {
+ struct group* p = getgrnam(TTY_GROUP);
+ if (!p)
+ p = getgrnam("wheel");
+ gid_t gid = p ? p->gr_gid : getgid ();
+
+ chown(d->ttyName.data(), getuid(), gid);
+ chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP);
+ }
+ goto gotpty;
+ }
+ ::close(d->masterFd);
+ d->masterFd = -1;
+ }
+ }
+ }
+
+ kdWarning(175) << "Can't open a pseudo teletype" << endl;
+ return false;
+
+ gotpty:
+ return _attachPty(d->masterFd);
+
+ return true;
+}
+
void KPty::close()
{
if (d->masterFd < 0)