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