[BACK]Return to mathcap.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.14

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>