makefiles: Always use the global SOURCES variable for .idl files.
[wine.git] / include / wine / unixlib.h
blob8a9522ec5329cba1b2a9028d84c39021e1f9b1fe
1 /*
2 * Definitions for Unix libraries
4 * Copyright (C) 2021 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_UNIXLIB_H
22 #define __WINE_WINE_UNIXLIB_H
24 #include "winternl.h"
26 typedef UINT64 unixlib_handle_t;
28 #ifdef WINE_UNIX_LIB
30 typedef NTSTATUS (*unixlib_entry_t)( void *args );
32 /* some useful helpers from ntdll */
33 extern const char *ntdll_get_build_dir(void);
34 extern const char *ntdll_get_data_dir(void);
35 extern DWORD ntdll_umbstowcs( const char *src, DWORD srclen, WCHAR *dst, DWORD dstlen );
36 extern int ntdll_wcstoumbs( const WCHAR *src, DWORD srclen, char *dst, DWORD dstlen, BOOL strict );
37 extern int ntdll_wcsicmp( const WCHAR *str1, const WCHAR *str2 );
38 extern int ntdll_wcsnicmp( const WCHAR *str1, const WCHAR *str2, int n );
39 extern NTSTATUS ntdll_init_syscalls( SYSTEM_SERVICE_TABLE *table, void **dispatcher );
41 /* exception handling */
43 #ifdef __i386__
44 typedef struct { int reg[16]; } __wine_jmp_buf;
45 #elif defined(__x86_64__)
46 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
47 #elif defined(__arm__)
48 typedef struct { int reg[28]; } __wine_jmp_buf;
49 #elif defined(__aarch64__)
50 typedef struct { __int64 reg[24]; } __wine_jmp_buf;
51 #else
52 typedef struct { int reg; } __wine_jmp_buf;
53 #endif
55 extern int __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
56 EXCEPTION_REGISTRATION_RECORD *frame );
57 extern void DECLSPEC_NORETURN __wine_longjmp( __wine_jmp_buf *buf, int retval );
58 extern void ntdll_set_exception_jmp_buf( __wine_jmp_buf *jmp );
60 #define __TRY \
61 do { __wine_jmp_buf __jmp; \
62 int __first = 1; \
63 for (;;) if (!__first) \
64 { \
65 do {
67 #define __EXCEPT \
68 } while(0); \
69 ntdll_set_exception_jmp_buf( NULL ); \
70 break; \
71 } else { \
72 if (__wine_setjmpex( &__jmp, NULL )) { \
73 do {
75 #define __ENDTRY \
76 } while (0); \
77 break; \
78 } \
79 ntdll_set_exception_jmp_buf( &__jmp ); \
80 __first = 0; \
81 } \
82 } while (0);
84 NTSTATUS KeUserModeCallback( ULONG id, const void *args, ULONG len, void **ret_ptr, ULONG *ret_len );
86 /* wide char string functions */
88 static inline int ntdll_iswspace( WCHAR wc )
90 return ('\t' <= wc && wc <= '\r') || wc == ' ' || wc == 0xa0;
93 static inline size_t ntdll_wcslen( const WCHAR *str )
95 const WCHAR *s = str;
96 while (*s) s++;
97 return s - str;
100 static inline WCHAR *ntdll_wcscpy( WCHAR *dst, const WCHAR *src )
102 WCHAR *p = dst;
103 while ((*p++ = *src++));
104 return dst;
107 static inline WCHAR *ntdll_wcscat( WCHAR *dst, const WCHAR *src )
109 ntdll_wcscpy( dst + ntdll_wcslen(dst), src );
110 return dst;
113 static inline int ntdll_wcscmp( const WCHAR *str1, const WCHAR *str2 )
115 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
116 return *str1 - *str2;
119 static inline int ntdll_wcsncmp( const WCHAR *str1, const WCHAR *str2, int n )
121 if (n <= 0) return 0;
122 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
123 return *str1 - *str2;
126 static inline WCHAR *ntdll_wcschr( const WCHAR *str, WCHAR ch )
128 do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
129 return NULL;
132 static inline WCHAR *ntdll_wcsrchr( const WCHAR *str, WCHAR ch )
134 WCHAR *ret = NULL;
135 do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
136 return ret;
139 static inline WCHAR *ntdll_wcspbrk( const WCHAR *str, const WCHAR *accept )
141 for ( ; *str; str++) if (ntdll_wcschr( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
142 return NULL;
145 static inline SIZE_T ntdll_wcsspn( const WCHAR *str, const WCHAR *accept )
147 const WCHAR *ptr;
148 for (ptr = str; *ptr; ptr++) if (!ntdll_wcschr( accept, *ptr )) break;
149 return ptr - str;
152 static inline SIZE_T ntdll_wcscspn( const WCHAR *str, const WCHAR *reject )
154 const WCHAR *ptr;
155 for (ptr = str; *ptr; ptr++) if (ntdll_wcschr( reject, *ptr )) break;
156 return ptr - str;
159 static inline LONG ntdll_wcstol( const WCHAR *s, WCHAR **end, int base )
161 BOOL negative = FALSE, empty = TRUE;
162 LONG ret = 0;
164 if (base < 0 || base == 1 || base > 36) return 0;
165 if (end) *end = (WCHAR *)s;
166 while (ntdll_iswspace(*s)) s++;
168 if (*s == '-')
170 negative = TRUE;
171 s++;
173 else if (*s == '+') s++;
175 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
177 base = 16;
178 s += 2;
180 if (base == 0) base = s[0] != '0' ? 10 : 8;
182 while (*s)
184 int v;
186 if ('0' <= *s && *s <= '9') v = *s - '0';
187 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
188 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
189 else break;
190 if (v >= base) break;
191 if (negative) v = -v;
192 s++;
193 empty = FALSE;
195 if (!negative && (ret > MAXLONG / base || ret * base > MAXLONG - v))
196 ret = MAXLONG;
197 else if (negative && (ret < (LONG)MINLONG / base || ret * base < (LONG)(MINLONG - v)))
198 ret = MINLONG;
199 else
200 ret = ret * base + v;
203 if (end && !empty) *end = (WCHAR *)s;
204 return ret;
207 static inline ULONG ntdll_wcstoul( const WCHAR *s, WCHAR **end, int base )
209 BOOL negative = FALSE, empty = TRUE;
210 ULONG ret = 0;
212 if (base < 0 || base == 1 || base > 36) return 0;
213 if (end) *end = (WCHAR *)s;
214 while (ntdll_iswspace(*s)) s++;
216 if (*s == '-')
218 negative = TRUE;
219 s++;
221 else if (*s == '+') s++;
223 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
225 base = 16;
226 s += 2;
228 if (base == 0) base = s[0] != '0' ? 10 : 8;
230 while (*s)
232 int v;
234 if ('0' <= *s && *s <= '9') v = *s - '0';
235 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
236 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
237 else break;
238 if (v >= base) break;
239 s++;
240 empty = FALSE;
242 if (ret > MAXDWORD / base || ret * base > MAXDWORD - v)
243 ret = MAXDWORD;
244 else
245 ret = ret * base + v;
248 if (end && !empty) *end = (WCHAR *)s;
249 return negative ? -ret : ret;
252 #define iswspace(ch) ntdll_iswspace(ch)
253 #define wcslen(str) ntdll_wcslen(str)
254 #define wcscpy(dst,src) ntdll_wcscpy(dst,src)
255 #define wcscat(dst,src) ntdll_wcscat(dst,src)
256 #define wcscmp(s1,s2) ntdll_wcscmp(s1,s2)
257 #define wcsncmp(s1,s2,n) ntdll_wcsncmp(s1,s2,n)
258 #define wcschr(str,ch) ntdll_wcschr(str,ch)
259 #define wcsrchr(str,ch) ntdll_wcsrchr(str,ch)
260 #define wcspbrk(str,ac) ntdll_wcspbrk(str,ac)
261 #define wcsspn(str,ac) ntdll_wcsspn(str,ac)
262 #define wcscspn(str,rej) ntdll_wcscspn(str,rej)
263 #define wcsicmp(s1, s2) ntdll_wcsicmp(s1,s2)
264 #define wcsnicmp(s1, s2,n) ntdll_wcsnicmp(s1,s2,n)
265 #define wcstol(str,e,b) ntdll_wcstol(str,e,b)
266 #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
268 #else /* WINE_UNIX_LIB */
270 extern NTSTATUS WINAPI __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args );
271 extern unixlib_handle_t __wine_unixlib_handle DECLSPEC_HIDDEN;
272 extern NTSTATUS (WINAPI *__wine_unix_call_dispatcher)( unixlib_handle_t, unsigned int, void * ) DECLSPEC_HIDDEN;
273 extern NTSTATUS WINAPI __wine_init_unix_call(void) DECLSPEC_HIDDEN;
275 #define WINE_UNIX_CALL(code,args) __wine_unix_call_dispatcher( __wine_unixlib_handle, (code), (args) )
277 #endif /* WINE_UNIX_LIB */
279 #endif /* __WINE_WINE_UNIXLIB_H */