bc88bad3b41e3161f47e439ca38f0a2131b87a3a
[packages/old-time.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2 * $Id: HsBase.h,v 1.34 2003/10/30 18:51:26 sof 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_TARGET_OS)
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_TARGET_OS)
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 /* in Signals.c */
133 extern HsInt nocldstop;
134
135 /* -----------------------------------------------------------------------------
136 64-bit operations, defined in longlong.c
137 -------------------------------------------------------------------------- */
138
139 #ifdef SUPPORT_LONG_LONGS
140
141 StgInt stg_gtWord64 (StgWord64, StgWord64);
142 StgInt stg_geWord64 (StgWord64, StgWord64);
143 StgInt stg_eqWord64 (StgWord64, StgWord64);
144 StgInt stg_neWord64 (StgWord64, StgWord64);
145 StgInt stg_ltWord64 (StgWord64, StgWord64);
146 StgInt stg_leWord64 (StgWord64, StgWord64);
147
148 StgInt stg_gtInt64 (StgInt64, StgInt64);
149 StgInt stg_geInt64 (StgInt64, StgInt64);
150 StgInt stg_eqInt64 (StgInt64, StgInt64);
151 StgInt stg_neInt64 (StgInt64, StgInt64);
152 StgInt stg_ltInt64 (StgInt64, StgInt64);
153 StgInt stg_leInt64 (StgInt64, StgInt64);
154
155 StgWord64 stg_remWord64 (StgWord64, StgWord64);
156 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
157
158 StgInt64 stg_remInt64 (StgInt64, StgInt64);
159 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
160 StgInt64 stg_negateInt64 (StgInt64);
161 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
162 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
163 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
164
165 StgWord64 stg_and64 (StgWord64, StgWord64);
166 StgWord64 stg_or64 (StgWord64, StgWord64);
167 StgWord64 stg_xor64 (StgWord64, StgWord64);
168 StgWord64 stg_not64 (StgWord64);
169
170 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
171 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
172 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
173 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
174 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
175
176 StgInt64 stg_intToInt64 (StgInt);
177 StgInt stg_int64ToInt (StgInt64);
178 StgWord64 stg_int64ToWord64 (StgInt64);
179
180 StgWord64 stg_wordToWord64 (StgWord);
181 StgWord stg_word64ToWord (StgWord64);
182 StgInt64 stg_word64ToInt64 (StgWord64);
183
184 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
185 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
186
187 #endif /* SUPPORT_LONG_LONGS */
188
189 /* -----------------------------------------------------------------------------
190 INLINE functions.
191
192 These functions are given as inlines here for when compiling via C,
193 but we also generate static versions into the cbits library for
194 when compiling to native code.
195 -------------------------------------------------------------------------- */
196
197 #ifndef INLINE
198 # if defined(_MSC_VER)
199 # define INLINE extern __inline
200 # else
201 # define INLINE extern inline
202 # endif
203 #endif
204
205 #if !defined(_MSC_VER)
206 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
207 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
208 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
209 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
210 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
211 #ifdef S_ISSOCK
212 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
213 #endif
214 #endif
215
216 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
217 INLINE int
218 __hscore_sigemptyset( sigset_t *set )
219 { return sigemptyset(set); }
220
221 INLINE int
222 __hscore_sigfillset( sigset_t *set )
223 { return sigfillset(set); }
224
225 INLINE int
226 __hscore_sigaddset( sigset_t * set, int s )
227 { return sigaddset(set,s); }
228
229 INLINE int
230 __hscore_sigdelset( sigset_t * set, int s )
231 { return sigdelset(set,s); }
232
233 INLINE int
234 __hscore_sigismember( sigset_t * set, int s )
235 { return sigismember(set,s); }
236 #endif
237
238 INLINE void *
239 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
240 { return memcpy(dst+dst_off, src, sz); }
241
242 INLINE void *
243 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
244 { return memcpy(dst, src+src_off, sz); }
245
246 INLINE HsBool
247 __hscore_supportsTextMode()
248 {
249 #if defined(mingw32_TARGET_OS)
250 return HS_BOOL_FALSE;
251 #else
252 return HS_BOOL_TRUE;
253 #endif
254 }
255
256 INLINE HsInt
257 __hscore_bufsiz()
258 {
259 return BUFSIZ;
260 }
261
262 INLINE HsInt
263 __hscore_seek_cur()
264 {
265 return SEEK_CUR;
266 }
267
268 INLINE HsInt
269 __hscore_o_binary()
270 {
271 #if defined(_MSC_VER)
272 return O_BINARY;
273 #else
274 return CONST_O_BINARY;
275 #endif
276 }
277
278 INLINE int
279 __hscore_o_rdonly()
280 {
281 #ifdef O_RDONLY
282 return O_RDONLY;
283 #else
284 return 0;
285 #endif
286 }
287
288 INLINE int
289 __hscore_o_wronly( void )
290 {
291 #ifdef O_WRONLY
292 return O_WRONLY;
293 #else
294 return 0;
295 #endif
296 }
297
298 INLINE int
299 __hscore_o_rdwr( void )
300 {
301 #ifdef O_RDWR
302 return O_RDWR;
303 #else
304 return 0;
305 #endif
306 }
307
308 INLINE int
309 __hscore_o_append( void )
310 {
311 #ifdef O_APPEND
312 return O_APPEND;
313 #else
314 return 0;
315 #endif
316 }
317
318 INLINE int
319 __hscore_o_creat( void )
320 {
321 #ifdef O_CREAT
322 return O_CREAT;
323 #else
324 return 0;
325 #endif
326 }
327
328 INLINE int
329 __hscore_o_excl( void )
330 {
331 #ifdef O_EXCL
332 return O_EXCL;
333 #else
334 return 0;
335 #endif
336 }
337
338 INLINE int
339 __hscore_o_trunc( void )
340 {
341 #ifdef O_TRUNC
342 return O_TRUNC;
343 #else
344 return 0;
345 #endif
346 }
347
348 INLINE int
349 __hscore_o_noctty( void )
350 {
351 #ifdef O_NOCTTY
352 return O_NOCTTY;
353 #else
354 return 0;
355 #endif
356 }
357
358 INLINE int
359 __hscore_o_nonblock( void )
360 {
361 #ifdef O_NONBLOCK
362 return O_NONBLOCK;
363 #else
364 return 0;
365 #endif
366 }
367
368 INLINE HsInt
369 __hscore_seek_set( void )
370 {
371 return SEEK_SET;
372 }
373
374 INLINE HsInt
375 __hscore_seek_end( void )
376 {
377 return SEEK_END;
378 }
379
380 INLINE HsInt
381 __hscore_setmode( HsInt fd, HsBool toBin )
382 {
383 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
384 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
385 #else
386 return 0;
387 #endif
388 }
389
390 INLINE HsInt
391 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr,
392 HsInt off, int sz )
393 {
394 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
395 if (isSock) {
396 return send(fd,(char *)ptr + off, sz, 0);
397 }
398 #endif
399 return write(fd,(char *)ptr + off, sz);
400 }
401
402 INLINE HsInt
403 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr,
404 HsInt off, int sz )
405 {
406 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
407 if (isSock) {
408 return recv(fd,(char *)ptr + off, sz, 0);
409 }
410 #endif
411 return read(fd,(char *)ptr + off, sz);
412
413 }
414
415 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
416 INLINE long *
417 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
418
419 INLINE char **
420 __hscore_Time_ghcTzname( void ) { return _tzname; }
421 #endif
422
423 INLINE HsInt
424 __hscore_mkdir( HsAddr pathName, HsInt mode )
425 {
426 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
427 return mkdir(pathName);
428 #else
429 return mkdir(pathName,mode);
430 #endif
431 }
432
433 INLINE HsInt
434 __hscore_lstat( HsAddr fname, HsAddr st )
435 {
436 #ifdef HAVE_LSTAT
437 return lstat((const char*)fname, (struct stat*)st);
438 #else
439 return stat((const char*)fname, (struct stat*)st);
440 #endif
441 }
442
443 #ifdef PATH_MAX
444 /* A size that will contain many path names, but not necessarily all
445 * (PATH_MAX is not defined on systems with unlimited path length,
446 * e.g. the Hurd).
447 */
448 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
449 #else
450 INLINE HsInt __hscore_long_path_size() { return 4096; }
451 #endif
452
453 #ifdef R_OK
454 INLINE mode_t __hscore_R_OK() { return R_OK; }
455 #endif
456 #ifdef W_OK
457 INLINE mode_t __hscore_W_OK() { return W_OK; }
458 #endif
459 #ifdef X_OK
460 INLINE mode_t __hscore_X_OK() { return X_OK; }
461 #endif
462
463 #ifdef S_IRUSR
464 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
465 #endif
466 #ifdef S_IWUSR
467 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
468 #endif
469 #ifdef S_IXUSR
470 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
471 #endif
472
473 #if !defined(_MSC_VER)
474 INLINE HsAddr
475 __hscore_d_name( struct dirent* d )
476 {
477 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
478 return (HsAddr)(&d->d_name);
479 #else
480 return (HsAddr)(d->d_name);
481 #endif
482 }
483 #endif
484
485 INLINE HsInt
486 __hscore_end_of_dir( void )
487 {
488 #ifndef mingw32_TARGET_OS
489 return 0;
490 #else
491 return ENOENT;
492 #endif
493 }
494
495 INLINE void
496 __hscore_free_dirent(HsAddr dEnt)
497 {
498 #if HAVE_READDIR_R
499 free(dEnt);
500 #endif
501 }
502
503 INLINE HsInt
504 __hscore_sizeof_stat( void )
505 {
506 return sizeof(struct stat);
507 }
508
509 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
510 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
511 #if !defined(_MSC_VER)
512 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
513 #endif
514
515 #if HAVE_TERMIOS_H
516 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
517
518 INLINE void
519 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
520
521 INLINE unsigned char*
522 __hscore_ptr_c_cc( struct termios* ts )
523 { return (unsigned char*) &ts->c_cc; }
524
525 INLINE HsInt
526 __hscore_sizeof_termios( void )
527 {
528 #ifndef mingw32_TARGET_OS
529 return sizeof(struct termios);
530 #else
531 return 0;
532 #endif
533 }
534 #endif
535
536 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
537 INLINE HsInt
538 __hscore_sizeof_sigset_t( void )
539 {
540 return sizeof(sigset_t);
541 }
542 #endif
543
544 INLINE int
545 __hscore_echo( void )
546 {
547 #ifdef ECHO
548 return ECHO;
549 #else
550 return 0;
551 #endif
552
553 }
554
555 INLINE int
556 __hscore_tcsanow( void )
557 {
558 #ifdef TCSANOW
559 return TCSANOW;
560 #else
561 return 0;
562 #endif
563
564 }
565
566 INLINE int
567 __hscore_icanon( void )
568 {
569 #ifdef ICANON
570 return ICANON;
571 #else
572 return 0;
573 #endif
574 }
575
576 INLINE int __hscore_vmin( void )
577 {
578 #ifdef VMIN
579 return VMIN;
580 #else
581 return 0;
582 #endif
583 }
584
585 INLINE int __hscore_vtime( void )
586 {
587 #ifdef VTIME
588 return VTIME;
589 #else
590 return 0;
591 #endif
592 }
593
594 INLINE int __hscore_sigttou( void )
595 {
596 #ifdef SIGTTOU
597 return SIGTTOU;
598 #else
599 return 0;
600 #endif
601 }
602
603 INLINE int __hscore_sig_block( void )
604 {
605 #ifdef SIG_BLOCK
606 return SIG_BLOCK;
607 #else
608 return 0;
609 #endif
610 }
611
612 INLINE int __hscore_sig_setmask( void )
613 {
614 #ifdef SIG_SETMASK
615 return SIG_SETMASK;
616 #else
617 return 0;
618 #endif
619 }
620
621 INLINE int
622 __hscore_f_getfl( void )
623 {
624 #ifdef F_GETFL
625 return F_GETFL;
626 #else
627 return 0;
628 #endif
629 }
630
631 INLINE int
632 __hscore_f_setfl( void )
633 {
634 #ifdef F_SETFL
635 return F_SETFL;
636 #else
637 return 0;
638 #endif
639 }
640
641 // defined in rts/RtsStartup.c.
642 extern void* __hscore_get_saved_termios(int fd);
643 extern void __hscore_set_saved_termios(int fd, void* ts);
644
645 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
646
647 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
648 INLINE int __hsposix_SIGABRT() { return SIGABRT; }
649 INLINE int __hsposix_SIGALRM() { return SIGALRM; }
650 INLINE int __hsposix_SIGBUS() { return SIGBUS; }
651 INLINE int __hsposix_SIGCHLD() { return SIGCHLD; }
652 INLINE int __hsposix_SIGCONT() { return SIGCONT; }
653 INLINE int __hsposix_SIGFPE() { return SIGFPE; }
654 INLINE int __hsposix_SIGHUP() { return SIGHUP; }
655 INLINE int __hsposix_SIGILL() { return SIGILL; }
656 INLINE int __hsposix_SIGINT() { return SIGINT; }
657 INLINE int __hsposix_SIGKILL() { return SIGKILL; }
658 INLINE int __hsposix_SIGPIPE() { return SIGPIPE; }
659 INLINE int __hsposix_SIGQUIT() { return SIGQUIT; }
660 INLINE int __hsposix_SIGSEGV() { return SIGSEGV; }
661 INLINE int __hsposix_SIGSTOP() { return SIGSTOP; }
662 INLINE int __hsposix_SIGTERM() { return SIGTERM; }
663 INLINE int __hsposix_SIGTSTP() { return SIGTSTP; }
664 INLINE int __hsposix_SIGTTIN() { return SIGTTIN; }
665 INLINE int __hsposix_SIGTTOU() { return SIGTTOU; }
666 INLINE int __hsposix_SIGUSR1() { return SIGUSR1; }
667 INLINE int __hsposix_SIGUSR2() { return SIGUSR2; }
668 #if HAVE_SIGPOLL
669 INLINE int __hsposix_SIGPOLL() { return SIGPOLL; }
670 #endif
671 INLINE int __hsposix_SIGPROF() { return SIGPROF; }
672 INLINE int __hsposix_SIGSYS() { return SIGSYS; }
673 INLINE int __hsposix_SIGTRAP() { return SIGTRAP; }
674 INLINE int __hsposix_SIGURG() { return SIGURG; }
675 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
676 INLINE int __hsposix_SIGXCPU() { return SIGXCPU; }
677 INLINE int __hsposix_SIGXFSZ() { return SIGXFSZ; }
678
679 INLINE int __hsposix_SIG_BLOCK() { return SIG_BLOCK; }
680 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
681 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
682 #endif /* mingw32_TARGET_OS */
683
684 #endif /* __HSBASE_H__ */
685