=================================================================== RCS file: /home/cvs/OpenXM/src/oxc/sm_ext.c,v retrieving revision 1.9 retrieving revision 1.12 diff -u -p -r1.9 -r1.12 --- OpenXM/src/oxc/sm_ext.c 2000/12/14 01:35:58 1.9 +++ OpenXM/src/oxc/sm_ext.c 2016/04/02 15:01:46 1.12 @@ -1,5 +1,5 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.8 2000/12/03 14:32:40 ohara Exp $ */ +/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.11 2016/04/01 18:12:39 ohara Exp $ */ #include #include @@ -11,19 +11,24 @@ #include "sm.h" static int sm_control_spawn(); -static void sm_control_terminate(); -static void sm_control_kill(); -static void sm_control_reset_pid(); +static int sm_control_terminate(); +static int sm_control_kill(); +static int sm_control_reset_pid(); static void pid_extend(); static int pid_lookup(pid_t pid); -static int pid_registed(pid_t pid); -static void pid_regist(pid_t pid); +static int pid_registered(pid_t pid); +static void pid_register(pid_t pid); static void pid_delete(pid_t pid); static int pid_reset(pid_t pid); static int pid_kill(pid_t pid); static void pid_kill_all(); +#define IS_CMO_INT32(c) ((c) && ((c)->tag == CMO_INT32)) +#define IS_CMO_STRING(c) ((c) && ((c)->tag == CMO_STRING)) +#define IS_CMO_LIST(c) ((c) && ((c)->tag == CMO_LIST)) +#define IS_CMO_LIST_LEN(c,n) (IS_CMO_LIST(c) && (list_length((c)) >= (n))) + /* ultra loose data base. */ static struct { int (*func_ptr)(); char *key; } tbl_lfunc[] = { {lf_oxc_open, "spawn"}, @@ -41,7 +46,7 @@ static struct { int (*func_ptr)(); int key; } tbl_smcm {sm_pops, SM_pops}, {sm_control_reset_pid, SM_control_reset_connection_server}, {sm_control_kill, SM_control_kill}, - {NULL, NULL} + {NULL, 0} }; extern OXFILE *stack_oxfp; @@ -49,7 +54,7 @@ extern OXFILE *stack_oxfp; int (*sm_search_f(int code))() { int i; - for (i=0; tbl_smcmd[i].key != NULL; i++) { + for (i=0; tbl_smcmd[i].key != 0; i++) { if (code == tbl_smcmd[i].key) { return tbl_smcmd[i].func_ptr; } @@ -73,7 +78,7 @@ Normally local functions push a return value to the st but, if error occurs, then these return non-positive numbers and the sm_executeFunction push an error object. */ -void sm_executeFunction() +int sm_executeFunction() { int (*func)(); int retcode = 0; @@ -82,11 +87,12 @@ void sm_executeFunction() func = lookup_localfunction(((cmo_string *)ob)->s); if (func != NULL) { if ((retcode = func()) > 0) { - return; + return 0; } } } push_error(retcode, ob); + return 0; } /* getargs() set number of popped objects to argc. */ @@ -98,7 +104,7 @@ static int getargs(cmo ***args) cmo_int32 *m = (cmo_int32 *)pop(); if (m->tag != CMO_INT32 || (argc = m->i) < 0) { - fprintf(stderr, "oxc: invalid arguments\n"); + ox_printf("oxc: invalid arguments\n"); }else { argv = (cmo **)malloc(sizeof(cmo *)*argc); for(i=0; i= pid_size) { pids_extend(); @@ -162,7 +168,7 @@ static void pid_delete(pid_t pid) static int pid_reset(pid_t pid) { - if (pid_registed(pid)) { + if (pid_registered(pid)) { kill(pid, SIGUSR1); return 1; } @@ -171,7 +177,7 @@ static int pid_reset(pid_t pid) static int pid_kill(pid_t pid) { - if (pid_registed(pid)) { + if (pid_registered(pid)) { kill(pid, SIGKILL); pid_delete(pid); return 1; @@ -198,87 +204,96 @@ cmo_error2 *type_checker(cmo *ob, int type) int lf_oxc_open() { - cmo_int32 *argc = pop(); + cmo_int32 *argc = (cmo_int32 *)pop(); if (argc->tag == CMO_INT32 && argc->i == 1) { return sm_control_spawn(); } - push_error(-1, argc); + push_error(-1, (cmo *)argc); return -1; } -int sm_control_spawn_typecheck(cmo_list *args, cmo_list *ports, cmo_int32 *port, cmo_string *sname) +int sm_control_spawn_typecheck(cmo_list *args, int *portp, char **s) { - char *cmd = sname->s; + cmo_int32 *port; + cmo_string *name; - return args->tag == CMO_LIST - && list_length(args) > 1 - && ports->tag == CMO_LIST - && list_length(ports) > 0 - && port->tag == CMO_INT32 - && sname->tag == CMO_STRING - && cmd != NULL - && which(cmd, getenv("PATH")) != NULL; + if( IS_CMO_LIST_LEN(args,2) ) { + name = (cmo_string *)list_nth(args, 1); + args = (cmo_list *)list_first_cmo(args); + if( IS_CMO_STRING(name) && IS_CMO_LIST_LEN(args,1) ) { + port = (cmo_int32 *)list_first_cmo(args); + if( IS_CMO_INT32(port) && name->s && which(name->s, getenv("PATH")) != NULL) { + *portp = port->i; + *s = name->s; + return 1; + } + } + } + return 0; } static int sm_control_spawn() { - cmo_list *args = pop(); - cmo_list *ports = list_first_cmo(args); - cmo_int32 *port = list_first_cmo(ports); - cmo_string *sname = list_nth(args, 1); - pid_t pid; + int port; + char *s; + pid_t pid; + cmo *args = pop(); - if (sm_control_spawn_typecheck(args, ports, port, sname)) { - pid = lf_oxc_open_main(sname->s, port->i); - if (pid > 0) { - push(new_cmo_int32(pid)); - pid_regist(pid); - fprintf(stderr, "oxc: spawns %s\n", sname->s); - return pid; - } - } - push_error(-1, args); - return 0; - + if (sm_control_spawn_typecheck((cmo_list *)args, &port, &s)) { + pid = lf_oxc_open_main(s, (short)port); + if (pid > 0) { + push((cmo *)new_cmo_int32(pid)); + pid_register(pid); + ox_printf("oxc: spawns %s\n", s); + return pid; + } + } + push_error(-1, (cmo *)args); + return 0; } -void sm_mathcap() +int sm_mathcap() { push((cmo *)oxf_cmo_mathcap(stack_oxfp)); + return 0; } -void sm_set_mathcap() +int sm_set_mathcap() { cmo_mathcap *m = (cmo_mathcap *)pop(); if (m->tag == CMO_MATHCAP) { oxf_mathcap_update(stack_oxfp, m); }else { - push_error(-1, m); + push_error(-1, (cmo *)m); /* an error object must be pushed */ } + return 0; } -static void sm_control_kill() +static int sm_control_kill() { pid_kill_all(); + return 0; } -static void sm_control_terminate() +static int sm_control_terminate() { cmo_int32 *m = (cmo_int32 *)pop(); pid_t pid = m->i; if (m->tag != CMO_INT32 || !pid_kill(pid)) { - push_error(-1, m); + push_error(-1, (cmo *)m); } + return 0; } -static void sm_control_reset_pid() +static int sm_control_reset_pid() { cmo_int32 *m = (cmo_int32 *)pop(); pid_t pid = m->i; if (m->tag != CMO_INT32 || !pid_reset(pid)) { - push_error(-1, m); - return; + push_error(-1, (cmo *)m); + return 0; } /* ... */ + return 0; }