OSDN Git Service

* fhandler.h (fhandler_tty_slave): Declare new methods.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / fhandler.h
index acecb63..90ded24 100644 (file)
@@ -29,7 +29,7 @@ details. */
      fhandler_dev_tape    (fhandler_tape.cc)
 
      fhandler_pipe
-     fhandler_socket      (net.cc)
+     fhandler_socket      (fhandler_socket.cc)
 
      fhandler_tty_slave   (tty.cc)
      fhandler_pty_master  (tty.cc)
@@ -39,6 +39,12 @@ details. */
 
      fhandler_windows    Windows messages I/O (fhandler_windows.cc)
 
+     fhandler_dev_random  /dev/[u]random implementation (fhandler_random.cc)
+
+     fhandler_dev_mem     /dev/mem implementation (fhandler_mem.cc)
+
+     fhandler_dev_clipboard    /dev/clipboard implementation (fhandler_clipboard.cc)
+
      fhandler_proc       Interesting possibility, not implemented yet
 */
 
@@ -91,8 +97,11 @@ enum
   FH_TAPE    = 0x00000012,     /* is a tape */
   FH_NULL    = 0x00000013,     /* is the null device */
   FH_ZERO    = 0x00000014,     /* is the zero device */
+  FH_RANDOM  = 0x00000015,     /* is a random device */
+  FH_MEM     = 0x00000016,     /* is a mem device */
+  FH_CLIPBOARD = 0x00000017, /* is a clipbaord device */
 
-  FH_NDEV    = 0x00000015,     /* Maximum number of devices */
+  FH_NDEV    = 0x00000018,     /* Maximum number of devices */
   FH_DEVMASK = 0x00000fff,     /* devices live here */
   FH_BAD     = 0xffffffff
 };
@@ -106,12 +115,29 @@ enum
 #define FHSTATOFF      0
 
 extern const char *windows_device_names[];
+extern struct __cygwin_perfile *perfile_table;
 #define __fmode (*(user_data->fmode_ptr))
 
 class select_record;
 class path_conv;
 class fhandler_disk_file;
 
+enum bg_check_types
+{
+  bg_error = -1,
+  bg_eof = 0,
+  bg_ok = 1,
+  bg_signalled = 2
+};
+
+enum executable_states
+{
+  is_executable,
+  not_executable,
+  dont_care_if_executable,
+  dont_know_if_executable
+};
+
 class fhandler_base
 {
 private:
@@ -122,9 +148,6 @@ private:
   int access_;
   HANDLE io_handle;
 
-  int rpos_; /* Used in text reading */
-  int rsize_;
-
   unsigned long namehash_;     /* hashed filename, used as inode num */
 
   /* Full unix path name of this file */
@@ -142,20 +165,15 @@ protected:
   char *win32_path_name_;
 
 public:
-  void set_name (const char *unix, const char *win32 = NULL, int unit = 0);
+  void set_name (const char * unix_path, const char * win32_path = NULL,
+                int unit = 0);
 
-  virtual fhandler_base& operator =(fhandler_base &x)
-  {
-    memcpy (this, &x, sizeof *this);
-    unix_path_name_ = x.unix_path_name_ ? strdup (x.unix_path_name_) : NULL;
-    win32_path_name_ = x.win32_path_name_ ? strdup (x.win32_path_name_) : NULL;
-    return *this;
-  };
+  virtual fhandler_base& operator =(fhandler_base &x);
   fhandler_base (DWORD dev, const char *name = 0, int unit = 0);
   virtual ~fhandler_base ();
 
   /* Non-virtual simple accessor functions. */
-  void set_io_handle (HANDLE);
+  void set_io_handle (HANDLE x) { io_handle = x; }
 
   void set_cb (size_t size) { cb = size; }
   DWORD get_device () { return status & FH_DEVMASK; }
@@ -180,6 +198,8 @@ public:
   void set_w_binary (int b) { FHCONDSETF (b, WBINARY); FHSETF (WBINSET); }
   void set_r_binary (int b) { FHCONDSETF (b, RBINARY); FHSETF (RBINSET); }
 
+  int get_default_fmode (int flags);
+
   int get_r_no_interrupt () { return FHISSETF (NOEINTR); }
   void set_r_no_interrupt (int b) { FHCONDSETF (b, NOEINTR); }
 
@@ -193,7 +213,10 @@ public:
   void set_need_fork_fixup () { FHSETF (FFIXUP); }
 
   virtual void set_close_on_exec (int val);
-  virtual void fixup_after_fork (HANDLE parent);
+
+  virtual void fixup_before_fork_exec (DWORD) {}
+  virtual void fixup_after_fork (HANDLE);
+  virtual void fixup_after_exec (HANDLE) {}
 
   int get_symlink_p () { return FHISSETF (SYMLINK); }
   void set_symlink_p (int val) { FHCONDSETF (val, SYMLINK); }
@@ -204,7 +227,10 @@ public:
   void set_socket_p () { FHSETF (LOCAL); }
 
   int get_execable_p () { return FHISSETF (EXECABL); }
-  void set_execable_p (int val) { FHCONDSETF (val, EXECABL); }
+  void set_execable_p (executable_states val)
+  {
+    FHCONDSETF (val == is_executable, EXECABL);
+  }
   void set_execable_p () { FHSETF (EXECABL); }
 
   int get_append_p () { return FHISSETF (APPEND); }
@@ -235,6 +261,9 @@ public:
   const char *get_win32_name () { return win32_path_name_; }
   unsigned long get_namehash () { return namehash_; }
 
+  virtual void hclose (HANDLE h) {CloseHandle (h);}
+  virtual void set_inheritance (HANDLE &h, int not_inheriting,
+                               const char *name = NULL);
 
   /* fixup fd possibly non-inherited handles after fork */
   void fork_fixup (HANDLE parent, HANDLE &h, const char *name);
@@ -246,8 +275,9 @@ public:
   }
   virtual int open (int flags, mode_t mode = 0);
   virtual int close ();
-  virtual int fstat (struct stat *buf);
+  virtual int fstat (struct stat *buf) { return stat_dev (get_device (), get_unit (), get_namehash (), buf); }
   virtual int ioctl (unsigned int cmd, void *);
+  virtual int fcntl (int cmd, void *);
   virtual char const * ttyname () { return get_name(); }
   virtual int read (void *ptr, size_t len);
   virtual int write (const void *ptr, size_t len);
@@ -256,6 +286,13 @@ public:
   virtual void dump ();
   virtual int dup (fhandler_base *child);
 
+  virtual HANDLE mmap (caddr_t *addr, size_t len, DWORD access,
+                       int flags, off_t off);
+  virtual int munmap (HANDLE h, caddr_t addr, size_t len);
+  virtual int msync (HANDLE h, caddr_t addr, size_t len, int flags);
+  virtual BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
+                                     DWORD size, void *address);
+
   void *operator new (size_t, void *p) {return p;}
 
   virtual void init (HANDLE, DWORD, mode_t);
@@ -278,16 +315,11 @@ public:
   virtual int raw_read (void *ptr, size_t ulen);
   virtual int raw_write (const void *ptr, size_t ulen);
 
-  /* Function to save state of a fhandler_base into memory. */
-  virtual int linearize (unsigned char *);
-  /* Function to de-linearize into a fd */
-  virtual int de_linearize (const char *, const char *, const char *);
-
   /* Virtual accessor functions to hide the fact
      that some fd's have two handles. */
-  virtual HANDLE get_handle () const { return io_handle; }
-  virtual HANDLE get_io_handle () const { return io_handle; }
-  virtual HANDLE get_output_handle () const { return io_handle; }
+  virtual HANDLE& get_handle () { return io_handle; }
+  virtual HANDLE& get_io_handle () { return io_handle; }
+  virtual HANDLE& get_output_handle () { return io_handle; }
   virtual BOOL hit_eof () {return FALSE;}
   virtual select_record *select_read (select_record *s);
   virtual select_record *select_write (select_record *s);
@@ -297,24 +329,38 @@ public:
   {
     return windows_device_names[FHDEVN (status)];
   }
-  virtual int bg_check (int) {return 1;}
+  virtual bg_check_types bg_check (int) {return bg_ok;}
+  void clear_readahead ()
+  {
+    raixput = raixget = ralen = rabuflen = 0;
+    rabuf = NULL;
+  }
+  void operator delete (void *);
 };
 
 class fhandler_socket: public fhandler_base
 {
 private:
   int addr_family;
+  struct _WSAPROTOCOL_INFOA *prot_info_ptr;
+
 public:
   fhandler_socket (const char *name = 0);
-  fhandler_socket (unsigned int, const char *name = 0);
   ~fhandler_socket ();
-  int get_socket () const { return (int) get_handle(); }
+  int get_socket () { return (int) get_handle(); }
   fhandler_socket * is_socket () { return this; }
   int write (const void *ptr, size_t len);
   int read (void *ptr, size_t len);
   int ioctl (unsigned int cmd, void *);
+  int fcntl (int cmd, void *);
   off_t lseek (off_t, int) { return 0; }
   int close ();
+  void hclose (HANDLE) {close ();}
+  int dup (fhandler_base *child);
+
+  virtual void fixup_before_fork_exec (DWORD);
+  void fixup_after_fork (HANDLE);
+  void fixup_after_exec (HANDLE parent) { fixup_after_fork (parent); }
 
   select_record *select_read (select_record *s);
   select_record *select_write (select_record *s);
@@ -351,6 +397,7 @@ protected:
   int lastblk_to_read : 1;
   int is_writing      : 1;
   int has_written     : 1;
+  int varblkop        : 1;
   int unit;
 
   virtual void clear (void);
@@ -366,9 +413,6 @@ protected:
 public:
   ~fhandler_dev_raw (void);
 
-  /* Function to de-linearize into a fd */
-  int de_linearize (const char *, const char *, const char *);
-
   int open (const char *path, int flags, mode_t mode = 0);
   int close (void);
 
@@ -380,6 +424,9 @@ public:
   int dup (fhandler_base *child);
 
   int ioctl (unsigned int cmd, void *buf);
+
+  void fixup_after_fork (HANDLE);
+  void fixup_after_exec (HANDLE parent) { fixup_after_fork (parent); }
 };
 
 class fhandler_dev_floppy: public fhandler_dev_raw
@@ -441,9 +488,6 @@ private:
 
 class fhandler_disk_file: public fhandler_base
 {
-private:
-  int check_execable_p (const char *path);
-
 public:
   fhandler_disk_file (const char *name);
 
@@ -453,6 +497,12 @@ public:
   int lock (int, struct flock *);
   BOOL is_device () { return FALSE; }
   int fstat (struct stat *buf);
+
+  HANDLE mmap (caddr_t *addr, size_t len, DWORD access, int flags, off_t off);
+  int munmap (HANDLE h, caddr_t addr, size_t len);
+  int msync (HANDLE h, caddr_t addr, size_t len, int flags);
+  BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
+                             DWORD size, void *address);
 };
 
 class fhandler_serial: public fhandler_base
@@ -486,7 +536,7 @@ public:
   void dump ();
   int is_tty () { return 1; }
   void fixup_after_fork (HANDLE parent);
-  int de_linearize (const char *, const char *, const char *);
+  void fixup_after_exec (HANDLE);
 
   /* We maintain a pgrp so that tcsetpgrp and tcgetpgrp work, but we
      don't use it for permissions checking.  fhandler_tty_slave does
@@ -499,6 +549,14 @@ public:
   int ready_for_read (int fd, DWORD howlong, int ignra);
 };
 
+#define acquire_output_mutex(ms) \
+  __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms);
+
+#define release_output_mutex() \
+  __release_output_mutex (__PRETTY_FUNCTION__, __LINE__);
+
+class tty;
+class tty_min;
 class fhandler_termios: public fhandler_base
 {
 protected:
@@ -510,10 +568,9 @@ public:
   fhandler_termios (DWORD dev, const char *name = 0, int unit = 0) :
   fhandler_base (dev, name, unit)
   {
-    // nothing to do
+    set_need_fork_fixup ();
   }
-  HANDLE restart_output_event;
-  HANDLE get_output_handle () const { return output_handle; }
+  HANDLE& get_output_handle () { return output_handle; }
   int line_edit (const char *rptr, int nread, int always_accept = 0);
   void set_output_handle (HANDLE h) { output_handle = h; }
   void tcinit (tty_min *this_tc, int force = FALSE);
@@ -521,17 +578,21 @@ public:
   int tcgetpgrp ();
   int tcsetpgrp (int pid);
   void set_ctty (int ttynum, int flags);
-  int bg_check (int sig);
+  bg_check_types bg_check (int sig);
+  virtual DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms) {return 1;}
+  virtual void __release_output_mutex (const char *fn, int ln) {}
+  void fixup_after_fork (HANDLE);
+  void fixup_after_exec (HANDLE parent) { fixup_after_fork (parent); }
 };
 
-/* This is a input and output console handle */
-class fhandler_console: public fhandler_termios
+enum ansi_intensity
 {
-private:
-
-/* Output state */
+  INTENSITY_INVISIBLE,
+  INTENSITY_DIM,
+  INTENSITY_NORMAL,
+  INTENSITY_BOLD
+};
 
-  // enum {normal, gotesc, gotsquare, gotarg1, gotcommand} state;
 #define normal 1
 #define gotesc 2
 #define gotsquare 3
@@ -541,13 +602,58 @@ private:
 #define gettitle 7
 #define eattitle 8
 #define MAXARGS 10
+
+/* This is a input and output console handle */
+class fhandler_console: public fhandler_termios
+{
+private:
+
+  WORD default_color, underline_color, dim_color;
+
+  /* Used to determine if an input keystroke should be modified with META. */
+  int meta_mask;
+
+/* Output state */
   int state_;
   int args_[MAXARGS];
   int nargs_;
-
-  DWORD default_color;
+  unsigned rarg;
+  BOOL saw_question_mark;
+
+  char my_title_buf [TITLESIZE + 1];
+
+  WORD current_win32_attr;
+  ansi_intensity intensity;
+  BOOL underline, blink, reverse;
+  WORD fg, bg;
+
+  /* saved cursor coordinates */
+  int savex, savey;
+
+  struct
+    {
+      short Top, Bottom;
+    } scroll_region;
+  struct
+    {
+      SHORT winTop;
+      SHORT winBottom;
+      COORD dwWinSize;
+      COORD dwBufferSize;
+      COORD dwCursorPosition;
+      WORD wAttributes;
+    } info;
+
+  COORD dwLastCursorPosition;
+  DWORD dwLastButtonState;
+  int nModifiers;
+
+  BOOL use_mouse;
+  BOOL raw_win32_keyboard_mode;
 
 /* Output calls */
+  void set_default_attr ();
+  WORD get_win32_attr ();
 
   BOOL fillin_info ();
   void clear_screen (int, int, int, int);
@@ -557,11 +663,13 @@ private:
   void cursor_rel (int, int);
   const unsigned char * write_normal (unsigned const char*, unsigned const char *);
   void char_command (char);
+  BOOL set_raw_win32_keyboard_mode (BOOL);
   int output_tcsetattr (int a, const struct termios *t);
 
 /* Input calls */
   int igncr_enabled ();
   int input_tcsetattr (int a, const struct termios *t);
+  void set_cursor_maybe ();
 
 public:
 
@@ -580,26 +688,21 @@ public:
   int tcsetattr (int a, const struct termios *t);
   int tcgetattr (struct termios *t);
 
-  int tcsetpgrp (const pid_t pid) { tc->pgid = pid; return 0; }
-
   /* Special dup as we must dup two handles */
   int dup (fhandler_base *child);
 
   int ioctl (unsigned int cmd, void *);
   void init (HANDLE, DWORD, mode_t);
+  bool mouse_aware () {return use_mouse;}
 
   select_record *select_read (select_record *s);
   select_record *select_write (select_record *s);
   select_record *select_except (select_record *s);
   int ready_for_read (int fd, DWORD howlong, int ignra);
-  int de_linearize (const char *, const char *, const char *);
+  void fixup_after_exec (HANDLE);
   void set_close_on_exec (int val);
   void fixup_after_fork (HANDLE parent);
-  void set_input_state ()
-  {
-    if (TTYISSETF (RSTCONS))
-      input_tcsetattr (0, &tc->ti);
-  }
+  void set_input_state ();
 };
 
 class fhandler_tty_common: public fhandler_termios
@@ -617,7 +720,8 @@ public:
                                // Ioctl() request in tty::cmd/arg.
   HANDLE ioctl_done_event;     // Raised by master on ioctl() completion.
                                // Ioctl() status in tty::ioctl_retval.
-  HANDLE output_mutex;
+  HANDLE output_mutex, input_mutex;
+  HANDLE input_available_event;
   HANDLE inuse;                        // used to indicate that a tty is in use
 
 
@@ -641,8 +745,6 @@ public:
 
 class fhandler_tty_slave: public fhandler_tty_common
 {
-  void send_ioctl_request ();
-
 public:
   /* Constructor */
   fhandler_tty_slave (const char *name);
@@ -659,6 +761,8 @@ public:
   int ioctl (unsigned int cmd, void *);
 
   off_t lseek (off_t, int) { return 0; }
+  select_record *select_read (select_record *s);
+  int ready_for_read (int fd, DWORD howlong, int ignra);
 };
 
 class fhandler_pty_master: public fhandler_tty_common
@@ -687,7 +791,6 @@ public:
   char *ptsname ();
 
   void set_close_on_exec (int val);
-  void fixup_after_fork (HANDLE parent);
   BOOL hit_eof ();
 };
 
@@ -702,7 +805,7 @@ public:
   int init (int);
   int init_console ();
   void fixup_after_fork (HANDLE parent);
-  int de_linearize (const char *, const char *, const char *);
+  void fixup_after_exec (HANDLE);
 };
 
 class fhandler_dev_null: public fhandler_base
@@ -729,6 +832,72 @@ public:
   void dump ();
 };
 
+class fhandler_dev_random: public fhandler_base
+{
+protected:
+  int unit;
+  HCRYPTPROV crypt_prov;
+  long pseudo;
+
+  BOOL crypt_gen_random (void *ptr, size_t len);
+  int pseudo_write (const void *ptr, size_t len);
+  int pseudo_read (void *ptr, size_t len);
+
+public:
+  fhandler_dev_random (const char *name, int unit);
+  int get_unit () { return unit; }
+  int open (const char *path, int flags, mode_t mode = 0);
+  int write (const void *ptr, size_t len);
+  int read (void *ptr, size_t len);
+  off_t lseek (off_t offset, int whence);
+  int close (void);
+  int dup (fhandler_base *child);
+
+  void dump ();
+};
+
+class fhandler_dev_mem: public fhandler_base
+{
+protected:
+  int unit;
+  DWORD mem_size;
+  DWORD pos;
+
+public:
+  fhandler_dev_mem (const char *name, int unit);
+  ~fhandler_dev_mem (void);
+
+  int open (const char *path, int flags, mode_t mode = 0);
+  int write (const void *ptr, size_t ulen);
+  int read (void *ptr, size_t ulen);
+  off_t lseek (off_t offset, int whence);
+  int close (void);
+  int fstat (struct stat *buf);
+  int dup (fhandler_base *child);
+
+  HANDLE mmap (caddr_t *addr, size_t len, DWORD access, int flags, off_t off);
+  int munmap (HANDLE h, caddr_t addr, size_t len);
+  int msync (HANDLE h, caddr_t addr, size_t len, int flags);
+  BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
+                             DWORD size, void *address);
+
+  void dump ();
+} ;
+
+class fhandler_dev_clipboard: public fhandler_base
+{
+public:
+  fhandler_dev_clipboard (const char *name);
+  int is_windows (void) { return 1; }
+  int open (const char *path, int flags, mode_t mode = 0);
+  int write (const void *ptr, size_t len);
+  int read (void *ptr, size_t len);
+  off_t lseek (off_t offset, int whence);
+  int close (void);
+
+  void dump ();
+};
+
 class fhandler_windows: public fhandler_base
 {
 private:
@@ -781,25 +950,23 @@ struct select_record
   void (*cleanup) (select_record *me, class select_stuff *stuff);
   struct select_record *next;
 
-  select_record (fhandler_base *in_fh = NULL) {memset (this, 0, sizeof(select_record)); fh = in_fh;}
-  select_record (int) : fd (0), h (NULL), fh (0), saw_error (0), windows_handle (0),
-                    read_ready (0), write_ready (0), except_ready (0),
-                    read_selected (0), write_selected (0), except_selected (0),
-                    startup (NULL), poll (NULL), verify (NULL), cleanup (NULL),
-                    next (NULL) {}
+  select_record (fhandler_base *in_fh = NULL) : fd (0), h (NULL),
+                fh (in_fh), saw_error (0), windows_handle (0),
+                read_ready (0), write_ready (0), except_ready (0),
+                read_selected (0), write_selected (0), except_selected (0),
+                startup (NULL), poll (NULL), verify (NULL), cleanup (NULL),
+                next (NULL) {}
 };
 
 class select_stuff
 {
 public:
   ~select_stuff ();
-  select_stuff (): always_ready (0), windows_used (0),
-                  total (0), start (0)
+  select_stuff (): always_ready (0), windows_used (0), start (0)
   {
     memset (device_specific, 0, sizeof (device_specific));
   }
   BOOL always_ready, windows_used;
-  int total;
   select_record start;
   void *device_specific[FH_NDEV];
 
@@ -809,9 +976,6 @@ public:
   int wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds, DWORD ms);
 };
 
-uid_t __stdcall get_file_owner (int, const char *);
-gid_t __stdcall get_file_group (int, const char *);
-
-void __stdcall set_inheritance (HANDLE &h, int val, const char *name = NULL);
+int __stdcall set_console_state_for_spawn ();
 
 #endif /* _FHANDLER_H_ */