=================================================================== RCS file: /home/cvs/OpenXM/src/ox_toolkit/mathcap.c,v retrieving revision 1.4 retrieving revision 1.12.2.1 diff -u -p -r1.4 -r1.12.2.1 --- OpenXM/src/ox_toolkit/mathcap.c 2000/11/21 07:59:08 1.4 +++ OpenXM/src/ox_toolkit/mathcap.c 2005/10/12 03:49:17 1.12.2.1 @@ -1,344 +1,193 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.3 2000/11/18 05:49:18 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.12 2005/07/26 12:52:05 ohara Exp $ */ /* This module includes functions for handling mathcap databases. */ +/* This version will be in the new-mathcap-branch. */ +#include #include +#include #include "ox_toolkit.h" -/* 送信許可されている cmo, sm のリストを得る */ -#define smdb_get_allow_all(x) mcdb_get_allow_all((x)) -#define cmodb_get_allow_all(x) mcdb_get_allow_all((x)) +#define MATHCAP_1ST_FORMAT "(CMO_LIST (CMO_INT32 %d) (CMO_STRING \"%s\") (CMO_STRING \"%s\") (CMO_STRING \"%s\"))" -/* 全ての種類の cmo の送信を許可/不許可にする */ -#define cmodb_deny_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_DENY) -#define cmodb_allow_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_ALLOW) - -typedef struct { - int tag; - int flag; -} mcdb; - -typedef struct { - mcdb *cmodb; - mcdb *smdb; -} 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 *cmodb, cmo *ob); -static int cmodb_isallow_cmo_list(mcdb *cmodb, cmo_list *ob); -static int cmodb_isallow_cmo_mathcap(mcdb *cmodb, cmo_mathcap *ob); -static int cmodb_isallow_cmo_monomial32(mcdb *cmodb, cmo_monomial32 *ob); -static int cmodb_isallow_tag(mcdb *cmodb, int tag); -static mcdb *mcdb_lookup(mcdb *db, int tag); -static mcdb *new_mcdb(int *src); -static void cmodb_allow(mcdb *cmodb, int tag); -static void cmodb_deny(mcdb *cmodb, int tag); -static void cmodb_update(mcdb *cmodb, 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); -static void sysinfo_set(int version, char *version_string, char *sysname); - -static int cmodb_d[] = { +static int default_cmd[] = { + SM_popCMO, + SM_popString, + SM_mathcap, + SM_pops, + SM_executeStringByLocalParser, + SM_executeFunction, + SM_control_kill, + SM_control_reset_connection, + 0 }; +static int default_cmo[] = { CMO_NULL, CMO_INT32, - CMO_DATUM, CMO_STRING, CMO_MATHCAP, CMO_LIST, CMO_MONOMIAL32, CMO_ZZ, + CMO_QQ, CMO_ZERO, CMO_DMS_GENERIC, CMO_RING_BY_NAME, - CMO_INDETERMINATE, + CMO_RECURSIVE_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, - 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; icmodb = new_mcdb(cmodb_d); - new->smdb = 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 { - int version; - char *version_string; + int ox_version; char *sysname; + char *version; char *hosttype; - int *cmo_tags; - int *sm_cmds; -} sysinfo = {0, "NO VERSION", "NONAME", "UNKNOWN", cmodb_d, smdb_d}; +} sysinfo = {OX_PROTOCOL_VERSION, "ox_toolkit", OX_TOOLKIT_VERSION, "generic"}; -/* 次の tag をもつ cmo の送信が許可されているかを調べる */ -static int cmodb_isallow_tag(mcdb *cmodb, int tag) -{ - while (cmodb->tag != 0 && cmodb->tag != tag) { - cmodb++; - } - return cmodb->flag; -} +mathcap default_mathcap = {default_cmd, default_cmo}; -static int cmodb_isallow_cmo_list(mcdb *cmodb, cmo_list *ob) -{ - cell *el; - if (cmodb_isallow_tag(cmodb, ob->tag)) { - el = list_first(ob); - while (!list_endof(ob, el)) { - if (!cmodb_isallow_cmo(cmodb, el->cmo)) { - return MATHCAP_FLAG_DENY; - } - el = list_next(el); - } - return MATHCAP_FLAG_ALLOW; - } - return MATHCAP_FLAG_DENY; -} +static int ilen(int a[]); +static int *icopy(int s[]); +static int *icopyn(int s[], int n); +static int cmo_int32_to_int(cmo_int32* m); +static int *imerge(int *base, int *diff); +static mathcap *mathcap_merge_io(mathcap *this, mathcap *diff); -__inline__ -static int cmodb_isallow_cmo_monomial32(mcdb *cmodb, cmo_monomial32 *ob) +mathcap *new_mathcap() { - return cmodb_isallow_tag(cmodb, ob->tag) - && cmodb_isallow_cmo(cmodb, ob->coef); + mathcap *mcap = MALLOC(sizeof(mathcap)); + mcap->cmo = icopy(default_cmo); + mcap->cmd = icopy(default_cmd); + return mcap; } -__inline__ -static int cmodb_isallow_cmo_mathcap(mcdb *cmodb, cmo_mathcap *ob) +mathcap *new_mathcap_set(int *cmd, int *cmo) { - return cmodb_isallow_tag(cmodb, ob->tag) - && cmodb_isallow_cmo(cmodb, ob->ob); + mathcap *mcap = MALLOC(sizeof(mathcap)); + mcap->cmd = (cmd)? cmd: icopy(default_cmd); + mcap->cmo = (cmo)? cmo: icopy(default_cmo); + return mcap; } -/* 次の cmo の送信が許可されているかを調べる */ -static int cmodb_isallow_cmo(mcdb *cmodb, cmo *ob) +cmo_list *cmo_mathcap_1st() { - int tag = ob->tag; - switch(tag) { - case CMO_LIST: - case CMO_DISTRIBUTED_POLYNOMIAL: - return cmodb_isallow_cmo_list(cmodb, (cmo_list *)ob); - case CMO_MATHCAP: - case CMO_ERROR2: - case CMO_RING_BY_NAME: - case CMO_INDETERMINATE: - return cmodb_isallow_cmo_mathcap(cmodb, (cmo_mathcap *)ob); - case CMO_MONOMIAL32: - return cmodb_isallow_cmo_monomial32(cmodb, (cmo_monomial32 *)ob); - default: - return cmodb_isallow_tag(cmodb, tag); + char buffer[BUFSIZ]; + static char format[] = MATHCAP_1ST_FORMAT; + int len = sizeof(format) + 32 + + strlen(sysinfo.sysname) + + strlen(sysinfo.version) + + strlen(sysinfo.hosttype); + if (len < BUFSIZ) { + sprintf(buffer, format, sysinfo.ox_version, sysinfo.sysname, + sysinfo.version, sysinfo.hosttype); + return (cmo_list *)ox_parse_lisp(buffer); } + return NULL; } -/* 次の tag をもつ cmo の送信を許可する */ -__inline__ -static void cmodb_allow(mcdb *cmodb, int tag) +/* 0: terminator of array of integer. */ +static int ilen(int a[]) { - mcdb_ctl(cmodb, tag, MATHCAP_FLAG_ALLOW); + int i=0; + if (a != NULL) { + for( ; a[i] !=0; i++) { + } + } + return i; } -/* 次の tag をもつ cmo の送信を不許可にする */ -__inline__ -static void cmodb_deny(mcdb *cmodb, int tag) +static int *icopy(int s[]) { - mcdb_ctl(cmodb, tag, MATHCAP_FLAG_DENY); + int n = sizeof(int)*(ilen(s)+1); + int *d = MALLOC(n); + memcpy(d,s,n); + return d; } -/* システム情報を得る */ -static cmo_list *sysinfo_get_all() +static int *icopyn(int s[], int n) { - cmo_list *syslist = new_cmo_list(); - cmo_int32 *ver = new_cmo_int32(sysinfo.version); - cmo_string *vers = new_cmo_string(sysinfo.version_string); - 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); + int *d = MALLOC((n = sizeof(int)*(n+1))); + memcpy(d,s,n); + return d; } -__inline__ -static char *new_string(char *s) +cmo_mathcap *new_cmo_mathcap_by_mathcap(mathcap *mcap) { - char *t = malloc(sizeof(s)+1); - strcpy(t, s); - return t; + cmo_list *cap1, *cap2, *cap3, *cap4; + cap1 = cmo_mathcap_1st(); + 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"); - sysinfo.hosttype = (host != NULL)? new_string(host): "UNKNOWN"; - sysinfo.sysname = new_string(sysname); - sysinfo.version_string = new_string(vstr); - sysinfo.version = version; + return m->i; } -void mathcap_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! */ - if (cmos == NULL) { - cmos = cmodb_d; - } - if (sms == NULL) { - sms = smdb_d; - } - /* ... */ + int *cmd = list_to_array_map(list_nth(cap->ob, 1), cmo_int32_to_int); + int *cmo = list_to_array_map(list_nth(list_nth(cap->ob, 2), 1), + cmo_int32_to_int); + return new_mathcap_set(cmd, cmo); } -/* データベースから cmo_mathcap を生成する */ -cmo_mathcap* mathcap_get(mathcap *this) +/* if base is unsorted. */ +static int *imerge(int *base, int *diff) { - cmo_list *mc = new_cmo_list(); - cmo_list *l3 = new_cmo_list(); - list_append(l3, list_appendl(new_cmo_list(), - new_cmo_int32(OX_DATA), - cmodb_get_allow_all(this->cmodb), NULL)); - list_appendl(mc, (cmo *)sysinfo_get_all(), - (cmo *)smdb_get_allow_all(this->smdb), (cmo *)l3, NULL); - return new_cmo_mathcap((cmo *)mc); -} - -static void cmodb_update(mcdb *cmodb, 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(cmodb, ob->i); + int i,j,k; + int n = ilen(base); + int m = ilen(diff); + int *t = ALLOCA(sizeof(int)*(n+1)); + int *ret; + for(i=0,j=0; icmo, diff->cmo); + FREE(this->cmo); + this->cmo = tmp; + return this; } -/* ( ..., ( type, (...) ), (cmo_int32, (...) ), ... ) */ -/* ^^^^^ Here! */ -static cmo_list *get_messagetypes(cmo_list *ob, int type) +/* for compatibility */ +void mathcap_init(char *version, char *sysname) { - cmo_list *c; - cell *el; - - 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; + sysinfo.hosttype = getenv("HOSTTYPE"); + sysinfo.version = version; + sysinfo.sysname = sysname; } - -/* cmo_mathcap->ob = ( (...), (...), ( ( cmo_int32, (...) ), ...), ...) */ -/* ^^^^^ Here! */ -__inline__ -static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc) +cmo_mathcap* mathcap_get(mathcap *this) { - cmo_list *ob = (cmo_list *)list_nth((cmo_list *)mc->ob, 2); - return get_messagetypes(ob, OX_DATA); + return new_cmo_mathcap_by_mathcap(this); } - -/* 受信した mathcap データを反映させる */ -/* this == NULL のとき、はじめて mathcap* オブジェクトをせいせいする */ -mathcap *mathcap_update(mathcap *this, cmo_mathcap *mc) +mathcap *mathcap_update(mathcap *this, cmo_mathcap *m) { - cmo_list *types; - if (cmo_mathcap_isvalid(mc)) { - types = cmo_mathcap_get_cmotypes(mc); - if (types != NULL) { - cmodb_deny_all(this->cmodb); /* すべての cmo の送信を禁止 */ - cmodb_update(this->cmodb, types); - } - } - return this; + return mathcap_merge_io(this, new_mathcap_by_cmo_mathcap(m)); }