-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.c
255 lines (213 loc) · 5.91 KB
/
util.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include "util.h"
#include "global.h"
int
read_size(int fd, char* buff, int size) {
int sts;
int pos = 0;
int remain = size;
if (size < 0) return -1;
if (size == 0) return 1;
do {
sts = read(fd, &buff[pos], remain);
if (-1 == sts) return -1;
if ( 0 == sts) return -1;
pos += sts;
remain -= sts;
} while (remain > 0);
return 1;
}
int
write_size(int fd, char* buff, int size) {
int sts;
int pos = 0;
int remain = size;
if (size < 0) return -1;
if (size == 0) return 1;
do {
sts = write(fd, &buff[pos], remain);
if (-1 == sts) return -1;
if ( 0 == sts) return -1;
pos += sts;
remain -= sts;
} while (remain > 0);
return 1;
}
wchar_t*
to_wchar(const char *src) {
wchar_t *dest;
int len, i;
len = strlen(src);
dest = GC_MALLOC((len+1)*sizeof(wchar_t));
ALLOC_SAFE(dest);
for (i=0; i<=len; i++) {
dest[i] = ((wchar_t)src[i]) & 0xff;
}
return dest;
}
char*
to_char(const wchar_t *src) {
char *dest;
int len, i;
len = wcslen(src);
dest = GC_MALLOC(len+1);
ALLOC_SAFE(dest);
for (i=0; i<=len; i++) {
dest[i] = (char)src[i];
}
return dest;
}
void
println(Toy_Interp *interp, wchar_t *msg) {
Toy_Type *l;
l = new_list(new_symbol(L"println"));
list_append(l, new_string_str(msg));
toy_call(interp, l);
return;
}
char*
encode_dirent(Toy_Interp *interp, wchar_t *name, encoder_error_info **info) {
Toy_Type *enc;
int iencoder;
encoder_error_info *error_info;
Cell *raw;
error_info = GC_MALLOC(sizeof(encoder_error_info));
ALLOC_SAFE(error_info);
memset(error_info, 0, sizeof(encoder_error_info));
*info = error_info;
iencoder = NENCODE_RAW;
enc = hash_get_t(interp->globals, const_DEFAULT_DIRENT_ENCODING);
if (enc) {
if (GET_TAG(enc) == SYMBOL) {
iencoder = get_encoding_index(cell_get_addr(enc->u.symbol.cell));
if (-1 == iencoder) {
error_info->errorno = EENCODE_BADENCODING;
error_info->pos = -1;
error_info->message = L"Bad encoder specified.";
return 0;
}
} else {
error_info->errorno = EENCODE_BADENCODING;
error_info->pos = -1;
error_info->message = L"Bad encoder specified, need symbol.";
return 0;
}
}
raw = encode_unicode_to_raw(new_cell(name), iencoder, error_info);
if (NULL == raw) return 0;
return to_char(cell_get_addr(raw));
}
Cell*
decode_dirent(Toy_Interp *interp, char *name, encoder_error_info **info) {
Toy_Type *enc;
int iencoder;
encoder_error_info *error_info;
Cell *unicode;
error_info = GC_MALLOC(sizeof(encoder_error_info));
ALLOC_SAFE(error_info);
memset(error_info, 0, sizeof(encoder_error_info));
*info = error_info;
iencoder = NENCODE_RAW;
enc = hash_get_t(interp->globals, const_DEFAULT_DIRENT_ENCODING);
if (enc) {
if (GET_TAG(enc) == SYMBOL) {
iencoder = get_encoding_index(cell_get_addr(enc->u.symbol.cell));
if (-1 == iencoder) {
error_info->errorno = EENCODE_BADENCODING;
error_info->pos = -1;
error_info->message = L"Bad encoder specified.";
return 0;
}
} else {
error_info->errorno = EENCODE_BADENCODING;
error_info->pos = -1;
error_info->message = L"Bad encoder specified, need symbol.";
return 0;
}
}
unicode = decode_raw_to_unicode(new_cell(to_wchar(name)), iencoder, error_info);
if (NULL == unicode) return 0;
return unicode;
}
wchar_t*
decode_error(Toy_Interp *interp, char *str) {
int iencoder;
Cell *c;
encoder_error_info *enc_error_info;
Toy_Type *enc;
enc = hash_get_t(interp->globals, const_DEFAULT_DIRENT_ENCODING);
if (NULL == enc) {
iencoder = get_encoding_index(DEFAULT_ERRSTR_ENCODING);
} else {
if (GET_TAG(enc) == SYMBOL) {
iencoder = get_encoding_index(cell_get_addr(enc->u.symbol.cell));
} else {
iencoder = get_encoding_index(DEFAULT_ERRSTR_ENCODING);
}
}
if (-1 == iencoder) {
return to_wchar(str);
}
enc_error_info = GC_MALLOC(sizeof(encoder_error_info));
ALLOC_SAFE(enc_error_info);
memset(enc_error_info, 0, sizeof(encoder_error_info));
c = decode_raw_to_unicode(new_cell(to_wchar(str)), iencoder, enc_error_info);
if (c == NULL) {
return to_wchar(str);
}
return cell_get_addr(c);
}
int
is_read_ready(int fd, int timeout_m) {
struct timeval timeout;
int maxfd;
fd_set read_fds;
int sts;
timeout.tv_sec = timeout_m / 1000;
timeout.tv_usec = (timeout_m % 1000) * 1000;
retry:
// fprintf(stderr, "DEBUG: timeout sec = %ld, usec = %ld\n", timeout.tv_sec, timeout.tv_usec);
maxfd = fd + 1;
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
errno = 0;
sts = select(maxfd, &read_fds, NULL, NULL, &timeout);
if (-1 == sts) {
if (errno == EINTR) goto retry;
return IRDY_ERR;
}
if (FD_ISSET(fd, &read_fds)) return IRDY_OK;
if ((timeout.tv_sec <= 0) && (timeout.tv_usec <= 0)) return IRDY_TOUT;
return IRDY_NO;
}
int
is_write_ready(int fd, int timeout_m) {
struct timeval timeout;
int maxfd;
fd_set write_fds;
int sts;
timeout.tv_sec = timeout_m / 1000;
timeout.tv_usec = (timeout_m % 1000) * 1000;
retry:
// fprintf(stderr, "DEBUG: timeout sec = %ld, usec = %ld\n", timeout.tv_sec, timeout.tv_usec);
maxfd = fd + 1;
FD_ZERO(&write_fds);
FD_SET(fd, &write_fds);
errno = 0;
sts = select(maxfd, NULL, &write_fds, NULL, &timeout);
if (-1 == sts) {
if (errno == EINTR) goto retry;
return IRDY_ERR;
}
if (FD_ISSET(fd, &write_fds)) return IRDY_OK;
if ((timeout.tv_sec <= 0) && (timeout.tv_usec <= 0)) return IRDY_TOUT;
return IRDY_NO;
}