forked from idris-lang/Idris-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idris_stdfgn.c
111 lines (93 loc) · 1.97 KB
/
idris_stdfgn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "idris_stdfgn.h"
#include "idris_rts.h"
#include "idris_gmp.h"
#include "idris_gc.h"
#include <sys/select.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
extern char** environ;
void putStr(char* str) {
printf("%s", str);
}
void* fileOpen(char* name, char* mode) {
FILE* f = fopen(name, mode);
return (void*)f;
}
void fileClose(void* h) {
FILE* f = (FILE*)h;
fclose(f);
}
int fileEOF(void* h) {
FILE* f = (FILE*)h;
return feof(f);
}
int fileError(void* h) {
FILE* f = (FILE*)h;
return ferror(f);
}
int idris_writeStr(void* h, char* str) {
FILE* f = (FILE*)h;
if (fputs(str, f)) {
return 0;
} else {
return -1;
}
}
int fpoll(void* h)
{
FILE* f = (FILE*)h;
fd_set x;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int fd = fileno(f);
FD_ZERO(&x);
FD_SET(fd, &x);
int r = select(fd+1, &x, 0, 0, &timeout);
return r;
}
void* do_popen(const char* cmd, const char* mode) {
FILE* f = popen(cmd, mode);
// int d = fileno(f);
// fcntl(d, F_SETFL, O_NONBLOCK);
return f;
}
int isNull(void* ptr) {
return ptr==NULL;
}
int idris_eqPtr(void* x, void* y) {
return x==y;
}
void* idris_stdin() {
return (void*)stdin;
}
char* getEnvPair(int i) {
return *(environ + i);
}
VAL idris_time() {
time_t t = time(NULL);
return MKBIGI(t);
}
VAL idris_mkFileError(VM* vm) {
VAL result;
switch(errno) {
// Make sure this corresponds to the FileError structure in
// Prelude.File
case ENOENT:
idris_constructor(result, vm, 2, 0, 0);
break;
case EACCES:
idris_constructor(result, vm, 3, 0, 0);
break;
default:
idris_constructor(result, vm, 4, 1, 0);
idris_setConArg(result, 0, MKINT((intptr_t)errno));
break;
}
return result;
}
void idris_forceGC(void* vm) {
idris_gc((VM*)vm);
}