]> code.delx.au - gnu-emacs/blob - src/w32.h
1efd562eadbfa9c874e016d1b191f0ce442abdf2
[gnu-emacs] / src / w32.h
1 #ifndef EMACS_W32_H
2 #define EMACS_W32_H
3
4 /* Support routines for the NT version of Emacs.
5 Copyright (C) 1994, 2001-2015 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 #ifdef CYGWIN
23 #error "w32.h is not compatible with Cygwin"
24 #endif
25
26 #include <windows.h>
27
28
29 /* File descriptor set emulation. */
30
31 /* MSVC runtime library has limit of 64 descriptors by default */
32 #define FD_SETSIZE 64
33 typedef struct {
34 unsigned int bits[FD_SETSIZE / 32];
35 } fd_set;
36
37 /* standard access macros */
38 #define FD_SET(n, p) \
39 do { \
40 if ((n) < FD_SETSIZE) { \
41 (p)->bits[(n)/32] |= (1 << (n)%32); \
42 } \
43 } while (0)
44 #define FD_CLR(n, p) \
45 do { \
46 if ((n) < FD_SETSIZE) { \
47 (p)->bits[(n)/32] &= ~(1 << (n)%32); \
48 } \
49 } while (0)
50 #define FD_ISSET(n, p) ((n) < FD_SETSIZE ? ((p)->bits[(n)/32] & (1 << (n)%32)) : 0)
51 #define FD_ZERO(p) memset((p), 0, sizeof(fd_set))
52
53 #define SELECT_TYPE fd_set
54
55 /* ------------------------------------------------------------------------- */
56
57 /* child_process.status values */
58 enum {
59 STATUS_READ_ERROR = -1,
60 STATUS_READ_READY,
61 STATUS_READ_IN_PROGRESS,
62 STATUS_READ_FAILED,
63 STATUS_READ_SUCCEEDED,
64 STATUS_READ_ACKNOWLEDGED,
65 STATUS_CONNECT_FAILED
66 };
67
68 /* This structure is used for both pipes and sockets; for
69 a socket, the process handle in pi is NULL. */
70 typedef struct _child_process
71 {
72 /* File descriptor for sockets and serial port connections, and for
73 reading output from async subprocesses; otherwise -1. */
74 int fd;
75 /* PID for subprocess, either async or not; otherwise -1. */
76 int pid;
77 /* Handle to an event object that is signaled when a read operation
78 is completed, either successfully (in which case there're indeed
79 "characters available") or not. Used by sys_select to wait for
80 output from subprocesses or socket/serial connections. */
81 HANDLE char_avail;
82 /* Handle to an event that is signaled to wake up the reader thread
83 and tell it to try reading more output from a subprocess. */
84 HANDLE char_consumed;
85 /* Handle to the reader thread to read output from a subprocess or a
86 socket or a comm port. */
87 HANDLE thrd;
88 /* Handle to the console window of a subprocess. Used to forcibly
89 terminate it by sys_kill. */
90 HWND hwnd;
91 /* Information about subprocess returned by CreateProcess. Includes
92 handles to the subprocess and its primary thread, and the
93 corresponding process ID and thread ID numbers. The PID is
94 mirrored by the 'pid' member above. The process handle is used
95 to wait on it. */
96 PROCESS_INFORMATION procinfo;
97 /* Status of subprocess/connection and of reading its output. For
98 values, see the enumeration above. */
99 volatile int status;
100 /* Used to store errno value of failed async 'connect' calls. */
101 volatile int errcode;
102 /* Holds a single character read by _sys_read_ahead, when a
103 subprocess has some output ready. */
104 char chr;
105 /* Used for async read operations on serial comm ports. */
106 OVERLAPPED ovl_read;
107 /* Used for async write operations on serial comm ports. */
108 OVERLAPPED ovl_write;
109 } child_process;
110
111 #define MAXDESC FD_SETSIZE
112 #define MAX_CHILDREN MAXDESC/2
113 #define CHILD_ACTIVE(cp) ((cp)->char_avail != NULL)
114
115 /* parallel array of private info on file handles */
116 typedef struct
117 {
118 unsigned flags;
119 HANDLE hnd;
120 child_process * cp;
121 } filedesc;
122
123 extern filedesc fd_info [ MAXDESC ];
124
125 /* fd_info flag definitions */
126 #define FILE_READ 0x0001
127 #define FILE_WRITE 0x0002
128 #define FILE_LISTEN 0x0004
129 #define FILE_CONNECT 0x0008
130 #define FILE_BINARY 0x0010
131 #define FILE_LAST_CR 0x0020
132 #define FILE_AT_EOF 0x0040
133 #define FILE_SEND_SIGCHLD 0x0080
134 #define FILE_PIPE 0x0100
135 #define FILE_SOCKET 0x0200
136 #define FILE_NDELAY 0x0400
137 #define FILE_SERIAL 0x0800
138
139 extern child_process * new_child (void);
140 extern void delete_child (child_process *cp);
141
142 /* ------------------------------------------------------------------------- */
143
144 /* Equivalent of strerror for W32 error codes. */
145 extern char * w32_strerror (int error_no);
146
147 /* Validate a pointer. */
148 extern int w32_valid_pointer_p (void *, int);
149
150 /* Get long (aka "true") form of file name, if it exists. */
151 extern BOOL w32_get_long_filename (const char * name, char * buf, int size);
152
153 /* Get the short (a.k.a. "8+3") form of a file name. */
154 extern unsigned int w32_get_short_filename (const char *, char *, int);
155
156 /* Prepare our standard handles for proper inheritance by child processes. */
157 extern void prepare_standard_handles (int in, int out,
158 int err, HANDLE handles[4]);
159
160 /* Reset our standard handles to their original state. */
161 extern void reset_standard_handles (int in, int out,
162 int err, HANDLE handles[4]);
163
164 /* Return the string resource associated with KEY of type TYPE. */
165 extern LPBYTE w32_get_resource (char * key, LPDWORD type);
166
167 extern void release_listen_threads (void);
168 extern void init_ntproc (int);
169 extern void term_ntproc (int);
170 extern HANDLE maybe_load_unicows_dll (void);
171 extern void globals_of_w32 (void);
172
173 extern void term_timers (void);
174 extern void init_timers (void);
175
176 extern int _sys_read_ahead (int fd);
177 extern int _sys_wait_accept (int fd);
178 extern int _sys_wait_connect (int fd);
179
180 extern HMODULE w32_delayed_load (Lisp_Object);
181
182 typedef int (WINAPI *MultiByteToWideChar_Proc)(UINT,DWORD,LPCSTR,int,LPWSTR,int);
183 typedef int (WINAPI *WideCharToMultiByte_Proc)(UINT,DWORD,LPCWSTR,int,LPSTR,int,LPCSTR,LPBOOL);
184 extern MultiByteToWideChar_Proc pMultiByteToWideChar;
185 extern WideCharToMultiByte_Proc pWideCharToMultiByte;
186
187 extern void init_environment (char **);
188 extern void check_windows_init_file (void);
189 extern void syms_of_ntproc (void);
190 extern void syms_of_ntterm (void);
191 extern void dostounix_filename (register char *);
192 extern void unixtodos_filename (register char *);
193 extern int filename_from_ansi (const char *, char *);
194 extern int filename_to_ansi (const char *, char *);
195 extern int filename_from_utf16 (const wchar_t *, char *);
196 extern int filename_to_utf16 (const char *, wchar_t *);
197 extern int codepage_for_filenames (CPINFO *);
198 extern Lisp_Object ansi_encode_filename (Lisp_Object);
199 extern int w32_copy_file (const char *, const char *, int, int, int);
200 extern int w32_accessible_directory_p (const char *, ptrdiff_t);
201
202 extern BOOL init_winsock (int load_now);
203 extern void srandom (int);
204 extern int random (void);
205
206 extern int fchmod (int, mode_t);
207 extern int sys_rename_replace (char const *, char const *, BOOL);
208 extern int pipe2 (int *, int);
209 extern void register_aux_fd (int);
210
211 extern void set_process_dir (char *);
212 extern int sys_spawnve (int, char *, char **, char **);
213 extern void register_child (pid_t, int);
214
215 extern void sys_sleep (int);
216 extern int sys_link (const char *, const char *);
217
218 /* Return total and free memory info. */
219 extern int w32_memory_info (unsigned long long *, unsigned long long *,
220 unsigned long long *, unsigned long long *);
221
222 /* Compare 2 UTF-8 strings in locale-dependent fashion. */
223 extern int w32_compare_strings (const char *, const char *, char *, int);
224
225 #ifdef HAVE_GNUTLS
226 #include <gnutls/gnutls.h>
227
228 /* GnuTLS pull (read from remote) interface. */
229 extern ssize_t emacs_gnutls_pull (gnutls_transport_ptr_t p,
230 void* buf, size_t sz);
231
232 /* GnuTLS push (write to remote) interface. */
233 extern ssize_t emacs_gnutls_push (gnutls_transport_ptr_t p,
234 const void* buf, size_t sz);
235 #endif /* HAVE_GNUTLS */
236
237 /* Definine a function that will be loaded from a DLL. */
238 #define DEF_DLL_FN(type, func, args) static type (FAR CDECL *fn_##func) args
239
240 /* Load a function from the DLL. */
241 #define LOAD_DLL_FN(lib, func) \
242 do \
243 { \
244 fn_##func = (void *) GetProcAddress (lib, #func); \
245 if (!fn_##func) \
246 return false; \
247 } \
248 while (false)
249
250 #endif /* EMACS_W32_H */