Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.3
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.3 ! ohara 2: /* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.2 2000/10/12 15:40:41 ohara Exp $ */
1.1 ohara 3:
4: /* This module includes functions for handling mathcap databases. */
5:
6: #include <stdlib.h>
7: #include "ox_toolkit.h"
8:
1.3 ! ohara 9: /* 送信許可されている cmo, sm のリストを得る */
! 10: #define smdb_get_allow_all(x) mcdb_get_allow_all((x)->sm_db)
! 11: #define cmodb_get_allow_all(x) mcdb_get_allow_all((x)->cmo_db)
! 12:
! 13: /* 全ての種類の cmo の送信を許可/不許可にする */
! 14: #define cmodb_deny_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_DENY)
! 15: #define cmodb_allow_all(x) mcdb_ctl_all((x), MATHCAP_FLAG_ALLOW)
! 16:
1.1 ohara 17: typedef struct {
18: int tag;
19: int flag;
1.2 ohara 20: } mcdb;
1.1 ohara 21:
1.3 ! ohara 22: typedef struct {
! 23: mcdb *cmo_db;
! 24: mcdb *sm_db;
! 25: } mathcap;
! 26:
! 27: static char *new_string(char *s);
! 28: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc);
! 29: static cmo_list *get_messagetypes(cmo_list *ob, int type);
! 30: static cmo_list *mcdb_get_allow_all(mcdb *db);
! 31: static cmo_list *sysinfo_get_all();
! 32: static int cmodb_isallow_cmo(mcdb *cmo_db, cmo *ob);
! 33: static int cmodb_isallow_cmo_list(mcdb *cmo_db, cmo_list *ob);
! 34: static int cmodb_isallow_cmo_mathcap(mcdb *cmo_db, cmo_mathcap *ob);
! 35: static int cmodb_isallow_cmo_monomial32(mcdb *cmo_db, cmo_monomial32 *ob);
! 36: static int cmodb_isallow_tag(mcdb *cmo_db, int tag);
1.2 ohara 37: static mcdb *mcdb_lookup(mcdb *db, int tag);
1.3 ! ohara 38: static mcdb *new_mcdb(int *src);
! 39: static void cmodb_allow(mcdb *cmo_db, int tag);
! 40: static void cmodb_deny(mcdb *cmo_db, int tag);
! 41: static void cmodb_update(mcdb *cmo_db, cmo_list* types);
1.2 ohara 42: static void mcdb_ctl(mcdb *db, int tag, int flag);
43: static void mcdb_ctl_all(mcdb *db, int flag);
1.3 ! ohara 44: static void mcdb_init(mcdb *m, int key);
! 45: void sysinfo_set(int version, char *version_string, char *sysname);
1.2 ohara 46:
1.3 ! ohara 47: static int cmodb_d[] = {
! 48: CMO_NULL,
! 49: CMO_INT32,
! 50: CMO_DATUM,
! 51: CMO_STRING,
! 52: CMO_MATHCAP,
! 53: CMO_LIST,
! 54: CMO_MONOMIAL32,
! 55: CMO_ZZ,
! 56: CMO_ZERO,
! 57: CMO_DMS_GENERIC,
! 58: CMO_RING_BY_NAME,
! 59: CMO_INDETERMINATE,
! 60: CMO_DISTRIBUTED_POLYNOMIAL,
! 61: CMO_ERROR2,
! 62: 0,
1.1 ohara 63: };
64:
1.3 ! ohara 65: static int smdb_d[] = {
! 66: SM_popCMO,
! 67: SM_popString,
! 68: SM_mathcap,
! 69: SM_pops,
! 70: SM_executeStringByLocalParser,
! 71: SM_executeFunction,
! 72: SM_setMathCap,
! 73: SM_shutdown,
! 74: SM_control_kill,
! 75: SM_control_reset_connection,
! 76: 0,
1.1 ohara 77: };
78:
1.3 ! ohara 79: __inline__
! 80: static void mcdb_init(mcdb *m, int key)
! 81: {
! 82: m->tag = key;
! 83: m->flag = MATHCAP_FLAG_ALLOW;
! 84: }
1.2 ohara 85:
1.3 ! ohara 86: static mcdb *new_mcdb(int *src)
1.2 ohara 87: {
1.3 ! ohara 88: mcdb *new;
! 89: int len=0;
! 90: int i;
! 91: while (src[len++] != 0) {
! 92: }
! 93: new = malloc(sizeof(mcdb)*len);
! 94: for(i=0; i<len; i++) {
! 95: mcdb_init(new+i, src[i]);
! 96: }
! 97: return new;
1.2 ohara 98: }
99:
1.3 ! ohara 100: mathcap *new_mathcap_d()
1.2 ohara 101: {
1.3 ! ohara 102: mathcap *new = malloc(sizeof(mathcap));
! 103: new->cmo_db = new_mcdb(cmodb_d);
! 104: new->sm_db = new_mcdb(smdb_d);
! 105: return new;
1.2 ohara 106: }
107:
108: /* 次の tag についてのキーを探す */
109: static mcdb *mcdb_lookup(mcdb *db, int tag)
110: {
111: while (db->tag != 0) {
112: if (db->tag == tag) {
113: return db;
114: }
115: db++;
116: }
117: return NULL;
118: }
119:
120: /* tag に対する送信制御 */
121: static void mcdb_ctl(mcdb *db, int tag, int flag)
122: {
123: mcdb *e = mcdb_lookup(db, tag);
124: if (e != NULL) {
125: e->flag = flag;
126: }
127: }
128:
129: /* 全データに対する送信制御 */
130: static void mcdb_ctl_all(mcdb *db, int flag)
131: {
132: while (db->tag != 0) {
133: db->flag = flag;
134: db++;
135: }
136: }
137:
138: /* 送信許可されている tag のリストを得る */
139: static cmo_list *mcdb_get_allow_all(mcdb *db)
140: {
141: cmo_list *list = new_cmo_list();
142: while (db->tag != 0) {
143: if (db->flag == MATHCAP_FLAG_ALLOW) {
144: list_append(list, (cmo *)new_cmo_int32(db->tag));
145: }
146: db++;
147: }
148: return list;
149: }
150:
1.1 ohara 151: static struct {
152: int version;
153: char *version_string;
154: char *sysname;
155: char *hosttype;
1.3 ! ohara 156: } sysinfo = {0, "NO VERSION", "NONAME", "UNKNOWN"};
1.1 ohara 157:
158: /* 次の tag をもつ cmo の送信が許可されているかを調べる */
1.3 ! ohara 159: static int cmodb_isallow_tag(mcdb *cmo_db, int tag)
1.1 ohara 160: {
1.3 ! ohara 161: while (cmo_db->tag != 0 && cmo_db->tag != tag) {
! 162: cmo_db++;
1.1 ohara 163: }
1.3 ! ohara 164: return cmo_db->flag;
1.1 ohara 165: }
166:
1.3 ! ohara 167: static int cmodb_isallow_cmo_list(mcdb *cmo_db, cmo_list *ob)
1.1 ohara 168: {
169: cell *el;
1.3 ! ohara 170: if (cmodb_isallow_tag(cmo_db, ob->tag)) {
1.1 ohara 171: el = list_first(ob);
172: while (!list_endof(ob, el)) {
1.3 ! ohara 173: if (!cmodb_isallow_cmo(cmo_db, el->cmo)) {
1.1 ohara 174: return MATHCAP_FLAG_DENY;
175: }
176: el = list_next(el);
177: }
178: return MATHCAP_FLAG_ALLOW;
179: }
180: return MATHCAP_FLAG_DENY;
181: }
182:
1.2 ohara 183: __inline__
1.3 ! ohara 184: static int cmodb_isallow_cmo_monomial32(mcdb *cmo_db, cmo_monomial32 *ob)
1.1 ohara 185: {
1.3 ! ohara 186: return cmodb_isallow_tag(cmo_db, ob->tag)
! 187: && cmodb_isallow_cmo(cmo_db, ob->coef);
1.1 ohara 188: }
189:
1.2 ohara 190: __inline__
1.3 ! ohara 191: static int cmodb_isallow_cmo_mathcap(mcdb *cmo_db, cmo_mathcap *ob)
1.1 ohara 192: {
1.3 ! ohara 193: return cmodb_isallow_tag(cmo_db, ob->tag)
! 194: && cmodb_isallow_cmo(cmo_db, ob->ob);
1.1 ohara 195: }
196:
197: /* 次の cmo の送信が許可されているかを調べる */
1.3 ! ohara 198: static int cmodb_isallow_cmo(mcdb *cmo_db, cmo *ob)
1.1 ohara 199: {
200: int tag = ob->tag;
201: switch(tag) {
202: case CMO_LIST:
203: case CMO_DISTRIBUTED_POLYNOMIAL:
1.3 ! ohara 204: return cmodb_isallow_cmo_list(cmo_db, (cmo_list *)ob);
1.1 ohara 205: case CMO_MATHCAP:
206: case CMO_ERROR2:
207: case CMO_RING_BY_NAME:
208: case CMO_INDETERMINATE:
1.3 ! ohara 209: return cmodb_isallow_cmo_mathcap(cmo_db, (cmo_mathcap *)ob);
1.1 ohara 210: case CMO_MONOMIAL32:
1.3 ! ohara 211: return cmodb_isallow_cmo_monomial32(cmo_db, (cmo_monomial32 *)ob);
1.1 ohara 212: default:
1.3 ! ohara 213: return cmodb_isallow_tag(cmo_db, tag);
1.1 ohara 214: }
215: }
216:
217: /* 次の tag をもつ cmo の送信を許可する */
1.2 ohara 218: __inline__
1.3 ! ohara 219: static void cmodb_allow(mcdb *cmo_db, int tag)
1.1 ohara 220: {
1.3 ! ohara 221: mcdb_ctl(cmo_db, tag, MATHCAP_FLAG_ALLOW);
1.1 ohara 222: }
223:
224: /* 次の tag をもつ cmo の送信を不許可にする */
1.2 ohara 225: __inline__
1.3 ! ohara 226: static void cmodb_deny(mcdb *cmo_db, int tag)
1.1 ohara 227: {
1.3 ! ohara 228: mcdb_ctl(cmo_db, tag, MATHCAP_FLAG_DENY);
1.1 ohara 229: }
230:
231: /* システム情報を得る */
1.3 ! ohara 232: static cmo_list *sysinfo_get_all()
1.1 ohara 233: {
234: cmo_list *syslist = new_cmo_list();
1.3 ! ohara 235: cmo_int32 *ver = new_cmo_int32(sysinfo.version);
! 236: cmo_string *vers = new_cmo_string(sysinfo.version_string);
! 237: cmo_string *host = new_cmo_string(sysinfo.hosttype);
! 238: cmo_string *sname = new_cmo_string(sysinfo.sysname);
! 239: return list_appendl(syslist, ver, sname, vers, host, NULL);
1.1 ohara 240: }
241:
1.3 ! ohara 242: __inline__
1.1 ohara 243: static char *new_string(char *s)
244: {
245: char *t = malloc(sizeof(s)+1);
246: strcpy(t, s);
247: return t;
248: }
249:
1.3 ! ohara 250: void sysinfo_set(int version, char *vstr, char *sysname)
1.1 ohara 251: {
252: char *host = getenv("HOSTTYPE");
1.3 ! ohara 253: sysinfo.hosttype = (host != NULL)? new_string(host): "UNKNOWN";
! 254: sysinfo.sysname = new_string(sysname);
! 255: sysinfo.version_string = new_string(vstr);
! 256: sysinfo.version = version;
! 257: }
1.1 ohara 258:
1.3 ! ohara 259: void mathcap_global_init(int ver, char *vstr, char *sysname, int cmos[], int sms[])
! 260: {
! 261: sysinfo_set(ver, vstr, sysname); /* global! */
! 262: if (cmos == NULL) {
! 263: cmos = cmodb_d;
! 264: }
! 265: if (sms == NULL) {
! 266: sms = smdb_d;
! 267: }
! 268: /* ... */
1.1 ohara 269: }
270:
271: /* データベースから cmo_mathcap を生成する */
1.3 ! ohara 272: cmo_mathcap* mathcap_get(mathcap *this)
1.1 ohara 273: {
274: cmo_list *mc = new_cmo_list();
275: cmo_list *l3 = new_cmo_list();
1.3 ! ohara 276: list_append(l3, list_appendl(new_cmo_list(),
! 277: new_cmo_int32(OX_DATA),
! 278: cmodb_get_allow_all(this), NULL));
! 279: list_appendl(mc, (cmo *)sysinfo_get_all(),
! 280: (cmo *)smdb_get_allow_all(this), (cmo *)l3, NULL);
1.1 ohara 281: return new_cmo_mathcap((cmo *)mc);
282: }
283:
1.3 ! ohara 284: static void cmodb_update(mcdb *cmo_db, cmo_list* types)
1.1 ohara 285: {
286: cell *el = list_first(types);
287: cmo_int32 *ob;
288: while(!list_endof(types, el)) {
289: ob = (cmo_int32 *)el->cmo;
290: if (ob->tag == CMO_INT32) {
1.3 ! ohara 291: cmodb_allow(cmo_db, ob->i);
1.1 ohara 292: }
293: el = list_next(el);
294: }
295: }
296:
297: /* cmo_mathcap が妥当な構造を持つかを調べる. (未実装) */
298: int cmo_mathcap_isvalid(cmo_mathcap *mc)
299: {
1.3 ! ohara 300: /* print_cmo(mc); */
1.1 ohara 301: return 1;
302: }
303:
304: /* ( ..., ( type, (...) ), (cmo_int32, (...) ), ... ) */
305: /* ^^^^^ Here! */
306: static cmo_list *get_messagetypes(cmo_list *ob, int type)
307: {
308: cmo_list *c;
309: cell *el;
310:
311: for (el = list_first(ob); !list_endof(ob, el); el = list_next(el)) {
312: c = (cmo_list *)el->cmo;
313: if (((cmo_int32 *)list_nth(c, 0))->i == type) {
314: return (cmo_list *)list_nth(c, 1);
315: }
316: }
317: return NULL;
318: }
319:
320: /* cmo_mathcap->ob = ( (...), (...), ( ( cmo_int32, (...) ), ...), ...) */
321: /* ^^^^^ Here! */
1.2 ohara 322: __inline__
1.1 ohara 323: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc)
324: {
325: cmo_list *ob = (cmo_list *)list_nth((cmo_list *)mc->ob, 2);
326: return get_messagetypes(ob, OX_DATA);
327: }
328:
329: /* 受信した mathcap データを反映させる */
1.3 ! ohara 330: /* this == NULL のとき、はじめて mathcap* オブジェクトをせいせいする */
! 331: mathcap *mathcap_update(mathcap *this, cmo_mathcap *mc)
1.1 ohara 332: {
333: cmo_list *types;
334: if (cmo_mathcap_isvalid(mc)) {
335: types = cmo_mathcap_get_cmotypes(mc);
336: if (types != NULL) {
1.3 ! ohara 337: cmodb_deny_all(this->cmo_db); /* すべての cmo の送信を禁止 */
! 338: cmodb_update(this->cmo_db, types);
1.1 ohara 339: }
340: }
1.3 ! ohara 341: return this;
1.1 ohara 342: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>