convertin plan9 functions to new API

This commit is contained in:
Alex Shinn 2010-03-30 22:06:16 +09:00
parent a7794e8189
commit 71821fc95a

View file

@ -1,17 +1,17 @@
/* plan9.c -- extended Plan 9 system utils */ /* plan9.c -- extended Plan 9 system utils */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */ /* Copyright (c) 2009-2010 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */ /* BSD-style license: http://synthcode.com/license.txt */
sexp sexp_rand (sexp ctx) { sexp sexp_rand (sexp ctx sexp_api_params(self, n)) {
return sexp_make_fixnum(rand()); return sexp_make_fixnum(rand());
} }
sexp sexp_srand (sexp ctx, sexp seed) { sexp sexp_srand (sexp ctx sexp_api_params(self, n), sexp seed) {
srand(sexp_unbox_fixnum(seed)); srand(sexp_unbox_fixnum(seed));
return SEXP_VOID; return SEXP_VOID;
} }
sexp sexp_file_exists_p (sexp ctx, sexp path) { sexp sexp_file_exists_p (sexp ctx sexp_api_params(self, n), sexp path) {
int res; int res;
uchar statbuf[STATMAX]; uchar statbuf[STATMAX];
if (! sexp_stringp(path)) if (! sexp_stringp(path))
@ -20,7 +20,7 @@ sexp sexp_file_exists_p (sexp ctx, sexp path) {
return (res < 0) ? SEXP_FALSE : SEXP_TRUE; return (res < 0) ? SEXP_FALSE : SEXP_TRUE;
} }
sexp sexp_fdopen (sexp ctx, sexp fd, sexp mode) { sexp sexp_fdopen (sexp ctx sexp_api_params(self, n), sexp fd, sexp mode) {
FILE *f; FILE *f;
if (! sexp_integerp(fd)) if (! sexp_integerp(fd))
return sexp_type_exception(ctx, "fdopen: not an integer", fd); return sexp_type_exception(ctx, "fdopen: not an integer", fd);
@ -36,17 +36,17 @@ sexp sexp_fdopen (sexp ctx, sexp fd, sexp mode) {
return sexp_make_input_port(ctx, f, SEXP_FALSE); return sexp_make_input_port(ctx, f, SEXP_FALSE);
} }
sexp sexp_fileno (sexp ctx, sexp port) { sexp sexp_fileno (sexp ctx sexp_api_params(self, n), sexp port) {
if (! sexp_portp(port)) if (! sexp_portp(port))
return sexp_type_exception(ctx, "fileno: not a port", port); return sexp_type_exception(ctx, "fileno: not a port", port);
return sexp_make_fixnum(fileno(sexp_port_stream(port))); return sexp_make_fixnum(fileno(sexp_port_stream(port)));
} }
sexp sexp_fork (sexp ctx) { sexp sexp_fork (sexp ctx sexp_api_params(self, n)) {
return sexp_make_fixnum(fork()); return sexp_make_fixnum(fork());
} }
sexp sexp_exec (sexp ctx, sexp name, sexp args) { sexp sexp_exec (sexp ctx sexp_api_params(self, n), sexp name, sexp args) {
int i, len = sexp_unbox_fixnum(sexp_length(ctx, args)); int i, len = sexp_unbox_fixnum(sexp_length(ctx, args));
char **argv = malloc((len+1)*sizeof(char*)); char **argv = malloc((len+1)*sizeof(char*));
for (i=0; i<len; i++, args=sexp_cdr(args)) for (i=0; i<len; i++, args=sexp_cdr(args))
@ -56,30 +56,30 @@ sexp sexp_exec (sexp ctx, sexp name, sexp args) {
return SEXP_VOID; /* won't really return */ return SEXP_VOID; /* won't really return */
} }
void sexp_exits (sexp ctx, sexp msg) { void sexp_exits (sexp ctx sexp_api_params(self, n), sexp msg) {
exits(sexp_string_data(sexp_stringp(msg) exits(sexp_string_data(sexp_stringp(msg)
? msg : sexp_write_to_string(ctx, msg))); ? msg : sexp_write_to_string(ctx, msg)));
} }
sexp sexp_dup (sexp ctx, sexp oldfd, sexp newfd) { sexp sexp_dup (sexp ctx sexp_api_params(self, n), sexp oldfd, sexp newfd) {
return sexp_make_fixnum(dup(sexp_unbox_fixnum(oldfd), return sexp_make_fixnum(dup(sexp_unbox_fixnum(oldfd),
sexp_unbox_fixnum(newfd))); sexp_unbox_fixnum(newfd)));
} }
sexp sexp_pipe (sexp ctx) { sexp sexp_pipe (sexp ctx sexp_api_params(self, n)) {
int fds[2]; int fds[2];
pipe(fds); pipe(fds);
return sexp_list2(ctx, sexp_make_fixnum(fds[0]), sexp_make_fixnum(fds[1])); return sexp_list2(ctx, sexp_make_fixnum(fds[0]), sexp_make_fixnum(fds[1]));
} }
sexp sexp_sleep (sexp ctx, sexp msecs) { sexp sexp_sleep (sexp ctx sexp_api_params(self, n), sexp msecs) {
if (! sexp_integerp(msecs)) if (! sexp_integerp(msecs))
return sexp_type_exception(ctx, "sleep: not an integer", msecs); return sexp_type_exception(ctx, "sleep: not an integer", msecs);
sleep(sexp_unbox_fixnum(msecs)); sleep(sexp_unbox_fixnum(msecs));
return SEXP_VOID; return SEXP_VOID;
} }
sexp sexp_getenv (sexp ctx, sexp name) { sexp sexp_getenv (sexp ctx sexp_api_params(self, n), sexp name) {
char *value; char *value;
if (! sexp_stringp(name)) if (! sexp_stringp(name))
return sexp_type_exception(ctx, "getenv: not a string", name); return sexp_type_exception(ctx, "getenv: not a string", name);
@ -87,28 +87,28 @@ sexp sexp_getenv (sexp ctx, sexp name) {
return ((! value) ? SEXP_FALSE : sexp_c_string(ctx, value, -1)); return ((! value) ? SEXP_FALSE : sexp_c_string(ctx, value, -1));
} }
sexp sexp_getwd (sexp ctx) { sexp sexp_getwd (sexp ctx sexp_api_params(self, n)) {
char buf[512]; char buf[512];
getwd(buf, 512); getwd(buf, 512);
return sexp_c_string(ctx, buf, -1); return sexp_c_string(ctx, buf, -1);
} }
sexp sexp_chdir (sexp ctx, sexp path) { sexp sexp_chdir (sexp ctx sexp_api_params(self, n), sexp path) {
if (! sexp_stringp(path)) if (! sexp_stringp(path))
return sexp_type_exception(ctx, "chdir: not a string", path); return sexp_type_exception(ctx, "chdir: not a string", path);
chdir(sexp_string_data(path)); chdir(sexp_string_data(path));
return SEXP_VOID; return SEXP_VOID;
} }
sexp sexp_getuser (sexp ctx) { sexp sexp_getuser (sexp ctx sexp_api_params(self, n)) {
return sexp_c_string(ctx, getuser(), -1); return sexp_c_string(ctx, getuser(), -1);
} }
sexp sexp_sysname (sexp ctx) { sexp sexp_sysname (sexp ctx sexp_api_params(self, n)) {
return sexp_c_string(ctx, sysname(), -1); return sexp_c_string(ctx, sysname(), -1);
} }
sexp sexp_wait (sexp ctx) { /* just return (pid msg) */ sexp sexp_wait (sexp ctx sexp_api_params(self, n)) { /* just return (pid msg) */
Waitmsg *wmsg; Waitmsg *wmsg;
sexp res; sexp res;
sexp_gc_var(ctx, msg, s_msg); sexp_gc_var(ctx, msg, s_msg);
@ -120,7 +120,7 @@ sexp sexp_wait (sexp ctx) { /* just return (pid msg) */
return res; return res;
} }
sexp sexp_postnote (sexp ctx, sexp pid, sexp note) { sexp sexp_postnote (sexp ctx sexp_api_params(self, n), sexp pid, sexp note) {
if (! sexp_integerp(pid)) if (! sexp_integerp(pid))
return sexp_type_exception(ctx, "postnote: not an integer", pid); return sexp_type_exception(ctx, "postnote: not an integer", pid);
if (! sexp_stringp(note)) if (! sexp_stringp(note))
@ -282,7 +282,7 @@ void sexp_9p_end (Srv *srv) {
sexp_gc_release(ctx, ptr, s_ptr); sexp_gc_release(ctx, ptr, s_ptr);
} }
sexp sexp_postmountsrv (sexp ctx, sexp ls, sexp name, sexp mtpt, sexp flags) { sexp sexp_postmountsrv (sexp ctx sexp_api_params(self, n), sexp ls, sexp name, sexp mtpt, sexp flags) {
Srv s; Srv s;
struct sexp_plan9_srv p9s; struct sexp_plan9_srv p9s;
if (! sexp_listp(ctx, ls)) if (! sexp_listp(ctx, ls))
@ -316,35 +316,35 @@ sexp sexp_postmountsrv (sexp ctx, sexp ls, sexp name, sexp mtpt, sexp flags) {
return SEXP_UNDEF; return SEXP_UNDEF;
} }
sexp sexp_9p_req_offset (sexp ctx, sexp req) { sexp sexp_9p_req_offset (sexp ctx sexp_api_params(self, n), sexp req) {
return sexp_make_integer(ctx, ((Req*)sexp_cpointer_value(req))->ifcall.offset); return sexp_make_integer(ctx, ((Req*)sexp_cpointer_value(req))->ifcall.offset);
} }
sexp sexp_9p_req_count (sexp ctx, sexp req) { sexp sexp_9p_req_count (sexp ctx sexp_api_params(self, n), sexp req) {
return sexp_make_integer(ctx, ((Req*)sexp_cpointer_value(req))->ifcall.count); return sexp_make_integer(ctx, ((Req*)sexp_cpointer_value(req))->ifcall.count);
} }
#if 0 #if 0
sexp sexp_9p_req_path (sexp ctx, sexp req) { sexp sexp_9p_req_path (sexp ctx sexp_api_params(self, n), sexp req) {
return sexp_c_string(ctx, ((Req*)sexp_cpointer_value(req))->fid->qid.path, -1); return sexp_c_string(ctx, ((Req*)sexp_cpointer_value(req))->fid->qid.path, -1);
} }
#endif #endif
sexp sexp_9p_req_fid (sexp ctx, sexp req) { sexp sexp_9p_req_fid (sexp ctx sexp_api_params(self, n), sexp req) {
return sexp_make_cpointer(ctx, SEXP_CPOINTER, ((Req*)sexp_cpointer_value(req))->fid, SEXP_FALSE, 0); return sexp_make_cpointer(ctx, SEXP_CPOINTER, ((Req*)sexp_cpointer_value(req))->fid, SEXP_FALSE, 0);
} }
sexp sexp_9p_req_newfid (sexp ctx, sexp req) { sexp sexp_9p_req_newfid (sexp ctx sexp_api_params(self, n), sexp req) {
return sexp_make_cpointer(ctx, SEXP_CPOINTER, ((Req*)sexp_cpointer_value(req))->newfid, SEXP_FALSE, 0); return sexp_make_cpointer(ctx, SEXP_CPOINTER, ((Req*)sexp_cpointer_value(req))->newfid, SEXP_FALSE, 0);
} }
sexp sexp_9p_respond (sexp ctx, sexp req, sexp err) { sexp sexp_9p_respond (sexp ctx sexp_api_params(self, n), sexp req, sexp err) {
char *cerr = sexp_stringp(err) ? sexp_string_data(err) : nil; char *cerr = sexp_stringp(err) ? sexp_string_data(err) : nil;
respond(sexp_cpointer_value(req), cerr); respond(sexp_cpointer_value(req), cerr);
return SEXP_VOID; return SEXP_VOID;
} }
sexp sexp_9p_responderror (sexp ctx, sexp req) { sexp sexp_9p_responderror (sexp ctx sexp_api_params(self, n), sexp req) {
responderror(sexp_cpointer_value(req)); responderror(sexp_cpointer_value(req));
return SEXP_VOID; return SEXP_VOID;
} }