aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cygw32.c169
-rw-r--r--src/cygw32.h59
-rw-r--r--src/w32select.h30
3 files changed, 258 insertions, 0 deletions
diff --git a/src/cygw32.c b/src/cygw32.c
new file mode 100644
index 00000000000..065ab948118
--- /dev/null
+++ b/src/cygw32.c
@@ -0,0 +1,169 @@
1/* Cygwin support routines.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19
20#include "cygw32.h"
21#include "character.h"
22#include "buffer.h"
23#include <unistd.h>
24#include <fcntl.h>
25static Lisp_Object Qutf_16_le;
26
27static Lisp_Object
28fchdir_unwind (Lisp_Object dir_fd)
29{
30 (void) fchdir (XFASTINT (dir_fd));
31 (void) close (XFASTINT (dir_fd));
32 return Qnil;
33}
34
35static void
36chdir_to_default_directory ()
37{
38 Lisp_Object new_cwd;
39 int old_cwd_fd = open (".", O_RDONLY | O_DIRECTORY);
40
41 if (old_cwd_fd == -1)
42 error ("could not open current directory: %s", strerror (errno));
43
44 record_unwind_protect (fchdir_unwind, make_number (old_cwd_fd));
45
46 new_cwd = Funhandled_file_name_directory (
47 Fexpand_file_name (build_string ("."), Qnil));
48 if (!STRINGP (new_cwd))
49 new_cwd = build_string ("/");
50
51 if (chdir (SDATA (ENCODE_FILE (new_cwd))))
52 error ("could not chdir: %s", strerror (errno));
53}
54
55static Lisp_Object
56conv_filename_to_w32_unicode (Lisp_Object in, int absolute_p)
57{
58 ssize_t converted_len;
59 Lisp_Object converted;
60 unsigned flags;
61 int count = SPECPDL_INDEX ();
62
63 chdir_to_default_directory ();
64
65 flags = CCP_POSIX_TO_WIN_W;
66 if (!absolute_p) {
67 flags |= CCP_RELATIVE;
68 }
69
70 in = ENCODE_FILE (in);
71
72 converted_len = cygwin_conv_path (flags, SDATA (in), NULL, 0);
73 if (converted_len < 2)
74 error ("cygwin_conv_path: %s", strerror (errno));
75
76 converted = make_uninit_string (converted_len - 1);
77 if (cygwin_conv_path (flags, SDATA (in),
78 SDATA (converted), converted_len))
79 error ("cygwin_conv_path: %s", strerror (errno));
80
81 return unbind_to (count, converted);
82}
83
84static Lisp_Object
85conv_filename_from_w32_unicode (const wchar_t* in, int absolute_p)
86{
87 ssize_t converted_len;
88 Lisp_Object converted;
89 unsigned flags;
90 int count = SPECPDL_INDEX ();
91
92 chdir_to_default_directory ();
93
94 flags = CCP_WIN_W_TO_POSIX;
95 if (!absolute_p) {
96 flags |= CCP_RELATIVE;
97 }
98
99 converted_len = cygwin_conv_path (flags, in, NULL, 0);
100 if (converted_len < 1)
101 error ("cygwin_conv_path: %s", strerror (errno));
102
103 converted = make_uninit_string (converted_len - 1 /*subtract terminator*/);
104 if (cygwin_conv_path (flags, in, SDATA (converted), converted_len))
105 error ("cygwin_conv_path: %s", strerror (errno));
106
107 return unbind_to (count, DECODE_FILE (converted));
108}
109
110Lisp_Object
111from_unicode (Lisp_Object str)
112{
113 CHECK_STRING (str);
114 if (!STRING_MULTIBYTE (str) &&
115 SBYTES (str) & 1)
116 {
117 str = Fsubstring (str, make_number (0), make_number (-1));
118 }
119
120 return code_convert_string_norecord (str, Qutf_16_le, 0);
121}
122
123wchar_t *
124to_unicode (Lisp_Object str, Lisp_Object *buf)
125{
126 *buf = code_convert_string_norecord (str, Qutf_16_le, 1);
127 /* We need to make a another copy (in addition to the one made by
128 code_convert_string_norecord) to ensure that the final string is
129 _doubly_ zero terminated --- that is, that the string is
130 terminated by two zero bytes and one utf-16le null character.
131 Because strings are already terminated with a single zero byte,
132 we just add one additional zero. */
133 str = make_uninit_string (SBYTES (*buf) + 1);
134 memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf));
135 SDATA (str) [SBYTES (*buf)] = '\0';
136 *buf = str;
137 return WCSDATA (*buf);
138}
139
140DEFUN ("cygwin-convert-path-to-windows",
141 Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
142 1, 2, 0,
143 doc: /* Convert PATH to a Windows path. If ABSOLUTE-P if
144 non-nil, return an absolute path.*/)
145 (Lisp_Object path, Lisp_Object absolute_p)
146{
147 return from_unicode (
148 conv_filename_to_w32_unicode (path, absolute_p == Qnil ? 0 : 1));
149}
150
151DEFUN ("cygwin-convert-path-from-windows",
152 Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
153 1, 2, 0,
154 doc: /* Convert a Windows path to a Cygwin path. If ABSOLUTE-P
155 if non-nil, return an absolute path.*/)
156 (Lisp_Object path, Lisp_Object absolute_p)
157{
158 return conv_filename_from_w32_unicode (to_unicode (path, &path),
159 absolute_p == Qnil ? 0 : 1);
160}
161
162void
163syms_of_cygw32 (void)
164{
165 /* No, not utf-16-le: that one has a BOM. */
166 DEFSYM (Qutf_16_le, "utf-16le");
167 defsubr (&Scygwin_convert_path_from_windows);
168 defsubr (&Scygwin_convert_path_to_windows);
169}
diff --git a/src/cygw32.h b/src/cygw32.h
new file mode 100644
index 00000000000..c63343c3f5a
--- /dev/null
+++ b/src/cygw32.h
@@ -0,0 +1,59 @@
1/* Header for Cygwin support routines.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19#ifndef CYGW32_H
20#define CYGW32_H
21#include <config.h>
22#include <windef.h>
23#include <sys/cygwin.h>
24#include <wchar.h>
25
26#include <signal.h>
27#include <stdio.h>
28#include <limits.h>
29#include <errno.h>
30#include <math.h>
31#include <setjmp.h>
32
33#include "lisp.h"
34#include "coding.h"
35
36/* *** Character conversion *** */
37
38/* Access the wide-character string stored in a Lisp string object. */
39#define WCSDATA(x) ((wchar_t *) SDATA (x))
40
41/* Convert the Emacs string in STR to UTF-16LE and store a new string
42 containing the encoded version of STR into *BUF. BUF may safely
43 point to STR on entry. */
44extern wchar_t *to_unicode (Lisp_Object str, Lisp_Object *buf);
45
46/* Convert STR, a UTF-16LE encoded string embedded in an Emacs string
47 object, to a normal Emacs string and return it. */
48extern Lisp_Object from_unicode (Lisp_Object str);
49
50/* *** Path conversion. *** */
51
52EXFUN (Fcygwin_convert_path_to_windows, 2);
53EXFUN (Fcygwin_convert_path_from_windows, 2);
54
55/* *** Misc *** */
56extern void syms_of_cygw32 (void);
57extern char * w32_strerror (int error_no);
58
59#endif /* CYGW32_H */
diff --git a/src/w32select.h b/src/w32select.h
new file mode 100644
index 00000000000..6924d4d51ae
--- /dev/null
+++ b/src/w32select.h
@@ -0,0 +1,30 @@
1/* Selection processing for Emacs on the Microsoft W32 API.
2
3Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20#ifndef W32SELECT_H
21#define W32SELECT_H
22#include <windows.h>
23
24#define HAVE_W32SELECT 1
25
26extern void syms_of_w32select (void);
27extern void globals_of_w32select (void);
28extern void term_w32select (void);
29
30#endif