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