Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions
ptystream.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ptys

Defines

#define PTYNAMELEN   10 /* Length of PTY/TTY device name: /dev/pty?? */

Functions

int pty_create (struct ptys *ptyp, char *const argv[], int rows, int cols, int x_pixels, int y_pixels, int errfd, int noblock, int noecho, int noexport, int debug)
int pty_resize (struct ptys *ptyp, int rows, int cols, int xpix, int ypix)
int pty_close (struct ptys *ptyp)

Class Documentation

struct ptys

Definition at line 51 of file ptystream.h.

Collaboration diagram for ptys:
Class Members
int debug
int errpipeFD
long pid
char ptydev
int ptyFD
char ttydev

Define Documentation

#define PTYNAMELEN   10 /* Length of PTY/TTY device name: /dev/pty?? */

Definition at line 49 of file ptystream.h.


Function Documentation

int pty_close ( struct ptys ptyp)

Definition at line 237 of file ptystream.c.

{
  if (!ptyp) {
    pty_error("pty_close: NULL value for PTY structure", NULL);
    return -1;
  }

  kill(ptyp->pid, SIGKILL);
  ptyp->pid = 0;

  close(ptyp->ptyFD);
  ptyp->ptyFD = -1;

  if (ptyp->errpipeFD >= 0) {
    close(ptyp->errpipeFD);
    ptyp->errpipeFD = -1;
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pty_create ( struct ptys ptyp,
char *const  argv[],
int  rows,
int  cols,
int  x_pixels,
int  y_pixels,
int  errfd,
int  noblock,
int  noecho,
int  noexport,
int  debug 
)

Definition at line 112 of file ptystream.c.

{
  pid_t child_pid;
  int errfd2;

  if (!ptyp) {
    pty_error("pty_create: NULL value for PTY structure", NULL);
    return -1;
  }

  /* Set debug flag */
  ptyp->debug = debug;

#ifdef DEBUG_LTERM
  if (ptyp->debug)
    fprintf(stderr, "00-pty_create: errfd=%d, noblock=%d, noecho=%d, noexport=%d\n",
                     errfd, noblock, noecho, noexport);
#endif

  /* Open PTY */
  if (openPTY(ptyp, noblock) == -1) return -1;

#ifndef BSDFAMILY
  /* Set default TTY size */
  if (pty_resize(ptyp, rows, cols, x_pixels, y_pixels) != 0)
    return -1;
#endif

  if (errfd >= -1) {
    /* No STDERR pipe */
    ptyp->errpipeFD = -1;
    errfd2 = errfd;

  } else {
    /* Create pipe to handle STDERR output */
    int pipeFD[2];

    if (pipe(pipeFD) == -1) {
      pty_error("pty_create: STDERR pipe creation failed", NULL);
      return -1;
    }

    /* Copy pipe file descriptors */
    ptyp->errpipeFD = pipeFD[0];
    errfd2          = pipeFD[1];
  }

  /* Fork a child process (VFORK) */
  child_pid = vfork();
  if (child_pid < 0) {
    pty_error("pty_create: vfork failed", NULL);
    return -1;
  }

  ptyp->pid = child_pid;

#ifdef DEBUG_LTERM
  if (ptyp->debug)
    fprintf(stderr, "00-pty_create: Fork child pid = %d, initially attached to %s\n",
                     child_pid, ttyname(0));
#endif

  if (child_pid == 0) {
    /* Child process */

    /* Attach child to slave TTY */
    if (attachToTTY(ptyp, errfd2, noecho) == -1) return -1;

#ifdef BSDFAMILY
    /* Set default TTY size */
    if (pty_resize(NULL, rows, cols, x_pixels, y_pixels) != 0)
      return -1;
#endif

    /* Set default signal handling */
    signal(SIGINT, SIG_DFL);
    signal(SIGQUIT, SIG_DFL);
    signal(SIGCHLD, SIG_DFL);

    /* Set ignore signal handling */
    signal(SIGTSTP, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGTTOU, SIG_IGN);

    if (argv != NULL) {
      /* Execute specified command with arguments */
      if (noexport)
        execve(argv[0], argv, NULL);
      else
        execvp(argv[0], argv);

      pty_error("Error in executing command ", argv[0]);
      return -1;

    } else {
      /* Execute $SHELL or /bin/sh by default */
      char *shell = (char *) getenv("SHELL");

      if ((shell == NULL) || (*shell == '\0'))
        shell = "/bin/sh";

      if (noexport)
        execle(shell, shell, NULL, NULL);
      else
        execlp(shell, shell, NULL);

      pty_error("pty_create: Error in executing command ", shell);
      return -1;
    }

  }

  if (errfd < -1) {
    /* Close write end of STDERR pipe in parent process */
    close(errfd2);
  }

  /* Return from parent */
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pty_resize ( struct ptys ptyp,
int  rows,
int  cols,
int  xpix,
int  ypix 
)

Definition at line 262 of file ptystream.c.

{
  struct winsize wsize;
  int fd = ptyp ? ptyp->ptyFD : 0;

  /* Set TTY window size */
  wsize.ws_row = (unsigned short) rows;
  wsize.ws_col = (unsigned short) cols;
  wsize.ws_xpixel = (unsigned short) xpix;
  wsize.ws_ypixel = (unsigned short) ypix;

  if (ioctl(fd, TIOCSWINSZ, &wsize ) == -1) {
    pty_error("pty_resize: Failed to set TTY window size", NULL);
    return -1;
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: