1 /*
2  * Copyright (C) 2003-2012, Parrot Foundation.
3  */
4 
5 #ifndef PARROT_PLATFORM_INTERFACE_H_GUARD
6 #define PARROT_PLATFORM_INTERFACE_H_GUARD
7 /*
8 ** platform_interface.h
9 */
10 #include "parrot/config.h"
11 #include "parrot/interpreter.h"
12 
13 #include <math.h>
14 
15 #if PARROT_HAS_HEADER_LIMITS
16 #  include <limits.h>
17 #endif
18 
19 #ifndef PARROT_HAS_TIMESPEC
20 struct timespec {
21     time_t tv_sec;
22     long   tv_nsec;
23 };
24 #endif /* PARROT_HAS_TIMESPEC */
25 
26 #ifdef _MSC_VER
27 
28 #  ifndef LLONG_MAX
29 #    define LLONG_MAX _I64_MAX
30 #  endif
31 #  ifndef LLONG_MIN
32 #    define LLONG_MIN _I64_MIN
33 #  endif
34 
35 #  if _MSC_VER >= 1400
36 #    define strdup _strdup
37 #  endif
38 
39 /* These disable certain Level 4 Warnings */
40 #  pragma warning(disable: 4100) /* disables 'unreferenced formal parameter'
41                                   * warnings */
42 #  pragma warning(disable: 4115) /* disables 'named type definition in
43                                   * parentheses' warnings triggered in VC98
44                                   * include files */
45 #  pragma warning(disable: 4505) /* disables 'unreferenced local function has
46                                   * been removed' warnings in header files */
47 
48 #endif /* _MSC_VER */
49 
50 /*
51  * init
52  */
53 
54 void Parrot_platform_init_code(void);
55 
56 /*
57  * Errors
58  */
59 
60 STRING *Parrot_platform_strerror(PARROT_INTERP, INTVAL error);
61 
62 /*
63 ** I/O:
64 */
65 
66 #ifdef _WIN32
67 #  define PIO_INVALID_HANDLE ((void *)-1)
68 typedef void *PIOHANDLE;
69 typedef HUGEINTVAL PIOOFF_T;
70 #else
71 #  define PIO_INVALID_HANDLE -1
72 typedef INTVAL PIOHANDLE;
73 typedef off_t PIOOFF_T;
74 #endif
75 
76 PIOHANDLE Parrot_io_internal_std_os_handle(PARROT_INTERP, INTVAL fileno);
77 PIOHANDLE Parrot_io_internal_open(PARROT_INTERP, ARGIN(const STRING * const path),
78                                   INTVAL flags);
79 PIOHANDLE Parrot_io_internal_dup(PARROT_INTERP, PIOHANDLE handle);
80 INTVAL Parrot_io_internal_close(PARROT_INTERP, PIOHANDLE handle);
81 INTVAL Parrot_io_internal_is_tty(PARROT_INTERP, PIOHANDLE fd);
82 PARROT_CONST_FUNCTION
83 INTVAL Parrot_io_internal_getblksize(PIOHANDLE fd);
84 INTVAL Parrot_io_internal_flush(PARROT_INTERP, PIOHANDLE os_handle);
85 size_t Parrot_io_internal_read(PARROT_INTERP, PIOHANDLE os_handle, ARGOUT(char *buf),
86                                size_t len);
87 size_t Parrot_io_internal_write(PARROT_INTERP, PIOHANDLE os_handle,
88         ARGIN(const char *buf), size_t len);
89 PIOOFF_T Parrot_io_internal_seek(PARROT_INTERP, PIOHANDLE os_handle,
90         PIOOFF_T offset, INTVAL whence);
91 PIOOFF_T Parrot_io_internal_tell(PARROT_INTERP, PIOHANDLE os_handle);
92 PIOHANDLE Parrot_io_internal_open_pipe(PARROT_INTERP, ARGIN(const STRING * const command),
93                                        INTVAL flags,
94                                        ARGOUT(INTVAL *pid_out));
95 INTVAL Parrot_io_internal_pipe(PARROT_INTERP, ARGMOD(PIOHANDLE *reader),
96         ARGMOD(PIOHANDLE *writer));
97 
98 PARROT_EXPORT
99 INTVAL Parrot_io_internal_async(PARROT_INTERP, ARGMOD(PMC *pmc), INTVAL async);
100 
101 /*
102  * Socket
103  */
104 
105 PMC *Parrot_io_internal_getaddrinfo(PARROT_INTERP, ARGIN(STRING *addr), INTVAL port,
106         INTVAL protocol, INTVAL family, INTVAL passive);
107 INTVAL Parrot_io_internal_addr_match(PARROT_INTERP, ARGIN(PMC *sa), INTVAL family, INTVAL type,
108             INTVAL protocol);
109 STRING *Parrot_io_internal_getnameinfo(PARROT_INTERP, ARGIN(const void *addr), INTVAL addr_len);
110 INTVAL Parrot_io_internal_getprotobyname(PARROT_INTERP, ARGIN(STRING *name));
111 PIOHANDLE Parrot_io_internal_socket(PARROT_INTERP, int fam, int type, int proto);
112 void Parrot_io_internal_connect(PARROT_INTERP, PIOHANDLE handle, ARGIN(void *addr),
113         INTVAL addr_len);
114 void Parrot_io_internal_bind(PARROT_INTERP, PIOHANDLE handle, ARGIN(void *addr), INTVAL addr_len);
115 void Parrot_io_internal_listen(PARROT_INTERP, PIOHANDLE handle, INTVAL sec);
116 PIOHANDLE Parrot_io_internal_accept(PARROT_INTERP, PIOHANDLE handle, ARGOUT(PMC * remote_addr));
117 INTVAL Parrot_io_internal_send(PARROT_INTERP, PIOHANDLE handle, ARGIN(const char *buf),
118         size_t len);
119 INTVAL Parrot_io_internal_recv(PARROT_INTERP, PIOHANDLE handle, ARGOUT(char *buf), size_t len);
120 INTVAL Parrot_io_internal_poll(PARROT_INTERP, PIOHANDLE handle, int which, int sec, int usec);
121 INTVAL Parrot_io_internal_close_socket(PARROT_INTERP, PIOHANDLE handle);
122 
123 /*
124  * Files and directories
125  */
126 
127 /* &gen_from_def(stat.pasm) */
128 
129 #define STAT_EXISTS               0
130 #define STAT_FILESIZE             1
131 #define STAT_ISDIR                2
132 #define STAT_ISREG                3
133 #define STAT_ISDEV                4
134 #define STAT_CREATETIME           5
135 #define STAT_ACCESSTIME           6
136 #define STAT_MODIFYTIME           7
137 #define STAT_CHANGETIME           8
138 #define STAT_BACKUPTIME           9
139 #define STAT_UID                 10
140 #define STAT_GID                 11
141 #define STAT_ISLNK               12
142 #define STAT_PLATFORM_DEV        -1
143 #define STAT_PLATFORM_INODE      -2
144 #define STAT_PLATFORM_MODE       -3
145 #define STAT_PLATFORM_NLINKS     -4
146 #define STAT_PLATFORM_DEVTYPE    -5
147 #define STAT_PLATFORM_BLOCKSIZE  -6
148 #define STAT_PLATFORM_BLOCKS     -7
149 
150 #define STAT_TYPE_UNKNOWN         0
151 #define STAT_TYPE_FILE            1
152 #define STAT_TYPE_DIRECTORY       2
153 #define STAT_TYPE_PIPE            3
154 #define STAT_TYPE_LINK            4
155 #define STAT_TYPE_DEVICE          5
156 
157 /* &end_gen */
158 
159 typedef struct _Parrot_Stat_Buf {
160     INTVAL     type;
161     HUGEINTVAL size;
162     INTVAL     uid;
163     INTVAL     gid;
164     INTVAL     dev;
165     HUGEINTVAL inode;
166     INTVAL     mode;
167     INTVAL     n_links;
168     INTVAL     block_size;
169     INTVAL     blocks;
170 
171 #ifdef PARROT_HAS_STAT_ST_TIMESPEC_T
172     st_timespec_t create_time;
173     st_timespec_t access_time;
174     st_timespec_t modify_time;
175     st_timespec_t change_time;
176 #else
177     struct timespec create_time;
178     struct timespec access_time;
179     struct timespec modify_time;
180     struct timespec change_time;
181 #endif
182 } Parrot_Stat_Buf;
183 
184 PARROT_EXPORT
185 STRING *Parrot_file_getcwd(PARROT_INTERP);
186 
187 PARROT_EXPORT
188 void Parrot_file_mkdir(PARROT_INTERP, ARGIN(STRING *path), INTVAL mode);
189 
190 PARROT_EXPORT
191 void Parrot_file_chdir(PARROT_INTERP, ARGIN(STRING *path));
192 
193 PARROT_EXPORT
194 void Parrot_file_rmdir(PARROT_INTERP, ARGIN(STRING *path));
195 
196 PARROT_EXPORT
197 void Parrot_file_unlink(PARROT_INTERP, ARGIN(STRING *path));
198 
199 PARROT_EXPORT
200 void Parrot_file_stat(PARROT_INTERP, ARGIN(STRING *path), ARGOUT(Parrot_Stat_Buf *buf));
201 
202 PARROT_EXPORT
203 void Parrot_file_lstat(PARROT_INTERP, ARGIN(STRING *path), ARGOUT(Parrot_Stat_Buf *buf));
204 
205 PARROT_EXPORT
206 void Parrot_file_fstat(PARROT_INTERP, PIOHANDLE handle, ARGOUT(Parrot_Stat_Buf *buf));
207 
208 PARROT_EXPORT
209 INTVAL Parrot_file_stat_intval(PARROT_INTERP, ARGIN(STRING *path), INTVAL thing);
210 
211 PARROT_EXPORT
212 INTVAL Parrot_file_lstat_intval(PARROT_INTERP, ARGIN(STRING * path), INTVAL thing);
213 
214 PARROT_EXPORT
215 INTVAL Parrot_file_fstat_intval(PARROT_INTERP, PIOHANDLE os_handle, INTVAL thing);
216 
217 PARROT_EXPORT
218 void Parrot_file_symlink(PARROT_INTERP, ARGIN(STRING *from), ARGIN(STRING *to));
219 
220 PARROT_EXPORT
221 STRING *Parrot_file_readlink(PARROT_INTERP, ARGIN(STRING *path));
222 
223 PARROT_EXPORT
224 void Parrot_file_link(PARROT_INTERP, ARGIN(STRING *from), ARGIN(STRING *to));
225 
226 PARROT_EXPORT
227 INTVAL Parrot_file_umask(PARROT_INTERP, INTVAL mask);
228 
229 PARROT_EXPORT
230 void Parrot_file_chroot(PARROT_INTERP, ARGIN(STRING *path));
231 
232 PARROT_EXPORT
233 PMC *Parrot_file_readdir(PARROT_INTERP, ARGIN(STRING *path));
234 
235 PARROT_EXPORT
236 void Parrot_file_rename(PARROT_INTERP, ARGIN(STRING *from), ARGIN(STRING *to));
237 
238 PARROT_EXPORT
239 void Parrot_file_chmod(PARROT_INTERP, ARGIN(STRING *path), INTVAL mode);
240 
241 PARROT_EXPORT
242 INTVAL Parrot_file_can_read(PARROT_INTERP, ARGIN(STRING *path));
243 
244 PARROT_EXPORT
245 INTVAL Parrot_file_can_write(PARROT_INTERP, ARGIN(STRING *path));
246 
247 PARROT_EXPORT
248 INTVAL Parrot_file_can_execute(PARROT_INTERP, ARGIN(STRING *path));
249 
250 /*
251 ** Math:
252 */
253 
254 extern int Parrot_signbit(double x);
255 #if NUMVAL_SIZE == 12
256 int Parrot_signbit_l(long double x);
257 #endif
258 
259 #ifndef signbit
260 #  if NUMVAL_SIZE == 8
261 #    define signbit(x) Parrot_signbit(x)
262 #  else
263 #    define signbit(x) Parrot_signbit_l(x)
264 #  endif
265 #endif
266 
267 #define Parrot_is_nzero(x) ((x) == 0.0 && signbit(x))
268 
269 /*
270 ** Memory:
271 */
272 
273 void *Parrot_memalign(size_t align, size_t size);
274 void *Parrot_memalign_if_possible(size_t align, size_t size);
275 void Parrot_free_memalign(void *);
276 
277 #if !defined(PARROT_HAS_SOME_MEMALIGN)
278 #  define Parrot_memalign_if_possible(a, s) malloc(s)
279 #else
280 #  define Parrot_memalign_if_possible(a, s) Parrot_memalign((a), (s))
281 #endif
282 
283 /*
284 ** Processes
285 */
286 
287 typedef enum Parrot_proc_exec_enum {
288     /*
289      * Activates RTLD_GLOBAL on *NIX systems, making symbols from the newly
290      * loaded library visible to other libraries; this is usually needed if
291      * it will load libraries itself.
292      */
293     PARROT_EXEC_STDIN   = 0x01,
294     PARROT_EXEC_STDOUT  = 0x02,
295     PARROT_EXEC_STDERR  = 0x04
296 } Parrot_proc_exec_flags;
297 
298 PARROT_EXPORT
299 INTVAL Parrot_Run_OS_Command(Interp*, STRING *);
300 
301 PARROT_EXPORT
302 INTVAL Parrot_Run_OS_Command_Argv(Interp*, PMC *);
303 
304 PARROT_EXPORT
305 UINTVAL Parrot_getpid(void);
306 
307 INTVAL Parrot_proc_exec(Interp *, STRING *command, INTVAL flags, PIOHANDLE *handles);
308 INTVAL Parrot_proc_waitpid(Interp *, INTVAL pid);
309 
310 /*
311 ** Time
312 */
313 
314 PARROT_EXPORT
315 void Parrot_sleep(unsigned int seconds);
316 
317 PARROT_EXPORT
318 void Parrot_usleep(unsigned int microseconds);
319 void Parrot_floatval_sleep(FLOATVAL time);
320 
321 PARROT_EXPORT
322 INTVAL Parrot_intval_time(void);
323 
324 PARROT_EXPORT
325 FLOATVAL Parrot_floatval_time(void);
326 
327 PARROT_EXPORT
328 struct tm * Parrot_gmtime_r(const time_t *, struct tm *);
329 
330 PARROT_EXPORT
331 struct tm * Parrot_localtime_r(const time_t *, struct tm *);
332 
333 PARROT_EXPORT
334 char* Parrot_asctime_r(const struct tm*, char *);
335 
336 /*
337  * Env
338  */
339 
340 PARROT_EXPORT
341 void Parrot_setenv(PARROT_INTERP, const STRING *name, const STRING *value);
342 
343 PARROT_EXPORT
344 void Parrot_unsetenv(PARROT_INTERP, const STRING *name);
345 
346 PARROT_EXPORT
347 STRING * Parrot_getenv(PARROT_INTERP, const STRING *name);
348 
349 /*
350 ** Dynamic Loading:
351 */
352 
353 /*
354  * The second argument to Parrot_dlopen below provides portable access to
355  * non-default behavior of dynamic linkers.
356  *
357  * All flags will be ignored on platforms for which they are inapplicable.
358  */
359 
360 /* &gen_from_enum(dlopenflags.pasm) */
361 typedef enum Parrot_dlopen_enum {
362     /*
363      * Activates RTLD_GLOBAL on *NIX systems, making symbols from the newly
364      * loaded library visible to other libraries; this is usually needed if
365      * it will load libraries itself.
366      */
367     Parrot_dlopen_global_FLAG   = 0x01
368 } Parrot_dlopen_flags;
369 /* &end_gen */
370 
371 void *Parrot_dlopen(const char *filename, Parrot_dlopen_flags flags);
372 const char *Parrot_dlerror(void);
373 void *Parrot_dlsym(void *handle, const char *symbol);
374 int Parrot_dlclose(void *handle);
375 
376 /*
377  * encoding
378  */
379 void Parrot_init_platform_encoding(PARROT_INTERP);
380 size_t Parrot_str_platform_strlen(PARROT_INTERP, const char *s);
381 
382 /*
383  * system timer
384  */
385 
386 #ifdef PARROT_HAS_SOME_SYS_TIMER
387 
388 void * new_sys_timer_ms(void);
389 void start_sys_timer_ms(void *handle, int ms);
390 void stop_sys_timer_ms(void *handle);
391 int get_sys_timer_ms(void *handle);
392 
393 #else
394 
395 #  define new_sys_timer_ms() NULL
396 #  define start_sys_timer_ms(h, m)
397 #  define stop_sys_timer_ms(h)
398 #  define get_sys_timer_ms(h) 0
399 
400 #endif
401 
402 /*
403  * high-resolution timer support
404  */
405 
406 PARROT_EXPORT
407 UHUGEINTVAL Parrot_hires_get_time(void);
408 
409 PARROT_EXPORT
410 PARROT_CONST_FUNCTION
411 UINTVAL     Parrot_hires_get_tick_duration(void);
412 
413 /*
414  * user information
415  */
416 
417 PARROT_EXPORT
418 UINTVAL Parrot_get_user_id(void);
419 
420 /*
421  * system memory
422  */
423 
424 PARROT_EXPORT
425 size_t Parrot_sysmem_amount(Interp*);
426 
427 /*
428  * Entropy
429  */
430 
431 PARROT_EXPORT
432 INTVAL Parrot_get_entropy(PARROT_INTERP);
433 
434 /*
435  * CPU
436  */
437 PARROT_EXPORT
438 STRING *Parrot_get_cpu_type(PARROT_INTERP);
439 
440 PARROT_EXPORT
441 INTVAL Parrot_get_num_cpus(PARROT_INTERP);
442 
443 
444 #endif /* PARROT_PLATFORM_INTERFACE_H_GUARD */
445 
446 /*
447  * Local variables:
448  *   c-file-style: "parrot"
449  * End:
450  * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
451  */
452