[project @ 2002-02-07 11:13:29 by simonmar]
[ghc-base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsBase.h,v 1.1 2002/02/07 11:13:30 simonmar Exp $
3  *
4  * (c) The University of Glasgow 2001-2002
5  *
6  * Definitions for package `core' which are visible in Haskell land.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef HSCORE_H
11 #define HSCORE_H
12
13 #include "config.h"
14 #include "HsFFI.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif
28 #ifdef HAVE_FCNTL_H
29 # include <fcntl.h>
30 #endif
31 #ifdef HAVE_TERMIOS_H
32 #include <termios.h>
33 #endif
34 #ifdef HAVE_SIGNAL_H
35 #include <signal.h>
36 #endif
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43 #ifdef HAVE_DIRENT_H
44 #include <dirent.h>
45 #endif
46 #ifdef HAVE_UTIME_H
47 #include <utime.h>
48 #endif
49 #if defined(HAVE_GETTIMEOFDAY)
50 #  ifdef HAVE_SYS_TIME_H
51 #   include <sys/time.h>
52 #  endif
53 #elif defined(HAVE_GETCLOCK)
54 # ifdef HAVE_SYS_TIMERS_H
55 #  define POSIX_4D9 1
56 #  include <sys/timers.h>
57 # endif
58 #endif
59 #if defined(HAVE_TIME_H)
60 # include <time.h>
61 #endif
62 #ifdef HAVE_SYS_TIMEB_H
63 #include <sys/timeb.h>
64 #endif
65 #ifdef HAVE_WINDOWS_H
66 #include <windows.h>
67 #endif
68 #ifdef HAVE_SYS_TIMES_H
69 #include <sys/times.h>
70 #endif
71 #ifdef HAVE_WINSOCK_H
72 #include <winsock.h>
73 #endif
74
75 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
76 # if defined(HAVE_SYS_RESOURCE_H)
77 #  include <sys/resource.h>
78 # endif
79 #endif
80
81 #ifdef hpux_TARGET_OS
82 #include <sys/syscall.h>
83 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
84 #define HAVE_GETRUSAGE
85 #endif
86
87 /* For System */
88 #ifdef HAVE_SYS_WAIT_H
89 #include <sys/wait.h>
90 #endif
91 #ifdef HAVE_VFORK_H
92 #include <vfork.h>
93 #endif
94 #include "lockFile.h"
95 #include "dirUtils.h"
96 #include "errUtils.h"
97
98 #ifdef _WIN32
99 #include <io.h>
100 #include <fcntl.h>
101 #endif
102
103 /* in ghc_errno.c */
104 int *ghcErrno(void);
105
106 /* in system.c */
107 HsInt systemCmd(HsAddr cmd);
108
109 /* in inputReady.c */
110 int inputReady(int fd, int msecs, int isSock);
111
112 /* -----------------------------------------------------------------------------
113    INLINE functions.
114
115    These functions are given as inlines here for when compiling via C,
116    but we also generate static versions into the cbits library for
117    when compiling to native code.
118    -------------------------------------------------------------------------- */
119
120 #ifndef INLINE
121 #define INLINE extern inline
122 #endif
123
124 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
125 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
126 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
127 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
128 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
129 #ifdef S_ISSOCK
130 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
131 #endif
132
133 #ifndef mingw32_TARGET_OS
134 INLINE void
135 __hscore_sigemptyset( sigset_t *set )
136 { sigemptyset(set); }
137 #endif
138
139 INLINE void *
140 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
141 { return memcpy(dst+dst_off, src, sz); }
142
143 INLINE void *
144 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
145 { return memcpy(dst, src+src_off, sz); }
146
147 INLINE HsBool
148 __hscore_supportsTextMode()
149 {
150 #if defined(mingw32_TARGET_OS)
151   return HS_BOOL_FALSE;
152 #else
153   return HS_BOOL_TRUE;
154 #endif
155 }
156
157 INLINE HsInt
158 __hscore_bufsiz()
159 {
160   return BUFSIZ;
161 }
162
163 INLINE HsInt
164 __hscore_seek_cur()
165 {
166   return SEEK_CUR;
167 }
168
169 INLINE HsInt
170 __hscore_o_binary()
171 {
172 #ifdef HAVE_O_BINARY
173   return O_BINARY;
174 #else
175   return 0;
176 #endif
177 }
178
179 INLINE int
180 __hscore_o_rdonly()
181 {
182 #ifdef O_RDONLY
183   return O_RDONLY;
184 #else
185   return 0;
186 #endif
187 }
188
189 INLINE int
190 __hscore_o_wronly( void )
191 {
192 #ifdef O_WRONLY
193   return O_WRONLY;
194 #else
195   return 0;
196 #endif
197 }
198
199 INLINE int
200 __hscore_o_rdwr( void )
201 {
202 #ifdef O_RDWR
203   return O_RDWR;
204 #else
205   return 0;
206 #endif
207 }
208
209 INLINE int
210 __hscore_o_append( void )
211 {
212 #ifdef O_APPEND
213   return O_APPEND;
214 #else
215   return 0;
216 #endif
217 }
218
219 INLINE int
220 __hscore_o_creat( void )
221 {
222 #ifdef O_CREAT
223   return O_CREAT;
224 #else
225   return 0;
226 #endif
227 }
228
229 INLINE int
230 __hscore_o_excl( void )
231 {
232 #ifdef O_EXCL
233   return O_EXCL;
234 #else
235   return 0;
236 #endif
237 }
238
239 INLINE int
240 __hscore_o_trunc( void )
241 {
242 #ifdef O_TRUNC
243   return O_TRUNC;
244 #else
245   return 0;
246 #endif
247 }
248
249 INLINE int
250 __hscore_o_noctty( void )
251 {
252 #ifdef O_NOCTTY
253   return O_NOCTTY;
254 #else
255   return 0;
256 #endif
257 }
258
259 INLINE int
260 __hscore_o_nonblock( void )
261 {
262 #ifdef O_NONBLOCK
263   return O_NONBLOCK;
264 #else
265   return 0;
266 #endif
267 }
268
269 INLINE HsInt
270 __hscore_seek_set( void )
271 {
272   return SEEK_SET;
273 }
274
275 INLINE HsInt
276 __hscore_seek_end( void )
277 {
278   return SEEK_END;
279 }
280
281 INLINE HsInt
282 __hscore_setmode( HsInt fd, HsBool toBin )
283 {
284 #ifdef _WIN32
285   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
286 #else
287   return 0;
288 #endif  
289 }
290
291 INLINE HsInt
292 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
293                            HsInt off, int sz )
294 {
295 #ifdef _WIN32
296   if (isSock) {
297     return send(fd,ptr + off, sz, 0);
298   }
299 #endif
300   return write(fd,ptr + off, sz);
301 }
302
303 INLINE HsInt
304 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
305                           HsInt off, int sz )
306 {
307 #ifdef _WIN32
308   if (isSock) {
309     return recv(fd,ptr + off, sz, 0);
310   }
311 #endif
312   return read(fd,ptr + off, sz);
313
314 }
315
316 #ifdef mingw32_TARGET_OS
317 INLINE long *
318 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
319
320 INLINE char **
321 __hscore_Time_ghcTzname( void ) { return _tzname; }
322 #endif
323
324 INLINE HsInt
325 __hscore_mkdir( HsAddr pathName, HsInt mode )
326 {
327 #if defined(mingw32_TARGET_OS)
328   return mkdir(pathName);
329 #else
330   return mkdir(pathName,mode);
331 #endif
332 }
333
334 INLINE HsInt
335 __hscore_lstat( HsAddr fname, HsAddr st )
336 {
337 #ifdef HAVE_LSTAT
338   return lstat((const char*)fname, (struct stat*)st);
339 #else
340   return stat((const char*)fname, (struct stat*)st);
341 #endif
342 }
343
344 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
345
346 INLINE mode_t __hscore_R_OK() { return R_OK; }
347 INLINE mode_t __hscore_W_OK() { return W_OK; }
348 INLINE mode_t __hscore_X_OK() { return X_OK; }
349
350 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
351 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
352 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
353
354 INLINE HsAddr
355 __hscore_d_name( struct dirent* d )
356
357 #ifndef mingw32_TARGET_OS
358   return (HsAddr)(&d->d_name);
359 #else
360   return (HsAddr)(d->d_name);
361 #endif
362 }
363
364 INLINE HsInt
365 __hscore_end_of_dir( void )
366 {
367 #ifndef mingw32_TARGET_OS
368   return 0;
369 #else
370   return ENOENT;
371 #endif  
372 }
373
374 INLINE void
375 __hscore_free_dirent(HsAddr dEnt)
376 {
377 #if HAVE_READDIR_R
378   free(dEnt);
379 #endif
380 }
381
382 INLINE HsInt
383 __hscore_sizeof_stat( void )
384 {
385   return sizeof(struct stat);
386 }
387
388 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
389 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
390 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
391
392 #if HAVE_TERMIOS_H
393 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
394
395 INLINE void
396 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
397
398 INLINE unsigned char*
399 __hscore_ptr_c_cc( struct termios* ts ) 
400 { return (unsigned char*) &ts->c_cc; }
401 #endif
402
403 INLINE HsInt
404 __hscore_sizeof_termios( void )
405 {
406 #ifndef mingw32_TARGET_OS
407   return sizeof(struct termios);
408 #else
409   return 0;
410 #endif
411 }
412
413 INLINE HsInt
414 __hscore_sizeof_sigset_t( void )
415 {
416 #ifndef mingw32_TARGET_OS
417   return sizeof(sigset_t);
418 #else
419   return 0;
420 #endif
421 }
422
423 INLINE int
424 __hscore_echo( void )
425 {
426 #ifdef ECHO
427   return ECHO;
428 #else
429   return 0;
430 #endif
431
432 }
433
434 INLINE int
435 __hscore_tcsanow( void )
436 {
437 #ifdef TCSANOW
438   return TCSANOW;
439 #else
440   return 0;
441 #endif
442
443 }
444
445 INLINE int
446 __hscore_icanon( void )
447 {
448 #ifdef ICANON
449   return ICANON;
450 #else
451   return 0;
452 #endif
453 }
454
455 INLINE int __hscore_vmin( void )
456 {
457 #ifdef VMIN
458   return VMIN;
459 #else
460   return 0;
461 #endif
462 }
463
464 INLINE int __hscore_vtime( void )
465 {
466 #ifdef VTIME
467   return VTIME;
468 #else
469   return 0;
470 #endif
471 }
472
473 INLINE int __hscore_sigttou( void )
474 {
475 #ifdef SIGTTOU
476   return SIGTTOU;
477 #else
478   return 0;
479 #endif
480 }
481
482 INLINE int __hscore_sig_block( void )
483 {
484 #ifdef SIG_BLOCK
485   return SIG_BLOCK;
486 #else
487   return 0;
488 #endif
489 }
490
491 INLINE int __hscore_sig_setmask( void )
492 {
493 #ifdef SIG_SETMASK
494   return SIG_SETMASK;
495 #else
496   return 0;
497 #endif
498 }
499
500 INLINE int
501 __hscore_f_getfl( void )
502 {
503 #ifdef F_GETFL
504   return F_GETFL;
505 #else
506   return 0;
507 #endif
508 }
509
510 INLINE int
511 __hscore_f_setfl( void )
512 {
513 #ifdef F_SETFL
514   return F_SETFL;
515 #else
516   return 0;
517 #endif
518 }
519
520 #endif
521