[project @ 2003-09-02 16:07:08 by ross]
[packages/base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2 * $Id: HsBase.h,v 1.25 2003/09/02 16:07:11 ross Exp $
3 *
4 * (c) The University of Glasgow 2001-2002
5 *
6 * Definitions for package `base' which are visible in Haskell land.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #ifndef __HSBASE_H__
11 #define __HSBASE_H__
12
13 #include "config.h"
14 #include "HsFFI.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #ifdef HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif
32 #ifdef HAVE_TERMIOS_H
33 #include <termios.h>
34 #endif
35 #ifdef HAVE_SIGNAL_H
36 #include <signal.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #ifdef HAVE_DIRENT_H
45 #include <dirent.h>
46 #endif
47 #ifdef HAVE_UTIME_H
48 #include <utime.h>
49 #endif
50 #ifdef HAVE_SYS_UTSNAME_H
51 #include <sys/utsname.h>
52 #endif
53 #if defined(HAVE_GETTIMEOFDAY)
54 # ifdef HAVE_SYS_TIME_H
55 # include <sys/time.h>
56 # endif
57 #elif defined(HAVE_GETCLOCK)
58 # ifdef HAVE_SYS_TIMERS_H
59 # define POSIX_4D9 1
60 # include <sys/timers.h>
61 # endif
62 #endif
63 #if defined(HAVE_TIME_H)
64 # include <time.h>
65 #endif
66 #ifdef HAVE_SYS_TIMEB_H
67 #include <sys/timeb.h>
68 #endif
69 #ifdef HAVE_WINDOWS_H
70 #include <windows.h>
71 #endif
72 #ifdef HAVE_SYS_TIMES_H
73 #include <sys/times.h>
74 #endif
75 #if defined(HAVE_WINSOCK_H) && defined(__MINGW32__)
76 #include <winsock.h>
77 #endif
78 #ifdef HAVE_LIMITS_H
79 #include <limits.h>
80 #endif
81 #ifdef HAVE_WCTYPE_H
82 #include <wctype.h>
83 #endif
84
85 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
86 # if defined(HAVE_SYS_RESOURCE_H)
87 # include <sys/resource.h>
88 # endif
89 #endif
90
91 #ifdef hpux_TARGET_OS
92 #include <sys/syscall.h>
93 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
94 #define HAVE_GETRUSAGE
95 #endif
96
97 /* For System */
98 #ifdef HAVE_SYS_WAIT_H
99 #include <sys/wait.h>
100 #endif
101 #ifdef HAVE_VFORK_H
102 #include <vfork.h>
103 #endif
104 #include "lockFile.h"
105 #include "dirUtils.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 rawSystem.c */
120 #if defined(mingw32_TARGET_OS)
121 HsInt rawSystem(HsAddr cmd);
122 #else
123 HsInt rawSystem(HsAddr cmd, HsAddr args);
124 #endif
125
126 /* in inputReady.c */
127 int inputReady(int fd, int msecs, int isSock);
128
129 /* in writeError.c */
130 void writeErrString__(HsAddr msg_hdr, HsAddr msg, HsInt len);
131
132 /* -----------------------------------------------------------------------------
133 64-bit operations, defined in longlong.c
134 -------------------------------------------------------------------------- */
135
136 #ifdef SUPPORT_LONG_LONGS
137
138 StgInt stg_gtWord64 (StgWord64, StgWord64);
139 StgInt stg_geWord64 (StgWord64, StgWord64);
140 StgInt stg_eqWord64 (StgWord64, StgWord64);
141 StgInt stg_neWord64 (StgWord64, StgWord64);
142 StgInt stg_ltWord64 (StgWord64, StgWord64);
143 StgInt stg_leWord64 (StgWord64, StgWord64);
144
145 StgInt stg_gtInt64 (StgInt64, StgInt64);
146 StgInt stg_geInt64 (StgInt64, StgInt64);
147 StgInt stg_eqInt64 (StgInt64, StgInt64);
148 StgInt stg_neInt64 (StgInt64, StgInt64);
149 StgInt stg_ltInt64 (StgInt64, StgInt64);
150 StgInt stg_leInt64 (StgInt64, StgInt64);
151
152 StgWord64 stg_remWord64 (StgWord64, StgWord64);
153 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
154
155 StgInt64 stg_remInt64 (StgInt64, StgInt64);
156 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
157 StgInt64 stg_negateInt64 (StgInt64);
158 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
159 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
160 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
161
162 StgWord64 stg_and64 (StgWord64, StgWord64);
163 StgWord64 stg_or64 (StgWord64, StgWord64);
164 StgWord64 stg_xor64 (StgWord64, StgWord64);
165 StgWord64 stg_not64 (StgWord64);
166
167 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
168 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
169 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
170 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
171 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
172
173 StgInt64 stg_intToInt64 (StgInt);
174 StgInt stg_int64ToInt (StgInt64);
175 StgWord64 stg_int64ToWord64 (StgInt64);
176
177 StgWord64 stg_wordToWord64 (StgWord);
178 StgWord stg_word64ToWord (StgWord64);
179 StgInt64 stg_word64ToInt64 (StgWord64);
180
181 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
182 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
183
184 #endif /* SUPPORT_LONG_LONGS */
185
186 /* -----------------------------------------------------------------------------
187 INLINE functions.
188
189 These functions are given as inlines here for when compiling via C,
190 but we also generate static versions into the cbits library for
191 when compiling to native code.
192 -------------------------------------------------------------------------- */
193
194 #ifndef INLINE
195 #define INLINE extern inline
196 #endif
197
198 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
199 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
200 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
201 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
202 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
203 #ifdef S_ISSOCK
204 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
205 #endif
206
207 #ifndef mingw32_TARGET_OS
208 INLINE int
209 __hscore_sigemptyset( sigset_t *set )
210 { return sigemptyset(set); }
211
212 INLINE int
213 __hscore_sigfillset( sigset_t *set )
214 { return sigfillset(set); }
215
216 INLINE int
217 __hscore_sigaddset( sigset_t * set, int s )
218 { return sigaddset(set,s); }
219
220 INLINE int
221 __hscore_sigdelset( sigset_t * set, int s )
222 { return sigdelset(set,s); }
223
224 INLINE int
225 __hscore_sigismember( sigset_t * set, int s )
226 { return sigismember(set,s); }
227 #endif
228
229 INLINE void *
230 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
231 { return memcpy(dst+dst_off, src, sz); }
232
233 INLINE void *
234 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
235 { return memcpy(dst, src+src_off, sz); }
236
237 INLINE HsBool
238 __hscore_supportsTextMode()
239 {
240 #if defined(mingw32_TARGET_OS)
241 return HS_BOOL_FALSE;
242 #else
243 return HS_BOOL_TRUE;
244 #endif
245 }
246
247 INLINE HsInt
248 __hscore_bufsiz()
249 {
250 return BUFSIZ;
251 }
252
253 INLINE HsInt
254 __hscore_seek_cur()
255 {
256 return SEEK_CUR;
257 }
258
259 INLINE HsInt
260 __hscore_o_binary()
261 {
262 return CONST_O_BINARY;
263 }
264
265 INLINE int
266 __hscore_o_rdonly()
267 {
268 #ifdef O_RDONLY
269 return O_RDONLY;
270 #else
271 return 0;
272 #endif
273 }
274
275 INLINE int
276 __hscore_o_wronly( void )
277 {
278 #ifdef O_WRONLY
279 return O_WRONLY;
280 #else
281 return 0;
282 #endif
283 }
284
285 INLINE int
286 __hscore_o_rdwr( void )
287 {
288 #ifdef O_RDWR
289 return O_RDWR;
290 #else
291 return 0;
292 #endif
293 }
294
295 INLINE int
296 __hscore_o_append( void )
297 {
298 #ifdef O_APPEND
299 return O_APPEND;
300 #else
301 return 0;
302 #endif
303 }
304
305 INLINE int
306 __hscore_o_creat( void )
307 {
308 #ifdef O_CREAT
309 return O_CREAT;
310 #else
311 return 0;
312 #endif
313 }
314
315 INLINE int
316 __hscore_o_excl( void )
317 {
318 #ifdef O_EXCL
319 return O_EXCL;
320 #else
321 return 0;
322 #endif
323 }
324
325 INLINE int
326 __hscore_o_trunc( void )
327 {
328 #ifdef O_TRUNC
329 return O_TRUNC;
330 #else
331 return 0;
332 #endif
333 }
334
335 INLINE int
336 __hscore_o_noctty( void )
337 {
338 #ifdef O_NOCTTY
339 return O_NOCTTY;
340 #else
341 return 0;
342 #endif
343 }
344
345 INLINE int
346 __hscore_o_nonblock( void )
347 {
348 #ifdef O_NONBLOCK
349 return O_NONBLOCK;
350 #else
351 return 0;
352 #endif
353 }
354
355 INLINE HsInt
356 __hscore_seek_set( void )
357 {
358 return SEEK_SET;
359 }
360
361 INLINE HsInt
362 __hscore_seek_end( void )
363 {
364 return SEEK_END;
365 }
366
367 INLINE HsInt
368 __hscore_setmode( HsInt fd, HsBool toBin )
369 {
370 #if defined(__MINGW32__)
371 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
372 #else
373 return 0;
374 #endif
375 }
376
377 INLINE HsInt
378 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr,
379 HsInt off, int sz )
380 {
381 #if defined(__MINGW32__)
382 if (isSock) {
383 return send(fd,(char *)ptr + off, sz, 0);
384 }
385 #endif
386 return write(fd,(char *)ptr + off, sz);
387 }
388
389 INLINE HsInt
390 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr,
391 HsInt off, int sz )
392 {
393 #if defined(__MINGW32__)
394 if (isSock) {
395 return recv(fd,(char *)ptr + off, sz, 0);
396 }
397 #endif
398 return read(fd,(char *)ptr + off, sz);
399
400 }
401
402 #if defined(__MINGW32__)
403 INLINE long *
404 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
405
406 INLINE char **
407 __hscore_Time_ghcTzname( void ) { return _tzname; }
408 #endif
409
410 INLINE HsInt
411 __hscore_mkdir( HsAddr pathName, HsInt mode )
412 {
413 #if defined(__MINGW32__)
414 return mkdir(pathName);
415 #else
416 return mkdir(pathName,mode);
417 #endif
418 }
419
420 INLINE HsInt
421 __hscore_lstat( HsAddr fname, HsAddr st )
422 {
423 #ifdef HAVE_LSTAT
424 return lstat((const char*)fname, (struct stat*)st);
425 #else
426 return stat((const char*)fname, (struct stat*)st);
427 #endif
428 }
429
430 #ifdef PATH_MAX
431 /* A size that will contain many path names, but not necessarily all
432 * (PATH_MAX is not defined on systems with unlimited path length,
433 * e.g. the Hurd).
434 */
435 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
436 #else
437 INLINE HsInt __hscore_long_path_size() { return 4096; }
438 #endif
439
440 INLINE mode_t __hscore_R_OK() { return R_OK; }
441 INLINE mode_t __hscore_W_OK() { return W_OK; }
442 INLINE mode_t __hscore_X_OK() { return X_OK; }
443
444 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
445 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
446 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
447
448 INLINE HsAddr
449 __hscore_d_name( struct dirent* d )
450 {
451 #ifndef mingw32_TARGET_OS
452 return (HsAddr)(&d->d_name);
453 #else
454 return (HsAddr)(d->d_name);
455 #endif
456 }
457
458 INLINE HsInt
459 __hscore_end_of_dir( void )
460 {
461 #ifndef mingw32_TARGET_OS
462 return 0;
463 #else
464 return ENOENT;
465 #endif
466 }
467
468 INLINE void
469 __hscore_free_dirent(HsAddr dEnt)
470 {
471 #if HAVE_READDIR_R
472 free(dEnt);
473 #endif
474 }
475
476 INLINE HsInt
477 __hscore_sizeof_stat( void )
478 {
479 return sizeof(struct stat);
480 }
481
482 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
483 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
484 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
485
486 #if HAVE_TERMIOS_H
487 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
488
489 INLINE void
490 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
491
492 INLINE unsigned char*
493 __hscore_ptr_c_cc( struct termios* ts )
494 { return (unsigned char*) &ts->c_cc; }
495 #endif
496
497 INLINE HsInt
498 __hscore_sizeof_termios( void )
499 {
500 #ifndef mingw32_TARGET_OS
501 return sizeof(struct termios);
502 #else
503 return 0;
504 #endif
505 }
506
507 INLINE HsInt
508 __hscore_sizeof_sigset_t( void )
509 {
510 #ifndef mingw32_TARGET_OS
511 return sizeof(sigset_t);
512 #else
513 return 0;
514 #endif
515 }
516
517 INLINE int
518 __hscore_echo( void )
519 {
520 #ifdef ECHO
521 return ECHO;
522 #else
523 return 0;
524 #endif
525
526 }
527
528 INLINE int
529 __hscore_tcsanow( void )
530 {
531 #ifdef TCSANOW
532 return TCSANOW;
533 #else
534 return 0;
535 #endif
536
537 }
538
539 INLINE int
540 __hscore_icanon( void )
541 {
542 #ifdef ICANON
543 return ICANON;
544 #else
545 return 0;
546 #endif
547 }
548
549 INLINE int __hscore_vmin( void )
550 {
551 #ifdef VMIN
552 return VMIN;
553 #else
554 return 0;
555 #endif
556 }
557
558 INLINE int __hscore_vtime( void )
559 {
560 #ifdef VTIME
561 return VTIME;
562 #else
563 return 0;
564 #endif
565 }
566
567 INLINE int __hscore_sigttou( void )
568 {
569 #ifdef SIGTTOU
570 return SIGTTOU;
571 #else
572 return 0;
573 #endif
574 }
575
576 INLINE int __hscore_sig_block( void )
577 {
578 #ifdef SIG_BLOCK
579 return SIG_BLOCK;
580 #else
581 return 0;
582 #endif
583 }
584
585 INLINE int __hscore_sig_setmask( void )
586 {
587 #ifdef SIG_SETMASK
588 return SIG_SETMASK;
589 #else
590 return 0;
591 #endif
592 }
593
594 INLINE int
595 __hscore_f_getfl( void )
596 {
597 #ifdef F_GETFL
598 return F_GETFL;
599 #else
600 return 0;
601 #endif
602 }
603
604 INLINE int
605 __hscore_f_setfl( void )
606 {
607 #ifdef F_SETFL
608 return F_SETFL;
609 #else
610 return 0;
611 #endif
612 }
613
614 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
615
616 #ifndef mingw32_TARGET_OS
617 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
618 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
619 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
620 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
621 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
622 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
623 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
624 INLINE int __hsposix_SIGILL() { return SIGILL; }
625 INLINE int __hsposix_SIGINT() { return SIGINT; }
626 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
627 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
628 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
629 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
630 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
631 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
632 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
633 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
634 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
635 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
636 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
637 #if HAVE_SIGPOLL
638 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
639 #endif
640 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
641 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
642 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
643 INLINE int __hsposix_SIGURG() { return SIGURG; }
644 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
645 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
646 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
647
648 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
649 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_SETMASK; }
650 INLINE int __hsposix_SIG_SETMASK() { return SIG_UNBLOCK; }
651 #endif /* mingw32_TARGET_OS */
652
653 #endif /* __HSBASE_H__ */
654