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