version 1.3, 2000/11/18 05:49:18 |
version 1.12, 2005/07/26 12:52:05 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.2 2000/10/12 15:40:41 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.11 2005/03/04 06:29:46 ohara Exp $ */ |
|
|
/* This module includes functions for handling mathcap databases. */ |
/* This module includes functions for handling mathcap databases. */ |
|
|
|
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <string.h> |
#include "ox_toolkit.h" |
#include "ox_toolkit.h" |
|
|
/* 送信許可されている cmo, sm のリストを得る */ |
#define MATHCAP_1ST_FORMAT "(CMO_LIST (CMO_INT32 %d) (CMO_STRING \"%s\") (CMO_STRING \"%s\") (CMO_STRING \"%s\"))" |
#define smdb_get_allow_all(x) mcdb_get_allow_all((x)->sm_db) |
|
#define cmodb_get_allow_all(x) mcdb_get_allow_all((x)->cmo_db) |
|
|
|
/* 全ての種類の cmo の送信を許可/不許可にする */ |
static int default_cmd[] = { |
#define cmodb_deny_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_DENY) |
SM_popCMO, |
#define cmodb_allow_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_ALLOW) |
SM_popString, |
|
SM_mathcap, |
typedef struct { |
SM_pops, |
int tag; |
SM_executeStringByLocalParser, |
int flag; |
SM_executeFunction, |
} mcdb; |
SM_control_kill, |
|
SM_control_reset_connection, |
typedef struct { |
0 }; |
mcdb *cmo_db; |
static int default_cmo[] = { |
mcdb *sm_db; |
|
} mathcap; |
|
|
|
static char *new_string(char *s); |
|
static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc); |
|
static cmo_list *get_messagetypes(cmo_list *ob, int type); |
|
static cmo_list *mcdb_get_allow_all(mcdb *db); |
|
static cmo_list *sysinfo_get_all(); |
|
static int cmodb_isallow_cmo(mcdb *cmo_db, cmo *ob); |
|
static int cmodb_isallow_cmo_list(mcdb *cmo_db, cmo_list *ob); |
|
static int cmodb_isallow_cmo_mathcap(mcdb *cmo_db, cmo_mathcap *ob); |
|
static int cmodb_isallow_cmo_monomial32(mcdb *cmo_db, cmo_monomial32 *ob); |
|
static int cmodb_isallow_tag(mcdb *cmo_db, int tag); |
|
static mcdb *mcdb_lookup(mcdb *db, int tag); |
|
static mcdb *new_mcdb(int *src); |
|
static void cmodb_allow(mcdb *cmo_db, int tag); |
|
static void cmodb_deny(mcdb *cmo_db, int tag); |
|
static void cmodb_update(mcdb *cmo_db, cmo_list* types); |
|
static void mcdb_ctl(mcdb *db, int tag, int flag); |
|
static void mcdb_ctl_all(mcdb *db, int flag); |
|
static void mcdb_init(mcdb *m, int key); |
|
void sysinfo_set(int version, char *version_string, char *sysname); |
|
|
|
static int cmodb_d[] = { |
|
CMO_NULL, |
CMO_NULL, |
CMO_INT32, |
CMO_INT32, |
CMO_DATUM, |
|
CMO_STRING, |
CMO_STRING, |
CMO_MATHCAP, |
CMO_MATHCAP, |
CMO_LIST, |
CMO_LIST, |
CMO_MONOMIAL32, |
CMO_MONOMIAL32, |
CMO_ZZ, |
CMO_ZZ, |
|
CMO_QQ, |
CMO_ZERO, |
CMO_ZERO, |
CMO_DMS_GENERIC, |
CMO_DMS_GENERIC, |
CMO_RING_BY_NAME, |
CMO_RING_BY_NAME, |
CMO_INDETERMINATE, |
CMO_RECURSIVE_POLYNOMIAL, |
CMO_DISTRIBUTED_POLYNOMIAL, |
CMO_DISTRIBUTED_POLYNOMIAL, |
|
CMO_POLYNOMIAL_IN_ONE_VARIABLE, |
|
CMO_64BIT_MACHINE_DOUBLE, |
|
CMO_IEEE_DOUBLE_FLOAT, |
|
CMO_INDETERMINATE, |
|
CMO_TREE, |
|
CMO_LAMBDA, |
CMO_ERROR2, |
CMO_ERROR2, |
0, |
0 }; |
}; |
static int default_oxtag[] = { OX_DATA, 0 }; |
|
|
static int smdb_d[] = { |
|
SM_popCMO, |
|
SM_popString, |
|
SM_mathcap, |
|
SM_pops, |
|
SM_executeStringByLocalParser, |
|
SM_executeFunction, |
|
SM_setMathCap, |
|
SM_shutdown, |
|
SM_control_kill, |
|
SM_control_reset_connection, |
|
0, |
|
}; |
|
|
|
__inline__ |
|
static void mcdb_init(mcdb *m, int key) |
|
{ |
|
m->tag = key; |
|
m->flag = MATHCAP_FLAG_ALLOW; |
|
} |
|
|
|
static mcdb *new_mcdb(int *src) |
|
{ |
|
mcdb *new; |
|
int len=0; |
|
int i; |
|
while (src[len++] != 0) { |
|
} |
|
new = malloc(sizeof(mcdb)*len); |
|
for(i=0; i<len; i++) { |
|
mcdb_init(new+i, src[i]); |
|
} |
|
return new; |
|
} |
|
|
|
mathcap *new_mathcap_d() |
|
{ |
|
mathcap *new = malloc(sizeof(mathcap)); |
|
new->cmo_db = new_mcdb(cmodb_d); |
|
new->sm_db = new_mcdb(smdb_d); |
|
return new; |
|
} |
|
|
|
/* 次の tag についてのキーを探す */ |
|
static mcdb *mcdb_lookup(mcdb *db, int tag) |
|
{ |
|
while (db->tag != 0) { |
|
if (db->tag == tag) { |
|
return db; |
|
} |
|
db++; |
|
} |
|
return NULL; |
|
} |
|
|
|
/* tag に対する送信制御 */ |
|
static void mcdb_ctl(mcdb *db, int tag, int flag) |
|
{ |
|
mcdb *e = mcdb_lookup(db, tag); |
|
if (e != NULL) { |
|
e->flag = flag; |
|
} |
|
} |
|
|
|
/* 全データに対する送信制御 */ |
|
static void mcdb_ctl_all(mcdb *db, int flag) |
|
{ |
|
while (db->tag != 0) { |
|
db->flag = flag; |
|
db++; |
|
} |
|
} |
|
|
|
/* 送信許可されている tag のリストを得る */ |
|
static cmo_list *mcdb_get_allow_all(mcdb *db) |
|
{ |
|
cmo_list *list = new_cmo_list(); |
|
while (db->tag != 0) { |
|
if (db->flag == MATHCAP_FLAG_ALLOW) { |
|
list_append(list, (cmo *)new_cmo_int32(db->tag)); |
|
} |
|
db++; |
|
} |
|
return list; |
|
} |
|
|
|
static struct { |
static struct { |
int version; |
int ox_version; |
char *version_string; |
|
char *sysname; |
char *sysname; |
|
char *version; |
char *hosttype; |
char *hosttype; |
} sysinfo = {0, "NO VERSION", "NONAME", "UNKNOWN"}; |
} sysinfo = {OX_PROTOCOL_VERSION, "ox_toolkit", OX_TOOLKIT_VERSION, "generic"}; |
|
|
/* 次の tag をもつ cmo の送信が許可されているかを調べる */ |
mathcap default_mathcap = {default_cmd, default_cmo}; |
static int cmodb_isallow_tag(mcdb *cmo_db, int tag) |
|
{ |
|
while (cmo_db->tag != 0 && cmo_db->tag != tag) { |
|
cmo_db++; |
|
} |
|
return cmo_db->flag; |
|
} |
|
|
|
static int cmodb_isallow_cmo_list(mcdb *cmo_db, cmo_list *ob) |
static int ilen(int a[]); |
{ |
static int *icopy(int s[]); |
cell *el; |
static int *icopyn(int s[], int n); |
if (cmodb_isallow_tag(cmo_db, ob->tag)) { |
static int cmo_int32_to_int(cmo_int32* m); |
el = list_first(ob); |
static int *imerge(int *base, int *diff); |
while (!list_endof(ob, el)) { |
static mathcap *mathcap_merge_io(mathcap *this, mathcap *diff); |
if (!cmodb_isallow_cmo(cmo_db, el->cmo)) { |
|
return MATHCAP_FLAG_DENY; |
|
} |
|
el = list_next(el); |
|
} |
|
return MATHCAP_FLAG_ALLOW; |
|
} |
|
return MATHCAP_FLAG_DENY; |
|
} |
|
|
|
__inline__ |
mathcap *new_mathcap() |
static int cmodb_isallow_cmo_monomial32(mcdb *cmo_db, cmo_monomial32 *ob) |
|
{ |
{ |
return cmodb_isallow_tag(cmo_db, ob->tag) |
mathcap *mcap = MALLOC(sizeof(mathcap)); |
&& cmodb_isallow_cmo(cmo_db, ob->coef); |
mcap->cmo = icopy(default_cmo); |
|
mcap->cmd = icopy(default_cmd); |
|
return mcap; |
} |
} |
|
|
__inline__ |
mathcap *new_mathcap_set(int *cmd, int *cmo) |
static int cmodb_isallow_cmo_mathcap(mcdb *cmo_db, cmo_mathcap *ob) |
|
{ |
{ |
return cmodb_isallow_tag(cmo_db, ob->tag) |
mathcap *mcap = MALLOC(sizeof(mathcap)); |
&& cmodb_isallow_cmo(cmo_db, ob->ob); |
mcap->cmd = (cmd)? cmd: icopy(default_cmd); |
|
mcap->cmo = (cmo)? cmo: icopy(default_cmo); |
|
return mcap; |
} |
} |
|
|
/* 次の cmo の送信が許可されているかを調べる */ |
cmo_list *cmo_mathcap_1st() |
static int cmodb_isallow_cmo(mcdb *cmo_db, cmo *ob) |
|
{ |
{ |
int tag = ob->tag; |
char buffer[BUFSIZ]; |
switch(tag) { |
static char format[] = MATHCAP_1ST_FORMAT; |
case CMO_LIST: |
int len = sizeof(format) + 32 |
case CMO_DISTRIBUTED_POLYNOMIAL: |
+ strlen(sysinfo.sysname) |
return cmodb_isallow_cmo_list(cmo_db, (cmo_list *)ob); |
+ strlen(sysinfo.version) |
case CMO_MATHCAP: |
+ strlen(sysinfo.hosttype); |
case CMO_ERROR2: |
if (len < BUFSIZ) { |
case CMO_RING_BY_NAME: |
sprintf(buffer, format, sysinfo.ox_version, sysinfo.sysname, |
case CMO_INDETERMINATE: |
sysinfo.version, sysinfo.hosttype); |
return cmodb_isallow_cmo_mathcap(cmo_db, (cmo_mathcap *)ob); |
return (cmo_list *)ox_parse_lisp(buffer); |
case CMO_MONOMIAL32: |
|
return cmodb_isallow_cmo_monomial32(cmo_db, (cmo_monomial32 *)ob); |
|
default: |
|
return cmodb_isallow_tag(cmo_db, tag); |
|
} |
} |
|
return NULL; |
} |
} |
|
|
/* 次の tag をもつ cmo の送信を許可する */ |
/* 0: terminator of array of integer. */ |
__inline__ |
static int ilen(int a[]) |
static void cmodb_allow(mcdb *cmo_db, int tag) |
|
{ |
{ |
mcdb_ctl(cmo_db, tag, MATHCAP_FLAG_ALLOW); |
int i=0; |
|
if (a != NULL) { |
|
for( ; a[i] !=0; i++) { |
|
} |
|
} |
|
return i; |
} |
} |
|
|
/* 次の tag をもつ cmo の送信を不許可にする */ |
static int *icopy(int s[]) |
__inline__ |
|
static void cmodb_deny(mcdb *cmo_db, int tag) |
|
{ |
{ |
mcdb_ctl(cmo_db, tag, MATHCAP_FLAG_DENY); |
int n = sizeof(int)*(ilen(s)+1); |
|
int *d = MALLOC(n); |
|
memcpy(d,s,n); |
|
return d; |
} |
} |
|
|
/* システム情報を得る */ |
static int *icopyn(int s[], int n) |
static cmo_list *sysinfo_get_all() |
|
{ |
{ |
cmo_list *syslist = new_cmo_list(); |
int *d = MALLOC((n = sizeof(int)*(n+1))); |
cmo_int32 *ver = new_cmo_int32(sysinfo.version); |
memcpy(d,s,n); |
cmo_string *vers = new_cmo_string(sysinfo.version_string); |
return d; |
cmo_string *host = new_cmo_string(sysinfo.hosttype); |
|
cmo_string *sname = new_cmo_string(sysinfo.sysname); |
|
return list_appendl(syslist, ver, sname, vers, host, NULL); |
|
} |
} |
|
|
__inline__ |
cmo_mathcap *new_cmo_mathcap_by_mathcap(mathcap *mcap) |
static char *new_string(char *s) |
|
{ |
{ |
char *t = malloc(sizeof(s)+1); |
cmo_list *cap1, *cap2, *cap3, *cap4; |
strcpy(t, s); |
cap1 = cmo_mathcap_1st(); |
return t; |
cap2 = new_cmo_list_map(mcap->cmd, ilen(mcap->cmd), new_cmo_int32); |
|
cap3 = new_cmo_list_map(default_oxtag, |
|
ilen(default_oxtag), |
|
new_cmo_int32); |
|
cap4 = new_cmo_list_map(mcap->cmo, ilen(mcap->cmo), new_cmo_int32); |
|
/* new_cmo_mathcap([cap1, cap2, [cap3, cap4]]) */ |
|
return new_cmo_mathcap( |
|
(cmo *)list_appendl(NULL, cap1, cap2, list_appendl(NULL, cap3, cap4, NULL))); |
} |
} |
|
|
void sysinfo_set(int version, char *vstr, char *sysname) |
static int cmo_int32_to_int(cmo_int32* m) |
{ |
{ |
char *host = getenv("HOSTTYPE"); |
return m->i; |
sysinfo.hosttype = (host != NULL)? new_string(host): "UNKNOWN"; |
|
sysinfo.sysname = new_string(sysname); |
|
sysinfo.version_string = new_string(vstr); |
|
sysinfo.version = version; |
|
} |
} |
|
|
void mathcap_global_init(int ver, char *vstr, char *sysname, int cmos[], int sms[]) |
mathcap *new_mathcap_by_cmo_mathcap(cmo_mathcap *cap) |
{ |
{ |
sysinfo_set(ver, vstr, sysname); /* global! */ |
int *cmd = list_to_array_map(list_nth(cap->ob, 1), cmo_int32_to_int); |
if (cmos == NULL) { |
int *cmo = list_to_array_map(list_nth(list_nth(cap->ob, 2), 1), |
cmos = cmodb_d; |
cmo_int32_to_int); |
} |
return new_mathcap_set(cmd, cmo); |
if (sms == NULL) { |
|
sms = smdb_d; |
|
} |
|
/* ... */ |
|
} |
} |
|
|
/* データベースから cmo_mathcap を生成する */ |
/* if base is unsorted. */ |
cmo_mathcap* mathcap_get(mathcap *this) |
static int *imerge(int *base, int *diff) |
{ |
{ |
cmo_list *mc = new_cmo_list(); |
int i,j,k; |
cmo_list *l3 = new_cmo_list(); |
int n = ilen(base); |
list_append(l3, list_appendl(new_cmo_list(), |
int m = ilen(diff); |
new_cmo_int32(OX_DATA), |
int *t = ALLOCA(sizeof(int)*(n+1)); |
cmodb_get_allow_all(this), NULL)); |
int *ret; |
list_appendl(mc, (cmo *)sysinfo_get_all(), |
for(i=0,j=0; i<n; i++) { |
(cmo *)smdb_get_allow_all(this), (cmo *)l3, NULL); |
for(k=0; k<m; k++) { |
return new_cmo_mathcap((cmo *)mc); |
if (base[i] == diff[k]) { |
} |
t[j++] = base[i]; |
|
break; |
static void cmodb_update(mcdb *cmo_db, cmo_list* types) |
} |
{ |
|
cell *el = list_first(types); |
|
cmo_int32 *ob; |
|
while(!list_endof(types, el)) { |
|
ob = (cmo_int32 *)el->cmo; |
|
if (ob->tag == CMO_INT32) { |
|
cmodb_allow(cmo_db, ob->i); |
|
} |
} |
el = list_next(el); |
|
} |
} |
|
t[j] = 0; |
|
ret = icopyn(t,j); |
|
return ret; |
} |
} |
|
|
/* cmo_mathcap が妥当な構造を持つかを調べる. (未実装) */ |
static mathcap *mathcap_merge_io(mathcap *this, mathcap *diff) |
int cmo_mathcap_isvalid(cmo_mathcap *mc) |
|
{ |
{ |
/* print_cmo(mc); */ |
int *tmp; |
return 1; |
tmp = imerge(this->cmo, diff->cmo); |
|
FREE(this->cmo); |
|
this->cmo = tmp; |
|
return this; |
} |
} |
|
|
/* ( ..., ( type, (...) ), (cmo_int32, (...) ), ... ) */ |
/* for compatibility */ |
/* ^^^^^ Here! */ |
void mathcap_init(char *version, char *sysname) |
static cmo_list *get_messagetypes(cmo_list *ob, int type) |
|
{ |
{ |
cmo_list *c; |
sysinfo.hosttype = getenv("HOSTTYPE"); |
cell *el; |
sysinfo.version = version; |
|
sysinfo.sysname = sysname; |
for (el = list_first(ob); !list_endof(ob, el); el = list_next(el)) { |
|
c = (cmo_list *)el->cmo; |
|
if (((cmo_int32 *)list_nth(c, 0))->i == type) { |
|
return (cmo_list *)list_nth(c, 1); |
|
} |
|
} |
|
return NULL; |
|
} |
} |
|
cmo_mathcap* mathcap_get(mathcap *this) |
/* cmo_mathcap->ob = ( (...), (...), ( ( cmo_int32, (...) ), ...), ...) */ |
|
/* ^^^^^ Here! */ |
|
__inline__ |
|
static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc) |
|
{ |
{ |
cmo_list *ob = (cmo_list *)list_nth((cmo_list *)mc->ob, 2); |
return new_cmo_mathcap_by_mathcap(this); |
return get_messagetypes(ob, OX_DATA); |
|
} |
} |
|
mathcap *mathcap_update(mathcap *this, cmo_mathcap *m) |
/* 受信した mathcap データを反映させる */ |
|
/* this == NULL のとき、はじめて mathcap* オブジェクトをせいせいする */ |
|
mathcap *mathcap_update(mathcap *this, cmo_mathcap *mc) |
|
{ |
{ |
cmo_list *types; |
return mathcap_merge_io(this, new_mathcap_by_cmo_mathcap(m)); |
if (cmo_mathcap_isvalid(mc)) { |
|
types = cmo_mathcap_get_cmotypes(mc); |
|
if (types != NULL) { |
|
cmodb_deny_all(this->cmo_db); /* すべての cmo の送信を禁止 */ |
|
cmodb_update(this->cmo_db, types); |
|
} |
|
} |
|
return this; |
|
} |
} |