summaryrefslogtreecommitdiff
path: root/lib/std/syswrap+plan9.myr
blob: 17b1f3940cd75b655038fe0abfc75a5b982cbd80 (plain)
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
use sys

use "option"
use "types"
use "errno"
use "result"
use "cstrconv"
use "strfind"
use "syswrap-ss"

pkg std =
	type fd		= sys.fd
	type pid	= sys.pid
	type fdopt	= sys.fdopt
	type whence = int64

	type sysinfo = struct
		system	: byte[:]
		version	: byte[:]
		release	: byte[:]
		arch	: byte[:]
	;;

	const Seekset	: whence = 0
	const Seekcur	: whence = 1
	const Seekend	: whence = 2

	const Failmem	: byte#	= (-1 : byte#)

	const Ordonly  	: fdopt = (sys.Ordonly	: fdopt)
	const Owronly  	: fdopt = (sys.Owronly	: fdopt)
	const Ordwr    	: fdopt = (sys.Ordwr	: fdopt)
	const Otrunc   	: fdopt = (sys.Otrunc	: fdopt)
	const Ocexec	: fdopt = (sys.Ocexec	: fdopt)
	const Ocreat   	: fdopt = 0x1000000	/* emulated by redirecting to creat(). */
	const Oappend  	: fdopt = 0x2000000	/* emulated by seeking to EOF */
	const Odir	: fdopt = 0x0	/* no-op on plan9 */

	/* fd stuff */
	const open	: (path : byte[:], opts : fdopt -> result(fd, errno))
	const openmode	: (path : byte[:], opts : fdopt, mode : int64 -> result(fd, errno))
	const close	: (fd : fd -> errno)
	const read	: (fd : fd, buf : byte[:] -> result(size, errno))
	const pread	: (fd : fd, buf : byte[:], off : off -> result(size, errno))
	const write	: (fd : fd, buf : byte[:] -> result(size, errno))
	const pwrite	: (fd : fd, buf : byte[:], off : off -> result(size, errno))
	const seek	: (fd : fd, delta : off, whence : whence -> result(off, errno))
	const pipe	: (fds : fd[2]# -> errno)
	const dup2	: (ofd : fd, nfd : fd -> result(fd, errno))

	/* useful/portable bits of stat */
	const fmtime	: (f : byte[:]	-> result(time, errno))
	const fsize	: (f : byte[:]	-> result(off, errno))
	const fexists	: (f : byte[:]	-> bool)
	const fisdir	: (f : byte[:]	-> bool)
	const fisreg	: (f : byte[:]	-> bool)

	/* the important bits that uname provides */
	const getsysinfo	: (si : sysinfo# -> void)

	/* path manipulation */
	const mkdir	: (path : byte[:], mode : int64 -> errno)
	const chdir	: (path : byte[:] -> bool)
	const remove	: (path : byte[:] -> bool)

	/* process stuff */
	const getpid	: ( -> pid)
	const suicide	: ( -> void)
	const fork	: (-> pid)
	const execv	: (cmd : byte[:], args : byte[:][:] -> errno)
	const execve	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> errno)

	pkglocal const Canunmap	: bool = true
	pkglocal const getmem	: (sz : size -> byte#)
	pkglocal const freemem	: (p : byte#, sz : size -> void)
	pkglocal const curtime	: (-> time)
	pkglocal const p9errstr	: (buf : byte[:] -> byte[:])

	/* statbuf offsets */
	pkglocal const Sizeoff	: int64 = 0
	pkglocal const Sizesz	: int64 = 2
	pkglocal const Typeoff	: int64 = Sizeoff + Sizesz
	pkglocal const Typesz	: int64 = 2
	pkglocal const Devoff	: int64 = Typeoff + Typesz
	pkglocal const Devsz	: int64 = 4
	pkglocal const Qidtypeoff	: int64 = Devoff + Devsz
	pkglocal const Qidtypesz	: int64 = 1
	pkglocal const Qidversoff	: int64 = Qidtypeoff + Qidtypesz
	pkglocal const Qidverssz	: int64	= 4
	pkglocal const Qidpathoff	: int64 = Qidversoff + Qidverssz
	pkglocal const Qidpathsz	: int64 = 8
	pkglocal const Modeoff	: int64 = Qidpathoff + Qidpathsz
	pkglocal const Modesz	: int64 = 4
	pkglocal const Atimeoff	: int64 = Modeoff + Modesz
	pkglocal const Atimesz	: int64 = 4
	pkglocal const Mtimeoff	: int64 = Atimeoff + Atimesz
	pkglocal const Mtimesz	: int64 = 4
	pkglocal const Lengthoff	: int64 = Mtimeoff + Mtimesz
	pkglocal const Lengthsz	: int64 = 8
	pkglocal const Stringsoff	: int64 = Lengthoff + Lengthsz
;;

/* UGLY: circular dependency breaking... */
extern const getenvv : (name : byte[:], default : byte[:] -> byte[:])

/* fd stuff */
const open	= {path, opts;	-> check(sys.open(path, (opts : sys.fdopt)))}
const openmode	= {path, opts, mode;
	var fd


	if opts & Ocreat != 0
		opts &= ~Ocreat
		fd = sys.create(path, (opts : sys.fdopt), (mode : int))
	else
		fd = sys.open(path, (opts : sys.fdopt))
	;;
	if opts & Oappend != 0
		sys.seek(fd, 0, 2)
	;;
	-> check(fd)
}


/* useful/portable bits of stat */
const fexists = {path
	var buf : byte[4]	/* big enough for size, nothing else. */

	-> sys.stat(path, buf[:]) >= 0
}

const fmtime = {path
	var buf	: byte[Stringsoff + 512]	/* enough space for some strings */

	if sys.stat(path, buf[:]) < Stringsoff
		-> `Err Emisc
	;;
	-> `Ok (_getle32(buf[Mtimeoff:Mtimeoff + 8]) : time)
}

const fsize = {path
	var buf	: byte[Stringsoff + 512]	/* enough space for some strings */

	if sys.stat(path, buf[:]) < Stringsoff
		-> `Err Emisc
	;;
	-> `Ok (_getle64(buf[Lengthoff:Lengthoff + 8]) : off)
}

extern const put : (fmt : byte[:], args : ... -> int64)
const fisdir = {path
	var buf	: byte[Stringsoff + 39]	/* enough space for qid type */

	if sys.stat(path, buf[:]) < Stringsoff
		-> false
	;;
	-> (_getle32(buf[Modeoff:Modeoff + 4]) & sys.Dmdir) != 0
}

const fisreg = {path
	var buf	: byte[Stringsoff + 512]	/* enough space for some strings */
	var ty

	if sys.stat(path, buf[:]) < Stringsoff
		-> false
	;;

	ty = _getle16(buf[Typeoff:Typeoff + 2])
	if (_getle32(buf[Modeoff:Modeoff + 4]) & sys.Dmdir) != 0
		-> false
	/* ugh. maybe this call should just be '!fisdir()' */
	elif ty == '|' || ty == 's' || ty == 'm'
		-> false
	else
		-> true
	;;
}

const getsysinfo = {si
	si.system = getenvv("osname", "Plan9")
	si.release = "4"
	si.version = "0"
	si.arch = getenvv("objtype", "amd64")
}

const close	= {fd;		-> (sys.close((fd : sys.fd)) : errno)}
const read	= {fd, buf;	-> check(sys.pread((fd : sys.fd), buf, -1))}
const pread	= {fd, buf,off;	-> check(sys.pread((fd : sys.fd), buf, (off : sys.off)))}
const write	= {fd, buf;	-> check(sys.pwrite((fd : sys.fd), buf, -1))}
const pwrite	= {fd, buf,off;	-> check(sys.pwrite((fd : sys.fd), buf, (off : sys.off)))}
const seek	= {fd, off, whence; 	-> check(sys.seek((fd : sys.fd), (off : sys.off), (whence : int64)))}
const pipe	= {fds;		-> (sys.pipe((fds : sys.fd[2]#)) : errno)}
const dup2	= {ofd, nfd;	-> check(sys.dup((ofd : sys.fd), (nfd : sys.fd)))}

/* path manipulation */
const remove	= {path;	-> sys.remove(path) == 0}
const chdir	= {path;	-> sys.chdir(path) == 0}
const mkdir	= {path, mode;
	var fd

	fd = sys.create(path, sys.Ordonly, sys.Dmdir | (mode : int))
	if fd < 0
		-> lasterr()
	;;
	sys.close(fd)
	-> 0
}

/* process stuff */
const getpid	= {;	-> (sys.tosptr.pid : pid)}
const suicide	= {;	(0 : byte#)#}	/* let's happy segfault!! t */
const fork	= {;		-> (sys.rfork(sys.Rffdg | sys.Rfrend | sys.Rfproc) : pid)}
const execv	= {cmd, args;
	sys.exec(cmd, args)
	-> lasterr()
}

const execve	= {cmd, args, env;
	sys.exec(cmd, args)
	-> lasterr()
}



/* memory stuff */
const getmem	= {sz
	var endp, oldp

	oldp = sys.curbrk
	endp = (sys.curbrk : intptr) + (sz : intptr)
	endp = (endp + 4095) & ~4095
	if sys.brk_((endp : byte#)) < 0
		-> Failmem
	;;
	sys.curbrk = (endp : byte#)
	-> oldp
}
	
const freemem = {p, sz
	/* FIXME: we leak address space */
	sys.segfree(p, (sz : sys.size))
}

const curtime = {
	-> (sys.nsec()/1000 : time)
}

const p9errstr = {errbuf
	var i

	sys.errstr(errbuf)
	for i = 0; errbuf[i] != 0 && i < errbuf.len; i++
		continue
	;;
	-> errbuf[:i]
}

/* FIXME: will be needed when we resize stat bufs when statting.
const statsz = {buf
	-> (buf[0] : int64)) | ((buf[1] << 8 : int64))
}
*/

generic check = {e : @a::(integral, numeric) -> result(@b, errno)
	if e < 0
		-> `Err lasterr()
	else
		-> `Ok (e : @b)
	;;
}

/* duplicated code to break dependency cycle */
generic _getle16 = {buf
	-> ((buf[0] : @a::(numeric,integral)) << 0) | \
		((buf[1] : @a::(numeric,integral)) << 8)
}

generic _getle32 = {buf
	-> ((buf[0] : @a::(numeric,integral)) << 0) | \
		((buf[1] : @a::(numeric,integral)) << 8) | \
		((buf[2] : @a::(numeric,integral)) << 16) | \
		((buf[3] : @a::(numeric,integral)) << 24)
}

generic _getle64 = {buf
	-> ((buf[0] : @a::(numeric,integral))  << 0) | \
		((buf[1] : @a::(numeric,integral))  << 8) | \
		((buf[2] : @a::(numeric,integral))  << 16) | \
		((buf[3] : @a::(numeric,integral))  << 24) | \
		((buf[4] : @a::(numeric,integral))  << 32) | \
		((buf[5] : @a::(numeric,integral))  << 40) | \
		((buf[6] : @a::(numeric,integral))  << 48) | \
		((buf[7] : @a::(numeric,integral))  << 56)
}