From: Michael Wallner Date: Thu, 17 Dec 2015 15:44:19 +0000 (+0100) Subject: add some psis X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=974c9580535a1668dd6eed9992d582ebcc29b27a;p=m6w6%2Fext-psi add some psis --- diff --git a/psi.d/errno.psi b/psi.d/errno.psi new file mode 100644 index 0000000..4c987d6 --- /dev/null +++ b/psi.d/errno.psi @@ -0,0 +1,5 @@ +// redirected macro +extern int errno(); +function psi\errno() : int { + return to_int(errno); +} diff --git a/psi.d/stdio.psi b/psi.d/stdio.psi new file mode 100644 index 0000000..3e54ac4 --- /dev/null +++ b/psi.d/stdio.psi @@ -0,0 +1,66 @@ +// extern FILE *fopen(char *path, char *mode); +function psi\fopen(string $path, string $mode) : object { + let path = pathval($path); + let mode = strval($mode); + return to_object(*fopen); +} + +// extern int fclose(FILE *stream); +function psi\fclose(object $stream) : int { + let stream = objval($stream); + return to_int(fclose); +} + +// extern size_t fread(void *buf, size_t len, size_t n, FILE *stream); +function psi\fread(object $stream, string &$data = NULL, int $len = 1, int $n = 8192) : int { + let len = intval($len); + let n = intval($n); + let stream = objval($stream); + let buf = calloc(n, len); + return to_int(fread); + set $data = to_string(buf, fread * len); +} + +// extern int feof(FILE *stream); +function psi\feof(object $stream) : int { + let stream = objval($stream); + return to_int(feof); +} + +// extern int fileno(FILE *stream); +function psi\fileno(object $stream) : int { + let stream = objval($stream); + return to_int(fileno); +} + +// extern int ferror(FILE *stream); +function psi\ferror(object $stream) : int { + let stream = objval($stream); + return to_int(ferror); +} + +// extern void clearerr(FILE *stream); +function psi\clearerr(object $stream) : void { + let stream = objval($stream); + return void(clearerr); +} + +// extern int fseek(FILE *stream, long offset, int whence); +function psi\fseek(object $stream, int $offset, int $whence) : int { + let stream = objval($stream); + let offset = intval($offset); + let whence = intval($whence); + return to_int(fseek); +} + +//extern long ftell(FILE *stream); +function psi\ftell(object $stream) : int { + let stream = objval($stream); + return to_int(ftell); +} + +// extern void rewind(FILE *stream); +function psi\rewind(object $stream) : void { + let stream = objval($stream); + return void(rewind); +} diff --git a/psi.d/string.psi b/psi.d/string.psi new file mode 100644 index 0000000..5ea4db4 --- /dev/null +++ b/psi.d/string.psi @@ -0,0 +1,5 @@ +extern char *strerror(int errno); +function psi\strerror(int $errno) : string { + let errno = intval($errno); + return to_string(strerror); +} diff --git a/src/context.c b/src/context.c index 1a20af9..3c86f99 100644 --- a/src/context.c +++ b/src/context.c @@ -150,24 +150,24 @@ static struct psi_predef_const { PSI_MACROS -size_t psi_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) -{ - size_t rv = fread(ptr, size, nmemb, stream); +int psi_glob(const char *pattern, int flags, + int (*errfunc) (const char *epath, int eerrno), + glob_t *pglob) { + size_t offs = flags & GLOB_DOOFFS ? pglob->gl_offs : 0; + int rv = glob(pattern, flags, errfunc, pglob); + if (pglob->gl_pathv) { + while (offs--) { + pglob->gl_pathv[offs] = NULL; + } + } return rv; } -FILE *psi_fopen(const char *path, const char *mode) -{ - FILE *f = fopen(path, mode); - return f; -} - static struct psi_func_redir { const char *name; void (*func)(void); } psi_func_redirs[] = { - {"fopen", (void (*)(void)) psi_fopen}, - {"fread", (void (*)(void)) psi_fread}, + {"glob", (void (*)(void)) psi_glob}, PSI_REDIRS {0} };