OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [tags/] [gdb/] [gdb-6.8/] [gdb-6.8.openrisc-2.1/] [readline/] [examples/] [rlfe/] [pty.c] - Diff between revs 24 and 33

Only display areas with differences | Details | Blame | View Log

Rev 24 Rev 33
/* Copyright (c) 1993-2002
/* Copyright (c) 1993-2002
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 * Copyright (c) 1987 Oliver Laumann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * any later version.
 *
 *
 * This program is distributed in the hope that it will be useful,
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * GNU General Public License for more details.
 *
 *
 * You should have received a copy of the GNU General Public License
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, write to the
 * along with this program (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *
 ****************************************************************
 ****************************************************************
 */
 */
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
#include <signal.h>
#include <signal.h>
 
 
#include "config.h"
#include "config.h"
#include "screen.h"
#include "screen.h"
 
 
#ifndef sun
#ifndef sun
# include <sys/ioctl.h>
# include <sys/ioctl.h>
#endif
#endif
 
 
/* for solaris 2.1, Unixware (SVR4.2) and possibly others */
/* for solaris 2.1, Unixware (SVR4.2) and possibly others */
#ifdef HAVE_SVR4_PTYS
#ifdef HAVE_SVR4_PTYS
# include <sys/stropts.h>
# include <sys/stropts.h>
#endif
#endif
 
 
#if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
#if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
# include <sys/ttold.h>
# include <sys/ttold.h>
#endif
#endif
 
 
#ifdef ISC
#ifdef ISC
# include <sys/tty.h>
# include <sys/tty.h>
# include <sys/sioctl.h>
# include <sys/sioctl.h>
# include <sys/pty.h>
# include <sys/pty.h>
#endif
#endif
 
 
#ifdef sgi
#ifdef sgi
# include <sys/sysmacros.h>
# include <sys/sysmacros.h>
#endif /* sgi */
#endif /* sgi */
 
 
#include "extern.h"
#include "extern.h"
 
 
/*
/*
 * if no PTYRANGE[01] is in the config file, we pick a default
 * if no PTYRANGE[01] is in the config file, we pick a default
 */
 */
#ifndef PTYRANGE0
#ifndef PTYRANGE0
# define PTYRANGE0 "qpr"
# define PTYRANGE0 "qpr"
#endif
#endif
#ifndef PTYRANGE1
#ifndef PTYRANGE1
# define PTYRANGE1 "0123456789abcdef"
# define PTYRANGE1 "0123456789abcdef"
#endif
#endif
 
 
/* SVR4 pseudo ttys don't seem to work with SCO-5 */
/* SVR4 pseudo ttys don't seem to work with SCO-5 */
#ifdef M_UNIX
#ifdef M_UNIX
# undef HAVE_SVR4_PTYS
# undef HAVE_SVR4_PTYS
#endif
#endif
 
 
extern int eff_uid;
extern int eff_uid;
 
 
/* used for opening a new pty-pair: */
/* used for opening a new pty-pair: */
static char PtyName[32], TtyName[32];
static char PtyName[32], TtyName[32];
 
 
#if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
#if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
# ifdef hpux
# ifdef hpux
static char PtyProto[] = "/dev/ptym/ptyXY";
static char PtyProto[] = "/dev/ptym/ptyXY";
static char TtyProto[] = "/dev/pty/ttyXY";
static char TtyProto[] = "/dev/pty/ttyXY";
# else
# else
#  ifdef M_UNIX
#  ifdef M_UNIX
static char PtyProto[] = "/dev/ptypXY";
static char PtyProto[] = "/dev/ptypXY";
static char TtyProto[] = "/dev/ttypXY";
static char TtyProto[] = "/dev/ttypXY";
#  else
#  else
static char PtyProto[] = "/dev/ptyXY";
static char PtyProto[] = "/dev/ptyXY";
static char TtyProto[] = "/dev/ttyXY";
static char TtyProto[] = "/dev/ttyXY";
#  endif
#  endif
# endif /* hpux */
# endif /* hpux */
#endif
#endif
 
 
static void initmaster __P((int));
static void initmaster __P((int));
 
 
#if defined(sun)
#if defined(sun)
/* sun's utmp_update program opens the salve side, thus corrupting
/* sun's utmp_update program opens the salve side, thus corrupting
 */
 */
int pty_preopen = 1;
int pty_preopen = 1;
#else
#else
int pty_preopen = 0;
int pty_preopen = 0;
#endif
#endif
 
 
/*
/*
 *  Open all ptys with O_NOCTTY, just to be on the safe side
 *  Open all ptys with O_NOCTTY, just to be on the safe side
 *  (RISCos mips breaks otherwise)
 *  (RISCos mips breaks otherwise)
 */
 */
#ifndef O_NOCTTY
#ifndef O_NOCTTY
# define O_NOCTTY 0
# define O_NOCTTY 0
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
static void
static void
initmaster(f)
initmaster(f)
int f;
int f;
{
{
#ifdef POSIX
#ifdef POSIX
  tcflush(f, TCIOFLUSH);
  tcflush(f, TCIOFLUSH);
#else
#else
# ifdef TIOCFLUSH
# ifdef TIOCFLUSH
  (void) ioctl(f, TIOCFLUSH, (char *) 0);
  (void) ioctl(f, TIOCFLUSH, (char *) 0);
# endif
# endif
#endif
#endif
#ifdef LOCKPTY
#ifdef LOCKPTY
  (void) ioctl(f, TIOCEXCL, (char *) 0);
  (void) ioctl(f, TIOCEXCL, (char *) 0);
#endif
#endif
}
}
 
 
void
void
InitPTY(f)
InitPTY(f)
int f;
int f;
{
{
  if (f < 0)
  if (f < 0)
    return;
    return;
#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
  if (ioctl(f, I_PUSH, "ptem"))
  if (ioctl(f, I_PUSH, "ptem"))
    Panic(errno, "InitPTY: cannot I_PUSH ptem");
    Panic(errno, "InitPTY: cannot I_PUSH ptem");
  if (ioctl(f, I_PUSH, "ldterm"))
  if (ioctl(f, I_PUSH, "ldterm"))
    Panic(errno, "InitPTY: cannot I_PUSH ldterm");
    Panic(errno, "InitPTY: cannot I_PUSH ldterm");
# ifdef sun
# ifdef sun
  if (ioctl(f, I_PUSH, "ttcompat"))
  if (ioctl(f, I_PUSH, "ttcompat"))
    Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
    Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
# endif
# endif
#endif
#endif
}
}
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(OSX) && !defined(PTY_DONE)
#if defined(OSX) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  register int f;
  register int f;
  if ((f = open_controlling_pty(TtyName)) < 0)
  if ((f = open_controlling_pty(TtyName)) < 0)
    return -1;
    return -1;
  initmaster(f);
  initmaster(f);
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
#if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  char *m, *s;
  char *m, *s;
  register int f;
  register int f;
 
 
  if ((f = getpseudotty(&s, &m)) < 0)
  if ((f = getpseudotty(&s, &m)) < 0)
    return -1;
    return -1;
#ifdef _SEQUENT_
#ifdef _SEQUENT_
  fvhangup(s);
  fvhangup(s);
#endif
#endif
  strncpy(PtyName, m, sizeof(PtyName));
  strncpy(PtyName, m, sizeof(PtyName));
  strncpy(TtyName, s, sizeof(TtyName));
  strncpy(TtyName, s, sizeof(TtyName));
  initmaster(f);
  initmaster(f);
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(__sgi) && !defined(PTY_DONE)
#if defined(__sgi) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  int f;
  int f;
  char *name, *_getpty();
  char *name, *_getpty();
  sigret_t (*sigcld)__P(SIGPROTOARG);
  sigret_t (*sigcld)__P(SIGPROTOARG);
 
 
  /*
  /*
   * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
   * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
   * exec() /usr/adm/mkpts
   * exec() /usr/adm/mkpts
   */
   */
  sigcld = signal(SIGCHLD, SIG_DFL);
  sigcld = signal(SIGCHLD, SIG_DFL);
  name = _getpty(&f, O_RDWR | O_NONBLOCK, 0600, 0);
  name = _getpty(&f, O_RDWR | O_NONBLOCK, 0600, 0);
  signal(SIGCHLD, sigcld);
  signal(SIGCHLD, sigcld);
 
 
  if (name == 0)
  if (name == 0)
    return -1;
    return -1;
  initmaster(f);
  initmaster(f);
  *ttyn = name;
  *ttyn = name;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  register int f;
  register int f;
  struct stat buf;
  struct stat buf;
 
 
  strcpy(PtyName, "/dev/ptc");
  strcpy(PtyName, "/dev/ptc");
  if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
  if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
    return -1;
    return -1;
  if (fstat(f, &buf) < 0)
  if (fstat(f, &buf) < 0)
    {
    {
      close(f);
      close(f);
      return -1;
      return -1;
    }
    }
  sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
  sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
  initmaster(f);
  initmaster(f);
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  register int f;
  register int f;
  char *m, *ptsname();
  char *m, *ptsname();
  int unlockpt __P((int)), grantpt __P((int));
  int unlockpt __P((int)), grantpt __P((int));
#if defined(HAVE_GETPT) && defined(linux)
#if defined(HAVE_GETPT) && defined(linux)
  int getpt __P((void));
  int getpt __P((void));
#endif
#endif
  sigret_t (*sigcld)__P(SIGPROTOARG);
  sigret_t (*sigcld)__P(SIGPROTOARG);
 
 
  strcpy(PtyName, "/dev/ptmx");
  strcpy(PtyName, "/dev/ptmx");
#if defined(HAVE_GETPT) && defined(linux)
#if defined(HAVE_GETPT) && defined(linux)
  if ((f = getpt()) == -1)
  if ((f = getpt()) == -1)
#else
#else
  if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
  if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
#endif
#endif
    return -1;
    return -1;
 
 
  /*
  /*
   * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
   * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
   * exec()s pt_chmod
   * exec()s pt_chmod
   */
   */
  sigcld = signal(SIGCHLD, SIG_DFL);
  sigcld = signal(SIGCHLD, SIG_DFL);
  if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
  if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
    {
    {
      signal(SIGCHLD, sigcld);
      signal(SIGCHLD, sigcld);
      close(f);
      close(f);
      return -1;
      return -1;
    }
    }
  signal(SIGCHLD, sigcld);
  signal(SIGCHLD, sigcld);
  strncpy(TtyName, m, sizeof(TtyName));
  strncpy(TtyName, m, sizeof(TtyName));
  initmaster(f);
  initmaster(f);
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
 
 
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  register int f;
  register int f;
 
 
  /* a dumb looking loop replaced by mycrofts code: */
  /* a dumb looking loop replaced by mycrofts code: */
  strcpy (PtyName, "/dev/ptc");
  strcpy (PtyName, "/dev/ptc");
  if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
  if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
    return -1;
    return -1;
  strncpy(TtyName, ttyname(f), sizeof(TtyName));
  strncpy(TtyName, ttyname(f), sizeof(TtyName));
  if (eff_uid && access(TtyName, R_OK | W_OK))
  if (eff_uid && access(TtyName, R_OK | W_OK))
    {
    {
      close(f);
      close(f);
      return -1;
      return -1;
    }
    }
  initmaster(f);
  initmaster(f);
# ifdef _IBMR2
# ifdef _IBMR2
  pty_preopen = 1;
  pty_preopen = 1;
# endif
# endif
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
#if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
#define PTY_DONE
#define PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  int f, s;
  int f, s;
  if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
  if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
    return -1;
    return -1;
  close(s);
  close(s);
  initmaster(f);
  initmaster(f);
  pty_preopen = 1;
  pty_preopen = 1;
  *ttyn = TtyName;
  *ttyn = TtyName;
  return f;
  return f;
}
}
#endif
#endif
 
 
/***************************************************************/
/***************************************************************/
 
 
#ifndef PTY_DONE
#ifndef PTY_DONE
int
int
OpenPTY(ttyn)
OpenPTY(ttyn)
char **ttyn;
char **ttyn;
{
{
  register char *p, *q, *l, *d;
  register char *p, *q, *l, *d;
  register int f;
  register int f;
 
 
  debug("OpenPTY: Using BSD style ptys.\n");
  debug("OpenPTY: Using BSD style ptys.\n");
  strcpy(PtyName, PtyProto);
  strcpy(PtyName, PtyProto);
  strcpy(TtyName, TtyProto);
  strcpy(TtyName, TtyProto);
  for (p = PtyName; *p != 'X'; p++)
  for (p = PtyName; *p != 'X'; p++)
    ;
    ;
  for (q = TtyName; *q != 'X'; q++)
  for (q = TtyName; *q != 'X'; q++)
    ;
    ;
  for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
  for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
    {
    {
      for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
      for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
        {
        {
          debug1("OpenPTY tries '%s'\n", PtyName);
          debug1("OpenPTY tries '%s'\n", PtyName);
          if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
          if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
            continue;
            continue;
          q[0] = *l;
          q[0] = *l;
          q[1] = *d;
          q[1] = *d;
          if (eff_uid && access(TtyName, R_OK | W_OK))
          if (eff_uid && access(TtyName, R_OK | W_OK))
            {
            {
              close(f);
              close(f);
              continue;
              continue;
            }
            }
#if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
#if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
          /* Hack to ensure that the slave side of the pty is
          /* Hack to ensure that the slave side of the pty is
           * unused. May not work in anything other than SunOS4.1
           * unused. May not work in anything other than SunOS4.1
           */
           */
            {
            {
              int pgrp;
              int pgrp;
 
 
              /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
              /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
              if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
              if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
                {
                {
                  close(f);
                  close(f);
                  continue;
                  continue;
                }
                }
            }
            }
#endif
#endif
          initmaster(f);
          initmaster(f);
          *ttyn = TtyName;
          *ttyn = TtyName;
          return f;
          return f;
        }
        }
    }
    }
  return -1;
  return -1;
}
}
#endif
#endif
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.