View of xos/usr/xsh/tty.c


XOS | Parent Directory | View | Download

/* Terminal de controle */
/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   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
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include "tty.h"
 
#include <signal.h>
#include <unistd.h>
#include <termios.h>
 
int tty;
 
static int tty_saved;
static struct termios saved_termios;
 
/* Groupe d'avant-plan */
 
void wait_foreground()
{
  pid_t pgid, tty_pgid;
  void (*func)(int);
 
  pgid = getpgrp();
  while ((tty_pgid = tcgetpgrp(tty)) != -1 && tty_pgid != pgid) {
    func = signal(SIGTTIN, SIG_DFL);
    kill(0, SIGTTIN);
    signal(SIGTTIN, func);
  }
}
 
void set_tty_pgid(pid_t pgid)
{
  void (*func)(int);
 
  func = signal(SIGTTOU, SIG_IGN);
  tcsetpgrp(tty, pgid);
  signal(SIGTTOU, func);
}
 
/* Attributs */
 
void save_tty_attributes()
{
  tty_saved = 0;
  if (tcgetattr(tty, &saved_termios) == -1)
    return;
  tty_saved = 1;
}
 
void restore_tty_attributes()
{
  if (tty_saved)
    tcsetattr(tty, TCSANOW, &saved_termios);
}
 
/* Signaux */
 
void disable_tty_signals()
{
  struct termios termios;
 
  if (tcgetattr(tty, &termios) == -1)
    return;
  termios.c_lflag &= ~ISIG;
  tcsetattr(tty, TCSANOW, &termios);
}
 
void enable_tty_signals()
{
  struct termios termios;
 
  if (tcgetattr(tty, &termios) == -1)
    return;
  termios.c_lflag |= ISIG;
  tcsetattr(tty, TCSANOW, &termios);
}