-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixio.c
14804 lines (13014 loc) · 427 KB
/
fixio.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FixScript IO v0.8 (modified) - https://www.fixscript.org/
* Copyright (c) 2019-2024 Martin Dvorak <jezek2@advel.cz>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
// ZLIB code available at http://public-domain.advel.cz/ under CC0 license
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error "FixIO supports little endian CPUs only"
#endif
#if defined(FIXBUILD_BINCOMPAT) && defined(__linux__)
#define fcntl fcntl__64
#endif
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <wchar.h>
#ifdef _WIN32
#define UNICODE
#define _UNICODE
#include <winsock2.h>
#include <mswsock.h>
#include <windows.h>
#else
#ifndef __wasm__
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/file.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <errno.h>
#ifndef __wasm__
#include <dirent.h>
#endif
#include <pthread.h>
#ifndef __wasm__
#include <signal.h>
#ifdef __linux__
#include <sys/epoll.h>
#else
#include <sys/poll.h>
#endif
#endif
#include <time.h>
#ifndef __wasm__
#include <sys/time.h>
#include <sys/wait.h>
#endif
#include <locale.h>
#ifndef __wasm__
#include <sys/ioctl.h>
#include <termios.h>
#endif
#endif
#ifdef __APPLE__
#include <mach/mach_time.h>
#include <xlocale.h>
#endif
#if defined(__APPLE__) || defined(__HAIKU__)
#include <sched.h>
#endif
#ifdef __wasm__
#include <wasm-support.h>
#endif
#ifdef FIXIO_SQLITE
#include "sqlite3.h"
#endif
#include "fixio.h"
#if defined(FIXBUILD_BINCOMPAT) && defined(__linux__)
#undef fcntl
extern int fcntl(int fd, int cmd, ...);
#if defined(__i386__)
asm(".symver fcntl,fcntl@GLIBC_2.0");
#elif defined(__x86_64__)
asm(".symver memcpy,memcpy@GLIBC_2.2.5");
#elif defined(__arm__)
#endif
#endif
enum {
ASYNC_READ = 1 << 0,
ASYNC_WRITE = 1 << 1
};
#if defined(__linux__)
#define USE_EPOLL
#elif defined(__wasm__)
#elif !defined(_WIN32)
#define USE_POLL
#endif
#if defined(USE_EPOLL)
typedef struct {
int epoll_fd;
int pipe_read_fd;
int pipe_write_fd;
struct epoll_event events[32];
int cur, cnt;
} Poll;
static Poll *poll_create()
{
Poll *poll;
struct epoll_event event;
int fds[2], flags;
poll = calloc(1, sizeof(Poll));
poll->epoll_fd = epoll_create(4);
if (poll->epoll_fd == -1) {
free(poll);
return NULL;
}
if (pipe(fds) != 0) {
close(poll->epoll_fd);
free(poll);
return NULL;
}
poll->pipe_read_fd = fds[0];
poll->pipe_write_fd = fds[1];
flags = fcntl(poll->pipe_read_fd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(poll->pipe_read_fd, F_SETFL, flags);
event.events = EPOLLIN;
event.data.ptr = NULL;
if (epoll_ctl(poll->epoll_fd, EPOLL_CTL_ADD, poll->pipe_read_fd, &event) != 0) {
close(poll->pipe_read_fd);
close(poll->pipe_write_fd);
close(poll->epoll_fd);
free(poll);
return NULL;
}
return poll;
}
static void poll_destroy(Poll *poll)
{
close(poll->pipe_read_fd);
close(poll->pipe_write_fd);
close(poll->epoll_fd);
free(poll);
}
static int poll_add_socket(Poll *poll, int fd, void *data, int mode)
{
struct epoll_event event;
event.events = 0;
if (mode & ASYNC_READ) event.events |= EPOLLIN;
if (mode & ASYNC_WRITE) event.events |= EPOLLOUT;
event.data.ptr = data;
return epoll_ctl(poll->epoll_fd, EPOLL_CTL_ADD, fd, &event) == 0;
}
static int poll_remove_socket(Poll *poll, int fd)
{
struct epoll_event event;
return epoll_ctl(poll->epoll_fd, EPOLL_CTL_DEL, fd, &event) == 0;
}
static int poll_update_socket(Poll *poll, int fd, void *data, int mode)
{
struct epoll_event event;
event.events = 0;
if (mode & ASYNC_READ) event.events |= EPOLLIN;
if (mode & ASYNC_WRITE) event.events |= EPOLLOUT;
event.data.ptr = data;
return epoll_ctl(poll->epoll_fd, EPOLL_CTL_MOD, fd, &event) == 0;
}
static void poll_interrupt(Poll *poll)
{
char c;
while (write(poll->pipe_write_fd, &c, 1) == 0);
}
static void poll_wait(Poll *poll, int timeout)
{
if (poll->cur < poll->cnt) return;
poll->cur = 0;
poll->cnt = epoll_wait(poll->epoll_fd, poll->events, sizeof(poll->events)/sizeof(struct epoll_event), timeout);
if (poll->cnt < 0) {
poll->cnt = 0;
}
}
static void *poll_get_event(Poll *poll, int *flags)
{
struct epoll_event *event;
char c;
for (;;) {
if (poll->cur >= poll->cnt) return NULL;
event = &poll->events[poll->cur++];
if (!event->data.ptr && (event->events & EPOLLIN)) {
while (read(poll->pipe_read_fd, &c, 1) == 1);
continue;
}
*flags = 0;
if (event->events & EPOLLIN) *flags |= ASYNC_READ;
if (event->events & EPOLLOUT) *flags |= ASYNC_WRITE;
return event->data.ptr;
}
}
#elif defined(USE_POLL)
typedef struct {
int epoll_fd;
int pipe_read_fd;
int pipe_write_fd;
struct pollfd *fds;
void **data;
int cap, cnt;
int pending;
} Poll;
static Poll *poll_create()
{
Poll *poll;
int fds[2], flags;
poll = calloc(1, sizeof(Poll));
poll->cap = 4;
poll->fds = malloc(poll->cap * sizeof(struct pollfd));
poll->data = malloc(poll->cap * sizeof(void *));
if (!poll->fds || !poll->data) {
free(poll->fds);
free(poll->data);
free(poll);
return NULL;
}
if (pipe(fds) != 0) {
free(poll->fds);
free(poll->data);
free(poll);
return NULL;
}
poll->pipe_read_fd = fds[0];
poll->pipe_write_fd = fds[1];
flags = fcntl(poll->pipe_read_fd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(poll->pipe_read_fd, F_SETFL, flags);
poll->cnt = 1;
poll->fds[0].fd = poll->pipe_read_fd;
poll->fds[0].events = POLLIN;
poll->data[0] = NULL;
return poll;
}
static void poll_destroy(Poll *poll)
{
close(poll->pipe_read_fd);
close(poll->pipe_write_fd);
free(poll->fds);
free(poll->data);
free(poll);
}
static int poll_add_socket(Poll *poll, int fd, void *data, int mode)
{
struct pollfd *new_fds;
void **new_data;
int i, new_cap;
for (i=0; i<poll->cnt; i++) {
if (poll->fds[i].fd == fd) return 0;
}
if (poll->cnt == poll->cap) {
if (poll->cap > (1<<26)) {
return 0;
}
new_cap = poll->cap << 1;
new_fds = realloc(poll->fds, new_cap * sizeof(struct pollfd));
if (!new_fds) {
return 0;
}
poll->fds = new_fds;
new_data = realloc(poll->data, new_cap * sizeof(void *));
if (!new_data) {
return 0;
}
poll->data = new_data;
poll->cap = new_cap;
}
poll->fds[poll->cnt].fd = fd;
poll->fds[poll->cnt].events = 0;
if (mode & ASYNC_READ) poll->fds[poll->cnt].events |= POLLIN;
if (mode & ASYNC_WRITE) poll->fds[poll->cnt].events |= POLLOUT;
poll->data[poll->cnt] = data;
poll->cnt++;
return 1;
}
static int poll_remove_socket(Poll *poll, int fd)
{
int i;
for (i=0; i<poll->cnt; i++) {
if (poll->fds[i].fd == fd) {
poll->fds[i] = poll->fds[poll->cnt-1];
poll->data[i] = poll->data[poll->cnt-1];
poll->cnt--;
return 1;
}
}
return 0;
}
static int poll_update_socket(Poll *poll, int fd, void *data, int mode)
{
int i;
for (i=0; i<poll->cnt; i++) {
if (poll->fds[i].fd == fd) {
poll->fds[i].events = 0;
if (mode & ASYNC_READ) poll->fds[i].events |= POLLIN;
if (mode & ASYNC_WRITE) poll->fds[i].events |= POLLOUT;
poll->data[i] = data;
return 1;
}
}
return 0;
}
static void poll_interrupt(Poll *poll)
{
char c;
while (write(poll->pipe_write_fd, &c, 1) == 0);
}
static void poll_wait(Poll *_poll, int timeout)
{
if (_poll->pending > 0) return;
_poll->pending = poll(_poll->fds, _poll->cnt, timeout);
if (_poll->pending < 0) {
_poll->pending = 0;
}
}
static void *poll_get_event(Poll *poll, int *flags)
{
int i;
char c;
if (poll->pending == 0) return NULL;
for (i=0; i<poll->cnt; i++) {
if (!poll->data[i] && (poll->fds[i].revents & POLLIN)) {
while (read(poll->pipe_read_fd, &c, 1) == 1);
if (--poll->pending == 0) return NULL;
continue;
}
if (poll->fds[i].revents & (POLLIN | POLLOUT)) {
*flags = 0;
if (poll->fds[i].revents & POLLIN) *flags |= ASYNC_READ;
if (poll->fds[i].revents & POLLOUT) *flags |= ASYNC_WRITE;
poll->pending--;
return poll->data[i];
}
}
return NULL;
}
#endif
#ifdef __wasm__
typedef int pid_t;
#endif
#ifdef _WIN32
#define ETIMEDOUT -1000
typedef CRITICAL_SECTION pthread_mutex_t;
typedef HANDLE pthread_cond_t;
#endif
enum {
ZC_GZIP = 1 << 0,
ZC_COMPRESS = 1 << 1
};
enum {
GZIP_FTEXT = 1 << 0,
GZIP_FHCRC = 1 << 1,
GZIP_FEXTRA = 1 << 2,
GZIP_FNAME = 1 << 3,
GZIP_FCOMMENT = 1 << 4
};
enum {
COMP_ERROR = -1,
COMP_DONE = 0,
COMP_FLUSH = 1,
COMP_MORE = 2
};
enum {
TYPE_FILE = 0,
TYPE_DIRECTORY = 1,
TYPE_SPECIAL = 2,
TYPE_SYMLINK = 0x80
};
enum {
SCRIPT_FILE_READ = 0x01,
SCRIPT_FILE_WRITE = 0x02,
SCRIPT_FILE_CREATE = 0x04,
SCRIPT_FILE_TRUNCATE = 0x08,
SCRIPT_FILE_APPEND = 0x10
};
enum {
EVENT_READ = 0x01,
EVENT_WRITE = 0x02
};
enum {
ASYNC_TCP_CONNECTION,
ASYNC_TCP_SERVER
};
enum {
REDIR_IN = 0x01,
REDIR_OUT = 0x02,
REDIR_ERR = 0x04,
REDIR_MERGE_ERR = 0x08
};
enum {
PRINT_NORMAL,
PRINT_LOG,
PRINT_PROMPT,
PRINT_PROGRESS
};
enum {
EVENT_NONE,
EVENT_CHAR_TYPED,
EVENT_KEY_PRESSED,
EVENT_CONSOLE_RESIZED
};
#ifdef _WIN32
#undef MOD_SHIFT
#undef MOD_ALT
#endif
enum {
MOD_CTRL = 0x01,
MOD_SHIFT = 0x02,
MOD_ALT = 0x04
};
enum {
KEY_NONE,
KEY_ESCAPE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
KEY_F7,
KEY_F8,
KEY_F9,
KEY_F10,
KEY_F11,
KEY_F12,
KEY_PRINT_SCREEN,
KEY_SCROLL_LOCK,
KEY_PAUSE,
KEY_GRAVE,
KEY_NUM1,
KEY_NUM2,
KEY_NUM3,
KEY_NUM4,
KEY_NUM5,
KEY_NUM6,
KEY_NUM7,
KEY_NUM8,
KEY_NUM9,
KEY_NUM0,
KEY_MINUS,
KEY_EQUAL,
KEY_BACKSPACE,
KEY_TAB,
KEY_Q,
KEY_W,
KEY_E,
KEY_R,
KEY_T,
KEY_Y,
KEY_U,
KEY_I,
KEY_O,
KEY_P,
KEY_LBRACKET,
KEY_RBRACKET,
KEY_BACKSLASH,
KEY_CAPS_LOCK,
KEY_A,
KEY_S,
KEY_D,
KEY_F,
KEY_G,
KEY_H,
KEY_J,
KEY_K,
KEY_L,
KEY_SEMICOLON,
KEY_APOSTROPHE,
KEY_ENTER,
KEY_LSHIFT,
KEY_Z,
KEY_X,
KEY_C,
KEY_V,
KEY_B,
KEY_N,
KEY_M,
KEY_COMMA,
KEY_PERIOD,
KEY_SLASH,
KEY_RSHIFT,
KEY_LCONTROL,
KEY_LMETA,
KEY_LALT,
KEY_SPACE,
KEY_RALT,
KEY_RMETA,
KEY_RMENU,
KEY_RCONTROL,
KEY_INSERT,
KEY_DELETE,
KEY_HOME,
KEY_END,
KEY_PAGE_UP,
KEY_PAGE_DOWN,
KEY_LEFT,
KEY_UP,
KEY_RIGHT,
KEY_DOWN,
KEY_NUM_LOCK,
KEY_NUMPAD_SLASH,
KEY_NUMPAD_STAR,
KEY_NUMPAD_MINUS,
KEY_NUMPAD_PLUS,
KEY_NUMPAD_ENTER,
KEY_NUMPAD_DOT,
KEY_NUMPAD0,
KEY_NUMPAD1,
KEY_NUMPAD2,
KEY_NUMPAD3,
KEY_NUMPAD4,
KEY_NUMPAD5,
KEY_NUMPAD6,
KEY_NUMPAD7,
KEY_NUMPAD8,
KEY_NUMPAD9
};
#define KEY_PRESSED(key, mod) ((EVENT_KEY_PRESSED << 28) | ((mod) << 24) | (key))
#ifdef FIXIO_SQLITE
enum {
SQL_TYPE_UNKNOWN = -1,
SQL_TYPE_INTEGER,
SQL_TYPE_LONG,
SQL_TYPE_FLOAT,
SQL_TYPE_DOUBLE,
SQL_TYPE_STRING,
SQL_TYPE_BINARY
};
#endif
typedef struct {
const unsigned char *src, *src_end;
unsigned char *dest, *dest_end;
} ZCommon;
// note: the source buffer (not the current pointers) must be able to store at least 258 bytes
#define ZCOMP_NUM_BUCKETS 4096 // 4096*8*2 = 64KB
#define ZCOMP_NUM_SLOTS 8
typedef struct {
const unsigned char *src, *src_end;
unsigned char *dest, *dest_end;
int src_final, src_flush;
int flushable;
int state;
uint32_t bits;
int num_bits;
unsigned char extra[7];
int extra_len;
unsigned char circular[32768];
int circular_pos, circular_written;
unsigned short hash[ZCOMP_NUM_BUCKETS * ZCOMP_NUM_SLOTS];
} ZCompress;
// note: the source buffer (not the current pointers) must be able to store at least 570 bytes
typedef struct {
const unsigned char *src, *src_end;
unsigned char *dest, *dest_end;
int state;
uint32_t bits;
int num_bits;
int final_block;
int remaining, dist;
uint16_t lit_symbols[288], lit_counts[16];
uint8_t dist_symbols[32], dist_counts[16];
char circular[32768];
int circular_pos, circular_written;
} ZUncompress;
typedef struct {
ZCompress z;
int state;
unsigned char extra[10];
int extra_len;
uint32_t crc;
uint32_t in_size;
} GZipCompress;
typedef struct {
ZUncompress z;
int state;
int flags;
int remaining;
uint32_t crc;
uint32_t out_size;
} GZipUncompress;
typedef struct {
Heap *heap;
void *state;
int size;
int read, written;
} CompressHandle;
typedef struct {
volatile int refcnt;
volatile int closed;
volatile int locked;
#if defined(_WIN32)
HANDLE handle;
#elif defined(__wasm__)
wasm_file_t *file;
#else
int fd;
#endif
int mode;
} FileHandle;
typedef struct {
volatile int refcnt;
volatile int closed;
#if defined(_WIN32)
SOCKET socket;
#else
int fd;
#endif
int in_nonblocking, want_nonblocking;
} TCPConnectionHandle;
typedef struct {
volatile int refcnt;
volatile int closed;
#if defined(_WIN32)
SOCKET socket;
#else
int fd;
#endif
} TCPServerHandle;
typedef struct AsyncThreadResult {
int type;
Value callback;
Value data;
#if defined(_WIN32)
SOCKET socket;
#else
int fd;
#endif
struct AsyncThreadResult *next;
} AsyncThreadResult;
typedef struct AsyncTimer {
int immediate;
uint32_t time;
Value callback;
Value data;
struct AsyncTimer *next;
} AsyncTimer;
#ifdef _WIN32
typedef struct {
DWORD transferred;
ULONG_PTR key;
void *overlapped;
} CompletedIO;
#endif
typedef struct {
volatile int refcnt;
pthread_mutex_t mutex;
AsyncThreadResult *thread_results;
#if defined(_WIN32)
HANDLE iocp;
CompletedIO completed_ios[32];
int num_events;
#elif defined(__wasm__)
void *poll;
#else
Poll *poll;
#endif
int quit;
Value quit_value;
AsyncTimer *timers;
pthread_mutex_t foreign_mutex;
pthread_cond_t foreign_cond;
IOEventNotifyFunc foreign_notify_func;
void *foreign_notify_data;
int foreign_processed;
} AsyncProcess;
typedef struct {
Value callback;
Value data;
Value array;
int off, len;
#if defined(_WIN32)
char buf[1024];
WSAOVERLAPPED overlapped;
#endif
} AsyncRead;
typedef struct {
Value callback;
Value data;
#if defined(_WIN32)
char buf[1024];
WSAOVERLAPPED overlapped;
#else
int result;
#endif
} AsyncWrite;
typedef struct {
AsyncProcess *proc;
int type;
#if defined(_WIN32)
SOCKET socket;
#else
int fd;
int last_active;
#endif
int active;
AsyncRead read;
AsyncWrite write;
} AsyncHandle;
typedef struct {
AsyncProcess *proc;
int type;
#if defined(_WIN32)
SOCKET socket;
SOCKET accept_socket;
char buf[(sizeof(struct sockaddr_in)+16)*2];
OVERLAPPED overlapped;
#else
int fd;
int last_active;
#endif
int active;
Value callback;
Value data;
} AsyncServerHandle;
typedef struct {
volatile int refcnt;
int flags;
#ifdef _WIN32
HANDLE process;
HANDLE in;
HANDLE out;
HANDLE err;
#else
volatile pid_t pid;
volatile int ret_value;
int in_fd;
int out_fd;
int err_fd;
#endif
} ProcessHandle;
typedef struct {
NativeFunc func;
void *data;
Value custom_func;
int recursive;
#ifdef __wasm__
ContinuationResultFunc cont_func;
void *cont_data;
#endif
} LogFunc;
#ifdef FIXIO_SQLITE
typedef struct {
sqlite3 *db;
} SQLiteHandle;
typedef struct {
sqlite3_stmt *stmt;
int done;
int ignore_step;
} SQLiteStatementHandle;
#endif
typedef int (*CompressFunc)(void *st);
#define NUM_HANDLE_TYPES 11
#define HANDLE_TYPE_ZCOMPRESS (handles_offset+0)
#define HANDLE_TYPE_ZUNCOMPRESS (handles_offset+1)
#define HANDLE_TYPE_GZIP_COMPRESS (handles_offset+2)
#define HANDLE_TYPE_GZIP_UNCOMPRESS (handles_offset+3)
#define HANDLE_TYPE_FILE (handles_offset+4)
#define HANDLE_TYPE_TCP_CONNECTION (handles_offset+5)
#define HANDLE_TYPE_TCP_SERVER (handles_offset+6)
#define HANDLE_TYPE_ASYNC (handles_offset+7)
#define HANDLE_TYPE_PROCESS (handles_offset+8)
#define HANDLE_TYPE_SQLITE (handles_offset+9)
#define HANDLE_TYPE_SQLITE_STMT (handles_offset+10)
static volatile int handles_offset;
static volatile int async_process_key;
static volatile int global_initialized;
typedef void (*ThreadFunc)(void *data);
typedef struct Thread {
pthread_mutex_t mutex;
pthread_cond_t cond;
ThreadFunc func;
void *data;
struct Thread *next;
} Thread;
#ifndef __wasm__
static volatile pthread_mutex_t *threads_mutex;
static Thread *threads_pool;
#endif
static volatile pthread_mutex_t *console_mutex;
#ifndef __wasm__
static int console_initialized = 0;
static char *console_clear_line;
#endif
#if !defined(_WIN32) && !defined(__wasm__)
static volatile pthread_mutex_t *console_auto_flush_mutex;
static volatile pthread_cond_t *console_auto_flush_cond;
static pthread_t console_auto_flush_thread;
static int console_auto_flush_trigger = 0;
static int console_auto_flush_active = 0;
static uint8_t console_input_buf[256];
static int console_input_len = 0;
#endif
#ifdef __APPLE__
extern const char **environ;
#endif
#ifdef _WIN32
static volatile int gui_app = -1;
#endif
#if !defined(_WIN32) && !defined(__HAIKU__)
static volatile locale_t cur_locale = NULL;
#endif
static volatile int console_is_present = -1;
#ifndef __wasm__
static volatile int console_size = -1;
static volatile int console_send_size_event = 0;
#endif
static int console_active = 0;
#ifdef _WIN32
static WORD console_cur_attr;
static CONSOLE_SCREEN_BUFFER_INFO last_csbi;
#else
static volatile int native_charset_utf8 = 0;
#endif
#if defined(_WIN32)
static int pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
{
InitializeCriticalSection(mutex);
return 0;
}
static int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
DeleteCriticalSection(mutex);
return 0;
}
static int pthread_mutex_lock(pthread_mutex_t *mutex)
{
EnterCriticalSection(mutex);
return 0;
}
static int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
return 0;
}
static int pthread_cond_init(pthread_cond_t *cond, void *attr)
{
*cond = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!(*cond)) {
return -1;
}
return 0;
}
static int pthread_cond_destroy(pthread_cond_t *cond)
{
CloseHandle(*cond);
*cond = NULL;
return 0;
}
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
WaitForSingleObject(*cond, INFINITE);
EnterCriticalSection(mutex);
return 0;
}
static int pthread_cond_timedwait_relative(pthread_cond_t *cond, pthread_mutex_t *mutex, int64_t timeout)
{
int ret = 0;
LeaveCriticalSection(mutex);
if (WaitForSingleObject(*cond, timeout/1000000) == WAIT_TIMEOUT) {
ret = ETIMEDOUT;
}
EnterCriticalSection(mutex);