shithub: mc

ref: 3e35cbe5f783ddfb57747e53ee3b70454b71001e
dir: /libstd/syswrap+plan9.myr/

View raw version
use sys
use "types.use"

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

	const Ordonly  	: fdopt = sys.Ordonly	castto(fdopt)
	const Owronly  	: fdopt = sys.Owronly	castto(fdopt)
	const Ordwr    	: fdopt = sys.Ordwr	castto(fdopt)
	const Otrunc   	: fdopt = sys.Otrunc	castto(fdopt)

	const Ocreat	: fdopt = 0x100000	/* Emulated by redirecting to create().*/
	const Oappend	: fdopt = 0	/* FIXME: Plan 9 doesn't seem to have an append bit? */
	const Odir	: fdopt = 0	/* Plan 9 doesn't need this */

	/* fd stuff */
	const open	: (path : byte[:], opts : fdopt -> fd)
	const openmode	: (path : byte[:], opts : fdopt, mode : int64 -> fd)
	const close	: (fd : fd -> int64)
	const creat	: (path : byte[:], mode : int64 -> fd)
	const read	: (fd : fd, buf : byte[:] -> size)
	const write	: (fd : fd, buf : byte[:] -> size)
	const pipe	: (fds : fd[2]# -> int64)

	/* path manipulation */
	const mkdir	: (path : byte[:], mode : int64 -> int64)
	const unlink	: (path : byte[:] -> int)

	/* process stuff */
	const getpid	: ( -> pid)
	const kill	: (pid:pid, sig:int64 -> int64)
	const fork	: (-> pid)
	const execv	: (cmd : byte[:], args : byte[:][:] -> int64)
	const execve	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
	const exit	: (status:int -> void)
	const waitpid	: (pid:pid, loc:int32#, opt : int64	-> int64)
;;

extern const _tos : 

/* fd stuff */
const open	= {path, opts;	-> openmode(path, opts, 0o777)}
const openmode	= {path, opts, mod
	if opts & Ocreat != 0
		-> sys.create(path, opts castto(sys.fdopt), mode castto(int)) castto(fd)
	else
		-> sys.open(path, opts castto(sys.fdopt)) castto(fd)
	;;
}
const close	= {fd;		-> sys.close(fd castto(sys.fd)) castto(int64)}
const creat	= {path, mode;	-> sys.create(path, sys.Owronly | sys.Otrunc, mode castto(int)) castto(fd)}
const read	= {fd, buf;	-> sys.pread(fd castto(sys.fd), buf, -1 castto(sys.off)) castto(size)}
const write	= {fd, buf;	-> sys.pwrite(fd castto(sys.fd), buf, -1 castto(sys.off)) castto(size)}
const pipe	= {fds;		-> sys.pipe(fds castto(sys.fd[2]#))}

/* path manipulation */
const mkdir	= {path, mod
	var fd

	fd = sys.create(path, sys.Ordonly, sys.Dmdir | (mode castto(int)))
	if fd >= 0
		sys.close(fd)
		-> 0
	else
		-> -1
	;;
}
const unlink	= {path;	-> sys.remove(path)}

/* process stuff */
const getpid	= {;		-> sys.gettos().pid}
const suicide	= {pid;		-> (0 castto(int#))#}
const fork	= {;		-> sys.rfork(sys.Rfproc | sys.Rffdg | sys.Rfrend) castto(pid)}
const execv	= {cmd, args;	-> sys.exec(cmd, args) castto(int64)}
const execve	= {cmd, args, env;	-> sys.exec(cmd, args) castto(int64)}	/* FIXME: don't ignore env */
const exit	= {status
	if status == 0
		sys.exits("")
	else
		sys.exits("failure")
	;;
}
const waitpid	= {pid, loc, opt
	/* FIXME: this is very wrong. */
	var buf : byte[512]
	sys.await(buf[:])
	loc# = 0
	-> 0
}