
#pragma once

#include <String.h>
#include <string.h>

typedef int bool;

//
// class PtyTty is a master_pty/slave_tty pair.
//
//
// A PtyTty will try to Open() a pty/tty pair on construction.
//
// The pty and tty are opened in read/write mode.
//
// Whenever a PtyTty Has_pair(),
//   the pty and tty file descriptors and device names can be observed.
//   
// A PtyTty can always be asked to Close() its pair.
//
// The destructor will Close() the pair.
//

class PtyTty
{
  const int length_of_dev_name =10;

  bool have_pair;

  int  the_pty;
  int  the_tty;
  char ptydev [ length_of_dev_name +1 ];
  char ttydev [ length_of_dev_name +1 ];

  bool get_pair();
  bool try_to_open_dev_pair();

  bool OK_devs();

 public:

  PtyTty()  { Open(); };
  ~PtyTty() { Close(); };

  void Open();
  void Open(String& device_id_chars);
  void Close();

  bool Has_pair();

  // File descriptors.
  int  master_pty();
  int   slave_tty();

  // Device names.
  String  pty_dev();
  String  tty_dev();

  operator= (PtyTty& pair);

  void  make_slave_tty_the_controlling_tty();
};

inline bool PtyTty::Has_pair () {return have_pair;}

inline int PtyTty::master_pty()
{
  if(have_pair) return the_pty;
  else          return -1;
}
