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