[project @ 2002-03-19 11:24:51 by simonmar]
[haskell-directory.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsBase.h,v 1.4 2002/03/19 11:24:52 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 #include "timeUtils.h"
105 #endif
106
107 /* in ghc_errno.c */
108 int *ghcErrno(void);
109
110 /* in system.c */
111 HsInt systemCmd(HsAddr cmd);
112
113 /* in inputReady.c */
114 int inputReady(int fd, int msecs, int isSock);
115
116 /* -----------------------------------------------------------------------------
117    64-bit operations, defined in longlong.c
118    -------------------------------------------------------------------------- */
119
120 #ifdef SUPPORT_LONG_LONGS
121
122 StgInt stg_gtWord64 (StgWord64, StgWord64);
123 StgInt stg_geWord64 (StgWord64, StgWord64);
124 StgInt stg_eqWord64 (StgWord64, StgWord64);
125 StgInt stg_neWord64 (StgWord64, StgWord64);
126 StgInt stg_ltWord64 (StgWord64, StgWord64);
127 StgInt stg_leWord64 (StgWord64, StgWord64);
128
129 StgInt stg_gtInt64 (StgInt64, StgInt64);
130 StgInt stg_geInt64 (StgInt64, StgInt64);
131 StgInt stg_eqInt64 (StgInt64, StgInt64);
132 StgInt stg_neInt64 (StgInt64, StgInt64);
133 StgInt stg_ltInt64 (StgInt64, StgInt64);
134 StgInt stg_leInt64 (StgInt64, StgInt64);
135
136 StgWord64 stg_remWord64  (StgWord64, StgWord64);
137 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
138
139 StgInt64 stg_remInt64    (StgInt64, StgInt64);
140 StgInt64 stg_quotInt64   (StgInt64, StgInt64);
141 StgInt64 stg_negateInt64 (StgInt64);
142 StgInt64 stg_plusInt64   (StgInt64, StgInt64);
143 StgInt64 stg_minusInt64  (StgInt64, StgInt64);
144 StgInt64 stg_timesInt64  (StgInt64, StgInt64);
145
146 StgWord64 stg_and64  (StgWord64, StgWord64);
147 StgWord64 stg_or64   (StgWord64, StgWord64);
148 StgWord64 stg_xor64  (StgWord64, StgWord64);
149 StgWord64 stg_not64  (StgWord64);
150
151 StgWord64 stg_uncheckedShiftL64   (StgWord64, StgInt);
152 StgWord64 stg_uncheckedShiftRL64  (StgWord64, StgInt);
153 StgInt64  stg_uncheckedIShiftL64  (StgInt64, StgInt);
154 StgInt64  stg_uncheckedIShiftRL64 (StgInt64, StgInt);
155 StgInt64  stg_uncheckedIShiftRA64 (StgInt64, StgInt);
156
157 StgInt64  stg_intToInt64    (StgInt);
158 StgInt    stg_int64ToInt    (StgInt64);
159 StgWord64 stg_int64ToWord64 (StgInt64);
160
161 StgWord64 stg_wordToWord64  (StgWord);
162 StgWord   stg_word64ToWord  (StgWord64);
163 StgInt64  stg_word64ToInt64 (StgWord64);
164
165 StgInt64  stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
166 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
167
168 #endif /* SUPPORT_LONG_LONGS */
169
170 /* -----------------------------------------------------------------------------
171    INLINE functions.
172
173    These functions are given as inlines here for when compiling via C,
174    but we also generate static versions into the cbits library for
175    when compiling to native code.
176    -------------------------------------------------------------------------- */
177
178 #ifndef INLINE
179 #define INLINE extern inline
180 #endif
181
182 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
183 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
184 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
185 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
186 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
187 #ifdef S_ISSOCK
188 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
189 #endif
190
191 #ifndef mingw32_TARGET_OS
192 INLINE void
193 __hscore_sigemptyset( sigset_t *set )
194 { sigemptyset(set); }
195 #endif
196
197 INLINE void *
198 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
199 { return memcpy(dst+dst_off, src, sz); }
200
201 INLINE void *
202 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
203 { return memcpy(dst, src+src_off, sz); }
204
205 INLINE HsBool
206 __hscore_supportsTextMode()
207 {
208 #if defined(mingw32_TARGET_OS)
209   return HS_BOOL_FALSE;
210 #else
211   return HS_BOOL_TRUE;
212 #endif
213 }
214
215 INLINE HsInt
216 __hscore_bufsiz()
217 {
218   return BUFSIZ;
219 }
220
221 INLINE HsInt
222 __hscore_seek_cur()
223 {
224   return SEEK_CUR;
225 }
226
227 INLINE HsInt
228 __hscore_o_binary()
229 {
230 #ifdef HAVE_O_BINARY
231   return O_BINARY;
232 #else
233   return 0;
234 #endif
235 }
236
237 INLINE int
238 __hscore_o_rdonly()
239 {
240 #ifdef O_RDONLY
241   return O_RDONLY;
242 #else
243   return 0;
244 #endif
245 }
246
247 INLINE int
248 __hscore_o_wronly( void )
249 {
250 #ifdef O_WRONLY
251   return O_WRONLY;
252 #else
253   return 0;
254 #endif
255 }
256
257 INLINE int
258 __hscore_o_rdwr( void )
259 {
260 #ifdef O_RDWR
261   return O_RDWR;
262 #else
263   return 0;
264 #endif
265 }
266
267 INLINE int
268 __hscore_o_append( void )
269 {
270 #ifdef O_APPEND
271   return O_APPEND;
272 #else
273   return 0;
274 #endif
275 }
276
277 INLINE int
278 __hscore_o_creat( void )
279 {
280 #ifdef O_CREAT
281   return O_CREAT;
282 #else
283   return 0;
284 #endif
285 }
286
287 INLINE int
288 __hscore_o_excl( void )
289 {
290 #ifdef O_EXCL
291   return O_EXCL;
292 #else
293   return 0;
294 #endif
295 }
296
297 INLINE int
298 __hscore_o_trunc( void )
299 {
300 #ifdef O_TRUNC
301   return O_TRUNC;
302 #else
303   return 0;
304 #endif
305 }
306
307 INLINE int
308 __hscore_o_noctty( void )
309 {
310 #ifdef O_NOCTTY
311   return O_NOCTTY;
312 #else
313   return 0;
314 #endif
315 }
316
317 INLINE int
318 __hscore_o_nonblock( void )
319 {
320 #ifdef O_NONBLOCK
321   return O_NONBLOCK;
322 #else
323   return 0;
324 #endif
325 }
326
327 INLINE HsInt
328 __hscore_seek_set( void )
329 {
330   return SEEK_SET;
331 }
332
333 INLINE HsInt
334 __hscore_seek_end( void )
335 {
336   return SEEK_END;
337 }
338
339 INLINE HsInt
340 __hscore_setmode( HsInt fd, HsBool toBin )
341 {
342 #ifdef _WIN32
343   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
344 #else
345   return 0;
346 #endif  
347 }
348
349 INLINE HsInt
350 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
351                            HsInt off, int sz )
352 {
353 #ifdef _WIN32
354   if (isSock) {
355     return send(fd,ptr + off, sz, 0);
356   }
357 #endif
358   return write(fd,ptr + off, sz);
359 }
360
361 INLINE HsInt
362 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
363                           HsInt off, int sz )
364 {
365 #ifdef _WIN32
366   if (isSock) {
367     return recv(fd,ptr + off, sz, 0);
368   }
369 #endif
370   return read(fd,ptr + off, sz);
371
372 }
373
374 #ifdef mingw32_TARGET_OS
375 INLINE long *
376 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
377
378 INLINE char **
379 __hscore_Time_ghcTzname( void ) { return _tzname; }
380 #endif
381
382 INLINE HsInt
383 __hscore_mkdir( HsAddr pathName, HsInt mode )
384 {
385 #if defined(mingw32_TARGET_OS)
386   return mkdir(pathName);
387 #else
388   return mkdir(pathName,mode);
389 #endif
390 }
391
392 INLINE HsInt
393 __hscore_lstat( HsAddr fname, HsAddr st )
394 {
395 #ifdef HAVE_LSTAT
396   return lstat((const char*)fname, (struct stat*)st);
397 #else
398   return stat((const char*)fname, (struct stat*)st);
399 #endif
400 }
401
402 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
403
404 INLINE mode_t __hscore_R_OK() { return R_OK; }
405 INLINE mode_t __hscore_W_OK() { return W_OK; }
406 INLINE mode_t __hscore_X_OK() { return X_OK; }
407
408 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
409 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
410 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
411
412 INLINE HsAddr
413 __hscore_d_name( struct dirent* d )
414
415 #ifndef mingw32_TARGET_OS
416   return (HsAddr)(&d->d_name);
417 #else
418   return (HsAddr)(d->d_name);
419 #endif
420 }
421
422 INLINE HsInt
423 __hscore_end_of_dir( void )
424 {
425 #ifndef mingw32_TARGET_OS
426   return 0;
427 #else
428   return ENOENT;
429 #endif  
430 }
431
432 INLINE void
433 __hscore_free_dirent(HsAddr dEnt)
434 {
435 #if HAVE_READDIR_R
436   free(dEnt);
437 #endif
438 }
439
440 INLINE HsInt
441 __hscore_sizeof_stat( void )
442 {
443   return sizeof(struct stat);
444 }
445
446 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
447 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
448 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
449
450 #if HAVE_TERMIOS_H
451 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
452
453 INLINE void
454 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
455
456 INLINE unsigned char*
457 __hscore_ptr_c_cc( struct termios* ts ) 
458 { return (unsigned char*) &ts->c_cc; }
459 #endif
460
461 INLINE HsInt
462 __hscore_sizeof_termios( void )
463 {
464 #ifndef mingw32_TARGET_OS
465   return sizeof(struct termios);
466 #else
467   return 0;
468 #endif
469 }
470
471 INLINE HsInt
472 __hscore_sizeof_sigset_t( void )
473 {
474 #ifndef mingw32_TARGET_OS
475   return sizeof(sigset_t);
476 #else
477   return 0;
478 #endif
479 }
480
481 INLINE int
482 __hscore_echo( void )
483 {
484 #ifdef ECHO
485   return ECHO;
486 #else
487   return 0;
488 #endif
489
490 }
491
492 INLINE int
493 __hscore_tcsanow( void )
494 {
495 #ifdef TCSANOW
496   return TCSANOW;
497 #else
498   return 0;
499 #endif
500
501 }
502
503 INLINE int
504 __hscore_icanon( void )
505 {
506 #ifdef ICANON
507   return ICANON;
508 #else
509   return 0;
510 #endif
511 }
512
513 INLINE int __hscore_vmin( void )
514 {
515 #ifdef VMIN
516   return VMIN;
517 #else
518   return 0;
519 #endif
520 }
521
522 INLINE int __hscore_vtime( void )
523 {
524 #ifdef VTIME
525   return VTIME;
526 #else
527   return 0;
528 #endif
529 }
530
531 INLINE int __hscore_sigttou( void )
532 {
533 #ifdef SIGTTOU
534   return SIGTTOU;
535 #else
536   return 0;
537 #endif
538 }
539
540 INLINE int __hscore_sig_block( void )
541 {
542 #ifdef SIG_BLOCK
543   return SIG_BLOCK;
544 #else
545   return 0;
546 #endif
547 }
548
549 INLINE int __hscore_sig_setmask( void )
550 {
551 #ifdef SIG_SETMASK
552   return SIG_SETMASK;
553 #else
554   return 0;
555 #endif
556 }
557
558 INLINE int
559 __hscore_f_getfl( void )
560 {
561 #ifdef F_GETFL
562   return F_GETFL;
563 #else
564   return 0;
565 #endif
566 }
567
568 INLINE int
569 __hscore_f_setfl( void )
570 {
571 #ifdef F_SETFL
572   return F_SETFL;
573 #else
574   return 0;
575 #endif
576 }
577
578 #endif
579