Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.13
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.13 ! takayama 2: /* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.12 2005/07/26 12:52:05 ohara Exp $ */
1.1 ohara 3:
4: /* This module includes functions for handling mathcap databases. */
5:
6: #include <stdlib.h>
1.5 ohara 7: #include <string.h>
1.1 ohara 8: #include "ox_toolkit.h"
9:
1.13 ! takayama 10: #define MATHCAP_FLAG_DENY 0
! 11: #define MATHCAP_FLAG_ALLOW 1
1.3 ohara 12:
1.13 ! takayama 13: static void table_init(table *m, int key);
! 14: static table *new_table(int *src);
! 15: static table *table_lookup(table *tbl, int tag);
! 16: static void table_ctl(table *tbl, int tag, int flag);
! 17: static void table_ctl_all(table *tbl, int flag);
! 18: static cmo_list *table_get_all(table *tbl);
! 19: static void table_allow(table *tbl, int tag);
! 20: static void table_deny(table *tbl, int tag);
! 21: static void table_update(table *cmotbl, cmo_list* types);
! 22: static int table_allowQ_tag(table *tbl, int tag);
! 23: static int table_allowQ_cmo_list(table *cmotbl, cmo_list *ob);
! 24: static int table_allowQ_cmo_monomial32(table *cmotbl, cmo_monomial32 *ob);
! 25: static int table_allowQ_cmo_mathcap(table *cmotbl, cmo_mathcap *ob);
! 26: static int table_allowQ_cmo(table *cmotbl, cmo *ob);
! 27: static cmo_list *sysinfo_get();
! 28: static char *new_string(char *s);
! 29: static int *new_int_array(int *array);
! 30: static cmo_list *get_messagetypes(cmo_list *ob, int type);
! 31: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc);
! 32:
! 33: static int cmotbl_a[] = {
1.3 ohara 34: CMO_NULL,
35: CMO_INT32,
1.13 ! takayama 36: CMO_DATUM,
1.3 ohara 37: CMO_STRING,
38: CMO_MATHCAP,
39: CMO_LIST,
40: CMO_MONOMIAL32,
41: CMO_ZZ,
1.11 ohara 42: CMO_QQ,
1.13 ! takayama 43: CMO_IEEE_DOUBLE_FLOAT,
1.3 ohara 44: CMO_ZERO,
45: CMO_DMS_GENERIC,
46: CMO_RING_BY_NAME,
1.13 ! takayama 47: CMO_INDETERMINATE,
1.12 ohara 48: CMO_DISTRIBUTED_POLYNOMIAL,
1.3 ohara 49: CMO_ERROR2,
1.13 ! takayama 50: 0,
! 51: };
! 52:
! 53: static int smtbl_a[] = {
! 54: SM_popCMO,
! 55: SM_popString,
! 56: SM_mathcap,
! 57: SM_pops,
! 58: SM_executeStringByLocalParser,
! 59: SM_executeFunction,
! 60: SM_setMathCap,
! 61: SM_shutdown,
! 62: SM_control_kill,
! 63: SM_control_reset_connection,
! 64: SM_control_spawn_server,
! 65: SM_control_terminate_server,
! 66: 0,
! 67: };
1.1 ohara 68:
1.5 ohara 69: static struct {
1.13 ! takayama 70: int version;
! 71: char *version_string;
1.5 ohara 72: char *sysname;
73: char *hosttype;
1.13 ! takayama 74: int *cmo_tags;
! 75: int *sm_cmds;
! 76: } sysinfo = {0, "NO VERSION", "NONAME", "UNKNOWN", cmotbl_a, smtbl_a};
1.5 ohara 77:
1.13 ! takayama 78: __inline__
! 79: static void table_init(table *m, int key)
! 80: {
! 81: m->tag = key;
! 82: m->flag = MATHCAP_FLAG_ALLOW;
! 83: }
1.2 ohara 84:
1.13 ! takayama 85: static table *new_table(int *src)
! 86: {
! 87: table *new;
! 88: int len=0;
! 89: int i;
! 90: while (src[len++] != 0) {
! 91: }
! 92: new = MALLOC(sizeof(table)*len);
! 93: for(i=0; i<len; i++) {
! 94: table_init(new+i, src[i]);
! 95: }
! 96: return new;
! 97: }
1.2 ohara 98:
1.13 ! takayama 99: /* looking for an item of the tag */
! 100: static table *table_lookup(table *tbl, int tag)
1.2 ohara 101: {
1.13 ! takayama 102: while (tbl->tag != 0) {
! 103: if (tbl->tag == tag) {
! 104: return tbl;
! 105: }
! 106: tbl++;
1.2 ohara 107: }
108: return NULL;
109: }
110:
1.13 ! takayama 111: /* controller about a cmo identified by the tag */
! 112: static void table_ctl(table *tbl, int tag, int flag)
1.2 ohara 113: {
1.13 ! takayama 114: table *e = table_lookup(tbl, tag);
! 115: if (e != NULL) {
! 116: e->flag = flag;
1.2 ohara 117: }
118: }
119:
1.13 ! takayama 120: /* controller about all CMObjects */
! 121: static void table_ctl_all(table *tbl, int flag)
1.2 ohara 122: {
1.13 ! takayama 123: while (tbl->tag != 0) {
! 124: tbl->flag = flag;
! 125: tbl++;
! 126: }
1.2 ohara 127: }
128:
1.13 ! takayama 129: /* getting the list of tags of all allowed objects */
! 130: static cmo_list *table_get_all(table *tbl)
1.2 ohara 131: {
1.13 ! takayama 132: cmo_list *list = new_cmo_list();
! 133: while (tbl->tag != 0) {
! 134: if (tbl->flag == MATHCAP_FLAG_ALLOW) {
! 135: list_append(list, (cmo *)new_cmo_int32(tbl->tag));
! 136: }
! 137: tbl++;
! 138: }
! 139: return list;
1.2 ohara 140: }
141:
1.13 ! takayama 142: /* giving a permssion to send objects identified by the tag. */
! 143: __inline__
! 144: static void table_allow(table *tbl, int tag)
1.5 ohara 145: {
1.13 ! takayama 146: table_ctl(tbl, tag, MATHCAP_FLAG_ALLOW);
1.5 ohara 147: }
148:
1.13 ! takayama 149: /* taking a permssion to send objects identified by the tag. */
! 150: __inline__
! 151: static void table_deny(table *tbl, int tag)
1.5 ohara 152: {
1.13 ! takayama 153: table_ctl(tbl, tag, MATHCAP_FLAG_DENY);
1.5 ohara 154: }
155:
1.13 ! takayama 156: static void table_update(table *cmotbl, cmo_list* types)
1.5 ohara 157: {
1.13 ! takayama 158: cell *el = list_first(types);
! 159: cmo_int32 *ob;
! 160: while(!list_endof(types, el)) {
! 161: ob = (cmo_int32 *)el->cmo;
! 162: if (ob->tag == CMO_INT32) {
! 163: table_allow(cmotbl, ob->i);
! 164: }
! 165: el = list_next(el);
! 166: }
1.5 ohara 167: }
1.1 ohara 168:
1.13 ! takayama 169: /* getting a permission to send objects identified by the tag. */
! 170: static int table_allowQ_tag(table *tbl, int tag)
1.1 ohara 171: {
1.13 ! takayama 172: while (tbl->tag != 0 && tbl->tag != tag) {
! 173: tbl++;
! 174: }
! 175: return tbl->flag;
! 176: }
! 177:
! 178: static int table_allowQ_cmo_list(table *cmotbl, cmo_list *ob)
! 179: {
! 180: cell *el;
! 181: if (table_allowQ_tag(cmotbl, ob->tag)) {
! 182: el = list_first(ob);
! 183: while (!list_endof(ob, el)) {
! 184: if (!table_allowQ_cmo(cmotbl, el->cmo)) {
! 185: return MATHCAP_FLAG_DENY;
1.1 ohara 186: }
1.13 ! takayama 187: el = list_next(el);
1.1 ohara 188: }
1.13 ! takayama 189: return MATHCAP_FLAG_ALLOW;
! 190: }
! 191: return MATHCAP_FLAG_DENY;
! 192: }
! 193:
! 194: __inline__
! 195: static int table_allowQ_cmo_monomial32(table *cmotbl, cmo_monomial32 *ob)
! 196: {
! 197: return table_allowQ_tag(cmotbl, ob->tag)
! 198: && table_allowQ_cmo(cmotbl, ob->coef);
! 199: }
! 200:
! 201: __inline__
! 202: static int table_allowQ_cmo_mathcap(table *cmotbl, cmo_mathcap *ob)
! 203: {
! 204: return table_allowQ_tag(cmotbl, ob->tag)
! 205: && table_allowQ_cmo(cmotbl, ob->ob);
! 206: }
! 207:
! 208: /* getting a permission to send the following object. */
! 209: static int table_allowQ_cmo(table *cmotbl, cmo *ob)
! 210: {
! 211: int tag = ob->tag;
! 212: switch(tag) {
! 213: case CMO_LIST:
! 214: case CMO_DISTRIBUTED_POLYNOMIAL:
! 215: return table_allowQ_cmo_list(cmotbl, (cmo_list *)ob);
! 216: case CMO_MATHCAP:
! 217: case CMO_ERROR2:
! 218: case CMO_RING_BY_NAME:
! 219: case CMO_INDETERMINATE:
! 220: return table_allowQ_cmo_mathcap(cmotbl, (cmo_mathcap *)ob);
! 221: case CMO_MONOMIAL32:
! 222: return table_allowQ_cmo_monomial32(cmotbl, (cmo_monomial32 *)ob);
! 223: default:
! 224: return table_allowQ_tag(cmotbl, tag);
1.1 ohara 225: }
226: }
227:
1.13 ! takayama 228: /* getting the System Information */
! 229: static cmo_list *sysinfo_get()
! 230: {
! 231: cmo_list *syslist = new_cmo_list();
! 232: cmo_int32 *ver = new_cmo_int32(sysinfo.version);
! 233: cmo_string *vers = new_cmo_string(sysinfo.version_string);
! 234: cmo_string *host = new_cmo_string(sysinfo.hosttype);
! 235: cmo_string *sname = new_cmo_string(sysinfo.sysname);
! 236: return list_appendl(syslist, ver, sname, vers, host, NULL);
! 237: }
! 238:
! 239: static char *new_string(char *s)
! 240: {
! 241: char *t = MALLOC(strlen(s)+1);
! 242: strcpy(t, s);
! 243: return t;
! 244: }
! 245:
! 246: static int *new_int_array(int *array)
1.1 ohara 247: {
1.13 ! takayama 248: int *new_array;
! 249: int length = 0;
! 250: while(array[length++] != 0)
! 251: ;
! 252: new_array = MALLOC(sizeof(int)*length);
! 253: return memcpy(new_array, array, sizeof(int)*length);
! 254: }
! 255:
! 256: void mathcap_init(int ver, char *vstr, char *sysname, int cmos[], int sms[])
! 257: {
! 258: char *host = getenv("HOSTTYPE");
! 259: sysinfo.hosttype = (host != NULL)? new_string(host): "UNKNOWN";
! 260: sysinfo.sysname = new_string(sysname);
! 261: sysinfo.version_string = new_string(vstr);
! 262: sysinfo.version = ver;
! 263: if (cmos != NULL) {
! 264: sysinfo.cmo_tags = new_int_array(cmos);
! 265: }
! 266: if (sms != NULL) {
! 267: sysinfo.sm_cmds = new_int_array(sms);
! 268: }
1.5 ohara 269: }
270:
1.13 ! takayama 271: mathcap *new_mathcap()
1.1 ohara 272: {
1.13 ! takayama 273: mathcap *new = MALLOC(sizeof(mathcap));
! 274: new->cmotbl = new_table(sysinfo.cmo_tags);
! 275: new->smtbl = new_table(sysinfo.sm_cmds);
! 276: return new;
1.3 ohara 277: }
1.13 ! takayama 278:
! 279: /* generating a cmo_mathcap by a local database. */
1.3 ohara 280: cmo_mathcap* mathcap_get(mathcap *this)
1.1 ohara 281: {
1.13 ! takayama 282: cmo_list *mc = new_cmo_list();
! 283: cmo_list *l3 = new_cmo_list();
! 284: list_append(l3, (cmo *)list_appendl(new_cmo_list(),
! 285: new_cmo_int32(OX_DATA),
! 286: table_get_all(this->cmotbl), NULL));
! 287: list_appendl(mc, (cmo *)sysinfo_get(),
! 288: (cmo *)table_get_all(this->smtbl), (cmo *)l3, NULL);
! 289: return new_cmo_mathcap((cmo *)mc);
! 290: }
! 291:
! 292: /* ( ..., ( type, (...) ), (cmo_int32, (...) ), ... ) */
! 293: /* ^^^^^ Here! */
! 294: static cmo_list *get_messagetypes(cmo_list *ob, int type)
! 295: {
! 296: cmo_list *c;
! 297: cell *el;
! 298:
! 299: for (el = list_first(ob); !list_endof(ob, el); el = list_next(el)) {
! 300: c = (cmo_list *)el->cmo;
! 301: if (((cmo_int32 *)list_nth(c, 0))->i == type) {
! 302: return (cmo_list *)list_nth(c, 1);
! 303: }
! 304: }
! 305: return NULL;
1.1 ohara 306: }
1.13 ! takayama 307:
! 308: /* cmo_mathcap->ob = ( (...), (...), ( ( cmo_int32, (...) ), ...), ...) */
! 309: /* ^^^^^ Here! */
! 310: __inline__
! 311: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc)
! 312: {
! 313: cmo_list *ob = (cmo_list *)list_nth((cmo_list *)mc->ob, 2);
! 314: return get_messagetypes(ob, OX_DATA);
! 315: }
! 316:
! 317: /* The mathcap_update integrates received cmo_mathcap into the mathcap
! 318: database. If this == NULL, then an instance of mathcap is generated. */
! 319: mathcap *mathcap_update(mathcap *this, cmo_mathcap *mc)
! 320: {
! 321: cmo_list *types;
! 322: types = cmo_mathcap_get_cmotypes(mc);
! 323: if (types != NULL) {
! 324: table_ctl_all(this->cmotbl, MATHCAP_FLAG_DENY);
! 325: table_update(this->cmotbl, types);
! 326: }
! 327:
! 328: return this;
! 329: }
! 330:
! 331: int mathcap_allowQ_cmo(mathcap *this, cmo *ob)
1.5 ohara 332: {
1.13 ! takayama 333: return table_allowQ_cmo(this->cmotbl, ob);
1.1 ohara 334: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>