diff options
Diffstat (limited to 'lib/fs.c')
-rw-r--r-- | lib/fs.c | 170 |
1 files changed, 85 insertions, 85 deletions
@@ -26,7 +26,7 @@ #include <pwd.h> #include <glob.h> -#include "../module.h" +#include "ucode/module.h" #define err_return(err) do { last_error = err; return NULL; } while(0) @@ -36,7 +36,7 @@ static uc_ressource_type_t *file_type, *proc_type, *dir_type; static int last_error = 0; static uc_value_t * -uc_fs_error(uc_vm *vm, size_t nargs) +uc_fs_error(uc_vm_t *vm, size_t nargs) { uc_value_t *errmsg; @@ -50,16 +50,16 @@ uc_fs_error(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_read_common(uc_vm *vm, size_t nargs, const char *type) +uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type) { - uc_value_t *limit = uc_get_arg(0); + uc_value_t *limit = uc_fn_arg(0); uc_value_t *rv = NULL; char buf[128], *p = NULL, *tmp; size_t rlen, len = 0; const char *lstr; int64_t lsize; - FILE **fp = uc_get_self(type); + FILE **fp = uc_fn_this(type); if (!fp || !*fp) err_return(EBADF); @@ -142,13 +142,13 @@ uc_fs_read_common(uc_vm *vm, size_t nargs, const char *type) } static uc_value_t * -uc_fs_write_common(uc_vm *vm, size_t nargs, const char *type) +uc_fs_write_common(uc_vm_t *vm, size_t nargs, const char *type) { - uc_value_t *data = uc_get_arg(0); + uc_value_t *data = uc_fn_arg(0); size_t len, wsize; char *str; - FILE **fp = uc_get_self(type); + FILE **fp = uc_fn_this(type); if (!fp || !*fp) err_return(EBADF); @@ -172,9 +172,9 @@ uc_fs_write_common(uc_vm *vm, size_t nargs, const char *type) static uc_value_t * -uc_fs_pclose(uc_vm *vm, size_t nargs) +uc_fs_pclose(uc_vm_t *vm, size_t nargs) { - FILE **fp = uc_get_self("fs.proc"); + FILE **fp = uc_fn_this("fs.proc"); int rc; if (!fp || !*fp) @@ -196,22 +196,22 @@ uc_fs_pclose(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_pread(uc_vm *vm, size_t nargs) +uc_fs_pread(uc_vm_t *vm, size_t nargs) { return uc_fs_read_common(vm, nargs, "fs.proc"); } static uc_value_t * -uc_fs_pwrite(uc_vm *vm, size_t nargs) +uc_fs_pwrite(uc_vm_t *vm, size_t nargs) { return uc_fs_write_common(vm, nargs, "fs.proc"); } static uc_value_t * -uc_fs_popen(uc_vm *vm, size_t nargs) +uc_fs_popen(uc_vm_t *vm, size_t nargs) { - uc_value_t *comm = uc_get_arg(0); - uc_value_t *mode = uc_get_arg(1); + uc_value_t *comm = uc_fn_arg(0); + uc_value_t *mode = uc_fn_arg(1); FILE *fp; if (ucv_type(comm) != UC_STRING) @@ -223,14 +223,14 @@ uc_fs_popen(uc_vm *vm, size_t nargs) if (!fp) err_return(errno); - return uc_alloc_ressource(proc_type, fp); + return uc_ressource_new(proc_type, fp); } static uc_value_t * -uc_fs_close(uc_vm *vm, size_t nargs) +uc_fs_close(uc_vm_t *vm, size_t nargs) { - FILE **fp = uc_get_self("fs.file"); + FILE **fp = uc_fn_this("fs.file"); if (!fp || !*fp) err_return(EBADF); @@ -242,26 +242,26 @@ uc_fs_close(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_read(uc_vm *vm, size_t nargs) +uc_fs_read(uc_vm_t *vm, size_t nargs) { return uc_fs_read_common(vm, nargs, "fs.file"); } static uc_value_t * -uc_fs_write(uc_vm *vm, size_t nargs) +uc_fs_write(uc_vm_t *vm, size_t nargs) { return uc_fs_write_common(vm, nargs, "fs.file"); } static uc_value_t * -uc_fs_seek(uc_vm *vm, size_t nargs) +uc_fs_seek(uc_vm_t *vm, size_t nargs) { - uc_value_t *ofs = uc_get_arg(0); - uc_value_t *how = uc_get_arg(1); + uc_value_t *ofs = uc_fn_arg(0); + uc_value_t *how = uc_fn_arg(1); int whence, res; long offset; - FILE **fp = uc_get_self("fs.file"); + FILE **fp = uc_fn_this("fs.file"); if (!fp || !*fp) err_return(EBADF); @@ -289,11 +289,11 @@ uc_fs_seek(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_tell(uc_vm *vm, size_t nargs) +uc_fs_tell(uc_vm_t *vm, size_t nargs) { long offset; - FILE **fp = uc_get_self("fs.file"); + FILE **fp = uc_fn_this("fs.file"); if (!fp || !*fp) err_return(EBADF); @@ -307,10 +307,10 @@ uc_fs_tell(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_open(uc_vm *vm, size_t nargs) +uc_fs_open(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); - uc_value_t *mode = uc_get_arg(1); + uc_value_t *path = uc_fn_arg(0); + uc_value_t *mode = uc_fn_arg(1); FILE *fp; if (ucv_type(path) != UC_STRING) @@ -322,14 +322,14 @@ uc_fs_open(uc_vm *vm, size_t nargs) if (!fp) err_return(errno); - return uc_alloc_ressource(file_type, fp); + return uc_ressource_new(file_type, fp); } static uc_value_t * -uc_fs_readdir(uc_vm *vm, size_t nargs) +uc_fs_readdir(uc_vm_t *vm, size_t nargs) { - DIR **dp = uc_get_self("fs.dir"); + DIR **dp = uc_fn_this("fs.dir"); struct dirent *e; if (!dp || !*dp) @@ -345,9 +345,9 @@ uc_fs_readdir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_telldir(uc_vm *vm, size_t nargs) +uc_fs_telldir(uc_vm_t *vm, size_t nargs) { - DIR **dp = uc_get_self("fs.dir"); + DIR **dp = uc_fn_this("fs.dir"); long position; if (!dp || !*dp) @@ -362,10 +362,10 @@ uc_fs_telldir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_seekdir(uc_vm *vm, size_t nargs) +uc_fs_seekdir(uc_vm_t *vm, size_t nargs) { - uc_value_t *ofs = uc_get_arg(0); - DIR **dp = uc_get_self("fs.dir"); + uc_value_t *ofs = uc_fn_arg(0); + DIR **dp = uc_fn_this("fs.dir"); long position; if (ucv_type(ofs) != UC_INTEGER) @@ -382,9 +382,9 @@ uc_fs_seekdir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_closedir(uc_vm *vm, size_t nargs) +uc_fs_closedir(uc_vm_t *vm, size_t nargs) { - DIR **dp = uc_get_self("fs.dir"); + DIR **dp = uc_fn_this("fs.dir"); if (!dp || !*dp) err_return(EBADF); @@ -396,9 +396,9 @@ uc_fs_closedir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_opendir(uc_vm *vm, size_t nargs) +uc_fs_opendir(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); DIR *dp; if (ucv_type(path) != UC_STRING) @@ -409,13 +409,13 @@ uc_fs_opendir(uc_vm *vm, size_t nargs) if (!dp) err_return(errno); - return uc_alloc_ressource(dir_type, dp); + return uc_ressource_new(dir_type, dp); } static uc_value_t * -uc_fs_readlink(uc_vm *vm, size_t nargs) +uc_fs_readlink(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); uc_value_t *res; ssize_t buflen = 0, rv; char *buf = NULL, *tmp; @@ -453,9 +453,9 @@ uc_fs_readlink(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_stat_common(uc_vm *vm, size_t nargs, bool use_lstat) +uc_fs_stat_common(uc_vm_t *vm, size_t nargs, bool use_lstat) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); uc_value_t *res, *o; struct stat st; int rv; @@ -537,22 +537,22 @@ uc_fs_stat_common(uc_vm *vm, size_t nargs, bool use_lstat) } static uc_value_t * -uc_fs_stat(uc_vm *vm, size_t nargs) +uc_fs_stat(uc_vm_t *vm, size_t nargs) { return uc_fs_stat_common(vm, nargs, false); } static uc_value_t * -uc_fs_lstat(uc_vm *vm, size_t nargs) +uc_fs_lstat(uc_vm_t *vm, size_t nargs) { return uc_fs_stat_common(vm, nargs, true); } static uc_value_t * -uc_fs_mkdir(uc_vm *vm, size_t nargs) +uc_fs_mkdir(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); - uc_value_t *mode = uc_get_arg(1); + uc_value_t *path = uc_fn_arg(0); + uc_value_t *mode = uc_fn_arg(1); if (ucv_type(path) != UC_STRING || (mode && ucv_type(mode) != UC_INTEGER)) @@ -565,9 +565,9 @@ uc_fs_mkdir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_rmdir(uc_vm *vm, size_t nargs) +uc_fs_rmdir(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); if (ucv_type(path) != UC_STRING) err_return(EINVAL); @@ -579,10 +579,10 @@ uc_fs_rmdir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_symlink(uc_vm *vm, size_t nargs) +uc_fs_symlink(uc_vm_t *vm, size_t nargs) { - uc_value_t *dest = uc_get_arg(0); - uc_value_t *path = uc_get_arg(1); + uc_value_t *dest = uc_fn_arg(0); + uc_value_t *path = uc_fn_arg(1); if (ucv_type(dest) != UC_STRING || ucv_type(path) != UC_STRING) @@ -595,9 +595,9 @@ uc_fs_symlink(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_unlink(uc_vm *vm, size_t nargs) +uc_fs_unlink(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); if (ucv_type(path) != UC_STRING) err_return(EINVAL); @@ -609,7 +609,7 @@ uc_fs_unlink(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_getcwd(uc_vm *vm, size_t nargs) +uc_fs_getcwd(uc_vm_t *vm, size_t nargs) { uc_value_t *res; char *buf = NULL, *tmp; @@ -645,9 +645,9 @@ uc_fs_getcwd(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_chdir(uc_vm *vm, size_t nargs) +uc_fs_chdir(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); + uc_value_t *path = uc_fn_arg(0); if (ucv_type(path) != UC_STRING) err_return(EINVAL); @@ -659,10 +659,10 @@ uc_fs_chdir(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_chmod(uc_vm *vm, size_t nargs) +uc_fs_chmod(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); - uc_value_t *mode = uc_get_arg(1); + uc_value_t *path = uc_fn_arg(0); + uc_value_t *mode = uc_fn_arg(1); if (ucv_type(path) != UC_STRING || ucv_type(mode) != UC_INTEGER) @@ -769,11 +769,11 @@ uc_fs_resolve_group(uc_value_t *v, gid_t *gid) } static uc_value_t * -uc_fs_chown(uc_vm *vm, size_t nargs) +uc_fs_chown(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); - uc_value_t *user = uc_get_arg(1); - uc_value_t *group = uc_get_arg(2); + uc_value_t *path = uc_fn_arg(0); + uc_value_t *user = uc_fn_arg(1); + uc_value_t *group = uc_fn_arg(2); uid_t uid; gid_t gid; @@ -791,10 +791,10 @@ uc_fs_chown(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_rename(uc_vm *vm, size_t nargs) +uc_fs_rename(uc_vm_t *vm, size_t nargs) { - uc_value_t *oldpath = uc_get_arg(0); - uc_value_t *newpath = uc_get_arg(1); + uc_value_t *oldpath = uc_fn_arg(0); + uc_value_t *newpath = uc_fn_arg(1); if (ucv_type(oldpath) != UC_STRING || ucv_type(newpath) != UC_STRING) @@ -807,14 +807,14 @@ uc_fs_rename(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_fs_glob(uc_vm *vm, size_t nargs) +uc_fs_glob(uc_vm_t *vm, size_t nargs) { uc_value_t *pat, *arr; glob_t gl = { 0 }; size_t i; for (i = 0; i < nargs; i++) { - pat = uc_get_arg(i); + pat = uc_fn_arg(i); if (ucv_type(pat) != UC_STRING) { globfree(&gl); @@ -835,14 +835,14 @@ uc_fs_glob(uc_vm *vm, size_t nargs) } -static const uc_cfunction_list proc_fns[] = { +static const uc_function_list_t proc_fns[] = { { "read", uc_fs_pread }, { "write", uc_fs_pwrite }, { "close", uc_fs_pclose }, { "error", uc_fs_error }, }; -static const uc_cfunction_list file_fns[] = { +static const uc_function_list_t file_fns[] = { { "read", uc_fs_read }, { "write", uc_fs_write }, { "seek", uc_fs_seek }, @@ -851,7 +851,7 @@ static const uc_cfunction_list file_fns[] = { { "error", uc_fs_error }, }; -static const uc_cfunction_list dir_fns[] = { +static const uc_function_list_t dir_fns[] = { { "read", uc_fs_readdir }, { "seek", uc_fs_seekdir }, { "tell", uc_fs_telldir }, @@ -859,7 +859,7 @@ static const uc_cfunction_list dir_fns[] = { { "error", uc_fs_error }, }; -static const uc_cfunction_list global_fns[] = { +static const uc_function_list_t global_fns[] = { { "error", uc_fs_error }, { "open", uc_fs_open }, { "opendir", uc_fs_opendir }, @@ -904,15 +904,15 @@ static void close_dir(void *ud) closedir(dp); } -void uc_module_init(uc_value_t *scope) +void uc_module_init(uc_vm_t *vm, uc_value_t *scope) { - uc_add_proto_functions(scope, global_fns); + uc_function_list_register(scope, global_fns); - proc_type = uc_declare_type("fs.proc", proc_fns, close_proc); - file_type = uc_declare_type("fs.file", file_fns, close_file); - dir_type = uc_declare_type("fs.dir", dir_fns, close_dir); + proc_type = uc_type_declare(vm, "fs.proc", proc_fns, close_proc); + file_type = uc_type_declare(vm, "fs.file", file_fns, close_file); + dir_type = uc_type_declare(vm, "fs.dir", dir_fns, close_dir); - ucv_object_add(scope, "stdin", uc_alloc_ressource(file_type, stdin)); - ucv_object_add(scope, "stdout", uc_alloc_ressource(file_type, stdout)); - ucv_object_add(scope, "stderr", uc_alloc_ressource(file_type, stderr)); + ucv_object_add(scope, "stdin", uc_ressource_new(file_type, stdin)); + ucv_object_add(scope, "stdout", uc_ressource_new(file_type, stdout)); + ucv_object_add(scope, "stderr", uc_ressource_new(file_type, stderr)); } |