version 1.6, 2000/11/30 10:27:02 |
version 1.9, 2000/12/14 01:35:58 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.5 2000/11/28 18:11:42 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.8 2000/12/03 14:32:40 ohara Exp $ */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
#include <ox_toolkit.h> |
#include <ox_toolkit.h> |
#include "sm.h" |
#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 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 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(); |
|
|
/* ultra loose data base. */ |
/* ultra loose data base. */ |
static db db_localfunc[] = { |
static struct { int (*func_ptr)(); char *key; } tbl_lfunc[] = { |
{lf_oxc_open, "spawn"}, |
{lf_oxc_open, "spawn"}, |
|
{lf_oxc_open, "oxc_open"}, |
{NULL, NULL} |
{NULL, NULL} |
}; |
}; |
|
|
static db db_sm[] = { |
static struct { int (*func_ptr)(); int key; } tbl_smcmd[] = { |
{sm_control_spawn, SM_control_spawn_server}, |
{sm_control_spawn, SM_control_spawn_server}, |
{sm_control_terminate, SM_control_terminate_server}, |
{sm_control_terminate, SM_control_terminate_server}, |
{sm_executeFunction, SM_executeFunction}, |
{sm_executeFunction, SM_executeFunction}, |
Line 24 static db db_sm[] = { |
|
Line 39 static db db_sm[] = { |
|
{sm_set_mathcap, SM_setMathCap}, |
{sm_set_mathcap, SM_setMathCap}, |
{sm_popCMO, SM_popCMO}, |
{sm_popCMO, SM_popCMO}, |
{sm_pops, SM_pops}, |
{sm_pops, SM_pops}, |
{sm_control_reset_pid, SM_control_reset_connection_pid}, |
{sm_control_reset_pid, SM_control_reset_connection_server}, |
{sm_control_kill, SM_control_kill}, |
{sm_control_kill, SM_control_kill}, |
{NULL, NULL} |
{NULL, NULL} |
}; |
}; |
|
|
extern OXFILE *stack_oxfp; |
extern OXFILE *stack_oxfp; |
|
|
__inline__ |
int (*sm_search_f(int code))() |
static int (*db_search(void *key, db *dbs, int (*cmp)(void *, void *)))() |
|
{ |
{ |
while (dbs->key != NULL) { |
int i; |
if (cmp(key, dbs->key) == 0) { |
for (i=0; tbl_smcmd[i].key != NULL; i++) { |
return dbs->func_ptr; |
if (code == tbl_smcmd[i].key) { |
|
return tbl_smcmd[i].func_ptr; |
} |
} |
dbs++; |
|
} |
} |
return NULL; |
return NULL; |
} |
} |
|
|
static int (*lookup_localfunction(char *name))() |
static int (*lookup_localfunction(char *name))() |
{ |
{ |
return db_search(name, db_localfunc, strcmp); |
int i; |
|
for (i=0; tbl_lfunc[i].key != NULL; i++) { |
|
if (strcmp(name, tbl_lfunc[i].key) == 0) { |
|
return tbl_lfunc[i].func_ptr; |
|
} |
|
} |
|
return NULL; |
} |
} |
|
|
/* |
/* |
Line 108 static void pids_extend() |
|
Line 128 static void pids_extend() |
|
pids = pids2; |
pids = pids2; |
} |
} |
|
|
int pid_lookup(pid_t pid) |
static int pid_lookup(pid_t pid) |
{ |
{ |
int i; |
int i; |
for(i=0; i<pid_ptr; i++) { |
for(i=0; i<pid_ptr; i++) { |
Line 119 int pid_lookup(pid_t pid) |
|
Line 139 int pid_lookup(pid_t pid) |
|
return -1; |
return -1; |
} |
} |
|
|
int pid_registed(pid_t pid) |
static int pid_registed(pid_t pid) |
{ |
{ |
return pid_lookup(pid)+1; |
return pid_lookup(pid)+1; |
} |
} |
|
|
void pid_regist(pid_t pid) |
static void pid_regist(pid_t pid) |
{ |
{ |
if (pid_ptr >= pid_size) { |
if (pid_ptr >= pid_size) { |
pids_extend(); |
pids_extend(); |
} |
} |
pids[pid_ptr++] = pid; |
pids[pid_ptr++] = pid; |
} |
} |
|
|
void pid_delete(pid_t pid) |
static void pid_delete(pid_t pid) |
{ |
{ |
int i = pid_lookup(pid); |
int i = pid_lookup(pid); |
if (i >= 0 && i != --pid_ptr) { |
if (i >= 0 && i != --pid_ptr) { |
Line 140 void pid_delete(pid_t pid) |
|
Line 160 void pid_delete(pid_t pid) |
|
} |
} |
} |
} |
|
|
int pid_reset(pid_t pid) |
static int pid_reset(pid_t pid) |
{ |
{ |
if (pid_registed(pid)) { |
if (pid_registed(pid)) { |
kill(pid, SIGUSR1); |
kill(pid, SIGUSR1); |
return 1; |
return 1; |
} |
} |
return 0; |
return 0; |
} |
} |
|
|
int pid_kill(pid_t pid) |
static int pid_kill(pid_t pid) |
{ |
{ |
if (pid_registed(pid)) { |
if (pid_registed(pid)) { |
kill(pid, SIGKILL); |
kill(pid, SIGKILL); |
pid_delete(pid); |
pid_delete(pid); |
return 1; |
return 1; |
} |
} |
return 0; |
return 0; |
} |
} |
|
|
/* Killing all child processes */ |
/* Killing all child processes */ |
void pid_kill_all() |
static void pid_kill_all() |
{ |
{ |
while(pid_ptr > 0) { |
while(pid_ptr > 0) { |
kill(pids[--pid_ptr], SIGKILL); |
kill(pids[--pid_ptr], SIGKILL); |
} |
} |
} |
} |
|
|
cmo_error2 *type_checker(cmo *ob, int type) |
cmo_error2 *type_checker(cmo *ob, int type) |
{ |
{ |
/* cmo_error2 *err_ob; */ |
/* cmo_error2 *err_ob; */ |
if (ob->tag != type) { |
if (ob->tag != type) { |
/* push and return an error object */ |
/* push and return an error object */ |
} |
} |
return NULL; |
return NULL; |
} |
} |
|
|
int sm_control_spawn_main(int argc, cmo *argv[]) |
int lf_oxc_open() |
{ |
{ |
char *cmd = ((cmo_string *)argv[0])->s; |
cmo_int32 *argc = pop(); |
int port = ((cmo_int32 *)argv[1])->i; |
if (argc->tag == CMO_INT32 && argc->i == 1) { |
pid_t pid = lf_oxc_open_main(cmd, port); |
return sm_control_spawn(); |
if (pid > 0) { |
} |
push(new_cmo_int32(pid)); |
push_error(-1, argc); |
pid_regist(pid); |
return -1; |
} |
|
return pid; |
|
} |
} |
|
|
int lf_oxc_open() |
int sm_control_spawn_typecheck(cmo_list *args, cmo_list *ports, cmo_int32 *port, cmo_string *sname) |
{ |
{ |
cmo **argv; |
char *cmd = sname->s; |
if (getargs(&argv) != 2 || |
|
type_checker(argv[0], CMO_STRING) != NULL |
return args->tag == CMO_LIST |
|| type_checker(argv[0], CMO_INT32) != NULL) { |
&& list_length(args) > 1 |
fprintf(stderr, "oxc: invalid arguments\n"); |
&& ports->tag == CMO_LIST |
return -1; |
&& list_length(ports) > 0 |
} |
&& port->tag == CMO_INT32 |
return sm_control_spawn_main(2, argv); |
&& sname->tag == CMO_STRING |
|
&& cmd != NULL |
|
&& which(cmd, getenv("PATH")) != NULL; |
} |
} |
|
|
int sm_control_spawn() |
static int sm_control_spawn() |
{ |
{ |
cmo *argv[2]; |
cmo_list *args = pop(); |
argv[0] = pop(); |
cmo_list *ports = list_first_cmo(args); |
argv[1] = pop(); |
cmo_int32 *port = list_first_cmo(ports); |
|
cmo_string *sname = list_nth(args, 1); |
|
pid_t pid; |
|
|
if (type_checker(argv[0], CMO_STRING) != NULL |
if (sm_control_spawn_typecheck(args, ports, port, sname)) { |
|| type_checker(argv[0], CMO_INT32) != NULL) { |
pid = lf_oxc_open_main(sname->s, port->i); |
fprintf(stderr, "oxc: invalid arguments\n"); |
if (pid > 0) { |
return -1; |
push(new_cmo_int32(pid)); |
} |
pid_regist(pid); |
return sm_control_spawn_main(2, argv); |
fprintf(stderr, "oxc: spawns %s\n", sname->s); |
|
return pid; |
|
} |
|
} |
|
push_error(-1, args); |
|
return 0; |
|
|
} |
} |
|
|
void sm_mathcap() |
void sm_mathcap() |
Line 230 void sm_set_mathcap() |
|
Line 258 void sm_set_mathcap() |
|
} |
} |
} |
} |
|
|
void sm_control_kill() |
static void sm_control_kill() |
{ |
{ |
pid_kill_all(); |
pid_kill_all(); |
} |
} |
|
|
void sm_control_terminate() |
static void sm_control_terminate() |
{ |
{ |
cmo_int32 *m = (cmo_int32 *)pop(); |
cmo_int32 *m = (cmo_int32 *)pop(); |
pid_t pid = m->i; |
pid_t pid = m->i; |
Line 244 void sm_control_terminate() |
|
Line 272 void sm_control_terminate() |
|
} |
} |
} |
} |
|
|
void sm_control_reset_pid() |
static void sm_control_reset_pid() |
{ |
{ |
cmo_int32 *m = (cmo_int32 *)pop(); |
cmo_int32 *m = (cmo_int32 *)pop(); |
pid_t pid = m->i; |
pid_t pid = m->i; |
if (m->tag != CMO_INT32 || !pid_reset(pid)) { |
if (m->tag != CMO_INT32 || !pid_reset(pid)) { |
push_error(-1, m); |
push_error(-1, m); |
return; |
return; |
} |
} |
/* ... */ |
/* ... */ |
} |
|
|
|
static int intcmp(int key1, int key2) |
|
{ |
|
return key1 != key2; |
|
} |
|
|
|
int (*sm_search_f(int code))() |
|
{ |
|
return db_search(code, db_sm, intcmp); |
|
} |
} |