use dynwind_begin and dynwind_end
[bpt/emacs.git] / src / cygw32.c
index ab7ee91..9808cc6 100644 (file)
@@ -1,5 +1,5 @@
 /* Cygwin support routines.
-   Copyright (C) 2011-201 Free Software Foundation, Inc.
+   Copyright (C) 2011-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -22,26 +22,24 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "buffer.h"
 #include <unistd.h>
 #include <fcntl.h>
-static Lisp_Object Qutf_16_le;
 
-static Lisp_Object
-fchdir_unwind (Lisp_Object dir_fd)
+static void
+fchdir_unwind (int dir_fd)
 {
-  (void) fchdir (XFASTINT (dir_fd));
-  (void) close (XFASTINT (dir_fd));
-  return Qnil;
+  (void) fchdir (dir_fd);
+  (void) close (dir_fd);
 }
 
 static void
 chdir_to_default_directory ()
 {
   Lisp_Object new_cwd;
-  int old_cwd_fd = open (".", O_RDONLY | O_DIRECTORY);
+  int old_cwd_fd = emacs_open (".", O_RDONLY | O_DIRECTORY, 0);
 
   if (old_cwd_fd == -1)
     error ("could not open current directory: %s", strerror (errno));
 
-  record_unwind_protect (fchdir_unwind, make_number (old_cwd_fd));
+  record_unwind_protect_int (fchdir_unwind, old_cwd_fd);
 
   new_cwd = Funhandled_file_name_directory (
     Fexpand_file_name (build_string ("."), Qnil));
@@ -58,7 +56,7 @@ conv_filename_to_w32_unicode (Lisp_Object in, int absolute_p)
   ssize_t converted_len;
   Lisp_Object converted;
   unsigned flags;
-  int count = SPECPDL_INDEX ();
+  dynwind_begin ();
 
   chdir_to_default_directory ();
 
@@ -78,7 +76,8 @@ conv_filename_to_w32_unicode (Lisp_Object in, int absolute_p)
                         SDATA (converted), converted_len))
     error ("cygwin_conv_path: %s", strerror (errno));
 
-  return unbind_to (count, converted);
+  dynwind_end ();
+  return converted;
 }
 
 static Lisp_Object
@@ -87,7 +86,7 @@ conv_filename_from_w32_unicode (const wchar_t* in, int absolute_p)
   ssize_t converted_len;
   Lisp_Object converted;
   unsigned flags;
-  int count = SPECPDL_INDEX ();
+  dynwind_begin ();
 
   chdir_to_default_directory ();
 
@@ -104,66 +103,39 @@ conv_filename_from_w32_unicode (const wchar_t* in, int absolute_p)
   if (cygwin_conv_path (flags, in, SDATA (converted), converted_len))
     error ("cygwin_conv_path: %s", strerror (errno));
 
-  return unbind_to (count, DECODE_FILE (converted));
-}
-
-Lisp_Object
-from_unicode (Lisp_Object str)
-{
-  CHECK_STRING (str);
-  if (!STRING_MULTIBYTE (str) &&
-      SBYTES (str) & 1)
-    {
-      str = Fsubstring (str, make_number (0), make_number (-1));
-    }
-
-  return code_convert_string_norecord (str, Qutf_16_le, 0);
-}
-
-wchar_t *
-to_unicode (Lisp_Object str, Lisp_Object *buf)
-{
-  *buf = code_convert_string_norecord (str, Qutf_16_le, 1);
-  /* We need to make a another copy (in addition to the one made by
-     code_convert_string_norecord) to ensure that the final string is
-     _doubly_ zero terminated --- that is, that the string is
-     terminated by two zero bytes and one utf-16le null character.
-     Because strings are already terminated with a single zero byte,
-     we just add one additional zero. */
-  str = make_uninit_string (SBYTES (*buf) + 1);
-  memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf));
-  SDATA (str) [SBYTES (*buf)] = '\0';
-  *buf = str;
-  return WCSDATA (*buf);
+  Lisp_Object tem0 = DECODE_FILE (converted);
+  dynwind_end ();
+  return tem0;
 }
 
-DEFUN ("cygwin-convert-path-to-windows",
-       Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
+DEFUN ("cygwin-convert-file-name-to-windows",
+       Fcygwin_convert_file_name_to_windows,
+       Scygwin_convert_file_name_to_windows,
        1, 2, 0,
-       doc: /* Convert PATH to a Windows path.  If ABSOLUTE-P if
-               non-nil, return an absolute path.*/)
-  (Lisp_Object path, Lisp_Object absolute_p)
+       doc: /* Convert a Cygwin file name FILE to a Windows-style file name.
+If ABSOLUTE-P is non-nil, return an absolute file name.
+For the reverse operation, see `cygwin-convert-file-name-from-windows'.  */)
+  (Lisp_Object file, Lisp_Object absolute_p)
 {
   return from_unicode (
-    conv_filename_to_w32_unicode (path, absolute_p == Qnil ? 0 : 1));
+    conv_filename_to_w32_unicode (file, EQ (absolute_p, Qnil) ? 0 : 1));
 }
 
-DEFUN ("cygwin-convert-path-from-windows",
-       Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
+DEFUN ("cygwin-convert-file-name-from-windows",
+       Fcygwin_convert_file_name_from_windows,
+       Scygwin_convert_file_name_from_windows,
        1, 2, 0,
-       doc: /* Convert a Windows path to a Cygwin path.  If ABSOLUTE-P
-               if non-nil, return an absolute path.*/)
-  (Lisp_Object path, Lisp_Object absolute_p)
+       doc: /* Convert a Windows-style file name FILE to a Cygwin file name.
+If ABSOLUTE-P is non-nil, return an absolute file name.
+For the reverse operation, see `cygwin-convert-file-name-to-windows'.  */)
+  (Lisp_Object file, Lisp_Object absolute_p)
 {
-  return conv_filename_from_w32_unicode (to_unicode (path, &path),
-                                         absolute_p == Qnil ? 0 : 1);
+  return conv_filename_from_w32_unicode (to_unicode (file, &file),
+                                         EQ (absolute_p, Qnil) ? 0 : 1);
 }
 
 void
 syms_of_cygw32 (void)
 {
-  /* No, not utf-16-le: that one has a BOM.  */
-  DEFSYM (Qutf_16_le, "utf-16le");
-  defsubr (&Scygwin_convert_path_from_windows);
-  defsubr (&Scygwin_convert_path_to_windows);
+#include "cygw32.x"
 }