Reimplement terminal parameters in C; clean up term.c, create terminal.c.
[bpt/emacs.git] / src / sysdep.c
index 581a225..7f9d189 100644 (file)
@@ -1,6 +1,6 @@
 /* Interfaces to system-dependent kernel and library entries.
    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
-     2003, 2004, 2005  Free Software Foundation, Inc.
+                 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -16,8 +16,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -49,13 +49,9 @@ extern void srandom P_ ((unsigned int));
 #include "sysselect.h"
 
 #include "blockinput.h"
-#undef NULL
 
 #ifdef MAC_OS8
-/* It is essential to include stdlib.h so that this file picks up
-   the correct definitions of rand, srand, and RAND_MAX.
-   Otherwise random numbers will not work correctly.  */
-#include <stdlib.h>
+#include <sys/param.h>
 
 #ifndef subprocesses
 /* Nonzero means delete a process right away if it exits (process.c).  */
@@ -190,6 +186,7 @@ extern int quit_char;
 #define _P_WAIT 0
 int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
 int _CRTAPI1 _getpid (void);
+extern char *getwd (char *);
 #endif
 
 #ifdef NONSYSTEM_DIR_LIBRARY
@@ -256,6 +253,81 @@ void hft_reset P_ ((struct tty_display_info *));
 
 SIGMASKTYPE sigprocmask_set;
 
+
+#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
+
+/* Return the current working directory.  Returns NULL on errors.
+   Any other returned value must be freed with free. This is used
+   only when get_current_dir_name is not defined on the system.  */
+char*
+get_current_dir_name ()
+{
+  char *buf;
+  char *pwd;
+  struct stat dotstat, pwdstat;
+  /* If PWD is accurate, use it instead of calling getwd.  PWD is
+     sometimes a nicer name, and using it may avoid a fatal error if a
+     parent directory is searchable but not readable.  */
+    if ((pwd = getenv ("PWD")) != 0
+      && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
+      && stat (pwd, &pwdstat) == 0
+      && stat (".", &dotstat) == 0
+      && dotstat.st_ino == pwdstat.st_ino
+      && dotstat.st_dev == pwdstat.st_dev
+#ifdef MAXPATHLEN
+      && strlen (pwd) < MAXPATHLEN
+#endif
+      )
+    {
+      buf = (char *) malloc (strlen (pwd) + 1);
+      if (!buf)
+        return NULL;
+      strcpy (buf, pwd);
+    }
+#ifdef HAVE_GETCWD
+  else
+    {
+      size_t buf_size = 1024;
+      buf = (char *) malloc (buf_size);
+      if (!buf)
+        return NULL;
+      for (;;)
+        {
+          if (getcwd (buf, buf_size) == buf)
+            break;
+          if (errno != ERANGE)
+            {
+              int tmp_errno = errno;
+              free (buf);
+              errno = tmp_errno;
+              return NULL;
+            }
+          buf_size *= 2;
+          buf = (char *) realloc (buf, buf_size);
+          if (!buf)
+            return NULL;
+        }
+    }
+#else
+  else
+    {
+      /* We need MAXPATHLEN here.  */
+      buf = (char *) malloc (MAXPATHLEN + 1);
+      if (!buf)
+        return NULL;
+      if (getwd (buf) == NULL)
+        {
+          int tmp_errno = errno;
+          free (buf);
+          errno = tmp_errno;
+          return NULL;
+        }
+    }
+#endif
+  return buf;
+}
+#endif
+
 \f
 /* Discard pending input on all input descriptors.  */
 
@@ -960,6 +1032,7 @@ reset_sigio (fd)
 
 #ifdef FASYNC          /* F_SETFL does not imply existence of FASYNC */
 /* XXX Uhm, FASYNC is not used anymore here. */
+/* XXX Yeah, but you need it for SIGIO, don't you? */
 
 void
 request_sigio ()
@@ -967,7 +1040,7 @@ request_sigio ()
   /* XXX read_socket_hook is not global anymore.  Is blocking SIGIO
      bad under X? */
 #if 0
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 #endif
 
@@ -985,7 +1058,7 @@ unrequest_sigio (void)
   /* XXX read_socket_hook is not global anymore.  Is blocking SIGIO
      bad under X? */
 #if 0
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 #endif
   
@@ -1004,7 +1077,7 @@ request_sigio ()
 {
   int on = 1;
 
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   /* XXX CURTTY() is bogus here. */
@@ -1017,7 +1090,7 @@ unrequest_sigio ()
 {
   int off = 0;
 
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   /* XXX CURTTY() is bogus here. */
@@ -1037,7 +1110,7 @@ request_sigio ()
   int on = 1;
   sigset_t st;
 
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   sigemptyset (&st);
@@ -1052,7 +1125,7 @@ unrequest_sigio ()
 {
   int off = 0;
 
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   ioctl (0, FIOASYNC, &off);  /* XXX This fails for multiple ttys. */
@@ -1065,7 +1138,7 @@ unrequest_sigio ()
 void
 request_sigio ()
 {
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   croak ("request_sigio");
@@ -1074,7 +1147,7 @@ request_sigio ()
 void
 unrequest_sigio ()
 {
-  if (read_socket_hook)
+  if (noninteractive || read_socket_hook)
     return;
 
   croak ("unrequest_sigio");
@@ -1124,8 +1197,12 @@ narrow_foreground_group (int fd)
   int me = getpid ();
 
   setpgrp (0, inherited_pgroup);
+#if 0
+  /* XXX inherited_pgroup should not be zero here, but GTK seems to
+     mess this up. */
   if (! inherited_pgroup)
     abort ();                   /* Should not happen. */
+#endif
   if (inherited_pgroup != me)
       EMACS_SET_TTY_PGRP (fd, &me); /* XXX This only works on the controlling tty. */
   setpgrp (0, me);
@@ -1705,7 +1782,7 @@ nil means don't delete them until `list-processes' is run.  */);
   setbuf (tty_out->output, (char *) _sobuf);
 #endif
 
-  tty_set_terminal_modes (tty_out->display);
+  tty_set_terminal_modes (tty_out->device);
 
   if (!tty_out->term_initted)
     {
@@ -1721,6 +1798,7 @@ nil means don't delete them until `list-processes' is run.  */);
 
   if (tty_out->term_initted && no_redraw_on_reenter)
     {
+      /* XXX This seems wrong on multi-tty. */
       if (display_completed)
        direct_output_forward_char (0);
     }
@@ -1789,10 +1867,16 @@ get_tty_size (int fd, int *widthp, int *heightp)
 #else
 #ifdef VMS
 
+  /* Use a fresh channel since the current one may have stale info
+     (for example, from prior to a suspend); and to avoid a dependency
+     in the init sequence.  */
+  int chan;
   struct sensemode tty;
 
-  SYS$QIOW (0, fd, IO$_SENSEMODE, &tty, 0, 0,
-           &tty.class, 12, 0, 0, 0, 0);
+  SYS$ASSIGN (&input_dsc, &chan, 0, 0);
+  SYS$QIOW (0, chan, IO$_SENSEMODE, &tty, 0, 0,
+            &tty.class, 12, 0, 0, 0, 0);
+  SYS$DASSGN (chan);
   *widthp = tty.scr_wid;
   *heightp = tty.scr_len;
 
@@ -1888,7 +1972,7 @@ reset_sys_modes (tty_out)
   else
     {                  /* have to do it the hard way */
       int i;
-      turn_off_insert (tty_out);
+      tty_turn_off_insert (tty_out);
       
       for (i = curX (tty_out); i < FrameCols (tty_out) - 1; i++)
         {
@@ -1910,8 +1994,8 @@ reset_sys_modes (tty_out)
   }
 #endif
 
-  tty_reset_terminal_modes (tty_out->display);
-  fflush (tty_out->output);
+  tty_reset_terminal_modes (tty_out->device);
+
 #ifdef BSD_SYSTEM
 #ifndef BSD4_1
   /* Avoid possible loss of output when changing terminal modes.  */
@@ -2252,12 +2336,16 @@ reset_sigio (fd)
 void
 request_sigio ()
 {
+  if (noninteractive)
+    return;
   croak ("request sigio");
 }
 
 void
 unrequest_sigio ()
 {
+  if (noninteractive)
+    return;
   croak ("unrequest sigio");
 }
 
@@ -2801,6 +2889,8 @@ reset_sigio (fd)
 void
 request_sigio ()
 {
+  if (noninteractive)
+    return;
   sigrelse (SIGTINT);
 
   interrupts_deferred = 0;
@@ -2809,6 +2899,8 @@ request_sigio ()
 void
 unrequest_sigio ()
 {
+  if (noninteractive)
+    return;
   sighold (SIGTINT);
 
   interrupts_deferred = 1;
@@ -3907,7 +3999,6 @@ rmdir (dpath)
 \f
 /* Functions for VMS */
 #ifdef VMS
-#include "vms-pwd.h"
 #include <acldef.h>
 #include <chpdef.h>
 #include <jpidef.h>