view modules/os.tp @ 347:ff7ea11b4b60

Add length method to executable bytearrays
author Michael Pavone <pavone@retrodev.com>
date Fri, 10 Apr 2015 00:48:12 -0700
parents f987bb2a1911
children
line wrap: on
line source
{
	_constant <- macro: :name {
		quote: (llMessage: name withVars: {
			uintret <- obj_int32 ptr
		} andCode: {
			uintret <- make_object: (addr_of: obj_int32_meta) NULL 0
			uintret num!: name
			uintret
		})
	}
	#{
		includeSystemHeader: "sys/stat.h"
		includeSystemHeader: "fcntl.h"
		includeSystemHeader: "stdlib.h"
		includeSystemHeader: "time.h"
		includeSystemHeader: "unistd.h"

		llMessage: write withVars: {
			str <- string ptr
			intret <- obj_int32 ptr
			filedes <- obj_int32 ptr
		} andCode: :filedes str {
			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
			intret num!: (write: (filedes num) (str data) (str bytes))
			intret
		}

		llMessage: read withVars: {
			str <- string ptr
			size <- obj_int32 ptr
			filedes <- obj_int32 ptr
		} andCode: :filedes size {
			str <- make_object: (addr_of: string_meta) NULL 0
			str data!: (GC_MALLOC_ATOMIC: (size num) + 1)
			str bytes!: (read: (filedes num) (str data) (size num))
			if: (str bytes) < 0 {
				str bytes!: 0
			}
			str len!: (str bytes)
			(str data) set: (str bytes) 0
			str
		}

		llMessage: readFrom:to withVars: {
			buffer <- object ptr
			bufOpaque <- cpointer ptr
			size <- obj_int32 ptr
			filedes <- obj_int32 ptr
			intret <- obj_int32 ptr
		} andCode: :filedes buffer {
			bufOpaque <- (mcall: _buf_ptr 1 buffer) castTo: (cpointer ptr)
			size <- (mcall: length 1 buffer) castTo: (obj_int32 ptr)
			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
			intret num!: (read: (filedes num) (bufOpaque val) (size num))
			intret
		}

		open <- :filename flags {
			open: filename flags createPerm: 0b110110110
		}

		llMessage: open:createPerm withVars: {
			filename <- string ptr
			flags <- obj_int32 ptr
			perm <- obj_int32 ptr
			filedes <- obj_int32 ptr
		} andCode: :filename flags :perm {
			filedes <- make_object: (addr_of: obj_int32_meta) NULL 0
			filedes num!: (open: (filename data) (flags num) (perm num))
			filedes
		}

		llMessage: close withVars: {
			filedes <- obj_int32 ptr
			intret <- obj_int32 ptr
		} andCode: :filedes {
			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
			intret num!: (close: (filedes num))
			intret
		}

		_constant: O_RDONLY
		_constant: O_WRONLY
		_constant: O_RDWR
		_constant: O_CREAT
		_constant: O_APPEND
		_constant: O_TRUNC

		llMessage: rand withVars: {
			intret <- obj_int32 ptr
		} andCode: {
			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
			intret num!: (rand: )
			intret
		}

		llMessage: srand withVars: {
			oseed <- object ptr
			seed <- obj_int32 ptr
		} andCode: :oseed {
			seed <- (mcall: i32 1 oseed) castTo: (obj_int32 ptr)
			srand: (seed num)
			seed
		}

		llMessage: time withVars: {
			intret64 <- obj_int64 ptr
		} andCode: {
			intret64 <- make_object: (addr_of: obj_int64_meta) NULL 0
			intret64 num!: (time: NULL)
			intret64
		}

		llMessage: sleep withVars: {
			osecs <- object ptr
			secs <- obj_int32 ptr
			intret <- obj_int32 ptr
		} andCode: :osecs {
			secs <- (mcall: i32 1 osecs) castTo: (obj_int32 ptr)
			intret <- make_object: (addr_of: obj_int32_meta) NULL 0
			intret num!: (sleep: (secs num))
			intret
		}

		llMessage: execv withVars: {
			opath <- object ptr
			path <- string ptr
			eargs <- object ptr
			oarglen <- object ptr
			arglen <- obj_int32 ptr
			i <- int32_t
			oi <- obj_int32 ptr
			oarg <- object ptr
			arg <- string ptr
			cargs <- (char ptr) ptr
		} andCode: :opath eargs {
			path <- (mcall: string 1 opath) castTo: (string ptr)
			oarglen <- mcall: length 1 eargs
			arglen <- (mcall: int32 1 oarglen) castTo: (obj_int32 ptr)
			cargs <- GC_MALLOC_ATOMIC: (sizeof: (char ptr)) * ((arglen num) + 1)
			i <- 0
			while: { i < (arglen num) } do: {
				oi <- make_object: (addr_of: obj_int32_meta) NULL 0
				oi num!: i
				oarg <- mcall: get 2 eargs oi
				arg <- (mcall: string 1 oarg) castTo: (string ptr)
				cargs set: i (arg data)
				i <- i + 1
			}
			cargs set: i NULL
			i <- execv: (path data) cargs
			oi <- make_object: (addr_of: obj_int32_meta) NULL 0
			oi num!: i
			oi
		}

		llMessage: execvp withVars: {
			opath <- object ptr
			path <- string ptr
			eargs <- object ptr
			oarglen <- object ptr
			arglen <- obj_int32 ptr
			i <- int32_t
			oi <- obj_int32 ptr
			oarg <- object ptr
			arg <- string ptr
			cargs <- (char ptr) ptr
		} andCode: :opath eargs {
			path <- (mcall: string 1 opath) castTo: (string ptr)
			oarglen <- mcall: length 1 eargs
			arglen <- (mcall: int32 1 oarglen) castTo: (obj_int32 ptr)
			cargs <- GC_MALLOC_ATOMIC: (sizeof: (char ptr)) * ((arglen num) + 1)
			i <- 0
			while: { i < (arglen num) } do: {
				oi <- make_object: (addr_of: obj_int32_meta) NULL 0
				oi num!: i
				oarg <- mcall: get 2 eargs oi
				arg <- (mcall: string 1 oarg) castTo: (string ptr)
				cargs set: i (arg data)
				i <- i + 1
			}
			cargs set: i NULL
			i <- execvp: (path data) cargs
			oi <- make_object: (addr_of: obj_int32_meta) NULL 0
			oi num!: i
			oi
		}

		llMessage: execve withVars: {
			opath <- object ptr
			path <- string ptr
			eargs <- object ptr
			env <- object ptr
			oarglen <- object ptr
			arglen <- obj_int32 ptr
			i <- int32_t
			oi <- obj_int32 ptr
			oarg <- object ptr
			arg <- string ptr
			cargs <- (char ptr) ptr
			cenv <- (char ptr) ptr
		} andCode: :opath eargs env {
			path <- (mcall: string 1 opath) castTo: (string ptr)
			oarglen <- mcall: length 1 eargs
			arglen <- (mcall: int32 1 oarglen) castTo: (obj_int32 ptr)
			cargs <- GC_MALLOC_ATOMIC: (sizeof: (char ptr)) * ((arglen num) + 1)
			i <- 0
			while: { i < (arglen num) } do: {
				oi <- make_object: (addr_of: obj_int32_meta) NULL 0
				oi num!: i
				oarg <- mcall: get 2 eargs oi
				arg <- (mcall: string 1 oarg) castTo: (string ptr)
				cargs set: i (arg data)
				i <- i + 1
			}
			cargs set: i NULL

			oarglen <- mcall: length 1 eargs
			arglen <- (mcall: int32 1 oarglen) castTo: (obj_int32 ptr)
			cenv <- GC_MALLOC_ATOMIC: (sizeof: (char ptr)) * ((arglen num) + 1)
			i <- 0
			while: { i < (arglen num) } do: {
				oi <- make_object: (addr_of: obj_int32_meta) NULL 0
				oi num!: i
				oarg <- mcall: get 2 env oi
				arg <- (mcall: string 1 oarg) castTo: (string ptr)
				cenv set: i (arg data)
				i <- i + 1
			}
			cenv set: i NULL

			i <- execve: (path data) cargs cenv
			oi <- make_object: (addr_of: obj_int32_meta) NULL 0
			oi num!: i
			oi
		}
	}
}