-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.nelua
122 lines (106 loc) · 4.1 KB
/
sys.nelua
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
-- dl
## cdefine '_GNU_SOURCE'
## cinclude '<dlfcn.h>'
## linklib 'dl'
global function dlsym(handle: pointer, name: cstring): pointer <cimport, nodecl> end
global RTLD_NEXT: pointer <cimport, nodecl, const>
-- stdlib
## cinclude '<stdlib.h>'
global function realpath(name: cstring, resolved: cstring): cstring <cimport, nodecl> end
global function setenv(name: cstring, value: cstring, overwrite: cint): cint <cimport, nodecl> end
global function getenv(name: cstring): cstring <cimport, nodecl> end
global function unsetenv(name: cstring): cint <cimport, nodecl> end
global function exit(status: cint): void <cimport, nodecl> end
global function _exit(status: cint): void <cimport, nodecl> end
-- stdio
## cinclude '<stdio.h>'
global FILE <cimport,nodecl,forwarddecl> = @record{}
-- errno
## cinclude '<errno.h>'
global errno: cint <cimport, nodecl>
global EINTR: cint <cimport, nodecl, const>
-- time
## cinclude '<time.h>'
global timespec: type <cimport, nodecl, ctypedef 'timespec'> = @record{
tv_sec: clong,
tv_nsec: clong
}
global function nanosleep(requested_time: *timespec, remaining: *timespec): cint <cimport, nodecl> end
global function clock_gettime(clock_id: cint, tp: *timespec): cint <cimport, nodecl> end
global CLOCK_MONOTONIC: cint <comptime> = 1
-- signal
## cinclude '<signal.h>'
global function signal(sig: cint, handler: function(cint): void): function(cint): void <cimport, nodecl> end
global SIGKILL: cint <cimport, nodecl, const>
global SIGTERM: cint <cimport, nodecl, const>
global SIGSEGV: cint <cimport, nodecl, const>
global SIGBUS: cint <cimport, nodecl, const>
global SIGFPE: cint <cimport, nodecl, const>
global SIGABRT: cint <cimport, nodecl, const>
global SIGINT: cint <cimport, nodecl, const>
global SIGUSR2: cint <cimport, nodecl, const>
-- unistd
## cinclude '<unistd.h>'
global function getpid(): cint <cimport, nodecl> end
global function setpgid(pid: cint, pgid: cint): cint <cimport, nodecl> end
global function fork(): cint <cimport, nodecl> end
global function kill(pid: cint, sig: cint): cint <cimport, nodecl> end
global function read(fd: cint, buf: pointer, nbytes: csize): clong <cimport, nodecl> end
global function close(fd: cint): cint <cimport, nodecl> end
global function access(name: cstring, type: cint): cint <cimport, nodecl> end
-- wait
## cinclude '<sys/wait.h>'
global function waitpid(pid: cint, stat_loc: *cint, options: cint): cint <cimport, nodecl> end
-- inotify
## cinclude '<sys/inotify.h>'
global inotify_event: type <cimport, nodecl, ctypedef> = @record{
wd: cint,
mask: uint32,
cookie: uint32,
len: uint32,
name: [0]cchar
}
global function inotify_init(): cint <cimport, nodecl> end
global function inotify_add_watch(fd: cint, name: cstring, mask: uint32): cint <cimport, nodecl> end
global function inotify_rm_watch(fd: cint, wd: cint): cint <cimport, nodecl> end
global IN_MODIFY: cint <cimport, nodecl, const>
global IN_CREATE: cint <cimport, nodecl, const>
global IN_DELETE_SELF: cint <cimport, nodecl, const>
global IN_MOVE_SELF: cint <cimport, nodecl, const>
-- fcntl
## cinclude '<fcntl.h>'
global O_WRONLY: cint <cimport, nodecl, const>
global O_RDWR: cint <cimport, nodecl, const>
global O_CREAT: cint <cimport, nodecl, const>
global O_APPEND: cint <cimport, nodecl, const>
global F_OK: cint <cimport, nodecl, const>
-- terminal color list
global Colors = @record{}
global Colors.Yellow: string = '\27[33m'
global Colors.Red: string = '\27[31m'
global Colors.Reset: string = '\27[0m'
-- utilities
global function usleep(ms: uint64): void
local ts: timespec
ts.tv_sec = ms // 1000000
ts.tv_nsec = (ms % 1000000) * 1000
nanosleep(&ts, nilptr)
end
global function uticks(): uint64
local ts: timespec
clock_gettime(CLOCK_MONOTONIC, &ts)
return ts.tv_sec * 1000000 + ts.tv_nsec /// 1000
end
global function killwait(pid: cint, sig: cint): cint
kill(pid, sig)
return waitpid(pid, nilptr, 0)
end
global function filexists(path: cstring <const>): boolean
return access(path, F_OK) == 0
end
global function resolvpath(path: cstring, buf: cstring, len: cint): string
if #path < len and realpath(path, buf) then
return (@string){buf, #buf}
end
return (@string){}
end